1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-20 14:23:08 +01:00

Merge pull request #21454 from Gymnasiast/more-to-gamestate

Move _rides, _tileElements and _restrictedScenery to GameState_t
This commit is contained in:
Michael Steenbeek
2024-02-27 16:53:49 +01:00
committed by GitHub
5 changed files with 63 additions and 42 deletions

View File

@@ -10,9 +10,11 @@
#pragma once #pragma once
#include "Date.h" #include "Date.h"
#include "Limits.h"
#include "interface/ZoomLevel.h" #include "interface/ZoomLevel.h"
#include "management/Finance.h" #include "management/Finance.h"
#include "management/NewsItem.h" #include "management/NewsItem.h"
#include "ride/Ride.h"
#include "scenario/Scenario.h" #include "scenario/Scenario.h"
#include "world/Banner.h" #include "world/Banner.h"
#include "world/Climate.h" #include "world/Climate.h"
@@ -76,6 +78,11 @@ namespace OpenRCT2
std::string ScenarioCompletedBy; std::string ScenarioCompletedBy;
std::vector<Banner> Banners; std::vector<Banner> Banners;
// Ride storage for all the rides in the park, rides with RideId::Null are considered free.
std::array<Ride, OpenRCT2::Limits::MaxRidesInPark> Rides{};
std::vector<TileElement> TileElements;
std::vector<ScenerySelection> RestrictedScenery;
News::ItemQueues NewsItems; News::ItemQueues NewsItems;

View File

