libtcod
Loading...
Searching...
No Matches
TCODPath Class Reference

#include <path.hpp>

Classes

struct  WrapperData

Public Member Functions

 TCODPath (const TCODMap *map, float diagonalCost=1.41f)
 First, you have to allocate a path using a map from the Field of view module.
 TCODPath (int width, int height, const ITCODPathCallback *listener, void *userData, float diagonalCost=1.41f)
 Since the walkable status of a cell may depend on a lot of parameters (the creature type, the weather, the terrain type...), you can also create a path by providing a function rather than relying on a TCODMap.
 TCODPath (const TCODPath &)=delete
TCODPathoperator= (const TCODPath &)=delete
 TCODPath (TCODPath &&rhs) noexcept
TCODPathoperator= (TCODPath &&rhs) noexcept
virtual ~TCODPath ()
 To release the resources used by a path, destroy it with :TCODPath::~TCODPath() TCODDijkstra::~TCODDijkstra()void TCOD_path_delete(TCOD_path_t path) void TCOD_dijkstra_delete(TCOD_dijkstra_t dijkstra)path_delete(path) dijkstra_delete(dijkstra)void TCODPath::Dispose() void TCODDijkstra::Dispose().
bool compute (int ox, int oy, int dx, int dy)
 Once you created a TCODPath object, you can compute the path between two points:
void reverse ()
 Once you computed a path, you can exchange origin and destination :void TCODPath::reverse() void TCODDijkstra::reverse()void TCOD_path_reverse(TCOD_path_t path) void TCOD_dijkstra_reverse(TCOD_dijkstra_t dijkstra)path_reverse(path) dijkstra_reverse(dijkstra)void TCODPath::reverse() void TCODDijkstra::reverse().
void getOrigin (int *x, int *y) const
 You can read the current origin and destination cells with getOrigin/getDestination. Note that when you walk the path, the origin changes at each step.void TCODPath::getOrigin(int *x,int *y) const void TCODPath::getDestination(int *x,int *y) constvoid TCOD_path_get_origin(TCOD_path_t path, int *x, int *y) void TCOD_path_get_destination(TCOD_path_t path, int *x, int *y)path_get_origin(path) # returns x,y path_get_destination(path) # returns x,yvoid TCODPath::getOrigin(out int x, out int y) void TCODPath::getDestination(out int x, out int y).
void getDestination (int *x, int *y) const
int size () const
 You can get the number of steps needed to reach destination :int TCODPath::size() const int TCODDijkstra::size() constint TCOD_path_size(TCOD_path_t path) int TCOD_dijkstra_size(TCOD_dijkstra_t dijkstra)path_size(path) dijkstra_size(dijkstra)int TCODPath::size() int TCODDijkstra::size().
void get (int index, int *x, int *y) const
 You can get the coordinates of each point along the path :void TCODPath::get(int index, int *x, int *y) const void TCODDijkstra::get(int index, int *x, int *y) constvoid TCOD_path_get(TCOD_path_t path, int index, int *x, int *y) void TCOD_dijkstra_get(TCOD_dijkstra_t dijkstra, int index, int *x, int *y)path_get(path, index) # returns x,y dijkstra_get(dijkstra, index) # returns x,yint TCODPath::size() int TCODDijkstra::size().
bool isEmpty () const
 If you want a creature to follow the path, a more convenient way is to walk the path : You know when you reached destination when the path is empty :bool TCODPath::isEmpty() const bool TCODDijkstra::isEmpty() constbool TCOD_path_is_empty(TCOD_path_t path) bool TCOD_dijkstra_is_empty(TCOD_dijkstra_t dijkstra)path_is_empty(path) dijkstra_is_empty(dijkstra)bool TCODPath::isEmpty() bool TCODDijkstra::isEmpty().
bool walk (int *x, int *y, bool recalculateWhenNeeded)
 You can walk the path and go to the next step with : Note that walking the path consume one step (and decrease the path size by one).

Protected Attributes

TCOD_path_t data {}
struct TCODPath::WrapperData cppData

Friends

