Skip to content

Commit 023b4f2

Browse files
committed
fixed pathfinding module
1 parent 6e636ef commit 023b4f2

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

build_libtcod.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,41 @@ def _get_library_crossplatform():
527527
void TCOD_noise_delete(TCOD_noise_t noise);
528528
529529
530+
// PATH
531+
532+
typedef float (*TCOD_path_func_t)( int xFrom, int yFrom, int xTo, int yTo, void *user_data );
533+
typedef void *TCOD_path_t;
534+
535+
TCOD_path_t TCOD_path_new_using_map(TCOD_map_t map, float diagonalCost);
536+
TCOD_path_t TCOD_path_new_using_function(int map_width, int map_height, TCOD_path_func_t func, void *user_data, float diagonalCost);
537+
538+
bool TCOD_path_compute(TCOD_path_t path, int ox,int oy, int dx, int dy);
539+
bool TCOD_path_walk(TCOD_path_t path, int *x, int *y, bool recalculate_when_needed);
540+
bool TCOD_path_is_empty(TCOD_path_t path);
541+
int TCOD_path_size(TCOD_path_t path);
542+
void TCOD_path_reverse(TCOD_path_t path);
543+
void TCOD_path_get(TCOD_path_t path, int index, int *x, int *y);
544+
void TCOD_path_get_origin(TCOD_path_t path, int *x, int *y);
545+
void TCOD_path_get_destination(TCOD_path_t path, int *x, int *y);
546+
void TCOD_path_delete(TCOD_path_t path);
547+
548+
/* Dijkstra stuff - by Mingos*/
549+
550+
typedef void *TCOD_dijkstra_t;
551+
552+
TCOD_dijkstra_t TCOD_dijkstra_new (TCOD_map_t map, float diagonalCost);
553+
TCOD_dijkstra_t TCOD_dijkstra_new_using_function(int map_width, int map_height, TCOD_path_func_t func, void *user_data, float diagonalCost);
554+
void TCOD_dijkstra_compute (TCOD_dijkstra_t dijkstra, int root_x, int root_y);
555+
float TCOD_dijkstra_get_distance (TCOD_dijkstra_t dijkstra, int x, int y);
556+
bool TCOD_dijkstra_path_set (TCOD_dijkstra_t dijkstra, int x, int y);
557+
bool TCOD_dijkstra_is_empty(TCOD_dijkstra_t path);
558+
int TCOD_dijkstra_size(TCOD_dijkstra_t path);
559+
void TCOD_dijkstra_reverse(TCOD_dijkstra_t path);
560+
void TCOD_dijkstra_get(TCOD_dijkstra_t path, int index, int *x, int *y);
561+
bool TCOD_dijkstra_path_walk (TCOD_dijkstra_t dijkstra, int *x, int *y);
562+
void TCOD_dijkstra_delete (TCOD_dijkstra_t dijkstra);
563+
564+
530565
// CUSTOM FUNCTONS
531566
532567
void set_char(TCOD_console_t console, int x, int y,

tdl/map.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AStar(object):
3434

3535
__slots__ = ('_as_parameter_', '_callback', '__weakref__')
3636

37-
_create_callback = _ffi.callback('float(int, int, int, int, void*)')
37+
3838

3939
def __init__(self, width, height, callback,
4040
diagnalCost=_math.sqrt(2), advanced=False):
@@ -91,15 +91,15 @@ def newCallback(sourceX, sourceY, destX, destY, null):
9191
if pathCost:
9292
return pathCost
9393
return 0.0
94-
self._callback = self._create_callback(newCallback)
94+
self._callback = _ffi.callback('TCOD_path_func_t')(newCallback)
9595
"""A cffi callback to be kept in memory."""
9696

9797
self._as_parameter_ = _lib.TCOD_path_new_using_function(width, height,
9898
self._callback, _ffi.NULL, diagnalCost)
9999

100100
def __del__(self):
101101
if self._as_parameter_:
102-
_lib.TCOD_path_delete(self)
102+
_lib.TCOD_path_delete(self._as_parameter_)
103103
self._as_parameter_ = None
104104

105105
def getPath(self, origX, origY, destX, destY):
@@ -112,13 +112,13 @@ def getPath(self, origX, origY, destX, destY):
112112
113113
If no path is found then an empty list is returned.
114114
"""
115-
found = _lib.TCOD_path_compute(self, origX, origY, destX, destY)
115+
found = _lib.TCOD_path_compute(self._as_parameter_, origX, origY, destX, destY)
116116
if not found:
117117
return [] # path not found
118118
x, y = _ffi.new('int *'), _ffi.new('int *')
119-
recalculate = _ffi.new('bool *', True)
119+
recalculate = True
120120
path = []
121-
while _lib.TCOD_path_walk(self, x, y, recalculate):
121+
while _lib.TCOD_path_walk(self._as_parameter_, x, y, recalculate):
122122
path.append((x[0], y[0]))
123123
return path
124124

0 commit comments

Comments
 (0)