OpenXcom  1.0
Open-source clone of the original X-Com
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Static Public Member Functions | List of all members
OpenXcom::Pathfinding Class Reference

A utility class that calculates the shortest path between two points on the battlescape map. More...

#include <Pathfinding.h>

Public Member Functions

bool isOnStairs (const Position &startPosition, const Position &endPosition)
 Determines whether the unit is going up a stairs. More...
 
bool isBlocked (Tile *startTile, Tile *endTile, const int direction, BattleUnit *missileTarget)
 Determines whether or not movement between starttile and endtile is possible in the direction. More...
 
 Pathfinding (SavedBattleGame *save)
 Creates a new Pathfinding class. More...
 
 ~Pathfinding ()
 Cleans up the Pathfinding. More...
 
void calculate (BattleUnit *unit, Position endPosition, BattleUnit *missileTarget=0, int maxTUCost=1000)
 Calculates the shortest path. More...
 
int getStartDirection ()
 Checks whether a path is ready and gives the first direction. More...
 
int dequeuePath ()
 Dequeues a direction. More...
 
int getTUCost (const Position &startPosition, const int direction, Position *endPosition, BattleUnit *unit, BattleUnit *target, bool missile)
 Gets the TU cost to move from 1 tile to the other. More...
 
void abortPath ()
 Aborts the current path. More...
 
bool getStrafeMove () const
 Gets the strafe move setting. More...
 
bool validateUpDown (BattleUnit *bu, Position startPosition, const int direction)
 Checks, for the up/down button, if the movement is valid. More...
 
bool previewPath (bool bRemove=false)
 Previews the path. More...
 
bool removePreview ()
 Removes the path preview. More...
 
void setUnit (BattleUnit *unit)
 Sets _unit in order to abuse low-level pathfinding functions from outside the class. More...
 
std::vector< int > findReachable (BattleUnit *unit, int tuMax)
 Gets all reachable tiles, based on cost. More...
 
int getTotalTUCost () const
 Gets _totalTUCost; finds out whether we can hike somewhere in this turn or not.
 
bool isPathPreviewed () const
 Gets the path preview setting. More...
 
bool isModifierUsed () const
 Gets the modifier setting. More...
 
const std::vector< int > & getPath ()
 Gets a reference to the path. More...
 
std::vector< int > copyPath () const
 Makes a copy to the path. More...
 

Static Public Member Functions

static void directionToVector (const int direction, Position *vector)
 Converts direction to a vector. More...
 
static void vectorToDirection (const Position &vector, int &dir)
 Converts a vector to a direction. More...
 

Detailed Description

A utility class that calculates the shortest path between two points on the battlescape map.

Constructor & Destructor Documentation

OpenXcom::Pathfinding::Pathfinding ( SavedBattleGame save)

Creates a new Pathfinding class.

Sets up a Pathfinding.

Parameters
savepointer to SavedBattleGame object.
OpenXcom::Pathfinding::~Pathfinding ( )

Cleans up the Pathfinding.

Deletes the Pathfinding.

Member Function Documentation

void OpenXcom::Pathfinding::abortPath ( )

Aborts the current path.

Clears the path vector.

void OpenXcom::Pathfinding::calculate ( BattleUnit unit,
Position  endPosition,
BattleUnit target = 0,
int  maxTUCost = 1000 
)

Calculates the shortest path.

Parameters
unitUnit taking the path.
endPositionThe position we want to reach.
targetTarget of the path.
maxTUCostMaximum time units the path can cost.
std::vector< int > OpenXcom::Pathfinding::copyPath ( ) const

Makes a copy to the path.

Makes a copy of the current path.

Returns
a copy of the path.
int OpenXcom::Pathfinding::dequeuePath ( )

Dequeues a direction.

Dequeues the next path direction.

Ie returns the direction and removes it from queue.

Returns
Direction where the unit needs to go next, -1 if it's the end of the path.
void OpenXcom::Pathfinding::directionToVector ( const int  direction,
Position vector 
)
static

Converts direction to a vector.

