From 1a49588a45878cb82293794af3954080609a7219 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 12 Jan 2013 01:55:13 +0000 Subject: [PATCH] - added smarts to the AI so it takes note of very active battle locations and scouts there. --- source/glest_game/ai/ai.cpp | 13 +++++++- source/glest_game/ai/ai_interface.cpp | 45 +++++++++++++++++++++++++++ source/glest_game/ai/ai_interface.h | 4 +++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index e9935447..f77cdc45 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -735,7 +735,18 @@ void Ai::sendScoutPatrol(){ && random.randRange(0, 2) == 1; bool megaResourceAttack=(aiInterface->getControlType() == ctCpuMega || aiInterface->getControlType() == ctNetworkCpuMega) && random.randRange(0, 1) == 1; - if( megaResourceAttack || ultraResourceAttack) { + + std::vector warningEnemyList = aiInterface->getEnemyWarningPositionList(); + if(warningEnemyList.empty() == false) { + for(int i = warningEnemyList.size() - 1; i <= 0; --i) { + Vec2i &checkPos = warningEnemyList[i]; + pos = checkPos; + possibleTargetFound = true; + aiInterface->removeEnemyWarningPositionFromList(checkPos); + break; + } + } + else if( megaResourceAttack || ultraResourceAttack) { Map *map= aiInterface->getMap(); const TechTree *tt= aiInterface->getTechTree(); diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index ef399cd7..2801946b 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -832,9 +832,23 @@ bool AiInterface::isFreeCells(const Vec2i &pos, int size, Field field){ return world->getMap()->isFreeCells(pos, size, field); } +void AiInterface::removeEnemyWarningPositionFromList(Vec2i &checkPos) { + for(int i = enemyWarningPositionList.size() - 1; i <= 0; --i) { + Vec2i &pos = enemyWarningPositionList[i]; + + if(checkPos == pos) { + enemyWarningPositionList.erase(enemyWarningPositionList.begin()+i); + break; + } + } +} + const Unit *AiInterface::getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int radius) { Map *map= world->getMap(); + const int CHECK_RADIUS = 12; + const int WARNING_ENEMY_COUNT = 6; + for(int i = 0; i < world->getFactionCount(); ++i) { for(int j = 0; j < world->getFaction(i)->getUnitCount(); ++j) { Unit * unit= world->getFaction(i)->getUnit(j); @@ -850,6 +864,37 @@ const Unit *AiInterface::getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int if(pos.dist(getHomeLocation()) < radius) { printLog(2, "Being attacked at pos "+intToStr(pos.x)+","+intToStr(pos.y)+"\n"); + // Now check if there are more than x enemies in sight and if + // so make note of the position + int foundEnemies = 0; + std::map foundEnemyList; + for(int aiX = pos.x-CHECK_RADIUS; aiX < pos.x + CHECK_RADIUS; ++aiX) { + for(int aiY = pos.y-CHECK_RADIUS; aiY < pos.y + CHECK_RADIUS; ++aiY) { + Vec2i checkPos(aiX,aiY); + if(map->isInside(checkPos) && map->isInsideSurface(map->toSurfCoords(checkPos))) { + Cell *cAI = map->getCell(checkPos); + SurfaceCell *scAI = map->getSurfaceCell(Map::toSurfCoords(checkPos)); + if(scAI != NULL && cAI != NULL && cAI->getUnit(field) != NULL && sc->isVisible(teamIndex)) { + const Unit *checkUnit = cAI->getUnit(field); + if(foundEnemyList.find(checkUnit->getId()) == foundEnemyList.end()) { + bool cannotSeeUnitAI = (checkUnit->getType()->hasCellMap() == true && + checkUnit->getType()->getAllowEmptyCellMap() == true && + checkUnit->getType()->hasEmptyCellMap() == true); + if(cannotSeeUnitAI == false && isAlly(checkUnit) == false + && checkUnit->isAlive() == true) { + foundEnemies++; + foundEnemyList[checkUnit->getId()] = true; + } + } + } + } + } + } + if(foundEnemies >= WARNING_ENEMY_COUNT) { + if(std::find(enemyWarningPositionList.begin(),enemyWarningPositionList.end(),pos) == enemyWarningPositionList.end()) { + enemyWarningPositionList.push_back(pos); + } + } return unit; } } diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index 295b6d6d..2e7a990f 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -80,6 +80,7 @@ private: Mutex *aiMutex; AiInterfaceThread *workerThread; + std::vector enemyWarningPositionList; public: AiInterface(Game &game, int factionIndex, int teamIndex, int useStartLocation=-1); @@ -88,6 +89,9 @@ public: //main void update(); + std::vector getEnemyWarningPositionList() const { return enemyWarningPositionList; } + void removeEnemyWarningPositionFromList(Vec2i &checkPos); + inline Mutex * getMutex() {return aiMutex;} void signalWorkerThread(int frameIndex);