# npf.cpp

Go to the documentation of this file.

    00001 /* $Id: npf.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013 #include "../../debug.h"
    00014 #include "../../landscape.h"
    00015 #include "../../network/network.h"
    00016 #include "../../functions.h"
    00017 #include "../../ship.h"
    00018 #include "../../roadstop_base.h"
    00019 #include "../pathfinder_func.h"
    00020 #include "../pathfinder_type.h"
    00021 #include "../follow_track.hpp"
    00022 #include "aystar.h"
    00023
    00024 enum {
    00025   NPF_HASH_BITS = 12,
    00026   /* Do no change below values */
    00027   NPF_HASH_SIZE = 1 << NPF_HASH_BITS,
    00028   NPF_HASH_HALFBITS = NPF_HASH_BITS / 2,
    00029   NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1
    00030 };
    00031
    00032 /* Meant to be stored in AyStar.targetdata */
    00033 struct NPFFindStationOrTileData {
    00034   TileIndex dest_coords;
    00035   StationID station_index;
    00036   bool reserve_path;
    00037   StationType station_type;
    00038   bool not_articulated;
    00039   const Vehicle *v;
    00040 };
    00041
    00042 /* Indices into AyStar.userdata[] */
    00043 enum {
    00044   NPF_TYPE = 0,
    00045   NPF_SUB_TYPE,
    00046   NPF_OWNER,
    00047   NPF_RAILTYPES,
    00048 };
    00049
    00050 /* Indices into AyStarNode.userdata[] */
    00051 enum {
    00052   NPF_TRACKDIR_CHOICE = 0,
    00053   NPF_NODE_FLAGS,
    00054 };
    00055
    00056 /* Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFSetFlag() and NPFGetFlag() to use them. */
    00057 enum NPFNodeFlag {
    00058   NPF_FLAG_SEEN_SIGNAL,
    00059   NPF_FLAG_2ND_SIGNAL,
    00060   NPF_FLAG_3RD_SIGNAL,
    00061   NPF_FLAG_REVERSE,
    00062   NPF_FLAG_LAST_SIGNAL_RED,
    00063   NPF_FLAG_LAST_SIGNAL_BLOCK,
    00064   NPF_FLAG_IGNORE_START_TILE,
    00065   NPF_FLAG_TARGET_RESERVED,
    00066   NPF_FLAG_IGNORE_RESERVED,
    00067 };
    00068
    00069 /* Meant to be stored in AyStar.userpath */
    00070 struct NPFFoundTargetData {
    00071   uint best_bird_dist;
    00072   uint best_path_dist;
    00073   Trackdir best_trackdir;
    00074   AyStarNode node;
    00075   bool res_okay;
    00076 };
    00077
    00078 static AyStar _npf_aystar;
    00079
    00080 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
    00081  * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
    00082  */
    00083 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
    00084 static const uint _trackdir_length[TRACKDIR_END] = {
    00085   NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
    00086   0, 0,
    00087   NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
    00088 };
    00089
    00093 static inline bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag)
    00094 {
    00095   return HasBit(node->user_data[NPF_NODE_FLAGS], flag);
    00096 }
    00097
    00101 static inline void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value)
    00102 {
    00103   SB(node->user_data[NPF_NODE_FLAGS], flag, 1, value);
    00104 }
    00105
    00112 static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
    00113 {
    00114   const uint dx = Delta(TileX(t0), TileX(t1));
    00115   const uint dy = Delta(TileY(t0), TileY(t1));
    00116
    00117   const uint straightTracks = 2 * min(dx, dy); // The number of straight (not full length) tracks
    00118   /* OPTIMISATION:
    00119    * Original: diagTracks = max(dx, dy) - min(dx,dy);
    00120    * Proof:
    00121    * (dx+dy) - straightTracks  == (min + max) - straightTracks = min + max - 2 * min = max - min */
    00122   const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks.
    00123
    00124   /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
    00125    * precision */
    00126   return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
    00127 }
    00128
    00129
    00130 #if 0
    00131 static uint NTPHash(uint key1, uint key2)
    00132 {
    00133   /* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
    00134   return PATHFIND_HASH_TILE(key1);
    00135 }
    00136 #endif
    00137
    00145 static uint NPFHash(uint key1, uint key2)
    00146 {
    00147   /* TODO: think of a better hash? */
    00148   uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
    00149   uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
    00150
    00151   assert(IsValidTrackdir((Trackdir)key2));
    00152   assert(IsValidTile(key1));
    00153   return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
    00154 }
    00155
    00156 static int32 NPFCalcZero(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00157 {
    00158   return 0;
    00159 }
    00160
    00161 /* Calcs the heuristic to the target station or tile. For train stations, it
    00162  * takes into account the direction of approach.
    00163  */
    00164 static int32 NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00165 {
    00166   NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
    00167   NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
    00168   TileIndex from = current->tile;
    00169   TileIndex to = fstd->dest_coords;
    00170   uint dist;
    00171
    00172   /* for train-stations, we are going to aim for the closest station tile */
    00173   if (as->user_data[NPF_TYPE] != TRANSPORT_WATER && fstd->station_index != INVALID_STATION)
    00174     to = CalcClosestStationTile(fstd->station_index, from, fstd->station_type);
    00175
    00176   if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
    00177     /* Since roads only have diagonal pieces, we use manhattan distance here */
    00178     dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
    00179   } else {
    00180     /* Ships and trains can also go diagonal, so the minimum distance is shorter */
    00181     dist = NPFDistanceTrack(from, to);
    00182   }
    00183
    00184   DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
    00185
    00186   if (dist < ftd->best_bird_dist) {
    00187     ftd->best_bird_dist = dist;
    00188     ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
    00189   }
    00190   return dist;
    00191 }
    00192
    00193
    00194 /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
    00195  * get here, either getting it from the current choice or from the parent's
    00196  * choice */
    00197 static void NPFFillTrackdirChoice(AyStarNode *current, OpenListNode *parent)
    00198 {
    00199   if (parent->path.parent == NULL) {
    00200     Trackdir trackdir = current->direction;
    00201     /* This is a first order decision, so we'd better save the
    00202      * direction we chose */
    00203     current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
    00204     DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
    00205   } else {
    00206     /* We've already made the decision, so just save our parent's decision */
    00207     current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
    00208   }
    00209 }
    00210
    00211 /* Will return the cost of the tunnel. If it is an entry, it will return the
    00212  * cost of that tile. If the tile is an exit, it will return the tunnel length
    00213  * including the exit tile. Requires that this is a Tunnel tile */
    00214 static uint NPFTunnelCost(AyStarNode *current)
    00215 {
    00216   DiagDirection exitdir = TrackdirToExitdir(current->direction);
    00217   TileIndex tile = current->tile;
    00218   if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
    00219     /* We just popped out if this tunnel, since were
    00220      * facing the tunnel exit */
    00221     return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
    00222     /* @todo: Penalty for tunnels? */
    00223   } else {
    00224     /* We are entering the tunnel, the enter tile is just a
    00225      * straight track */
    00226     return NPF_TILE_LENGTH;
    00227   }
    00228 }
    00229
    00230 static inline uint NPFBridgeCost(AyStarNode *current)
    00231 {
    00232   return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
    00233 }
    00234
    00235 static uint NPFSlopeCost(AyStarNode *current)
    00236 {
    00237   TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir(current->direction));
    00238
    00239   /* Get center of tiles */
    00240   int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
    00241   int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
    00242   int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
    00243   int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
    00244
    00245   int dx4 = (x2 - x1) / 4;
    00246   int dy4 = (y2 - y1) / 4;
    00247
    00248   /* Get the height on both sides of the tile edge.
    00249    * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
    00250    */
    00251   int z1 = GetSlopeZ(x1 + dx4, y1 + dy4);
    00252   int z2 = GetSlopeZ(x2 - dx4, y2 - dy4);
    00253
    00254   if (z2 - z1 > 1) {
    00255     /* Slope up */
    00256     return _settings_game.pf.npf.npf_rail_slope_penalty;
    00257   }
    00258   return 0;
    00259   /* Should we give a bonus for slope down? Probably not, we
    00260    * could just substract that bonus from the penalty, because
    00261    * there is only one level of steepness... */
    00262 }
    00263
    00264 static uint NPFReservedTrackCost(AyStarNode *current)
    00265 {
    00266   TileIndex tile = current->tile;
    00267   TrackBits track = TrackToTrackBits(TrackdirToTrack(current->direction));
    00268   TrackBits res = GetReservedTrackbits(tile);
    00269
    00270   if (NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL) || NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK) || ((res & track) == TRACK_BIT_NONE && !TracksOverlap(res | track))) return 0;
    00271
    00272   if (IsTileType(tile, MP_TUNNELBRIDGE)) {
    00273     DiagDirection exitdir = TrackdirToExitdir(current->direction);
    00274     if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
    00275       return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty * (GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile)) + 1);
    00276     }
    00277   }
    00278   return  _settings_game.pf.npf.npf_rail_pbs_cross_penalty;
    00279 }
    00280
    00285 static void NPFMarkTile(TileIndex tile)
    00286 {
    00287 #ifndef NO_DEBUG_MESSAGES
    00288   if (_debug_npf_level < 1 || _networking) return;
    00289   switch (GetTileType(tile)) {
    00290     case MP_RAILWAY:
    00291       /* DEBUG: mark visited tiles by mowing the grass under them ;-) */
    00292       if (!IsRailDepot(tile)) {
    00293         SetRailGroundType(tile, RAIL_GROUND_BARREN);
    00294         MarkTileDirtyByTile(tile);
    00295       }
    00296       break;
    00297
    00298     case MP_ROAD:
    00299       if (!IsRoadDepot(tile)) {
    00300         SetRoadside(tile, ROADSIDE_BARREN);
    00301         MarkTileDirtyByTile(tile);
    00302       }
    00303       break;
    00304
    00305     default:
    00306       break;
    00307   }
    00308 #endif
    00309 }
    00310
    00311 static int32 NPFWaterPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00312 {
    00313   /* TileIndex tile = current->tile; */
    00314   int32 cost = 0;
    00315   Trackdir trackdir = current->direction;
    00316
    00317   cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
    00318
    00319   if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
    00320     cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys
    00321
    00322   if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
    00323     cost += _settings_game.pf.npf.npf_water_curve_penalty;
    00324
    00325   /* @todo More penalties? */
    00326
    00327   return cost;
    00328 }
    00329
    00330 /* Determine the cost of this node, for road tracks */
    00331 static int32 NPFRoadPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00332 {
    00333   TileIndex tile = current->tile;
    00334   int32 cost = 0;
    00335
    00336   /* Determine base length */
    00337   switch (GetTileType(tile)) {
    00338     case MP_TUNNELBRIDGE:
    00339       cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
    00340       break;
    00341
    00342     case MP_ROAD:
    00343       cost = NPF_TILE_LENGTH;
    00344       /* Increase the cost for level crossings */
    00345       if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty;
    00346       break;
    00347
    00348     case MP_STATION: {
    00349       cost = NPF_TILE_LENGTH;
    00350       const RoadStop *rs = RoadStop::GetByTile(tile, GetRoadStopType(tile));
    00351       if (IsDriveThroughStopTile(tile)) {
    00352         /* Increase the cost for drive-through road stops */
    00353         cost += _settings_game.pf.npf.npf_road_drive_through_penalty;
    00354         DiagDirection dir = TrackdirToExitdir(current->direction);
    00355         if (!RoadStop::IsDriveThroughRoadStopContinuation(tile, tile - TileOffsByDiagDir(dir))) {
    00356           /* When we're the first road stop in a 'queue' of them we increase
    00357            * cost based on the fill percentage of the whole queue. */
    00358           const RoadStop::Entry *entry = rs->GetEntry(dir);
    00359           cost += entry->GetOccupied() * _settings_game.pf.npf.npf_road_dt_occupied_penalty / entry->GetLength();
    00360         }
    00361       } else {
    00362         /* Increase cost for filled road stops */
    00363         cost += _settings_game.pf.npf.npf_road_bay_occupied_penalty * (!rs->IsFreeBay(0) + !rs->IsFreeBay(1)) / 2;
    00364       }
    00365     } break;
    00366
    00367     default:
    00368       break;
    00369   }
    00370
    00371   /* Determine extra costs */
    00372
    00373   /* Check for slope */
    00374   cost += NPFSlopeCost(current);
    00375
    00376   /* Check for turns. Road vehicles only really drive diagonal, turns are
    00377    * represented by non-diagonal tracks */
    00378   if (!IsDiagonalTrackdir(current->direction))
    00379     cost += _settings_game.pf.npf.npf_road_curve_penalty;
    00380
    00381   NPFMarkTile(tile);
    00382   DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
    00383   return cost;
    00384 }
    00385
    00386
    00387 /* Determine the cost of this node, for railway tracks */
    00388 static int32 NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent)
    00389 {
    00390   TileIndex tile = current->tile;
    00391   Trackdir trackdir = current->direction;
    00392   int32 cost = 0;
    00393   /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
    00394   OpenListNode new_node;
    00395
    00396   /* Determine base length */
    00397   switch (GetTileType(tile)) {
    00398     case MP_TUNNELBRIDGE:
    00399       cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
    00400       break;
    00401
    00402     case MP_RAILWAY:
    00403       cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
    00404       break;
    00405
    00406     case MP_ROAD: // Railway crossing
    00407       cost = NPF_TILE_LENGTH;
    00408       break;
    00409
    00410     case MP_STATION:
    00411       /* We give a station tile a penalty. Logically we would only want to give
    00412        * station tiles that are not our destination this penalty. This would
    00413        * discourage trains to drive through busy stations. But, we can just
    00414        * give any station tile a penalty, because every possible route will get
    00415        * this penalty exactly once, on its end tile (if it's a station) and it
    00416        * will therefore not make a difference. */
    00417       cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty;
    00418
    00419       if (IsRailWaypoint(tile)) {
    00420         NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
    00421         if (fstd->v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(tile) == fstd->v->current_order.GetDestination()) {
    00422           /* This waypoint is our destination; maybe this isn't an unreserved
    00423            * one, so check that and if so see that as the last signal being
    00424            * red. This way waypoints near stations should work better. */
    00425           const Train *train = Train::From(fstd->v);
    00426           CFollowTrackRail ft(train);
    00427           TileIndex t = tile;
    00428           Trackdir td = trackdir;
    00429           while (ft.Follow(t, td)) {
    00430             assert(t != ft.m_new_tile);
    00431             t = ft.m_new_tile;
    00432             if (KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
    00433               /* We encountered a junction; it's going to be too complex to
    00434                * handle this perfectly, so just bail out. There is no simple
    00435                * free path, so try the other possibilities. */
    00436               td = INVALID_TRACKDIR;
    00437               break;
    00438             }
    00439             td = RemoveFirstTrackdir(&ft.m_new_td_bits);
    00440             /* If this is a safe waiting position we're done searching for it */
    00441             if (IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg)) break;
    00442           }
    00443           if (td == INVALID_TRACKDIR ||
    00444               !IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg) ||
    00445               !IsWaitingPositionFree(train, t, td, _settings_game.pf.forbid_90_deg)) {
    00446             cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
    00447           }
    00448         }
    00449       }
    00450       break;
    00451
    00452     default:
    00453       break;
    00454   }
    00455
    00456   /* Determine extra costs */
    00457
    00458   /* Check for signals */
    00459   if (IsTileType(tile, MP_RAILWAY)) {
    00460     if (HasSignalOnTrackdir(tile, trackdir)) {
    00461       SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
    00462       /* Ordinary track with signals */
    00463       if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
    00464         /* Signal facing us is red */
    00465         if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
    00466           /* Penalize the first signal we
    00467            * encounter, if it is red */
    00468
    00469           /* Is this a presignal exit or combo? */
    00470           if (!IsPbsSignal(sigtype)) {
    00471             if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
    00472               /* Penalise exit and combo signals differently (heavier) */
    00473               cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty;
    00474             } else {
    00475               cost += _settings_game.pf.npf.npf_rail_firstred_penalty;
    00476             }
    00477           }
    00478         }
    00479         /* Record the state of this signal */
    00480         NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
    00481       } else {
    00482         /* Record the state of this signal */
    00483         NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
    00484       }
    00485       if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
    00486         if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) {
    00487           NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true);
    00488         } else {
    00489           NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true);
    00490         }
    00491       } else {
    00492         NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
    00493       }
    00494       NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK, !IsPbsSignal(sigtype));
    00495     }
    00496
    00497     if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) {
    00498       cost += _settings_game.pf.npf.npf_rail_pbs_signal_back_penalty;
    00499     }
    00500   }
    00501
    00502   /* Penalise the tile if it is a target tile and the last signal was
    00503    * red */
    00504   /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
    00505    * of course... */
    00506   new_node.path.node = *current;
    00507   if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
    00508     cost += _settings_game.pf.npf.npf_rail_lastred_penalty;
    00509
    00510   /* Check for slope */
    00511   cost += NPFSlopeCost(current);
    00512
    00513   /* Check for turns */
    00514   if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
    00515     cost += _settings_game.pf.npf.npf_rail_curve_penalty;
    00516   /* TODO, with realistic acceleration, also the amount of straight track between
    00517    *      curves should be taken into account, as this affects the speed limit. */
    00518
    00519   /* Check for reverse in depot */
    00520   if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
    00521     /* Penalise any depot tile that is not the last tile in the path. This
    00522      * _should_ penalise every occurence of reversing in a depot (and only
    00523      * that) */
    00524     cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty;
    00525   }
    00526
    00527   /* Check for occupied track */
    00528   cost += NPFReservedTrackCost(current);
    00529
    00530   NPFMarkTile(tile);
    00531   DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
    00532   return cost;
    00533 }
    00534
    00535 /* Will find any depot */
    00536 static int32 NPFFindDepot(AyStar *as, OpenListNode *current)
    00537 {
    00538   /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
    00539    * since checking the cache not that much faster than the actual check */
    00540   return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
    00541     AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
    00542 }
    00543
    00545 static int32 NPFFindSafeTile(AyStar *as, OpenListNode *current)
    00546 {
    00547   const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
    00548
    00549   return
    00550     IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) &&
    00551     IsWaitingPositionFree(v, current->path.node.tile, current->path.node.direction, _settings_game.pf.forbid_90_deg) ?
    00552       AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
    00553 }
    00554
    00555 /* Will find a station identified using the NPFFindStationOrTileData */
    00556 static int32 NPFFindStationOrTile(AyStar *as, OpenListNode *current)
    00557 {
    00558   NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target;
    00559   AyStarNode *node = &current->path.node;
    00560   TileIndex tile = node->tile;
    00561
    00562   if (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) return AYSTAR_FOUND_END_NODE;
    00563
    00564   if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) {
    00565     if (fstd->v->type == VEH_TRAIN) return AYSTAR_FOUND_END_NODE;
    00566
    00567     assert(fstd->v->type == VEH_ROAD);
    00568     /* Only if it is a valid station *and* we can stop there */
    00569     if (GetStationType(tile) == fstd->station_type && (fstd->not_articulated || IsDriveThroughStopTile(tile))) return AYSTAR_FOUND_END_NODE;
    00570   }
    00571   return AYSTAR_DONE;
    00572 }
    00573
    00581 static const PathNode *FindSafePosition(PathNode *path, const Train *v)
    00582 {
    00583   /* If there is no signal, reserve the whole path. */
    00584   PathNode *sig = path;
    00585
    00586   for (; path->parent != NULL; path = path->parent) {
    00587     if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) {
    00588       sig = path;
    00589     }
    00590   }
    00591
    00592   return sig;
    00593 }
    00594
    00598 static void ClearPathReservation(const PathNode *start, const PathNode *end)
    00599 {
    00600   bool first_run = true;
    00601   for (; start != end; start = start->parent) {
    00602     if (IsRailStationTile(start->node.tile) && first_run) {
    00603       SetRailStationPlatformReservation(start->node.tile, TrackdirToExitdir(start->node.direction), false);
    00604     } else {
    00605       UnreserveRailTrack(start->node.tile, TrackdirToTrack(start->node.direction));
    00606     }
    00607     first_run = false;
    00608   }
    00609 }
    00610
    00617 static void NPFSaveTargetData(AyStar *as, OpenListNode *current)
    00618 {
    00619   NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path;
    00620   ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
    00621   ftd->best_path_dist = current->g;
    00622   ftd->best_bird_dist = 0;
    00623   ftd->node = current->path.node;
    00624   ftd->res_okay = false;
    00625
    00626   if (as->user_target != NULL && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && as->user_data[NPF_TYPE] == TRANSPORT_RAIL) {
    00627     /* Path reservation is requested. */
    00628     const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v);
    00629
    00630     const PathNode *target = FindSafePosition(&current->path, v);
    00631     ftd->node = target->node;
    00632
    00633     /* If the target is a station skip to platform end. */
    00634     if (IsRailStationTile(target->node.tile)) {
    00635       DiagDirection dir = TrackdirToExitdir(target->node.direction);
    00636       uint len = Station::GetByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir);
    00637       TileIndex end_tile = TILE_ADD(target->node.tile, (len - 1) * TileOffsByDiagDir(dir));
    00638
    00639       /* Update only end tile, trackdir of a station stays the same. */
    00640       ftd->node.tile = end_tile;
    00641       if (!IsWaitingPositionFree(v, end_tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
    00642       SetRailStationPlatformReservation(target->node.tile, dir, true);
    00643       SetRailStationReservation(target->node.tile, false);
    00644     } else {
    00645       if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return;
    00646     }
    00647
    00648     for (const PathNode *cur = target; cur->parent != NULL; cur = cur->parent) {
    00649       if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) {
    00650         /* Reservation failed, undo. */
    00651         ClearPathReservation(target, cur);
    00652         return;
    00653       }
    00654     }
    00655
    00656     ftd->res_okay = true;
    00657   }
    00658 }
    00659
    00669 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
    00670 {
    00671   if (IsTileType(tile, MP_RAILWAY) || // Rail tile (also rail depot)
    00672       HasStationTileRail(tile) ||     // Rail station tile/waypoint
    00673       IsRoadDepotTile(tile) ||        // Road depot tile
    00674       IsStandardRoadStopTile(tile)) { // Road station tile (but not drive-through stops)
    00675     return IsTileOwner(tile, owner);  // You need to own these tiles entirely to use them
    00676   }
    00677
    00678   switch (GetTileType(tile)) {
    00679     case MP_ROAD:
    00680       /* rail-road crossing : are we looking at the railway part? */
    00681       if (IsLevelCrossing(tile) &&
    00682           DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
    00683         return IsTileOwner(tile, owner); // Railway needs owner check, while the street is public
    00684       }
    00685       break;
    00686
    00687     case MP_TUNNELBRIDGE:
    00688       if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
    00689         return IsTileOwner(tile, owner);
    00690       }
    00691       break;
    00692
    00693     default:
    00694       break;
    00695   }
    00696
    00697   return true; // no need to check
    00698 }
    00699
    00700
    00704 static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
    00705 {
    00706   assert(IsDepotTypeTile(tile, type));
    00707
    00708   switch (type) {
    00709     case TRANSPORT_RAIL:  return GetRailDepotDirection(tile);
    00710     case TRANSPORT_ROAD:  return GetRoadDepotDirection(tile);
    00711     case TRANSPORT_WATER: return GetShipDepotDirection(tile);
    00712     default: return INVALID_DIAGDIR; // Not reached
    00713   }
    00714 }
    00715
    00717 static DiagDirection GetSingleTramBit(TileIndex tile)
    00718 {
    00719   if (IsNormalRoadTile(tile)) {
    00720     RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
    00721     switch (rb) {
    00722       case ROAD_NW: return DIAGDIR_NW;
    00723       case ROAD_SW: return DIAGDIR_SW;
    00724       case ROAD_SE: return DIAGDIR_SE;
    00725       case ROAD_NE: return DIAGDIR_NE;
    00726       default: break;
    00727     }
    00728   }
    00729   return INVALID_DIAGDIR;
    00730 }
    00731
    00742 static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
    00743 {
    00744   if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type);
    00745
    00746   if (type == TRANSPORT_ROAD) {
    00747     if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
    00748     if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
    00749   }
    00750
    00751   return INVALID_DIAGDIR;
    00752 }
    00753
    00763 static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
    00764 {
    00765   DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
    00766   return single_entry != INVALID_DIAGDIR && single_entry != dir;
    00767 }
    00768
    00780 static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
    00781 {
    00782   /* Check tunnel entries and bridge ramps */
    00783   if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
    00784
    00785   /* Test ownership */
    00786   if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false;
    00787
    00788   /* check correct rail type (mono, maglev, etc) */
    00789   if (type == TRANSPORT_RAIL) {
    00790     RailType rail_type = GetTileRailType(tile);
    00791     if (!HasBit(railtypes, rail_type)) return false;
    00792   }
    00793
    00794   /* Depots, standard roadstops and single tram bits can only be entered from one direction */
    00795   DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
    00796   if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
    00797
    00798   return true;
    00799 }
    00800
    00812 static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
    00813 {
    00814   TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
    00815
    00816   if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
    00817     /* GetTileTrackStatus() returns 0 for single tram bits.
    00818      * As we cannot change it there (easily) without breaking something, change it here */
    00819     switch (GetSingleTramBit(dst_tile)) {
    00820       case DIAGDIR_NE:
    00821       case DIAGDIR_SW:
    00822         trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
    00823         break;
    00824
    00825       case DIAGDIR_NW:
    00826       case DIAGDIR_SE:
    00827         trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
    00828         break;
    00829
    00830       default: break;
    00831     }
    00832   }
    00833
    00834   DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
    00835
    00836   /* Select only trackdirs we can reach from our current trackdir */
    00837   trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
    00838
    00839   /* Filter out trackdirs that would make 90 deg turns for trains */
    00840   if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
    00841
    00842   DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
    00843
    00844   return trackdirbits;
    00845 }
    00846
    00847
    00848 /* Will just follow the results of GetTileTrackStatus concerning where we can
    00849  * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
    00850  * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
    00851  * entry and exit are neighbours. Will fill
    00852  * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
    00853  * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
    00854 static void NPFFollowTrack(AyStar *aystar, OpenListNode *current)
    00855 {
    00856   /* We leave src_tile on track src_trackdir in direction src_exitdir */
    00857   Trackdir src_trackdir = current->path.node.direction;
    00858   TileIndex src_tile = current->path.node.tile;
    00859   DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
    00860
    00861   /* Is src_tile valid, and can be used?
    00862    * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
    00863    * 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. */
    00864   bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_START_TILE));
    00865
    00866   /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
    00867   TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
    00868   uint subtype = aystar->user_data[NPF_SUB_TYPE];
    00869
    00870   /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
    00871   aystar->num_neighbours = 0;
    00872   DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
    00873
    00874   /* We want to determine the tile we arrive, and which choices we have there */
    00875   TileIndex dst_tile;
    00876   TrackdirBits trackdirbits;
    00877
    00878   /* Find dest tile */
    00879   if (ignore_src_tile) {
    00880     /* Do not perform any checks that involve src_tile */
    00881     dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
    00882     trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
    00883   } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
    00884     /* We drive through the wormhole and arrive on the other side */
    00885     dst_tile = GetOtherTunnelBridgeEnd(src_tile);
    00886     trackdirbits = TrackdirToTrackdirBits(src_trackdir);
    00887   } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
    00888     /* We can only reverse on this tile */
    00889     dst_tile = src_tile;
    00890     src_trackdir = ReverseTrackdir(src_trackdir);
    00891     trackdirbits = TrackdirToTrackdirBits(src_trackdir);
    00892   } else {
    00893     /* We leave src_tile in src_exitdir and reach dst_tile */
    00894     dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
    00895
    00896     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;
    00897
    00898     if (dst_tile == INVALID_TILE) {
    00899       /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
    00900       if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
    00901
    00902       dst_tile = src_tile;
    00903       src_trackdir = ReverseTrackdir(src_trackdir);
    00904     }
    00905
    00906     trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
    00907
    00908     if (trackdirbits == 0) {
    00909       /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
    00910       if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
    00911
    00912       dst_tile = src_tile;
    00913       src_trackdir = ReverseTrackdir(src_trackdir);
    00914
    00915       trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
    00916     }
    00917   }
    00918
    00919   if (NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_RESERVED)) {
    00920     /* Mask out any reserved tracks. */
    00921     TrackBits reserved = GetReservedTrackbits(dst_tile);
    00922     trackdirbits &= ~TrackBitsToTrackdirBits(reserved);
    00923
    00924     uint bits = TrackdirBitsToTrackBits(trackdirbits);
    00925     int i;
    00926     FOR_EACH_SET_BIT(i, bits) {
    00927       if (TracksOverlap(reserved | TrackToTrackBits((Track)i))) trackdirbits &= ~TrackToTrackdirBits((Track)i);
    00928     }
    00929   }
    00930
    00931   /* Enumerate possible track */
    00932   uint i = 0;
    00933   while (trackdirbits != 0) {
    00934     Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
    00935     DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
    00936
    00937     /* Tile with signals? */
    00938     if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
    00939       if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir)))
    00940         /* If there's a one-way signal not pointing towards us, stop going in this direction. */
    00941         break;
    00942     }
    00943     {
    00944       /* We've found ourselves a neighbour :-) */
    00945       AyStarNode *neighbour = &aystar->neighbours[i];
    00946       neighbour->tile = dst_tile;
    00947       neighbour->direction = dst_trackdir;
    00948       /* Save user data */
    00949       neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
    00950       NPFFillTrackdirChoice(neighbour, current);
    00951     }
    00952     i++;
    00953   }
    00954   aystar->num_neighbours = i;
    00955 }
    00956
    00957 /*
    00958  * Plan a route to the specified target (which is checked by target_proc),
    00959  * from start1 and if not NULL, from start2 as well. The type of transport we
    00960  * are checking is in type. reverse_penalty is applied to all routes that
    00961  * originate from the second start node.
    00962  * When we are looking for one specific target (optionally multiple tiles), we
    00963  * should use a good heuristic to perform aystar search. When we search for
    00964  * multiple targets that are spread around, we should perform a breadth first
    00965  * search by specifiying CalcZero as our heuristic.
    00966  */
    00967 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)
    00968 {
    00969   int r;
    00970   NPFFoundTargetData result;
    00971
    00972   /* Initialize procs */
    00973   _npf_aystar.CalculateH = heuristic_proc;
    00974   _npf_aystar.EndNodeCheck = target_proc;
    00975   _npf_aystar.FoundEndNode = NPFSaveTargetData;
    00976   _npf_aystar.GetNeighbours = NPFFollowTrack;
    00977   switch (type) {
    00978     default: NOT_REACHED();
    00979     case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
    00980     case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
    00981     case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
    00982   }
    00983
    00984   /* Initialize Start Node(s) */
    00985   start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00986   start1->user_data[NPF_NODE_FLAGS] = 0;
    00987   NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
    00988   _npf_aystar.addstart(&_npf_aystar, start1, 0);
    00989   if (start2) {
    00990     start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    00991     start2->user_data[NPF_NODE_FLAGS] = 0;
    00992     NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
    00993     NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
    00994     _npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
    00995   }
    00996
    00997   /* Initialize result */
    00998   result.best_bird_dist = UINT_MAX;
    00999   result.best_path_dist = UINT_MAX;
    01000   result.best_trackdir  = INVALID_TRACKDIR;
    01001   result.node.tile      = INVALID_TILE;
    01002   result.res_okay       = false;
    01003   _npf_aystar.user_path = &result;
    01004
    01005   /* Initialize target */
    01006   _npf_aystar.user_target = target;
    01007
    01008   /* Initialize user_data */
    01009   _npf_aystar.user_data[NPF_TYPE] = type;
    01010   _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
    01011   _npf_aystar.user_data[NPF_OWNER] = owner;
    01012   _npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
    01013
    01014   /* GO! */
    01015   r = AyStarMain_Main(&_npf_aystar);
    01016   assert(r != AYSTAR_STILL_BUSY);
    01017
    01018   if (result.best_bird_dist != 0) {
    01019     if (target != NULL) {
    01020       DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
    01021     } else {
    01022       /* Assumption: target == NULL, so we are looking for a depot */
    01023       DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
    01024     }
    01025
    01026   }
    01027   return result;
    01028 }
    01029
    01030 /* Will search as below, but with two start nodes, the second being the
    01031  * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which
    01032  * direction was taken (NPFGetFlag(result.node, NPF_FLAG_REVERSE)) */
    01033 static NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
    01034 {
    01035   AyStarNode start1;
    01036   AyStarNode start2;
    01037
    01038   start1.tile = tile1;
    01039   start2.tile = tile2;
    01040   /* We set this in case the target is also the start tile, we will just
    01041    * return a not found then */
    01042   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01043   start1.direction = trackdir1;
    01044   start2.direction = trackdir2;
    01045   start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01046
    01047   return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
    01048 }
    01049
    01050 /* Will search from the given tile and direction, for a route to the given
    01051  * station for the given transport type. See the declaration of
    01052  * NPFFoundTargetData above for the meaning of the result. */
    01053 static NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
    01054 {
    01055   return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes);
    01056 }
    01057
    01058 /* Search using breadth first. Good for little track choice and inaccurate
    01059  * heuristic, such as railway/road with two start nodes, the second being the reverse. Call
    01060  * NPFGetFlag(result.node, NPF_FLAG_REVERSE) to see from which node the path
    01061  * orginated. All pathfs from the second node will have the given
    01062  * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full
    01063  * tile).
    01064  */
    01065 static 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)
    01066 {
    01067   AyStarNode start1;
    01068   AyStarNode start2;
    01069
    01070   start1.tile = tile1;
    01071   start2.tile = tile2;
    01072   /* We set this in case the target is also the start tile, we will just
    01073    * return a not found then */
    01074   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01075   start1.direction = trackdir1;
    01076   start2.direction = trackdir2;
    01077   start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01078
    01079   /* perform a breadth first search. Target is NULL,
    01080    * since we are just looking for any depot...*/
    01081   return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
    01082 }
    01083
    01084 void InitializeNPF()
    01085 {
    01086   static bool first_init = true;
    01087   if (first_init) {
    01088     first_init = false;
    01089     init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
    01090   } else {
    01091     AyStarMain_Clear(&_npf_aystar);
    01092   }
    01093   _npf_aystar.loops_per_tick = 0;
    01094   _npf_aystar.max_path_cost = 0;
    01095   //_npf_aystar.max_search_nodes = 0;
    01096   /* We will limit the number of nodes for now, until we have a better
    01097    * solution to really fix performance */
    01098   _npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes;
    01099 }
    01100
    01101 static void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, const Vehicle *v, bool reserve_path = false)
    01102 {
    01103   /* Ships don't really reach their stations, but the tile in front. So don't
    01104    * save the station id for ships. For roadvehs we don't store it either,
    01105    * because multistop depends on vehicles actually reaching the exact
    01106    * dest_tile, not just any stop of that station.
    01107    * So only for train orders to stations we fill fstd->station_index, for all
    01108    * others only dest_coords */
    01109   if (v->type != VEH_SHIP && (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_WAYPOINT))) {
    01110     assert(v->type == VEH_TRAIN || v->type == VEH_ROAD);
    01111     fstd->station_index = v->current_order.GetDestination();
    01112     fstd->station_type = (v->type == VEH_TRAIN) ? (v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT) : (RoadVehicle::From(v)->IsBus() ? STATION_BUS : STATION_TRUCK);
    01113     fstd->not_articulated = v->type == VEH_ROAD && !RoadVehicle::From(v)->HasArticulatedPart();
    01114     /* Let's take the closest tile of the station as our target for vehicles */
    01115     fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile, fstd->station_type);
    01116   } else {
    01117     fstd->dest_coords = v->dest_tile;
    01118     fstd->station_index = INVALID_STATION;
    01119   }
    01120   fstd->reserve_path = reserve_path;
    01121   fstd->v = v;
    01122 }
    01123
    01124 /*** Road vehicles ***/
    01125
    01126 FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty)
    01127 {
    01128   Trackdir trackdir = v->GetVehicleTrackdir();
    01129
    01130   NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, v->tile, ReverseTrackdir(trackdir), false, TRANSPORT_ROAD, v->compatible_roadtypes, v->owner, INVALID_RAILTYPES, 0);
    01131
    01132   if (ftd.best_bird_dist != 0) return FindDepotData();
    01133
    01134   /* Found target */
    01135   /* Our caller expects a number of tiles, so we just approximate that
    01136    * number by this. It might not be completely what we want, but it will
    01137    * work for now :-) We can possibly change this when the old pathfinder
    01138    * is removed. */
    01139   return FindDepotData(ftd.node.tile, ftd.best_path_dist);
    01140 }
    01141
    01142 Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs)
    01143 {
    01144   NPFFindStationOrTileData fstd;
    01145
    01146   NPFFillWithOrderData(&fstd, v);
    01147   Trackdir trackdir = DiagDirToDiagTrackdir(enterdir);
    01148
    01149   NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_ROAD, v->compatible_roadtypes, v->owner, INVALID_RAILTYPES);
    01150   if (ftd.best_trackdir == INVALID_TRACKDIR) {
    01151     /* We are already at our target. Just do something
    01152      * @todo: maybe display error?
    01153      * @todo: go straight ahead if possible? */
    01154     return (Trackdir)FindFirstBit2x64(trackdirs);
    01155   }
    01156
    01157   /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
    01158    * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
    01159    * we did not find our target, but ftd.best_trackdir contains the direction leading
    01160    * to the tile closest to our target. */
    01161   return ftd.best_trackdir;
    01162 }
    01163
    01164 /*** Ships ***/
    01165
    01166 Track NPFShipChooseTrack(const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
    01167 {
    01168   NPFFindStationOrTileData fstd;
    01169   Trackdir trackdir = v->GetVehicleTrackdir();
    01170   assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
    01171
    01172   NPFFillWithOrderData(&fstd, v);
    01173
    01174   NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
    01175
    01176   /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
    01177    * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
    01178    * we did not find our target, but ftd.best_trackdir contains the direction leading
    01179    * to the tile closest to our target. */
    01180   if (ftd.best_trackdir == 0xff) return INVALID_TRACK;
    01181   return TrackdirToTrack(ftd.best_trackdir);
    01182 }
    01183
    01184 /*** Trains ***/
    01185
    01186 FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_penalty)
    01187 {
    01188   const Train *last = v->Last();
    01189   Trackdir trackdir = v->GetVehicleTrackdir();
    01190   Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
    01191
    01192   assert(trackdir != INVALID_TRACKDIR);
    01193   NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes, NPF_INFINITE_PENALTY);
    01194   if (ftd.best_bird_dist != 0) return FindDepotData();
    01195
    01196   /* Found target */
    01197   /* Our caller expects a number of tiles, so we just approximate that
    01198    * number by this. It might not be completely what we want, but it will
    01199    * work for now :-) We can possibly change this when the old pathfinder
    01200    * is removed. */
    01201   return FindDepotData(ftd.node.tile, ftd.best_path_dist, NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE));
    01202 }
    01203
    01204 bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype)
    01205 {
    01206   assert(v->type == VEH_TRAIN);
    01207
    01208   NPFFindStationOrTileData fstd;
    01209   fstd.v = v;
    01210   fstd.reserve_path = true;
    01211
    01212   AyStarNode start1;
    01213   start1.tile = tile;
    01214   /* We set this in case the target is also the start tile, we will just
    01215    * return a not found then */
    01216   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
    01217   start1.direction = trackdir;
    01218   NPFSetFlag(&start1, NPF_FLAG_IGNORE_RESERVED, true);
    01219
    01220   RailTypes railtypes = v->compatible_railtypes;
    01221   if (override_railtype) railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes;
    01222
    01223   /* perform a breadth first search. Target is NULL,
    01224    * since we are just looking for any safe tile...*/
    01225   return NPFRouteInternal(&start1, true, NULL, false, &fstd, NPFFindSafeTile, NPFCalcZero, TRANSPORT_RAIL, 0, v->owner, railtypes, 0).res_okay;
    01226 }
    01227
    01228 bool NPFTrainCheckReverse(const Train *v)
    01229 {
    01230   NPFFindStationOrTileData fstd;
    01231   NPFFoundTargetData ftd;
    01232   const Train *last = v->Last();
    01233
    01234   NPFFillWithOrderData(&fstd, v);
    01235
    01236   Trackdir trackdir = v->GetVehicleTrackdir();
    01237   Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir());
    01238   assert(trackdir != INVALID_TRACKDIR);
    01239   assert(trackdir_rev != INVALID_TRACKDIR);
    01240
    01241   ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes);
    01242   /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */
    01243   return ftd.best_bird_dist != 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE);
    01244 }
    01245
    01246 Track NPFTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_found, bool reserve_track, struct PBSTileInfo *target)
    01247 {
    01248   NPFFindStationOrTileData fstd;
    01249   NPFFillWithOrderData(&fstd, v, reserve_track);
    01250
    01251   PBSTileInfo origin = FollowTrainReservation(v);
    01252   assert(IsValidTrackdir(origin.trackdir));
    01253
    01254   NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, TRANSPORT_RAIL, 0, v->owner, v->compatible_railtypes);
    01255
    01256   if (target != NULL) {
    01257     target->tile = ftd.node.tile;
    01258     target->trackdir = (Trackdir)ftd.node.direction;
    01259     target->okay = ftd.res_okay;
    01260   }
    01261
    01262   if (ftd.best_trackdir == INVALID_TRACKDIR) {
    01263     /* We are already at our target. Just do something
    01264      * @todo maybe display error?
    01265      * @todo: go straight ahead if possible? */
    01266     if (path_not_found) *path_not_found = false;
    01267     return FindFirstTrack(tracks);
    01268   }
    01269
    01270   /* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
    01271    * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
    01272    * we did not find our target, but ftd.best_trackdir contains the direction leading
    01273    * to the tile closest to our target. */
    01274   if (path_not_found != NULL) *path_not_found = (ftd.best_bird_dist != 0);
    01275   /* Discard enterdir information, making it a normal track */
    01276   return TrackdirToTrack(ftd.best_trackdir);
    01277 }