# npf.cpp

Go to the documentation of this file.

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