matthijs@1247: #include "stdafx.h" matthijs@1247: #include "ttd.h" tron@1299: #include "debug.h" matthijs@1247: #include "npf.h" matthijs@1247: #include "aystar.h" matthijs@1247: #include "macros.h" matthijs@1247: #include "pathfind.h" matthijs@1247: #include "station.h" matthijs@1247: #include "tile.h" truelight@1313: #include "depot.h" matthijs@1247: matthijs@1247: AyStar _train_find_station; matthijs@1247: AyStar _train_find_depot; matthijs@1247: AyStar _road_find_station; matthijs@1247: AyStar _road_find_depot; matthijs@1247: AyStar _npf_aystar; matthijs@1247: matthijs@1247: /* Maps a trackdir to the bit that stores its status in the map arrays, in the matthijs@1247: * direction along with the trackdir */ matthijs@1247: const byte _signal_along_trackdir[14] = { matthijs@1247: 0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, matthijs@1247: 0x40, 0x40, 0x40, 0x10, 0x80, 0x20 matthijs@1247: }; matthijs@1247: matthijs@1247: /* Maps a trackdir to the bit that stores its status in the map arrays, in the matthijs@1247: * direction against the trackdir */ matthijs@1247: const byte _signal_against_trackdir[14] = { matthijs@1247: 0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, matthijs@1247: 0x80, 0x80, 0x80, 0x20, 0x40, 0x10 matthijs@1247: }; matthijs@1247: matthijs@1247: /* Maps a trackdir to the trackdirs that can be reached from it (ie, when matthijs@1247: * entering the next tile */ matthijs@1247: const uint16 _trackdir_reaches_trackdirs[14] = { pasky@1463: 0x1009, 0x0016, 0x1009, 0x0016, 0x0520, 0x0016, 0, 0, pasky@1463: 0x0520, 0x2A00, 0x2A00, 0x0520, 0x2A00, 0x1009 matthijs@1247: }; matthijs@1247: matthijs@1751: const uint16 _next_trackdir[14] = { matthijs@1751: 0, 1, 3, 2, 5, 4, 0, 0, matthijs@1751: 8, 9, 11, 10, 13, 12 matthijs@1751: }; matthijs@1751: matthijs@1247: /* Maps a trackdir to all trackdirs that make 90 deg turns with it. */ matthijs@1247: const uint16 _trackdir_crosses_trackdirs[14] = { pasky@1463: 0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C, 0, 0, pasky@1463: 0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C matthijs@1247: }; matthijs@1247: matthijs@1247: /* Maps a track to all tracks that make 90 deg turns with it. */ matthijs@1247: const byte _track_crosses_tracks[6] = { matthijs@1247: 0x2, /* Track 1 -> Track 2 */ matthijs@1247: 0x1, /* Track 2 -> Track 1 */ matthijs@1247: 0x30, /* Upper -> Left | Right */ matthijs@1247: 0x30, /* Lower -> Left | Right */ matthijs@1247: 0x0C, /* Left -> Upper | Lower */ matthijs@1247: 0x0C, /* Right -> Upper | Lower */ matthijs@1247: }; matthijs@1247: matthijs@1247: /* Maps a trackdir to the (4-way) direction the tile is exited when following matthijs@1247: * that trackdir */ matthijs@1247: const byte _trackdir_to_exitdir[14] = { matthijs@1247: 0,1,0,1,2,1, 0,0, matthijs@1247: 2,3,3,2,3,0, matthijs@1247: }; matthijs@1247: matthijs@1247: const byte _track_exitdir_to_trackdir[6][4] = { matthijs@1247: {0, 0xff, 8, 0xff}, matthijs@1247: {0xff, 1, 0xff, 9}, matthijs@1247: {2, 0xff, 0xff, 10}, matthijs@1247: {0xff, 3, 11, 0xf}, matthijs@1247: {0xff, 0xff, 4, 12}, matthijs@1247: {13, 5, 0xff, 0xff} matthijs@1247: }; matthijs@1247: matthijs@1247: const byte _track_direction_to_trackdir[6][8] = { matthijs@1247: {0xff, 0, 0xff, 0xff, 0xff, 8, 0xff, 0xff}, matthijs@1247: {0xff, 0xff, 0xff, 1, 0xff, 0xff, 0xff, 9}, matthijs@1247: {0xff, 0xff, 2, 0xff, 0xff, 0xff, 10, 0xff}, matthijs@1247: {0xff, 0xff, 3, 0xff, 0xff, 0xff, 11, 0xff}, matthijs@1247: {12, 0xff, 0xff, 0xff, 4, 0xff, 0xff, 0xff}, matthijs@1247: {13, 0xff, 0xff, 0xff, 5, 0xff, 0xff, 0xff} matthijs@1247: }; matthijs@1247: matthijs@1247: const byte _dir_to_diag_trackdir[4] = { matthijs@1247: 0, 1, 8, 9, matthijs@1247: }; matthijs@1247: matthijs@1247: const byte _reverse_dir[4] = { matthijs@1247: 2, 3, 0, 1 matthijs@1247: }; matthijs@1247: matthijs@1247: const byte _reverse_trackdir[14] = { matthijs@1247: 8, 9, 10, 11, 12, 13, 0xFF, 0xFF, matthijs@1247: 0, 1, 2, 3, 4, 5 matthijs@1247: }; 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@1247: static const uint _trackdir_length[14] = { 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: matthijs@1247: 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: } matthijs@1247: matthijs@1661: 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@1661: /* The value of 14 below is based on the maximum value of key2 (13) */ matthijs@1661: return ((((part1 << NPF_HASH_HALFBITS) | part2)) + (NPF_HASH_SIZE * key2 / 14)) % NPF_HASH_SIZE; matthijs@1661: } matthijs@1661: matthijs@1247: int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent) { 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: */ matthijs@1452: TileIndex CalcClosestStationTile(int station, TileIndex tile) { matthijs@1452: const Station* st = GetStation(station); matthijs@1452: matthijs@1452: int x1,x2,x3,tx; matthijs@1452: int y1,y2,y3,ty; matthijs@1452: pasky@1463: x1 = TileX(st->train_tile); y1 = TileY(st->train_tile); // topmost corner of station pasky@1463: x2 = x1 + st->trainst_w - 1; y2 = y1 + st->trainst_h - 1; // lowermost corner of station pasky@1463: x3 = TileX(tile); y3 = TileY(tile); // tile we take the distance from matthijs@1452: matthijs@1452: // we are going the aim for the x coordinate of the closest corner matthijs@1452: // but if we are between those coordinates, we will aim for our own x coordinate matthijs@1452: if (x3 < x1) matthijs@1452: tx = x1; matthijs@1452: else if (x3 > x2) matthijs@1452: tx = x2; matthijs@1452: else matthijs@1452: tx = x3; matthijs@1452: matthijs@1452: // same for y coordinate, see above comment matthijs@1452: if (y3 < y1) matthijs@1452: ty = y1; matthijs@1452: else if (y3 > y2) matthijs@1452: ty = y2; matthijs@1452: else matthijs@1452: ty = y3; matthijs@1452: matthijs@1452: // return the tile of our target coordinates matthijs@1452: return TILE_XY(tx,ty); matthijs@1452: }; 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: */ matthijs@1452: int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent) { matthijs@1452: NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; matthijs@1452: NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; matthijs@1452: TileIndex from = current->tile; matthijs@1452: TileIndex to = fstd->dest_coords; pasky@1453: uint dist; matthijs@1452: matthijs@1452: // for train-stations, we are going to aim for the closest station tile matthijs@1452: if ((as->user_data[NPF_TYPE] == TRANSPORT_RAIL) && (fstd->station_index != -1)) pasky@1453: to = CalcClosestStationTile(fstd->station_index, from); matthijs@1452: matthijs@1677: if (as->user_data[NPF_TYPE] == TRANSPORT_ROAD) matthijs@1677: /* Since roads only have diagonal pieces, we use manhattan distance here */ matthijs@1677: dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH; matthijs@1677: else matthijs@1677: /* Ships and trains can also go diagonal, so the minimum distance is shorter */ matthijs@1677: dist = DistanceTrack(from, to) * NPF_TILE_LENGTH; matthijs@1452: matthijs@1452: if (dist < ftd->best_bird_dist) { matthijs@1452: ftd->best_bird_dist = dist; matthijs@1452: ftd->best_trackdir = current->user_data[NPF_TRACKDIR_CHOICE]; matthijs@1452: } matthijs@1678: DEBUG(npf, 4)("Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist); matthijs@1452: return dist; matthijs@1452: } matthijs@1452: matthijs@1452: matthijs@1247: /* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to matthijs@1247: * get here, either getting it from the current choice or from the parent's matthijs@1247: * choice */ matthijs@1247: void NPFFillTrackdirChoice(AyStarNode* current, OpenListNode* parent) matthijs@1247: { matthijs@1247: if (parent->path.parent == NULL) { matthijs@1247: byte trackdir = current->direction; matthijs@1247: /* This is a first order decision, so we'd better save the matthijs@1247: * direction we chose */ matthijs@1247: current->user_data[NPF_TRACKDIR_CHOICE] = trackdir; matthijs@1678: DEBUG(npf, 6)("Saving trackdir: %#x", trackdir); matthijs@1247: } else { matthijs@1247: /* We've already made the decision, so just save our parent's matthijs@1247: * decision */ matthijs@1247: current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE]; matthijs@1247: } matthijs@1247: matthijs@1247: } matthijs@1247: matthijs@1247: /* Will return the cost of the tunnel. If it is an entry, it will return the matthijs@1247: * cost of that tile. If the tile is an exit, it will return the tunnel length matthijs@1247: * including the exit tile. Requires that this is a Tunnel tile */ matthijs@1247: uint NPFTunnelCost(AyStarNode* current) { matthijs@1247: byte exitdir = _trackdir_to_exitdir[current->direction]; matthijs@1247: TileIndex tile = current->tile; matthijs@1247: if ( (uint)(_map5[tile] & 3) == _reverse_dir[exitdir]) { matthijs@1247: /* We just popped out if this tunnel, since were matthijs@1247: * facing the tunnel exit */ matthijs@1247: FindLengthOfTunnelResult flotr; matthijs@1247: flotr = FindLengthOfTunnel(tile, _reverse_dir[exitdir]); matthijs@1247: return flotr.length * NPF_TILE_LENGTH; matthijs@1247: //TODO: Penalty for tunnels? matthijs@1247: } else { matthijs@1247: /* We are entering the tunnel, the enter tile is just a matthijs@1247: * straight track */ matthijs@1247: return NPF_TILE_LENGTH; matthijs@1247: } matthijs@1247: } matthijs@1247: matthijs@1247: uint NPFSlopeCost(AyStarNode* current) { matthijs@1247: TileIndex next = current->tile + TileOffsByDir(_trackdir_to_exitdir[current->direction]); matthijs@1503: int x,y; matthijs@1503: int8 z1,z2; matthijs@1503: matthijs@1503: x = TileX(current->tile) * 16; matthijs@1503: y = TileY(current->tile) * 16; matthijs@1503: z1 = GetSlopeZ(x+8, y+8); matthijs@1503: matthijs@1503: x = TileX(next) * 16; matthijs@1503: y = TileY(next) * 16; matthijs@1503: z2 = GetSlopeZ(x+8, y+8); matthijs@1503: matthijs@1503: if ((z2 - z1) > 1) { matthijs@1247: /* Slope up */ matthijs@1247: return _patches.npf_rail_slope_penalty; matthijs@1247: } matthijs@1247: return 0; matthijs@1247: /* Should we give a bonus for slope down? Probably not, we matthijs@1247: * could just substract that bonus from the penalty, because matthijs@1247: * there is only one level of steepness... */ matthijs@1247: } matthijs@1247: matthijs@1678: /* Mark tiles by mowing the grass when npf debug level >= 1 */ matthijs@1247: void NPFMarkTile(TileIndex tile) { matthijs@1678: #ifdef NO_DEBUG_MESSAGES matthijs@1678: return; matthijs@1678: #else matthijs@1678: if (_debug_npf_level >= 1) matthijs@1678: switch(GetTileType(tile)) { matthijs@1678: case MP_RAILWAY: matthijs@1678: /* DEBUG: mark visited tiles by mowing the grass under them matthijs@1678: * ;-) */ matthijs@1753: if (!IsTileDepotType(tile, TRANSPORT_RAIL)) { matthijs@1753: _map2[tile] &= ~15; /* Clear bits 0-3 */ matthijs@1753: MarkTileDirtyByTile(tile); matthijs@1753: } matthijs@1753: break; matthijs@1753: case MP_STREET: matthijs@1753: if (!IsTileDepotType(tile, TRANSPORT_ROAD)) { matthijs@1753: _map3_hi[tile] &= ~0x70; /* Clear bits 4-6 */ matthijs@1753: MarkTileDirtyByTile(tile); matthijs@1753: } matthijs@1678: break; matthijs@1678: default: matthijs@1678: break; matthijs@1678: } matthijs@1678: #endif matthijs@1247: } matthijs@1247: matthijs@1247: int32 NPFWaterPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) { matthijs@1247: //TileIndex tile = current->tile; matthijs@1247: int32 cost = 0; matthijs@1247: byte trackdir = current->direction; matthijs@1247: matthijs@1247: cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ matthijs@1247: matthijs@1751: if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(current->direction)) matthijs@1751: cost += _patches.npf_buoy_penalty; /* A small penalty for going over buoys */ matthijs@1751: matthijs@1751: if (current->direction != _next_trackdir[parent->path.node.direction]) matthijs@1751: cost += _patches.npf_water_curve_penalty; matthijs@1751: matthijs@1751: /* TODO More penalties? */ matthijs@1247: matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine the cost of this node, for road tracks */ matthijs@1247: int32 NPFRoadPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) { 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: matthijs@1247: if ((_map5[tile] & 0xF0)==0) { matthijs@1247: cost = NPFTunnelCost(current); matthijs@1247: break; matthijs@1247: } matthijs@1247: /* Fall through if above if is false, it is a bridge matthijs@1247: * then. We treat that as ordinary rail */ matthijs@1247: case MP_STREET: matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@1247: break; matthijs@1247: default: matthijs@1247: break; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine extra costs */ matthijs@1247: matthijs@1247: /* Check for slope */ matthijs@1247: cost += NPFSlopeCost(current); matthijs@1247: matthijs@1247: /* Check for turns */ matthijs@1247: //TODO matthijs@1247: matthijs@1247: NPFMarkTile(tile); matthijs@1678: DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: matthijs@1247: /* Determine the cost of this node, for railway tracks */ matthijs@1247: int32 NPFRailPathCost(AyStar* as, AyStarNode* current, OpenListNode* parent) { matthijs@1247: TileIndex tile = current->tile; matthijs@1247: byte trackdir = current->direction; matthijs@1247: int32 cost = 0; matthijs@1777: /* HACK: We create a OpenListNode manualy, so we can call EndNodeCheck */ truelight@1617: OpenListNode new_node; truelight@1617: matthijs@1247: /* Determine base length */ matthijs@1247: switch (GetTileType(tile)) { matthijs@1247: case MP_TUNNELBRIDGE: matthijs@1247: if ((_map5[tile] & 0xF0)==0) { matthijs@1247: cost = NPFTunnelCost(current); matthijs@1247: break; matthijs@1247: } matthijs@1247: /* Fall through if above if is false, it is a bridge matthijs@1247: * then. We treat that as ordinary rail */ matthijs@1247: case MP_RAILWAY: matthijs@1247: cost = _trackdir_length[trackdir]; /* Should be different for diagonal tracks */ matthijs@1247: break; matthijs@1247: case MP_STREET: /* Railway crossing */ matthijs@1247: cost = NPF_TILE_LENGTH; matthijs@1247: break; matthijs@1503: case MP_STATION: matthijs@1503: /* We give a station tile a penalty. Logically we would only matthijs@1503: * want to give station tiles that are not our destination matthijs@1503: * this penalty. This would discourage trains to drive through matthijs@1503: * busy stations. But, we can just give any station tile a matthijs@1503: * penalty, because every possible route will get this penalty matthijs@1503: * exactly once, on its end tile (if it's a station) and it matthijs@1503: * will therefore not make a difference. */ matthijs@1503: cost = NPF_TILE_LENGTH + _patches.npf_rail_station_penalty; matthijs@1503: break; matthijs@1247: default: matthijs@1247: break; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine extra costs */ matthijs@1247: matthijs@1459: /* Check for signals */ matthijs@1502: if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40 && (_map3_lo[tile] & _signal_along_trackdir[trackdir]) != 0) { matthijs@1459: /* Ordinary track with signals */ matthijs@1247: if ((_map2[tile] & _signal_along_trackdir[trackdir]) == 0) { 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? */ matthijs@1643: if ((_map3_hi[tile] & 0x3) == 0x2 || (_map3_hi[tile] & 0x3) == 0x3) matthijs@1643: /* Penalise exit and combo signals differently (heavier) */ matthijs@1643: cost += _patches.npf_rail_firstred_exit_penalty; matthijs@1643: else matthijs@1643: cost += _patches.npf_rail_firstred_penalty; 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 */ truelight@1617: new_node.path.node = *current; truelight@1617: 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@1751: if (current->direction != _next_trackdir[parent->path.node.direction]) matthijs@1460: cost += _patches.npf_rail_curve_penalty; matthijs@1460: //TODO, with realistic acceleration, also the amount of straight track between matthijs@1460: // curves should be taken into account, as this affects the speed limit. matthijs@1247: matthijs@1777: /* Check for reverse in depot */ matthijs@1777: 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) */ matthijs@1777: cost += _patches.npf_rail_depot_reverse_penalty; matthijs@1777: matthijs@1247: /* Check for occupied track */ matthijs@1247: //TODO matthijs@1247: matthijs@1247: NPFMarkTile(tile); matthijs@1678: DEBUG(npf, 4)("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: /* Will find any depot */ truelight@1617: int32 NPFFindDepot(AyStar* as, OpenListNode *current) { truelight@1617: TileIndex tile = current->path.node.tile; matthijs@1777: matthijs@1777: /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below, matthijs@1777: * since checking the cache not that much faster than the actual check */ matthijs@1330: if (IsTileDepotType(tile, as->user_data[NPF_TYPE])) matthijs@1247: return AYSTAR_FOUND_END_NODE; matthijs@1247: else matthijs@1247: return AYSTAR_DONE; matthijs@1247: } matthijs@1247: matthijs@1247: /* Will find a station identified using the NPFFindStationOrTileData */ truelight@1617: int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current) { matthijs@1464: NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; truelight@1617: AyStarNode *node = ¤t->path.node; matthijs@1464: TileIndex tile = node->tile; matthijs@1459: matthijs@1459: /* See if we checked this before */ matthijs@1459: if (NPFGetFlag(node, NPF_FLAG_TARGET_CHECKED)) matthijs@1459: return NPFGetFlag(node, NPF_FLAG_IS_TARGET); matthijs@1459: /* We're gonna check this now and store the result, let's mark that */ matthijs@1459: NPFSetFlag(node, NPF_FLAG_TARGET_CHECKED, true); matthijs@1459: matthijs@1247: /* If GetNeighbours said we could get here, we assume the station type matthijs@1247: * is correct */ matthijs@1459: if ( matthijs@1459: (fstd->station_index == -1 && tile == fstd->dest_coords) || /* We've found the tile, or */ matthijs@1247: (IsTileType(tile, MP_STATION) && _map2[tile] == fstd->station_index) /* the station */ matthijs@1459: ) { matthijs@1459: NPFSetFlag(node, NPF_FLAG_TARGET_CHECKED, true); matthijs@1459: return AYSTAR_FOUND_END_NODE; matthijs@1459: } else { matthijs@1459: NPFSetFlag(node, NPF_FLAG_TARGET_CHECKED, false); 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: */ matthijs@1247: void NPFSaveTargetData(AyStar* as, OpenListNode* current) { matthijs@1247: NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; matthijs@1247: ftd->best_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@1749: /** matthijs@1749: * Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile. matthijs@1749: * Note that there is no check if the given trackdir is actually present on matthijs@1749: * the tile! matthijs@1749: * The given trackdir is used when there are (could be) multiple rail types on matthijs@1749: * one tile. matthijs@1749: */ matthijs@1749: static inline RailType GetTileRailType(TileIndex tile, byte trackdir) matthijs@1749: { matthijs@1749: byte type = INVALID_RAILTYPE; matthijs@1749: switch (GetTileType(tile)) { matthijs@1749: case MP_RAILWAY: matthijs@1749: /* railway track */ matthijs@1749: type = _map3_lo[tile] & RAILTYPE_MASK; matthijs@1749: break; matthijs@1749: case MP_STREET: matthijs@1749: /* rail/road crossing */ matthijs@1749: if ((_map5[tile] & 0xF0) == 0x10) matthijs@1749: type = _map3_hi[tile] & RAILTYPE_MASK; matthijs@1749: break; matthijs@1749: case MP_STATION: matthijs@1749: if (IsTrainStationTile(tile)) matthijs@1749: type = _map3_lo[tile] & RAILTYPE_MASK; matthijs@1749: break; matthijs@1749: case MP_TUNNELBRIDGE: matthijs@1749: /* railway tunnel */ matthijs@1749: if ((_map5[tile] & 0xFC) == 0) type = _map3_lo[tile] & RAILTYPE_MASK; matthijs@1749: /* railway bridge ending */ matthijs@1749: if ((_map5[tile] & 0xC6) == 0x80) type = _map3_lo[tile] & RAILTYPE_MASK; matthijs@1749: /* on railway bridge */ matthijs@1749: if ((_map5[tile] & 0xC6) == 0xC0 && (_map5[tile] & 0x1) == (_trackdir_to_exitdir[trackdir] & 0x1)) matthijs@1749: type = (_map3_lo[tile] >> 4) & RAILTYPE_MASK; matthijs@1749: /* under bridge (any type) */ matthijs@1749: if ((_map5[tile] & 0xC0) == 0xC0 && (_map5[tile] & 0x1) != (trackdir & 0x1)) matthijs@1749: type = _map3_lo[tile] & RAILTYPE_MASK; matthijs@1749: break; matthijs@1749: default: matthijs@1749: break; matthijs@1749: } matthijs@1749: return type; matthijs@1749: } matthijs@1749: 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 */ matthijs@1247: void NPFFollowTrack(AyStar* aystar, OpenListNode* current) { matthijs@1247: byte src_trackdir = current->path.node.direction; matthijs@1247: TileIndex src_tile = current->path.node.tile; matthijs@1247: byte src_exitdir = _trackdir_to_exitdir[src_trackdir]; matthijs@1247: FindLengthOfTunnelResult flotr; matthijs@1247: TileIndex dst_tile; matthijs@1247: int i = 0; matthijs@1247: uint trackdirs, ts; matthijs@1247: TransportType type = aystar->user_data[NPF_TYPE]; matthijs@1247: /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */ matthijs@1247: aystar->num_neighbours = 0; matthijs@1678: DEBUG(npf, 4)("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); matthijs@1247: matthijs@1247: /* Find dest tile */ matthijs@1247: if (IsTileType(src_tile, MP_TUNNELBRIDGE) && (_map5[src_tile] & 0xF0)==0 && (_map5[src_tile] & 3) == src_exitdir) { matthijs@1247: /* This is a tunnel. We know this tunnel is our type, matthijs@1247: * otherwise we wouldn't have got here. It is also facing us, matthijs@1247: * so we should skip it's body */ matthijs@1247: flotr = FindLengthOfTunnel(src_tile, src_exitdir); matthijs@1247: dst_tile = flotr.tile; matthijs@1247: } else { matthijs@1650: if (type != TRANSPORT_WATER && (IsRoadStationTile(src_tile) || IsTileDepotType(src_tile, type))){ matthijs@1247: /* This is a road station or a train or road depot. We can enter and exit matthijs@1247: * those from one side only. Trackdirs don't support that (yet), so we'll matthijs@1247: * do this here. */ matthijs@1247: matthijs@1247: byte exitdir; matthijs@1247: /* Find out the exit direction first */ matthijs@1247: if (IsRoadStationTile(src_tile)) matthijs@1247: exitdir = GetRoadStationDir(src_tile); matthijs@1247: else /* Train or road depot. Direction is stored the same for both, in map5 */ matthijs@1650: exitdir = GetDepotDirection(src_tile, type); matthijs@1247: matthijs@1777: /* Let's see if were headed the right way into the depot, and reverse matthijs@1777: * otherwise (only for trains, since only with trains you can matthijs@1777: * (sometimes) reach tiles after reversing that you couldn't reach matthijs@1777: * without reversing. */ matthijs@1777: if (src_trackdir == _dir_to_diag_trackdir[_reverse_dir[exitdir]] && type == TRANSPORT_RAIL) matthijs@1644: /* We are headed inwards. We can only reverse here, so we'll not matthijs@1644: * consider this direction, but jump ahead to the reverse direction. matthijs@1644: * It would be nicer to return one neighbour here (the reverse matthijs@1644: * trackdir of the one we are considering now) and then considering matthijs@1644: * that one to return the tracks outside of the depot. But, because matthijs@1644: * the code layout is cleaner this way, we will just pretend we are matthijs@1644: * reversed already */ matthijs@1644: src_trackdir = _reverse_trackdir[src_trackdir]; matthijs@1247: } matthijs@1247: /* This a normal tile, a bridge, a tunnel exit, etc. */ matthijs@1247: dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDir(_trackdir_to_exitdir[src_trackdir])); matthijs@1247: if (dst_tile == INVALID_TILE) { matthijs@1247: /* We reached the border of the map */ matthijs@1247: /* TODO Nicer control flow for this */ matthijs@1247: return; matthijs@1247: } matthijs@1247: } matthijs@1247: matthijs@1749: /* check correct rail type (mono, maglev, etc) matthijs@1749: * XXX: This now compares with the previous tile, which should not pose a matthijs@1749: * problem, but it might be nicer to compare with the first tile, or even matthijs@1749: * the type of the vehicle... Maybe an NPF_RAILTYPE userdata sometime? */ matthijs@1749: if (type == TRANSPORT_RAIL) { matthijs@1749: byte src_type = GetTileRailType(src_tile, src_trackdir); matthijs@1749: byte dst_type = GetTileRailType(dst_tile, _trackdir_to_exitdir[src_trackdir]); matthijs@1749: if (src_type != dst_type) { matthijs@1749: return; matthijs@1749: } matthijs@1749: } matthijs@1330: matthijs@1330: /* Check the owner of the tile */ matthijs@1330: if ( matthijs@1650: IsTileType(dst_tile, MP_RAILWAY) /* Rail tile (also rail depot) */ matthijs@1650: || IsTrainStationTile(dst_tile) /* Rail station tile */ matthijs@1330: || IsTileDepotType(dst_tile, TRANSPORT_ROAD) /* Road depot tile */ matthijs@1650: || IsRoadStationTile(dst_tile) /* Road station tile */ matthijs@1330: || IsTileDepotType(dst_tile, TRANSPORT_WATER) /* Water depot tile */ matthijs@1330: ) /* TODO: Crossings, tunnels and bridges are "public" now */ matthijs@1330: /* The above cases are "private" tiles, we need to check the owner */ matthijs@1330: if (!IsTileOwner(dst_tile, aystar->user_data[NPF_OWNER])) matthijs@1330: return; matthijs@1247: matthijs@1247: /* Determine available tracks */ matthijs@1655: if (type != TRANSPORT_WATER && (IsRoadStationTile(dst_tile) || IsTileDepotType(dst_tile, type))){ matthijs@1655: /* Road stations and road and train depots return 0 on GTTS, so we have to do this by hand... */ matthijs@1330: byte exitdir; matthijs@1330: if (IsRoadStationTile(dst_tile)) matthijs@1330: exitdir = GetRoadStationDir(dst_tile); matthijs@1655: else /* Road or train depot */ matthijs@1650: exitdir = GetDepotDirection(dst_tile, type); matthijs@1650: /* Find the trackdirs that are available for a depot or station with this matthijs@1650: * orientation. They are only "inwards", since we are reaching this tile matthijs@1650: * from some other tile. This prevents vehicles driving into depots from matthijs@1650: * the back */ matthijs@1655: ts = (1 << _dir_to_diag_trackdir[_reverse_dir[exitdir]]); matthijs@1247: } else { matthijs@1247: ts = GetTileTrackStatus(dst_tile, type); matthijs@1247: } matthijs@1247: trackdirs = ts & 0x3F3F; /* Filter out signal status and the unused bits */ matthijs@1247: matthijs@1678: DEBUG(npf, 4)("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirs); matthijs@1247: /* Select only trackdirs we can reach from our current trackdir */ matthijs@1247: trackdirs &= _trackdir_reaches_trackdirs[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 */ matthijs@1247: trackdirs &= ~_trackdir_crosses_trackdirs[src_trackdir]; matthijs@1678: DEBUG(npf,6)("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirs); matthijs@1247: matthijs@1247: /* Enumerate possible track */ matthijs@1247: while (trackdirs != 0) { matthijs@1247: byte dst_trackdir; matthijs@1247: dst_trackdir = FindFirstBit2x64(trackdirs); matthijs@1247: trackdirs = KillFirstBit2x64(trackdirs); matthijs@1678: DEBUG(npf, 5)("Expanded into trackdir: %d, remaining trackdirs: %#x", dst_trackdir, trackdirs); matthijs@1247: matthijs@1247: /* Check for oneway signal against us */ matthijs@1247: if (IsTileType(dst_tile, MP_RAILWAY) && (_map5[dst_tile]&0xC0) == 0x40) { matthijs@1247: // the tile has a signal matthijs@1247: byte signal_present = _map3_lo[dst_tile]; matthijs@1247: if (!(signal_present & _signal_along_trackdir[dst_trackdir])) { matthijs@1247: // if one way signal not pointing towards us, stop going in this direction. matthijs@1247: if (signal_present & _signal_against_trackdir[dst_trackdir]) matthijs@1247: break; matthijs@1247: } 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: */ matthijs@1777: NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner, uint reverse_penalty) { matthijs@1247: int r; matthijs@1247: NPFFoundTargetData result; matthijs@1247: matthijs@1247: /* Initialize procs */ matthijs@1247: _npf_aystar.CalculateH = heuristic_proc; matthijs@1247: _npf_aystar.EndNodeCheck = target_proc; matthijs@1247: _npf_aystar.FoundEndNode = NPFSaveTargetData; matthijs@1247: _npf_aystar.GetNeighbours = NPFFollowTrack; matthijs@1247: if (type == TRANSPORT_RAIL) matthijs@1247: _npf_aystar.CalculateG = NPFRailPathCost; matthijs@1247: else if (type == TRANSPORT_ROAD) matthijs@1247: _npf_aystar.CalculateG = NPFRoadPathCost; matthijs@1247: else if (type == TRANSPORT_WATER) matthijs@1247: _npf_aystar.CalculateG = NPFWaterPathCost; matthijs@1247: else matthijs@1247: assert(0); matthijs@1247: matthijs@1247: /* Initialize Start Node(s) */ matthijs@1247: start1->user_data[NPF_TRACKDIR_CHOICE] = 0xff; matthijs@1247: start1->user_data[NPF_NODE_FLAGS] = 0; matthijs@1777: _npf_aystar.addstart(&_npf_aystar, start1, 0); matthijs@1247: if (start2) { matthijs@1247: start2->user_data[NPF_TRACKDIR_CHOICE] = 0xff; 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@1247: result.best_trackdir = 0xff; matthijs@1247: _npf_aystar.user_path = &result; matthijs@1247: matthijs@1247: /* Initialize target */ matthijs@1247: _npf_aystar.user_target = target; matthijs@1247: matthijs@1247: /* Initialize user_data */ matthijs@1247: _npf_aystar.user_data[NPF_TYPE] = type; matthijs@1330: _npf_aystar.user_data[NPF_OWNER] = owner; matthijs@1247: matthijs@1247: /* GO! */ matthijs@1247: r = AyStarMain_Main(&_npf_aystar); matthijs@1247: assert(r != AYSTAR_STILL_BUSY); matthijs@1247: matthijs@1247: if (result.best_bird_dist != 0) { matthijs@1247: if (target) { matthijs@1247: DEBUG(misc, 1) ("NPF: Could not find route to 0x%x from 0x%x.", target->dest_coords, start1->tile); matthijs@1247: } else { matthijs@1247: /* Assumption: target == NULL, so we are looking for a depot */ matthijs@1247: DEBUG(misc, 1) ("NPF: Could not find route to a depot from 0x%x.", start1->tile); matthijs@1247: } matthijs@1247: matthijs@1247: } matthijs@1247: return result; matthijs@1247: } matthijs@1247: matthijs@1330: NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, byte trackdir1, TileIndex tile2, byte trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner) { 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@1777: start1.user_data[NPF_TRACKDIR_CHOICE] = 0xff; matthijs@1247: start1.direction = trackdir1; matthijs@1247: start2.direction = trackdir2; matthijs@1777: start2.user_data[NPF_TRACKDIR_CHOICE] = 0xff; matthijs@1247: matthijs@1777: return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner, 0); matthijs@1247: } matthijs@1247: matthijs@1330: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, byte trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner) { matthijs@1777: return NPFRouteToStationOrTileTwoWay(tile, trackdir, INVALID_TILE, 0, target, type, owner); matthijs@1777: } matthijs@1247: matthijs@1777: NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, byte trackdir1, TileIndex tile2, byte trackdir2, TransportType type, Owner owner, uint reverse_penalty) { 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@1777: start1.user_data[NPF_TRACKDIR_CHOICE] = 0xff; matthijs@1777: start1.direction = trackdir1; matthijs@1777: start2.direction = trackdir2; matthijs@1777: start2.user_data[NPF_TRACKDIR_CHOICE] = 0xff; matthijs@1247: matthijs@1777: /* perform a breadth first search. Target is NULL, matthijs@1777: * since we are just looking for any depot...*/ matthijs@1777: return NPFRouteInternal(&start1, (IsValidTile(tile2) ? &start2 : NULL), NULL, NPFFindDepot, NPFCalcZero, type, owner, reverse_penalty); matthijs@1247: } matthijs@1247: matthijs@1330: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, byte trackdir, TransportType type, Owner owner) { matthijs@1777: return NPFRouteToDepotBreadthFirstTwoWay(tile, trackdir, INVALID_TILE, 0, type, owner, 0); matthijs@1247: } matthijs@1247: matthijs@1330: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, byte trackdir, TransportType type, Owner owner) { matthijs@1247: /* Okay, what we're gonna do. First, we look at all depots, calculate matthijs@1247: * the manhatten distance to get to each depot. We then sort them by matthijs@1247: * distance. We start by trying to plan a route to the closest, then matthijs@1247: * the next closest, etc. We stop when the best route we have found so matthijs@1247: * far, is shorter than the manhattan distance. This will obviously matthijs@1247: * always find the closest depot. It will probably be most efficient matthijs@1247: * for ships, since the heuristic will not be to far off then. I hope. matthijs@1247: */ matthijs@1247: Queue depots; matthijs@1247: int r; matthijs@1247: NPFFoundTargetData best_result; matthijs@1247: NPFFoundTargetData result; matthijs@1247: NPFFindStationOrTileData target; matthijs@1247: AyStarNode start; matthijs@1247: Depot* current; truelight@1313: Depot *depot; matthijs@1247: matthijs@1247: init_InsSort(&depots); matthijs@1247: /* Okay, let's find all depots that we can use first */ truelight@1313: FOR_ALL_DEPOTS(depot) { matthijs@1330: /* Check if this is really a valid depot, it is of the needed type and matthijs@1330: * owner */ tron@1338: if (IsValidDepot(depot) && IsTileDepotType(depot->xy, type) && IsTileOwner(depot->xy, owner)) matthijs@1330: /* If so, let's add it to the queue, sorted by distance */ truelight@1313: depots.push(&depots, depot, DistanceManhattan(tile, depot->xy)); matthijs@1247: } matthijs@1247: matthijs@1247: /* Now, let's initialise the aystar */ matthijs@1247: matthijs@1247: /* Initialize procs */ matthijs@1247: _npf_aystar.CalculateH = NPFCalcStationOrTileHeuristic; matthijs@1247: _npf_aystar.EndNodeCheck = NPFFindStationOrTile; matthijs@1247: _npf_aystar.FoundEndNode = NPFSaveTargetData; matthijs@1247: _npf_aystar.GetNeighbours = NPFFollowTrack; matthijs@1247: if (type == TRANSPORT_RAIL) matthijs@1247: _npf_aystar.CalculateG = NPFRailPathCost; matthijs@1247: else if (type == TRANSPORT_ROAD) matthijs@1247: _npf_aystar.CalculateG = NPFRoadPathCost; matthijs@1247: else if (type == TRANSPORT_WATER) matthijs@1247: _npf_aystar.CalculateG = NPFWaterPathCost; matthijs@1247: else matthijs@1247: assert(0); matthijs@1247: matthijs@1247: /* Initialize target */ matthijs@1247: target.station_index = -1; /* We will initialize dest_coords inside the loop below */ matthijs@1247: _npf_aystar.user_target = ⌖ matthijs@1247: matthijs@1247: /* Initialize user_data */ matthijs@1247: _npf_aystar.user_data[NPF_TYPE] = type; matthijs@1330: _npf_aystar.user_data[NPF_OWNER] = owner; matthijs@1247: matthijs@1247: /* Initialize Start Node */ matthijs@1247: start.tile = tile; matthijs@1247: start.direction = trackdir; /* We will initialize user_data inside the loop below */ matthijs@1247: matthijs@1247: /* Initialize Result */ matthijs@1247: _npf_aystar.user_path = &result; matthijs@1247: best_result.best_path_dist = (uint)-1; matthijs@1330: best_result.best_bird_dist = (uint)-1; matthijs@1247: matthijs@1247: /* Just iterate the depots in order of increasing distance */ matthijs@1247: while ((current = depots.pop(&depots))) { matthijs@1247: /* Check to see if we already have a path shorter than this matthijs@1330: * depot's manhattan distance. HACK: We call DistanceManhattan matthijs@1247: * again, we should probably modify the queue to give us that matthijs@1247: * value... */ matthijs@1247: if ( DistanceManhattan(tile, current->xy * NPF_TILE_LENGTH) > best_result.best_path_dist) matthijs@1247: break; matthijs@1247: matthijs@1247: /* Initialize Start Node */ matthijs@1247: /* We set this in case the target is also the start tile, we will just matthijs@1247: * return a not found then */ matthijs@1247: start.user_data[NPF_TRACKDIR_CHOICE] = 0xff; 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@1247: result.best_trackdir = 0xff; matthijs@1247: matthijs@1247: /* Initialize target */ matthijs@1247: target.dest_coords = current->xy; matthijs@1247: matthijs@1247: /* GO! */ matthijs@1247: r = AyStarMain_Main(&_npf_aystar); matthijs@1247: assert(r != AYSTAR_STILL_BUSY); matthijs@1247: matthijs@1247: /* This depot is closer */ matthijs@1247: if (result.best_path_dist < best_result.best_path_dist) matthijs@1247: best_result = result; matthijs@1247: } matthijs@1247: if (result.best_bird_dist != 0) { matthijs@1247: DEBUG(misc, 1) ("NPF: Could not find route to any depot from 0x%x.", tile); matthijs@1247: } matthijs@1247: return best_result; matthijs@1247: } matthijs@1247: matthijs@1247: void InitializeNPF(void) matthijs@1247: { matthijs@1661: init_AyStar(&_npf_aystar, NPFHash, NPF_HASH_SIZE); pasky@1463: _npf_aystar.loops_per_tick = 0; pasky@1463: _npf_aystar.max_path_cost = 0; matthijs@1700: //_npf_aystar.max_search_nodes = 0; matthijs@1700: /* We will limit the number of nodes for now, until we have a better matthijs@1700: * solution to really fix performance */ matthijs@1700: _npf_aystar.max_search_nodes = _patches.npf_max_search_nodes; pasky@1463: #if 0 pasky@1463: init_AyStar(&_train_find_station, NTPHash, 1024); pasky@1463: init_AyStar(&_train_find_depot, NTPHash, 1024); pasky@1463: init_AyStar(&_road_find_station, NTPHash, 1024); pasky@1463: init_AyStar(&_road_find_depot, NTPHash, 1024); matthijs@1247: pasky@1463: _train_find_station.loops_per_tick = 0; pasky@1463: _train_find_depot.loops_per_tick = 0; pasky@1463: _road_find_station.loops_per_tick = 0; pasky@1463: _road_find_depot.loops_per_tick = 0; matthijs@1247: pasky@1463: _train_find_station.max_path_cost = 0; pasky@1463: _train_find_depot.max_path_cost = 0; pasky@1463: _road_find_station.max_path_cost = 0; pasky@1463: _road_find_depot.max_path_cost = 0; matthijs@1247: pasky@1463: _train_find_station.max_search_nodes = 0; pasky@1463: _train_find_depot.max_search_nodes = 0; pasky@1463: _road_find_station.max_search_nodes = 0; pasky@1463: _road_find_depot.max_search_nodes = 0; matthijs@1247: pasky@1463: _train_find_station.CalculateG = NPFRailPathCost; pasky@1463: _train_find_depot.CalculateG = NPFRailPathCost; pasky@1463: _road_find_station.CalculateG = NPFRoadPathCost; pasky@1463: _road_find_depot.CalculateG = NPFRoadPathCost; pasky@1463: pasky@1463: _train_find_station.CalculateH = NPFCalcStationHeuristic; pasky@1463: _train_find_depot.CalculateH = NPFCalcStationHeuristic; pasky@1463: _road_find_station.CalculateH = NPFCalcStationHeuristic; pasky@1463: _road_find_depot.CalculateH = NPFCalcStationHeuristic; pasky@1463: pasky@1463: _train_find_station.EndNodeCheck = NPFFindStationOrTile; pasky@1463: _train_find_depot.EndNodeCheck = NPFFindStationOrTile; pasky@1463: _road_find_station.EndNodeCheck = NPFFindStationOrTile; pasky@1463: _road_find_depot.EndNodeCheck = NPFFindStationOrTile; pasky@1463: pasky@1463: _train_find_station.FoundEndNode = NPFSaveTargetData; pasky@1463: _train_find_depot.FoundEndNode = NPFSaveTargetData; pasky@1463: _road_find_station.FoundEndNode = NPFSaveTargetData; pasky@1463: _road_find_depot.FoundEndNode = NPFSaveTargetData; pasky@1463: pasky@1463: _train_find_station.GetNeighbours = NPFFollowTrack; pasky@1463: _train_find_depot.GetNeighbours = NPFFollowTrack; pasky@1463: _road_find_station.GetNeighbours = NPFFollowTrack; pasky@1463: _road_find_depot.GetNeighbours = NPFFollowTrack; pasky@1463: #endif matthijs@1247: } matthijs@1247: matthijs@1247: void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v) { matthijs@1247: /* Ships don't really reach their stations, but the tile in front. So don't matthijs@1247: * save the station id for ships. For roadvehs we don't store it either, matthijs@1247: * because multistop depends on vehicles actually reaching the exact matthijs@1247: * dest_tile, not just any stop of that station. matthijs@1247: * So only for train orders to stations we fill fstd->station_index, for all matthijs@1247: * others only dest_coords */ matthijs@1247: if ((v->current_order.type) == OT_GOTO_STATION && v->type == VEH_Train) { matthijs@1247: fstd->station_index = v->current_order.station; matthijs@1452: /* Let's take the closest tile of the station as our target for trains */ matthijs@1452: fstd->dest_coords = CalcClosestStationTile(v->current_order.station, v->tile); matthijs@1247: } else { matthijs@1247: fstd->dest_coords = v->dest_tile; matthijs@1247: fstd->station_index = -1; matthijs@1247: } matthijs@1247: }