libtcoddocumentation

16.2. Computing the path

Computing an A* path

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

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

bool TCOD_path_compute(TCOD_path_t path, int ox,int oy, int dx, int dy)

path_compute(path, ox, oy, dx, dy)

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

ParameterDescription
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.
Example:

TCODMap *myMap = new TCODMap(50,50);
TCODPath *path = new TCODPath(myMap); // allocate the path
path->compute(5,5,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)


Reversing a path

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

ParameterDescription
pathIn the C version, the path handler returned by a creation function.
Example:

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

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

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)


Computing a Dijkstra grid

In case of Dijkstra, this works in a slightly different way. In order to be able to compute a path, Dijkstra must first analyse the distances from the selected root (origin) node to all other nodes:

void TCODDijkstra::compute(int rootX, int rootY)

void TCOD_dijkstra_compute(TCOD_dijkstra_t dijkstra, int root_x, int root_y)

dijkstra_compute(dijkstra, root_x, root_y)

void TCODDijkstra::compute(int rootX, int rootY)

ParameterDescription
dijkstraIn the C version, the path handler returned by a creation function.
root_x,root_yCoordinates of the root node (origin) of the path.
The coordinates should be inside the map, at a walkable position. Otherwise, the function's behaviour will be undefined.

Computing a path from a Dijkstra grid

After the map is analysed and all the distances from the root node are known, an unlimited number of paths can be set, all originating at the root node, using:
The path setting function will return true if there's a path from the root node to the destination node. Otherwise, it will return false.

bool TCODDijkstra::setPath(int toX, int toY)

bool TCOD_dijkstra_path_set(TCOD_dijkstra_t dijkstra, int to_x, int to_y)

dijkstra_path_set(dijkstra, to_x, to_y)

bool TCODDijkstra::setPath(int toX, int toY)

ParameterDescription
dijkstraIn the C version, the path handler returned by a creation function.
to_x,to_yCoordinates of the destination node of the path.
Example:

TCODMap *myMap = new TCODMap(50,50);
TCODDijkstra *dijkstra = new TCODDijkstra(myMap); // allocate the path
dijkstra->compute(25,25); // calculate distance from 25,25 to all other nodes
dijkstra->setPath(5,5); // calculate a path to node 5,5
dijkstra->setPath(45,45);

TCOD_map_t my_map=TCOD_map_new(50,50);
TCOD_dijkstra_t
dijkstra = TCOD_dijkstra_new(my_map);
TCOD_dijkstra_compute(dijkstra,25,25);
TCOD_dijkstra_path_set(dijkstra,5,5);
TCOD_dijkstra_path_set(dijkstra,45,45);

my_map=libtcod.map_new(50,50)
dijkstra = libtcod.dijkstra_new(my_map)
libtcod.dijkstra_compute(dijkstra,25,25)
libtcod.dijkstra_path_set(dijkstra,5,5)
libtcod.dijkstra_path_set(dijkstra,45,45)