libtcoddocumentation

16.3. Reading path information

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

Getting the path origin and destination

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)

void TCODPath::getOrigin(out int x, out int y)
void
TCODPath::getDestination(out int x, out int y)

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

Getting the path length

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

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

Read the path cells' coordinates

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)

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

ParameterDescription
path,dijkstra In 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.
Example:

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 ("Dijsktra 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


Checking if the path is empty

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

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

Walking the path

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)

ParameterDescription
path,dijkstra In 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.
Example:

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


Getting the distance from a cell to the root node

You can get the distance of any set of coordinates from the root node:
Note that if the coordinates x,y are outside of the map or are a non-walkable position, the function will return -1.0f. This functionality is only available for Dijkstra's algorithm.

float TCODDijkstra::getDistance(int x, int y)

float TCOD_dijkstra_get_distance(TCOD_dijkstra_t dijkstra, int x, int y)

dijkstra_get_distance(dijkstra, x, y)

float TCODDijkstra::getDistance(int x, int y)

ParameterDescription
dijkstraIn the C version, the path handler returned by a creation function.
x,yThe coordinates whose distance from the root node are to be checked