void AI::obedientZombie(int index, std::deque<int> myOrders) { int x, y, humanIndex, wallIndex; Zombie* me = &zombies[index]; switch (myOrders.front()) { case 0: x = nextX(me->x(), me->y(), me->facing()); y = nextY(me->x(), me->y(), me->facing()); humanIndex = getHuman(x, y); if (humanIndex > -1) { myOrders.clear(); me->attack(humans[humanIndex]); } else me->move(); break; case 1: me->turn(-1); break; case 2: me->turn(1); break; } if (myOrders.size() > 0) myOrders.pop_front(); if (myOrders.size() > 0) orders[me->id()] = myOrders; else orders.erase(me->id()); }
bool GradientAscent1D::singleStep() { float newX = nextX(); std::tuple<float, float> funcOutput = (*(config->f))(newX); previousx = currentx; previousdx = currentdx; currentx = newX; currentdx = std::get<1>(funcOutput); currentVal = std::get<0>(funcOutput); // Decrease temperature when derivative flips sign if (sign(previousdx) == -1 * sign(currentdx)) { temperature *= config->temperatureDescent; } iterationCount++; return continueExecution(); }
char Agent::stuck() { int x = head().X; int y = head().Y; string moves; if(Team()==1) moves="uldr"; else moves="drul"; for(int i=0;i<3;i++) { Point nextp; nextp.X=nextX(x,moves[i]); nextp.Y=nextY(y,moves[i]); //cout << "distans for stuck " << getdistance(head(),nextp) << endl; if(isEmpty(nextp.X,nextp.Y)) //&& getdistance(head(),nextp)>1 ) { /*cout << "raft too if!!" << endl;*/ cout << "stuck "<< moves[i] << endl; return moves[i]; } } return moves[3]; }
//human i runs around randomly and attacks things if they get in the way void AI::scaredHuman(int i) { int attacks = 1; int moves; int tries; int dir; int x, y; int oldX, oldY; int zombieIndex, crateIndex; tries = 0; moves = humans[i].moves(); oldX = humans[i].x(); oldY = humans[i].y(); while ((moves > 0 || attacks > 0) && tries < 12) { dir = rand() % 6; x = nextX(oldX, oldY, dir); y = nextY(oldX, oldY, dir); zombieIndex = getZombie(x, y); crateIndex = getCrate(x, y); if (crateIndex > -1) { humans[i].grab(crates[crateIndex]); } if (zombieIndex == -1 && getWall(x, y) == -1 && moves > 0) { humans[i].move(x, y); oldX = x; oldY = y; moves -= 1; } else if (zombieIndex > -1 && attacks > 0) { humans[i].attack(zombies[zombieIndex]); attacks -= 1; } tries += 1; } }
//Attacks nearby zombies, then builds walls around himself. //Does not move. void AI::builderHuman(int i) { int attacks = 1; int tries = 0; int dir; int x, y; int zombieIndex, crateIndex, humanIndex, wallIndex; int moves = humans[i].moves(); while ((moves > 0 || attacks > 0) && tries < 12) { dir = rand() % 6; x = nextX(humans[i].x(), humans[i].y(), dir); y = nextY(humans[i].x(), humans[i].y(), dir); zombieIndex = getZombie(x, y); crateIndex = getCrate(x, y); humanIndex = getHuman(x, y); wallIndex = getHuman(x, y); if (crateIndex > -1) { humans[i].grab(crates[crateIndex]); } if (zombieIndex == -1 && humanIndex == -1 && moves > 0) { humans[i].build(x, y); moves -= 1; } else if (zombieIndex > -1 && attacks > 0) { humans[i].attack(zombies[zombieIndex]); attacks -= 1; } tries += 1; } }
//Zombie i is given orders based soley on its smell and the object // directly in front of them. void AI::blindZombie(int i) { int x, y, wallIndex, humanIndex, zombieIndex; bool allowEat = (int(getZombieCap()) == zombies.size()); if (zombies[i].smell() == 0) { zombies[i].turn((rand()%2) * 2 - 1); } else { x = nextX(zombies[i].x(), zombies[i].y(), zombies[i].facing()); y = nextY(zombies[i].x(), zombies[i].y(), zombies[i].facing()); wallIndex = getWall(x, y); humanIndex = getHuman(x, y); zombieIndex = getZombie(x, y); if (humanIndex > -1) { zombies[i].attack(humans[humanIndex]); } else if (wallIndex > -1) { zombies[i].attack(walls[wallIndex]); } else if (zombieIndex > -1 && allowEat) { zombies[i].eat(zombies[zombieIndex]); eaten += 1; } else { zombies[i].move(); } } }
void rayCast(Vector2i src, Vector2i dst, RAY_CALLBACK callback, void *data) { if (!callback(src, 0, data) || src == dst) // Start at src. { return; // Callback gave up after the first point, or there are no other points. } Vector2i srcM = map_coord(src); Vector2i dstM = map_coord(dst); Vector2i step, tile, cur, end; initSteps(srcM.x, dstM.x, tile.x, step.x, cur.x, end.x); initSteps(srcM.y, dstM.y, tile.y, step.y, cur.y, end.y); Vector2i prev(0, 0); // Dummy initialisation. bool first = true; Vector2i nextX(0, 0), nextY(0, 0); // Dummy initialisations. bool canX = tryStep(tile.x, step.x, cur.x, end.x, nextX.x, nextX.y, src.x, src.y, dst.x, dst.y); bool canY = tryStep(tile.y, step.y, cur.y, end.y, nextY.y, nextY.x, src.y, src.x, dst.y, dst.x); while (canX || canY) { int32_t xDist = abs(nextX.x - src.x) + abs(nextX.y - src.y); int32_t yDist = abs(nextY.x - src.x) + abs(nextY.y - src.y); Vector2i sel; Vector2i selTile; if (canX && (!canY || xDist < yDist)) // The line crosses a vertical grid line next. { sel = nextX; selTile = tile; canX = tryStep(tile.x, step.x, cur.x, end.x, nextX.x, nextX.y, src.x, src.y, dst.x, dst.y); } else // The line crosses a horizontal grid line next. { assert(canY); sel = nextY; selTile = tile; canY = tryStep(tile.y, step.y, cur.y, end.y, nextY.y, nextY.x, src.y, src.x, dst.y, dst.x); } if (!first) { // Find midpoint. Vector2i avg = (prev + sel) / 2; // But make sure it's on the right tile, since it could be off-by-one if the line passes exactly through a grid intersection. avg.x = std::min(std::max(avg.x, world_coord(selTile.x)), world_coord(selTile.x + 1) - 1); avg.y = std::min(std::max(avg.y, world_coord(selTile.y)), world_coord(selTile.y + 1) - 1); if (!worldOnMap(avg) || !callback(avg, iHypot(avg), data)) { return; // Callback doesn't want any more points, or we reached the edge of the map, so return. } } prev = sel; first = false; } // Include the endpoint. if (!worldOnMap(dst)) { return; // Stop, since reached the edge of the map. } callback(dst, iHypot(dst), data); }
char Agent::percept() { int x = head().X; int y = head().Y; //cout << "head : " << x << " , " << y << endl; string moves; if(Team()==1) moves="drul"; //harkat da jahate padsaat gard else moves="uldr"; // harkat da jahate saat gard for (int i =0;i<map->MapSize.X;i++) for (int j =0;j<map->MapSize.Y;j++) mark[i][j]=false; //hameye mark haro false mikone! hamaro pak mikone for (int i =0;i<map->MapSize.X;i++) for (int j =0;j<map->MapSize.Y;j++) enemy_mark[i][j]=false; //hameye enemy_mark haro false mikone! hamaro pak mikone for (int i =0;i<4;i++) for (int j =0;j<4;j++) score[i][j]=0; //hameye score haro 0 mikone for (int move=0;move<4;move++) // emtiaz dehi be har harekat { Point here_now; here_now.X=nextX(x,moves[move]); here_now.Y=nextY(y,moves[move]); //mark[here_now.X][here_now.Y]=true;//jaii ke mire! if (isEmpty(here_now.X,here_now.Y)) // age por bood ke hichi! { //cout << "x and y now :" << here_now.X << " , "<<here_now.Y; score[move][1]=painting(here_now,'m')+1; //chand ta khoone mitoone bere //cout << "painting of " << moves[move] <<" is : "<< score[move][1] << endl; Point enemy_head=ehead(); //sare yaroo ro mirize to enemy_head int enemy_score[4]={0}; // emtiaz haro hame 0 mokine! enemy_mark[here_now.X][here_now.Y]=true; //injaii ro ke alan hast mark mikone! for (int enemy_move=0;enemy_move<4;enemy_move++) //baraye har harekat harif mikhad emtiaz hesab bokone { Point enemy_pos; enemy_pos.X=nextX(enemy_head.X,moves[enemy_move]); enemy_pos.Y=nextY(enemy_head.Y,moves[enemy_move]); enemy_mark[enemy_pos.X][enemy_pos.Y]=true; if (isEmpty(enemy_pos.X,enemy_pos.Y)) { enemy_score[enemy_move]=painting(enemy_pos,'e'); //cout << "\t painting enemy " << moves[enemy_move] << " is : "<< enemy_score[enemy_move] << endl; //cout << "\t"<< "enemy pos: "<< enemy_pos.X << " , " << enemy_pos.Y << endl; for (int i =0;i<map->MapSize.Y;i++) //enemy mark ro 0 mikone for (int j =0;j<map->MapSize.X;j++) enemy_mark[i][j]=false; //hameye enemy_mark haro false mikone! hamaro pak mikone } } int minus=findmin(enemy_score,4,0); for (int i=0;i<4;i++) { if (enemy_score[i]!=0) enemy_score[i]=enemy_score[i]-minus; score[move][2]+=enemy_score[i]; //cout << "\t now enemy score of " << moves[i] <<" is " << enemy_score[i] <<endl; } score[move][3]=getdistance(here_now,ehead()); if (score[move][3] <= 1) {/*cout << "nazdiiiiiiiiiiik!"<<endl;*/ stuck(); } for (int i =0;i<map->MapSize.Y;i++) for (int j =0;j<map->MapSize.X;j++) mark[i][j]=false; //hameye mark haro false mikone! } } int min=10000; for (int i=0;i<4;i++) if (score[i][1]<min && score[i][1]!=0) min = score[i][1]; for (int i=0;i<4;i++) if (score[i][1]!=0) { score[i][1]=score[i][1]-min; //cout << "now the painting of " <<moves[i] << " is : " << score[i][1] <<"and the min is " << min <<endl; } min=10000; int min_id; for (int i=0;i<4;i++) if (score[i][3]<min && score[i][3]!=0) { min = score[i][3]; min_id=i; } for (int i=0;i<4;i++) { if (i!=min_id) score[i][3]=0; else score[i][3]=2; /////////////////////////////////////////////////////////////////////////////////////////////////// } //cout << "now the closest is : "<< moves[min_id] << " Point : " << score[min_id][3] << endl; //jamm score ha for (int move=0;move<4;move++) { score[move][0]+=score[move][1]+score[move][3]-score[move][2]; } int max_char_id=0; int max_score=0; for (int move=0;move<4;move++) { if (max_score<score[move][0]) { //cout <<"score move "<< moves[move] <<" is bigger than previouse :"<< score[move][0] <<endl; max_score = score[move][0]; max_char_id = move; } } cout << "our algorithm output: "<< moves[max_char_id] <<endl << "\t and the score is " << max_score<<endl ; if (max_score!=0) return moves[max_char_id]; return stuck(); //return stuck(); }
//Zombie i is given orders based on an A* search to the nearest human // where nearest is defined by the zombieDistance function //Smart zombies will not break down walls, and will do nothing if // no path is available to the nearest human. void AI::smartZombie(int index) { const int FORGET = 5; //forget the last x of the generated path. const int REMEMBER = 5; //go no more than x turns without recalulating path int forgotten = 0; Zombie* me = &zombies[index]; Human* target = NULL; int bestDis = 9999999; int nextDis; int x, y, humanIndex, wallIndex; std::deque<int> path; std::cout << "smart" << std::endl; for (int i = 0; i < humans.size(); i++) { nextDis = zombieDistance(me->x(), me->y(), humans[i].x(), humans[i].y(), me->facing()); if (nextDis < bestDis) { bestDis = nextDis; target = &humans[i]; } } if (target != NULL) { path = findZombiePath(me->x(), me->y(), target->x(), target->y(), me->facing()); std::cout << "(" << me->x() << "," << me->y() << "," << target->x() << "," << target->y() << "," << me->facing() << ") = "; for (int k = 0; k < path.size(); k++) { std::cout << path[k] << " "; } if (path.size() == 0) { std::cout << "No action"; } std::cout << std::endl; if (path.size() > 1) { path.pop_front(); switch (path.front()) { case 0: x = nextX(me->x(), me->y(), me->facing()); y = nextY(me->x(), me->y(), me->facing()); humanIndex = getHuman(x, y); wallIndex = getWall(x, y); if (humanIndex > -1) me->attack(humans[humanIndex]); else if (wallIndex > -1) me->attack(walls[wallIndex]); else me->move(); break; case 1: me->turn(-1); break; case 2: me->turn(1); break; } /* while (path.size() > 0 && forgotten < FORGET) { path.pop_back(); forgotten += 1; } while (path.size() > REMEMBER) { path.pop_back(); } if (path.size() > 0) orders[me->id()] = path; */ } } std::cout << "end" << std::endl; }