# yapf_costrail.hpp

Go to the documentation of this file.

    00001 /* $Id: yapf_costrail.hpp 11383 2007-11-04 23:06:39Z truelight $ */
    00002
    00005 #ifndef  YAPF_COSTRAIL_HPP
    00006 #define  YAPF_COSTRAIL_HPP
    00007
    00008
    00009 template <class Types>
    00010 class CYapfCostRailT
    00011   : public CYapfCostBase
    00012   , public CostRailSettings
    00013 {
    00014 public:
    00015   typedef typename Types::Tpf Tpf;
    00016   typedef typename Types::TrackFollower TrackFollower;
    00017   typedef typename Types::NodeList::Titem Node;
    00018   typedef typename Node::Key Key;
    00019   typedef typename Node::CachedData CachedData;
    00020
    00021 protected:
    00022
    00023   /* Structure used inside PfCalcCost() to keep basic tile information. */
    00024   struct TILE {
    00025     TileIndex   tile;
    00026     Trackdir    td;
    00027     TileType    tile_type;
    00028     RailType    rail_type;
    00029
    00030     TILE()
    00031     {
    00032       tile = INVALID_TILE;
    00033       td = INVALID_TRACKDIR;
    00034       tile_type = MP_VOID;
    00035       rail_type = INVALID_RAILTYPE;
    00036     }
    00037
    00038     TILE(TileIndex tile, Trackdir td)
    00039     {
    00040       this->tile = tile;
    00041       this->td = td;
    00042       this->tile_type = GetTileType(tile);
    00043       this->rail_type = GetTileRailType(tile);
    00044     }
    00045
    00046     TILE(const TILE &src)
    00047     {
    00048       tile = src.tile;
    00049       td = src.td;
    00050       tile_type = src.tile_type;
    00051       rail_type = src.rail_type;
    00052     }
    00053   };
    00054
    00055 protected:
    00056   int           m_max_cost;
    00057   CBlobT<int>   m_sig_look_ahead_costs;
    00058   bool          m_disable_cache;
    00059
    00060 public:
    00061   bool          m_stopped_on_first_two_way_signal;
    00062 protected:
    00063
    00064   static const int s_max_segment_cost = 10000;
    00065
    00066   CYapfCostRailT()
    00067     : m_max_cost(0)
    00068     , m_disable_cache(false)
    00069     , m_stopped_on_first_two_way_signal(false)
    00070   {
    00071     // pre-compute look-ahead penalties into array
    00072     int p0 = Yapf().PfGetSettings().rail_look_ahead_signal_p0;
    00073     int p1 = Yapf().PfGetSettings().rail_look_ahead_signal_p1;
    00074     int p2 = Yapf().PfGetSettings().rail_look_ahead_signal_p2;
    00075     int *pen = m_sig_look_ahead_costs.GrowSizeNC(Yapf().PfGetSettings().rail_look_ahead_max_signals);
    00076     for (uint i = 0; i < Yapf().PfGetSettings().rail_look_ahead_max_signals; i++)
    00077       pen[i] = p0 + i * (p1 + i * p2);
    00078   }
    00079
    00081   Tpf& Yapf() {return *static_cast<Tpf*>(this);}
    00082
    00083 public:
    00084   FORCEINLINE int SlopeCost(TileIndex tile, Trackdir td)
    00085   {
    00086     CPerfStart perf_cost(Yapf().m_perf_slope_cost);
    00087     if (!stSlopeCost(tile, td)) return 0;
    00088     return Yapf().PfGetSettings().rail_slope_penalty;
    00089   }
    00090
    00091   FORCEINLINE int CurveCost(Trackdir td1, Trackdir td2)
    00092   {
    00093     assert(IsValidTrackdir(td1));
    00094     assert(IsValidTrackdir(td2));
    00095     int cost = 0;
    00096     if (TrackFollower::Allow90degTurns()
    00097         && ((TrackdirToTrackdirBits(td2) & (TrackdirBits)TrackdirCrossesTrackdirs(td1)) != 0)) {
    00098       // 90-deg curve penalty
    00099       cost += Yapf().PfGetSettings().rail_curve90_penalty;
    00100     } else if (td2 != NextTrackdir(td1)) {
    00101       // 45-deg curve penalty
    00102       cost += Yapf().PfGetSettings().rail_curve45_penalty;
    00103     }
    00104     return cost;
    00105   }
    00106
    00108   FORCEINLINE int OneTileCost(TileIndex& tile, Trackdir trackdir)
    00109   {
    00110     int cost = 0;
    00111     // set base cost
    00112     if (IsDiagonalTrackdir(trackdir)) {
    00113       cost += YAPF_TILE_LENGTH;
    00114       switch (GetTileType(tile)) {
    00115         case MP_ROAD:
    00116           /* Increase the cost for level crossings */
    00117           if (IsLevelCrossing(tile))
    00118             cost += Yapf().PfGetSettings().rail_crossing_penalty;
    00119           break;
    00120
    00121         default:
    00122           break;
    00123       }
    00124     } else {
    00125       // non-diagonal trackdir
    00126       cost = YAPF_TILE_CORNER_LENGTH;
    00127     }
    00128     return cost;
    00129   }
    00130
    00131   int SignalCost(Node& n, TileIndex tile, Trackdir trackdir)
    00132   {
    00133     int cost = 0;
    00134     // if there is one-way signal in the opposite direction, then it is not our way
    00135     CPerfStart perf_cost(Yapf().m_perf_other_cost);
    00136     if (IsTileType(tile, MP_RAILWAY)) {
    00137       bool has_signal_against = HasSignalOnTrackdir(tile, ReverseTrackdir(trackdir));
    00138       bool has_signal_along = HasSignalOnTrackdir(tile, trackdir);
    00139       if (has_signal_against && !has_signal_along) {
    00140         // one-way signal in opposite direction
    00141         n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
    00142       } else if (has_signal_along) {
    00143         SignalState sig_state = GetSignalStateByTrackdir(tile, trackdir);
    00144         // cache the look-ahead polynomial constant only if we didn't pass more signals than the look-ahead limit is
    00145         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;
    00146         if (sig_state != SIGNAL_STATE_RED) {
    00147           // green signal
    00148           n.flags_u.flags_s.m_last_signal_was_red = false;
    00149           // negative look-ahead red-signal penalties would cause problems later, so use them as positive penalties for green signal
    00150           if (look_ahead_cost < 0) {
    00151             // add its negation to the cost
    00152             cost -= look_ahead_cost;
    00153           }
    00154         } else {
    00155           // we have a red signal in our direction
    00156           // was it first signal which is two-way?
    00157           if (Yapf().TreatFirstRedTwoWaySignalAsEOL() && n.flags_u.flags_s.m_choice_seen && has_signal_against && n.m_num_signals_passed == 0) {
    00158             // yes, the first signal is two-way red signal => DEAD END
    00159             n.m_segment->m_end_segment_reason |= ESRB_DEAD_END;
    00160             Yapf().m_stopped_on_first_two_way_signal = true;
    00161             return -1;
    00162           }
    00163           SignalType sig_type = GetSignalType(tile, TrackdirToTrack(trackdir));
    00164           n.m_last_red_signal_type = sig_type;
    00165           n.flags_u.flags_s.m_last_signal_was_red = true;
    00166
    00167           // look-ahead signal penalty
    00168           if (look_ahead_cost > 0) {
    00169             // add the look ahead penalty only if it is positive
    00170             cost += look_ahead_cost;
    00171           }
    00172
    00173           // special signal penalties
    00174           if (n.m_num_signals_passed == 0) {
    00175             switch (sig_type) {
    00176               case SIGTYPE_COMBO:
    00177               case SIGTYPE_EXIT:   cost += Yapf().PfGetSettings().rail_firstred_exit_penalty; break; // first signal is red pre-signal-exit
    00178               case SIGTYPE_NORMAL:
    00179               case SIGTYPE_ENTRY:  cost += Yapf().PfGetSettings().rail_firstred_penalty; break;
    00180             };
    00181           }
    00182         }
    00183         n.m_num_signals_passed++;
    00184         n.m_segment->m_last_signal_tile = tile;
    00185         n.m_segment->m_last_signal_td = trackdir;
    00186       }
    00187     }
    00188     return cost;
    00189   }
    00190
    00191   FORCEINLINE int PlatformLengthPenalty(int platform_length)
    00192   {
    00193     int cost = 0;
    00194     const Vehicle* v = Yapf().GetVehicle();
    00195     assert(v != NULL);
    00196     assert(v->type == VEH_TRAIN);
    00197     assert(v->u.rail.cached_total_length != 0);
    00198     int needed_platform_length = (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE;
    00199     if (platform_length > needed_platform_length) {
    00200       // apply penalty for longer platform than needed
    00201       cost += Yapf().PfGetSettings().rail_longer_platform_penalty;
    00202     } else if (needed_platform_length > platform_length) {
    00203       // apply penalty for shorter platform than needed
    00204       cost += Yapf().PfGetSettings().rail_shorter_platform_penalty;
    00205     }
    00206     return cost;
    00207   }
    00208
    00209 public:
    00210   FORCEINLINE void SetMaxCost(int max_cost) {m_max_cost = max_cost;}
    00211
    00212
    00213
    00217   FORCEINLINE bool PfCalcCost(Node &n, const TrackFollower *tf)
    00218   {
    00219     assert(!n.flags_u.flags_s.m_targed_seen);
    00220     assert(tf->m_new_tile == n.m_key.m_tile);
    00221     assert((TrackdirToTrackdirBits(n.m_key.m_td) & tf->m_new_td_bits) != TRACKDIR_BIT_NONE);
    00222
    00223     CPerfStart perf_cost(Yapf().m_perf_cost);
    00224
    00225     /* Does the node have some parent node? */
    00226     bool has_parent = (n.m_parent != NULL);
    00227
    00228     /* Do we already have a cached segment? */
    00229     CachedData &segment = *n.m_segment;
    00230     bool is_cached_segment = (segment.m_cost >= 0);
    00231
    00232     int parent_cost = has_parent ? n.m_parent->m_cost : 0;
    00233
    00234     /* Each node cost contains 2 or 3 main components:
    00235      *  1. Transition cost - cost of the move from previous node (tile):
    00236      *    - curve cost (or zero for straight move)
    00237      *  2. Tile cost:
    00238      *    - base tile cost
    00239      *      - YAPF_TILE_LENGTH for diagonal tiles
    00240      *      - YAPF_TILE_CORNER_LENGTH for non-diagonal tiles
    00241      *    - tile penalties
    00242      *      - tile slope penalty (upward slopes)
    00243      *      - red signal penalty
    00244      *      - level crossing penalty
    00245      *      - speed-limit penalty (bridges)
    00246      *      - station platform penalty
    00247      *      - penalty for reversing in the depot
    00248      *      - etc.
    00249      *  3. Extra cost (applies to the last node only)
    00250      *    - last red signal penalty
    00251      *    - penalty for too long or too short platform on the destination station
    00252      */
    00253     int transition_cost = 0;
    00254     int extra_cost = 0;
    00255
    00256     /* Segment: one or more tiles connected by contiguous tracks of the same type.
    00257      * Each segment cost includes 'Tile cost' for all its tiles (including the first
    00258      * and last), and the 'Transition cost' between its tiles. The first transition
    00259      * cost of segment entry (move from the 'parent' node) is not included!
    00260      */
    00261     int segment_entry_cost = 0;
    00262     int segment_cost = 0;
    00263
    00264     const Vehicle* v = Yapf().GetVehicle();
    00265
    00266     // start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment
    00267     TILE cur(n.m_key.m_tile, n.m_key.m_td);
    00268
    00269     // the previous tile will be needed for transition cost calculations
    00270     TILE prev = !has_parent ? TILE() : TILE(n.m_parent->GetLastTile(), n.m_parent->GetLastTrackdir());
    00271
    00272     EndSegmentReasonBits end_segment_reason = ESRB_NONE;
    00273
    00274     TrackFollower tf_local(v, &Yapf().m_perf_ts_cost);
    00275
    00276     if (!has_parent) {
    00277       /* We will jump to the middle of the cost calculator assuming that segment cache is not used. */
    00278       assert(!is_cached_segment);
    00279       /* Skip the first transition cost calculation. */
    00280       goto no_entry_cost;
    00281     }
    00282
    00283     for (;;) {
    00284       /* Transition cost (cost of the move from previous tile) */
    00285       transition_cost = Yapf().CurveCost(prev.td, cur.td);
    00286
    00287       /* First transition cost counts against segment entry cost, other transitions
    00288        * inside segment will come to segment cost (and will be cached) */
    00289       if (segment_cost == 0) {
    00290         /* We just entered the loop. First transition cost goes to segment entry cost)*/
    00291         segment_entry_cost = transition_cost;
    00292         transition_cost = 0;
    00293
    00294         /* It is the right time now to look if we can reuse the cached segment cost. */
    00295         if (is_cached_segment) {
    00296           /* Yes, we already know the segment cost. */
    00297           segment_cost = segment.m_cost;
    00298           /* We know also the reason why the segment ends. */
    00299           end_segment_reason = segment.m_end_segment_reason;
    00300           /* We will need also some information about the last signal (if it was red). */
    00301           if (segment.m_last_signal_tile != INVALID_TILE) {
    00302             assert(HasSignalOnTrackdir(segment.m_last_signal_tile, segment.m_last_signal_td));
    00303             SignalState sig_state = GetSignalStateByTrackdir(segment.m_last_signal_tile, segment.m_last_signal_td);
    00304             bool is_red = (sig_state == SIGNAL_STATE_RED);
    00305             n.flags_u.flags_s.m_last_signal_was_red = is_red;
    00306             if (is_red) {
    00307               n.m_last_red_signal_type = GetSignalType(segment.m_last_signal_tile, TrackdirToTrack(segment.m_last_signal_td));
    00308             }
    00309           }
    00310           /* No further calculation needed. */
    00311           cur = TILE(n.GetLastTile(), n.GetLastTrackdir());
    00312           break;
    00313         }
    00314       } else {
    00315         /* Other than first transition cost count as the regular segment cost. */
    00316         segment_cost += transition_cost;
    00317       }
    00318
    00319 no_entry_cost: // jump here at the beginning if the node has no parent (it is the first node)
    00320
    00321       /* All other tile costs will be calculated here. */
    00322       segment_cost += Yapf().OneTileCost(cur.tile, cur.td);
    00323
    00324       /* If we skipped some tunnel/bridge/station tiles, add their base cost */
    00325       segment_cost += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
    00326
    00327       /* Slope cost. */
    00328       segment_cost += Yapf().SlopeCost(cur.tile, cur.td);
    00329
    00330       /* Signal cost (routine can modify segment data). */
    00331       segment_cost += Yapf().SignalCost(n, cur.tile, cur.td);
    00332       end_segment_reason = segment.m_end_segment_reason;
    00333
    00334       /* Tests for 'potential target' reasons to close the segment. */
    00335       if (cur.tile == prev.tile) {
    00336         /* Penalty for reversing in a depot. */
    00337         assert(IsRailDepot(cur.tile));
    00338         segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty;
    00339         /* We will end in this pass (depot is possible target) */
    00340         end_segment_reason |= ESRB_DEPOT;
    00341
    00342       } else if (tf->m_is_station) {
    00343         /* Station penalties. */
    00344         uint platform_length = tf->m_tiles_skipped + 1;
    00345         /* We don't know yet if the station is our target or not. Act like
    00346          * if it is pass-through station (not our destination). */
    00347         segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length;
    00348         /* We will end in this pass (station is possible target) */
    00349         end_segment_reason |= ESRB_STATION;
    00350
    00351       } else if (cur.tile_type == MP_RAILWAY && IsRailWaypoint(cur.tile)) {
    00352         /* Waypoint is also a good reason to finish. */
    00353         end_segment_reason |= ESRB_WAYPOINT;
    00354       }
    00355
    00356       /* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise
    00357        * it would cause desync in MP. */
    00358       if (n.m_num_signals_passed < m_sig_look_ahead_costs.Size())
    00359       {
    00360         int min_speed = 0;
    00361         int max_speed = tf->GetSpeedLimit(&min_speed);
    00362         if (max_speed < v->max_speed)
    00363           extra_cost += YAPF_TILE_LENGTH * (v->max_speed - max_speed) * (4 + tf->m_tiles_skipped) / v->max_speed;
    00364         if (min_speed > v->max_speed)
    00365           extra_cost += YAPF_TILE_LENGTH * (min_speed - v->max_speed);
    00366       }
    00367
    00368       /* Finish if we already exceeded the maximum path cost (i.e. when
    00369        * searching for the nearest depot). */
    00370       if (m_max_cost > 0 && (parent_cost + segment_entry_cost + segment_cost) > m_max_cost) {
    00371         end_segment_reason |= ESRB_PATH_TOO_LONG;
    00372       }
    00373
    00374       /* Move to the next tile/trackdir. */
    00375       tf = &tf_local;
    00376       tf_local.Init(v, &Yapf().m_perf_ts_cost);
    00377
    00378       if (!tf_local.Follow(cur.tile, cur.td)) {
    00379         assert(tf_local.m_err != TrackFollower::EC_NONE);
    00380         /* Can't move to the next tile (EOL?). */
    00381         if (tf_local.m_err == TrackFollower::EC_RAIL_TYPE) {
    00382           end_segment_reason |= ESRB_RAIL_TYPE;
    00383         } else {
    00384           end_segment_reason |= ESRB_DEAD_END;
    00385         }
    00386         break;
    00387       }
    00388
    00389       /* Check if the next tile is not a choice. */
    00390       if (KillFirstBit(tf_local.m_new_td_bits) != TRACKDIR_BIT_NONE) {
    00391         /* More than one segment will follow. Close this one. */
    00392         end_segment_reason |= ESRB_CHOICE_FOLLOWS;
    00393         break;
    00394       }
    00395
    00396       /* Gather the next tile/trackdir/tile_type/rail_type. */
    00397       TILE next(tf_local.m_new_tile, (Trackdir)FindFirstBit2x64(tf_local.m_new_td_bits));
    00398
    00399       /* Check the next tile for the rail type. */
    00400       if (next.rail_type != cur.rail_type) {
    00401         /* Segment must consist from the same rail_type tiles. */
    00402         end_segment_reason |= ESRB_RAIL_TYPE;
    00403         break;
    00404       }
    00405
    00406       /* Avoid infinite looping. */
    00407       if (next.tile == n.m_key.m_tile && next.td == n.m_key.m_td) {
    00408         end_segment_reason |= ESRB_INFINITE_LOOP;
    00409         break;
    00410       }
    00411
    00412       if (segment_cost > s_max_segment_cost) {
    00413         /* Potentially in the infinite loop (or only very long segment?). We should
    00414          * not force it to finish prematurely unless we are on a regular tile. */
    00415         if (IsTileType(tf->m_new_tile, MP_RAILWAY)) {
    00416           end_segment_reason |= ESRB_SEGMENT_TOO_LONG;
    00417           break;
    00418         }
    00419       }
    00420
    00421       /* Any other reason bit set? */
    00422       if (end_segment_reason != ESRB_NONE) {
    00423         break;
    00424       }
    00425
    00426       /* For the next loop set new prev and cur tile info. */
    00427       prev = cur;
    00428       cur = next;
    00429
    00430     } // for (;;)
    00431
    00432     bool target_seen = false;
    00433     if ((end_segment_reason & ESRB_POSSIBLE_TARGET) != ESRB_NONE) {
    00434       /* Depot, station or waypoint. */
    00435       if (Yapf().PfDetectDestination(cur.tile, cur.td)) {
    00436         /* Destination found. */
    00437         target_seen = true;
    00438       }
    00439     }
    00440
    00441     /* Update the segment if needed. */
    00442     if (!is_cached_segment) {
    00443       /* Write back the segment information so it can be reused the next time. */
    00444       segment.m_cost = segment_cost;
    00445       segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK;
    00446       /* Save end of segment back to the node. */
    00447       n.SetLastTileTrackdir(cur.tile, cur.td);
    00448     }
    00449
    00450     /* Do we have an excuse why not to continue pathfinding in this direction? */
    00451     if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) {
    00452       /* Reason to not continue. Stop this PF branch. */
    00453       return false;
    00454     }
    00455
    00456     /* Special costs for the case we have reached our target. */
    00457     if (target_seen) {
    00458       n.flags_u.flags_s.m_targed_seen = true;
    00459       /* Last-red and last-red-exit penalties. */
    00460       if (n.flags_u.flags_s.m_last_signal_was_red) {
    00461         if (n.m_last_red_signal_type == SIGTYPE_EXIT) {
    00462           // last signal was red pre-signal-exit
    00463           extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty;
    00464         } else {
    00465           // last signal was red, but not exit
    00466           extra_cost += Yapf().PfGetSettings().rail_lastred_penalty;
    00467         }
    00468       }
    00469
    00470       /* Station platform-length penalty. */
    00471       if ((end_segment_reason & ESRB_STATION) != ESRB_NONE) {
    00472         Station *st = GetStationByTile(n.GetLastTile());
    00473         assert(st != NULL);
    00474         uint platform_length = st->GetPlatformLength(n.GetLastTile(), ReverseDiagDir(TrackdirToExitdir(n.GetLastTrackdir())));
    00475         /* Reduce the extra cost caused by passing-station penalty (each station receives it in the segment cost). */
    00476         extra_cost -= Yapf().PfGetSettings().rail_station_penalty * platform_length;
    00477         /* Add penalty for the inappropriate platform length. */
    00478         extra_cost += PlatformLengthPenalty(platform_length);
    00479       }
    00480     }
    00481
    00482     // total node cost
    00483     n.m_cost = parent_cost + segment_entry_cost + segment_cost + extra_cost;
    00484
    00485     return true;
    00486   }
    00487
    00488   FORCEINLINE bool CanUseGlobalCache(Node& n) const
    00489   {
    00490     return !m_disable_cache
    00491       && (n.m_parent != NULL)
    00492       && (n.m_parent->m_num_signals_passed >= m_sig_look_ahead_costs.Size());
    00493   }
    00494
    00495   FORCEINLINE void ConnectNodeToCachedData(Node& n, CachedData& ci)
    00496   {
    00497     n.m_segment = &ci;
    00498     if (n.m_segment->m_cost < 0) {
    00499       n.m_segment->m_last_tile = n.m_key.m_tile;
    00500       n.m_segment->m_last_td = n.m_key.m_td;
    00501     }
    00502   }
    00503
    00504   void DisableCache(bool disable)
    00505   {
    00506     m_disable_cache = disable;
    00507   }
    00508 };
    00509
    00510
    00511
    00512 #endif /* YAPF_COSTRAIL_HPP */