Browse Source

#3173 - fix trap crash on random hero

Andrii Danylchenko 1 year ago
parent
commit
142d0083d5
2 changed files with 17 additions and 15 deletions
  1. 1 2
      lib/gameState/CGameState.cpp
  2. 16 13
      lib/mapObjects/CGHeroInstance.cpp

+ 1 - 2
lib/gameState/CGameState.cpp

@@ -121,8 +121,7 @@ HeroTypeID CGameState::pickUnusedHeroTypeRandomly(const PlayerColor & owner)
 		return *notAllowedHeroesButStillBetterThanCrash.begin();
 		return *notAllowedHeroesButStillBetterThanCrash.begin();
 
 
 	logGlobal->error("No free heroes at all!");
 	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
 int CGameState::getDate(Date mode) const

+ 16 - 13
lib/mapObjects/CGHeroInstance.cpp

@@ -1531,22 +1531,25 @@ std::string CGHeroInstance::getHeroTypeName() const
 
 
 void CGHeroInstance::afterAddToMap(CMap * map)
 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
+		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())
 		{
 		{
-			logGlobal->error("Hero is already on the map at %s", (*existingHero)->visitablePos().toString());
+			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");
+				throw std::runtime_error("Hero is already on the map");
+			}
 		}
 		}
 	}
 	}