diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 52c79cf1..9efe8d4e 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -424,21 +424,68 @@ Vec2i Ai::getRandomHomePosition(){ // ==================== actions ==================== void Ai::sendScoutPatrol(){ - Vec2i pos; - int unit; + Vec2i pos; + int unit; + bool possibleTargetFound= false; - startLoc= (startLoc+1) % aiInterface->getMapMaxPlayers(); - pos= aiInterface->getStartLocation(startLoc); + if((aiInterface->getControlType() == ctCpuMega || aiInterface->getControlType() == ctNetworkCpuMega) + && random.randRange(0, 1) == 1) + { + Map *map= aiInterface->getMap(); - if(aiInterface->getFactionIndex()!=startLoc){ - if(findAbleUnit(&unit, ccAttack, false)){ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + const TechTree *tt= aiInterface->getTechTree(); + const ResourceType *rt= tt->getResourceType(0); + int tryCount= 0; + int height= map->getH(); + int width= map->getW(); - aiInterface->giveCommand(unit, ccAttack, pos); - aiInterface->printLog(2, "Scout patrol sent to: " + intToStr(pos.x)+","+intToStr(pos.y)+"\n"); + for(int i= 0; i < tt->getResourceTypeCount(); ++i){ + const ResourceType *rt_= tt->getResourceType(i); + const Resource *r= aiInterface->getResource(rt); + + if(rt_->getClass() == rcTech){ + rt=rt_; + break; + } + } + //printf("looking for resource %s\n",rt->getName().c_str()); + while(possibleTargetFound == false){ + tryCount++; + if(tryCount == 4){ + //printf("no target found\n"); + break; + } + pos= Vec2i(random.randRange(2, width - 2), random.randRange(2, height - 2)); + if(map->isInside(pos) && map->isInsideSurface(map->toSurfCoords(pos))){ + //printf("is inside map\n"); + // find first resource in this area + Vec2i resPos; + if(aiInterface->isResourceInRegion(pos, rt, resPos, 20)){ + // found a possible target. + pos= resPos; + //printf("lets try the new target\n"); + possibleTargetFound= true; + break; + } + } + //else printf("is outside map\n"); } } + if(possibleTargetFound == false){ + startLoc= (startLoc + 1) % aiInterface->getMapMaxPlayers(); + pos= aiInterface->getStartLocation(startLoc); + printf("och noe\n"); + } + if(aiInterface->getHomeLocation() != pos){ + if(findAbleUnit(&unit, ccAttack, false)){ + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + aiInterface->giveCommand(unit, ccAttack, pos); + aiInterface->printLog(2, "Scout patrol sent to: " + intToStr(pos.x) + "," + intToStr(pos.y) + "\n"); + } + } } void Ai::massiveAttack(const Vec2i &pos, Field field, bool ultraAttack){ diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index 725b9181..106e9f02 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -428,6 +428,38 @@ const TechTree *AiInterface::getTechTree(){ return world->getTechTree(); } + +bool AiInterface::isResourceInRegion(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, int range) const { + const Map *map= world->getMap(); + RandomGen random; + int xi=1; + int xj=1; + if(random.randRange(0,1)==1){ + xi=-1; + } + if(random.randRange(0,1)==1){ + xj=-1; + } + for(int i = -range; i <= range; ++i) { + for(int j = -range; j <= range; ++j) { + int ii=xi*i; + int jj=xj*j; + if(map->isInside(pos.x + ii, pos.y + jj)) { + Resource *r= map->getSurfaceCell(map->toSurfCoords(Vec2i(pos.x + ii, pos.y + jj)))->getResource(); + if(r != NULL) { + if(r->getType() == rt) { + resourcePos= pos + Vec2i(ii,jj); + return true; + } + } + } + } + } + return false; +} + + + //returns if there is a resource next to a unit, in "resourcePos" is stored the relative position of the resource bool AiInterface::isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, Faction *faction, bool fallbackToPeersHarvestingSameResource) const { const Map *map= world->getMap(); diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index b92b1240..bb1e000b 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -86,6 +86,7 @@ public: const FactionType *getMyFactionType(); Faction *getMyFaction(); const TechTree *getTechTree(); + bool isResourceInRegion(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, int range) const; bool isResourceNear(const Vec2i &pos, const ResourceType *rt, Vec2i &resourcePos, Faction *faction, bool fallbackToPeersHarvestingSameResource) const; bool getNearestSightedResource(const ResourceType *rt, const Vec2i &pos, Vec2i &resultPos, bool usableResourceTypeOnly); bool isAlly(const Unit *unit) const; diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index 65fe9519..fdaf2c9e 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -40,7 +40,7 @@ namespace Glest{ namespace Game{ const int PathFinder::maxFreeSearchRadius = 10; //const int PathFinder::pathFindNodesMax= 400; -int PathFinder::pathFindNodesAbsoluteMax = 1500; +int PathFinder::pathFindNodesAbsoluteMax = 900; int PathFinder::pathFindNodesMax = 1500; const int PathFinder::pathFindRefresh = 10; const int PathFinder::pathFindBailoutRadius = 20;