tron@2186: /* $Id$ */ tron@2186: belugas@6674: /** @file npf.cpp */ belugas@6674: matthijs@1247: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3234: #include "bridge_map.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" maedhros@6949: #include "landscape.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" tron@3315: #include "station_map.h" truelight@1313: #include "depot.h" tron@3154: #include "tunnel_map.h" rubidium@5720: #include "network/network.h" tron@3957: #include "water_map.h" smatz@8579: #include "tunnelbridge_map.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: { skidd13@8466: const uint dx = Delta(TileX(t0), TileX(t1)); skidd13@8466: 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: belugas@6674: /* we are going the aim for the x coordinate of the closest corner belugas@6674: * but if we are between those coordinates, we will aim for our own x coordinate */ skidd13@8418: x = Clamp(TileX(tile), minx, maxx); matthijs@1452: belugas@6674: /* same for y coordinate, see above comment */ skidd13@8418: y = Clamp(TileY(tile), miny, maxy); matthijs@1452: belugas@6674: /* 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: belugas@6674: /* 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; smatz@8579: if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) { matthijs@1247: /* We just popped out if this tunnel, since were matthijs@1247: * facing the tunnel exit */ matthijs@1247: FindLengthOfTunnelResult flotr; tron@3147: flotr = FindLengthOfTunnel(tile, ReverseDiagDir(exitdir)); matthijs@1247: return flotr.length * NPF_TILE_LENGTH; belugas@6674: /* @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: { celestar@5573: return NPF_TILE_LENGTH * GetBridgeLength(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@8260: /* Get center of tiles */ rubidium@8260: int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2; rubidium@8260: int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2; rubidium@8260: int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2; rubidium@8260: int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2; matthijs@1503: rubidium@8260: int dx4 = (x2 - x1) / 4; rubidium@8260: int dy4 = (y2 - y1) / 4; rubidium@8260: rubidium@8260: /* Get the height on both sides of the tile edge. rubidium@8260: * Avoid testing the height on the tile-center. This will fail for halftile-foundations. rubidium@8260: */ rubidium@8260: int z1 = GetSlopeZ(x1 + dx4, y1 + dy4); rubidium@8260: int z2 = GetSlopeZ(x2 - dx4, y2 - dy4); matthijs@1503: tron@2951: 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@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 ;-) */ tron@3017: if (!IsTileDepotType(tile, TRANSPORT_RAIL)) { celestar@3531: SetRailGroundType(tile, RAIL_GROUND_BARREN); tron@3017: MarkTileDirtyByTile(tile); tron@3017: } tron@3017: break; tron@3017: rubidium@7866: case MP_ROAD: tron@3017: if (!IsTileDepotType(tile, TRANSPORT_ROAD)) { 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: { belugas@6674: /* TileIndex tile = current->tile; */ matthijs@1247: int32 cost = 0; matthijs@1950: Trackdir trackdir = (Trackdir)current->direction; matthijs@1247: belugas@6674: cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks matthijs@1247: matthijs@1950: if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) belugas@6674: 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: belugas@6674: /* @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@7866: case MP_ROAD: matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@2006: /* Increase the cost for level crossings */ tron@4000: if (IsLevelCrossing(tile)) cost += _patches.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 */ rubidium@6338: if (IsDriveThroughStopTile(tile)) cost += _patches.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)) matthijs@1941: cost += _patches.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@7866: 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. */ matthijs@1503: cost = NPF_TILE_LENGTH + _patches.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? */ glx@7266: SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir)); tron@2951: if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) { matthijs@1643: /* Penalise exit and combo signals differently (heavier) */ matthijs@1643: cost += _patches.npf_rail_firstred_exit_penalty; tron@2951: } else { matthijs@1643: cost += _patches.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)) 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; belugas@6674: /*TODO, with realistic acceleration, also the amount of straight track between belugas@6674: * curves should be taken into account, as this affects the speed limit. */ matthijs@1247: Darkvater@2916: /* Check for reverse in depot */ Darkvater@2916: if (IsTileDepotType(tile, TRANSPORT_RAIL) && 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) */ Darkvater@2916: cost += _patches.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@5838: return IsTileDepotType(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. 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: { tron@2951: if (IsTileType(tile, MP_RAILWAY) || /* Rail tile (also rail depot) */ celestar@3442: IsRailwayStationTile(tile) || /* Rail station tile */ tron@2951: IsTileDepotType(tile, TRANSPORT_ROAD) || /* Road depot tile */ rubidium@6338: IsStandardRoadStopTile(tile) || /* Road station tile (but not drive-through stops) */ tron@2951: IsTileDepotType(tile, TRANSPORT_WATER)) { /* Water depot tile */ 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@7866: 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: smatz@8584: 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: belugas@6674: 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: { tron@3957: assert(IsTileDepotType(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: 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: { 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@3295: TileIndex dst_tile = INVALID_TILE; matthijs@1950: int i; rubidium@5838: uint32 ts; rubidium@5838: TrackdirBits trackdirbits; rubidium@5838: TransportType type = (TransportType)aystar->user_data[NPF_TYPE]; rubidium@7179: uint subtype = aystar->user_data[NPF_SUB_TYPE]; celestar@5573: bool override_dst_check = false; 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: matthijs@1247: /* Find dest tile */ smatz@8584: if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { smatz@8584: /* This is a tunnel/bridge. We know this tunnel/bridge 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 */ smatz@8584: dst_tile = IsTunnel(src_tile) ? GetOtherTunnelEnd(src_tile) : GetOtherBridgeEnd(src_tile); celestar@5573: override_dst_check = true; rubidium@6338: } else if (type != TRANSPORT_WATER && (IsStandardRoadStopTile(src_tile) || IsTileDepotType(src_tile, type))) { rubidium@6338: /* This is a road station (non drive-through) or a train or road depot. We can enter and exit matthijs@3295: * those from one side only. Trackdirs don't support that (yet), so we'll matthijs@3295: * do this here. */ matthijs@1247: matthijs@3295: DiagDirection exitdir; matthijs@3295: /* Find out the exit direction first */ celestar@3404: if (IsRoadStopTile(src_tile)) { celestar@3404: exitdir = GetRoadStopDir(src_tile); belugas@6674: } else { // Train or road depot matthijs@3295: exitdir = GetDepotDirection(src_tile, type); matthijs@3295: } matthijs@1247: matthijs@3295: /* Let's see if were headed the right way into the depot */ matthijs@3295: if (src_trackdir == DiagdirToDiagTrackdir(ReverseDiagDir(exitdir))) { matthijs@3295: /* We are headed inwards. We cannot go through the back of the depot. matthijs@3295: * For rail, we can now reverse. Reversing for road vehicles is never matthijs@3295: * useful, since you cannot take paths you couldn't take before matthijs@3295: * reversing (as with rail). */ matthijs@3295: if (type == TRANSPORT_RAIL) { matthijs@3295: /* We can only reverse here, so we'll not consider this direction, but matthijs@3295: * jump ahead to the reverse direction. It would be nicer to return matthijs@3295: * one neighbour here (the reverse trackdir of the one we are matthijs@3295: * considering now) and then considering that one to return the tracks matthijs@3295: * outside of the depot. But, because the code layout is cleaner this matthijs@3295: * way, we will just pretend we are reversed already */ matthijs@1944: src_trackdir = ReverseTrackdir(src_trackdir); glx@4561: dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(exitdir)); matthijs@3295: } else { matthijs@3295: dst_tile = INVALID_TILE; /* Road vehicle heading inwards: dead end */ tron@2951: } matthijs@3309: } else { glx@4561: dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(exitdir)); matthijs@1247: } matthijs@3295: } else { matthijs@1247: /* This a normal tile, a bridge, a tunnel exit, etc. */ glx@4561: dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(TrackdirToExitdir(src_trackdir))); matthijs@3295: } matthijs@3295: if (dst_tile == INVALID_TILE) { matthijs@3295: /* We reached the border of the map */ matthijs@3295: /* TODO Nicer control flow for this */ matthijs@3295: return; 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 */ smatz@8584: if (!override_dst_check && IsTileType(dst_tile, MP_TUNNELBRIDGE) && smatz@8584: GetTunnelBridgeDirection(dst_tile) != src_exitdir) { smatz@8584: return; tron@2493: } matthijs@1965: matthijs@2006: /* check correct rail type (mono, maglev, etc) */ matthijs@1749: if (type == TRANSPORT_RAIL) { tron@6480: RailType dst_type = GetTileRailType(dst_tile); skidd13@8424: if (!HasBit(aystar->user_data[NPF_RAILTYPES], dst_type)) matthijs@1749: return; matthijs@1749: } matthijs@1330: matthijs@1330: /* Check the owner of the tile */ rubidium@5838: if (!VehicleMayEnterTile((Owner)aystar->user_data[NPF_OWNER], dst_tile, TrackdirToExitdir(src_trackdir))) { matthijs@1967: return; matthijs@1967: } matthijs@1247: matthijs@1247: /* Determine available tracks */ rubidium@6338: if (type != TRANSPORT_WATER && (IsStandardRoadStopTile(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; celestar@3404: if (IsRoadStopTile(dst_tile)) { celestar@3404: exitdir = GetRoadStopDir(dst_tile); belugas@6674: } else { // Road or train depot matthijs@1650: exitdir = GetDepotDirection(dst_tile, type); tron@2951: } 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 */ tron@3147: ts = TrackdirToTrackdirBits(DiagdirToDiagTrackdir(ReverseDiagDir(exitdir))); matthijs@1247: } else { rubidium@7179: ts = GetTileTrackStatus(dst_tile, type, subtype); matthijs@1247: } rubidium@5838: trackdirbits = (TrackdirBits)(ts & TRACKDIR_BIT_MASK); /* Filter out signal status and the unused bits */ matthijs@1247: Darkvater@5568: DEBUG(npf, 4, "Next node: (%d, %d) [%d], possible trackdirs: 0x%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 */ Darkvater@2916: trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir); hackykid@2008: Darkvater@5568: DEBUG(npf, 6, "After filtering: (%d, %d), possible trackdirs: 0x%X", TileX(dst_tile), TileY(dst_tile), trackdirbits); matthijs@1247: matthijs@1950: i = 0; matthijs@1247: /* Enumerate possible track */ 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)) belugas@6674: /* 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: */ rubidium@7179: static NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypeMask 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; 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; rubidium@7179: _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: rubidium@7179: NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask 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: rubidium@7179: return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, sub_type, owner, railtypes, 0); matthijs@1247: } matthijs@1247: rubidium@7179: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) tron@1983: { rubidium@7179: return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, target, type, sub_type, owner, railtypes); matthijs@1777: } matthijs@1247: rubidium@7179: NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypeMask 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...*/ rubidium@7179: return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, sub_type, owner, railtypes, reverse_penalty); matthijs@1247: } matthijs@1247: rubidium@7179: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes) tron@1983: { rubidium@7179: return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, INVALID_TRACKDIR, type, sub_type, owner, railtypes, 0); matthijs@1247: } matthijs@1247: rubidium@7179: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask 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 */ truelight@4346: if (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; 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; rubidium@7179: _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; 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@7586: static bool first_init = true; rubidium@7586: if (first_init) { rubidium@7586: first_init = false; rubidium@7586: init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE); rubidium@7586: } else { rubidium@7586: AyStarMain_Clear(&_npf_aystar); rubidium@7586: } 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 */ rubidium@6585: if (v->current_order.type == OT_GOTO_STATION && v->type == VEH_TRAIN) { tron@4527: fstd->station_index = v->current_order.dest; matthijs@1452: /* Let's take the closest tile of the station as our target for trains */ tron@4527: fstd->dest_coords = CalcClosestStationTile(v->current_order.dest, v->tile); matthijs@1247: } else { matthijs@1247: fstd->dest_coords = v->dest_tile; tron@3135: fstd->station_index = INVALID_STATION; matthijs@1247: } matthijs@1247: }