@@ -439,7 +439,7 @@ void News::DisableNewsItems(News::ItemType type, uint32_t assoc)
{ {
auto& gameState = GetGameState(); auto& gameState = GetGameState();
// TODO: write test invalidating windows // TODO: write test invalidating windows
gameState.NewsItems.ForeachRecentNews([type, assoc, gameState](auto& newsItem) { gameState.NewsItems.ForeachRecentNews([type, assoc, &gameState](auto& newsItem) {
if (type == newsItem.Type && assoc == newsItem.Assoc) if (type == newsItem.Type && assoc == newsItem.Assoc)
{ {
newsItem.SetFlags(News::ItemFlags::HasButton); newsItem.SetFlags(News::ItemFlags::HasButton);

View File

@@ -96,10 +96,7 @@ static constexpr int32_t RideInspectionInterval[] = {
10, 20, 30, 45, 60, 120, 0, 0, 10, 20, 30, 45, 60, 120, 0, 0,
}; };
// Ride storage for all the rides in the park, rides with RideId::Null are considered free. // This is the highest used index + 1 of the GameState_t::Rides array.
static std::array<Ride, OpenRCT2::Limits::MaxRidesInPark> _rides{};
// This is the highest used index + 1 of the _rides array.
static size_t _endOfUsedRange = 0; static size_t _endOfUsedRange = 0;
// A special instance of Ride that is used to draw previews such as the track designs. // A special instance of Ride that is used to draw previews such as the track designs.
@@ -131,10 +128,11 @@ RideManager GetRideManager()
size_t RideManager::size() const size_t RideManager::size() const
{ {
auto& gameState = GetGameState();
size_t count = 0; size_t count = 0;
for (size_t i = 0; i < _endOfUsedRange; i++) for (size_t i = 0; i < _endOfUsedRange; i++)
{ {
if (!_rides[i].id.IsNull()) if (!gameState.Rides[i].id.IsNull())
{ {
count++; count++;
} }
@@ -159,9 +157,10 @@ RideManager::Iterator RideManager::get(RideId rideId)
RideId GetNextFreeRideId() RideId GetNextFreeRideId()
{ {
for (RideId::UnderlyingType i = 0; i < _rides.size(); i++) auto& gameState = GetGameState();
for (RideId::UnderlyingType i = 0; i < gameState.Rides.size(); i++)
{ {
if (_rides[i].id.IsNull()) if (gameState.Rides[i].id.IsNull())
{ {
return RideId::FromUnderlying(i); return RideId::FromUnderlying(i);
} }
@@ -174,7 +173,7 @@ Ride* RideAllocateAtIndex(RideId index)
const auto idx = index.ToUnderlying(); const auto idx = index.ToUnderlying();
_endOfUsedRange = std::max<size_t>(idx + 1, _endOfUsedRange); _endOfUsedRange = std::max<size_t>(idx + 1, _endOfUsedRange);
auto result = &_rides[idx]; auto result = &GetGameState().Rides[idx];
assert(result->id == RideId::GetNull()); assert(result->id == RideId::GetNull());
result->id = index; result->id = index;
@@ -196,16 +195,17 @@ static void RideReset(Ride& ride)
void RideDelete(RideId id) void RideDelete(RideId id)
{ {
auto& gameState = GetGameState();
const auto idx = id.ToUnderlying(); const auto idx = id.ToUnderlying();
assert(idx < _rides.size()); assert(idx < gameState.Rides.size());
assert(_rides[idx].type != RIDE_TYPE_NULL); assert(gameState.Rides[idx].type != RIDE_TYPE_NULL);
auto& ride = _rides[idx]; auto& ride = gameState.Rides[idx];
RideReset(ride); RideReset(ride);
// Shrink maximum ride size. // Shrink maximum ride size.
while (_endOfUsedRange > 0 && _rides[_endOfUsedRange - 1].id.IsNull()) while (_endOfUsedRange > 0 && gameState.Rides[_endOfUsedRange - 1].id.IsNull())
{ {
_endOfUsedRange--; _endOfUsedRange--;
} }
@@ -218,14 +218,15 @@ Ride* GetRide(RideId index)
return nullptr; return nullptr;
} }
auto& gameState = GetGameState();
const auto idx = index.ToUnderlying(); const auto idx = index.ToUnderlying();
assert(idx < _rides.size()); assert(idx < gameState.Rides.size());
if (idx >= _rides.size()) if (idx >= gameState.Rides.size())
{ {
return nullptr; return nullptr;
} }
auto& ride = _rides[idx]; auto& ride = gameState.Rides[idx];
if (ride.type != RIDE_TYPE_NULL) if (ride.type != RIDE_TYPE_NULL)
{ {
assert(ride.id == index); assert(ride.id == index);
@@ -942,7 +943,8 @@ bool Ride::SupportsStatus(RideStatus s) const
*/ */
void RideInitAll() void RideInitAll()
{ {
std::for_each(std::begin(_rides), std::end(_rides), RideReset); auto& gameState = GetGameState();
std::for_each(std::begin(gameState.Rides), std::end(gameState.Rides), RideReset);
_endOfUsedRange = 0; _endOfUsedRange = 0;
} }

View File

@@ -106,7 +106,6 @@ uint32_t gLandRemainingConstructionSales;
bool gMapLandRightsUpdateSuccess; bool gMapLandRightsUpdateSuccess;
static TilePointerIndex<TileElement> _tileIndex; static TilePointerIndex<TileElement> _tileIndex;
static std::vector<TileElement> _tileElements;
static TilePointerIndex<TileElement> _tileIndexStash; static TilePointerIndex<TileElement> _tileIndexStash;
static std::vector<TileElement> _tileElementsStash; static std::vector<TileElement> _tileElementsStash;
static size_t _tileElementsInUse; static size_t _tileElementsInUse;
@@ -116,8 +115,9 @@ static int32_t _currentRotationStash;
void StashMap() void StashMap()
{ {
auto& gameState = GetGameState();
_tileIndexStash = std::move(_tileIndex); _tileIndexStash = std::move(_tileIndex);
_tileElementsStash = std::move(_tileElements); _tileElementsStash = std::move(gameState.TileElements);
_mapSizeStash = GetGameState().MapSize; _mapSizeStash = GetGameState().MapSize;
_currentRotationStash = gCurrentRotation; _currentRotationStash = gCurrentRotation;
_tileElementsInUseStash = _tileElementsInUse; _tileElementsInUseStash = _tileElementsInUse;
@@ -125,8 +125,9 @@ void StashMap()
void UnstashMap() void UnstashMap()
{ {
auto& gameState = GetGameState();
_tileIndex = std::move(_tileIndexStash); _tileIndex = std::move(_tileIndexStash);
_tileElements = std::move(_tileElementsStash); gameState.TileElements = std::move(_tileElementsStash);
GetGameState().MapSize = _mapSizeStash; GetGameState().MapSize = _mapSizeStash;
gCurrentRotation = _currentRotationStash; gCurrentRotation = _currentRotationStash;
_tileElementsInUse = _tileElementsInUseStash; _tileElementsInUse = _tileElementsInUseStash;
@@ -150,14 +151,16 @@ CoordsXY GetMapSizeMaxXY()
const std::vector<TileElement>& GetTileElements() const std::vector<TileElement>& GetTileElements()
{ {
return _tileElements; return GetGameState().TileElements;
} }
void SetTileElements(std::vector<TileElement>&& tileElements) void SetTileElements(std::vector<TileElement>&& tileElements)
{ {
_tileElements = std::move(tileElements); auto& gameState = GetGameState();
_tileIndex = TilePointerIndex<TileElement>(MAXIMUM_MAP_SIZE_TECHNICAL, _tileElements.data(), _tileElements.size()); gameState.TileElements = std::move(tileElements);
_tileElementsInUse = _tileElements.size(); _tileIndex = TilePointerIndex<TileElement>(
MAXIMUM_MAP_SIZE_TECHNICAL, gameState.TileElements.data(), gameState.TileElements.size());
_tileElementsInUse = gameState.TileElements.size();
} }
static TileElement GetDefaultSurfaceElement() static TileElement GetDefaultSurfaceElement()
@@ -180,7 +183,7 @@ static TileElement GetDefaultSurfaceElement()
std::vector<TileElement> GetReorganisedTileElementsWithoutGhosts() std::vector<TileElement> GetReorganisedTileElementsWithoutGhosts()
{ {
std::vector<TileElement> newElements; std::vector<TileElement> newElements;
newElements.reserve(std::max(MIN_TILE_ELEMENTS, _tileElements.size())); newElements.reserve(std::max(MIN_TILE_ELEMENTS, GetGameState().TileElements.size()));
for (int32_t y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++) for (int32_t y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++)
{ {
for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++) for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++)
@@ -245,7 +248,7 @@ static void ReorganiseTileElements(size_t capacity)
void ReorganiseTileElements() void ReorganiseTileElements()
{ {
ReorganiseTileElements(_tileElements.size()); ReorganiseTileElements(GetGameState().TileElements.size());
} }
static bool MapCheckFreeElementsAndReorganise(size_t numElementsOnTile, size_t numNewElements) static bool MapCheckFreeElementsAndReorganise(size_t numElementsOnTile, size_t numNewElements)
@@ -256,19 +259,20 @@ static bool MapCheckFreeElementsAndReorganise(size_t numElementsOnTile, size_t n
return false; return false;
} }
auto& gameState = GetGameState();
auto totalElementsRequired = numElementsOnTile + numNewElements; auto totalElementsRequired = numElementsOnTile + numNewElements;
auto freeElements = _tileElements.capacity() - _tileElements.size(); auto freeElements = gameState.TileElements.capacity() - gameState.TileElements.size();
if (freeElements >= totalElementsRequired) if (freeElements >= totalElementsRequired)
{ {
return true; return true;
} }
// if space issue is due to fragmentation then Reorg Tiles without increasing capacity // if space issue is due to fragmentation then Reorg Tiles without increasing capacity
if (_tileElements.size() > totalElementsRequired + _tileElementsInUse) if (gameState.TileElements.size() > totalElementsRequired + _tileElementsInUse)
{ {
ReorganiseTileElements(); ReorganiseTileElements();
// This check is not expected to fail // This check is not expected to fail
freeElements = _tileElements.capacity() - _tileElements.size(); freeElements = gameState.TileElements.capacity() - gameState.TileElements.size();
if (freeElements >= totalElementsRequired) if (freeElements >= totalElementsRequired)
{ {
return true; return true;
@@ -276,7 +280,7 @@ static bool MapCheckFreeElementsAndReorganise(size_t numElementsOnTile, size_t n
} }
// Capacity must increase to handle the space (Note capacity can go above MAX_TILE_ELEMENTS) // Capacity must increase to handle the space (Note capacity can go above MAX_TILE_ELEMENTS)
auto newCapacity = _tileElements.capacity() * 2; auto newCapacity = gameState.TileElements.capacity() * 2;
ReorganiseTileElements(newCapacity); ReorganiseTileElements(newCapacity);
return true; return true;
} }
@@ -524,7 +528,8 @@ void MapCountRemainingLandRights()
*/ */
void MapStripGhostFlagFromElements() void MapStripGhostFlagFromElements()
{ {
for (auto& element : _tileElements) auto& gameState = GetGameState();
for (auto& element : gameState.TileElements)
{ {
element.SetGhost(false); element.SetGhost(false);
} }
@@ -1033,9 +1038,10 @@ void TileElementRemove(TileElement* tileElement)
(tileElement - 1)->SetLastForTile(true); (tileElement - 1)->SetLastForTile(true);
tileElement->BaseHeight = MAX_ELEMENT_HEIGHT; tileElement->BaseHeight = MAX_ELEMENT_HEIGHT;
_tileElementsInUse--; _tileElementsInUse--;
if (tileElement == &_tileElements.back()) auto& gameState = GetGameState();
if (tileElement == &gameState.TileElements.back())
{ {
_tileElements.pop_back(); gameState.TileElements.pop_back();
} }
} }
@@ -1160,10 +1166,11 @@ static TileElement* AllocateTileElements(size_t numElementsOnTile, size_t numNew
return nullptr; return nullptr;
} }
auto oldSize = _tileElements.size(); auto& gameState = GetGameState();
_tileElements.resize(_tileElements.size() + numElementsOnTile + numNewElements); auto oldSize = gameState.TileElements.size();
gameState.TileElements.resize(gameState.TileElements.size() + numElementsOnTile + numNewElements);
_tileElementsInUse += numNewElements; _tileElementsInUse += numNewElements;
return &_tileElements[oldSize]; return &gameState.TileElements[oldSize];
} }
/** /**

View File

@@ -60,7 +60,7 @@ int16_t gSceneryCtrlPressZ;
money64 gClearSceneryCost; money64 gClearSceneryCost;
static std::vector<ScenerySelection> _restrictedScenery; using namespace OpenRCT2;
// rct2: 0x009A3E74 // rct2: 0x009A3E74
const CoordsXY SceneryQuadrantOffsets[] = { const CoordsXY SceneryQuadrantOffsets[] = {
@@ -373,17 +373,19 @@ static bool IsSceneryEntryValid(const ScenerySelection& item)
bool IsSceneryItemRestricted(const ScenerySelection& item) bool IsSceneryItemRestricted(const ScenerySelection& item)
{ {
return std::find(std::begin(_restrictedScenery), std::end(_restrictedScenery), item) != std::end(_restrictedScenery); auto& gameState = GetGameState();
return std::find(std::begin(gameState.RestrictedScenery), std::end(gameState.RestrictedScenery), item)
!= std::end(gameState.RestrictedScenery);
} }
void ClearRestrictedScenery() void ClearRestrictedScenery()
{ {
_restrictedScenery.clear(); GetGameState().RestrictedScenery.clear();
} }
std::vector<ScenerySelection>& GetRestrictedScenery() std::vector<ScenerySelection>& GetRestrictedScenery()
{ {
return _restrictedScenery; return GetGameState().RestrictedScenery;
} }
static std::vector<ScenerySelection> GetAllMiscScenery() static std::vector<ScenerySelection> GetAllMiscScenery()
@@ -418,16 +420,19 @@ static std::vector<ScenerySelection> GetAllMiscScenery()
void RestrictAllMiscScenery() void RestrictAllMiscScenery()
{ {
auto& gameState = GetGameState();
auto miscScenery = GetAllMiscScenery(); auto miscScenery = GetAllMiscScenery();
_restrictedScenery.insert(_restrictedScenery.begin(), miscScenery.begin(), miscScenery.end()); gameState.RestrictedScenery.insert(gameState.RestrictedScenery.begin(), miscScenery.begin(), miscScenery.end());
} }
void MarkAllUnrestrictedSceneryAsInvented() void MarkAllUnrestrictedSceneryAsInvented()
{ {
auto& gameState = GetGameState();
auto miscScenery = GetAllMiscScenery(); auto miscScenery = GetAllMiscScenery();
for (const auto& sceneryItem : miscScenery) for (const auto& sceneryItem : miscScenery)
{ {
if (std::find(_restrictedScenery.begin(), _restrictedScenery.end(), sceneryItem) == _restrictedScenery.end()) if (std::find(gameState.RestrictedScenery.begin(), gameState.RestrictedScenery.end(), sceneryItem)
== gameState.RestrictedScenery.end())
{ {
ScenerySetInvented(sceneryItem); ScenerySetInvented(sceneryItem);
} }