From 89c020d3fe1d3757c5dc45491d7bf461a2f60a36 Mon Sep 17 00:00:00 2001 From: ZehMatt Date: Thu, 24 Aug 2017 13:45:32 +0200 Subject: [PATCH] RideCreateAction takes the colours now as input. Add ride_get_entry_index, same code is used at multiple spots. Rename variable 'rei' to 'rideEntryIndex' --- src/openrct2/actions/GameActionCompat.cpp | 5 ++ src/openrct2/actions/RideCreateAction.hpp | 71 +++++++---------------- src/openrct2/ride/ride.c | 36 ++++++++++++ src/openrct2/ride/ride.h | 2 + 4 files changed, 65 insertions(+), 49 deletions(-) diff --git a/src/openrct2/actions/GameActionCompat.cpp b/src/openrct2/actions/GameActionCompat.cpp index 07c06e1f6d..32cb14e2e4 100644 --- a/src/openrct2/actions/GameActionCompat.cpp +++ b/src/openrct2/actions/GameActionCompat.cpp @@ -109,6 +109,11 @@ extern "C" auto gameAction = RideCreateAction(); gameAction.rideType = listItem.type; gameAction.rideSubType = listItem.entry_index; + + sint32 rideEntryIndex = ride_get_entry_index(gameAction.rideType, gameAction.rideSubType); + gameAction.colourPreset1 = ride_get_random_colour_preset_index(gameAction.rideType); + gameAction.colourPreset2 = ride_get_unused_preset_vehicle_colour(gameAction.rideType, rideEntryIndex); + gameAction.SetCallback([](const GameAction *ga, const RideCreateGameActionResult * result) { if (result->Error != GA_ERROR::OK) diff --git a/src/openrct2/actions/RideCreateAction.hpp b/src/openrct2/actions/RideCreateAction.hpp index 6d4cb1be46..dce33bf342 100644 --- a/src/openrct2/actions/RideCreateAction.hpp +++ b/src/openrct2/actions/RideCreateAction.hpp @@ -39,7 +39,6 @@ struct RideCreateGameActionResult : public GameActionResult RideCreateGameActionResult(GA_ERROR error, rct_string_id message) : GameActionResult(error, message) {} sint32 rideIndex; - uint32 rideColor; }; struct RideCreateAction : public GameActionBase @@ -47,12 +46,14 @@ struct RideCreateAction : public GameActionBase(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_RIDE_TYPE); } - sint32 rideIndex = ride_get_empty_slot(); - if (rideIndex == -1) + sint32 rideEntryIndex = ride_get_entry_index(rideType, rideSubType); + if (rideEntryIndex >= 128) { - return std::make_unique(GA_ERROR::DISALLOWED, STR_TOO_MANY_RIDES); + return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_RIDE_TYPE); } - sint32 subType = GetSubType(); - if (subType >= 128) + + const track_colour_preset_list *colourPresets = &RideColourPresets[rideType]; + if (colourPreset1 >= colourPresets->count) { + // FIXME: Add new error string. + return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_RIDE_TYPE); + } + + rct_ride_entry *rideEntry = get_ride_entry(rideEntryIndex); + vehicle_colour_preset_list *presetList = rideEntry->vehicle_preset_list; + if (colourPreset2 >= presetList->count) + { + // FIXME: Add new error string. return std::make_unique(GA_ERROR::INVALID_PARAMETERS, STR_INVALID_RIDE_TYPE); } @@ -82,11 +93,10 @@ public: rct_ride_entry * rideEntry; auto res = std::make_unique(); - sint32 rideEntryIndex = GetSubType(); + sint32 rideEntryIndex = ride_get_entry_index(rideType, rideSubType); sint32 rideIndex = ride_get_empty_slot(); res->rideIndex = rideIndex; - res->rideColor = ride_get_random_colour_preset_index(rideType) | (ride_get_unused_preset_vehicle_colour(rideType, rideEntryIndex) << 8); auto ride = get_ride(rideIndex); rideEntry = get_ride_entry(rideEntryIndex); @@ -100,7 +110,7 @@ public: ride->type = rideType; ride->subtype = rideEntryIndex; - ride_set_colour_preset(ride, res->rideColor & 0xFF); + ride_set_colour_preset(ride, colourPreset1); ride->overall_view.xy = RCT_XY8_UNDEFINED; // Ride name @@ -269,49 +279,12 @@ public: ride->num_circuits = 1; ride->mode = ride_get_default_mode(ride); ride->min_max_cars_per_train = (rideEntry->min_cars_in_train << 4) | rideEntry->max_cars_in_train; - ride_set_vehicle_colours_to_random_preset(ride, 0xFF & (res->rideColor >> 8)); + ride_set_vehicle_colours_to_random_preset(ride, colourPreset2); window_invalidate_by_class(WC_RIDE_LIST); res->ExpenditureType = RCT_EXPENDITURE_TYPE_RIDE_CONSTRUCTION; res->Position.x = (uint16)0x8000; return std::move(res); - } - -private: - sint32 GetSubType() const - { - sint32 subType = rideSubType; - - if (subType == RIDE_ENTRY_INDEX_NULL) - { - uint8 *availableRideEntries = get_ride_entry_indices_for_ride_type(rideType); - for (uint8 *rei = availableRideEntries; *rei != RIDE_ENTRY_INDEX_NULL; rei++) - { - rct_ride_entry *rideEntry = get_ride_entry(*rei); - if (rideEntry == NULL) - { - return RIDE_ENTRY_INDEX_NULL; - } - - // Can happen in select-by-track-type mode - if (!ride_entry_is_invented(*rei) && !gCheatsIgnoreResearchStatus) - { - continue; - } - - if (!(rideEntry->flags & RIDE_ENTRY_FLAG_SEPARATE_RIDE) || rideTypeShouldLoseSeparateFlag(rideEntry)) - { - subType = *rei; - break; - } - } - if (subType == RIDE_ENTRY_INDEX_NULL) - { - subType = availableRideEntries[0]; - } - } - - return subType; - } + } }; diff --git a/src/openrct2/ride/ride.c b/src/openrct2/ride/ride.c index d85970e240..ed4b3193ab 100644 --- a/src/openrct2/ride/ride.c +++ b/src/openrct2/ride/ride.c @@ -8357,3 +8357,39 @@ bool ride_entry_has_category(const rct_ride_entry * rideEntry, uint8 category) return false; } + +sint32 ride_get_entry_index(sint32 rideType, sint32 rideSubType) +{ + sint32 subType = rideSubType; + + if (subType == RIDE_ENTRY_INDEX_NULL) + { + uint8 *availableRideEntries = get_ride_entry_indices_for_ride_type(rideType); + for (uint8 *rideEntryIndex = availableRideEntries; *rideEntryIndex != RIDE_ENTRY_INDEX_NULL; rideEntryIndex++) + { + rct_ride_entry *rideEntry = get_ride_entry(*rideEntryIndex); + if (rideEntry == NULL) + { + return RIDE_ENTRY_INDEX_NULL; + } + + // Can happen in select-by-track-type mode + if (!ride_entry_is_invented(*rideEntryIndex) && !gCheatsIgnoreResearchStatus) + { + continue; + } + + if (!(rideEntry->flags & RIDE_ENTRY_FLAG_SEPARATE_RIDE_NAME) || rideTypeShouldLoseSeparateFlag(rideEntry)) + { + subType = *rideEntryIndex; + break; + } + } + if (subType == RIDE_ENTRY_INDEX_NULL) + { + subType = availableRideEntries[0]; + } + } + + return subType; +} diff --git a/src/openrct2/ride/ride.h b/src/openrct2/ride/ride.h index 15ccc0dcbb..eb471d9b78 100644 --- a/src/openrct2/ride/ride.h +++ b/src/openrct2/ride/ride.h @@ -1206,6 +1206,8 @@ sint32 get_booster_speed(uint8 rideType, sint32 rawSpeed); void fix_invalid_vehicle_sprite_sizes(); bool ride_entry_has_category(const rct_ride_entry * rideEntry, uint8 category); +sint32 ride_get_entry_index(sint32 rideType, sint32 rideSubType); + #ifdef __cplusplus } #endif