tron@2186: /* $Id$ */ tron@2186: rubidium@9111: /** @file npf.h New A* pathfinder. */ belugas@6348: matthijs@1247: #ifndef NPF_H matthijs@1247: #define NPF_H matthijs@1247: matthijs@1247: #include "aystar.h" rubidium@8785: #include "station_type.h" rubidium@8785: #include "rail_type.h" rubidium@8785: #include "player_type.h" rubidium@8139: #include "vehicle_type.h" rubidium@8108: #include "tile_type.h" rubidium@8139: #include "track_type.h" rubidium@8236: #include "core/bitmath_func.hpp" rubidium@9126: #include "transport_type.h" matthijs@1247: belugas@6348: /* mowing grass */ matthijs@1661: enum { belugas@6348: NPF_HASH_BITS = 12, ///< The size of the hash used in pathfinding. Just changing this value should be sufficient to change the hash size. Should be an even value. matthijs@1661: /* Do no change below values */ matthijs@1661: NPF_HASH_SIZE = 1 << NPF_HASH_BITS, matthijs@1661: NPF_HASH_HALFBITS = NPF_HASH_BITS / 2, matthijs@1661: NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1 matthijs@1661: }; matthijs@1247: celestar@3358: /* For new pathfinding. Define here so it is globally available without having celestar@3358: * to include npf.h */ celestar@3358: enum { celestar@3358: NPF_TILE_LENGTH = 100 celestar@3358: }; celestar@3358: matthijs@1777: enum { matthijs@1777: /** This penalty is the equivalent of "inifite", which means that paths that matthijs@1777: * get this penalty will be chosen, but only if there is no other route matthijs@1777: * without it. Be careful with not applying this penalty to often, or the matthijs@1777: * total path cost might overflow.. matthijs@1777: * For now, this is just a Very Big Penalty, we might actually implement matthijs@1777: * this in a nicer way :-) matthijs@1777: */ matthijs@1777: NPF_INFINITE_PENALTY = 1000 * NPF_TILE_LENGTH matthijs@1777: }; matthijs@1777: belugas@6348: /* Meant to be stored in AyStar.targetdata */ belugas@6348: struct NPFFindStationOrTileData { belugas@6348: TileIndex dest_coords; ///< An indication of where the station is, for heuristic purposes, or the target tile belugas@6348: StationID station_index; ///< station index we're heading for, or INVALID_STATION when we're heading for a tile rubidium@9804: bool reserve_path; ///< Indicates whether the found path should be reserved rubidium@9804: const Vehicle* v; ///< The vehicle we are pathfinding for rubidium@6248: }; matthijs@1247: belugas@6348: /* Indices into AyStar.userdata[] */ belugas@6348: enum { belugas@6348: NPF_TYPE = 0, ///< Contains a TransportTypes value rubidium@6683: NPF_SUB_TYPE, ///< Contains the sub transport type belugas@6348: NPF_OWNER, ///< Contains an Owner value belugas@6348: NPF_RAILTYPES, ///< Contains a bitmask the compatible RailTypes of the engine when NPF_TYPE == TRANSPORT_RAIL. Unused otherwise. matthijs@1247: }; matthijs@1247: belugas@6348: /* Indices into AyStarNode.userdata[] */ belugas@6348: enum { belugas@6348: NPF_TRACKDIR_CHOICE = 0, ///< The trackdir chosen to get here matthijs@1247: NPF_NODE_FLAGS, matthijs@1247: }; hackykid@2008: belugas@6348: /* Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFGetBit() and NPFGetBit() to use them. */ belugas@6348: enum NPFNodeFlag { frosch@8510: NPF_FLAG_SEEN_SIGNAL, ///< Used to mark that a signal was seen on the way, for rail only rubidium@9805: NPF_FLAG_2ND_SIGNAL, ///< Used to mark that two signals were seen, rail only rubidium@9805: NPF_FLAG_3RD_SIGNAL, ///< Used to mark that three signals were seen, rail only frosch@8510: NPF_FLAG_REVERSE, ///< Used to mark that this node was reached from the second start node, if applicable frosch@8510: NPF_FLAG_LAST_SIGNAL_RED, ///< Used to mark that the last signal on this path was red frosch@8510: NPF_FLAG_IGNORE_START_TILE, ///< Used to mark that the start tile is invalid, and searching should start from the second tile on rubidium@9804: NPF_FLAG_TARGET_RESERVED, ///< Used to mark that the possible reservation target is already reserved rubidium@9806: NPF_FLAG_IGNORE_RESERVED, ///< Used to mark that reserved tiles should be considered impassable rubidium@6248: }; matthijs@1247: belugas@6348: /* Meant to be stored in AyStar.userpath */ belugas@6348: struct NPFFoundTargetData { belugas@6348: uint best_bird_dist; ///< The best heuristic found. Is 0 if the target was found belugas@6348: uint best_path_dist; ///< The shortest path. Is (uint)-1 if no path is found belugas@6348: Trackdir best_trackdir; ///< The trackdir that leads to the shortest path/closest birds dist belugas@6348: AyStarNode node; ///< The node within the target the search led us to rubidium@9804: bool res_okay; ///< True if a path reservation could be made rubidium@6248: }; matthijs@1247: matthijs@1247: /* These functions below are _not_ re-entrant, in favor of speed! */ matthijs@1247: matthijs@1247: /* Will search from the given tile and direction, for a route to the given matthijs@1247: * station for the given transport type. See the declaration of matthijs@1247: * NPFFoundTargetData above for the meaning of the result. */ frosch@8510: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); tron@2951: matthijs@1247: /* Will search as above, but with two start nodes, the second being the matthijs@1459: * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which matthijs@1459: * direction was taken (NPFGetBit(result.node, NPF_FLAG_REVERSE)) */ frosch@8510: NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); matthijs@1247: matthijs@1247: /* Will search a route to the closest depot. */ matthijs@1247: matthijs@1247: /* Search using breadth first. Good for little track choice and inaccurate matthijs@1777: * heuristic, such as railway/road.*/ frosch@8510: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); matthijs@1777: /* Same as above but with two start nodes, the second being the reverse. Call matthijs@1777: * NPFGetBit(result.node, NPF_FLAG_REVERSE) to see from which node the path matthijs@1777: * orginated. All pathfs from the second node will have the given matthijs@1777: * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full matthijs@1777: * tile). matthijs@1777: */ frosch@8510: NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty); matthijs@1247: /* Search by trying each depot in order of Manhattan Distance. Good for lots matthijs@1330: * of choices and accurate heuristics, such as water. */ frosch@8510: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, TransportType type, uint sub_type, Owner owner, RailTypes railtypes); matthijs@1247: rubidium@9806: /** rubidium@9806: * Search for any safe tile using a breadth first search and try to reserve a path. rubidium@9806: */ rubidium@9806: NPFFoundTargetData NPFRouteToSafeTile(const Vehicle *v, TileIndex tile, Trackdir trackdir,bool override_railtype); rubidium@9806: rubidium@9806: rubidium@9804: void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, Vehicle *v, bool reserve_path = false); matthijs@1247: matthijs@1459: matthijs@1459: /* matthijs@1459: * Functions to manipulate the various NPF related flags on an AyStarNode. matthijs@1459: */ matthijs@1459: matthijs@1459: /** matthijs@1459: * Returns the current value of the given flag on the given AyStarNode. matthijs@1459: */ matthijs@1459: static inline bool NPFGetFlag(const AyStarNode* node, NPFNodeFlag flag) matthijs@1459: { skidd13@7928: return HasBit(node->user_data[NPF_NODE_FLAGS], flag); matthijs@1459: } matthijs@1459: matthijs@1459: /** matthijs@1459: * Sets the given flag on the given AyStarNode to the given value. matthijs@1459: */ matthijs@1459: static inline void NPFSetFlag(AyStarNode* node, NPFNodeFlag flag, bool value) matthijs@1459: { matthijs@1459: if (value) skidd13@7931: SetBit(node->user_data[NPF_NODE_FLAGS], flag); matthijs@1459: else skidd13@7929: ClrBit(node->user_data[NPF_NODE_FLAGS], flag); matthijs@1459: } matthijs@1459: Darkvater@2436: #endif /* NPF_H */