# yapf_costrail.hpp

Go to the documentation of this file.

    00001 /* $Id: yapf_costrail.hpp 15718 2009-03-15 00:32:18Z rubidium $ */
    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   /* Structure used inside PfCalcCost() to keep basic tile information. */
    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     /* pre-compute look-ahead penalties into array */
    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       /* 90-deg curve penalty */
    00104       cost += Yapf().PfGetSettings().rail_curve90_penalty;
    00105     } else if (td2 != NextTrackdir(td1)) {
    00106       /* 45-deg curve penalty */
    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     /* set base cost */
    00127     if (IsDiagonalTrackdir(trackdir)) {
    00128       cost += YAPF_TILE_LENGTH;
    00129       switch (GetTileType(tile)) {
    00130         case MP_ROAD:
    00131           /* Increase the cost for level crossings */
    00132           if (IsLevelCrossing(tile)) {
    00133             cost += Yapf().PfGetSettings().rail_crossing_penalty;
    00134           }
    00135           break;
    00136
    00137         default:
    00138           break;
    00139       }
    00140     } else {
    00141       /* non-diagonal trackdir */
    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     /* if there is one-way signal in the opposite direction, then it is not our way */
    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         /* one-way signal in opposite direction */
    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           /* cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is */
    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             /* green signal */
    00190             n.flags_u.flags_s.m_last_signal_was_red = false;
    00191             /* negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal */
    00192             if (look_ahead_cost < 0) {
    00193               /* add its negation to the cost */
    00194               cost -= look_ahead_cost;
    00195             }
    00196           } else {
    00197             SignalType sig_type = GetSignalType(tile, TrackdirToTrack(trackdir));
    00198             /* we have a red signal in our direction
    00199              * was it first signal which is two-way? */
    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               /* yes, the first signal is two-way red signal => DEAD END */
    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             /* look-ahead signal penalty */
    00210             if (!IsPbsSignal(sig_type) && look_ahead_cost > 0) {
    00211               /* add the look ahead penalty only if it is positive */
    00212               cost += look_ahead_cost;
    00213             }
    00214
    00215             /* special signal penalties */
    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; // first signal is red pre-signal-exit
    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       /* apply penalty for longer platform than needed */
    00250       cost += Yapf().PfGetSettings().rail_longer_platform_penalty;
    00251     } else if (needed_platform_length > platform_length) {
    00252       /* apply penalty for shorter platform than needed */
    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     /* Does the node have some parent node? */
    00276     bool has_parent = (n.m_parent != NULL);
    00277
    00278     /* Do we already have a cached segment? */
    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     /* Each node cost contains 2 or 3 main components:
    00285      *  1. Transition cost - cost of the move from previous node (tile):
    00286      *    - curve cost (or zero for straight move)
    00287      *  2. Tile cost:
    00288      *    - base tile cost
    00289      *      - YAPF_TILE_LENGTH for diagonal tiles
    00290      *      - YAPF_TILE_CORNER_LENGTH for non-diagonal tiles
    00291      *    - tile penalties
    00292      *      - tile slope penalty (upward slopes)
    00293      *      - red signal penalty
    00294      *      - level crossing penalty
    00295      *      - speed-limit penalty (bridges)
    00296      *      - station platform penalty
    00297      *      - penalty for reversing in the depot
    00298      *      - etc.
    00299      *  3. Extra cost (applies to the last node only)
    00300      *    - last red signal penalty
    00301      *    - penalty for too long or too short platform on the destination station
    00302      */
    00303     int transition_cost = 0;
    00304     int extra_cost = 0;
    00305
    00306     /* Segment: one or more tiles connected by contiguous tracks of the same type.
    00307      * Each segment cost includes 'Tile cost' for all its tiles (including the first
    00308      * and last), and the 'Transition cost' between its tiles. The first transition
    00309      * cost of segment entry (move from the 'parent' node) is not included!
    00310      */
    00311     int segment_entry_cost = 0;
    00312     int segment_cost = 0;
    00313
    00314     const Vehicle *v = Yapf().GetVehicle();
    00315
    00316     /* start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment */
    00317     TILE cur(n.m_key.m_tile, n.m_key.m_td);
    00318
    00319     /* the previous tile will be needed for transition cost calculations */
    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       /* We will jump to the middle of the cost calculator assuming that segment cache is not used. */
    00328       assert(!is_cached_segment);
    00329       /* Skip the first transition cost calculation. */
    00330       goto no_entry_cost;
    00331     }
    00332
    00333     for (;;) {
    00334       /* Transition cost (cost of the move from previous tile) */
    00335       transition_cost = Yapf().CurveCost(prev.td, cur.td);
    00336       transition_cost += Yapf().SwitchCost(prev.tile, cur.tile, TrackdirToExitdir(prev.td));
    00337
    00338       /* First transition cost counts against segment entry cost, other transitions
    00339        * inside segment will come to segment cost (and will be cached) */
    00340       if (segment_cost == 0) {
    00341         /* We just entered the loop. First transition cost goes to segment entry cost)*/
    00342         segment_entry_cost = transition_cost;
    00343         transition_cost = 0;
    00344
    00345         /* It is the right time now to look if we can reuse the cached segment cost. */
    00346         if (is_cached_segment) {
    00347           /* Yes, we already know the segment cost. */
    00348           segment_cost = segment.m_cost;
    00349           /* We know also the reason why the segment ends. */
    00350           end_segment_reason = segment.m_end_segment_reason;
    00351           /* We will need also some information about the last signal (if it was red). */
    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           /* No further calculation needed. */
    00362           cur = TILE(n.GetLastTile(), n.GetLastTrackdir());
    00363           break;
    00364         }
    00365       } else {
    00366         /* Other than first transition cost count as the regular segment cost. */
    00367         segment_cost += transition_cost;
    00368       }
    00369
    00370 no_entry_cost: // jump here at the beginning if the node has no parent (it is the first node)
    00371
    00372       /* All other tile costs will be calculated here. */
    00373       segment_cost += Yapf().OneTileCost(cur.tile, cur.td);
    00374
    00375       /* If we skipped some tunnel/bridge/station tiles, add their base cost */
    00376       segment_cost += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
    00377
    00378       /* Slope cost. */
    00379       segment_cost += Yapf().SlopeCost(cur.tile, cur.td);
    00380
    00381       /* Reserved tiles. */
    00382       segment_cost += Yapf().ReservationCost(n, cur.tile, cur.td, tf->m_tiles_skipped);
    00383
    00384       /* Signal cost (routine can modify segment data). */
    00385       segment_cost += Yapf().SignalCost(n, cur.tile, cur.td);
    00386       end_segment_reason = segment.m_end_segment_reason;
    00387
    00388       /* Tests for 'potential target' reasons to close the segment. */
    00389       if (cur.tile == prev.tile) {
    00390         /* Penalty for reversing in a depot. */
    00391         assert(IsRailDepot(cur.tile));
    00392         segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty;
    00393         /* We will end in this pass (depot is possible target) */
    00394         end_segment_reason |= ESRB_DEPOT;
    00395
    00396       } else if (tf->m_is_station) {
    00397         /* Station penalties. */
    00398         uint platform_length = tf->m_tiles_skipped + 1;
    00399         /* We don't know yet if the station is our target or not. Act like
    00400          * if it is pass-through station (not our destination). */
    00401         segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
    00402         /* We will end in this pass (station is possible target) */
    00403         end_segment_reason |= ESRB_STATION;
    00404
    00405       } else if (cur.tile_type == MP_RAILWAY && IsRailWaypoint(cur.tile)) {
    00406         /* Waypoint is also a good reason to finish. */
    00407         end_segment_reason |= ESRB_WAYPOINT;
    00408       } else if (TrackFollower::DoTrackMasking() && cur.tile_type == MP_RAILWAY) {
    00409         /* Searching for a safe tile? */
    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       /* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise
    00416        * it would cause desync in MP. */
    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       /* Finish if we already exceeded the maximum path cost (i.e. when
    00430        * searching for the nearest depot). */
    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       /* Move to the next tile/trackdir. */
    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         /* Can't move to the next tile (EOL?). */
    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       /* Check if the next tile is not a choice. */
    00455       if (KillFirstBit(tf_local.m_new_td_bits) != TRACKDIR_BIT_NONE) {
    00456         /* More than one segment will follow. Close this one. */
    00457         end_segment_reason |= ESRB_CHOICE_FOLLOWS;
    00458         break;
    00459       }
    00460
    00461       /* Gather the next tile/trackdir/tile_type/rail_type. */
    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         /* Possible safe tile. */
    00466         end_segment_reason |= ESRB_SAFE_TILE;
    00467       }
    00468
    00469       /* Check the next tile for the rail type. */
    00470       if (next.rail_type != cur.rail_type) {
    00471         /* Segment must consist from the same rail_type tiles. */
    00472         end_segment_reason |= ESRB_RAIL_TYPE;
    00473         break;
    00474       }
    00475
    00476       /* Avoid infinite looping. */
    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         /* Potentially in the infinite loop (or only very long segment?). We should
    00484          * not force it to finish prematurely unless we are on a regular tile. */
    00485         if (IsTileType(tf->m_new_tile, MP_RAILWAY)) {
    00486           end_segment_reason |= ESRB_SEGMENT_TOO_LONG;
    00487           break;
    00488         }
    00489       }
    00490
    00491       /* Any other reason bit set? */
    00492       if (end_segment_reason != ESRB_NONE) {
    00493         break;
    00494       }
    00495
    00496       /* For the next loop set new prev and cur tile info. */
    00497       prev = cur;
    00498       cur = next;
    00499
    00500     } // for (;;)
    00501
    00502     bool target_seen = false;
    00503     if ((end_segment_reason & ESRB_POSSIBLE_TARGET) != ESRB_NONE) {
    00504       /* Depot, station or waypoint. */
    00505       if (Yapf().PfDetectDestination(cur.tile, cur.td)) {
    00506         /* Destination found. */
    00507         target_seen = true;
    00508       }
    00509     }
    00510
    00511     /* Update the segment if needed. */
    00512     if (!is_cached_segment) {
    00513       /* Write back the segment information so it can be reused the next time. */
    00514       segment.m_cost = segment_cost;
    00515       segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK;
    00516       /* Save end of segment back to the node. */
    00517       n.SetLastTileTrackdir(cur.tile, cur.td);
    00518     }
    00519
    00520     /* Do we have an excuse why not to continue pathfinding in this direction? */
    00521     if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) {
    00522       /* Reason to not continue. Stop this PF branch. */
    00523       return false;
    00524     }
    00525
    00526     /* Special costs for the case we have reached our target. */
    00527     if (target_seen) {
    00528       n.flags_u.flags_s.m_targed_seen = true;
    00529       /* Last-red and last-red-exit penalties. */
    00530       if (n.flags_u.flags_s.m_last_signal_was_red) {
    00531         if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
    00532           /* last signal was red pre-signal-exit */
    00533           extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
    00534         } else {
    00535           /* last signal was red, but not exit */
    00536           extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
    00537         }
    00538       }
    00539
    00540       /* Station platform-length penalty. */
    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         /* Reduce the extra cost caused by passing-station penalty (each station receives it in the segment cost). */
    00546         extra_cost -= Yapf().PfGetSettings().rail_station_penalty * platform_length;
    00547         /* Add penalty for the inappropriate platform length. */
    00548         extra_cost += PlatformLengthPenalty(platform_length);
    00549       }
    00550     }
    00551
    00552     /* total node cost */
    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 /* YAPF_COSTRAIL_HPP */