diff --git a/src/openrct2-ui/windows/MazeConstruction.cpp b/src/openrct2-ui/windows/MazeConstruction.cpp index 93006dd0c6..99b2dd5521 100644 --- a/src/openrct2-ui/windows/MazeConstruction.cpp +++ b/src/openrct2-ui/windows/MazeConstruction.cpp @@ -331,7 +331,7 @@ static void window_maze_construction_update(rct_window* w) default: break; } - sub_6C94D8(); + updateGhostTrackAndArrow(); } /** diff --git a/src/openrct2-ui/windows/RideConstruction.cpp b/src/openrct2-ui/windows/RideConstruction.cpp index 8879ec1607..431d4db888 100644 --- a/src/openrct2-ui/windows/RideConstruction.cpp +++ b/src/openrct2-ui/windows/RideConstruction.cpp @@ -466,7 +466,7 @@ static void window_ride_construction_select_map_tiles( Ride* ride, int32_t trackType, int32_t trackDirection, const CoordsXY& tileCoords); static void window_ride_construction_show_special_track_dropdown(rct_window* w, rct_widget* widget); static void ride_selected_track_set_seat_rotation(int32_t seatRotation); -static void loc_6C7502(int32_t al); +static void UpdateLiftHillSelected(int32_t slope); static void ride_construction_set_brakes_speed(int32_t brakesSpeed); static void ride_construction_tooldown_entrance_exit(const ScreenCoordsXY& screenCoords); @@ -1376,11 +1376,11 @@ static void window_ride_construction_mousedown(rct_window* w, rct_widgetindex wi } if (w->widgets[WIDX_SLOPE_DOWN_STEEP].tooltip == STR_RIDE_CONSTRUCTION_STEEP_SLOPE_DOWN_TIP) { - loc_6C7502(8); + UpdateLiftHillSelected(TRACK_SLOPE_DOWN_60); } else { - loc_6C7502(10); + UpdateLiftHillSelected(TRACK_SLOPE_UP_90); } break; case WIDX_SLOPE_DOWN: @@ -1389,7 +1389,7 @@ static void window_ride_construction_mousedown(rct_window* w, rct_widgetindex wi { _currentTrackBankEnd = TRACK_BANK_NONE; } - loc_6C7502(6); + UpdateLiftHillSelected(TRACK_SLOPE_DOWN_25); break; case WIDX_LEVEL: ride_construction_invalidate_current_track(); @@ -1415,7 +1415,7 @@ static void window_ride_construction_mousedown(rct_window* w, rct_widgetindex wi _currentTrackBankEnd = TRACK_BANK_RIGHT; } } - loc_6C7502(0); + UpdateLiftHillSelected(TRACK_SLOPE_NONE); break; case WIDX_SLOPE_UP: ride_construction_invalidate_current_track(); @@ -1433,7 +1433,7 @@ static void window_ride_construction_mousedown(rct_window* w, rct_widgetindex wi } else { - loc_6C7502(2); + UpdateLiftHillSelected(TRACK_SLOPE_UP_25); } break; case WIDX_SLOPE_UP_STEEP: @@ -1508,11 +1508,11 @@ static void window_ride_construction_mousedown(rct_window* w, rct_widgetindex wi } if (w->widgets[WIDX_SLOPE_UP_STEEP].tooltip == STR_RIDE_CONSTRUCTION_STEEP_SLOPE_UP_TIP) { - loc_6C7502(4); + UpdateLiftHillSelected(TRACK_SLOPE_UP_60); } else { - loc_6C7502(18); + UpdateLiftHillSelected(TRACK_SLOPE_DOWN_90); } break; case WIDX_CHAIN_LIFT: @@ -1888,7 +1888,8 @@ static void window_ride_construction_mouseup_demolish(rct_window* w) // Invalidate the selected track element or make sure it's at origin??? direction = _currentTrackPieceDirection; track_type_t type = _currentTrackPieceType; - auto newCoords = sub_6C683D({ _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); + auto newCoords = GetTrackElementOriginAndApplyChanges( + { _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); if (newCoords == std::nullopt) { window_ride_construction_update_active_elements(); @@ -1918,7 +1919,8 @@ static void window_ride_construction_mouseup_demolish(rct_window* w) { direction = _currentTrackPieceDirection; type = _currentTrackPieceType; - newCoords = sub_6C683D({ _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); + newCoords = GetTrackElementOriginAndApplyChanges( + { _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); if (newCoords == std::nullopt) { @@ -2089,7 +2091,7 @@ static void window_ride_construction_update(rct_window* w) break; } - sub_6C94D8(); + updateGhostTrackAndArrow(); } /** @@ -2489,7 +2491,7 @@ void window_ride_construction_update_active_elements_impl() _selectedTrackType = TrackElemType::None; if (_rideConstructionState == RideConstructionState::Selected) { - if (sub_6C683D( + if (GetTrackElementOriginAndApplyChanges( { _currentTrackBegin, static_cast(_currentTrackPieceDirection & 3) }, _currentTrackPieceType, 0, &tileElement, 0) != std::nullopt) @@ -2527,7 +2529,7 @@ void window_ride_construction_update_enabled_track_pieces() * * rct2: 0x006C94D8 */ -void sub_6C94D8() +void updateGhostTrackAndArrow() { ride_id_t rideIndex; int32_t direction, type, liftHillAndAlternativeState; @@ -2551,6 +2553,7 @@ void sub_6C94D8() case RideConstructionState::Front: case RideConstructionState::Back: { + // place ghost piece if (!(_currentTrackSelectionFlags & TRACK_SELECTION_FLAG_TRACK)) { if (window_ride_construction_update_state( @@ -2565,7 +2568,7 @@ void sub_6C94D8() window_ride_construction_update_active_elements(); } } - + // update flashing arrow auto curTime = platform_get_ticks(); if (_rideConstructionNextArrowPulse >= curTime) break; @@ -2575,6 +2578,7 @@ void sub_6C94D8() trackPos = _currentTrackBegin; direction = _currentTrackPieceDirection; type = _currentTrackPieceType; + // diagonal pieces trigger this if (direction >= 4) direction += 4; if (_rideConstructionState == RideConstructionState::Back) @@ -2599,7 +2603,8 @@ void sub_6C94D8() type = _currentTrackPieceType; uint16_t flags = _currentTrackSelectionFlags & TRACK_SELECTION_FLAG_ARROW ? TRACK_ELEMENT_SET_HIGHLIGHT_TRUE : TRACK_ELEMENT_SET_HIGHLIGHT_FALSE; - auto newCoords = sub_6C683D({ _currentTrackBegin, static_cast(direction) }, type, 0, nullptr, flags); + auto newCoords = GetTrackElementOriginAndApplyChanges( + { _currentTrackBegin, static_cast(direction) }, type, 0, nullptr, flags); if (newCoords == std::nullopt) { ride_construction_remove_ghosts(); @@ -3356,7 +3361,7 @@ static void window_ride_construction_show_special_track_dropdown(rct_window* w, */ static void ride_selected_track_set_seat_rotation(int32_t seatRotation) { - sub_6C683D( + GetTrackElementOriginAndApplyChanges( { _currentTrackBegin, static_cast(_currentTrackPieceDirection & 3) }, _currentTrackPieceType, seatRotation, nullptr, TRACK_ELEMENT_SET_SEAT_ROTATION); window_ride_construction_update_active_elements(); @@ -3366,15 +3371,21 @@ static void ride_selected_track_set_seat_rotation(int32_t seatRotation) * * rct2: 0x006C7502 */ -static void loc_6C7502(int32_t al) +static void UpdateLiftHillSelected(int32_t slope) { - _currentTrackSlopeEnd = al; + _currentTrackSlopeEnd = slope; _currentTrackPrice = MONEY32_UNDEFINED; if (_rideConstructionState == RideConstructionState::Front) { - if (al != 2 && al != 4 && al != 0) + switch (slope) { - _currentTrackLiftHill &= ~CONSTRUCTION_LIFT_HILL_SELECTED; + case TRACK_SLOPE_NONE: + case TRACK_SLOPE_UP_25: + case TRACK_SLOPE_UP_60: + break; + default: + _currentTrackLiftHill &= ~CONSTRUCTION_LIFT_HILL_SELECTED; + break; } } window_ride_construction_update_active_elements(); @@ -3388,7 +3399,7 @@ static void ride_construction_set_brakes_speed(int32_t brakesSpeed) { TileElement* tileElement; - if (sub_6C683D( + if (GetTrackElementOriginAndApplyChanges( { _currentTrackBegin, static_cast(_currentTrackPieceDirection & 3) }, _currentTrackPieceType, 0, &tileElement, 0) != std::nullopt) diff --git a/src/openrct2/actions/MazeSetTrackAction.cpp b/src/openrct2/actions/MazeSetTrackAction.cpp index f0dd07396f..19e38f95d0 100644 --- a/src/openrct2/actions/MazeSetTrackAction.cpp +++ b/src/openrct2/actions/MazeSetTrackAction.cpp @@ -293,7 +293,7 @@ GameActions::Result::Ptr MazeSetTrackAction::Execute() const if ((tileElement->AsTrack()->GetMazeEntry() & 0x8888) == 0x8888) { tile_element_remove(tileElement); - sub_6CB945(ride); + ride->ValidateStations(); ride->maze_tiles--; } diff --git a/src/openrct2/actions/RideDemolishAction.cpp b/src/openrct2/actions/RideDemolishAction.cpp index ecc24ea6ce..3591fe98c0 100644 --- a/src/openrct2/actions/RideDemolishAction.cpp +++ b/src/openrct2/actions/RideDemolishAction.cpp @@ -129,7 +129,7 @@ GameActions::Result::Ptr RideDemolishAction::DemolishRide(Ride* ride) const ride_remove_peeps(ride); ride->StopGuestsQueuing(); - sub_6CB945(ride); + ride->ValidateStations(); ride_clear_leftover_entrances(ride); News::DisableNewsItems(News::ItemType::Ride, _rideIndex); diff --git a/src/openrct2/actions/RideSetColourSchemeAction.cpp b/src/openrct2/actions/RideSetColourSchemeAction.cpp index 8e245cc3df..064d30bb9c 100644 --- a/src/openrct2/actions/RideSetColourSchemeAction.cpp +++ b/src/openrct2/actions/RideSetColourSchemeAction.cpp @@ -61,7 +61,7 @@ GameActions::Result::Ptr RideSetColourSchemeAction::Execute() const res->Expenditure = ExpenditureType::RideConstruction; res->ErrorTitle = STR_CANT_SET_COLOUR_SCHEME; - sub_6C683D(_loc, _trackType, _newColourScheme, nullptr, TRACK_ELEMENT_SET_COLOUR_SCHEME); + GetTrackElementOriginAndApplyChanges(_loc, _trackType, _newColourScheme, nullptr, TRACK_ELEMENT_SET_COLOUR_SCHEME); return res; } diff --git a/src/openrct2/actions/TrackPlaceAction.cpp b/src/openrct2/actions/TrackPlaceAction.cpp index 2f36dfc5c8..ab58057ab4 100644 --- a/src/openrct2/actions/TrackPlaceAction.cpp +++ b/src/openrct2/actions/TrackPlaceAction.cpp @@ -629,7 +629,7 @@ GameActions::Result::Ptr TrackPlaceAction::Execute() const { track_add_station_element({ mapLoc, _origin.direction }, _rideIndex, GAME_COMMAND_FLAG_APPLY, _fromTrackDesign); } - sub_6CB945(ride); + ride->ValidateStations(); ride->UpdateMaxVehicles(); } diff --git a/src/openrct2/actions/TrackRemoveAction.cpp b/src/openrct2/actions/TrackRemoveAction.cpp index 8e1bf2b942..65c1a1b1b1 100644 --- a/src/openrct2/actions/TrackRemoveAction.cpp +++ b/src/openrct2/actions/TrackRemoveAction.cpp @@ -426,7 +426,7 @@ GameActions::Result::Ptr TrackRemoveAction::Execute() const footpath_remove_edges_at(mapLoc, tileElement); } tile_element_remove(tileElement); - sub_6CB945(ride); + ride->ValidateStations(); if (!(GetFlags() & GAME_COMMAND_FLAG_GHOST)) { ride->UpdateMaxVehicles(); diff --git a/src/openrct2/ride/Ride.cpp b/src/openrct2/ride/Ride.cpp index dd232f6f07..62dee8b349 100644 --- a/src/openrct2/ride/Ride.cpp +++ b/src/openrct2/ride/Ride.cpp @@ -2863,7 +2863,8 @@ static void ride_set_boat_hire_return_point(Ride* ride, CoordsXYE* startElement) auto trackCoords = CoordsXYZ{ trackBeginEnd.begin_x, trackBeginEnd.begin_y, trackBeginEnd.begin_z }; int32_t direction = trackBeginEnd.begin_direction; trackType = trackBeginEnd.begin_element->AsTrack()->GetTrackType(); - auto newCoords = sub_6C683D({ trackCoords, static_cast(direction) }, trackType, 0, &returnPos.element, 0); + auto newCoords = GetTrackElementOriginAndApplyChanges( + { trackCoords, static_cast(direction) }, trackType, 0, &returnPos.element, 0); returnPos = newCoords == std::nullopt ? CoordsXYE{ trackCoords, returnPos.element } : CoordsXYE{ *newCoords, returnPos.element }; }; @@ -3610,7 +3611,7 @@ static bool ride_initialise_cable_lift_track(Ride* ride, bool isApplying) auto tmpLoc = CoordsXYZ{ it.current, tileElement->GetBaseZ() }; auto direction = tileElement->GetDirection(); trackType = tileElement->AsTrack()->GetTrackType(); - sub_6C683D({ tmpLoc, direction }, trackType, 0, &tileElement, flags); + GetTrackElementOriginAndApplyChanges({ tmpLoc, direction }, trackType, 0, &tileElement, flags); } } return true; diff --git a/src/openrct2/ride/Ride.h b/src/openrct2/ride/Ride.h index be14e2431a..fcaa9a7ad1 100644 --- a/src/openrct2/ride/Ride.h +++ b/src/openrct2/ride/Ride.h @@ -424,6 +424,7 @@ public: bool SupportsStatus(RideStatus s) const; void StopGuestsQueuing(); + void ValidateStations(); bool Open(bool isApplying); bool Test(RideStatus newStatus, bool isApplying); @@ -1147,7 +1148,7 @@ void ride_breakdown_add_news_item(Ride* ride); Staff* ride_find_closest_mechanic(Ride* ride, int32_t forInspection); int32_t ride_initialise_construction_window(Ride* ride); void ride_construction_invalidate_current_track(); -std::optional sub_6C683D( +std::optional GetTrackElementOriginAndApplyChanges( const CoordsXYZD& location, track_type_t type, uint16_t extra_params, TileElement** output_element, uint16_t flags); void ride_set_map_tooltip(TileElement* tileElement); void ride_prepare_breakdown(Ride* ride, int32_t breakdownReason); @@ -1218,9 +1219,7 @@ enum class RideSetSetting : uint8_t; money32 set_operating_setting(ride_id_t rideId, RideSetSetting setting, uint8_t value); money32 set_operating_setting_nested(ride_id_t rideId, RideSetSetting setting, uint8_t value, uint8_t flags); -void sub_6CB945(Ride* ride); - -void sub_6C94D8(); +void updateGhostTrackAndArrow(); void ride_reset_all_names(); diff --git a/src/openrct2/ride/RideConstruction.cpp b/src/openrct2/ride/RideConstruction.cpp index de5f99dfd3..2cfa63038c 100644 --- a/src/openrct2/ride/RideConstruction.cpp +++ b/src/openrct2/ride/RideConstruction.cpp @@ -379,7 +379,7 @@ void ride_clear_blocked_tiles(Ride* ride) * di : output_element * bp : flags */ -std::optional sub_6C683D( +std::optional GetTrackElementOriginAndApplyChanges( const CoordsXYZD& location, track_type_t type, uint16_t extra_params, TileElement** output_element, uint16_t flags) { // Find the relevant track piece, prefer sequence 0 (this ensures correct behaviour for diagonal track pieces) @@ -549,7 +549,7 @@ void ride_construction_invalidate_current_track() switch (_rideConstructionState) { case RideConstructionState::Selected: - sub_6C683D( + GetTrackElementOriginAndApplyChanges( { _currentTrackBegin, static_cast(_currentTrackPieceDirection & 3) }, _currentTrackPieceType, 0, nullptr, TRACK_ELEMENT_SET_HIGHLIGHT_FALSE); break; @@ -750,7 +750,8 @@ void ride_select_next_section() int32_t direction = _currentTrackPieceDirection; int32_t type = _currentTrackPieceType; TileElement* tileElement; - auto newCoords = sub_6C683D({ _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); + auto newCoords = GetTrackElementOriginAndApplyChanges( + { _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); if (newCoords == std::nullopt) { _rideConstructionState = RideConstructionState::State0; @@ -816,7 +817,8 @@ void ride_select_previous_section() int32_t direction = _currentTrackPieceDirection; int32_t type = _currentTrackPieceType; TileElement* tileElement; - auto newCoords = sub_6C683D({ _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); + auto newCoords = GetTrackElementOriginAndApplyChanges( + { _currentTrackBegin, static_cast(direction & 3) }, type, 0, &tileElement, 0); if (newCoords == std::nullopt) { _rideConstructionState = RideConstructionState::State0; @@ -1034,7 +1036,7 @@ bool ride_modify(CoordsXYE* input) auto tileCoords = CoordsXYZ{ tileElement, tileElement.element->GetBaseZ() }; auto direction = tileElement.element->GetDirection(); auto type = tileElement.element->AsTrack()->GetTrackType(); - auto newCoords = sub_6C683D({ tileCoords, direction }, type, 0, nullptr, 0); + auto newCoords = GetTrackElementOriginAndApplyChanges({ tileCoords, direction }, type, 0, nullptr, 0); if (newCoords == std::nullopt) return false; @@ -1200,42 +1202,39 @@ money32 set_operating_setting_nested(ride_id_t rideId, RideSetSetting setting, u return res->Error == GameActions::Status::Ok ? 0 : MONEY32_UNDEFINED; } -static int32_t loc_6CD18E( +// for a 3xN or nx3 grid, find the direction that faces the inside. +static Direction GetEntranceExitDirection( int16_t mapX, int16_t mapY, int16_t entranceMinX, int16_t entranceMinY, int16_t entranceMaxX, int16_t entranceMaxY) { - int32_t direction = 0; if (mapX == entranceMinX) { if (mapY > entranceMinY && mapY < entranceMaxY) { - return direction; + return 0; } } - direction = 1; if (mapY == entranceMaxY) { if (mapX > entranceMinX && mapX < entranceMaxX) { - return direction; + return 1; } } - direction = 2; if (mapX == entranceMaxX) { if (mapY > entranceMinY && mapY < entranceMaxY) { - return direction; + return 2; } } - direction = 3; if (mapY == entranceMinY) { if (mapX > entranceMinX && mapX < entranceMaxX) { - return direction; + return 3; } } - return -1; + return INVALID_DIRECTION; } /** @@ -1245,8 +1244,8 @@ static int32_t loc_6CD18E( CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenCoordsXY& screenCoords) { CoordsXYZD entranceExitCoords{}; - gRideEntranceExitPlaceDirection = INVALID_DIRECTION; + // determine if the mouse is hovering over a station - that's the station to add the entrance to auto info = get_map_coordinates_from_pos(screenCoords, EnumsToFlags(ViewportInteractionItem::Ride)); if (info.SpriteType != ViewportInteractionItem::None) { @@ -1279,17 +1278,14 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenC auto stationBaseZ = ride->stations[gRideEntranceExitPlaceStationIndex].GetBaseZ(); - auto coords = screen_get_map_xy_with_z(screenCoords, stationBaseZ); - if (!coords) + auto coordsAtHeight = screen_get_map_xy_with_z(screenCoords, stationBaseZ); + if (!coordsAtHeight) { entranceExitCoords.setNull(); return entranceExitCoords; } - auto word_F4418C = coords->x; - auto word_F4418E = coords->y; - - entranceExitCoords = { coords->ToTileStart(), stationBaseZ, INVALID_DIRECTION }; + entranceExitCoords = { coordsAtHeight->ToTileStart(), stationBaseZ, INVALID_DIRECTION }; if (ride->type == RIDE_TYPE_NULL) { @@ -1304,26 +1300,33 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenC return entranceExitCoords; } + // flat rides if (ride->GetRideTypeDescriptor().HasFlag(RIDE_TYPE_FLAG_HAS_SINGLE_PIECE_STATION)) { - auto mapX = (word_F4418C & 0x1F) - 16; - auto mapY = (word_F4418E & 0x1F) - 16; + // find the quadrant the mouse is hovering over - that's the direction to start searching for a station TileElement + Direction startDirection = 0; + auto mapX = (coordsAtHeight->x & 0x1F) - 16; + auto mapY = (coordsAtHeight->y & 0x1F) - 16; if (std::abs(mapX) < std::abs(mapY)) { - entranceExitCoords.direction = mapY < 0 ? 3 : 1; + startDirection = mapY < 0 ? 3 : 1; } else { - entranceExitCoords.direction = mapX < 0 ? 0 : 2; + startDirection = mapX < 0 ? 0 : 2; } - - for (int32_t i = 0; i < MAX_STATIONS; i++) + // check all 4 directions, starting with the mouse's quadrant + for (uint8_t directionIncrement = 0; directionIncrement < 4; directionIncrement++) { - mapX = entranceExitCoords.x + CoordsDirectionDelta[entranceExitCoords.direction].x; - mapY = entranceExitCoords.y + CoordsDirectionDelta[entranceExitCoords.direction].y; - if (map_is_location_valid({ mapX, mapY })) + entranceExitCoords.direction = (startDirection + directionIncrement) & 3; + // search for TrackElement one tile over, shifted in the search direction + auto nextLocation = entranceExitCoords; + nextLocation.x += CoordsDirectionDelta[entranceExitCoords.direction].x; + nextLocation.y += CoordsDirectionDelta[entranceExitCoords.direction].y; + if (map_is_location_valid(nextLocation)) { - auto tileElement = map_get_first_element_at({ mapX, mapY }); + // iterate over every element in the tile until we find what we want + auto tileElement = map_get_first_element_at(nextLocation); if (tileElement == nullptr) continue; do @@ -1336,37 +1339,42 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenC continue; if (tileElement->AsTrack()->GetTrackType() == TrackElemType::Maze) { + // if it's a maze, it can place the entrance and exit immediately entranceExitCoords.direction = direction_reverse(entranceExitCoords.direction); gRideEntranceExitPlaceDirection = entranceExitCoords.direction; return entranceExitCoords; } if (tileElement->AsTrack()->GetStationIndex() != gRideEntranceExitPlaceStationIndex) continue; + // if it's not a maze, the sequence properties for the TrackElement must be found to determine if an + // entrance can be placed on that side - int32_t eax = (entranceExitCoords.direction + 2 - tileElement->GetDirection()) - & TILE_ELEMENT_DIRECTION_MASK; + // get the exit hut side relative to the TrackElement + Direction direction = (direction_reverse(entranceExitCoords.direction) - tileElement->GetDirection()) & 3; const auto& ted = GetTrackElementDescriptor(tileElement->AsTrack()->GetTrackType()); - if (ted.SequenceProperties[tileElement->AsTrack()->GetSequenceIndex()] & (1 << eax)) + if (ted.SequenceProperties[tileElement->AsTrack()->GetSequenceIndex()] & (1 << direction)) { + // if that side of the TrackElement supports stations, the entrance hut is valid and faces away from the + // station entranceExitCoords.direction = direction_reverse(entranceExitCoords.direction); gRideEntranceExitPlaceDirection = entranceExitCoords.direction; return entranceExitCoords; } } while (!(tileElement++)->IsLastForTile()); } - entranceExitCoords.direction = (entranceExitCoords.direction + 1) & 3; } gRideEntranceExitPlaceDirection = INVALID_DIRECTION; } else { - auto mapX = stationStart.x; - auto mapY = stationStart.y; - auto entranceMinX = mapX; - auto entranceMinY = mapY; - auto entranceMaxX = mapX; - auto entranceMaxY = mapY; + // create a 2-point rectangle. The rectangle will have a width or height of 0 depending on station direction or length + // (if the station is 0) The 2-point rectangle is ballooned on all four sides by 1 tile for a 3xN or Nx3 rectangle. + // Whatever edge the entrance hut is on, rotate it outward to get entrance direction. + CoordsXY nextLocation = { stationStart.x, stationStart.y }; + CoordsXY entranceMin = { nextLocation }; + CoordsXY entranceMax = { nextLocation }; + // get the beginning of the station TrackElement auto tileElement = ride_get_station_start_track_element(ride, gRideEntranceExitPlaceStationIndex); if (tileElement == nullptr) { @@ -1376,13 +1384,13 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenC entranceExitCoords.direction = tileElement->GetDirection(); auto stationDirection = entranceExitCoords.direction; + // find additional station TrackElements and extend the line of valid entrance locations while (true) { - entranceMaxX = mapX; - entranceMaxY = mapY; - mapX -= CoordsDirectionDelta[entranceExitCoords.direction].x; - mapY -= CoordsDirectionDelta[entranceExitCoords.direction].y; - tileElement = map_get_first_element_at({ mapX, mapY }); + entranceMax = { nextLocation }; + nextLocation.x -= CoordsDirectionDelta[entranceExitCoords.direction].x; + nextLocation.y -= CoordsDirectionDelta[entranceExitCoords.direction].y; + tileElement = map_get_first_element_at(nextLocation); if (tileElement == nullptr) break; bool goToNextTile = false; @@ -1405,25 +1413,25 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenC if (!goToNextTile) break; } - - mapX = entranceMinX; - if (mapX > entranceMaxX) + // swap variables for valid ranges + nextLocation.x = entranceMin.x; + if (nextLocation.x > entranceMax.x) { - entranceMinX = entranceMaxX; - entranceMaxX = mapX; + entranceMin.x = entranceMax.x; + entranceMax.x = nextLocation.x; } - mapY = entranceMinY; - if (mapY > entranceMaxY) + nextLocation.y = entranceMin.y; + if (nextLocation.y > entranceMax.y) { - entranceMinY = entranceMaxY; - entranceMaxY = mapY; + entranceMin.y = entranceMax.y; + entranceMax.y = nextLocation.y; } - - auto direction = loc_6CD18E( - entranceExitCoords.x, entranceExitCoords.y, entranceMinX - 32, entranceMinY - 32, entranceMaxX + 32, - entranceMaxY + 32); - if (direction != -1 && direction != stationDirection && direction != direction_reverse(stationDirection)) + // for a 3xN or Nx3 grid, determine the direction toward the center + auto direction = GetEntranceExitDirection( + entranceExitCoords.x, entranceExitCoords.y, entranceMin.x - 32, entranceMin.y - 32, entranceMax.x + 32, + entranceMax.y + 32); + if (direction != INVALID_DIRECTION && direction != stationDirection && direction != direction_reverse(stationDirection)) { entranceExitCoords.direction = direction; gRideEntranceExitPlaceDirection = entranceExitCoords.direction; @@ -1437,23 +1445,25 @@ CoordsXYZD ride_get_entrance_or_exit_position_from_screen_position(const ScreenC * * rct2: 0x006CB945 */ -void sub_6CB945(Ride* ride) +void Ride::ValidateStations() { const TrackElementDescriptor* ted; - if (ride->type != RIDE_TYPE_MAZE) + if (type != RIDE_TYPE_MAZE) { + // find the stations of the ride to begin stepping over track elements from for (StationIndex stationId = 0; stationId < MAX_STATIONS; ++stationId) { - if (ride->stations[stationId].Start.isNull()) + if (stations[stationId].Start.isNull()) continue; - CoordsXYZ location = ride->stations[stationId].GetStart(); + CoordsXYZ location = stations[stationId].GetStart(); uint8_t direction = INVALID_DIRECTION; bool specialTrack = false; TileElement* tileElement = nullptr; while (true) { + // search backwards for the previous station TrackElement (only if the first station TrackElement is found) if (direction != INVALID_DIRECTION) { location.x -= CoordsDirectionDelta[direction].x; @@ -1463,6 +1473,7 @@ void sub_6CB945(Ride* ride) if (tileElement == nullptr) break; + // find the target TrackElement on the tile it's supposed to appear on bool trackFound = false; do { @@ -1470,12 +1481,13 @@ void sub_6CB945(Ride* ride) continue; if (tileElement->GetType() != TILE_ELEMENT_TYPE_TRACK) continue; - if (tileElement->AsTrack()->GetRideIndex() != ride->id) + if (tileElement->AsTrack()->GetRideIndex() != id) continue; if (tileElement->AsTrack()->GetSequenceIndex() != 0) continue; ted = &GetTrackElementDescriptor(tileElement->AsTrack()->GetTrackType()); + // keep searching for a station piece (coaster station, tower ride base, shops, and flat ride base) if (!(ted->SequenceProperties[0] & TRACK_SEQUENCE_FLAG_ORIGIN)) continue; @@ -1487,22 +1499,26 @@ void sub_6CB945(Ride* ride) { break; } - + // update the StationIndex, get the TrackElement's rotation tileElement->AsTrack()->SetStationIndex(stationId); direction = tileElement->GetDirection(); - if (ride->GetRideTypeDescriptor().HasFlag(RIDE_TYPE_FLAG_HAS_SINGLE_PIECE_STATION)) + // TODO: update this to look at the ted sequence length + // tower ride, flat ride, shop + if (GetRideTypeDescriptor().HasFlag(RIDE_TYPE_FLAG_HAS_SINGLE_PIECE_STATION)) { + // if the track has multiple sequences, stop looking for the next one. specialTrack = true; break; } } + // if the track piece is not a tower ride, flat ride, or shop, continue to the next StationIndex if (!specialTrack) { continue; } - + // update all the blocks with StationIndex ted = &GetTrackElementDescriptor(tileElement->AsTrack()->GetTrackType()); const rct_preview_track* trackBlock = ted->Block; while ((++trackBlock)->index != 0xFF) @@ -1513,6 +1529,7 @@ void sub_6CB945(Ride* ride) tileElement = map_get_first_element_at(blockLocation); if (tileElement == nullptr) break; + // find the target TrackElement on the tile it's supposed to appear on do { if (blockLocation.z != tileElement->GetBaseZ()) @@ -1530,6 +1547,7 @@ void sub_6CB945(Ride* ride) if (!trackFound) { + // Critical error! Stop trying to find the next sequence to set StationIndex. break; } @@ -1537,22 +1555,22 @@ void sub_6CB945(Ride* ride) } } } - + // determine what entrances and exits exist FixedVector locations; for (StationIndex stationId = 0; stationId < MAX_STATIONS; ++stationId) { - auto entrance = ride_get_entrance_location(ride, stationId); + auto entrance = ride_get_entrance_location(this, stationId); if (!entrance.isNull()) { locations.push_back(entrance); - ride_clear_entrance_location(ride, stationId); + ride_clear_entrance_location(this, stationId); } - auto exit = ride_get_exit_location(ride, stationId); + auto exit = ride_get_exit_location(this, stationId); if (!exit.isNull()) { locations.push_back(exit); - ride_clear_exit_location(ride, stationId); + ride_clear_exit_location(this, stationId); } } @@ -1560,7 +1578,8 @@ void sub_6CB945(Ride* ride) for (const TileCoordsXYZD& locationCoords : locations) { auto locationList = ++locationListIter; - + // determine if there's another hut at this location later in the array + // if there is, skip it. The last hut in the array at the location is not skipped bool duplicateLocation = false; while (locationList != locations.cend()) { @@ -1575,9 +1594,10 @@ void sub_6CB945(Ride* ride) if (duplicateLocation) { + // if it's a duplicate continue to the next hut continue; } - + // if it's not a duplicate location CoordsXY location = locationCoords.ToCoordsXY(); TileElement* tileElement = map_get_first_element_at(location); @@ -1589,15 +1609,18 @@ void sub_6CB945(Ride* ride) continue; if (tileElement->base_height != locationCoords.z) continue; - if (tileElement->AsEntrance()->GetRideIndex() != ride->id) + if (tileElement->AsEntrance()->GetRideIndex() != id) continue; + // if it's a park entrance continue to the next tile element if (tileElement->AsEntrance()->GetEntranceType() > ENTRANCE_TYPE_RIDE_EXIT) continue; + // find the station that's connected to this hut CoordsXY nextLocation = location; nextLocation.x += CoordsDirectionDelta[tileElement->GetDirection()].x; nextLocation.y += CoordsDirectionDelta[tileElement->GetDirection()].y; + // if there's no connected station, remove the hut (see below) bool shouldRemove = true; TileElement* trackElement = map_get_first_element_at(nextLocation); if (trackElement == nullptr) @@ -1606,7 +1629,7 @@ void sub_6CB945(Ride* ride) { if (trackElement->GetType() != TILE_ELEMENT_TYPE_TRACK) continue; - if (trackElement->AsTrack()->GetRideIndex() != ride->id) + if (trackElement->AsTrack()->GetRideIndex() != id) continue; if (trackElement->base_height != tileElement->base_height) continue; @@ -1614,41 +1637,45 @@ void sub_6CB945(Ride* ride) auto trackType = trackElement->AsTrack()->GetTrackType(); uint8_t trackSequence = trackElement->AsTrack()->GetSequenceIndex(); + // determine where the hut is relative to the station track Direction direction = (tileElement->GetDirection() - direction_reverse(trackElement->GetDirection())) & 3; + // if the hut is not on a valid side, remove the hut ted = &GetTrackElementDescriptor(trackType); if (!(ted->SequenceProperties[trackSequence] & (1 << direction))) { continue; } - + // get the StationIndex for the station StationIndex stationId = 0; if (trackType != TrackElemType::Maze) { stationId = trackElement->AsTrack()->GetStationIndex(); } - if (tileElement->AsEntrance()->GetEntranceType() == ENTRANCE_TYPE_RIDE_EXIT) { - if (!ride_get_exit_location(ride, stationId).isNull()) + // if the location is already set for this station, big problem! + if (!ride_get_exit_location(this, stationId).isNull()) break; - - CoordsXYZD loc = { location, ride->stations[stationId].GetBaseZ(), tileElement->GetDirection() }; - ride_set_exit_location(ride, stationId, TileCoordsXYZD{ loc }); + // set the station's exit hut location to this hut + CoordsXYZD loc = { location, stations[stationId].GetBaseZ(), tileElement->GetDirection() }; + ride_set_exit_location(this, stationId, TileCoordsXYZD{ loc }); } else { - if (!ride_get_entrance_location(ride, stationId).isNull()) + // if the location is already set for this station, big problem! + if (!ride_get_entrance_location(this, stationId).isNull()) break; - - CoordsXYZD loc = { location, ride->stations[stationId].GetBaseZ(), tileElement->GetDirection() }; - ride_set_entrance_location(ride, stationId, TileCoordsXYZD{ loc }); + // set the station's entrance hut location to this hut + CoordsXYZD loc = { location, stations[stationId].GetBaseZ(), tileElement->GetDirection() }; + ride_set_entrance_location(this, stationId, TileCoordsXYZD{ loc }); } - + // set the hut's StationIndex as this station tileElement->AsEntrance()->SetStationIndex(stationId); shouldRemove = false; } while (!(trackElement++)->IsLastForTile()); + // remove the hut and clean up if necessary if (shouldRemove) { footpath_queue_chain_reset(); diff --git a/src/openrct2/ride/Track.h b/src/openrct2/ride/Track.h index ac21d47da3..fe1c86484b 100644 --- a/src/openrct2/ride/Track.h +++ b/src/openrct2/ride/Track.h @@ -526,10 +526,10 @@ namespace TrackElemType enum { - TRACK_SEQUENCE_FLAG_DIRECTION_0 = (1 << 0), - TRACK_SEQUENCE_FLAG_DIRECTION_1 = (1 << 1), - TRACK_SEQUENCE_FLAG_DIRECTION_2 = (1 << 2), - TRACK_SEQUENCE_FLAG_DIRECTION_3 = (1 << 3), + TRACK_SEQUENCE_FLAG_DIRECTION_0 = (1 << 0), // Huts/path connect to front + TRACK_SEQUENCE_FLAG_DIRECTION_1 = (1 << 1), // Huts/path connect to right + TRACK_SEQUENCE_FLAG_DIRECTION_2 = (1 << 2), // Huts/path connect to back + TRACK_SEQUENCE_FLAG_DIRECTION_3 = (1 << 3), // Huts/path connect to left TRACK_SEQUENCE_FLAG_ORIGIN = (1 << 4), // 0x10 TRACK_SEQUENCE_FLAG_CONNECTS_TO_PATH = (1 << 5), // 0x20 TRACK_SEQUENCE_FLAG_DISALLOW_DOORS = (1 << 6), // 0x40 diff --git a/src/openrct2/ride/TrackDesign.cpp b/src/openrct2/ride/TrackDesign.cpp index e6394da8fb..c3c042ed5f 100644 --- a/src/openrct2/ride/TrackDesign.cpp +++ b/src/openrct2/ride/TrackDesign.cpp @@ -173,7 +173,8 @@ rct_string_id TrackDesign::CreateTrackDesignTrack(const Ride& ride) auto trackType = trackElement.element->AsTrack()->GetTrackType(); uint8_t direction = trackElement.element->GetDirection(); _saveDirection = direction; - auto newCoords = sub_6C683D({ trackElement, z, direction }, trackType, 0, &trackElement.element, 0); + auto newCoords = GetTrackElementOriginAndApplyChanges( + { trackElement, z, direction }, trackType, 0, &trackElement.element, 0); if (newCoords == std::nullopt) { @@ -229,7 +230,8 @@ rct_string_id TrackDesign::CreateTrackDesignTrack(const Ride& ride) z = trackElement.element->GetBaseZ(); direction = trackElement.element->GetDirection(); trackType = trackElement.element->AsTrack()->GetTrackType(); - newCoords = sub_6C683D({ trackElement, z, direction }, trackType, 0, &trackElement.element, 0); + newCoords = GetTrackElementOriginAndApplyChanges( + { trackElement, z, direction }, trackType, 0, &trackElement.element, 0); if (newCoords == std::nullopt) { @@ -1769,7 +1771,7 @@ static std::optional track_design_place_ride(TrackDesign* td6, const Co if (_trackDesignPlaceOperation == PTD_OPERATION_REMOVE_GHOST) { - sub_6CB945(ride); + ride->ValidateStations(); ride->Delete(); } return totalCost;