Refactor station index a bit

This commit is contained in:
Gymnasiast 2020-03-21 15:11:50 +01:00
parent 32e589efbe
commit 2f7a26e8da
No known key found for this signature in database
GPG Key ID: DBFFF47AB2CA3EDD
9 changed files with 69 additions and 67 deletions

View File

@ -459,8 +459,8 @@ void game_fix_save_vars()
auto curName = peep->GetName(); auto curName = peep->GetName();
log_warning( log_warning(
"Peep %u (%s) has invalid ride station = %u for ride %u.", spriteIndex, curName.c_str(), srcStation, rideIdx); "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); auto station = ride_get_first_valid_station_exit(ride);
if (station == -1) if (station == STATION_INDEX_NULL)
{ {
log_warning("Couldn't find station, removing peep %u", spriteIndex); log_warning("Couldn't find station, removing peep %u", spriteIndex);
peepsToRemove.push_back(peep); peepsToRemove.push_back(peep);

View File

@ -4130,8 +4130,8 @@ void Guest::UpdateRideLeaveVehicle()
if (ride_station >= MAX_STATIONS) if (ride_station >= MAX_STATIONS)
{ {
// HACK #5658: Some parks have hacked rides which end up in this state // HACK #5658: Some parks have hacked rides which end up in this state
int8_t bestStationIndex = ride_get_first_valid_station_exit(ride); auto bestStationIndex = ride_get_first_valid_station_exit(ride);
if (bestStationIndex == -1) if (bestStationIndex == STATION_INDEX_NULL)
{ {
bestStationIndex = 0; bestStationIndex = 0;
} }

View File

@ -1026,11 +1026,11 @@ void ride_clear_for_construction(Ride* ride)
void ride_remove_peeps(Ride* ride) void ride_remove_peeps(Ride* ride)
{ {
// Find first station // 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 // Get exit position and direction
auto exitPosition = CoordsXYZD{ 0, 0, 0, INVALID_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(); auto location = ride_get_exit_location(ride, stationIndex).ToCoordsXYZD();
if (!location.isNull()) if (!location.isNull())
@ -2227,8 +2227,8 @@ static void ride_inspection_update(Ride* ride)
ride->lifecycle_flags |= RIDE_LIFECYCLE_DUE_INSPECTION; ride->lifecycle_flags |= RIDE_LIFECYCLE_DUE_INSPECTION;
ride->mechanic_status = RIDE_MECHANIC_STATUS_CALLING; ride->mechanic_status = RIDE_MECHANIC_STATUS_CALLING;
int8_t stationIndex = ride_get_first_valid_station_exit(ride); auto stationIndex = ride_get_first_valid_station_exit(ride);
ride->inspection_station = (stationIndex != -1) ? stationIndex : 0; ride->inspection_station = (stationIndex != STATION_INDEX_NULL) ? stationIndex : 0;
} }
static int32_t get_age_penalty(Ride* ride) 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) void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason)
{ {
int32_t i; StationIndex i;
uint16_t vehicleSpriteIdx; uint16_t vehicleSpriteIdx;
Vehicle* vehicle; Vehicle* vehicle;
@ -2433,7 +2433,7 @@ void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason)
case BREAKDOWN_CONTROL_FAILURE: case BREAKDOWN_CONTROL_FAILURE:
// Inspect first station with an exit // Inspect first station with an exit
i = ride_get_first_valid_station_exit(ride); i = ride_get_first_valid_station_exit(ride);
if (i != -1) if (i != STATION_INDEX_NULL)
{ {
ride->inspection_station = i; 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 // 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) // Unsure if this was supposed to choose a random station (or random station with an exit)
i = ride_get_first_valid_station_exit(ride); i = ride_get_first_valid_station_exit(ride);
if (i != -1) if (i != STATION_INDEX_NULL)
{ {
ride->inspection_station = i; 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 * returns stationIndex of first station on success
* -1 on failure. * -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; gGameCommandErrorText = STR_NOT_YET_CONSTRUCTED;
if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_HAS_NO_TRACK)) 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) if (ride->type == RIDE_TYPE_MAZE)
return -1; return STATION_INDEX_NULL;
gGameCommandErrorText = STR_REQUIRES_A_STATION_PLATFORM; gGameCommandErrorText = STR_REQUIRES_A_STATION_PLATFORM;
return -1; return STATION_INDEX_NULL;
} }
return stationIndex; return stationIndex;

View File

@ -1165,7 +1165,7 @@ int32_t ride_music_params_update(
const CoordsXYZ& rideCoords, Ride* ride, uint16_t sampleRate, uint32_t position, uint8_t* tuneId); const CoordsXYZ& rideCoords, Ride* ride, uint16_t sampleRate, uint32_t position, uint8_t* tuneId);
void ride_music_update_final(); void ride_music_update_final();
void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason); 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); TileElement* ride_get_station_exit_element(const CoordsXYZ& elementPos);
void ride_set_status(Ride* ride, int32_t status); void ride_set_status(Ride* ride, int32_t status);
void ride_set_name(Ride* ride, const char* name, uint32_t flags); void ride_set_name(Ride* ride, const char* name, uint32_t flags);

View File

@ -1423,10 +1423,10 @@ static rating_tuple ride_ratings_get_drop_ratings(Ride* ride)
*/ */
static int32_t ride_ratings_get_scenery_score(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; CoordsXY location;
if (i == -1) if (i == STATION_INDEX_NULL)
{ {
return 0; return 0;
} }

View File

@ -14,18 +14,18 @@
#include "../world/Sprite.h" #include "../world/Sprite.h"
#include "Track.h" #include "Track.h"
static void ride_update_station_blocksection(Ride* ride, int32_t stationIndex); static void ride_update_station_blocksection(Ride* ride, StationIndex stationIndex);
static void ride_update_station_bumpercar(Ride* ride, int32_t stationIndex); static void ride_update_station_bumpercar(Ride* ride, StationIndex stationIndex);
static void ride_update_station_normal(Ride* ride, int32_t stationIndex); static void ride_update_station_normal(Ride* ride, StationIndex stationIndex);
static void ride_update_station_race(Ride* ride, int32_t stationIndex); static void ride_update_station_race(Ride* ride, StationIndex stationIndex);
static void ride_race_init_vehicle_speeds(Ride* ride); 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 * 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()) if (ride->stations[stationIndex].Start.isNull())
return; return;
@ -52,7 +52,7 @@ void ride_update_station(Ride* ride, int32_t stationIndex)
* *
* rct2: 0x006AC0A1 * 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); 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 * 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 // Change of station depart flag should really call invalidate_station_start
// but since dodgems do not have station lights there is no point. // 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 * 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; int32_t time = ride->stations[stationIndex].Depart & STATION_DEPART_MASK;
if ((ride->lifecycle_flags & (RIDE_LIFECYCLE_BROKEN_DOWN | RIDE_LIFECYCLE_CRASHED)) 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 * 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))) 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 * 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; auto startPos = ride->stations[stationIndex].Start;
TileElement* tileElement = ride_get_station_start_track_element(ride, stationIndex); 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() }); 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(); auto stationStart = ride->stations[stationIndex].GetStart();
@ -366,68 +366,68 @@ TileElement* ride_get_station_exit_element(const CoordsXYZ& elementPos)
return nullptr; 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()) if (!ride->stations[i].Exit.isNull())
{ {
return i; 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()) if (!ride->stations[i].Start.isNull())
{ {
return i; 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()) if (ride->stations[i].Start.isNull())
{ {
return i; 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; 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; 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(); 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(); 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; 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; ride->stations[stationIndex].Exit = location;
} }

View File

@ -16,16 +16,18 @@ struct Ride;
using StationIndex = uint8_t; using StationIndex = uint8_t;
void ride_update_station(Ride* ride, int32_t stationIndex); constexpr const StationIndex STATION_INDEX_NULL = 0xFF;
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);
TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const int32_t stationIndex); void ride_update_station(Ride* ride, StationIndex stationIndex);
TileCoordsXYZD ride_get_exit_location(const Ride* ride, const int32_t 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); TileCoordsXYZD ride_get_entrance_location(const Ride* ride, const StationIndex stationIndex);
void ride_clear_exit_location(Ride* ride, const int32_t 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_clear_entrance_location(Ride* ride, const StationIndex stationIndex);
void ride_set_exit_location(Ride* ride, const int32_t stationIndex, const TileCoordsXYZD& location); 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);

