Browse Source

Fixed crash at month of double population.

doublingCreatures vector was never properly initialized.
DjWarmonger 12 years ago
parent
commit
57ea1ed1b8
2 changed files with 8 additions and 6 deletions
  1. 6 3
      lib/CCreatureHandler.cpp
  2. 2 3
      server/CGameHandler.cpp

+ 6 - 3
lib/CCreatureHandler.cpp

@@ -342,6 +342,11 @@ void CCreatureHandler::loadObject(std::string scope, std::string name, const Jso
 	object->idNumber = CreatureID(index);
 	object->iconIndex = object->idNumber + 2;
 
+	if(data["hasDoubleWeek"].Bool()) //
+	{
+		doubledCreatures.insert (object->idNumber); //we need to have id (or identifier) before it is inserted
+	}
+
 	assert(creatures[index] == nullptr); // ensure that this id was not loaded before
 	creatures[index] = object;
 
@@ -635,9 +640,6 @@ void CCreatureHandler::loadCreatureJson(CCreature * creature, const JsonNode & c
 		});
 	}
 
-	if(config["hasDoubleWeek"].Bool())
-		doubledCreatures.insert(creature->idNumber);
-
 	creature->animation.projectileImageName = config["graphics"]["missile"]["projectile"].String();
 
 	creature->special = config["special"].Bool() || config["disabled"].Bool();
@@ -1037,6 +1039,7 @@ CreatureID CCreatureHandler::pickRandomMonster(const boost::function<int()> &ran
 
 		return vstd::pickRandomElementOf(allowed, randGen);
 	}
+	assert (r >= 0); //should always be, but it crashed
 	return CreatureID(r);
 }
 

+ 2 - 3
server/CGameHandler.cpp

@@ -1127,9 +1127,8 @@ void CGameHandler::newTurn()
 					}
 					else if(VLC->creh->doubledCreatures.size())
 					{
-						auto it = VLC->creh->doubledCreatures.cbegin();
-						std::advance (it, rand() % VLC->creh->doubledCreatures.size()); //picking random element of set is tiring
-						n.creatureid = *it;
+						const std::vector<CreatureID> doubledCreatures (VLC->creh->doubledCreatures.begin(), VLC->creh->doubledCreatures.end());
+						n.creatureid = vstd::pickRandomElementOf (doubledCreatures, boost::ref(rand));
 					}
 					else
 					{