tron@2186: /* $Id$ */ tron@2186: rubidium@10455: /** @file npf.cpp Implementation of the NPF pathfinder. */ glx@9505: matthijs@1247: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@9723: #include "npf.h" tron@3234: #include "bridge_map.h" tron@1299: #include "debug.h" rubidium@9723: #include "tile_cmd.h" rubidium@9723: #include "bridge.h" rubidium@9599: #include "landscape.h" matthijs@1247: #include "aystar.h" matthijs@1247: #include "pathfind.h" rubidium@9837: #include "station_base.h" tron@3315: #include "station_map.h" rubidium@10249: #include "depot_base.h" rubidium@10249: #include "depot_map.h" tron@3154: #include "tunnel_map.h" rubidium@5720: #include "network/network.h" tron@3957: #include "water_map.h" rubidium@9723: #include "tunnelbridge_map.h" rubidium@9723: #include "functions.h" rubidium@9723: #include "vehicle_base.h" rubidium@9723: #include "settings_type.h" rubidium@9724: #include "tunnelbridge.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: { rubidium@9723: const uint dx = Delta(TileX(t0), TileX(t1)); rubidium@9723: const uint dy = Delta(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: tron@2951: 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. rubidium@4434: * @param key1 The TileIndex of the tile to hash rubidium@4434: * @param key2 The Trackdir of the track on the tile. matthijs@1950: * rubidium@4434: * @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: rubidium@5838: assert(IsValidTrackdir((Trackdir)key2)); matthijs@1950: assert(IsValidTile(key1)); tron@2951: 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: glx@9505: /* we are going the aim for the x coordinate of the closest corner glx@9505: * but if we are between those coordinates, we will aim for our own x coordinate */ rubidium@9722: x = Clamp(TileX(tile), minx, maxx); matthijs@1452: glx@9505: /* same for y coordinate, see above comment */ rubidium@9722: y = Clamp(TileY(tile), miny, maxy); matthijs@1452: glx@9505: /* return the tile of our target coordinates */ tron@1983: return TileXY(x, y); tron@2182: } matthijs@1452: 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: glx@9505: /* for train-stations, we are going to aim for the closest station tile */ tron@3135: if (as->user_data[NPF_TYPE] == TRANSPORT_RAIL && fstd->station_index != INVALID_STATION) pasky@1453: to = CalcClosestStationTile(fstd->station_index, from); matthijs@1452: tron@2951: 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; tron@2951: } else { matthijs@1677: /* Ships and trains can also go diagonal, so the minimum distance is shorter */ matthijs@2403: dist = NPFDistanceTrack(from, to); tron@2951: } matthijs@1452: Darkvater@5568: DEBUG(npf, 4, "Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist); hackykid@2008: Darkvater@2916: if (dist < ftd->best_bird_dist) { matthijs@1452: ftd->best_bird_dist = dist; rubidium@5838: ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE]; matthijs@1452: } matthijs@1452: return dist; matthijs@1452: } matthijs@1452: tron@2951: 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; Darkvater@5568: DEBUG(npf, 6, "Saving trackdir: 0x%X", trackdir); matthijs@1247: } else { tron@2951: /* We've already made the decision, so just save our parent's 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: /* 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; rubidium@9723: if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) { matthijs@1247: /* We just popped out if this tunnel, since were matthijs@1247: * facing the tunnel exit */ rubidium@9724: return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1); glx@9505: /* @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: celestar@5573: static inline uint NPFBridgeCost(AyStarNode *current) celestar@5573: { rubidium@9724: return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile)); celestar@5573: } celestar@5573: tron@1983: static uint NPFSlopeCost(AyStarNode* current) tron@1983: { rubidium@5838: TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir((Trackdir)current->direction)); matthijs@1503: rubidium@9722: /* Get center of tiles */ rubidium@9722: int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2; rubidium@9722: int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2; rubidium@9722: int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2; rubidium@9722: int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2; matthijs@1503: rubidium@9722: int dx4 = (x2 - x1) / 4; rubidium@9722: int dy4 = (y2 - y1) / 4; rubidium@9722: rubidium@9722: /* Get the height on both sides of the tile edge. rubidium@9722: * Avoid testing the height on the tile-center. This will fail for halftile-foundations. rubidium@9722: */ rubidium@9722: int z1 = GetSlopeZ(x1 + dx4, y1 + dy4); rubidium@9722: int z2 = GetSlopeZ(x2 - dx4, y2 - dy4); matthijs@1503: tron@2951: if (z2 - z1 > 1) { matthijs@1247: /* Slope up */ glx@10776: return _settings_game.pf.npf.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@3532: /** matthijs@3532: * Mark tiles by mowing the grass when npf debug level >= 1. matthijs@3532: * Will not work for multiplayer games, since it can (will) cause desyncs. matthijs@3532: */ tron@1983: static void NPFMarkTile(TileIndex tile) tron@1983: { tron@4077: #ifndef NO_DEBUG_MESSAGES matthijs@3532: if (_debug_npf_level < 1 || _networking) return; tron@3017: switch (GetTileType(tile)) { tron@3017: case MP_RAILWAY: tron@3017: /* DEBUG: mark visited tiles by mowing the grass under them ;-) */ rubidium@10249: if (!IsRailDepot(tile)) { celestar@3531: SetRailGroundType(tile, RAIL_GROUND_BARREN); tron@3017: MarkTileDirtyByTile(tile); tron@3017: } tron@3017: break; tron@3017: rubidium@9694: case MP_ROAD: rubidium@10249: if (!IsRoadDepot(tile)) { tron@4048: SetRoadside(tile, ROADSIDE_BARREN); tron@3017: MarkTileDirtyByTile(tile); tron@3017: } tron@3017: break; tron@3017: tron@3017: default: tron@3017: break; tron@3017: } matthijs@1678: #endif matthijs@1247: } matthijs@1247: tron@1983: static int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) tron@1983: { glx@9505: /* TileIndex tile = current->tile; */ matthijs@1247: int32 cost = 0; matthijs@1950: Trackdir trackdir = (Trackdir)current->direction; matthijs@1247: glx@9505: cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks matthijs@1247: matthijs@1950: if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) glx@10776: cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys matthijs@1751: matthijs@1950: if (current->direction != NextTrackdir((Trackdir)parent->path.node.direction)) glx@10776: cost += _settings_game.pf.npf.npf_water_curve_penalty; matthijs@1751: glx@9505: /* @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: celestar@5573: cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current); peter1138@2605: break; tron@2951: rubidium@9694: case MP_ROAD: matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@2006: /* Increase the cost for level crossings */ glx@10776: if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty; matthijs@1247: break; tron@2951: rubidium@6338: case MP_STATION: rubidium@6338: cost = NPF_TILE_LENGTH; rubidium@6338: /* Increase the cost for drive-through road stops */ glx@10776: if (IsDriveThroughStopTile(tile)) cost += _settings_game.pf.npf.npf_road_drive_through_penalty; rubidium@6338: break; rubidium@6338: 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 */ rubidium@5838: if (!IsDiagonalTrackdir((Trackdir)current->direction)) glx@10776: cost += _settings_game.pf.npf.npf_road_curve_penalty; matthijs@1247: matthijs@1247: NPFMarkTile(tile); Darkvater@5568: 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; tron@3017: /* HACK: We create a OpenListNode manually, 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: celestar@5573: cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current); celestar@5573: break; tron@2951: matthijs@1247: case MP_RAILWAY: matthijs@1247: cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ matthijs@1247: break; tron@2951: rubidium@9694: case MP_ROAD: /* Railway crossing */ matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@1247: break; tron@2951: matthijs@1503: case MP_STATION: tron@2951: /* We give a station tile a penalty. Logically we would only want to give tron@2951: * station tiles that are not our destination this penalty. This would tron@2951: * discourage trains to drive through busy stations. But, we can just tron@2951: * give any station tile a penalty, because every possible route will get tron@2951: * this penalty exactly once, on its end tile (if it's a station) and it tron@2951: * will therefore not make a difference. */ glx@10776: cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty; matthijs@1503: break; tron@2951: matthijs@1247: default: matthijs@1247: break; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine extra costs */ matthijs@1247: matthijs@1459: /* Check for signals */ matthijs@1944: if (IsTileType(tile, MP_RAILWAY) && HasSignalOnTrackdir(tile, trackdir)) { matthijs@1459: /* Ordinary track with signals */ celestar@3520: if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) { matthijs@1247: /* Signal facing us is red */ matthijs@1459: if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) { matthijs@1247: /* Penalize the first signal we matthijs@1247: * encounter, if it is red */ matthijs@1643: matthijs@1643: /* Is this a presignal exit or combo? */ rubidium@9625: SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir)); tron@2951: if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) { matthijs@1643: /* Penalise exit and combo signals differently (heavier) */ glx@10776: cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty; tron@2951: } else { glx@10776: cost += _settings_game.pf.npf.npf_rail_firstred_penalty; tron@2951: } matthijs@1247: } matthijs@1459: /* Record the state of this signal */ matthijs@1459: NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true); matthijs@1459: } else { matthijs@1459: /* Record the state of this signal */ matthijs@1459: NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false); matthijs@1247: } matthijs@1459: NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true); matthijs@1247: } matthijs@1247: matthijs@1459: /* Penalise the tile if it is a target tile and the last signal was matthijs@1459: * red */ matthijs@1950: /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell matthijs@1950: * of course... */ truelight@1617: new_node.path.node = *current; matthijs@1950: if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) glx@10776: cost += _settings_game.pf.npf.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)) glx@10776: cost += _settings_game.pf.npf.npf_rail_curve_penalty; glx@9505: /*TODO, with realistic acceleration, also the amount of straight track between glx@9505: * curves should be taken into account, as this affects the speed limit. */ matthijs@1247: Darkvater@2916: /* Check for reverse in depot */ rubidium@10249: if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) { 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) */ glx@10776: cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty; hackykid@2008: } matthijs@1777: matthijs@1247: /* Check for occupied track */ matthijs@1247: //TODO matthijs@1247: matthijs@1247: NPFMarkTile(tile); Darkvater@5568: 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: { 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 */ rubidium@10249: return IsDepotTypeTile(current->path.node.tile, (TransportType)as->user_data[NPF_TYPE]) ? tron@4000: AYSTAR_FOUND_END_NODE : 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: matthijs@1247: /* If GetNeighbours said we could get here, we assume the station type matthijs@1247: * is correct */ matthijs@1459: if ( tron@3135: (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) || /* We've found the tile, or */ tron@3315: (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) /* the station */ matthijs@1459: ) { 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; 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. glx@9732: * @param enterdir The direction in 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: */ glx@9732: static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir) matthijs@1967: { rubidium@10249: if (IsTileType(tile, MP_RAILWAY) || /* Rail tile (also rail depot) */ rubidium@10249: IsRailwayStationTile(tile) || /* Rail station tile */ rubidium@10249: IsRoadDepotTile(tile) || /* Road depot tile */ glx@9732: IsStandardRoadStopTile(tile)) { /* Road station tile (but not drive-through stops) */ matthijs@1967: return IsTileOwner(tile, owner); /* You need to own these tiles entirely to use them */ tron@2951: } matthijs@1967: matthijs@1967: switch (GetTileType(tile)) { rubidium@9694: case MP_ROAD: matthijs@1967: /* rail-road crossing : are we looking at the railway part? */ tron@3498: if (IsLevelCrossing(tile) && tron@3498: DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) { matthijs@1967: return IsTileOwner(tile, owner); /* Railway needs owner check, while the street is public */ tron@3498: } matthijs@1967: break; tron@2951: matthijs@1967: case MP_TUNNELBRIDGE: rubidium@9723: if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { matthijs@1967: return IsTileOwner(tile, owner); tron@2951: } matthijs@1967: break; tron@2951: matthijs@1967: default: matthijs@1967: break; matthijs@1967: } matthijs@1967: glx@9505: return true; // no need to check matthijs@1967: } matthijs@1967: tron@3957: tron@3957: /** tron@3957: * Returns the direction the exit of the depot on the given tile is facing. tron@3957: */ tron@3957: static DiagDirection GetDepotDirection(TileIndex tile, TransportType type) tron@3957: { rubidium@10249: assert(IsDepotTypeTile(tile, type)); tron@3957: tron@3957: switch (type) { tron@3957: case TRANSPORT_RAIL: return GetRailDepotDirection(tile); tron@3957: case TRANSPORT_ROAD: return GetRoadDepotDirection(tile); tron@3957: case TRANSPORT_WATER: return GetShipDepotDirection(tile); tron@3957: default: return INVALID_DIAGDIR; /* Not reached */ tron@3957: } tron@3957: } tron@3957: glx@9732: /** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */ glx@9732: static DiagDirection GetSingleTramBit(TileIndex tile) glx@9732: { glx@9732: if (IsNormalRoadTile(tile)) { glx@9732: RoadBits rb = GetRoadBits(tile, ROADTYPE_TRAM); glx@9732: switch (rb) { glx@9732: case ROAD_NW: return DIAGDIR_NW; glx@9732: case ROAD_SW: return DIAGDIR_SW; glx@9732: case ROAD_SE: return DIAGDIR_SE; glx@9732: case ROAD_NE: return DIAGDIR_NE; glx@9732: default: break; glx@9732: } glx@9732: } glx@9732: return INVALID_DIAGDIR; glx@9732: } glx@9732: glx@9732: /** glx@9732: * Tests if a tile can be entered or left only from one side. glx@9732: * glx@9732: * Depots, non-drive-through roadstops, and tiles with single trambits are tested. glx@9732: * glx@9732: * @param tile The tile of interest. glx@9732: * @param type The transporttype of the vehicle. glx@9732: * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. glx@9732: * @return The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions glx@9732: */ glx@9732: static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype) glx@9732: { rubidium@10249: if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type); glx@9732: glx@9732: if (type == TRANSPORT_ROAD) { glx@9732: if (IsStandardRoadStopTile(tile)) return GetRoadStopDir(tile); glx@9732: if (HasBit(subtype, ROADTYPE_TRAM)) return GetSingleTramBit(tile); glx@9732: } glx@9732: glx@9732: return INVALID_DIAGDIR; glx@9732: } glx@9732: glx@9732: /** glx@9732: * Tests if a vehicle must reverse on a tile. glx@9732: * glx@9732: * @param tile The tile of interest. glx@9732: * @param dir The direction in which the vehicle drives on a tile. glx@9732: * @param type The transporttype of the vehicle. glx@9732: * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. glx@9732: * @return true iff the vehicle must reverse on the tile. glx@9732: */ glx@9732: static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype) glx@9732: { glx@9732: DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype); glx@9732: return single_entry != INVALID_DIAGDIR && single_entry != dir; glx@9732: } glx@9732: glx@9732: /** glx@9732: * Tests if a vehicle can enter a tile. glx@9732: * glx@9732: * @param tile The tile of interest. glx@9732: * @param dir The direction in which the vehicle drives onto a tile. glx@9732: * @param type The transporttype of the vehicle. glx@9732: * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. glx@9732: * @param railtypes For TRANSPORT_RAIL the compatible RailTypes of the vehicle. glx@9732: * @param owner The owner of the vehicle. glx@9732: * @return true iff the vehicle can enter the tile. glx@9732: */ glx@9732: static bool CanEnterTile(TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner) glx@9732: { glx@9732: /* Check tunnel entries and bridge ramps */ glx@9732: if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false; glx@9732: glx@9732: /* Test ownership */ glx@9732: if (!CanEnterTileOwnerCheck(owner, tile, dir)) return false; glx@9732: glx@9732: /* check correct rail type (mono, maglev, etc) */ glx@9732: if (type == TRANSPORT_RAIL) { glx@9732: RailType rail_type = GetTileRailType(tile); glx@9732: if (!HasBit(railtypes, rail_type)) return false; glx@9732: } glx@9732: glx@9732: /* Depots, standard roadstops and single tram bits can only be entered from one direction */ glx@9732: DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype); glx@9732: if (single_entry != INVALID_DIAGDIR && single_entry != ReverseDiagDir(dir)) return false; glx@9732: glx@9732: return true; glx@9732: } glx@9732: glx@9732: /** glx@9732: * Returns the driveable Trackdirs on a tile. glx@9732: * glx@9732: * One-way-roads are taken into account. Signals are not tested. glx@9732: * glx@9732: * @param dst_tile The tile of interest. glx@9732: * @param src_trackdir The direction the vehicle is currently moving. glx@9732: * @param type The transporttype of the vehicle. glx@9732: * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. glx@9732: * @return The Trackdirs the vehicle can continue moving on. glx@9732: */ glx@9732: static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype) glx@9732: { glx@9732: TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype)); glx@9732: glx@9732: if (trackdirbits == 0 && type == TRANSPORT_ROAD && HasBit(subtype, ROADTYPE_TRAM)) { glx@9732: /* GetTileTrackStatus() returns 0 for single tram bits. glx@9732: * As we cannot change it there (easily) without breaking something, change it here */ glx@9732: switch (GetSingleTramBit(dst_tile)) { glx@9732: case DIAGDIR_NE: glx@9732: case DIAGDIR_SW: glx@9732: trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW; glx@9732: break; glx@9732: glx@9732: case DIAGDIR_NW: glx@9732: case DIAGDIR_SE: glx@9732: trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE; glx@9732: break; glx@9732: glx@9732: default: break; glx@9732: } glx@9732: } glx@9732: glx@9732: DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits); glx@9732: glx@9732: /* Select only trackdirs we can reach from our current trackdir */ glx@9732: trackdirbits &= TrackdirReachesTrackdirs(src_trackdir); glx@9732: glx@9732: /* Filter out trackdirs that would make 90 deg turns for trains */ glx@10776: if (_settings_game.pf.forbid_90_deg && (type == TRANSPORT_RAIL || type == TRANSPORT_WATER)) trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir); glx@9732: glx@9732: DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits); glx@9732: glx@9732: return trackdirbits; glx@9732: } glx@9732: tron@3957: 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: { glx@9732: /* We leave src_tile on track src_trackdir in direction src_exitdir */ 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); glx@9732: glx@9732: /* Is src_tile valid, and can be used? glx@9732: * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir. glx@9732: * 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. */ glx@9732: bool ignore_src_tile = (current->path.parent == NULL && NPFGetFlag(¤t->path.node, NPF_FLAG_IGNORE_START_TILE)); glx@9732: glx@9732: /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */ rubidium@5838: TransportType type = (TransportType)aystar->user_data[NPF_TYPE]; glx@9624: uint subtype = aystar->user_data[NPF_SUB_TYPE]; glx@9732: matthijs@1247: /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */ matthijs@1247: aystar->num_neighbours = 0; Darkvater@5568: DEBUG(npf, 4, "Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); matthijs@1247: glx@9732: /* We want to determine the tile we arrive, and which choices we have there */ glx@9732: TileIndex dst_tile; glx@9732: TrackdirBits trackdirbits; glx@9732: matthijs@1247: /* Find dest tile */ glx@9732: if (ignore_src_tile) { glx@9732: /* Do not perform any checks that involve src_tile */ glx@9732: dst_tile = src_tile + TileOffsByDiagDir(src_exitdir); glx@9732: trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype); glx@9732: } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { glx@9732: /* We drive through the wormhole and arrive on the other side */ rubidium@9723: dst_tile = GetOtherTunnelBridgeEnd(src_tile); glx@9732: trackdirbits = TrackdirToTrackdirBits(src_trackdir); glx@9732: } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) { glx@9732: /* We can only reverse on this tile */ glx@9732: dst_tile = src_tile; glx@9732: src_trackdir = ReverseTrackdir(src_trackdir); glx@9732: trackdirbits = TrackdirToTrackdirBits(src_trackdir); glx@9732: } else { glx@9732: /* We leave src_tile in src_exitdir and reach dst_tile */ glx@9732: dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir)); matthijs@1247: glx@9732: 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; glx@9732: glx@9732: if (dst_tile == INVALID_TILE) { glx@9732: /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */ glx@9732: if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return; glx@9732: glx@9732: dst_tile = src_tile; glx@9732: src_trackdir = ReverseTrackdir(src_trackdir); matthijs@3295: } matthijs@1247: glx@9732: trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype); glx@9732: glx@9732: if (trackdirbits == 0) { glx@9732: /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */ glx@9732: if (type != TRANSPORT_ROAD || HasBit(subtype, ROADTYPE_TRAM)) return; glx@9732: glx@9732: dst_tile = src_tile; glx@9732: src_trackdir = ReverseTrackdir(src_trackdir); glx@9732: glx@9732: trackdirbits = GetDriveableTrackdirBits(dst_tile, src_trackdir, type, subtype); matthijs@1247: } tron@2493: } matthijs@1965: matthijs@1247: /* Enumerate possible track */ glx@9732: uint i = 0; matthijs@1944: while (trackdirbits != 0) { KUDr@5849: Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits); Darkvater@5568: DEBUG(npf, 5, "Expanded into trackdir: %d, remaining trackdirs: 0x%X", dst_trackdir, trackdirbits); matthijs@1247: matthijs@1247: /* Check for oneway signal against us */ rubidium@3792: if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) { matthijs@1944: if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir)) glx@9505: /* 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: */ glx@9732: 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) 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; tron@4000: switch (type) { tron@4000: default: NOT_REACHED(); tron@4000: case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break; tron@4000: case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break; tron@4000: case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break; tron@4000: } matthijs@1247: 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; glx@9732: NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1); 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; glx@9732: NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2); 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; glx@9624: _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type; matthijs@1330: _npf_aystar.user_data[NPF_OWNER] = owner; celestar@3355: _npf_aystar.user_data[NPF_RAILTYPES] = railtypes; 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) { Darkvater@3053: if (target != NULL) { Darkvater@5568: DEBUG(npf, 1, "Could not find route to tile 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 */ Darkvater@5568: DEBUG(npf, 1, "Could not find route to a depot from tile 0x%X.", start1->tile); matthijs@1247: } matthijs@1247: matthijs@1247: } matthijs@1247: return result; matthijs@1247: } matthijs@1247: glx@9732: 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) 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: glx@9732: return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0); matthijs@1247: } matthijs@1247: glx@9732: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) tron@1983: { glx@9732: return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, type, sub_type, owner, railtypes); matthijs@1777: } matthijs@1247: glx@9732: 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) 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...*/ glx@9732: return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : NULL), ignore_start_tile2, NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty); matthijs@1247: } matthijs@1247: glx@9732: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) tron@1983: { glx@9732: return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, type, sub_type, owner, railtypes, 0); matthijs@1247: } matthijs@1247: glx@9732: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) 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; glx@4424: NPFFoundTargetData best_result = {(uint)-1, (uint)-1, INVALID_TRACKDIR, {INVALID_TILE, 0, {0, 0}}}; 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 */ rubidium@10249: if (IsDepotTypeTile(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; tron@4077: switch (type) { tron@4077: default: NOT_REACHED(); tron@4077: case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break; tron@4077: case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break; tron@4077: case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break; tron@4077: } matthijs@1247: matthijs@1247: /* Initialize target */ tron@3135: target.station_index = INVALID_STATION; /* 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; glx@9624: _npf_aystar.user_data[NPF_SUB_TYPE] = sub_type; matthijs@1330: _npf_aystar.user_data[NPF_OWNER] = owner; 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 */ rubidium@5838: while ((current = (Depot*)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; glx@9732: NPFSetFlag(&start, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile); 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) { Darkvater@5568: DEBUG(npf, 1, "Could not find route to any depot from tile 0x%X.", tile); matthijs@1247: } matthijs@1247: return best_result; matthijs@1247: } matthijs@1247: rubidium@6573: void InitializeNPF() matthijs@1247: { rubidium@9631: static bool first_init = true; rubidium@9631: if (first_init) { rubidium@9631: first_init = false; rubidium@9631: init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE); rubidium@9631: } else { rubidium@9631: AyStarMain_Clear(&_npf_aystar); rubidium@9631: } 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 */ glx@10776: _npf_aystar.max_search_nodes = _settings_game.pf.npf.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 */ rubidium@9869: if (v->current_order.IsType(OT_GOTO_STATION) && v->type == VEH_TRAIN) { rubidium@9869: fstd->station_index = v->current_order.GetDestination(); matthijs@1452: /* Let's take the closest tile of the station as our target for trains */ rubidium@9869: fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile); matthijs@1247: } else { matthijs@1247: fstd->dest_coords = v->dest_tile; tron@3135: fstd->station_index = INVALID_STATION; matthijs@1247: } matthijs@1247: }