From e4866061895a2039a69e277a0c0acdad2db70bb8 Mon Sep 17 00:00:00 2001 From: Harry Hopkinson <63599884+Harry-Hopkinson@users.noreply.github.com> Date: Sun, 10 Mar 2024 18:16:28 +0000 Subject: [PATCH] Move gPeepSpawns to GameState_t (#21570) --- src/openrct2/Editor.cpp | 2 +- src/openrct2/GameState.h | 2 ++ src/openrct2/actions/CheatSetAction.cpp | 2 +- src/openrct2/actions/FootpathPlaceAction.cpp | 7 +++-- src/openrct2/actions/FootpathRemoveAction.cpp | 7 +++-- src/openrct2/actions/LandSetRightsAction.cpp | 9 +++--- src/openrct2/actions/PeepSpawnPlaceAction.cpp | 30 +++++++++++-------- .../paint/tile_element/Paint.Surface.cpp | 5 ++-- src/openrct2/park/ParkFile.cpp | 2 +- src/openrct2/peep/GuestPathfinding.cpp | 6 ++-- src/openrct2/rct1/S4Importer.cpp | 5 ++-- src/openrct2/rct2/S6Importer.cpp | 5 ++-- src/openrct2/world/Map.cpp | 8 ++--- src/openrct2/world/Map.h | 1 - src/openrct2/world/Park.cpp | 7 +++-- 15 files changed, 55 insertions(+), 43 deletions(-) diff --git a/src/openrct2/Editor.cpp b/src/openrct2/Editor.cpp index 3c6deb1040..174f7ad753 100644 --- a/src/openrct2/Editor.cpp +++ b/src/openrct2/Editor.cpp @@ -529,7 +529,7 @@ namespace Editor } } - if (gPeepSpawns.empty()) + if (gameState.PeepSpawns.empty()) { return { false, STR_PEEP_SPAWNS_NOT_SET }; } diff --git a/src/openrct2/GameState.h b/src/openrct2/GameState.h index 539febfc85..b6b3a70e35 100644 --- a/src/openrct2/GameState.h +++ b/src/openrct2/GameState.h @@ -101,6 +101,8 @@ namespace OpenRCT2 std::vector RestrictedScenery; + std::vector PeepSpawns; + News::ItemQueues NewsItems; colour_t StaffHandymanColour; diff --git a/src/openrct2/actions/CheatSetAction.cpp b/src/openrct2/actions/CheatSetAction.cpp index c66939fe12..929ad83a06 100644 --- a/src/openrct2/actions/CheatSetAction.cpp +++ b/src/openrct2/actions/CheatSetAction.cpp @@ -767,7 +767,7 @@ void CheatSetAction::OwnAllLand() const } // Completely unown peep spawn points - for (const auto& spawn : gPeepSpawns) + for (const auto& spawn : GetGameState().PeepSpawns) { auto* surfaceElement = MapGetSurfaceElementAt(spawn); if (surfaceElement != nullptr) diff --git a/src/openrct2/actions/FootpathPlaceAction.cpp b/src/openrct2/actions/FootpathPlaceAction.cpp index 342f00ea57..05385e3d64 100644 --- a/src/openrct2/actions/FootpathPlaceAction.cpp +++ b/src/openrct2/actions/FootpathPlaceAction.cpp @@ -474,11 +474,12 @@ void FootpathPlaceAction::AutomaticallySetPeepSpawn() const } } - if (gPeepSpawns.empty()) + auto& gameState = GetGameState(); + if (gameState.PeepSpawns.empty()) { - gPeepSpawns.emplace_back(); + gameState.PeepSpawns.emplace_back(); } - PeepSpawn* peepSpawn = &gPeepSpawns[0]; + PeepSpawn* peepSpawn = &gameState.PeepSpawns[0]; peepSpawn->x = _loc.x + (DirectionOffsets[direction].x * 15) + 16; peepSpawn->y = _loc.y + (DirectionOffsets[direction].y * 15) + 16; peepSpawn->direction = direction; diff --git a/src/openrct2/actions/FootpathRemoveAction.cpp b/src/openrct2/actions/FootpathRemoveAction.cpp index 96f3536247..8f11c358df 100644 --- a/src/openrct2/actions/FootpathRemoveAction.cpp +++ b/src/openrct2/actions/FootpathRemoveAction.cpp @@ -103,16 +103,17 @@ GameActions::Result FootpathRemoveAction::Execute() const TileElementRemove(footpathElement); FootpathUpdateQueueChains(); + auto& gameState = GetGameState(); // Remove the spawn point (if there is one in the current tile) - gPeepSpawns.erase( + gameState.PeepSpawns.erase( std::remove_if( - gPeepSpawns.begin(), gPeepSpawns.end(), + gameState.PeepSpawns.begin(), gameState.PeepSpawns.end(), [this](const CoordsXYZ& spawn) { { return spawn.ToTileStart() == _loc.ToTileStart(); } }), - gPeepSpawns.end()); + gameState.PeepSpawns.end()); } else { diff --git a/src/openrct2/actions/LandSetRightsAction.cpp b/src/openrct2/actions/LandSetRightsAction.cpp index 9b7de8c1d6..9ffc1cc6fe 100644 --- a/src/openrct2/actions/LandSetRightsAction.cpp +++ b/src/openrct2/actions/LandSetRightsAction.cpp @@ -184,18 +184,19 @@ GameActions::Result LandSetRightsAction::MapBuyLandRightsForTile(const CoordsXY& } } - res.Cost = GetGameState().LandPrice; + auto& gameState = GetGameState(); + res.Cost = gameState.LandPrice; if (isExecuting) { if (_ownership != OWNERSHIP_UNOWNED) { - gPeepSpawns.erase( + gameState.PeepSpawns.erase( std::remove_if( - gPeepSpawns.begin(), gPeepSpawns.end(), + gameState.PeepSpawns.begin(), gameState.PeepSpawns.end(), [x = loc.x, y = loc.y](const auto& spawn) { return Floor2(spawn.x, 32) == x && Floor2(spawn.y, 32) == y; }), - gPeepSpawns.end()); + gameState.PeepSpawns.end()); } surfaceElement->SetOwnership(_ownership); ParkUpdateFencesAroundTile(loc); diff --git a/src/openrct2/actions/PeepSpawnPlaceAction.cpp b/src/openrct2/actions/PeepSpawnPlaceAction.cpp index ecd87e166c..fb6c2134ad 100644 --- a/src/openrct2/actions/PeepSpawnPlaceAction.cpp +++ b/src/openrct2/actions/PeepSpawnPlaceAction.cpp @@ -19,6 +19,8 @@ #include "../world/Park.h" #include "../world/Surface.h" +using namespace OpenRCT2; + PeepSpawnPlaceAction::PeepSpawnPlaceAction(const CoordsXYZD& location) : _location(location) { @@ -43,7 +45,7 @@ void PeepSpawnPlaceAction::Serialise(DataSerialiser& stream) GameActions::Result PeepSpawnPlaceAction::Query() const { - if (!(gScreenFlags & SCREEN_FLAGS_EDITOR) && !OpenRCT2::GetGameState().Cheats.SandboxMode) + if (!(gScreenFlags & SCREEN_FLAGS_EDITOR) && !GetGameState().Cheats.SandboxMode) { return GameActions::Result(GameActions::Status::NotInEditorMode, STR_ERR_CANT_PLACE_PEEP_SPAWN_HERE, STR_NONE); } @@ -100,35 +102,37 @@ GameActions::Result PeepSpawnPlaceAction::Execute() const spawn.z = _location.z; spawn.direction = _location.direction; + auto& gameState = GetGameState(); // When attempting to place a peep spawn on a tile that already contains it, // remove that peep spawn instead. - if (!gPeepSpawns.empty()) + if (!gameState.PeepSpawns.empty()) { // When searching for existing spawns, ignore the direction. - auto foundSpawn = std::find_if(gPeepSpawns.begin(), gPeepSpawns.end(), [spawn](const CoordsXYZ& existingSpawn) { - { - return existingSpawn.ToTileStart() == spawn.ToTileStart(); - } - }); + auto foundSpawn = std::find_if( + gameState.PeepSpawns.begin(), gameState.PeepSpawns.end(), [spawn](const CoordsXYZ& existingSpawn) { + { + return existingSpawn.ToTileStart() == spawn.ToTileStart(); + } + }); - if (foundSpawn != std::end(gPeepSpawns)) + if (foundSpawn != std::end(gameState.PeepSpawns)) { - gPeepSpawns.erase(foundSpawn); + gameState.PeepSpawns.erase(foundSpawn); MapInvalidateTileFull(spawn); return res; } } // If we have reached our max peep spawns, remove the oldest spawns - while (gPeepSpawns.size() >= OpenRCT2::Limits::MaxPeepSpawns) + while (gameState.PeepSpawns.size() >= Limits::MaxPeepSpawns) { - PeepSpawn oldestSpawn = *gPeepSpawns.begin(); - gPeepSpawns.erase(gPeepSpawns.begin()); + PeepSpawn oldestSpawn = *gameState.PeepSpawns.begin(); + gameState.PeepSpawns.erase(gameState.PeepSpawns.begin()); MapInvalidateTileFull(oldestSpawn); } // Set peep spawn - gPeepSpawns.push_back(spawn); + gameState.PeepSpawns.push_back(spawn); // Invalidate tile MapInvalidateTileFull(_location); diff --git a/src/openrct2/paint/tile_element/Paint.Surface.cpp b/src/openrct2/paint/tile_element/Paint.Surface.cpp index cde1e0a5d7..aa7d52b3f5 100644 --- a/src/openrct2/paint/tile_element/Paint.Surface.cpp +++ b/src/openrct2/paint/tile_element/Paint.Surface.cpp @@ -1189,12 +1189,13 @@ void PaintSurface(PaintSession& session, uint8_t direction, uint16_t height, con PaintPatrolArea(session, tileElement, height, surfaceShape); + auto& gameState = GetGameState(); // Draw Peep Spawns - if (((gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) || GetGameState().Cheats.SandboxMode) + if (((gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR) || gameState.Cheats.SandboxMode) && session.ViewFlags & VIEWPORT_FLAG_LAND_OWNERSHIP) { const CoordsXY& pos = session.MapPosition; - for (auto& spawn : gPeepSpawns) + for (auto& spawn : gameState.PeepSpawns) { if ((spawn.x & 0xFFE0) == pos.x && (spawn.y & 0xFFE0) == pos.y) { diff --git a/src/openrct2/park/ParkFile.cpp b/src/openrct2/park/ParkFile.cpp index 00ab7f958b..2ca2cb402e 100644 --- a/src/openrct2/park/ParkFile.cpp +++ b/src/openrct2/park/ParkFile.cpp @@ -528,7 +528,7 @@ namespace OpenRCT2 cs.ReadWrite(gameState.GuestInitialThirst); cs.ReadWrite(gameState.NextGuestNumber); - cs.ReadWriteVector(gPeepSpawns, [&cs](PeepSpawn& spawn) { + cs.ReadWriteVector(gameState.PeepSpawns, [&cs](PeepSpawn& spawn) { cs.ReadWrite(spawn.x); cs.ReadWrite(spawn.y); cs.ReadWrite(spawn.z); diff --git a/src/openrct2/peep/GuestPathfinding.cpp b/src/openrct2/peep/GuestPathfinding.cpp index bc93899351..51512b06d9 100644 --- a/src/openrct2/peep/GuestPathfinding.cpp +++ b/src/openrct2/peep/GuestPathfinding.cpp @@ -1589,14 +1589,14 @@ namespace OpenRCT2::PathFinding * Gets the nearest peep spawn relative to point, by using Manhattan distance. * @param x x coordinate of location * @param y y coordinate of location - * @return Index of gPeepSpawns (or 0xFF if no peep spawns exist). + * @return Index of gameState.PeepSpawns (or 0xFF if no peep spawns exist). */ static uint8_t GetNearestPeepSpawnIndex(uint16_t x, uint16_t y) { uint8_t chosenSpawn = 0xFF; uint16_t nearestDist = 0xFFFF; uint8_t i = 0; - for (const auto& spawn : gPeepSpawns) + for (const auto& spawn : GetGameState().PeepSpawns) { uint16_t dist = abs(spawn.x - x) + abs(spawn.y - y); if (dist < nearestDist) @@ -1622,7 +1622,7 @@ namespace OpenRCT2::PathFinding if (chosenSpawn == 0xFF) return GuestPathfindAimless(peep, edges); - const auto peepSpawnLoc = gPeepSpawns[chosenSpawn].ToTileStart(); + const auto peepSpawnLoc = GetGameState().PeepSpawns[chosenSpawn].ToTileStart(); Direction direction = peepSpawnLoc.direction; if (peepSpawnLoc.x == peep.NextLoc.x && peepSpawnLoc.y == peep.NextLoc.y) diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 1926551344..ac2758f4f5 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -1410,14 +1410,15 @@ namespace RCT1 void ImportPeepSpawns() { - gPeepSpawns.clear(); + auto& gameState = GetGameState(); + gameState.PeepSpawns.clear(); for (size_t i = 0; i < Limits::MaxPeepSpawns; i++) { if (_s4.PeepSpawn[i].x != RCT12_PEEP_SPAWN_UNDEFINED) { PeepSpawn spawn = { _s4.PeepSpawn[i].x, _s4.PeepSpawn[i].y, _s4.PeepSpawn[i].z * 16, _s4.PeepSpawn[i].direction }; - gPeepSpawns.push_back(spawn); + gameState.PeepSpawns.push_back(spawn); } } } diff --git a/src/openrct2/rct2/S6Importer.cpp b/src/openrct2/rct2/S6Importer.cpp index 28f13df83a..802308716f 100644 --- a/src/openrct2/rct2/S6Importer.cpp +++ b/src/openrct2/rct2/S6Importer.cpp @@ -1760,14 +1760,15 @@ namespace RCT2 _s6.PeepSpawns[0].z = 7; } - gPeepSpawns.clear(); + auto& gameState = GetGameState(); + gameState.PeepSpawns.clear(); for (size_t i = 0; i < Limits::MaxPeepSpawns; i++) { if (_s6.PeepSpawns[i].x != RCT12_PEEP_SPAWN_UNDEFINED) { PeepSpawn spawn = { _s6.PeepSpawns[i].x, _s6.PeepSpawns[i].y, _s6.PeepSpawns[i].z * 16, _s6.PeepSpawns[i].direction }; - gPeepSpawns.push_back(spawn); + gameState.PeepSpawns.push_back(spawn); } } } diff --git a/src/openrct2/world/Map.cpp b/src/openrct2/world/Map.cpp index d1b5e81d3e..6dea32e1a7 100644 --- a/src/openrct2/world/Map.cpp +++ b/src/openrct2/world/Map.cpp @@ -92,7 +92,6 @@ TileCoordsXY gWidePathTileLoopPosition; uint16_t gGrassSceneryTileLoopPosition; std::vector gMapSelectionTiles; -std::vector gPeepSpawns; bool gLandMountainMode; bool gLandPaintMode; @@ -1553,12 +1552,13 @@ static void ClearElementAt(const CoordsXY& loc, TileElement** elementPtr) */ static void ClearElementsAt(const CoordsXY& loc) { + auto& gameState = GetGameState(); // Remove the spawn point (if there is one in the current tile) - gPeepSpawns.erase( + gameState.PeepSpawns.erase( std::remove_if( - gPeepSpawns.begin(), gPeepSpawns.end(), + gameState.PeepSpawns.begin(), gameState.PeepSpawns.end(), [loc](const CoordsXY& spawn) { return spawn.ToTileStart() == loc.ToTileStart(); }), - gPeepSpawns.end()); + gameState.PeepSpawns.end()); TileElement* tileElement = MapGetFirstElementAt(loc); if (tileElement == nullptr) diff --git a/src/openrct2/world/Map.h b/src/openrct2/world/Map.h index 8708ebc0cf..b83626a45e 100644 --- a/src/openrct2/world/Map.h +++ b/src/openrct2/world/Map.h @@ -122,7 +122,6 @@ extern CoordsXYZ gMapSelectArrowPosition; extern uint8_t gMapSelectArrowDirection; extern std::vector gMapSelectionTiles; -extern std::vector gPeepSpawns; // Used in the land tool window to enable mountain tool / land smoothing extern bool gLandMountainMode; diff --git a/src/openrct2/world/Park.cpp b/src/openrct2/world/Park.cpp index c68322a187..5ed5f11495 100644 --- a/src/openrct2/world/Park.cpp +++ b/src/openrct2/world/Park.cpp @@ -57,9 +57,10 @@ static int32_t _forcedParkRating = -1; */ static PeepSpawn* GetRandomPeepSpawn() { - if (!gPeepSpawns.empty()) + auto& gameState = GetGameState(); + if (!gameState.PeepSpawns.empty()) { - return &gPeepSpawns[ScenarioRand() % gPeepSpawns.size()]; + return &gameState.PeepSpawns[ScenarioRand() % gameState.PeepSpawns.size()]; } return nullptr; @@ -255,7 +256,7 @@ void Park::Initialise() gameState.ParkEntranceFee = 10.00_GBP; - gPeepSpawns.clear(); + gameState.PeepSpawns.clear(); ParkEntranceReset(); gameState.ResearchPriorities = EnumsToFlags(