bool AiInterface::getNearestSightedResource(const ResourceType *rt, const Vec2i &pos, Vec2i &resultPos){ float tmpDist; float nearestDist= infinity; bool anyResource= false; const Map *map= world->getMap(); for(int i=0; i<map->getW(); ++i){ for(int j=0; j<map->getH(); ++j){ Vec2i surfPos= Map::toSurfCoords(Vec2i(i, j)); //if explored cell if(map->getSurfaceCell(surfPos)->isExplored(teamIndex)){ Resource *r= map->getSurfaceCell(surfPos)->getResource(); //if resource cell if(r!=NULL && r->getType()==rt){ tmpDist= pos.dist(Vec2i(i, j)); if(tmpDist<nearestDist){ anyResource= true; nearestDist= tmpDist; resultPos= Vec2i(i, j); } } } } } return anyResource; }
bool Snapshot::CheckIfBeingAttacked(Vec2i &pos, Field &field) { fprintf(logs, "In check if being attacked....\n"); if (aiInterface == NULL) fprintf(logs, " aiInterface is NULL\n"); fflush(logs); int count= aiInterface->onSightUnitCount(); const Unit *unit; int radius = baseRadius; for(int i=0; i<count; ++i){ unit= aiInterface->getOnSightUnit(i); if(!aiInterface->isAlly(unit) && unit->isAlive()){ pos= unit->getPos(); field= unit->getCurrField(); if(pos.dist(aiInterface->getHomeLocation())<radius) { aiInterface->printLog(4, "Being attacked at pos "+intToStr(pos.x)+","+intToStr(pos.y)+"\n"); fprintf(logs," Being attacked at pos %d %d \n" , pos.x ,pos.y); fflush(logs); beingAttacked = 1; return true; } } } aiInterface->printLog(4, "Not Being attacked \n"); fprintf(logs," Not Being attacked \n"); fflush(logs); beingAttacked = 0; return false; }