Close #12331: Use CoordsXY in track_remove_station_element (#12609)

* Update track_remove_station_element signature

Part of #12331: This commit will update the
signature of track_remove_station_element and
its callers, to use CoordsXYZD.

* Fix track_remove_station_element local vars

Part of #12331: This commit will ensure that
references to the previous arguments in
track_remove_station_element will now use the new
CoordsXYZD argument.

* Refactor remove in track_remove_station_element

Part of #12331: This commit refactors the removeX/Y
to use CoordsXY.

* Refactor station0 in track_remove_station_element

Part of #12331: This commit will refactor stationX/Y0
to use CoordsXY.

* Close #12331 track_remove_station_element refactor

This commit refactors stationX/Y1 to use CoordsXY,
and fixes the smallZ vs bigZ problem in callers of
track_remove_station_element. These are the final
changes for this issue.

* Added operator for CoordsXYZD minus CoordsXY.

This commit adds a missing operator for subtracting
a CoordsXY from a CoordsXYZD. This was needed for
refactoring Track.cpp

* Refactor track_remove_station_element Coord use

This commit utilises the overloaded operators for
Coords and ensures that they are used so that the
function implementation is more readable.

* Close #12331 track_remove_station_element refactor

This commit also fixes a bug in which a small Z
value (the Height of a RideStation) was being
updated with a big Z value without scaling. It
adds a few extra refactors in calls of the
track_remove_station_element function, and
changes the contributors to add ion232.
This commit is contained in:
Arran Ireland 2020-08-09 07:25:44 +01:00 committed by GitHub
parent 4de58e1fd4
commit 89e4714198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 48 deletions

View File

@ -151,6 +151,7 @@ The following people are not part of the development team, but have been contrib
* Sidney Kuyateh (autinerd)
* Łukasz Pękalski (Lukasz-Pe)
* (quale)
* Arran Ireland (ion232)
## Toolchain
* (Balletie) - macOS

View File

