tron@2186: /* $Id$ */ tron@2186: belugas@6674: /** @file npf.h */ belugas@6674: matthijs@1247: #ifndef NPF_H matthijs@1247: #define NPF_H matthijs@1247: Darkvater@1891: #include "openttd.h" matthijs@1247: #include "aystar.h" tron@3135: #include "station.h" matthijs@1247: #include "vehicle.h" matthijs@1942: #include "tile.h" matthijs@1247: belugas@6674: /* mowing grass */ matthijs@1661: enum { belugas@6674: 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@6674: /* Meant to be stored in AyStar.targetdata */ belugas@6674: struct NPFFindStationOrTileData { belugas@6674: TileIndex dest_coords; ///< An indication of where the station is, for heuristic purposes, or the target tile belugas@6674: StationID station_index; ///< station index we're heading for, or INVALID_STATION when we're heading for a tile rubidium@6574: }; matthijs@1247: belugas@6674: /* Indices into AyStar.userdata[] */ belugas@6674: enum { belugas@6674: NPF_TYPE = 0, ///< Contains a TransportTypes value richk@6719: NPF_SUB_TYPE, ///< Contains the sub transport type belugas@6674: NPF_OWNER, ///< Contains an Owner value belugas@6674: NPF_RAILTYPES, ///< Contains a bitmask the compatible RailTypes of the engine when NPF_TYPE == TRANSPORT_RAIL. Unused otherwise. matthijs@1247: }; matthijs@1247: belugas@6674: /* Indices into AyStarNode.userdata[] */ belugas@6674: enum { belugas@6674: NPF_TRACKDIR_CHOICE = 0, ///< The trackdir chosen to get here matthijs@1247: NPF_NODE_FLAGS, matthijs@1247: }; hackykid@2008: belugas@6674: /* Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFGetBit() and NPFGetBit() to use them. */ belugas@6674: enum NPFNodeFlag { belugas@6674: NPF_FLAG_SEEN_SIGNAL, ///< Used to mark that a signal was seen on the way, for rail only belugas@6674: NPF_FLAG_REVERSE, ///< Used to mark that this node was reached from the second start node, if applicable belugas@6674: NPF_FLAG_LAST_SIGNAL_RED, ///< Used to mark that the last signal on this path was red rubidium@6574: }; matthijs@1247: belugas@6674: /* Meant to be stored in AyStar.userpath */ belugas@6674: struct NPFFoundTargetData { belugas@6674: uint best_bird_dist; ///< The best heuristic found. Is 0 if the target was found belugas@6674: uint best_path_dist; ///< The shortest path. Is (uint)-1 if no path is found belugas@6674: Trackdir best_trackdir; ///< The trackdir that leads to the shortest path/closest birds dist belugas@6674: AyStarNode node; ///< The node within the target the search led us to rubidium@6574: }; 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. */ richk@6719: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask 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)) */ richk@6719: NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask 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.*/ richk@6719: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask 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: */ richk@6719: NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, uint sub_type, Owner owner, RailTypeMask 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. */ richk@6719: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, uint sub_type, Owner owner, RailTypeMask railtypes); matthijs@1247: matthijs@1247: void NPFFillWithOrderData(NPFFindStationOrTileData* fstd, Vehicle* v); 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: { matthijs@1459: 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) matthijs@1459: SETBIT(node->user_data[NPF_NODE_FLAGS], flag); matthijs@1459: else matthijs@1459: CLRBIT(node->user_data[NPF_NODE_FLAGS], flag); matthijs@1459: } matthijs@1459: Darkvater@2436: #endif /* NPF_H */