OpenTTD
|
Implementation of the NPF pathfinder. More...
#include "../../stdafx.h"
#include "../../network/network.h"
#include "../../viewport_func.h"
#include "../../ship.h"
#include "../../roadstop_base.h"
#include "../pathfinder_func.h"
#include "../pathfinder_type.h"
#include "../follow_track.hpp"
#include "aystar.h"
#include "../../safeguards.h"
Go to the source code of this file.
Data Structures | |
struct | NPFFindStationOrTileData |
Meant to be stored in AyStar.targetdata. More... | |
struct | NPFFoundTargetData |
Meant to be stored in AyStar.userpath. More... |
Macros | |
#define | NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH) |
Enumerations | |
enum | AyStarUserDataType { NPF_TYPE = 0, NPF_SUB_TYPE, NPF_OWNER, NPF_RAILTYPES } |
Indices into AyStar.userdata[]. More... | |
enum | AyStarNodeUserDataType { NPF_TRACKDIR_CHOICE = 0, NPF_NODE_FLAGS } |
Indices into AyStarNode.userdata[]. More... | |
enum | NPFNodeFlag { NPF_FLAG_SEEN_SIGNAL, NPF_FLAG_2ND_SIGNAL, NPF_FLAG_3RD_SIGNAL, NPF_FLAG_REVERSE, NPF_FLAG_LAST_SIGNAL_RED, NPF_FLAG_LAST_SIGNAL_BLOCK, NPF_FLAG_IGNORE_START_TILE, NPF_FLAG_TARGET_RESERVED, NPF_FLAG_IGNORE_RESERVED } |
Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. More... |
Functions | |
static bool | NPFGetFlag (const AyStarNode *node, NPFNodeFlag flag) |
Returns the current value of the given flag on the given AyStarNode. | |
static void | NPFSetFlag (AyStarNode *node, NPFNodeFlag flag, bool value) |
Sets the given flag on the given AyStarNode to the given value. | |
static uint | NPFDistanceTrack (TileIndex t0, TileIndex t1) |
Calculates the minimum distance travelled to get from t0 to t1 when only using tracks (ie, only making 45 degree turns). | |
static uint | NPFHash (uint key1, uint key2) |
Calculates a hash value for use in the NPF. | |
static int32 | NPFCalcZero (AyStar *as, AyStarNode *current, OpenListNode *parent) |
static int32 | NPFCalcStationOrTileHeuristic (AyStar *as, AyStarNode *current, OpenListNode *parent) |
static void | NPFFillTrackdirChoice (AyStarNode *current, OpenListNode *parent) |
static uint | NPFTunnelCost (AyStarNode *current) |
static uint | NPFBridgeCost (AyStarNode *current) |
static uint | NPFSlopeCost (AyStarNode *current) |
static uint | NPFReservedTrackCost (AyStarNode *current) |
static void | NPFMarkTile (TileIndex tile) |
Mark tiles by mowing the grass when npf debug level >= 1. | |
static int32 | NPFWaterPathCost (AyStar *as, AyStarNode *current, OpenListNode *parent) |
static int32 | NPFRoadPathCost (AyStar *as, AyStarNode *current, OpenListNode *parent) |
static int32 | NPFRailPathCost (AyStar *as, AyStarNode *current, OpenListNode *parent) |
static int32 | NPFFindDepot (AyStar *as, OpenListNode *current) |
static int32 | NPFFindSafeTile (AyStar *as, OpenListNode *current) |
Find any safe and free tile. | |
static int32 | NPFFindStationOrTile (AyStar *as, OpenListNode *current) |
static const PathNode * | FindSafePosition (PathNode *path, const Train *v) |
Find the node containing the first signal on the path. | |
static void | ClearPathReservation (const PathNode *start, const PathNode *end) |
Lift the reservation of the tiles from start till end , excluding end itself. | |
static void | NPFSaveTargetData (AyStar *as, OpenListNode *current) |
To be called when current contains the (shortest route to) the target node. | |
static bool | CanEnterTileOwnerCheck (Owner owner, TileIndex tile, DiagDirection enterdir) |
Finds out if a given company's vehicles are allowed to enter a given tile. | |
static DiagDirection | GetDepotDirection (TileIndex tile, TransportType type) |
Returns the direction the exit of the depot on the given tile is facing. | |
static DiagDirection | GetSingleTramBit (TileIndex tile) |
Tests if a tile is a road tile with a single tramtrack (tram can reverse) | |
static DiagDirection | GetTileSingleEntry (TileIndex tile, TransportType type, uint subtype) |
Tests if a tile can be entered or left only from one side. | |
static bool | ForceReverse (TileIndex tile, DiagDirection dir, TransportType type, uint subtype) |
Tests if a vehicle must reverse on a tile. | |
static bool | CanEnterTile (TileIndex tile, DiagDirection dir, TransportType type, uint subtype, RailTypes railtypes, Owner owner) |
Tests if a vehicle can enter a tile. | |
static TrackdirBits | GetDriveableTrackdirBits (TileIndex dst_tile, Trackdir src_trackdir, TransportType type, uint subtype) |
Returns the driveable Trackdirs on a tile. | |
static void | NPFFollowTrack (AyStar *aystar, OpenListNode *current) |
static NPFFoundTargetData | NPFRouteInternal (AyStarNode *start1, bool ignore_start_tile1, AyStarNode *start2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, TransportType type, uint sub_type, Owner owner, RailTypes railtypes, uint reverse_penalty) |
static 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) |
static NPFFoundTargetData | NPFRouteToStationOrTile (TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes) |
static NPFFoundTargetData | NPFRouteToDepotBreadthFirstTwoWay (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, uint reverse_penalty) |
void | InitializeNPF () |
static void | NPFFillWithOrderData (NPFFindStationOrTileData *fstd, const Vehicle *v, bool reserve_path=false) |
FindDepotData | NPFRoadVehicleFindNearestDepot (const RoadVehicle *v, int max_penalty) |
Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF. | |
Trackdir | NPFRoadVehicleChooseTrack (const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs, bool &path_found) |
Finds the best path for given road vehicle using NPF. | |
Track | NPFShipChooseTrack (const Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found) |
Finds the best path for given ship using NPF. | |
bool | NPFShipCheckReverse (const Ship *v) |
Returns true if it is better to reverse the ship before leaving depot using NPF. | |
FindDepotData | NPFTrainFindNearestDepot (const Train *v, int max_penalty) |
Used when user sends train to the nearest depot or if train needs servicing using NPF. | |
bool | NPFTrainFindNearestSafeTile (const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype) |
Try to extend the reserved path of a train to the nearest safe tile using NPF. | |
bool | NPFTrainCheckReverse (const Train *v) |
Returns true if it is better to reverse the train before leaving station using NPF. | |
Track | NPFTrainChooseTrack (const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, struct PBSTileInfo *target) |
Finds the best path for given train using NPF. |
Variables | |
static const uint | 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. | |
static const uint | NPF_HASH_SIZE = 1 << NPF_HASH_BITS |
static const uint | NPF_HASH_HALFBITS = NPF_HASH_BITS / 2 |
static const uint | NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1 |
static AyStar | _npf_aystar |
static const uint | _trackdir_length [TRACKDIR_END] |
Implementation of the NPF pathfinder.
Definition in file npf.cpp.
enum AyStarUserDataType |
enum NPFNodeFlag |
Flags for AyStarNode.userdata[NPF_NODE_FLAGS].
Use NPFSetFlag() and NPFGetFlag() to use them.
|
static |
Tests if a vehicle can enter a tile.
tile | The tile of interest. |
dir | The direction in which the vehicle drives onto a tile. |
type | The transporttype of the vehicle. |
subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
railtypes | For TRANSPORT_RAIL the compatible RailTypes of the vehicle. |
owner | The owner of the vehicle. |
Definition at line 776 of file npf.cpp.
References CanEnterTileOwnerCheck(), GetTileRailType(), GetTileSingleEntry(), GetTunnelBridgeDirection(), HasBit(), INVALID_DIAGDIR, IsTileType(), MP_TUNNELBRIDGE, ReverseDiagDir(), and TRANSPORT_RAIL.
|
static |
Finds out if a given company's vehicles are allowed to enter a given tile.
owner | The owner of the vehicle. |
tile | The tile that is about to be entered. |
enterdir | The direction in which the vehicle wants to enter the tile. |
Definition at line 665 of file npf.cpp.
References DiagDirToAxis(), GetCrossingRoadAxis(), GetTileType(), GetTunnelBridgeTransportType(), HasStationTileRail(), IsLevelCrossing(), IsRoadDepotTile(), IsStandardRoadStopTile(), IsTileOwner(), IsTileType(), MP_RAILWAY, MP_ROAD, MP_TUNNELBRIDGE, and TRANSPORT_RAIL.
Referenced by CanEnterTile().
Find the node containing the first signal on the path.
If the first signal is on the very first two tiles of the path, the second signal is returned. If no suitable signal is present, the last node of the path is returned.
Definition at line 577 of file npf.cpp.
References _settings_game, PathfinderSettings::forbid_90_deg, IsSafeWaitingPosition(), PathNode::parent, and GameSettings::pf.
Referenced by NPFSaveTargetData().
|
inlinestatic |
Tests if a vehicle must reverse on a tile.
tile | The tile of interest. |
dir | The direction in which the vehicle drives on a tile. |
type | The transporttype of the vehicle. |
subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
Definition at line 759 of file npf.cpp.
References GetTileSingleEntry(), and INVALID_DIAGDIR.
|
static |
Returns the driveable Trackdirs on a tile.
One-way-roads are taken into account. Signals are not tested.
dst_tile | The tile of interest. |
src_trackdir | The direction the vehicle is currently moving. |
type | The transporttype of the vehicle. |
subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
Definition at line 808 of file npf.cpp.
References _settings_game, DEBUG, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, PathfinderSettings::forbid_90_deg, GetSingleTramBit(), GetTileTrackStatus(), HasBit(), GameSettings::pf, ROADTYPE_TRAM, TileX(), TileY(), TRACKDIR_BIT_X_NE, TRACKDIR_BIT_X_SW, TRACKDIR_BIT_Y_NW, TRACKDIR_BIT_Y_SE, TrackdirCrossesTrackdirs(), TrackdirReachesTrackdirs(), TrackStatusToTrackdirBits(), TRANSPORT_RAIL, TRANSPORT_ROAD, and TRANSPORT_WATER.
|
static |
Tests if a tile can be entered or left only from one side.
Depots, non-drive-through roadstops, and tiles with single trambits are tested.
tile | The tile of interest. |
type | The transporttype of the vehicle. |
subtype | For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. |
Definition at line 738 of file npf.cpp.
References GetDepotDirection(), GetRoadStopDir(), GetSingleTramBit(), HasBit(), INVALID_DIAGDIR, IsDepotTypeTile(), IsStandardRoadStopTile(), ROADTYPE_TRAM, TRANSPORT_ROAD, and TRANSPORT_WATER.
Referenced by CanEnterTile(), and ForceReverse().
Calculates the minimum distance travelled to get from t0 to t1 when only using tracks (ie, only making 45 degree turns).
Returns the distance in the NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to prevent rounding.
Definition at line 110 of file npf.cpp.
References Delta(), min(), NPF_TILE_LENGTH, STRAIGHT_TRACK_LENGTH, TileX(), and TileY().
|
static |
Find any safe and free tile.
Definition at line 542 of file npf.cpp.
References _settings_game, AYSTAR_DONE, AYSTAR_FOUND_END_NODE, PathfinderSettings::forbid_90_deg, SpecializedVehicle< Train, Type >::From(), IsSafeWaitingPosition(), IsWaitingPositionFree(), and GameSettings::pf.
Referenced by NPFTrainFindNearestSafeTile().
|
static |
Calculates a hash value for use in the NPF.
key1 | The TileIndex of the tile to hash |
key2 | The Trackdir of the track on the tile. |
Definition at line 134 of file npf.cpp.
References IsValidTile(), IsValidTrackdir(), TileX(), TileY(), and TRACKDIR_END.
|
static |
Mark tiles by mowing the grass when npf debug level >= 1.
Will not work for multiplayer games, since it can (will) cause desyncs.
Definition at line 275 of file npf.cpp.
References _networking, GetTileType(), IsRailDepot(), IsRoadDepot(), MarkTileDirtyByTile(), MP_RAILWAY, MP_ROAD, RAIL_GROUND_BARREN, ROADSIDE_BARREN, and SetRoadside().
Trackdir NPFRoadVehicleChooseTrack | ( | const RoadVehicle * | v, |
TileIndex | tile, | ||
DiagDirection | enterdir, | ||
TrackdirBits | trackdirs, | ||
bool & | path_found | ||
) |
Finds the best path for given road vehicle using NPF.
v | the RV that needs to find a path |
tile | the tile to find the path from (should be next tile the RV is about to enter) |
enterdir | diagonal direction which the RV will enter this new tile from |
trackdirs | available trackdirs on the new tile (to choose from) |
path_found | [out] Whether a path has been found (true) or has been guessed (false) |
Definition at line 1138 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_trackdir, DiagDirToDiagTrackdir(), FindFirstBit2x64(), INVALID_RAILTYPES, INVALID_TRACKDIR, Vehicle::owner, TileOffsByDiagDir(), and TRANSPORT_ROAD.
Referenced by RoadFindPathToDest().
FindDepotData NPFRoadVehicleFindNearestDepot | ( | const RoadVehicle * | v, |
int | max_penalty | ||
) |
Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF.
v | vehicle that needs to go to some depot |
max_penalty | max distance (in pathfinder penalty) from the current vehicle position (used also as optimization - the pathfinder can stop path finding if max_penalty was reached and no depot was seen) |
Definition at line 1122 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_path_dist, RoadVehicle::GetVehicleTrackdir(), INVALID_RAILTYPES, NPFFoundTargetData::node, Vehicle::owner, ReverseTrackdir(), Vehicle::tile, and TRANSPORT_ROAD.
|
static |
To be called when current
contains the (shortest route to) the target node.
Will fill the contents of the NPFFoundTargetData using AyStarNode[NPF_TRACKDIR_CHOICE]. If requested, path reservation is done here.
Definition at line 613 of file npf.cpp.
References _settings_game, NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_path_dist, NPFFoundTargetData::best_trackdir, ClearPathReservation(), FindSafePosition(), PathfinderSettings::forbid_90_deg, SpecializedVehicle< Train, Type >::From(), SpecializedStation< Station, false >::GetByTile(), Station::GetPlatformLength(), IsRailStationTile(), IsWaitingPositionFree(), NPFFoundTargetData::node, NPF_TRACKDIR_CHOICE, NPF_TYPE, PathNode::parent, GameSettings::pf, NPFFoundTargetData::res_okay, SetRailStationPlatformReservation(), SetRailStationReservation(), TILE_ADD, TileOffsByDiagDir(), TrackdirToExitdir(), TrackdirToTrack(), TRANSPORT_RAIL, and TryReserveRailTrack().
bool NPFShipCheckReverse | ( | const Ship * | v | ) |
Returns true if it is better to reverse the ship before leaving depot using NPF.
v | the ship leaving the depot |
Definition at line 1183 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, Ship::GetVehicleTrackdir(), INVALID_RAILTYPES, INVALID_TRACKDIR, NPFFoundTargetData::node, NPF_FLAG_REVERSE, NPFGetFlag(), Vehicle::owner, ReverseTrackdir(), Vehicle::tile, and TRANSPORT_WATER.
Track NPFShipChooseTrack | ( | const Ship * | v, |
TileIndex | tile, | ||
DiagDirection | enterdir, | ||
TrackBits | tracks, | ||
bool & | path_found | ||
) |
Finds the best path for given ship using NPF.
v | the ship that needs to find a path |
tile | the tile to find the path from (should be next tile the ship is about to enter) |
enterdir | diagonal direction which the ship will enter this new tile from |
tracks | available tracks on the new tile (to choose from) |
path_found | [out] Whether a path has been found (true) or has been guessed (false) |
Definition at line 1164 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_trackdir, Ship::GetVehicleTrackdir(), INVALID_RAILTYPES, INVALID_TRACK, INVALID_TRACKDIR, Vehicle::owner, TileOffsByDiagDir(), TrackdirToTrack(), and TRANSPORT_WATER.
Referenced by ChooseShipTrack().
bool NPFTrainCheckReverse | ( | const Train * | v | ) |
Returns true if it is better to reverse the train before leaving station using NPF.
v | the train leaving the station |
Definition at line 1247 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, Train::GetVehicleTrackdir(), INVALID_TRACKDIR, SpecializedVehicle< T, Type >::Last(), NPFFoundTargetData::node, NPF_FLAG_REVERSE, NPFGetFlag(), Vehicle::owner, ReverseTrackdir(), Vehicle::tile, and TRANSPORT_RAIL.
Track NPFTrainChooseTrack | ( | const Train * | v, |
TileIndex | tile, | ||
DiagDirection | enterdir, | ||
TrackBits | tracks, | ||
bool & | path_found, | ||
bool | reserve_track, | ||
struct PBSTileInfo * | target | ||
) |
Finds the best path for given train using NPF.
v | the train that needs to find a path |
tile | the tile to find the path from (should be next tile the train is about to enter) |
enterdir | diagonal direction which the RV will enter this new tile from |
tracks | available trackdirs on the new tile (to choose from) |
path_found | [out] Whether a path has been found (true) or has been guessed (false) |
reserve_track | indicates whether YAPF should try to reserve the found path |
target | [out] the target tile of the reservation, free is set to true if path was reserved |
Definition at line 1265 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_trackdir, FindFirstTrack(), FollowTrainReservation(), INVALID_TRACKDIR, IsValidTrackdir(), NPFFoundTargetData::node, PBSTileInfo::okay, Vehicle::owner, NPFFoundTargetData::res_okay, PBSTileInfo::tile, PBSTileInfo::trackdir, TrackdirToTrack(), and TRANSPORT_RAIL.
Referenced by DoTrainPathfind().
FindDepotData NPFTrainFindNearestDepot | ( | const Train * | v, |
int | max_penalty | ||
) |
Used when user sends train to the nearest depot or if train needs servicing using NPF.
v | train that needs to go to some depot |
max_penalty | max max_penalty (in pathfinder penalty) from the current train position (used also as optimization - the pathfinder can stop path finding if max_penalty was reached and no depot was seen) |
Definition at line 1202 of file npf.cpp.
References NPFFoundTargetData::best_bird_dist, NPFFoundTargetData::best_path_dist, Train::GetVehicleTrackdir(), INVALID_TRACKDIR, SpecializedVehicle< T, Type >::Last(), NPFFoundTargetData::node, NPF_FLAG_REVERSE, NPF_INFINITE_PENALTY, NPFGetFlag(), Vehicle::owner, ReverseTrackdir(), Vehicle::tile, TRANSPORT_RAIL, and NPFFindStationOrTileData::v.
Referenced by FindClosestTrainDepot().
bool NPFTrainFindNearestSafeTile | ( | const Train * | v, |
TileIndex | tile, | ||
Trackdir | td, | ||
bool | override_railtype | ||
) |
Try to extend the reserved path of a train to the nearest safe tile using NPF.
v | The train that needs to find a safe tile. |
tile | Last tile of the current reserved path. |
td | Last trackdir of the current reserved path. |
override_railtype | Should all physically compatible railtypes be searched, even if the vehicle can't run on them on its own? |
Definition at line 1223 of file npf.cpp.
References RailtypeInfo::compatible_railtypes, GetRailTypeInfo(), INVALID_TRACKDIR, NPF_FLAG_IGNORE_RESERVED, NPF_TRACKDIR_CHOICE, NPFFindSafeTile(), NPFSetFlag(), Vehicle::owner, NPFFoundTargetData::res_okay, NPFFindStationOrTileData::reserve_path, TRANSPORT_RAIL, BaseVehicle::type, NPFFindStationOrTileData::v, and VEH_TRAIN.
Referenced by TryReserveSafeTrack().
|
static |