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