Skip to content

Commit

Permalink
Fix #10511: Limit 'go to nearest depot' orders
Browse files Browse the repository at this point in the history
Adds a limit to the 'go to nearest depot' orders, applicable to ships, trains and road vehicles. The limit is based on the current pathfinder selected for that vehicle type and it limits the maximum distance/penalty for the pathfinder in order to lower cpu usage if the calling is constant on a large transport network.
  • Loading branch information
SamuXarick committed Dec 5, 2023
1 parent 58c252b commit b0ac816
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/aircraft.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ struct Aircraft FINAL : public SpecializedVehicle<Aircraft, VEH_AIRCRAFT> {
void OnNewDay() override;
uint Crash(bool flooded = false) override;
TileIndex GetOrderStationLocation(StationID station) override;
ClosestDepot FindClosestDepot() override;
ClosestDepot FindClosestDepot(bool limited = false) override;

/**
* Check if the aircraft type is a normal flying device; eg
Expand Down
2 changes: 1 addition & 1 deletion src/aircraft_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ CommandCost CmdBuildAircraft(DoCommandFlag flags, TileIndex tile, const Engine *
}


ClosestDepot Aircraft::FindClosestDepot()
ClosestDepot Aircraft::FindClosestDepot([[maybe_unused]] bool limited)
{
const Station *st = GetTargetAirportIfValid(this);
/* If the station is not a valid airport or if it has no hangars */
Expand Down
2 changes: 1 addition & 1 deletion src/order_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,7 @@ bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool

if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
/* We need to search for the nearest depot (hangar). */
ClosestDepot closestDepot = v->FindClosestDepot();
ClosestDepot closestDepot = v->FindClosestDepot(true);

if (closestDepot.found) {
/* PBS reservations cannot reverse */
Expand Down
2 changes: 1 addition & 1 deletion src/roadveh.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct RoadVehicle FINAL : public GroundVehicle<RoadVehicle, VEH_ROAD> {
uint Crash(bool flooded = false) override;
Trackdir GetVehicleTrackdir() const override;
TileIndex GetOrderStationLocation(StationID station) override;
ClosestDepot FindClosestDepot() override;
ClosestDepot FindClosestDepot(bool limited = false) override;

bool IsBus() const;

Expand Down
12 changes: 10 additions & 2 deletions src/roadveh_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,17 @@ static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance
}
}

ClosestDepot RoadVehicle::FindClosestDepot()
ClosestDepot RoadVehicle::FindClosestDepot(bool limited)
{
FindDepotData rfdd = FindClosestRoadDepot(this, 0);
uint max_penalty = 0;
if (limited) {
switch (_settings_game.pf.pathfinder_for_roadvehs) {
case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break;
case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break;
default: NOT_REACHED();
}
}
FindDepotData rfdd = FindClosestRoadDepot(this, max_penalty);
if (rfdd.best_length == UINT_MAX) return ClosestDepot();

return ClosestDepot(rfdd.tile, GetDepotIndex(rfdd.tile));
Expand Down
2 changes: 1 addition & 1 deletion src/ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Ship FINAL : public SpecializedVehicle<Ship, VEH_SHIP> {
void OnNewDay() override;
Trackdir GetVehicleTrackdir() const override;
TileIndex GetOrderStationLocation(StationID station) override;
ClosestDepot FindClosestDepot() override;
ClosestDepot FindClosestDepot(bool limited = false) override;
void UpdateCache();
void SetDestTile(TileIndex tile) override;
};
Expand Down
12 changes: 10 additions & 2 deletions src/ship_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,17 @@ CommandCost CmdBuildShip(DoCommandFlag flags, TileIndex tile, const Engine *e, V
return CommandCost();
}

ClosestDepot Ship::FindClosestDepot()
ClosestDepot Ship::FindClosestDepot(bool limited)
{
const Depot *depot = FindClosestShipDepot(this, 0);
uint max_distance = 0;
if (limited) {
switch (_settings_game.pf.pathfinder_for_ships) {
case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
default: NOT_REACHED();
}
}
const Depot *depot = FindClosestShipDepot(this, max_distance);
if (depot == nullptr) return ClosestDepot();

return ClosestDepot(depot->xy, depot->index);
Expand Down
2 changes: 1 addition & 1 deletion src/train.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ struct Train FINAL : public GroundVehicle<Train, VEH_TRAIN> {
uint Crash(bool flooded = false) override;
Trackdir GetVehicleTrackdir() const override;
TileIndex GetOrderStationLocation(StationID station) override;
ClosestDepot FindClosestDepot() override;
ClosestDepot FindClosestDepot(bool limited = false) override;

void ReserveTrackUnderConsist() const;

Expand Down
12 changes: 10 additions & 2 deletions src/train_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2170,9 +2170,17 @@ static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
}
}

ClosestDepot Train::FindClosestDepot()
ClosestDepot Train::FindClosestDepot(bool limited)
{
FindDepotData tfdd = FindClosestTrainDepot(this, 0);
uint max_penalty = 0;
if (limited) {
switch (_settings_game.pf.pathfinder_for_trains) {
case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break;
case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break;
default: NOT_REACHED();
}
}
FindDepotData tfdd = FindClosestTrainDepot(this, max_penalty);
if (tfdd.best_length == UINT_MAX) return ClosestDepot();

return ClosestDepot(tfdd.tile, GetDepotIndex(tfdd.tile), tfdd.reverse);
Expand Down
3 changes: 2 additions & 1 deletion src/vehicle_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,10 @@ struct Vehicle : VehiclePool::PoolItem<&_vehicle_pool>, BaseVehicle, BaseConsist
/**
* Find the closest depot for this vehicle and tell us the location,
* DestinationID and whether we should reverse.
* @param limited whether to perform a search within a maximum distance.
* @return A structure with information about the closest depot, if found.
*/
virtual ClosestDepot FindClosestDepot() { return {}; }
virtual ClosestDepot FindClosestDepot([[maybe_unused]] bool limited = false) { return {}; }

virtual void SetDestTile(TileIndex tile) { this->dest_tile = tile; }

Expand Down

0 comments on commit b0ac816

Please sign in to comment.