10 #ifndef YAPF_COSTRAIL_HPP
11 #define YAPF_COSTRAIL_HPP
14 #include "../../pbs.h"
16 template <
class Types>
19 typedef typename Types::Tpf
Tpf;
20 typedef typename Types::TrackFollower TrackFollower;
21 typedef typename Types::NodeList::Titem
Node;
22 typedef typename Node::Key
Key;
23 typedef typename Node::CachedData CachedData;
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;
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));
82 return *
static_cast<Tpf *
>(
this);
89 return Yapf().PfGetSettings().rail_slope_penalty;
97 if (TrackFollower::Allow90degTurns()
100 cost +=
Yapf().PfGetSettings().rail_curve90_penalty;
103 cost +=
Yapf().PfGetSettings().rail_curve45_penalty;
113 if (t1 && t2)
return Yapf().PfGetSettings().rail_doubleslip_penalty;
129 cost +=
Yapf().PfGetSettings().rail_crossing_penalty;
147 for (; skipped >= 0; skipped--, tile += diff) {
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;
160 return Yapf().PfGetSettings().rail_pbs_station_penalty * (skipped + 1);
162 int cost =
Yapf().PfGetSettings().rail_pbs_cross_penalty;
164 return cost * (skipped + 1);
178 n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
180 if (has_signal_along) {
184 n.m_last_signal_type = sig_type;
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;
190 n.flags_u.flags_s.m_last_signal_was_red =
false;
192 if (look_ahead_cost < 0) {
194 cost -= look_ahead_cost;
199 if (!IsPbsSignal(sig_type) &&
Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
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;
210 if (!IsPbsSignal(sig_type) && look_ahead_cost > 0) {
212 cost += look_ahead_cost;
216 if (n.m_num_signals_passed == 0) {
219 case SIGTYPE_EXIT: cost +=
Yapf().PfGetSettings().rail_firstred_exit_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)
244 assert(v !=
nullptr);
248 if (missing_platform_length < 0) {
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) {
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)
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)));
276 bool has_parent = (n.m_parent !=
nullptr);
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;
303 int transition_cost = 0;
311 int segment_entry_cost = 0;
312 int segment_cost = 0;
317 TILE cur(n.m_key.m_tile, n.m_key.m_td);
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());
328 assert(!is_cached_segment);
335 transition_cost =
Yapf().CurveCost(prev.td, cur.td);
340 if (segment_cost == 0) {
342 segment_entry_cost = transition_cost;
346 if (is_cached_segment) {
348 segment_cost = segment.m_cost;
350 end_segment_reason = segment.m_end_segment_reason;
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));
362 cur =
TILE(n.GetLastTile(), n.GetLastTrackdir());
367 segment_cost += transition_cost;
373 segment_cost +=
Yapf().OneTileCost(cur.tile, cur.td);
379 segment_cost +=
Yapf().SlopeCost(cur.tile, cur.td);
382 segment_cost +=
Yapf().SignalCost(n, cur.tile, cur.td);
385 segment_cost +=
Yapf().ReservationCost(n, cur.tile, cur.td, tf->m_tiles_skipped);
387 end_segment_reason = segment.m_end_segment_reason;
390 if (cur.tile == prev.tile) {
393 segment_cost +=
Yapf().PfGetSettings().rail_depot_reverse_penalty;
397 end_segment_reason |= ESRB_DEPOT;
411 while (ft.
Follow(t, td)) {
414 if (t == cur.tile || --max_tiles == 0) {
437 extra_cost +=
Yapf().PfGetSettings().rail_lastred_penalty;
441 end_segment_reason |= ESRB_WAYPOINT;
443 }
else if (tf->m_is_station) {
445 uint platform_length = tf->m_tiles_skipped + 1;
448 segment_cost +=
Yapf().PfGetSettings().rail_station_penalty * platform_length;
450 end_segment_reason |= ESRB_STATION;
452 }
else if (TrackFollower::DoTrackMasking() && cur.tile_type ==
MP_RAILWAY) {
455 end_segment_reason |= ESRB_SAFE_TILE;
461 if (n.m_num_signals_passed < m_sig_look_ahead_costs.size())
464 int max_speed = tf->GetSpeedLimit(&min_speed);
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) {
477 end_segment_reason |= ESRB_PATH_TOO_LONG;
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);
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;
494 end_segment_reason |= ESRB_SAFE_TILE;
502 end_segment_reason |= ESRB_CHOICE_FOLLOWS;
512 end_segment_reason |= ESRB_SAFE_TILE;
515 end_segment_reason |= ESRB_SAFE_TILE | ESRB_DEAD_END;
516 extra_cost +=
Yapf().PfGetSettings().rail_lastred_exit_penalty;
521 if (next.rail_type != cur.rail_type) {
523 end_segment_reason |= ESRB_RAIL_TYPE;
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) {
537 end_segment_reason |= ESRB_SEGMENT_TOO_LONG;
543 if (end_segment_reason != ESRB_NONE) {
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) {
559 if (
Yapf().PfDetectDestination(cur.tile, cur.td)) {
566 if (!is_cached_segment) {
568 segment.m_cost = segment_cost;
569 segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK;
571 n.SetLastTileTrackdir(cur.tile, cur.td);
575 if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) {
582 n.flags_u.flags_s.m_targed_seen =
true;
584 if (n.flags_u.flags_s.m_last_signal_was_red) {
587 extra_cost +=
Yapf().PfGetSettings().rail_lastred_exit_penalty;
588 }
else if (!IsPbsSignal(n.m_last_red_signal_type)) {
590 extra_cost +=
Yapf().PfGetSettings().rail_lastred_penalty;
595 if ((end_segment_reason & ESRB_STATION) != ESRB_NONE) {
597 assert(st !=
nullptr);
600 extra_cost -=
Yapf().PfGetSettings().rail_station_penalty * platform_length;
602 extra_cost += PlatformLengthPenalty(platform_length);
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;