Files
OpenRCT2-Web/src/openrct2/ride/Track.cpp
T

782 lines
25 KiB
C++
Raw Normal View History

2014-04-23 01:50:40 +01:00
/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
2014-04-23 01:50:40 +01:00
*
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
2014-04-23 01:50:40 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-04-23 01:50:40 +01:00
*****************************************************************************/
2018-06-22 23:14:18 +02:00
#include "Track.h"
2017-12-13 13:02:24 +01:00
#include "../Cheats.h"
2024-07-09 16:23:31 +02:00
#include "../Diagnostic.h"
2017-11-30 18:17:06 +01:00
#include "../Game.h"
2024-03-03 21:44:15 +00:00
#include "../GameState.h"
2025-02-17 07:24:03 -05:00
#include "../audio/Audio.h"
2018-06-22 23:14:18 +02:00
#include "../config/Config.h"
2018-01-05 23:45:53 +00:00
#include "../interface/Viewport.h"
2017-10-06 22:37:06 +02:00
#include "../management/Finance.h"
2025-02-24 04:43:06 -05:00
#include "../network/Network.h"
2022-02-18 21:57:00 +01:00
#include "../platform/Platform.h"
2018-01-09 10:40:42 +01:00
#include "../rct1/RCT1.h"
#include "../ride/RideColour.h"
#include "../sawyer_coding/SawyerCoding.h"
2018-01-11 10:59:26 +01:00
#include "../world/Footpath.h"
2018-01-03 14:56:08 +01:00
#include "../world/Map.h"
2018-01-11 10:59:26 +01:00
#include "../world/MapAnimation.h"
2017-12-31 21:40:00 +01:00
#include "../world/Park.h"
2018-01-11 10:59:26 +01:00
#include "../world/Scenery.h"
2024-10-15 22:02:31 +02:00
#include "../world/tile_element/TileElement.h"
#include "../world/tile_element/TrackElement.h"
2017-12-31 13:21:34 +01:00
#include "Ride.h"
2018-01-10 00:00:09 +01:00
#include "RideData.h"
2018-06-22 23:14:18 +02:00
#include "RideRatings.h"
2017-10-23 14:47:12 +02:00
#include "Station.h"
2017-10-16 12:02:23 +02:00
#include "TrackData.h"
2018-03-19 23:28:40 +01:00
#include "TrackDesign.h"
2014-04-23 01:50:40 +01:00
2024-07-09 16:43:03 +02:00
#include <cassert>
2024-03-03 21:44:15 +00:00
using namespace OpenRCT2;
using namespace OpenRCT2::TrackMetaData;
using OpenRCT2::GameActions::CommandFlag;
using OpenRCT2::GameActions::CommandFlags;
2014-04-23 01:50:40 +01:00
2024-10-11 15:30:27 +02:00
PitchAndRoll TrackPitchAndRollStart(OpenRCT2::TrackElemType trackType)
2020-06-14 21:03:46 -03:00
{
const auto& ted = GetTrackElementDescriptor(trackType);
2024-08-11 14:52:25 +02:00
return { ted.definition.pitchStart, ted.definition.rollStart };
2020-06-14 21:03:46 -03:00
}
2024-10-11 15:30:27 +02:00
PitchAndRoll TrackPitchAndRollEnd(OpenRCT2::TrackElemType trackType)
2020-06-14 21:03:46 -03:00
{
const auto& ted = GetTrackElementDescriptor(trackType);
2024-08-11 14:52:25 +02:00
return { ted.definition.pitchEnd, ted.definition.rollEnd };
2020-06-14 21:03:46 -03:00
}
/**
* Helper method to determine if a connects to b by its bank and angle, not location.
*/
int32_t TrackIsConnectedByShape(TileElement* a, TileElement* b)
{
2024-01-14 23:36:12 +01:00
auto trackType = a->AsTrack()->GetTrackType();
const auto* ted = &GetTrackElementDescriptor(trackType);
2024-08-11 14:52:25 +02:00
auto aBank = ted->definition.rollEnd;
auto aAngle = ted->definition.pitchEnd;
aBank = TrackGetActualBank(a, aBank);
2018-09-17 21:54:36 +02:00
trackType = b->AsTrack()->GetTrackType();
ted = &GetTrackElementDescriptor(trackType);
2024-08-11 14:52:25 +02:00
auto bBank = ted->definition.rollStart;
auto bAngle = ted->definition.pitchStart;
bBank = TrackGetActualBank(b, bBank);
2017-06-06 23:24:18 +02:00
return aBank == bBank && aAngle == bAngle;
2015-02-17 17:00:47 +00:00
}
2015-02-26 19:02:26 +00:00
2022-01-19 15:17:11 +02:00
static TileElement* find_station_element(const CoordsXYZD& loc, RideId rideIndex)
2016-02-14 15:29:53 +00:00
{
TileElement* tileElement = MapGetFirstElementAt(loc);
if (tileElement == nullptr)
return nullptr;
2017-10-17 13:55:53 +02:00
do
{
2020-03-07 21:54:47 +01:00
if (loc.z != tileElement->GetBaseZ())
2017-10-17 13:55:53 +02:00
continue;
2021-12-11 00:39:39 +01:00
if (tileElement->GetType() != TileElementType::Track)
2017-10-17 13:55:53 +02:00
continue;
2020-03-07 22:14:29 +01:00
if (tileElement->GetDirection() != loc.direction)
2017-10-17 13:55:53 +02:00
continue;
2018-09-18 13:10:29 +02:00
if (tileElement->AsTrack()->GetRideIndex() != rideIndex)
2017-10-17 13:55:53 +02:00
continue;
2020-06-05 22:55:15 +02:00
if (!tileElement->AsTrack()->IsStation())
2017-10-17 13:55:53 +02:00
continue;
2016-02-14 15:29:53 +00:00
2017-10-31 14:03:45 +01:00
return tileElement;
2018-06-22 23:14:18 +02:00
} while (!(tileElement++)->IsLastForTile());
2017-10-17 13:55:53 +02:00
return nullptr;
2016-02-14 15:29:53 +00:00
}
2023-01-07 14:02:18 +01:00
static void ride_remove_station(Ride& ride, const CoordsXYZ& location)
2016-02-14 15:29:53 +00:00
{
2025-03-09 00:26:25 +01:00
for (auto& station : ride.getStations())
2017-07-18 12:23:24 +02:00
{
auto stationStart = station.GetStart();
2020-03-07 22:14:29 +01:00
if (stationStart == location)
2017-07-18 12:23:24 +02:00
{
station.Start.SetNull();
2025-03-09 00:26:25 +01:00
ride.numStations--;
2017-06-06 23:24:18 +02:00
break;
}
}
2016-02-14 15:29:53 +00:00
}
2015-09-05 20:34:56 +01:00
/**
*
* rct2: 0x006C4D89
*/
ResultWithMessage TrackAddStationElement(CoordsXYZD loc, RideId rideIndex, CommandFlags flags, bool fromTrackDesign)
2015-09-05 20:34:56 +01:00
{
auto ride = GetRide(rideIndex);
if (ride == nullptr)
return { false };
CoordsXY stationBackLoc = loc;
CoordsXY stationFrontLoc = loc;
int32_t stationLength = 1;
2016-02-14 15:29:53 +00:00
2025-03-09 00:26:25 +01:00
if (ride->getRideTypeDescriptor().HasFlag(RtdFlag::hasSinglePieceStation))
2017-10-17 13:55:53 +02:00
{
2025-03-09 00:26:25 +01:00
if (ride->numStations >= Limits::kMaxStationsPerRide)
2017-10-17 13:55:53 +02:00
{
return { false, STR_NO_MORE_STATIONS_ALLOWED_ON_THIS_RIDE };
2017-06-06 23:24:18 +02:00
}
if (flags.has(CommandFlag::apply))
2017-07-18 13:39:12 +02:00
{
auto stationIndex = RideGetFirstEmptyStationStart(*ride);
assert(!stationIndex.IsNull());
2016-02-14 15:29:53 +00:00
2025-03-09 00:26:25 +01:00
auto& station = ride->getStation(stationIndex);
station.Start.x = loc.x;
station.Start.y = loc.y;
2024-07-26 18:23:25 +10:00
station.Height = loc.z / kCoordsZStep;
station.Depart = 1;
station.Length = 0;
2025-03-09 00:26:25 +01:00
ride->numStations++;
2017-06-06 23:24:18 +02:00
}
return { true };
2017-06-06 23:24:18 +02:00
}
2016-02-14 15:29:53 +00:00
2018-11-01 13:53:50 +01:00
TileElement* stationElement;
2016-02-14 15:29:53 +00:00
2017-06-06 23:24:18 +02:00
// Search backwards for more station
loc = { stationBackLoc, loc.z, loc.direction };
2017-10-17 13:55:53 +02:00
do
{
2020-03-07 22:14:29 +01:00
loc -= CoordsDirectionDelta[loc.direction];
2016-02-14 15:29:53 +00:00
2020-03-07 22:14:29 +01:00
stationElement = find_station_element(loc, rideIndex);
2017-10-17 13:55:53 +02:00
if (stationElement != nullptr)
{
if (stationElement->AsTrack()->GetTrackType() == TrackElemType::EndStation)
2017-10-17 13:55:53 +02:00
{
if (flags.has(CommandFlag::apply))
2017-10-17 13:55:53 +02:00
{
2023-01-07 14:02:18 +01:00
ride_remove_station(*ride, loc);
2017-06-06 23:24:18 +02:00
}
}
2016-02-14 15:29:53 +00:00
stationBackLoc = loc;
2017-06-06 23:24:18 +02:00
stationLength++;
}
2018-06-22 23:14:18 +02:00
} while (stationElement != nullptr);
2016-02-14 15:29:53 +00:00
2017-06-06 23:24:18 +02:00
// Search forwards for more station
loc = { stationFrontLoc, loc.z, loc.direction };
2017-10-17 13:55:53 +02:00
do
{
2020-03-07 22:14:29 +01:00
loc += CoordsDirectionDelta[loc.direction];
2016-02-14 15:29:53 +00:00
2020-03-07 22:14:29 +01:00
stationElement = find_station_element(loc, rideIndex);
2017-10-17 13:55:53 +02:00
if (stationElement != nullptr)
{
if (stationElement->AsTrack()->GetTrackType() == TrackElemType::EndStation)
2017-10-17 13:55:53 +02:00
{
if (flags.has(CommandFlag::apply))
2017-10-17 13:55:53 +02:00
{
2023-01-07 14:02:18 +01:00
ride_remove_station(*ride, loc);
2017-06-06 23:24:18 +02:00
}
}
2016-02-14 15:29:53 +00:00
stationFrontLoc = loc;
2017-06-06 23:24:18 +02:00
stationLength++;
}
2018-06-22 23:14:18 +02:00
} while (stationElement != nullptr);
2016-02-14 15:29:53 +00:00
// When attempting to place a track design, it sometimes happens that the front and back of station 0 are built,
// but the middle is not. Allow this, so the track place function can actually finish building all 4 stations.
// This _might_ cause issues if the track designs is bugged and actually has 5.
2025-03-09 00:26:25 +01:00
if (stationBackLoc == stationFrontLoc && ride->numStations >= Limits::kMaxStationsPerRide && !fromTrackDesign)
2017-10-17 13:55:53 +02:00
{
return { false, STR_NO_MORE_STATIONS_ALLOWED_ON_THIS_RIDE };
2017-06-06 23:24:18 +02:00
}
2016-02-14 15:29:53 +00:00
if (stationLength > kMaxStationPlatformLength)
2017-10-17 13:55:53 +02:00
{
return { false, STR_STATION_PLATFORM_TOO_LONG };
2017-06-06 23:24:18 +02:00
}
2016-02-14 15:29:53 +00:00
if (flags.has(CommandFlag::apply))
2017-10-17 13:55:53 +02:00
{
loc = { stationFrontLoc, loc.z, loc.direction };
2016-02-14 15:29:53 +00:00
2017-06-06 23:24:18 +02:00
bool finaliseStationDone;
2017-10-17 13:55:53 +02:00
do
{
2017-06-06 23:24:18 +02:00
finaliseStationDone = true;
2016-02-14 15:29:53 +00:00
2020-03-07 22:14:29 +01:00
stationElement = find_station_element(loc, rideIndex);
2017-10-17 13:55:53 +02:00
if (stationElement != nullptr)
{
2024-10-11 15:30:27 +02:00
OpenRCT2::TrackElemType targetTrackType;
if (stationFrontLoc == loc)
2017-07-18 13:39:12 +02:00
{
auto stationIndex = RideGetFirstEmptyStationStart(*ride);
if (stationIndex.IsNull())
{
LOG_VERBOSE("No empty station starts, not updating metadata! This can happen with hacked rides.");
}
else
{
2025-03-09 00:26:25 +01:00
auto& station = ride->getStation(stationIndex);
station.Start = loc;
2024-07-26 18:23:25 +10:00
station.Height = loc.z / kCoordsZStep;
station.Depart = 1;
station.Length = stationLength;
2025-03-09 00:26:25 +01:00
ride->numStations++;
}
2016-02-14 15:29:53 +00:00
targetTrackType = TrackElemType::EndStation;
2017-10-17 13:55:53 +02:00
}
else if (stationBackLoc == loc)
2017-10-17 13:55:53 +02:00
{
targetTrackType = TrackElemType::BeginStation;
2017-10-17 13:55:53 +02:00
}
else
{
targetTrackType = TrackElemType::MiddleStation;
2017-06-06 23:24:18 +02:00
}
2018-09-17 21:54:36 +02:00
stationElement->AsTrack()->SetTrackType(targetTrackType);
2016-02-14 15:29:53 +00:00
MapInvalidateElement(loc, stationElement);
2016-02-14 15:29:53 +00:00
if (stationBackLoc != loc)
2017-10-17 13:55:53 +02:00
{
2020-03-07 22:14:29 +01:00
loc -= CoordsDirectionDelta[loc.direction];
2017-06-06 23:24:18 +02:00
finaliseStationDone = false;
}
}
2018-06-22 23:14:18 +02:00
} while (!finaliseStationDone);
2017-06-06 23:24:18 +02:00
}
return { true };
2015-09-05 20:34:56 +01:00
}
2016-02-14 17:37:28 +00:00
/**
*
* rct2: 0x006C494B
*/
ResultWithMessage TrackRemoveStationElement(const CoordsXYZD& loc, RideId rideIndex, CommandFlags flags)
2016-02-14 17:37:28 +00:00
{
auto ride = GetRide(rideIndex);
if (ride == nullptr)
return { false };
CoordsXYZD removeLoc = loc;
CoordsXYZD stationBackLoc = loc;
CoordsXYZD stationFrontLoc = loc;
int32_t stationLength = 0;
2023-01-21 16:41:03 +01:00
int32_t ByteF441D1 = -1;
2016-02-14 17:37:28 +00:00
2025-03-09 00:26:25 +01:00
if (ride->getRideTypeDescriptor().HasFlag(RtdFlag::hasSinglePieceStation))
2017-10-17 13:55:53 +02:00
{
TileElement* tileElement = MapGetTrackElementAtWithDirectionFromRide(loc, rideIndex);
2017-10-31 14:03:45 +01:00
if (tileElement != nullptr)
2017-10-17 13:55:53 +02:00
{
if (flags.has(CommandFlag::apply))
2017-10-17 13:55:53 +02:00
{
2023-01-07 14:02:18 +01:00
ride_remove_station(*ride, loc);
2017-06-06 23:24:18 +02:00
}
}
return { true };
2017-06-06 23:24:18 +02:00
}
2016-02-14 17:37:28 +00:00
2018-11-01 13:53:50 +01:00
TileElement* stationElement;
2016-02-14 17:37:28 +00:00
2017-06-06 23:24:18 +02:00
// Search backwards for more station
CoordsXYZD currentLoc = stationBackLoc;
while ((stationElement = find_station_element(currentLoc, rideIndex)) != nullptr)
2017-10-17 13:55:53 +02:00
{
if (stationElement->AsTrack()->GetTrackType() == TrackElemType::EndStation)
2017-10-17 13:55:53 +02:00
{
if (flags.has(CommandFlag::apply))
2017-10-17 13:55:53 +02:00
{
2023-01-07 14:02:18 +01:00
ride_remove_station(*ride, currentLoc);
2017-06-06 23:24:18 +02:00
}
}
2016-02-14 17:37:28 +00:00
stationBackLoc = currentLoc;
2023-01-21 16:41:03 +01:00
ByteF441D1++;
2016-02-14 17:37:28 +00:00
currentLoc -= CoordsDirectionDelta[currentLoc.direction];
2017-06-06 23:24:18 +02:00
}
2016-02-14 17:37:28 +00:00
2017-06-06 23:24:18 +02:00
// Search forwards for more station
currentLoc = stationFrontLoc;
2017-10-17 13:55:53 +02:00
do
{
currentLoc += CoordsDirectionDelta[currentLoc.direction];
2016-02-14 17:37:28 +00:00
stationElement = find_station_element(currentLoc, rideIndex);
2017-10-17 13:55:53 +02:00
if (stationElement != nullptr)
{
if (stationElement->AsTrack()->GetTrackType() == TrackElemType::EndStation)
2017-10-17 13:55:53 +02:00
{
if (flags.has(CommandFlag::apply))
2017-10-17 13:55:53 +02:00
{
2023-01-07 14:02:18 +01:00
ride_remove_station(*ride, currentLoc);
2017-06-06 23:24:18 +02:00
}
}
stationFrontLoc = currentLoc;
2017-06-06 23:24:18 +02:00
stationLength++;
}
2018-06-22 23:14:18 +02:00
} while (stationElement != nullptr);
2016-02-14 17:37:28 +00:00
if (!(flags.has(CommandFlag::apply)))
2017-10-17 13:55:53 +02:00
{
2025-03-09 00:26:25 +01:00
if ((removeLoc != stationBackLoc) && (removeLoc != stationFrontLoc) && ride->numStations >= Limits::kMaxStationsPerRide)
2017-10-17 13:55:53 +02:00
{
return { false, STR_NO_MORE_STATIONS_ALLOWED_ON_THIS_RIDE };
2017-10-17 13:55:53 +02:00
}
2021-09-15 22:22:15 +02:00
return { true };
2017-06-06 23:24:18 +02:00
}
2016-02-14 17:37:28 +00:00
currentLoc = stationFrontLoc;
2017-06-06 23:24:18 +02:00
bool finaliseStationDone;
2017-10-17 13:55:53 +02:00
do
{
2017-06-06 23:24:18 +02:00
finaliseStationDone = true;
2016-02-14 17:37:28 +00:00
if (currentLoc != removeLoc)
2017-10-17 13:55:53 +02:00
{
stationElement = find_station_element(currentLoc, rideIndex);
2017-10-17 13:55:53 +02:00
if (stationElement != nullptr)
{
2024-10-11 15:30:27 +02:00
OpenRCT2::TrackElemType targetTrackType;
if ((currentLoc == stationFrontLoc) || (currentLoc + CoordsDirectionDelta[currentLoc.direction] == removeLoc))
2017-07-18 13:39:12 +02:00
{
auto stationIndex = RideGetFirstEmptyStationStart(*ride);
if (stationIndex.IsNull())
{
LOG_VERBOSE("No empty station starts, not updating metadata! This can happen with hacked rides.");
}
else
{
2025-03-09 00:26:25 +01:00
auto& station = ride->getStation(stationIndex);
station.Start = currentLoc;
2024-07-26 18:23:25 +10:00
station.Height = currentLoc.z / kCoordsZStep;
station.Depart = 1;
2023-01-21 16:41:03 +01:00
station.Length = stationLength != 0 ? stationLength : ByteF441D1;
2025-03-09 00:26:25 +01:00
ride->numStations++;
}
2016-02-14 17:37:28 +00:00
2018-06-22 23:14:18 +02:00
stationLength = 0;
targetTrackType = TrackElemType::EndStation;
2017-10-17 13:55:53 +02:00
}
else
{
if (currentLoc - CoordsDirectionDelta[currentLoc.direction] == removeLoc)
2017-10-17 13:55:53 +02:00
{
targetTrackType = TrackElemType::BeginStation;
2017-10-17 13:55:53 +02:00
}
else
{
if (currentLoc == stationBackLoc)
2017-10-17 13:55:53 +02:00
{
targetTrackType = TrackElemType::BeginStation;
2017-10-17 13:55:53 +02:00
}
else
{
targetTrackType = TrackElemType::MiddleStation;
2017-06-06 23:24:18 +02:00
}
}
}
2018-09-17 21:54:36 +02:00
stationElement->AsTrack()->SetTrackType(targetTrackType);
2016-02-14 17:37:28 +00:00
MapInvalidateElement(currentLoc, stationElement);
2017-06-06 23:24:18 +02:00
}
}
2016-02-14 17:37:28 +00:00
if (currentLoc != stationBackLoc)
2017-10-17 13:55:53 +02:00
{
currentLoc -= CoordsDirectionDelta[currentLoc.direction];
2017-06-06 23:24:18 +02:00
finaliseStationDone = false;
}
2018-06-22 23:14:18 +02:00
} while (!finaliseStationDone);
2016-11-13 20:17:49 +01:00
return { true };
2016-02-14 17:37:28 +00:00
}
void TrackCircuitIteratorBegin(TrackCircuitIterator* it, CoordsXYE first)
{
2018-06-22 23:14:18 +02:00
it->last = first;
it->first = nullptr;
2017-06-06 23:24:18 +02:00
it->firstIteration = true;
2018-06-22 23:14:18 +02:00
it->looped = false;
}
bool TrackCircuitIteratorPrevious(TrackCircuitIterator* it)
2015-09-13 02:59:55 +01:00
{
TrackBeginEnd trackBeginEnd;
2015-09-13 02:59:55 +01:00
2017-10-17 13:55:53 +02:00
if (it->first == nullptr)
{
if (!TrackBlockGetPrevious({ it->last.x, it->last.y, it->last.element }, &trackBeginEnd))
2017-06-06 23:24:18 +02:00
return false;
2015-09-13 02:59:55 +01:00
2018-06-22 23:14:18 +02:00
it->current.x = trackBeginEnd.begin_x;
it->current.y = trackBeginEnd.begin_y;
it->current.element = trackBeginEnd.begin_element;
it->currentZ = trackBeginEnd.begin_z;
2017-06-06 23:24:18 +02:00
it->currentDirection = trackBeginEnd.begin_direction;
2015-09-13 02:59:55 +01:00
2017-06-06 23:24:18 +02:00
it->first = it->current.element;
return true;
2017-10-17 13:55:53 +02:00
}
2021-09-15 22:22:15 +02:00
if (!it->firstIteration && it->first == it->current.element)
2017-10-17 13:55:53 +02:00
{
2021-09-15 22:22:15 +02:00
it->looped = true;
return false;
2017-06-06 23:24:18 +02:00
}
2021-09-15 22:22:15 +02:00
it->firstIteration = false;
it->last = it->current;
if (TrackBlockGetPrevious({ it->last.x, it->last.y, it->last.element }, &trackBeginEnd))
2021-09-15 22:22:15 +02:00
{
it->current.x = trackBeginEnd.end_x;
it->current.y = trackBeginEnd.end_y;
it->current.element = trackBeginEnd.begin_element;
it->currentZ = trackBeginEnd.begin_z;
it->currentDirection = trackBeginEnd.begin_direction;
return true;
}
return false;
2015-09-13 02:59:55 +01:00
}
bool TrackCircuitIteratorNext(TrackCircuitIterator* it)
{
2017-10-17 13:55:53 +02:00
if (it->first == nullptr)
{
if (!TrackBlockGetNext(&it->last, &it->current, &it->currentZ, &it->currentDirection))
2017-06-06 23:24:18 +02:00
return false;
2017-06-06 23:24:18 +02:00
it->first = it->current.element;
return true;
2017-10-17 13:55:53 +02:00
}
2021-09-15 22:22:15 +02:00
if (!it->firstIteration && it->first == it->current.element)
{
it->looped = true;
return false;
2017-06-06 23:24:18 +02:00
}
2021-09-15 22:22:15 +02:00
it->firstIteration = false;
it->last = it->current;
return TrackBlockGetNext(&it->last, &it->current, &it->currentZ, &it->currentDirection);
}
bool TrackCircuitIteratorsMatch(const TrackCircuitIterator* firstIt, const TrackCircuitIterator* secondIt)
{
2018-06-22 23:14:18 +02:00
return (
firstIt->currentZ == secondIt->currentZ && firstIt->currentDirection == secondIt->currentDirection
&& firstIt->current.x == secondIt->current.x && firstIt->current.y == secondIt->current.y);
}
void TrackGetBack(const CoordsXYE& input, CoordsXYE* output)
{
2018-06-22 23:14:18 +02:00
CoordsXYE lastTrack;
TrackBeginEnd currentTrack;
2018-06-22 23:14:18 +02:00
bool result;
lastTrack = input;
2017-10-17 13:55:53 +02:00
do
{
result = TrackBlockGetPrevious(lastTrack, &currentTrack);
2017-10-17 13:55:53 +02:00
if (result)
{
2018-06-22 23:14:18 +02:00
lastTrack.x = currentTrack.begin_x;
lastTrack.y = currentTrack.begin_y;
2017-06-06 23:24:18 +02:00
lastTrack.element = currentTrack.begin_element;
}
2018-06-22 23:14:18 +02:00
} while (result);
2017-06-06 23:24:18 +02:00
*output = lastTrack;
}
void TrackGetFront(const CoordsXYE& input, CoordsXYE* output)
{
2018-02-15 17:42:53 +01:00
CoordsXYE lastTrack, currentTrack;
2018-06-22 23:14:18 +02:00
int32_t z, direction;
bool result;
lastTrack = input;
2017-10-17 13:55:53 +02:00
do
{
result = TrackBlockGetNext(&lastTrack, &currentTrack, &z, &direction);
2017-10-17 13:55:53 +02:00
if (result)
{
2017-06-06 23:24:18 +02:00
lastTrack = currentTrack;
}
2018-06-22 23:14:18 +02:00
} while (result);
2017-06-06 23:24:18 +02:00
*output = lastTrack;
}
TrackElement* TrackGetPreviousBlock(CoordsXYZ& location, TileElement* tileElement)
{
CoordsXYZ startLocation = location;
TrackBeginEnd trackBeginEnd, slowTrackBeginEnd;
TileElement slowTileElement = *tileElement;
bool counter = true;
CoordsXY slowLocation = location;
do
{
if (!TrackBlockGetPrevious({ location, tileElement }, &trackBeginEnd))
{
return nullptr;
}
if (trackBeginEnd.begin_x == startLocation.x && trackBeginEnd.begin_y == startLocation.y
&& tileElement == trackBeginEnd.begin_element)
{
return nullptr;
}
location.x = trackBeginEnd.end_x;
location.y = trackBeginEnd.end_y;
location.z = trackBeginEnd.begin_z;
tileElement = trackBeginEnd.begin_element;
// #2081: prevent infinite loop
counter = !counter;
if (counter)
{
TrackBlockGetPrevious({ slowLocation, &slowTileElement }, &slowTrackBeginEnd);
slowLocation.x = slowTrackBeginEnd.end_x;
slowLocation.y = slowTrackBeginEnd.end_y;
slowTileElement = *(slowTrackBeginEnd.begin_element);
if (slowLocation == location && slowTileElement.GetBaseZ() == tileElement->GetBaseZ()
&& slowTileElement.GetType() == tileElement->GetType()
&& slowTileElement.GetDirection() == tileElement->GetDirection())
{
return nullptr;
}
}
} while (!(trackBeginEnd.begin_element->AsTrack()->IsBlockStart()));
// Get the start of the track block instead of the end
location = { trackBeginEnd.begin_x, trackBeginEnd.begin_y, trackBeginEnd.begin_z };
auto trackOrigin = MapGetTrackElementAtOfTypeSeq(location, trackBeginEnd.begin_element->AsTrack()->GetTrackType(), 0);
if (trackOrigin == nullptr)
{
return nullptr;
}
return trackOrigin->AsTrack();
}
2024-01-14 23:46:28 +01:00
TrackRoll TrackGetActualBank(TileElement* tileElement, TrackRoll bank)
2015-11-22 12:14:53 +00:00
{
auto ride = GetRide(tileElement->AsTrack()->GetRideIndex());
if (ride != nullptr)
{
bool isInverted = tileElement->AsTrack()->IsInverted();
return TrackGetActualBank2(ride->type, isInverted, bank);
}
return bank;
2015-11-22 12:14:53 +00:00
}
2024-12-22 19:59:36 +01:00
TrackRoll TrackGetActualBank2(ride_type_t rideType, bool isInverted, TrackRoll bank)
2015-11-22 12:14:53 +00:00
{
2024-08-13 22:20:25 +02:00
if (GetRideTypeDescriptor(rideType).HasFlag(RtdFlag::hasInvertedVariant))
2017-10-17 13:55:53 +02:00
{
if (isInverted)
2017-10-17 13:55:53 +02:00
{
2024-01-14 23:46:28 +01:00
if (bank == TrackRoll::None)
2017-10-17 13:55:53 +02:00
{
2024-01-14 23:46:28 +01:00
bank = TrackRoll::UpsideDown;
2017-10-17 13:55:53 +02:00
}
2024-01-14 23:46:28 +01:00
else if (bank == TrackRoll::UpsideDown)
2017-10-17 13:55:53 +02:00
{
2024-01-14 23:46:28 +01:00
bank = TrackRoll::None;
2017-06-06 23:24:18 +02:00
}
}
}
return bank;
2015-11-22 12:14:53 +00:00
}
2015-11-22 14:55:46 +00:00
2024-01-14 23:46:28 +01:00
TrackRoll TrackGetActualBank3(bool useInvertedSprites, TileElement* tileElement)
2015-12-13 16:10:30 +00:00
{
auto trackType = tileElement->AsTrack()->GetTrackType();
const auto& ted = GetTrackElementDescriptor(trackType);
2024-08-11 14:52:25 +02:00
auto bankStart = ted.definition.rollStart;
auto ride = GetRide(tileElement->AsTrack()->GetRideIndex());
if (ride == nullptr)
return bankStart;
bool isInverted = useInvertedSprites ^ tileElement->AsTrack()->IsInverted();
return TrackGetActualBank2(ride->type, isInverted, bankStart);
2015-12-13 16:10:30 +00:00
}
2024-10-11 15:30:27 +02:00
bool TrackTypeIsStation(OpenRCT2::TrackElemType trackType)
2020-06-05 22:55:15 +02:00
{
switch (trackType)
2017-10-17 13:55:53 +02:00
{
case TrackElemType::EndStation:
case TrackElemType::BeginStation:
case TrackElemType::MiddleStation:
2018-06-22 23:14:18 +02:00
return true;
default:
return false;
2017-06-06 23:24:18 +02:00
}
2015-11-22 14:55:46 +00:00
}
2015-11-22 17:50:33 +00:00
2024-10-11 15:30:27 +02:00
bool TrackTypeIsBrakes(OpenRCT2::TrackElemType trackType)
2022-01-28 00:46:35 -08:00
{
switch (trackType)
{
case TrackElemType::Brakes:
case TrackElemType::DiagBrakes:
case TrackElemType::Down25Brakes:
case TrackElemType::DiagDown25Brakes:
return true;
default:
return false;
}
2022-01-28 00:46:35 -08:00
}
2024-10-11 15:30:27 +02:00
bool TrackTypeIsBlockBrakes(OpenRCT2::TrackElemType trackType)
2022-01-28 00:46:35 -08:00
{
return (trackType == TrackElemType::BlockBrakes) || (trackType == TrackElemType::DiagBlockBrakes);
}
2024-10-11 15:30:27 +02:00
bool TrackTypeIsBooster(OpenRCT2::TrackElemType trackType)
2023-12-02 09:53:16 -08:00
{
switch (trackType)
{
case TrackElemType::Booster:
case TrackElemType::DiagBooster:
return true;
default:
return false;
}
2023-12-02 09:53:16 -08:00
}
2024-10-11 15:30:27 +02:00
bool TrackElementIsCovered(OpenRCT2::TrackElemType trackElementType)
2015-11-22 17:50:33 +00:00
{
2017-10-17 13:55:53 +02:00
switch (trackElementType)
{
case TrackElemType::FlatCovered:
case TrackElemType::Up25Covered:
case TrackElemType::Up60Covered:
case TrackElemType::FlatToUp25Covered:
case TrackElemType::Up25ToUp60Covered:
case TrackElemType::Up60ToUp25Covered:
case TrackElemType::Up25ToFlatCovered:
case TrackElemType::Down25Covered:
case TrackElemType::Down60Covered:
case TrackElemType::FlatToDown25Covered:
case TrackElemType::Down25ToDown60Covered:
case TrackElemType::Down60ToDown25Covered:
case TrackElemType::Down25ToFlatCovered:
case TrackElemType::LeftQuarterTurn5TilesCovered:
case TrackElemType::RightQuarterTurn5TilesCovered:
case TrackElemType::SBendLeftCovered:
case TrackElemType::SBendRightCovered:
case TrackElemType::LeftQuarterTurn3TilesCovered:
case TrackElemType::RightQuarterTurn3TilesCovered:
2018-06-22 23:14:18 +02:00
return true;
default:
return false;
2017-06-06 23:24:18 +02:00
}
2015-11-22 17:50:33 +00:00
}
2017-06-20 14:23:49 +02:00
2024-10-11 15:30:27 +02:00
OpenRCT2::TrackElemType UncoverTrackElement(OpenRCT2::TrackElemType trackElementType)
{
switch (trackElementType)
{
case TrackElemType::FlatCovered:
return TrackElemType::Flat;
case TrackElemType::Up25Covered:
return TrackElemType::Up25;
case TrackElemType::Up60Covered:
return TrackElemType::Up60;
case TrackElemType::FlatToUp25Covered:
return TrackElemType::FlatToUp25;
case TrackElemType::Up25ToUp60Covered:
return TrackElemType::Up25ToUp60;
case TrackElemType::Up60ToUp25Covered:
return TrackElemType::Up60ToUp25;
case TrackElemType::Up25ToFlatCovered:
return TrackElemType::Up25ToFlat;
case TrackElemType::Down25Covered:
return TrackElemType::Down25;
case TrackElemType::Down60Covered:
return TrackElemType::Down60;
case TrackElemType::FlatToDown25Covered:
return TrackElemType::FlatToDown25;
case TrackElemType::Down25ToDown60Covered:
return TrackElemType::Down25ToDown60;
case TrackElemType::Down60ToDown25Covered:
return TrackElemType::Down60ToDown25;
case TrackElemType::Down25ToFlatCovered:
return TrackElemType::Down25ToFlat;
case TrackElemType::LeftQuarterTurn5TilesCovered:
return TrackElemType::LeftQuarterTurn5Tiles;
case TrackElemType::RightQuarterTurn5TilesCovered:
return TrackElemType::RightQuarterTurn5Tiles;
case TrackElemType::SBendLeftCovered:
return TrackElemType::SBendLeft;
case TrackElemType::SBendRightCovered:
return TrackElemType::SBendRight;
case TrackElemType::LeftQuarterTurn3TilesCovered:
return TrackElemType::LeftQuarterTurn3Tiles;
case TrackElemType::RightQuarterTurn3TilesCovered:
return TrackElemType::RightQuarterTurn3Tiles;
default:
return trackElementType;
}
}
2024-10-11 15:30:27 +02:00
bool TrackTypeHasSpeedSetting(OpenRCT2::TrackElemType trackType)
2017-06-20 14:23:49 +02:00
{
2023-12-02 09:53:16 -08:00
return TrackTypeIsBooster(trackType) || TrackTypeIsBrakes(trackType) || TrackTypeIsBlockBrakes(trackType);
2017-06-20 14:23:49 +02:00
}
2017-10-04 17:14:53 +02:00
2024-10-11 15:30:27 +02:00
bool TrackTypeIsHelix(OpenRCT2::TrackElemType trackType)
2023-01-03 00:10:07 +01:00
{
if (trackType >= TrackElemType::LeftHalfBankedHelixUpSmall && trackType <= TrackElemType::RightHalfBankedHelixDownLarge)
return true;
if (trackType >= TrackElemType::LeftQuarterBankedHelixLargeUp && trackType <= TrackElemType::RightQuarterHelixLargeDown)
return true;
return false;
}
2022-03-13 03:02:55 +00:00
std::optional<CoordsXYZD> GetTrackSegmentOrigin(const CoordsXYE& posEl)
{
if (posEl.element == nullptr)
return {};
2022-03-13 03:02:55 +00:00
auto trackEl = posEl.element->AsTrack();
if (trackEl == nullptr)
return {};
const auto& ted = GetTrackElementDescriptor(trackEl->GetTrackType());
auto direction = trackEl->GetDirection();
auto coords = CoordsXYZ(posEl.x, posEl.y, trackEl->GetBaseZ());
// Subtract the current sequence's offset
2024-09-12 22:48:21 +02:00
auto sequenceIndex = trackEl->GetSequenceIndex();
if (sequenceIndex >= ted.numSequences)
2022-12-10 22:31:23 +01:00
return {};
2024-09-12 22:48:21 +02:00
const auto& trackBlock = ted.sequences[sequenceIndex].clearance;
CoordsXY trackBlockOffset = { trackBlock.x, trackBlock.y };
coords += trackBlockOffset.Rotate(DirectionReverse(direction));
coords.z -= trackBlock.z;
2022-03-13 03:02:55 +00:00
return CoordsXYZD(coords, direction);
}