2021-05-15 18:23:11 +02:00
|
|
|
/*
|
2021-05-15 21:04:26 +02:00
|
|
|
* CaptureObjectsBehavior.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2021-05-15 18:23:11 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "../VCAI.h"
|
2021-05-15 20:57:36 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-15 18:23:11 +02:00
|
|
|
#include "../AIhelper.h"
|
2021-05-15 20:56:31 +02:00
|
|
|
#include "../Goals/ExecuteHeroChain.h"
|
2021-05-15 18:23:11 +02:00
|
|
|
#include "CaptureObjectsBehavior.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
#include "lib/mapping/CMap.h" //for victory conditions
|
|
|
|
#include "lib/CPathfinder.h"
|
|
|
|
|
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
|
|
|
extern boost::thread_specific_ptr<VCAI> ai;
|
|
|
|
extern FuzzyHelper * fh;
|
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
std::string CaptureObjectsBehavior::toString() const
|
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return "Capture objects";
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
Goals::TGoalVec CaptureObjectsBehavior::getTasks()
|
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
Goals::TGoalVec tasks;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
|
|
|
auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void{
|
|
|
|
if(objs.empty())
|
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
logAi->debug("Scanning objects, count %d", objs.size());
|
2021-05-15 21:02:27 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
for(auto objToVisit : objs)
|
|
|
|
{
|
2021-05-16 13:19:00 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
2021-05-15 20:57:27 +02:00
|
|
|
logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
|
|
|
|
#endif
|
|
|
|
|
2021-05-15 18:23:11 +02:00
|
|
|
if(!shouldVisitObject(objToVisit))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const int3 pos = objToVisit->visitablePos();
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
auto paths = ai->ah->getPathsToTile(pos);
|
|
|
|
std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
|
|
|
|
std::shared_ptr<ExecuteHeroChain> closestWay;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 1
|
2021-05-16 12:51:32 +02:00
|
|
|
logAi->trace("Found %d paths", paths.size());
|
2021-05-15 20:57:27 +02:00
|
|
|
#endif
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
for(auto & path : paths)
|
2021-05-15 18:23:11 +02:00
|
|
|
{
|
2021-05-16 13:19:00 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-15 20:57:27 +02:00
|
|
|
logAi->trace("Path found %s", path.toString());
|
2021-05-15 20:56:31 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 13:15:03 +02:00
|
|
|
if(path.getFirstBlockedAction())
|
2021-05-16 12:52:30 +02:00
|
|
|
{
|
2021-05-16 13:19:00 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-16 12:52:30 +02:00
|
|
|
// TODO: decomposition?
|
|
|
|
logAi->trace("Ignore path. Action is blocked.");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:57:36 +02:00
|
|
|
if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
|
|
|
|
{
|
2021-05-16 13:19:00 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
|
|
|
logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.heroArmy->getArmyStrength());
|
2021-05-15 20:57:36 +02:00
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
if(!shouldVisit(path.targetHero, objToVisit))
|
2021-05-15 20:57:27 +02:00
|
|
|
continue;
|
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
auto hero = path.targetHero;
|
2021-05-16 13:13:40 +02:00
|
|
|
auto danger = path.getTotalDanger();
|
2021-05-16 12:52:30 +02:00
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
if(ai->ah->getHeroRole(hero) == HeroRole::SCOUT && danger == 0 && path.exchangeCount > 1)
|
2021-05-16 12:52:30 +02:00
|
|
|
continue;
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
|
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
#ifdef AI_TRACE_LEVEL >= 2
|
2021-05-15 20:57:27 +02:00
|
|
|
logAi->trace(
|
2021-05-16 12:51:32 +02:00
|
|
|
"It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
|
2021-05-15 20:57:27 +02:00
|
|
|
isSafe ? "safe" : "not safe",
|
2021-05-16 13:19:00 +02:00
|
|
|
objToVisit->getObjectName(),
|
2021-05-15 20:57:27 +02:00
|
|
|
hero->name,
|
|
|
|
path.getHeroStrength(),
|
|
|
|
danger,
|
2021-05-16 13:15:03 +02:00
|
|
|
path.getTotalArmyLoss());
|
2021-05-15 20:57:27 +02:00
|
|
|
#endif
|
2021-05-15 20:56:31 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
if(isSafe)
|
2021-05-15 20:56:31 +02:00
|
|
|
{
|
|
|
|
auto newWay = std::make_shared<ExecuteHeroChain>(path, objToVisit);
|
|
|
|
|
|
|
|
waysToVisitObj.push_back(newWay);
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
if(!closestWay || closestWay->evaluationContext.movementCost > newWay->evaluationContext.movementCost)
|
|
|
|
closestWay = newWay;
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
if(waysToVisitObj.empty())
|
|
|
|
continue;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-15 20:56:31 +02:00
|
|
|
for(auto way : waysToVisitObj)
|
|
|
|
{
|
2021-05-16 13:13:56 +02:00
|
|
|
if(ai->nullkiller->arePathHeroesLocked(way->getPath()))
|
|
|
|
continue;
|
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
if(ai->nullkiller->getHeroLockedReason(way->hero.get()) == HeroLockedReason::STARTUP)
|
|
|
|
continue;
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
way->evaluationContext.closestWayRatio
|
2021-05-16 13:19:00 +02:00
|
|
|
= closestWay->evaluationContext.movementCost / way->evaluationContext.movementCost;
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
tasks.push_back(sptr(*way));
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
if(specificObjects)
|
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
captureObjects(objectsToCapture);
|
|
|
|
}
|
2021-05-15 20:57:27 +02:00
|
|
|
else
|
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
captureObjects(std::vector<const CGObjectInstance*>(ai->visitableObjs.begin(), ai->visitableObjs.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CaptureObjectsBehavior::shouldVisitObject(ObjectIdRef obj) const
|
|
|
|
{
|
|
|
|
const CGObjectInstance* objInstance = obj;
|
|
|
|
|
2021-05-15 21:04:53 +02:00
|
|
|
if(!objInstance)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(objectTypes.size() && !vstd::contains(objectTypes, objInstance->ID.num))
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-15 21:04:53 +02:00
|
|
|
if(objectSubTypes.size() && !vstd::contains(objectSubTypes, objInstance->subID))
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int3 pos = objInstance->visitablePos();
|
|
|
|
|
2021-05-16 12:52:30 +02:00
|
|
|
if(objInstance->ID != Obj::CREATURE_GENERATOR1 && vstd::contains(ai->alreadyVisited, objInstance)
|
|
|
|
|| obj->wasVisited(ai->playerID))
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto playerRelations = cb->getPlayerRelations(ai->playerID, objInstance->tempOwner);
|
2021-05-15 20:57:27 +02:00
|
|
|
if(playerRelations != PlayerRelations::ENEMIES && !isWeeklyRevisitable(objInstance))
|
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//it may be hero visiting this obj
|
|
|
|
//we don't try visiting object on which allied or owned hero stands
|
|
|
|
// -> it will just trigger exchange windows and AI will be confused that obj behind doesn't get visited
|
2021-05-15 21:03:58 +02:00
|
|
|
const CGObjectInstance * topObj = cb->getTopObj(pos);
|
|
|
|
|
|
|
|
if(!topObj)
|
|
|
|
return false; // partly visible obj but its visitable pos is not visible.
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
if(topObj->ID == Obj::HERO && cb->getPlayerRelations(ai->playerID, topObj->tempOwner) != PlayerRelations::ENEMIES)
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true; //all of the following is met
|
|
|
|
}
|