From 02b2f35143a909468043186372a47876d506403c Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Sat, 5 Mar 2022 15:51:03 +0200 Subject: [PATCH] Nullkiller: fix crash --- AI/Nullkiller/Behaviors/DefenceBehavior.cpp | 2 +- AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/AI/Nullkiller/Behaviors/DefenceBehavior.cpp b/AI/Nullkiller/Behaviors/DefenceBehavior.cpp index 2c95d1b5c..88263ecb1 100644 --- a/AI/Nullkiller/Behaviors/DefenceBehavior.cpp +++ b/AI/Nullkiller/Behaviors/DefenceBehavior.cpp @@ -65,7 +65,7 @@ void DefenceBehavior::evaluateDefence(Goals::TGoalVec & tasks, const CGTownInsta { if(!ai->nullkiller->isHeroLocked(town->garrisonHero.get())) { - if(!town->visitingHero) + if(!town->visitingHero && cb->getHeroesInfo().size() < GameConstants::MAX_HEROES_PER_PLAYER) { tasks.push_back(Goals::sptr(Goals::ExchangeSwapTownHeroes(town, nullptr).setpriority(5))); } diff --git a/AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp b/AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp index f35213a37..0b570d975 100644 --- a/AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp +++ b/AI/Nullkiller/Goals/ExchangeSwapTownHeroes.cpp @@ -42,13 +42,22 @@ void ExchangeSwapTownHeroes::accept(AIGateway * ai) { if(!garrisonHero) { - if(!town->garrisonHero) + auto currentGarrisonHero = town->garrisonHero; + + if(!currentGarrisonHero) throw cannotFulfillGoalException("Invalid configuration. There is no hero in town garrison."); cb->swapGarrisonHero(town); + + if(currentGarrisonHero.get() != town->visitingHero.get()) + { + logAi->error("VisitingHero is empty, expected %s", currentGarrisonHero->name); + return; + } + ai->buildArmyIn(town); - ai->nullkiller->unlockHero(town->visitingHero.get()); - logAi->debug("Extracted hero %s from garrison of %s", town->visitingHero->name, town->name); + ai->nullkiller->unlockHero(currentGarrisonHero.get()); + logAi->debug("Extracted hero %s from garrison of %s", currentGarrisonHero->name, town->name); return; }