OpenTTD
npf.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../../stdafx.h"
13 #include "../../network/network.h"
14 #include "../../viewport_func.h"
15 #include "../../ship.h"
16 #include "../../roadstop_base.h"
17 #include "../pathfinder_func.h"
18 #include "../pathfinder_type.h"
19 #include "../follow_track.hpp"
20 #include "aystar.h"
21 
22 #include "../../safeguards.h"
23 
24 static const uint NPF_HASH_BITS = 12;
25 /* Do no change below values */
26 static const uint NPF_HASH_SIZE = 1 << NPF_HASH_BITS;
27 static const uint NPF_HASH_HALFBITS = NPF_HASH_BITS / 2;
28 static const uint NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1;
29 
33  StationID station_index;
34  bool reserve_path;
37  const Vehicle *v;
38 };
39 
42  Owner owner;
43  TransportType type;
44  RailTypes railtypes;
45  RoadTypes roadtypes;
46 };
47 
51  NPF_NODE_FLAGS,
52 };
53 
65 };
66 
73  bool res_okay;
74 };
75 
76 static AyStar _npf_aystar;
77 
78 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
79  * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
80  */
81 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
82 static const uint _trackdir_length[TRACKDIR_END] = {
83  NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
84  0, 0,
85  NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
86 };
87 
91 static inline bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag)
92 {
93  return HasBit(node->user_data[NPF_NODE_FLAGS], flag);
94 }
95 
99 static inline void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value)
100 {
101  SB(node->user_data[NPF_NODE_FLAGS], flag, 1, value);
102 }
103 
104 bool CheckIgnoreFirstTile(const PathNode *node)
105 {
106  return (node->parent == NULL && HasBit(node->node.user_data[NPF_NODE_FLAGS], NPF_FLAG_IGNORE_START_TILE));
107 }
108 
116 {
117  const uint dx = Delta(TileX(t0), TileX(t1));
118  const uint dy = Delta(TileY(t0), TileY(t1));
119 
120  const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
121  /* OPTIMISATION:
122  * Original: diagTracks = max(dx, dy) - min(dx,dy);
123  * Proof:
124  * (dx+dy) - straightTracks == (min + max) - straightTracks = min + max - 2 * min = max - min */
125  const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
126 
127  /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
128  * precision */
129  return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
130 }
131 
139 static uint NPFHash(uint key1, uint key2)
140 {
141  /* TODO: think of a better hash? */
142  uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
143  uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
144 
145  assert(IsValidTrackdir((Trackdir)key2));
146  assert(IsValidTile(key1));
147  return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
148 }
149 
150 static int32 NPFCalcZero(AyStar *as, AyStarNode *current, OpenListNode *parent)
151 {
152  return 0;
153 }
154 
155 /* Calculates the heuristic to the target station or tile. For train stations, it
156  * takes into account the direction of approach.
157  */
158 static int32 NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, OpenListNode *parent)
159 {
160  NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
161  NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
162  TileIndex from = current->tile;
163  TileIndex to = fstd->dest_coords;
164  uint dist;
165  AyStarUserData *user = (AyStarUserData *)as->user_data;
166 
167  /* for train-stations, we are going to aim for the closest station tile */
168  if (user->type != TRANSPORT_WATER && fstd->station_index != INVALID_STATION) {
169  to = CalcClosestStationTile(fstd->station_index, from, fstd->station_type);
170  }
171 
172  if (user->type == TRANSPORT_ROAD) {
173  /* Since roads only have diagonal pieces, we use manhattan distance here */
174  dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
175  } else {
176  /* Ships and trains can also go diagonal, so the minimum distance is shorter */
177  dist = NPFDistanceTrack(from, to);
178  }
179 
180  DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
181 
182  if (dist < ftd->best_bird_dist) {
183  ftd->best_bird_dist = dist;
184  ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
185  }
186  return dist;
187 }
188 
189 
190 /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
191  * get here, either getting it from the current choice or from the parent's
192  * choice */
193 static void NPFFillTrackdirChoice(AyStarNode *current, OpenListNode *parent)
194 {
195  if (parent->path.parent == NULL) {
196  Trackdir trackdir = current->direction;
197  /* This is a first order decision, so we'd better save the
198  * direction we chose */
199  current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
200  DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
201  } else {
202  /* We've already made the decision, so just save our parent's decision */
203  current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
204  }
205 }
206 
207 /* Will return the cost of the tunnel. If it is an entry, it will return the
208  * cost of that tile. If the tile is an exit, it will return the tunnel length
209  * including the exit tile. Requires that this is a Tunnel tile */
210 static uint NPFTunnelCost(AyStarNode *current)
211 {
212  DiagDirection exitdir = TrackdirToExitdir(current->direction);
213  TileIndex tile = current->tile;
214  if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
215  /* We just popped out if this tunnel, since were
216  * facing the tunnel exit */
217  return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
218  /* @todo: Penalty for tunnels? */
219  } else {
220  /* We are entering the tunnel, the enter tile is just a
221  * straight track */
222  return NPF_TILE_LENGTH;
223  }
224 }
225 
226 static inline uint NPFBridgeCost(AyStarNode *current)
227 {
228  return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
229 }
230 
231 static uint NPFSlopeCost(AyStarNode *current)
232 {
233  TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir(current->direction));
234 
235  /* Get center of tiles */
236  int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
237  int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
238  int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
239  int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
240 
241  int dx4 = (x2 - x1) / 4;
242  int dy4 = (y2 - y1) / 4;
243 
244  /* Get the height on both sides of the tile edge.
245  * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
246  */
247  int z1 = GetSlopePixelZ(x1 + dx4, y1 + dy4);
248  int z2 = GetSlopePixelZ(x2 - dx4, y2 - dy4);
249 
250  if (z2 - z1 > 1) {
251  /* Slope up */
253  }
254  return 0;
255  /* Should we give a bonus for slope down? Probably not, we
256  * could just subtract that bonus from the penalty, because
257  * there is only one level of steepness... */
258 }
259 
260 static uint NPFReservedTrackCost(AyStarNode *current)
261 {
262  TileIndex tile = current->tile;
263  TrackBits track = TrackToTrackBits(TrackdirToTrack(current->direction));
264  TrackBits res = GetReservedTrackbits(tile);
265 
266  if (NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL) || NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK) || ((res & track) == TRACK_BIT_NONE && !TracksOverlap(res | track))) return 0;
267 
268  if (IsTileType(tile, MP_TUNNELBRIDGE)) {
269  DiagDirection exitdir = TrackdirToExitdir(current->direction);
270  if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
272  }
273  }
275 }
276 
281 static void NPFMarkTile(TileIndex tile)
282 {
283  if (_debug_npf_level < 1 || _networking) return;
284  switch (GetTileType(tile)) {
285  case MP_RAILWAY:
286  /* DEBUG: mark visited tiles by mowing the grass under them ;-) */
287  if (!IsRailDepot(tile)) {
288  SetRailGroundType(tile, RAIL_GROUND_BARREN);
289  MarkTileDirtyByTile(tile);
290  }
291  break;
292 
293  case MP_ROAD:
294  if (!IsRoadDepot(tile)) {
296  MarkTileDirtyByTile(tile);
297  }
298  break;
299 
300  default:
301  break;
302  }
303 }
304 
305 static int32 NPFWaterPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
306 {
307  /* TileIndex tile = current->tile; */
308  int32 cost = 0;
309  Trackdir trackdir = current->direction;
310 
311  cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
312 
313  if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) {
314  cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys
315  }
316 
317  if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) {
319  }
320 
321  /* @todo More penalties? */
322 
323  return cost;
324 }
325 
326 /* Determine the cost of this node, for road tracks */
327 static int32 NPFRoadPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
328 {
329  TileIndex tile = current->tile;
330  int32 cost = 0;
331 
332  /* Determine base length */
333  switch (GetTileType(tile)) {
334  case MP_TUNNELBRIDGE:
335  cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
336  break;
337 
338  case MP_ROAD:
339  cost = NPF_TILE_LENGTH;
340  /* Increase the cost for level crossings */
342  break;
343 
344  case MP_STATION: {
345  cost = NPF_TILE_LENGTH;
346  const RoadStop *rs = RoadStop::GetByTile(tile, GetRoadStopType(tile));
347  if (IsDriveThroughStopTile(tile)) {
348  /* Increase the cost for drive-through road stops */
350  DiagDirection dir = TrackdirToExitdir(current->direction);
352  /* When we're the first road stop in a 'queue' of them we increase
353  * cost based on the fill percentage of the whole queue. */
354  const RoadStop::Entry *entry = rs->GetEntry(dir);
356  }
357  } else {
358  /* Increase cost for filled road stops */
359  cost += _settings_game.pf.npf.npf_road_bay_occupied_penalty * (!rs->IsFreeBay(0) + !rs->IsFreeBay(1)) / 2;
360  }
361  break;
362  }
363 
364  default:
365  break;
366  }
367 
368  /* Determine extra costs */
369 
370  /* Check for slope */
371  cost += NPFSlopeCost(current);
372 
373  /* Check for turns. Road vehicles only really drive diagonal, turns are
374  * represented by non-diagonal tracks */
375  if (!IsDiagonalTrackdir(current->direction)) {
377  }
378 
379  NPFMarkTile(tile);
380  DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
381  return cost;
382 }
383 
384 
385 /* Determine the cost of this node, for railway tracks */
386 static int32 NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
387 {
388  TileIndex tile = current->tile;
389  Trackdir trackdir = current->direction;
390  int32 cost = 0;
391  /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
392  OpenListNode new_node;
393 
394  /* Determine base length */
395  switch (GetTileType(tile)) {
396  case MP_TUNNELBRIDGE:
397  cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
398  break;
399 
400  case MP_RAILWAY:
401  cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
402  break;
403 
404  case MP_ROAD: // Railway crossing
405  cost = NPF_TILE_LENGTH;
406  break;
407 
408  case MP_STATION:
409  /* We give a station tile a penalty. Logically we would only want to give
410  * station tiles that are not our destination this penalty. This would
411  * discourage trains to drive through busy stations. But, we can just
412  * give any station tile a penalty, because every possible route will get
413  * this penalty exactly once, on its end tile (if it's a station) and it
414  * will therefore not make a difference. */
416 
417  if (IsRailWaypoint(tile)) {
418  NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
419  if (fstd->v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(tile) == fstd->v->current_order.GetDestination()) {
420  /* This waypoint is our destination; maybe this isn't an unreserved
421  * one, so check that and if so see that as the last signal being
422  * red. This way waypoints near stations should work better. */
423  const Train *train = Train::From(fstd->v);
424  CFollowTrackRail ft(train);
425  TileIndex t = tile;
426  Trackdir td = trackdir;
427  while (ft.Follow(t, td)) {
428  assert(t != ft.m_new_tile);
429  t = ft.m_new_tile;
431  /* We encountered a junction; it's going to be too complex to
432  * handle this perfectly, so just bail out. There is no simple
433  * free path, so try the other possibilities. */
434  td = INVALID_TRACKDIR;
435  break;
436  }
438  /* If this is a safe waiting position we're done searching for it */
439  if (IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg)) break;
440  }
441  if (td == INVALID_TRACKDIR ||
442  !IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg) ||
445  }
446  }
447  }
448  break;
449 
450  default:
451  break;
452  }
453 
454  /* Determine extra costs */
455 
456  /* Check for signals */
457  if (IsTileType(tile, MP_RAILWAY)) {
458  if (HasSignalOnTrackdir(tile, trackdir)) {
459  SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
460  /* Ordinary track with signals */
461  if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
462  /* Signal facing us is red */
463  if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
464  /* Penalize the first signal we
465  * encounter, if it is red */
466 
467  /* Is this a presignal exit or combo? */
468  if (!IsPbsSignal(sigtype)) {
469  if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
470  /* Penalise exit and combo signals differently (heavier) */
472  } else {
474  }
475  }
476  }
477  /* Record the state of this signal. Path signals are assumed to
478  * be green as the signal state of them has no meaning for this. */
479  NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, !IsPbsSignal(sigtype));
480  } else {
481  /* Record the state of this signal */
482  NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
483  }
484  if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
485  if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) {
486  NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true);
487  } else {
488  NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true);
489  }
490  } else {
491  NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
492  }
493  NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK, !IsPbsSignal(sigtype));
494  }
495 
496  if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) {
498  }
499  }
500 
501  /* Penalise the tile if it is a target tile and the last signal was
502  * red */
503  /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
504  * of course... */
505  new_node.path.node = *current;
506  if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) {
508  }
509 
510  /* Check for slope */
511  cost += NPFSlopeCost(current);
512 
513  /* Check for turns */
514  if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) {
516  }
517  /* TODO, with realistic acceleration, also the amount of straight track between
518  * curves should be taken into account, as this affects the speed limit. */
519 
520  /* Check for reverse in depot */
521  if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
522  /* Penalise any depot tile that is not the last tile in the path. This
523  * _should_ penalise every occurrence of reversing in a depot (and only
524  * that) */
526  }
527 
528  /* Check for occupied track */
529  cost += NPFReservedTrackCost(current);
530 
531  NPFMarkTile(tile);
532  DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
533  return cost;
534 }
535 
536 /* Will find any depot */
537 static int32 NPFFindDepot(const AyStar *as, const OpenListNode *current)
538 {
539  AyStarUserData *user = (AyStarUserData *)as->user_data;
540  /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
541  * since checking the cache not that much faster than the actual check */
542  return IsDepotTypeTile(current->path.node.tile, user->type) ?
544 }
545 
547 static int32 NPFFindSafeTile(const AyStar *as, const OpenListNode *current)
548 {
549  const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
550 
551  return (IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
552  IsWaitingPositionFree(v, current->path.node.tile, current->path.node.direction, _settings_game.pf.forbid_90_deg)) ?
554 }
555 
556 /* Will find a station identified using the NPFFindStationOrTileData */
557 static int32 NPFFindStationOrTile(const AyStar *as, const OpenListNode *current)
558 {
559  NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
560  const AyStarNode *node = &current->path.node;
561  TileIndex tile = node->tile;
562 
563  if (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) return AYSTAR_FOUND_END_NODE;
564 
565  if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) {
566  if (fstd->v->type == VEH_TRAIN) return AYSTAR_FOUND_END_NODE;
567 
568  assert(fstd->v->type == VEH_ROAD);
569  /* Only if it is a valid station *and* we can stop there */
570  if (GetStationType(tile) == fstd->station_type && (fstd->not_articulated || IsDriveThroughStopTile(tile))) return AYSTAR_FOUND_END_NODE;
571  }
572  return AYSTAR_DONE;
573 }
574 
582 static const PathNode *FindSafePosition(PathNode *path, const Train *v)
583 {
584  /* If there is no signal, reserve the whole path. */
585  PathNode *sig = path;
586 
587  for (; path->parent != NULL; path = path->parent) {
588  if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
589  sig = path;
590  }
591  }
592 
593  return sig;
594 }
595 
599 static void ClearPathReservation(const PathNode *start, const PathNode *end)
600 {
601  bool first_run = true;
602  for (; start != end; start = start->parent) {
603  if (IsRailStationTile(start->node.tile) && first_run) {
604  SetRailStationPlatformReservation(start->node.tile, TrackdirToExitdir(start->node.direction), false);
605  } else {
606  UnreserveRailTrack(start->node.tile, TrackdirToTrack(start->node.direction));
607  }
608  first_run = false;
609  }
610 }
611 
618 static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
619 {
620  AyStarUserData *user = (AyStarUserData *)as->user_data;
621  NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
622  ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
623  ftd->best_path_dist = current->g;
624  ftd->best_bird_dist = 0;
625  ftd->node = current->path.node;
626  ftd->res_okay = false;
627 
628  if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && user->type == TRANSPORT_RAIL) {
629  /* Path reservation is requested. */
630  const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
631 
632  const PathNode *target = FindSafePosition(&current->path, v);
633  ftd->node = target->node;
634 
635  /* If the target is a station skip to platform end. */
636  if (IsRailStationTile(target->node.tile)) {
637  DiagDirection dir = TrackdirToExitdir(target->node.direction);
638  uint len = Station::GetByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir);
639  TileIndex end_tile = TILE_ADD(target->node.tile, (len - 1) * TileOffsByDiagDir(dir));
640 
641  /* Update only end tile, trackdir of a station stays the same. */
642  ftd->node.tile = end_tile;
643  if (!IsWaitingPositionFree(v, end_tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
644  SetRailStationPlatformReservation(target->node.tile, dir, true);
645  SetRailStationReservation(target->node.tile, false);
646  } else {
647  if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
648  }
649 
650  for (const PathNode *cur = target; cur->parent != NULL; cur = cur->parent) {
651  if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) {
652  /* Reservation failed, undo. */
653  ClearPathReservation(target, cur);
654  return;
655  }
656  }
657 
658  ftd->res_okay = true;
659  }
660 }
661 
671 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
672 {
673  if (IsTileType(tile, MP_RAILWAY) || // Rail tile (also rail depot)
674  HasStationTileRail(tile) || // Rail station tile/waypoint
675  IsRoadDepotTile(tile) || // Road depot tile
676  IsStandardRoadStopTile(tile)) { // Road station tile (but not drive-through stops)
677  return IsTileOwner(tile, owner); // You need to own these tiles entirely to use them
678  }
679 
680  switch (GetTileType(tile)) {
681  case MP_ROAD:
682  /* rail-road crossing : are we looking at the railway part? */
683  if (IsLevelCrossing(tile) &&
684  DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
685  return IsTileOwner(tile, owner); // Railway needs owner check, while the street is public
686  }
687  break;
688 
689  case MP_TUNNELBRIDGE:
691  return IsTileOwner(tile, owner);
692  }
693  break;
694 
695  default:
696  break;
697  }
698 
699  return true; // no need to check
700 }
701 
702 
707 {
708  assert(IsDepotTypeTile(tile, type));
709 
710  switch (type) {
711  case TRANSPORT_RAIL: return GetRailDepotDirection(tile);
712  case TRANSPORT_ROAD: return GetRoadDepotDirection(tile);
713  case TRANSPORT_WATER: return GetShipDepotDirection(tile);
714  default: return INVALID_DIAGDIR; // Not reached
715  }
716 }
717 
720 {
721  if (IsNormalRoadTile(tile)) {
722  RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
723  switch (rb) {
724  case ROAD_NW: return DIAGDIR_NW;
725  case ROAD_SW: return DIAGDIR_SW;
726  case ROAD_SE: return DIAGDIR_SE;
727  case ROAD_NE: return DIAGDIR_NE;
728  default: break;
729  }
730  }
731  return INVALID_DIAGDIR;
732 }
733 
744 static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
745 {
746  if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type);
747 
748  if (type == TRANSPORT_ROAD) {
749  if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
750  if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
751  }
752 
753  return INVALID_DIAGDIR;
754 }
755 
765 static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
766 {
767  DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
768  return single_entry != INVALID_DIAGDIR && single_entry != dir;
769 }
770 
779 static bool CanEnterTile(TileIndex tile, DiagDirection dir, AyStarUserData *user)
780 {
781  /* Check tunnel entries and bridge ramps */
782  if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
783 
784  /* Test ownership */
785  if (!CanEnterTileOwnerCheck(user->owner, tile, dir)) return false;
786 
787  /* check correct rail type (mono, maglev, etc) */
788  if (user->type == TRANSPORT_RAIL) {
789  RailType rail_type = GetTileRailType(tile);
790  if (!HasBit(user->railtypes, rail_type)) return false;
791  }
792 
793  /* Depots, standard roadstops and single tram bits can only be entered from one direction */
794  DiagDirection single_entry = GetTileSingleEntry(tile, user->type, user->roadtypes);
795  if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
796 
797  return true;
798 }
799 
811 static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
812 {
813  TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
814 
815  if (trackdirbits == TRACKDIR_BIT_NONE && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
816  /* GetTileTrackStatus() returns 0 for single tram bits.
817  * As we cannot change it there (easily) without breaking something, change it here */
818  switch (GetSingleTramBit(dst_tile)) {
819  case DIAGDIR_NE:
820  case DIAGDIR_SW:
821  trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
822  break;
823 
824  case DIAGDIR_NW:
825  case DIAGDIR_SE:
826  trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
827  break;
828 
829  default: break;
830  }
831  }
832 
833  DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
834 
835  /* Select only trackdirs we can reach from our current trackdir */
836  trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
837 
838  /* Filter out trackdirs that would make 90 deg turns for trains */
839  if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
840 
841  DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
842 
843  return trackdirbits;
844 }
845 
846 
847 /* Will just follow the results of GetTileTrackStatus concerning where we can
848  * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
849  * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
850  * entry and exit are neighbours. Will fill
851  * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
852  * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
853 static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
854 {
855  AyStarUserData *user = (AyStarUserData *)aystar->user_data;
856  /* We leave src_tile on track src_trackdir in direction src_exitdir */
857  Trackdir src_trackdir = current->path.node.direction;
858  TileIndex src_tile = current->path.node.tile;
859  DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
860 
861  /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
862  TransportType type = user->type;
863  uint subtype = user->roadtypes;
864 
865  /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
866  aystar->num_neighbours = 0;
867  DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
868 
869  /* We want to determine the tile we arrive, and which choices we have there */
870  TileIndex dst_tile;
871  TrackdirBits trackdirbits;
872 
873  /* Find dest tile */
874  /* Is src_tile valid, and can be used?
875  * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
876  * But we must not check the validity of this move, as src_tile is totally unrelated to the move, if a roadvehicle reversed on a junction. */
877  if (CheckIgnoreFirstTile(&current->path)) {
878  /* Do not perform any checks that involve src_tile */
879  dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
880  trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
881  } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
882  /* We drive through the wormhole and arrive on the other side */
883  dst_tile = GetOtherTunnelBridgeEnd(src_tile);
884  trackdirbits = TrackdirToTrackdirBits(src_trackdir);
885  } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
886  /* We can only reverse on this tile */
887  dst_tile = src_tile;
888  src_trackdir = ReverseTrackdir(src_trackdir);
889  trackdirbits = TrackdirToTrackdirBits(src_trackdir);
890  } else {
891  /* We leave src_tile in src_exitdir and reach dst_tile */
892  dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
893 
894  if (dst_tile != INVALID_TILE && !CanEnterTile(dst_tile, src_exitdir, user)) dst_tile = INVALID_TILE;
895 
896  if (dst_tile == INVALID_TILE) {
897  /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
898  if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
899 
900  dst_tile = src_tile;
901  src_trackdir = ReverseTrackdir(src_trackdir);
902  }
903 
904  trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
905 
906  if (trackdirbits == TRACKDIR_BIT_NONE) {
907  /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
908  if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
909 
910  dst_tile = src_tile;
911  src_trackdir = ReverseTrackdir(src_trackdir);
912 
913  trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
914  }
915  }
916 
917  if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
918  /* Mask out any reserved tracks. */
919  TrackBits reserved = GetReservedTrackbits(dst_tile);
920  trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
921 
922  Track t;
923  FOR_EACH_SET_TRACK(t, TrackdirBitsToTrackBits(trackdirbits)) {
924  if (TracksOverlap(reserved | TrackToTrackBits(t))) trackdirbits &= ~TrackToTrackdirBits(t);
925  }
926  }
927 
928  /* Enumerate possible track */
929  uint i = 0;
930  while (trackdirbits != TRACKDIR_BIT_NONE) {
931  Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
932  DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
933 
934  /* Tile with signals? */
935  if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
936  if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir))) {
937  /* If there's a one-way signal not pointing towards us, stop going in this direction. */
938  break;
939  }
940  }
941  {
942  /* We've found ourselves a neighbour :-) */
943  AyStarNode *neighbour = &aystar->neighbours[i];
944  neighbour->tile = dst_tile;
945  neighbour->direction = dst_trackdir;
946  /* Save user data */
947  neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
948  NPFFillTrackdirChoice(neighbour, current);
949  }
950  i++;
951  }
952  aystar->num_neighbours = i;
953 }
954 
955 /*
956  * Plan a route to the specified target (which is checked by target_proc),
957  * from start1 and if not NULL, from start2 as well. The type of transport we
958  * are checking is in type. reverse_penalty is applied to all routes that
959  * originate from the second start node.
960  * When we are looking for one specific target (optionally multiple tiles), we
961  * should use a good heuristic to perform aystar search. When we search for
962  * multiple targets that are spread around, we should perform a breadth first
963  * search by specifiying CalcZero as our heuristic.
964  */
965 static NPFFoundTargetData NPFRouteInternal(AyStarNode *start1, bool ignore_start_tile1, AyStarNode *start2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, AyStarUserData *user, uint reverse_penalty, bool ignore_reserved = false, int max_penalty = 0)
966 {
967  int r;
968  NPFFoundTargetData result;
969 
970  /* Initialize procs */
971  _npf_aystar.max_path_cost = max_penalty;
972  _npf_aystar.CalculateH = heuristic_proc;
973  _npf_aystar.EndNodeCheck = target_proc;
974  _npf_aystar.FoundEndNode = NPFSaveTargetData;
975  _npf_aystar.GetNeighbours = NPFFollowTrack;
976  switch (user->type) {
977  default: NOT_REACHED();
978  case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break;
979  case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break;
980  case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
981  }
982 
983  /* Initialize Start Node(s) */
984  start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
985  start1->user_data[NPF_NODE_FLAGS] = 0;
986  NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
987  NPFSetFlag(start1, NPF_FLAG_IGNORE_RESERVED, ignore_reserved);
988  _npf_aystar.AddStartNode(start1, 0);
989  if (start2 != NULL) {
990  start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
991  start2->user_data[NPF_NODE_FLAGS] = 0;
992  NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
993  NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
994  NPFSetFlag(start2, NPF_FLAG_IGNORE_RESERVED, ignore_reserved);
995  _npf_aystar.AddStartNode(start2, reverse_penalty);
996  }
997 
998  /* Initialize result */
999  result.best_bird_dist = UINT_MAX;
1000  result.best_path_dist = UINT_MAX;
1002  result.node.tile = INVALID_TILE;
1003  result.res_okay = false;
1004  _npf_aystar.user_path = &result;
1005 
1006  /* Initialize target */
1007  _npf_aystar.user_target = target;
1008 
1009  /* Initialize user_data */
1010  _npf_aystar.user_data = user;
1011 
1012  /* GO! */
1013  r = _npf_aystar.Main();
1014  assert(r != AYSTAR_STILL_BUSY);
1015 
1016  if (result.best_bird_dist != 0) {
1017  if (target != NULL) {
1018  DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
1019  } else {
1020  /* Assumption: target == NULL, so we are looking for a depot */
1021  DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
1022  }
1023 
1024  }
1025  return result;
1026 }
1027 
1028 /* Will search as below, but with two start nodes, the second being the
1029  * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which
1030  * direction was taken (NPFGetFlag(result.node, NPF_FLAG_REVERSE)) */
1031 static NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStarUserData *user)
1032 {
1033  AyStarNode start1;
1034  AyStarNode start2;
1035 
1036  start1.tile = tile1;
1037  start2.tile = tile2;
1038  start1.direction = trackdir1;
1039  start2.direction = trackdir2;
1040 
1041  return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, user, 0);
1042 }
1043 
1044 /* Will search from the given tile and direction, for a route to the given
1045  * station for the given transport type. See the declaration of
1046  * NPFFoundTargetData above for the meaning of the result. */
1047 static NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, AyStarUserData *user)
1048 {
1049  return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, user);
1050 }
1051 
1052 /* Search using breadth first. Good for little track choice and inaccurate
1053  * heuristic, such as railway/road with two start nodes, the second being the reverse. Call
1054  * NPFGetFlag(result.node, NPF_FLAG_REVERSE) to see from which node the path
1055  * originated. All paths from the second node will have the given
1056  * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full
1057  * tile).
1058  */
1059 static NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStarUserData *user, uint reverse_penalty, int max_penalty)
1060 {
1061  AyStarNode start1;
1062  AyStarNode start2;
1063 
1064  start1.tile = tile1;
1065  start2.tile = tile2;
1066  start1.direction = trackdir1;
1067  start2.direction = trackdir2;
1068 
1069  /* perform a breadth first search. Target is NULL,
1070  * since we are just looking for any depot...*/
1071  return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindDepot, NPFCalcZero, user, reverse_penalty, false, max_penalty);
1072 }
1073 
1074 void InitializeNPF()
1075 {
1076  static bool first_init = true;
1077  if (first_init) {
1078  first_init = false;
1079  _npf_aystar.Init(NPFHash, NPF_HASH_SIZE);
1080  } else {
1081  _npf_aystar.Clear();
1082  }
1083  _npf_aystar.loops_per_tick = 0;
1084  _npf_aystar.max_path_cost = 0;
1085  //_npf_aystar.max_search_nodes = 0;
1086  /* We will limit the number of nodes for now, until we have a better
1087  * solution to really fix performance */
1089 }
1090 
1091 static void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, const Vehicle *v, bool reserve_path = false)
1092 {
1093  /* Ships don't really reach their stations, but the tile in front. So don't
1094  * save the station id for ships. For roadvehs we don't store it either,
1095  * because multistop depends on vehicles actually reaching the exact
1096  * dest_tile, not just any stop of that station.
1097  * So only for train orders to stations we fill fstd->station_index, for all
1098  * others only dest_coords */
1099  if (v->type != VEH_SHIP && (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_WAYPOINT))) {
1100  assert(v->IsGroundVehicle());
1102  fstd->station_type = (v->type == VEH_TRAIN) ? (v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT) : (RoadVehicle::From(v)->IsBus() ? STATION_BUS : STATION_TRUCK);
1104  /* Let's take the closest tile of the station as our target for vehicles */
1106  } else {
1107  fstd->dest_coords = v->dest_tile;
1108  fstd->station_index = INVALID_STATION;
1109  }
1110  fstd->reserve_path = reserve_path;
1111  fstd->v = v;
1112 }
1113 
1114 /*** Road vehicles ***/
1115 
1117 {
1118  Trackdir trackdir = v->GetVehicleTrackdir();
1119 
1120  AyStarUserData user = { v->owner, TRANSPORT_ROAD, INVALID_RAILTYPES, v->compatible_roadtypes };
1121  NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, INVALID_TILE, INVALID_TRACKDIR, false, NULL, &user, 0, max_penalty);
1122 
1123  if (ftd.best_bird_dist != 0) return FindDepotData();
1124 
1125  /* Found target */
1126  /* Our caller expects a number of tiles, so we just approximate that
1127  * number by this. It might not be completely what we want, but it will
1128  * work for now :-) We can possibly change this when the old pathfinder
1129  * is removed. */
1130  return FindDepotData(ftd.node.tile, ftd.best_path_dist);
1131 }
1132 
1133 Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, bool &path_found)
1134 {
1136 
1137  NPFFillWithOrderData(&fstd, v);
1138  Trackdir trackdir = DiagDirToDiagTrackdir(enterdir);
1139 
1140  AyStarUserData user = { v->owner, TRANSPORT_ROAD, INVALID_RAILTYPES, v->compatible_roadtypes };
1141  NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, &user);
1142 
1143  assert(ftd.best_trackdir != INVALID_TRACKDIR);
1144 
1145  /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1146  * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1147  * we did not find our target, but ftd.best_trackdir contains the direction leading
1148  * to the tile closest to our target. */
1149  path_found = (ftd.best_bird_dist == 0);
1150  return ftd.best_trackdir;
1151 }
1152 
1153 /*** Ships ***/
1154 
1155 Track NPFShipChooseTrack(const Ship *v, bool &path_found)
1156 {
1158  Trackdir trackdir = v->GetVehicleTrackdir();
1159  assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
1160 
1161  NPFFillWithOrderData(&fstd, v);
1162 
1164  NPFFoundTargetData ftd = NPFRouteToStationOrTile(v->tile, trackdir, true, &fstd, &user);
1165 
1166  assert(ftd.best_trackdir != INVALID_TRACKDIR);
1167 
1168  /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1169  * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1170  * we did not find our target, but ftd.best_trackdir contains the direction leading
1171  * to the tile closest to our target. */
1172  path_found = (ftd.best_bird_dist == 0);
1173  return TrackdirToTrack(ftd.best_trackdir);
1174 }
1175 
1177 {
1179  NPFFoundTargetData ftd;
1180 
1181  NPFFillWithOrderData(&fstd, v);
1182 
1183  Trackdir trackdir = v->GetVehicleTrackdir();
1184  Trackdir trackdir_rev = ReverseTrackdir(trackdir);
1185  assert(trackdir != INVALID_TRACKDIR);
1186  assert(trackdir_rev != INVALID_TRACKDIR);
1187 
1189  ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, v->tile, trackdir_rev, false, &fstd, &user);
1190  /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
1191  return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
1192 }
1193 
1194 /*** Trains ***/
1195 
1197 {
1198  const Train *last = v->Last();
1199  Trackdir trackdir = v->GetVehicleTrackdir();
1200  Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
1202  fstd.v = v;
1203  fstd.reserve_path = false;
1204 
1205  assert(trackdir != INVALID_TRACKDIR);
1206  AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE };
1207  NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, &user, NPF_INFINITE_PENALTY, max_penalty);
1208  if (ftd.best_bird_dist != 0) return FindDepotData();
1209 
1210  /* Found target */
1211  /* Our caller expects a number of tiles, so we just approximate that
1212  * number by this. It might not be completely what we want, but it will
1213  * work for now :-) We can possibly change this when the old pathfinder
1214  * is removed. */
1215  return FindDepotData(ftd.node.tile, ftd.best_path_dist, NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE));
1216 }
1217 
1218 bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
1219 {
1220  assert(v->type == VEH_TRAIN);
1221 
1223  fstd.v = v;
1224  fstd.reserve_path = true;
1225 
1226  AyStarNode start1;
1227  start1.tile = tile;
1228  start1.direction = trackdir;
1229 
1230  RailTypes railtypes = v->compatible_railtypes;
1231  if (override_railtype) railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
1232 
1233  /* perform a breadth first search. Target is NULL,
1234  * since we are just looking for any safe tile...*/
1235  AyStarUserData user = { v->owner, TRANSPORT_RAIL, railtypes, ROADTYPES_NONE };
1236  return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, &user, 0, true).res_okay;
1237 }
1238 
1240 {
1242  NPFFoundTargetData ftd;
1243  const Train *last = v->Last();
1244 
1245  NPFFillWithOrderData(&fstd, v);
1246 
1247  Trackdir trackdir = v->GetVehicleTrackdir();
1248  Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
1249  assert(trackdir != INVALID_TRACKDIR);
1250  assert(trackdir_rev != INVALID_TRACKDIR);
1251 
1252  AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE };
1253  ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, &user);
1254  /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
1255  return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
1256 }
1257 
1258 Track NPFTrainChooseTrack(const Train *v, bool &path_found, bool reserve_track, struct PBSTileInfo *target)
1259 {
1261  NPFFillWithOrderData(&fstd, v, reserve_track);
1262 
1263  PBSTileInfo origin = FollowTrainReservation(v);
1264  assert(IsValidTrackdir(origin.trackdir));
1265 
1266  AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE };
1267  NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, &user);
1268 
1269  if (target != NULL) {
1270  target->tile = ftd.node.tile;
1271  target->trackdir = (Trackdir)ftd.node.direction;
1272  target->okay = ftd.res_okay;
1273  }
1274 
1275  assert(ftd.best_trackdir != INVALID_TRACKDIR);
1276 
1277  /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1278  * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1279  * we did not find our target, but ftd.best_trackdir contains the direction leading
1280  * to the tile closest to our target. */
1281  path_found = (ftd.best_bird_dist == 0);
1282  /* Discard enterdir information, making it a normal track */
1283  return TrackdirToTrack(ftd.best_trackdir);
1284 }
TileIndex GetOtherBridgeEnd(TileIndex tile)
Starting at one bridge end finds the other bridge end.
Definition: bridge_map.cpp:61
static TileType GetTileType(TileIndex tile)
Get the tiletype of a given tile.
Definition: tile_map.h:98
void Init(Hash_HashProc hash, uint num_buckets)
Initialize an AyStar.
Definition: aystar.cpp:295
Used to mark that the start tile is invalid, and searching should start from the second tile on...
Definition: npf.cpp:62
presignal inter-block
Definition: signal_type.h:29
static bool HasSignalOnTrackdir(TileIndex tile, Trackdir trackdir)
Checks for the presence of signals along the given trackdir on the given rail tile.
Definition: rail_map.h:427
Used to mark that the possible reservation target is already reserved.
Definition: npf.cpp:63
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
bool _networking
are we in networking mode?
Definition: network.cpp:56
static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
Returns the direction the exit of the depot on the given tile is facing.
Definition: npf.cpp:706
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:298
static TransportType GetTunnelBridgeTransportType(TileIndex t)
Tunnel: Get the transport type of the tunnel (road or rail) Bridge: Get the transport type of the bri...
Internal node.
Definition: aystar.h:57
bool Follow(TileIndex old_tile, Trackdir old_td)
main follower routine.
presignal block exit
Definition: signal_type.h:28
bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
Try to extend the reserved path of a train to the nearest safe tile using NPF.
Definition: npf.cpp:1218
SignalType
Type of signal, i.e.
Definition: signal_type.h:25
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
TrackdirBits m_new_td_bits
the new set of available trackdirs
static RoadStopType GetRoadStopType(TileIndex t)
Get the road stop type of this tile.
Definition: station_map.h:57
Flag for an invalid DiagDirection.
static Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition: track_func.h:272
int GetLength() const
Get the length of this drive through stop.
Definition: roadstop_base.h:49
Used for iterations.
Definition: track_type.h:92
Used to mark that two signals were seen, rail only.
Definition: npf.cpp:57
Track
These are used to specify a single track.
Definition: track_type.h:21
A tile with road (or tram tracks)
Definition: tile_type.h:45
byte loops_per_tick
How many loops are there called before Main() gives control back to the caller. 0 = until done...
Definition: aystar.h:140
uint max_path_cost
If the g-value goes over this number, it stops searching, 0 = infinite.
Definition: aystar.h:141
static TrackdirBits TrackToTrackdirBits(Track track)
Returns a TrackdirBit mask from a given Track.
Definition: track_func.h:304
int GetOccupied() const
Get the amount of occupied space in this drive through stop.
Definition: roadstop_base.h:58
static const PathNode * FindSafePosition(PathNode *path, const Train *v)
Find the node containing the first signal on the path.
Definition: npf.cpp:582
TileIndex dest_tile
Heading for this tile.
Definition: vehicle_base.h:237
static bool IsOnewaySignal(TileIndex t, Track track)
One-way signals can&#39;t be passed the &#39;wrong&#39; way.
Definition: rail_map.h:320
Transport over water.
NPFSettings npf
pathfinder settings for the new pathfinder
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
Not an end-tile, or wrong direction.
Definition: aystar.h:34
South-west part.
Definition: road_type.h:58
PathfinderSettings pf
settings for all pathfinders
uint32 npf_max_search_nodes
The maximum amount of search nodes a single NPF run should take.
Vehicle data structure.
Definition: vehicle_base.h:212
NPFNodeFlag
Flags for AyStarNode.userdata[NPF_NODE_FLAGS].
Definition: npf.cpp:55
static uint NPFHash(uint key1, uint key2)
Calculates a hash value for use in the NPF.
Definition: npf.cpp:139
static RoadBits GetRoadBits(TileIndex t, RoadType rt)
Get the present road bits for a specific road type.
Definition: road_map.h:111
Northeast, upper right on your monitor.
static 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:413
bool forbid_90_deg
forbid trains to make 90 deg turns
static void SetRailStationReservation(TileIndex t, bool b)
Set the reservation state of the rail station.
Definition: station_map.h:406
Nothing (dirt)
Definition: rail_map.h:487
A railway.
Definition: tile_type.h:44
static DiagDirection GetRoadDepotDirection(TileIndex t)
Get the direction of the exit of a road depot.
Definition: road_map.h:535
RailTypes compatible_railtypes
bitmask to the OTHER railtypes on which an engine of THIS railtype can physically travel ...
Definition: rail.h:182
uint32 npf_rail_firstred_penalty
the penalty for when the first signal is red (and it is not an exit or combo signal) ...
FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty)
Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF...
Definition: npf.cpp:1116
static bool IsStandardRoadStopTile(TileIndex t)
Is tile t a standard (non-drive through) road stop station?
Definition: station_map.h:224
static bool IsLevelCrossing(TileIndex t)
Return whether a tile is a level crossing.
Definition: road_map.h:68
static bool IsDriveThroughStopTile(TileIndex t)
Is tile t a drive through road stop station?
Definition: station_map.h:234
PBSTileInfo FollowTrainReservation(const Train *v, Vehicle **train_on_res)
Follow a train reservation to the last tile.
Definition: pbs.cpp:291
Meant to be stored in AyStar.targetdata.
Definition: npf.cpp:31
static DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition: track_func.h:449
static bool CanEnterTile(TileIndex tile, DiagDirection dir, AyStarUserData *user)
Tests if a vehicle can enter a tile.
Definition: npf.cpp:779
uint32 npf_crossing_penalty
the penalty for level crossings
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
bool TryReserveRailTrack(TileIndex tile, Track t, bool trigger_stations)
Try to reserve a specific track on a tile.
Definition: pbs.cpp:82
static StationType GetStationType(TileIndex t)
Get the station type of this tile.
Definition: station_map.h:45
static const uint TILE_SIZE
Tile size in world coordinates.
Definition: tile_type.h:15
This file has the header for AyStar.
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
uint32 npf_rail_pbs_cross_penalty
the penalty for crossing a reserved rail track
static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
Tests if a tile can be entered or left only from one side.
Definition: npf.cpp:744
Normal rail tile with signals.
Definition: rail_map.h:26
static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
Finds out if a given company&#39;s vehicles are allowed to enter a given tile.
Definition: npf.cpp:671
static bool IsValidTile(TileIndex tile)
Checks if a tile is valid.
Definition: tile_map.h:163
TrackBits
Bitfield corresponding to Track.
Definition: track_type.h:41
Buses, trucks and trams belong to this class.
Definition: roadveh.h:88
Track x-axis, direction north-east.
Definition: track_type.h:108
static bool IsTileOwner(TileIndex tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition: tile_map.h:216
AyStarNodeUserDataType
Indices into AyStarNode.userdata[].
Definition: npf.cpp:49
AyStar search algorithm struct.
Definition: aystar.h:118
static void ClearPathReservation(const PathNode *start, const PathNode *end)
Lift the reservation of the tiles from start till end, excluding end itself.
Definition: npf.cpp:599
static TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition: map_func.h:343
static bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition: track_func.h:62
Track NPFTrainChooseTrack(const Train *v, bool &path_found, bool reserve_track, struct PBSTileInfo *target)
Finds the best path for given train using NPF.
Definition: npf.cpp:1258
static bool IsTileType(TileIndex tile, TileType type)
Checks if a tile is a given tiletype.
Definition: tile_map.h:152
Meant to be stored in AyStar.userpath.
Definition: npf.cpp:68
uint32 npf_road_dt_occupied_penalty
the penalty multiplied by the fill percentage of a drive-through road stop
static bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
Tests if a vehicle must reverse on a tile.
Definition: npf.cpp:765
FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_penalty)
Used when user sends train to the nearest depot or if train needs servicing using NPF...
Definition: npf.cpp:1196
void AddStartNode(AyStarNode *start_node, uint g)
Adds a node from where to start an algorithm.
Definition: aystar.cpp:282
static TileIndexDiffC TileIndexDiffCByDiagDir(DiagDirection dir)
Returns the TileIndexDiffC offset from a DiagDirection.
Definition: map_func.h:270
static bool IsRailStationTile(TileIndex t)
Is this tile a station tile and a rail station?
Definition: station_map.h:103
uint32 npf_buoy_penalty
the penalty for going over (through) a buoy
Used to mark that reserved tiles should be considered impassable.
Definition: npf.cpp:64
static bool IsDepotTypeTile(TileIndex tile, TransportType type)
Check if a tile is a depot and it is a depot of the given type.
Definition: depot_map.h:20
static TrackdirBits TrackdirToTrackdirBits(Trackdir trackdir)
Maps a Trackdir to the corresponding TrackdirBits value.
Definition: track_func.h:121
static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
Calculates the minimum distance travelled to get from t0 to t1 when only using tracks (ie...
Definition: npf.cpp:115
TileIndex dest_coords
An indication of where the station is, for heuristic purposes, or the target tile.
Definition: npf.cpp:32
static DiagDirection GetRoadStopDir(TileIndex t)
Gets the direction the road stop entrance points towards.
Definition: station_map.h:258
bool res_okay
True if a path reservation could be made.
Definition: npf.cpp:73
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:63
Invalid railtypes.
Definition: rail_type.h:59
static DiagDirection GetRailDepotDirection(TileIndex t)
Returns the direction the depot is facing to.
Definition: rail_map.h:172
static DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
static uint GetTunnelBridgeLength(TileIndex begin, TileIndex end)
Calculates the length of a tunnel or a bridge (without end tiles)
Definition: tunnelbridge.h:26
Southeast.
Southwest.
static const int NPF_INFINITE_PENALTY
This penalty is the equivalent of "infinite", which means that paths that get this penalty will be ch...
bool IsSafeWaitingPosition(const Train *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
Determine whether a certain track on a tile is a safe position to end a path.
Definition: pbs.cpp:383
static bool HasStationTileRail(TileIndex t)
Has this station tile a rail? In other words, is this station tile a rail station or rail waypoint...
Definition: station_map.h:147
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition: pbs.cpp:429
A path of nodes.
Definition: aystar.h:47
Road on barren land.
Definition: road_map.h:448
Trackdir GetVehicleTrackdir() const
Returns the Trackdir on which the vehicle is currently located.
Trackdir best_trackdir
The trackdir that leads to the shortest path/closest birds dist.
Definition: npf.cpp:71
Trams.
Definition: road_type.h:25
#define FOR_EACH_SET_TRACK(var, track_bits)
Iterate through each set Track in a TrackBits value.
Definition: track_func.h:29
static Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
Removes first Trackdir from TrackdirBits and returns it.
Definition: track_func.h:166
static int32 NPFFindSafeTile(const AyStar *as, const OpenListNode *current)
Find any safe and free tile.
Definition: npf.cpp:547
uint max_search_nodes
The maximum number of nodes that will be expanded, 0 = infinite.
Definition: aystar.h:142
uint best_bird_dist
The best heuristic found. Is 0 if the target was found.
Definition: npf.cpp:69
RoadBits
Enumeration for the road parts on a tile.
Definition: road_type.h:55
static bool IsRoadDepotTile(TileIndex t)
Return whether a tile is a road depot tile.
Definition: road_map.h:99
Trackdir
Enumeration for tracks and directions.
Definition: track_type.h:74
static bool HasPbsSignalOnTrackdir(TileIndex tile, Trackdir td)
Is a pbs signal present along the trackdir?
Definition: rail_map.h:464
static Axis GetCrossingRoadAxis(TileIndex t)
Get the road axis of a level crossing.
Definition: road_map.h:295
uint32 npf_road_curve_penalty
the penalty for curves
static bool IsBuoyTile(TileIndex t)
Is tile t a buoy tile?
Definition: station_map.h:317
Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, bool &path_found)
Finds the best path for given road vehicle using NPF.
Definition: npf.cpp:1133
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
North-east part.
Definition: road_type.h:60
Northwest.
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:901
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:1785
static Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition: track_func.h:547
bool IsFreeBay(uint nr) const
Checks whether the given bay is free in this road stop.
Definition: roadstop_base.h:95
bool okay
True if tile is a safe waiting position, false otherwise.
Definition: pbs.h:31
static void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value)
Sets the given flag on the given AyStarNode to the given value.
Definition: npf.cpp:99
DiagDirection
Enumeration for diagonal directions.
Road vehicle type.
Definition: vehicle_type.h:27
StationID station_index
station index we&#39;re heading for, or INVALID_STATION when we&#39;re heading for a tile ...
Definition: npf.cpp:33
static TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
Maps a trackdir to the trackdirs that can be reached from it (ie, when entering the next tile...
Definition: track_func.h:594
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
bool NPFTrainCheckReverse(const Train *v)
Returns true if it is better to reverse the train before leaving station using NPF.
Definition: npf.cpp:1239
Used to mark that this node was reached from the second start node, if applicable.
Definition: npf.cpp:59
RailTypes
The different railtypes we support, but then a bitmask of them.
Definition: rail_type.h:53
int32 AyStar_EndNodeCheck(const AyStar *aystar, const OpenListNode *current)
Check whether the end-tile is found.
Definition: aystar.h:80
Used to mark that three signals were seen, rail only.
Definition: npf.cpp:58
PathNode * parent
The parent of this item.
Definition: aystar.h:49
static DiagDirection GetTunnelBridgeDirection(TileIndex t)
Get the direction pointing to the other end.
Track follower helper template class (can serve pathfinders and vehicle controllers).
Node in the search.
Definition: aystar.h:40
All ships have this type.
Definition: ship.h:28
RoadTypes
The different roadtypes we support, but then a bitmask of them.
Definition: road_type.h:36
bool reserve_path
Indicates whether the found path should be reserved.
Definition: npf.cpp:34
static 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...
static Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition: track_func.h:257
TileIndex tile
Tile the path ends, INVALID_TILE if no valid path was found.
Definition: pbs.h:29
int32 AyStar_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
Calculate the H-value for the AyStar algorithm.
Definition: aystar.h:95
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
Definition: landscape.cpp:591
static void SetRoadside(TileIndex tile, Roadside s)
Set the decorations of a road.
Definition: road_map.h:473
Ship vehicle type.
Definition: vehicle_type.h:28
North-west part.
Definition: road_type.h:57
Container for each entry point of a drive through road stop.
Definition: roadstop_base.h:34
Used to mark that the last signal on this path was a block signal.
Definition: npf.cpp:61
uint32 npf_rail_firstred_exit_penalty
the penalty for when the first signal is red (and it is an exit or combo signal)
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
South-east part.
Definition: road_type.h:59
This struct contains information about the end of a reserved path.
Definition: pbs.h:28
uint32 npf_rail_pbs_signal_back_penalty
the penalty for passing a pbs signal from the backside
static RailTileType GetRailTileType(TileIndex t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition: rail_map.h:37
Some checking was done, but no path found yet, and there are still items left to try.
Definition: aystar.h:31
static Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
static DiagDirection GetSingleTramBit(TileIndex tile)
Tests if a tile is a road tile with a single tramtrack (tram can reverse)
Definition: npf.cpp:719
static TileIndex GetOtherTunnelBridgeEnd(TileIndex t)
Determines type of the wormhole and returns its other end.
Transport by train.
static TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition: track_func.h:87
Indices into AyStar.userdata[].
Definition: npf.cpp:41
RailType GetTileRailType(TileIndex tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition: rail.cpp:157
static DiagDirection GetShipDepotDirection(TileIndex t)
Get the direction of the ship depot.
Definition: water_map.h:261
static StationID GetStationIndex(TileIndex t)
Get StationID from a tile.
Definition: station_map.h:29
static T KillFirstBit(T value)
Clear the first bit in an integer.
bool IsBus() const
Check whether a roadvehicle is a bus.
Definition: roadveh_cmd.cpp:81
StationType
Station types.
Definition: station_type.h:34
uint32 npf_road_bay_occupied_penalty
the penalty multiplied by the fill percentage of a road bay
static bool IsNormalRoadTile(TileIndex t)
Return whether a tile is a normal road tile.
Definition: road_map.h:57
bool IsGroundVehicle() const
Check if the vehicle is a ground vehicle.
Definition: vehicle_base.h:471
Trackdir GetVehicleTrackdir() const
Get the tracks of the train vehicle.
Definition: train_cmd.cpp:4012
static bool IsRailDepot(TileIndex t)
Is this rail tile a rail depot?
Definition: rail_map.h:96
Tunnel entry/exit and bridge heads.
Definition: tile_type.h:52
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:96
static bool IsRailDepotTile(TileIndex t)
Is this tile rail tile and a rail depot?
Definition: rail_map.h:106
uint32 npf_rail_slope_penalty
the penalty for sloping upwards
Track x-axis, direction south-west.
Definition: track_type.h:115
T * Last()
Get the last vehicle in the chain.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
bool not_articulated
The (road) vehicle is not articulated.
Definition: npf.cpp:36
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:159
Helper container to find a depot.
uint32 npf_rail_depot_reverse_penalty
the penalty for reversing in depots
Track y-axis, direction north-west.
Definition: track_type.h:116
uint32 npf_rail_curve_penalty
the penalty for curves
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
int Main()
This is the function you call to run AyStar.
Definition: aystar.cpp:247
OwnerByte owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
TrackBits GetReservedTrackbits(TileIndex t)
Get the reserved trackbits for any tile, regardless of type.
Definition: pbs.cpp:26
TileIndex m_new_tile
the new tile (the vehicle has entered)
TransportType
Available types of transport.
static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
Returns the driveable Trackdirs on a tile.
Definition: npf.cpp:811
AyStarNode node
The node within the target the search led us to.
Definition: npf.cpp:72
static bool IsDiagonalTrackdir(Trackdir trackdir)
Checks if a given Trackdir is diagonal.
Definition: track_func.h:641
Trackdir GetVehicleTrackdir() const
Returns the Trackdir on which the vehicle is currently located.
Definition: ship_cmd.cpp:253
A tile of a station.
Definition: tile_type.h:48
static Station * GetByTile(TileIndex tile)
Get the station belonging to a specific tile.
uint best_path_dist
The shortest path. Is UINT_MAX if no path is found.
Definition: npf.cpp:70
void SetRailStationPlatformReservation(TileIndex start, DiagDirection dir, bool b)
Set the reservation for a complete station platform.
Definition: pbs.cpp:59
#define STRAIGHT_TRACK_LENGTH
Approximation of the length of a straight track, relative to a diagonal track (ie the size of a tile ...
Definition: map_type.h:80
Transport by road vehicle.
const Entry * GetEntry(DiagDirection dir) const
Get the drive through road stop entry struct for the given direction.
static const int NPF_TILE_LENGTH
Length (penalty) of one tile with NPF.
static bool IsRailWaypoint(TileIndex t)
Is this station tile a rail waypoint?
Definition: station_map.h:114
uint32 npf_road_drive_through_penalty
the penalty for going through a drive-through road stop
TrackdirBits
Enumeration of bitmasks for the TrackDirs.
Definition: track_type.h:106
The signal is red.
Definition: signal_type.h:47
No roadtypes.
Definition: road_type.h:37
A Stop for a Road Vehicle.
Definition: roadstop_base.h:24
static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next)
Checks whether the &#39;next&#39; tile is still part of the road same drive through stop &#39;rs&#39; in the same dir...
Definition: roadstop.cpp:307
TileIndex GetOtherTunnelEnd(TileIndex tile)
Gets the other end of the tunnel.
Definition: tunnel_map.cpp:24
const Vehicle * v
The vehicle we are pathfinding for.
Definition: npf.cpp:37
Used to mark that the last signal on this path was red.
Definition: npf.cpp:60
Track y-axis, direction south-east.
Definition: track_type.h:109
Trackdir trackdir
The reserved trackdir on the tile.
Definition: pbs.h:30
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
The trackdir chosen to get here.
Definition: npf.cpp:50
static TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition: track_func.h:329
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Track NPFShipChooseTrack(const Ship *v, bool &path_found)
Finds the best path for given ship using NPF.
Definition: npf.cpp:1155
No track build.
Definition: track_type.h:107
static const uint NPF_HASH_BITS
The size of the hash used in pathfinding. Just changing this value should be sufficient to change the...
Definition: npf.cpp:24
static TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition: track_func.h:318
Owner
Enum for all companies/owners.
Definition: company_type.h:20
StationType station_type
The type of station we&#39;re heading for.
Definition: npf.cpp:35
Used to mark that a signal was seen on the way, for rail only.
Definition: npf.cpp:56
Flag for an invalid trackdir.
Definition: track_type.h:93
static bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag)
Returns the current value of the given flag on the given AyStarNode.
Definition: npf.cpp:91
static TileIndex AddTileIndexDiffCWrap(TileIndex tile, TileIndexDiffC diff)
Add a TileIndexDiffC to a TileIndex and returns the new one.
Definition: map_func.h:302
#define TILE_ADD(x, y)
Adds to tiles together.
Definition: map_func.h:246
static SignalState GetSignalStateByTrackdir(TileIndex tile, Trackdir trackdir)
Gets the state of the signal along the given trackdir.
Definition: rail_map.h:439
void Clear()
This function make the memory go back to zero.
Definition: aystar.cpp:224
uint GetPlatformLength(TileIndex tile, DiagDirection dir) const
Determines the REMAINING length of a platform, starting at (and including) the given tile...
Definition: station.cpp:249
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:232
static TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
Maps a trackdir to all trackdirs that make 90 deg turns with it.
Definition: track_func.h:616
uint32 npf_rail_lastred_penalty
the penalty for when the last signal is red
static bool IsTunnel(TileIndex t)
Is this a tunnel (entrance)?
Definition: tunnel_map.h:24
VehicleTypeByte type
Type of vehicle.
Definition: vehicle_type.h:56
static TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition: track_func.h:362
bool NPFShipCheckReverse(const Ship *v)
Returns true if it is better to reverse the ship before leaving depot using NPF.
Definition: npf.cpp:1176
No track.
Definition: track_type.h:42
static void NPFMarkTile(TileIndex tile)
Mark tiles by mowing the grass when npf debug level >= 1.
Definition: npf.cpp:281
static bool IsRoadDepot(TileIndex t)
Return whether a tile is a road depot.
Definition: road_map.h:89
static bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition: track_func.h:655
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:318
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition: roadstop.cpp:268
uint32 npf_water_curve_penalty
the penalty for curves
static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
To be called when current contains the (shortest route to) the target node.
Definition: npf.cpp:618
void UnreserveRailTrack(TileIndex tile, Track t)
Lift the reservation of a specific track on a tile.
Definition: pbs.cpp:143
An end node was found.
Definition: aystar.h:29
uint32 npf_rail_station_penalty
the penalty for station tiles
Train vehicle type.
Definition: vehicle_type.h:26