tron@2186: /* $Id$ */ tron@2186: matthijs@1247: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" matthijs@1247: #include "npf.h" matthijs@1247: #include "aystar.h" matthijs@1247: #include "macros.h" matthijs@1247: #include "pathfind.h" matthijs@1247: #include "station.h" matthijs@1247: #include "tile.h" truelight@1313: #include "depot.h" matthijs@1247: tron@1983: static AyStar _npf_aystar; matthijs@1247: matthijs@1247: /* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH, matthijs@1247: * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071 matthijs@1247: */ matthijs@1677: #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH) matthijs@1950: static const uint _trackdir_length[TRACKDIR_END] = { pasky@1463: NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, pasky@1463: 0, 0, pasky@1463: NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH matthijs@1247: }; matthijs@1247: hackykid@2008: /** matthijs@2403: * Calculates the minimum distance traveled to get from t0 to t1 when only matthijs@2403: * using tracks (ie, only making 45 degree turns). Returns the distance in the matthijs@2403: * NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to matthijs@2403: * prevent rounding. matthijs@2403: */ tron@2416: static uint NPFDistanceTrack(TileIndex t0, TileIndex t1) matthijs@2403: { matthijs@2403: const uint dx = abs(TileX(t0) - TileX(t1)); matthijs@2403: const uint dy = abs(TileY(t0) - TileY(t1)); matthijs@2403: matthijs@2403: const uint straightTracks = 2 * min(dx, dy); /* The number of straight (not full length) tracks */ matthijs@2403: /* OPTIMISATION: matthijs@2403: * Original: diagTracks = max(dx, dy) - min(dx,dy); matthijs@2403: * Proof: matthijs@2403: * (dx+dy) - straightTracks == (min + max) - straightTracks = min + max - 2 * min = max - min */ matthijs@2403: const uint diagTracks = dx + dy - straightTracks; /* The number of diagonal (full tile length) tracks. */ matthijs@2403: matthijs@2403: /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose matthijs@2403: * precision */ matthijs@2403: return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH; matthijs@2403: } matthijs@2403: matthijs@2403: /** hackykid@2008: * Check if a rail track is the end of the line. Will also consider 1-way signals to be the end of a line. hackykid@2008: * @param tile The tile on which the current track is. hackykid@2029: * @param trackdir The (track)direction in which you want to look. hackykid@2029: * @param enginetype The type of the engine for which we are checking this. hackykid@2008: */ tron@2416: static bool IsEndOfLine(TileIndex tile, Trackdir trackdir, RailType enginetype) hackykid@2008: { hackykid@2008: byte exitdir = TrackdirToExitdir(trackdir); hackykid@2008: TileIndex dst_tile; hackykid@2008: uint32 ts; hackykid@2008: matthijs@2403: /* Can always go into a tunnel */ tron@2493: if (IsTileType(tile, MP_TUNNELBRIDGE) && GB(_m[tile].m5, 4, 4) == 0 && tron@2493: GB(_m[tile].m5, 0, 2) == exitdir) { hackykid@2008: return false; tron@2493: } hackykid@2008: matthijs@2403: /* Cannot go through the back of a depot */ hackykid@2009: if (IsTileDepotType(tile, TRANSPORT_RAIL) && (exitdir != GetDepotDirection(tile, TRANSPORT_RAIL))) hackykid@2009: return true; hackykid@2008: hackykid@2008: /* Calculate next tile */ hackykid@2008: dst_tile = tile + TileOffsByDir(exitdir); hackykid@2008: // determine the track status on the next tile. hackykid@2008: ts = GetTileTrackStatus(dst_tile, TRANSPORT_RAIL) & TrackdirReachesTrackdirs(trackdir); hackykid@2008: hackykid@2008: // when none of the trackdir bits are set, we cant enter the new tile hackykid@2008: if ( (ts & TRACKDIR_BIT_MASK) == 0) hackykid@2008: return true; hackykid@2008: hackykid@2008: { hackykid@2009: byte dst_type = GetTileRailType(dst_tile, exitdir); hackykid@2029: if (!IsCompatibleRail(enginetype, dst_type)) hackykid@2008: return true; hackykid@2008: if (GetTileOwner(tile) != GetTileOwner(dst_tile)) hackykid@2008: return true; hackykid@2008: matthijs@2403: /* Prevent us from entering a depot from behind */ hackykid@2009: if (IsTileDepotType(dst_tile, TRANSPORT_RAIL) && (exitdir != ReverseDiagdir(GetDepotDirection(dst_tile, TRANSPORT_RAIL)))) hackykid@2008: return true; hackykid@2008: matthijs@2403: /* Prevent us from falling off a slope into a tunnel exit */ tron@2493: if (IsTileType(dst_tile, MP_TUNNELBRIDGE) && tron@2493: GB(_m[dst_tile].m5, 4, 4) == 0 && tron@2493: (DiagDirection)GB(_m[dst_tile].m5, 0, 2) == ReverseDiagdir(exitdir)) { tron@2493: return true; tron@2493: } matthijs@2403: hackykid@2008: /* Check for oneway signal against us */ hackykid@2008: if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TYPE_SIGNALS) { hackykid@2008: if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(FindFirstBit2x64(ts))) && !HasSignalOnTrackdir(dst_tile, FindFirstBit2x64(ts))) hackykid@2008: // if one way signal not pointing towards us, stop going in this direction. hackykid@2008: return true; hackykid@2008: } hackykid@2008: hackykid@2008: return false; hackykid@2008: } tron@2182: } hackykid@2008: Darkvater@2075: #if 0 tron@1983: static uint NTPHash(uint key1, uint key2) matthijs@1247: { matthijs@1661: /* This function uses the old hash, which is fixed on 10 bits (1024 buckets) */ matthijs@1247: return PATHFIND_HASH_TILE(key1); matthijs@1247: } Darkvater@2075: #endif matthijs@1247: matthijs@1950: /** matthijs@1950: * Calculates a hash value for use in the NPF. matthijs@1950: * @param key1 The TileIndex of the tile to hash matthijs@1950: * @param key1 The Trackdir of the track on the tile. matthijs@1950: * matthijs@1950: * @todo Think of a better hash. matthijs@1950: */ tron@1983: static uint NPFHash(uint key1, uint key2) matthijs@1661: { matthijs@1661: /* TODO: think of a better hash? */ matthijs@1661: uint part1 = TileX(key1) & NPF_HASH_HALFMASK; matthijs@1661: uint part2 = TileY(key1) & NPF_HASH_HALFMASK; matthijs@1950: matthijs@1950: assert(IsValidTrackdir(key2)); matthijs@1950: assert(IsValidTile(key1)); matthijs@1950: return ((((part1 << NPF_HASH_HALFBITS) | part2)) + (NPF_HASH_SIZE * key2 / TRACKDIR_END)) % NPF_HASH_SIZE; matthijs@1661: } matthijs@1661: tron@1983: static int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent) tron@1983: { matthijs@1247: return 0; matthijs@1247: } matthijs@1247: matthijs@1452: /* Calcs the tile of given station that is closest to a given tile matthijs@1452: * for this we assume the station is a rectangle, matthijs@1452: * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h) matthijs@1247: */ tron@1983: static TileIndex CalcClosestStationTile(StationID station, TileIndex tile) tron@1983: { matthijs@1452: const Station* st = GetStation(station); matthijs@1452: tron@1983: uint minx = TileX(st->train_tile); // topmost corner of station tron@1983: uint miny = TileY(st->train_tile); tron@1983: uint maxx = minx + st->trainst_w - 1; // lowermost corner of station tron@1983: uint maxy = miny + st->trainst_h - 1; tron@1983: uint x; tron@1983: uint y; matthijs@1452: matthijs@1452: // we are going the aim for the x coordinate of the closest corner matthijs@1452: // but if we are between those coordinates, we will aim for our own x coordinate tron@1983: x = clamp(TileX(tile), minx, maxx); matthijs@1452: matthijs@1452: // same for y coordinate, see above comment tron@1983: y = clamp(TileY(tile), miny, maxy); matthijs@1452: matthijs@1452: // return the tile of our target coordinates tron@1983: return TileXY(x, y); tron@2182: } matthijs@1452: hackykid@2008: /* On PBS pathfinding runs, this is called before pathfinding ends (BeforeExit aystar callback), and will hackykid@2008: * reserve the appropriate tracks, if needed. */ tron@2416: static void NPFReservePBSPath(AyStar *as) hackykid@2008: { hackykid@2008: NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; hackykid@2008: bool eol_end = false; hackykid@2008: hackykid@2008: if (ftd->best_trackdir == 0xFF) hackykid@2008: return; hackykid@2008: hackykid@2029: if (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_EXIT) && IsEndOfLine(ftd->node.tile, ftd->node.direction, as->user_data[NPF_RAILTYPE]) && !NPFGetFlag(&ftd->node, NPF_FLAG_SEEN_SIGNAL)) { hackykid@2008: /* The path ends in an end of line, we'll need to reserve a path. hackykid@2008: * We treat and end of line as a red exit signal */ hackykid@2008: eol_end = true; hackykid@2008: NPFSetFlag(&ftd->node, NPF_FLAG_PBS_EXIT, true); hackykid@2008: if (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_TARGET_SEEN)) hackykid@2008: NPFSetFlag(&ftd->node, NPF_FLAG_PBS_RED, true); hackykid@2008: } hackykid@2008: hackykid@2008: if (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_CHOICE)) { hackykid@2008: /* there have been no choices to make on our path, we dont care if our end signal is red */ hackykid@2008: NPFSetFlag(&ftd->node, NPF_FLAG_PBS_RED, false); hackykid@2008: } hackykid@2008: hackykid@2008: if (NPFGetFlag(&ftd->node, NPF_FLAG_PBS_EXIT) && // we passed an exit signal hackykid@2008: !NPFGetFlag(&ftd->node, NPF_FLAG_PBS_BLOCKED) && // we didnt encounter reserver tracks hackykid@2008: ((as->user_data[NPF_PBS_MODE] != PBS_MODE_GREEN) || (!NPFGetFlag(&ftd->node, NPF_FLAG_PBS_RED))) ) { // our mode permits having a red exit signal, or the signal is green hackykid@2008: PathNode parent; hackykid@2008: PathNode *curr; hackykid@2008: PathNode *prev; hackykid@2008: TileIndex start = INVALID_TILE; hackykid@2008: byte trackdir = 0; hackykid@2008: hackykid@2008: parent.node = ftd->node; hackykid@2008: parent.parent = &ftd->path; hackykid@2008: curr = &parent; hackykid@2008: prev = NULL; hackykid@2008: hackykid@2008: do { hackykid@2008: if (!NPFGetFlag(&curr->node, NPF_FLAG_PBS_EXIT) || eol_end) { hackykid@2008: /* check for already reserved track on this path, if they clash with what we hackykid@2008: currently trying to reserve, we have a self-crossing path :-( */ hackykid@2008: if ((PBSTileUnavail(curr->node.tile) & (1 << curr->node.direction)) hackykid@2008: && !(PBSTileReserved(curr->node.tile) & (1 << (curr->node.direction & 7))) hackykid@2008: && (start != INVALID_TILE)) { hackykid@2008: /* It's actually quite bad if this happens, it means the pathfinder hackykid@2008: * found a path that is intersecting with itself, which is a very bad hackykid@2008: * thing in a pbs block. Also there is not much we can do about it at hackykid@2008: * this point.... hackykid@2008: * BUT, you have to have a pretty fucked up junction layout for this to happen, hackykid@2008: * so we'll just stop this train, the user will eventually notice, so he can fix it. hackykid@2008: */ hackykid@2115: PBSClearPath(start, trackdir, curr->node.tile, curr->node.direction); hackykid@2008: NPFSetFlag(&ftd->node, NPF_FLAG_PBS_BLOCKED, true); hackykid@2008: DEBUG(pbs, 1) ("PBS: Self-crossing path!!!"); hackykid@2008: return; hackykid@2008: }; hackykid@2008: hackykid@2115: PBSReserveTrack(curr->node.tile, TrackdirToTrack(curr->node.direction) ); hackykid@2008: hackykid@2115: /* we want to reserve the last tile (with the signal) on the path too hackykid@2115: also remember this tile, cause its the end of the path (where we exit the block) */ hackykid@2115: if (start == INVALID_TILE) { hackykid@2115: if (prev != NULL) { hackykid@2115: PBSReserveTrack(prev->node.tile, TrackdirToTrack(prev->node.direction) ); hackykid@2115: start = prev->node.tile; hackykid@2115: trackdir = ReverseTrackdir(prev->node.direction); hackykid@2115: } else { hackykid@2115: start = curr->node.tile; hackykid@2115: trackdir = curr->node.direction; hackykid@2115: } hackykid@2008: } hackykid@2008: } hackykid@2008: hackykid@2008: prev = curr; hackykid@2008: curr = curr->parent; hackykid@2008: } while (curr != NULL); hackykid@2115: // we remember the tile/track where this path leaves the pbs junction hackykid@2115: ftd->node.tile = start; hackykid@2115: ftd->node.direction = trackdir; hackykid@2008: } hackykid@2008: } hackykid@2008: hackykid@2008: matthijs@1677: /* Calcs the heuristic to the target station or tile. For train stations, it matthijs@1677: * takes into account the direction of approach. matthijs@1452: */ tron@1983: static int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent) tron@1983: { matthijs@1452: NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; matthijs@1452: NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; matthijs@1452: TileIndex from = current->tile; matthijs@1452: TileIndex to = fstd->dest_coords; pasky@1453: uint dist; matthijs@1452: matthijs@1452: // for train-stations, we are going to aim for the closest station tile matthijs@1452: if ((as->user_data[NPF_TYPE] == TRANSPORT_RAIL) && (fstd->station_index != -1)) pasky@1453: to = CalcClosestStationTile(fstd->station_index, from); matthijs@1452: matthijs@1677: if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) matthijs@1677: /* Since roads only have diagonal pieces, we use manhattan distance here */ matthijs@1677: dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH; matthijs@1677: else matthijs@1677: /* Ships and trains can also go diagonal, so the minimum distance is shorter */ matthijs@2403: dist = NPFDistanceTrack(from, to); matthijs@1452: hackykid@2008: DEBUG(npf, 4)("Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist); hackykid@2008: hackykid@2008: /* for pbs runs, we ignore tiles inside the pbs block for the tracking hackykid@2008: of the 'closest' tile */ hackykid@2008: if ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) hackykid@2008: && (!NPFGetFlag(current , NPF_FLAG_SEEN_SIGNAL)) hackykid@2029: && (!IsEndOfLine(current->tile, current->direction, as->user_data[NPF_RAILTYPE]))) hackykid@2008: return dist; hackykid@2008: hackykid@2008: if ((dist < ftd->best_bird_dist) || hackykid@2008: /* for pbs runs, prefer tiles that pass a green exit signal to the pbs blocks */ hackykid@2008: ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) && !NPFGetFlag(current, NPF_FLAG_PBS_RED) && NPFGetFlag(&ftd->node, NPF_FLAG_PBS_RED)) hackykid@2008: ) { matthijs@1452: ftd->best_bird_dist = dist; matthijs@1452: ftd->best_trackdir = current->user_data[NPF_TRACKDIR_CHOICE]; hackykid@2008: ftd->path = parent->path; hackykid@2008: ftd->node = *current; matthijs@1452: } matthijs@1452: return dist; matthijs@1452: } matthijs@1452: matthijs@1247: /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to matthijs@1247: * get here, either getting it from the current choice or from the parent's matthijs@1247: * choice */ tron@1983: static void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent) matthijs@1247: { matthijs@1247: if (parent->path.parent == NULL) { matthijs@1950: Trackdir trackdir = (Trackdir)current->direction; matthijs@1247: /* This is a first order decision, so we'd better save the matthijs@1247: * direction we chose */ matthijs@1247: current->user_data[NPF_TRACKDIR_CHOICE] = trackdir; matthijs@1678: DEBUG(npf, 6)("Saving trackdir: %#x", trackdir); matthijs@1247: } else { matthijs@1247: /* We've already made the decision, so just save our parent's matthijs@1247: * decision */ matthijs@1247: current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE]; matthijs@1247: } matthijs@1247: matthijs@1247: } matthijs@1247: matthijs@1247: /* Will return the cost of the tunnel. If it is an entry, it will return the matthijs@1247: * cost of that tile. If the tile is an exit, it will return the tunnel length matthijs@1247: * including the exit tile. Requires that this is a Tunnel tile */ tron@1983: static uint NPFTunnelCost(AyStarNode* current) tron@1983: { matthijs@1950: DiagDirection exitdir = TrackdirToExitdir((Trackdir)current->direction); matthijs@1247: TileIndex tile = current->tile; tron@2493: if ((DiagDirection)GB(_m[tile].m5, 0, 2) == ReverseDiagdir(exitdir)) { matthijs@1247: /* We just popped out if this tunnel, since were matthijs@1247: * facing the tunnel exit */ matthijs@1247: FindLengthOfTunnelResult flotr; matthijs@1942: flotr = FindLengthOfTunnel(tile, ReverseDiagdir(exitdir)); matthijs@1247: return flotr.length * NPF_TILE_LENGTH; matthijs@1247: //TODO: Penalty for tunnels? matthijs@1247: } else { matthijs@1247: /* We are entering the tunnel, the enter tile is just a matthijs@1247: * straight track */ matthijs@1247: return NPF_TILE_LENGTH; matthijs@1247: } matthijs@1247: } matthijs@1247: tron@1983: static uint NPFSlopeCost(AyStarNode* current) tron@1983: { matthijs@1944: TileIndex next = current->tile + TileOffsByDir(TrackdirToExitdir(current->direction)); matthijs@1503: int x,y; matthijs@1503: int8 z1,z2; matthijs@1503: matthijs@1942: x = TileX(current->tile) * TILE_SIZE; matthijs@1942: y = TileY(current->tile) * TILE_SIZE; matthijs@1942: /* get the height of the center of the current tile */ matthijs@1942: z1 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT); matthijs@1503: matthijs@1942: x = TileX(next) * TILE_SIZE; matthijs@1942: y = TileY(next) * TILE_SIZE; matthijs@1942: /* get the height of the center of the next tile */ matthijs@1942: z2 = GetSlopeZ(x+TILE_HEIGHT, y+TILE_HEIGHT); matthijs@1503: matthijs@1503: if ((z2 - z1) > 1) { matthijs@1247: /* Slope up */ matthijs@1247: return _patches.npf_rail_slope_penalty; matthijs@1247: } matthijs@1247: return 0; matthijs@1247: /* Should we give a bonus for slope down? Probably not, we matthijs@1247: * could just substract that bonus from the penalty, because matthijs@1247: * there is only one level of steepness... */ matthijs@1247: } matthijs@1247: matthijs@1678: /* Mark tiles by mowing the grass when npf debug level >= 1 */ tron@1983: static void NPFMarkTile(TileIndex tile) tron@1983: { matthijs@1678: #ifdef NO_DEBUG_MESSAGES matthijs@1678: return; matthijs@1678: #else matthijs@1678: if (_debug_npf_level >= 1) matthijs@1678: switch(GetTileType(tile)) { matthijs@1678: case MP_RAILWAY: matthijs@1678: /* DEBUG: mark visited tiles by mowing the grass under them matthijs@1678: * ;-) */ matthijs@1753: if (!IsTileDepotType(tile, TRANSPORT_RAIL)) { tron@2493: SB(_m[tile].m2, 0, 4, 0); matthijs@1753: MarkTileDirtyByTile(tile); matthijs@1753: } matthijs@1753: break; matthijs@1753: case MP_STREET: matthijs@1753: if (!IsTileDepotType(tile, TRANSPORT_ROAD)) { tron@2493: SB(_m[tile].m2, 4, 3, 0); matthijs@1753: MarkTileDirtyByTile(tile); matthijs@1753: } matthijs@1678: break; matthijs@1678: default: matthijs@1678: break; matthijs@1678: } matthijs@1678: #endif matthijs@1247: } matthijs@1247: tron@1983: static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) tron@1983: { matthijs@1247: //TileIndex tile = current->tile; matthijs@1247: int32 cost = 0; matthijs@1950: Trackdir trackdir = (Trackdir)current->direction; matthijs@1247: matthijs@1247: cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ matthijs@1247: matthijs@1950: if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) matthijs@1751: cost += _patches.npf_buoy_penalty; /* A small penalty for going over buoys */ matthijs@1751: matthijs@1950: if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) matthijs@1751: cost += _patches.npf_water_curve_penalty; matthijs@1751: matthijs@1751: /* TODO More penalties? */ matthijs@1247: matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine the cost of this node, for road tracks */ tron@1983: static int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) tron@1983: { matthijs@1247: TileIndex tile = current->tile; matthijs@1247: int32 cost = 0; matthijs@1777: matthijs@1247: /* Determine base length */ matthijs@1247: switch (GetTileType(tile)) { matthijs@1247: case MP_TUNNELBRIDGE: tron@2493: if (GB(_m[tile].m5, 4, 4) == 0) { matthijs@1247: cost = NPFTunnelCost(current); matthijs@1247: break; matthijs@1247: } matthijs@1247: /* Fall through if above if is false, it is a bridge matthijs@2006: * then. We treat that as ordinary road */ matthijs@1247: case MP_STREET: matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@2006: /* Increase the cost for level crossings */ tron@2049: if ((_m[tile].m5 & 0xF0) == 0x10) matthijs@2006: cost += _patches.npf_crossing_penalty; matthijs@1247: break; matthijs@1247: default: matthijs@1247: break; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine extra costs */ matthijs@1247: matthijs@1247: /* Check for slope */ matthijs@1247: cost += NPFSlopeCost(current); matthijs@1247: matthijs@1941: /* Check for turns. Road vehicles only really drive diagonal, turns are matthijs@1941: * represented by non-diagonal tracks */ matthijs@1941: if (!IsDiagonalTrackdir(current->direction)) matthijs@1941: cost += _patches.npf_road_curve_penalty; matthijs@1247: matthijs@1247: NPFMarkTile(tile); matthijs@1678: DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: matthijs@1247: /* Determine the cost of this node, for railway tracks */ tron@1983: static int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) tron@1983: { matthijs@1247: TileIndex tile = current->tile; matthijs@1950: Trackdir trackdir = (Trackdir)current->direction; matthijs@1247: int32 cost = 0; matthijs@1777: /* HACK: We create a OpenListNode manualy, so we can call EndNodeCheck */ truelight@1617: OpenListNode new_node; truelight@1617: matthijs@1247: /* Determine base length */ matthijs@1247: switch (GetTileType(tile)) { matthijs@1247: case MP_TUNNELBRIDGE: tron@2493: if (GB(_m[tile].m5, 4, 4) == 0) { matthijs@1247: cost = NPFTunnelCost(current); matthijs@1247: break; matthijs@1247: } matthijs@1247: /* Fall through if above if is false, it is a bridge matthijs@1247: * then. We treat that as ordinary rail */ matthijs@1247: case MP_RAILWAY: matthijs@1247: cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ matthijs@1247: break; matthijs@1247: case MP_STREET: /* Railway crossing */ matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@1247: break; matthijs@1503: case MP_STATION: matthijs@1503: /* We give a station tile a penalty. Logically we would only matthijs@1503: * want to give station tiles that are not our destination matthijs@1503: * this penalty. This would discourage trains to drive through matthijs@1503: * busy stations. But, we can just give any station tile a matthijs@1503: * penalty, because every possible route will get this penalty matthijs@1503: * exactly once, on its end tile (if it's a station) and it matthijs@1503: * will therefore not make a difference. */ matthijs@1503: cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty; matthijs@1503: break; matthijs@1247: default: matthijs@1247: break; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine extra costs */ matthijs@1247: hackykid@2008: /* Check for reserved tracks (PBS) */ hackykid@2008: if ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) && !(NPFGetFlag(current, NPF_FLAG_PBS_EXIT)) && !(NPFGetFlag(current, NPF_FLAG_PBS_BLOCKED)) && (PBSTileUnavail(tile) & (1<EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) matthijs@1459: cost += _patches.npf_rail_lastred_penalty; matthijs@1459: matthijs@1247: /* Check for slope */ matthijs@1247: cost += NPFSlopeCost(current); matthijs@1247: matthijs@1247: /* Check for turns */ matthijs@1950: if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) matthijs@1460: cost += _patches.npf_rail_curve_penalty; matthijs@1460: //TODO, with realistic acceleration, also the amount of straight track between matthijs@1460: // curves should be taken into account, as this affects the speed limit. matthijs@1247: hackykid@2008: hackykid@2008: /* Check for depots */ hackykid@2008: if (IsTileDepotType(tile, TRANSPORT_RAIL)) { matthijs@1777: /* Penalise any depot tile that is not the last tile in the path. This matthijs@1777: * _should_ penalise every occurence of reversing in a depot (and only matthijs@1777: * that) */ hackykid@2008: if (as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) hackykid@2008: cost += _patches.npf_rail_depot_reverse_penalty; hackykid@2008: hackykid@2008: /* Do we treat this depot as a pbs signal? */ hackykid@2008: if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) { hackykid@2008: if (NPFGetFlag(current, NPF_FLAG_PBS_BLOCKED)) { hackykid@2008: cost += 1000; hackykid@2008: } hackykid@2164: if (PBSIsPbsSegment(tile, ReverseTrackdir(trackdir))) { hackykid@2008: NPFSetFlag(current, NPF_FLAG_PBS_EXIT, true); hackykid@2008: NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true); hackykid@2008: } hackykid@2008: } hackykid@2008: NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false); hackykid@2008: } matthijs@1777: matthijs@1247: /* Check for occupied track */ matthijs@1247: //TODO matthijs@1247: matthijs@1247: NPFMarkTile(tile); matthijs@1678: DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: /* Will find any depot */ tron@1983: static int32 NPFFindDepot(AyStar* as, OpenListNode *current) tron@1983: { truelight@1617: TileIndex tile = current->path.node.tile; matthijs@1777: matthijs@1777: /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below, matthijs@1777: * since checking the cache not that much faster than the actual check */ matthijs@1330: if (IsTileDepotType(tile, as->user_data[NPF_TYPE])) matthijs@1247: return AYSTAR_FOUND_END_NODE; matthijs@1247: else matthijs@1247: return AYSTAR_DONE; matthijs@1247: } matthijs@1247: matthijs@1247: /* Will find a station identified using the NPFFindStationOrTileData */ tron@1983: static int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current) tron@1983: { matthijs@1464: NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; truelight@1617: AyStarNode *node = ¤t->path.node; matthijs@1464: TileIndex tile = node->tile; matthijs@1459: hackykid@2008: if (tile == 0x4611c) { hackykid@2008: tile++; hackykid@2008: tile--; hackykid@2008: } hackykid@2008: matthijs@1247: /* If GetNeighbours said we could get here, we assume the station type matthijs@1247: * is correct */ matthijs@1459: if ( matthijs@1459: (fstd->station_index == -1 && tile == fstd->dest_coords) || /* We've found the tile, or */ tron@2049: (IsTileType(tile, MP_STATION) && _m[tile].m2 == fstd->station_index) || /* the station */ hackykid@2008: (NPFGetFlag(node, NPF_FLAG_PBS_TARGET_SEEN)) /* or, we've passed it already (for pbs) */ matthijs@1459: ) { hackykid@2008: NPFSetFlag(¤t->path.node, NPF_FLAG_PBS_TARGET_SEEN, true); hackykid@2008: /* for pbs runs, only accept we've found the target if we've also found a way out of the block */ hackykid@2029: if ((as->user_data[NPF_PBS_MODE] != PBS_MODE_NONE) && !NPFGetFlag(node, NPF_FLAG_SEEN_SIGNAL) && !IsEndOfLine(node->tile, node->direction, as->user_data[NPF_RAILTYPE])) hackykid@2008: return AYSTAR_DONE; matthijs@1459: return AYSTAR_FOUND_END_NODE; matthijs@1459: } else { matthijs@1247: return AYSTAR_DONE; matthijs@1459: } matthijs@1247: } matthijs@1247: matthijs@1247: /* To be called when current contains the (shortest route to) the target node. matthijs@1247: * Will fill the contents of the NPFFoundTargetData using matthijs@1247: * AyStarNode[NPF_TRACKDIR_CHOICE]. matthijs@1247: */ tron@1983: static void NPFSaveTargetData(AyStar* as, OpenListNode* current) tron@1983: { matthijs@1247: NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; matthijs@1950: ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE]; matthijs@1247: ftd->best_path_dist = current->g; matthijs@1247: ftd->best_bird_dist = 0; matthijs@1247: ftd->node = current->path.node; hackykid@2008: ftd->path = current->path; matthijs@1247: } matthijs@1247: matthijs@1967: /** matthijs@1967: * Finds out if a given player's vehicles are allowed to enter a given tile. matthijs@1967: * @param owner The owner of the vehicle. matthijs@1967: * @param tile The tile that is about to be entered. matthijs@1967: * @param enterdir The direction from which the vehicle wants to enter the tile. matthijs@1967: * @return true if the vehicle can enter the tile. matthijs@1967: * @todo This function should be used in other places than just NPF, matthijs@1967: * maybe moved to another file too. matthijs@1967: */ tron@1983: static bool VehicleMayEnterTile(Owner owner, TileIndex tile, DiagDirection enterdir) matthijs@1967: { matthijs@1967: if ( matthijs@1967: IsTileType(tile, MP_RAILWAY) /* Rail tile (also rail depot) */ matthijs@1967: || IsTrainStationTile(tile) /* Rail station tile */ matthijs@1967: || IsTileDepotType(tile, TRANSPORT_ROAD) /* Road depot tile */ matthijs@1967: || IsRoadStationTile(tile) /* Road station tile */ matthijs@1967: || IsTileDepotType(tile, TRANSPORT_WATER) /* Water depot tile */ matthijs@1967: ) matthijs@1967: return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */ matthijs@1967: matthijs@1967: switch (GetTileType(tile)) { matthijs@1967: case MP_STREET: matthijs@1967: /* rail-road crossing : are we looking at the railway part? */ matthijs@1967: if (IsLevelCrossing(tile) && GetCrossingTransportType(tile, TrackdirToTrack(DiagdirToDiagTrackdir(enterdir))) == TRANSPORT_RAIL) matthijs@1967: return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */ matthijs@1967: break; matthijs@1967: case MP_TUNNELBRIDGE: matthijs@1967: #if 0 matthijs@1967: /* OPTIMISATION: If we are on the middle of a bridge, we will not do the cpu matthijs@1967: * intensive owner check, instead we will just assume that if the vehicle matthijs@1967: * managed to get on the bridge, it is probably allowed to :-) matthijs@1967: */ tron@2493: if ((_m[tile].m5 & 0xC6) == 0xC0 && GB(_m[tile].m5, 0, 1) == (enterdir & 0x1)) { matthijs@1967: /* on the middle part of a railway bridge: find bridge ending */ tron@2049: while (IsTileType(tile, MP_TUNNELBRIDGE) && !((_m[tile].m5 & 0xC6) == 0x80)) { tron@2493: tile += TileOffsByDir(GB(_m[tile].m5, 0, 1)); matthijs@1967: } matthijs@1967: } matthijs@1967: /* if we were on a railway middle part, we are now at a railway bridge ending */ matthijs@1967: #endif matthijs@1967: if ( tron@2049: (_m[tile].m5 & 0xFC) == 0 /* railway tunnel */ tron@2049: || (_m[tile].m5 & 0xC6) == 0x80 /* railway bridge ending */ tron@2493: || ((_m[tile].m5 & 0xF8) == 0xE0 && GB(_m[tile].m5, 0, 1) != (enterdir & 0x1)) /* railway under bridge */ matthijs@1967: ) matthijs@1967: return IsTileOwner(tile, owner); matthijs@1967: break; matthijs@1967: default: matthijs@1967: break; matthijs@1967: } matthijs@1967: matthijs@1967: return true; /* no need to check */ matthijs@1967: } matthijs@1967: matthijs@1247: /* Will just follow the results of GetTileTrackStatus concerning where we can matthijs@1247: * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and matthijs@1247: * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the matthijs@1459: * entry and exit are neighbours. Will fill matthijs@1459: * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and matthijs@1459: * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */ tron@1983: static void NPFFollowTrack(AyStar* aystar, OpenListNode* current) tron@1983: { matthijs@1950: Trackdir src_trackdir = (Trackdir)current->path.node.direction; matthijs@1247: TileIndex src_tile = current->path.node.tile; matthijs@1944: DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir); matthijs@1247: FindLengthOfTunnelResult flotr; matthijs@1247: TileIndex dst_tile; matthijs@1950: int i; matthijs@1944: TrackdirBits trackdirbits, ts; matthijs@1247: TransportType type = aystar->user_data[NPF_TYPE]; matthijs@1247: /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */ matthijs@1247: aystar->num_neighbours = 0; matthijs@1678: DEBUG(npf, 4)("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); matthijs@1247: hackykid@2008: aystar->EndNodeCheck(aystar, current); hackykid@2008: matthijs@1247: /* Find dest tile */ tron@2493: if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GB(_m[src_tile].m5, 4, 4) == 0 && tron@2493: (DiagDirection)GB(_m[src_tile].m5, 0, 2) == src_exitdir) { matthijs@1247: /* This is a tunnel. We know this tunnel is our type, matthijs@1247: * otherwise we wouldn't have got here. It is also facing us, matthijs@1247: * so we should skip it's body */ matthijs@1247: flotr = FindLengthOfTunnel(src_tile, src_exitdir); matthijs@1247: dst_tile = flotr.tile; matthijs@1247: } else { matthijs@1650: if (type != TRANSPORT_WATER && (IsRoadStationTile(src_tile) || IsTileDepotType(src_tile, type))){ matthijs@1247: /* This is a road station or a train or road depot. We can enter and exit matthijs@1247: * those from one side only. Trackdirs don't support that (yet), so we'll matthijs@1247: * do this here. */ matthijs@1247: matthijs@1950: DiagDirection exitdir; matthijs@1247: /* Find out the exit direction first */ matthijs@1247: if (IsRoadStationTile(src_tile)) matthijs@1247: exitdir = GetRoadStationDir(src_tile); matthijs@1247: else /* Train or road depot. Direction is stored the same for both, in map5 */ matthijs@1650: exitdir = GetDepotDirection(src_tile, type); matthijs@1247: matthijs@1777: /* Let's see if were headed the right way into the depot, and reverse matthijs@1777: * otherwise (only for trains, since only with trains you can matthijs@1777: * (sometimes) reach tiles after reversing that you couldn't reach matthijs@1777: * without reversing. */ matthijs@1944: if (src_trackdir == DiagdirToDiagTrackdir(ReverseDiagdir(exitdir)) && type == TRANSPORT_RAIL) matthijs@1644: /* We are headed inwards. We can only reverse here, so we'll not matthijs@1644: * consider this direction, but jump ahead to the reverse direction. matthijs@1644: * It would be nicer to return one neighbour here (the reverse matthijs@1644: * trackdir of the one we are considering now) and then considering matthijs@1644: * that one to return the tracks outside of the depot. But, because matthijs@1644: * the code layout is cleaner this way, we will just pretend we are matthijs@1644: * reversed already */ matthijs@1944: src_trackdir = ReverseTrackdir(src_trackdir); matthijs@1247: } matthijs@1247: /* This a normal tile, a bridge, a tunnel exit, etc. */ matthijs@1944: dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(TrackdirToExitdir(src_trackdir))); matthijs@1247: if (dst_tile == INVALID_TILE) { matthijs@1247: /* We reached the border of the map */ matthijs@1247: /* TODO Nicer control flow for this */ matthijs@1247: return; matthijs@1247: } matthijs@1247: } matthijs@1247: matthijs@1965: /* I can't enter a tunnel entry/exit tile from a tile above the tunnel. Note matthijs@1965: * that I can enter the tunnel from a tile below the tunnel entrance. This matthijs@1965: * solves the problem of vehicles wanting to drive off a tunnel entrance */ tron@2493: if (IsTileType(dst_tile, MP_TUNNELBRIDGE) && GB(_m[dst_tile].m5, 4, 4) == 0 && tron@2493: GetTileZ(dst_tile) < GetTileZ(src_tile)) { matthijs@1965: return; tron@2493: } matthijs@1965: matthijs@2006: /* check correct rail type (mono, maglev, etc) */ matthijs@1749: if (type == TRANSPORT_RAIL) { matthijs@2006: RailType dst_type = GetTileRailType(dst_tile, src_trackdir); matthijs@2006: if (!IsCompatibleRail(aystar->user_data[NPF_RAILTYPE], dst_type)) matthijs@1749: return; matthijs@1749: } matthijs@1330: matthijs@1330: /* Check the owner of the tile */ matthijs@1967: if (!VehicleMayEnterTile(aystar->user_data[NPF_OWNER], dst_tile, TrackdirToExitdir(src_trackdir))) { matthijs@1967: return; matthijs@1967: } matthijs@1247: matthijs@1247: /* Determine available tracks */ matthijs@1655: if (type != TRANSPORT_WATER && (IsRoadStationTile(dst_tile) || IsTileDepotType(dst_tile, type))){ matthijs@1655: /* Road stations and road and train depots return 0 on GTTS, so we have to do this by hand... */ matthijs@1950: DiagDirection exitdir; matthijs@1330: if (IsRoadStationTile(dst_tile)) matthijs@1330: exitdir = GetRoadStationDir(dst_tile); matthijs@1655: else /* Road or train depot */ matthijs@1650: exitdir = GetDepotDirection(dst_tile, type); matthijs@1650: /* Find the trackdirs that are available for a depot or station with this matthijs@1650: * orientation. They are only "inwards", since we are reaching this tile matthijs@1650: * from some other tile. This prevents vehicles driving into depots from matthijs@1650: * the back */ matthijs@1942: ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagdir(exitdir))); matthijs@1247: } else { matthijs@1247: ts = GetTileTrackStatus(dst_tile, type); matthijs@1247: } hackykid@2008: trackdirbits = ts & TRACKDIR_BIT_MASK; /* Filter out signal status and the unused bits */ matthijs@1247: matthijs@1944: DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits); matthijs@1247: /* Select only trackdirs we can reach from our current trackdir */ matthijs@1944: trackdirbits &= TrackdirReachesTrackdirs(src_trackdir); matthijs@1247: if (_patches.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) /* Filter out trackdirs that would make 90 deg turns for trains */ hackykid@2008: hackykid@2008: trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir); hackykid@2008: hackykid@2008: if (KillFirstBit2x64(trackdirbits) != 0) hackykid@2008: NPFSetFlag(¤t->path.node, NPF_FLAG_PBS_CHOICE, true); hackykid@2008: hackykid@2008: /* When looking for 'any' route, ie when already inside a pbs block, discard all tracks that would cross hackykid@2008: other reserved tracks, so we *always* will find a valid route if there is one */ hackykid@2008: if (!(NPFGetFlag(¤t->path.node, NPF_FLAG_PBS_EXIT)) && (aystar->user_data[NPF_PBS_MODE] == PBS_MODE_ANY)) hackykid@2008: trackdirbits &= ~PBSTileUnavail(dst_tile); hackykid@2008: matthijs@1944: DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirbits); matthijs@1247: matthijs@1950: i = 0; matthijs@1247: /* Enumerate possible track */ matthijs@1944: while (trackdirbits != 0) { matthijs@1950: Trackdir dst_trackdir; matthijs@1944: dst_trackdir = FindFirstBit2x64(trackdirbits); matthijs@1944: trackdirbits = KillFirstBit2x64(trackdirbits); matthijs@1944: DEBUG(npf, 5)("Expanded into trackdir: %d, remaining trackdirs: %#x", dst_trackdir, trackdirbits); matthijs@1247: matthijs@1247: /* Check for oneway signal against us */ matthijs@1944: if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TYPE_SIGNALS) { matthijs@1944: if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir)) matthijs@1247: // if one way signal not pointing towards us, stop going in this direction. matthijs@1944: break; matthijs@1247: } matthijs@1247: { matthijs@1247: /* We've found ourselves a neighbour :-) */ matthijs@1247: AyStarNode* neighbour = &aystar->neighbours[i]; matthijs@1247: neighbour->tile = dst_tile; matthijs@1247: neighbour->direction = dst_trackdir; matthijs@1247: /* Save user data */ matthijs@1247: neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS]; matthijs@1247: NPFFillTrackdirChoice(neighbour, current); matthijs@1247: } matthijs@1247: i++; matthijs@1247: } matthijs@1247: aystar->num_neighbours = i; matthijs@1247: } matthijs@1247: matthijs@1247: /* matthijs@1247: * Plan a route to the specified target (which is checked by target_proc), matthijs@1247: * from start1 and if not NULL, from start2 as well. The type of transport we matthijs@1777: * are checking is in type. reverse_penalty is applied to all routes that matthijs@1777: * originate from the second start node. matthijs@1247: * When we are looking for one specific target (optionally multiple tiles), we matthijs@1247: * should use a good heuristic to perform aystar search. When we search for matthijs@1247: * multiple targets that are spread around, we should perform a breadth first matthijs@1247: * search by specifiying CalcZero as our heuristic. matthijs@1247: */ hackykid@2008: static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner, RailType railtype, uint reverse_penalty, byte pbs_mode) tron@1983: { matthijs@1247: int r; matthijs@1247: NPFFoundTargetData result; matthijs@1247: matthijs@1247: /* Initialize procs */ matthijs@1247: _npf_aystar.CalculateH = heuristic_proc; matthijs@1247: _npf_aystar.EndNodeCheck = target_proc; matthijs@1247: _npf_aystar.FoundEndNode = NPFSaveTargetData; matthijs@1247: _npf_aystar.GetNeighbours = NPFFollowTrack; matthijs@1247: if (type == TRANSPORT_RAIL) matthijs@1247: _npf_aystar.CalculateG = NPFRailPathCost; matthijs@1247: else if (type == TRANSPORT_ROAD) matthijs@1247: _npf_aystar.CalculateG = NPFRoadPathCost; matthijs@1247: else if (type == TRANSPORT_WATER) matthijs@1247: _npf_aystar.CalculateG = NPFWaterPathCost; matthijs@1247: else matthijs@1247: assert(0); matthijs@1247: hackykid@2008: if (pbs_mode != PBS_MODE_NONE) hackykid@2008: _npf_aystar.BeforeExit = NPFReservePBSPath; hackykid@2008: else hackykid@2008: _npf_aystar.BeforeExit = NULL; hackykid@2008: matthijs@1247: /* Initialize Start Node(s) */ matthijs@1942: start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1247: start1->user_data[NPF_NODE_FLAGS] = 0; matthijs@1777: _npf_aystar.addstart(&_npf_aystar, start1, 0); matthijs@1247: if (start2) { matthijs@1942: start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1459: start2->user_data[NPF_NODE_FLAGS] = 0; matthijs@1459: NPFSetFlag(start2, NPF_FLAG_REVERSE, true); matthijs@1777: _npf_aystar.addstart(&_npf_aystar, start2, reverse_penalty); matthijs@1247: } matthijs@1247: matthijs@1247: /* Initialize result */ matthijs@1247: result.best_bird_dist = (uint)-1; matthijs@1247: result.best_path_dist = (uint)-1; matthijs@1942: result.best_trackdir = INVALID_TRACKDIR; matthijs@1247: _npf_aystar.user_path = &result; matthijs@1247: matthijs@1247: /* Initialize target */ matthijs@1247: _npf_aystar.user_target = target; matthijs@1247: matthijs@1247: /* Initialize user_data */ matthijs@1247: _npf_aystar.user_data[NPF_TYPE] = type; matthijs@1330: _npf_aystar.user_data[NPF_OWNER] = owner; matthijs@2006: _npf_aystar.user_data[NPF_RAILTYPE] = railtype; hackykid@2008: _npf_aystar.user_data[NPF_PBS_MODE] = pbs_mode; matthijs@1247: matthijs@1247: /* GO! */ matthijs@1247: r = AyStarMain_Main(&_npf_aystar); matthijs@1247: assert(r != AYSTAR_STILL_BUSY); matthijs@1247: matthijs@1247: if (result.best_bird_dist != 0) { matthijs@1247: if (target) { matthijs@1247: DEBUG(misc, 1) ("NPF: Could not find route to 0x%x from 0x%x.", target->dest_coords, start1->tile); matthijs@1247: } else { matthijs@1247: /* Assumption: target == NULL, so we are looking for a depot */ matthijs@1247: DEBUG(misc, 1) ("NPF: Could not find route to a depot from 0x%x.", start1->tile); matthijs@1247: } matthijs@1247: matthijs@1247: } matthijs@1247: return result; matthijs@1247: } matthijs@1247: hackykid@2008: NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype, byte pbs_mode) tron@1983: { matthijs@1247: AyStarNode start1; matthijs@1247: AyStarNode start2; matthijs@1247: matthijs@1247: start1.tile = tile1; matthijs@1247: start2.tile = tile2; matthijs@1777: /* We set this in case the target is also the start tile, we will just matthijs@1777: * return a not found then */ matthijs@1942: start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1247: start1.direction = trackdir1; matthijs@1247: start2.direction = trackdir2; matthijs@1942: start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1247: hackykid@2008: return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, railtype, 0, pbs_mode); matthijs@1247: } matthijs@1247: hackykid@2008: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype, byte pbs_mode) tron@1983: { hackykid@2008: return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, 0, target, type, owner, railtype, pbs_mode); matthijs@1777: } matthijs@1247: matthijs@2006: NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, Owner owner, RailType railtype, uint reverse_penalty) tron@1983: { matthijs@1777: AyStarNode start1; matthijs@1777: AyStarNode start2; matthijs@1247: matthijs@1777: start1.tile = tile1; matthijs@1777: start2.tile = tile2; matthijs@1247: /* We set this in case the target is also the start tile, we will just matthijs@1247: * return a not found then */ matthijs@1942: start1.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1777: start1.direction = trackdir1; matthijs@1777: start2.direction = trackdir2; matthijs@1942: start2.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1247: matthijs@1777: /* perform a breadth first search. Target is NULL, matthijs@1777: * since we are just looking for any depot...*/ hackykid@2008: return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, railtype, reverse_penalty, PBS_MODE_NONE); matthijs@1247: } matthijs@1247: matthijs@2006: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype) tron@1983: { matthijs@2006: return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, 0, type, owner, railtype, 0); matthijs@1247: } matthijs@1247: matthijs@2006: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype) tron@1983: { matthijs@1247: /* Okay, what we're gonna do. First, we look at all depots, calculate matthijs@1247: * the manhatten distance to get to each depot. We then sort them by matthijs@1247: * distance. We start by trying to plan a route to the closest, then matthijs@1247: * the next closest, etc. We stop when the best route we have found so matthijs@1247: * far, is shorter than the manhattan distance. This will obviously matthijs@1247: * always find the closest depot. It will probably be most efficient matthijs@1247: * for ships, since the heuristic will not be to far off then. I hope. matthijs@1247: */ matthijs@1247: Queue depots; matthijs@1247: int r; matthijs@1247: NPFFoundTargetData best_result; matthijs@1247: NPFFoundTargetData result; matthijs@1247: NPFFindStationOrTileData target; matthijs@1247: AyStarNode start; matthijs@1247: Depot* current; truelight@1313: Depot *depot; matthijs@1247: matthijs@1247: init_InsSort(&depots); matthijs@1247: /* Okay, let's find all depots that we can use first */ truelight@1313: FOR_ALL_DEPOTS(depot) { matthijs@1330: /* Check if this is really a valid depot, it is of the needed type and matthijs@1330: * owner */ tron@1338: if (IsValidDepot(depot) && IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner)) matthijs@1330: /* If so, let's add it to the queue, sorted by distance */ truelight@1313: depots.push(&depots, depot, DistanceManhattan(tile, depot->xy)); matthijs@1247: } matthijs@1247: matthijs@1247: /* Now, let's initialise the aystar */ matthijs@1247: matthijs@1247: /* Initialize procs */ matthijs@1247: _npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic; matthijs@1247: _npf_aystar.EndNodeCheck = NPFFindStationOrTile; matthijs@1247: _npf_aystar.FoundEndNode = NPFSaveTargetData; matthijs@1247: _npf_aystar.GetNeighbours = NPFFollowTrack; matthijs@1247: if (type == TRANSPORT_RAIL) matthijs@1247: _npf_aystar.CalculateG = NPFRailPathCost; matthijs@1247: else if (type == TRANSPORT_ROAD) matthijs@1247: _npf_aystar.CalculateG = NPFRoadPathCost; matthijs@1247: else if (type == TRANSPORT_WATER) matthijs@1247: _npf_aystar.CalculateG = NPFWaterPathCost; matthijs@1247: else matthijs@1247: assert(0); matthijs@1247: hackykid@2008: _npf_aystar.BeforeExit = NULL; hackykid@2008: matthijs@1247: /* Initialize target */ matthijs@1247: target.station_index = -1; /* We will initialize dest_coords inside the loop below */ matthijs@1247: _npf_aystar.user_target = ⌖ matthijs@1247: matthijs@1247: /* Initialize user_data */ matthijs@1247: _npf_aystar.user_data[NPF_TYPE] = type; matthijs@1330: _npf_aystar.user_data[NPF_OWNER] = owner; hackykid@2008: _npf_aystar.user_data[NPF_PBS_MODE] = PBS_MODE_NONE; matthijs@1247: matthijs@1247: /* Initialize Start Node */ matthijs@1247: start.tile = tile; matthijs@1247: start.direction = trackdir; /* We will initialize user_data inside the loop below */ matthijs@1247: matthijs@1247: /* Initialize Result */ matthijs@1247: _npf_aystar.user_path = &result; matthijs@1247: best_result.best_path_dist = (uint)-1; matthijs@1330: best_result.best_bird_dist = (uint)-1; matthijs@1247: matthijs@1247: /* Just iterate the depots in order of increasing distance */ matthijs@1247: while ((current = depots.pop(&depots))) { matthijs@1247: /* Check to see if we already have a path shorter than this matthijs@1330: * depot's manhattan distance. HACK: We call DistanceManhattan matthijs@1247: * again, we should probably modify the queue to give us that matthijs@1247: * value... */ matthijs@1247: if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist) matthijs@1247: break; matthijs@1247: matthijs@1247: /* Initialize Start Node */ matthijs@1247: /* We set this in case the target is also the start tile, we will just matthijs@1247: * return a not found then */ matthijs@1942: start.user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; matthijs@1247: start.user_data[NPF_NODE_FLAGS] = 0; matthijs@1777: _npf_aystar.addstart(&_npf_aystar, &start, 0); matthijs@1247: matthijs@1247: /* Initialize result */ matthijs@1247: result.best_bird_dist = (uint)-1; matthijs@1247: result.best_path_dist = (uint)-1; matthijs@1942: result.best_trackdir = INVALID_TRACKDIR; matthijs@1247: matthijs@1247: /* Initialize target */ matthijs@1247: target.dest_coords = current->xy; matthijs@1247: matthijs@1247: /* GO! */ matthijs@1247: r = AyStarMain_Main(&_npf_aystar); matthijs@1247: assert(r != AYSTAR_STILL_BUSY); matthijs@1247: matthijs@1247: /* This depot is closer */ matthijs@1247: if (result.best_path_dist < best_result.best_path_dist) matthijs@1247: best_result = result; matthijs@1247: } matthijs@1247: if (result.best_bird_dist != 0) { matthijs@1247: DEBUG(misc, 1) ("NPF: Could not find route to any depot from 0x%x.", tile); matthijs@1247: } matthijs@1247: return best_result; matthijs@1247: } matthijs@1247: matthijs@1247: void InitializeNPF(void) matthijs@1247: { matthijs@1661: init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE); pasky@1463: _npf_aystar.loops_per_tick = 0; pasky@1463: _npf_aystar.max_path_cost = 0; matthijs@1700: //_npf_aystar.max_search_nodes = 0; matthijs@1700: /* We will limit the number of nodes for now, until we have a better matthijs@1700: * solution to really fix performance */ matthijs@1700: _npf_aystar.max_search_nodes = _patches.npf_max_search_nodes; matthijs@1247: } matthijs@1247: tron@1983: void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) tron@1983: { matthijs@1247: /* Ships don't really reach their stations, but the tile in front. So don't matthijs@1247: * save the station id for ships. For roadvehs we don't store it either, matthijs@1247: * because multistop depends on vehicles actually reaching the exact matthijs@1247: * dest_tile, not just any stop of that station. matthijs@1247: * So only for train orders to stations we fill fstd->station_index, for all matthijs@1247: * others only dest_coords */ matthijs@1247: if ((v->current_order.type) == OT_GOTO_STATION && v->type == VEH_Train) { matthijs@1247: fstd->station_index = v->current_order.station; matthijs@1452: /* Let's take the closest tile of the station as our target for trains */ matthijs@1452: fstd->dest_coords = CalcClosestStationTile(v->current_order.station, v->tile); matthijs@1247: } else { matthijs@1247: fstd->dest_coords = v->dest_tile; matthijs@1247: fstd->station_index = -1; matthijs@1247: } matthijs@1247: }