Class TCODPath

Nested Relationships

Nested Types

Class Documentation

class TCODPath

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:

Public 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::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) 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)

Parameters
  • map – The map. The path finder will use the ‘walkable’ property of the cells to find a path.

  • diagonalCost – Cost 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.

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) 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)

Parameters
  • width, height – The size of the map (in map cells).

  • callback – A 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.

  • userData – Custom data that will be passed to the function.

  • diagonalCost – Cost 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.

TCODPath(const TCODPath&) = delete
TCODPath &operator=(const TCODPath&) = delete
inline TCODPath(TCODPath &&rhs) noexcept
inline TCODPath &operator=(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()

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)

Parameters
  • path – In the C version, the path handler returned by one of the TCOD_path_new_* function.

  • dijkstra – In the C version, the path handler returned by one of the TCOD_dijkstra_new* function.

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

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

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)

Parameters
  • path – In the C version, the path handler returned by a creation function.

  • ox, oy – Coordinates of the origin of the path.

  • dx, dy – Coordinates 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.

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()

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

Parameters

path – In the C version, the path handler returned by a creation function.

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) const

void 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,y

void 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
  • path – In the C version, the path handler returned by a creation function.

  • x, y – The function returns the cell coordinates in these variables

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() const

int 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, dijkstra – In the C version, the path handler returned by a creation function.

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) const

void 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,y

int TCODPath::size() int TCODDijkstra::size()

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

Parameters
  • path, dijkstra – In the C version, the path handler returned by a creation function.

  • index – Step number. 0 <= index < path size

  • x, y – Address of the variables receiving the coordinates of the point.

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() const

bool 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, dijkstra – In the C version, the path handler returned by a creation function.

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).

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)

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

Parameters
  • path, dijkstra – In the C version, the path handler returned by a creation function.

  • x, y – Address of the variables receiving the coordinates of the next point.

  • recalculateWhenNeeded – If the next point is no longer walkable (another creature may be in the way), recalculate a new path and walk it.

Protected Attributes

TCOD_path_t data
struct TCODPath::WrapperData cppData

Friends

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

Public Members

void *userData
const ITCODPathCallback *listener