OpenRCT2/src/openrct2/world/Entrance.cpp

345 lines
11 KiB
C++
Raw Normal View History

2017-02-25 14:51:30 +01:00
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
2017-02-25 14:51:30 +01:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-02-25 14:51:30 +01:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-02-25 14:51:30 +01:00
*****************************************************************************/
2017-12-14 10:34:12 +01:00
#include "Entrance.h"
2018-06-22 23:17:03 +02:00
2017-12-13 13:02:24 +01:00
#include "../Cheats.h"
#include "../Context.h"
2017-11-30 18:17:06 +01:00
#include "../Game.h"
#include "../GameState.h"
2018-06-22 23:17:03 +02:00
#include "../OpenRCT2.h"
Split actions hpp files into separate h and cpp files (#13548) * Split up SmallSceneryPlace/Remove Added undo function for Remove Scenery * Refactor: Balloon and Banner actions hpp=>h/cpp * Refactor: rename all action *.hpp files to *.cpp This is preparation for separation in later commits. Note that without the complete set of commits in this branch, the code will not build. * Refactor Clear, Climate, Custom, and Footpath actions hpp=>h/cpp * VSCode: add src subdirectories to includePath * Refactor Guest actions hpp=>h/cpp * Refactor Land actions hpp=>h/cpp * Refactor LargeScenery actions hpp=>h/cpp * Refactor Load, Maze, Network actions hpp=>h/cpp * Refactor Park actions hpp=>h/cpp * Refactor/style: move private function declarations in actions *.h Previous action .h files included private function declarations with private member variables, before public function declarations. This commit re-orders the header files to the following order: - public member variables - private member variables - public functions - private functions * Refactor Pause action hpp=>h/cpp * Refactor Peep, Place, Player actions hpp=>h/cpp * Refactor Ride actions hpp=>h/cpp * Refactor Scenario, Set*, Sign* actions hpp=>h/cpp * Refactor SmallScenerySetColourAction hpp=>h/cpp * Refactor Staff actions hpp=>h/cpp * Refactor Surface, Tile, Track* actions hpp=>h/cpp * Refactor Wall and Water actions hpp=>h/cpp * Fix various includes and other compile errors Update includes for tests. Move static function declarations to .h files Add explicit includes to various files that were previously implicit (the required header was a nested include in an action hpp file, and the action .h file does not include that header) Move RideSetStatus string enum to the cpp file to avoid unused imports * Xcode: modify project file for actions refactor * Cleanup whitespace and end-of-file newlines Co-authored-by: duncanspumpkin <duncans_pumpkin@hotmail.co.uk>
2020-12-10 07:39:10 +01:00
#include "../actions/ParkEntranceRemoveAction.h"
#include "../actions/RideEntranceExitPlaceAction.h"
#include "../actions/RideEntranceExitRemoveAction.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/StringIds.h"
2017-10-06 22:37:06 +02:00
#include "../management/Finance.h"
2018-06-22 23:17:03 +02:00
#include "../network/network.h"
#include "../object/FootpathObject.h"
#include "../object/FootpathSurfaceObject.h"
#include "../object/ObjectManager.h"
2021-12-18 19:50:29 +01:00
#include "../ride/RideConstruction.h"
#include "../ride/Station.h"
2018-06-22 23:17:03 +02:00
#include "../ride/Track.h"
#include "Footpath.h"
#include "Map.h"
#include "MapAnimation.h"
#include "Park.h"
2017-02-25 14:51:30 +01:00
2018-12-15 20:20:04 +01:00
#include <algorithm>
using namespace OpenRCT2;
bool gParkEntranceGhostExists = false;
2019-12-12 10:58:27 +01:00
CoordsXYZD gParkEntranceGhostPosition = { 0, 0, 0, 0 };
2017-02-25 14:51:30 +01:00
2018-02-16 09:50:40 +01:00
CoordsXYZD gRideEntranceExitGhostPosition;
StationIndex gRideEntranceExitGhostStationIndex;
static money64 RideEntranceExitPlaceGhost(
2022-01-19 14:17:11 +01:00
RideId rideIndex, const CoordsXY& entranceExitCoords, Direction direction, uint8_t placeType, StationIndex stationNum)
{
auto rideEntranceExitPlaceAction = RideEntranceExitPlaceAction(
entranceExitCoords, direction, rideIndex, stationNum, placeType == ENTRANCE_TYPE_RIDE_EXIT);
rideEntranceExitPlaceAction.SetFlags(GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_GHOST);
auto res = GameActions::Execute(&rideEntranceExitPlaceAction);
return res.Error == GameActions::Status::Ok ? res.Cost : kMoney64Undefined;
}
2017-02-25 14:51:30 +01:00
2018-02-01 18:49:14 +01:00
/**
*
* rct2: 0x00666F9E
*/
2022-10-04 08:38:00 +02:00
void ParkEntranceRemoveGhost()
2018-02-01 18:49:14 +01:00
{
if (gParkEntranceGhostExists)
2017-02-25 14:51:30 +01:00
{
2018-02-01 18:49:14 +01:00
gParkEntranceGhostExists = false;
2019-12-12 10:58:27 +01:00
auto parkEntranceRemoveAction = ParkEntranceRemoveAction(gParkEntranceGhostPosition);
2019-03-24 21:41:14 +01:00
parkEntranceRemoveAction.SetFlags(GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED);
GameActions::Execute(&parkEntranceRemoveAction);
2017-02-25 14:51:30 +01:00
}
2018-02-01 18:49:14 +01:00
}
2017-02-25 14:51:30 +01:00
2022-10-04 08:38:00 +02:00
int32_t ParkEntranceGetIndex(const CoordsXYZ& entrancePos)
2018-02-01 18:49:14 +01:00
{
int32_t i = 0;
for (const auto& entrance : GetGameState().Park.Entrances)
2017-02-25 14:51:30 +01:00
{
if (entrancePos == entrance)
2017-03-01 22:19:15 +01:00
{
2018-02-01 18:49:14 +01:00
return i;
2017-03-01 22:19:15 +01:00
}
i++;
2017-02-25 14:51:30 +01:00
}
2018-02-01 18:49:14 +01:00
return -1;
}
2022-10-04 08:51:27 +02:00
void ParkEntranceReset()
2018-02-01 18:49:14 +01:00
{
GetGameState().Park.Entrances.clear();
2018-02-01 18:49:14 +01:00
}
2022-10-04 08:38:00 +02:00
void RideEntranceExitPlaceProvisionalGhost()
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:17:03 +02:00
if (_currentTrackSelectionFlags & TRACK_SELECTION_FLAG_ENTRANCE_OR_EXIT)
{
RideEntranceExitPlaceGhost(
_currentRideIndex, gRideEntranceExitGhostPosition, gRideEntranceExitGhostPosition.direction,
gRideEntranceExitPlaceType, gRideEntranceExitGhostStationIndex);
}
2018-02-01 18:49:14 +01:00
}
2022-10-04 08:38:00 +02:00
void RideEntranceExitRemoveGhost()
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:17:03 +02:00
if (_currentTrackSelectionFlags & TRACK_SELECTION_FLAG_ENTRANCE_OR_EXIT)
{
auto rideEntranceExitRemove = RideEntranceExitRemoveAction(
gRideEntranceExitGhostPosition, _currentRideIndex, gRideEntranceExitGhostStationIndex,
gRideEntranceExitPlaceType == ENTRANCE_TYPE_RIDE_EXIT);
2019-02-27 19:35:55 +01:00
rideEntranceExitRemove.SetFlags(GAME_COMMAND_FLAG_GHOST | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED);
GameActions::Execute(&rideEntranceExitRemove);
}
2018-02-01 18:49:14 +01:00
}
2018-02-01 18:49:14 +01:00
/**
*
* rct2: 0x006CA28C
*/
money64 RideEntranceExitPlaceGhost(
const Ride& ride, const CoordsXY& entranceExitCoords, Direction direction, int32_t placeType, StationIndex stationNum)
2018-02-01 18:49:14 +01:00
{
RideConstructionRemoveGhosts();
money64 result = RideEntranceExitPlaceGhost(ride.id, entranceExitCoords, direction, placeType, stationNum);
if (result != kMoney64Undefined)
2017-03-09 19:48:09 +01:00
{
2018-02-01 18:49:14 +01:00
_currentTrackSelectionFlags |= TRACK_SELECTION_FLAG_ENTRANCE_OR_EXIT;
gRideEntranceExitGhostPosition.x = entranceExitCoords.x;
gRideEntranceExitGhostPosition.y = entranceExitCoords.y;
2018-02-01 18:49:14 +01:00
gRideEntranceExitGhostPosition.direction = direction;
gRideEntranceExitGhostStationIndex = stationNum;
}
2018-02-01 18:49:14 +01:00
return result;
}
2018-02-01 18:49:14 +01:00
/**
* Replaces the outer hedge walls for an entrance placement removal.
* rct2: 0x00666D6F
*/
2022-10-04 08:38:00 +02:00
void MazeEntranceHedgeReplacement(const CoordsXYE& entrance)
2018-02-01 18:49:14 +01:00
{
int32_t direction = entrance.element->GetDirection();
auto hedgePos = entrance + CoordsDirectionDelta[direction];
int32_t z = entrance.element->GetBaseZ();
2022-01-19 14:17:11 +01:00
RideId rideIndex = entrance.element->AsEntrance()->GetRideIndex();
2018-02-01 18:49:14 +01:00
auto tileElement = MapGetFirstElementAt(hedgePos);
Avoid dereferencing map_get_first_element_at nullptr on libopenrct2 (#10013) * Avoid dereferencing map_get_first_element_at nullptr on Map.cpp * Avoid dereferencing map_get_first_element_at nullptr on MapAnimation.cpp Returning true or internal control variable, based on what was seen on `map_animation_invalidate_track_onridephoto` * Avoid dereferencing map_get_first_element_at nullptr on Park.cpp * Avoid dereferencing map_get_first_element_at nullptr on Scenery.cpp * Avoid dereferencing map_get_first_element_at nullptr on Sprite.cpp * Avoid dereferencing map_get_first_element_at nullptr on TileInspector.cpp * Avoid dereferencing map_get_first_element_at nullptr on Wall.cpp * Avoid dereferencing map_get_first_element_at nullptr on Fountain.cpp * Avoid dereferencing map_get_first_element_at nullptr on Footpath.cpp * Avoid dereferencing map_get_first_element_at nullptr on Entrance.cpp * Avoid dereferencing map_get_first_element_at nullptr on Banner.cpp * Avoid dereferencing map_get_first_element_at nullptr on Vehicle.cpp * Avoid dereferencing map_get_first_element_at nullptr on TrackDesignSave.cpp * Avoid dereferencing map_get_first_element_at nullptr on TrackDesign.cpp * Avoid dereferencing map_get_first_element_at nullptr on Track.cpp * Avoid dereferencing map_get_first_element_at nullptr on Station.cpp * Avoid dereferencing map_get_first_element_at nullptr on RideRatings.cpp * Avoid dereferencing map_get_first_element_at nullptr on Ride.cpp * Avoid dereferencing map_get_first_element_at nullptr on S4Importer.cpp * Avoid dereferencing map_get_first_element_at nullptr on Staff.cpp * Avoid dereferencing map_get_first_element_at nullptr on Peep.cpp * Avoid dereferencing map_get_first_element_at nullptr on GuestPathfinding.cpp * Avoid dereferencing map_get_first_element_at nullptr on Guest.cpp * Avoid dereferencing map_get_first_element_at nullptr on VirtualFloor.cpp * Avoid dereferencing map_get_first_element_at nullptr on Paint.TileElement.cpp * Fix issues raised on review * Fix remaining review issues. * Early exit on loops if tileElement is nullptr * Fix clang-format issues
2019-10-09 16:02:21 +02:00
if (tileElement == nullptr)
return;
2018-06-22 23:17:03 +02:00
do
{
2021-12-11 00:39:39 +01:00
if (tileElement->GetType() != TileElementType::Track)
2018-06-22 23:17:03 +02:00
continue;
2018-09-18 13:10:29 +02:00
if (tileElement->AsTrack()->GetRideIndex() != rideIndex)
2018-06-22 23:17:03 +02:00
continue;
if (tileElement->GetBaseZ() != z)
2018-06-22 23:17:03 +02:00
continue;
if (tileElement->AsTrack()->GetTrackType() != TrackElemType::Maze)
2018-06-22 23:17:03 +02:00
continue;
2018-02-01 18:49:14 +01:00
// Each maze element is split into 4 sections with 4 different walls
uint8_t mazeSection = direction * 4;
2018-02-01 18:49:14 +01:00
// Add the top outer wall
tileElement->AsTrack()->MazeEntryAdd(1 << ((mazeSection + 9) & 0x0F));
2018-02-01 18:49:14 +01:00
// Add the bottom outer wall
tileElement->AsTrack()->MazeEntryAdd(1 << ((mazeSection + 12) & 0x0F));
2018-02-01 18:49:14 +01:00
MapInvalidateTile({ hedgePos, tileElement->GetBaseZ(), tileElement->GetClearanceZ() });
2018-02-01 18:49:14 +01:00
return;
} while (!(tileElement++)->IsLastForTile());
2018-02-01 18:49:14 +01:00
}
2018-02-01 18:49:14 +01:00
/**
* Removes the hedge walls for an entrance placement.
* rct2: 0x00666CBE
*/
2022-10-04 08:38:00 +02:00
void MazeEntranceHedgeRemoval(const CoordsXYE& entrance)
2018-02-01 18:49:14 +01:00
{
int32_t direction = entrance.element->GetDirection();
auto hedgePos = entrance + CoordsDirectionDelta[direction];
int32_t z = entrance.element->GetBaseZ();
2022-01-19 14:17:11 +01:00
RideId rideIndex = entrance.element->AsEntrance()->GetRideIndex();
2018-02-01 18:49:14 +01:00
auto tileElement = MapGetFirstElementAt(hedgePos);
Avoid dereferencing map_get_first_element_at nullptr on libopenrct2 (#10013) * Avoid dereferencing map_get_first_element_at nullptr on Map.cpp * Avoid dereferencing map_get_first_element_at nullptr on MapAnimation.cpp Returning true or internal control variable, based on what was seen on `map_animation_invalidate_track_onridephoto` * Avoid dereferencing map_get_first_element_at nullptr on Park.cpp * Avoid dereferencing map_get_first_element_at nullptr on Scenery.cpp * Avoid dereferencing map_get_first_element_at nullptr on Sprite.cpp * Avoid dereferencing map_get_first_element_at nullptr on TileInspector.cpp * Avoid dereferencing map_get_first_element_at nullptr on Wall.cpp * Avoid dereferencing map_get_first_element_at nullptr on Fountain.cpp * Avoid dereferencing map_get_first_element_at nullptr on Footpath.cpp * Avoid dereferencing map_get_first_element_at nullptr on Entrance.cpp * Avoid dereferencing map_get_first_element_at nullptr on Banner.cpp * Avoid dereferencing map_get_first_element_at nullptr on Vehicle.cpp * Avoid dereferencing map_get_first_element_at nullptr on TrackDesignSave.cpp * Avoid dereferencing map_get_first_element_at nullptr on TrackDesign.cpp * Avoid dereferencing map_get_first_element_at nullptr on Track.cpp * Avoid dereferencing map_get_first_element_at nullptr on Station.cpp * Avoid dereferencing map_get_first_element_at nullptr on RideRatings.cpp * Avoid dereferencing map_get_first_element_at nullptr on Ride.cpp * Avoid dereferencing map_get_first_element_at nullptr on S4Importer.cpp * Avoid dereferencing map_get_first_element_at nullptr on Staff.cpp * Avoid dereferencing map_get_first_element_at nullptr on Peep.cpp * Avoid dereferencing map_get_first_element_at nullptr on GuestPathfinding.cpp * Avoid dereferencing map_get_first_element_at nullptr on Guest.cpp * Avoid dereferencing map_get_first_element_at nullptr on VirtualFloor.cpp * Avoid dereferencing map_get_first_element_at nullptr on Paint.TileElement.cpp * Fix issues raised on review * Fix remaining review issues. * Early exit on loops if tileElement is nullptr * Fix clang-format issues
2019-10-09 16:02:21 +02:00
if (tileElement == nullptr)
return;
2018-06-22 23:17:03 +02:00
do
{
2021-12-11 00:39:39 +01:00
if (tileElement->GetType() != TileElementType::Track)
2018-06-22 23:17:03 +02:00
continue;
2018-09-18 13:10:29 +02:00
if (tileElement->AsTrack()->GetRideIndex() != rideIndex)
2018-06-22 23:17:03 +02:00
continue;
if (tileElement->GetBaseZ() != z)
2018-06-22 23:17:03 +02:00
continue;
if (tileElement->AsTrack()->GetTrackType() != TrackElemType::Maze)
2018-06-22 23:17:03 +02:00
continue;
2018-02-01 18:49:14 +01:00
// Each maze element is split into 4 sections with 4 different walls
uint8_t mazeSection = direction * 4;
2018-02-01 18:49:14 +01:00
// Remove the top outer wall
tileElement->AsTrack()->MazeEntrySubtract(1 << ((mazeSection + 9) & 0x0F));
2018-02-01 18:49:14 +01:00
// Remove the bottom outer wall
tileElement->AsTrack()->MazeEntrySubtract(1 << ((mazeSection + 12) & 0x0F));
2018-02-01 18:49:14 +01:00
// Remove the intersecting wall
tileElement->AsTrack()->MazeEntrySubtract(1 << ((mazeSection + 10) & 0x0F));
2018-02-01 18:49:14 +01:00
// Remove the top hedge section
tileElement->AsTrack()->MazeEntrySubtract(1 << ((mazeSection + 11) & 0x0F));
2018-02-01 18:49:14 +01:00
// Remove the bottom hedge section
tileElement->AsTrack()->MazeEntrySubtract(1 << ((mazeSection + 15) & 0x0F));
2018-02-01 18:49:14 +01:00
MapInvalidateTile({ hedgePos, tileElement->GetBaseZ(), tileElement->GetClearanceZ() });
2018-02-01 18:49:14 +01:00
return;
} while (!(tileElement++)->IsLastForTile());
2018-02-01 18:49:14 +01:00
}
2022-10-04 08:38:00 +02:00
void ParkEntranceFixLocations(void)
2018-02-01 18:49:14 +01:00
{
auto& gameState = GetGameState();
// Fix ParkEntrance locations for which the tile_element no longer exists
gameState.Park.Entrances.erase(
std::remove_if(
gameState.Park.Entrances.begin(), gameState.Park.Entrances.end(),
[](const auto& entrance) { return MapGetParkEntranceElementAt(entrance, false) == nullptr; }),
gameState.Park.Entrances.end());
}
2022-10-04 08:38:00 +02:00
void ParkEntranceUpdateLocations()
{
auto& gameState = GetGameState();
gameState.Park.Entrances.clear();
TileElementIterator it;
TileElementIteratorBegin(&it);
while (TileElementIteratorNext(&it))
{
auto entranceElement = it.element->AsEntrance();
if (entranceElement != nullptr && entranceElement->GetEntranceType() == ENTRANCE_TYPE_PARK_ENTRANCE
&& entranceElement->GetSequenceIndex() == 0 && !entranceElement->IsGhost())
{
auto entrance = TileCoordsXYZD(it.x, it.y, it.element->BaseHeight, it.element->GetDirection()).ToCoordsXYZD();
gameState.Park.Entrances.push_back(entrance);
}
}
}
StationIndex EntranceElement::GetStationIndex() const
{
return stationIndex;
}
void EntranceElement::SetStationIndex(StationIndex newStationIndex)
{
stationIndex = newStationIndex;
}
2018-09-26 12:13:44 +02:00
uint8_t EntranceElement::GetEntranceType() const
{
return entranceType;
}
void EntranceElement::SetEntranceType(uint8_t newType)
{
entranceType = newType;
}
2018-09-26 12:30:27 +02:00
2022-01-19 14:17:11 +01:00
RideId EntranceElement::GetRideIndex() const
2018-09-26 12:30:27 +02:00
{
return rideIndex;
}
2022-01-19 14:17:11 +01:00
void EntranceElement::SetRideIndex(RideId newRideIndex)
2018-09-26 12:30:27 +02:00
{
rideIndex = newRideIndex;
}
uint8_t EntranceElement::GetSequenceIndex() const
{
return SequenceIndex & 0xF;
}
void EntranceElement::SetSequenceIndex(uint8_t newSequenceIndex)
{
SequenceIndex &= ~0xF;
SequenceIndex |= (newSequenceIndex & 0xF);
2018-09-26 13:02:51 +02:00
}
bool EntranceElement::HasLegacyPathEntry() const
{
return (flags2 & ENTRANCE_ELEMENT_FLAGS2_LEGACY_PATH_ENTRY) != 0;
}
ObjectEntryIndex EntranceElement::GetLegacyPathEntryIndex() const
2018-09-26 13:02:51 +02:00
{
if (HasLegacyPathEntry())
return PathType;
2021-09-15 22:22:15 +02:00
return OBJECT_ENTRY_INDEX_NULL;
}
const FootpathObject* EntranceElement::GetLegacyPathEntry() const
{
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
return static_cast<FootpathObject*>(objMgr.GetLoadedObject(ObjectType::Paths, GetLegacyPathEntryIndex()));
2018-09-26 13:02:51 +02:00
}
void EntranceElement::SetLegacyPathEntryIndex(ObjectEntryIndex newPathType)
2018-09-26 13:02:51 +02:00
{
2020-03-04 18:43:09 +01:00
PathType = newPathType;
flags2 |= ENTRANCE_ELEMENT_FLAGS2_LEGACY_PATH_ENTRY;
}
ObjectEntryIndex EntranceElement::GetSurfaceEntryIndex() const
{
if (HasLegacyPathEntry())
return OBJECT_ENTRY_INDEX_NULL;
2021-09-15 22:22:15 +02:00
return PathType;
}
const FootpathSurfaceObject* EntranceElement::GetSurfaceEntry() const
{
auto& objMgr = OpenRCT2::GetContext()->GetObjectManager();
return static_cast<FootpathSurfaceObject*>(objMgr.GetLoadedObject(ObjectType::FootpathSurface, GetSurfaceEntryIndex()));
}
void EntranceElement::SetSurfaceEntryIndex(ObjectEntryIndex newIndex)
{
PathType = newIndex;
flags2 &= ~ENTRANCE_ELEMENT_FLAGS2_LEGACY_PATH_ENTRY;
2018-09-26 14:52:16 +02:00
}
const PathSurfaceDescriptor* EntranceElement::GetPathSurfaceDescriptor() const
{
if (HasLegacyPathEntry())
{
const auto* legacyPathEntry = GetLegacyPathEntry();
if (legacyPathEntry == nullptr)
return nullptr;
return &legacyPathEntry->GetPathSurfaceDescriptor();
}
const auto* surfaceEntry = GetSurfaceEntry();
if (surfaceEntry == nullptr)
return nullptr;
return &surfaceEntry->GetDescriptor();
}