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