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 
8 /** @file npf.cpp Implementation of the NPF pathfinder. */
9 
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; ///< The size of the hash used in pathfinding. Just changing this value should be sufficient to change the hash size. Should be an even value.
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 
30 /** Meant to be stored in AyStar.targetdata */
31 struct NPFFindStationOrTileData {
32 	TileIndex dest_coords;    ///< An indication of where the station is, for heuristic purposes, or the target tile
33 	StationID station_index;  ///< station index we're heading for, or INVALID_STATION when we're heading for a tile
34 	bool reserve_path;        ///< Indicates whether the found path should be reserved
35 	StationType station_type; ///< The type of station we're heading for
36 	bool not_articulated;     ///< The (road) vehicle is not articulated
37 	const Vehicle *v;         ///< The vehicle we are pathfinding for
38 };
39 
40 /** Indices into AyStar.userdata[] */
41 struct AyStarUserData {
42 	Owner owner;
43 	TransportType type;
44 	RailTypes railtypes;
45 	RoadTypes roadtypes;
46 	uint subtype;
47 };
48 
49 /** Indices into AyStarNode.userdata[] */
50 enum AyStarNodeUserDataType {
51 	NPF_TRACKDIR_CHOICE = 0, ///< The trackdir chosen to get here
52 	NPF_NODE_FLAGS,
53 };
54 
55 /** Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFSetFlag() and NPFGetFlag() to use them. */
56 enum NPFNodeFlag {
57 	NPF_FLAG_SEEN_SIGNAL,       ///< Used to mark that a signal was seen on the way, for rail only
58 	NPF_FLAG_2ND_SIGNAL,        ///< Used to mark that two signals were seen, rail only
59 	NPF_FLAG_3RD_SIGNAL,        ///< Used to mark that three signals were seen, rail only
60 	NPF_FLAG_REVERSE,           ///< Used to mark that this node was reached from the second start node, if applicable
61 	NPF_FLAG_LAST_SIGNAL_RED,   ///< Used to mark that the last signal on this path was red
62 	NPF_FLAG_LAST_SIGNAL_BLOCK, ///< Used to mark that the last signal on this path was a block signal
63 	NPF_FLAG_IGNORE_START_TILE, ///< Used to mark that the start tile is invalid, and searching should start from the second tile on
64 	NPF_FLAG_TARGET_RESERVED,   ///< Used to mark that the possible reservation target is already reserved
65 	NPF_FLAG_IGNORE_RESERVED,   ///< Used to mark that reserved tiles should be considered impassable
66 };
67 
68 /** Meant to be stored in AyStar.userpath */
69 struct NPFFoundTargetData {
70 	uint best_bird_dist;    ///< The best heuristic found. Is 0 if the target was found
71 	uint best_path_dist;    ///< The shortest path. Is UINT_MAX if no path is found
72 	Trackdir best_trackdir; ///< The trackdir that leads to the shortest path/closest birds dist
73 	AyStarNode node;        ///< The node within the target the search led us to
74 	bool res_okay;          ///< True if a path reservation could be made
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 
89 /**
90  * Returns the current value of the given flag on the given AyStarNode.
91  */
NPFGetFlag(const AyStarNode * node,NPFNodeFlag flag)92 static inline bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag)
93 {
94 	return HasBit(node->user_data[NPF_NODE_FLAGS], flag);
95 }
96 
97 /**
98  * Sets the given flag on the given AyStarNode to the given value.
99  */
NPFSetFlag(AyStarNode * node,NPFNodeFlag flag,bool value)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 
CheckIgnoreFirstTile(const PathNode * node)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 
110 /**
111  * Calculates the minimum distance travelled to get from t0 to t1 when only
112  * using tracks (ie, only making 45 degree turns). Returns the distance in the
113  * NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to
114  * prevent rounding.
115  */
NPFDistanceTrack(TileIndex t0,TileIndex t1)116 static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
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 
133 /**
134  * Calculates a hash value for use in the NPF.
135  * @param key1 The TileIndex of the tile to hash
136  * @param key2 The Trackdir of the track on the tile.
137  *
138  * @todo Think of a better hash.
139  */
NPFHash(uint key1,uint key2)140 static uint NPFHash(uint key1, uint key2)
141 {
142 	/* TODO: think of a better hash? */
143 	uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
144 	uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
145 
146 	assert(IsValidTrackdir((Trackdir)key2));
147 	assert(IsValidTile(key1));
148 	return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
149 }
150 
NPFCalcZero(AyStar * as,AyStarNode * current,OpenListNode * parent)151 static int32 NPFCalcZero(AyStar *as, AyStarNode *current, OpenListNode *parent)
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  */
NPFCalcStationOrTileHeuristic(AyStar * as,AyStarNode * current,OpenListNode * parent)159 static int32 NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, OpenListNode *parent)
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 */
NPFFillTrackdirChoice(AyStarNode * current,OpenListNode * parent)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 */
NPFTunnelCost(AyStarNode * current)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 
NPFBridgeCost(AyStarNode * current)227 static inline uint NPFBridgeCost(AyStarNode *current)
228 {
229 	return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
230 }
231 
NPFSlopeCost(AyStarNode * current)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);
249 	int z2 = GetSlopePixelZ(x2 - dx4, y2 - dy4);
250 
251 	if (z2 - z1 > 1) {
252 		/* Slope up */
253 		return _settings_game.pf.npf.npf_rail_slope_penalty;
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 
NPFReservedTrackCost(AyStarNode * current)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)) {
272 			return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty * (GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile)) + 1);
273 		}
274 	}
275 	return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty;
276 }
277 
278 /**
279  * Mark tiles by mowing the grass when npf debug level >= 1.
280  * Will not work for multiplayer games, since it can (will) cause desyncs.
281  */
NPFMarkTile(TileIndex tile)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)) {
296 				SetRoadside(tile, ROADSIDE_BARREN);
297 				MarkTileDirtyByTile(tile);
298 			}
299 			break;
300 
301 		default:
302 			break;
303 	}
304 }
305 
CountShipProc(Vehicle * v,void * data)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 
NPFWaterPathCost(AyStar * as,AyStarNode * current,OpenListNode * parent)315 static int32 NPFWaterPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
316 {
317 	/* TileIndex tile = current->tile; */
318 	int32 cost = 0;
319 	Trackdir trackdir = current->direction;
320 
321 	cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
322 
323 	if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) {
324 		cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys
325 	}
326 
327 	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) {
328 		cost += _settings_game.pf.npf.npf_water_curve_penalty;
329 	}
330 
331 	if (IsDockingTile(current->tile)) {
332 		/* Check docking tile for occupancy */
333 		uint count = 0;
334 		HasVehicleOnPos(current->tile, &count, &CountShipProc);
335 		cost += count * 3 * _trackdir_length[trackdir];
336 	}
337 
338 	/* @todo More penalties? */
339 
340 	return cost;
341 }
342 
343 /* Determine the cost of this node, for road tracks */
NPFRoadPathCost(AyStar * as,AyStarNode * current,OpenListNode * parent)344 static int32 NPFRoadPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
345 {
346 	TileIndex tile = current->tile;
347 	int32 cost = 0;
348 
349 	/* Determine base length */
350 	switch (GetTileType(tile)) {
351 		case MP_TUNNELBRIDGE:
352 			cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
353 			break;
354 
355 		case MP_ROAD:
356 			cost = NPF_TILE_LENGTH;
357 			/* Increase the cost for level crossings */
358 			if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty;
359 			break;
360 
361 		case MP_STATION: {
362 			cost = NPF_TILE_LENGTH;
363 			const RoadStop *rs = RoadStop::GetByTile(tile, GetRoadStopType(tile));
364 			if (IsDriveThroughStopTile(tile)) {
365 				/* Increase the cost for drive-through road stops */
366 				cost += _settings_game.pf.npf.npf_road_drive_through_penalty;
367 				DiagDirection dir = TrackdirToExitdir(current->direction);
368 				if (!RoadStop::IsDriveThroughRoadStopContinuation(tile, tile - TileOffsByDiagDir(dir))) {
369 					/* When we're the first road stop in a 'queue' of them we increase
370 					 * cost based on the fill percentage of the whole queue. */
371 					const RoadStop::Entry *entry = rs->GetEntry(dir);
372 					cost += entry->GetOccupied() * _settings_game.pf.npf.npf_road_dt_occupied_penalty / entry->GetLength();
373 				}
374 			} else {
375 				/* Increase cost for filled road stops */
376 				cost += _settings_game.pf.npf.npf_road_bay_occupied_penalty * (!rs->IsFreeBay(0) + !rs->IsFreeBay(1)) / 2;
377 			}
378 			break;
379 		}
380 
381 		default:
382 			break;
383 	}
384 
385 	/* Determine extra costs */
386 
387 	/* Check for slope */
388 	cost += NPFSlopeCost(current);
389 
390 	/* Check for turns. Road vehicles only really drive diagonal, turns are
391 	 * represented by non-diagonal tracks */
392 	if (!IsDiagonalTrackdir(current->direction)) {
393 		cost += _settings_game.pf.npf.npf_road_curve_penalty;
394 	}
395 
396 	NPFMarkTile(tile);
397 	Debug(npf, 4, "Calculating G for: ({}, {}). Result: {}", TileX(current->tile), TileY(current->tile), cost);
398 	return cost;
399 }
400 
401 
402 /* Determine the cost of this node, for railway tracks */
NPFRailPathCost(AyStar * as,AyStarNode * current,OpenListNode * parent)403 static int32 NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
404 {
405 	TileIndex tile = current->tile;
406 	Trackdir trackdir = current->direction;
407 	int32 cost = 0;
408 	/* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
409 	OpenListNode new_node;
410 
411 	/* Determine base length */
412 	switch (GetTileType(tile)) {
413 		case MP_TUNNELBRIDGE:
414 			cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
415 			break;
416 
417 		case MP_RAILWAY:
418 			cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
419 			break;
420 
421 		case MP_ROAD: // Railway crossing
422 			cost = NPF_TILE_LENGTH;
423 			break;
424 
425 		case MP_STATION:
426 			/* We give a station tile a penalty. Logically we would only want to give
427 			 * station tiles that are not our destination this penalty. This would
428 			 * discourage trains to drive through busy stations. But, we can just
429 			 * give any station tile a penalty, because every possible route will get
430 			 * this penalty exactly once, on its end tile (if it's a station) and it
431 			 * will therefore not make a difference. */
432 			cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty;
433 
434 			if (IsRailWaypoint(tile)) {
435 				NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
436 				if (fstd->v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(tile) == fstd->v->current_order.GetDestination()) {
437 					/* This waypoint is our destination; maybe this isn't an unreserved
438 					 * one, so check that and if so see that as the last signal being
439 					 * red. This way waypoints near stations should work better. */
440 					const Train *train = Train::From(fstd->v);
441 					CFollowTrackRail ft(train);
442 					TileIndex t = tile;
443 					Trackdir td = trackdir;
444 					while (ft.Follow(t, td)) {
445 						assert(t != ft.m_new_tile);
446 						t = ft.m_new_tile;
447 						if (KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
448 							/* We encountered a junction; it's going to be too complex to
449 							 * handle this perfectly, so just bail out. There is no simple
450 							 * free path, so try the other possibilities. */
451 							td = INVALID_TRACKDIR;
452 							break;
453 						}
454 						td = RemoveFirstTrackdir(&ft.m_new_td_bits);
455 						/* If this is a safe waiting position we're done searching for it */
456 						if (IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg)) break;
457 					}
458 					if (td == INVALID_TRACKDIR ||
459 							!IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg) ||
460 							!IsWaitingPositionFree(train, t, td, _settings_game.pf.forbid_90_deg)) {
461 						cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
462 					}
463 				}
464 			}
465 			break;
466 
467 		default:
468 			break;
469 	}
470 
471 	/* Determine extra costs */
472 
473 	/* Check for signals */
474 	if (IsTileType(tile, MP_RAILWAY)) {
475 		if (HasSignalOnTrackdir(tile, trackdir)) {
476 			SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
477 			/* Ordinary track with signals */
478 			if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
479 				/* Signal facing us is red */
480 				if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
481 					/* Penalize the first signal we
482 					 * encounter, if it is red */
483 
484 					/* Is this a presignal exit or combo? */
485 					if (!IsPbsSignal(sigtype)) {
486 						if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
487 							/* Penalise exit and combo signals differently (heavier) */
488 							cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty;
489 						} else {
490 							cost += _settings_game.pf.npf.npf_rail_firstred_penalty;
491 						}
492 					}
493 				}
494 				/* Record the state of this signal. Path signals are assumed to
495 				 * be green as the signal state of them has no meaning for this. */
496 				NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, !IsPbsSignal(sigtype));
497 			} else {
498 				/* Record the state of this signal */
499 				NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
500 			}
501 			if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
502 				if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) {
503 					NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true);
504 				} else {
505 					NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true);
506 				}
507 			} else {
508 				NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
509 			}
510 			NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK, !IsPbsSignal(sigtype));
511 		}
512 
513 		if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) {
514 			cost += _settings_game.pf.npf.npf_rail_pbs_signal_back_penalty;
515 		}
516 	}
517 
518 	/* Penalise the tile if it is a target tile and the last signal was
519 	 * red */
520 	/* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
521 	 * of course... */
522 	new_node.path.node = *current;
523 	if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) {
524 		cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
525 	}
526 
527 	/* Check for slope */
528 	cost += NPFSlopeCost(current);
529 
530 	/* Check for turns */
531 	if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) {
532 		cost += _settings_game.pf.npf.npf_rail_curve_penalty;
533 	}
534 	/* TODO, with realistic acceleration, also the amount of straight track between
535 	 *      curves should be taken into account, as this affects the speed limit. */
536 
537 	/* Check for reverse in depot */
538 	if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
539 		/* Penalise any depot tile that is not the last tile in the path. This
540 		 * _should_ penalise every occurrence of reversing in a depot (and only
541 		 * that) */
542 		cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty;
543 	}
544 
545 	/* Check for occupied track */
546 	cost += NPFReservedTrackCost(current);
547 
548 	NPFMarkTile(tile);
549 	Debug(npf, 4, "Calculating G for: ({}, {}). Result: {}", TileX(current->tile), TileY(current->tile), cost);
550 	return cost;
551 }
552 
553 /* Will find any depot */
NPFFindDepot(const AyStar * as,const OpenListNode * current)554 static int32 NPFFindDepot(const AyStar *as, const OpenListNode *current)
555 {
556 	AyStarUserData *user = (AyStarUserData *)as->user_data;
557 	/* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
558 	 * since checking the cache not that much faster than the actual check */
559 	return IsDepotTypeTile(current->path.node.tile, user->type) ?
560 		AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
561 }
562 
563 /** Find any safe and free tile. */
NPFFindSafeTile(const AyStar * as,const OpenListNode * current)564 static int32 NPFFindSafeTile(const AyStar *as, const OpenListNode *current)
565 {
566 	const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
567 
568 	return (IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
569 			IsWaitingPositionFree(v, current->path.node.tile, current->path.node.direction, _settings_game.pf.forbid_90_deg)) ?
570 				AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
571 }
572 
573 /* Will find a station identified using the NPFFindStationOrTileData */
NPFFindStationOrTile(const AyStar * as,const OpenListNode * current)574 static int32 NPFFindStationOrTile(const AyStar *as, const OpenListNode *current)
575 {
576 	NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
577 	const AyStarNode *node = &current->path.node;
578 	TileIndex tile = node->tile;
579 
580 	if (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) return AYSTAR_FOUND_END_NODE;
581 
582 	if (fstd->v->type == VEH_SHIP) {
583 		/* Ships do not actually reach the destination station, so we check for a docking tile instead. */
584 		if (IsDockingTile(tile) && IsShipDestinationTile(tile, fstd->station_index)) return AYSTAR_FOUND_END_NODE;
585 		return AYSTAR_DONE;
586 	}
587 
588 	if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) {
589 		if (fstd->v->type == VEH_TRAIN) return AYSTAR_FOUND_END_NODE;
590 
591 		assert(fstd->v->type == VEH_ROAD);
592 		/* Only if it is a valid station *and* we can stop there */
593 		if (GetStationType(tile) == fstd->station_type && (fstd->not_articulated || IsDriveThroughStopTile(tile))) return AYSTAR_FOUND_END_NODE;
594 	}
595 	return AYSTAR_DONE;
596 }
597 
598 /**
599  * Find the node containing the first signal on the path.
600  *
601  * If the first signal is on the very first two tiles of the path,
602  * the second signal is returned. If no suitable signal is present, the
603  * last node of the path is returned.
604  */
FindSafePosition(PathNode * path,const Train * v)605 static const PathNode *FindSafePosition(PathNode *path, const Train *v)
606 {
607 	/* If there is no signal, reserve the whole path. */
608 	PathNode *sig = path;
609 
610 	for (; path->parent != nullptr; path = path->parent) {
611 		if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
612 			sig = path;
613 		}
614 	}
615 
616 	return sig;
617 }
618 
619 /**
620  * Lift the reservation of the tiles from @p start till @p end, excluding @p end itself.
621  */
ClearPathReservation(const PathNode * start,const PathNode * end)622 static void ClearPathReservation(const PathNode *start, const PathNode *end)
623 {
624 	bool first_run = true;
625 	for (; start != end; start = start->parent) {
626 		if (IsRailStationTile(start->node.tile) && first_run) {
627 			SetRailStationPlatformReservation(start->node.tile, TrackdirToExitdir(start->node.direction), false);
628 		} else {
629 			UnreserveRailTrack(start->node.tile, TrackdirToTrack(start->node.direction));
630 		}
631 		first_run = false;
632 	}
633 }
634 
635 /**
636  * To be called when @p current contains the (shortest route to) the target node.
637  * Will fill the contents of the NPFFoundTargetData using
638  * AyStarNode[NPF_TRACKDIR_CHOICE]. If requested, path reservation
639  * is done here.
640  */
NPFSaveTargetData(AyStar * as,OpenListNode * current)641 static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
642 {
643 	AyStarUserData *user = (AyStarUserData *)as->user_data;
644 	NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
645 	ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
646 	ftd->best_path_dist = current->g;
647 	ftd->best_bird_dist = 0;
648 	ftd->node = current->path.node;
649 	ftd->res_okay = false;
650 
651 	if (as->user_target != nullptr && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && user->type == TRANSPORT_RAIL) {
652 		/* Path reservation is requested. */
653 		const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
654 
655 		const PathNode *target = FindSafePosition(&current->path, v);
656 		ftd->node = target->node;
657 
658 		/* If the target is a station skip to platform end. */
659 		if (IsRailStationTile(target->node.tile)) {
660 			DiagDirection dir = TrackdirToExitdir(target->node.direction);
661 			uint len = Station::GetByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir);
662 			TileIndex end_tile = TILE_ADD(target->node.tile, (len - 1) * TileOffsByDiagDir(dir));
663 
664 			/* Update only end tile, trackdir of a station stays the same. */
665 			ftd->node.tile = end_tile;
666 			if (!IsWaitingPositionFree(v, end_tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
667 			SetRailStationPlatformReservation(target->node.tile, dir, true);
668 			SetRailStationReservation(target->node.tile, false);
669 		} else {
670 			if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
671 		}
672 
673 		for (const PathNode *cur = target; cur->parent != nullptr; cur = cur->parent) {
674 			if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) {
675 				/* Reservation failed, undo. */
676 				ClearPathReservation(target, cur);
677 				return;
678 			}
679 		}
680 
681 		ftd->res_okay = true;
682 	}
683 }
684 
685 /**
686  * Finds out if a given company's vehicles are allowed to enter a given tile.
687  * @param owner    The owner of the vehicle.
688  * @param tile     The tile that is about to be entered.
689  * @param enterdir The direction in which the vehicle wants to enter the tile.
690  * @return         true if the vehicle can enter the tile.
691  * @todo           This function should be used in other places than just NPF,
692  *                 maybe moved to another file too.
693  */
CanEnterTileOwnerCheck(Owner owner,TileIndex tile,DiagDirection enterdir)694 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
695 {
696 	if (IsTileType(tile, MP_RAILWAY) || // Rail tile (also rail depot)
697 			HasStationTileRail(tile) ||     // Rail station tile/waypoint
698 			IsRoadDepotTile(tile) ||        // Road depot tile
699 			IsStandardRoadStopTile(tile)) { // Road station tile (but not drive-through stops)
700 		return IsTileOwner(tile, owner);  // You need to own these tiles entirely to use them
701 	}
702 
703 	switch (GetTileType(tile)) {
704 		case MP_ROAD:
705 			/* rail-road crossing : are we looking at the railway part? */
706 			if (IsLevelCrossing(tile) &&
707 					DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
708 				return IsTileOwner(tile, owner); // Railway needs owner check, while the street is public
709 			}
710 			break;
711 
712 		case MP_TUNNELBRIDGE:
713 			if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
714 				return IsTileOwner(tile, owner);
715 			}
716 			break;
717 
718 		default:
719 			break;
720 	}
721 
722 	return true; // no need to check
723 }
724 
725 
726 /**
727  * Returns the direction the exit of the depot on the given tile is facing.
728  */
GetDepotDirection(TileIndex tile,TransportType type)729 static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
730 {
731 	assert(IsDepotTypeTile(tile, type));
732 
733 	switch (type) {
734 		case TRANSPORT_RAIL:  return GetRailDepotDirection(tile);
735 		case TRANSPORT_ROAD:  return GetRoadDepotDirection(tile);
736 		case TRANSPORT_WATER: return GetShipDepotDirection(tile);
737 		default: return INVALID_DIAGDIR; // Not reached
738 	}
739 }
740 
741 /** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */
GetSingleTramBit(TileIndex tile)742 static DiagDirection GetSingleTramBit(TileIndex tile)
743 {
744 	if (IsNormalRoadTile(tile)) {
745 		RoadBits rb = GetRoadBits(tile, RTT_TRAM);
746 		switch (rb) {
747 			case ROAD_NW: return DIAGDIR_NW;
748 			case ROAD_SW: return DIAGDIR_SW;
749 			case ROAD_SE: return DIAGDIR_SE;
750 			case ROAD_NE: return DIAGDIR_NE;
751 			default: break;
752 		}
753 	}
754 	return INVALID_DIAGDIR;
755 }
756 
757 /**
758  * Tests if a tile can be entered or left only from one side.
759  *
760  * Depots, non-drive-through roadstops, and tiles with single trambits are tested.
761  *
762  * @param tile The tile of interest.
763  * @param type The transporttype of the vehicle.
764  * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
765  * @return The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions
766  */
GetTileSingleEntry(TileIndex tile,TransportType type,uint subtype)767 static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
768 {
769 	if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type);
770 
771 	if (type == TRANSPORT_ROAD) {
772 		if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
773 		if ((RoadTramType)subtype == RTT_TRAM) return GetSingleTramBit(tile);
774 	}
775 
776 	return INVALID_DIAGDIR;
777 }
778 
779 /**
780  * Tests if a vehicle must reverse on a tile.
781  *
782  * @param tile The tile of interest.
783  * @param dir The direction in which the vehicle drives on a tile.
784  * @param type The transporttype of the vehicle.
785  * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
786  * @return true iff the vehicle must reverse on the tile.
787  */
ForceReverse(TileIndex tile,DiagDirection dir,TransportType type,uint subtype)788 static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
789 {
790 	DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
791 	return single_entry != INVALID_DIAGDIR && single_entry != dir;
792 }
793 
794 /**
795  * Tests if a vehicle can enter a tile.
796  *
797  * @param tile The tile of interest.
798  * @param dir The direction in which the vehicle drives onto a tile.
799  * @param user Vehicle information.
800  * @return true iff the vehicle can enter the tile.
801  */
CanEnterTile(TileIndex tile,DiagDirection dir,AyStarUserData * user)802 static bool CanEnterTile(TileIndex tile, DiagDirection dir, AyStarUserData *user)
803 {
804 	/* Check tunnel entries and bridge ramps */
805 	if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
806 
807 	/* Test ownership */
808 	if (!CanEnterTileOwnerCheck(user->owner, tile, dir)) return false;
809 
810 	/* check correct rail type (mono, maglev, etc) */
811 	switch (user->type) {
812 		case TRANSPORT_RAIL: {
813 			RailType rail_type = GetTileRailType(tile);
814 			if (!HasBit(user->railtypes, rail_type)) return false;
815 			break;
816 		}
817 
818 		case TRANSPORT_ROAD: {
819 			RoadType road_type = GetRoadType(tile, (RoadTramType)user->subtype);
820 			if (!HasBit(user->roadtypes, road_type)) return false;
821 			break;
822 		}
823 
824 		default: break;
825 	}
826 
827 	/* Depots, standard roadstops and single tram bits can only be entered from one direction */
828 	DiagDirection single_entry = GetTileSingleEntry(tile, user->type, user->subtype);
829 	return single_entry == INVALID_DIAGDIR || single_entry == ReverseDiagDir(dir);
830 }
831 
832 /**
833  * Returns the driveable Trackdirs on a tile.
834  *
835  * One-way-roads are taken into account. Signals are not tested.
836  *
837  * @param dst_tile The tile of interest.
838  * @param src_tile The originating tile.
839  * @param src_trackdir The direction the vehicle is currently moving.
840  * @param type The transporttype of the vehicle.
841  * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle.
842  * @return The Trackdirs the vehicle can continue moving on.
843  */
GetDriveableTrackdirBits(TileIndex dst_tile,TileIndex src_tile,Trackdir src_trackdir,TransportType type,uint subtype)844 static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, TileIndex src_tile, Trackdir src_trackdir, TransportType type, uint subtype)
845 {
846 	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
847 
848 	if (trackdirbits == TRACKDIR_BIT_NONE && type == TRANSPORT_ROAD && (RoadTramType)subtype == RTT_TRAM) {
849 		/* GetTileTrackStatus() returns 0 for single tram bits.
850 		 * As we cannot change it there (easily) without breaking something, change it here */
851 		switch (GetSingleTramBit(dst_tile)) {
852 			case DIAGDIR_NE:
853 			case DIAGDIR_SW:
854 				trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
855 				break;
856 
857 			case DIAGDIR_NW:
858 			case DIAGDIR_SE:
859 				trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
860 				break;
861 
862 			default: break;
863 		}
864 	}
865 
866 	Debug(npf, 4, "Next node: ({}, {}) [{}], possible trackdirs: 0x{:X}", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
867 
868 	/* Select only trackdirs we can reach from our current trackdir */
869 	trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
870 
871 	/* Filter out trackdirs that would make 90 deg turns for trains */
872 	if (type == TRANSPORT_RAIL && Rail90DegTurnDisallowed(GetTileRailType(src_tile), GetTileRailType(dst_tile))) {
873 		trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
874 	}
875 
876 	Debug(npf, 6, "After filtering: ({}, {}), possible trackdirs: 0x{:X}", TileX(dst_tile), TileY(dst_tile), trackdirbits);
877 
878 	return trackdirbits;
879 }
880 
881 
882 /* Will just follow the results of GetTileTrackStatus concerning where we can
883  * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
884  * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
885  * entry and exit are neighbours. Will fill
886  * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
887  * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
NPFFollowTrack(AyStar * aystar,OpenListNode * current)888 static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
889 {
890 	AyStarUserData *user = (AyStarUserData *)aystar->user_data;
891 	/* We leave src_tile on track src_trackdir in direction src_exitdir */
892 	Trackdir src_trackdir = current->path.node.direction;
893 	TileIndex src_tile = current->path.node.tile;
894 	DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
895 
896 	/* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
897 	TransportType type = user->type;
898 	uint subtype = user->subtype;
899 
900 	/* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
901 	aystar->num_neighbours = 0;
902 	Debug(npf, 4, "Expanding: ({}, {}, {}) [{}]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
903 
904 	/* We want to determine the tile we arrive, and which choices we have there */
905 	TileIndex dst_tile;
906 	TrackdirBits trackdirbits;
907 
908 	/* Find dest tile */
909 	/* Is src_tile valid, and can be used?
910 	 * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
911 	 * 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. */
912 	if (CheckIgnoreFirstTile(&current->path)) {
913 		/* Do not perform any checks that involve src_tile */
914 		dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
915 		trackdirbits = GetDriveableTrackdirBits(dst_tile, src_tile, src_trackdir, type, subtype);
916 	} else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
917 		/* We drive through the wormhole and arrive on the other side */
918 		dst_tile = GetOtherTunnelBridgeEnd(src_tile);
919 		trackdirbits = TrackdirToTrackdirBits(src_trackdir);
920 	} else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
921 		/* We can only reverse on this tile */
922 		dst_tile = src_tile;
923 		src_trackdir = ReverseTrackdir(src_trackdir);
924 		trackdirbits = TrackdirToTrackdirBits(src_trackdir);
925 	} else {
926 		/* We leave src_tile in src_exitdir and reach dst_tile */
927 		dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
928 
929 		if (dst_tile != INVALID_TILE && IsNormalRoadTile(dst_tile) && !CanEnterTile(dst_tile, src_exitdir, user)) dst_tile = INVALID_TILE;
930 
931 		if (dst_tile == INVALID_TILE) {
932 			/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
933 			if (type != TRANSPORT_ROAD || (RoadTramType)subtype == RTT_TRAM) return;
934 
935 			dst_tile = src_tile;
936 			src_trackdir = ReverseTrackdir(src_trackdir);
937 		}
938 
939 		trackdirbits = GetDriveableTrackdirBits(dst_tile, src_tile, src_trackdir, type, subtype);
940 
941 		if (trackdirbits == TRACKDIR_BIT_NONE) {
942 			/* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
943 			if (type != TRANSPORT_ROAD || (RoadTramType)subtype == RTT_TRAM) return;
944 
945 			dst_tile = src_tile;
946 			src_trackdir = ReverseTrackdir(src_trackdir);
947 
948 			trackdirbits = GetDriveableTrackdirBits(dst_tile, src_tile, src_trackdir, type, subtype);
949 		}
950 	}
951 
952 	if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
953 		/* Mask out any reserved tracks. */
954 		TrackBits reserved = GetReservedTrackbits(dst_tile);
955 		trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
956 
957 		for (Track t : SetTrackBitIterator(TrackdirBitsToTrackBits(trackdirbits))) {
958 			if (TracksOverlap(reserved | TrackToTrackBits(t))) trackdirbits &= ~TrackToTrackdirBits(t);
959 		}
960 	}
961 
962 	/* Enumerate possible track */
963 	uint i = 0;
964 	while (trackdirbits != TRACKDIR_BIT_NONE) {
965 		Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
966 		Debug(npf, 5, "Expanded into trackdir: {}, remaining trackdirs: 0x{:X}", dst_trackdir, trackdirbits);
967 
968 		/* Tile with signals? */
969 		if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
970 			if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir))) {
971 				/* If there's a one-way signal not pointing towards us, stop going in this direction. */
972 				break;
973 			}
974 		}
975 		{
976 			/* We've found ourselves a neighbour :-) */
977 			AyStarNode *neighbour = &aystar->neighbours[i];
978 			neighbour->tile = dst_tile;
979 			neighbour->direction = dst_trackdir;
980 			/* Save user data */
981 			neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
982 			NPFFillTrackdirChoice(neighbour, current);
983 		}
984 		i++;
985 	}
986 	aystar->num_neighbours = i;
987 }
988 
989 /*
990  * Plan a route to the specified target (which is checked by target_proc),
991  * from start1 and if not nullptr, from start2 as well. The type of transport we
992  * are checking is in type. reverse_penalty is applied to all routes that
993  * originate from the second start node.
994  * When we are looking for one specific target (optionally multiple tiles), we
995  * should use a good heuristic to perform aystar search. When we search for
996  * multiple targets that are spread around, we should perform a breadth first
997  * search by specifying CalcZero as our heuristic.
998  */
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 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)
1000 {
1001 	/* Initialize procs */
1002 	_npf_aystar.max_path_cost = max_penalty;
1003 	_npf_aystar.CalculateH = heuristic_proc;
1004 	_npf_aystar.EndNodeCheck = target_proc;
1005 	_npf_aystar.FoundEndNode = NPFSaveTargetData;
1006 	_npf_aystar.GetNeighbours = NPFFollowTrack;
1007 	switch (user->type) {
1008 		default: NOT_REACHED();
1009 		case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
1010 		case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
1011 		case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
1012 	}
1013 
1014 	/* Initialize Start Node(s) */
1015 	start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1016 	start1->user_data[NPF_NODE_FLAGS] = 0;
1017 	NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
1018 	NPFSetFlag(start1, NPF_FLAG_IGNORE_RESERVED, ignore_reserved);
1019 	_npf_aystar.AddStartNode(start1, 0);
1020 	if (start2 != nullptr) {
1021 		start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1022 		start2->user_data[NPF_NODE_FLAGS] = 0;
1023 		NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
1024 		NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
1025 		NPFSetFlag(start2, NPF_FLAG_IGNORE_RESERVED, ignore_reserved);
1026 		_npf_aystar.AddStartNode(start2, reverse_penalty);
1027 	}
1028 
1029 	/* Initialize result */
1030 	NPFFoundTargetData result;
1031 	result.best_bird_dist = UINT_MAX;
1032 	result.best_path_dist = UINT_MAX;
1033 	result.best_trackdir  = INVALID_TRACKDIR;
1034 	result.node.tile      = INVALID_TILE;
1035 	result.res_okay       = false;
1036 	_npf_aystar.user_path = &result;
1037 
1038 	/* Initialize target */
1039 	_npf_aystar.user_target = target;
1040 
1041 	/* Initialize user_data */
1042 	_npf_aystar.user_data = user;
1043 
1044 	/* GO! */
1045 	[[maybe_unused]] int r = _npf_aystar.Main();
1046 	assert(r != AYSTAR_STILL_BUSY);
1047 
1048 	if (result.best_bird_dist != 0) {
1049 		if (target != nullptr) {
1050 			Debug(npf, 1, "Could not find route to tile 0x{:X} from 0x{:X}.", target->dest_coords, start1->tile);
1051 		} else {
1052 			/* Assumption: target == nullptr, so we are looking for a depot */
1053 			Debug(npf, 1, "Could not find route to a depot from tile 0x{:X}.", start1->tile);
1054 		}
1055 
1056 	}
1057 	return result;
1058 }
1059 
1060 /* Will search as below, but with two start nodes, the second being the
1061  * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which
1062  * direction was taken (NPFGetFlag(result.node, NPF_FLAG_REVERSE)) */
NPFRouteToStationOrTileTwoWay(TileIndex tile1,Trackdir trackdir1,bool ignore_start_tile1,TileIndex tile2,Trackdir trackdir2,bool ignore_start_tile2,NPFFindStationOrTileData * target,AyStarUserData * user)1063 static NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStarUserData *user)
1064 {
1065 	AyStarNode start1;
1066 	AyStarNode start2;
1067 
1068 	start1.tile = tile1;
1069 	start2.tile = tile2;
1070 	start1.direction = trackdir1;
1071 	start2.direction = trackdir2;
1072 
1073 	return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : nullptr), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, user, 0);
1074 }
1075 
1076 /* Will search from the given tile and direction, for a route to the given
1077  * station for the given transport type. See the declaration of
1078  * NPFFoundTargetData above for the meaning of the result. */
NPFRouteToStationOrTile(TileIndex tile,Trackdir trackdir,bool ignore_start_tile,NPFFindStationOrTileData * target,AyStarUserData * user)1079 static NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, AyStarUserData *user)
1080 {
1081 	return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, user);
1082 }
1083 
1084 /* Search using breadth first. Good for little track choice and inaccurate
1085  * heuristic, such as railway/road with two start nodes, the second being the reverse. Call
1086  * NPFGetFlag(result.node, NPF_FLAG_REVERSE) to see from which node the path
1087  * originated. All paths from the second node will have the given
1088  * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full
1089  * tile).
1090  */
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 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)
1092 {
1093 	AyStarNode start1;
1094 	AyStarNode start2;
1095 
1096 	start1.tile = tile1;
1097 	start2.tile = tile2;
1098 	start1.direction = trackdir1;
1099 	start2.direction = trackdir2;
1100 
1101 	/* perform a breadth first search. Target is nullptr,
1102 	 * since we are just looking for any depot...*/
1103 	return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : nullptr), ignore_start_tile2, target, NPFFindDepot, NPFCalcZero, user, reverse_penalty, false, max_penalty);
1104 }
1105 
InitializeNPF()1106 void InitializeNPF()
1107 {
1108 	static bool first_init = true;
1109 	if (first_init) {
1110 		first_init = false;
1111 		_npf_aystar.Init(NPFHash, NPF_HASH_SIZE);
1112 	} else {
1113 		_npf_aystar.Clear();
1114 	}
1115 	_npf_aystar.loops_per_tick = 0;
1116 	_npf_aystar.max_path_cost = 0;
1117 	//_npf_aystar.max_search_nodes = 0;
1118 	/* We will limit the number of nodes for now, until we have a better
1119 	 * solution to really fix performance */
1120 	_npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes;
1121 }
1122 
NPFFillWithOrderData(NPFFindStationOrTileData * fstd,const Vehicle * v,bool reserve_path=false)1123 static void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, const Vehicle *v, bool reserve_path = false)
1124 {
1125 	/* Ships don't really reach their stations, but the tile in front. So don't
1126 	 * save the station id for ships. For roadvehs we don't store it either,
1127 	 * because multistop depends on vehicles actually reaching the exact
1128 	 * dest_tile, not just any stop of that station.
1129 	 * So only for train orders to stations we fill fstd->station_index, for all
1130 	 * others only dest_coords */
1131 	if (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_WAYPOINT)) {
1132 		fstd->station_index = v->current_order.GetDestination();
1133 		if (v->type == VEH_TRAIN) {
1134 			fstd->station_type = v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT;
1135 		} else if (v->type == VEH_ROAD) {
1136 			fstd->station_type = RoadVehicle::From(v)->IsBus() ? STATION_BUS : STATION_TRUCK;
1137 		} else if (v->type == VEH_SHIP) {
1138 			fstd->station_type = v->current_order.IsType(OT_GOTO_STATION) ? STATION_DOCK : STATION_BUOY;
1139 		}
1140 
1141 		fstd->not_articulated = v->type == VEH_ROAD && !RoadVehicle::From(v)->HasArticulatedPart();
1142 		/* Let's take the closest tile of the station as our target for vehicles */
1143 		fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile, fstd->station_type);
1144 	} else {
1145 		fstd->dest_coords = v->dest_tile;
1146 		fstd->station_index = INVALID_STATION;
1147 	}
1148 	fstd->reserve_path = reserve_path;
1149 	fstd->v = v;
1150 }
1151 
1152 /*** Road vehicles ***/
1153 
NPFRoadVehicleFindNearestDepot(const RoadVehicle * v,int max_penalty)1154 FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty)
1155 {
1156 	Trackdir trackdir = v->GetVehicleTrackdir();
1157 
1158 	AyStarUserData user = { v->owner, TRANSPORT_ROAD, RAILTYPES_NONE, v->compatible_roadtypes, GetRoadTramType(v->roadtype) };
1159 	NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, INVALID_TILE, INVALID_TRACKDIR, false, nullptr, &user, 0, max_penalty);
1160 
1161 	if (ftd.best_bird_dist != 0) return FindDepotData();
1162 
1163 	/* Found target */
1164 	/* Our caller expects a number of tiles, so we just approximate that
1165 	 * number by this. It might not be completely what we want, but it will
1166 	 * work for now :-) We can possibly change this when the old pathfinder
1167 	 * is removed. */
1168 	return FindDepotData(ftd.node.tile, ftd.best_path_dist);
1169 }
1170 
NPFRoadVehicleChooseTrack(const RoadVehicle * v,TileIndex tile,DiagDirection enterdir,bool & path_found)1171 Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, bool &path_found)
1172 {
1173 	NPFFindStationOrTileData fstd;
1174 
1175 	NPFFillWithOrderData(&fstd, v);
1176 	Trackdir trackdir = DiagDirToDiagTrackdir(enterdir);
1177 
1178 	AyStarUserData user = { v->owner, TRANSPORT_ROAD, RAILTYPES_NONE, v->compatible_roadtypes, GetRoadTramType(v->roadtype) };
1179 	NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, &user);
1180 
1181 	assert(ftd.best_trackdir != INVALID_TRACKDIR);
1182 
1183 	/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1184 	 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1185 	 * we did not find our target, but ftd.best_trackdir contains the direction leading
1186 	 * to the tile closest to our target. */
1187 	path_found = (ftd.best_bird_dist == 0);
1188 	return ftd.best_trackdir;
1189 }
1190 
1191 /*** Ships ***/
1192 
NPFShipChooseTrack(const Ship * v,bool & path_found)1193 Track NPFShipChooseTrack(const Ship *v, bool &path_found)
1194 {
1195 	NPFFindStationOrTileData fstd;
1196 	Trackdir trackdir = v->GetVehicleTrackdir();
1197 	assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
1198 
1199 	NPFFillWithOrderData(&fstd, v);
1200 
1201 	AyStarUserData user = { v->owner, TRANSPORT_WATER, RAILTYPES_NONE, ROADTYPES_NONE, 0 };
1202 	NPFFoundTargetData ftd = NPFRouteToStationOrTile(v->tile, trackdir, true, &fstd, &user);
1203 
1204 	assert(ftd.best_trackdir != INVALID_TRACKDIR);
1205 
1206 	/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1207 	 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1208 	 * we did not find our target, but ftd.best_trackdir contains the direction leading
1209 	 * to the tile closest to our target. */
1210 	path_found = (ftd.best_bird_dist == 0);
1211 	return TrackdirToTrack(ftd.best_trackdir);
1212 }
1213 
NPFShipCheckReverse(const Ship * v,Trackdir * best_td)1214 bool NPFShipCheckReverse(const Ship *v, Trackdir *best_td)
1215 {
1216 	NPFFindStationOrTileData fstd;
1217 	NPFFoundTargetData ftd;
1218 
1219 	NPFFillWithOrderData(&fstd, v);
1220 
1221 	Trackdir trackdir = v->GetVehicleTrackdir();
1222 	Trackdir trackdir_rev = ReverseTrackdir(trackdir);
1223 	assert(trackdir != INVALID_TRACKDIR);
1224 	assert(trackdir_rev != INVALID_TRACKDIR);
1225 
1226 	AyStarUserData user = { v->owner, TRANSPORT_WATER, RAILTYPES_NONE, ROADTYPES_NONE, 0 };
1227 	if (best_td != nullptr) {
1228 		DiagDirection entry = ReverseDiagDir(VehicleExitDir(v->direction, v->state));
1229 		TrackdirBits rtds = DiagdirReachesTrackdirs(entry) & TrackStatusToTrackdirBits(GetTileTrackStatus(v->tile, TRANSPORT_WATER, 0, entry));
1230 		Trackdir best = (Trackdir)FindFirstBit2x64(rtds);
1231 		rtds = KillFirstBit(rtds);
1232 		if (rtds == TRACKDIR_BIT_NONE) return false; /* At most one choice. */
1233 		for (; rtds != TRACKDIR_BIT_NONE; rtds = KillFirstBit(rtds)) {
1234 			Trackdir td = (Trackdir)FindFirstBit2x64(rtds);
1235 			ftd = NPFRouteToStationOrTileTwoWay(v->tile, best, false, v->tile, td, false, &fstd, &user);
1236 			if (ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) best = td;
1237 		}
1238 		if (ftd.best_bird_dist == 0) {
1239 			*best_td = best;
1240 			return true;
1241 		}
1242 	} else {
1243 		ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, v->tile, trackdir_rev, false, &fstd, &user);
1244 	}
1245 	/* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
1246 	return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
1247 }
1248 
1249 /*** Trains ***/
1250 
NPFTrainFindNearestDepot(const Train * v,int max_penalty)1251 FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_penalty)
1252 {
1253 	const Train *last = v->Last();
1254 	Trackdir trackdir = v->GetVehicleTrackdir();
1255 	Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
1256 	NPFFindStationOrTileData fstd;
1257 	fstd.v = v;
1258 	fstd.reserve_path = false;
1259 
1260 	assert(trackdir != INVALID_TRACKDIR);
1261 	AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE, 0 };
1262 	NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, &user, NPF_INFINITE_PENALTY, max_penalty);
1263 	if (ftd.best_bird_dist != 0) return FindDepotData();
1264 
1265 	/* Found target */
1266 	/* Our caller expects a number of tiles, so we just approximate that
1267 	 * number by this. It might not be completely what we want, but it will
1268 	 * work for now :-) We can possibly change this when the old pathfinder
1269 	 * is removed. */
1270 	return FindDepotData(ftd.node.tile, ftd.best_path_dist, NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE));
1271 }
1272 
NPFTrainFindNearestSafeTile(const Train * v,TileIndex tile,Trackdir trackdir,bool override_railtype)1273 bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
1274 {
1275 	assert(v->type == VEH_TRAIN);
1276 
1277 	NPFFindStationOrTileData fstd;
1278 	fstd.v = v;
1279 	fstd.reserve_path = true;
1280 
1281 	AyStarNode start1;
1282 	start1.tile = tile;
1283 	start1.direction = trackdir;
1284 
1285 	RailTypes railtypes = v->compatible_railtypes;
1286 	if (override_railtype) railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
1287 
1288 	/* perform a breadth first search. Target is nullptr,
1289 	 * since we are just looking for any safe tile...*/
1290 	AyStarUserData user = { v->owner, TRANSPORT_RAIL, railtypes, ROADTYPES_NONE, 0 };
1291 	return NPFRouteInternal(&start1, true, nullptr, false, &fstd, NPFFindSafeTile, NPFCalcZero, &user, 0, true).res_okay;
1292 }
1293 
NPFTrainCheckReverse(const Train * v)1294 bool NPFTrainCheckReverse(const Train *v)
1295 {
1296 	NPFFindStationOrTileData fstd;
1297 	NPFFoundTargetData ftd;
1298 	const Train *last = v->Last();
1299 
1300 	NPFFillWithOrderData(&fstd, v);
1301 
1302 	Trackdir trackdir = v->GetVehicleTrackdir();
1303 	Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
1304 	assert(trackdir != INVALID_TRACKDIR);
1305 	assert(trackdir_rev != INVALID_TRACKDIR);
1306 
1307 	AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE, 0 };
1308 	ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, &user);
1309 	/* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
1310 	return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
1311 }
1312 
NPFTrainChooseTrack(const Train * v,bool & path_found,bool reserve_track,struct PBSTileInfo * target)1313 Track NPFTrainChooseTrack(const Train *v, bool &path_found, bool reserve_track, struct PBSTileInfo *target)
1314 {
1315 	NPFFindStationOrTileData fstd;
1316 	NPFFillWithOrderData(&fstd, v, reserve_track);
1317 
1318 	PBSTileInfo origin = FollowTrainReservation(v);
1319 	assert(IsValidTrackdir(origin.trackdir));
1320 
1321 	AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE, 0 };
1322 	NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, &user);
1323 
1324 	if (target != nullptr) {
1325 		target->tile = ftd.node.tile;
1326 		target->trackdir = (Trackdir)ftd.node.direction;
1327 		target->okay = ftd.res_okay;
1328 	}
1329 
1330 	assert(ftd.best_trackdir != INVALID_TRACKDIR);
1331 
1332 	/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1333 	 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1334 	 * we did not find our target, but ftd.best_trackdir contains the direction leading
1335 	 * to the tile closest to our target. */
1336 	path_found = (ftd.best_bird_dist == 0);
1337 	/* Discard enterdir information, making it a normal track */
1338 	return TrackdirToTrack(ftd.best_trackdir);
1339 }
1340