2021-05-15 21:04:26 +02:00
|
|
|
/*
|
|
|
|
* Nullkiller.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:05 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "Nullkiller.h"
|
2021-05-16 14:39:38 +02:00
|
|
|
#include "../AIGateway.h"
|
2021-05-15 18:23:11 +02:00
|
|
|
#include "../Behaviors/CaptureObjectsBehavior.h"
|
|
|
|
#include "../Behaviors/RecruitHeroBehavior.h"
|
2021-05-15 20:57:31 +02:00
|
|
|
#include "../Behaviors/BuyArmyBehavior.h"
|
2021-05-15 21:04:26 +02:00
|
|
|
#include "../Behaviors/StartupBehavior.h"
|
2021-05-16 12:53:32 +02:00
|
|
|
#include "../Behaviors/DefenceBehavior.h"
|
2021-05-16 13:15:03 +02:00
|
|
|
#include "../Behaviors/BuildingBehavior.h"
|
2021-05-16 13:19:07 +02:00
|
|
|
#include "../Behaviors/GatherArmyBehavior.h"
|
2021-05-16 13:45:12 +02:00
|
|
|
#include "../Behaviors/ClusterBehavior.h"
|
2023-09-24 12:07:42 +02:00
|
|
|
#include "../Behaviors/StayAtTownBehavior.h"
|
2021-05-15 18:23:11 +02:00
|
|
|
#include "../Goals/Invalid.h"
|
2021-05-16 13:56:42 +02:00
|
|
|
#include "../Goals/Composition.h"
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2022-09-26 20:01:07 +02:00
|
|
|
namespace NKAI
|
|
|
|
{
|
|
|
|
|
2021-05-16 13:38:26 +02:00
|
|
|
using namespace Goals;
|
|
|
|
|
2024-01-20 22:54:30 +02:00
|
|
|
// while we play vcmieagles graph can be shared
|
|
|
|
std::unique_ptr<ObjectGraph> Nullkiller::baseGraph;
|
|
|
|
|
2021-05-15 18:23:38 +02:00
|
|
|
Nullkiller::Nullkiller()
|
2024-02-25 12:39:19 +02:00
|
|
|
:activeHero(nullptr), scanDepth(ScanDepth::MAIN_FULL), useHeroChain(true)
|
2021-05-15 18:23:38 +02:00
|
|
|
{
|
2024-02-25 12:39:19 +02:00
|
|
|
memory = std::make_unique<AIMemory>();
|
|
|
|
settings = std::make_unique<Settings>();
|
2020-05-04 17:58:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Nullkiller::init(std::shared_ptr<CCallback> cb, PlayerColor playerID)
|
|
|
|
{
|
|
|
|
this->cb = cb;
|
|
|
|
this->playerID = playerID;
|
|
|
|
|
|
|
|
priorityEvaluator.reset(new PriorityEvaluator(this));
|
2021-05-16 14:08:56 +02:00
|
|
|
priorityEvaluators.reset(
|
|
|
|
new SharedPool<PriorityEvaluator>(
|
|
|
|
[&]()->std::unique_ptr<PriorityEvaluator>
|
|
|
|
{
|
2022-12-07 23:36:20 +02:00
|
|
|
return std::make_unique<PriorityEvaluator>(this);
|
2021-05-16 14:08:56 +02:00
|
|
|
}));
|
|
|
|
|
2020-05-04 17:58:43 +02:00
|
|
|
dangerHitMap.reset(new DangerHitMapAnalyzer(this));
|
2021-05-16 13:57:33 +02:00
|
|
|
buildAnalyzer.reset(new BuildAnalyzer(this));
|
2020-05-04 17:58:43 +02:00
|
|
|
objectClusterizer.reset(new ObjectClusterizer(this));
|
|
|
|
dangerEvaluator.reset(new FuzzyHelper(this));
|
|
|
|
pathfinder.reset(new AIPathfinder(cb.get(), this));
|
|
|
|
armyManager.reset(new ArmyManager(cb.get(), this));
|
|
|
|
heroManager.reset(new HeroManager(cb.get(), this));
|
2021-05-16 13:56:42 +02:00
|
|
|
decomposer.reset(new DeepDecomposer());
|
2023-06-04 15:02:02 +02:00
|
|
|
armyFormation.reset(new ArmyFormation(cb, this));
|
2021-05-15 18:23:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:38:26 +02:00
|
|
|
Goals::TTask Nullkiller::choseBestTask(Goals::TTaskVec & tasks) const
|
2021-05-15 18:23:11 +02:00
|
|
|
{
|
2021-05-16 13:38:26 +02:00
|
|
|
Goals::TTask bestTask = *vstd::maxElementByFun(tasks, [](Goals::TTask task) -> float{
|
|
|
|
return task->priority;
|
2021-05-15 18:23:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return bestTask;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
Goals::TTask Nullkiller::choseBestTask(Goals::TSubgoal behavior, int decompositionMaxDepth) const
|
2021-05-15 18:23:11 +02:00
|
|
|
{
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2021-05-15 20:57:44 +02:00
|
|
|
logAi->debug("Checking behavior %s", behavior->toString());
|
2021-05-15 20:27:22 +02:00
|
|
|
|
2021-11-23 08:41:03 +02:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
Goals::TGoalVec elementarGoals = decomposer->decompose(behavior, decompositionMaxDepth);
|
|
|
|
Goals::TTaskVec tasks;
|
2021-05-16 13:57:33 +02:00
|
|
|
|
|
|
|
boost::this_thread::interruption_point();
|
2021-05-16 13:56:42 +02:00
|
|
|
|
|
|
|
for(auto goal : elementarGoals)
|
2021-05-15 18:23:38 +02:00
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
Goals::TTask task = Goals::taskptr(*goal);
|
2021-05-16 13:38:53 +02:00
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
if(task->priority <= 0)
|
|
|
|
task->priority = priorityEvaluator->evaluate(goal);
|
2021-05-16 13:38:26 +02:00
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
tasks.push_back(task);
|
2021-05-15 18:23:38 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:38:53 +02:00
|
|
|
if(tasks.empty())
|
|
|
|
{
|
2021-05-16 13:56:13 +02:00
|
|
|
logAi->debug("Behavior %s found no tasks. Time taken %ld", behavior->toString(), timeElapsed(start));
|
2021-05-16 13:38:53 +02:00
|
|
|
|
|
|
|
return Goals::taskptr(Goals::Invalid());
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:23:38 +02:00
|
|
|
auto task = choseBestTask(tasks);
|
|
|
|
|
2021-05-16 13:56:13 +02:00
|
|
|
logAi->debug(
|
|
|
|
"Behavior %s returns %s, priority %f. Time taken %ld",
|
|
|
|
behavior->toString(),
|
|
|
|
task->toString(),
|
|
|
|
task->priority,
|
|
|
|
timeElapsed(start));
|
2021-05-15 18:23:38 +02:00
|
|
|
|
|
|
|
return task;
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
2021-05-15 18:23:05 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
void Nullkiller::resetAiState()
|
|
|
|
{
|
2023-12-17 10:11:05 +02:00
|
|
|
std::unique_lock<std::mutex> lockGuard(aiStateMutex);
|
|
|
|
|
2021-05-16 14:39:38 +02:00
|
|
|
lockedResources = TResources();
|
2023-07-27 14:58:49 +02:00
|
|
|
scanDepth = ScanDepth::MAIN_FULL;
|
2020-05-04 17:58:43 +02:00
|
|
|
playerID = ai->playerID;
|
2021-05-15 20:57:27 +02:00
|
|
|
lockedHeroes.clear();
|
2021-05-16 13:13:35 +02:00
|
|
|
dangerHitMap->reset();
|
2022-09-06 20:14:22 +02:00
|
|
|
useHeroChain = true;
|
2024-01-20 22:54:30 +02:00
|
|
|
|
|
|
|
if(!baseGraph)
|
|
|
|
{
|
|
|
|
baseGraph = std::make_unique<ObjectGraph>();
|
|
|
|
baseGraph->updateGraph(this);
|
|
|
|
}
|
2021-05-15 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
void Nullkiller::updateAiState(int pass, bool fast)
|
2021-05-15 20:57:27 +02:00
|
|
|
{
|
2021-05-16 13:57:33 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2023-12-17 10:11:05 +02:00
|
|
|
std::unique_lock<std::mutex> lockGuard(aiStateMutex);
|
|
|
|
|
2021-11-23 08:41:03 +02:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2021-05-16 13:56:13 +02:00
|
|
|
|
2021-05-16 13:22:41 +02:00
|
|
|
activeHero = nullptr;
|
2023-02-28 09:07:59 +02:00
|
|
|
setTargetObject(-1);
|
2021-05-16 13:22:41 +02:00
|
|
|
|
2023-07-29 17:54:20 +02:00
|
|
|
decomposer->reset();
|
|
|
|
buildAnalyzer->update();
|
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
if(!fast)
|
|
|
|
{
|
|
|
|
memory->removeInvisibleObjects(cb.get());
|
2021-05-16 13:59:32 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
dangerHitMap->updateHitMap();
|
2023-08-08 17:37:42 +02:00
|
|
|
dangerHitMap->calculateTileOwners();
|
2021-05-15 21:03:58 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
boost::this_thread::interruption_point();
|
2021-05-16 13:57:33 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
heroManager->update();
|
|
|
|
logAi->trace("Updating paths");
|
2021-05-16 13:56:13 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
std::map<const CGHeroInstance *, HeroRole> activeHeroes;
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
for(auto hero : cb->getHeroesInfo())
|
|
|
|
{
|
|
|
|
if(getHeroLockedReason(hero) == HeroLockedReason::DEFENCE)
|
|
|
|
continue;
|
2021-05-16 13:13:56 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
activeHeroes[hero] = heroManager->getHeroRole(hero);
|
|
|
|
}
|
2021-05-15 20:57:27 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
PathfinderSettings cfg;
|
|
|
|
cfg.useHeroChain = useHeroChain;
|
2024-01-27 22:19:27 +02:00
|
|
|
cfg.allowBypassObjects = true;
|
2021-05-16 13:56:21 +02:00
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
if(scanDepth == ScanDepth::SMALL)
|
2022-09-06 20:14:22 +02:00
|
|
|
{
|
2024-02-25 12:39:19 +02:00
|
|
|
cfg.mainTurnDistanceLimit = ai->nullkiller->settings->getMainHeroTurnDistanceLimit();
|
2023-07-27 14:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(scanDepth != ScanDepth::ALL_FULL)
|
|
|
|
{
|
2024-02-25 12:39:19 +02:00
|
|
|
cfg.scoutTurnDistanceLimit = ai->nullkiller->settings->getScoutHeroTurnDistanceLimit();
|
2022-09-06 20:14:22 +02:00
|
|
|
}
|
2021-05-16 14:01:34 +02:00
|
|
|
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
pathfinder->updatePaths(activeHeroes, cfg);
|
2024-01-20 22:54:30 +02:00
|
|
|
pathfinder->updateGraphs(activeHeroes);
|
2021-05-16 13:56:13 +02:00
|
|
|
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
objectClusterizer->clusterize();
|
|
|
|
}
|
2021-05-16 13:15:03 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
armyManager->update();
|
2021-05-16 13:56:13 +02:00
|
|
|
|
|
|
|
logAi->debug("AI state updated in %ld", timeElapsed(start));
|
2021-05-15 20:57:27 +02:00
|
|
|
}
|
|
|
|
|
2021-05-16 13:22:41 +02:00
|
|
|
bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
|
|
|
|
{
|
2021-05-16 13:22:41 +02:00
|
|
|
if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->getObjectName(), path.toString());
|
2021-05-16 13:22:41 +02:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:13:56 +02:00
|
|
|
for(auto & node : path.nodes)
|
|
|
|
{
|
2021-05-16 13:22:41 +02:00
|
|
|
auto lockReason = getHeroLockedReason(node.targetHero);
|
|
|
|
|
|
|
|
if(lockReason != HeroLockedReason::NOT_LOCKED)
|
|
|
|
{
|
2022-09-26 20:01:07 +02:00
|
|
|
#if NKAI_TRACE_LEVEL >= 1
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->getObjectName(), path.toString());
|
2021-05-16 13:22:41 +02:00
|
|
|
#endif
|
2021-05-16 13:13:56 +02:00
|
|
|
return true;
|
2021-05-16 13:22:41 +02:00
|
|
|
}
|
2021-05-16 13:13:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-16 13:22:41 +02:00
|
|
|
HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
|
|
|
|
{
|
|
|
|
auto found = lockedHeroes.find(hero);
|
|
|
|
|
|
|
|
return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:23:05 +02:00
|
|
|
void Nullkiller::makeTurn()
|
|
|
|
{
|
2022-12-14 22:13:26 +02:00
|
|
|
boost::lock_guard<boost::mutex> sharedStorageLock(AISharedStorage::locker);
|
|
|
|
|
2021-05-16 13:56:42 +02:00
|
|
|
const int MAX_DEPTH = 10;
|
2023-06-04 15:02:02 +02:00
|
|
|
const float FAST_TASK_MINIMAL_PRIORITY = 0.7f;
|
2021-05-16 13:56:42 +02:00
|
|
|
|
2021-05-15 20:57:27 +02:00
|
|
|
resetAiState();
|
|
|
|
|
2024-02-25 12:39:19 +02:00
|
|
|
for(int i = 1; i <= settings->getMaxPass(); i++)
|
2021-05-15 18:23:11 +02:00
|
|
|
{
|
2021-05-16 13:56:21 +02:00
|
|
|
updateAiState(i);
|
2021-05-15 18:23:42 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
Goals::TTask bestTask = taskptr(Goals::Invalid());
|
2023-07-27 14:58:49 +02:00
|
|
|
|
2024-02-25 12:39:19 +02:00
|
|
|
for(;i <= settings->getMaxPass(); i++)
|
2022-09-06 20:14:22 +02:00
|
|
|
{
|
|
|
|
Goals::TTaskVec fastTasks = {
|
|
|
|
choseBestTask(sptr(BuyArmyBehavior()), 1),
|
|
|
|
choseBestTask(sptr(BuildingBehavior()), 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
bestTask = choseBestTask(fastTasks);
|
|
|
|
|
2023-02-28 09:07:59 +02:00
|
|
|
if(bestTask->priority >= FAST_TASK_MINIMAL_PRIORITY)
|
2022-09-06 20:14:22 +02:00
|
|
|
{
|
|
|
|
executeTask(bestTask);
|
|
|
|
updateAiState(i, true);
|
|
|
|
}
|
2023-07-27 14:58:49 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 20:14:22 +02:00
|
|
|
|
2021-05-16 13:38:26 +02:00
|
|
|
Goals::TTaskVec bestTasks = {
|
2022-09-06 20:14:22 +02:00
|
|
|
bestTask,
|
2023-03-11 11:42:44 +02:00
|
|
|
choseBestTask(sptr(RecruitHeroBehavior()), 1),
|
2021-05-16 13:56:42 +02:00
|
|
|
choseBestTask(sptr(CaptureObjectsBehavior()), 1),
|
|
|
|
choseBestTask(sptr(ClusterBehavior()), MAX_DEPTH),
|
|
|
|
choseBestTask(sptr(DefenceBehavior()), MAX_DEPTH),
|
2023-09-24 12:07:42 +02:00
|
|
|
choseBestTask(sptr(GatherArmyBehavior()), MAX_DEPTH),
|
|
|
|
choseBestTask(sptr(StayAtTownBehavior()), MAX_DEPTH)
|
2021-05-15 18:23:11 +02:00
|
|
|
};
|
|
|
|
|
2021-05-15 21:02:52 +02:00
|
|
|
if(cb->getDate(Date::DAY) == 1)
|
|
|
|
{
|
2021-05-16 13:56:42 +02:00
|
|
|
bestTasks.push_back(choseBestTask(sptr(StartupBehavior()), 1));
|
2021-05-15 21:02:52 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
bestTask = choseBestTask(bestTasks);
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
std::string taskDescription = bestTask->toString();
|
2021-05-16 14:01:34 +02:00
|
|
|
HeroPtr hero = bestTask->getHero();
|
2022-09-06 20:14:22 +02:00
|
|
|
HeroRole heroRole = HeroRole::MAIN;
|
|
|
|
|
|
|
|
if(hero.validAndSet())
|
|
|
|
heroRole = heroManager->getHeroRole(hero);
|
|
|
|
|
|
|
|
if(heroRole != HeroRole::MAIN || bestTask->getHeroExchangeCount() <= 1)
|
|
|
|
useHeroChain = false;
|
|
|
|
|
2023-07-27 14:58:49 +02:00
|
|
|
// TODO: better to check turn distance here instead of priority
|
2023-02-28 09:07:59 +02:00
|
|
|
if((heroRole != HeroRole::MAIN || bestTask->priority < SMALL_SCAN_MIN_PRIORITY)
|
2023-07-27 14:58:49 +02:00
|
|
|
&& scanDepth == ScanDepth::MAIN_FULL)
|
2021-05-16 14:01:34 +02:00
|
|
|
{
|
2023-02-28 09:07:59 +02:00
|
|
|
useHeroChain = false;
|
|
|
|
scanDepth = ScanDepth::SMALL;
|
2021-05-16 14:08:56 +02:00
|
|
|
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->trace(
|
2023-07-27 14:58:49 +02:00
|
|
|
"Goal %s has low priority %f so decreasing scan depth to gain performance.",
|
2023-10-03 07:16:12 +02:00
|
|
|
taskDescription,
|
2023-02-28 09:07:59 +02:00
|
|
|
bestTask->priority);
|
2021-05-16 14:01:34 +02:00
|
|
|
}
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2021-05-16 13:19:00 +02:00
|
|
|
if(bestTask->priority < MIN_PRIORITY)
|
|
|
|
{
|
2023-07-27 14:58:49 +02:00
|
|
|
auto heroes = cb->getHeroesInfo();
|
|
|
|
auto hasMp = vstd::contains_if(heroes, [](const CGHeroInstance * h) -> bool
|
|
|
|
{
|
|
|
|
return h->movementPointsRemaining() > 100;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(hasMp && scanDepth != ScanDepth::ALL_FULL)
|
|
|
|
{
|
|
|
|
logAi->trace(
|
|
|
|
"Goal %s has too low priority %f so increasing scan depth to full.",
|
2023-10-03 07:16:12 +02:00
|
|
|
taskDescription,
|
2023-07-27 14:58:49 +02:00
|
|
|
bestTask->priority);
|
|
|
|
|
|
|
|
scanDepth = ScanDepth::ALL_FULL;
|
|
|
|
useHeroChain = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-10-03 07:16:12 +02:00
|
|
|
logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", taskDescription);
|
2021-05-16 13:19:00 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
executeTask(bestTask);
|
2023-08-05 12:49:49 +02:00
|
|
|
|
2024-02-25 12:39:19 +02:00
|
|
|
if(i == settings->getMaxPass())
|
2023-08-05 12:49:49 +02:00
|
|
|
{
|
2024-02-25 12:39:19 +02:00
|
|
|
logAi->warn("Goal %s exceeded maxpass. Terminating AI turn.", taskDescription);
|
2023-08-05 12:49:49 +02:00
|
|
|
}
|
2022-09-06 20:14:22 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-16 14:08:39 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
void Nullkiller::executeTask(Goals::TTask task)
|
|
|
|
{
|
2023-03-19 19:04:12 +02:00
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
2022-09-06 20:14:22 +02:00
|
|
|
std::string taskDescr = task->toString();
|
2021-05-15 18:23:11 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
boost::this_thread::interruption_point();
|
|
|
|
logAi->debug("Trying to realize %s (value %2.3f)", taskDescr, task->priority);
|
2021-05-15 18:23:05 +02:00
|
|
|
|
2022-09-06 20:14:22 +02:00
|
|
|
try
|
|
|
|
{
|
2023-07-31 16:00:37 +02:00
|
|
|
task->accept(ai);
|
2023-03-19 19:04:12 +02:00
|
|
|
logAi->trace("Task %s completed in %lld", taskDescr, timeElapsed(start));
|
2022-09-06 20:14:22 +02:00
|
|
|
}
|
|
|
|
catch(goalFulfilledException &)
|
|
|
|
{
|
2023-03-19 19:04:12 +02:00
|
|
|
logAi->trace("Task %s completed in %lld", taskDescr, timeElapsed(start));
|
2022-09-06 20:14:22 +02:00
|
|
|
}
|
2022-11-03 21:16:49 +02:00
|
|
|
catch(cannotFulfillGoalException & e)
|
2022-09-06 20:14:22 +02:00
|
|
|
{
|
2023-02-28 09:07:59 +02:00
|
|
|
logAi->error("Failed to realize subgoal of type %s, I will stop.", taskDescr);
|
|
|
|
logAi->error("The error message was: %s", e.what());
|
2022-09-06 20:14:22 +02:00
|
|
|
|
2023-02-28 09:07:59 +02:00
|
|
|
#if NKAI_TRACE_LEVEL == 0
|
|
|
|
throw; // will be recatched and AI turn ended
|
|
|
|
#endif
|
2021-05-15 18:23:11 +02:00
|
|
|
}
|
2021-05-16 14:39:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TResources Nullkiller::getFreeResources() const
|
|
|
|
{
|
|
|
|
auto freeRes = cb->getResourceAmount() - lockedResources;
|
|
|
|
|
|
|
|
freeRes.positive();
|
|
|
|
|
|
|
|
return freeRes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Nullkiller::lockResources(const TResources & res)
|
|
|
|
{
|
|
|
|
lockedResources += res;
|
2021-11-23 08:41:03 +02:00
|
|
|
}
|
2022-09-26 20:01:07 +02:00
|
|
|
|
|
|
|
}
|