npf.cpp

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