Direction starts north = 0 and goes clockwise.

Parameters
directionSource direction.
vectorPointer to a position (which acts as a vector).
std::vector< int > OpenXcom::Pathfinding::findReachable ( BattleUnit unit,
int  tuMax 
)

Gets all reachable tiles, based on cost.

Locates all tiles reachable to *unit with a TU cost no more than tuMax.

Uses Dijkstra's algorithm.

Parameters
unitPointer to the unit.
tuMaxThe maximum cost of the path to each tile.
Returns
An array of reachable tiles, sorted in ascending order of cost. The first tile is the start location.
const std::vector< int > & OpenXcom::Pathfinding::getPath ( )

Gets a reference to the path.

Gets a reference to the current path.

Returns
the actual path.
int OpenXcom::Pathfinding::getStartDirection ( )

Checks whether a path is ready and gives the first direction.

Returns
Direction where the unit needs to go next, -1 if it's the end of the path.
bool OpenXcom::Pathfinding::getStrafeMove ( ) const

Gets the strafe move setting.

Returns
Strafe move.
int OpenXcom::Pathfinding::getTUCost ( const Position startPosition,
const int  direction,
Position endPosition,
BattleUnit unit,
BattleUnit target,
bool  missile 
)

Gets the TU cost to move from 1 tile to the other.

Gets the TU cost to move from 1 tile to the other (ONE STEP ONLY).

But also updates the endPosition, because it is possible the unit goes upstairs or falls down while walking.

Parameters
startPositionThe position to start from.
directionThe direction we are facing.
endPositionThe position we want to reach.
unitThe unit moving.
targetThe target unit.
missileIs this a guided missile?
Returns
TU cost or 255 if movement is impossible.
bool OpenXcom::Pathfinding::isBlocked ( Tile startTile,
Tile endTile,
const int  direction,
BattleUnit missileTarget 
)

Determines whether or not movement between starttile and endtile is possible in the direction.

Determines whether going from one tile to another blocks movement.

Parameters
startTileThe tile to start from.
endTileThe tile we want to reach.
directionThe direction we are facing.
missileTargetTarget for a missile.
Returns
True if the movement is blocked.
bool OpenXcom::Pathfinding::isModifierUsed ( ) const

Gets the modifier setting.

Checks whether a modifier key was used to enable strafing or running.

Returns
True, if a modifier was used.
bool OpenXcom::Pathfinding::isOnStairs ( const Position startPosition,
const Position endPosition 
)

Determines whether the unit is going up a stairs.

Parameters
startPositionThe position to start from.
endPositionThe position we wanna reach.
Returns
True if the unit is going up a stairs.
bool OpenXcom::Pathfinding::isPathPreviewed ( ) const

Gets the path preview setting.

Returns
True, if paths are previewed.
bool OpenXcom::Pathfinding::previewPath ( bool  bRemove = false)

Previews the path.

Marks tiles for the path preview.

Parameters
bRemoveRemove preview?
Returns
True, if a path is previewed.
bool OpenXcom::Pathfinding::removePreview ( )

Removes the path preview.

Unmarks the tiles used for the path preview.

Returns
True, if the previewed path was removed.
void OpenXcom::Pathfinding::setUnit ( BattleUnit unit)

Sets _unit in order to abuse low-level pathfinding functions from outside the class.

Parameters
unitUnit taking the path.
bool OpenXcom::Pathfinding::validateUpDown ( BattleUnit bu,
Position  startPosition,
const int  direction 
)

Checks, for the up/down button, if the movement is valid.

Either there is a grav lift or the unit can fly and there are no obstructions.

Parameters
buPointer to unit.
startPositionUnit starting position.
directionUp or Down
Returns
bool Whether it's valid.
void OpenXcom::Pathfinding::vectorToDirection ( const Position vector,
int &  dir 
)
static

Converts a vector to a direction.

Converts direction to a vector.

Direction starts north = 0 and goes clockwise.

Parameters
vectorPointer to a position (which acts as a vector).
dirResulting direction.

The documentation for this class was generated from the following files: