From fb9fab69a9680d6e1196cbc7cb6a96cf20443ac9 Mon Sep 17 00:00:00 2001 From: Helio Santos Date: Fri, 10 Jul 2020 08:57:51 -0700 Subject: [PATCH 1/3] Part of #12159: Uses CoordXY on find_closest_mechanic Updated find_closest_mechanic prototype to use CoordXY. Updated caller(s) accordingly. --- src/openrct2/ride/Ride.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 2765808647..82f0ba1abb 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -123,7 +123,7 @@ Direction gRideEntranceExitPlaceDirection; uint8_t gLastEntranceStyle; // Static function declarations -Peep* find_closest_mechanic(int32_t x, int32_t y, int32_t forInspection); +Peep* find_closest_mechanic(const CoordsXY& entrancePosition, int32_t forInspection); static void ride_breakdown_status_update(Ride* ride); static void ride_breakdown_update(Ride* ride); static void ride_call_closest_mechanic(Ride* ride); @@ -2670,7 +2670,7 @@ Peep* ride_find_closest_mechanic(Ride* ride, int32_t forInspection) // Set x,y to centre of the station exit for the mechanic search. auto centreMapLocation = mapLocation.ToTileCentre(); - return find_closest_mechanic(centreMapLocation.x, centreMapLocation.y, forInspection); + return find_closest_mechanic(centreMapLocation, forInspection); } /** @@ -2678,7 +2678,7 @@ Peep* ride_find_closest_mechanic(Ride* ride, int32_t forInspection) * rct2: 0x006B774B (forInspection = 0) * rct2: 0x006B78C3 (forInspection = 1) */ -Peep* find_closest_mechanic(int32_t x, int32_t y, int32_t forInspection) +Peep* find_closest_mechanic(const CoordsXY& entrancePosition, int32_t forInspection) { Peep* closestMechanic = nullptr; uint32_t closestDistance = std::numeric_limits::max(); @@ -2707,7 +2707,7 @@ Peep* find_closest_mechanic(int32_t x, int32_t y, int32_t forInspection) continue; } - auto location = CoordsXY(x, y).ToTileStart(); + auto location = entrancePosition.ToTileStart(); if (map_is_location_in_park(location)) if (!peep->AsStaff()->IsLocationInPatrol(location)) continue; @@ -2716,7 +2716,7 @@ Peep* find_closest_mechanic(int32_t x, int32_t y, int32_t forInspection) continue; // Manhattan distance - uint32_t distance = std::abs(peep->x - x) + std::abs(peep->y - y); + uint32_t distance = std::abs(peep->x - entrancePosition.x) + std::abs(peep->y - entrancePosition.y); if (distance < closestDistance) { closestDistance = distance; From b0a4f2a97ebb04b6a1a0f484998dc89d98fc4c3f Mon Sep 17 00:00:00 2001 From: Helio Santos Date: Fri, 10 Jul 2020 09:40:23 -0700 Subject: [PATCH 2/3] Part of #12159: updated get_station_platform to use CoordsXYRangedZ Updated get_station_platform function to use CoordsXYRangedZ. Updated caller(s) accordingly. Implemented constructor for CoordsXYRangedZ that takes a CoordsXYZ as argument. --- src/openrct2/ride/Ride.cpp | 10 ++-- src/openrct2/ride/Ride.h | 2 +- src/openrct2/ride/Vehicle.cpp | 2 +- src/openrct2/world/Location.hpp | 93 ++++++++++++++++++--------------- 4 files changed, 57 insertions(+), 50 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 82f0ba1abb..b4337d4de8 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -7211,10 +7211,10 @@ money16 ride_get_price(const Ride* ride) * Return the tile_element of an adjacent station at x,y,z(+-2). * Returns nullptr if no suitable tile_element is found. */ -TileElement* get_station_platform(int32_t x, int32_t y, int32_t z, int32_t z_tolerance) +TileElement* get_station_platform(const CoordsXYRangedZ& coords) { bool foundTileElement = false; - TileElement* tileElement = map_get_first_element_at({ x, y }); + TileElement* tileElement = map_get_first_element_at(coords); if (tileElement != nullptr) { do @@ -7225,7 +7225,7 @@ TileElement* get_station_platform(int32_t x, int32_t y, int32_t z, int32_t z_tol if (!tileElement->AsTrack()->IsStation()) continue; - if (z - z_tolerance > tileElement->GetBaseZ() || z + z_tolerance < tileElement->GetBaseZ()) + if (coords.baseZ > tileElement->GetBaseZ() || coords.clearanceZ < tileElement->GetBaseZ()) { /* The base height if tileElement is not within * the z tolerance. */ @@ -7256,7 +7256,7 @@ static bool check_for_adjacent_station(int32_t x, int32_t y, int32_t z, uint8_t { adjX += CoordsDirectionDelta[direction].x; adjY += CoordsDirectionDelta[direction].y; - TileElement* stationElement = get_station_platform(adjX, adjY, z, 2 * COORDS_Z_STEP); + TileElement* stationElement = get_station_platform({ { adjX, adjY, z }, z + 2 * COORDS_Z_STEP }); if (stationElement != nullptr) { auto rideIndex = stationElement->AsTrack()->GetRideIndex(); @@ -7285,7 +7285,7 @@ bool ride_has_adjacent_station(Ride* ride) if (!stationStart.isNull()) { /* Get the map element for the station start. */ - TileElement* stationElement = get_station_platform(stationStart.x, stationStart.y, stationStart.z, 0); + TileElement* stationElement = get_station_platform({ stationStart, stationStart.z + 0 }); if (stationElement == nullptr) { continue; diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index dc82a5432e..cce23d736d 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -1201,7 +1201,7 @@ void window_ride_construction_do_entrance_exit_check(); money16 ride_get_price(const Ride* ride); -TileElement* get_station_platform(int32_t x, int32_t y, int32_t z, int32_t z_tolerance); +TileElement* get_station_platform(const CoordsXYRangedZ& coords); bool ride_has_adjacent_station(Ride* ride); bool ride_has_station_shelter(Ride* ride); bool ride_has_ratings(const Ride* ride); diff --git a/src/openrct2/ride/Vehicle.cpp b/src/openrct2/ride/Vehicle.cpp index b9dc2b3cea..374c4c8886 100644 --- a/src/openrct2/ride/Vehicle.cpp +++ b/src/openrct2/ride/Vehicle.cpp @@ -2698,7 +2698,7 @@ static bool try_add_synchronised_station(const CoordsXYZ& coords) return false; } - TileElement* tileElement = get_station_platform(coords.x, coords.y, coords.z, 2 * COORDS_Z_STEP); + TileElement* tileElement = get_station_platform({ coords, coords.z + 2 * COORDS_Z_STEP }); if (tileElement == nullptr) { /* No station platform element found, diff --git a/src/openrct2/world/Location.hpp b/src/openrct2/world/Location.hpp index c7567e14cf..1fac1d938c 100644 --- a/src/openrct2/world/Location.hpp +++ b/src/openrct2/world/Location.hpp @@ -218,6 +218,49 @@ struct CoordsXY } }; +struct CoordsXYZ : public CoordsXY +{ + int32_t z = 0; + + CoordsXYZ() = default; + constexpr CoordsXYZ(int32_t _x, int32_t _y, int32_t _z) + : CoordsXY(_x, _y) + , z(_z) + { + } + + constexpr CoordsXYZ(const CoordsXY& c, int32_t _z) + : CoordsXY(c) + , z(_z) + { + } + + const CoordsXYZ operator+(const CoordsXYZ& rhs) const + { + return { x + rhs.x, y + rhs.y, z + rhs.z }; + } + + const CoordsXYZ operator-(const CoordsXYZ& rhs) const + { + return { x - rhs.x, y - rhs.y, z - rhs.z }; + } + + bool operator==(const CoordsXYZ& other) const + { + return x == other.x && y == other.y && z == other.z; + } + + CoordsXYZ ToTileStart() const + { + return { floor2(x, COORDS_XY_STEP), floor2(y, COORDS_XY_STEP), z }; + } + + CoordsXYZ ToTileCentre() const + { + return ToTileStart() + CoordsXYZ{ COORDS_XY_HALF_TILE, COORDS_XY_HALF_TILE, 0 }; + } +}; + struct CoordsXYRangedZ : public CoordsXY { int32_t baseZ = 0; @@ -237,6 +280,13 @@ struct CoordsXYRangedZ : public CoordsXY , clearanceZ(_clearanceZ) { } + + constexpr CoordsXYRangedZ(const CoordsXYZ& _c, int32_t _clearanceZ) + : CoordsXY(_c) + , baseZ(_c.z) + , clearanceZ(_clearanceZ) + { + } }; struct TileCoordsXY @@ -337,49 +387,6 @@ struct TileCoordsXY } }; -struct CoordsXYZ : public CoordsXY -{ - int32_t z = 0; - - CoordsXYZ() = default; - constexpr CoordsXYZ(int32_t _x, int32_t _y, int32_t _z) - : CoordsXY(_x, _y) - , z(_z) - { - } - - constexpr CoordsXYZ(const CoordsXY& c, int32_t _z) - : CoordsXY(c) - , z(_z) - { - } - - const CoordsXYZ operator+(const CoordsXYZ& rhs) const - { - return { x + rhs.x, y + rhs.y, z + rhs.z }; - } - - const CoordsXYZ operator-(const CoordsXYZ& rhs) const - { - return { x - rhs.x, y - rhs.y, z - rhs.z }; - } - - bool operator==(const CoordsXYZ& other) const - { - return x == other.x && y == other.y && z == other.z; - } - - CoordsXYZ ToTileStart() const - { - return { floor2(x, COORDS_XY_STEP), floor2(y, COORDS_XY_STEP), z }; - } - - CoordsXYZ ToTileCentre() const - { - return ToTileStart() + CoordsXYZ{ COORDS_XY_HALF_TILE, COORDS_XY_HALF_TILE, 0 }; - } -}; - struct TileCoordsXYZ : public TileCoordsXY { int32_t z = 0; From 925d21eeda14d10edf0920aceed85f8fea20a43b Mon Sep 17 00:00:00 2001 From: Helio Santos Date: Fri, 10 Jul 2020 09:59:06 -0700 Subject: [PATCH 3/3] Part of #12159: update check_for_adjacent_station to use CoordsXYZ& Update check_for_adjacent_station to use CoordsXYZ&. Updated caller(s) accordingly. --- src/openrct2/ride/Ride.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index b4337d4de8..f8ff735cb0 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -7247,16 +7247,17 @@ TileElement* get_station_platform(const CoordsXYRangedZ& coords) /** * Check for an adjacent station to x,y,z in direction. */ -static bool check_for_adjacent_station(int32_t x, int32_t y, int32_t z, uint8_t direction) +static bool check_for_adjacent_station(const CoordsXYZ& stationCoords, uint8_t direction) { bool found = false; - int32_t adjX = x; - int32_t adjY = y; + int32_t adjX = stationCoords.x; + int32_t adjY = stationCoords.y; for (uint32_t i = 0; i <= RIDE_ADJACENCY_CHECK_DISTANCE; i++) { adjX += CoordsDirectionDelta[direction].x; adjY += CoordsDirectionDelta[direction].y; - TileElement* stationElement = get_station_platform({ { adjX, adjY, z }, z + 2 * COORDS_Z_STEP }); + TileElement* stationElement = get_station_platform( + { { adjX, adjY, stationCoords.z }, stationCoords.z + 2 * COORDS_Z_STEP }); if (stationElement != nullptr) { auto rideIndex = stationElement->AsTrack()->GetRideIndex(); @@ -7292,12 +7293,12 @@ bool ride_has_adjacent_station(Ride* ride) } /* Check the first side of the station */ int32_t direction = stationElement->GetDirectionWithOffset(1); - found = check_for_adjacent_station(stationStart.x, stationStart.y, stationStart.z, direction); + found = check_for_adjacent_station(stationStart, direction); if (found) break; /* Check the other side of the station */ direction = direction_reverse(direction); - found = check_for_adjacent_station(stationStart.x, stationStart.y, stationStart.z, direction); + found = check_for_adjacent_station(stationStart, direction); if (found) break; }