diff --git a/src/pathfinder/yapf/yapf_costrail.hpp b/src/pathfinder/yapf/yapf_costrail.hpp index 1c2b1e61fb..e3a4211149 100644 --- a/src/pathfinder/yapf/yapf_costrail.hpp +++ b/src/pathfinder/yapf/yapf_costrail.hpp @@ -161,12 +161,18 @@ public: if (n.num_signals_passed >= this->sig_look_ahead_costs.size() / 2) return 0; if (!IsPbsSignal(n.last_signal_type)) return 0; + const Train *v = Yapf().GetVehicle(); + assert(v != nullptr); + assert(v->type == VEH_TRAIN); + /* the longer a train waits, the more it will consider longer routes around blocking trains */ + int impatience = std::max(0, v->wait_counter - DAY_TICKS / 2); + if (IsRailStationTile(tile) && IsAnyStationTileReserved(tile, trackdir, skipped)) { - return Yapf().PfGetSettings().rail_pbs_station_penalty * (skipped + 1); + return Yapf().PfGetSettings().rail_pbs_station_penalty * (skipped + 1) + impatience; } else if (TrackOverlapsTracks(GetReservedTrackbits(tile), TrackdirToTrack(trackdir))) { int cost = Yapf().PfGetSettings().rail_pbs_cross_penalty; if (!IsDiagonalTrackdir(trackdir)) cost = (cost * YAPF_TILE_CORNER_LENGTH) / YAPF_TILE_LENGTH; - return cost * (skipped + 1); + return cost * (skipped + 1) + impatience; } return 0; }