# yapf_destrail.hpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #ifndef  YAPF_DESTRAIL_HPP
    00013 #define  YAPF_DESTRAIL_HPP
    00014
    00015 class CYapfDestinationRailBase
    00016 {
    00017 protected:
    00018   RailTypes m_compatible_railtypes;
    00019
    00020 public:
    00021   void SetDestination(const Train *v, bool override_rail_type = false)
    00022   {
    00023     m_compatible_railtypes = v->compatible_railtypes;
    00024     if (override_rail_type) m_compatible_railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
    00025   }
    00026
    00027   bool IsCompatibleRailType(RailType rt)
    00028   {
    00029     return HasBit(m_compatible_railtypes, rt);
    00030   }
    00031
    00032   RailTypes GetCompatibleRailTypes() const
    00033   {
    00034     return m_compatible_railtypes;
    00035   }
    00036 };
    00037
    00038 template <class Types>
    00039 class CYapfDestinationAnyDepotRailT
    00040   : public CYapfDestinationRailBase
    00041 {
    00042 public:
    00043   typedef typename Types::Tpf Tpf;
    00044   typedef typename Types::NodeList::Titem Node;
    00045   typedef typename Node::Key Key;
    00046
    00048   Tpf& Yapf()
    00049   {
    00050     return *static_cast<Tpf*>(this);
    00051   }
    00052
    00054   FORCEINLINE bool PfDetectDestination(Node& n)
    00055   {
    00056     return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
    00057   }
    00058
    00060   FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td)
    00061   {
    00062     bool bDest = IsRailDepotTile(tile);
    00063     return bDest;
    00064   }
    00065
    00068   FORCEINLINE bool PfCalcEstimate(Node& n)
    00069   {
    00070     n.m_estimate = n.m_cost;
    00071     return true;
    00072   }
    00073 };
    00074
    00075 template <class Types>
    00076 class CYapfDestinationAnySafeTileRailT
    00077   : public CYapfDestinationRailBase
    00078 {
    00079 public:
    00080   typedef typename Types::Tpf Tpf;
    00081   typedef typename Types::NodeList::Titem Node;
    00082   typedef typename Node::Key Key;
    00083   typedef typename Types::TrackFollower TrackFollower;
    00084
    00086   Tpf& Yapf()
    00087   {
    00088     return *static_cast<Tpf*>(this);
    00089   }
    00090
    00092   FORCEINLINE bool PfDetectDestination(Node& n)
    00093   {
    00094     return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
    00095   }
    00096
    00098   FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td)
    00099   {
    00100     return
    00101       IsSafeWaitingPosition(Yapf().GetVehicle(), tile, td, true, !TrackFollower::Allow90degTurns()) &&
    00102       IsWaitingPositionFree(Yapf().GetVehicle(), tile, td, !TrackFollower::Allow90degTurns());
    00103   }
    00104
    00107   FORCEINLINE bool PfCalcEstimate(Node& n)
    00108   {
    00109     n.m_estimate = n.m_cost;
    00110     return true;
    00111   }
    00112 };
    00113
    00114 template <class Types>
    00115 class CYapfDestinationTileOrStationRailT
    00116   : public CYapfDestinationRailBase
    00117 {
    00118 public:
    00119   typedef typename Types::Tpf Tpf;
    00120   typedef typename Types::NodeList::Titem Node;
    00121   typedef typename Node::Key Key;
    00122
    00123 protected:
    00124   TileIndex    m_destTile;
    00125   TrackdirBits m_destTrackdirs;
    00126   StationID    m_dest_station_id;
    00127
    00129   Tpf& Yapf()
    00130   {
    00131     return *static_cast<Tpf*>(this);
    00132   }
    00133
    00134 public:
    00135   void SetDestination(const Train *v)
    00136   {
    00137     switch (v->current_order.GetType()) {
    00138       case OT_GOTO_STATION:
    00139       case OT_GOTO_WAYPOINT:
    00140         m_destTile = CalcClosestStationTile(v->current_order.GetDestination(), v->tile, v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT);
    00141         m_dest_station_id = v->current_order.GetDestination();
    00142         m_destTrackdirs = INVALID_TRACKDIR_BIT;
    00143         break;
    00144
    00145       default:
    00146         m_destTile = v->dest_tile;
    00147         m_dest_station_id = INVALID_STATION;
    00148         m_destTrackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_RAIL, 0));
    00149         break;
    00150     }
    00151     CYapfDestinationRailBase::SetDestination(v);
    00152   }
    00153
    00155   FORCEINLINE bool PfDetectDestination(Node& n)
    00156   {
    00157     return PfDetectDestination(n.GetLastTile(), n.GetLastTrackdir());
    00158   }
    00159
    00161   FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td)
    00162   {
    00163     bool bDest;
    00164     if (m_dest_station_id != INVALID_STATION) {
    00165       bDest = HasStationTileRail(tile)
    00166         && (GetStationIndex(tile) == m_dest_station_id)
    00167         && (GetRailStationTrack(tile) == TrackdirToTrack(td));
    00168     } else {
    00169       bDest = (tile == m_destTile)
    00170         && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE);
    00171     }
    00172     return bDest;
    00173   }
    00174
    00177   FORCEINLINE bool PfCalcEstimate(Node& n)
    00178   {
    00179     static const int dg_dir_to_x_offs[] = {-1, 0, 1, 0};
    00180     static const int dg_dir_to_y_offs[] = {0, 1, 0, -1};
    00181     if (PfDetectDestination(n)) {
    00182       n.m_estimate = n.m_cost;
    00183       return true;
    00184     }
    00185
    00186     TileIndex tile = n.GetLastTile();
    00187     DiagDirection exitdir = TrackdirToExitdir(n.GetLastTrackdir());
    00188     int x1 = 2 * TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
    00189     int y1 = 2 * TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
    00190     int x2 = 2 * TileX(m_destTile);
    00191     int y2 = 2 * TileY(m_destTile);
    00192     int dx = abs(x1 - x2);
    00193     int dy = abs(y1 - y2);
    00194     int dmin = min(dx, dy);
    00195     int dxy = abs(dx - dy);
    00196     int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
    00197     n.m_estimate = n.m_cost + d;
    00198     assert(n.m_estimate >= n.m_parent->m_estimate);
    00199     return true;
    00200   }
    00201 };
    00202
    00203 #endif /* YAPF_DESTRAIL_HPP */