float TCOD_path_func (int xFrom, int yFrom, int xTo, int yTo, void *data)

Detailed Description

This toolkit allows one to easily calculate the optimal path between two points in your dungeon by using either the A* algorithm or Dijkstra's algorithm. Please note that the paths generated with the two algorithms may differ slightly. Due to how they're implemented, A* will usually prefer diagonal moves over orthogonal, while Dijkstra will have the opposite preference. In other words, paths from point X to point Y will look like this:

Dijkstra:      A*:
..........   ..........
.X........   .X*.......
..*.......   ...**.....
...*......   .....**...
....****Y.   .......*Y.
..........   ..........

Constructor & Destructor Documentation

◆ TCODPath() [1/2]

TCODPath::TCODPath ( const TCODMap * map,
float diagonalCost = 1.41f )

First, you have to allocate a path using a map from the Field of view module.

TCODPath::TCODPath(const TCODMap *map, float diagonalCost=1.41f) TCODDijkstra::TCODDijkstra(const TCODMap *map, float diagonalCost=1.41f)

TCOD_path_t TCOD_path_new_using_map(TCOD_map_t map, float diagonalCost) TCOD_dijkstra_t TCOD_dijkstra_new(TCOD_map_t map, float diagonalCost)

path_new_using_map(map, diagonalCost=1.41) dijkstra_new(map, diagonalCost=1.41)

TCODPath(TCODMap map, float diagonalCost) TCODPath(TCODMap map) TCODDijkstra(TCODMap map, float diagonalCost) TCODDijkstra(TCODMap map)

Parameters
mapThe map. The path finder will use the 'walkable' property of the cells to find a path.
diagonalCostCost of a diagonal movement compared to an horizontal or vertical movement. On a standard cartesian map, it should be sqrt(2) (1.41f). It you want the same cost for all movements, use 1.0f. If you don't want the path finder to use diagonal movements, use 0.0f.

A* : TCODMap *myMap = new TCODMap(50,50); TCODPath *path = new TCODPath(myMap); // allocate the path Dijkstra: TCODMap *myMap = new TCODMap(50,50); TCODDijkstra *dijkstra = new TCODDijkstra(myMap); // allocate the path

A* : TCOD_map_t my_map=TCOD_map_new(50,50,true); TCOD_path_t path = TCOD_path_new_using_map(my_map,1.41f); Dijkstra : TCOD_map_t my_map=TCOD_map_new(50,50,true); TCOD_dijkstra_t dijkstra = TCOD_dijkstra_new(my_map,1.41f);

A* :

my_map=libtcod.map_new(50,50,True) path = libtcod.path_new_using_map(my_map)

Dijkstra

my_map=libtcod.map_new(50,50,True) dijkstra = libtcod.dijkstra_new(my_map)

◆ TCODPath() [2/2]

TCODPath::TCODPath ( int width,
int height,
const ITCODPathCallback * listener,
void * userData,
float diagonalCost = 1.41f )

Since the walkable status of a cell may depend on a lot of parameters (the creature type, the weather, the terrain type...), you can also create a path by providing a function rather than relying on a TCODMap.

Callback : class ITCODPathCallback { public: virtual float getWalkCost( int xFrom, int yFrom, int xTo, int yTo, void <em>userData ) const = 0; }; A constructor: TCODPath::TCODPath(int width, int height, const ITCODPathCallback *callback, void *userData, float diagonalCost=1.41f) Dijkstra constructor TCODDijkstra::TCODDijkstra(int width, int height, const ITCODPathCallback *callback, void *userData, float diagonalCost=1.41f)

typedef float (*TCOD_path_func_t)( int xFrom, int yFrom, int xTo, int yTo, void *user_data ) TCOD_path_t TCOD_path_new_using_function(int width, int height, TCOD_path_func_t callback, void *user_data, float diagonalCost) TCOD_dijkstra_t TCOD_dijkstra_new_using_function(int width, int height, TCOD_path_func_t callback, void *user_data, float diagonalCost)

def path_func(xFrom,yFrom,xTo,yTo,userData) : ... path_new_using_function(width, height, path_func, user_data=0, diagonalCost=1.41) dijkstra_new_using_function(width, height, path_func, user_data=0, diagonalCost=1.41)

