tron@2186: /* $Id$ */ tron@2186: matthijs@1247: #ifndef NPF_H matthijs@1247: #define NPF_H matthijs@1247: Darkvater@1891: #include "openttd.h" matthijs@1247: #include "aystar.h" matthijs@1247: #include "vehicle.h" matthijs@1942: #include "tile.h" tron@2159: #include "variables.h" matthijs@1247: matthijs@1247: //mowing grass matthijs@1661: enum { matthijs@1661: 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: 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: matthijs@1247: typedef struct NPFFindStationOrTileData { /* Meant to be stored in AyStar.targetdata */ matthijs@1247: TileIndex dest_coords; /* An indication of where the station is, for heuristic purposes, or the target tile */ matthijs@1247: int station_index; /* station index we're heading for, or -1 when we're heading for a tile */ matthijs@1247: } NPFFindStationOrTileData; matthijs@1247: matthijs@1247: enum { /* Indices into AyStar.userdata[] */ matthijs@1330: NPF_TYPE = 0, /* Contains a TransportTypes value */ matthijs@1330: NPF_OWNER, /* Contains an Owner value */ matthijs@2006: NPF_RAILTYPE, /* Contains the RailType value of the engine when NPF_TYPE == TRANSPORT_RAIL. Unused otherwise. */ matthijs@1247: }; matthijs@1247: matthijs@1247: enum { /* Indices into AyStarNode.userdata[] */ matthijs@1247: NPF_TRACKDIR_CHOICE = 0, /* The trackdir chosen to get here */ matthijs@1247: NPF_NODE_FLAGS, matthijs@1247: }; hackykid@2008: matthijs@1459: typedef enum { /* Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFGetBit() and NPFGetBit() to use them. */ matthijs@1459: NPF_FLAG_SEEN_SIGNAL, /* Used to mark that a signal was seen on the way, for rail only */ matthijs@1459: NPF_FLAG_REVERSE, /* Used to mark that this node was reached from the second start node, if applicable */ matthijs@1459: NPF_FLAG_LAST_SIGNAL_RED, /* Used to mark that the last signal on this path was red */ matthijs@1459: } NPFNodeFlag; matthijs@1247: matthijs@1247: typedef struct NPFFoundTargetData { /* Meant to be stored in AyStar.userpath */ matthijs@1247: uint best_bird_dist; /* The best heuristic found. Is 0 if the target was found */ matthijs@1247: uint best_path_dist; /* The shortest path. Is (uint)-1 if no path is found */ matthijs@1942: Trackdir best_trackdir; /* The trackdir that leads to the shortest path/closest birds dist */ matthijs@1247: AyStarNode node; /* The node within the target the search led us to */ matthijs@1247: } NPFFoundTargetData; 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. */ Darkvater@2916: NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype); 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)) */ Darkvater@2916: NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailType railtype); 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.*/ matthijs@2006: NPFFoundTargetData NPFRouteToDepotBreadthFirst(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype); 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: */ matthijs@2006: NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, TileIndex tile2, Trackdir trackdir2, TransportType type, Owner owner, RailType railtype, 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. */ matthijs@2006: NPFFoundTargetData NPFRouteToDepotTrialError(TileIndex tile, Trackdir trackdir, TransportType type, Owner owner, RailType railtype); 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 */