From 2f7a26e8da34a196917e6885df8b1cad443bcb60 Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sat, 21 Mar 2020 15:11:50 +0100 Subject: [PATCH] Refactor station index a bit --- src/openrct2/Game.cpp | 4 +-- src/openrct2/peep/Guest.cpp | 4 +-- src/openrct2/ride/Ride.cpp | 26 +++++++-------- src/openrct2/ride/Ride.h | 2 +- src/openrct2/ride/RideRatings.cpp | 4 +-- src/openrct2/ride/Station.cpp | 54 +++++++++++++++---------------- src/openrct2/ride/Station.h | 22 +++++++------ src/openrct2/ride/Track.cpp | 12 +++---- test/testpaint/Compat.cpp | 8 ++--- 9 files changed, 69 insertions(+), 67 deletions(-) diff --git a/src/openrct2/Game.cpp b/src/openrct2/Game.cpp index 30a8bdc8d7..be26276479 100644 --- a/src/openrct2/Game.cpp +++ b/src/openrct2/Game.cpp @@ -459,8 +459,8 @@ void game_fix_save_vars() auto curName = peep->GetName(); log_warning( "Peep %u (%s) has invalid ride station = %u for ride %u.", spriteIndex, curName.c_str(), srcStation, rideIdx); - int8_t station = ride_get_first_valid_station_exit(ride); - if (station == -1) + auto station = ride_get_first_valid_station_exit(ride); + if (station == STATION_INDEX_NULL) { log_warning("Couldn't find station, removing peep %u", spriteIndex); peepsToRemove.push_back(peep); diff --git a/src/openrct2/peep/Guest.cpp b/src/openrct2/peep/Guest.cpp index 832e4fa6c7..e4ec636326 100644 --- a/src/openrct2/peep/Guest.cpp +++ b/src/openrct2/peep/Guest.cpp @@ -4130,8 +4130,8 @@ void Guest::UpdateRideLeaveVehicle() if (ride_station >= MAX_STATIONS) { // HACK #5658: Some parks have hacked rides which end up in this state - int8_t bestStationIndex = ride_get_first_valid_station_exit(ride); - if (bestStationIndex == -1) + auto bestStationIndex = ride_get_first_valid_station_exit(ride); + if (bestStationIndex == STATION_INDEX_NULL) { bestStationIndex = 0; } diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index 4b2108eb41..1999ccbfff 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -1026,11 +1026,11 @@ void ride_clear_for_construction(Ride* ride) void ride_remove_peeps(Ride* ride) { // Find first station - int8_t stationIndex = ride_get_first_valid_station_start(ride); + auto stationIndex = ride_get_first_valid_station_start(ride); // Get exit position and direction auto exitPosition = CoordsXYZD{ 0, 0, 0, INVALID_DIRECTION }; - if (stationIndex != -1) + if (stationIndex != STATION_INDEX_NULL) { auto location = ride_get_exit_location(ride, stationIndex).ToCoordsXYZD(); if (!location.isNull()) @@ -2227,8 +2227,8 @@ static void ride_inspection_update(Ride* ride) ride->lifecycle_flags |= RIDE_LIFECYCLE_DUE_INSPECTION; ride->mechanic_status = RIDE_MECHANIC_STATUS_CALLING; - int8_t stationIndex = ride_get_first_valid_station_exit(ride); - ride->inspection_station = (stationIndex != -1) ? stationIndex : 0; + auto stationIndex = ride_get_first_valid_station_exit(ride); + ride->inspection_station = (stationIndex != STATION_INDEX_NULL) ? stationIndex : 0; } static int32_t get_age_penalty(Ride* ride) @@ -2414,7 +2414,7 @@ static void choose_random_train_to_breakdown_safe(Ride* ride) */ void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason) { - int32_t i; + StationIndex i; uint16_t vehicleSpriteIdx; Vehicle* vehicle; @@ -2433,7 +2433,7 @@ void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason) case BREAKDOWN_CONTROL_FAILURE: // Inspect first station with an exit i = ride_get_first_valid_station_exit(ride); - if (i != -1) + if (i != STATION_INDEX_NULL) { ride->inspection_station = i; } @@ -2488,7 +2488,7 @@ void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason) // Original code generates a random number but does not use it // Unsure if this was supposed to choose a random station (or random station with an exit) i = ride_get_first_valid_station_exit(ride); - if (i != -1) + if (i != STATION_INDEX_NULL) { ride->inspection_station = i; } @@ -3772,21 +3772,21 @@ static int32_t ride_mode_check_valid_station_numbers(Ride* ride) * returns stationIndex of first station on success * -1 on failure. */ -static int32_t ride_mode_check_station_present(Ride* ride) +static StationIndex ride_mode_check_station_present(Ride* ride) { - int32_t stationIndex = ride_get_first_valid_station_start(ride); + auto stationIndex = ride_get_first_valid_station_start(ride); - if (stationIndex == -1) + if (stationIndex == STATION_INDEX_NULL) { gGameCommandErrorText = STR_NOT_YET_CONSTRUCTED; if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_NO_TRACK)) - return -1; + return STATION_INDEX_NULL; if (ride->type == RIDE_TYPE_MAZE) - return -1; + return STATION_INDEX_NULL; gGameCommandErrorText = STR_REQUIRES_A_STATION_PLATFORM; - return -1; + return STATION_INDEX_NULL; } return stationIndex; diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index db4e8f27d9..c18cc9252a 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -1165,7 +1165,7 @@ int32_t ride_music_params_update( const CoordsXYZ& rideCoords, Ride* ride, uint16_t sampleRate, uint32_t position, uint8_t* tuneId); void ride_music_update_final(); void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason); -TileElement* ride_get_station_start_track_element(Ride* ride, int32_t stationIndex); +TileElement* ride_get_station_start_track_element(Ride* ride, StationIndex stationIndex); TileElement* ride_get_station_exit_element(const CoordsXYZ& elementPos); void ride_set_status(Ride* ride, int32_t status); void ride_set_name(Ride* ride, const char* name, uint32_t flags); diff --git a/src/openrct2/ride/RideRatings.cpp b/src/openrct2/ride/RideRatings.cpp index 097391a660..3e4a0b2c69 100644 --- a/src/openrct2/ride/RideRatings.cpp +++ b/src/openrct2/ride/RideRatings.cpp @@ -1423,10 +1423,10 @@ static rating_tuple ride_ratings_get_drop_ratings(Ride* ride) */ static int32_t ride_ratings_get_scenery_score(Ride* ride) { - int8_t i = ride_get_first_valid_station_start(ride); + auto i = ride_get_first_valid_station_start(ride); CoordsXY location; - if (i == -1) + if (i == STATION_INDEX_NULL) { return 0; } diff --git a/src/openrct2/ride/Station.cpp b/src/openrct2/ride/Station.cpp index a594fe2e1e..a6836f2b52 100644 --- a/src/openrct2/ride/Station.cpp +++ b/src/openrct2/ride/Station.cpp @@ -14,18 +14,18 @@ #include "../world/Sprite.h" #include "Track.h" -static void ride_update_station_blocksection(Ride* ride, int32_t stationIndex); -static void ride_update_station_bumpercar(Ride* ride, int32_t stationIndex); -static void ride_update_station_normal(Ride* ride, int32_t stationIndex); -static void ride_update_station_race(Ride* ride, int32_t stationIndex); +static void ride_update_station_blocksection(Ride* ride, StationIndex stationIndex); +static void ride_update_station_bumpercar(Ride* ride, StationIndex stationIndex); +static void ride_update_station_normal(Ride* ride, StationIndex stationIndex); +static void ride_update_station_race(Ride* ride, StationIndex stationIndex); static void ride_race_init_vehicle_speeds(Ride* ride); -static void ride_invalidate_station_start(Ride* ride, int32_t stationIndex, bool greenLight); +static void ride_invalidate_station_start(Ride* ride, StationIndex stationIndex, bool greenLight); /** * * rct2: 0x006ABFFB */ -void ride_update_station(Ride* ride, int32_t stationIndex) +void ride_update_station(Ride* ride, StationIndex stationIndex) { if (ride->stations[stationIndex].Start.isNull()) return; @@ -52,7 +52,7 @@ void ride_update_station(Ride* ride, int32_t stationIndex) * * rct2: 0x006AC0A1 */ -static void ride_update_station_blocksection(Ride* ride, int32_t stationIndex) +static void ride_update_station_blocksection(Ride* ride, StationIndex stationIndex) { TileElement* tileElement = ride_get_station_start_track_element(ride, stationIndex); @@ -83,7 +83,7 @@ static void ride_update_station_blocksection(Ride* ride, int32_t stationIndex) * * rct2: 0x006AC12B */ -static void ride_update_station_bumpercar(Ride* ride, int32_t stationIndex) +static void ride_update_station_bumpercar(Ride* ride, StationIndex stationIndex) { // Change of station depart flag should really call invalidate_station_start // but since dodgems do not have station lights there is no point. @@ -145,7 +145,7 @@ static void ride_update_station_bumpercar(Ride* ride, int32_t stationIndex) * * rct2: 0x006AC02C */ -static void ride_update_station_normal(Ride* ride, int32_t stationIndex) +static void ride_update_station_normal(Ride* ride, StationIndex stationIndex) { int32_t time = ride->stations[stationIndex].Depart & STATION_DEPART_MASK; if ((ride->lifecycle_flags & (RIDE_LIFECYCLE_BROKEN_DOWN | RIDE_LIFECYCLE_CRASHED)) @@ -179,7 +179,7 @@ static void ride_update_station_normal(Ride* ride, int32_t stationIndex) * * rct2: 0x006AC1DF */ -static void ride_update_station_race(Ride* ride, int32_t stationIndex) +static void ride_update_station_race(Ride* ride, StationIndex stationIndex) { if (ride->status == RIDE_STATUS_CLOSED || (ride->lifecycle_flags & (RIDE_LIFECYCLE_BROKEN_DOWN | RIDE_LIFECYCLE_CRASHED))) { @@ -316,7 +316,7 @@ static void ride_race_init_vehicle_speeds(Ride* ride) * * rct2: 0x006AC2C7 */ -static void ride_invalidate_station_start(Ride* ride, int32_t stationIndex, bool greenLight) +static void ride_invalidate_station_start(Ride* ride, StationIndex stationIndex, bool greenLight) { auto startPos = ride->stations[stationIndex].Start; TileElement* tileElement = ride_get_station_start_track_element(ride, stationIndex); @@ -331,7 +331,7 @@ static void ride_invalidate_station_start(Ride* ride, int32_t stationIndex, bool map_invalidate_tile_zoom1({ startPos, tileElement->GetBaseZ(), tileElement->GetClearanceZ() }); } -TileElement* ride_get_station_start_track_element(Ride* ride, int32_t stationIndex) +TileElement* ride_get_station_start_track_element(Ride* ride, StationIndex stationIndex) { auto stationStart = ride->stations[stationIndex].GetStart(); @@ -366,68 +366,68 @@ TileElement* ride_get_station_exit_element(const CoordsXYZ& elementPos) return nullptr; } -int8_t ride_get_first_valid_station_exit(Ride* ride) +StationIndex ride_get_first_valid_station_exit(Ride* ride) { - for (int32_t i = 0; i < MAX_STATIONS; i++) + for (StationIndex i = 0; i < MAX_STATIONS; i++) { if (!ride->stations[i].Exit.isNull()) { return i; } } - return -1; + return STATION_INDEX_NULL; } -int8_t ride_get_first_valid_station_start(const Ride* ride) +StationIndex ride_get_first_valid_station_start(const Ride* ride) { - for (int8_t i = 0; i < MAX_STATIONS; i++) + for (StationIndex i = 0; i < MAX_STATIONS; i++) { if (!ride->stations[i].Start.isNull()) { return i; } } - return -1; + return STATION_INDEX_NULL; } -int8_t ride_get_first_empty_station_start(const Ride* ride) +StationIndex ride_get_first_empty_station_start(const Ride* ride) { - for (int8_t i = 0; i < MAX_STATIONS; i++) + for (StationIndex i = 0; i < MAX_STATIONS; i++) { if (ride->stations[i].Start.isNull()) { return i; } } - return -1; + return STATION_INDEX_NULL; } -TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const int32_t stationIndex) +TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const StationIndex stationIndex) { return ride->stations[stationIndex].Entrance; } -TileCoordsXYZD ride_get_exit_location(const Ride* ride, const int32_t stationIndex) +TileCoordsXYZD ride_get_exit_location(const Ride* ride, const StationIndex stationIndex) { return ride->stations[stationIndex].Exit; } -void ride_clear_entrance_location(Ride* ride, const int32_t stationIndex) +void ride_clear_entrance_location(Ride* ride, const StationIndex stationIndex) { ride->stations[stationIndex].Entrance.setNull(); } -void ride_clear_exit_location(Ride* ride, const int32_t stationIndex) +void ride_clear_exit_location(Ride* ride, const StationIndex stationIndex) { ride->stations[stationIndex].Exit.setNull(); } -void ride_set_entrance_location(Ride* ride, const int32_t stationIndex, const TileCoordsXYZD& location) +void ride_set_entrance_location(Ride* ride, const StationIndex stationIndex, const TileCoordsXYZD& location) { ride->stations[stationIndex].Entrance = location; } -void ride_set_exit_location(Ride* ride, const int32_t stationIndex, const TileCoordsXYZD& location) +void ride_set_exit_location(Ride* ride, const StationIndex stationIndex, const TileCoordsXYZD& location) { ride->stations[stationIndex].Exit = location; } diff --git a/src/openrct2/ride/Station.h b/src/openrct2/ride/Station.h index 3ba6b08eeb..6b73bc8073 100644 --- a/src/openrct2/ride/Station.h +++ b/src/openrct2/ride/Station.h @@ -16,16 +16,18 @@ struct Ride; using StationIndex = uint8_t; -void ride_update_station(Ride* ride, int32_t stationIndex); -int8_t ride_get_first_valid_station_exit(Ride* ride); -int8_t ride_get_first_valid_station_start(const Ride* ride); -int8_t ride_get_first_empty_station_start(const Ride* ride); +constexpr const StationIndex STATION_INDEX_NULL = 0xFF; -TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const int32_t stationIndex); -TileCoordsXYZD ride_get_exit_location(const Ride* ride, const int32_t stationIndex); +void ride_update_station(Ride* ride, StationIndex stationIndex); +StationIndex ride_get_first_valid_station_exit(Ride* ride); +StationIndex ride_get_first_valid_station_start(const Ride* ride); +StationIndex ride_get_first_empty_station_start(const Ride* ride); -void ride_clear_entrance_location(Ride* ride, const int32_t stationIndex); -void ride_clear_exit_location(Ride* ride, const int32_t stationIndex); +TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const StationIndex stationIndex); +TileCoordsXYZD ride_get_exit_location(const Ride* ride, const StationIndex stationIndex); -void ride_set_entrance_location(Ride* ride, const int32_t stationIndex, const TileCoordsXYZD& location); -void ride_set_exit_location(Ride* ride, const int32_t stationIndex, const TileCoordsXYZD& location); +void ride_clear_entrance_location(Ride* ride, const StationIndex stationIndex); +void ride_clear_exit_location(Ride* ride, const StationIndex stationIndex); + +void ride_set_entrance_location(Ride* ride, const StationIndex stationIndex, const TileCoordsXYZD& location); +void ride_set_exit_location(Ride* ride, const StationIndex stationIndex, const TileCoordsXYZD& location); diff --git a/src/openrct2/ride/Track.cpp b/src/openrct2/ride/Track.cpp index ef37102b48..c70065c221 100644 --- a/src/openrct2/ride/Track.cpp +++ b/src/openrct2/ride/Track.cpp @@ -656,8 +656,8 @@ bool track_add_station_element(CoordsXYZD loc, ride_id_t rideIndex, int32_t flag } if (flags & GAME_COMMAND_FLAG_APPLY) { - int8_t stationIndex = ride_get_first_empty_station_start(ride); - assert(stationIndex != -1); + auto stationIndex = ride_get_first_empty_station_start(ride); + assert(stationIndex != STATION_INDEX_NULL); ride->stations[stationIndex].Start.x = loc.x; ride->stations[stationIndex].Start.y = loc.y; @@ -742,8 +742,8 @@ bool track_add_station_element(CoordsXYZD loc, ride_id_t rideIndex, int32_t flag int32_t targetTrackType; if (stationLoc1 == loc) { - int8_t stationIndex = ride_get_first_empty_station_start(ride); - assert(stationIndex != -1); + auto stationIndex = ride_get_first_empty_station_start(ride); + assert(stationIndex != STATION_INDEX_NULL); ride->stations[stationIndex].Start = loc; ride->stations[stationIndex].Height = loc.z / COORDS_Z_STEP; @@ -886,8 +886,8 @@ bool track_remove_station_element(int32_t x, int32_t y, int32_t z, Direction dir if (x == stationX1 && y == stationY1) { loc_6C4BF5:; - int8_t stationIndex = ride_get_first_empty_station_start(ride); - assert(stationIndex != -1); + auto stationIndex = ride_get_first_empty_station_start(ride); + assert(stationIndex != STATION_INDEX_NULL); ride->stations[stationIndex].Start.x = x; ride->stations[stationIndex].Start.y = y; diff --git a/test/testpaint/Compat.cpp b/test/testpaint/Compat.cpp index e712c66667..5b527c65f8 100644 --- a/test/testpaint/Compat.cpp +++ b/test/testpaint/Compat.cpp @@ -61,8 +61,8 @@ const TileCoordsXY TileDirectionDelta[] = { }; // clang-format on -TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const int32_t stationIndex); -TileCoordsXYZD ride_get_exit_location(const Ride* ride, const int32_t stationIndex); +TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const StationIndex stationIndex); +TileCoordsXYZD ride_get_exit_location(const Ride* ride, const StationIndex stationIndex); uint8_t get_current_rotation() { @@ -396,12 +396,12 @@ void TrackElement::SetHasChain(bool on) } } -TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const int32_t stationIndex) +TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const StationIndex stationIndex) { return ride->stations[stationIndex].Entrance; } -TileCoordsXYZD ride_get_exit_location(const Ride* ride, const int32_t stationIndex) +TileCoordsXYZD ride_get_exit_location(const Ride* ride, const StationIndex stationIndex) { return ride->stations[stationIndex].Exit; }