Răsfoiți Sursa

- Refactoring legacy code
- Some checks for #2311

DjWarmonger 10 ani în urmă
părinte
comite
abe88ea890
2 a modificat fișierele cu 19 adăugiri și 14 ștergeri
  1. 6 0
      lib/NetPacksLib.cpp
  2. 13 14
      server/CGameHandler.cpp

+ 6 - 0
lib/NetPacksLib.cpp

@@ -468,6 +468,12 @@ static int getDir(int3 src, int3 dst)
 void TryMoveHero::applyGs( CGameState *gs )
 {
 	CGHeroInstance *h = gs->getHero(id);
+	if (!h)
+	{
+		logGlobal->errorStream() << "Attempt ot move unavailable hero " << id;
+		return;
+	}
+
 	h->movement = movePoints;
 
 	if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end)

+ 13 - 14
server/CGameHandler.cpp

@@ -4772,42 +4772,41 @@ void CGameHandler::checkVictoryLossConditionsForPlayer(PlayerColor player)
 		else
 		{
 			//player lost -> all his objects become unflagged (neutral)
-			auto hlp = p->heroes;
-			for (auto i = hlp.cbegin(); i != hlp.cend(); i++) //eliminate heroes
-				removeObject(*i);
+			for (auto h : p->heroes) //eliminate heroes
+				if (h.get())
+					removeObject(h);
 
-			for (auto i = gs->map->objects.cbegin(); i != gs->map->objects.cend(); i++) //unflag objs
+			for (auto obj : gs->map->objects) //unflag objs
 			{
-				if(*i  &&  (*i)->tempOwner == player)
-					setOwner(*i,PlayerColor::NEUTRAL);
+				if(obj.get() && obj->tempOwner == player)
+					setOwner(obj, PlayerColor::NEUTRAL);
 			}
 
 			//eliminating one player may cause victory of another:
 			std::set<PlayerColor> playerColors;
-			for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
+			for (auto p : gs->players) //FIXME: players may have different colors, iterate by over players and not integers
 			{
-				if(player.getNum() != i) playerColors.insert(PlayerColor(i));
+				if (p.first != player)
+					playerColors.insert(p.first);
 			}
 
 			//notify all players
-			for (auto i = gs->players.cbegin(); i!=gs->players.cend(); i++)
+			for (auto pc : playerColors)
 			{
-				if(i->first != player && gs->getPlayer(i->first)->status == EPlayerStatus::INGAME)
+				if (gs->getPlayer(pc)->status == EPlayerStatus::INGAME)
 				{
 					InfoWindow iw;
 					getVictoryLossMessage(player, victoryLossCheckResult.invert(), iw);
-					iw.player = i->first;
+					iw.player = pc;
 					sendAndApply(&iw);
 				}
 			}
-
-
 			checkVictoryLossConditions(playerColors);
 		}
 
 		auto playerInfo = gs->getPlayer(gs->currentPlayer, false);
 		// If we are called before the actual game start, there might be no current player
-		if(playerInfo && playerInfo->status != EPlayerStatus::INGAME)
+		if (playerInfo && playerInfo->status != EPlayerStatus::INGAME)
 		{
 			// If player making turn has lost his turn must be over as well
 			states.setFlag(gs->currentPlayer, &PlayerStatus::makingTurn, false);