From 1d79946825fb6799e418d4a63b941b9175b535cb Mon Sep 17 00:00:00 2001 From: Xilmi Date: Sun, 1 Dec 2024 23:00:35 +0100 Subject: [PATCH] Fix for AI not defending in some cases Instead of skipping the defense of other towns entirely when hiring a hero was considered as a way of defending, now only hiring the same hero twice will be avoided. --- AI/Nullkiller/Behaviors/DefenceBehavior.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/AI/Nullkiller/Behaviors/DefenceBehavior.cpp b/AI/Nullkiller/Behaviors/DefenceBehavior.cpp index 86bd7e77c..08875cb37 100644 --- a/AI/Nullkiller/Behaviors/DefenceBehavior.cpp +++ b/AI/Nullkiller/Behaviors/DefenceBehavior.cpp @@ -41,9 +41,6 @@ Goals::TGoalVec DefenceBehavior::decompose(const Nullkiller * ai) const for(auto town : ai->cb->getTownsInfo()) { evaluateDefence(tasks, town, ai); - //Let's do only one defence-task per pass since otherwise it can try to hire the same hero twice - if (!tasks.empty()) - break; } return tasks; @@ -422,6 +419,18 @@ void DefenceBehavior::evaluateRecruitingHero(Goals::TGoalVec & tasks, const HitM if(hero->getTotalStrength() < threat.danger) continue; + bool heroAlreadyHiredInOtherTown = false; + for (const auto& task : tasks) + { + if (auto recruitGoal = dynamic_cast(task.get())) + { + if (recruitGoal->getHero() == hero) + heroAlreadyHiredInOtherTown = true; + } + } + if (heroAlreadyHiredInOtherTown) + continue; + auto myHeroes = ai->cb->getHeroesInfo(); #if NKAI_TRACE_LEVEL >= 1