TCODPath(int width, int height, ITCODPathCallback listener, float diagonalCost) TCODPath(int width, int height, ITCODPathCallback listener) TCODDijkstra(int width, int height, ITCODPathCallback listener, float diagonalCost) TCODDijkstra(int width, int height, ITCODPathCallback listener)

Parameters
width,heightThe size of the map (in map cells).
callbackA custom function that must return the walk cost from coordinates xFrom,yFrom to coordinates xTo,yTo. The cost must be > 0.0f if the cell xTo,yTo is walkable. It must be equal to 0.0f if it's not. You must not take additional cost due to diagonal movements into account as it's already done by the pathfinder.
userDataCustom data that will be passed to the function.
diagonalCostCost of a diagonal movement compared to an horizontal or vertical movement. On a standard cartesian map, it should be sqrt(2) (1.41f). It you want the same cost for all movements, use 1.0f. If you don't want the path finder to use diagonal movements, use 0.0f.

class MyCallback : public ITCODPathCallback { public : float getWalkCost(int xFrom, int yFrom, int xTo, int yTo, void *userData ) const { ... } }; TCODPath *path = new TCODPath(50,50,new MyCallback(),NULL); // allocate the path TCODDijkstra *dijkstra = new TCODDijkstra(50,50,new MyCallback(),NULL); // allocate Dijkstra

float my_func(int xFrom, int yFrom, int xTo, int yTo, void *user_data) { ... } TCOD_path_t path = TCOD_path_new_using_function(50,50,my_func,NULL,1.41f); TCOD_dijkstra_t dijkstra = TCOD_dijkstra_new_using_function(50,50,my_func,NULL,1.41f);

def my_func(xFrom, yFrom, xTo, yTo, user_data) :

return a float cost for this movement

return 1.0 path = libtcod.path_new_using_function(50,50,my_func) dijkstra = libtcod.dijkstra_new_using_function(50,50,my_func)

◆ ~TCODPath()

virtual TCODPath::~TCODPath ( )
virtual

To release the resources used by a path, destroy it with :TCODPath::~TCODPath() TCODDijkstra::~TCODDijkstra()void TCOD_path_delete(TCOD_path_t path) void TCOD_dijkstra_delete(TCOD_dijkstra_t dijkstra)path_delete(path) dijkstra_delete(dijkstra)void TCODPath::Dispose() void TCODDijkstra::Dispose().

Parameters
pathIn the C version, the path handler returned by one of the TCOD_path_new_* function.
dijkstraIn the C version, the path handler returned by one of the TCOD_dijkstra_new* function.

TCODPath *path = new TCODPath(myMap); // allocate the path use the path... delete path; // destroy the path

TCODDijkstra *dijkstra = new TCODDijkstra(myMap); // allocate the path use the path... delete dijkstra; // destroy the path

TCOD_path_t path = TCOD_path_new_using_map(my_map); use the path ... TCOD_path_delete(path);

TCOD_dijkstra_t dijkstra = TCOD_dijkstra_new(my_map); use the path ... TCOD_dijkstra_delete(dijkstra);

path = libtcod.path_new_using_map(my_map)

use the path ...

libtcod.path_delete(path)

dijkstra = libtcod.dijkstra_new(my_map)

use the path ...

libtcod.dijkstra_delete(dijkstra)

Member Function Documentation

◆ compute()

bool TCODPath::compute ( int ox,
int oy,
int dx,
int dy )

Once you created a TCODPath object, you can compute the path between two points:

Parameters
pathIn the C version, the path handler returned by a creation function.
ox,oyCoordinates of the origin of the path.
dx,dyCoordinates of the destination of the path. Both points should be inside the map, and at a walkable position. The function returns false if there is no possible path.

TCODMap *myMap = new TCODMap(50,50); TCODPath *path = new TCODPath(myMap); // allocate the path path->compute(5,5,25,25); // calculate path from 5,5 to 25,25

