1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-18 17:40:48 +02:00
vcmi/AI/Nullkiller/Behaviors/CaptureObjectsBehavior.cpp

265 lines
6.3 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
{
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);
}
2024-03-31 17:39:00 +02:00
Goals::TGoalVec CaptureObjectsBehavior::getVisitGoals(const std::vector<AIPath> & paths, const Nullkiller * nullkiller, const CGObjectInstance * objToVisit)
{
Goals::TGoalVec tasks;
2021-05-16 13:45:12 +02:00
tasks.reserve(paths.size());
std::unordered_map<HeroRole, const AIPath *> closestWaysByRole;
2021-05-16 13:45:12 +02:00
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
2024-03-29 20:39:03 +02:00
if(nullkiller->dangerHitMap->enemyCanKillOurHeroesAlongThePath(path))
2021-05-16 13:45:12 +02:00
{
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;
}
2024-03-29 20:39:03 +02:00
if(objToVisit && !shouldVisit(nullkiller, path.targetHero, objToVisit))
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path. Hero %s should not visit obj %s", path.targetHero->getNameTranslated(), objToVisit->getObjectName());
#endif
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
2024-03-29 20:39:03 +02:00
if(nullkiller->heroManager->getHeroRole(hero) == HeroRole::SCOUT
&& (path.getTotalDanger() == 0 || path.turn() > 0)
&& path.exchangeCount > 1)
{
#if NKAI_TRACE_LEVEL >= 2
logAi->trace("Ignore path. Hero %s is SCOUT, chain used and no danger", path.targetHero->getNameTranslated());
#endif
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)
{
2024-03-31 17:39:00 +02:00
auto subGoal = firstBlockedAction->decompose(nullkiller, 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);
2024-03-29 20:39:03 +02:00
auto heroRole = nullkiller->heroManager->getHeroRole(path.targetHero);
2023-03-05 15:42:15 +02:00
auto & closestWay = closestWaysByRole[heroRole];
if(!closestWay || closestWay->movementCost() > path.movementCost())
2023-03-05 15:42:15 +02:00
{
2021-05-16 13:45:12 +02:00
closestWay = &path;
2023-03-05 15:42:15 +02:00
}
2024-03-29 20:39:03 +02:00
if(!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
for(auto way : waysToVisitObj)
{
2024-03-29 20:39:03 +02:00
auto heroRole = nullkiller->heroManager->getHeroRole(way->getPath().targetHero);
auto closestWay = closestWaysByRole[heroRole];
if(closestWay)
2023-03-05 15:42:15 +02:00
{
way->closestWayRatio
= closestWay->movementCost() / way->getPath().movementCost();
}
}
return tasks;
}
2024-03-31 17:39:00 +02:00
void CaptureObjectsBehavior::decomposeObjects(
Goals::TGoalVec & result,
const std::vector<const CGObjectInstance *> & objs,
const Nullkiller * nullkiller) const
{
2024-03-31 17:39:00 +02:00
if(objs.empty())
{
return;
}
2024-03-29 20:39:03 +02:00
std::mutex sync;
2021-05-15 21:04:53 +02:00
2024-03-31 17:39:00 +02:00
logAi->debug("Scanning objects, count %d", objs.size());
2024-03-29 20:39:03 +02:00
2024-03-31 17:39:00 +02:00
// tbb::blocked_range<size_t> r(0, objs.size());
tbb::parallel_for(
tbb::blocked_range<size_t>(0, objs.size()),
[this, &objs, &sync, &result, nullkiller](const tbb::blocked_range<size_t> & r)
2021-05-16 13:45:12 +02:00
{
2024-03-31 17:39:00 +02:00
std::vector<AIPath> paths;
Goals::TGoalVec tasksLocal;
2021-05-16 13:45:12 +02:00
2024-03-31 17:39:00 +02:00
for(auto i = r.begin(); i != r.end(); i++)
2024-03-29 20:39:03 +02:00
{
2024-03-31 17:39:00 +02:00
auto objToVisit = objs[i];
2024-03-29 20:39:03 +02:00
2024-03-31 17:39:00 +02:00
if(!objectMatchesFilter(objToVisit))
continue;
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 1
2024-03-31 17:39:00 +02:00
logAi->trace("Checking object %s, %s", objToVisit->getObjectName(), objToVisit->visitablePos().toString());
2021-05-16 13:45:12 +02:00
#endif
2024-03-31 17:39:00 +02:00
nullkiller->pathfinder->calculatePathInfo(paths, objToVisit->visitablePos(), nullkiller->settings->isObjectGraphAllowed());
2021-05-16 13:45:12 +02:00
2022-09-26 20:01:07 +02:00
#if NKAI_TRACE_LEVEL >= 1
2024-03-31 17:39:00 +02:00
logAi->trace("Found %d paths", paths.size());
2021-05-16 13:45:12 +02:00
#endif
2024-03-31 17:39:00 +02:00
vstd::concatenate(tasksLocal, getVisitGoals(paths, nullkiller, objToVisit));
}
2021-05-16 13:45:45 +02:00
2024-03-31 17:39:00 +02:00
std::lock_guard<std::mutex> lock(sync);
vstd::concatenate(result, tasksLocal);
});
}
2024-03-29 20:39:03 +02:00
2024-03-31 17:39:00 +02:00
Goals::TGoalVec CaptureObjectsBehavior::decompose(const Nullkiller * ai) const
{
Goals::TGoalVec tasks;
2024-03-29 20:39:03 +02:00
2024-03-31 17:39:00 +02:00
vstd::erase_if(tasks, [](TSubgoal task) -> bool
{
return task->invalid();
});
2021-05-16 13:45:12 +02:00
if(specificObjects)
{
2024-03-31 17:39:00 +02:00
decomposeObjects(tasks, objectsToCapture, ai);
}
else if(objectTypes.size())
{
2024-03-31 17:39:00 +02:00
decomposeObjects(
tasks,
std::vector<const CGObjectInstance *>(
2024-03-31 17:39:00 +02:00
ai->memory->visitableObjs.begin(),
ai->memory->visitableObjs.end()),
ai);
}
2021-05-16 13:45:12 +02:00
else
{
2024-03-31 17:39:00 +02:00
decomposeObjects(tasks, ai->objectClusterizer->getNearbyObjects(), ai);
2024-03-31 17:39:00 +02:00
if(tasks.empty() || ai->getScanDepth() != ScanDepth::SMALL)
decomposeObjects(tasks, ai->objectClusterizer->getFarObjects(), ai);
2021-05-16 13:45:45 +02:00
}
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
}