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

Generated on Sat Dec 26 20:06:03 2009 for OpenTTD by  doxygen 1.5.6