/*! \author Luxor */ void cChar::follow( pChar pc ) { if ( isFrozen() ) { if ( hasPath() ) safedelete( path ); return; } if ( dist( getBody()->getPosition(), pc->getBody()->getPosition() ) <= 1.0 ) { // Target reached if ( hasPath() ) safedelete( path ); facexy( pc->getBody()->getPosition().x, pc->getBody()->getPosition().y ); return; } if ( !hasPath() || path->targetReached() ) { // We haven't got a right path, call the pathfinding. pathFind( pc->getBody()->getPosition(), true ); walkNextStep(); return; } double distance = dist( path->getFinalPos(), pc->getBody()->getPosition() ); if ( distance <= 3.0 ) { // Path finalPos is pretty near... let's not overhead the processor walkNextStep(); } else { // Path finalPos is too far, call the pathfinding. pathFind( pc->getBody()->getPosition(), true ); walkNextStep(); } }
/* * Parses the input into commands and arguments using whitespace * Then calls the associated function or error * Maximum number of arguments is 5 */ void parse(char *in) { int argNum = 1; char *arg, *argArr[6] = {NULL, NULL, NULL, NULL, NULL, NULL}; arg = (char *) malloc(sizeof(char) * (strlen(in) + 1)); /* Parse command */ arg = strtok(in, " \r\n\t"); argArr[0] = arg; if (argArr[0] == NULL) { fprintf(stderr, "ERROR: unknown command\n"); return; } /* Parse arguments */ arg = strtok(NULL, " \r\n\t"); while (arg != NULL && argNum < 6) { if (strcmp(arg, " ") && strcmp(arg, "\r") && strcmp(arg, "\n")) { argArr[argNum] = arg; argNum++; } arg = strtok(NULL, " \r\n\t"); } /* Execute according to command */ if (strcmp(argArr[0], "exit") == 0) { printf("EXITING SHELL...\n"); exit(EXIT_SUCCESS); } else if (strcmp(argArr[0], "cd") == 0) { cmd_cd(argArr[1]); } else if (strcmp(argArr[0], "pushd") == 0) { char *oldwd = (char *) malloc(sizeof(char) * strlen(cwd)); strcpy(oldwd, cwd); if (cmd_cd(argArr[1]) == 0) dirStack = stackPush(dirStack, oldwd); } else if (strcmp(argArr[0], "popd") == 0) { if (dirStack != NULL) { cmd_cd(dirStack->str); dirStack = stackPop(dirStack); } else { fprintf(stderr, "ERROR: stack is empty\n"); } } else if (strcmp(argArr[0], "dirs") == 0) { stackPrint(dirStack); } else if (strcmp(argArr[0], "path") == 0) { if (argArr[1] == NULL && argArr[1] == NULL) listPrint(pathStack); else if (strcmp(argArr[1], "+") == 0) pathFind(1, argArr[2]); else if (strcmp(argArr[1], "-") == 0) pathFind(-1, argArr[2]); else fprintf(stderr, "ERROR: bad flag: %s\n", argArr[1]); } else { cmd_exec(argArr, argNum); } };
AStarPath::AStarPath(Unit* thisUnit, int startX, int startY, int destX, int destY,GameState* gameState, int distance, bool onBuilding) { this->thisUnit = thisUnit; this->startX = startX; this->startY = startY; this->destX = destX; this->destY = destY; this->distance = distance; this->onBuilding = onBuilding; this->gameState = gameState; for (unsigned int i = 0; i < 880; i++) { for(unsigned int j = 0; j < 880; j++) { map[i][j] = NULL; } } pathFind(); }
static PyObject * path_get_path(PathPathObject *self, PyObject *args) { int startx, starty, starthead, destx, desty; int *result; if (!PyArg_ParseTuple(args, "(ii)i(ii)", &startx, &starty, &starthead, &destx, &desty)) return NULL; Py_BEGIN_ALLOW_THREADS result = pathFind(self->path,startx,starty,starthead,destx,desty); Py_END_ALLOW_THREADS if (result == NULL) Py_RETURN_NONE; PyObject *list = PyList_New(0); int i; for (i = 2; i < (result[0]*2+2); i += 2) PyList_Append(list, Py_BuildValue("(ii)",result[i],result[i+1])); free(result); Py_INCREF(list); return list; }
// -------------------------------------------------------- // -------------------------------------------------------- void SMAHeat::BuildVisionCache(World const & parWorld, Agent* parAgent) { std::cout << "build Cluster internal link <=> Agent" << std::endl; ReachabilityCluster * cluster = NULL; for (auto const & currentCluster : FReachabilityClusters) { if (currentCluster->Contains(parAgent)) cluster = currentCluster; } ReachablePathFind pathFind(&parWorld.GetCelluls(), cluster); // Reachabilite entre les different agent et les lien interieur for (auto& link : cluster->GetLinks()) { Position p1 = { link->x, link->y }; Position p2 = { parAgent->X(), parAgent->Y() }; pathFind.Init(p1, p2); unsigned int dist = pathFind.ComputePath(); if (dist) { link->ReachableAgents.push_back(AgentWithDist(parAgent, dist)); } } }
void RepairCrew::update(float delta) { P<PlayerSpaceship> ship; if (game_server) ship = game_server->getObjectById(ship_id); else if (game_client) ship = game_client->getObjectById(ship_id); else { destroy(); return; } if (game_server && !ship) { destroy(); return; } if (!ship || !ship->ship_template) return; if (position.x < -0.5) { ship->ship_template->interiorSize(); int n=irandom(0, ship->ship_template->rooms.size() - 1); position = sf::Vector2f(ship->ship_template->rooms[n].position + sf::Vector2i(irandom(0, ship->ship_template->rooms[n].size.x - 1), irandom(0, ship->ship_template->rooms[n].size.y - 1))); target_position = sf::Vector2i(position); } action_delay -= delta; sf::Vector2i pos = sf::Vector2i(position + sf::Vector2f(0.5, 0.5)); switch(action) { case RC_Idle: { action_delay = 1.0 / move_speed; if (pos != target_position) { ERepairCrewDirection new_direction = pathFind(pos, target_position, ship->ship_template); if (new_direction != RC_None) { action = RC_Move; direction = new_direction; } } position = sf::Vector2f(pos); ESystem system = ship->ship_template->getSystemAtRoom(pos); if (system != SYS_None) { ship->systems[system].health += repair_per_second * delta; if (ship->systems[system].health > 1.0) ship->systems[system].health = 1.0; } if (ship->auto_repair_enabled && pos == target_position && (system == SYS_None || !ship->hasSystem(system) || ship->systems[system].health == 1.0)) { int n=irandom(0, SYS_COUNT - 1); if (ship->hasSystem(ESystem(n)) && ship->systems[n].health < 1.0) { for(unsigned int idx=0; idx<ship->ship_template->rooms.size(); idx++) { if (ship->ship_template->rooms[idx].system == ESystem(n)) { target_position = ship->ship_template->rooms[idx].position + sf::Vector2i(irandom(0, ship->ship_template->rooms[idx].size.x - 1), irandom(0, ship->ship_template->rooms[idx].size.y - 1)); } } } } } break; case RC_Move: switch(direction) { case RC_None: break; case RC_Left: position.x -= delta * move_speed; break; case RC_Right: position.x += delta * move_speed; break; case RC_Up: position.y -= delta * move_speed; break; case RC_Down: position.y += delta * move_speed; break; } if (action_delay < 0.0) action = RC_Idle; break; } }
int main() { sf::RenderWindow Window; Window.create(sf::VideoMode(640, 640), "Fortress Survival 0.0.4, a new approach!"); Window.setFramerateLimit(300); Selection selection(Selection(2, sf::Color::White)); Player player(Player(sf::Vector2f(32, 32), sf::Vector2f(32, 32))); TileMap tile(TileMap(sf::Vector2f(Window.getSize().x / tileW, Window.getSize().y / tileH), sf::Color::Red)); for(int i = 0; i < tileH; i++) { for(int j = 0; j < tileW; j++) { if(level[i][j] == '0') map[j][i] = 0; else if(level[i][j] == '1') map[j][i] = 1; } } int xA, yA, xB, yB; std::string route; while(Window.isOpen()) { sf::Event Event; while(Window.pollEvent(Event)) { switch(Event.type) { case sf::Event::Closed: Window.close(); break; } } tile.Update(Window); selection.Update(Window); player.IsSelected(selection, Window); //player.Walk(Window); if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) { xA = player.rect.getPosition().x / 32; yA = player.rect.getPosition().y / 32; xB = sf::Mouse::getPosition(Window).x / 32; yB = sf::Mouse::getPosition(Window).y /32; //std::cout<<"Map Size (X,Y): "<<tileW<<","<<tileH<<std::endl; //std::cout<<"Start: "<<xA<<","<<yA<<std::endl; //std::cout<<"Finish: "<<xB<<","<<yB<<std::endl; // get the route //clock_t start = clock(); route = pathFind(xA, yA, xB, yB); if(route=="") std::cout<<"An empty route generated!"<<std::endl; //clock_t end = clock(); //double time_elapsed = double(end - start); //std::cout<<"Time to calculate the route (ms): "<<time_elapsed<<std::endl; //std::cout<<"Route:"<<std::endl; //std::cout<<route<<std::endl<<std::endl; // follow the route on the map and display it if(route.length()>0) { int j; char c; int x=xA; int y=yA; map[x][y]=2; for(int i=0;i<route.length();i++) { c =route.at(i); j=atoi(&c); x=x+dx[j]; y=y+dy[j]; map[x][y]=3; } map[x][y]=4; // display the map with the route /*for(int y=0;y<tileH;y++) { for(int x=0;x<tileW;x++) if(map[x][y]==0) std::cout<<"."; else if(map[x][y]==1) std::cout<<"O"; //obstacle else if(map[x][y]==2) std::cout<<"S"; //start else if(map[x][y]==3) std::cout<<"R"; //route else if(map[x][y]==4) std::cout<<"F"; //finish std::cout<<std::endl; }*/ for(int i = 0; i < tileH; i++) { for(int j = 0; j < tileW; j++) { if(level[i][j] == '0') map[j][i] = 0; else if(level[i+1][j] == '1') map[j][i] = 1; } } } } for(int i = 0; i < route.size(); i++) { sf::Time wtf; if(route[i] == '0') player.rect.move(32, 0); else if(route[i] == '1') player.rect.move(32, 32); else if(route[i] == '2') player.rect.move(0, 32); else if(route[i] == '3') player.rect.move(-32, 32); else if(route[i] == '4') player.rect.move(-32, 0); else if(route[i] == '5') player.rect.move(-32, -32); else if(route[i] == '6') player.rect.move(0, -32); else if(route[i] == '7') player.rect.move(32, -32); } if(player.rect.getPosition().x > Window.getSize().x || player.rect.getPosition().y > Window.getSize().x) { player.rect.setPosition(32, 32); } std::cout<<"Player pos: "<<player.rect.getPosition().x<<", "<<player.rect.getPosition().y<<"\n"; Window.display(); Window.clear(); } return 0; }
void cServant::Update(double dt) { if (route.empty()) { patrolPath.location = 0; route = pathFind(currTile.x, currTile.y, patrolPath.WayPointTileList[patrolPath.location].x, patrolPath.WayPointTileList[patrolPath.location].y); } if ((gotoNavi || gotoRoam) && routeCounter == 0 && routeCounter2 == 0 && routeCounter3 == 0) { if (gotoNavi) { route3 = ""; routeCounter3 = 0; hasSetDest2 = false; } else if (gotoRoam) { route = ""; routeCounter = 0; patrolPath.location = 0; AI_STATE = AS_ROAM; } } switch (AI_STATE) { case cServant::AS_ROAM: gotoRoam = false; if (currTile.x == patrolPath.WayPointTileList[patrolPath.location].x && currTile.y == patrolPath.WayPointTileList[patrolPath.location].y && routeCounter == 0 && rotating == false) { patrolPath.location++; if (patrolPath.location >= patrolPath.WayPointTileList.size()) patrolPath.location = 0; route = pathFind(currTile.x, currTile.y, patrolPath.WayPointTileList[patrolPath.location].x, patrolPath.WayPointTileList[patrolPath.location].y); } executePath(dt, route, routeCounter); if (gotoServe && routeCounter == 0) { route2 = ""; routeCounter2 = 0; hasSetDest = false; } break; default: break; } }
/* test routine */ int test_main(int args, char ** argv) { int i,j; waypoint_vec_t wmap; region_map_t regs; INIT(wmap); INIT(regs); bool ret; for (i=0; i<7; i++) { PUSH_BACK(regs, regions[i]); } int self_refs = 0; bool r = makeWaypointTable(&wmap, ®s, NR_POINTS, points); iprintf ("Waypoint Table: \n"); iprintf (" \t "); for (i=0; i<SIZE(wmap); ++i) { iprintf("%6d ", i); } iprintf("\n"); for (i=0; i<SIZE(wmap); ++i) { iprintf ("%d: %5s\t (%7.1f,%7.1f) ", i, points[i].comment, GET(wmap, i).pos.x, GET(wmap, i).pos.y); for (j=0; j<SIZE(wmap); j++) { double dist = GET(wmap, i).distances[j]; char sep = ((j+1)%5) == 0? '|':' '; if (dist == INFINITY) { iprintf (" %c", sep); } else if (dist < 0.01) { if (i == j) { iprintf (" * %c", sep); self_refs++; } else { iprintf (" X %c", sep); } } else { iprintf ("%6.1f%c", GET(wmap, i).distances[j], sep); } } iprintf ("\n"); } iprintf("\nSelf References: %d\n", self_refs); iprintf ("Starting waypoint-waypoint tests.\n"); for (i=0; i<SIZE(wmap); ++i) { int_vec_t path; INIT(path); ret = pathFind(&path, GET(wmap,0).pos, GET(wmap, i).pos, &wmap, ®s); iprintf ("RESULT: %d to %d: ", 0, i); for (j=0; j<SIZE(path); ++j) { iprintf ("%s (%d) ", points[GET(path,j)].comment, GET(path,j)); } iprintf ("\n"); } char startbuffer[128]; char endbuffer[128]; iprintf ("Interactive mode. Hit Ctrl-C to exit.\n"); while (true) { iprintf ("\nStart: "); fgets(startbuffer, 127, stdin); startbuffer[127] = 0; iprintf ("End: "); fgets(endbuffer, 127, stdin); endbuffer[127] = 0; char *s, *e; if (strchr(startbuffer, '\n')) { (*strchr(startbuffer, '\n')) = 0; } if (strchr(endbuffer, '\n')) { (*strchr(endbuffer, '\n')) = 0; } s = startbuffer; while (isspace(*s)) s++; e = endbuffer; while (isspace(*e)) e++; int start=-1, end=-1; for (i=0; i<SIZE(wmap) && start < 0; ++i) { if (!strcasecmp(s, points[i].comment)) { start = i; } } for (i=0; i<SIZE(wmap) && end < 0; ++i) { if (!strcasecmp(e, points[i].comment)) { end = i; } } if (start < 0) { iprintf("%s not found.\n", s); continue; } if (end < 0) { iprintf("%s not found.\n", e); continue; } iprintf ("Routing from %s to %s...\n", points[start].comment, points[end].comment); int_vec_t path; INIT(path); ret = pathFind(&path, GET(wmap, start).pos, GET(wmap, end).pos, &wmap, ®s); for (j=0; j<SIZE(path); ++j) { iprintf ("%s (%d) ", points[GET(path,j)].comment, GET(path,j)); } } }
int overmindAttack(gameMap_t * self, sf::RenderWindow & window, selection_t * cUnit,sf::Sprite & aCell, sf::Sprite & aEnemy, cursor_t * gCur, sf::Sprite & gameBackground, sf::Sprite & gameInterface, sf::Sprite & gameMenu, sf::Sprite & gameEndTurn, int player, sf::Sprite & cTc, player_t*pl, sf::Sprite & cBc //, //int XI, //int YJ ){ for(int i = 0;i<cellNumX;i++){ for(int j = 0;j<cellNumY;j++){ if(cUnit->cell==NULL){ return 0; } if ((float)sqrt((cUnit->cell->iX-i)*(cUnit->cell->iX-i)+(cUnit->cell->jY-j)*(cUnit->cell->jY-j)) <= (float)(cUnit->cell->unit->aRange)) { std::string tmp; if(self->cell[i][j].unit!=NULL && self->cell[i][j].unit->player != 1 && self->cell[i][j].obj_un_null == 1) { self->cell[i][j].obj_un_null = 0; std::string tmp; tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,i,j,self); if(tmp!="" && calculateLength(tmp) <= cUnit->cell->unit->aRange && cUnit->cell->unit->cPoints>0){ int tmpD = tmp[0] - 48; self->cell[i][j].obj_un_null = 1; cUnit->cell->unit->direction = tmpD; window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,1,pl); //pData(window,cUnit); sf::Texture tmpT; sf::Sprite tmpS; tmpT.loadFromFile(self->cell[i][j].unit->sprites.path[10]); tmpS.setTexture(tmpT); tmpS.setPosition(self->cell[i][j].X + rand()%50+20,self->cell[i][j].Y + rand()%50+20); window.draw(tmpS); sf::Texture tmpT1; sf::Sprite tmpS1; tmpT1.loadFromFile(self->cell[i][j].unit->sprites.path[11+self->cell[i][j].unit->direction]); tmpS1.setTexture(tmpT1); tmpS1.setPosition(self->cell[i][j].X,self->cell[i][j].Y); window.draw(tmpS1); window.display(); Sleep(400); cUnit->cell->unit->cPoints-=1; int tmpDmg = cUnit->cell->unit->dmg - self->cell[i][j].unit->armor; if(tmpDmg<0){tmpDmg=0;} self->cell[i][j].unit->hp-=tmpDmg; if(self->cell[i][j].unit->hp<=0){ sf::Texture tmpT; sf::Sprite tmpS; tmpT.loadFromFile(self->cell[i][j].unit->sprites.path[8]); tmpS.setTexture(tmpT); tmpS.setPosition(self->cell[i][j].X,self->cell[i][j].Y); window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,1,pl); window.draw(cBc); window.draw(tmpS); window.display(); Sleep(400); deleteUnit(self->cell[i][j].unit); self->cell[i][j].unit = NULL; self->cell[i][j].obj_un_null = 0; return 1; } return 1; }else{ self->cell[i][j].obj_un_null = 1; } } } } } return 0; }
int overmindFoundResources(gameMap_t * self, sf::RenderWindow & window, selection_t * cUnit, sf::Sprite & aCell, sf::Sprite & aEnemy, cursor_t * gCur, sf::Sprite & gameBackground, sf::Sprite & gameInterface, sf::Sprite & gameMenu, sf::Sprite & gameEndTurn, int player, sf::Sprite & cTc, player_t*pl, sf::Sprite & cBc,int XI,int YJ,resources_t * OP){ int tmpWay = 10000; int tmpCount; for(int i = 0;i<OP->count;i++){ if(OP->op[i]->object->currentPlayer!=1){ std::string tmp; self->cell[OP->op[i]->iX][OP->op[i]->jY].obj_un_null=0; tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[i]->iX,OP->op[i]->jY,self); if(tmpWay>calculateLength(tmp)){ tmpCount = i; } self->cell[OP->op[i]->iX][OP->op[i]->jY].obj_un_null=1; } } int tmpXX1 = cUnit->cell->iX; int tmpYY1 = cUnit->cell->jY; int tmpXX2 = OP->op[tmpCount]->iX+1; int tmpYY2 = OP->op[tmpCount]->jY+1; std::string tmp = pathFind(tmpXX1,tmpYY1,tmpXX2,tmpYY2,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX,OP->op[tmpCount]->jY+1,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX+1,OP->op[tmpCount]->jY,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX+1,OP->op[tmpCount]->jY-1,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX-1,OP->op[tmpCount]->jY+1,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX-1,OP->op[tmpCount]->jY,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX,OP->op[tmpCount]->jY-1,self); if(tmp!=""){ goto startMoving; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,OP->op[tmpCount]->iX-1,OP->op[tmpCount]->jY-1,self); if(tmp!=""){ goto startMoving; } return 0; startMoving:; int tmpCP; int tmpInt = tmp[0] - 48; if(tmpInt==0||tmpInt==2||tmpInt==4||tmpInt==6){ tmpCP=-1; }else{ tmpCP=-2; } if(cUnit->cell->unit->cPoints+tmpCP>=0){ cUnit->cell->unit->cPoints+=tmpCP; //cUnit->cell->unit->direction=tmpInt; cUnit->cell->unit->direction = tmpInt; /* *Start move! */ int tmpX = cUnit->cell->iX; int tmpY = cUnit->cell->jY; self->cell[tmpX][tmpY].obj_un_null=0; int tmpCX = cUnit->cell->X; int tmpCY = cUnit->cell->Y; sf::Texture tmpT; sf::Sprite tmpS; int tX = cUnit->cell->iX; int tY = cUnit->cell->jY; tmpT.loadFromFile(cUnit->cell->unit->sprites.path[cUnit->cell->unit->direction]); tmpS.setTexture(tmpT); switch(tmpInt){ case 0: tX+=1; break; case 1: tX+=1; tY+=1; break; case 2: tY+=1; break; case 3: tX-=1; tY+=1; break; case 4: tX-=1; break; case 5: tX-=1; tY-=1; break; case 6: tY-=1; break; case 7: tY-=1; tX+=1; break; } for(int a = 0 ; a<10;a++){ switch(tmpInt){ case 0: tmpCX+=10; break; case 1: tmpCX+=10; tmpCY+=10; break; case 2: tmpCY+=10; break; case 3: tmpCX-=10; tmpCY+=10; break; case 4: tmpCX-=10; break; case 5: tmpCX-=10; tmpCY-=10; break; case 6: tmpCY-=10; break; case 7: tmpCY-=10; tmpCX+=10; break; } Sleep(20); window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); /* window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl);*/ tmpS.setPosition(tmpCX,tmpCY); window.draw(tmpS); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl); window.draw(cTc); window.draw(cBc); window.display(); } self->cell[tX][tY].unit = self->cell[tmpX][tmpY].unit; self->cell[tX][tY].obj_un_null=1; cUnit->cell = &self->cell[tX][tY]; cUnit->status = 1; int TEST = cUnit->cell->unit->cPoints; self->cell[tmpX][tmpY].unit = NULL; window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); /* window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl);*/ tmpS.setPosition(tmpCX,tmpCY); window.draw(tmpS); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl); window.draw(cTc); window.draw(cBc); window.display(); return 1; } return 0; }
/** * Es llamado desde el bucle de la conexión. Decide el target para un bot. * Este */ int ClientIA::decideMovement(const bot & mybot){ const bot::position & pos = mybot.get_position(); start.x = pos.first; start.y = pos.second; bool aggresive; //Ataca si su puntuacón (incluyendo la posición global) es mejor que la del enemigo. float myPunt= -(1.0/(float)mybot.get_energy())-(5*(((*mapE)(pos.first,pos.second)+(*mapA)(pos.first,pos.second))+10.0)); float enemyPunt; if (!enemies.empty()){ enemyPunt= -(1.0/(float)enemies[0].get_energy())+(5*(((*mapE)(enemies[0].get_position().first,enemies[0].get_position().second)+(*mapA)(enemies[0].get_position().first, enemies[0].get_position().second))-10.0)); aggresive= myPunt>=enemyPunt; }else enemyPunt=-10; //traza //std::cout << mybot.get_team() << "e " << aggresive << "my " << myPunt << "enemy " << enemyPunt << std::endl; //Si es mejor su posición, entrará en modo agresivo y se moverá hacia el enemigo. if (aggresive) { const bot::position & epos = enemies[0].get_position(); target.x = epos.first; target.y = epos.second; }else{ //Si no lo es, huirá a la parte mas segura de al lado del bot aliado mas fuerte (incluyendo posición estratégica) if (!allies.empty()){ const bot::position & apos = allies[0].get_position(); float minor = 0.0; float act = 0.0; target.x = apos.first; target.y=apos.second; for(int i=apos.first-1;i<=(signed)(apos.first+1);i++) //3 for(int j=apos.second-1;j<=(signed)(apos.second+1);j++) //3x3 { if (i>=0 && i<sy && j>=0 && j<sx){ if(std::abs(i-(signed)apos.first)==1 || std::abs(j-(signed)apos.second)==1){ act = (*mapE)(i,j)+(*mapA)(i,j); if (act<minor && act>-7.0F){ minor=act; target.x = i; target.y=j; } } } } } } //Si el destino es el mismo bot, no hace nada. (Pocas veces pasará) if (start.x==target.x && start.y==target.y) return 0; //Busca el camino hacia el target. Si no es alcanzable, attkdir será 0 int attkdir = pathFind(start,target); if (attkdir == 0) { //Si no es alcanzable (está encerrado) Atacará al enemigo con menos vida. //KAMIKAZE MODE! xD std::vector<bot *> bs= const_cast<bots*>(&_bots)->adjacent(pos); if (!bs.empty()){ for(unsigned int i = 0; i<bs.size(); ++i) { if (_id == bs[i]->get_team()){ bs.erase(bs.begin()+i); } } if (!bs.empty()) { std::sort(bs.begin(), bs.end(), [](bot* a, bot* b){ return (a->get_energy()<=b->get_energy()); }); target.x=bs[0]->get_position().first; target.y=bs[0]->get_position().second; return pathFind(start,target); } } } return attkdir; }
void findWay(gameMap_t * self, sf::RenderWindow & window, selection_t * cUnit, sf::Sprite & aCell, sf::Sprite & aEnemy, cursor_t * gCur, sf::Sprite & gameBackground, sf::Sprite & gameInterface, sf::Sprite & gameMenu, sf::Sprite & gameEndTurn, int player, sf::Sprite & cTc, player_t*pl, sf::Sprite & cBc){ if(cUnit->status==0){ return; } for(int i = 0;i<cellNumX;i++){ for(int j = 0;j<cellNumY;j++){ int added = 0; if (cUnit->cell!=NULL&&(float)sqrt((cUnit->cell->iX-i)*(cUnit->cell->iX-i)+(cUnit->cell->jY-j)*(cUnit->cell->jY-j)) <= (float)(cUnit->cell->unit->cPoints) || (float)sqrt((cUnit->cell->iX-i)*(cUnit->cell->iX-i)+(cUnit->cell->jY-j)*(cUnit->cell->jY-j)) <= (float)(cUnit->cell->unit->aRange)) { std::string tmp; int tmpSt = self->cell[i][j].obj_un_null; if(self->cell[i][j].unit!=NULL && self->cell[i][j].unit->player != cUnit->cell->unit->player && self->cell[i][j].obj_un_null == 1) { self->cell[i][j].obj_un_null = 0; tmpSt = 1; } tmp = pathFind(cUnit->cell->iX,cUnit->cell->jY,i,j,self); if(tmp!="" && calculateLength(tmp)<=cUnit->cell->unit->cPoints){ aCell.setPosition(self->cell[i][j].X,self->cell[i][j].Y); window.draw(aCell); if(gCur->status==1&&gCur->cell->iX == i &&gCur->cell->jY == j && tmp!=""&& self->cell[gCur->cell->iX][gCur->cell->jY].unit == NULL){ if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){ cUnit->cell->unit->cPoints-=calculateLength(tmp); int tmpInt = tmp[0] - 48; cUnit->cell->unit->direction=tmpInt; int tmpX = 0, tmpY = 0; cUnit->cell->unit->direction = tmpInt; tmpX = cUnit->cell->iX; tmpY = cUnit->cell->jY; self->cell[tmpX][tmpY].obj_un_null=0; int tmpCX = cUnit->cell->X; int tmpCY = cUnit->cell->Y; for(int l = 0;l<tmp.length();l++){ int tmpD = tmp[l] - 48; cUnit->cell->unit->direction = tmpD; sf::Texture tmpT; sf::Sprite tmpS; tmpT.loadFromFile(cUnit->cell->unit->sprites.path[cUnit->cell->unit->direction]); tmpS.setTexture(tmpT); for(int a = 0 ; a<10;a++){ switch(tmpD){ case 0: tmpCX+=10; break; case 1: tmpCX+=10; tmpCY+=10; break; case 2: tmpCY+=10; break; case 3: tmpCX-=10; tmpCY+=10; break; case 4: tmpCX-=10; break; case 5: tmpCX-=10; tmpCY-=10; break; case 6: tmpCY-=10; break; case 7: tmpCY-=10; tmpCX+=10; break; } Sleep(20); window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); /* window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl);*/ tmpS.setPosition(tmpCX,tmpCY); window.draw(tmpS); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl); window.draw(cTc); window.draw(cBc); pData(window,cUnit); window.display(); } } self->cell[gCur->cell->iX][gCur->cell->jY].unit = cUnit->cell->unit; self->cell[gCur->cell->iX][gCur->cell->jY].obj_un_null=1; added = 1; setSelect(&self->cell[gCur->cell->iX][gCur->cell->jY],cUnit); self->cell[tmpX][tmpY].unit = NULL; } } } if(tmpSt == 1 && tmp!="" && calculateLength(tmp)<=cUnit->cell->unit->aRange&&cUnit->cell->unit->cPoints>0){ aEnemy.setPosition(self->cell[i][j].X,self->cell[i][j].Y); window.draw(aEnemy); if(gCur->status==1&&gCur->cell->iX == i &&gCur->cell->jY == j&& self->cell[gCur->cell->iX][gCur->cell->jY].unit != NULL && cUnit->cell->unit->player!=self->cell[i][j].unit->player){ if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){ if(added!=1){ self->cell[i][j].obj_un_null = tmpSt; } int tmpD = tmp[0] - 48; cUnit->cell->unit->direction = tmpD; window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); sf::Texture tmpT; sf::Sprite tmpS; tmpT.loadFromFile(self->cell[i][j].unit->sprites.path[10]); tmpS.setTexture(tmpT); tmpS.setPosition(self->cell[i][j].X + rand()%50+20,self->cell[i][j].Y + rand()%50+20); window.draw(tmpS); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl); pData(window,cUnit); sf::Texture tmpT1; sf::Sprite tmpS1; tmpT1.loadFromFile(self->cell[i][j].unit->sprites.path[11+cUnit->cell->unit->direction]); tmpS1.setTexture(tmpT1); tmpS1.setPosition(cUnit->cell->X,cUnit->cell->Y); window.draw(tmpS1); window.display(); Sleep(400); cUnit->cell->unit->cPoints-=1; int tmpDmg = cUnit->cell->unit->dmg - self->cell[i][j].unit->armor; if(tmpDmg<0){tmpDmg=0;} self->cell[i][j].unit->hp-=tmpDmg; if(self->cell[i][j].unit->hp<=0){ sf::Texture tmpT; sf::Sprite tmpS; tmpT.loadFromFile(self->cell[i][j].unit->sprites.path[8]); tmpS.setTexture(tmpT); tmpS.setPosition(self->cell[i][j].X,self->cell[i][j].Y); window.clear(sf::Color::Black); window.draw(gameBackground); drawUnits(self,window); window.draw(gameInterface); window.draw(gameMenu); drawMinMap(window,player,self); window.draw(gameEndTurn); drawPlayer(window,player,pl); pData(window,cUnit); window.draw(cBc); window.draw(tmpS); window.display(); Sleep(400); deleteUnit(self->cell[i][j].unit); self->cell[i][j].unit = NULL; self->cell[i][j].obj_un_null = 0; return; } } } } if(added!=1){ self->cell[i][j].obj_un_null = tmpSt; } } } } }
// -------------------------------------------------------- // -------------------------------------------------------- void SMAHeat::BuildVisionCache(World const & parWorld) { std::cout << "Vuild Vision cache" << std::endl; CellularAutomata const & celluls = parWorld.GetCelluls(); PointI const & mapSize = celluls.GetSize(); std::cout << "build Cluster external link" << std::endl; for (unsigned int i = 0; i < FReachabilityClusters.size(); ++i) { ReachabilityCluster * currentCluster = FReachabilityClusters[i]; // bot { unsigned int otherClusterIndex = i + 0 + 1 * mapSize.x / CLUSTER_SIZE; if (otherClusterIndex < FReachabilityClusters.size()) { ReachabilityCluster * oCluster = FReachabilityClusters[otherClusterIndex]; if (currentCluster->MaxX() >= mapSize.x) continue; CreateLinkBot(celluls, currentCluster, oCluster, currentCluster->MinX(), currentCluster->MinX() + CLUSTER_SIZE, currentCluster->MaxY()); } } // right { unsigned int otherClusterIndex = i + 1 + 0 * mapSize.x / CLUSTER_SIZE; if (otherClusterIndex < FReachabilityClusters.size()) { ReachabilityCluster * oCluster = FReachabilityClusters[otherClusterIndex]; if (currentCluster->MaxX() >= mapSize.x) continue; CreateLinkRight(celluls, currentCluster, oCluster, currentCluster->MinY(), currentCluster->MinY() + CLUSTER_SIZE, currentCluster->MaxX()); } } } std::cout << "build Cluster internal link <=> link" << std::endl; for (ReachabilityCluster * cluster : FReachabilityClusters) { ReachablePathFind pathFind(&celluls, cluster); // Reachabilite entre les different lien vers l'exterieur for (auto& link : cluster->GetLinks()) { for (auto const & oLink : cluster->GetLinks()) { if (link == oLink) continue; Position p1 = { link->x, link->y }; Position p2 = { oLink->x, oLink->y }; pathFind.Init(p1, p2); unsigned int dist = pathFind.ComputePath(); if (dist) { link->ReachableLinks.push_back(LinkWithDist(oLink, dist)); } } } } std::cout << "build Cluster: End" << std::endl; std::vector<Link const *> links; for (ReachabilityCluster * cluster : FReachabilityClusters) for (auto& link : cluster->GetLinks()) links.push_back(link); FLinkPathFinder.Init(links, mapSize.x, mapSize.y); }