From abe88ea89050e1af2f0dc8963068cdbd626a43fe Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sat, 24 Oct 2015 15:09:46 +0200 Subject: [PATCH] - Refactoring legacy code - Some checks for #2311 --- lib/NetPacksLib.cpp | 6 ++++++ server/CGameHandler.cpp | 27 +++++++++++++-------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/NetPacksLib.cpp b/lib/NetPacksLib.cpp index 1dca7243d..88bdac197 100644 --- a/lib/NetPacksLib.cpp +++ b/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) diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 429c7fb8a..ee5f5f436 100644 --- a/server/CGameHandler.cpp +++ b/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 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);