# yapf_ship.cpp

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 #include "../../stdafx.h"
    00013 #include "../../ship.h"
    00014
    00015 #include "yapf.hpp"
    00016 #include "yapf_node_ship.hpp"
    00017
    00019 template <class Types>
    00020 class CYapfFollowShipT
    00021 {
    00022 public:
    00023   typedef typename Types::Tpf Tpf;
    00024   typedef typename Types::TrackFollower TrackFollower;
    00025   typedef typename Types::NodeList::Titem Node;
    00026   typedef typename Node::Key Key;
    00027
    00028 protected:
    00030   inline Tpf& Yapf()
    00031   {
    00032     return *static_cast<Tpf*>(this);
    00033   }
    00034
    00035 public:
    00041   inline void PfFollowNode(Node& old_node)
    00042   {
    00043     TrackFollower F(Yapf().GetVehicle());
    00044     if (F.Follow(old_node.m_key.m_tile, old_node.m_key.m_td)) {
    00045       Yapf().AddMultipleNodes(&old_node, F);
    00046     }
    00047   }
    00048
    00050   inline char TransportTypeChar() const
    00051   {
    00052     return 'w';
    00053   }
    00054
    00055   static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found)
    00056   {
    00057     /* handle special case - when next tile is destination tile */
    00058     if (tile == v->dest_tile) {
    00059       /* convert tracks to trackdirs */
    00060       TrackdirBits trackdirs = (TrackdirBits)(tracks | ((int)tracks << 8));
    00061       /* choose any trackdir reachable from enterdir */
    00062       trackdirs &= DiagdirReachesTrackdirs(enterdir);
    00063       return (Trackdir)FindFirstBit2x64(trackdirs);
    00064     }
    00065
    00066     /* move back to the old tile/trackdir (where ship is coming from) */
    00067     TileIndex src_tile = TILE_ADD(tile, TileOffsByDiagDir(ReverseDiagDir(enterdir)));
    00068     Trackdir trackdir = v->GetVehicleTrackdir();
    00069     assert(IsValidTrackdir(trackdir));
    00070
    00071     /* convert origin trackdir to TrackdirBits */
    00072     TrackdirBits trackdirs = TrackdirToTrackdirBits(trackdir);
    00073     /* get available trackdirs on the destination tile */
    00074     TrackdirBits dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_WATER, 0));
    00075
    00076     /* create pathfinder instance */
    00077     Tpf pf;
    00078     /* set origin and destination nodes */
    00079     pf.SetOrigin(src_tile, trackdirs);
    00080     pf.SetDestination(v->dest_tile, dest_trackdirs);
    00081     /* find best path */
    00082     path_found = pf.FindPath(v);
    00083
    00084     Trackdir next_trackdir = INVALID_TRACKDIR; // this would mean "path not found"
    00085
    00086     Node *pNode = pf.GetBestNode();
    00087     if (pNode != NULL) {
    00088       /* walk through the path back to the origin */
    00089       Node *pPrevNode = NULL;
    00090       while (pNode->m_parent != NULL) {
    00091         pPrevNode = pNode;
    00092         pNode = pNode->m_parent;
    00093       }
    00094       /* return trackdir from the best next node (direct child of origin) */
    00095       Node& best_next_node = *pPrevNode;
    00096       assert(best_next_node.GetTile() == tile);
    00097       next_trackdir = best_next_node.GetTrackdir();
    00098     }
    00099     return next_trackdir;
    00100   }
    00101 };
    00102
    00104 template <class Types>
    00105 class CYapfCostShipT
    00106 {
    00107 public:
    00108   typedef typename Types::Tpf Tpf;
    00109   typedef typename Types::TrackFollower TrackFollower;
    00110   typedef typename Types::NodeList::Titem Node;
    00111   typedef typename Node::Key Key;
    00112
    00113 protected:
    00115   Tpf& Yapf()
    00116   {
    00117     return *static_cast<Tpf*>(this);
    00118   }
    00119
    00120 public:
    00126   inline bool PfCalcCost(Node& n, const TrackFollower *tf)
    00127   {
    00128     /* base tile cost depending on distance */
    00129     int c = IsDiagonalTrackdir(n.GetTrackdir()) ? YAPF_TILE_LENGTH : YAPF_TILE_CORNER_LENGTH;
    00130     /* additional penalty for curves */
    00131     if (n.m_parent != NULL && n.GetTrackdir() != NextTrackdir(n.m_parent->GetTrackdir())) {
    00132       /* new trackdir does not match the next one when going straight */
    00133       c += YAPF_TILE_LENGTH;
    00134     }
    00135
    00136     /* Skipped tile cost for aqueducts. */
    00137     c += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
    00138
    00139     /* Ocean/canal speed penalty. */
    00140     const ShipVehicleInfo *svi = ShipVehInfo(Yapf().GetVehicle()->engine_type);
    00141     byte speed_frac = (GetEffectiveWaterClass(n.GetTile()) == WATER_CLASS_SEA) ? svi->ocean_speed_frac : svi->canal_speed_frac;
    00142     if (speed_frac > 0) c += YAPF_TILE_LENGTH * (1 + tf->m_tiles_skipped) * speed_frac / (256 - speed_frac);
    00143
    00144     /* apply it */
    00145     n.m_cost = n.m_parent->m_cost + c;
    00146     return true;
    00147   }
    00148 };
    00149
    00154 template <class Tpf_, class Ttrack_follower, class Tnode_list>
    00155 struct CYapfShip_TypesT
    00156 {
    00158   typedef CYapfShip_TypesT<Tpf_, Ttrack_follower, Tnode_list>  Types;
    00159
    00161   typedef Tpf_                              Tpf;
    00163   typedef Ttrack_follower                   TrackFollower;
    00165   typedef Tnode_list                        NodeList;
    00166   typedef Ship                              VehicleType;
    00168   typedef CYapfBaseT<Types>                 PfBase;        // base pathfinder class
    00169   typedef CYapfFollowShipT<Types>           PfFollow;      // node follower
    00170   typedef CYapfOriginTileT<Types>           PfOrigin;      // origin provider
    00171   typedef CYapfDestinationTileT<Types>      PfDestination; // destination/distance provider
    00172   typedef CYapfSegmentCostCacheNoneT<Types> PfCache;       // segment cost cache provider
    00173   typedef CYapfCostShipT<Types>             PfCost;        // cost provider
    00174 };
    00175
    00176 /* YAPF type 1 - uses TileIndex/Trackdir as Node key, allows 90-deg turns */
    00177 struct CYapfShip1 : CYapfT<CYapfShip_TypesT<CYapfShip1, CFollowTrackWater    , CShipNodeListTrackDir> > {};
    00178 /* YAPF type 2 - uses TileIndex/DiagDirection as Node key, allows 90-deg turns */
    00179 struct CYapfShip2 : CYapfT<CYapfShip_TypesT<CYapfShip2, CFollowTrackWater    , CShipNodeListExitDir > > {};
    00180 /* YAPF type 3 - uses TileIndex/Trackdir as Node key, forbids 90-deg turns */
    00181 struct CYapfShip3 : CYapfT<CYapfShip_TypesT<CYapfShip3, CFollowTrackWaterNo90, CShipNodeListTrackDir> > {};
    00182
    00184 Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found)
    00185 {
    00186   /* default is YAPF type 2 */
    00187   typedef Trackdir (*PfnChooseShipTrack)(const Ship*, TileIndex, DiagDirection, TrackBits, bool &path_found);
    00188   PfnChooseShipTrack pfnChooseShipTrack = CYapfShip2::ChooseShipTrack; // default: ExitDir, allow 90-deg
    00189
    00190   /* check if non-default YAPF type needed */
    00191   if (_settings_game.pf.forbid_90_deg) {
    00192     pfnChooseShipTrack = &CYapfShip3::ChooseShipTrack; // Trackdir, forbid 90-deg
    00193   } else if (_settings_game.pf.yapf.disable_node_optimization) {
    00194     pfnChooseShipTrack = &CYapfShip1::ChooseShipTrack; // Trackdir, allow 90-deg
    00195   }
    00196
    00197   Trackdir td_ret = pfnChooseShipTrack(v, tile, enterdir, tracks, path_found);
    00198   return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK;
    00199 }