From aa6fb0e9e519200715903dc29a26075ce2eb4d2b Mon Sep 17 00:00:00 2001 From: Kuhnovic <68320206+Kuhnovic@users.noreply.github.com> Date: Fri, 26 Sep 2025 13:35:11 +0200 Subject: [PATCH] Codechange: Simplified logic of ship leaving a depot. (#14661) --- src/ship_cmd.cpp | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 2591099c95..f741c93f8a 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -359,7 +359,12 @@ static bool CheckReverseShip(const Ship *v, Trackdir *trackdir = nullptr) return YapfShipCheckReverse(v, trackdir); } -static bool CheckShipLeaveDepot(Ship *v) +/** + * Checks whether a ship should stay in the depot. + * @param v Ship to check. + * @return True if the ship should stay in the depot, false if it has to leave. + */ +static bool CheckShipStayInDepot(Ship *v) { if (!v->IsChainInDepot()) return false; @@ -382,34 +387,13 @@ static bool CheckShipLeaveDepot(Ship *v) return u->type == VEH_SHIP && u->cur_speed != 0; })) return true; - TileIndex tile = v->tile; - Axis axis = GetShipDepotAxis(tile); + assert(v->GetVehicleTrackdir() == TRACKDIR_X_NE || v->GetVehicleTrackdir() == TRACKDIR_Y_NW); + v->direction = DiagDirToDir(TrackdirToExitdir(v->GetVehicleTrackdir())); + if (CheckReverseShip(v)) v->direction = ReverseDir(v->direction); - DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis)); - TileIndex north_neighbour = TileAdd(tile, TileOffsByDiagDir(north_dir)); - DiagDirection south_dir = AxisToDiagDir(axis); - TileIndex south_neighbour = TileAdd(tile, 2 * TileOffsByDiagDir(south_dir)); - - TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour); - TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour); - if (north_tracks && south_tracks) { - if (CheckReverseShip(v)) north_tracks = TRACK_BIT_NONE; - } - - if (north_tracks) { - /* Leave towards north */ - v->rotation = v->direction = DiagDirToDir(north_dir); - } else if (south_tracks) { - /* Leave towards south */ - v->rotation = v->direction = DiagDirToDir(south_dir); - } else { - /* Both ways blocked */ - return false; - } - - v->state = AxisToTrackBits(axis); + v->state = AxisToTrackBits(GetShipDepotAxis(v->tile)); + v->rotation = v->direction; v->vehstatus.Reset(VehState::Hidden); - v->cur_speed = 0; v->UpdateViewport(true, true); SetWindowDirty(WC_VEHICLE_DEPOT, v->tile); @@ -698,7 +682,7 @@ static void ShipController(Ship *v) if (v->current_order.IsType(OT_LOADING)) return; - if (CheckShipLeaveDepot(v)) return; + if (CheckShipStayInDepot(v)) return; v->ShowVisualEffect();