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"
|
2021-05-16 14:39:38 +02:00
|
|
|
#include "../AIGateway.h"
|
2021-05-15 20:57:36 +02:00
|
|
|
#include "../Engine/Nullkiller.h"
|
2021-05-16 13:38:53 +02:00
|
|
|
#include "../Goals/Composition.h"
|
2021-05-15 20:56:31 +02:00
|
|
|
#include "../Goals/ExecuteHeroChain.h"
|
2022-09-26 20:01:07 +02:00
|
|
|
#include "../Goals/Invalid.h"
|
2021-05-15 18:23:11 +02:00
|
|
|
#include "CaptureObjectsBehavior.h"
|
|
|
|
#include "../AIUtility.h"
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-15 18:23:11 +02:00
|
|
|
extern boost::thread_specific_ptr<CCallback> cb;
|
2021-05-16 14:39:38 +02:00
|
|
|
extern boost::thread_specific_ptr<AIGateway> ai;
|
2021-05-15 18:23:11 +02:00
|
|
|
|
|
|
|
using namespace Goals;
|
|
|
|
|
2021-05-16 13:38:53 +02:00
|
|
|
template <typename T>
|
|
|
|
bool vectorEquals(const std::vector<T> & v1, const std::vector<T> & v2)
|
|
|
|
{
|
|
|
|
return vstd::contains_if(v1, [&](T o) -> bool
|
|
|
|
{
|
|
|
|
return vstd::contains(v2, o);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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-16 13:38:53 +02:00
|
|
|
bool CaptureObjectsBehavior::operator==(const CaptureObjectsBehavior & other) const
|
|
|
|
{
|
|
|
|
if(specificObjects != other.specificObjects)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(specificObjects)
|
|
|
|
return vectorEquals(objectsToCapture, other.objectsToCapture);
|
|
|
|
|
|
|
|
return vectorEquals(objectTypes, other.objectTypes)
|
|
|
|
&& vectorEquals(objectSubTypes, other.objectSubTypes);
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
Goals::TGoalVec CaptureObjectsBehavior::getVisitGoals(const std::vector<AIPath> & paths, const CGObjectInstance * objToVisit)
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
Goals::TGoalVec tasks;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
tasks.reserve(paths.size());
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
const AIPath * closestWay = nullptr;
|
|
|
|
std::vector<ExecuteHeroChain *> waysToVisitObj;
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
for(auto & path : paths)
|
|
|
|
{
|
|
|
|
tasks.push_back(sptr(Goals::Invalid()));
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2021-05-16 13:45:12 +02:00
|
|
|
logAi->trace("Path found %s", path.toString());
|
2021-05-15 20:56:31 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
if(ai->nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->trace("Ignore path. Target hero can be killed by enemy. Our power %lld", path.getHeroStrength());
|
2021-05-15 20:57:36 +02:00
|
|
|
#endif
|
2021-05-16 13:45:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-15 20:57:36 +02:00
|
|
|
|
2021-05-16 14:08:56 +02:00
|
|
|
if(objToVisit && !shouldVisit(ai->nullkiller.get(), path.targetHero, objToVisit))
|
2021-05-16 13:45:12 +02:00
|
|
|
continue;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
auto hero = path.targetHero;
|
|
|
|
auto danger = path.getTotalDanger();
|
2021-05-16 12:52:30 +02:00
|
|
|
|
2023-03-05 15:42:15 +02:00
|
|
|
if(ai->nullkiller->heroManager->getHeroRole(hero) == HeroRole::SCOUT && path.exchangeCount > 1)
|
2021-05-16 13:45:12 +02:00
|
|
|
continue;
|
2021-05-16 12:52:30 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
auto firstBlockedAction = path.getFirstBlockedAction();
|
|
|
|
if(firstBlockedAction)
|
|
|
|
{
|
|
|
|
auto subGoal = firstBlockedAction->decompose(path.targetHero);
|
2021-05-16 13:38:53 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2021-05-16 13:45:12 +02:00
|
|
|
logAi->trace("Decomposing special action %s returns %s", firstBlockedAction->toString(), subGoal->toString());
|
2021-05-16 13:38:53 +02:00
|
|
|
#endif
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
if(!subGoal->invalid())
|
|
|
|
{
|
|
|
|
Composition composition;
|
|
|
|
|
|
|
|
composition.addNext(ExecuteHeroChain(path, objToVisit));
|
|
|
|
composition.addNext(subGoal);
|
2021-05-16 13:38:53 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
tasks[tasks.size() - 1] = sptr(composition);
|
|
|
|
}
|
2021-05-16 13:38:53 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-16 13:38:53 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
auto isSafe = isSafeToVisit(hero, path.heroArmy, danger);
|
2019-12-15 15:47:21 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 2
|
2021-05-16 13:45:12 +02:00
|
|
|
logAi->trace(
|
|
|
|
"It is %s to visit %s by %s with army %lld, danger %lld and army loss %lld",
|
|
|
|
isSafe ? "safe" : "not safe",
|
|
|
|
objToVisit ? objToVisit->getObjectName() : path.targetTile().toString(),
|
2023-02-28 09:07:59 +02:00
|
|
|
hero->getObjectName(),
|
2021-05-16 13:45:12 +02:00
|
|
|
path.getHeroStrength(),
|
|
|
|
danger,
|
|
|
|
path.getTotalArmyLoss());
|
2021-05-15 20:57:27 +02:00
|
|
|
#endif
|
2021-05-15 20:56:31 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
if(isSafe)
|
|
|
|
{
|
|
|
|
auto newWay = new ExecuteHeroChain(path, objToVisit);
|
|
|
|
TSubgoal sharedPtr;
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
sharedPtr.reset(newWay);
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2023-03-05 15:42:15 +02:00
|
|
|
auto heroRole = ai->nullkiller->heroManager->getHeroRole(path.targetHero);
|
|
|
|
|
|
|
|
if(heroRole == HeroRole::SCOUT
|
|
|
|
&& (!closestWay || closestWay->movementCost() > path.movementCost()))
|
|
|
|
{
|
2021-05-16 13:45:12 +02:00
|
|
|
closestWay = &path;
|
2023-03-05 15:42:15 +02:00
|
|
|
}
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
if(!ai->nullkiller->arePathHeroesLocked(path))
|
2021-05-15 20:56:31 +02:00
|
|
|
{
|
2021-05-16 13:45:12 +02:00
|
|
|
waysToVisitObj.push_back(newWay);
|
|
|
|
tasks[tasks.size() - 1] = sharedPtr;
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-16 13:45:12 +02:00
|
|
|
|
2023-03-05 15:42:15 +02:00
|
|
|
if(closestWay)
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2023-03-05 15:42:15 +02:00
|
|
|
for(auto way : waysToVisitObj)
|
|
|
|
{
|
|
|
|
way->closestWayRatio
|
|
|
|
= closestWay->movementCost() / way->getPath().movementCost();
|
|
|
|
}
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
Goals::TGoalVec CaptureObjectsBehavior::decompose() const
|
2021-05-15 18:23:11 +02:00
|
|
|
{
|
2021-05-16 13:45:12 +02:00
|
|
|
Goals::TGoalVec tasks;
|
2021-05-15 21:04:53 +02:00
|
|
|
|
2021-05-16 14:09:44 +02:00
|
|
|
auto captureObjects = [&](const std::vector<const CGObjectInstance*> & objs) -> void
|
|
|
|
{
|
2021-05-16 13:45:12 +02:00
|
|
|
if(objs.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logAi->debug("Scanning objects, count %d", objs.size());
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
for(auto objToVisit : objs)
|
2021-05-16 14:00:24 +02:00
|
|
|
{
|
|
|
|
if(!objectMatchesFilter(objToVisit))
|
|
|
|
continue;
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2021-05-16 13:45:12 +02:00
|
|
|
logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const int3 pos = objToVisit->visitablePos();
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
auto paths = ai->nullkiller->pathfinder->getPathInfo(pos);
|
2021-05-16 13:45:12 +02:00
|
|
|
std::vector<std::shared_ptr<ExecuteHeroChain>> waysToVisitObj;
|
|
|
|
std::shared_ptr<ExecuteHeroChain> closestWay;
|
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2021-05-16 13:45:12 +02:00
|
|
|
logAi->trace("Found %d paths", paths.size());
|
|
|
|
#endif
|
|
|
|
vstd::concatenate(tasks, getVisitGoals(paths, objToVisit));
|
|
|
|
}
|
2021-05-16 13:45:45 +02:00
|
|
|
|
|
|
|
vstd::erase_if(tasks, [](TSubgoal task) -> bool
|
|
|
|
{
|
|
|
|
return task->invalid();
|
|
|
|
});
|
2021-05-16 13:45:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if(specificObjects)
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-16 13:45:12 +02:00
|
|
|
captureObjects(objectsToCapture);
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
2021-05-16 13:56:42 +02:00
|
|
|
else if(objectTypes.size())
|
|
|
|
{
|
|
|
|
captureObjects(
|
|
|
|
std::vector<const CGObjectInstance *>(
|
|
|
|
ai->nullkiller->memory->visitableObjs.begin(),
|
|
|
|
ai->nullkiller->memory->visitableObjs.end()));
|
|
|
|
}
|
2021-05-16 13:45:12 +02:00
|
|
|
else
|
2021-05-16 13:22:41 +02:00
|
|
|
{
|
2021-05-16 13:45:12 +02:00
|
|
|
captureObjects(ai->nullkiller->objectClusterizer->getNearbyObjects());
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:45:45 +02:00
|
|
|
if(tasks.empty())
|
|
|
|
captureObjects(ai->nullkiller->objectClusterizer->getFarObjects());
|
|
|
|
}
|
2021-05-16 13:45:12 +02:00
|
|
|
|
|
|
|
return tasks;
|
|
|
|
}
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:56:27 +02:00
|
|
|
bool CaptureObjectsBehavior::objectMatchesFilter(const CGObjectInstance * obj) const
|
2021-05-16 13:45:12 +02:00
|
|
|
{
|
|
|
|
if(objectTypes.size() && !vstd::contains(objectTypes, obj->ID.num))
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
if(objectSubTypes.size() && !vstd::contains(objectSubTypes, obj->subID))
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-15 18:23:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:45:12 +02:00
|
|
|
return true;
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|