Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/system/TurnManager.cpp
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 20 Lines | |||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#if 0 | #if 0 | ||||
#define NETTURN_LOG(...) debug_printf(__VA_ARGS__) | #define NETTURN_LOG(...) debug_printf(__VA_ARGS__) | ||||
#else | #else | ||||
#define NETTURN_LOG(...) | #define NETTURN_LOG(...) | ||||
#endif | #endif | ||||
const CStr CTurnManager::EventNameSavegameLoaded = "SavegameLoaded"; | |||||
CTurnManager::CTurnManager(CSimulation2& simulation, u32 defaultTurnLength, u32 commandDelay, int clientId, IReplayLogger& replay) | CTurnManager::CTurnManager(CSimulation2& simulation, u32 defaultTurnLength, u32 commandDelay, int clientId, IReplayLogger& replay) | ||||
: m_Simulation2(simulation), m_CurrentTurn(0), m_CommandDelay(commandDelay), m_ReadyTurn(commandDelay - 1), m_TurnLength(defaultTurnLength), | : m_Simulation2(simulation), m_CurrentTurn(0), m_CommandDelay(commandDelay), m_ReadyTurn(commandDelay - 1), m_TurnLength(defaultTurnLength), | ||||
m_PlayerId(-1), m_ClientId(clientId), m_DeltaSimTime(0), m_Replay(replay), | m_PlayerId(-1), m_ClientId(clientId), m_DeltaSimTime(0), m_Replay(replay), | ||||
m_FinalTurn(std::numeric_limits<u32>::max()), m_TimeWarpNumTurns(0) | m_FinalTurn(std::numeric_limits<u32>::max()), m_TimeWarpNumTurns(0) | ||||
{ | { | ||||
ScriptRequest rq(m_Simulation2.GetScriptInterface()); | |||||
m_QuickSaveMetadata.init(rq.cx); | |||||
m_QueuedCommands.resize(1); | m_QueuedCommands.resize(1); | ||||
} | } | ||||
void CTurnManager::ResetState(u32 newCurrentTurn, u32 newReadyTurn) | void CTurnManager::ResetState(u32 newCurrentTurn, u32 newReadyTurn) | ||||
{ | { | ||||
m_CurrentTurn = newCurrentTurn; | m_CurrentTurn = newCurrentTurn; | ||||
m_ReadyTurn = newReadyTurn; | m_ReadyTurn = newReadyTurn; | ||||
m_DeltaSimTime = 0; | m_DeltaSimTime = 0; | ||||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | void CTurnManager::RewindTimeWarp() | ||||
m_Simulation2.DeserializeState(stream); | m_Simulation2.DeserializeState(stream); | ||||
m_TimeWarpStates.pop_back(); | m_TimeWarpStates.pop_back(); | ||||
// Reset the turn manager state, so we won't execute stray commands and | // Reset the turn manager state, so we won't execute stray commands and | ||||
// won't do the next snapshot until the appropriate time. | // won't do the next snapshot until the appropriate time. | ||||
// (Ideally we ought to serialise the turn manager state and restore it | // (Ideally we ought to serialise the turn manager state and restore it | ||||
// here, but this is simpler for now.) | // here, but this is simpler for now.) | ||||
ResetState(1, m_CommandDelay); | ResetState(1, m_CommandDelay); | ||||
} | } | ||||
Stan: quicksave?
Bonus question, should those messages be translated since they are not technical… | |||||
Done Inline ActionsGood remark. I'm not familiar with the internationalization system though. Other than calling g_L10n.Translate(...), is there anything else I should add you reckon? sotirangelo: Good remark. I'm not familiar with the internationalization system though. Other than calling… | |||||
void CTurnManager::QuickSave(JS::HandleValue GUIMetadata) | |||||
{ | |||||
TIMER(L"QuickSave"); | |||||
std::stringstream stream; | |||||
if (!m_Simulation2.SerializeState(stream)) | |||||
{ | |||||
LOGERROR("Failed to quicksave game"); | |||||
return; | |||||
} | |||||
m_QuickSaveState = stream.str(); | |||||
ScriptRequest rq(m_Simulation2.GetScriptInterface()); | |||||
m_QuickSaveMetadata.set(Script::DeepCopy(rq, GUIMetadata)); | |||||
// Freeze state to ensure that consectuvie loads don't modify the state | |||||
Script::FreezeObject(rq, m_QuickSaveMetadata, true); | |||||
LOGMESSAGERENDER("Quicksaved game"); | |||||
} | |||||
void CTurnManager::QuickLoad() | |||||
{ | |||||
TIMER(L"QuickLoad"); | |||||
if (m_QuickSaveState.empty()) | |||||
{ | |||||
LOGERROR("Cannot quickload game - no game was quicksaved"); | |||||
return; | |||||
} | |||||
std::stringstream stream(m_QuickSaveState); | |||||
if (!m_Simulation2.DeserializeState(stream)) | |||||
{ | |||||
LOGERROR("Failed to quickload game"); | |||||
return; | |||||
} | |||||
// See RewindTimeWarp | |||||
ResetState(1, m_CommandDelay); | |||||
if (!g_GUI) | |||||
return; | |||||
ScriptRequest rq(m_Simulation2.GetScriptInterface()); | |||||
// Provide a copy, so that GUI components don't have to clone to get mutable objects | |||||
JS::RootedValue quickSaveMetadataClone(rq.cx, Script::DeepCopy(rq, m_QuickSaveMetadata)); | |||||
JS::RootedValueArray<1> paramData(rq.cx); | |||||
paramData[0].set(quickSaveMetadataClone); | |||||
g_GUI->SendEventToAll(EventNameSavegameLoaded, paramData); | |||||
LOGMESSAGERENDER("Quickloaded game"); | |||||
} |
Wildfire Games · Phabricator
quicksave?
Bonus question, should those messages be translated since they are not technical errors but user facing ones