TCOD_map_t my_map=TCOD_map_new(50,50); TCOD_path_t path = TCOD_path_new_using_map(my_map); TCOD_path_compute(path,5,5,25,25);

my_map=libtcod.map_new(50,50) path = libtcod.path_new_using_map(my_map) libtcod.path_compute(path,5,5,25,25)

◆ get()

void TCODPath::get ( int index,
int * x,
int * y ) const

You can get the coordinates of each point along the path :void TCODPath::get(int index, int *x, int *y) const void TCODDijkstra::get(int index, int *x, int *y) constvoid TCOD_path_get(TCOD_path_t path, int index, int *x, int *y) void TCOD_dijkstra_get(TCOD_dijkstra_t dijkstra, int index, int *x, int *y)path_get(path, index) # returns x,y dijkstra_get(dijkstra, index) # returns x,yint TCODPath::size() int TCODDijkstra::size().

Parameters
path,dijkstraIn the C version, the path handler returned by a creation function.
indexStep number. 0 <= index < path size
x,yAddress of the variables receiving the coordinates of the point.

for (int i=0; i < path->size(); i++ ) { int x,y; path->get(i,&x,&y); printf ("Astar coord : %d %d\n", x,y ); } for (int i=0; i < dijkstra->size(); i++ ) { int x,y; dijkstra->get(i,&x,&y); printf ("Dijkstra coord : %d %d\n", x,y ); }

int i; for (i=0; i < TCOD_path_size(path); i++ ) { int x,y; TCOD_path_get(path,i,&x,&y); printf ("Astar coord : %d %d\n", x,y ); } for (i=0; i < TCOD_dijkstra_size(dijkstra); i++ ) { int x,y; TCOD_dijkstra_get(dijkstra,i,&x,&y); printf ("Dijkstra coord : %d %d\n", x,y ); }

for i in range (libtcod.path_size(path)) : x,y=libtcod.path_get(path,i) print 'Astar coord : ',x,y for i in range (libtcod.dijkstra_size(dijkstra)) : x,y=libtcod.dijkstra_get(dijkstra,i) print 'Dijkstra coord : ',x,y

◆ getOrigin()

void TCODPath::getOrigin ( int * x,
int * y ) const

You can read the current origin and destination cells with getOrigin/getDestination. Note that when you walk the path, the origin changes at each step.void TCODPath::getOrigin(int *x,int *y) const void TCODPath::getDestination(int *x,int *y) constvoid TCOD_path_get_origin(TCOD_path_t path, int *x, int *y) void TCOD_path_get_destination(TCOD_path_t path, int *x, int *y)path_get_origin(path) # returns x,y path_get_destination(path) # returns x,yvoid TCODPath::getOrigin(out int x, out int y) void TCODPath::getDestination(out int x, out int y).

Once the path has been computed, you can get information about it using of one those functions.

Parameters
pathIn the C version, the path handler returned by a creation function.
x,yThe function returns the cell coordinates in these variables

◆ isEmpty()

bool TCODPath::isEmpty ( ) const

If you want a creature to follow the path, a more convenient way is to walk the path : You know when you reached destination when the path is empty :bool TCODPath::isEmpty() const bool TCODDijkstra::isEmpty() constbool TCOD_path_is_empty(TCOD_path_t path) bool TCOD_dijkstra_is_empty(TCOD_dijkstra_t dijkstra)path_is_empty(path) dijkstra_is_empty(dijkstra)bool TCODPath::isEmpty() bool TCODDijkstra::isEmpty().

Parameters
path,dijkstraIn the C version, the path handler returned by a creation function.

◆ reverse()

void TCODPath::reverse ( )

Once you computed a path, you can exchange origin and destination :void TCODPath::reverse() void TCODDijkstra::reverse()void TCOD_path_reverse(TCOD_path_t path) void TCOD_dijkstra_reverse(TCOD_dijkstra_t dijkstra)path_reverse(path) dijkstra_reverse(dijkstra)void TCODPath::reverse() void TCODDijkstra::reverse().

Parameters
pathIn the C version, the path handler returned by a creation function.

