2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file yapf_costrail.hpp Cost determination for rails. */
10 #ifndef YAPF_COSTRAIL_HPP
11 #define YAPF_COSTRAIL_HPP
14 #include "../../pbs.h"
16 template <class Types
>
17 class CYapfCostRailT
: public CYapfCostBase
{
19 typedef typename
Types::Tpf Tpf
; ///< the pathfinder class (derived from THIS class)
20 typedef typename
Types::TrackFollower TrackFollower
;
21 typedef typename
Types::NodeList::Titem Node
; ///< this will be our node type
22 typedef typename
Node::Key Key
; ///< key to hash tables
23 typedef typename
Node::CachedData CachedData
;
27 /* Structure used inside PfCalcCost() to keep basic tile information. */
37 td
= INVALID_TRACKDIR
;
39 rail_type
= INVALID_RAILTYPE
;
42 TILE(TileIndex tile
, Trackdir td
)
46 this->tile_type
= GetTileType(tile
);
47 this->rail_type
= GetTileRailType(tile
);
53 * @note maximum cost doesn't work with caching enabled
54 * @todo fix maximum cost failing with caching (e.g. FS#2900)
58 std::vector
<int> m_sig_look_ahead_costs
;
61 bool m_stopped_on_first_two_way_signal
;
64 static const int s_max_segment_cost
= 10000;
66 CYapfCostRailT() : m_max_cost(0), m_disable_cache(false), m_stopped_on_first_two_way_signal(false)
68 /* pre-compute look-ahead penalties into array */
69 int p0
= Yapf().PfGetSettings().rail_look_ahead_signal_p0
;
70 int p1
= Yapf().PfGetSettings().rail_look_ahead_signal_p1
;
71 int p2
= Yapf().PfGetSettings().rail_look_ahead_signal_p2
;
72 m_sig_look_ahead_costs
.clear();
73 m_sig_look_ahead_costs
.reserve(Yapf().PfGetSettings().rail_look_ahead_max_signals
);
74 for (uint i
= 0; i
< Yapf().PfGetSettings().rail_look_ahead_max_signals
; i
++) {
75 m_sig_look_ahead_costs
.push_back(p0
+ i
* (p1
+ i
* p2
));
79 /** to access inherited path finder */
82 return *static_cast<Tpf
*>(this);
86 inline int SlopeCost(TileIndex tile
, Trackdir td
)
88 if (!stSlopeCost(tile
, td
)) return 0;
89 return Yapf().PfGetSettings().rail_slope_penalty
;
92 inline int CurveCost(Trackdir td1
, Trackdir td2
)
94 assert(IsValidTrackdir(td1
));
95 assert(IsValidTrackdir(td2
));
97 if (TrackFollower::Allow90degTurns()
98 && HasTrackdir(TrackdirCrossesTrackdirs(td1
), td2
)) {
99 /* 90-deg curve penalty */
100 cost
+= Yapf().PfGetSettings().rail_curve90_penalty
;
101 } else if (td2
!= NextTrackdir(td1
)) {
102 /* 45-deg curve penalty */
103 cost
+= Yapf().PfGetSettings().rail_curve45_penalty
;
108 inline int SwitchCost(TileIndex tile1
, TileIndex tile2
, DiagDirection exitdir
)
110 if (IsPlainRailTile(tile1
) && IsPlainRailTile(tile2
)) {
111 bool t1
= KillFirstBit(GetTrackBits(tile1
) & DiagdirReachesTracks(ReverseDiagDir(exitdir
))) != TRACK_BIT_NONE
;
112 bool t2
= KillFirstBit(GetTrackBits(tile2
) & DiagdirReachesTracks(exitdir
)) != TRACK_BIT_NONE
;
113 if (t1
&& t2
) return Yapf().PfGetSettings().rail_doubleslip_penalty
;
118 /** Return one tile cost (base cost + level crossing penalty). */
119 inline int OneTileCost(TileIndex
&tile
, Trackdir trackdir
)
123 if (IsDiagonalTrackdir(trackdir
)) {
124 cost
+= YAPF_TILE_LENGTH
;
125 switch (GetTileType(tile
)) {
127 /* Increase the cost for level crossings */
128 if (IsLevelCrossing(tile
)) {
129 cost
+= Yapf().PfGetSettings().rail_crossing_penalty
;
137 /* non-diagonal trackdir */
138 cost
= YAPF_TILE_CORNER_LENGTH
;
143 /** Check for a reserved station platform. */
144 inline bool IsAnyStationTileReserved(TileIndex tile
, Trackdir trackdir
, int skipped
)
146 TileIndexDiff diff
= TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(trackdir
)));
147 for (; skipped
>= 0; skipped
--, tile
+= diff
) {
148 if (HasStationReservation(tile
)) return true;
153 /** The cost for reserved tiles, including skipped ones. */
154 inline int ReservationCost(Node
&n
, TileIndex tile
, Trackdir trackdir
, int skipped
)
156 if (n
.m_num_signals_passed
>= m_sig_look_ahead_costs
.size() / 2) return 0;
157 if (!IsPbsSignal(n
.m_last_signal_type
)) return 0;
159 if (IsRailStationTile(tile
) && IsAnyStationTileReserved(tile
, trackdir
, skipped
)) {
160 return Yapf().PfGetSettings().rail_pbs_station_penalty
* (skipped
+ 1);
161 } else if (TrackOverlapsTracks(GetReservedTrackbits(tile
), TrackdirToTrack(trackdir
))) {
162 int cost
= Yapf().PfGetSettings().rail_pbs_cross_penalty
;
163 if (!IsDiagonalTrackdir(trackdir
)) cost
= (cost
* YAPF_TILE_CORNER_LENGTH
) / YAPF_TILE_LENGTH
;
164 return cost
* (skipped
+ 1);
169 int SignalCost(Node
&n
, TileIndex tile
, Trackdir trackdir
)
172 /* if there is one-way signal in the opposite direction, then it is not our way */
173 if (IsTileType(tile
, MP_RAILWAY
)) {
174 bool has_signal_against
= HasSignalOnTrackdir(tile
, ReverseTrackdir(trackdir
));
175 bool has_signal_along
= HasSignalOnTrackdir(tile
, trackdir
);
176 if (has_signal_against
&& !has_signal_along
&& IsOnewaySignal(tile
, TrackdirToTrack(trackdir
))) {
177 /* one-way signal in opposite direction */
178 n
.m_segment
->m_end_segment_reason
|= ESRB_DEAD_END
;
180 if (has_signal_along
) {
181 SignalState sig_state
= GetSignalStateByTrackdir(tile
, trackdir
);
182 SignalType sig_type
= GetSignalType(tile
, TrackdirToTrack(trackdir
));
184 n
.m_last_signal_type
= sig_type
;
186 /* cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is */
187 int look_ahead_cost
= (n
.m_num_signals_passed
< m_sig_look_ahead_costs
.size()) ? m_sig_look_ahead_costs
[n
.m_num_signals_passed
] : 0;
188 if (sig_state
!= SIGNAL_STATE_RED
) {
190 n
.flags_u
.flags_s
.m_last_signal_was_red
= false;
191 /* negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal */
192 if (look_ahead_cost
< 0) {
193 /* add its negation to the cost */
194 cost
-= look_ahead_cost
;
197 /* we have a red signal in our direction
198 * was it first signal which is two-way? */
199 if (!IsPbsSignal(sig_type
) && Yapf().TreatFirstRedTwoWaySignalAsEOL() && n
.flags_u
.flags_s
.m_choice_seen
&& has_signal_against
&& n
.m_num_signals_passed
== 0) {
200 /* yes, the first signal is two-way red signal => DEAD END. Prune this branch... */
201 Yapf().PruneIntermediateNodeBranch(&n
);
202 n
.m_segment
->m_end_segment_reason
|= ESRB_DEAD_END
;
203 Yapf().m_stopped_on_first_two_way_signal
= true;
206 n
.m_last_red_signal_type
= sig_type
;
207 n
.flags_u
.flags_s
.m_last_signal_was_red
= true;
209 /* look-ahead signal penalty */
210 if (!IsPbsSignal(sig_type
) && look_ahead_cost
> 0) {
211 /* add the look ahead penalty only if it is positive */
212 cost
+= look_ahead_cost
;
215 /* special signal penalties */
216 if (n
.m_num_signals_passed
== 0) {
219 case SIGTYPE_EXIT
: cost
+= Yapf().PfGetSettings().rail_firstred_exit_penalty
; break; // first signal is red pre-signal-exit
221 case SIGTYPE_ENTRY
: cost
+= Yapf().PfGetSettings().rail_firstred_penalty
; break;
227 n
.m_num_signals_passed
++;
228 n
.m_segment
->m_last_signal_tile
= tile
;
229 n
.m_segment
->m_last_signal_td
= trackdir
;
232 if (has_signal_against
&& IsPbsSignal(GetSignalType(tile
, TrackdirToTrack(trackdir
)))) {
233 cost
+= n
.m_num_signals_passed
< Yapf().PfGetSettings().rail_look_ahead_max_signals
? Yapf().PfGetSettings().rail_pbs_signal_back_penalty
: 0;
240 inline int PlatformLengthPenalty(int platform_length
)
243 const Train
*v
= Yapf().GetVehicle();
244 assert(v
!= nullptr);
245 assert(v
->type
== VEH_TRAIN
);
246 assert(v
->gcache
.cached_total_length
!= 0);
247 int missing_platform_length
= CeilDiv(v
->gcache
.cached_total_length
, TILE_SIZE
) - platform_length
;
248 if (missing_platform_length
< 0) {
249 /* apply penalty for longer platform than needed */
250 cost
+= Yapf().PfGetSettings().rail_longer_platform_penalty
+ Yapf().PfGetSettings().rail_longer_platform_per_tile_penalty
* -missing_platform_length
;
251 } else if (missing_platform_length
> 0) {
252 /* apply penalty for shorter platform than needed */
253 cost
+= Yapf().PfGetSettings().rail_shorter_platform_penalty
+ Yapf().PfGetSettings().rail_shorter_platform_per_tile_penalty
* missing_platform_length
;
259 inline void SetMaxCost(int max_cost
)
261 m_max_cost
= max_cost
;
265 * Called by YAPF to calculate the cost from the origin to the given node.
266 * Calculates only the cost of given node, adds it to the parent node cost
267 * and stores the result into Node::m_cost member
269 inline bool PfCalcCost(Node
&n
, const TrackFollower
*tf
)
271 assert(!n
.flags_u
.flags_s
.m_targed_seen
);
272 assert(tf
->m_new_tile
== n
.m_key
.m_tile
);
273 assert((HasTrackdir(tf
->m_new_td_bits
, n
.m_key
.m_td
)));
275 /* Does the node have some parent node? */
276 bool has_parent
= (n
.m_parent
!= nullptr);
278 /* Do we already have a cached segment? */
279 CachedData
&segment
= *n
.m_segment
;
280 bool is_cached_segment
= (segment
.m_cost
>= 0);
282 int parent_cost
= has_parent
? n
.m_parent
->m_cost
: 0;
284 /* Each node cost contains 2 or 3 main components:
285 * 1. Transition cost - cost of the move from previous node (tile):
286 * - curve cost (or zero for straight move)
289 * - YAPF_TILE_LENGTH for diagonal tiles
290 * - YAPF_TILE_CORNER_LENGTH for non-diagonal tiles
292 * - tile slope penalty (upward slopes)
293 * - red signal penalty
294 * - level crossing penalty
295 * - speed-limit penalty (bridges)
296 * - station platform penalty
297 * - penalty for reversing in the depot
299 * 3. Extra cost (applies to the last node only)
300 * - last red signal penalty
301 * - penalty for too long or too short platform on the destination station
303 int transition_cost
= 0;
306 /* Segment: one or more tiles connected by contiguous tracks of the same type.
307 * Each segment cost includes 'Tile cost' for all its tiles (including the first
308 * and last), and the 'Transition cost' between its tiles. The first transition
309 * cost of segment entry (move from the 'parent' node) is not included!
311 int segment_entry_cost
= 0;
312 int segment_cost
= 0;
314 const Train
*v
= Yapf().GetVehicle();
316 /* start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment */
317 TILE
cur(n
.m_key
.m_tile
, n
.m_key
.m_td
);
319 /* the previous tile will be needed for transition cost calculations */
320 TILE prev
= !has_parent
? TILE() : TILE(n
.m_parent
->GetLastTile(), n
.m_parent
->GetLastTrackdir());
322 EndSegmentReasonBits end_segment_reason
= ESRB_NONE
;
324 TrackFollower
tf_local(v
, Yapf().GetCompatibleRailTypes());
327 /* We will jump to the middle of the cost calculator assuming that segment cache is not used. */
328 assert(!is_cached_segment
);
329 /* Skip the first transition cost calculation. */
334 /* Transition cost (cost of the move from previous tile) */
335 transition_cost
= Yapf().CurveCost(prev
.td
, cur
.td
);
336 transition_cost
+= Yapf().SwitchCost(prev
.tile
, cur
.tile
, TrackdirToExitdir(prev
.td
));
338 /* First transition cost counts against segment entry cost, other transitions
339 * inside segment will come to segment cost (and will be cached) */
340 if (segment_cost
== 0) {
341 /* We just entered the loop. First transition cost goes to segment entry cost)*/
342 segment_entry_cost
= transition_cost
;
345 /* It is the right time now to look if we can reuse the cached segment cost. */
346 if (is_cached_segment
) {
347 /* Yes, we already know the segment cost. */
348 segment_cost
= segment
.m_cost
;
349 /* We know also the reason why the segment ends. */
350 end_segment_reason
= segment
.m_end_segment_reason
;
351 /* We will need also some information about the last signal (if it was red). */
352 if (segment
.m_last_signal_tile
!= INVALID_TILE
) {
353 assert(HasSignalOnTrackdir(segment
.m_last_signal_tile
, segment
.m_last_signal_td
));
354 SignalState sig_state
= GetSignalStateByTrackdir(segment
.m_last_signal_tile
, segment
.m_last_signal_td
);
355 bool is_red
= (sig_state
== SIGNAL_STATE_RED
);
356 n
.flags_u
.flags_s
.m_last_signal_was_red
= is_red
;
358 n
.m_last_red_signal_type
= GetSignalType(segment
.m_last_signal_tile
, TrackdirToTrack(segment
.m_last_signal_td
));
361 /* No further calculation needed. */
362 cur
= TILE(n
.GetLastTile(), n
.GetLastTrackdir());
366 /* Other than first transition cost count as the regular segment cost. */
367 segment_cost
+= transition_cost
;
370 no_entry_cost
: // jump here at the beginning if the node has no parent (it is the first node)
372 /* All other tile costs will be calculated here. */
373 segment_cost
+= Yapf().OneTileCost(cur
.tile
, cur
.td
);
375 /* If we skipped some tunnel/bridge/station tiles, add their base cost */
376 segment_cost
+= YAPF_TILE_LENGTH
* tf
->m_tiles_skipped
;
379 segment_cost
+= Yapf().SlopeCost(cur
.tile
, cur
.td
);
381 /* Signal cost (routine can modify segment data). */
382 segment_cost
+= Yapf().SignalCost(n
, cur
.tile
, cur
.td
);
384 /* Reserved tiles. */
385 segment_cost
+= Yapf().ReservationCost(n
, cur
.tile
, cur
.td
, tf
->m_tiles_skipped
);
387 end_segment_reason
= segment
.m_end_segment_reason
;
389 /* Tests for 'potential target' reasons to close the segment. */
390 if (cur
.tile
== prev
.tile
) {
391 /* Penalty for reversing in a depot. */
392 assert(IsRailDepot(cur
.tile
));
393 segment_cost
+= Yapf().PfGetSettings().rail_depot_reverse_penalty
;
395 } else if (IsRailDepotTile(cur
.tile
)) {
396 /* We will end in this pass (depot is possible target) */
397 end_segment_reason
|= ESRB_DEPOT
;
399 } else if (cur
.tile_type
== MP_STATION
&& IsRailWaypoint(cur
.tile
)) {
400 if (v
->current_order
.IsType(OT_GOTO_WAYPOINT
) &&
401 GetStationIndex(cur
.tile
) == v
->current_order
.GetDestination() &&
402 !Waypoint::Get(v
->current_order
.GetDestination())->IsSingleTile()) {
403 /* This waypoint is our destination; maybe this isn't an unreserved
404 * one, so check that and if so see that as the last signal being
405 * red. This way waypoints near stations should work better. */
406 CFollowTrackRail
ft(v
);
407 TileIndex t
= cur
.tile
;
408 Trackdir td
= cur
.td
;
409 /* Arbitrary maximum tiles to follow to avoid infinite loops. */
411 while (ft
.Follow(t
, td
)) {
412 assert(t
!= ft
.m_new_tile
);
414 if (t
== cur
.tile
|| --max_tiles
== 0) {
415 /* We looped back on ourself or found another loop, bail out. */
416 td
= INVALID_TRACKDIR
;
419 if (KillFirstBit(ft
.m_new_td_bits
) != TRACKDIR_BIT_NONE
) {
420 /* We encountered a junction; it's going to be too complex to
421 * handle this perfectly, so just bail out. There is no simple
422 * free path, so try the other possibilities. */
423 td
= INVALID_TRACKDIR
;
426 td
= RemoveFirstTrackdir(&ft
.m_new_td_bits
);
427 /* If this is a safe waiting position we're done searching for it */
428 if (IsSafeWaitingPosition(v
, t
, td
, true, _settings_game
.pf
.forbid_90_deg
)) break;
431 /* In the case this platform is (possibly) occupied we add penalty so the
432 * other platforms of this waypoint are evaluated as well, i.e. we assume
433 * that there is a red signal in the waypoint when it's occupied. */
434 if (td
== INVALID_TRACKDIR
||
435 !IsSafeWaitingPosition(v
, t
, td
, true, _settings_game
.pf
.forbid_90_deg
) ||
436 !IsWaitingPositionFree(v
, t
, td
, _settings_game
.pf
.forbid_90_deg
)) {
437 extra_cost
+= Yapf().PfGetSettings().rail_lastred_penalty
;
440 /* Waypoint is also a good reason to finish. */
441 end_segment_reason
|= ESRB_WAYPOINT
;
443 } else if (tf
->m_is_station
) {
444 /* Station penalties. */
445 uint platform_length
= tf
->m_tiles_skipped
+ 1;
446 /* We don't know yet if the station is our target or not. Act like
447 * if it is pass-through station (not our destination). */
448 segment_cost
+= Yapf().PfGetSettings().rail_station_penalty
* platform_length
;
449 /* We will end in this pass (station is possible target) */
450 end_segment_reason
|= ESRB_STATION
;
452 } else if (TrackFollower::DoTrackMasking() && cur
.tile_type
== MP_RAILWAY
) {
453 /* Searching for a safe tile? */
454 if (HasSignalOnTrackdir(cur
.tile
, cur
.td
) && !IsPbsSignal(GetSignalType(cur
.tile
, TrackdirToTrack(cur
.td
)))) {
455 end_segment_reason
|= ESRB_SAFE_TILE
;
459 /* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise
460 * it would cause desync in MP. */
461 if (n
.m_num_signals_passed
< m_sig_look_ahead_costs
.size())
464 int max_speed
= tf
->GetSpeedLimit(&min_speed
);
465 int max_veh_speed
= std::min
<int>(v
->GetDisplayMaxSpeed(), v
->current_order
.GetMaxSpeed());
466 if (max_speed
< max_veh_speed
) {
467 extra_cost
+= YAPF_TILE_LENGTH
* (max_veh_speed
- max_speed
) * (4 + tf
->m_tiles_skipped
) / max_veh_speed
;
469 if (min_speed
> max_veh_speed
) {
470 extra_cost
+= YAPF_TILE_LENGTH
* (min_speed
- max_veh_speed
);
474 /* Finish if we already exceeded the maximum path cost (i.e. when
475 * searching for the nearest depot). */
476 if (m_max_cost
> 0 && (parent_cost
+ segment_entry_cost
+ segment_cost
) > m_max_cost
) {
477 end_segment_reason
|= ESRB_PATH_TOO_LONG
;
480 /* Move to the next tile/trackdir. */
482 tf_local
.Init(v
, Yapf().GetCompatibleRailTypes());
484 if (!tf_local
.Follow(cur
.tile
, cur
.td
)) {
485 assert(tf_local
.m_err
!= TrackFollower::EC_NONE
);
486 /* Can't move to the next tile (EOL?). */
487 if (tf_local
.m_err
== TrackFollower::EC_RAIL_ROAD_TYPE
) {
488 end_segment_reason
|= ESRB_RAIL_TYPE
;
490 end_segment_reason
|= ESRB_DEAD_END
;
493 if (TrackFollower::DoTrackMasking() && !HasOnewaySignalBlockingTrackdir(cur
.tile
, cur
.td
)) {
494 end_segment_reason
|= ESRB_SAFE_TILE
;
499 /* Check if the next tile is not a choice. */
500 if (KillFirstBit(tf_local
.m_new_td_bits
) != TRACKDIR_BIT_NONE
) {
501 /* More than one segment will follow. Close this one. */
502 end_segment_reason
|= ESRB_CHOICE_FOLLOWS
;
506 /* Gather the next tile/trackdir/tile_type/rail_type. */
507 TILE
next(tf_local
.m_new_tile
, (Trackdir
)FindFirstBit(tf_local
.m_new_td_bits
));
509 if (TrackFollower::DoTrackMasking() && IsTileType(next
.tile
, MP_RAILWAY
)) {
510 if (HasSignalOnTrackdir(next
.tile
, next
.td
) && IsPbsSignal(GetSignalType(next
.tile
, TrackdirToTrack(next
.td
)))) {
511 /* Possible safe tile. */
512 end_segment_reason
|= ESRB_SAFE_TILE
;
513 } else if (HasSignalOnTrackdir(next
.tile
, ReverseTrackdir(next
.td
)) && GetSignalType(next
.tile
, TrackdirToTrack(next
.td
)) == SIGTYPE_PBS_ONEWAY
) {
514 /* Possible safe tile, but not so good as it's the back of a signal... */
515 end_segment_reason
|= ESRB_SAFE_TILE
| ESRB_DEAD_END
;
516 extra_cost
+= Yapf().PfGetSettings().rail_lastred_exit_penalty
;
520 /* Check the next tile for the rail type. */
521 if (next
.rail_type
!= cur
.rail_type
) {
522 /* Segment must consist from the same rail_type tiles. */
523 end_segment_reason
|= ESRB_RAIL_TYPE
;
527 /* Avoid infinite looping. */
528 if (next
.tile
== n
.m_key
.m_tile
&& next
.td
== n
.m_key
.m_td
) {
529 end_segment_reason
|= ESRB_INFINITE_LOOP
;
533 if (segment_cost
> s_max_segment_cost
) {
534 /* Potentially in the infinite loop (or only very long segment?). We should
535 * not force it to finish prematurely unless we are on a regular tile. */
536 if (IsTileType(tf
->m_new_tile
, MP_RAILWAY
)) {
537 end_segment_reason
|= ESRB_SEGMENT_TOO_LONG
;
542 /* Any other reason bit set? */
543 if (end_segment_reason
!= ESRB_NONE
) {
547 /* For the next loop set new prev and cur tile info. */
553 /* Don't consider path any further it if exceeded max_cost. */
554 if (end_segment_reason
& ESRB_PATH_TOO_LONG
) return false;
556 bool target_seen
= false;
557 if ((end_segment_reason
& ESRB_POSSIBLE_TARGET
) != ESRB_NONE
) {
558 /* Depot, station or waypoint. */
559 if (Yapf().PfDetectDestination(cur
.tile
, cur
.td
)) {
560 /* Destination found. */
565 /* Update the segment if needed. */
566 if (!is_cached_segment
) {
567 /* Write back the segment information so it can be reused the next time. */
568 segment
.m_cost
= segment_cost
;
569 segment
.m_end_segment_reason
= end_segment_reason
& ESRB_CACHED_MASK
;
570 /* Save end of segment back to the node. */
571 n
.SetLastTileTrackdir(cur
.tile
, cur
.td
);
574 /* Do we have an excuse why not to continue pathfinding in this direction? */
575 if (!target_seen
&& (end_segment_reason
& ESRB_ABORT_PF_MASK
) != ESRB_NONE
) {
576 /* Reason to not continue. Stop this PF branch. */
580 /* Special costs for the case we have reached our target. */
582 n
.flags_u
.flags_s
.m_targed_seen
= true;
583 /* Last-red and last-red-exit penalties. */
584 if (n
.flags_u
.flags_s
.m_last_signal_was_red
) {
585 if (n
.m_last_red_signal_type
== SIGTYPE_EXIT
) {
586 /* last signal was red pre-signal-exit */
587 extra_cost
+= Yapf().PfGetSettings().rail_lastred_exit_penalty
;
588 } else if (!IsPbsSignal(n
.m_last_red_signal_type
)) {
589 /* Last signal was red, but not exit or path signal. */
590 extra_cost
+= Yapf().PfGetSettings().rail_lastred_penalty
;
594 /* Station platform-length penalty. */
595 if ((end_segment_reason
& ESRB_STATION
) != ESRB_NONE
) {
596 const BaseStation
*st
= BaseStation::GetByTile(n
.GetLastTile());
597 assert(st
!= nullptr);
598 uint platform_length
= st
->GetPlatformLength(n
.GetLastTile(), ReverseDiagDir(TrackdirToExitdir(n
.GetLastTrackdir())));
599 /* Reduce the extra cost caused by passing-station penalty (each station receives it in the segment cost). */
600 extra_cost
-= Yapf().PfGetSettings().rail_station_penalty
* platform_length
;
601 /* Add penalty for the inappropriate platform length. */
602 extra_cost
+= PlatformLengthPenalty(platform_length
);
606 /* total node cost */
607 n
.m_cost
= parent_cost
+ segment_entry_cost
+ segment_cost
+ extra_cost
;
612 inline bool CanUseGlobalCache(Node
&n
) const
614 return !m_disable_cache
615 && (n
.m_parent
!= nullptr)
616 && (n
.m_parent
->m_num_signals_passed
>= m_sig_look_ahead_costs
.size());
619 inline void ConnectNodeToCachedData(Node
&n
, CachedData
&ci
)
622 if (n
.m_segment
->m_cost
< 0) {
623 n
.m_segment
->m_last_tile
= n
.m_key
.m_tile
;
624 n
.m_segment
->m_last_td
= n
.m_key
.m_td
;
628 void DisableCache(bool disable
)
630 m_disable_cache
= disable
;
634 #endif /* YAPF_COSTRAIL_HPP */