# npf.cpp

Go to the documentation of this file.

    00001 /* $Id: npf.cpp 15518 2009-02-19 00:15:36Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "npf.h"
    00007 #include "debug.h"
    00008 #include "landscape.h"
    00009 #include "depot_base.h"
    00010 #include "depot_map.h"
    00011 #include "network/network.h"
    00012 #include "tunnelbridge_map.h"
    00013 #include "functions.h"
    00014 #include "vehicle_base.h"
    00015 #include "tunnelbridge.h"
    00016 #include "pbs.h"
    00017 #include "settings_type.h"
    00018 #include "pathfind.h"
    00019
    00020 static AyStar _npf_aystar;
    00021
    00022 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
    00023  * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
    00024  */
    00025 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
    00026 static const uint _trackdir_length[TRACKDIR_END] = {
    00027   NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
    00028   0, 0,
    00029   NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
    00030 };
    00031
    00038 static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
    00039 {
    00040   const uint dx = Delta(TileX(t0), TileX(t1));
    00041   const uint dy = Delta(TileY(t0), TileY(t1));
    00042
    00043   const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
    00044   /* OPTIMISATION:
    00045    * Original: diagTracks = max(dx, dy) - min(dx,dy);
    00046    * Proof:
    00047    * (dx+dy) - straightTracks  == (min + max) - straightTracks = min + max - 2 * min = max - min */
    00048   const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
    00049
    00050   /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
    00051    * precision */
    00052   return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
    00053 }
    00054
    00055
    00056 #if 0
    00057 static uint NTPHash(uint key1, uint key2)
    00058 {
    00059   /* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
    00060   return PATHFIND_HASH_TILE(key1);
    00061 }
    00062 #endif
    00063
    00071 static uint NPFHash(uint key1, uint key2)
    00072 {
    00073   /* TODO: think of a better hash? */
    00074   uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
    00075   uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
    00076
    00077   assert(IsValidTrackdir((Trackdir)key2));
    00078   assert(IsValidTile(key1));
    00079   return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
    00080 }
    00081
    00082 static int32 NPFCalcZero(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00083 {
    00084   return 0;
    00085 }
    00086
    00087 /* Calcs the heuristic to the target station or tile. For train stations, it
    00088  * takes into account the direction of approach.
    00089  */
    00090 static int32 NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00091 {
    00092   NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
    00093   NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
    00094   TileIndex from = current->tile;
    00095   TileIndex to = fstd->dest_coords;
    00096   uint dist;
    00097
    00098   /* for train-stations, we are going to aim for the closest station tile */
    00099   if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION)
    00100     to = CalcClosestStationTile(fstd->station_index, from);
    00101
    00102   if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
    00103     /* Since roads only have diagonal pieces, we use manhattan distance here */
    00104     dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
    00105   } else {
    00106     /* Ships and trains can also go diagonal, so the minimum distance is shorter */
    00107     dist = NPFDistanceTrack(from, to);
    00108   }
    00109
    00110   DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
    00111
    00112   if (dist < ftd->best_bird_dist) {
    00113     ftd->best_bird_dist = dist;
    00114     ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
    00115   }
    00116   return dist;
    00117 }
    00118
    00119
    00120 /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
    00121  * get here, either getting it from the current choice or from the parent's
    00122  * choice */
    00123 static void NPFFillTrackdirChoice(AyStarNode *current, OpenListNode *parent)
    00124 {
    00125   if (parent->path.parent == NULL) {
    00126     Trackdir trackdir = current->direction;
    00127     /* This is a first order decision, so we'd better save the
    00128      * direction we chose */
    00129     current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
    00130     DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
    00131   } else {
    00132     /* We've already made the decision, so just save our parent's decision */
    00133     current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
    00134   }
    00135 }
    00136
    00137 /* Will return the cost of the tunnel. If it is an entry, it will return the
    00138  * cost of that tile. If the tile is an exit, it will return the tunnel length
    00139  * including the exit tile. Requires that this is a Tunnel tile */
    00140 static uint NPFTunnelCost(AyStarNode *current)
    00141 {
    00142   DiagDirection exitdir = TrackdirToExitdir(current->direction);
    00143   TileIndex tile = current->tile;
    00144   if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
    00145     /* We just popped out if this tunnel, since were
    00146      * facing the tunnel exit */
    00147     return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
    00148     /* @todo: Penalty for tunnels? */
    00149   } else {
    00150     /* We are entering the tunnel, the enter tile is just a
    00151      * straight track */
    00152     return NPF_TILE_LENGTH;
    00153   }
    00154 }
    00155
    00156 static inline uint NPFBridgeCost(AyStarNode *current)
    00157 {
    00158   return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
    00159 }
    00160
    00161 static uint NPFSlopeCost(AyStarNode *current)
    00162 {
    00163   TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir(current->direction));
    00164
    00165   /* Get center of tiles */
    00166   int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
    00167   int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
    00168   int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
    00169   int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
    00170
    00171   int dx4 = (x2 - x1) / 4;
    00172   int dy4 = (y2 - y1) / 4;
    00173
    00174   /* Get the height on both sides of the tile edge.
    00175    * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
    00176    */
    00177   int z1 = GetSlopeZ(x1 + dx4, y1 + dy4);
    00178   int z2 = GetSlopeZ(x2 - dx4, y2 - dy4);
    00179
    00180   if (z2 - z1 > 1) {
    00181     /* Slope up */
    00182     return _settings_game.pf.npf.npf_rail_slope_penalty;
    00183   }
    00184   return 0;
    00185   /* Should we give a bonus for slope down? Probably not, we
    00186    * could just substract that bonus from the penalty, because
    00187    * there is only one level of steepness... */
    00188 }
    00189
    00190 static uint NPFReservedTrackCost(AyStarNode *current)
    00191 {
    00192   TileIndex tile = current->tile;
    00193   TrackBits track = TrackToTrackBits(TrackdirToTrack(current->direction));
    00194   TrackBits res = GetReservedTrackbits(tile);
    00195
    00196   if (NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL) || ((res & track) == TRACK_BIT_NONE && !TracksOverlap(res | track))) return 0;
    00197
    00198   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00199     DiagDirection exitdir = TrackdirToExitdir(current->direction);
    00200     if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
    00201       return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty * (GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile)) + 1);
    00202     }
    00203   }
    00204   return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty;
    00205 }
    00206
    00211 static void NPFMarkTile(TileIndex tile)
    00212 {
    00213 #ifndef NO_DEBUG_MESSAGES
    00214   if (_debug_npf_level < 1 || _networking) return;
    00215   switch (GetTileType(tile)) {
    00216     case MP_RAILWAY:
    00217       /* DEBUG: mark visited tiles by mowing the grass under them ;-) */
    00218       if (!IsRailDepot(tile)) {
    00219         SetRailGroundType(tile, RAIL_GROUND_BARREN);
    00220         MarkTileDirtyByTile(tile);
    00221       }
    00222       break;
    00223
    00224     case MP_ROAD:
    00225       if (!IsRoadDepot(tile)) {
    00226         SetRoadside(tile, ROADSIDE_BARREN);
    00227         MarkTileDirtyByTile(tile);
    00228       }
    00229       break;
    00230
    00231     default:
    00232       break;
    00233   }
    00234 #endif
    00235 }
    00236
    00237 static int32 NPFWaterPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00238 {
    00239   /* TileIndex tile = current->tile; */
    00240   int32 cost = 0;
    00241   Trackdir trackdir = current->direction;
    00242
    00243   cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
    00244
    00245   if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
    00246     cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys
    00247
    00248   if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
    00249     cost += _settings_game.pf.npf.npf_water_curve_penalty;
    00250
    00251   /* @todo More penalties? */
    00252
    00253   return cost;
    00254 }
    00255
    00256 /* Determine the cost of this node, for road tracks */
    00257 static int32 NPFRoadPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00258 {
    00259   TileIndex tile = current->tile;
    00260   int32 cost = 0;
    00261
    00262   /* Determine base length */
    00263   switch (GetTileType(tile)) {
    00264     case MP_TUNNELBRIDGE:
    00265       cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
    00266       break;
    00267
    00268     case MP_ROAD:
    00269       cost = NPF_TILE_LENGTH;
    00270       /* Increase the cost for level crossings */
    00271       if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty;
    00272       break;
    00273
    00274     case MP_STATION:
    00275       cost = NPF_TILE_LENGTH;
    00276       /* Increase the cost for drive-through road stops */
    00277       if (IsDriveThroughStopTile(tile)) cost += _settings_game.pf.npf.npf_road_drive_through_penalty;
    00278       break;
    00279
    00280     default:
    00281       break;
    00282   }
    00283
    00284   /* Determine extra costs */
    00285
    00286   /* Check for slope */
    00287   cost += NPFSlopeCost(current);
    00288
    00289   /* Check for turns. Road vehicles only really drive diagonal, turns are
    00290    * represented by non-diagonal tracks */
    00291   if (!IsDiagonalTrackdir(current->direction))
    00292     cost += _settings_game.pf.npf.npf_road_curve_penalty;
    00293
    00294   NPFMarkTile(tile);
    00295   DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
    00296   return cost;
    00297 }
    00298
    00299
    00300 /* Determine the cost of this node, for railway tracks */
    00301 static int32 NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00302 {
    00303   TileIndex tile = current->tile;
    00304   Trackdir trackdir = current->direction;
    00305   int32 cost = 0;
    00306   /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
    00307   OpenListNode new_node;
    00308
    00309   /* Determine base length */
    00310   switch (GetTileType(tile)) {
    00311     case MP_TUNNELBRIDGE:
    00312       cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
    00313       break;
    00314
    00315     case MP_RAILWAY:
    00316       cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
    00317       break;
    00318
    00319     case MP_ROAD: /* Railway crossing */
    00320       cost = NPF_TILE_LENGTH;
    00321       break;
    00322
    00323     case MP_STATION:
    00324       /* We give a station tile a penalty. Logically we would only want to give
    00325        * station tiles that are not our destination this penalty. This would
    00326        * discourage trains to drive through busy stations. But, we can just
    00327        * give any station tile a penalty, because every possible route will get
    00328        * this penalty exactly once, on its end tile (if it's a station) and it
    00329        * will therefore not make a difference. */
    00330       cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty;
    00331       break;
    00332
    00333     default:
    00334       break;
    00335   }
    00336
    00337   /* Determine extra costs */
    00338
    00339   /* Check for signals */
    00340   if (IsTileType(tile, MP_RAILWAY)) {
    00341     if (HasSignalOnTrackdir(tile, trackdir)) {
    00342       /* Ordinary track with signals */
    00343       if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
    00344         /* Signal facing us is red */
    00345         if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
    00346           /* Penalize the first signal we
    00347            * encounter, if it is red */
    00348
    00349           /* Is this a presignal exit or combo? */
    00350           SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
    00351           if (!IsPbsSignal(sigtype)) {
    00352             if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
    00353               /* Penalise exit and combo signals differently (heavier) */
    00354               cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty;
    00355             } else {
    00356               cost += _settings_game.pf.npf.npf_rail_firstred_penalty;
    00357             }
    00358           }
    00359         }
    00360         /* Record the state of this signal */
    00361         NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
    00362       } else {
    00363         /* Record the state of this signal */
    00364         NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
    00365       }
    00366       if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
    00367         if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) {
    00368           NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true);
    00369         } else {
    00370           NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true);
    00371         }
    00372       } else {
    00373         NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
    00374       }
    00375     }
    00376
    00377     if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) {
    00378       cost += _settings_game.pf.npf.npf_rail_pbs_signal_back_penalty;
    00379     }
    00380   }
    00381
    00382   /* Penalise the tile if it is a target tile and the last signal was
    00383    * red */
    00384   /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
    00385    * of course... */
    00386   new_node.path.node = *current;
    00387   if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
    00388     cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
    00389
    00390   /* Check for slope */
    00391   cost += NPFSlopeCost(current);
    00392
    00393   /* Check for turns */
    00394   if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
    00395     cost += _settings_game.pf.npf.npf_rail_curve_penalty;
    00396   /*TODO, with realistic acceleration, also the amount of straight track between
    00397    *      curves should be taken into account, as this affects the speed limit. */
    00398
    00399   /* Check for reverse in depot */
    00400   if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
    00401     /* Penalise any depot tile that is not the last tile in the path. This
    00402      * _should_ penalise every occurence of reversing in a depot (and only
    00403      * that) */
    00404     cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty;
    00405   }
    00406
    00407   /* Check for occupied track */
    00408   cost += NPFReservedTrackCost(current);
    00409
    00410   NPFMarkTile(tile);
    00411   DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
    00412   return cost;
    00413 }
    00414
    00415 /* Will find any depot */
    00416 static int32 NPFFindDepot(AyStar *as, OpenListNode *current)
    00417 {
    00418   /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
    00419    * since checking the cache not that much faster than the actual check */
    00420   return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
    00421     AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
    00422 }
    00423
    00425 static int32 NPFFindSafeTile(AyStar *as, OpenListNode *current)
    00426 {
    00427   const Vehicle *v = ((NPFFindStationOrTileData*)as->user_target)->v;
    00428
    00429   return
    00430     IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
    00431     IsWaitingPositionFree(v, current->path.node.tile, current->path.node.direction, _settings_game.pf.forbid_90_deg) ?
    00432       AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
    00433 }
    00434
    00435 /* Will find a station identified using the NPFFindStationOrTileData */
    00436 static int32 NPFFindStationOrTile(AyStar *as, OpenListNode *current)
    00437 {
    00438   NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
    00439   AyStarNode *node = &current->path.node;
    00440   TileIndex tile = node->tile;
    00441
    00442   /* If GetNeighbours said we could get here, we assume the station type
    00443    * is correct */
    00444   if (
    00445     (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */
    00446     (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */
    00447   ) {
    00448     return AYSTAR_FOUND_END_NODE;
    00449   } else {
    00450     return AYSTAR_DONE;
    00451   }
    00452 }
    00453
    00461 static const PathNode *FindSafePosition(PathNode *path, const Vehicle *v)
    00462 {
    00463   /* If there is no signal, reserve the whole path. */
    00464   PathNode *sig = path;
    00465
    00466   for(; path->parent != NULL; path = path->parent) {
    00467     if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
    00468       sig = path;
    00469     }
    00470   }
    00471
    00472   return sig;
    00473 }
    00474
    00478 static void ClearPathReservation(const PathNode *start, const PathNode *end)
    00479 {
    00480   bool first_run = true;
    00481   for (; start != end; start = start->parent) {
    00482     if (IsRailwayStationTile(start->node.tile) && first_run) {
    00483       SetRailwayStationPlatformReservation(start->node.tile, TrackdirToExitdir(start->node.direction), false);
    00484     } else {
    00485       UnreserveRailTrack(start->node.tile, TrackdirToTrack(start->node.direction));
    00486     }
    00487     first_run = false;
    00488   }
    00489 }
    00490
    00497 static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
    00498 {
    00499   NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
    00500   ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
    00501   ftd->best_path_dist = current->g;
    00502   ftd->best_bird_dist = 0;
    00503   ftd->node = current->path.node;
    00504   ftd->res_okay = false;
    00505
    00506   if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && as->user_data[NPF_TYPE] == TRANSPORT_RAIL) {
    00507     /* Path reservation is requested. */
    00508     const Vehicle  *v = ((NPFFindStationOrTileData*)as->user_target)->v;
    00509
    00510     const PathNode *target = FindSafePosition(&current->path, v);
    00511     ftd->node = target->node;
    00512
    00513     /* If the target is a station skip to platform end. */
    00514     if (IsRailwayStationTile(target->node.tile)) {
    00515       DiagDirection dir = TrackdirToExitdir(target->node.direction);
    00516       uint len = GetStationByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir);
    00517       TileIndex end_tile = TILE_ADD(target->node.tile, (len - 1) * TileOffsByDiagDir(dir));
    00518
    00519       /* Update only end tile, trackdir of a station stays the same. */
    00520       ftd->node.tile = end_tile;
    00521       if (!IsWaitingPositionFree(v, end_tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
    00522       SetRailwayStationPlatformReservation(target->node.tile, dir, true);
    00523       SetRailwayStationReservation(target->node.tile, false);
    00524     } else {
    00525       if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
    00526     }
    00527
    00528     for (const PathNode *cur = target; cur->parent != NULL; cur = cur->parent) {
    00529       if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) {
    00530         /* Reservation failed, undo. */
    00531         ClearPathReservation(target, cur);
    00532         return;
    00533       }
    00534     }
    00535
    00536     ftd->res_okay = true;
    00537   }
    00538 }
    00539
    00549 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
    00550 {
    00551   if (IsTileType(tile, MP_RAILWAY) ||             /* Rail tile (also rail depot) */
    00552       IsRailwayStationTile(tile) ||   /* Rail station tile */
    00553       IsRoadDepotTile(tile) ||        /* Road depot tile */
    00554       IsStandardRoadStopTile(tile)) { /* Road station tile (but not drive-through stops) */
    00555     return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */
    00556   }
    00557
    00558   switch (GetTileType(tile)) {
    00559     case MP_ROAD:
    00560       /* rail-road crossing : are we looking at the railway part? */
    00561       if (IsLevelCrossing(tile) &&
    00562           DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
    00563         return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */
    00564       }
    00565       break;
    00566
    00567     case MP_TUNNELBRIDGE:
    00568       if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
    00569         return IsTileOwner(tile, owner);
    00570       }
    00571       break;
    00572
    00573     default:
    00574       break;
    00575   }
    00576
    00577   return true; // no need to check
    00578 }
    00579
    00580
    00584 static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
    00585 {
    00586   assert(IsDepotTypeTile(tile, type));
    00587
    00588   switch (type) {
    00589     case TRANSPORT_RAIL:  return GetRailDepotDirection(tile);
    00590     case TRANSPORT_ROAD:  return GetRoadDepotDirection(tile);
    00591     case TRANSPORT_WATER: return GetShipDepotDirection(tile);
    00592     default: return INVALID_DIAGDIR; /* Not reached */
    00593   }
    00594 }
    00595
    00597 static DiagDirection GetSingleTramBit(TileIndex tile)
    00598 {
    00599   if (IsNormalRoadTile(tile)) {
    00600     RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
    00601     switch (rb) {
    00602       case ROAD_NW: return DIAGDIR_NW;
    00603       case ROAD_SW: return DIAGDIR_SW;
    00604       case ROAD_SE: return DIAGDIR_SE;
    00605       case ROAD_NE: return DIAGDIR_NE;
    00606       default: break;
    00607     }
    00608   }
    00609   return INVALID_DIAGDIR;
    00610 }
    00611
    00622 static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
    00623 {
    00624   if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type);
    00625
    00626   if (type == TRANSPORT_ROAD) {
    00627     if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
    00628     if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
    00629   }
    00630
    00631   return INVALID_DIAGDIR;
    00632 }
    00633
    00643 static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
    00644 {
    00645   DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
    00646   return single_entry != INVALID_DIAGDIR && single_entry != dir;
    00647 }
    00648
    00660 static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
    00661 {
    00662   /* Check tunnel entries and bridge ramps */
    00663   if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
    00664
    00665   /* Test ownership */
    00666   if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false;
    00667
    00668   /* check correct rail type (mono, maglev, etc) */
    00669   if (type == TRANSPORT_RAIL) {
    00670     RailType rail_type = GetTileRailType(tile);
    00671     if (!HasBit(railtypes, rail_type)) return false;
    00672   }
    00673
    00674   /* Depots, standard roadstops and single tram bits can only be entered from one direction */
    00675   DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
    00676   if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
    00677
    00678   return true;
    00679 }
    00680
    00692 static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
    00693 {
    00694   TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
    00695
    00696   if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
    00697     /* GetTileTrackStatus() returns 0 for single tram bits.
    00698      * As we cannot change it there (easily) without breaking something, change it here */
    00699     switch (GetSingleTramBit(dst_tile)) {
    00700       case DIAGDIR_NE:
    00701       case DIAGDIR_SW:
    00702         trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
    00703         break;
    00704
    00705       case DIAGDIR_NW:
    00706       case DIAGDIR_SE:
    00707         trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
    00708         break;
    00709
    00710       default: break;
    00711     }
    00712   }
    00713
    00714   DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
    00715
    00716   /* Select only trackdirs we can reach from our current trackdir */
    00717   trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
    00718
    00719   /* Filter out trackdirs that would make 90 deg turns for trains */
    00720   if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
    00721
    00722   DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
    00723
    00724   return trackdirbits;
    00725 }
    00726
    00727
    00728 /* Will just follow the results of GetTileTrackStatus concerning where we can
    00729  * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
    00730  * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
    00731  * entry and exit are neighbours. Will fill
    00732  * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
    00733  * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
    00734 static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
    00735 {
    00736   /* We leave src_tile on track src_trackdir in direction src_exitdir */
    00737   Trackdir src_trackdir = current->path.node.direction;
    00738   TileIndex src_tile = current->path.node.tile;
    00739   DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
    00740
    00741   /* Is src_tile valid, and can be used?
    00742    * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
    00743    * 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. */
    00744   bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_START_TILE));
    00745
    00746   /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
    00747   TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
    00748   uint subtype = aystar->user_data[NPF_SUB_TYPE];
    00749
    00750   /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
    00751   aystar->num_neighbours = 0;
    00752   DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
    00753
    00754   /* We want to determine the tile we arrive, and which choices we have there */
    00755   TileIndex dst_tile;
    00756   TrackdirBits trackdirbits;
    00757
    00758   /* Find dest tile */
    00759   if (ignore_src_tile) {
    00760     /* Do not perform any checks that involve src_tile */
    00761     dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
    00762     trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
    00763   } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
    00764     /* We drive through the wormhole and arrive on the other side */
    00765     dst_tile = GetOtherTunnelBridgeEnd(src_tile);
    00766     trackdirbits = TrackdirToTrackdirBits(src_trackdir);
    00767   } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
    00768     /* We can only reverse on this tile */
    00769     dst_tile = src_tile;
    00770     src_trackdir = ReverseTrackdir(src_trackdir);
    00771     trackdirbits = TrackdirToTrackdirBits(src_trackdir);
    00772   } else {
    00773     /* We leave src_tile in src_exitdir and reach dst_tile */
    00774     dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
    00775
    00776     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;
    00777
    00778     if (dst_tile == INVALID_TILE) {
    00779       /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
    00780       if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
    00781
    00782       dst_tile = src_tile;
    00783       src_trackdir = ReverseTrackdir(src_trackdir);
    00784     }
    00785
    00786     trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
    00787
    00788     if (trackdirbits == 0) {
    00789       /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
    00790       if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
    00791
    00792       dst_tile = src_tile;
    00793       src_trackdir = ReverseTrackdir(src_trackdir);
    00794
    00795       trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
    00796     }
    00797   }
    00798
    00799   if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
    00800     /* Mask out any reserved tracks. */
    00801     TrackBits reserved = GetReservedTrackbits(dst_tile);
    00802     trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
    00803
    00804     uint bits = TrackdirBitsToTrackBits(trackdirbits);
    00805     int i;
    00806     FOR_EACH_SET_BIT(i, bits) {
    00807       if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) trackdirbits &= ~TrackToTrackdirBits((Track)i);
    00808     }
    00809   }
    00810
    00811   /* Enumerate possible track */
    00812   uint i = 0;
    00813   while (trackdirbits != 0) {
    00814     Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
    00815     DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
    00816
    00817     /* Tile with signals? */
    00818     if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
    00819       if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir)))
    00820         /* If there's a one-way signal not pointing towards us, stop going in this direction. */
    00821         break;
    00822     }
    00823     {
    00824       /* We've found ourselves a neighbour :-) */
    00825       AyStarNode *neighbour = &aystar->neighbours[i];
    00826       neighbour->tile = dst_tile;
    00827       neighbour->direction = dst_trackdir;
    00828       /* Save user data */
    00829       neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
    00830       NPFFillTrackdirChoice(neighbour, current);
    00831     }
    00832     i++;
    00833   }
    00834   aystar->num_neighbours = i;
    00835 }
    00836
    00837 /*
    00838  * Plan a route to the specified target (which is checked by target_proc),
    00839  * from start1 and if not NULL, from start2 as well. The type of transport we
    00840  * are checking is in type. reverse_penalty is applied to all routes that
    00841  * originate from the second start node.
    00842  * When we are looking for one specific target (optionally multiple tiles), we
    00843  * should use a good heuristic to perform aystar search. When we search for
    00844  * multiple targets that are spread around, we should perform a breadth first
    00845  * search by specifiying CalcZero as our heuristic.
    00846  */
    00847 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)
    00848 {
    00849   int r;
    00850   NPFFoundTargetData result;
    00851
    00852   /* Initialize procs */
    00853   _npf_aystar.CalculateH = heuristic_proc;
    00854   _npf_aystar.EndNodeCheck = target_proc;
    00855   _npf_aystar.FoundEndNode = NPFSaveTargetData;
    00856   _npf_aystar.GetNeighbours = NPFFollowTrack;
    00857   switch (type) {
    00858     default: NOT_REACHED();
    00859     case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
    00860     case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
    00861     case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
    00862   }
    00863
    00864   /* Initialize Start Node(s) */
    00865   start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00866   start1->user_data[NPF_NODE_FLAGS] = 0;
    00867   NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
    00868   _npf_aystar.addstart(&_npf_aystar, start1, 0);
    00869   if (start2) {
    00870     start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00871     start2->user_data[NPF_NODE_FLAGS] = 0;
    00872     NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
    00873     NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
    00874     _npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
    00875   }
    00876
    00877   /* Initialize result */
    00878   result.best_bird_dist = UINT_MAX;
    00879   result.best_path_dist = UINT_MAX;
    00880   result.best_trackdir  = INVALID_TRACKDIR;
    00881   result.node.tile      = INVALID_TILE;
    00882   result.res_okay       = false;
    00883   _npf_aystar.user_path = &result;
    00884
    00885   /* Initialize target */
    00886   _npf_aystar.user_target = target;
    00887
    00888   /* Initialize user_data */
    00889   _npf_aystar.user_data[NPF_TYPE] = type;
    00890   _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
    00891   _npf_aystar.user_data[NPF_OWNER] = owner;
    00892   _npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
    00893
    00894   /* GO! */
    00895   r = AyStarMain_Main(&_npf_aystar);
    00896   assert(r != AYSTAR_STILL_BUSY);
    00897
    00898   if (result.best_bird_dist != 0) {
    00899     if (target != NULL) {
    00900       DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
    00901     } else {
    00902       /* Assumption: target == NULL, so we are looking for a depot */
    00903       DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
    00904     }
    00905
    00906   }
    00907   return result;
    00908 }
    00909
    00910 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)
    00911 {
    00912   AyStarNode start1;
    00913   AyStarNode start2;
    00914
    00915   start1.tile = tile1;
    00916   start2.tile = tile2;
    00917   /* We set this in case the target is also the start tile, we will just
    00918    * return a not found then */
    00919   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00920   start1.direction = trackdir1;
    00921   start2.direction = trackdir2;
    00922   start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00923
    00924   return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
    00925 }
    00926
    00927 NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
    00928 {
    00929   return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes);
    00930 }
    00931
    00932 NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty)
    00933 {
    00934   AyStarNode start1;
    00935   AyStarNode start2;
    00936
    00937   start1.tile = tile1;
    00938   start2.tile = tile2;
    00939   /* We set this in case the target is also the start tile, we will just
    00940    * return a not found then */
    00941   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00942   start1.direction = trackdir1;
    00943   start2.direction = trackdir2;
    00944   start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00945
    00946   /* perform a breadth first search. Target is NULL,
    00947    * since we are just looking for any depot...*/
    00948   return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
    00949 }
    00950
    00951 NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
    00952 {
    00953   return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, type, sub_type, owner, railtypes, 0);
    00954 }
    00955
    00956 NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
    00957 {
    00958   /* Okay, what we're gonna do. First, we look at all depots, calculate
    00959    * the manhatten distance to get to each depot. We then sort them by
    00960    * distance. We start by trying to plan a route to the closest, then
    00961    * the next closest, etc. We stop when the best route we have found so
    00962    * far, is shorter than the manhattan distance. This will obviously
    00963    * always find the closest depot. It will probably be most efficient
    00964    * for ships, since the heuristic will not be to far off then. I hope.
    00965    */
    00966   Queue depots;
    00967   int r;
    00968   NPFFoundTargetData best_result = {UINT_MAX, UINT_MAX, INVALID_TRACKDIR, {INVALID_TILE, INVALID_TRACKDIR, {0, 0}}, false};
    00969   NPFFoundTargetData result;
    00970   NPFFindStationOrTileData target;
    00971   AyStarNode start;
    00972   Depot *current;
    00973   Depot *depot;
    00974
    00975   init_InsSort(&depots);
    00976   /* Okay, let's find all depots that we can use first */
    00977   FOR_ALL_DEPOTS(depot) {
    00978     /* Check if this is really a valid depot, it is of the needed type and
    00979      * owner */
    00980     if (IsDepotTypeTile(depot->xy, type) && IsTileOwner(depot->xy, owner))
    00981       /* If so, let's add it to the queue, sorted by distance */
    00982       depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
    00983   }
    00984
    00985   /* Now, let's initialise the aystar */
    00986
    00987   /* Initialize procs */
    00988   _npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
    00989   _npf_aystar.EndNodeCheck = NPFFindStationOrTile;
    00990   _npf_aystar.FoundEndNode = NPFSaveTargetData;
    00991   _npf_aystar.GetNeighbours = NPFFollowTrack;
    00992   switch (type) {
    00993     default: NOT_REACHED();
    00994     case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
    00995     case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
    00996     case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
    00997   }
    00998
    00999   /* Initialize target */
    01000   target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
    01001   _npf_aystar.user_target = &target;
    01002
    01003   /* Initialize user_data */
    01004   _npf_aystar.user_data[NPF_TYPE] = type;
    01005   _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
    01006   _npf_aystar.user_data[NPF_OWNER] = owner;
    01007
    01008   /* Initialize Start Node */
    01009   start.tile = tile;
    01010   start.direction = trackdir; /* We will initialize user_data inside the loop below */
    01011
    01012   /* Initialize Result */
    01013   _npf_aystar.user_path = &result;
    01014   best_result.best_path_dist = UINT_MAX;
    01015   best_result.best_bird_dist = UINT_MAX;
    01016
    01017   /* Just iterate the depots in order of increasing distance */
    01018   while ((current = (Depot*)depots.pop(&depots))) {
    01019     /* Check to see if we already have a path shorter than this
    01020      * depot's manhattan distance. HACK: We call DistanceManhattan
    01021      * again, we should probably modify the queue to give us that
    01022      * value... */
    01023     if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
    01024       break;
    01025
    01026     /* Initialize Start Node */
    01027     /* We set this in case the target is also the start tile, we will just
    01028      * return a not found then */
    01029     start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01030     start.user_data[NPF_NODE_FLAGS] = 0;
    01031     NPFSetFlag(&start, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile);
    01032     _npf_aystar.addstart(&_npf_aystar, &start, 0);
    01033
    01034     /* Initialize result */
    01035     result.best_bird_dist = UINT_MAX;
    01036     result.best_path_dist = UINT_MAX;
    01037     result.best_trackdir = INVALID_TRACKDIR;
    01038
    01039     /* Initialize target */
    01040     target.dest_coords = current->xy;
    01041
    01042     /* GO! */
    01043     r = AyStarMain_Main(&_npf_aystar);
    01044     assert(r != AYSTAR_STILL_BUSY);
    01045
    01046     /* This depot is closer */
    01047     if (result.best_path_dist < best_result.best_path_dist)
    01048       best_result = result;
    01049   }
    01050   if (result.best_bird_dist != 0) {
    01051     DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile);
    01052   }
    01053   return best_result;
    01054 }
    01055
    01056 NPFFoundTargetData NPFRouteToSafeTile(const Vehicle *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
    01057 {
    01058   assert(v->type == VEH_TRAIN);
    01059
    01060   NPFFindStationOrTileData fstd;
    01061   fstd.v = v;
    01062   fstd.reserve_path = true;
    01063
    01064   AyStarNode start1;
    01065   start1.tile = tile;
    01066   /* We set this in case the target is also the start tile, we will just
    01067    * return a not found then */
    01068   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01069   start1.direction = trackdir;
    01070   NPFSetFlag(&start1, NPF_FLAG_IGNORE_RESERVED, true);
    01071
    01072   RailTypes railtypes = v->u.rail.compatible_railtypes;
    01073   if (override_railtype) railtypes |= GetRailTypeInfo(v->u.rail.railtype)->compatible_railtypes;
    01074
    01075   /* perform a breadth first search. Target is NULL,
    01076    * since we are just looking for any safe tile...*/
    01077   return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, TRANSPORT_RAIL, 0, v->owner, railtypes, 0);
    01078 }
    01079
    01080 void InitializeNPF()
    01081 {
    01082   static bool first_init = true;
    01083   if (first_init) {
    01084     first_init = false;
    01085     init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
    01086   } else {
    01087     AyStarMain_Clear(&_npf_aystar);
    01088   }
    01089   _npf_aystar.loops_per_tick = 0;
    01090   _npf_aystar.max_path_cost = 0;
    01091   //_npf_aystar.max_search_nodes = 0;
    01092   /* We will limit the number of nodes for now, until we have a better
    01093    * solution to really fix performance */
    01094   _npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes;
    01095 }
    01096
    01097 void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, Vehicle *v, bool reserve_path)
    01098 {
    01099   /* Ships don't really reach their stations, but the tile in front. So don't
    01100    * save the station id for ships. For roadvehs we don't store it either,
    01101    * because multistop depends on vehicles actually reaching the exact
    01102    * dest_tile, not just any stop of that station.
    01103    * So only for train orders to stations we fill fstd->station_index, for all
    01104    * others only dest_coords */
    01105   if (v->current_order.IsType(OT_GOTO_STATION) && v->type == VEH_TRAIN) {
    01106     fstd->station_index = v->current_order.GetDestination();
    01107     /* Let's take the closest tile of the station as our target for trains */
    01108     fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile);
    01109   } else {
    01110     fstd->dest_coords = v->dest_tile;
    01111     fstd->station_index = INVALID_STATION;
    01112   }
    01113   fstd->reserve_path = reserve_path;
    01114   fstd->v = v;
    01115 }