TCODMap *myMap = new TCODMap(50,50); TCODPath *path = new TCODPath(myMap); // allocate the path path->compute(5,5,25,25); // calculate path from 5,5 to 25,25 path->reverse(); // now the path goes from 25,25 to 5,5

TCOD_map_t my_map=TCOD_map_new(50,50); TCOD_path_t path = TCOD_path_new_using_map(my_map); TCOD_path_compute(path,5,5,25,25); // calculate path from 5,5 to 25,25 TCOD_path_reverse(path); // now the path goes from 25,25 to 5,5

my_map=libtcod.map_new(50,50) path = libtcod.path_new_using_map(my_map) libtcod.path_compute(path,5,5,25,25) # calculate path from 5,5 to 25,25 libtcod.path_reverse(path) # now the path goes from 25,25 to 5,5

◆ size()

int TCODPath::size ( ) const

You can get the number of steps needed to reach destination :int TCODPath::size() const int TCODDijkstra::size() constint TCOD_path_size(TCOD_path_t path) int TCOD_dijkstra_size(TCOD_dijkstra_t dijkstra)path_size(path) dijkstra_size(dijkstra)int TCODPath::size() int TCODDijkstra::size().

Parameters
path,dijkstraIn the C version, the path handler returned by a creation function.

◆ walk()

bool TCODPath::walk ( int * x,
int * y,
bool recalculateWhenNeeded )

You can walk the path and go to the next step with : Note that walking the path consume one step (and decrease the path size by one).

The function returns false if recalculateWhenNeeded is false and the next cell on the path is no longer walkable, or if recalculateWhenNeeded is true, the next cell on the path is no longer walkable and no other path has been found. Also note that recalculateWhenNeeded only applies to A*.

bool TCODPath::walk(int *x, int *y, bool recalculateWhenNeeded) bool TCODDijkstra::walk(int *x, int *y)

bool TCOD_path_walk(TCOD_path_t path, int *x, int *y, bool recalculate_when_needed) bool TCOD_dijkstra_walk(TCOD_dijkstra_t dijkstra, int *x, int *y)

path_walk(TCOD_path_t path, recalculate_when_needed) # returns x,y or None,None if no path dijkstra_walk(TCOD_dijkstra_t dijkstra)

bool TCODPath::walk(ref int x, ref int y, bool recalculateWhenNeeded) bool TCODDijkstra::walk(ref int x, ref int y)

Parameters
path,dijkstraIn the C version, the path handler returned by a creation function.
x,yAddress of the variables receiving the coordinates of the next point.
recalculateWhenNeededIf the next point is no longer walkable (another creature may be in the way), recalculate a new path and walk it.

while (! path->isEmpty()) { int x,y; if (path->walk(&x,&y,true)) { printf ("Astar coord: %d %d\n",x,y ); } else { printf ("I'm stuck!\n" ); break; } } while (! dijkstra->isEmpty()) { int x,y; if (dijkstra->walk(&x,&y)) { printf ("Dijkstra coord: %d %d\n",x,y ); } else { printf ("I'm stuck!\n" ); break; } }

while (! TCOD_path_is_empty(path)) { int x,y; if (TCOD_path_walk(path,&x,&y,true)) { printf ("Astar coord: %d %d\n",x,y ); } else { printf ("I'm stuck!\n" ); break; } } while (! TCOD_dijkstra_is_empty(dijkstra)) { int x,y; if (TCOD_dijkstra_walk(dijkstra,&x,&y)) { printf ("Dijkstra coord: %d %d\n",x,y ); } else { printf ("I'm stuck!\n" ); break; } }

while not libtcod.path_is_empty(path)) : x,y=libtcod.path_walk(path,True) if not x is None : print 'Astar coord: ',x,y else : print "I'm stuck!" break while not libtcod.dijkstra_is_empty(dijkstra)) : x,y=libtcod.dijkstra_walk(dijkstra,True) if not x is None : print 'Dijkstra coord: ',x,y else : print "I'm stuck!" break


The documentation for this class was generated from the following file:
  • C:/Users/4b796/Projects/libtcod/src/libtcod/path.hpp