# yapf_costrail.hpp

Go to the documentation of this file.

    00001 /* $Id: yapf_costrail.hpp 14428 2008-10-01 15:48:44Z michi_cc $ */
    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
    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       // 90-deg curve penalty
    00100       cost += Yapf().PfGetSettings().rail_curve90_penalty;
    00101     } else if (td2 != NextTrackdir(td1)) {
    00102       // 45-deg curve penalty
    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     // set base cost
    00123     if (IsDiagonalTrackdir(trackdir)) {
    00124       cost += YAPF_TILE_LENGTH;
    00125       switch (GetTileType(tile)) {
    00126         case MP_ROAD:
    00127           /* Increase the cost for level crossings */
    00128           if (IsLevelCrossing(tile))
    00129             cost += Yapf().PfGetSettings().rail_crossing_penalty;
    00130           break;
    00131
    00132         default:
    00133           break;
    00134       }
    00135     } else {
    00136       // non-diagonal trackdir
    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     // if there is one-way signal in the opposite direction, then it is not our way
    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         // one-way signal in opposite direction
    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           // cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
    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             // green signal
    00185             n.flags_u.flags_s.m_last_signal_was_red = false;
    00186             // negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal
    00187             if (look_ahead_cost < 0) {
    00188               // add its negation to the cost
    00189               cost -= look_ahead_cost;
    00190             }
    00191           } else {
    00192             SignalType sig_type = GetSignalType(tile, TrackdirToTrack(trackdir));
    00193             // we have a red signal in our direction
    00194             // was it first signal which is two-way?
    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               // yes, the first signal is two-way red signal => DEAD END
    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             // look-ahead signal penalty
    00205             if (!IsPbsSignal(sig_type) && look_ahead_cost > 0) {
    00206               // add the look ahead penalty only if it is positive
    00207               cost += look_ahead_cost;
    00208             }
    00209
    00210             // special signal penalties
    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; // first signal is red pre-signal-exit
    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       // apply penalty for longer platform than needed
    00245       cost += Yapf().PfGetSettings().rail_longer_platform_penalty;
    00246     } else if (needed_platform_length > platform_length) {
    00247       // apply penalty for shorter platform than needed
    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     /* Does the node have some parent node? */
    00270     bool has_parent = (n.m_parent != NULL);
    00271
    00272     /* Do we already have a cached segment? */
    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     /* Each node cost contains 2 or 3 main components:
    00279      *  1. Transition cost - cost of the move from previous node (tile):
    00280      *    - curve cost (or zero for straight move)
    00281      *  2. Tile cost:
    00282      *    - base tile cost
    00283      *      - YAPF_TILE_LENGTH for diagonal tiles
    00284      *      - YAPF_TILE_CORNER_LENGTH for non-diagonal tiles
    00285      *    - tile penalties
    00286      *      - tile slope penalty (upward slopes)
    00287      *      - red signal penalty
    00288      *      - level crossing penalty
    00289      *      - speed-limit penalty (bridges)
    00290      *      - station platform penalty
    00291      *      - penalty for reversing in the depot
    00292      *      - etc.
    00293      *  3. Extra cost (applies to the last node only)
    00294      *    - last red signal penalty
    00295      *    - penalty for too long or too short platform on the destination station
    00296      */
    00297     int transition_cost = 0;
    00298     int extra_cost = 0;
    00299
    00300     /* Segment: one or more tiles connected by contiguous tracks of the same type.
    00301      * Each segment cost includes 'Tile cost' for all its tiles (including the first
    00302      * and last), and the 'Transition cost' between its tiles. The first transition
    00303      * cost of segment entry (move from the 'parent' node) is not included!
    00304      */
    00305     int segment_entry_cost = 0;
    00306     int segment_cost = 0;
    00307
    00308     const Vehicle *v = Yapf().GetVehicle();
    00309
    00310     // start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
    00311     TILE cur(n.m_key.m_tile, n.m_key.m_td);
    00312
    00313     // the previous tile will be needed for transition cost calculations
    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       /* We will jump to the middle of the cost calculator assuming that segment cache is not used. */
    00322       assert(!is_cached_segment);
    00323       /* Skip the first transition cost calculation. */
    00324       goto no_entry_cost;
    00325     }
    00326
    00327     for (;;) {
    00328       /* Transition cost (cost of the move from previous tile) */
    00329       transition_cost = Yapf().CurveCost(prev.td, cur.td);
    00330       transition_cost += Yapf().SwitchCost(prev.tile, cur.tile, TrackdirToExitdir(prev.td));
    00331
    00332       /* First transition cost counts against segment entry cost, other transitions
    00333        * inside segment will come to segment cost (and will be cached) */
    00334       if (segment_cost == 0) {
    00335         /* We just entered the loop. First transition cost goes to segment entry cost)*/
    00336         segment_entry_cost = transition_cost;
    00337         transition_cost = 0;
    00338
    00339         /* It is the right time now to look if we can reuse the cached segment cost. */
    00340         if (is_cached_segment) {
    00341           /* Yes, we already know the segment cost. */
    00342           segment_cost = segment.m_cost;
    00343           /* We know also the reason why the segment ends. */
    00344           end_segment_reason = segment.m_end_segment_reason;
    00345           /* We will need also some information about the last signal (if it was red). */
    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           /* No further calculation needed. */
    00356           cur = TILE(n.GetLastTile(), n.GetLastTrackdir());
    00357           break;
    00358         }
    00359       } else {
    00360         /* Other than first transition cost count as the regular segment cost. */
    00361         segment_cost += transition_cost;
    00362       }
    00363
    00364 no_entry_cost: // jump here at the beginning if the node has no parent (it is the first node)
    00365
    00366       /* All other tile costs will be calculated here. */
    00367       segment_cost += Yapf().OneTileCost(cur.tile, cur.td);
    00368
    00369       /* If we skipped some tunnel/bridge/station tiles, add their base cost */
    00370       segment_cost += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
    00371
    00372       /* Slope cost. */
    00373       segment_cost += Yapf().SlopeCost(cur.tile, cur.td);
    00374
    00375       /* Reserved tiles. */
    00376       segment_cost += Yapf().ReservationCost(n, cur.tile, cur.td, tf->m_tiles_skipped);
    00377
    00378       /* Signal cost (routine can modify segment data). */
    00379       segment_cost += Yapf().SignalCost(n, cur.tile, cur.td);
    00380       end_segment_reason = segment.m_end_segment_reason;
    00381
    00382       /* Tests for 'potential target' reasons to close the segment. */
    00383       if (cur.tile == prev.tile) {
    00384         /* Penalty for reversing in a depot. */
    00385         assert(IsRailDepot(cur.tile));
    00386         segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty;
    00387         /* We will end in this pass (depot is possible target) */
    00388         end_segment_reason |= ESRB_DEPOT;
    00389
    00390       } else if (tf->m_is_station) {
    00391         /* Station penalties. */
    00392         uint platform_length = tf->m_tiles_skipped + 1;
    00393         /* We don't know yet if the station is our target or not. Act like
    00394          * if it is pass-through station (not our destination). */
    00395         segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
    00396         /* We will end in this pass (station is possible target) */
    00397         end_segment_reason |= ESRB_STATION;
    00398
    00399       } else if (cur.tile_type == MP_RAILWAY && IsRailWaypoint(cur.tile)) {
    00400         /* Waypoint is also a good reason to finish. */
    00401         end_segment_reason |= ESRB_WAYPOINT;
    00402       } else if (TrackFollower::DoTrackMasking() && cur.tile_type == MP_RAILWAY) {
    00403         /* Searching for a safe tile? */
    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       /* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise
    00410        * it would cause desync in MP. */
    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       /* Finish if we already exceeded the maximum path cost (i.e. when
    00422        * searching for the nearest depot). */
    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       /* Move to the next tile/trackdir. */
    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         /* Can't move to the next tile (EOL?). */
    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       /* Check if the next tile is not a choice. */
    00447       if (KillFirstBit(tf_local.m_new_td_bits) != TRACKDIR_BIT_NONE) {
    00448         /* More than one segment will follow. Close this one. */
    00449         end_segment_reason |= ESRB_CHOICE_FOLLOWS;
    00450         break;
    00451       }
    00452
    00453       /* Gather the next tile/trackdir/tile_type/rail_type. */
    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         /* Possible safe tile. */
    00458         end_segment_reason |= ESRB_SAFE_TILE;
    00459       }
    00460
    00461       /* Check the next tile for the rail type. */
    00462       if (next.rail_type != cur.rail_type) {
    00463         /* Segment must consist from the same rail_type tiles. */
    00464         end_segment_reason |= ESRB_RAIL_TYPE;
    00465         break;
    00466       }
    00467
    00468       /* Avoid infinite looping. */
    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         /* Potentially in the infinite loop (or only very long segment?). We should
    00476          * not force it to finish prematurely unless we are on a regular tile. */
    00477         if (IsTileType(tf->m_new_tile, MP_RAILWAY)) {
    00478           end_segment_reason |= ESRB_SEGMENT_TOO_LONG;
    00479           break;
    00480         }
    00481       }
    00482
    00483       /* Any other reason bit set? */
    00484       if (end_segment_reason != ESRB_NONE) {
    00485         break;
    00486       }
    00487
    00488       /* For the next loop set new prev and cur tile info. */
    00489       prev = cur;
    00490       cur = next;
    00491
    00492     } // for (;;)
    00493
    00494     bool target_seen = false;
    00495     if ((end_segment_reason & ESRB_POSSIBLE_TARGET) != ESRB_NONE) {
    00496       /* Depot, station or waypoint. */
    00497       if (Yapf().PfDetectDestination(cur.tile, cur.td)) {
    00498         /* Destination found. */
    00499         target_seen = true;
    00500       }
    00501     }
    00502
    00503     /* Update the segment if needed. */
    00504     if (!is_cached_segment) {
    00505       /* Write back the segment information so it can be reused the next time. */
    00506       segment.m_cost = segment_cost;
    00507       segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK;
    00508       /* Save end of segment back to the node. */
    00509       n.SetLastTileTrackdir(cur.tile, cur.td);
    00510     }
    00511
    00512     /* Do we have an excuse why not to continue pathfinding in this direction? */
    00513     if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) {
    00514       /* Reason to not continue. Stop this PF branch. */
    00515       return false;
    00516     }
    00517
    00518     /* Special costs for the case we have reached our target. */
    00519     if (target_seen) {
    00520       n.flags_u.flags_s.m_targed_seen = true;
    00521       /* Last-red and last-red-exit penalties. */
    00522       if (n.flags_u.flags_s.m_last_signal_was_red) {
    00523         if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
    00524           // last signal was red pre-signal-exit
    00525           extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
    00526         } else {
    00527           // last signal was red, but not exit
    00528           extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
    00529         }
    00530       }
    00531
    00532       /* Station platform-length penalty. */
    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         /* Reduce the extra cost caused by passing-station penalty (each station receives it in the segment cost). */
    00538         extra_cost -= Yapf().PfGetSettings().rail_station_penalty * platform_length;
    00539         /* Add penalty for the inappropriate platform length. */
    00540         extra_cost += PlatformLengthPenalty(platform_length);
    00541       }
    00542     }
    00543
    00544     // total node cost
    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 /* YAPF_COSTRAIL_HPP */