|
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 = bitmask |
|
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, 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
|
|
|
""" |
|
35
|
|
|
|
|
36
|
|
|
def __init__(self, width, height): |
|
37
|
|
|
assert ffi.sizeof('cell_t') == 1 # assert buffer alignment |
|
38
|
|
|
self.buffer = np.zeros((height, width), dtype=np.uint8) |
|
39
|
|
|
|
|
40
|
|
|
self.cdata = ffi.new('map_t*') |
|
41
|
|
|
self.cdata.width = self.width = width |
|
42
|
|
|
self.cdata.height = self.height = height |
|
43
|
|
|
self.cdata.nbcells = width * height |
|
44
|
|
|
self.cdata.cells = ffi.cast('cell_t*', self.buffer.ctypes.data) |
|
45
|
|
|
|
|
46
|
|
|
self.transparent = BufferBitProxy(self.buffer, 0x01) |
|
47
|
|
|
self.walkable = BufferBitProxy(self.buffer, 0x02) |
|
48
|
|
|
self.fov = BufferBitProxy(self.buffer, 0x04) |
|
49
|
|
|
|
|
50
|
|
|
def compute_fov(self, x, y, radius=0, light_walls=True, |
|
51
|
|
|
algorithm=lib.FOV_RESTRICTIVE): |
|
52
|
|
|
""" |
|
53
|
|
|
|
|
54
|
|
|
Args: |
|
55
|
|
|
x (int): |
|
56
|
|
|
y (int): |
|
57
|
|
|
radius (int): |
|
58
|
|
|
light_walls (bool): |
|
59
|
|
|
algorithm (int): Defaults to FOV_RESTRICTIVE |
|
60
|
|
|
""" |
|
61
|
|
|
lib.TCOD_map_compute_fov(self.cdata, x, y, radius, light_walls, |
|
62
|
|
|
algorithm) |
|
63
|
|
|
|
|
64
|
|
|
def __getstate__(self): |
|
65
|
|
|
"""Return this objects state. |
|
66
|
|
|
|
|
67
|
|
|
This method allows the usage of the :any:`copy` and :any:`pickle` |
|
68
|
|
|
modules with this class. |
|
69
|
|
|
""" |
|
70
|
|
|
return {'size': (self.width, self.height), 'buffer': self.buffer} |
|
71
|
|
|
|
|
72
|
|
|
def __setstate__(self, state): |
|
73
|
|
|
"""Unpack this object from a saved state. A new buffer is used.""" |
|
74
|
|
|
self.__init__(*state['size']) |
|
75
|
|
|
self.buffer[...] = state['buffer'] |
|
76
|
|
|
|
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.