From 142d0083d519c40fdffcb4212e3cf018d16e4c2a Mon Sep 17 00:00:00 2001 From: Andrii Danylchenko Date: Sun, 12 Nov 2023 16:14:06 +0200 Subject: [PATCH] #3173 - fix trap crash on random hero --- lib/gameState/CGameState.cpp | 3 +-- lib/mapObjects/CGHeroInstance.cpp | 31 +++++++++++++++++-------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/gameState/CGameState.cpp b/lib/gameState/CGameState.cpp index beb8eeda4..0a70046fd 100644 --- a/lib/gameState/CGameState.cpp +++ b/lib/gameState/CGameState.cpp @@ -121,8 +121,7 @@ HeroTypeID CGameState::pickUnusedHeroTypeRandomly(const PlayerColor & owner) return *notAllowedHeroesButStillBetterThanCrash.begin(); logGlobal->error("No free heroes at all!"); - assert(0); //current code can't handle this situation - return HeroTypeID::NONE; // no available heroes at all + throw std::runtime_error("Can not allocate hero. All heroes are already used."); } int CGameState::getDate(Date mode) const diff --git a/lib/mapObjects/CGHeroInstance.cpp b/lib/mapObjects/CGHeroInstance.cpp index 6fa16d8ce..8e0eec91f 100644 --- a/lib/mapObjects/CGHeroInstance.cpp +++ b/lib/mapObjects/CGHeroInstance.cpp @@ -1531,22 +1531,25 @@ std::string CGHeroInstance::getHeroTypeName() const void CGHeroInstance::afterAddToMap(CMap * map) { - auto existingHero = std::find_if(map->objects.begin(), map->objects.end(), [&](const CGObjectInstance * o) ->bool - { - return o && (o->ID == Obj::HERO || o->ID == Obj::PRISON) && o->subID == subID && o != this; - }); - - if(existingHero != map->objects.end()) + if(ID != Obj::RANDOM_HERO) { - if(settings["session"]["editor"].Bool()) - { - logGlobal->warn("Hero is already on the map at %s", (*existingHero)->visitablePos().toString()); - } - else - { - logGlobal->error("Hero is already on the map at %s", (*existingHero)->visitablePos().toString()); + auto existingHero = std::find_if(map->objects.begin(), map->objects.end(), [&](const CGObjectInstance * o) ->bool + { + return o && (o->ID == Obj::HERO || o->ID == Obj::PRISON) && o->subID == subID && o != this; + }); - throw std::runtime_error("Hero is already on the map"); + if(existingHero != map->objects.end()) + { + if(settings["session"]["editor"].Bool()) + { + logGlobal->warn("Hero is already on the map at %s", (*existingHero)->visitablePos().toString()); + } + else + { + logGlobal->error("Hero is already on the map at %s", (*existingHero)->visitablePos().toString()); + + throw std::runtime_error("Hero is already on the map"); + } } }