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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

374 lines
11 KiB
C++
Raw Normal View History

/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 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"
2026-02-09 15:07:42 +01:00
#include "actions/GameActionRunner.h"
2019-05-11 21:31:34 +02:00
#include "config/Config.h"
2026-01-05 20:09:34 +01:00
#include "drawing/Drawing.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 "scenario/Scenario.h"
#include "scenes/editor/EditorScene.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/Map.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.park, gameState);
2024-03-26 15:08:17 +01:00
FinanceInit();
BannerInit(gameState);
RideInitAll();
gameState.entities.resetAllEntities();
2024-03-26 15:08:17 +01:00
UpdateConsolidatedPatrolAreas();
ResetDate();
Weather::reset();
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
}
Network::Update();
if (Network::GetMode() == Network::Mode::client && Network::GetStatus() == Network::Status::connected
&& Network::GetAuthstatus() == Network::Auth::ok)
{
numUpdates = std::clamp<uint32_t>(Network::GetServerTick() - 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();
2025-10-07 20:14:37 +02:00
if (Network::GetMode() == 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 && Network::GetNumPlayers() == 1)
{
2024-03-26 15:08:17 +01:00
isPaused |= true;
}
}
bool didRunSingleFrame = false;
if (isPaused)
{
if (gDoSingleUpdate && Network::GetMode() == Network::Mode::none)
2024-03-26 15:08:17 +01:00
{
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 (Network::GetMode() == Network::Mode::server)
2024-03-26 15:08:17 +01:00
{
// Make sure the client always knows about what tick the host is on.
Network::SendTick();
2024-03-26 15:08:17 +01:00
}
// Keep updating the money effect even when paused.
auto& gameState = getGameState();
gameState.entities.updateMoneyEffect();
2024-03-26 15:08:17 +01:00
// Post-tick network update
Network::PostTick();
2024-03-26 15:08:17 +01:00
// Post-tick game actions.
GameActions::ProcessQueue(gameState);
gameState.entities.updateEntitiesSpatialIndex();
}
}
// Network has to always tick.
if (numUpdates == 0)
{
Network::Tick();
}
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
{
if (InputGetState() == InputState::reset || InputGetState() == InputState::normal)
2024-03-26 15:08:17 +01:00
{
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
Network::Flush();
2024-03-26 15:08:17 +01:00
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();
Network::Tick();
2024-03-26 15:08:17 +01:00
auto& gameState = getGameState();
2024-09-02 21:42:37 +01:00
if (Network::GetMode() == Network::Mode::server)
2024-03-26 15:08:17 +01:00
{
if (Network::GamestateSnapshotsEnabled())
2024-03-26 15:08:17 +01:00
{
gameStateCreateStateSnapshot();
}
// Send current tick out.
Network::SendTick();
2024-03-26 15:08:17 +01:00
}
else if (Network::GetMode() == Network::Mode::client)
2024-03-26 15:08:17 +01:00
{
// Don't run past the server, this condition can happen during map changes.
if (Network::GetServerTick() == gameState.currentTicks)
2024-03-26 15:08:17 +01:00
{
gInUpdateCode = false;
return;
}
// Check desync.
bool desynced = Network::CheckDesynchronisation();
2024-03-26 15:08:17 +01:00
if (desynced)
{
// If desync debugging is enabled and we are still connected request the specific game state from server.
if (Network::GamestateSnapshotsEnabled() && Network::GetStatus() == Network::Status::connected)
2024-03-26 15:08:17 +01:00
{
// Create snapshot from this tick so we can compare it later
// as we won't pause the game on this event.
gameStateCreateStateSnapshot();
Network::RequestGamestateSnapshot();
2024-03-26 15:08:17 +01:00
}
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);
Weather::update();
2024-03-26 15:08:17 +01:00
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();
gameState.entities.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
{
auto& park = gameState.park;
Park::Update(park, gameState);
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();
Weather::updateSound();
EditorScene::OpenWindowsForCurrentStep();
2024-03-26 15:08:17 +01:00
// Update windows
// WindowDispatchUpdateAll();
gameState.entities.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();
}
GameActions::ProcessQueue(gameState);
Network::PostTick();
Network::Flush();
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