OpenTTD Source  14.0-RC1
yapf_ship.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../../stdafx.h"
11 #include "../../ship.h"
12 #include "../../industry.h"
13 #include "../../vehicle_func.h"
14 
15 #include "yapf.hpp"
16 #include "yapf_node_ship.hpp"
17 #include "yapf_ship_regions.h"
18 #include "../water_regions.h"
19 
20 #include "../../safeguards.h"
21 
22 constexpr int NUMBER_OR_WATER_REGIONS_LOOKAHEAD = 4;
23 constexpr int MAX_SHIP_PF_NODES = (NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1) * WATER_REGION_NUMBER_OF_TILES * 4; // 4 possible exit dirs per tile.
24 
25 constexpr int SHIP_LOST_PATH_LENGTH = 8; // The length of the (aimless) path assigned when a ship is lost.
26 
27 template <class Types>
29 {
30 public:
31  typedef typename Types::Tpf Tpf;
32  typedef typename Types::TrackFollower TrackFollower;
33  typedef typename Types::NodeList::Titem Node;
34  typedef typename Node::Key Key;
35 
36 protected:
37  TileIndex m_destTile;
38  TrackdirBits m_destTrackdirs;
39  StationID m_destStation;
40 
41  bool m_has_intermediate_dest = false;
42  TileIndex m_intermediate_dest_tile;
43  WaterRegionPatchDesc m_intermediate_dest_region_patch;
44 
45 public:
46  void SetDestination(const Ship *v)
47  {
48  if (v->current_order.IsType(OT_GOTO_STATION)) {
49  m_destStation = v->current_order.GetDestination();
50  m_destTile = CalcClosestStationTile(m_destStation, v->tile, STATION_DOCK);
51  m_destTrackdirs = INVALID_TRACKDIR_BIT;
52  } else {
53  m_destStation = INVALID_STATION;
54  m_destTile = v->dest_tile;
56  }
57  }
58 
59  void SetIntermediateDestination(const WaterRegionPatchDesc &water_region_patch)
60  {
61  m_has_intermediate_dest = true;
62  m_intermediate_dest_tile = GetWaterRegionCenterTile(water_region_patch);
63  m_intermediate_dest_region_patch = water_region_patch;
64  }
65 
66 protected:
68  inline Tpf& Yapf()
69  {
70  return *static_cast<Tpf*>(this);
71  }
72 
73 public:
75  inline bool PfDetectDestination(Node &n)
76  {
77  return PfDetectDestinationTile(n.m_segment_last_tile, n.m_segment_last_td);
78  }
79 
80  inline bool PfDetectDestinationTile(TileIndex tile, Trackdir trackdir)
81  {
82  if (m_has_intermediate_dest) {
83  /* GetWaterRegionInfo is much faster than GetWaterRegionPatchInfo so we try that first. */
84  if (GetWaterRegionInfo(tile) != m_intermediate_dest_region_patch) return false;
85  return GetWaterRegionPatchInfo(tile) == m_intermediate_dest_region_patch;
86  }
87 
88  if (m_destStation != INVALID_STATION) return IsDockingTile(tile) && IsShipDestinationTile(tile, m_destStation);
89 
90  return tile == m_destTile && ((m_destTrackdirs & TrackdirToTrackdirBits(trackdir)) != TRACKDIR_BIT_NONE);
91  }
92 
97  inline bool PfCalcEstimate(Node &n)
98  {
99  const TileIndex destination_tile = m_has_intermediate_dest ? m_intermediate_dest_tile : m_destTile;
100 
101  static const int dg_dir_to_x_offs[] = { -1, 0, 1, 0 };
102  static const int dg_dir_to_y_offs[] = { 0, 1, 0, -1 };
103  if (PfDetectDestination(n)) {
104  n.m_estimate = n.m_cost;
105  return true;
106  }
107 
108  TileIndex tile = n.m_segment_last_tile;
109  DiagDirection exitdir = TrackdirToExitdir(n.m_segment_last_td);
110  int x1 = 2 * TileX(tile) + dg_dir_to_x_offs[(int)exitdir];
111  int y1 = 2 * TileY(tile) + dg_dir_to_y_offs[(int)exitdir];
112  int x2 = 2 * TileX(destination_tile);
113  int y2 = 2 * TileY(destination_tile);
114  int dx = abs(x1 - x2);
115  int dy = abs(y1 - y2);
116  int dmin = std::min(dx, dy);
117  int dxy = abs(dx - dy);
118  int d = dmin * YAPF_TILE_CORNER_LENGTH + (dxy - 1) * (YAPF_TILE_LENGTH / 2);
119  n.m_estimate = n.m_cost + d;
120  assert(n.m_estimate >= n.m_parent->m_estimate);
121  return true;
122  }
123 };
124 
126 template <class Types>
128 {
129 public:
130  typedef typename Types::Tpf Tpf;
131  typedef typename Types::TrackFollower TrackFollower;
132  typedef typename Types::NodeList::Titem Node;
133  typedef typename Node::Key Key;
134 
135 protected:
137  inline Tpf &Yapf()
138  {
139  return *static_cast<Tpf*>(this);
140  }
141 
142  std::vector<WaterRegionDesc> m_water_region_corridor;
143 
144 public:
150  inline void PfFollowNode(Node &old_node)
151  {
152  TrackFollower F(Yapf().GetVehicle());
153  if (F.Follow(old_node.m_key.m_tile, old_node.m_key.m_td)) {
154  if (m_water_region_corridor.empty()
155  || std::find(m_water_region_corridor.begin(), m_water_region_corridor.end(),
156  GetWaterRegionInfo(F.m_new_tile)) != m_water_region_corridor.end()) {
157  Yapf().AddMultipleNodes(&old_node, F);
158  }
159  }
160  }
161 
163  inline void RestrictSearch(const std::vector<WaterRegionPatchDesc> &path)
164  {
165  m_water_region_corridor.clear();
166  for (const WaterRegionPatchDesc &path_entry : path) m_water_region_corridor.push_back(path_entry);
167  }
168 
170  inline char TransportTypeChar() const
171  {
172  return 'w';
173  }
174 
176  static Trackdir GetRandomFollowUpTrackdir(const Ship *v, TileIndex tile, Trackdir dir, bool include_90_degree_turns)
177  {
178  TrackFollower follower(v);
179  if (follower.Follow(tile, dir)) {
180  tile = follower.m_new_tile;
181  TrackdirBits dirs = follower.m_new_td_bits;
182  if (!include_90_degree_turns) dirs &= ~TrackdirCrossesTrackdirs(dir);
183  const int strip_amount = _random.Next(CountBits(dirs));
184  for (int s = 0; s < strip_amount; ++s) RemoveFirstTrackdir(&dirs);
185  return FindFirstTrackdir(dirs);
186  }
187  return INVALID_TRACKDIR;
188  }
189 
191  static Trackdir CreateRandomPath(const Ship *v, TileIndex tile, Trackdir dir, ShipPathCache &path_cache, int path_length)
192  {
193  for (int i = 0; i < path_length; ++i) {
194  const Trackdir random_dir = GetRandomFollowUpTrackdir(v, tile, dir, false);
195  if (random_dir == INVALID_TRACKDIR) break;
196  path_cache.push_back(random_dir);
197  }
198 
199  if (path_cache.empty()) return INVALID_TRACKDIR;
200 
201  const Trackdir result = path_cache.front();
202  path_cache.pop_front();
203  return result;
204  }
205 
206  static Trackdir ChooseShipTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
207  {
208  /* Handle special case - when next tile is destination tile. */
209  if (tile == v->dest_tile) {
210  /* Convert tracks to trackdirs */
211  TrackdirBits trackdirs = TrackBitsToTrackdirBits(tracks);
212  /* Limit to trackdirs reachable from enterdir. */
213  trackdirs &= DiagdirReachesTrackdirs(enterdir);
214 
215  /* use vehicle's current direction if that's possible, otherwise use first usable one. */
216  Trackdir veh_dir = v->GetVehicleTrackdir();
217  return (HasTrackdir(trackdirs, veh_dir)) ? veh_dir : (Trackdir)FindFirstBit(trackdirs);
218  }
219 
220  /* Move back to the old tile/trackdir (where ship is coming from). */
221  const TileIndex src_tile = TileAddByDiagDir(tile, ReverseDiagDir(enterdir));
222  const Trackdir trackdir = v->GetVehicleTrackdir();
223  assert(IsValidTrackdir(trackdir));
224 
225  /* Convert origin trackdir to TrackdirBits. */
226  const TrackdirBits trackdirs = TrackdirToTrackdirBits(trackdir);
227 
228  const std::vector<WaterRegionPatchDesc> high_level_path = YapfShipFindWaterRegionPath(v, tile, NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1);
229  if (high_level_path.empty()) {
230  path_found = false;
231  /* Make the ship move around aimlessly. This prevents repeated pathfinder calls and clearly indicates that the ship is lost. */
232  return CreateRandomPath(v, src_tile, trackdir, path_cache, SHIP_LOST_PATH_LENGTH);
233  }
234 
235  /* Try one time without restricting the search area, which generally results in better and more natural looking paths.
236  * However the pathfinder can hit the node limit in certain situations such as long aqueducts or maze-like terrain.
237  * If that happens we run the pathfinder again, but restricted only to the regions provided by the region pathfinder. */
238  for (int attempt = 0; attempt < 2; ++attempt) {
239  Tpf pf(MAX_SHIP_PF_NODES);
240 
241  /* Set origin and destination nodes */
242  pf.SetOrigin(src_tile, trackdirs);
243  pf.SetDestination(v);
244  const bool is_intermediate_destination = static_cast<int>(high_level_path.size()) >= NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1;
245  if (is_intermediate_destination) pf.SetIntermediateDestination(high_level_path.back());
246 
247  /* Restrict the search area to prevent the low level pathfinder from expanding too many nodes. This can happen
248  * when the terrain is very "maze-like" or when the high level path "teleports" via a very long aqueduct. */
249  if (attempt > 0) pf.RestrictSearch(high_level_path);
250 
251  /* Find best path. */
252  path_found = pf.FindPath(v);
253  Node *node = pf.GetBestNode();
254  if (attempt == 0 && !path_found) continue; // Try again with restricted search area.
255  if (!path_found || node == nullptr) GetRandomFollowUpTrackdir(v, src_tile, trackdir, true);
256 
257  /* Return only the path within the current water region if an intermediate destination was returned. If not, cache the entire path
258  * to the final destination tile. The low-level pathfinder might actually prefer a different docking tile in a nearby region. Without
259  * caching the full path the ship can get stuck in a loop. */
260  const WaterRegionPatchDesc end_water_patch = GetWaterRegionPatchInfo(node->GetTile());
261  const WaterRegionPatchDesc start_water_patch = GetWaterRegionPatchInfo(tile);
262  while (node->m_parent) {
263  const WaterRegionPatchDesc node_water_patch = GetWaterRegionPatchInfo(node->GetTile());
264  if (node_water_patch == start_water_patch || (!is_intermediate_destination && node_water_patch != end_water_patch)) {
265  path_cache.push_front(node->GetTrackdir());
266  }
267  node = node->m_parent;
268  }
269  assert(!path_cache.empty());
270 
271  /* Take out the last trackdir as the result. */
272  const Trackdir result = path_cache.front();
273  path_cache.pop_front();
274 
275  /* Clear path cache when in final water region patch. This is to allow ships to spread over different docking tiles dynamically. */
276  if (start_water_patch == end_water_patch) path_cache.clear();
277 
278  return result;
279  }
280 
281  return INVALID_TRACKDIR;
282  }
283 
294  static bool CheckShipReverse(const Ship *v, TileIndex tile, Trackdir td1, Trackdir td2, Trackdir *trackdir)
295  {
296  const std::vector<WaterRegionPatchDesc> high_level_path = YapfShipFindWaterRegionPath(v, tile, NUMBER_OR_WATER_REGIONS_LOOKAHEAD + 1);
297  if (high_level_path.empty()) {
298  if (trackdir) *trackdir = INVALID_TRACKDIR;
299  return false;
300  }
301 
302  /* Create pathfinder instance. */
303  Tpf pf(MAX_SHIP_PF_NODES);
304  /* Set origin and destination nodes. */
305  if (trackdir == nullptr) {
306  pf.SetOrigin(tile, TrackdirToTrackdirBits(td1) | TrackdirToTrackdirBits(td2));
307  } else {
310  pf.SetOrigin(tile, rtds);
311  }
312  pf.SetDestination(v);
313  if (high_level_path.size() > 1) pf.SetIntermediateDestination(high_level_path.back());
314  pf.RestrictSearch(high_level_path);
315 
316  /* Find best path. */
317  if (!pf.FindPath(v)) return false;
318 
319  Node *pNode = pf.GetBestNode();
320  if (pNode == nullptr) return false;
321 
322  /* Path was found, walk through the path back to the origin. */
323  while (pNode->m_parent != nullptr) {
324  pNode = pNode->m_parent;
325  }
326 
327  Trackdir best_trackdir = pNode->GetTrackdir();
328  if (trackdir != nullptr) {
329  *trackdir = best_trackdir;
330  } else {
331  assert(best_trackdir == td1 || best_trackdir == td2);
332  }
333  return best_trackdir != td1;
334  }
335 };
336 
338 template <class Types>
340 {
341 public:
342  typedef typename Types::Tpf Tpf;
343  typedef typename Types::TrackFollower TrackFollower;
344  typedef typename Types::NodeList::Titem Node;
345  typedef typename Node::Key Key;
346 
349  {
350  return *static_cast<Tpf*>(this);
351  }
352 
353 public:
354  inline int CurveCost(Trackdir td1, Trackdir td2)
355  {
356  assert(IsValidTrackdir(td1));
357  assert(IsValidTrackdir(td2));
358 
359  if (HasTrackdir(TrackdirCrossesTrackdirs(td1), td2)) {
360  /* 90-deg curve penalty. */
361  return Yapf().PfGetSettings().ship_curve90_penalty;
362  } else if (td2 != NextTrackdir(td1)) {
363  /* 45-deg curve penalty. */
364  return Yapf().PfGetSettings().ship_curve45_penalty;
365  }
366  return 0;
367  }
368 
369  static Vehicle *CountShipProc(Vehicle *v, void *data)
370  {
371  uint *count = (uint*)data;
372  /* Ignore other vehicles (aircraft) and ships inside depot. */
373  if (v->type == VEH_SHIP && (v->vehstatus & VS_HIDDEN) == 0) (*count)++;
374 
375  return nullptr;
376  }
377 
383  inline bool PfCalcCost(Node &n, const TrackFollower *tf)
384  {
385  /* Base tile cost depending on distance. */
386  int c = IsDiagonalTrackdir(n.GetTrackdir()) ? YAPF_TILE_LENGTH : YAPF_TILE_CORNER_LENGTH;
387  /* Additional penalty for curves. */
388  c += CurveCost(n.m_parent->GetTrackdir(), n.GetTrackdir());
389 
390  if (IsDockingTile(n.GetTile())) {
391  /* Check docking tile for occupancy. */
392  uint count = 0;
393  HasVehicleOnPos(n.GetTile(), &count, &CountShipProc);
394  c += count * 3 * YAPF_TILE_LENGTH;
395  }
396 
397  /* Skipped tile cost for aqueducts. */
398  c += YAPF_TILE_LENGTH * tf->m_tiles_skipped;
399 
400  /* Ocean/canal speed penalty. */
401  const ShipVehicleInfo *svi = ShipVehInfo(Yapf().GetVehicle()->engine_type);
402  byte speed_frac = (GetEffectiveWaterClass(n.GetTile()) == WATER_CLASS_SEA) ? svi->ocean_speed_frac : svi->canal_speed_frac;
403  if (speed_frac > 0) c += YAPF_TILE_LENGTH * (1 + tf->m_tiles_skipped) * speed_frac / (256 - speed_frac);
404 
405  /* Apply it. */
406  n.m_cost = n.m_parent->m_cost + c;
407  return true;
408  }
409 };
410 
415 template <class Tpf_, class Ttrack_follower, class Tnode_list>
417 {
419  typedef Tpf_ Tpf;
420  typedef Ttrack_follower TrackFollower;
421  typedef Tnode_list NodeList;
422  typedef Ship VehicleType;
423 
431 };
432 
433 struct CYapfShip : CYapfT<CYapfShip_TypesT<CYapfShip, CFollowTrackWater, CShipNodeListExitDir > >
434 {
435  explicit CYapfShip(int max_nodes) { m_max_search_nodes = max_nodes; }
436 };
437 
439 Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
440 {
441  Trackdir td_ret = CYapfShip::ChooseShipTrack(v, tile, enterdir, tracks, path_found, path_cache);
442  return (td_ret != INVALID_TRACKDIR) ? TrackdirToTrack(td_ret) : INVALID_TRACK;
443 }
444 
445 bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir)
446 {
447  Trackdir td = v->GetVehicleTrackdir();
448  Trackdir td_rev = ReverseTrackdir(td);
449  TileIndex tile = v->tile;
450  return CYapfShip::CheckShipReverse(v, tile, td, td_rev, trackdir);
451 }
ChooseShipTrack
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
Runs the pathfinder to choose a track to continue along.
Definition: ship_cmd.cpp:506
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:71
CYapfShip_TypesT
Config struct of YAPF for ships.
Definition: yapf_ship.cpp:416
TrackStatusToTrackdirBits
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:352
HasVehicleOnPos
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Checks whether a vehicle is on a specific location.
Definition: vehicle.cpp:520
WaterRegionPatchDesc
Describes a single interconnected patch of water within a particular water region.
Definition: water_regions.h:26
ShipVehicleInfo::canal_speed_frac
byte canal_speed_frac
Fraction of maximum speed for canal/river tiles.
Definition: engine_type.h:78
Ship::GetVehicleTrackdir
Trackdir GetVehicleTrackdir() const override
Returns the Trackdir on which the vehicle is currently located.
Definition: ship_cmd.cpp:292
CYapfDestinationTileWaterT::PfCalcEstimate
bool PfCalcEstimate(Node &n)
Called by YAPF to calculate cost estimate.
Definition: yapf_ship.cpp:97
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:104
TrackdirToTrack
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:262
TrackdirToExitdir
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:439
TrackdirToTrackdirBits
TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition: track_func.h:111
yapf.hpp
CYapfDestinationTileWaterT::Key
Node::Key Key
key to hash tables.
Definition: yapf_ship.cpp:34
Vehicle::vehstatus
byte vehstatus
Status.
Definition: vehicle_base.h:348
CYapfShip_TypesT::Tpf
Tpf_ Tpf
Pathfinder type.
Definition: yapf_ship.cpp:419
YapfShipCheckReverse
bool YapfShipCheckReverse(const Ship *v, Trackdir *trackdir)
Returns true if it is better to reverse the ship before leaving depot using YAPF.
Definition: yapf_ship.cpp:445
CYapfShip_TypesT::PfFollow
CYapfFollowShipT< Types > PfFollow
Node follower.
Definition: yapf_ship.cpp:426
yapf_node_ship.hpp
FindFirstTrackdir
Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition: track_func.h:211
CYapfFollowShipT::CreateRandomPath
static Trackdir CreateRandomPath(const Ship *v, TileIndex tile, Trackdir dir, ShipPathCache &path_cache, int path_length)
Creates a random path, avoids 90 degree turns.
Definition: yapf_ship.cpp:191
CYapfDestinationTileWaterT::PfDetectDestination
bool PfDetectDestination(Node &n)
Called by YAPF to detect if node ends in the desired destination.
Definition: yapf_ship.cpp:75
GetEffectiveWaterClass
WaterClass GetEffectiveWaterClass(TileIndex tile)
Determine the effective WaterClass for a ship travelling on a tile.
Definition: ship_cmd.cpp:55
CYapfShip_TypesT::TrackFollower
Ttrack_follower TrackFollower
Track follower helper class.
Definition: yapf_ship.cpp:420
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:37
GetWaterRegionCenterTile
TileIndex GetWaterRegionCenterTile(const WaterRegionDesc &water_region)
Returns the center tile of a particular water region.
Definition: water_regions.cpp:271
CYapfFollowShipT::GetRandomFollowUpTrackdir
static Trackdir GetRandomFollowUpTrackdir(const Ship *v, TileIndex tile, Trackdir dir, bool include_90_degree_turns)
Returns a random trackdir that can be reached from the current tile/trackdir, or INVALID_TRACK if non...
Definition: yapf_ship.cpp:176
TRANSPORT_WATER
@ TRANSPORT_WATER
Transport over water.
Definition: transport_type.h:29
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
CYapfT
YAPF template that uses Ttypes template argument to determine all YAPF components (base classes) from...
Definition: yapf_common.hpp:183
NextTrackdir
Trackdir NextTrackdir(Trackdir trackdir)
Maps a trackdir to the trackdir that you will end up on if you go straight ahead.
Definition: track_func.h:403
CYapfDestinationTileWaterT::Node
Types::NodeList::Titem Node
this will be our node type.
Definition: yapf_ship.cpp:33
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
CYapfFollowShipT::RestrictSearch
void RestrictSearch(const std::vector< WaterRegionPatchDesc > &path)
Restricts the search by creating corridor or water regions through which the ship is allowed to trave...
Definition: yapf_ship.cpp:163
CYapfCostShipT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_ship.cpp:348
GetTileTrackStatus
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:556
CYapfOriginTileT
YAPF origin provider base class - used when origin is one tile / multiple trackdirs.
Definition: yapf_common.hpp:15
CYapfFollowShipT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class).
Definition: yapf_ship.cpp:130
GetWaterRegionPatchInfo
WaterRegionPatchDesc GetWaterRegionPatchInfo(TileIndex tile)
Returns basic water region patch information for the provided tile.
Definition: water_regions.cpp:289
VS_HIDDEN
@ VS_HIDDEN
Vehicle is not visible.
Definition: vehicle_base.h:33
CYapfDestinationTileWaterT::Yapf
Tpf & Yapf()
To access inherited path finder.
Definition: yapf_ship.cpp:68
GetWaterRegionInfo
WaterRegionDesc GetWaterRegionInfo(TileIndex tile)
Returns basic water region information for the provided tile.
Definition: water_regions.cpp:280
Vehicle::dest_tile
TileIndex dest_tile
Heading for this tile.
Definition: vehicle_base.h:267
TrackBitsToTrackdirBits
TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:319
CYapfShip_TypesT::PfCost
CYapfCostShipT< Types > PfCost
Cost provider.
Definition: yapf_ship.cpp:430
yapf_ship_regions.h
CYapfCostShipT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class).
Definition: yapf_ship.cpp:342
CYapfFollowShipT::PfFollowNode
void PfFollowNode(Node &old_node)
Called by YAPF to move from the given node to the next tile.
Definition: yapf_ship.cpp:150
YAPF_TILE_LENGTH
static const int YAPF_TILE_LENGTH
Length (penalty) of one tile with YAPF.
Definition: pathfinder_type.h:29
Vehicle::tile
TileIndex tile
Current tile index.
Definition: vehicle_base.h:260
TRACKDIR_BIT_NONE
@ TRACKDIR_BIT_NONE
No track build.
Definition: track_type.h:99
ReverseDiagDir
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Definition: direction_func.h:118
Vehicle::current_order
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:349
CYapfCostShipT::Key
Node::Key Key
key to hash tables.
Definition: yapf_ship.cpp:345
CYapfDestinationTileWaterT
Definition: yapf_ship.cpp:28
ReverseTrackdir
Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:247
DiagdirReachesTrackdirs
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition: track_func.h:555
CYapfFollowShipT::CheckShipReverse
static bool CheckShipReverse(const Ship *v, TileIndex tile, Trackdir td1, Trackdir td2, Trackdir *trackdir)
Check whether a ship should reverse to reach its destination.
Definition: yapf_ship.cpp:294
CYapfFollowShipT::Key
Node::Key Key
key to hash tables.
Definition: yapf_ship.cpp:133
VehicleExitDir
DiagDirection VehicleExitDir(Direction direction, TrackBits track)
Determine the side in which the vehicle will leave the tile.
Definition: track_func.h:714
Randomizer::Next
uint32_t Next()
Generate the next pseudo random number.
Definition: random_func.cpp:43
RemoveFirstTrackdir
Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
Removes first Trackdir from TrackdirBits and returns it.
Definition: track_func.h:156
INVALID_TRACKDIR
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition: track_type.h:86
DiagDirection
DiagDirection
Enumeration for diagonal directions.
Definition: direction_type.h:73
CYapfShip_TypesT::Types
CYapfShip_TypesT< Tpf_, Ttrack_follower, Tnode_list > Types
Shortcut for this struct type.
Definition: yapf_ship.cpp:418
CountBits
constexpr uint CountBits(T value)
Counts the number of set bits in a variable.
Definition: bitmath_func.hpp:243
WATER_CLASS_SEA
@ WATER_CLASS_SEA
Sea.
Definition: water_map.h:48
Vehicle::direction
Direction direction
facing
Definition: vehicle_base.h:302
CYapfShip_TypesT::PfBase
CYapfBaseT< Types > PfBase
Pathfinder components (modules).
Definition: yapf_ship.cpp:425
CYapfSegmentCostCacheNoneT
CYapfSegmentCostCacheNoneT - the formal only yapf cost cache provider that implements PfNodeCacheFetc...
Definition: yapf_costcache.hpp:21
Ship
All ships have this type.
Definition: ship.h:24
CYapfFollowShipT::Yapf
Tpf & Yapf()
to access inherited path finder
Definition: yapf_ship.cpp:137
IsDockingTile
bool IsDockingTile(Tile t)
Checks whether the tile is marked as a dockling tile.
Definition: water_map.h:374
abs
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition: math_func.hpp:23
CYapfCostShipT::Node
Types::NodeList::Titem Node
this will be our node type.
Definition: yapf_ship.cpp:344
CalcClosestStationTile
TileIndex CalcClosestStationTile(StationID station, TileIndex tile, StationType station_type)
Calculates the tile of given station that is closest to a given tile for this we assume the station i...
Definition: pathfinder_func.h:25
CYapfFollowShipT
Node Follower module of YAPF for ships.
Definition: yapf_ship.cpp:127
Ship::state
TrackBits state
The "track" the ship is following.
Definition: ship.h:25
CYapfShip_TypesT::PfCache
CYapfSegmentCostCacheNoneT< Types > PfCache
Segment cost cache provider.
Definition: yapf_ship.cpp:429
ShipVehicleInfo
Information about a ship vehicle.
Definition: engine_type.h:67
CYapfShip_TypesT::PfOrigin
CYapfOriginTileT< Types > PfOrigin
Origin provider.
Definition: yapf_ship.cpp:427
CYapfCostShipT::PfCalcCost
bool PfCalcCost(Node &n, const TrackFollower *tf)
Called by YAPF to calculate the cost from the origin to the given node.
Definition: yapf_ship.cpp:383
IsDiagonalTrackdir
bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition: track_func.h:631
TrackBits
TrackBits
Allow incrementing of Track variables.
Definition: track_type.h:35
CYapfCostShipT
Cost Provider module of YAPF for ships.
Definition: yapf_ship.cpp:339
YapfShipFindWaterRegionPath
std::vector< WaterRegionPatchDesc > YapfShipFindWaterRegionPath(const Ship *v, TileIndex start_tile, int max_returned_path_length)
Finds a path at the water region level.
Definition: yapf_ship_regions.cpp:310
YAPF_TILE_CORNER_LENGTH
static const int YAPF_TILE_CORNER_LENGTH
Length (penalty) of a corner with YAPF.
Definition: pathfinder_type.h:32
IsShipDestinationTile
bool IsShipDestinationTile(TileIndex tile, StationID station)
Test if a tile is a docking tile for the given station.
Definition: ship_cmd.cpp:664
Trackdir
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:67
CYapfBaseT
CYapfBaseT - A-star type path finder base class.
Definition: yapf_base.hpp:47
CYapfDestinationTileWaterT::Tpf
Types::Tpf Tpf
the pathfinder class (derived from THIS class).
Definition: yapf_ship.cpp:31
TrackdirCrossesTrackdirs
TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
Maps a trackdir to all trackdirs that make 90 deg turns with it.
Definition: track_func.h:606
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:51
LinkGraph::BaseNode
Node of the link graph.
Definition: linkgraph.h:90
TrackdirBits
TrackdirBits
Allow incrementing of Trackdir variables.
Definition: track_type.h:98
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
CYapfShip_TypesT::PfDestination
CYapfDestinationTileWaterT< Types > PfDestination
Destination/distance provider.
Definition: yapf_ship.cpp:428
Track
Track
These are used to specify a single track.
Definition: track_type.h:19
HasTrackdir
bool HasTrackdir(TrackdirBits trackdirs, Trackdir trackdir)
Checks whether a TrackdirBits has a given Trackdir.
Definition: track_func.h:340
IsValidTrackdir
bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition: track_func.h:52
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
CYapfShip
Definition: yapf_ship.cpp:433
CYapfFollowShipT::Node
Types::NodeList::Titem Node
this will be our node type.
Definition: yapf_ship.cpp:132
TileAddByDiagDir
TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition: map_func.h:604
INVALID_TRACKDIR_BIT
@ INVALID_TRACKDIR_BIT
Flag for an invalid trackdirbit value.
Definition: track_type.h:114
YapfShipChooseTrack
Track YapfShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, ShipPathCache &path_cache)
Ship controller helper - path finder invoker.
Definition: yapf_ship.cpp:439
CYapfFollowShipT::TransportTypeChar
char TransportTypeChar() const
Return debug report character to identify the transportation type.
Definition: yapf_ship.cpp:170
ShipVehicleInfo::ocean_speed_frac
byte ocean_speed_frac
Fraction of maximum speed for ocean tiles.
Definition: engine_type.h:77
INVALID_TRACK
@ INVALID_TRACK
Flag for an invalid track.
Definition: track_type.h:28
FindFirstBit
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
Definition: bitmath_func.hpp:194