Skip to content

Commit

Permalink
Change: Use a max_penalty parameter instead, and add a virtual functi…
Browse files Browse the repository at this point in the history
…on to get the proper value

This virtual function can also be used in CheckIfXXXNeedsService.
  • Loading branch information
SamuXarick committed Dec 6, 2023
1 parent 24f9ea6 commit c46407b
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 56 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(bool limited = false) override;
ClosestDepot FindClosestDepot(uint32_t max_penalty = 0) 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([[maybe_unused]] bool limited)
ClosestDepot Aircraft::FindClosestDepot([[maybe_unused]] uint32_t max_penalty)
{
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(true);
ClosestDepot closestDepot = v->FindClosestDepot(v->GetMaxDepotSearchPenalty());

if (closestDepot.found) {
/* PBS reservations cannot reverse */
Expand Down
3 changes: 2 additions & 1 deletion src/roadveh.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ 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(bool limited = false) override;
ClosestDepot FindClosestDepot(uint32_t max_penalty = 0) override;
uint32_t GetMaxDepotSearchPenalty() const override;

bool IsBus() const;

Expand Down
26 changes: 11 additions & 15 deletions src/roadveh_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,8 @@ static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance
}
}

ClosestDepot RoadVehicle::FindClosestDepot(bool limited)
ClosestDepot RoadVehicle::FindClosestDepot(uint32_t max_penalty)
{
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();

Expand Down Expand Up @@ -1679,12 +1671,7 @@ static void CheckIfRoadVehNeedsService(RoadVehicle *v)
return;
}

uint max_penalty;
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();
}
uint max_penalty = v->GetMaxDepotSearchPenalty();

FindDepotData rfdd = FindClosestRoadDepot(v, max_penalty);
/* Only go to the depot if it is not too far out of our way. */
Expand Down Expand Up @@ -1773,3 +1760,12 @@ uint16_t RoadVehicle::GetMaxWeight() const

return weight;
}

uint32_t RoadVehicle::GetMaxDepotSearchPenalty() const
{
switch (_settings_game.pf.pathfinder_for_roadvehs) {
case VPF_NPF: return _settings_game.pf.npf.maximum_go_to_depot_penalty;
case VPF_YAPF: return _settings_game.pf.yapf.maximum_go_to_depot_penalty;
default: NOT_REACHED();
}
}
3 changes: 2 additions & 1 deletion src/ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ struct Ship FINAL : public SpecializedVehicle<Ship, VEH_SHIP> {
void OnNewDay() override;
Trackdir GetVehicleTrackdir() const override;
TileIndex GetOrderStationLocation(StationID station) override;
ClosestDepot FindClosestDepot(bool limited = false) override;
ClosestDepot FindClosestDepot(uint32_t max_penalty = 0) override;
uint32_t GetMaxDepotSearchPenalty() const override;
void UpdateCache();
void SetDestTile(TileIndex tile) override;
};
Expand Down
30 changes: 12 additions & 18 deletions src/ship_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,7 @@ static void CheckIfShipNeedsService(Vehicle *v)
return;
}

uint max_distance;
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(v, max_distance);
const Depot *depot = FindClosestShipDepot(v, v->GetMaxDepotSearchPenalty());

if (depot == nullptr) {
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
Expand Down Expand Up @@ -925,18 +918,19 @@ CommandCost CmdBuildShip(DoCommandFlag flags, TileIndex tile, const Engine *e, V
return CommandCost();
}

ClosestDepot Ship::FindClosestDepot(bool limited)
ClosestDepot Ship::FindClosestDepot(uint32_t max_penalty)
{
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);
const Depot *depot = FindClosestShipDepot(this, max_penalty);
if (depot == nullptr) return ClosestDepot();

return ClosestDepot(depot->xy, depot->index);
}

uint32_t Ship::GetMaxDepotSearchPenalty() const
{
switch (_settings_game.pf.pathfinder_for_ships) {
case VPF_NPF: return _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH;
case VPF_YAPF: return _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH;
default: NOT_REACHED();
}
}
3 changes: 2 additions & 1 deletion src/train.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ 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(bool limited = false) override;
ClosestDepot FindClosestDepot(uint32_t max_penalty = 0) override;
uint32_t GetMaxDepotSearchPenalty() const override;

void ReserveTrackUnderConsist() const;

Expand Down
26 changes: 11 additions & 15 deletions src/train_cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2170,16 +2170,8 @@ static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
}
}

ClosestDepot Train::FindClosestDepot(bool limited)
ClosestDepot Train::FindClosestDepot(uint32_t max_penalty)
{
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();

Expand Down Expand Up @@ -4137,12 +4129,7 @@ static void CheckIfTrainNeedsService(Train *v)
return;
}

uint max_penalty;
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();
}
uint max_penalty = v->GetMaxDepotSearchPenalty();

FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
/* Only go to the depot if it is not too far out of our way. */
Expand Down Expand Up @@ -4243,3 +4230,12 @@ uint16_t Train::GetMaxWeight() const

return weight;
}

uint32_t Train::GetMaxDepotSearchPenalty() const
{
switch (_settings_game.pf.pathfinder_for_trains) {
case VPF_NPF: return _settings_game.pf.npf.maximum_go_to_depot_penalty;
case VPF_YAPF: return _settings_game.pf.yapf.maximum_go_to_depot_penalty;
default: NOT_REACHED();
}
}
6 changes: 4 additions & 2 deletions src/vehicle_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -783,10 +783,12 @@ 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.
* @param max_penalty value used to limit the search within a maximum distance (0 for unlimited).
* @return A structure with information about the closest depot, if found.
*/
virtual ClosestDepot FindClosestDepot([[maybe_unused]] bool limited = false) { return {}; }
virtual ClosestDepot FindClosestDepot([[maybe_unused]] uint32_t max_penalty = 0) { return {}; }

virtual uint32_t GetMaxDepotSearchPenalty() const { return 0; }

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

Expand Down

0 comments on commit c46407b

Please sign in to comment.