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