From 69925fb88701b60348d70dcb40f4896104a6295d Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 19 Feb 2013 04:53:24 +0000 Subject: [PATCH] - bugfixes for joining network games --- source/glest_game/ai/path_finder.cpp | 11 +++ source/glest_game/ai/path_finder.h | 1 + source/glest_game/game/game.cpp | 81 ++++++++++++++--------- source/glest_game/game/game.h | 1 + source/glest_game/type_instances/unit.cpp | 15 +++-- source/glest_game/world/unit_updater.cpp | 6 ++ source/glest_game/world/unit_updater.h | 2 + source/glest_game/world/world.cpp | 2 + 8 files changed, 83 insertions(+), 36 deletions(-) diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index f9d42497..27bd4a27 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -96,6 +96,17 @@ PathFinder::~PathFinder() { factionMutex = NULL; } +void PathFinder::clearCaches() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(factionMutex,mutexOwnerId); + + for(int i = 0; i < GameConstants::maxPlayers; ++i) { + factions[i].precachedTravelState.clear(); + factions[i].precachedPath.clear(); + factions[i].badCellList.clear(); + } +} + void PathFinder::clearUnitPrecache(Unit *unit) { static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); MutexSafeWrapper safeMutex(factionMutex,mutexOwnerId); diff --git a/source/glest_game/ai/path_finder.h b/source/glest_game/ai/path_finder.h index af0900c0..68d854e6 100644 --- a/source/glest_game/ai/path_finder.h +++ b/source/glest_game/ai/path_finder.h @@ -162,6 +162,7 @@ public: TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL,int frameIndex=-1); void clearUnitPrecache(Unit *unit); void removeUnitPrecache(Unit *unit); + void clearCaches(); int findNodeIndex(Node *node, Nodes &nodeList); int findNodeIndex(Node *node, std::vector &nodeList); diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index fb0ed913..c35ec3c8 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -79,6 +79,7 @@ Game::Game() : ProgramState(NULL) { avgRenderFps=0; currentAvgRenderFpsTotal=0; paused=false; + pauseStateChanged=false; gameOver=false; renderNetworkStatus=false; showFullConsole=false; @@ -233,6 +234,7 @@ void Game::resetMembers() { currentAvgRenderFpsTotal=0; tickCount=0; paused= false; + pauseStateChanged=false; gameOver= false; renderNetworkStatus= false; speed= 1; @@ -1990,6 +1992,47 @@ void Game::update() { //return; } } + //else if(server->getPauseForInGameConnection() == true && paused == true && + else if(paused == true && pauseStateChanged == true) { + pauseStateChanged = false; + + //NetworkManager &networkManager= NetworkManager::getInstance(); + //NetworkRole role = networkManager.getNetworkRole(); + + //if(role == nrServer) { + bool saveNetworkGame = false; + + ServerInterface *server = NetworkManager::getInstance().getServerInterface(); + for(int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + ConnectionSlot *slot = server->getSlot(faction->getStartLocationIndex()); + if(slot != NULL && slot->getJoinGameInProgress() == true) { + saveNetworkGame = true; + break; + } + } + + if(saveNetworkGame == true) { + //printf("Saved network game to disk\n"); + + string file = this->saveGame(GameConstants::saveNetworkGameFileServer,"temp/"); + char szBuf[8096]=""; + Lang &lang= Lang::getInstance(); + snprintf(szBuf,8096,lang.get("GameSaved","",true).c_str(),file.c_str()); + console.addLine(szBuf); + + for(int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + ConnectionSlot *slot = server->getSlot(faction->getStartLocationIndex()); + if(slot != NULL && slot->getJoinGameInProgress() == true) { + + NetworkMessageReady networkMessageReady(0); + slot->sendMessage(&networkMessageReady); + } + } + } + //} + } else if(server->getUnPauseForInGameConnection() == true && paused == true) { //printf("^^^ getUnPauseForInGameConnection triggered!\n"); @@ -5193,6 +5236,7 @@ void Game::setPaused(bool value,bool forceAllowPauseStateChange,bool clearCaches if(value == false) { console.addLine(lang.get("GameResumed")); paused= false; + pauseStateChanged = true; if(clearCaches == true) { world.clearCaches(); @@ -5206,41 +5250,14 @@ void Game::setPaused(bool value,bool forceAllowPauseStateChange,bool clearCaches else { console.addLine(lang.get("GamePaused")); paused= true; + pauseStateChanged = true; + //!!! - NetworkManager &networkManager= NetworkManager::getInstance(); - NetworkRole role = networkManager.getNetworkRole(); - - if(role == nrServer) { - bool saveNetworkGame = false; - - ServerInterface *server = NetworkManager::getInstance().getServerInterface(); + if(clearCaches == true) { + world.clearCaches(); for(int i = 0; i < world.getFactionCount(); ++i) { Faction *faction = world.getFaction(i); - ConnectionSlot *slot = server->getSlot(faction->getStartLocationIndex()); - if(slot != NULL && slot->getJoinGameInProgress() == true) { - saveNetworkGame = true; - break; - } - } - - if(saveNetworkGame == true) { - //printf("Saved network game to disk\n"); - - string file = this->saveGame(GameConstants::saveNetworkGameFileServer,"temp/"); - char szBuf[8096]=""; - Lang &lang= Lang::getInstance(); - snprintf(szBuf,8096,lang.get("GameSaved","",true).c_str(),file.c_str()); - console.addLine(szBuf); - - for(int i = 0; i < world.getFactionCount(); ++i) { - Faction *faction = world.getFaction(i); - ConnectionSlot *slot = server->getSlot(faction->getStartLocationIndex()); - if(slot != NULL && slot->getJoinGameInProgress() == true) { - - NetworkMessageReady networkMessageReady(0); - slot->sendMessage(&networkMessageReady); - } - } + faction->clearCaches(); } } } diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index b98a773c..5b621230 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -90,6 +90,7 @@ private: int totalRenderFps, renderFps, lastRenderFps, avgRenderFps,currentAvgRenderFpsTotal; uint64 tickCount; bool paused; + bool pauseStateChanged; bool gameOver; bool renderNetworkStatus; bool showFullConsole; diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 7d78f7eb..c4e71f90 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -4134,10 +4134,15 @@ Unit * Unit::loadGame(const XmlNode *rootNode, GameSettings *settings, Faction * result->rotation = unitNode->getAttribute("rotation")->getFloatValue(); //world->placeUnitAtLocation(newUnitPos, generationArea, unit, true); - result->setPos(newUnitPos); - Vec2i meetingPos = newUnitPos-Vec2i(1); - result->setMeetingPos(meetingPos); - + //result->setPos(newUnitPos); + //Vec2i meetingPos = newUnitPos-Vec2i(1); + //result->setMeetingPos(meetingPos); + result->pos = newUnitPos; + result->lastPos = Vec2i::strToVec2(unitNode->getAttribute("lastPos")->getValue()); + result->meetingPos = Vec2i::strToVec2(unitNode->getAttribute("meetingPos")->getValue()); + // Attempt to improve performance + //result->exploreCells(); + //result->calculateFogOfWarRadius(); // -------------------------- result->hp = unitNode->getAttribute("hp")->getIntValue(); @@ -4511,6 +4516,8 @@ Unit * Unit::loadGame(const XmlNode *rootNode, GameSettings *settings, Faction * //result->born(); } + result->pos = newUnitPos; + result->lastPos = Vec2i::strToVec2(unitNode->getAttribute("lastPos")->getValue()); result->meetingPos = Vec2i::strToVec2(unitNode->getAttribute("meetingPos")->getValue()); if(unitNode->hasAttribute("currentPathFinderDesiredFinalPos")) { diff --git a/source/glest_game/world/unit_updater.cpp b/source/glest_game/world/unit_updater.cpp index ddefadd8..e804c965 100644 --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -2625,6 +2625,12 @@ void UnitUpdater::saveGame(XmlNode *rootNode) { // std::map > > UnitRangeCellsLookupItemCache; } +void UnitUpdater::clearCaches() { + if(pathFinder != NULL) { + pathFinder->clearCaches(); + } +} + void UnitUpdater::loadGame(const XmlNode *rootNode) { const XmlNode *unitupdaterNode = rootNode->getChild("UnitUpdater"); diff --git a/source/glest_game/world/unit_updater.h b/source/glest_game/world/unit_updater.h index 909ad459..8953f802 100644 --- a/source/glest_game/world/unit_updater.h +++ b/source/glest_game/world/unit_updater.h @@ -136,6 +136,8 @@ public: void saveGame(XmlNode *rootNode); void loadGame(const XmlNode *rootNode); + void clearCaches(); + private: //attack void hit(Unit *attacker); diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index 00fd4f11..b4597210 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -1428,6 +1428,8 @@ void World::clearCaches() { ExploredCellsLookupItemCache.clear(); ExploredCellsLookupItemCacheTimer.clear(); ExploredCellsLookupItemCacheTimerCount = 0; + + unitUpdater.clearCaches(); } void World::togglePauseGame(bool pauseStatus,bool forceAllowPauseStateChange) {