00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef YAPF_COSTRAIL_HPP
00013 #define YAPF_COSTRAIL_HPP
00014
00015 #include "../../pbs.h"
00016
00017 template <class Types>
00018 class CYapfCostRailT
00019 : public CYapfCostBase
00020 {
00021 public:
00022 typedef typename Types::Tpf Tpf;
00023 typedef typename Types::TrackFollower TrackFollower;
00024 typedef typename Types::NodeList::Titem Node;
00025 typedef typename Node::Key Key;
00026 typedef typename Node::CachedData CachedData;
00027
00028 protected:
00029
00030
00031 struct TILE {
00032 TileIndex tile;
00033 Trackdir td;
00034 TileType tile_type;
00035 RailType rail_type;
00036
00037 TILE()
00038 {
00039 tile = INVALID_TILE;
00040 td = INVALID_TRACKDIR;
00041 tile_type = MP_VOID;
00042 rail_type = INVALID_RAILTYPE;
00043 }
00044
00045 TILE(TileIndex tile, Trackdir td)
00046 {
00047 this->tile = tile;
00048 this->td = td;
00049 this->tile_type = GetTileType(tile);
00050 this->rail_type = GetTileRailType(tile);
00051 }
00052
00053 TILE(const TILE &src)
00054 {
00055 tile = src.tile;
00056 td = src.td;
00057 tile_type = src.tile_type;
00058 rail_type = src.rail_type;
00059 }
00060 };
00061
00062 protected:
00067 int m_max_cost;
00068 CBlobT<int> m_sig_look_ahead_costs;
00069 bool m_disable_cache;
00070
00071 public:
00072 bool m_stopped_on_first_two_way_signal;
00073 protected:
00074
00075 static const int s_max_segment_cost = 10000;
00076
00077 CYapfCostRailT()
00078 : m_max_cost(0)
00079 , m_disable_cache(false)
00080 , m_stopped_on_first_two_way_signal(false)
00081 {
00082
00083 int p0 = Yapf().PfGetSettings().rail_look_ahead_signal_p0;
00084 int p1 = Yapf().PfGetSettings().rail_look_ahead_signal_p1;
00085 int p2 = Yapf().PfGetSettings().rail_look_ahead_signal_p2;
00086 int *pen = m_sig_look_ahead_costs.GrowSizeNC(Yapf().PfGetSettings().rail_look_ahead_max_signals);
00087 for (uint i = 0; i < Yapf().PfGetSettings().rail_look_ahead_max_signals; i++) {
00088 pen[i] = p0 + i * (p1 + i * p2);
00089 }
00090 }
00091
00093 Tpf& Yapf()
00094 {
00095 return *static_cast<Tpf*>(this);
00096 }
00097
00098 public:
00099 FORCEINLINE int SlopeCost(TileIndex tile, Trackdir td)
00100 {
00101 CPerfStart perf_cost(Yapf().m_perf_slope_cost);
00102 if (!stSlopeCost(tile, td)) return 0;
00103 return Yapf().PfGetSettings().rail_slope_penalty;
00104 }
00105
00106 FORCEINLINE int CurveCost(Trackdir td1, Trackdir td2)
00107 {
00108 assert(IsValidTrackdir(td1));
00109 assert(IsValidTrackdir(td2));
00110 int cost = 0;
00111 if (TrackFollower::Allow90degTurns()
00112 && ((TrackdirToTrackdirBits(td2) & (TrackdirBits)TrackdirCrossesTrackdirs(td1)) != 0)) {
00113
00114 cost += Yapf().PfGetSettings().rail_curve90_penalty;
00115 } else if (td2 != NextTrackdir(td1)) {
00116
00117 cost += Yapf().PfGetSettings().rail_curve45_penalty;
00118 }
00119 return cost;
00120 }
00121
00122 FORCEINLINE int SwitchCost(TileIndex tile1, TileIndex tile2, DiagDirection exitdir)
00123 {
00124 if (IsPlainRailTile(tile1) && IsPlainRailTile(tile2)) {
00125 bool t1 = KillFirstBit(GetTrackBits(tile1) & DiagdirReachesTracks(ReverseDiagDir(exitdir))) != TRACK_BIT_NONE;
00126 bool t2 = KillFirstBit(GetTrackBits(tile2) & DiagdirReachesTracks(exitdir)) != TRACK_BIT_NONE;
00127 if (t1 && t2) return Yapf().PfGetSettings().rail_doubleslip_penalty;
00128 }
00129 return 0;
00130 }
00131
00133 FORCEINLINE int OneTileCost(TileIndex& tile, Trackdir trackdir)
00134 {
00135 int cost = 0;
00136
00137 if (IsDiagonalTrackdir(trackdir)) {
00138 cost += YAPF_TILE_LENGTH;
00139 switch (GetTileType(tile)) {
00140 case MP_ROAD:
00141
00142 if (IsLevelCrossing(tile)) {
00143 cost += Yapf().PfGetSettings().rail_crossing_penalty;
00144 }
00145 break;
00146
00147 default:
00148 break;
00149 }
00150 } else {
00151
00152 cost = YAPF_TILE_CORNER_LENGTH;
00153 }
00154 return cost;
00155 }
00156
00158 FORCEINLINE bool IsAnyStationTileReserved(TileIndex tile, Trackdir trackdir, int skipped)
00159 {
00160 TileIndexDiff diff = TileOffsByDiagDir(TrackdirToExitdir(ReverseTrackdir(trackdir)));
00161 for (; skipped >= 0; skipped--, tile += diff) {
00162 if (HasStationReservation(tile)) return true;
00163 }
00164 return false;
00165 }
00166
00168 FORCEINLINE int ReservationCost(Node& n, TileIndex tile, Trackdir trackdir, int skipped)
00169 {
00170 if (n.m_num_signals_passed >= m_sig_look_ahead_costs.Size() / 2) return 0;
00171 if (!IsPbsSignal(n.m_last_signal_type)) return 0;
00172
00173 if (IsRailStationTile(tile) && IsAnyStationTileReserved(tile, trackdir, skipped)) {
00174 return Yapf().PfGetSettings().rail_pbs_station_penalty * (skipped + 1);
00175 } else if (TrackOverlapsTracks(GetReservedTrackbits(tile), TrackdirToTrack(trackdir))) {
00176 int cost = Yapf().PfGetSettings().rail_pbs_cross_penalty;
00177 if (!IsDiagonalTrackdir(trackdir)) cost = (cost * YAPF_TILE_CORNER_LENGTH) / YAPF_TILE_LENGTH;
00178 return cost * (skipped + 1);
00179 }
00180 return 0;
00181 }
00182
00183 int SignalCost(Node& n, TileIndex tile, Trackdir trackdir)
00184 {
00185 int cost = 0;
00186
00187 CPerfStart perf_cost(Yapf().m_perf_other_cost);
00188 if (IsTileType(tile, MP_RAILWAY)) {
00189 bool has_signal_against = HasSignalOnTrackdir(tile, ReverseTrackdir(trackdir));
00190 bool has_signal_along = HasSignalOnTrackdir(tile, trackdir);
00191 if (has_signal_against && !has_signal_along && IsOnewaySignal(tile, TrackdirToTrack(trackdir))) {
00192
00193 n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
00194 } else {
00195 if (has_signal_along) {
00196 SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
00197 SignalType sig_type = GetSignalType(tile, TrackdirToTrack(trackdir));
00198
00199 n.m_last_signal_type = sig_type;
00200
00201
00202 int look_ahead_cost = (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) ? m_sig_look_ahead_costs.Data()[n.m_num_signals_passed] : 0;
00203 if (sig_state != SIGNAL_STATE_RED) {
00204
00205 n.flags_u.flags_s.m_last_signal_was_red = false;
00206
00207 if (look_ahead_cost < 0) {
00208
00209 cost -= look_ahead_cost;
00210 }
00211 } else {
00212
00213
00214 if (!IsPbsSignal(sig_type) && Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
00215
00216 n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
00217 Yapf().m_stopped_on_first_two_way_signal = true;
00218 return -1;
00219 }
00220 n.m_last_red_signal_type = sig_type;
00221 n.flags_u.flags_s.m_last_signal_was_red = true;
00222
00223
00224 if (!IsPbsSignal(sig_type) && look_ahead_cost > 0) {
00225
00226 cost += look_ahead_cost;
00227 }
00228
00229
00230 if (n.m_num_signals_passed == 0) {
00231 switch (sig_type) {
00232 case SIGTYPE_PROG:
00233 case SIGTYPE_COMBO:
00234 case SIGTYPE_EXIT: cost += Yapf().PfGetSettings().rail_firstred_exit_penalty; break;
00235 case SIGTYPE_NORMAL:
00236 case SIGTYPE_ENTRY: cost += Yapf().PfGetSettings().rail_firstred_penalty; break;
00237 default: break;
00238 }
00239 }
00240 }
00241
00242 n.m_num_signals_passed++;
00243 n.m_segment->m_last_signal_tile = tile;
00244 n.m_segment->m_last_signal_td = trackdir;
00245 }
00246
00247 if (has_signal_against && IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) {
00248 cost += n.m_num_signals_passed < Yapf().PfGetSettings().rail_look_ahead_max_signals ? Yapf().PfGetSettings().rail_pbs_signal_back_penalty : 0;
00249 }
00250 }
00251 }
00252 return cost;
00253 }
00254
00255 FORCEINLINE int PlatformLengthPenalty(int platform_length)
00256 {
00257 int cost = 0;
00258 const Train *v = Yapf().GetVehicle();
00259 assert(v != NULL);
00260 assert(v->type == VEH_TRAIN);
00261 assert(v->gcache.cached_total_length != 0);
00262 int missing_platform_length = CeilDiv(v->gcache.cached_total_length, TILE_SIZE) - platform_length;
00263 if (missing_platform_length < 0) {
00264
00265 cost += Yapf().PfGetSettings().rail_longer_platform_penalty + Yapf().PfGetSettings().rail_longer_platform_per_tile_penalty * -missing_platform_length;
00266 } else if (missing_platform_length > 0) {
00267
00268 cost += Yapf().PfGetSettings().rail_shorter_platform_penalty + Yapf().PfGetSettings().rail_shorter_platform_per_tile_penalty * missing_platform_length;
00269 }
00270 return cost;
00271 }
00272
00273 public:
00274 FORCEINLINE void SetMaxCost(int max_cost)
00275 {
00276 m_max_cost = max_cost;
00277 }
00278
00284 FORCEINLINE bool PfCalcCost(Node &n, const TrackFollower *tf)
00285 {
00286 assert(!n.flags_u.flags_s.m_targed_seen);
00287 assert(tf->m_new_tile == n.m_key.m_tile);
00288 assert((TrackdirToTrackdirBits(n.m_key.m_td) & tf->m_new_td_bits) != TRACKDIR_BIT_NONE);
00289
00290 CPerfStart perf_cost(Yapf().m_perf_cost);
00291
00292
00293 bool has_parent = (n.m_parent != NULL);
00294
00295
00296 CachedData &segment = *n.m_segment;
00297 bool is_cached_segment = (segment.m_cost >= 0);
00298
00299 int parent_cost = has_parent ? n.m_parent->m_cost : 0;
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 int transition_cost = 0;
00321 int extra_cost = 0;
00322
00323
00324
00325
00326
00327
00328 int segment_entry_cost = 0;
00329 int segment_cost = 0;
00330
00331 const Train *v = Yapf().GetVehicle();
00332
00333
00334 TILE cur(n.m_key.m_tile, n.m_key.m_td);
00335
00336
00337 TILE prev = !has_parent ? TILE() : TILE(n.m_parent->GetLastTile(), n.m_parent->GetLastTrackdir());
00338
00339 EndSegmentReasonBits end_segment_reason = ESRB_NONE;
00340
00341 TrackFollower tf_local(v, Yapf().GetCompatibleRailTypes(), &Yapf().m_perf_ts_cost);
00342
00343 if (!has_parent) {
00344
00345 assert(!is_cached_segment);
00346
00347 goto no_entry_cost;
00348 }
00349
00350 for (;;) {
00351
00352 transition_cost = Yapf().CurveCost(prev.td, cur.td);
00353 transition_cost += Yapf().SwitchCost(prev.tile, cur.tile, TrackdirToExitdir(prev.td));
00354
00355
00356
00357 if (segment_cost == 0) {
00358
00359 segment_entry_cost = transition_cost;
00360 transition_cost = 0;
00361
00362
00363 if (is_cached_segment) {
00364
00365 segment_cost = segment.m_cost;
00366
00367 end_segment_reason = segment.m_end_segment_reason;
00368
00369 if (segment.m_last_signal_tile != INVALID_TILE) {
00370 assert(HasSignalOnTrackdir(segment.m_last_signal_tile, segment.m_last_signal_td));
00371 SignalState sig_state = GetSignalStateByTrackdir(segment.m_last_signal_tile, segment.m_last_signal_td);
00372 bool is_red = (sig_state == SIGNAL_STATE_RED);
00373 n.flags_u.flags_s.m_last_signal_was_red = is_red;
00374 if (is_red) {
00375 n.m_last_red_signal_type = GetSignalType(segment.m_last_signal_tile, TrackdirToTrack(segment.m_last_signal_td));
00376 }
00377 }
00378
00379 cur = TILE(n.GetLastTile(), n.GetLastTrackdir());
00380 break;
00381 }
00382 } else {
00383
00384 segment_cost += transition_cost;
00385 }
00386
00387 no_entry_cost:
00388
00389
00390 segment_cost += Yapf().OneTileCost(cur.tile, cur.td);
00391
00392
00393 segment_cost += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
00394
00395
00396 segment_cost += Yapf().SlopeCost(cur.tile, cur.td);
00397
00398
00399 segment_cost += Yapf().SignalCost(n, cur.tile, cur.td);
00400
00401
00402 segment_cost += Yapf().ReservationCost(n, cur.tile, cur.td, tf->m_tiles_skipped);
00403
00404 end_segment_reason = segment.m_end_segment_reason;
00405
00406
00407 if (cur.tile == prev.tile) {
00408
00409 assert(IsRailDepot(cur.tile));
00410 segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty;
00411
00412 end_segment_reason |= ESRB_DEPOT;
00413
00414 } else if (cur.tile_type == MP_STATION && IsRailWaypoint(cur.tile)) {
00415 if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
00416 GetStationIndex(cur.tile) == v->current_order.GetDestination() &&
00417 !Waypoint::Get(v->current_order.GetDestination())->IsSingleTile()) {
00418
00419
00420
00421 CFollowTrackRail ft(v);
00422 TileIndex t = cur.tile;
00423 Trackdir td = cur.td;
00424 while (ft.Follow(t, td)) {
00425 assert(t != ft.m_new_tile);
00426 t = ft.m_new_tile;
00427 if (KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
00428
00429
00430
00431 td = INVALID_TRACKDIR;
00432 break;
00433 }
00434 td = RemoveFirstTrackdir(&ft.m_new_td_bits);
00435
00436 if (IsSafeWaitingPosition(v, t, td, true, _settings_game.pf.forbid_90_deg)) break;
00437 }
00438
00439
00440
00441
00442 if (td == INVALID_TRACKDIR ||
00443 !IsSafeWaitingPosition(v, t, td, true, _settings_game.pf.forbid_90_deg) ||
00444 !IsWaitingPositionFree(v, t, td, _settings_game.pf.forbid_90_deg)) {
00445 extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
00446 }
00447 }
00448
00449 end_segment_reason |= ESRB_WAYPOINT;
00450
00451 } else if (tf->m_is_station) {
00452
00453 uint platform_length = tf->m_tiles_skipped + 1;
00454
00455
00456 segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
00457
00458 end_segment_reason |= ESRB_STATION;
00459
00460 } else if (TrackFollower::DoTrackMasking() && cur.tile_type == MP_RAILWAY) {
00461
00462 if (HasSignalOnTrackdir(cur.tile, cur.td) && !IsPbsSignal(GetSignalType(cur.tile, TrackdirToTrack(cur.td)))) {
00463 end_segment_reason |= ESRB_SAFE_TILE;
00464 }
00465 }
00466
00467
00468
00469 if (n.m_num_signals_passed < m_sig_look_ahead_costs.Size())
00470 {
00471 int min_speed = 0;
00472 int max_speed = tf->GetSpeedLimit(&min_speed);
00473 int max_veh_speed = v->GetDisplayMaxSpeed();
00474 if (max_speed < max_veh_speed) {
00475 extra_cost += YAPF_TILE_LENGTH * (max_veh_speed - max_speed) * (4 + tf->m_tiles_skipped) / max_veh_speed;
00476 }
00477 if (min_speed > max_veh_speed) {
00478 extra_cost += YAPF_TILE_LENGTH * (min_speed - max_veh_speed);
00479 }
00480 }
00481
00482
00483
00484 if (m_max_cost > 0 && (parent_cost + segment_entry_cost + segment_cost) > m_max_cost) {
00485 end_segment_reason |= ESRB_PATH_TOO_LONG;
00486 }
00487
00488
00489 tf = &tf_local;
00490 tf_local.Init(v, Yapf().GetCompatibleRailTypes(), &Yapf().m_perf_ts_cost);
00491
00492 if (!tf_local.Follow(cur.tile, cur.td)) {
00493 assert(tf_local.m_err != TrackFollower::EC_NONE);
00494
00495 if (tf_local.m_err == TrackFollower::EC_RAIL_TYPE) {
00496 end_segment_reason |= ESRB_RAIL_TYPE;
00497 } else {
00498 end_segment_reason |= ESRB_DEAD_END;
00499 }
00500
00501 if (TrackFollower::DoTrackMasking() && !HasOnewaySignalBlockingTrackdir(cur.tile, cur.td)) {
00502 end_segment_reason |= ESRB_SAFE_TILE;
00503 }
00504 break;
00505 }
00506
00507
00508 if (KillFirstBit(tf_local.m_new_td_bits) != TRACKDIR_BIT_NONE) {
00509
00510 end_segment_reason |= ESRB_CHOICE_FOLLOWS;
00511 break;
00512 }
00513
00514
00515 TILE next(tf_local.m_new_tile, (Trackdir)FindFirstBit2x64(tf_local.m_new_td_bits));
00516
00517
00518 if (_settings_game.pf.back_of_one_way_pbs_waiting_point) {
00519 if (TrackFollower::DoTrackMasking() && IsTileType(next.tile, MP_RAILWAY)) {
00520 if (HasSignalOnTrackdir(next.tile, next.td) && IsPbsSignal(GetSignalType(next.tile, TrackdirToTrack(next.td)))) {
00521
00522 end_segment_reason |= ESRB_SAFE_TILE;
00523 } else if (HasSignalOnTrackdir(next.tile, ReverseTrackdir(next.td)) && GetSignalType(next.tile, TrackdirToTrack(next.td)) == SIGTYPE_PBS_ONEWAY) {
00524
00525 end_segment_reason |= ESRB_SAFE_TILE | ESRB_DEAD_END;
00526 extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
00527 }
00528 }
00529 } else {
00530 if (TrackFollower::DoTrackMasking() && HasPbsSignalOnTrackdir(next.tile, next.td)) {
00531
00532 end_segment_reason |= ESRB_SAFE_TILE;
00533 }
00534 }
00535
00536
00537 if (next.rail_type != cur.rail_type) {
00538
00539 end_segment_reason |= ESRB_RAIL_TYPE;
00540 break;
00541 }
00542
00543
00544 if (next.tile == n.m_key.m_tile && next.td == n.m_key.m_td) {
00545 end_segment_reason |= ESRB_INFINITE_LOOP;
00546 break;
00547 }
00548
00549 if (segment_cost > s_max_segment_cost) {
00550
00551
00552 if (IsTileType(tf->m_new_tile, MP_RAILWAY)) {
00553 end_segment_reason |= ESRB_SEGMENT_TOO_LONG;
00554 break;
00555 }
00556 }
00557
00558
00559 if (end_segment_reason != ESRB_NONE) {
00560 break;
00561 }
00562
00563
00564 prev = cur;
00565 cur = next;
00566
00567 }
00568
00569 bool target_seen = false;
00570 if ((end_segment_reason & ESRB_POSSIBLE_TARGET) != ESRB_NONE) {
00571
00572 if (Yapf().PfDetectDestination(cur.tile, cur.td)) {
00573
00574 target_seen = true;
00575 }
00576 }
00577
00578
00579 if (!is_cached_segment) {
00580
00581 segment.m_cost = segment_cost;
00582 segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK;
00583
00584 n.SetLastTileTrackdir(cur.tile, cur.td);
00585 }
00586
00587
00588 if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) {
00589
00590 return false;
00591 }
00592
00593
00594 if (target_seen) {
00595 n.flags_u.flags_s.m_targed_seen = true;
00596
00597 if (n.flags_u.flags_s.m_last_signal_was_red) {
00598 if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
00599
00600 extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
00601 } else if (!IsPbsSignal(n.m_last_red_signal_type)) {
00602
00603 extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
00604 }
00605 }
00606
00607
00608 if ((end_segment_reason & ESRB_STATION) != ESRB_NONE) {
00609 const BaseStation *st = BaseStation::GetByTile(n.GetLastTile());
00610 assert(st != NULL);
00611 uint platform_length = st->GetPlatformLength(n.GetLastTile(), ReverseDiagDir(TrackdirToExitdir(n.GetLastTrackdir())));
00612
00613 extra_cost -= Yapf().PfGetSettings().rail_station_penalty * platform_length;
00614
00615 extra_cost += PlatformLengthPenalty(platform_length);
00616 }
00617 }
00618
00619
00620 n.m_cost = parent_cost + segment_entry_cost + segment_cost + extra_cost;
00621
00622 return true;
00623 }
00624
00625 FORCEINLINE bool CanUseGlobalCache(Node& n) const
00626 {
00627 return !m_disable_cache
00628 && (n.m_parent != NULL)
00629 && (n.m_parent->m_num_signals_passed >= m_sig_look_ahead_costs.Size());
00630 }
00631
00632 FORCEINLINE void ConnectNodeToCachedData(Node& n, CachedData& ci)
00633 {
00634 n.m_segment = &ci;
00635 if (n.m_segment->m_cost < 0) {
00636 n.m_segment->m_last_tile = n.m_key.m_tile;
00637 n.m_segment->m_last_td = n.m_key.m_td;
00638 }
00639 }
00640
00641 void DisableCache(bool disable)
00642 {
00643 m_disable_cache = disable;
00644 }
00645 };
00646
00647 #endif