void BehaviorTankAI::doDefend() { tank_t me = getMyTank(); Point myLocation(me.pos[0], me.pos[1]); // if we're close, start the defense behavior if(SearchTools::distance(myLocation, pointToDefend) < 15 && currentBehavior->getType() != DEFENDBEHAVIOR) { Behavior* newBehavior = new DefendBehavior(*currentBehavior); delete currentBehavior; currentBehavior = newBehavior; } }
//returns a vector of only valid moves vector<location> AI::validMoves(int x, int y) { bool valid[] = {true,true,true,true,true,true}; int stillValid=6; for(unsigned int o=0;o<6;o++) { if(!inBounds(x+offset[o].x,y+offset[o].y)) { valid[o] = false; stillValid--; } for(unsigned int w=0;valid[o] && w<walls.size();w++) { if(walls[w].x()==x+offset[o].x && walls[w].y()==y+offset[o].y) { valid[o]=false; stillValid--; } } for(unsigned int z=0;valid[o] && z<zombies.size();z++) { if(zombies[z].x()==x+offset[o].x && zombies[z].y()==y+offset[o].y) { valid[o]=false; stillValid--; } } for(unsigned int h=0;valid[o] && h<humans.size();h++) { if(humans[h].x()==x+offset[o].x && humans[h].y()==y+offset[o].y) { valid[o]=false; stillValid--; } } } location myLocation(x,y); vector<location> returnVector(stillValid); for(unsigned int o=0,i=0;o<6;o++) { if(valid[o]) { returnVector[i] = myLocation+offset[o]; i++; } } return returnVector; }