1
|
|
|
"""libtcod map attributes and field-of-view functions. |
2
|
|
|
|
3
|
|
|
Example:: |
4
|
|
|
|
5
|
|
|
>>> import tcod.map |
6
|
|
|
>>> m = tcod.map.Map(width=3, height=4) |
7
|
|
|
>>> m.walkable |
8
|
|
|
array([[False, False, False], |
9
|
|
|
[False, False, False], |
10
|
|
|
[False, False, False], |
11
|
|
|
[False, False, False]], dtype=bool) |
12
|
|
|
|
13
|
|
|
# Like the rest of the tcod modules, all arrays here are |
14
|
|
|
# in row-major order and are addressed with [y,x] |
15
|
|
|
>>> m.transparent[:] = True # Sets all to True. |
16
|
|
|
>>> m.transparent[1:3,0] = False # Sets (1, 0) and (2, 0) to False. |
17
|
|
|
>>> m.transparent |
18
|
|
|
array([[ True, True, True], |
19
|
|
|
[False, True, True], |
20
|
|
|
[False, True, True], |
21
|
|
|
[ True, True, True]], dtype=bool) |
22
|
|
|
|
23
|
|
|
>>> m.compute_fov(0, 0) |
24
|
|
|
>>> m.fov |
25
|
|
|
array([[ True, True, True], |
26
|
|
|
[ True, True, True], |
27
|
|
|
[False, True, True], |
28
|
|
|
[False, False, True]], dtype=bool) |
29
|
|
|
>>> m.fov[3,1] |
30
|
|
|
False |
31
|
|
|
|
32
|
|
|
""" |
33
|
|
|
|
34
|
|
|
from __future__ import absolute_import |
35
|
|
|
|
36
|
|
|
import numpy as np |
|
|
|
|
37
|
|
|
|
38
|
|
|
from tcod.libtcod import lib, ffi |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
class Map(object): |
42
|
|
|
"""A map containing libtcod attributes. |
43
|
|
|
|
44
|
|
|
.. versionchanged:: 4.1 |
45
|
|
|
`transparent`, `walkable`, and `fov` are now numpy boolean arrays. |
46
|
|
|
|
47
|
|
|
Args: |
48
|
|
|
width (int): Width of the new Map. |
49
|
|
|
height (int): Height of the new Map. |
50
|
|
|
|
51
|
|
|
Attributes: |
52
|
|
|
width (int): Read only width of this Map. |
53
|
|
|
height (int): Read only height of this Map. |
54
|
|
|
transparent: A boolean array of transparent cells. |
55
|
|
|
walkable: A boolean array of walkable cells. |
56
|
|
|
fov: A boolean array of the cells lit by :any:'compute_fov'. |
57
|
|
|
|
58
|
|
|
""" |
59
|
|
|
|
60
|
|
|
def __init__(self, width, height): |
61
|
|
|
assert ffi.sizeof('cell_t') == 3 # assert buffer alignment |
62
|
|
|
self.width = width |
63
|
|
|
self.height = height |
64
|
|
|
|
65
|
|
|
self.__buffer = np.zeros((height, width, 3), dtype=np.bool_) |
66
|
|
|
self.map_c = self.__as_cdata() |
67
|
|
|
|
68
|
|
|
def __as_cdata(self): |
|
|
|
|
69
|
|
|
return ffi.new( |
70
|
|
|
'map_t *', |
71
|
|
|
( |
72
|
|
|
self.width, |
73
|
|
|
self.height, |
74
|
|
|
self.width * self.height, |
75
|
|
|
ffi.cast('cell_t*', self.__buffer.ctypes.data), |
76
|
|
|
) |
77
|
|
|
) |
78
|
|
|
|
79
|
|
|
@property |
80
|
|
|
def transparent(self): |
|
|
|
|
81
|
|
|
return self.__buffer[:,:,0] |
|
|
|
|
82
|
|
|
|
83
|
|
|
@property |
84
|
|
|
def walkable(self): |
|
|
|
|
85
|
|
|
return self.__buffer[:,:,1] |
|
|
|
|
86
|
|
|
|
87
|
|
|
@property |
88
|
|
|
def fov(self): |
|
|
|
|
89
|
|
|
return self.__buffer[:,:,2] |
|
|
|
|
90
|
|
|
|
91
|
|
|
def compute_fov(self, x, y, radius=0, light_walls=True, |
|
|
|
|
92
|
|
|
algorithm=lib.FOV_RESTRICTIVE): |
93
|
|
|
"""Compute a field-of-view on the current instance. |
94
|
|
|
|
95
|
|
|
Args: |
96
|
|
|
x (int): Point of view, x-coordinate. |
97
|
|
|
y (int): Point of view, y-coordinate. |
98
|
|
|
radius (int): Maximum view distance from the point of view. |
99
|
|
|
|
100
|
|
|
A value of `0` will give an infinite distance. |
101
|
|
|
light_walls (bool): Light up walls, or only the floor. |
102
|
|
|
algorithm (int): Defaults to tcod.FOV_RESTRICTIVE |
103
|
|
|
""" |
104
|
|
|
lib.TCOD_map_compute_fov( |
105
|
|
|
self.map_c, x, y, radius, light_walls, algorithm) |
106
|
|
|
|
107
|
|
|
def __setstate__(self, state): |
108
|
|
|
if '_Map__buffer' not in state: # deprecated |
109
|
|
|
self.__buffer = np.zeros((state['height'], state['width'], 3), |
110
|
|
|
dtype=np.bool_) |
111
|
|
|
self.__buffer[:,:,0] = state['buffer'] & 0x01 |
|
|
|
|
112
|
|
|
self.__buffer[:,:,1] = state['buffer'] & 0x02 |
|
|
|
|
113
|
|
|
self.__buffer[:,:,2] = state['buffer'] & 0x04 |
|
|
|
|
114
|
|
|
del state['buffer'] |
115
|
|
|
self.__dict__.update(state) |
116
|
|
|
self.map_c = self.__as_cdata() |
117
|
|
|
|
118
|
|
|
def __getstate__(self): |
119
|
|
|
state = self.__dict__.copy() |
120
|
|
|
del state['map_c'] |
121
|
|
|
return state |
122
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.