Ticket #9: visualreplay-WIP-r16491_no_gui.patch
File visualreplay-WIP-r16491_no_gui.patch, 10.3 KB (added by , 9 years ago) |
---|
-
source/network/NetTurnManager.cpp
38 38 #include <iomanip> 39 39 40 40 static const int DEFAULT_TURN_LENGTH_MP = 500; 41 static const int DEFAULT_TURN_LENGTH_SP = 200;41 static const int DEFAULT_TURN_LENGTH_SP = 500; 42 42 43 43 static const int COMMAND_DELAY = 2; 44 44 … … 467 467 468 468 469 469 470 CNetReplayTurnManager::CNetReplayTurnManager(CSimulation2& simulation, IReplayLogger& replay) : 471 CNetLocalTurnManager(simulation, replay) 472 { 473 } 474 475 void CNetReplayTurnManager::StoreReplayCommand(u32 turn, int player, const std::string& command) 476 { 477 m_ReplayCommands[turn][player].push_back(command); 478 } 479 480 void CNetReplayTurnManager::NotifyFinishedUpdate(u32 turn) 481 { 482 DoTurn(turn); 483 } 484 485 void CNetReplayTurnManager::DoTurn(u32 turn) 486 { 487 std::map<int, std::vector<std::string> > playerCommands = m_ReplayCommands[turn]; 488 std::map<int, std::vector<std::string> >::iterator it; 489 for (it = playerCommands.begin(); it != playerCommands.end(); ++it) 490 { 491 int player = it->first; 492 for (size_t i = 0; i < it->second.size(); ++i) 493 { 494 //JS::HandleValue data; 495 JS::RootedValue data(m_Simulation2.GetScriptInterface().GetContext()); 496 // JS::MutableHandleValue data; 497 m_Simulation2.GetScriptInterface().ParseJSON(it->second[i], &data); 498 AddCommand(m_ClientId, player, data, m_CurrentTurn + 1); 499 } 500 } 501 } 502 503 504 505 470 506 CNetServerTurnManager::CNetServerTurnManager(CNetServerWorker& server) : 471 507 m_NetServer(server), m_ReadyTurn(1), m_TurnLength(DEFAULT_TURN_LENGTH_MP) 472 508 { -
source/network/NetTurnManager.h
22 22 23 23 #include <list> 24 24 #include <map> 25 25 #include <vector> 26 26 class CNetServerWorker; 27 27 class CNetClient; 28 28 class CSimulationMessage; … … 189 189 std::string m_QuickSaveMetadata; 190 190 }; 191 191 192 192 193 /** 193 194 * Implementation of CNetTurnManager for network clients. 194 195 */ … … 233 234 }; 234 235 235 236 237 236 238 /** 239 * Implementation of CNetTurnManager for replay games. 240 */ 241 class CNetReplayTurnManager : public CNetLocalTurnManager 242 { 243 public: 244 CNetReplayTurnManager(CSimulation2& simulation, IReplayLogger& replay); 245 246 void StoreReplayCommand(u32 turn, int player, const std::string& command); 247 248 protected: 249 virtual void NotifyFinishedUpdate(u32 turn); 250 251 void DoTurn(u32 turn); 252 253 std::map<u32, std::map<int, std::vector<std::string> > > m_ReplayCommands; 254 }; 255 /** 237 256 * The server-side counterpart to CNetClientTurnManager. 238 257 * Records the turn state of each client, and sends turn advancement messages 239 258 * when all clients are ready. -
source/ps/Game.cpp
63 63 * Constructor 64 64 * 65 65 **/ 66 CGame::CGame(bool disableGraphics ):66 CGame::CGame(bool disableGraphics, bool replayLog): 67 67 m_World(new CWorld(this)), 68 68 m_Simulation2(new CSimulation2(&m_World->GetUnitManager(), g_ScriptRuntime, m_World->GetTerrain())), 69 69 m_GameView(disableGraphics ? NULL : new CGameView(this)), … … 71 71 m_Paused(false), 72 72 m_SimRate(1.0f), 73 73 m_PlayerID(-1), 74 m_IsSavedGame(false) 74 m_IsSavedGame(false), 75 m_IsReplay(false), 76 m_ReplayStream(NULL) 75 77 { 76 78 m_ReplayLogger = new CReplayLogger(m_Simulation2->GetScriptInterface()); 77 // TODO: should use CDummyReplayLogger unless activated by cmd-line arg, perhaps? 79 /* TODO: use CDummyReplayLogger ? 80 if (replayLog) 81 m_ReplayLogger = new CReplayLogger(m_Simulation2->GetScriptInterface()); 82 else 83 m_ReplayLogger = new CDummyReplayLogger(); 84 */ 78 85 79 86 // Need to set the CObjectManager references after various objects have 80 87 // been initialised, so do it here rather than via the initialisers above. … … 85 92 86 93 m_Simulation2->LoadDefaultScripts(); 87 94 } 95 int CGame::LoadReplayData() 96 { 97 ENSURE(m_IsReplay); 98 ENSURE(!m_ReplayPath.empty()); 88 99 100 CNetReplayTurnManager* replayTurnMgr = static_cast<CNetReplayTurnManager*>(GetTurnManager()); 101 102 u32 currentTurn = 0; 103 std::string type; 104 while ((*m_ReplayStream >> type).good()) 105 { 106 if (type == "turn") 107 { 108 u32 turn = 0; 109 u32 turnLength = 0; 110 *m_ReplayStream >> turn >> turnLength; 111 ENSURE(turn == currentTurn); 112 } 113 else if (type == "cmd") 114 { 115 int player; 116 *m_ReplayStream >> player; 117 118 std::string line; 119 std::getline(*m_ReplayStream, line); 120 replayTurnMgr->StoreReplayCommand(currentTurn, player, line); 121 } 122 else if (type == "hash" || type == "hash-quick") 123 { 124 // Ignored for now 125 std::string replayHash; 126 *m_ReplayStream >> replayHash; 127 } 128 else if (type == "end") 129 { 130 currentTurn++; 131 } 132 else 133 { 134 CancelLoad(L"Failed to load replay data (unrecognized content)"); 135 } 136 } 137 m_FinalReplayTurn = currentTurn + 1; 138 139 return 0; 140 } 141 void CGame::StartReplay(const std::string& replayPath) 142 { 143 m_IsReplay = true; 144 ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); 145 146 SetTurnManager(new CNetReplayTurnManager(*m_Simulation2, GetReplayLogger())); 147 148 m_ReplayPath = replayPath; 149 m_ReplayStream = new std::ifstream(m_ReplayPath.c_str()); 150 ENSURE(m_ReplayStream->good()); 151 152 std::string type; 153 ENSURE((*m_ReplayStream >> type).good() && type == "start"); 154 155 std::string line; 156 std::getline(*m_ReplayStream, line); 157 JS::RootedValue attribs(scriptInterface.GetContext()); 158 scriptInterface.ParseJSON(line, &attribs); 159 StartGame(&attribs, ""); 160 } 89 161 /** 90 162 * Destructor 91 163 * … … 101 173 delete m_Simulation2; 102 174 delete m_World; 103 175 delete m_ReplayLogger; 176 delete m_ReplayStream; 104 177 } 105 178 106 179 void CGame::SetTurnManager(CNetTurnManager* turnManager) … … 125 198 ScriptInterface& scriptInterface = m_Simulation2->GetScriptInterface(); 126 199 JSContext* cx = scriptInterface.GetContext(); 127 200 JSAutoRequest rq(cx); 128 201 129 202 m_InitialSavedState = savedState; 130 203 m_IsSavedGame = !savedState.empty(); 131 204 … … 176 249 if (m_IsSavedGame) 177 250 RegMemFun(this, &CGame::LoadInitialState, L"Loading game", 1000); 178 251 252 if (m_IsReplay) 253 RegMemFun(this, &CGame::LoadReplayData, L"Loading replay data", 1000); 254 179 255 LDR_EndRegistering(); 180 256 } 181 257 … … 208 284 { 209 285 JSContext* cx = m_Simulation2->GetScriptInterface().GetContext(); 210 286 JSAutoRequest rq(cx); 211 287 212 288 // Call the script function InitGame only for new games, not saved games 213 289 if (!m_IsSavedGame) 214 290 { … … 230 306 Interpolate(0, 0); 231 307 232 308 m_GameStarted=true; 233 309 234 310 // Render a frame to begin loading assets 235 311 if (CRenderer::IsInitialised()) 236 312 Render(); … … 289 365 return true; 290 366 291 367 const double deltaSimTime = deltaRealTime * m_SimRate; 292 368 293 369 bool ok = true; 294 370 if (deltaSimTime) 295 371 { … … 310 386 PROFILE3("gui sim update"); 311 387 g_GUI->SendEventToAll("SimulationUpdate"); 312 388 } 389 if (m_IsReplay && m_TurnManager->GetCurrentTurn() == m_FinalReplayTurn) 390 g_GUI->SendEventToAll("ReplayFinished"); 313 391 314 392 GetView()->GetLOSTexture().MakeDirty(); 315 393 } 316 394 317 395 if (CRenderer::IsInitialised()) 318 396 g_Renderer.GetTimeManager().Update(deltaSimTime); 319 397 } -
source/ps/Game.h
20 20 21 21 #include "ps/Errors.h" 22 22 #include <vector> 23 23 #include <map> 24 24 #include "scriptinterface/ScriptVal.h" 25 25 26 26 class CWorld; … … 65 65 CNetTurnManager* m_TurnManager; 66 66 67 67 public: 68 CGame(bool disableGraphics = false );68 CGame(bool disableGraphics = false, bool replayLog = true); 69 69 ~CGame(); 70 70 71 71 /** … … 76 76 void StartGame(JS::MutableHandleValue attribs, const std::string& savedState); 77 77 PSRETURN ReallyStartGame(); 78 78 79 void StartReplay(const std::string& replayPath); 80 79 81 /** 80 82 * Periodic heartbeat that controls the process. performs all per-frame updates. 81 83 * Simulation update is called and game status update is called. … … 171 173 int LoadInitialState(); 172 174 std::string m_InitialSavedState; // valid between RegisterInit and LoadInitialState 173 175 bool m_IsSavedGame; // true if loading a saved game; false for a new game 176 177 int LoadReplayData(); 178 std::string m_ReplayPath; 179 bool m_IsReplay; 180 std::istream* m_ReplayStream; 181 u32 m_FinalReplayTurn; 174 182 }; 175 183 176 184 extern CGame *g_Game; -
source/ps/GameSetup/GameSetup.cpp
880 880 } 881 881 882 882 bool Autostart(const CmdLineArgs& args); 883 bool VisualReplay(const CmdLineArgs& args); 883 884 884 885 bool Init(const CmdLineArgs& args, int flags) 885 886 { … … 1073 1074 1074 1075 try 1075 1076 { 1076 if (! Autostart(args))1077 if (!VisualReplay(args) && !Autostart(args)) 1077 1078 { 1078 1079 const bool setup_gui = ((flags & INIT_NO_GUI) == 0); 1079 1080 // We only want to display the splash screen at startup … … 1473 1474 return true; 1474 1475 } 1475 1476 1477 bool VisualReplay(const CmdLineArgs& args) 1478 { 1479 CStr replayPath = args.Get("replay-visual"); 1480 if (!replayPath.empty()) 1481 { 1482 g_Game = new CGame(false, false); 1483 1484 g_Game->SetPlayerID(1); 1485 g_Game->StartReplay(replayPath); 1486 1487 // TODO: Non progressive load can fail - need a decent way to handle this 1488 LDR_NonprogressiveLoad(); 1489 1490 PSRETURN ret = g_Game->ReallyStartGame(); 1491 ENSURE(ret == PSRETURN_OK); 1492 1493 ScriptInterface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); 1494 1495 InitPs(true, L"page_replay.xml", &scriptInterface, JS::UndefinedHandleValue); 1496 return true; 1497 } 1498 1499 return false; 1500 } 1501 1476 1502 void CancelLoad(const CStrW& message) 1477 1503 { 1478 1504 shared_ptr<ScriptInterface> pScriptInterface = g_GUI->GetActiveGUI()->GetScriptInterface();