22import math
33import ctypes
44
5- from .__tcod import _lib
5+ from .__tcod import _lib , _PATHCALL
66
77class 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
3343class 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