Files
@ r23755:c8be91297a35
Branch filter:
Location: cpp/openttd-patchpack/source/src/pathfinder/yapf/yapf_ship.cpp - annotation
r23755:c8be91297a35
10.5 KiB
text/x-c
Update: Translations from eints
hungarian: 3 changes by Brumi
hungarian: 3 changes by Brumi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13831:e72cec269b3c r13825:5de0a5d39b9e r13825:5de0a5d39b9e r17574:8f948fe83988 r13825:5de0a5d39b9e r21383:942c32fb8b0e r21383:942c32fb8b0e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r18782:6453522c2154 r13825:5de0a5d39b9e r22182:16fb91fde98a r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r15610:623a23fb6560 r15610:623a23fb6560 r13825:5de0a5d39b9e r15613:193c12018337 r15613:193c12018337 r22182:16fb91fde98a r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r18782:6453522c2154 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23178:e613ba71d020 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23214:c04c9a31fcbc r20008:f9bad3dc2be1 r13825:5de0a5d39b9e r20008:f9bad3dc2be1 r20008:f9bad3dc2be1 r20008:f9bad3dc2be1 r22940:b114928ab861 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r22848:fca1207f7d32 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r16768:069c18958074 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23607:36c15679007d r23178:e613ba71d020 r23607:36c15679007d r23178:e613ba71d020 r13825:5de0a5d39b9e r23607:36c15679007d r23607:36c15679007d r23423:b49906a7682e r23423:b49906a7682e r23670:17ce345f83e6 r23178:e613ba71d020 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r22182:16fb91fde98a r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23178:e613ba71d020 r23178:e613ba71d020 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r23607:36c15679007d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r23607:36c15679007d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r22182:16fb91fde98a r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r23417:4615b1310164 r15610:623a23fb6560 r15610:623a23fb6560 r13825:5de0a5d39b9e r15613:193c12018337 r15613:193c12018337 r22182:16fb91fde98a r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23417:4615b1310164 r13825:5de0a5d39b9e r17982:320ae85f0acc r13825:5de0a5d39b9e r13825:5de0a5d39b9e r17982:320ae85f0acc r17982:320ae85f0acc r17982:320ae85f0acc r17982:320ae85f0acc r17982:320ae85f0acc r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r15610:623a23fb6560 r15610:623a23fb6560 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13833:38705072ae77 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23398:38ccf1798c05 r13825:5de0a5d39b9e r23398:38ccf1798c05 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r23418:7f588ca21399 r13825:5de0a5d39b9e r23178:e613ba71d020 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23178:e613ba71d020 r23398:38ccf1798c05 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23418:7f588ca21399 r23398:38ccf1798c05 r13825:5de0a5d39b9e r13825:5de0a5d39b9e r23178:e613ba71d020 r13828:41995609971a r13825:5de0a5d39b9e r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r23398:38ccf1798c05 r19550:f624172dec6d r19550:f624172dec6d r23418:7f588ca21399 r23398:38ccf1798c05 r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d r19550:f624172dec6d | /* $Id$ */
/*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file yapf_ship.cpp Implementation of YAPF for ships. */
#include "../../stdafx.h"
#include "../../ship.h"
#include "yapf.hpp"
#include "yapf_node_ship.hpp"
#include "../../safeguards.h"
/** Node Follower module of YAPF for ships */
template <class Types>
class CYapfFollowShipT
{
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
/** to access inherited path finder */
inline Tpf& Yapf()
{
return *static_cast<Tpf *>(this);
}
public:
/**
* Called by YAPF to move from the given node to the next tile. For each
* reachable trackdir on the new tile creates new node, initializes it
* and adds it to the open list by calling Yapf().AddNewNode(n)
*/
inline void PfFollowNode(Node &old_node)
{
TrackFollower F(Yapf().GetVehicle());
if (F.Follow(old_node.m_key.m_tile, old_node.m_key.m_td)) {
Yapf().AddMultipleNodes(&old_node, F);
}
}
/** return debug report character to identify the transportation type */
inline char TransportTypeChar() const
{
return 'w';
}
static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
{
/* handle special case - when next tile is destination tile */
if (tile == v->dest_tile) {
/* convert tracks to trackdirs */
TrackdirBits trackdirs = TrackBitsToTrackdirBits(tracks);
/* limit to trackdirs reachable from enterdir */
trackdirs &= DiagdirReachesTrackdirs(enterdir);
/* use vehicle's current direction if that's possible, otherwise use first usable one. */
Trackdir veh_dir = v->GetVehicleTrackdir();
return (HasTrackdir(trackdirs, veh_dir)) ? veh_dir : (Trackdir)FindFirstBit2x64(trackdirs);
}
/* move back to the old tile/trackdir (where ship is coming from) */
TileIndex src_tile = TileAddByDiagDir(tile, ReverseDiagDir(enterdir));
Trackdir trackdir = v->GetVehicleTrackdir();
assert(IsValidTrackdir(trackdir));
/* convert origin trackdir to TrackdirBits */
TrackdirBits trackdirs = TrackdirToTrackdirBits(trackdir);
/* get available trackdirs on the destination tile */
TrackdirBits dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_WATER, 0));
/* create pathfinder instance */
Tpf pf;
/* set origin and destination nodes */
pf.SetOrigin(src_tile, trackdirs);
pf.SetDestination(v->dest_tile, dest_trackdirs);
/* find best path */
path_found = pf.FindPath(v);
Trackdir next_trackdir = INVALID_TRACKDIR; // this would mean "path not found"
Node *pNode = pf.GetBestNode();
if (pNode != nullptr) {
uint steps = 0;
for (Node *n = pNode; n->m_parent != nullptr; n = n->m_parent) steps++;
/* walk through the path back to the origin */
Node *pPrevNode = nullptr;
while (pNode->m_parent != nullptr) {
steps--;
if (steps > 0 && steps < YAPF_SHIP_PATH_CACHE_LENGTH) {
path_cache.push_front(pNode->GetTrackdir());
}
pPrevNode = pNode;
pNode = pNode->m_parent;
}
/* return trackdir from the best next node (direct child of origin) */
Node &best_next_node = *pPrevNode;
assert(best_next_node.GetTile() == tile);
next_trackdir = best_next_node.GetTrackdir();
/* remove last element for the special case when tile == dest_tile */
if (path_found && !path_cache.empty()) path_cache.pop_back();
}
return next_trackdir;
}
/**
* Check whether a ship should reverse to reach its destination.
* Called when leaving depot.
* @param v Ship
* @param tile Current position
* @param td1 Forward direction
* @param td2 Reverse direction
* @return true if the reverse direction is better
*/
static bool CheckShipReverse(const Ship *v, TileIndex tile, Trackdir td1, Trackdir td2)
{
/* get available trackdirs on the destination tile */
TrackdirBits dest_trackdirs = TrackStatusToTrackdirBits(GetTileTrackStatus(v->dest_tile, TRANSPORT_WATER, 0));
/* create pathfinder instance */
Tpf pf;
/* set origin and destination nodes */
pf.SetOrigin(tile, TrackdirToTrackdirBits(td1) | TrackdirToTrackdirBits(td2));
pf.SetDestination(v->dest_tile, dest_trackdirs);
/* find best path */
if (!pf.FindPath(v)) return false;
Node *pNode = pf.GetBestNode();
if (pNode == nullptr) return false;
/* path was found
* walk through the path back to the origin */
while (pNode->m_parent != nullptr) {
pNode = pNode->m_parent;
}
Trackdir best_trackdir = pNode->GetTrackdir();
assert(best_trackdir == td1 || best_trackdir == td2);
return best_trackdir == td2;
}
};
/** Cost Provider module of YAPF for ships */
template <class Types>
class CYapfCostShipT
{
public:
typedef typename Types::Tpf Tpf; ///< the pathfinder class (derived from THIS class)
typedef typename Types::TrackFollower TrackFollower;
typedef typename Types::NodeList::Titem Node; ///< this will be our node type
typedef typename Node::Key Key; ///< key to hash tables
protected:
/** to access inherited path finder */
Tpf& Yapf()
{
return *static_cast<Tpf *>(this);
}
public:
inline int CurveCost(Trackdir td1, Trackdir td2)
{
assert(IsValidTrackdir(td1));
assert(IsValidTrackdir(td2));
if (HasTrackdir(TrackdirCrossesTrackdirs(td1), td2)) {
/* 90-deg curve penalty */
return Yapf().PfGetSettings().ship_curve90_penalty;
} else if (td2 != NextTrackdir(td1)) {
/* 45-deg curve penalty */
return Yapf().PfGetSettings().ship_curve45_penalty;
}
return 0;
}
/**
* Called by YAPF to calculate the cost from the origin to the given node.
* Calculates only the cost of given node, adds it to the parent node cost
* and stores the result into Node::m_cost member
*/
inline bool PfCalcCost(Node &n, const TrackFollower *tf)
{
/* base tile cost depending on distance */
int c = IsDiagonalTrackdir(n.GetTrackdir()) ? YAPF_TILE_LENGTH : YAPF_TILE_CORNER_LENGTH;
/* additional penalty for curves */
c += CurveCost(n.m_parent->GetTrackdir(), n.GetTrackdir());
/* Skipped tile cost for aqueducts. */
c += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
/* Ocean/canal speed penalty. */
const ShipVehicleInfo *svi = ShipVehInfo(Yapf().GetVehicle()->engine_type);
byte speed_frac = (GetEffectiveWaterClass(n.GetTile()) == WATER_CLASS_SEA) ? svi->ocean_speed_frac : svi->canal_speed_frac;
if (speed_frac > 0) c += YAPF_TILE_LENGTH * (1 + tf->m_tiles_skipped) * speed_frac / (256 - speed_frac);
/* apply it */
n.m_cost = n.m_parent->m_cost + c;
return true;
}
};
/**
* Config struct of YAPF for ships.
* Defines all 6 base YAPF modules as classes providing services for CYapfBaseT.
*/
template <class Tpf_, class Ttrack_follower, class Tnode_list>
struct CYapfShip_TypesT
{
/** Types - shortcut for this struct type */
typedef CYapfShip_TypesT<Tpf_, Ttrack_follower, Tnode_list> Types;
/** Tpf - pathfinder type */
typedef Tpf_ Tpf;
/** track follower helper class */
typedef Ttrack_follower TrackFollower;
/** node list type */
typedef Tnode_list NodeList;
typedef Ship VehicleType;
/** pathfinder components (modules) */
typedef CYapfBaseT<Types> PfBase; // base pathfinder class
typedef CYapfFollowShipT<Types> PfFollow; // node follower
typedef CYapfOriginTileT<Types> PfOrigin; // origin provider
typedef CYapfDestinationTileT<Types> PfDestination; // destination/distance provider
typedef CYapfSegmentCostCacheNoneT<Types> PfCache; // segment cost cache provider
typedef CYapfCostShipT<Types> PfCost; // cost provider
};
/* YAPF type 1 - uses TileIndex/Trackdir as Node key */
struct CYapfShip1 : CYapfT<CYapfShip_TypesT<CYapfShip1, CFollowTrackWater , CShipNodeListTrackDir> > {};
/* YAPF type 2 - uses TileIndex/DiagDirection as Node key */
struct CYapfShip2 : CYapfT<CYapfShip_TypesT<CYapfShip2, CFollowTrackWater , CShipNodeListExitDir > > {};
static inline bool RequireTrackdirKey()
{
/* If the two curve penalties are not equal, then it is not possible to use the
* ExitDir keyed node list, as it there will be key overlap. Using Trackdir keyed
* nodes means potentially more paths are tested, which would be wasteful if it's
* not necessary.
*/
return _settings_game.pf.yapf.ship_curve45_penalty != _settings_game.pf.yapf.ship_curve90_penalty;
}
/** Ship controller helper - path finder invoker */
Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
{
/* default is YAPF type 2 */
typedef Trackdir (*PfnChooseShipTrack)(const Ship*, TileIndex, DiagDirection, TrackBits, bool &path_found, ShipPathCache &path_cache);
PfnChooseShipTrack pfnChooseShipTrack = CYapfShip2::ChooseShipTrack; // default: ExitDir
/* check if non-default YAPF type needed */
if (_settings_game.pf.yapf.disable_node_optimization || RequireTrackdirKey()) {
pfnChooseShipTrack = &CYapfShip1::ChooseShipTrack; // Trackdir
}
Trackdir td_ret = pfnChooseShipTrack(v, tile, enterdir, tracks, path_found, path_cache);
return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK;
}
bool YapfShipCheckReverse(const Ship *v)
{
Trackdir td = v->GetVehicleTrackdir();
Trackdir td_rev = ReverseTrackdir(td);
TileIndex tile = v->tile;
typedef bool (*PfnCheckReverseShip)(const Ship*, TileIndex, Trackdir, Trackdir);
PfnCheckReverseShip pfnCheckReverseShip = CYapfShip2::CheckShipReverse; // default: ExitDir
/* check if non-default YAPF type needed */
if (_settings_game.pf.yapf.disable_node_optimization || RequireTrackdirKey()) {
pfnCheckReverseShip = &CYapfShip1::CheckShipReverse; // Trackdir
}
bool reverse = pfnCheckReverseShip(v, tile, td, td_rev);
return reverse;
}
|