@ -216,7 +216,7 @@ public:
if (entranceDirections & TRACK_SEQUENCE_FLAG_ORIGIN && (tileElement->AsTrack()->GetSequenceIndex() == 0))
{
if (!track_remove_station_element(mapLoc.x, mapLoc.y, mapLoc.z / 8, _origin.direction, rideIndex, 0))
if (!track_remove_station_element({ mapLoc, _origin.direction }, rideIndex, 0))
{
return MakeResult(GA_ERROR::UNKNOWN, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, gGameCommandErrorText);
}
@ -406,7 +406,7 @@ public:
if (entranceDirections & TRACK_SEQUENCE_FLAG_ORIGIN && (tileElement->AsTrack()->GetSequenceIndex() == 0))
{
if (!track_remove_station_element(mapLoc.x, mapLoc.y, mapLoc.z / 8, _origin.direction, rideIndex, 0))
if (!track_remove_station_element({ mapLoc, _origin.direction }, rideIndex, 0))
{
return MakeResult(GA_ERROR::UNKNOWN, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, gGameCommandErrorText);
}
@ -429,8 +429,7 @@ public:
if (entranceDirections & TRACK_SEQUENCE_FLAG_ORIGIN && (tileElement->AsTrack()->GetSequenceIndex() == 0))
{
if (!track_remove_station_element(
mapLoc.x, mapLoc.y, mapLoc.z / 8, _origin.direction, rideIndex, GAME_COMMAND_FLAG_APPLY))
if (!track_remove_station_element({ mapLoc, _origin.direction }, rideIndex, GAME_COMMAND_FLAG_APPLY))
{
return MakeResult(GA_ERROR::UNKNOWN, STR_RIDE_CONSTRUCTION_CANT_REMOVE_THIS, gGameCommandErrorText);
}

View File

@ -797,29 +797,26 @@ bool track_add_station_element(CoordsXYZD loc, ride_id_t rideIndex, int32_t flag
*
* rct2: 0x006C494B
*/
bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction direction, ride_id_t rideIndex, int32_t flags)
bool track_remove_station_element(const CoordsXYZD& loc, ride_id_t rideIndex, int32_t flags)
{
auto ride = get_ride(rideIndex);
if (ride == nullptr)
return false;
int32_t removeX = x;
int32_t removeY = y;
int32_t stationX0 = x;
int32_t stationY0 = y;
int32_t stationX1 = x;
int32_t stationY1 = y;
CoordsXYZD removeLoc = loc;
CoordsXYZD stationBackLoc = loc;
CoordsXYZD stationFrontLoc = loc;
int32_t stationLength = 0;
int32_t byte_F441D1 = -1;
if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_SINGLE_PIECE_STATION))
{
TileElement* tileElement = map_get_track_element_at_with_direction_from_ride({ x, y, z << 3, direction }, rideIndex);
TileElement* tileElement = map_get_track_element_at_with_direction_from_ride(loc, rideIndex);
if (tileElement != nullptr)
{
if (flags & GAME_COMMAND_FLAG_APPLY)
{
ride_remove_station(ride, { x, y, z * COORDS_Z_STEP });
ride_remove_station(ride, loc);
}
}
return true;
@ -828,55 +825,47 @@ bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction dir
TileElement* stationElement;
// Search backwards for more station
x = stationX0;
y = stationY0;
while ((stationElement = find_station_element({ x, y, z * COORDS_Z_STEP, direction }, rideIndex)) != nullptr)
CoordsXYZD currentLoc = stationBackLoc;
while ((stationElement = find_station_element(currentLoc, rideIndex)) != nullptr)
{
if (stationElement->AsTrack()->GetTrackType() == TRACK_ELEM_END_STATION)
{
if (flags & GAME_COMMAND_FLAG_APPLY)
{
ride_remove_station(ride, { x, y, z * COORDS_Z_STEP });
ride_remove_station(ride, currentLoc);
}
}
stationX0 = x;
stationY0 = y;
stationBackLoc = currentLoc;
byte_F441D1++;
x -= CoordsDirectionDelta[direction].x;
y -= CoordsDirectionDelta[direction].y;
currentLoc -= CoordsDirectionDelta[currentLoc.direction];
}
// Search forwards for more station
x = stationX1;
y = stationY1;
currentLoc = stationFrontLoc;
do
{
x += CoordsDirectionDelta[direction].x;
y += CoordsDirectionDelta[direction].y;
currentLoc += CoordsDirectionDelta[currentLoc.direction];
stationElement = find_station_element({ x, y, z * COORDS_Z_STEP, direction }, rideIndex);
stationElement = find_station_element(currentLoc, rideIndex);
if (stationElement != nullptr)
{
if (stationElement->AsTrack()->GetTrackType() == TRACK_ELEM_END_STATION)
{
if (flags & GAME_COMMAND_FLAG_APPLY)
{
ride_remove_station(ride, { x, y, z * COORDS_Z_STEP });
ride_remove_station(ride, currentLoc);
}
}
stationX1 = x;
stationY1 = y;
stationFrontLoc = currentLoc;
stationLength++;
}
} while (stationElement != nullptr);
if (!(flags & GAME_COMMAND_FLAG_APPLY))
{
if ((removeX != stationX0 || removeY != stationY0) && (removeX != stationX1 || removeY != stationY1)
&& ride->num_stations >= MAX_STATIONS)
if ((removeLoc != stationBackLoc) && (removeLoc != stationFrontLoc) && ride->num_stations >= MAX_STATIONS)
{
gGameCommandErrorText = STR_NO_MORE_STATIONS_ALLOWED_ON_THIS_RIDE;
return false;
@ -887,21 +876,19 @@ bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction dir
}
}
x = stationX1;
y = stationY1;
currentLoc = stationFrontLoc;
bool finaliseStationDone;
do
{
finaliseStationDone = true;
if (x != removeX || y != removeY)
if (currentLoc != removeLoc)
{
stationElement = find_station_element({ x, y, z * COORDS_Z_STEP, direction }, rideIndex);
stationElement = find_station_element(currentLoc, rideIndex);
if (stationElement != nullptr)
{
int32_t targetTrackType;
if ((x == stationX1 && y == stationY1)
|| (x + CoordsDirectionDelta[direction].x == removeX && y + CoordsDirectionDelta[direction].y == removeY))
if ((currentLoc == stationFrontLoc) || (currentLoc + CoordsDirectionDelta[currentLoc.direction] == removeLoc))
{
auto stationIndex = ride_get_first_empty_station_start(ride);
if (stationIndex == STATION_INDEX_NULL)
@ -910,9 +897,8 @@ bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction dir
}
else
{
ride->stations[stationIndex].Start.x = x;
ride->stations[stationIndex].Start.y = y;
ride->stations[stationIndex].Height = z;
ride->stations[stationIndex].Start = currentLoc;
ride->stations[stationIndex].Height = currentLoc.z / COORDS_Z_STEP;
ride->stations[stationIndex].Depart = 1;
ride->stations[stationIndex].Length = stationLength != 0 ? stationLength : byte_F441D1;
ride->num_stations++;
@ -923,13 +909,13 @@ bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction dir
}
else
{
if (x - CoordsDirectionDelta[direction].x == removeX && y - CoordsDirectionDelta[direction].y == removeY)
if (currentLoc - CoordsDirectionDelta[currentLoc.direction] == removeLoc)
{
targetTrackType = TRACK_ELEM_BEGIN_STATION;
}
else
{
if (x == stationX0 && y == stationY0)
if (currentLoc == stationBackLoc)
{
targetTrackType = TRACK_ELEM_BEGIN_STATION;
}
@ -941,14 +927,13 @@ bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction dir
}
stationElement->AsTrack()->SetTrackType(targetTrackType);
map_invalidate_element({ x, y }, stationElement);
map_invalidate_element(currentLoc, stationElement);
}
}
if (x != stationX0 || y != stationY0)
if (currentLoc != stationBackLoc)
{
x -= CoordsDirectionDelta[direction].x;
y -= CoordsDirectionDelta[direction].y;
currentLoc -= CoordsDirectionDelta[currentLoc.direction];
finaliseStationDone = false;
}
} while (!finaliseStationDone);

View File

@ -580,7 +580,7 @@ roll_type_t track_get_actual_bank_2(int32_t rideType, bool isInverted, roll_type
roll_type_t track_get_actual_bank_3(bool useInvertedSprites, TileElement* tileElement);
bool track_add_station_element(CoordsXYZD loc, ride_id_t rideIndex, int32_t flags, bool fromTrackDesign);
bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction direction, ride_id_t rideIndex, int32_t flags);
bool track_remove_station_element(const CoordsXYZD& loc, ride_id_t rideIndex, int32_t flags);
money32 maze_set_track(
uint16_t x, uint16_t y, uint16_t z, uint8_t flags, bool initialPlacement, uint8_t direction, ride_id_t rideIndex,

View File

@ -553,6 +553,11 @@ struct CoordsXYZD : public CoordsXYZ
return { x + rhs.x, y + rhs.y, z + rhs.z, direction };
}
const CoordsXYZD operator-(const CoordsXY& rhs) const
{
return { x - rhs.x, y - rhs.y, z, direction };
}
const CoordsXYZD operator-(const CoordsXYZ& rhs) const
{
return { x - rhs.x, y - rhs.y, z - rhs.z, direction };