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] = { matthijs@1247: 0x1009, 0x0016, 0x1009, 0x0016, 0x0520, 0x0016, 0, 0, matthijs@1247: 0x0520, 0x2A00, 0x2A00, 0x0520, 0x2A00, 0x1009 matthijs@1247: }; matthijs@1247: matthijs@1247: /* Maps a trackdir to all trackdirs that make 90 deg turns with it. */ matthijs@1247: const uint16 _trackdir_crosses_trackdirs[14] = { matthijs@1247: 0x0202, 0x0101, 0x3030, 0x3030, 0x0C0C, 0x0C0C, 0, 0, matthijs@1247: 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@1247: #define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * 0.7071) matthijs@1247: static const uint _trackdir_length[14] = { matthijs@1247: NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, 0, 0, matthijs@1247: 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@1247: return PATHFIND_HASH_TILE(key1); matthijs@1247: } matthijs@1247: matthijs@1247: int32 NPFCalcZero(AyStar* as, AyStarNode* current, OpenListNode* parent) { matthijs@1247: return 0; matthijs@1247: } matthijs@1247: matthijs@1247: /* Calcs the heuristic to the target station (using NPFFindStationOrTileData). After matthijs@1247: * will save the heuristic into NPFFoundTargetData if it is the smallest until matthijs@1247: * now. It will then also save AyStarNode.user_data[NPF_TRACKDIR_CHOICE] in matthijs@1247: * best_trackdir matthijs@1247: */ matthijs@1247: int32 NPFCalcStationOrTileHeuristic(AyStar* as, AyStarNode* current, OpenListNode* parent) { matthijs@1247: NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; matthijs@1247: NPFFoundTargetData* ftd = (NPFFoundTargetData*)as->user_path; matthijs@1247: TileIndex from = current->tile; matthijs@1247: TileIndex to = fstd->dest_coords; matthijs@1247: uint dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH; matthijs@1247: matthijs@1247: if (dist < ftd->best_bird_dist) { matthijs@1247: ftd->best_bird_dist = dist; matthijs@1247: ftd->best_trackdir = current->user_data[NPF_TRACKDIR_CHOICE]; matthijs@1247: } matthijs@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Calculating H for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), dist); matthijs@1247: #endif matthijs@1247: return dist; matthijs@1247: } matthijs@1247: 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@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Saving trackdir: %#x", trackdir); matthijs@1247: #endif 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@1247: if (GetTileZ(next) > GetTileZ(current->tile)) { 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@1247: void NPFMarkTile(TileIndex tile) { matthijs@1247: switch(GetTileType(tile)) { matthijs@1247: case MP_RAILWAY: matthijs@1247: case MP_STREET: matthijs@1247: /* DEBUG: mark visited tiles by mowing the grass under them matthijs@1247: * ;-) */ matthijs@1247: _map2[tile] &= ~15; matthijs@1247: MarkTileDirtyByTile(tile); matthijs@1247: break; matthijs@1247: default: matthijs@1247: break; matthijs@1247: } 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@1247: /* TODO 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@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: #ifdef NPF_MARKROUTE matthijs@1247: NPFMarkTile(tile); matthijs@1247: #endif matthijs@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); matthijs@1247: #endif 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@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@1247: default: matthijs@1247: break; matthijs@1247: } matthijs@1247: matthijs@1247: /* Determine extra costs */ matthijs@1247: matthijs@1247: /* Ordinary track with signals */ matthijs@1247: if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40) { matthijs@1247: if ((_map2[tile] & _signal_along_trackdir[trackdir]) == 0) { matthijs@1247: /* Signal facing us is red */ matthijs@1247: if (!(current->user_data[NPF_NODE_FLAGS] & NPF_FLAG_SEEN_SIGNAL)) { matthijs@1247: /* Penalize the first signal we matthijs@1247: * encounter, if it is red */ matthijs@1247: cost += _patches.npf_rail_firstred_penalty; matthijs@1247: } matthijs@1247: } matthijs@1247: current->user_data[NPF_NODE_FLAGS] |= NPF_FLAG_SEEN_SIGNAL; matthijs@1247: } 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: /* Check for occupied track */ matthijs@1247: //TODO matthijs@1247: matthijs@1247: /* Check for station tiles */ matthijs@1247: if (IsTileType(tile, MP_STATION)) { matthijs@1247: /* We give a station tile a penalty. Logically we would only matthijs@1247: * want to give station tiles that are not our destination matthijs@1247: * this penalty. This would discourage trains to drive through matthijs@1247: * busy stations. But, we can just give any station tile a matthijs@1247: * penalty, because every possible route will get this penalty matthijs@1247: * exactly once, on its end tile (if it's a station) and it matthijs@1247: * will therefore not make a difference. */ matthijs@1247: cost += _patches.npf_rail_station_penalty; matthijs@1247: } matthijs@1247: matthijs@1247: #ifdef NPF_MARKROUTE matthijs@1247: NPFMarkTile(tile); matthijs@1247: #endif matthijs@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Calculating G for: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); matthijs@1247: #endif matthijs@1247: return cost; matthijs@1247: } matthijs@1247: matthijs@1247: /* Will find any depot */ matthijs@1247: int32 NPFFindDepot(AyStar* as, OpenListNode *current) { matthijs@1247: TileIndex tile = current->path.node.tile; 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 */ matthijs@1247: int32 NPFFindStationOrTile(AyStar* as, OpenListNode *current) { matthijs@1247: /* If GetNeighbours said we could get here, we assume the station type matthijs@1247: * is correct */ matthijs@1247: NPFFindStationOrTileData* fstd = (NPFFindStationOrTileData*)as->user_target; matthijs@1247: TileIndex tile = current->path.node.tile; matthijs@1247: if ( (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@1247: ) matthijs@1247: return AYSTAR_FOUND_END_NODE; matthijs@1247: else matthijs@1247: return AYSTAR_DONE; 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@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@1247: * entry and exit are neighbours. Will fill AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an matthijs@1247: * appropriate value, and copy AyStarNode.user_data[NPF_NODE_FLAGS] from the matthijs@1247: * 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@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Expanding: (%d, %d, %d) [%d]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); matthijs@1247: #endif 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@1330: if (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@1247: exitdir = _map5[src_tile] & 3; /* Extract the direction from the map */ matthijs@1247: matthijs@1247: /* Let's see if were headed the right way */ matthijs@1247: if (src_trackdir != _dir_to_diag_trackdir[exitdir]) matthijs@1247: /* Not going out of the station/depot through the exit, but the back. No matthijs@1247: * neighbours then. */ matthijs@1247: return; 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@1247: // TODO: check correct rail type (mono, maglev, etc) matthijs@1330: matthijs@1330: /* Check the owner of the tile */ matthijs@1330: if ( matthijs@1330: IsTileType(dst_tile, MP_RAILWAY) /* Rail tile */ matthijs@1330: || IsTileDepotType(dst_tile, TRANSPORT_ROAD) /* Road depot tile */ matthijs@1330: || IsTileType(dst_tile, MP_STATION) /* 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@1330: if (type == TRANSPORT_ROAD && (IsRoadStationTile(dst_tile) || IsTileDepotType(dst_tile, TRANSPORT_ROAD))){ matthijs@1247: /* Road stations and 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@1330: else /* Road depot */ matthijs@1330: /* Find the trackdirs that are available for a depot with this orientation. They are in both directions */ matthijs@1330: exitdir = _map5[dst_tile] & 3; /* Extract the direction from the map */ matthijs@1330: ts = (1 << _dir_to_diag_trackdir[exitdir]) matthijs@1330: | (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@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Next node: (%d, %d) [%d], possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirs); matthijs@1247: #endif 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@1247: #ifdef NPF_DEBUG matthijs@1247: debug("After filtering: (%d, %d), possible trackdirs: %#x", TileX(dst_tile), TileY(dst_tile), trackdirs); matthijs@1247: #endif 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@1247: #ifdef NPF_DEBUG matthijs@1247: debug("Expanded into trackdir: %d, remaining trackdirs: %#x", dst_trackdir, trackdirs); matthijs@1247: #endif 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@1247: * are checking is in type. 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@1330: NPFFoundTargetData NPFRouteInternal(AyStarNode* start1, AyStarNode* start2, NPFFindStationOrTileData* target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, Owner owner) { 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@1247: _npf_aystar.addstart(&_npf_aystar, start1); matthijs@1247: if (start2) { matthijs@1247: start2->user_data[NPF_TRACKDIR_CHOICE] = 0xff; matthijs@1247: start2->user_data[NPF_NODE_FLAGS] = NPF_FLAG_REVERSE; matthijs@1247: _npf_aystar.addstart(&_npf_aystar, start2); 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@1247: start1.direction = trackdir1; matthijs@1247: start2.direction = trackdir2; matthijs@1247: matthijs@1330: return NPFRouteInternal(&start1, &start2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner); matthijs@1247: } matthijs@1247: matthijs@1330: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, byte trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner) { matthijs@1247: AyStarNode start; matthijs@1247: matthijs@1247: assert(tile != 0); matthijs@1247: matthijs@1247: start.tile = tile; matthijs@1247: start.direction = trackdir; 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: matthijs@1330: return NPFRouteInternal(&start, NULL, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, type, owner); matthijs@1247: } matthijs@1247: matthijs@1330: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, byte trackdir, TransportType type, Owner owner) { matthijs@1247: AyStarNode start; matthijs@1247: matthijs@1247: start.tile = tile; matthijs@1247: start.direction = trackdir; 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: matthijs@1247: /* perform a breadth first search. Target is NULL, matthijs@1247: * since we are just looking for any depot...*/ matthijs@1330: return NPFRouteInternal(&start, NULL, NULL, NPFFindDepot, NPFCalcZero, type, owner); 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@1247: _npf_aystar.addstart(&_npf_aystar, &start); 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@1247: init_AyStar(&_npf_aystar, NTPHash, 1024); matthijs@1247: _npf_aystar.loops_per_tick = 0; matthijs@1247: _npf_aystar.max_path_cost = 0; matthijs@1247: _npf_aystar.max_search_nodes = 0; matthijs@1247: /* matthijs@1247: init_AyStar(&_train_find_station, NTPHash, 1024); matthijs@1247: init_AyStar(&_train_find_depot, NTPHash, 1024); matthijs@1247: init_AyStar(&_road_find_station, NTPHash, 1024); matthijs@1247: init_AyStar(&_road_find_depot, NTPHash, 1024); matthijs@1247: matthijs@1247: _train_find_station.loops_per_tick = 0; matthijs@1247: _train_find_depot.loops_per_tick = 0; matthijs@1247: _road_find_station.loops_per_tick = 0; matthijs@1247: _road_find_depot.loops_per_tick = 0; matthijs@1247: matthijs@1247: _train_find_station.max_path_cost = 0; matthijs@1247: _train_find_depot.max_path_cost = 0; matthijs@1247: _road_find_station.max_path_cost = 0; matthijs@1247: _road_find_depot.max_path_cost = 0; matthijs@1247: matthijs@1247: _train_find_station.max_search_nodes = 0; matthijs@1247: _train_find_depot.max_search_nodes = 0; matthijs@1247: _road_find_station.max_search_nodes = 0; matthijs@1247: _road_find_depot.max_search_nodes = 0; matthijs@1247: matthijs@1247: _train_find_station.CalculateG = NPFRailPathCost; matthijs@1247: _train_find_depot.CalculateG = NPFRailPathCost; matthijs@1247: _road_find_station.CalculateG = NPFRoadPathCost; matthijs@1247: _road_find_depot.CalculateG = NPFRoadPathCost; matthijs@1247: matthijs@1247: _train_find_station.CalculateH = NPFCalcStationHeuristic; matthijs@1247: _train_find_depot.CalculateH = NPFCalcStationHeuristic; matthijs@1247: _road_find_station.CalculateH = NPFCalcStationHeuristic; matthijs@1247: _road_find_depot.CalculateH = NPFCalcStationHeuristic; matthijs@1247: matthijs@1247: _train_find_station.EndNodeCheck = NPFFindStationOrTile; matthijs@1247: _train_find_depot.EndNodeCheck = NPFFindStationOrTile; matthijs@1247: _road_find_station.EndNodeCheck = NPFFindStationOrTile; matthijs@1247: _road_find_depot.EndNodeCheck = NPFFindStationOrTile; matthijs@1247: matthijs@1247: _train_find_station.FoundEndNode = NPFSaveTargetData; matthijs@1247: _train_find_depot.FoundEndNode = NPFSaveTargetData; matthijs@1247: _road_find_station.FoundEndNode = NPFSaveTargetData; matthijs@1247: _road_find_depot.FoundEndNode = NPFSaveTargetData; matthijs@1247: matthijs@1247: _train_find_station.GetNeighbours = NPFFollowTrack; matthijs@1247: _train_find_depot.GetNeighbours = NPFFollowTrack; matthijs@1247: _road_find_station.GetNeighbours = NPFFollowTrack; matthijs@1247: _road_find_depot.GetNeighbours = NPFFollowTrack; matthijs@1247: */ 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) { darkvater@1248: const Station* st = GetStation(v->current_order.station); darkvater@1248: TileIndexDiffC center = {st->trainst_w/2, st->trainst_h/2}; matthijs@1247: fstd->station_index = v->current_order.station; matthijs@1247: /* Let's take the center of the station as our target tile for trains */ matthijs@1247: fstd->dest_coords = TILE_ADD(st->train_tile, ToTileIndexDiff(center)); matthijs@1247: } else { matthijs@1247: fstd->dest_coords = v->dest_tile; matthijs@1247: fstd->station_index = -1; matthijs@1247: } matthijs@1247: }