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