npf.cpp

Go to the documentation of this file.
00001 /* $Id: npf.cpp 12199 2008-02-20 17:49:50Z frosch $ */
00002 
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "npf.h"
00008 #include "bridge_map.h"
00009 #include "debug.h"
00010 #include "tile_cmd.h"
00011 #include "bridge.h"
00012 #include "landscape.h"
00013 #include "aystar.h"
00014 #include "pathfind.h"
00015 #include "station.h"
00016 #include "station_map.h"
00017 #include "depot.h"
00018 #include "tunnel_map.h"
00019 #include "network/network.h"
00020 #include "water_map.h"
00021 #include "tunnelbridge_map.h"
00022 #include "functions.h"
00023 #include "vehicle_base.h"
00024 #include "settings_type.h"
00025 #include "tunnelbridge.h"
00026 
00027 static AyStar _npf_aystar;
00028 
00029 /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH,
00030  * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071
00031  */
00032 #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH)
00033 static const uint _trackdir_length[TRACKDIR_END] = {
00034   NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH,
00035   0, 0,
00036   NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH
00037 };
00038 
00045 static uint NPFDistanceTrack(TileIndex t0, TileIndex t1)
00046 {
00047   const uint dx = Delta(TileX(t0), TileX(t1));
00048   const uint dy = Delta(TileY(t0), TileY(t1));
00049 
00050   const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */
00051   /* OPTIMISATION:
00052    * Original: diagTracks = max(dx, dy) - min(dx,dy);
00053    * Proof:
00054    * (dx+dy) - straightTracks  == (min + max) - straightTracks = min + max - 2 * min = max - min */
00055   const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */
00056 
00057   /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose
00058    * precision */
00059   return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH;
00060 }
00061 
00062 
00063 #if 0
00064 static uint NTPHash(uint key1, uint key2)
00065 {
00066   /* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */
00067   return PATHFIND_HASH_TILE(key1);
00068 }
00069 #endif
00070 
00078 static uint NPFHash(uint key1, uint key2)
00079 {
00080   /* TODO: think of a better hash? */
00081   uint part1 = TileX(key1) & NPF_HASH_HALFMASK;
00082   uint part2 = TileY(key1) & NPF_HASH_HALFMASK;
00083 
00084   assert(IsValidTrackdir((Trackdir)key2));
00085   assert(IsValidTile(key1));
00086   return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE;
00087 }
00088 
00089 static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent)
00090 {
00091   return 0;
00092 }
00093 
00094 /* Calcs the tile of given station that is closest to a given tile
00095  * for this we assume the station is a rectangle,
00096  * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h)
00097  */
00098 static TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
00099 {
00100   const Station* st = GetStation(station);
00101 
00102   uint minx = TileX(st->train_tile);  // topmost corner of station
00103   uint miny = TileY(st->train_tile);
00104   uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
00105   uint maxy = miny + st->trainst_h - 1;
00106   uint x;
00107   uint y;
00108 
00109   /* we are going the aim for the x coordinate of the closest corner
00110    * but if we are between those coordinates, we will aim for our own x coordinate */
00111   x = Clamp(TileX(tile), minx, maxx);
00112 
00113   /* same for y coordinate, see above comment */
00114   y = Clamp(TileY(tile), miny, maxy);
00115 
00116   /* return the tile of our target coordinates */
00117   return TileXY(x, y);
00118 }
00119 
00120 /* Calcs the heuristic to the target station or tile. For train stations, it
00121  * takes into account the direction of approach.
00122  */
00123 static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent)
00124 {
00125   NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
00126   NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
00127   TileIndex from = current->tile;
00128   TileIndex to = fstd->dest_coords;
00129   uint dist;
00130 
00131   /* for train-stations, we are going to aim for the closest station tile */
00132   if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION)
00133     to = CalcClosestStationTile(fstd->station_index, from);
00134 
00135   if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) {
00136     /* Since roads only have diagonal pieces, we use manhattan distance here */
00137     dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH;
00138   } else {
00139     /* Ships and trains can also go diagonal, so the minimum distance is shorter */
00140     dist = NPFDistanceTrack(from, to);
00141   }
00142 
00143   DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist);
00144 
00145   if (dist < ftd->best_bird_dist) {
00146     ftd->best_bird_dist = dist;
00147     ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE];
00148   }
00149   return dist;
00150 }
00151 
00152 
00153 /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to
00154  * get here, either getting it from the current choice or from the parent's
00155  * choice */
00156 static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent)
00157 {
00158   if (parent->path.parent == NULL) {
00159     Trackdir trackdir = (Trackdir)current->direction;
00160     /* This is a first order decision, so we'd better save the
00161      * direction we chose */
00162     current->user_data[NPF_TRACKDIR_CHOICE] = trackdir;
00163     DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir);
00164   } else {
00165     /* We've already made the decision, so just save our parent's decision */
00166     current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE];
00167   }
00168 }
00169 
00170 /* Will return the cost of the tunnel. If it is an entry, it will return the
00171  * cost of that tile. If the tile is an exit, it will return the tunnel length
00172  * including the exit tile. Requires that this is a Tunnel tile */
00173 static uint NPFTunnelCost(AyStarNode* current)
00174 {
00175   DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction);
00176   TileIndex tile = current->tile;
00177   if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) {
00178     /* We just popped out if this tunnel, since were
00179      * facing the tunnel exit */
00180     return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1);
00181     /* @todo: Penalty for tunnels? */
00182   } else {
00183     /* We are entering the tunnel, the enter tile is just a
00184      * straight track */
00185     return NPF_TILE_LENGTH;
00186   }
00187 }
00188 
00189 static inline uint NPFBridgeCost(AyStarNode *current)
00190 {
00191   return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile));
00192 }
00193 
00194 static uint NPFSlopeCost(AyStarNode* current)
00195 {
00196   TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir((Trackdir)current->direction));
00197 
00198   /* Get center of tiles */
00199   int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2;
00200   int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2;
00201   int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2;
00202   int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2;
00203 
00204   int dx4 = (x2 - x1) / 4;
00205   int dy4 = (y2 - y1) / 4;
00206 
00207   /* Get the height on both sides of the tile edge.
00208    * Avoid testing the height on the tile-center. This will fail for halftile-foundations.
00209    */
00210   int z1 = GetSlopeZ(x1 + dx4, y1 + dy4);
00211   int z2 = GetSlopeZ(x2 - dx4, y2 - dy4);
00212 
00213   if (z2 - z1 > 1) {
00214     /* Slope up */
00215     return _patches.npf_rail_slope_penalty;
00216   }
00217   return 0;
00218   /* Should we give a bonus for slope down? Probably not, we
00219    * could just substract that bonus from the penalty, because
00220    * there is only one level of steepness... */
00221 }
00222 
00227 static void NPFMarkTile(TileIndex tile)
00228 {
00229 #ifndef NO_DEBUG_MESSAGES
00230   if (_debug_npf_level < 1 || _networking) return;
00231   switch (GetTileType(tile)) {
00232     case MP_RAILWAY:
00233       /* DEBUG: mark visited tiles by mowing the grass under them ;-) */
00234       if (!IsTileDepotType(tile, TRANSPORT_RAIL)) {
00235         SetRailGroundType(tile, RAIL_GROUND_BARREN);
00236         MarkTileDirtyByTile(tile);
00237       }
00238       break;
00239 
00240     case MP_ROAD:
00241       if (!IsTileDepotType(tile, TRANSPORT_ROAD)) {
00242         SetRoadside(tile, ROADSIDE_BARREN);
00243         MarkTileDirtyByTile(tile);
00244       }
00245       break;
00246 
00247     default:
00248       break;
00249   }
00250 #endif
00251 }
00252 
00253 static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
00254 {
00255   /* TileIndex tile = current->tile; */
00256   int32 cost = 0;
00257   Trackdir trackdir = (Trackdir)current->direction;
00258 
00259   cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks
00260 
00261   if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir))
00262     cost += _patches.npf_buoy_penalty; // A small penalty for going over buoys
00263 
00264   if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
00265     cost += _patches.npf_water_curve_penalty;
00266 
00267   /* @todo More penalties? */
00268 
00269   return cost;
00270 }
00271 
00272 /* Determine the cost of this node, for road tracks */
00273 static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
00274 {
00275   TileIndex tile = current->tile;
00276   int32 cost = 0;
00277 
00278   /* Determine base length */
00279   switch (GetTileType(tile)) {
00280     case MP_TUNNELBRIDGE:
00281       cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
00282       break;
00283 
00284     case MP_ROAD:
00285       cost = NPF_TILE_LENGTH;
00286       /* Increase the cost for level crossings */
00287       if (IsLevelCrossing(tile)) cost += _patches.npf_crossing_penalty;
00288       break;
00289 
00290     case MP_STATION:
00291       cost = NPF_TILE_LENGTH;
00292       /* Increase the cost for drive-through road stops */
00293       if (IsDriveThroughStopTile(tile)) cost += _patches.npf_road_drive_through_penalty;
00294       break;
00295 
00296     default:
00297       break;
00298   }
00299 
00300   /* Determine extra costs */
00301 
00302   /* Check for slope */
00303   cost += NPFSlopeCost(current);
00304 
00305   /* Check for turns. Road vehicles only really drive diagonal, turns are
00306    * represented by non-diagonal tracks */
00307   if (!IsDiagonalTrackdir((Trackdir)current->direction))
00308     cost += _patches.npf_road_curve_penalty;
00309 
00310   NPFMarkTile(tile);
00311   DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
00312   return cost;
00313 }
00314 
00315 
00316 /* Determine the cost of this node, for railway tracks */
00317 static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent)
00318 {
00319   TileIndex tile = current->tile;
00320   Trackdir trackdir = (Trackdir)current->direction;
00321   int32 cost = 0;
00322   /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */
00323   OpenListNode new_node;
00324 
00325   /* Determine base length */
00326   switch (GetTileType(tile)) {
00327     case MP_TUNNELBRIDGE:
00328       cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current);
00329       break;
00330 
00331     case MP_RAILWAY:
00332       cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */
00333       break;
00334 
00335     case MP_ROAD: /* Railway crossing */
00336       cost = NPF_TILE_LENGTH;
00337       break;
00338 
00339     case MP_STATION:
00340       /* We give a station tile a penalty. Logically we would only want to give
00341        * station tiles that are not our destination this penalty. This would
00342        * discourage trains to drive through busy stations. But, we can just
00343        * give any station tile a penalty, because every possible route will get
00344        * this penalty exactly once, on its end tile (if it's a station) and it
00345        * will therefore not make a difference. */
00346       cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty;
00347       break;
00348 
00349     default:
00350       break;
00351   }
00352 
00353   /* Determine extra costs */
00354 
00355   /* Check for signals */
00356   if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir)) {
00357     /* Ordinary track with signals */
00358     if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) {
00359       /* Signal facing us is red */
00360       if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) {
00361         /* Penalize the first signal we
00362          * encounter, if it is red */
00363 
00364         /* Is this a presignal exit or combo? */
00365         SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir));
00366         if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) {
00367           /* Penalise exit and combo signals differently (heavier) */
00368           cost += _patches.npf_rail_firstred_exit_penalty;
00369         } else {
00370           cost += _patches.npf_rail_firstred_penalty;
00371         }
00372       }
00373       /* Record the state of this signal */
00374       NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true);
00375     } else {
00376       /* Record the state of this signal */
00377       NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false);
00378     }
00379     NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true);
00380   }
00381 
00382   /* Penalise the tile if it is a target tile and the last signal was
00383    * red */
00384   /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell
00385    * of course... */
00386   new_node.path.node = *current;
00387   if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED))
00388     cost += _patches.npf_rail_lastred_penalty;
00389 
00390   /* Check for slope */
00391   cost += NPFSlopeCost(current);
00392 
00393   /* Check for turns */
00394   if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction))
00395     cost += _patches.npf_rail_curve_penalty;
00396   /*TODO, with realistic acceleration, also the amount of straight track between
00397    *      curves should be taken into account, as this affects the speed limit. */
00398 
00399   /* Check for reverse in depot */
00400   if (IsTileDepotType(tile, TRANSPORT_RAIL) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) {
00401     /* Penalise any depot tile that is not the last tile in the path. This
00402      * _should_ penalise every occurence of reversing in a depot (and only
00403      * that) */
00404     cost += _patches.npf_rail_depot_reverse_penalty;
00405   }
00406 
00407   /* Check for occupied track */
00408   //TODO
00409 
00410   NPFMarkTile(tile);
00411   DEBUG(npf, 4, "Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost);
00412   return cost;
00413 }
00414 
00415 /* Will find any depot */
00416 static int32 NPFFindDepot(AyStar* as, OpenListNode *current)
00417 {
00418   /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below,
00419    * since checking the cache not that much faster than the actual check */
00420   return IsTileDepotType(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ?
00421     AYSTAR_FOUND_END_NODE : AYSTAR_DONE;
00422 }
00423 
00424 /* Will find a station identified using the NPFFindStationOrTileData */
00425 static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current)
00426 {
00427   NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target;
00428   AyStarNode *node = &current->path.node;
00429   TileIndex tile = node->tile;
00430 
00431   /* If GetNeighbours said we could get here, we assume the station type
00432    * is correct */
00433   if (
00434     (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */
00435     (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */
00436   ) {
00437     return AYSTAR_FOUND_END_NODE;
00438   } else {
00439     return AYSTAR_DONE;
00440   }
00441 }
00442 
00443 /* To be called when current contains the (shortest route to) the target node.
00444  * Will fill the contents of the NPFFoundTargetData using
00445  * AyStarNode[NPF_TRACKDIR_CHOICE].
00446  */
00447 static void NPFSaveTargetData(AyStar* as, OpenListNode* current)
00448 {
00449   NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path;
00450   ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE];
00451   ftd->best_path_dist = current->g;
00452   ftd->best_bird_dist = 0;
00453   ftd->node = current->path.node;
00454 }
00455 
00465 static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir)
00466 {
00467   if (IsTileType(tile, MP_RAILWAY) ||           /* Rail tile (also rail depot) */
00468       IsRailwayStationTile(tile) ||               /* Rail station tile */
00469       IsTileDepotType(tile, TRANSPORT_ROAD) ||  /* Road depot tile */
00470       IsStandardRoadStopTile(tile)) { /* Road station tile (but not drive-through stops) */
00471     return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */
00472   }
00473 
00474   switch (GetTileType(tile)) {
00475     case MP_ROAD:
00476       /* rail-road crossing : are we looking at the railway part? */
00477       if (IsLevelCrossing(tile) &&
00478           DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) {
00479         return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */
00480       }
00481       break;
00482 
00483     case MP_TUNNELBRIDGE:
00484       if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) {
00485         return IsTileOwner(tile, owner);
00486       }
00487       break;
00488 
00489     default:
00490       break;
00491   }
00492 
00493   return true; // no need to check
00494 }
00495 
00496 
00500 static DiagDirection GetDepotDirection(TileIndex tile, TransportType type)
00501 {
00502   assert(IsTileDepotType(tile, type));
00503 
00504   switch (type) {
00505     case TRANSPORT_RAIL:  return GetRailDepotDirection(tile);
00506     case TRANSPORT_ROAD:  return GetRoadDepotDirection(tile);
00507     case TRANSPORT_WATER: return GetShipDepotDirection(tile);
00508     default: return INVALID_DIAGDIR; /* Not reached */
00509   }
00510 }
00511 
00513 static DiagDirection GetSingleTramBit(TileIndex tile)
00514 {
00515   if (IsNormalRoadTile(tile)) {
00516     RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM);
00517     switch (rb) {
00518       case ROAD_NW: return DIAGDIR_NW;
00519       case ROAD_SW: return DIAGDIR_SW;
00520       case ROAD_SE: return DIAGDIR_SE;
00521       case ROAD_NE: return DIAGDIR_NE;
00522       default: break;
00523     }
00524   }
00525   return INVALID_DIAGDIR;
00526 }
00527 
00538 static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype)
00539 {
00540   if (type != TRANSPORT_WATER && IsTileDepotType(tile, type)) return GetDepotDirection(tile, type);
00541 
00542   if (type == TRANSPORT_ROAD) {
00543     if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile);
00544     if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile);
00545   }
00546 
00547   return INVALID_DIAGDIR;
00548 }
00549 
00559 static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype)
00560 {
00561   DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
00562   return single_entry != INVALID_DIAGDIR && single_entry != dir;
00563 }
00564 
00576 static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner)
00577 {
00578   /* Check tunnel entries and bridge ramps */
00579   if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false;
00580 
00581   /* Test ownership */
00582   if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false;
00583 
00584   /* check correct rail type (mono, maglev, etc) */
00585   if (type == TRANSPORT_RAIL) {
00586     RailType rail_type = GetTileRailType(tile);
00587     if (!HasBit(railtypes, rail_type)) return false;
00588   }
00589 
00590   /* Depots, standard roadstops and single tram bits can only be entered from one direction */
00591   DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype);
00592   if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false;
00593 
00594   return true;
00595 }
00596 
00608 static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype)
00609 {
00610   TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype));
00611 
00612   if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) {
00613     /* GetTileTrackStatus() returns 0 for single tram bits.
00614      * As we cannot change it there (easily) without breaking something, change it here */
00615     switch (GetSingleTramBit(dst_tile)) {
00616       case DIAGDIR_NE:
00617       case DIAGDIR_SW:
00618         trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
00619         break;
00620 
00621       case DIAGDIR_NW:
00622       case DIAGDIR_SE:
00623         trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
00624         break;
00625 
00626       default: break;
00627     }
00628   }
00629 
00630   DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits);
00631 
00632   /* Select only trackdirs we can reach from our current trackdir */
00633   trackdirbits &= TrackdirReachesTrackdirs(src_trackdir);
00634 
00635   /* Filter out trackdirs that would make 90 deg turns for trains */
00636   if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir);
00637 
00638   DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits);
00639 
00640   return trackdirbits;
00641 }
00642 
00643 
00644 /* Will just follow the results of GetTileTrackStatus concerning where we can
00645  * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and
00646  * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the
00647  * entry and exit are neighbours. Will fill
00648  * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and
00649  * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */
00650 static void NPFFollowTrack(AyStar* aystar, OpenListNode* current)
00651 {
00652   /* We leave src_tile on track src_trackdir in direction src_exitdir */
00653   Trackdir src_trackdir = (Trackdir)current->path.node.direction;
00654   TileIndex src_tile = current->path.node.tile;
00655   DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir);
00656 
00657   /* Is src_tile valid, and can be used?
00658    * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir.
00659    * 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. */
00660   bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(&current->path.node, NPF_FLAG_IGNORE_START_TILE));
00661 
00662   /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */
00663   TransportType type = (TransportType)aystar->user_data[NPF_TYPE];
00664   uint subtype = aystar->user_data[NPF_SUB_TYPE];
00665 
00666   /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */
00667   aystar->num_neighbours = 0;
00668   DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile);
00669 
00670   /* We want to determine the tile we arrive, and which choices we have there */
00671   TileIndex dst_tile;
00672   TrackdirBits trackdirbits;
00673 
00674   /* Find dest tile */
00675   if (ignore_src_tile) {
00676     /* Do not perform any checks that involve src_tile */
00677     dst_tile = src_tile + TileOffsByDiagDir(src_exitdir);
00678     trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
00679   } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) {
00680     /* We drive through the wormhole and arrive on the other side */
00681     dst_tile = GetOtherTunnelBridgeEnd(src_tile);
00682     trackdirbits = TrackdirToTrackdirBits(src_trackdir);
00683   } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) {
00684     /* We can only reverse on this tile */
00685     dst_tile = src_tile;
00686     src_trackdir = ReverseTrackdir(src_trackdir);
00687     trackdirbits = TrackdirToTrackdirBits(src_trackdir);
00688   } else {
00689     /* We leave src_tile in src_exitdir and reach dst_tile */
00690     dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir));
00691 
00692     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;
00693 
00694     if (dst_tile == INVALID_TILE) {
00695       /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
00696       if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
00697 
00698       dst_tile = src_tile;
00699       src_trackdir = ReverseTrackdir(src_trackdir);
00700     }
00701 
00702     trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
00703 
00704     if (trackdirbits == 0) {
00705       /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */
00706       if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return;
00707 
00708       dst_tile = src_tile;
00709       src_trackdir = ReverseTrackdir(src_trackdir);
00710 
00711       trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype);
00712     }
00713   }
00714 
00715   /* Enumerate possible track */
00716   uint i = 0;
00717   while (trackdirbits != 0) {
00718     Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits);
00719     DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits);
00720 
00721     /* Check for oneway signal against us */
00722     if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) {
00723       if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir))
00724         /* if one way signal not pointing towards us, stop going in this direction. */
00725         break;
00726     }
00727     {
00728       /* We've found ourselves a neighbour :-) */
00729       AyStarNode* neighbour = &aystar->neighbours[i];
00730       neighbour->tile = dst_tile;
00731       neighbour->direction = dst_trackdir;
00732       /* Save user data */
00733       neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS];
00734       NPFFillTrackdirChoice(neighbour, current);
00735     }
00736     i++;
00737   }
00738   aystar->num_neighbours = i;
00739 }
00740 
00741 /*
00742  * Plan a route to the specified target (which is checked by target_proc),
00743  * from start1 and if not NULL, from start2 as well. The type of transport we
00744  * are checking is in type. reverse_penalty is applied to all routes that
00745  * originate from the second start node.
00746  * When we are looking for one specific target (optionally multiple tiles), we
00747  * should use a good heuristic to perform aystar search. When we search for
00748  * multiple targets that are spread around, we should perform a breadth first
00749  * search by specifiying CalcZero as our heuristic.
00750  */
00751 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)
00752 {
00753   int r;
00754   NPFFoundTargetData result;
00755 
00756   /* Initialize procs */
00757   _npf_aystar.CalculateH = heuristic_proc;
00758   _npf_aystar.EndNodeCheck = target_proc;
00759   _npf_aystar.FoundEndNode = NPFSaveTargetData;
00760   _npf_aystar.GetNeighbours = NPFFollowTrack;
00761   switch (type) {
00762     default: NOT_REACHED();
00763     case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
00764     case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
00765     case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
00766   }
00767 
00768   /* Initialize Start Node(s) */
00769   start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00770   start1->user_data[NPF_NODE_FLAGS] = 0;
00771   NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1);
00772   _npf_aystar.addstart(&_npf_aystar, start1, 0);
00773   if (start2) {
00774     start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00775     start2->user_data[NPF_NODE_FLAGS] = 0;
00776     NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2);
00777     NPFSetFlag(start2, NPF_FLAG_REVERSE, true);
00778     _npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty);
00779   }
00780 
00781   /* Initialize result */
00782   result.best_bird_dist = (uint)-1;
00783   result.best_path_dist = (uint)-1;
00784   result.best_trackdir = INVALID_TRACKDIR;
00785   _npf_aystar.user_path = &result;
00786 
00787   /* Initialize target */
00788   _npf_aystar.user_target = target;
00789 
00790   /* Initialize user_data */
00791   _npf_aystar.user_data[NPF_TYPE] = type;
00792   _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
00793   _npf_aystar.user_data[NPF_OWNER] = owner;
00794   _npf_aystar.user_data[NPF_RAILTYPES] = railtypes;
00795 
00796   /* GO! */
00797   r = AyStarMain_Main(&_npf_aystar);
00798   assert(r != AYSTAR_STILL_BUSY);
00799 
00800   if (result.best_bird_dist != 0) {
00801     if (target != NULL) {
00802       DEBUG(npf, 1, "Could not find route to tile 0x%X from 0x%X.", target->dest_coords, start1->tile);
00803     } else {
00804       /* Assumption: target == NULL, so we are looking for a depot */
00805       DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile);
00806     }
00807 
00808   }
00809   return result;
00810 }
00811 
00812 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)
00813 {
00814   AyStarNode start1;
00815   AyStarNode start2;
00816 
00817   start1.tile = tile1;
00818   start2.tile = tile2;
00819   /* We set this in case the target is also the start tile, we will just
00820    * return a not found then */
00821   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00822   start1.direction = trackdir1;
00823   start2.direction = trackdir2;
00824   start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00825 
00826   return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0);
00827 }
00828 
00829 NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
00830 {
00831   return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes);
00832 }
00833 
00834 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)
00835 {
00836   AyStarNode start1;
00837   AyStarNode start2;
00838 
00839   start1.tile = tile1;
00840   start2.tile = tile2;
00841   /* We set this in case the target is also the start tile, we will just
00842    * return a not found then */
00843   start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00844   start1.direction = trackdir1;
00845   start2.direction = trackdir2;
00846   start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00847 
00848   /* perform a breadth first search. Target is NULL,
00849    * since we are just looking for any depot...*/
00850   return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty);
00851 }
00852 
00853 NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
00854 {
00855   return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, type, sub_type, owner, railtypes, 0);
00856 }
00857 
00858 NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
00859 {
00860   /* Okay, what we're gonna do. First, we look at all depots, calculate
00861    * the manhatten distance to get to each depot. We then sort them by
00862    * distance. We start by trying to plan a route to the closest, then
00863    * the next closest, etc. We stop when the best route we have found so
00864    * far, is shorter than the manhattan distance. This will obviously
00865    * always find the closest depot. It will probably be most efficient
00866    * for ships, since the heuristic will not be to far off then. I hope.
00867    */
00868   Queue depots;
00869   int r;
00870   NPFFoundTargetData best_result = {(uint)-1, (uint)-1, INVALID_TRACKDIR, {INVALID_TILE, 0, {0, 0}}};
00871   NPFFoundTargetData result;
00872   NPFFindStationOrTileData target;
00873   AyStarNode start;
00874   Depot* current;
00875   Depot *depot;
00876 
00877   init_InsSort(&depots);
00878   /* Okay, let's find all depots that we can use first */
00879   FOR_ALL_DEPOTS(depot) {
00880     /* Check if this is really a valid depot, it is of the needed type and
00881      * owner */
00882     if (IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner))
00883       /* If so, let's add it to the queue, sorted by distance */
00884       depots.push(&depots, depot, DistanceManhattan(tile, depot->xy));
00885   }
00886 
00887   /* Now, let's initialise the aystar */
00888 
00889   /* Initialize procs */
00890   _npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic;
00891   _npf_aystar.EndNodeCheck = NPFFindStationOrTile;
00892   _npf_aystar.FoundEndNode = NPFSaveTargetData;
00893   _npf_aystar.GetNeighbours = NPFFollowTrack;
00894   switch (type) {
00895     default: NOT_REACHED();
00896     case TRANSPORT_RAIL:  _npf_aystar.CalculateG = NPFRailPathCost;  break;
00897     case TRANSPORT_ROAD:  _npf_aystar.CalculateG = NPFRoadPathCost;  break;
00898     case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break;
00899   }
00900 
00901   /* Initialize target */
00902   target.station_index = INVALID_STATION; /* We will initialize dest_coords inside the loop below */
00903   _npf_aystar.user_target = &target;
00904 
00905   /* Initialize user_data */
00906   _npf_aystar.user_data[NPF_TYPE] = type;
00907   _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type;
00908   _npf_aystar.user_data[NPF_OWNER] = owner;
00909 
00910   /* Initialize Start Node */
00911   start.tile = tile;
00912   start.direction = trackdir; /* We will initialize user_data inside the loop below */
00913 
00914   /* Initialize Result */
00915   _npf_aystar.user_path = &result;
00916   best_result.best_path_dist = (uint)-1;
00917   best_result.best_bird_dist = (uint)-1;
00918 
00919   /* Just iterate the depots in order of increasing distance */
00920   while ((current = (Depot*)depots.pop(&depots))) {
00921     /* Check to see if we already have a path shorter than this
00922      * depot's manhattan distance. HACK: We call DistanceManhattan
00923      * again, we should probably modify the queue to give us that
00924      * value... */
00925     if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist)
00926       break;
00927 
00928     /* Initialize Start Node */
00929     /* We set this in case the target is also the start tile, we will just
00930      * return a not found then */
00931     start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR;
00932     start.user_data[NPF_NODE_FLAGS] = 0;
00933     NPFSetFlag(&start, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile);
00934     _npf_aystar.addstart(&_npf_aystar, &start, 0);
00935 
00936     /* Initialize result */
00937     result.best_bird_dist = (uint)-1;
00938     result.best_path_dist = (uint)-1;
00939     result.best_trackdir = INVALID_TRACKDIR;
00940 
00941     /* Initialize target */
00942     target.dest_coords = current->xy;
00943 
00944     /* GO! */
00945     r = AyStarMain_Main(&_npf_aystar);
00946     assert(r != AYSTAR_STILL_BUSY);
00947 
00948     /* This depot is closer */
00949     if (result.best_path_dist < best_result.best_path_dist)
00950       best_result = result;
00951   }
00952   if (result.best_bird_dist != 0) {
00953     DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile);
00954   }
00955   return best_result;
00956 }
00957 
00958 void InitializeNPF()
00959 {
00960   static bool first_init = true;
00961   if (first_init) {
00962     first_init = false;
00963     init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE);
00964   } else {
00965     AyStarMain_Clear(&_npf_aystar);
00966   }
00967   _npf_aystar.loops_per_tick = 0;
00968   _npf_aystar.max_path_cost = 0;
00969   //_npf_aystar.max_search_nodes = 0;
00970   /* We will limit the number of nodes for now, until we have a better
00971    * solution to really fix performance */
00972   _npf_aystar.max_search_nodes = _patches.npf_max_search_nodes;
00973 }
00974 
00975 void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v)
00976 {
00977   /* Ships don't really reach their stations, but the tile in front. So don't
00978    * save the station id for ships. For roadvehs we don't store it either,
00979    * because multistop depends on vehicles actually reaching the exact
00980    * dest_tile, not just any stop of that station.
00981    * So only for train orders to stations we fill fstd->station_index, for all
00982    * others only dest_coords */
00983   if (v->current_order.type == OT_GOTO_STATION && v->type == VEH_TRAIN) {
00984     fstd->station_index = v->current_order.dest;
00985     /* Let's take the closest tile of the station as our target for trains */
00986     fstd->dest_coords = CalcClosestStationTile(v->current_order.dest, v->tile);
00987   } else {
00988     fstd->dest_coords = v->dest_tile;
00989     fstd->station_index = INVALID_STATION;
00990   }
00991 }

Generated on Mon Sep 22 20:34:17 2008 for openttd by  doxygen 1.5.6