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

297 lines
7.0 KiB
C++
Raw Normal View History

/*
* 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
*
*/
#include "StdInc.h"
#include "Nullkiller.h"
2021-05-16 14:39:38 +02:00
#include "../AIGateway.h"
#include "../Behaviors/CaptureObjectsBehavior.h"
#include "../Behaviors/RecruitHeroBehavior.h"
#include "../Behaviors/BuyArmyBehavior.h"
#include "../Behaviors/StartupBehavior.h"
2021-05-16 12:53:32 +02:00
#include "../Behaviors/DefenceBehavior.h"
#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"
#include "../Goals/Invalid.h"
#include "../Goals/Composition.h"
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;
#if AI_TRACE_LEVEL >= 1
#define MAXPASS 1000000
#else
#define MAXPASS 30
#endif
Nullkiller::Nullkiller()
{
memory.reset(new AIMemory());
}
void Nullkiller::init(std::shared_ptr<CCallback> cb, PlayerColor playerID)
{
this->cb = cb;
this->playerID = playerID;
priorityEvaluator.reset(new PriorityEvaluator(this));
priorityEvaluators.reset(
new SharedPool<PriorityEvaluator>(
[&]()->std::unique_ptr<PriorityEvaluator>
{
return make_unique<PriorityEvaluator>(this);
}));
dangerHitMap.reset(new DangerHitMapAnalyzer(this));
2021-05-16 13:57:33 +02:00
buildAnalyzer.reset(new BuildAnalyzer(this));
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));
decomposer.reset(new DeepDecomposer());
}
Goals::TTask Nullkiller::choseBestTask(Goals::TTaskVec & tasks) const
{
Goals::TTask bestTask = *vstd::maxElementByFun(tasks, [](Goals::TTask task) -> float{
return task->priority;
});
return bestTask;
}
Goals::TTask Nullkiller::choseBestTask(Goals::TSubgoal behavior, int decompositionMaxDepth) const
{
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();
Goals::TGoalVec elementarGoals = decomposer->decompose(behavior, decompositionMaxDepth);
Goals::TTaskVec tasks;
2021-05-16 13:57:33 +02:00
boost::this_thread::interruption_point();
for(auto goal : elementarGoals)
{
Goals::TTask task = Goals::taskptr(*goal);
2021-05-16 13:38:53 +02:00
if(task->priority <= 0)
task->priority = priorityEvaluator->evaluate(goal);
tasks.push_back(task);
}
2021-05-16 13:38:53 +02:00
if(tasks.empty())
{
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());
}
auto task = choseBestTask(tasks);
logAi->debug(
"Behavior %s returns %s, priority %f. Time taken %ld",
behavior->toString(),
task->toString(),
task->priority,
timeElapsed(start));
return task;
}
void Nullkiller::resetAiState()
{
2021-05-16 14:39:38 +02:00
lockedResources = TResources();
2021-05-16 14:01:34 +02:00
scanDepth = ScanDepth::SMALL;
playerID = ai->playerID;
lockedHeroes.clear();
dangerHitMap->reset();
}
void Nullkiller::updateAiState(int pass)
{
2021-05-16 13:57:33 +02:00
boost::this_thread::interruption_point();
2021-11-23 08:41:03 +02:00
auto start = std::chrono::high_resolution_clock::now();
activeHero = nullptr;
memory->removeInvisibleObjects(cb.get());
dangerHitMap->updateHitMap();
2021-05-16 13:57:33 +02:00
boost::this_thread::interruption_point();
heroManager->update();
logAi->trace("Updating paths");
std::map<const CGHeroInstance *, HeroRole> activeHeroes;
for(auto hero : cb->getHeroesInfo())
{
if(getHeroLockedReason(hero) == HeroLockedReason::DEFENCE)
continue;
activeHeroes[hero] = heroManager->getHeroRole(hero);
}
PathfinderSettings cfg;
cfg.useHeroChain = true;
cfg.scoutTurnDistanceLimit = SCOUT_TURN_DISTANCE_LIMIT;
2021-05-16 14:01:34 +02:00
if(scanDepth != ScanDepth::FULL)
{
cfg.mainTurnDistanceLimit = MAIN_TURN_DISTANCE_LIMIT * ((int)scanDepth + 1);
}
pathfinder->updatePaths(activeHeroes, cfg);
armyManager->update();
2021-05-16 13:45:12 +02:00
objectClusterizer->clusterize();
buildAnalyzer->update();
decomposer->reset();
logAi->debug("AI state updated in %ld", timeElapsed(start));
}
bool Nullkiller::isHeroLocked(const CGHeroInstance * hero) const
{
return getHeroLockedReason(hero) != HeroLockedReason::NOT_LOCKED;
}
bool Nullkiller::arePathHeroesLocked(const AIPath & path) const
{
if(getHeroLockedReason(path.targetHero) == HeroLockedReason::STARTUP)
{
#if AI_TRACE_LEVEL >= 1
logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
#endif
return true;
}
for(auto & node : path.nodes)
{
auto lockReason = getHeroLockedReason(node.targetHero);
if(lockReason != HeroLockedReason::NOT_LOCKED)
{
#if AI_TRACE_LEVEL >= 1
logAi->trace("Hero %s is locked by STARTUP. Discarding %s", path.targetHero->name, path.toString());
#endif
return true;
}
}
return false;
}
HeroLockedReason Nullkiller::getHeroLockedReason(const CGHeroInstance * hero) const
{
auto found = lockedHeroes.find(hero);
return found != lockedHeroes.end() ? found->second : HeroLockedReason::NOT_LOCKED;
}
void Nullkiller::makeTurn()
{
const int MAX_DEPTH = 10;
resetAiState();
for(int i = 1; i <= MAXPASS; i++)
{
updateAiState(i);
2021-05-15 18:23:42 +02:00
Goals::TTaskVec bestTasks = {
choseBestTask(sptr(BuyArmyBehavior()), 1),
choseBestTask(sptr(CaptureObjectsBehavior()), 1),
choseBestTask(sptr(ClusterBehavior()), MAX_DEPTH),
choseBestTask(sptr(RecruitHeroBehavior()), 1),
choseBestTask(sptr(DefenceBehavior()), MAX_DEPTH),
choseBestTask(sptr(BuildingBehavior()), 1),
choseBestTask(sptr(GatherArmyBehavior()), MAX_DEPTH)
};
2021-05-15 21:02:52 +02:00
if(cb->getDate(Date::DAY) == 1)
{
bestTasks.push_back(choseBestTask(sptr(StartupBehavior()), 1));
2021-05-15 21:02:52 +02:00
}
Goals::TTask bestTask = choseBestTask(bestTasks);
2021-05-16 14:01:34 +02:00
HeroPtr hero = bestTask->getHero();
if(bestTask->priority < NEXT_SCAN_MIN_PRIORITY
&& scanDepth != ScanDepth::FULL)
{
HeroRole heroRole = HeroRole::MAIN;
if(hero.validAndSet())
heroRole = heroManager->getHeroRole(hero);
2021-05-16 14:39:38 +02:00
if(heroRole == HeroRole::MAIN || bestTask->priority < MIN_PRIORITY)
{
logAi->trace(
"Goal %s has too low priority %f so increasing scan depth",
bestTask->toString(),
bestTask->priority);
scanDepth = (ScanDepth)((int)scanDepth + 1);
continue;
}
2021-05-16 14:01:34 +02:00
}
if(bestTask->priority < MIN_PRIORITY)
{
logAi->trace("Goal %s has too low priority. It is not worth doing it. Ending turn.", bestTask->toString());
return;
}
2021-05-16 14:08:39 +02:00
std::string taskDescr = bestTask->toString();
2021-05-16 13:57:33 +02:00
boost::this_thread::interruption_point();
2021-05-16 14:08:39 +02:00
logAi->debug("Trying to realize %s (value %2.3f)", taskDescr, bestTask->priority);
try
{
bestTask->accept(ai.get());
}
catch(goalFulfilledException &)
{
logAi->trace("Task %s completed", bestTask->toString());
}
2021-05-16 13:22:37 +02:00
catch(std::exception & e)
{
2021-05-16 14:08:39 +02:00
logAi->debug("Failed to realize subgoal of type %s, I will stop.", taskDescr);
logAi->debug("The error message was: %s", e.what());
return;
2021-05-16 13:22:37 +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
}