diff --git a/src/aircraft.h b/src/aircraft.h index d3b9336f1f..44a2bac7bb 100644 --- a/src/aircraft.h +++ b/src/aircraft.h @@ -110,7 +110,7 @@ struct Aircraft final : public SpecializedVehicle { uint Crash(bool flooded = false) override; TileIndex GetOrderStationLocation(StationID station) override; TileIndex GetCargoTile() const override { return this->First()->tile; } - ClosestDepot FindClosestDepot() override; + ClosestDepot FindClosestDepot(bool may_reverse = false) override; /** * Check if the aircraft type is a normal flying device; eg diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index aac97c84c9..7627a83874 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -396,7 +396,7 @@ CommandCost CmdBuildAircraft(DoCommandFlags flags, TileIndex tile, const Engine } -ClosestDepot Aircraft::FindClosestDepot() +ClosestDepot Aircraft::FindClosestDepot([[maybe_unused]] bool may_reverse) { const Station *st = GetTargetAirportIfValid(this); /* If the station is not a valid airport or if it has no hangars */ diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp index 7b4b675d3c..9f54034734 100644 --- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -1896,10 +1896,11 @@ VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v) * Update the vehicle's destination tile from an order. * @param order the order the vehicle currently has * @param v the vehicle to update + * @param may_reverse Whether the vehicle is allowed to reverse when executing the updated order. * @param conditional_depth the depth (amount of steps) to go with conditional orders. This to prevent infinite loops. * @param pbs_look_ahead Whether we are forecasting orders for pbs reservations in advance. If true, the order indices must not be modified. */ -bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead) +bool UpdateOrderDest(Vehicle *v, const Order *order, bool may_reverse, int conditional_depth, bool pbs_look_ahead) { if (conditional_depth > v->GetNumOrders()) { v->current_order.Free(); @@ -1926,7 +1927,7 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool if (v->dest_tile == 0 && TimerGameEconomy::date_fract != (v->index % Ticks::DAY_TICKS)) break; /* We need to search for the nearest depot (hangar). */ - ClosestDepot closest_depot = v->FindClosestDepot(); + ClosestDepot closest_depot = v->FindClosestDepot(may_reverse); if (closest_depot.found) { /* PBS reservations cannot reverse */ @@ -2018,7 +2019,7 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool } v->current_order = *order; - return UpdateOrderDest(v, order, conditional_depth + 1, pbs_look_ahead); + return UpdateOrderDest(v, order, may_reverse, conditional_depth + 1, pbs_look_ahead); } /** @@ -2116,7 +2117,7 @@ bool ProcessOrders(Vehicle *v) break; } - return UpdateOrderDest(v, order) && may_reverse; + return UpdateOrderDest(v, order, may_reverse) && may_reverse; } /** diff --git a/src/order_func.h b/src/order_func.h index ff7d865f1a..2df86d342b 100644 --- a/src/order_func.h +++ b/src/order_func.h @@ -20,7 +20,7 @@ void InvalidateVehicleOrder(const Vehicle *v, int data); void CheckOrders(const Vehicle*); void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist = false, bool reset_order_indices = true); bool ProcessOrders(Vehicle *v); -bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth = 0, bool pbs_look_ahead = false); +bool UpdateOrderDest(Vehicle *v, const Order *order, bool may_reverse = false, int conditional_depth = 0, bool pbs_look_ahead = false); VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v); uint GetOrderDistance(VehicleOrderID prev, VehicleOrderID cur, const Vehicle *v, int conditional_depth = 0); diff --git a/src/pathfinder/yapf/yapf.h b/src/pathfinder/yapf/yapf.h index 88f38334f0..c86624a55a 100644 --- a/src/pathfinder/yapf/yapf.h +++ b/src/pathfinder/yapf/yapf.h @@ -40,9 +40,10 @@ bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir); * @param max_penalty max distance (in pathfinder penalty) from the current ship position * (used also as optimization - the pathfinder can stop path finding if max_penalty * was reached and no depot was seen) + * @param may_reverse whether the ship is allowed to reverse * @return the data about the depot */ -FindDepotData YapfShipFindNearestDepot(const Ship *v, int max_penalty); +FindDepotData YapfShipFindNearestDepot(const Ship *v, int max_penalty, bool may_reverse); /** * Finds the best path for given road vehicle using YAPF. diff --git a/src/pathfinder/yapf/yapf_ship.cpp b/src/pathfinder/yapf/yapf_ship.cpp index 9704bd913b..8a675024eb 100644 --- a/src/pathfinder/yapf/yapf_ship.cpp +++ b/src/pathfinder/yapf/yapf_ship.cpp @@ -369,9 +369,10 @@ public: * Find the best depot for a ship. * @param v Ship * @param max_penalty maximum pathfinder cost. + * @param may_reverse whether the ship is allowed to reverse. * @return FindDepotData with the best depot tile, cost and whether to reverse. */ - static inline FindDepotData FindNearestDepot(const Ship *v, int max_penalty) + static inline FindDepotData FindNearestDepot(const Ship *v, int max_penalty, bool may_reverse) { FindDepotData depot; @@ -379,7 +380,7 @@ public: ShipPathCache dummy_cache; TileIndex tile = INVALID_TILE; Trackdir best_origin_dir = INVALID_TRACKDIR; - const bool search_both_ways = max_penalty == 0; + const bool search_both_ways = may_reverse && max_penalty == 0; const Trackdir forward_dir = v->GetVehicleTrackdir(); const Trackdir reverse_dir = ReverseTrackdir(forward_dir); const TrackdirBits forward_dirs = TrackdirToTrackdirBits(forward_dir); @@ -515,7 +516,7 @@ bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir) return CYapfShip::CheckShipReverse(v, trackdir); } -FindDepotData YapfShipFindNearestDepot(const Ship *v, int max_penalty) +FindDepotData YapfShipFindNearestDepot(const Ship *v, int max_penalty, bool may_reverse) { - return CYapfShip::FindNearestDepot(v, max_penalty); + return CYapfShip::FindNearestDepot(v, max_penalty, may_reverse); } diff --git a/src/roadveh.h b/src/roadveh.h index 56aae919c4..00df046dc5 100644 --- a/src/roadveh.h +++ b/src/roadveh.h @@ -132,7 +132,7 @@ struct RoadVehicle final : public GroundVehicle { uint Crash(bool flooded = false) override; Trackdir GetVehicleTrackdir() const override; TileIndex GetOrderStationLocation(StationID station) override; - ClosestDepot FindClosestDepot() override; + ClosestDepot FindClosestDepot(bool may_reverse = false) override; bool IsBus() const; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index e3564f0991..904d08039a 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -346,7 +346,7 @@ static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance return YapfRoadVehicleFindNearestDepot(v, max_distance); } -ClosestDepot RoadVehicle::FindClosestDepot() +ClosestDepot RoadVehicle::FindClosestDepot([[maybe_unused]] bool may_reverse) { FindDepotData rfdd = FindClosestRoadDepot(this, 0); if (rfdd.best_length == UINT_MAX) return ClosestDepot(); diff --git a/src/ship.h b/src/ship.h index c4789bf954..cae564d009 100644 --- a/src/ship.h +++ b/src/ship.h @@ -57,7 +57,7 @@ struct Ship final : public SpecializedVehicle { void OnNewEconomyDay() override; Trackdir GetVehicleTrackdir() const override; TileIndex GetOrderStationLocation(StationID station) override; - ClosestDepot FindClosestDepot() override; + ClosestDepot FindClosestDepot(bool may_reverse = false) override; void UpdateCache(); void SetDestTile(TileIndex tile) override; }; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 7a67ad0569..56bcfb4d0b 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -142,12 +142,12 @@ void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpri result->Set(_ship_sprites[spritenum] + direction); } -static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance) +static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance, bool may_reverse = false) { const TileIndex tile = v->tile; if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) return Depot::GetByTile(tile); - FindDepotData sfdd = YapfShipFindNearestDepot(Ship::From(v), max_distance); + FindDepotData sfdd = YapfShipFindNearestDepot(Ship::From(v), max_distance, may_reverse); if (sfdd.tile == INVALID_TILE) return nullptr; return Depot::GetByTile(sfdd.tile); @@ -906,9 +906,9 @@ CommandCost CmdBuildShip(DoCommandFlags flags, TileIndex tile, const Engine *e, return CommandCost(); } -ClosestDepot Ship::FindClosestDepot() +ClosestDepot Ship::FindClosestDepot(bool may_reverse) { - const Depot *depot = FindClosestShipDepot(this, 0); + const Depot *depot = FindClosestShipDepot(this, 0, may_reverse); if (depot == nullptr) return ClosestDepot(); return ClosestDepot(depot->xy, depot->index); diff --git a/src/train.h b/src/train.h index b7ff1a6581..4ce7c7ebd6 100644 --- a/src/train.h +++ b/src/train.h @@ -129,7 +129,7 @@ struct Train final : public GroundVehicle { uint Crash(bool flooded = false) override; Trackdir GetVehicleTrackdir() const override; TileIndex GetOrderStationLocation(StationID station) override; - ClosestDepot FindClosestDepot() override; + ClosestDepot FindClosestDepot(bool may_reverse = false) override; void ReserveTrackUnderConsist() const; diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index ab2472af15..7a0194b707 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -2176,7 +2176,7 @@ static FindDepotData FindClosestTrainDepot(Train *v, int max_distance) return YapfTrainFindNearestDepot(v, max_distance); } -ClosestDepot Train::FindClosestDepot() +ClosestDepot Train::FindClosestDepot([[maybe_unused]] bool may_reverse) { FindDepotData tfdd = FindClosestTrainDepot(this, 0); if (tfdd.best_length == UINT_MAX) return ClosestDepot(); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index e688e19c6e..04b46ce069 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -2583,7 +2583,7 @@ CommandCost Vehicle::SendToDepot(DoCommandFlags flags, DepotCommandFlags command return CommandCost(); } - ClosestDepot closest_depot = this->FindClosestDepot(); + ClosestDepot closest_depot = this->FindClosestDepot(true); static const StringID no_depot[] = {STR_ERROR_UNABLE_TO_FIND_ROUTE_TO, STR_ERROR_UNABLE_TO_FIND_LOCAL_DEPOT, STR_ERROR_UNABLE_TO_FIND_LOCAL_DEPOT, STR_ERROR_CAN_T_SEND_AIRCRAFT_TO_HANGAR}; if (!closest_depot.found) return CommandCost(no_depot[this->type]); diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 58c311e804..6eb0317f8f 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -792,9 +792,10 @@ public: /** * Find the closest depot for this vehicle and tell us the location, * DestinationID and whether we should reverse. + * @param may_reverse Whether the vehicle is allowed to reverse. * @return A structure with information about the closest depot, if found. */ - virtual ClosestDepot FindClosestDepot() { return {}; } + virtual ClosestDepot FindClosestDepot([[maybe_unused]] bool may_reverse = false) { return {}; } virtual void SetDestTile(TileIndex tile) { this->dest_tile = tile; }