Files
OpenRCT2-Web/src/openrct2/GameState.cpp
T

361 lines
11 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2018-06-15 14:07:34 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "GameState.h"
2018-06-22 23:25:16 +02:00
#include "Game.h"
2019-05-11 21:31:34 +02:00
#include "GameStateSnapshots.h"
#include "Input.h"
2018-06-22 23:25:16 +02:00
#include "OpenRCT2.h"
#include "ReplayManager.h"
#include "actions/GameAction.h"
2019-05-11 21:31:34 +02:00
#include "config/Config.h"
#include "entity/EntityTweener.h"
2022-03-08 00:14:52 +00:00
#include "entity/PatrolArea.h"
#include "interface/Screenshot.h"
2022-02-18 21:57:00 +01:00
#include "platform/Platform.h"
2022-01-18 10:21:20 -08:00
#include "profiling/Profiling.h"
2021-05-29 06:01:32 +01:00
#include "ride/Vehicle.h"
#include "scenes/title/TitleScene.h"
#include "scenes/title/TitleSequencePlayer.h"
2018-03-18 23:35:58 +00:00
#include "scripting/ScriptEngine.h"
2018-06-22 23:25:16 +02:00
#include "ui/UiContext.h"
#include "windows/Intent.h"
#include "world/MapAnimation.h"
2025-08-13 19:55:58 +02:00
#include "world/Park.h"
#include "world/Scenery.h"
2018-03-18 23:35:58 +00:00
using namespace OpenRCT2::Scripting;
namespace OpenRCT2
2023-12-27 23:57:53 +02:00
{
static auto _gameState = std::make_unique<GameState_t>();
GameState_t& getGameState()
{
return *_gameState;
}
void swapGameState(std::unique_ptr<GameState_t>& otherState)
{
_gameState.swap(otherState);
}
2023-12-27 23:57:53 +02:00
2024-03-26 15:08:17 +01:00
/**
* Initialises the map, park etc. basically all S6 data.
*/
void gameStateInitAll(GameState_t& gameState, const TileCoordsXY& mapSize)
{
PROFILED_FUNCTION();
2017-07-23 01:06:24 +01:00
2024-03-26 15:08:17 +01:00
gInMapInitCode = true;
gameState.currentTicks = 0;
2022-01-18 10:21:20 -08:00
2024-03-26 15:08:17 +01:00
MapInit(mapSize);
Park::Initialise(gameState);
2024-03-26 15:08:17 +01:00
FinanceInit();
BannerInit(gameState);
RideInitAll();
ResetAllEntities();
UpdateConsolidatedPatrolAreas();
ResetDate();
2025-02-04 16:25:17 +01:00
ClimateReset();
2025-03-18 23:25:25 +01:00
News::InitQueue(gameState);
2024-03-26 15:08:17 +01:00
gInMapInitCode = false;
gameState.nextGuestNumber = 1;
2024-03-26 15:08:17 +01:00
ContextInit();
auto sceneryIntent = Intent(INTENT_ACTION_SET_DEFAULT_SCENERY_CONFIG);
ContextBroadcastIntent(&sceneryIntent);
auto clipboardIntent = Intent(INTENT_ACTION_CLEAR_TILE_INSPECTOR_CLIPBOARD);
ContextBroadcastIntent(&clipboardIntent);
2024-03-26 15:08:17 +01:00
LoadPalette();
2024-03-26 15:08:17 +01:00
CheatsReset();
ClearRestrictedScenery();
#ifdef ENABLE_SCRIPTING
2024-03-26 15:08:17 +01:00
auto& scriptEngine = GetContext()->GetScriptEngine();
scriptEngine.ClearParkStorage();
#endif
2024-03-26 15:08:17 +01:00
EntityTweener::Get().Reset();
}
2024-03-26 15:08:17 +01:00
/**
* Function will be called every kGameUpdateTimeMS.
* It has its own loop which might run multiple updates per call such as
* when operating as a client it may run multiple updates to catch up with the server tick,
* another influence can be the game speed setting.
*/
void gameStateTick()
{
2024-03-26 15:08:17 +01:00
PROFILED_FUNCTION();
// Normal game play will update only once every kGameUpdateTimeMS
uint32_t numUpdates = 1;
// 0x006E3AEC // screen_game_process_mouse_input();
ScreenshotCheck();
GameHandleKeyboardInput();
if (GameIsNotPaused() && gPreviewingTitleSequenceInGame)
2018-06-02 12:33:20 +01:00
{
auto player = GetContext()->GetUiContext().GetTitleSequencePlayer();
2024-03-26 15:08:17 +01:00
if (player != nullptr)
{
player->Update();
}
2018-06-02 12:33:20 +01:00
}
2024-03-26 15:08:17 +01:00
NetworkUpdate();
2024-03-26 15:08:17 +01:00
if (NetworkGetMode() == NETWORK_MODE_CLIENT && NetworkGetStatus() == NETWORK_STATUS_CONNECTED
&& NetworkGetAuthstatus() == NetworkAuth::Ok)
{
numUpdates = std::clamp<uint32_t>(NetworkGetServerTick() - getGameState().currentTicks, 0, 10);
}
else
{
2024-03-26 15:08:17 +01:00
// Determine how many times we need to update the game
if (gGameSpeed > 1)
{
2024-03-26 15:08:17 +01:00
// Update more often if game speed is above normal.
numUpdates = 1 << (gGameSpeed - 1);
}
}
2024-03-26 15:08:17 +01:00
bool isPaused = GameIsPaused();
2024-05-10 12:10:21 +02:00
if (NetworkGetMode() == NETWORK_MODE_SERVER && Config::Get().network.PauseServerIfNoClients)
{
2024-03-26 15:08:17 +01:00
// If we are headless we always have 1 player (host), pause if no one else is around.
if (gOpenRCT2Headless && NetworkGetNumPlayers() == 1)
{
2024-03-26 15:08:17 +01:00
isPaused |= true;
}
}
bool didRunSingleFrame = false;
if (isPaused)
{
if (gDoSingleUpdate && NetworkGetMode() == NETWORK_MODE_NONE)
{
didRunSingleFrame = true;
PauseToggle();
numUpdates = 1;
}
else
{
2024-03-26 15:08:17 +01:00
// NOTE: Here are a few special cases that would be normally handled in UpdateLogic.
// If the game is paused it will not call UpdateLogic at all.
numUpdates = 0;
if (NetworkGetMode() == NETWORK_MODE_SERVER)
{
// Make sure the client always knows about what tick the host is on.
NetworkSendTick();
}
// Keep updating the money effect even when paused.
UpdateMoneyEffect();
// Post-tick network update
NetworkProcessPending();
// Post-tick game actions.
GameActions::ProcessQueue();
UpdateEntitiesSpatialIndex();
}
}
2024-03-26 15:08:17 +01:00
// Update the game one or more times
for (uint32_t i = 0; i < numUpdates; i++)
2019-05-11 21:31:34 +02:00
{
2024-03-26 15:08:17 +01:00
gameStateUpdateLogic();
if (gGameSpeed == 1)
2019-05-11 21:31:34 +02:00
{
2024-03-26 15:08:17 +01:00
if (InputGetState() == InputState::Reset || InputGetState() == InputState::Normal)
{
2025-03-26 13:00:54 +01:00
if (gInputFlags.has(InputFlag::viewportScrolling))
2024-03-26 15:08:17 +01:00
{
2025-03-26 13:00:54 +01:00
gInputFlags.unset(InputFlag::viewportScrolling);
2024-03-26 15:08:17 +01:00
break;
}
}
else
{
break;
}
}
// Don't call UpdateLogic again if the game was just paused.
isPaused |= GameIsPaused();
if (isPaused)
break;
}
2019-05-11 21:31:34 +02:00
2024-03-26 15:08:17 +01:00
NetworkFlush();
if (!gOpenRCT2Headless)
{
2025-03-26 13:00:54 +01:00
gInputFlags.unset(InputFlag::viewportScrolling);
2024-03-26 15:08:17 +01:00
}
// Always perform autosave check, even when paused
2025-03-05 21:45:04 +01:00
if (gLegacyScene != LegacyScene::titleSequence && gLegacyScene != LegacyScene::trackDesigner
&& gLegacyScene != LegacyScene::trackDesignsManager)
2024-03-26 15:08:17 +01:00
{
ScenarioAutosaveCheck();
}
2025-03-05 21:45:04 +01:00
if (didRunSingleFrame && GameIsNotPaused() && gLegacyScene != LegacyScene::titleSequence)
2024-03-26 15:08:17 +01:00
{
PauseToggle();
}
gDoSingleUpdate = false;
}
static void gameStateCreateStateSnapshot()
{
PROFILED_FUNCTION();
IGameStateSnapshots* snapshots = GetContext()->GetGameStateSnapshots();
auto& snapshot = snapshots->CreateSnapshot();
snapshots->Capture(snapshot);
snapshots->LinkSnapshot(snapshot, getGameState().currentTicks, ScenarioRandState().s0);
2024-03-26 15:08:17 +01:00
}
void gameStateUpdateLogic()
{
PROFILED_FUNCTION();
gInUpdateCode = true;
gScreenAge++;
if (gScreenAge == 0)
gScreenAge--;
GetContext()->GetReplayManager()->Update();
NetworkUpdate();
auto& gameState = getGameState();
2024-09-02 21:42:37 +01:00
2024-03-26 15:08:17 +01:00
if (NetworkGetMode() == NETWORK_MODE_SERVER)
{
if (NetworkGamestateSnapshotsEnabled())
{
gameStateCreateStateSnapshot();
}
// Send current tick out.
NetworkSendTick();
}
else if (NetworkGetMode() == NETWORK_MODE_CLIENT)
{
// Don't run past the server, this condition can happen during map changes.
if (NetworkGetServerTick() == gameState.currentTicks)
2024-03-26 15:08:17 +01:00
{
gInUpdateCode = false;
return;
}
// Check desync.
bool desynced = NetworkCheckDesynchronisation();
if (desynced)
{
// If desync debugging is enabled and we are still connected request the specific game state from server.
if (NetworkGamestateSnapshotsEnabled() && NetworkGetStatus() == NETWORK_STATUS_CONNECTED)
{
// Create snapshot from this tick so we can compare it later
// as we won't pause the game on this event.
gameStateCreateStateSnapshot();
NetworkRequestGamestateSnapshot();
}
2019-05-11 21:31:34 +02:00
}
}
#ifdef ENABLE_SCRIPTING
2024-03-26 15:08:17 +01:00
// Stash the current day number before updating the date so that we
// know if the day number changes on this tick.
auto day = gameState.date.GetDay();
2020-02-23 12:55:48 +00:00
#endif
2020-02-18 22:31:54 +00:00
2024-03-26 17:32:36 +01:00
DateUpdate(gameState);
2020-02-18 22:31:54 +00:00
2024-03-26 15:08:17 +01:00
ScenarioUpdate(gameState);
ClimateUpdate();
MapUpdateTiles();
2024-03-26 15:08:17 +01:00
// Temporarily remove provisional paths to prevent peep from interacting with them
auto removeProvisionalIntent = Intent(INTENT_ACTION_REMOVE_PROVISIONAL_ELEMENTS);
ContextBroadcastIntent(&removeProvisionalIntent);
2024-03-26 15:08:17 +01:00
MapUpdatePathWideFlags();
PeepUpdateAll();
auto restoreProvisionalIntent = Intent(INTENT_ACTION_RESTORE_PROVISIONAL_ELEMENTS);
ContextBroadcastIntent(&restoreProvisionalIntent);
2024-03-26 15:08:17 +01:00
VehicleUpdateAll();
UpdateAllMiscEntities();
2025-03-09 00:26:25 +01:00
Ride::updateAll();
2017-07-23 01:06:24 +01:00
2025-03-05 21:45:04 +01:00
if (!isInEditorMode())
2024-03-26 15:08:17 +01:00
{
Park::Update(gameState, gameState.date);
2024-03-26 15:08:17 +01:00
}
2017-07-23 01:06:24 +01:00
2024-03-26 15:08:17 +01:00
ResearchUpdate();
2025-08-17 06:19:01 -03:00
RideRating::UpdateAll();
2024-03-26 15:08:17 +01:00
RideMeasurementsUpdate();
News::UpdateCurrentItem();
MapAnimations::InvalidateAndUpdateAll();
2024-03-26 15:08:17 +01:00
VehicleSoundsUpdate();
PeepUpdateCrowdNoise();
ClimateUpdateSound();
EditorOpenWindowsForCurrentStep();
2024-03-26 15:08:17 +01:00
// Update windows
// WindowDispatchUpdateAll();
UpdateEntitiesSpatialIndex();
2024-03-26 15:08:17 +01:00
// Start autosave timer after update
2024-04-04 18:18:25 +02:00
if (gLastAutoSaveUpdate == kAutosavePause)
2024-03-26 15:08:17 +01:00
{
gLastAutoSaveUpdate = Platform::GetTicks();
}
2024-03-26 15:08:17 +01:00
GameActions::ProcessQueue();
2024-03-26 15:08:17 +01:00
NetworkProcessPending();
NetworkFlush();
gameState.currentTicks++;
#ifdef ENABLE_SCRIPTING
2024-03-26 15:08:17 +01:00
auto& hookEngine = GetContext()->GetScriptEngine().GetHookEngine();
2025-03-26 12:16:47 +01:00
hookEngine.Call(HookType::intervalTick, true);
if (day != gameState.date.GetDay())
2024-03-26 15:08:17 +01:00
{
2025-03-26 12:16:47 +01:00
hookEngine.Call(HookType::intervalDay, true);
2024-03-26 15:08:17 +01:00
}
#endif
2024-03-26 15:08:17 +01:00
gInUpdateCode = false;
}
} // namespace OpenRCT2