|
1
|
|
|
|
|
|
|
|
|
|
2
|
|
|
from __future__ import absolute_import as _ |
|
3
|
|
|
|
|
4
|
|
|
import numpy as np |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
from tcod.libtcod import lib, ffi |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class BufferBitProxy(object): |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
def __init__(self, buffer, bitmask): |
|
12
|
|
|
self.buffer = buffer |
|
13
|
|
|
self.bitmask = np.asarray(bitmask, dtype=np.uint8) |
|
14
|
|
|
|
|
15
|
|
|
def __getitem__(self, index): |
|
16
|
|
|
return (self.buffer[index] & self.bitmask) != 0 |
|
17
|
|
|
|
|
18
|
|
|
def __setitem__(self, index, values): |
|
19
|
|
|
self.buffer[index] &= 0xff ^ self.bitmask |
|
20
|
|
|
self.buffer[index] |= self.bitmask * np.asarray(values, dtype=bool) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class Map(object): |
|
24
|
|
|
""" |
|
25
|
|
|
.. versionadded:: 2.0 |
|
26
|
|
|
|
|
27
|
|
|
Args: |
|
28
|
|
|
width (int): Width of the new Map. |
|
29
|
|
|
height (int): Height of the new Map. |
|
30
|
|
|
|
|
31
|
|
|
Attributes: |
|
32
|
|
|
width (int): Read only width of this Map. |
|
33
|
|
|
height (int): Read only height of this Map. |
|
34
|
|
|
map_c (CData): A cffi pointer to a TCOD_map_t object. |
|
35
|
|
|
.. versionadded:: 2.5 |
|
36
|
|
|
""" |
|
37
|
|
|
|
|
38
|
|
|
def __init__(self, width, height): |
|
39
|
|
|
assert ffi.sizeof('cell_t') == 1 # assert buffer alignment |
|
40
|
|
|
self.buffer = np.zeros((height, width), dtype=np.uint8) |
|
41
|
|
|
|
|
42
|
|
|
self.map_c = ffi.new('map_t*') |
|
43
|
|
|
self.map_c.width = self.width = width |
|
44
|
|
|
self.map_c.height = self.height = height |
|
45
|
|
|
self.map_c.nbcells = width * height |
|
46
|
|
|
self.map_c.cells = ffi.cast('cell_t*', self.buffer.ctypes.data) |
|
47
|
|
|
|
|
48
|
|
|
self._init_proxies() |
|
49
|
|
|
|
|
50
|
|
|
def _init_proxies(self): |
|
|
|
|
|
|
51
|
|
|
self.transparent = BufferBitProxy(self.buffer, 0x01) |
|
52
|
|
|
self.walkable = BufferBitProxy(self.buffer, 0x02) |
|
53
|
|
|
self.fov = BufferBitProxy(self.buffer, 0x04) |
|
54
|
|
|
|
|
55
|
|
|
def compute_fov(self, x, y, radius=0, light_walls=True, |
|
56
|
|
|
algorithm=lib.FOV_RESTRICTIVE): |
|
57
|
|
|
""" |
|
58
|
|
|
|
|
59
|
|
|
Args: |
|
60
|
|
|
x (int): |
|
61
|
|
|
y (int): |
|
62
|
|
|
radius (int): |
|
63
|
|
|
light_walls (bool): |
|
64
|
|
|
algorithm (int): Defaults to FOV_RESTRICTIVE |
|
65
|
|
|
""" |
|
66
|
|
|
lib.TCOD_map_compute_fov(self.map_c, x, y, radius, light_walls, |
|
67
|
|
|
algorithm) |
|
68
|
|
|
|
|
69
|
|
|
def __getstate__(self): |
|
70
|
|
|
"""Return this objects state. |
|
71
|
|
|
|
|
72
|
|
|
This method allows the usage of the :mod:`copy` and :mod:`pickle` |
|
73
|
|
|
modules with this class. |
|
74
|
|
|
""" |
|
75
|
|
|
state = self.__dict__.copy() |
|
76
|
|
|
del state['map_c'] |
|
77
|
|
|
return state |
|
78
|
|
|
return {'size': (self.width, self.height), 'buffer': self.buffer} |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
def __setstate__(self, state): |
|
81
|
|
|
"""Unpack this object from a saved state. A new buffer is used.""" |
|
82
|
|
|
if 'width' not in state or 'height' not in state: |
|
83
|
|
|
state['width'], state['height'] = state['size'] |
|
84
|
|
|
del state['size'] |
|
85
|
|
|
|
|
86
|
|
|
self.__dict__.update(state) |
|
87
|
|
|
|
|
88
|
|
|
self.map_c = ffi.new( |
|
89
|
|
|
'map_t *', |
|
90
|
|
|
( |
|
91
|
|
|
self.width, |
|
92
|
|
|
self.height, |
|
93
|
|
|
self.width * self.height, |
|
94
|
|
|
ffi.cast('cell_t*', self.buffer.ctypes.data), |
|
95
|
|
|
) |
|
96
|
|
|
) |
|
97
|
|
|
|
|
98
|
|
|
self._init_proxies() |
|
99
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.