From 62935b4ba4f5ad65e7a71cecb932fffdbb49bcdd Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Sat, 20 Jan 2024 17:15:51 +0200 Subject: [PATCH] Fix crash on using StupidAI for player actions --- AI/StupidAI/StupidAI.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/AI/StupidAI/StupidAI.cpp b/AI/StupidAI/StupidAI.cpp index 3888e5a6b..b24cf3285 100644 --- a/AI/StupidAI/StupidAI.cpp +++ b/AI/StupidAI/StupidAI.cpp @@ -16,8 +16,6 @@ #include "../../lib/battle/BattleAction.h" #include "../../lib/battle/BattleInfo.h" -static std::shared_ptr cbc; - CStupidAI::CStupidAI() : side(-1) , wasWaitingForRealize(false) @@ -41,7 +39,7 @@ void CStupidAI::initBattleInterface(std::shared_ptr ENV, std::share { print("init called, saving ptr to IBattleCallback"); env = ENV; - cbc = cb = CB; + cb = CB; wasWaitingForRealize = CB->waitTillRealize; wasUnlockingGs = CB->unlockGsWhenWaiting; @@ -72,11 +70,11 @@ public: std::vector attackFrom; //for melee fight EnemyInfo(const CStack * _s) : s(_s), adi(0), adr(0) {} - void calcDmg(const BattleID & battleID, const CStack * ourStack) + void calcDmg(std::shared_ptr cb, const BattleID & battleID, const CStack * ourStack) { // FIXME: provide distance info for Jousting bonus DamageEstimation retal; - DamageEstimation dmg = cbc->getBattle(battleID)->battleEstimateDamage(ourStack, s, 0, &retal); + DamageEstimation dmg = cb->getBattle(battleID)->battleEstimateDamage(ourStack, s, 0, &retal); adi = static_cast((dmg.damage.min + dmg.damage.max) / 2); adr = static_cast((retal.damage.min + retal.damage.max) / 2); } @@ -92,14 +90,14 @@ bool isMoreProfitable(const EnemyInfo &ei1, const EnemyInfo& ei2) return (ei1.adi-ei1.adr) < (ei2.adi - ei2.adr); } -static bool willSecondHexBlockMoreEnemyShooters(const BattleID & battleID, const BattleHex &h1, const BattleHex &h2) +static bool willSecondHexBlockMoreEnemyShooters(std::shared_ptr cb, const BattleID & battleID, const BattleHex &h1, const BattleHex &h2) { int shooters[2] = {0}; //count of shooters on hexes for(int i = 0; i < 2; i++) { for (auto & neighbour : (i ? h2 : h1).neighbouringTiles()) - if(const auto * s = cbc->getBattle(battleID)->battleGetUnitByPos(neighbour)) + if(const auto * s = cb->getBattle(battleID)->battleGetUnitByPos(neighbour)) if(s->isShooter()) shooters[i]++; } @@ -169,10 +167,10 @@ void CStupidAI::activeStack(const BattleID & battleID, const CStack * stack) } for ( auto & enemy : enemiesReachable ) - enemy.calcDmg(battleID, stack); + enemy.calcDmg(cb, battleID, stack); for ( auto & enemy : enemiesShootable ) - enemy.calcDmg(battleID, stack); + enemy.calcDmg(cb, battleID, stack); if(enemiesShootable.size()) { @@ -183,7 +181,7 @@ void CStupidAI::activeStack(const BattleID & battleID, const CStack * stack) else if(enemiesReachable.size()) { const EnemyInfo &ei= *std::max_element(enemiesReachable.begin(), enemiesReachable.end(), &isMoreProfitable); - BattleHex targetHex = *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), [&](auto a, auto b) { return willSecondHexBlockMoreEnemyShooters(battleID, a, b);}); + BattleHex targetHex = *std::max_element(ei.attackFrom.begin(), ei.attackFrom.end(), [&](auto a, auto b) { return willSecondHexBlockMoreEnemyShooters(cb, battleID, a, b);}); cb->battleMakeUnitAction(battleID, BattleAction::makeMeleeAttack(stack, ei.s->getPosition(), targetHex)); return;