11
22import itertools
3+ import ctypes
4+ import array
35
46from .__tcod import _lib
57
68class Map (object ):
79
8- def __init__ (self , width , height ):
10+ def __init__ (self , width , height , callback ):
911 self ._width = int (width )
1012 self ._height = int (height )
1113 self ._size = self ._width * self ._height
1214 self ._tcodMap = _lib .TCOD_map_new (width , height )
1315 self ._as_parameter_ = self ._tcodMap
14- self ._fovConf = (0 , 0 , 0 ) # fov settings (x, y, radius)
16+ self ._callback = callback
17+ self ._clean = set ()
1518 #self._walkable = array.array('b', [0] * self._size)
1619 #self._transparent = array.array('b', [0] * self._size)
1720
1821 def __del__ (self ):
1922 _lib .TCOD_map_delete (self )
2023
24+ def _pointsInRadius (self , x , y , radius ):
25+ 'returns a list of (x, y) items'
26+ x = range (max (0 , x - radius ), min (x + radius + 1 , self ._width ))
27+ y = range (max (0 , y - radius ), min (y + radius + 1 , self ._height ))
28+ return itertools .product (x , y )
29+
30+ def _pointsInRadiusC (self , x , y , radius ):
31+ 'returns a list of ((x, ctypeX), (y, ctypeY)) items'
32+ c_int = ctypes .c_int
33+ x = ((i , c_int (i )) for i in
34+ range (max (0 , x - radius ), min (x + radius + 1 , self ._width )))
35+ y = ((i , c_int (i )) for i in
36+ range (max (0 , y - radius ), min (y + radius + 1 , self ._height )))
37+ return itertools .product (x , y )
38+
2139 def setFromCallbacks (self , walkableCall , transparentCall ):
2240 for x , y in itertools .product (range (self ._width ), range (self ._height )):
2341 _lib .TCOD_map_set_properties (self ._tcodMap , x , y ,
@@ -30,6 +48,15 @@ def set(self, x, y, walkable, transparent):
3048 _lib .TCOD_map_set_properties (self ._as_parameter_ ,
3149 x , y , walkable , transparent )
3250
51+ def _updateMap (self , x , y , radius ):
52+ c_bool = ctypes .c_bool
53+ for (x , cX ),(y , cY ) in self ._pointsInRadiusC (x , y , radius ):
54+ #if (x, y) not in self._clean:
55+ # self._clean.add((x,y))
56+ transparent = c_bool (self ._callback (x , y ))
57+ _lib .TCOD_map_set_properties (self ._as_parameter_ ,
58+ cX , cY , transparent , transparent )
59+
3360 def computeFOV (self , x , y , fov = 'PERMISSIVE' , radius = 8 , lightWalls = True ):
3461 oldFOV = fov
3562 fov = str (fov ).upper ()
@@ -41,15 +68,19 @@ def computeFOV(self, x, y, fov='PERMISSIVE', radius=8, lightWalls=True):
4168 fov = 4 + int (fov [10 ])
4269 else :
4370 raise TDLError ('No such fov as %s' % oldFOV )
71+
72+ self ._updateMap (x , y , radius )
4473 _lib .TCOD_map_compute_fov (self , x , y , radius , lightWalls , fov )
45- self ._fovConf = (x , y , radius )
46- return self ._iterFOV ()
74+ return self ._listFOV (x , y , radius )
4775
48- def _iterFOV (self ):
49- x , y , radius = self ._fovConf
76+ def _iterFOV (self , x , y , radius ):
5077 inFOV = _lib .TCOD_map_is_in_fov
5178 map = self ._as_parameter_
52- for x ,y in itertools .product (range (x - radius , x + radius + 1 ),
53- range (y - radius , y + radius + 1 )):
54- if inFOV (map , x , y ):
79+ for (x , cX ),(y , cY ) in self ._pointsInRadiusC (x , y , radius ):
80+ if inFOV (map , cX , cY ):
5581 yield (x , y )
82+
83+ def _listFOV (self , x , y , radius ):
84+ return list (self ._iterFOV (x , y , radius ))
85+
86+ __all__ = [Map ]
0 commit comments