From 0265de77fad8cc0ff7e8e9426d7ef5b600e0e745 Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Sun, 16 May 2021 15:08:39 +0300 Subject: [PATCH] Nullkiller: fix crash, refactor --- AI/Nullkiller/Engine/Nullkiller.cpp | 6 +++-- AI/Nullkiller/Pathfinding/AINodeStorage.cpp | 5 ++++- AI/Nullkiller/Pathfinding/Actors.cpp | 25 ++++++++++++--------- AI/Nullkiller/Pathfinding/Actors.h | 2 ++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/AI/Nullkiller/Engine/Nullkiller.cpp b/AI/Nullkiller/Engine/Nullkiller.cpp index 2afa9084b..bc4a657ea 100644 --- a/AI/Nullkiller/Engine/Nullkiller.cpp +++ b/AI/Nullkiller/Engine/Nullkiller.cpp @@ -243,8 +243,10 @@ void Nullkiller::makeTurn() return; } + std::string taskDescr = bestTask->toString(); + boost::this_thread::interruption_point(); - logAi->debug("Trying to realize %s (value %2.3f)", bestTask->toString(), bestTask->priority); + logAi->debug("Trying to realize %s (value %2.3f)", taskDescr, bestTask->priority); try { @@ -256,7 +258,7 @@ void Nullkiller::makeTurn() } catch(std::exception & e) { - logAi->debug("Failed to realize subgoal of type %s, I will stop.", bestTask->toString()); + logAi->debug("Failed to realize subgoal of type %s, I will stop.", taskDescr); logAi->debug("The error message was: %s", e.what()); return; diff --git a/AI/Nullkiller/Pathfinding/AINodeStorage.cpp b/AI/Nullkiller/Pathfinding/AINodeStorage.cpp index db43a9cd8..111b778ac 100644 --- a/AI/Nullkiller/Pathfinding/AINodeStorage.cpp +++ b/AI/Nullkiller/Pathfinding/AINodeStorage.cpp @@ -30,6 +30,9 @@ const uint64_t FirstActorMask = 1; const int BUCKET_COUNT = 11; const int BUCKET_SIZE = GameConstants::MAX_HEROES_PER_PLAYER; const int NUM_CHAINS = BUCKET_COUNT * BUCKET_SIZE; +const uint64_t MIN_ARMY_STRENGTH_FOR_CHAIN = 5000; +const uint64_t MIN_ARMY_STRENGTH_FOR_NEXT_ACTOR = 1000; + AISharedStorage::AISharedStorage(int3 sizes) { @@ -535,7 +538,7 @@ bool AINodeStorage::selectNextActor() if(nextActor != actors.end()) { - if(nextActor->get()->armyValue < 1000) + if(nextActor->get()->armyValue < MIN_ARMY_STRENGTH_FOR_NEXT_ACTOR) return false; chainMask = nextActor->get()->chainMask; diff --git a/AI/Nullkiller/Pathfinding/Actors.cpp b/AI/Nullkiller/Pathfinding/Actors.cpp index 1e239456d..d9c88c749 100644 --- a/AI/Nullkiller/Pathfinding/Actors.cpp +++ b/AI/Nullkiller/Pathfinding/Actors.cpp @@ -93,11 +93,11 @@ HeroActor::HeroActor(const CGHeroInstance * hero, HeroRole heroRole, uint64_t ch } HeroActor::HeroActor( - const ChainActor * carrier, - const ChainActor * other, - const HeroExchangeArmy * army, + const ChainActor * carrier, + const ChainActor * other, + const HeroExchangeArmy * army, const Nullkiller * ai) - :ChainActor(carrier, other, army) + :ChainActor(carrier, other, army) { exchangeMap.reset(new HeroExchangeMap(this, ai)); armyCost += army->armyCost; @@ -124,7 +124,7 @@ void ChainActor::setBaseActor(HeroActor * base) void HeroActor::setupSpecialActors() { - auto allActors = std::vector{ this }; + auto allActors = std::vector{this}; for(ChainActor & specialActor : specialActors) { @@ -240,6 +240,9 @@ HeroActor * HeroExchangeMap::tryExchange(const ChainActor * other) return nullptr; } + if(other->isMovable && other->armyValue <= actor->armyValue / 10 && other->armyValue < MIN_ARMY_STRENGTH_FOR_CHAIN) + return nullptr; + TResources availableResources = resources - actor->armyCost - other->armyCost; HeroExchangeArmy * upgradedInitialArmy = tryUpgrade(actor->creatureSet, other->getActorObject(), availableResources); HeroExchangeArmy * newArmy; @@ -277,7 +280,7 @@ HeroActor * HeroExchangeMap::tryExchange(const ChainActor * other) 100.0f * reinforcement / actor->armyValue); #endif - if(reinforcement <= actor->armyValue / 10 && reinforcement < 1000) + if(reinforcement <= actor->armyValue / 10 && reinforcement < MIN_ARMY_STRENGTH_FOR_CHAIN) { delete newArmy; @@ -367,10 +370,10 @@ HillFortActor::HillFortActor(const CGObjectInstance * hillFort, uint64_t chainMa } DwellingActor::DwellingActor(const CGDwelling * dwelling, uint64_t chainMask, bool waitForGrowth, int dayOfWeek) - :ObjectActor( - dwelling, - getDwellingCreatures(dwelling, waitForGrowth), - chainMask, + : ObjectActor( + dwelling, + getDwellingCreatures(dwelling, waitForGrowth), + chainMask, getInitialTurn(waitForGrowth, dayOfWeek)), dwelling(dwelling) { @@ -409,7 +412,7 @@ CCreatureSet * DwellingActor::getDwellingCreatures(const CGDwelling * dwelling, auto creature = creatureInfo.second.back().toCreature(); auto count = creatureInfo.first; - + if(waitForGrowth) { const CGTownInstance * town = dynamic_cast(dwelling); diff --git a/AI/Nullkiller/Pathfinding/Actors.h b/AI/Nullkiller/Pathfinding/Actors.h index 3701a1d81..e3fed3981 100644 --- a/AI/Nullkiller/Pathfinding/Actors.h +++ b/AI/Nullkiller/Pathfinding/Actors.h @@ -15,6 +15,8 @@ #include "../AIUtility.h" #include "Actions/SpecialAction.h" +extern const uint64_t MIN_ARMY_STRENGTH_FOR_CHAIN; + class HeroActor; class Nullkiller;