View File

@ -656,8 +656,8 @@ bool track_add_station_element(CoordsXYZD loc, ride_id_t rideIndex, int32_t flag
} }
if (flags & GAME_COMMAND_FLAG_APPLY) if (flags & GAME_COMMAND_FLAG_APPLY)
{ {
int8_t stationIndex = ride_get_first_empty_station_start(ride); auto stationIndex = ride_get_first_empty_station_start(ride);
assert(stationIndex != -1); assert(stationIndex != STATION_INDEX_NULL);
ride->stations[stationIndex].Start.x = loc.x; ride->stations[stationIndex].Start.x = loc.x;
ride->stations[stationIndex].Start.y = loc.y; 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; int32_t targetTrackType;
if (stationLoc1 == loc) if (stationLoc1 == loc)
{ {
int8_t stationIndex = ride_get_first_empty_station_start(ride); auto stationIndex = ride_get_first_empty_station_start(ride);
assert(stationIndex != -1); assert(stationIndex != STATION_INDEX_NULL);
ride->stations[stationIndex].Start = loc; ride->stations[stationIndex].Start = loc;
ride->stations[stationIndex].Height = loc.z / COORDS_Z_STEP; 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) if (x == stationX1 && y == stationY1)
{ {
loc_6C4BF5:; loc_6C4BF5:;
int8_t stationIndex = ride_get_first_empty_station_start(ride); auto stationIndex = ride_get_first_empty_station_start(ride);
assert(stationIndex != -1); assert(stationIndex != STATION_INDEX_NULL);
ride->stations[stationIndex].Start.x = x; ride->stations[stationIndex].Start.x = x;
ride->stations[stationIndex].Start.y = y; ride->stations[stationIndex].Start.y = y;

View File

@ -61,8 +61,8 @@ const TileCoordsXY TileDirectionDelta[] = {
}; };
// clang-format on // clang-format on
TileCoordsXYZD ride_get_entrance_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 int32_t stationIndex); TileCoordsXYZD ride_get_exit_location(const Ride* ride, const StationIndex stationIndex);
uint8_t get_current_rotation() 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; 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; return ride->stations[stationIndex].Exit;
} }