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