Skip to content

Commit 8bbf253

Browse files
4B796C65@gmail.com4B796C65@gmail.com
authored andcommitted
Pathfinding works now
1 parent 904c4b3 commit 8bbf253

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

tdl/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,8 @@ class Console(_MetaConsole):
450450
def __init__(self, width, height):
451451
"""Create a new offscreen console
452452
"""
453+
if not _rootinitialized:
454+
raise TDLError('Can not create Console\'s before tdl.init')
453455
self._as_parameter_ = _lib.TCOD_console_new(width, height)
454456
self.width = width
455457
self.height = height

tdl/__tcod.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,12 @@ def __repr__(self):
484484
# PATH
485485

486486
TCOD_path_t = c_void_p
487-
PATHCALL = CFUNCTYPE(c_float, c_int, c_int, c_int, c_int, c_void_p)
487+
_PATHCALL = CFUNCTYPE(c_float, c_int, c_int, c_int, c_int, py_object)
488488

489489
_lib.TCOD_path_new_using_map.restype = TCOD_path_t
490490
_lib.TCOD_path_new_using_map.argtypes = (TCOD_map_t, c_float)
491491
_lib.TCOD_path_new_using_function.restype = TCOD_path_t
492-
_lib.TCOD_path_new_using_function.argtypes = (c_int, c_int, PATHCALL, c_void_p, c_float)
492+
_lib.TCOD_path_new_using_function.argtypes = (c_int, c_int, _PATHCALL, py_object, c_float)
493493
_lib.TCOD_path_compute.restype = c_bool
494494
_lib.TCOD_path_compute.argtypes = (TCOD_path_t, c_int, c_int, c_int, c_int)
495495
_lib.TCOD_path_walk.restype = c_bool
@@ -506,7 +506,7 @@ def __repr__(self):
506506
_lib.TCOD_dijkstra_new.restype = TCOD_dijkstra_t
507507
_lib.TCOD_dijkstra_new.argtypes = (TCOD_map_t, c_float)
508508
_lib.TCOD_dijkstra_new_using_function.restype = TCOD_dijkstra_t
509-
_lib.TCOD_dijkstra_new_using_function.argtypes = (c_int, c_int, PATHCALL, c_void_p, c_float)
509+
_lib.TCOD_dijkstra_new_using_function.argtypes = (c_int, c_int, POINTER(_PATHCALL), py_object, c_float)
510510
_lib.TCOD_dijkstra_compute.restype = None
511511
_lib.TCOD_dijkstra_compute.argtypes = (TCOD_dijkstra_t, c_int, c_int)
512512
_lib.TCOD_dijkstra_get_distance.restype = c_float

tdl/fov.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Map(object):
99

10-
def __init__(self, width, height, callback):
10+
def __init__(self, width, height, callback=None):
1111
self._width = int(width)
1212
self._height = int(height)
1313
self._size = self._width * self._height
@@ -49,6 +49,8 @@ def set(self, x, y, walkable, transparent):
4949
x, y, walkable, transparent)
5050

5151
def _updateMap(self, x, y, radius):
52+
if not self._callback:
53+
return
5254
c_bool = ctypes.c_bool
5355
for (x, cX),(y, cY) in self._pointsInRadiusC(x, y, radius):
5456
#if (x, y) not in self._clean:

tdl/path.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,43 @@
22
import math
33
import ctypes
44

5-
from .__tcod import _lib
5+
from .__tcod import _lib, _PATHCALL
66

77
class AStar(object):
88

99
def __init__(self, width, height, callback, diagnalCost=math.sqrt(2)):
1010
def newCallback(fromX, fromY, toX, toY, null):
1111
pathCost = callback(toX, toY) # expecting a float or 0
12-
return pathCost
13-
self.callback = newCallback
12+
if pathCost:
13+
return pathCost
14+
return 0.0
15+
self.callback = _PATHCALL(newCallback)
1416
self._as_parameter_ = _lib.TCOD_path_new_using_function(width, height,
15-
newCallback, None, diagnalCost)
17+
self.callback, None, diagnalCost)
1618

19+
@classmethod
20+
def newFromMap(cls, map, diagnalCost=math.sqrt(2)):
21+
self = cls.__new__(cls)
22+
self.callback = None
23+
self._as_parameter_ = _lib.TCOD_path_new_using_map(map, diagnalCost)
24+
return self
25+
26+
def __del__(self):
27+
_lib.TCOD_path_delete(self)
28+
1729
def getPath(self, origX, origY, destX, destY):
1830
found = _lib.TCOD_path_compute(self, origX, origY, destX, destY)
1931
if not found:
20-
pass # path not found, not sure what to do
32+
return [] # path not found
2133
x, y = ctypes.c_int(), ctypes.c_int()
2234
xRef, yRef = ctypes.byref(x), ctypes.byref(y)
2335
recalculate = ctypes.c_bool(False)
2436
path = []
2537
while _lib.TCOD_path_walk(self, xRef, yRef, recalculate):
26-
path.append(x.value, y.value)
38+
path.append((x.value, y.value))
2739
return path
2840

2941

30-
def __del__(self):
31-
_lib.TCOD_path_delete(self)
3242

3343
class Dijkstra(object):
3444

@@ -38,9 +48,16 @@ def newCallback(fromX, fromY, toX, toY, null):
3848
return pathCost
3949
self.callback = newCallback
4050
self._as_parameter_ = _lib.TCOD_dijkstra_new_using_function(width, height,
41-
newCallback, None, diagnalCost)
51+
_PATHCALL(newCallback), None, diagnalCost)
4252
# add code to compute here with x,y
4353

54+
def __del__(self):
55+
_lib.TCOD_dijkstra_delete(self)
56+
57+
def setPos(self, x, y):
58+
self.x, self.y = x, y
59+
_lib.TCOD_dijkstra_compute(self, x, y)
60+
4461
def getPathFrom(self, startX, startY):
4562
pass
4663

0 commit comments

Comments
 (0)