1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-28 23:06:24 +02:00
vcmi/AI/Nullkiller/Behaviors/CaptureObjectsBehavior.cpp

235 lines
5.4 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
#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"
#include "CaptureObjectsBehavior.h"
#include "../AIUtility.h"
2022-09-26 20:01:07 +02:00
namespace NKAI
{
extern boost::thread_specific_ptr<CCallback> cb;
2021-05-16 14:39:38 +02:00
extern boost::thread_specific_ptr<AIGateway> ai;
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);
});
}
std::string CaptureObjectsBehavior::toString() const
{
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)
{
Goals::TGoalVec tasks;
2021-05-16 13:45:12 +02:00
tasks.reserve(paths.size());
2021-05-16 13:45:12 +02:00
const AIPath * closestWay = nullptr;
std::vector<ExecuteHeroChain *> waysToVisitObj;
2021-05-16 13:45:12 +02:00
for(auto & path : paths)
{
tasks.push_back(sptr(Goals::Invalid()));
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());
#endif
2021-05-16 13:45:12 +02:00
continue;
}
if(objToVisit && !shouldVisit(ai->nullkiller.get(), path.targetHero, objToVisit))
2021-05-16 13:45:12 +02:00
continue;
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);
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());
#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-16 13:45:12 +02:00
sharedPtr.reset(newWay);
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-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-16 13:45:12 +02:00
2023-03-05 15:42:15 +02:00
if(closestWay)
{
2023-03-05 15:42:15 +02:00
for(auto way : waysToVisitObj)
{
way->closestWayRatio
= closestWay->movementCost() / way->getPath().movementCost();
}
}
return tasks;
}
2021-05-16 13:45:12 +02:00
Goals::TGoalVec CaptureObjectsBehavior::decompose() const
{
2021-05-16 13:45:12 +02:00
Goals::TGoalVec tasks;
2021-05-15 21:04:53 +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-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();
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-16 13:45:12 +02:00
captureObjects(objectsToCapture);
}
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:45:12 +02:00
captureObjects(ai->nullkiller->objectClusterizer->getNearbyObjects());
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;
}
bool CaptureObjectsBehavior::objectMatchesFilter(const CGObjectInstance * obj) const
2021-05-16 13:45:12 +02:00
{
if(objectTypes.size() && !vstd::contains(objectTypes, obj->ID.num))
{
return false;
}
2021-05-16 13:45:12 +02:00
if(objectSubTypes.size() && !vstd::contains(objectSubTypes, obj->subID))
{
return false;
}
2021-05-16 13:45:12 +02:00
return true;
}
2022-09-26 20:01:07 +02:00
}