void AI::moveTowardsTarget(Bot& actor, Unit& target) { int dist = INT_MAX,startDist = 0; while(actor.steps()>0 && dist > actor.range() && dist != startDist) { //startDist = distance(actor.x(),actor.y(),actor.size(), target.x(),target.y(),target.size()); startDist = distance(actor.x(),actor.y(),actor.size(), target.x(),target.y(),1)+1; cout<<"Distance to target: "<<startDist<<endl; dist = startDist; int dir = 0; // find the best, non blocked direction for(unsigned int d=0;d<DIR_SIZE;d++) { int x=actor.x()+xMod[d]; int y=actor.y()+yMod[d]; if(x>=0 && x<boardX() && y>=0 && y<boardY()) { //int tempDist = distance(x,y,actor.size(), target.x(),target.y(),target.size()); int tempDist = distance(x,y,actor.size(), target.x(),target.y(),1); if(tempDist < dist) { bool blocked = false; // check if blocked TODO add other blocking calls for(unsigned int b=0;b<bots.size() && !blocked;b++) { //if(bots[b].x()==x && bots[b].y()==y && bots[b].partOf()==0) if(bots[b].partOf()==0 && distance(bots[b].x(),bots[b].y(),bots[b].size(),x,y,actor.size())==0 && bots[b].id() != actor.id()) { blocked=true; } } if(!blocked) { dir=d; dist = tempDist; } } } } if(dist != startDist) { cout<<"Big guy moving: "<<direction[dir]<<endl; actor.move(direction[dir]); } else { cout<<"Nothing worth going to!"<<endl; } } }
bool AI::inRange(Bot& actor, Unit& target) { // return distance(actor.x(),actor.y(),actor.size(),target.x(),target.y(),target.size()) <= actor.range(); return distance(actor.x(),actor.y(),actor.size(),target.x(),target.y(),1) <= actor.range(); }