< index
< 13. Path finding
< 13.2 Computing the path

=====================================
13.3 Reading path information
=====================================

> 13.4 Destroying a path
Once the path has been computed, you can get information about it using of one those functions.
You can read the current origin and destination cells with :

C++ : void getOrigin(int *x,int *y) const
      void getDestination(int *x,int *y) const
C   : 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)
Py  : path_get_origin(path) # returns x,y
      path_get_destination(path) # returns x,y

ParameterDescription
pathIn the C version, the path handler returned by a creation function.
x,yThe function returns the cell coordinates in these variables
Note that when you walk the path, the origin changes at each step.


You can get the number of steps needed to reach destination :

C++ : int TCODPath::size() const
C   : int TCOD_path_size(TCOD_path_t path)
Py  : path_size(path)

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


You can get the coordinates of each point along the path :

C++ : void TCODPath::get(int index, int *x, int *y) const
C   : void TCOD_path_get(TCOD_path_t path, int index, int *x, int *y)
Py  : path_get(path, index) # returns x,y

ParameterDescription
pathIn 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 :

C++ : for (int i=0; i < path->size(); i++ ) {
           int x,y;
           path->get(i,&x,&y);
           printf ("coord : %d %d\n", x,y );
      }
C   : int i;
      for (i=0; i < TCOD_path_size(path); i++ ) {
           int x,y;
           TCOD_path_get(path,i,&x,&y);
           printf ("coord : %d %d\n", x,y );
      }
Py  : for i in range (libtcod.path_size(path)) :
           x,y=libtcod.path_get(path,i)
           print 'coord : ',x,y



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 :

C++ : bool TCODPath::isEmpty() const
C   : bool TCOD_path_is_empty(TCOD_path_t path)
Py  : path_is_empty(path)

ParameterDescription
pathIn the C version, the path handler returned by a creation function.
You can walk the path and go to the next step with :

C++ : bool TCODPath::walk(int *x, int *y, bool recalculateWhenNeeded)
C   : bool TCOD_path_walk(TCOD_path_t path, int *x, int *y, bool recalculate_when_needed)
Py  : path_walk(TCOD_path_t path, recalculate_when_needed) # returns x,y or None,None if no path

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

C++ : while (! path->isEmpty()) {
           int x,y;
           if (path->walk(&x,&y,true)) {
               printf ("coord : %d %d\n", x,y );
           } else {
               printf ("I'm stuck !\n" );
               break;
           }
      }
C   : while (! TCOD_path_is_empty(path)) {
           int x,y;
           if (TCOD_path_walk(path,&x,&y,true)) {
               printf ("coord : %d %d\n", x,y );
           } else {
               printf ("I'm stuck !\n" );
               break;
           }
      }
Py  : while not libtcod.path_is_empty(path)) :
           x,y=libtcod.path_walk(path,True)
           if not x is None :
               print 'coord : ',x,y
           else :
               print "I'm stuck !"
               break

insert a comment