OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

npf.cpp
Go to the documentation of this file.
1 /* $Id: npf.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "../../stdafx.h"
13 #include "../../network/network.h"
14 #include "../../viewport_func.h"
15 #include "../../ship.h"
16 #include "../../roadstop_base.h"
17 #include "../pathfinder_func.h"
18 #include "../pathfinder_type.h"
19 #include "../follow_track.hpp"
20 #include "aystar.h"
21
22 #include "../../safeguards.h"
23
24 static const uint NPF_HASH_BITS = 12;
25 /* Do no change below values */
26 static const uint NPF_HASH_SIZE = 1 << NPF_HASH_BITS;
27 static const uint NPF_HASH_HALFBITS = NPF_HASH_BITS / 2;
28 static const uint NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1;
29
31 struct NPFFindStationOrTileData {
32 TileIndex dest_coords;
33 StationID station_index;
34 bool reserve_path;
35 StationType station_type;
36 bool not_articulated;
37 const Vehicle *v;
38 };
39
41 enum AyStarUserDataType {
42 NPF_TYPE = 0,
43 NPF_SUB_TYPE,
44 NPF_OWNER,
45 NPF_RAILTYPES,
46 };
47
49 enum AyStarNodeUserDataType {
50 NPF_TRACKDIR_CHOICE = 0,
51 NPF_NODE_FLAGS,
52 };
53
55 enum NPFNodeFlag {
56 NPF_FLAG_SEEN_SIGNAL,
57 NPF_FLAG_2ND_SIGNAL,
58 NPF_FLAG_3RD_SIGNAL,
59 NPF_FLAG_REVERSE,
60 NPF_FLAG_LAST_SIGNAL_RED,
61 NPF_FLAG_LAST_SIGNAL_BLOCK,
62 NPF_FLAG_IGNORE_START_TILE,
63 NPF_FLAG_TARGET_RESERVED,
64 NPF_FLAG_IGNORE_RESERVED,
65 };
66
68 struct NPFFoundTargetData {
69 uint best_bird_dist;
70 uint best_path_dist;
71 Trackdir best_trackdir;
72 AyStarNode node;
73 bool res_okay;
74 };
75
76 static AyStar _npf_aystar;
77
78 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
79 * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
80 */
81 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
82 static const uint _trackdir_length[TRACKDIR_END] = {
83 NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
84 0, 0,
85 NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
86 };
87
91 static inline bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag)
92 {
93 return HasBit(node->user_data[NPF_NODE_FLAGS], flag);
94 }
95
99 static inline void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value)
100 {
101 SB(node->user_data[NPF_NODE_FLAGS], flag, 1, value);
102 }
103
110 static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
111 {
112 const uint dx = Delta(TileX(t0), TileX(t1));
113 const uint dy = Delta(TileY(t0), TileY(t1));
114
115 const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
116 /* OPTIMISATION:
117 * Original: diagTracks = max(dx, dy) - min(dx,dy);
118 * Proof:
119 * (dx+dy) - straightTracks == (min + max) - straightTracks = min + max - 2 * min = max - min */
120 const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
121
122 /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
123 * precision */
124 return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
125 }
126
134 static uint NPFHash(uint key1, uint key2)
135 {
136 /* TODO: think of a better hash? */
137 uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
138 uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
139
140 assert(IsValidTrackdir((Trackdir)key2));
141 assert(IsValidTile(key1));
142 return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
143 }
144
145 static int32 NPFCalcZero(AyStar *as, AyStarNode *current, OpenListNode *parent)
146 {
147 return 0;
148 }
149
150 /* Calculates the heuristic to the target station or tile. For train stations, it
151 * takes into account the direction of approach.
152 */
153 static int32 NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, OpenListNode *parent)
154 {
155 NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
156 NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
157 TileIndex from = current->tile;
158 TileIndex to = fstd->dest_coords;
159 uint dist;
160
161 /* for train-stations, we are going to aim for the closest station tile */
162 if (as->user_data[NPF_TYPE] != TRANSPORT_WATER && fstd->station_index != INVALID_STATION) {
163 to = CalcClosestStationTile(fstd->station_index, from, fstd->station_type);
164 }
165
166 if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
167 /* Since roads only have diagonal pieces, we use manhattan distance here */
168 dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
169 } else {
170 /* Ships and trains can also go diagonal, so the minimum distance is shorter */
171 dist = NPFDistanceTrack(from, to);
172 }
173
174 DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
175
176 if (dist < ftd->best_bird_dist) {
177 ftd->best_bird_dist = dist;
178 ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
179 }
180 return dist;
181 }
182
183
184 /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
185 * get here, either getting it from the current choice or from the parent's
186 * choice */
187 static void NPFFillTrackdirChoice(AyStarNode *current, OpenListNode *parent)
188 {
189 if (parent->path.parent == NULL) {
190 Trackdir trackdir = current->direction;
191 /* This is a first order decision, so we'd better save the
192 * direction we chose */
193 current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
194 DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
195 } else {
196 /* We've already made the decision, so just save our parent's decision */
197 current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
198 }
199 }
200
201 /* Will return the cost of the tunnel. If it is an entry, it will return the
202 * cost of that tile. If the tile is an exit, it will return the tunnel length
203 * including the exit tile. Requires that this is a Tunnel tile */
204 static uint NPFTunnelCost(AyStarNode *current)
205 {
206 DiagDirection exitdir = TrackdirToExitdir(current->direction);
207 TileIndex tile = current->tile;
208 if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
209 /* We just popped out if this tunnel, since were
210 * facing the tunnel exit */
211 return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
212 /* @todo: Penalty for tunnels? */
213 } else {
214 /* We are entering the tunnel, the enter tile is just a
215 * straight track */
216 return NPF_TILE_LENGTH;
217 }
218 }
219
220 static inline uint NPFBridgeCost(AyStarNode *current)
221 {
222 return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
223 }
224
225 static uint NPFSlopeCost(AyStarNode *current)
226 {
227 TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir(current->direction));
228
229 /* Get center of tiles */
230 int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
231 int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
232 int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
233 int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
234
235 int dx4 = (x2 - x1) / 4;
236 int dy4 = (y2 - y1) / 4;
237
238 /* Get the height on both sides of the tile edge.
239 * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
240 */
241 int z1 = GetSlopePixelZ(x1 + dx4, y1 + dy4);
242 int z2 = GetSlopePixelZ(x2 - dx4, y2 - dy4);
243
244 if (z2 - z1 > 1) {
245 /* Slope up */
246 return _settings_game.pf.npf.npf_rail_slope_penalty;
247 }
248 return 0;
249 /* Should we give a bonus for slope down? Probably not, we
250 * could just subtract that bonus from the penalty, because
251 * there is only one level of steepness... */
252 }
253
254 static uint NPFReservedTrackCost(AyStarNode *current)
255 {
256 TileIndex tile = current->tile;
257 TrackBits track = TrackToTrackBits(TrackdirToTrack(current->direction));
258 TrackBits res = GetReservedTrackbits(tile);
259
260 if (NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL) || NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK) || ((res & track) == TRACK_BIT_NONE && !TracksOverlap(res | track))) return 0;
261
262 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
263 DiagDirection exitdir = TrackdirToExitdir(current->direction);
264 if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
265 return _settings_game.pf.npf.npf_rail_pbs_cross_penalty * (GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile)) + 1);
266 }
267 }
268 return _settings_game.pf.npf.npf_rail_pbs_cross_penalty;
269 }
270
275 static void NPFMarkTile(TileIndex tile)
276 {
277 #ifndef NO_DEBUG_MESSAGES
278 if (_debug_npf_level < 1 || _networking) return;
279 switch (GetTileType(tile)) {
280 case MP_RAILWAY:
281 /* DEBUG: mark visited tiles by mowing the grass under them ;-) */
282 if (!IsRailDepot(tile)) {
283 SetRailGroundType(tile, RAIL_GROUND_BARREN);
284 MarkTileDirtyByTile(tile);
285 }
286 break;
287
288 case MP_ROAD:
289 if (!IsRoadDepot(tile)) {
290 SetRoadside(tile, ROADSIDE_BARREN);
291 MarkTileDirtyByTile(tile);
292 }
293 break;
294
295 default:
296 break;
297 }
298 #endif
299 }
300
301 static int32 NPFWaterPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
302 {
303 /* TileIndex tile = current->tile; */
304 int32 cost = 0;
305 Trackdir trackdir = current->direction;
306
307 cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
308
309 if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) {
310 cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys
311 }
312
313 if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) {
314 cost += _settings_game.pf.npf.npf_water_curve_penalty;
315 }
316
317 /* @todo More penalties? */
318
319 return cost;
320 }
321
322 /* Determine the cost of this node, for road tracks */
323 static int32 NPFRoadPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
324 {
325 TileIndex tile = current->tile;
326 int32 cost = 0;
327
328 /* Determine base length */
329 switch (GetTileType(tile)) {
330 case MP_TUNNELBRIDGE:
331 cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
332 break;
333
334 case MP_ROAD:
335 cost = NPF_TILE_LENGTH;
336 /* Increase the cost for level crossings */
337 if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty;
338 break;
339
340 case MP_STATION: {
341 cost = NPF_TILE_LENGTH;
342 const RoadStop *rs = RoadStop::GetByTile(tile, GetRoadStopType(tile));
343 if (IsDriveThroughStopTile(tile)) {
344 /* Increase the cost for drive-through road stops */
345 cost += _settings_game.pf.npf.npf_road_drive_through_penalty;
346 DiagDirection dir = TrackdirToExitdir(current->direction);
347 if (!RoadStop::IsDriveThroughRoadStopContinuation(tile, tile - TileOffsByDiagDir(dir))) {
348 /* When we're the first road stop in a 'queue' of them we increase
349 * cost based on the fill percentage of the whole queue. */
350 const RoadStop::Entry *entry = rs->GetEntry(dir);
351 cost += entry->GetOccupied() * _settings_game.pf.npf.npf_road_dt_occupied_penalty / entry->GetLength();
352 }
353 } else {
354 /* Increase cost for filled road stops */
355 cost += _settings_game.pf.npf.npf_road_bay_occupied_penalty * (!rs->IsFreeBay(0) + !rs->IsFreeBay(1)) / 2;
356 }
357 break;
358 }
359
360 default:
361 break;
362 }
363
364 /* Determine extra costs */
365
366 /* Check for slope */
367 cost += NPFSlopeCost(current);
368
369 /* Check for turns. Road vehicles only really drive diagonal, turns are
370 * represented by non-diagonal tracks */
371 if (!IsDiagonalTrackdir(current->direction)) {
372 cost += _settings_game.pf.npf.npf_road_curve_penalty;
373 }
374
375 NPFMarkTile(tile);
376 DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
377 return cost;
378 }
379
380
381 /* Determine the cost of this node, for railway tracks */
382 static int32 NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
383 {
384 TileIndex tile = current->tile;
385 Trackdir trackdir = current->direction;
386 int32 cost = 0;
387 /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
388 OpenListNode new_node;
389
390 /* Determine base length */
391 switch (GetTileType(tile)) {
392 case MP_TUNNELBRIDGE:
393 cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
394 break;
395
396 case MP_RAILWAY:
397 cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
398 break;
399
400 case MP_ROAD: // Railway crossing
401 cost = NPF_TILE_LENGTH;
402 break;
403
404 case MP_STATION:
405 /* We give a station tile a penalty. Logically we would only want to give
406 * station tiles that are not our destination this penalty. This would
407 * discourage trains to drive through busy stations. But, we can just
408 * give any station tile a penalty, because every possible route will get
409 * this penalty exactly once, on its end tile (if it's a station) and it
410 * will therefore not make a difference. */
411 cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty;
412
413 if (IsRailWaypoint(tile)) {
414 NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
415 if (fstd->v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(tile) == fstd->v->current_order.GetDestination()) {
416 /* This waypoint is our destination; maybe this isn't an unreserved
417 * one, so check that and if so see that as the last signal being
418 * red. This way waypoints near stations should work better. */
419 const Train *train = Train::From(fstd->v);
420 CFollowTrackRail ft(train);
421 TileIndex t = tile;
422 Trackdir td = trackdir;
423 while (ft.Follow(t, td)) {
424 assert(t != ft.m_new_tile);
425 t = ft.m_new_tile;
426 if (KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
427 /* We encountered a junction; it's going to be too complex to
428 * handle this perfectly, so just bail out. There is no simple
429 * free path, so try the other possibilities. */
430 td = INVALID_TRACKDIR;
431 break;
432 }
433 td = RemoveFirstTrackdir(&ft.m_new_td_bits);
434 /* If this is a safe waiting position we're done searching for it */
435 if (IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg)) break;
436 }
437 if (td == INVALID_TRACKDIR ||
438 !IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg) ||
439 !IsWaitingPositionFree(train, t, td, _settings_game.pf.forbid_90_deg)) {
440 cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
441 }
442 }
443 }
444 break;
445
446 default:
447 break;
448 }
449
450 /* Determine extra costs */
451
452 /* Check for signals */
453 if (IsTileType(tile, MP_RAILWAY)) {
454 if (HasSignalOnTrackdir(tile, trackdir)) {
455 SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
456 /* Ordinary track with signals */
457 if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
458 /* Signal facing us is red */
459 if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
460 /* Penalize the first signal we
461 * encounter, if it is red */
462
463 /* Is this a presignal exit or combo? */
464 if (!IsPbsSignal(sigtype)) {
465 if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
466 /* Penalise exit and combo signals differently (heavier) */
467 cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty;
468 } else {
469 cost += _settings_game.pf.npf.npf_rail_firstred_penalty;
470 }
471 }
472 }
473 /* Record the state of this signal. Path signals are assumed to
474 * be green as the signal state of them has no meaning for this. */
475 NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, !IsPbsSignal(sigtype));
476 } else {
477 /* Record the state of this signal */
478 NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
479 }
480 if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
481 if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) {
482 NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true);
483 } else {
484 NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true);
485 }
486 } else {
487 NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
488 }
489 NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK, !IsPbsSignal(sigtype));
490 }
491
492 if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) {
493 cost += _settings_game.pf.npf.npf_rail_pbs_signal_back_penalty;
494 }
495 }
496
497 /* Penalise the tile if it is a target tile and the last signal was
498 * red */
499 /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
500 * of course... */
501 new_node.path.node = *current;
502 if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) {
503 cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
504 }
505
506 /* Check for slope */
507 cost += NPFSlopeCost(current);
508
509 /* Check for turns */
510 if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) {
511 cost += _settings_game.pf.npf.npf_rail_curve_penalty;
512 }
513 /* TODO, with realistic acceleration, also the amount of straight track between
514 * curves should be taken into account, as this affects the speed limit. */
515
516 /* Check for reverse in depot */
517 if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
518 /* Penalise any depot tile that is not the last tile in the path. This
519 * _should_ penalise every occurrence of reversing in a depot (and only
520 * that) */
521 cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty;
522 }
523
524 /* Check for occupied track */
525 cost += NPFReservedTrackCost(current);
526
527 NPFMarkTile(tile);
528 DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
529 return cost;
530 }
531
532 /* Will find any depot */
533 static int32 NPFFindDepot(AyStar *as, OpenListNode *current)
534 {
535 /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
536 * since checking the cache not that much faster than the actual check */
537 return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
538 AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
539 }
540
542 static int32 NPFFindSafeTile(AyStar *as, OpenListNode *current)
543 {
544 const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
545
546 return (IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
547 IsWaitingPositionFree(v, current->path.node.tile, current->path.node.direction, _settings_game.pf.forbid_90_deg)) ?
548 AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
549 }
550
551 /* Will find a station identified using the NPFFindStationOrTileData */
552 static int32 NPFFindStationOrTile(AyStar *as, OpenListNode *current)
553 {
554 NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
555 AyStarNode *node = &current->path.node;
556 TileIndex tile = node->tile;
557
558 if (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) return AYSTAR_FOUND_END_NODE;
559
560 if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) {
561 if (fstd->v->type == VEH_TRAIN) return AYSTAR_FOUND_END_NODE;
562
563 assert(fstd->v->type == VEH_ROAD);
564 /* Only if it is a valid station *and* we can stop there */
565 if (GetStationType(tile) == fstd->station_type && (fstd->not_articulated || IsDriveThroughStopTile(tile))) return AYSTAR_FOUND_END_NODE;
566 }
567 return AYSTAR_DONE;
568 }
569
577 static const PathNode *FindSafePosition(PathNode *path, const Train *v)
578 {
579 /* If there is no signal, reserve the whole path. */
580 PathNode *sig = path;
581
582 for (; path->parent != NULL; path = path->parent) {
583 if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
584 sig = path;
585 }
586 }
587
588 return sig;
589 }
590
594 static void ClearPathReservation(const PathNode *start, const PathNode *end)
595 {
596 bool first_run = true;
597 for (; start != end; start = start->parent) {
598 if (IsRailStationTile(start->node.tile) && first_run) {
599 SetRailStationPlatformReservation(start->node.tile, TrackdirToExitdir(start->node.direction), false);
600 } else {
601 UnreserveRailTrack(start->node.tile, TrackdirToTrack(start->node.direction));
602 }
603 first_run = false;
604 }
605 }
606
613 static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
614 {
615 NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
616 ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
617 ftd->best_path_dist = current->g;
618 ftd->best_bird_dist = 0;
619 ftd->node = current->path.node;
620 ftd->res_okay = false;
621
622 if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && as->user_data[NPF_TYPE] == TRANSPORT_RAIL) {
623 /* Path reservation is requested. */
624 const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
625
626 const PathNode *target = FindSafePosition(&current->path, v);
627 ftd->node = target->node;
628
629 /* If the target is a station skip to platform end. */
630 if (IsRailStationTile(target->node.tile)) {
631 DiagDirection dir = TrackdirToExitdir(target->node.direction);
632 uint len = Station::GetByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir);
633 TileIndex end_tile = TILE_ADD(target->node.tile, (len - 1) * TileOffsByDiagDir(dir));
634
635 /* Update only end tile, trackdir of a station stays the same. */
636 ftd->node.tile = end_tile;
637 if (!IsWaitingPositionFree(v, end_tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
638 SetRailStationPlatformReservation(target->node.tile, dir, true);
639 SetRailStationReservation(target->node.tile, false);
640 } else {
641 if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
642 }
643
644 for (const PathNode *cur = target; cur->parent != NULL; cur = cur->parent) {
645 if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) {
646 /* Reservation failed, undo. */
647 ClearPathReservation(target, cur);
648 return;
649 }
650 }
651
652 ftd->res_okay = true;
653 }
654 }
655
665 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
666 {
667 if (IsTileType(tile, MP_RAILWAY) || // Rail tile (also rail depot)
668 HasStationTileRail(tile) || // Rail station tile/waypoint
669 IsRoadDepotTile(tile) || // Road depot tile
670 IsStandardRoadStopTile(tile)) { // Road station tile (but not drive-through stops)
671 return IsTileOwner(tile, owner); // You need to own these tiles entirely to use them
672 }
673
674 switch (GetTileType(tile)) {
675 case MP_ROAD:
676 /* rail-road crossing : are we looking at the railway part? */
677 if (IsLevelCrossing(tile) &&
678 DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
679 return IsTileOwner(tile, owner); // Railway needs owner check, while the street is public
680 }
681 break;
682
683 case MP_TUNNELBRIDGE:
684 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
685 return IsTileOwner(tile, owner);
686 }
687 break;
688
689 default:
690 break;
691 }
692
693 return true; // no need to check
694 }
695
696
700 static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
701 {
702 assert(IsDepotTypeTile(tile, type));
703
704 switch (type) {
705 case TRANSPORT_RAIL: return GetRailDepotDirection(tile);
706 case TRANSPORT_ROAD: return GetRoadDepotDirection(tile);
707 case TRANSPORT_WATER: return GetShipDepotDirection(tile);
708 default: return INVALID_DIAGDIR; // Not reached
709 }
710 }
711
713 static DiagDirection GetSingleTramBit(TileIndex tile)
714 {
715 if (IsNormalRoadTile(tile)) {
716 RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
717 switch (rb) {
718 case ROAD_NW: return DIAGDIR_NW;
719 case ROAD_SW: return DIAGDIR_SW;
720 case ROAD_SE: return DIAGDIR_SE;
721 case ROAD_NE: return DIAGDIR_NE;
722 default: break;
723 }
724 }
725 return INVALID_DIAGDIR;
726 }
727
738 static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
739 {
740 if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type);
741
742 if (type == TRANSPORT_ROAD) {
743 if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
744 if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
745 }
746
747 return INVALID_DIAGDIR;
748 }
749
759 static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
760 {
761 DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
762 return single_entry != INVALID_DIAGDIR && single_entry != dir;
763 }
764
776 static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
777 {
778 /* Check tunnel entries and bridge ramps */
779 if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
780
781 /* Test ownership */
782 if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false;
783
784 /* check correct rail type (mono, maglev, etc) */
785 if (type == TRANSPORT_RAIL) {
786 RailType rail_type = GetTileRailType(tile);
787 if (!HasBit(railtypes, rail_type)) return false;
788 }
789
790 /* Depots, standard roadstops and single tram bits can only be entered from one direction */
791 DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
792 if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
793
794 return true;
795 }
796
808 static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
809 {
810 TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
811
812 if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
813 /* GetTileTrackStatus() returns 0 for single tram bits.
814 * As we cannot change it there (easily) without breaking something, change it here */
815 switch (GetSingleTramBit(dst_tile)) {
816 case DIAGDIR_NE:
817 case DIAGDIR_SW:
818 trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
819 break;
820
821 case DIAGDIR_NW:
822 case DIAGDIR_SE:
823 trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
824 break;
825
826 default: break;
827 }
828 }
829
830 DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
831
832 /* Select only trackdirs we can reach from our current trackdir */
833 trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
834
835 /* Filter out trackdirs that would make 90 deg turns for trains */
836 if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
837
838 DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
839
840 return trackdirbits;
841 }
842
843
844 /* Will just follow the results of GetTileTrackStatus concerning where we can
845 * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
846 * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
847 * entry and exit are neighbours. Will fill
848 * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
849 * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
850 static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
851 {
852 /* We leave src_tile on track src_trackdir in direction src_exitdir */
853 Trackdir src_trackdir = current->path.node.direction;
854 TileIndex src_tile = current->path.node.tile;
855 DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
856
857 /* Is src_tile valid, and can be used?
858 * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
859 * 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. */
860 bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_START_TILE));
861
862 /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
863 TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
864 uint subtype = aystar->user_data[NPF_SUB_TYPE];
865
866 /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
867 aystar->num_neighbours = 0;
868 DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
869
870 /* We want to determine the tile we arrive, and which choices we have there */
871 TileIndex dst_tile;
872 TrackdirBits trackdirbits;
873
874 /* Find dest tile */
875 if (ignore_src_tile) {
876 /* Do not perform any checks that involve src_tile */
877 dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
878 trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
879 } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
880 /* We drive through the wormhole and arrive on the other side */
881 dst_tile = GetOtherTunnelBridgeEnd(src_tile);
882 trackdirbits = TrackdirToTrackdirBits(src_trackdir);
883 } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
884 /* We can only reverse on this tile */
885 dst_tile = src_tile;
886 src_trackdir = ReverseTrackdir(src_trackdir);
887 trackdirbits = TrackdirToTrackdirBits(src_trackdir);
888 } else {
889 /* We leave src_tile in src_exitdir and reach dst_tile */
890 dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
891
892 if (dst_tile != INVALID_TILE && !CanEnterTile(dst_tile, src_exitdir, type, subtype, (RailTypes)aystar->user_data[NPF_RAILTYPES], (Owner)aystar->user_data[NPF_OWNER])) dst_tile = INVALID_TILE;
893
894 if (dst_tile == INVALID_TILE) {
895 /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
896 if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
897
898 dst_tile = src_tile;
899 src_trackdir = ReverseTrackdir(src_trackdir);
900 }
901
902 trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
903
904 if (trackdirbits == 0) {
905 /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
906 if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
907
908 dst_tile = src_tile;
909 src_trackdir = ReverseTrackdir(src_trackdir);
910
911 trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
912 }
913 }
914
915 if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
916 /* Mask out any reserved tracks. */
917 TrackBits reserved = GetReservedTrackbits(dst_tile);
918 trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
919
920 Track t;
921 FOR_EACH_SET_TRACK(t, TrackdirBitsToTrackBits(trackdirbits)) {
922 if (TracksOverlap(reserved | TrackToTrackBits(t))) trackdirbits &= ~TrackToTrackdirBits(t);
923 }
924 }
925
926 /* Enumerate possible track */
927 uint i = 0;
928 while (trackdirbits != 0) {
929 Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
930 DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
931
932 /* Tile with signals? */
933 if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
934 if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir))) {
935 /* If there's a one-way signal not pointing towards us, stop going in this direction. */
936 break;
937 }
938 }
939 {
940 /* We've found ourselves a neighbour :-) */
941 AyStarNode *neighbour = &aystar->neighbours[i];
942 neighbour->tile = dst_tile;
943 neighbour->direction = dst_trackdir;
944 /* Save user data */
945 neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
946 NPFFillTrackdirChoice(neighbour, current);
947 }
948 i++;
949 }
950 aystar->num_neighbours = i;
951 }
952
953 /*
954 * Plan a route to the specified target (which is checked by target_proc),
955 * from start1 and if not NULL, from start2 as well. The type of transport we
956 * are checking is in type. reverse_penalty is applied to all routes that
957 * originate from the second start node.
958 * When we are looking for one specific target (optionally multiple tiles), we
959 * should use a good heuristic to perform aystar search. When we search for
960 * multiple targets that are spread around, we should perform a breadth first
961 * search by specifiying CalcZero as our heuristic.
962 */
963 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, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
964 {
965 int r;
966 NPFFoundTargetData result;
967
968 /* Initialize procs */
969 _npf_aystar.CalculateH = heuristic_proc;
970 _npf_aystar.EndNodeCheck = target_proc;
971 _npf_aystar.FoundEndNode = NPFSaveTargetData;
972 _npf_aystar.GetNeighbours = NPFFollowTrack;
973 switch (type) {
974 default: NOT_REACHED();
975 case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break;
976 case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break;
977 case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
978 }
979
980 /* Initialize Start Node(s) */
981 start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
982 start1->user_data[NPF_NODE_FLAGS] = 0;
983 NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
984 _npf_aystar.AddStartNode(start1, 0);
985 if (start2 != NULL) {
986 start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
987 start2->user_data[NPF_NODE_FLAGS] = 0;
988 NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
989 NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
990 _npf_aystar.AddStartNode(start2, reverse_penalty);
991 }
992
993 /* Initialize result */
994 result.best_bird_dist = UINT_MAX;
995 result.best_path_dist = UINT_MAX;
996 result.best_trackdir = INVALID_TRACKDIR;
997 result.node.tile = INVALID_TILE;
998 result.res_okay = false;
999 _npf_aystar.user_path = &result;
1000
1001 /* Initialize target */
1002 _npf_aystar.user_target = target;
1003
1004 /* Initialize user_data */
1005 _npf_aystar.user_data[NPF_TYPE] = type;
1006 _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
1007 _npf_aystar.user_data[NPF_OWNER] = owner;
1008 _npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
1009
1010 /* GO! */
1011 r = _npf_aystar.Main();
1012 assert(r != AYSTAR_STILL_BUSY);
1013
1014 if (result.best_bird_dist != 0) {
1015 if (target != NULL) {
1016 DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
1017 } else {
1018 /* Assumption: target == NULL, so we are looking for a depot */
1019 DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
1020 }
1021
1022 }
1023 return result;
1024 }
1025
1026 /* Will search as below, but with two start nodes, the second being the
1027 * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which
1028 * direction was taken (NPFGetFlag(result.node, NPF_FLAG_REVERSE)) */
1029 static NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
1030 {
1031 AyStarNode start1;
1032 AyStarNode start2;
1033
1034 start1.tile = tile1;
1035 start2.tile = tile2;
1036 /* We set this in case the target is also the start tile, we will just
1037 * return a not found then */
1038 start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1039 start1.direction = trackdir1;
1040 start2.direction = trackdir2;
1041 start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1042
1043 return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
1044 }
1045
1046 /* Will search from the given tile and direction, for a route to the given
1047 * station for the given transport type. See the declaration of
1048 * NPFFoundTargetData above for the meaning of the result. */
1049 static NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
1050 {
1051 return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes);
1052 }
1053
1054 /* Search using breadth first. Good for little track choice and inaccurate
1055 * heuristic, such as railway/road with two start nodes, the second being the reverse. Call
1056 * NPFGetFlag(result.node, NPF_FLAG_REVERSE) to see from which node the path
1057 * originated. All paths from the second node will have the given
1058 * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full
1059 * tile).
1060 */
1061 static NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
1062 {
1063 AyStarNode start1;
1064 AyStarNode start2;
1065
1066 start1.tile = tile1;
1067 start2.tile = tile2;
1068 /* We set this in case the target is also the start tile, we will just
1069 * return a not found then */
1070 start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1071 start1.direction = trackdir1;
1072 start2.direction = trackdir2;
1073 start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1074
1075 /* perform a breadth first search. Target is NULL,
1076 * since we are just looking for any depot...*/
1077 return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
1078 }
1079
1080 void InitializeNPF()
1081 {
1082 static bool first_init = true;
1083 if (first_init) {
1084 first_init = false;
1085 _npf_aystar.Init(NPFHash, NPF_HASH_SIZE);
1086 } else {
1087 _npf_aystar.Clear();
1088 }
1089 _npf_aystar.loops_per_tick = 0;
1090 _npf_aystar.max_path_cost = 0;
1091 //_npf_aystar.max_search_nodes = 0;
1092 /* We will limit the number of nodes for now, until we have a better
1093 * solution to really fix performance */
1094 _npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes;
1095 }
1096
1097 static void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, const Vehicle *v, bool reserve_path = false)
1098 {
1099 /* Ships don't really reach their stations, but the tile in front. So don't
1100 * save the station id for ships. For roadvehs we don't store it either,
1101 * because multistop depends on vehicles actually reaching the exact
1102 * dest_tile, not just any stop of that station.
1103 * So only for train orders to stations we fill fstd->station_index, for all
1104 * others only dest_coords */
1105 if (v->type != VEH_SHIP && (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_WAYPOINT))) {
1106 assert(v->IsGroundVehicle());
1107 fstd->station_index = v->current_order.GetDestination();
1108 fstd->station_type = (v->type == VEH_TRAIN) ? (v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT) : (RoadVehicle::From(v)->IsBus() ? STATION_BUS : STATION_TRUCK);
1109 fstd->not_articulated = v->type == VEH_ROAD && !RoadVehicle::From(v)->HasArticulatedPart();
1110 /* Let's take the closest tile of the station as our target for vehicles */
1111 fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile, fstd->station_type);
1112 } else {
1113 fstd->dest_coords = v->dest_tile;
1114 fstd->station_index = INVALID_STATION;
1115 }
1116 fstd->reserve_path = reserve_path;
1117 fstd->v = v;
1118 }
1119
1120 /*** Road vehicles ***/
1121
1122 FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty)
1123 {
1124 Trackdir trackdir = v->GetVehicleTrackdir();
1125
1126 NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, v->tile, ReverseTrackdir(trackdir), false, NULL, TRANSPORT_ROAD, v->compatible_roadtypes, v->owner, INVALID_RAILTYPES, 0);
1127
1128 if (ftd.best_bird_dist != 0) return FindDepotData();
1129
1130 /* Found target */
1131 /* Our caller expects a number of tiles, so we just approximate that
1132 * number by this. It might not be completely what we want, but it will
1133 * work for now :-) We can possibly change this when the old pathfinder
1134 * is removed. */
1135 return FindDepotData(ftd.node.tile, ftd.best_path_dist);
1136 }
1137
1138 Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs, bool &path_found)
1139 {
1140 NPFFindStationOrTileData fstd;
1141
1142 NPFFillWithOrderData(&fstd, v);
1143 Trackdir trackdir = DiagDirToDiagTrackdir(enterdir);
1144
1145 NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_ROAD, v->compatible_roadtypes, v->owner, INVALID_RAILTYPES);
1146 if (ftd.best_trackdir == INVALID_TRACKDIR) {
1147 /* We are already at our target. Just do something
1148 * @todo: maybe display error?
1149 * @todo: go straight ahead if possible? */
1150 path_found = true;
1151 return (Trackdir)FindFirstBit2x64(trackdirs);
1152 }
1153
1154 /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1155 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1156 * we did not find our target, but ftd.best_trackdir contains the direction leading
1157 * to the tile closest to our target. */
1158 path_found = (ftd.best_bird_dist == 0);
1159 return ftd.best_trackdir;
1160 }
1161
1162 /*** Ships ***/
1163
1164 Track NPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found)
1165 {
1166 NPFFindStationOrTileData fstd;
1167 Trackdir trackdir = v->GetVehicleTrackdir();
1168 assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
1169
1170 NPFFillWithOrderData(&fstd, v);
1171
1172 NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
1173
1174 /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1175 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1176 * we did not find our target, but ftd.best_trackdir contains the direction leading
1177 * to the tile closest to our target. */
1178 path_found = (ftd.best_bird_dist == 0);
1179 if (ftd.best_trackdir == 0xff) return INVALID_TRACK;
1180 return TrackdirToTrack(ftd.best_trackdir);
1181 }
1182
1183 bool NPFShipCheckReverse(const Ship *v)
1184 {
1185 NPFFindStationOrTileData fstd;
1186 NPFFoundTargetData ftd;
1187
1188 NPFFillWithOrderData(&fstd, v);
1189
1190 Trackdir trackdir = v->GetVehicleTrackdir();
1191 Trackdir trackdir_rev = ReverseTrackdir(trackdir);
1192 assert(trackdir != INVALID_TRACKDIR);
1193 assert(trackdir_rev != INVALID_TRACKDIR);
1194
1195 ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, v->tile, trackdir_rev, false, &fstd, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
1196 /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
1197 return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
1198 }
1199
1200 /*** Trains ***/
1201
1202 FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_penalty)
1203 {
1204 const Train *last = v->Last();
1205 Trackdir trackdir = v->GetVehicleTrackdir();
1206 Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
1207 NPFFindStationOrTileData fstd;
1208 fstd.v = v;
1209 fstd.reserve_path = false;
1210
1211 assert(trackdir != INVALID_TRACKDIR);
1212 NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes, NPF_INFINITE_PENALTY);
1213 if (ftd.best_bird_dist != 0) return FindDepotData();
1214
1215 /* Found target */
1216 /* Our caller expects a number of tiles, so we just approximate that
1217 * number by this. It might not be completely what we want, but it will
1218 * work for now :-) We can possibly change this when the old pathfinder
1219 * is removed. */
1220 return FindDepotData(ftd.node.tile, ftd.best_path_dist, NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE));
1221 }
1222
1223 bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
1224 {
1225 assert(v->type == VEH_TRAIN);
1226
1227 NPFFindStationOrTileData fstd;
1228 fstd.v = v;
1229 fstd.reserve_path = true;
1230
1231 AyStarNode start1;
1232 start1.tile = tile;
1233 /* We set this in case the target is also the start tile, we will just
1234 * return a not found then */
1235 start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
1236 start1.direction = trackdir;
1237 NPFSetFlag(&start1, NPF_FLAG_IGNORE_RESERVED, true);
1238
1239 RailTypes railtypes = v->compatible_railtypes;
1240 if (override_railtype) railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
1241
1242 /* perform a breadth first search. Target is NULL,
1243 * since we are just looking for any safe tile...*/
1244 return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, TRANSPORT_RAIL, 0, v->owner, railtypes, 0).res_okay;
1245 }
1246
1247 bool NPFTrainCheckReverse(const Train *v)
1248 {
1249 NPFFindStationOrTileData fstd;
1250 NPFFoundTargetData ftd;
1251 const Train *last = v->Last();
1252
1253 NPFFillWithOrderData(&fstd, v);
1254
1255 Trackdir trackdir = v->GetVehicleTrackdir();
1256 Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
1257 assert(trackdir != INVALID_TRACKDIR);
1258 assert(trackdir_rev != INVALID_TRACKDIR);
1259
1260 ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes);
1261 /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
1262 return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
1263 }
1264
1265 Track NPFTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, struct PBSTileInfo *target)
1266 {
1267 NPFFindStationOrTileData fstd;
1268 NPFFillWithOrderData(&fstd, v, reserve_track);
1269
1270 PBSTileInfo origin = FollowTrainReservation(v);
1271 assert(IsValidTrackdir(origin.trackdir));
1272
1273 NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes);
1274
1275 if (target != NULL) {
1276 target->tile = ftd.node.tile;
1277 target->trackdir = (Trackdir)ftd.node.direction;
1278 target->okay = ftd.res_okay;
1279 }
1280
1281 if (ftd.best_trackdir == INVALID_TRACKDIR) {
1282 /* We are already at our target. Just do something
1283 * @todo maybe display error?
1284 * @todo: go straight ahead if possible? */
1285 path_found = true;
1286 return FindFirstTrack(tracks);
1287 }
1288
1289 /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
1290 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
1291 * we did not find our target, but ftd.best_trackdir contains the direction leading
1292 * to the tile closest to our target. */
1293 path_found = (ftd.best_bird_dist == 0);
1294 /* Discard enterdir information, making it a normal track */
1295 return TrackdirToTrack(ftd.best_trackdir);
1296 }