|
1
|
|
|
"""This module focuses on improvements to the Python libtcod API. |
|
2
|
|
|
""" |
|
3
|
|
|
from __future__ import absolute_import as _ |
|
4
|
|
|
|
|
5
|
|
|
import os as _os |
|
|
|
|
|
|
6
|
|
|
import sys as _sys |
|
7
|
|
|
|
|
8
|
|
|
import platform as _platform |
|
|
|
|
|
|
9
|
|
|
import weakref as _weakref |
|
|
|
|
|
|
10
|
|
|
import functools as _functools |
|
11
|
|
|
|
|
12
|
|
|
from tcod.libtcod import lib, ffi, BKGND_DEFAULT, BKGND_SET |
|
13
|
|
|
|
|
14
|
|
|
def _unpack_char_p(char_p): |
|
|
|
|
|
|
15
|
|
|
if char_p == ffi.NULL: |
|
|
|
|
|
|
16
|
|
|
return '' |
|
17
|
|
|
return ffi.string(char_p).decode() |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def _int(int_or_str): |
|
20
|
|
|
'return an integer where a single character string may be expected' |
|
21
|
|
|
if isinstance(int_or_str, str): |
|
22
|
|
|
return ord(int_or_str) |
|
23
|
|
|
if isinstance(int_or_str, bytes): |
|
24
|
|
|
return int_or_str[0] |
|
25
|
|
|
return int(int_or_str) # check for __count__ |
|
26
|
|
|
|
|
27
|
|
|
def _cdata(cdata): |
|
28
|
|
|
"""covert value into a cffi.CData instance""" |
|
29
|
|
|
try: # first check for _CDataWrapper |
|
30
|
|
|
cdata = cdata.cdata |
|
31
|
|
|
except AttributeError: # assume cdata is valid |
|
|
|
|
|
|
32
|
|
|
pass |
|
33
|
|
|
except KeyError: |
|
|
|
|
|
|
34
|
|
|
pass |
|
35
|
|
|
if cdata is None or cdata == 0: # convert None to NULL |
|
36
|
|
|
cdata = ffi.NULL |
|
|
|
|
|
|
37
|
|
|
return cdata |
|
38
|
|
|
|
|
39
|
|
|
if _sys.version_info[0] == 2: # Python 2 |
|
40
|
|
|
def _bytes(string): |
|
|
|
|
|
|
41
|
|
|
if isinstance(string, unicode): |
|
|
|
|
|
|
42
|
|
|
return string.encode() |
|
43
|
|
|
return string |
|
44
|
|
|
|
|
45
|
|
|
def _unicode(string): |
|
|
|
|
|
|
46
|
|
|
if not isinstance(string, unicode): |
|
|
|
|
|
|
47
|
|
|
return string.decode() |
|
48
|
|
|
return string |
|
49
|
|
|
|
|
50
|
|
|
else: # Python 3 |
|
51
|
|
|
def _bytes(string): |
|
|
|
|
|
|
52
|
|
|
if isinstance(string, str): |
|
53
|
|
|
return string.encode() |
|
54
|
|
|
return string |
|
55
|
|
|
|
|
56
|
|
|
def _unicode(string): |
|
|
|
|
|
|
57
|
|
|
if isinstance(string, bytes): |
|
58
|
|
|
return string.decode() |
|
59
|
|
|
return string |
|
60
|
|
|
|
|
61
|
|
|
def _fmt_bytes(string): |
|
|
|
|
|
|
62
|
|
|
return _bytes(string).replace(b'%', b'%%') |
|
63
|
|
|
|
|
64
|
|
|
def _fmt_unicode(string): |
|
|
|
|
|
|
65
|
|
|
return _unicode(string).replace(u'%', u'%%') |
|
66
|
|
|
|
|
67
|
|
|
class _PropagateException(): |
|
68
|
|
|
""" context manager designed to propagate exceptions outside of a cffi |
|
69
|
|
|
callback context. normally cffi suppresses the exception |
|
70
|
|
|
|
|
71
|
|
|
when propagate is called this class will hold onto the error until the |
|
72
|
|
|
control flow leaves the context, then the error will be raised |
|
73
|
|
|
|
|
74
|
|
|
with _PropagateException as propagate: |
|
75
|
|
|
# give propagate as onerror parameter for ffi.def_extern |
|
76
|
|
|
""" |
|
77
|
|
|
|
|
78
|
|
|
def __init__(self): |
|
79
|
|
|
self.exc_info = None # (exception, exc_value, traceback) |
|
80
|
|
|
|
|
81
|
|
|
def propagate(self, *exc_info): |
|
82
|
|
|
""" set an exception to be raised once this context exits |
|
83
|
|
|
|
|
84
|
|
|
if multiple errors are caught, only keep the first exception raised |
|
85
|
|
|
""" |
|
86
|
|
|
if not self.exc_info: |
|
87
|
|
|
self.exc_info = exc_info |
|
88
|
|
|
|
|
89
|
|
|
def __enter__(self): |
|
90
|
|
|
""" once in context, only the propagate call is needed to use this |
|
91
|
|
|
class effectively |
|
92
|
|
|
""" |
|
93
|
|
|
return self.propagate |
|
94
|
|
|
|
|
95
|
|
|
def __exit__(self, type, value, traceback): |
|
|
|
|
|
|
96
|
|
|
""" if we're holding on to an exception, raise it now |
|
97
|
|
|
|
|
98
|
|
|
prefers our held exception over any current raising error |
|
99
|
|
|
|
|
100
|
|
|
self.exc_info is reset now in case of nested manager shenanigans |
|
101
|
|
|
""" |
|
102
|
|
|
if self.exc_info: |
|
103
|
|
|
type, value, traceback = self.exc_info |
|
|
|
|
|
|
104
|
|
|
self.exc_info = None |
|
105
|
|
|
if type: |
|
106
|
|
|
# Python 2/3 compatible throw |
|
107
|
|
|
exception = type(value) |
|
108
|
|
|
exception.__traceback__ = traceback |
|
109
|
|
|
raise exception |
|
110
|
|
|
|
|
111
|
|
|
class _CDataWrapper(object): |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def __init__(self, *args, **kargs): |
|
114
|
|
|
self.cdata = self._get_cdata_from_args(*args, **kargs) |
|
115
|
|
|
if self.cdata == None: |
|
116
|
|
|
self.cdata = ffi.NULL |
|
|
|
|
|
|
117
|
|
|
super(_CDataWrapper, self).__init__() |
|
118
|
|
|
|
|
119
|
|
|
def _get_cdata_from_args(self, *args, **kargs): |
|
|
|
|
|
|
120
|
|
|
if len(args) == 1 and isinstance(args[0], ffi.CData) and not kargs: |
|
|
|
|
|
|
121
|
|
|
return args[0] |
|
122
|
|
|
else: |
|
123
|
|
|
return None |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
def __hash__(self): |
|
127
|
|
|
return hash(self.cdata) |
|
128
|
|
|
|
|
129
|
|
|
def __eq__(self, other): |
|
130
|
|
|
try: |
|
131
|
|
|
return self.cdata == other.cdata |
|
132
|
|
|
except AttributeError: |
|
133
|
|
|
return NotImplemented |
|
134
|
|
|
|
|
135
|
|
|
def __getattr__(self, attr): |
|
136
|
|
|
if 'cdata' in self.__dict__: |
|
137
|
|
|
return getattr(self.__dict__['cdata'], attr) |
|
138
|
|
|
raise AttributeError(attr) |
|
139
|
|
|
|
|
140
|
|
|
def __setattr__(self, attr, value): |
|
141
|
|
|
if hasattr(self, 'cdata') and hasattr(self.cdata, attr): |
|
142
|
|
|
setattr(self.cdata, attr, value) |
|
143
|
|
|
else: |
|
144
|
|
|
super(_CDataWrapper, self).__setattr__(attr, value) |
|
145
|
|
|
|
|
146
|
|
|
def _assert_cdata_is_not_null(func): |
|
147
|
|
|
"""Any BSP methods which use a cdata object in a TCOD call need to have |
|
148
|
|
|
a sanity check, otherwise it may end up passing a NULL pointer""" |
|
149
|
|
|
if __debug__: |
|
150
|
|
|
@_functools.wraps(func) |
|
151
|
|
|
def check_sanity(*args, **kargs): |
|
|
|
|
|
|
152
|
|
|
assert self.cdata != ffi.NULL and self.cdata is not None, \ |
|
|
|
|
|
|
153
|
|
|
'cannot use function, cdata is %r' % self.cdata |
|
|
|
|
|
|
154
|
|
|
return func(*args, **kargs) |
|
155
|
|
|
return func |
|
156
|
|
|
|
|
157
|
|
|
class BSP(object): |
|
158
|
|
|
""" |
|
159
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
Attributes: |
|
162
|
|
|
x (int): Rectangle left coordinate. |
|
163
|
|
|
y (int): Rectangle top coordinate. |
|
164
|
|
|
width (int): Rectangle width. |
|
165
|
|
|
height (int): Rectangle height. |
|
166
|
|
|
level (int): This nodes depth. |
|
167
|
|
|
position (int): The integer of where the node was split. |
|
168
|
|
|
horizontal (bool): This nodes split orientation. |
|
169
|
|
|
parent (Optional[BSP]): This nodes parent or None |
|
170
|
|
|
children (Optional[Tuple[BSP, BSP]]): |
|
171
|
|
|
A tuple of (left, right) BSP instances, or |
|
172
|
|
|
None if this BSP has no children. |
|
173
|
|
|
|
|
174
|
|
|
Args: |
|
175
|
|
|
x (int): Rectangle left coordinate. |
|
176
|
|
|
y (int): Rectangle top coordinate. |
|
177
|
|
|
width (int): Rectangle width. |
|
178
|
|
|
height (int): Rectangle height. |
|
179
|
|
|
|
|
180
|
|
|
.. versionchanged:: 2.0 |
|
181
|
|
|
You can create BSP's with this class contructor instead of using |
|
182
|
|
|
:any:`bsp_new_with_size`. |
|
183
|
|
|
""" |
|
184
|
|
|
|
|
185
|
|
|
def __init__(self, x, y, width, height): |
|
186
|
|
|
self.x = x |
|
187
|
|
|
self.y = y |
|
188
|
|
|
self.width = width |
|
189
|
|
|
self.height = height |
|
190
|
|
|
|
|
191
|
|
|
self.level = 0 |
|
192
|
|
|
self.position = 0 |
|
193
|
|
|
self.horizontal = False |
|
194
|
|
|
|
|
195
|
|
|
self.parent = None |
|
196
|
|
|
self.children = () |
|
197
|
|
|
|
|
198
|
|
|
@property |
|
199
|
|
|
def w(self): |
|
|
|
|
|
|
200
|
|
|
return self.width |
|
201
|
|
|
@w.setter |
|
202
|
|
|
def w(self, value): |
|
|
|
|
|
|
203
|
|
|
self.width = value |
|
204
|
|
|
|
|
205
|
|
|
@property |
|
206
|
|
|
def h(self): |
|
|
|
|
|
|
207
|
|
|
return self.height |
|
208
|
|
|
@h.setter |
|
209
|
|
|
def h(self, value): |
|
|
|
|
|
|
210
|
|
|
self.height = value |
|
211
|
|
|
|
|
212
|
|
|
def _as_cdata(self): |
|
|
|
|
|
|
213
|
|
|
cdata = ffi.gc(lib.TCOD_bsp_new_with_size(self.x, self.y, |
|
|
|
|
|
|
214
|
|
|
self.width, self.height), |
|
215
|
|
|
lib.TCOD_bsp_delete) |
|
|
|
|
|
|
216
|
|
|
cdata.level = self.level |
|
217
|
|
|
return cdata |
|
218
|
|
|
|
|
219
|
|
|
def __str__(self): |
|
220
|
|
|
"""Provide a useful readout when printed.""" |
|
221
|
|
|
status = 'leaf' |
|
222
|
|
|
if self.children: |
|
223
|
|
|
status = ('split at position=%i,horizontal=%r' % |
|
224
|
|
|
(self.position, self.horizontal)) |
|
225
|
|
|
|
|
226
|
|
|
return ('<%s(x=%i,y=%i,width=%i,height=%i)level=%i,%s>' % |
|
227
|
|
|
(self.__class__.__name__, |
|
228
|
|
|
self.x, self.y, self.width, self.height, self.level, status)) |
|
229
|
|
|
|
|
230
|
|
|
def _unpack_bsp_tree(self, cdata): |
|
|
|
|
|
|
231
|
|
|
self.x = cdata.x |
|
232
|
|
|
self.y = cdata.y |
|
233
|
|
|
self.width = cdata.w |
|
234
|
|
|
self.height = cdata.h |
|
235
|
|
|
self.level = cdata.level |
|
236
|
|
|
self.position = cdata.position |
|
237
|
|
|
self.horizontal = bool(cdata.horizontal) |
|
238
|
|
|
if lib.TCOD_bsp_is_leaf(cdata): |
|
|
|
|
|
|
239
|
|
|
return |
|
240
|
|
|
self.children = (BSP(0, 0, 0, 0), BSP(0, 0, 0, 0)) |
|
241
|
|
|
self.children[0].parent = self |
|
242
|
|
|
self.children[0]._unpack_bsp_tree(lib.TCOD_bsp_left(cdata)) |
|
|
|
|
|
|
243
|
|
|
self.children[1].parent = self |
|
244
|
|
|
self.children[1]._unpack_bsp_tree(lib.TCOD_bsp_right(cdata)) |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
def split_once(self, horizontal, position): |
|
247
|
|
|
""" |
|
248
|
|
|
|
|
249
|
|
|
.. versionadded:: 2.0 |
|
250
|
|
|
""" |
|
251
|
|
|
cdata = self._as_cdata() |
|
252
|
|
|
lib.TCOD_bsp_split_once(cdata, horizontal, position) |
|
|
|
|
|
|
253
|
|
|
self._unpack_bsp_tree(cdata) |
|
254
|
|
|
|
|
255
|
|
|
def split_recursive(self, depth, min_width, min_height, |
|
256
|
|
|
max_horizontal_ratio, max_vertical_raito, random=None): |
|
257
|
|
|
""" |
|
258
|
|
|
|
|
259
|
|
|
.. versionadded:: 2.0 |
|
260
|
|
|
""" |
|
261
|
|
|
cdata = self._as_cdata() |
|
262
|
|
|
lib.TCOD_bsp_split_recursive(cdata, random or ffi.NULL, |
|
|
|
|
|
|
263
|
|
|
depth, min_width, min_height, |
|
264
|
|
|
max_horizontal_ratio, max_vertical_raito) |
|
265
|
|
|
self._unpack_bsp_tree(cdata) |
|
266
|
|
|
|
|
267
|
|
|
def walk(self): |
|
268
|
|
|
"""Iterate over this BSP's hieracrhy. |
|
269
|
|
|
|
|
270
|
|
|
The iterator will include the instance which called it. |
|
271
|
|
|
It will traverse its own children and grandchildren, in no particular |
|
272
|
|
|
order. |
|
273
|
|
|
|
|
274
|
|
|
Returns: |
|
275
|
|
|
Iterator[BSP]: An iterator of BSP nodes. |
|
276
|
|
|
|
|
277
|
|
|
.. versionadded:: 2.0 |
|
278
|
|
|
""" |
|
279
|
|
|
return self._iter_post_order() |
|
280
|
|
|
|
|
281
|
|
|
def _iter_pre_order(self): |
|
|
|
|
|
|
282
|
|
|
yield self |
|
283
|
|
|
for child in self.children: |
|
284
|
|
|
for grandchild in child._iter_pre_order(): |
|
285
|
|
|
yield grandchild |
|
286
|
|
|
|
|
287
|
|
|
def _iter_in_order(self): |
|
|
|
|
|
|
288
|
|
|
if self.children: |
|
289
|
|
|
for grandchild in self.children[0]._iter_in_order(): |
|
290
|
|
|
yield grandchild |
|
291
|
|
|
yield self |
|
292
|
|
|
for grandchild in self.children[1]._iter_in_order(): |
|
293
|
|
|
yield grandchild |
|
294
|
|
|
else: |
|
295
|
|
|
yield self |
|
296
|
|
|
|
|
297
|
|
|
def _iter_post_order(self): |
|
|
|
|
|
|
298
|
|
|
for child in self.children: |
|
299
|
|
|
for grandchild in child._iter_post_order(): |
|
300
|
|
|
yield grandchild |
|
301
|
|
|
yield self |
|
302
|
|
|
|
|
303
|
|
|
def _iter_level_order(self): |
|
|
|
|
|
|
304
|
|
|
return sorted(self._iter_pre_order(), key=lambda n:n.level) |
|
|
|
|
|
|
305
|
|
|
|
|
306
|
|
|
def _iter_inverted_level_order(self): |
|
|
|
|
|
|
307
|
|
|
return reversed(self._iter_level_order()) |
|
308
|
|
|
|
|
309
|
|
|
def contains(self, x, y): |
|
310
|
|
|
"""Returns True if this node contains these coordinates. |
|
311
|
|
|
|
|
312
|
|
|
Args: |
|
313
|
|
|
x (int): X position to check. |
|
314
|
|
|
y (int): Y position to check. |
|
315
|
|
|
|
|
316
|
|
|
Returns: |
|
317
|
|
|
bool: True if this node contains these coordinates. |
|
318
|
|
|
Otherwise False. |
|
319
|
|
|
|
|
320
|
|
|
.. versionadded:: 2.0 |
|
321
|
|
|
""" |
|
322
|
|
|
return (self.x <= x < self.x + self.width and |
|
323
|
|
|
self.y <= y < self.y + self.height) |
|
324
|
|
|
|
|
325
|
|
|
def find_node(self, x, y): |
|
326
|
|
|
"""Return the deepest node which contains these coordinates. |
|
327
|
|
|
|
|
328
|
|
|
Returns: |
|
329
|
|
|
Optional[BSP]: BSP object or None. |
|
330
|
|
|
|
|
331
|
|
|
.. versionadded:: 2.0 |
|
332
|
|
|
""" |
|
333
|
|
|
if not self.contains(x, y): |
|
334
|
|
|
return None |
|
335
|
|
|
for child in self.children: |
|
336
|
|
|
found = child.find_node(x, y) |
|
337
|
|
|
if found: |
|
338
|
|
|
return found |
|
339
|
|
|
return self |
|
340
|
|
|
|
|
341
|
|
|
class Key(_CDataWrapper): |
|
342
|
|
|
"""Key Event instance |
|
343
|
|
|
|
|
344
|
|
|
Attributes: |
|
345
|
|
|
vk (int): TCOD_keycode_t key code |
|
346
|
|
|
c (int): character if vk == TCODK_CHAR else 0 |
|
347
|
|
|
text (Text): text[TCOD_KEY_TEXT_SIZE]; text if vk == TCODK_TEXT else text[0] == '\0' |
|
|
|
|
|
|
348
|
|
|
pressed (bool): does this correspond to a key press or key release event ? |
|
|
|
|
|
|
349
|
|
|
lalt (bool): True when left alt is held. |
|
350
|
|
|
lctrl (bool): True when left control is held. |
|
351
|
|
|
lmeta (bool): True when left meta key is held. |
|
352
|
|
|
ralt (bool): True when right alt is held. |
|
353
|
|
|
rctrl (bool): True when right control is held. |
|
354
|
|
|
rmeta (bool): True when right meta key is held. |
|
355
|
|
|
shift (bool): True when any shift is held. |
|
356
|
|
|
""" |
|
357
|
|
|
|
|
358
|
|
|
_BOOL_ATTRIBUTES = ('lalt', 'lctrl', 'lmeta', |
|
359
|
|
|
'ralt', 'rctrl', 'rmeta', 'pressed', 'shift') |
|
360
|
|
|
|
|
361
|
|
|
def __init__(self, *args, **kargs): |
|
362
|
|
|
super(Key, self).__init__(*args, **kargs) |
|
363
|
|
|
if self.cdata == ffi.NULL: |
|
|
|
|
|
|
364
|
|
|
self.cdata = ffi.new('TCOD_key_t*') |
|
|
|
|
|
|
365
|
|
|
|
|
366
|
|
|
def __getattr__(self, attr): |
|
367
|
|
|
if attr in self._BOOL_ATTRIBUTES: |
|
368
|
|
|
return bool(getattr(self.cdata, attr)) |
|
369
|
|
|
if attr == 'c': |
|
370
|
|
|
return ord(getattr(self.cdata, attr)) |
|
371
|
|
|
if attr == 'text': |
|
372
|
|
|
return _unpack_char_p(getattr(self.cdata, attr)) |
|
373
|
|
|
return super(Key, self).__getattr__(attr) |
|
374
|
|
|
|
|
375
|
|
|
class Mouse(_CDataWrapper): |
|
376
|
|
|
"""Mouse event instance |
|
377
|
|
|
|
|
378
|
|
|
Attributes: |
|
379
|
|
|
x (int): Absolute mouse position at pixel x. |
|
380
|
|
|
y (int): |
|
381
|
|
|
dx (int): Movement since last update in pixels. |
|
382
|
|
|
dy (int): |
|
383
|
|
|
cx (int): Cell coordinates in the root console. |
|
384
|
|
|
cy (int): |
|
385
|
|
|
dcx (int): Movement since last update in console cells. |
|
386
|
|
|
dcy (int): |
|
387
|
|
|
lbutton (bool): Left button status. |
|
388
|
|
|
rbutton (bool): Right button status. |
|
389
|
|
|
mbutton (bool): Middle button status. |
|
390
|
|
|
lbutton_pressed (bool): Left button pressed event. |
|
391
|
|
|
rbutton_pressed (bool): Right button pressed event. |
|
392
|
|
|
mbutton_pressed (bool): Middle button pressed event. |
|
393
|
|
|
wheel_up (bool): Wheel up event. |
|
394
|
|
|
wheel_down (bool): Wheel down event. |
|
395
|
|
|
""" |
|
396
|
|
|
|
|
397
|
|
|
def __init__(self, *args, **kargs): |
|
398
|
|
|
super(Mouse, self).__init__(*args, **kargs) |
|
399
|
|
|
if self.cdata == ffi.NULL: |
|
|
|
|
|
|
400
|
|
|
self.cdata = ffi.new('TCOD_mouse_t*') |
|
|
|
|
|
|
401
|
|
|
|
|
402
|
|
|
|
|
403
|
|
|
class Console(_CDataWrapper): |
|
404
|
|
|
""" |
|
405
|
|
|
Args: |
|
406
|
|
|
width (int): Width of the new Console. |
|
407
|
|
|
height (int): Height of the new Console. |
|
408
|
|
|
|
|
409
|
|
|
.. versionadded:: 2.0 |
|
410
|
|
|
""" |
|
411
|
|
|
|
|
412
|
|
|
def __init__(self, *args, **kargs): |
|
|
|
|
|
|
413
|
|
|
self.cdata = self._get_cdata_from_args(*args, **kargs) |
|
414
|
|
|
if self.cdata is None: |
|
415
|
|
|
self._init(*args, **kargs) |
|
416
|
|
|
|
|
417
|
|
|
def _init(self, width, height): |
|
|
|
|
|
|
418
|
|
|
self.cdata = ffi.gc(lib.TCOD_console_new(width, height), |
|
|
|
|
|
|
419
|
|
|
lib.TCOD_console_delete) |
|
|
|
|
|
|
420
|
|
|
|
|
421
|
|
|
def get_width(self): |
|
422
|
|
|
"""Return the width of this console. |
|
423
|
|
|
|
|
424
|
|
|
Returns: |
|
425
|
|
|
int: The width of a Console. |
|
426
|
|
|
""" |
|
427
|
|
|
return lib.TCOD_console_get_width(self.cdata) |
|
|
|
|
|
|
428
|
|
|
|
|
429
|
|
|
def get_height(self): |
|
430
|
|
|
"""Return the height of this console. |
|
431
|
|
|
|
|
432
|
|
|
Returns: |
|
433
|
|
|
int: The height of a Console. |
|
434
|
|
|
""" |
|
435
|
|
|
return lib.TCOD_console_get_height(self.cdata) |
|
|
|
|
|
|
436
|
|
|
|
|
437
|
|
|
def set_default_bg(self, color): |
|
438
|
|
|
"""Change the default backround color for this console. |
|
439
|
|
|
|
|
440
|
|
|
Args: |
|
441
|
|
|
color (Union[Tuple[int, int, int], Sequence[int]]): |
|
442
|
|
|
An (r, g, b) sequence or Color instance. |
|
443
|
|
|
""" |
|
444
|
|
|
lib.TCOD_console_set_default_background(self.cdata, color) |
|
|
|
|
|
|
445
|
|
|
|
|
446
|
|
|
def set_default_fg(self, color): |
|
447
|
|
|
"""Change the default foreground color for this console. |
|
448
|
|
|
|
|
449
|
|
|
Args: |
|
450
|
|
|
color (Union[Tuple[int, int, int], Sequence[int]]): |
|
451
|
|
|
An (r, g, b) sequence or Color instance. |
|
452
|
|
|
""" |
|
453
|
|
|
lib.TCOD_console_set_default_foreground(self.cdata, color) |
|
|
|
|
|
|
454
|
|
|
|
|
455
|
|
|
def clear(self): |
|
456
|
|
|
"""Reset this console to its default colors and the space character. |
|
457
|
|
|
""" |
|
458
|
|
|
lib.TCOD_console_clear(self.cdata) |
|
|
|
|
|
|
459
|
|
|
|
|
460
|
|
|
def put_char(self, x, y, ch, flag=BKGND_DEFAULT): |
|
461
|
|
|
"""Draw the character c at x,y using the default colors and a blend mode. |
|
|
|
|
|
|
462
|
|
|
|
|
463
|
|
|
Args: |
|
464
|
|
|
x (int): Character x position from the left. |
|
465
|
|
|
y (int): Character y position from the top. |
|
466
|
|
|
c (Union[int, AnyStr]): Character to draw, can be an integer or string. |
|
|
|
|
|
|
467
|
|
|
flag (int): Blending mode to use, defaults to BKGND_DEFAULT. |
|
468
|
|
|
""" |
|
469
|
|
|
lib.TCOD_console_put_char(self.cdata, x, y, _int(ch), flag) |
|
|
|
|
|
|
470
|
|
|
|
|
471
|
|
|
def put_char_ex(self, x, y, ch, fore, back): |
|
472
|
|
|
"""Draw the character c at x,y using the colors fore and back. |
|
473
|
|
|
|
|
474
|
|
|
Args: |
|
475
|
|
|
x (int): Character x position from the left. |
|
476
|
|
|
y (int): Character y position from the top. |
|
477
|
|
|
c (Union[int, AnyStr]): Character to draw, can be an integer or string. |
|
|
|
|
|
|
478
|
|
|
fore (Union[Tuple[int, int, int], Sequence[int]]): |
|
479
|
|
|
An (r, g, b) sequence or Color instance. |
|
480
|
|
|
back (Union[Tuple[int, int, int], Sequence[int]]): |
|
481
|
|
|
An (r, g, b) sequence or Color instance. |
|
482
|
|
|
""" |
|
483
|
|
|
lib.TCOD_console_put_char_ex(self.cdata, x, y, |
|
|
|
|
|
|
484
|
|
|
_int(ch), fore, back) |
|
485
|
|
|
|
|
486
|
|
|
def set_char_bg(self, x, y, col, flag=BKGND_SET): |
|
487
|
|
|
"""Change the background color of x,y to col using a blend mode. |
|
488
|
|
|
|
|
489
|
|
|
Args: |
|
490
|
|
|
x (int): Character x position from the left. |
|
491
|
|
|
y (int): Character y position from the top. |
|
492
|
|
|
col (Union[Tuple[int, int, int], Sequence[int]]): |
|
493
|
|
|
An (r, g, b) sequence or Color instance. |
|
494
|
|
|
flag (int): Blending mode to use, defaults to BKGND_SET. |
|
495
|
|
|
""" |
|
496
|
|
|
lib.TCOD_console_set_char_background(self.cdata, x, y, col, flag) |
|
|
|
|
|
|
497
|
|
|
|
|
498
|
|
|
def set_char_fg(self, x, y, color): |
|
|
|
|
|
|
499
|
|
|
"""Change the foreground color of x,y to col. |
|
500
|
|
|
|
|
501
|
|
|
Args: |
|
502
|
|
|
x (int): Character x position from the left. |
|
503
|
|
|
y (int): Character y position from the top. |
|
504
|
|
|
color (Union[Tuple[int, int, int], Sequence[int]]): |
|
505
|
|
|
An (r, g, b) sequence or Color instance. |
|
506
|
|
|
""" |
|
507
|
|
|
lib.TCOD_console_set_char_foreground(self.cdata, x, y, col) |
|
|
|
|
|
|
508
|
|
|
|
|
509
|
|
|
def set_char(self, x, y, ch): |
|
510
|
|
|
"""Change the character at x,y to c, keeping the current colors. |
|
511
|
|
|
|
|
512
|
|
|
Args: |
|
513
|
|
|
x (int): Character x position from the left. |
|
514
|
|
|
y (int): Character y position from the top. |
|
515
|
|
|
c (Union[int, AnyStr]): Character to draw, can be an integer or string. |
|
|
|
|
|
|
516
|
|
|
""" |
|
517
|
|
|
lib.TCOD_console_set_char(self.cdata, x, y, _int(ch)) |
|
|
|
|
|
|
518
|
|
|
|
|
519
|
|
|
def set_default_bg_blend(self, flag): |
|
520
|
|
|
"""Change the default blend mode for this console. |
|
521
|
|
|
|
|
522
|
|
|
Args: |
|
523
|
|
|
flag (int): Blend mode to use by default. |
|
524
|
|
|
""" |
|
525
|
|
|
lib.TCOD_console_set_background_flag(self.cdata, flag) |
|
|
|
|
|
|
526
|
|
|
|
|
527
|
|
|
def get_default_bg_blend(self): |
|
528
|
|
|
"""Return this consoles current blend mode. |
|
529
|
|
|
""" |
|
530
|
|
|
return lib.TCOD_console_get_background_flag(self.cdata) |
|
|
|
|
|
|
531
|
|
|
|
|
532
|
|
|
def set_alignment(self, alignment): |
|
533
|
|
|
"""Change this consoles current alignment mode. |
|
534
|
|
|
|
|
535
|
|
|
* tcod.LEFT |
|
536
|
|
|
* tcod.CENTER |
|
537
|
|
|
* tcod.RIGHT |
|
538
|
|
|
|
|
539
|
|
|
Args: |
|
540
|
|
|
alignment (int): |
|
541
|
|
|
""" |
|
542
|
|
|
lib.TCOD_console_set_alignment(self.cdata, alignment) |
|
|
|
|
|
|
543
|
|
|
|
|
544
|
|
|
def get_alignment(self): |
|
545
|
|
|
"""Return this consoles current alignment mode. |
|
546
|
|
|
""" |
|
547
|
|
|
return lib.TCOD_console_get_alignment(self.cdata) |
|
|
|
|
|
|
548
|
|
|
|
|
549
|
|
|
def print_str(self, x, y, fmt): |
|
550
|
|
|
"""Print a color formatted string on a console. |
|
551
|
|
|
|
|
552
|
|
|
Args: |
|
553
|
|
|
x (int): Character x position from the left. |
|
554
|
|
|
y (int): Character y position from the top. |
|
555
|
|
|
fmt (AnyStr): A unicode or bytes string optionaly using color codes. |
|
|
|
|
|
|
556
|
|
|
""" |
|
557
|
|
|
lib.TCOD_console_print_utf(self.cdata, x, y, _fmt_unicode(fmt)) |
|
|
|
|
|
|
558
|
|
|
|
|
559
|
|
|
def print_ex(self, x, y, flag, alignment, fmt): |
|
560
|
|
|
"""Print a string on a console using a blend mode and alignment mode. |
|
561
|
|
|
|
|
562
|
|
|
Args: |
|
563
|
|
|
x (int): Character x position from the left. |
|
564
|
|
|
y (int): Character y position from the top. |
|
565
|
|
|
""" |
|
566
|
|
|
lib.TCOD_console_print_ex_utf(self.cdata, x, y, |
|
|
|
|
|
|
567
|
|
|
flag, alignment, _fmt_unicode(fmt)) |
|
568
|
|
|
|
|
569
|
|
|
def print_rect(self, x, y, w, h, fmt): |
|
|
|
|
|
|
570
|
|
|
"""Print a string constrained to a rectangle. |
|
571
|
|
|
|
|
572
|
|
|
If h > 0 and the bottom of the rectangle is reached, |
|
573
|
|
|
the string is truncated. If h = 0, |
|
574
|
|
|
the string is only truncated if it reaches the bottom of the console. |
|
575
|
|
|
|
|
576
|
|
|
|
|
577
|
|
|
|
|
578
|
|
|
Returns: |
|
579
|
|
|
int: The number of lines of text once word-wrapped. |
|
580
|
|
|
""" |
|
581
|
|
|
return lib.TCOD_console_print_rect_utf( |
|
|
|
|
|
|
582
|
|
|
self.cdata, x, y, width, height, _fmt_unicode(fmt)) |
|
|
|
|
|
|
583
|
|
|
|
|
584
|
|
|
def print_rect_ex(self, x, y, w, h, flag, alignment, fmt): |
|
|
|
|
|
|
585
|
|
|
"""Print a string constrained to a rectangle with blend and alignment. |
|
586
|
|
|
|
|
587
|
|
|
Returns: |
|
588
|
|
|
int: The number of lines of text once word-wrapped. |
|
589
|
|
|
""" |
|
590
|
|
|
return lib.TCOD_console_print_rect_ex_utf(self.cdata, |
|
|
|
|
|
|
591
|
|
|
x, y, width, height, flag, alignment, _fmt_unicode(fmt)) |
|
|
|
|
|
|
592
|
|
|
|
|
593
|
|
|
def get_height_rect(self, x, y, width, height, fmt): |
|
594
|
|
|
"""Return the height of this text once word-wrapped into this rectangle. |
|
|
|
|
|
|
595
|
|
|
|
|
596
|
|
|
Returns: |
|
597
|
|
|
int: The number of lines of text once word-wrapped. |
|
598
|
|
|
""" |
|
599
|
|
|
return lib.TCOD_console_get_height_rect_utf( |
|
|
|
|
|
|
600
|
|
|
self.cdata, x, y, width, height, _fmt_unicode(fmt)) |
|
601
|
|
|
|
|
602
|
|
|
def rect(self, x, y, w, h, clr, flag=BKGND_DEFAULT): |
|
603
|
|
|
"""Draw a the background color on a rect optionally clearing the text. |
|
604
|
|
|
|
|
605
|
|
|
If clr is True the affected tiles are changed to space character. |
|
606
|
|
|
""" |
|
607
|
|
|
lib.TCOD_console_rect(self.cdata, x, y, w, h, clr, flag) |
|
|
|
|
|
|
608
|
|
|
|
|
609
|
|
|
def hline(self, x, y, width, flag=BKGND_DEFAULT): |
|
610
|
|
|
"""Draw a horizontal line on the console. |
|
611
|
|
|
|
|
612
|
|
|
This always uses the character 196, the horizontal line character. |
|
613
|
|
|
""" |
|
614
|
|
|
lib.TCOD_console_hline(self.cdata, x, y, width, flag) |
|
|
|
|
|
|
615
|
|
|
|
|
616
|
|
|
def vline(self, x, y, height, flag=BKGND_DEFAULT): |
|
617
|
|
|
"""Draw a vertical line on the console. |
|
618
|
|
|
|
|
619
|
|
|
This always uses the character 179, the vertical line character. |
|
620
|
|
|
""" |
|
621
|
|
|
lib.TCOD_console_vline(self.cdata, x, y, height, flag) |
|
|
|
|
|
|
622
|
|
|
|
|
623
|
|
|
def print_frame(self, x, y, w, h, clear=True, flag=BKGND_DEFAULT, fmt=b''): |
|
624
|
|
|
"""Draw a framed rectangle with optinal text. |
|
625
|
|
|
|
|
626
|
|
|
This uses the default background color and blend mode to fill the |
|
627
|
|
|
rectangle and the default foreground to draw the outline. |
|
628
|
|
|
|
|
629
|
|
|
fmt will be printed on the inside of the rectangle, word-wrapped. |
|
630
|
|
|
""" |
|
631
|
|
|
lib.TCOD_console_print_frame(self.cdata, x, y, w, h, clear, flag, |
|
|
|
|
|
|
632
|
|
|
_fmt_bytes(fmt)) |
|
633
|
|
|
|
|
634
|
|
|
def get_default_bg(self): |
|
635
|
|
|
"""Return this consoles default background color.""" |
|
636
|
|
|
return Color._new_from_cdata( |
|
|
|
|
|
|
637
|
|
|
lib.TCOD_console_get_default_background(self.cdata)) |
|
|
|
|
|
|
638
|
|
|
|
|
639
|
|
|
def get_default_fg(self): |
|
640
|
|
|
"""Return this consoles default foreground color.""" |
|
641
|
|
|
return Color._new_from_cdata( |
|
|
|
|
|
|
642
|
|
|
lib.TCOD_console_get_default_foreground(self.cdata)) |
|
|
|
|
|
|
643
|
|
|
|
|
644
|
|
|
def get_char_bg(self, x, y): |
|
645
|
|
|
"""Return the background color at the x,y of this console.""" |
|
646
|
|
|
return Color._new_from_cdata( |
|
|
|
|
|
|
647
|
|
|
lib.TCOD_console_get_char_background(self.cdata, x, y)) |
|
|
|
|
|
|
648
|
|
|
|
|
649
|
|
|
def get_char_fg(self, x, y): |
|
650
|
|
|
"""Return the foreground color at the x,y of this console.""" |
|
651
|
|
|
return Color._new_from_cdata( |
|
|
|
|
|
|
652
|
|
|
lib.TCOD_console_get_char_foreground(self.cdata, x, y)) |
|
|
|
|
|
|
653
|
|
|
|
|
654
|
|
|
def get_char(self, x, y): |
|
655
|
|
|
"""Return the character at the x,y of this console.""" |
|
656
|
|
|
return lib.TCOD_console_get_char(self.cdata, x, y) |
|
|
|
|
|
|
657
|
|
|
|
|
658
|
|
|
def blit(self, x, y, w, h, |
|
659
|
|
|
dest, dest_x, dest_y, fg_alpha=1, bg_alpha=1): |
|
|
|
|
|
|
660
|
|
|
"""Blit this console from x,y,w,h to the console dst at xdst,ydst.""" |
|
661
|
|
|
lib.TCOD_console_blit(self.cdata, x, y, w, h, |
|
|
|
|
|
|
662
|
|
|
_cdata(dst), dest_x, dest_y, fg_alpha, bg_alpha) |
|
|
|
|
|
|
663
|
|
|
|
|
664
|
|
|
def set_key_color(self, color): |
|
665
|
|
|
"""Set a consoles blit transparent color.""" |
|
666
|
|
|
lib.TCOD_console_set_key_color(self.cdata, color) |
|
|
|
|
|
|
667
|
|
|
|
|
668
|
|
|
def fill(self, ch=None, fg=None, bg=None): |
|
669
|
|
|
"""Fill this console with the given numpy array values. |
|
670
|
|
|
|
|
671
|
|
|
Args: |
|
672
|
|
|
ch (Optional[:any:`numpy.ndarray`]): |
|
673
|
|
|
A numpy integer array with a shape of (width, height) |
|
674
|
|
|
fg (Optional[:any:`numpy.ndarray`]): |
|
675
|
|
|
A numpy integer array with a shape of (width, height, 3) |
|
676
|
|
|
bg (Optional[:any:`numpy.ndarray`]): |
|
677
|
|
|
A numpy integer array with a shape of (width, height, 3) |
|
678
|
|
|
""" |
|
679
|
|
|
import numpy |
|
|
|
|
|
|
680
|
|
|
if ch: |
|
681
|
|
|
ch = numpy.ascontiguousarray(ch, dtype=numpy.intc) |
|
682
|
|
|
ch_array = ffi.cast('int *', ch.ctypes.data) |
|
|
|
|
|
|
683
|
|
|
lib.TCOD_console_fill_char(self.cdata, ch_array) |
|
|
|
|
|
|
684
|
|
|
if fg: |
|
685
|
|
|
r = numpy.ascontiguousarray(fg[:,:,0], dtype=numpy.intc) |
|
|
|
|
|
|
686
|
|
|
g = numpy.ascontiguousarray(fg[:,:,1], dtype=numpy.intc) |
|
|
|
|
|
|
687
|
|
|
b = numpy.ascontiguousarray(fg[:,:,2], dtype=numpy.intc) |
|
|
|
|
|
|
688
|
|
|
cr = ffi.cast('int *', r.ctypes.data) |
|
|
|
|
|
|
689
|
|
|
cg = ffi.cast('int *', g.ctypes.data) |
|
|
|
|
|
|
690
|
|
|
cb = ffi.cast('int *', b.ctypes.data) |
|
|
|
|
|
|
691
|
|
|
lib.TCOD_console_fill_foreground(self.cdata, cr, cg, cb) |
|
|
|
|
|
|
692
|
|
|
if bg: |
|
693
|
|
|
r = numpy.ascontiguousarray(bg[:,:,0], dtype=numpy.intc) |
|
|
|
|
|
|
694
|
|
|
g = numpy.ascontiguousarray(bg[:,:,1], dtype=numpy.intc) |
|
|
|
|
|
|
695
|
|
|
b = numpy.ascontiguousarray(bg[:,:,2], dtype=numpy.intc) |
|
|
|
|
|
|
696
|
|
|
cr = ffi.cast('int *', r.ctypes.data) |
|
|
|
|
|
|
697
|
|
|
cg = ffi.cast('int *', g.ctypes.data) |
|
|
|
|
|
|
698
|
|
|
cb = ffi.cast('int *', b.ctypes.data) |
|
|
|
|
|
|
699
|
|
|
lib.TCOD_console_fill_background(self.cdata, cr, cg, cb) |
|
|
|
|
|
|
700
|
|
|
|
|
701
|
|
|
|
|
702
|
|
|
class Image(_CDataWrapper): |
|
703
|
|
|
""" |
|
704
|
|
|
.. versionadded:: 2.0 |
|
705
|
|
|
|
|
706
|
|
|
Args: |
|
707
|
|
|
width (int): Width of the new Image. |
|
708
|
|
|
height (int): Height of the new Image. |
|
709
|
|
|
|
|
710
|
|
|
Attributes: |
|
711
|
|
|
width (int): Read only width of this Image. |
|
712
|
|
|
height (int): Read only height of this Image. |
|
713
|
|
|
""" |
|
714
|
|
|
def __init__(self, *args, **kargs): |
|
715
|
|
|
super(Image, self).__init__(*args, **kargs) |
|
716
|
|
|
if not self.cdata: |
|
717
|
|
|
self._init(*args, **kargs) |
|
718
|
|
|
self.width, self.height = self._get_size() |
|
719
|
|
|
|
|
720
|
|
|
def _init(self, width, height): |
|
|
|
|
|
|
721
|
|
|
self.cdata = ffi.gc(lib.TCOD_image_new(width, height), |
|
|
|
|
|
|
722
|
|
|
lib.TCOD_image_delete) |
|
|
|
|
|
|
723
|
|
|
|
|
724
|
|
|
def clear(self, color): |
|
725
|
|
|
"""Fill this entire Image with color. |
|
726
|
|
|
|
|
727
|
|
|
Args: |
|
728
|
|
|
color (Union[Tuple[int, int, int], Sequence[int]]): |
|
729
|
|
|
An (r, g, b) sequence or Color instance. |
|
730
|
|
|
""" |
|
731
|
|
|
lib.TCOD_image_clear(self.cdata, color) |
|
|
|
|
|
|
732
|
|
|
|
|
733
|
|
|
def invert(self): |
|
734
|
|
|
"""Invert all colors in this Image.""" |
|
735
|
|
|
lib.TCOD_image_invert(self.cdata) |
|
|
|
|
|
|
736
|
|
|
|
|
737
|
|
|
def hflip(self): |
|
738
|
|
|
"""Horizontally flip this Image.""" |
|
739
|
|
|
lib.TCOD_image_hflip(self.cdata) |
|
|
|
|
|
|
740
|
|
|
|
|
741
|
|
|
def rotate90(self, rotations=1): |
|
742
|
|
|
"""Rotate this Image clockwise in 90 degree steps. |
|
743
|
|
|
|
|
744
|
|
|
Args: |
|
745
|
|
|
rotations (int): Number of 90 degree clockwise rotations. |
|
746
|
|
|
""" |
|
747
|
|
|
lib.TCOD_image_rotate90(self.cdata, rotations) |
|
|
|
|
|
|
748
|
|
|
|
|
749
|
|
|
def vflip(self): |
|
750
|
|
|
"""Vertically flip this Image.""" |
|
751
|
|
|
lib.TCOD_image_vflip(self.cdata) |
|
|
|
|
|
|
752
|
|
|
|
|
753
|
|
|
def scale(self, width, height): |
|
754
|
|
|
"""Scale this Image to the new width and height. |
|
755
|
|
|
|
|
756
|
|
|
Args: |
|
757
|
|
|
width (int): The new width of the Image after scaling. |
|
758
|
|
|
height (int): The new height of the Image after scaling. |
|
759
|
|
|
""" |
|
760
|
|
|
lib.TCOD_image_scale(self.cdata, width, height) |
|
|
|
|
|
|
761
|
|
|
self.width, self.height = width, height |
|
762
|
|
|
|
|
763
|
|
|
def set_key_color(self, color): |
|
764
|
|
|
"""Set a color to be transparent during blitting functions. |
|
765
|
|
|
|
|
766
|
|
|
Args: |
|
767
|
|
|
color (Union[Tuple[int, int, int], Sequence[int]]): |
|
768
|
|
|
An (r, g, b) sequence or Color instance. |
|
769
|
|
|
""" |
|
770
|
|
|
lib.TCOD_image_set_key_color(self.cdata, color) |
|
|
|
|
|
|
771
|
|
|
|
|
772
|
|
|
def get_alpha(self, x, y): |
|
773
|
|
|
"""Get the Image alpha of the pixel at x, y. |
|
774
|
|
View Code Duplication |
|
|
|
|
|
|
|
775
|
|
|
Args: |
|
776
|
|
|
x (int): X pixel of the image. Starting from the left at 0. |
|
777
|
|
|
y (int): Y pixel of the image. Starting from the top at 0. |
|
778
|
|
|
|
|
779
|
|
|
Returns: |
|
780
|
|
|
int: The alpha value of the pixel. |
|
781
|
|
|
With 0 being fully transparent and 255 being fully opaque. |
|
782
|
|
View Code Duplication |
""" |
|
|
|
|
|
|
783
|
|
|
return lib.TCOD_image_get_alpha(self.cdata, x, y) |
|
|
|
|
|
|
784
|
|
|
|
|
785
|
|
|
def refresh_console(self, console): |
|
786
|
|
|
"""Update an Image created with :any:`tcod.image_from_console`. |
|
787
|
|
|
|
|
788
|
|
|
The console used with this function should have the same width and |
|
789
|
|
|
height as the Console given to :any:`tcod.image_from_console`. |
|
790
|
|
|
The font width and height must also be the same as when |
|
791
|
|
|
:any:`tcod.image_from_console` was called. |
|
792
|
|
|
|
|
793
|
|
|
Args: |
|
794
|
|
|
console (Console): A Console with a pixel width and height |
|
795
|
|
|
matching this Image. |
|
796
|
|
|
""" |
|
797
|
|
|
lib.TCOD_image_refresh_console(self.cdata, _cdata(console)) |
|
|
|
|
|
|
798
|
|
|
|
|
799
|
|
|
def _get_size(self): |
|
800
|
|
|
"""Return the (width, height) for this Image. |
|
801
|
|
|
|
|
802
|
|
|
Returns: |
|
803
|
|
|
Tuple[int, int]: The (width, height) of this Image |
|
804
|
|
|
""" |
|
805
|
|
|
w = ffi.new('int *') |
|
|
|
|
|
|
806
|
|
|
h = ffi.new('int *') |
|
|
|
|
|
|
807
|
|
|
lib.TCOD_image_get_size(self.cdata, w, h) |
|
|
|
|
|
|
808
|
|
|
return w[0], h[0] |
|
809
|
|
|
|
|
810
|
|
|
def get_pixel(self, x, y): |
|
811
|
|
|
"""Get the color of a pixel in this Image. |
|
812
|
|
|
|
|
813
|
|
|
Args: |
|
814
|
|
|
x (int): X pixel of the Image. Starting from the left at 0. |
|
815
|
|
|
y (int): Y pixel of the Image. Starting from the top at 0. |
|
816
|
|
|
|
|
817
|
|
|
Returns: |
|
818
|
|
|
Tuple[int, int, int]: |
|
819
|
|
|
An (r, g, b) tuple containing the pixels color value. |
|
820
|
|
|
Values are in a 0 to 255 range. |
|
821
|
|
|
""" |
|
822
|
|
|
return lib.TCOD_image_get_pixel(self.cdata, x, y) |
|
|
|
|
|
|
823
|
|
|
|
|
824
|
|
|
def get_mipmap_pixel(self, left, top, right, bottom): |
|
825
|
|
|
"""Get the average color of a rectangle in this Image. |
|
826
|
|
|
|
|
827
|
|
|
Parameters should stay within the following limits: |
|
828
|
|
|
* 0 <= left < right < Image.width |
|
829
|
|
|
* 0 <= top < bottom < Image.height |
|
830
|
|
|
|
|
831
|
|
|
Args: |
|
832
|
|
|
left (int): Left corner of the region. |
|
833
|
|
|
top (int): Top corner of the region. |
|
834
|
|
|
right (int): Right corner of the region. |
|
835
|
|
|
bottom (int): Bottom corner of the region. |
|
836
|
|
|
|
|
837
|
|
|
Returns: |
|
838
|
|
|
Tuple[int, int, int]: |
|
839
|
|
|
An (r, g, b) tuple containing the averaged color value. |
|
840
|
|
|
Values are in a 0 to 255 range. |
|
841
|
|
|
""" |
|
842
|
|
|
color = lib.TCOD_image_get_mipmap_pixel(self.cdata, |
|
|
|
|
|
|
843
|
|
|
left, top, right, bottom) |
|
844
|
|
|
return (color.r, color.g, color.b) |
|
845
|
|
|
|
|
846
|
|
|
def put_pixel(self, x, y, color): |
|
847
|
|
|
"""Change a pixel on this Image. |
|
848
|
|
|
|
|
849
|
|
|
Args: |
|
850
|
|
|
x (int): X pixel of the Image. Starting from the left at 0. |
|
851
|
|
|
y (int): Y pixel of the Image. Starting from the top at 0. |
|
852
|
|
|
color (Union[Tuple[int, int, int], Sequence[int]]): |
|
853
|
|
|
An (r, g, b) sequence or Color instance. |
|
854
|
|
|
""" |
|
855
|
|
|
lib.TCOD_image_put_pixel(self.cdata, x, y, color) |
|
|
|
|
|
|
856
|
|
|
|
|
857
|
|
|
def blit(self, console, x, y, bg_blend, scale_x, scale_y, angle): |
|
858
|
|
|
"""Blit onto a Console using scaling and rotation. |
|
859
|
|
|
|
|
860
|
|
|
Args: |
|
861
|
|
|
console (Console): Blit destination Console. |
|
862
|
|
|
x (int): Console X position for the center of the Image blit. |
|
863
|
|
|
y (int): Console Y position for the center of the Image blit. |
|
864
|
|
|
The Image blit is centered on this position. |
|
865
|
|
|
bg_blend (int): Background blending mode to use. |
|
866
|
|
|
scale_x (float): Scaling along Image x axis. |
|
867
|
|
|
Set to 1 for no scaling. Must be over 0. |
|
868
|
|
|
scale_y (float): Scaling along Image y axis. |
|
869
|
|
|
Set to 1 for no scaling. Must be over 0. |
|
870
|
|
|
angle (float): Rotation angle in radians. (Clockwise?) |
|
871
|
|
|
""" |
|
872
|
|
|
lib.TCOD_image_blit(self.cdata, _cdata(console), x, y, bg_blend, |
|
|
|
|
|
|
873
|
|
|
scale_x, scale_y, angle) |
|
874
|
|
|
|
|
875
|
|
|
def blit_rect(self, console, x, y, width, height, bg_blend): |
|
876
|
|
|
"""Blit onto a Console without scaling or rotation. |
|
877
|
|
|
|
|
878
|
|
|
Args: |
|
879
|
|
|
console (Console): Blit destination Console. |
|
880
|
|
|
x (int): Console tile X position starting from the left at 0. |
|
881
|
|
|
y (int): Console tile Y position starting from the top at 0. |
|
882
|
|
|
width (int): Use -1 for Image width. |
|
883
|
|
|
height (int): Use -1 for Image height. |
|
884
|
|
|
bg_blend (int): Background blending mode to use. |
|
885
|
|
|
""" |
|
886
|
|
|
lib.TCOD_image_blit_rect(self.cdata, _cdata(console), |
|
|
|
|
|
|
887
|
|
|
x, y, width, height, bg_blend) |
|
888
|
|
|
|
|
889
|
|
|
def blit_2x(self, console, dest_x, dest_y, |
|
890
|
|
|
img_x=0, img_y=0, img_width=-1, img_height=-1): |
|
891
|
|
|
"""Blit onto a Console with double resolution. |
|
892
|
|
|
|
|
893
|
|
|
Args: |
|
894
|
|
|
console (Console): Blit destination Console. |
|
895
|
|
|
dest_x (int): Console tile X position starting from the left at 0. |
|
896
|
|
|
dest_y (int): Console tile Y position starting from the top at 0. |
|
897
|
|
|
img_x (int): Left corner pixel of the Image to blit |
|
898
|
|
|
img_y (int): Top corner pixel of the Image to blit |
|
899
|
|
|
img_width (int): Width of the Image to blit. |
|
900
|
|
|
Use -1 for the full Image width. |
|
901
|
|
|
img_height (int): Height of the Image to blit. |
|
902
|
|
|
Use -1 for the full Image height. |
|
903
|
|
|
""" |
|
904
|
|
|
lib.TCOD_image_blit_2x(self.cdata, _cdata(console), dest_x, dest_y, |
|
|
|
|
|
|
905
|
|
|
img_x, img_y, img_width, img_height) |
|
906
|
|
|
|
|
907
|
|
|
def save_as(self, filename): |
|
908
|
|
|
"""Save the Image to a 32-bit .bmp or .png file. |
|
909
|
|
|
|
|
910
|
|
|
Args: |
|
911
|
|
|
filename (AnyStr): File path to same this Image. |
|
912
|
|
|
""" |
|
913
|
|
|
lib.TCOD_image_save(self.cdata, _bytes(filename)) |
|
|
|
|
|
|
914
|
|
|
|
|
915
|
|
|
class Random(_CDataWrapper): |
|
916
|
|
|
""" |
|
917
|
|
|
.. versionadded:: 2.0 |
|
918
|
|
|
|
|
919
|
|
|
If all you need is a random number generator then it's recommended |
|
920
|
|
|
that you use the :any:`random` module from the Python standard library. |
|
921
|
|
|
|
|
922
|
|
|
Args: |
|
923
|
|
|
seed (Hashable): The RNG seed. Should be a 32-bit integer, but any |
|
924
|
|
|
hashable object is accepted. |
|
925
|
|
|
algorithm (int): The algorithm to use. |
|
926
|
|
|
""" |
|
927
|
|
|
def __init__(self, *args, **kargs): |
|
928
|
|
|
super(Random, self).__init__(*args, **kargs) |
|
929
|
|
|
if not self.cdata: |
|
930
|
|
|
self._init(*args, **kargs) |
|
931
|
|
|
|
|
932
|
|
|
def _init(self, seed, algorithm): |
|
|
|
|
|
|
933
|
|
|
self.cdata = ffi.gc(lib.TCOD_random_new_from_seed(algorithm, |
|
|
|
|
|
|
934
|
|
|
hash(seed)), |
|
935
|
|
|
lib.TCOD_random_delete) |
|
|
|
|
|
|
936
|
|
|
|
|
937
|
|
|
|
|
938
|
|
|
def random_int(self, low, high, mean=None): |
|
939
|
|
|
"""Return a random integer from a linear or triangular range. |
|
940
|
|
|
|
|
941
|
|
|
Args: |
|
942
|
|
|
low (int): The lower bound of the random range, inclusive. |
|
943
|
|
|
high (int): The upper bound of the random range, inclusive. |
|
944
|
|
|
mean (Optional[int]): The mean return value, or None. |
|
945
|
|
|
|
|
946
|
|
|
Returns: |
|
947
|
|
|
int: A random number from the given range: low <= n <= high. |
|
948
|
|
|
""" |
|
949
|
|
|
lib.TCOD_random_set_distribution(self.cdata, |
|
|
|
|
|
|
950
|
|
|
lib.TCOD_DISTRIBUTION_LINEAR) |
|
|
|
|
|
|
951
|
|
|
if mean is None: |
|
952
|
|
|
return lib.TCOD_random_get_int(self.cdata, low, high) |
|
|
|
|
|
|
953
|
|
|
return lib.TCOD_random_get_int_mean(self.cdata, low, high, mean) |
|
|
|
|
|
|
954
|
|
|
|
|
955
|
|
|
|
|
956
|
|
|
def random_float(self, low, high, mean=None): |
|
957
|
|
|
"""Return a random float from a linear or triangular range. |
|
958
|
|
|
|
|
959
|
|
|
Args: |
|
960
|
|
|
low (float): The lower bound of the random range. |
|
961
|
|
|
high (float): The upper bound of the random range. |
|
962
|
|
|
mean (Optional[float]): The mean return value, or None. |
|
963
|
|
|
|
|
964
|
|
|
Returns: |
|
965
|
|
|
float: A random number from the given range: low <= n <= high. |
|
966
|
|
|
""" |
|
967
|
|
|
lib.TCOD_random_set_distribution(self.cdata, |
|
|
|
|
|
|
968
|
|
|
lib.TCOD_DISTRIBUTION_LINEAR) |
|
|
|
|
|
|
969
|
|
|
if mean is None: |
|
970
|
|
|
return lib.TCOD_random_get_double(self.cdata, low, high) |
|
|
|
|
|
|
971
|
|
|
return lib.TCOD_random_get_double_mean(self.cdata, low, high, mean) |
|
|
|
|
|
|
972
|
|
|
|
|
973
|
|
|
def gaussian(self, mu, sigma): |
|
974
|
|
|
"""Return a number from a random gaussian distribution. |
|
975
|
|
|
|
|
976
|
|
|
Args: |
|
977
|
|
|
mu (float): The mean returned value. |
|
978
|
|
|
sigma (float): The standard deviation. |
|
979
|
|
|
|
|
980
|
|
|
Returns: |
|
981
|
|
|
float: A random number derived from the given parameters. |
|
982
|
|
|
""" |
|
983
|
|
|
lib.TCOD_random_set_distribution(self.cdata, |
|
|
|
|
|
|
984
|
|
|
lib.TCOD_DISTRIBUTION_GAUSSIAN) |
|
|
|
|
|
|
985
|
|
|
return lib.TCOD_random_get_double(self.cdata, mu, sigma) |
|
|
|
|
|
|
986
|
|
|
|
|
987
|
|
|
def inverse_gaussian(self, mu, sigma): |
|
988
|
|
|
"""Return a number from a random inverse gaussian distribution. |
|
989
|
|
|
|
|
990
|
|
|
Args: |
|
991
|
|
|
mu (float): The mean returned value. |
|
992
|
|
|
sigma (float): The standard deviation. |
|
993
|
|
|
|
|
994
|
|
|
Returns: |
|
995
|
|
|
float: A random number derived from the given parameters. |
|
996
|
|
|
""" |
|
997
|
|
|
lib.TCOD_random_set_distribution(self.cdata, |
|
|
|
|
|
|
998
|
|
|
lib.TCOD_DISTRIBUTION_GAUSSIAN_INVERSE) |
|
|
|
|
|
|
999
|
|
|
return lib.TCOD_random_get_double(self.cdata, mu, sigma) |
|
|
|
|
|
|
1000
|
|
|
|
|
1001
|
|
|
def gaussian_range(self, low, high, mean=None): |
|
1002
|
|
|
"""Return a random gaussian number clamped to a range. |
|
1003
|
|
|
|
|
1004
|
|
|
When ``mean`` is None it will be automatically determined |
|
1005
|
|
|
from the ``low`` and ``high`` parameters. |
|
1006
|
|
|
|
|
1007
|
|
|
Args: |
|
1008
|
|
|
low (float): The lower bound of the random range. |
|
1009
|
|
|
high (float): The upper bound of the random range. |
|
1010
|
|
|
mean (Optional[float]): The mean return value, or None. |
|
1011
|
|
|
|
|
1012
|
|
|
Returns: |
|
1013
|
|
|
float: A clamped gaussian number. |
|
1014
|
|
|
""" |
|
1015
|
|
|
lib.TCOD_random_set_distribution(self.cdata, |
|
|
|
|
|
|
1016
|
|
|
lib.TCOD_DISTRIBUTION_GAUSSIAN_RANGE) |
|
|
|
|
|
|
1017
|
|
|
if mean is None: |
|
1018
|
|
|
return lib.TCOD_random_get_double(self.cdata, low, high) |
|
|
|
|
|
|
1019
|
|
|
return lib.TCOD_random_get_double_mean(self.cdata, low, high, mean) |
|
|
|
|
|
|
1020
|
|
|
|
|
1021
|
|
|
def inverse_gaussian_range(self, low, high, mean=None): |
|
1022
|
|
|
"""Return a random inverted gaussian number clamped to a range. |
|
1023
|
|
|
|
|
1024
|
|
|
When ``mean`` is None it will be automatically determined |
|
1025
|
|
|
from the ``low`` and ``high`` parameters. |
|
1026
|
|
|
|
|
1027
|
|
|
Args: |
|
1028
|
|
|
low (float): The lower bound of the random range. |
|
1029
|
|
|
high (float): The upper bound of the random range. |
|
1030
|
|
|
mean (Optional[float]): The mean return value, or None. |
|
1031
|
|
|
|
|
1032
|
|
|
Returns: |
|
1033
|
|
|
float: A clamped inverse gaussian number. |
|
1034
|
|
|
""" |
|
1035
|
|
|
lib.TCOD_random_set_distribution(self.cdata, |
|
|
|
|
|
|
1036
|
|
|
lib.TCOD_DISTRIBUTION_GAUSSIAN_RANGE_INVERSE) |
|
|
|
|
|
|
1037
|
|
|
if mean is None: |
|
1038
|
|
|
return lib.TCOD_random_get_double(self.cdata, low, high) |
|
|
|
|
|
|
1039
|
|
|
return lib.TCOD_random_get_double_mean(self.cdata, low, high, mean) |
|
|
|
|
|
|
1040
|
|
|
|
|
1041
|
|
|
# TODO: Eventually add these functions: |
|
|
|
|
|
|
1042
|
|
|
#def save(self): |
|
1043
|
|
|
# return ffi.gc(lib.TCOD_random_save(self.cdata), |
|
1044
|
|
|
# lib.TCOD_random_delete) |
|
1045
|
|
|
#def restore(self, backup): |
|
1046
|
|
|
# lib.TCOD_random_restore(self.cdata, backup) |
|
1047
|
|
|
|
|
1048
|
|
|
NOISE_IMP_SIMPLE = 0 |
|
1049
|
|
|
NOISE_IMP_FBM = 1 |
|
1050
|
|
|
NOISE_IMP_TURBULENCE = 2 |
|
1051
|
|
|
|
|
1052
|
|
|
class Noise(_CDataWrapper): |
|
1053
|
|
|
""" |
|
1054
|
|
|
.. versionadded:: 2.0 |
|
1055
|
|
|
|
|
1056
|
|
|
The ``hurst`` exponent describes the raggedness of the resultant noise, |
|
1057
|
|
|
with a higher value leading to a smoother noise. |
|
1058
|
|
|
Not used with NOISE_IMP_SIMPLE. |
|
1059
|
|
|
|
|
1060
|
|
|
``lacunarity`` is a multiplier that determines how fast the noise |
|
1061
|
|
|
frequency increases for each successive octave. |
|
1062
|
|
|
Not used with NOISE_IMP_SIMPLE. |
|
1063
|
|
|
|
|
1064
|
|
|
Args: |
|
1065
|
|
|
dimentions (int): Must be from 1 to 4. |
|
1066
|
|
|
algorithm (int): Defaults to NOISE_SIMPLEX |
|
1067
|
|
|
implementation (int): Defaults to NOISE_IMP_SIMPLE |
|
1068
|
|
|
hurst (float): The hurst exponent. Should be in the 0.0-1.0 range. |
|
1069
|
|
|
lacunarity (float): The noise lacunarity. |
|
1070
|
|
|
octaves (float): The level of detail on fBm and turbulence |
|
1071
|
|
|
implementations. |
|
1072
|
|
|
rand (Optional[Random]): A Random instance, or None. |
|
1073
|
|
|
""" |
|
1074
|
|
|
def __init__(self, *args, **kargs): |
|
1075
|
|
|
self.octaves = 4 |
|
1076
|
|
|
self.implementation = NOISE_IMP_SIMPLE |
|
1077
|
|
|
self._cdata_random = None # keep alive the random cdata instance |
|
1078
|
|
|
self._algorithm = None |
|
1079
|
|
|
self._dimentions = None |
|
1080
|
|
|
self._hurst = None |
|
1081
|
|
|
self._lacunarity = None |
|
1082
|
|
|
super(Noise, self).__init__(*args, **kargs) |
|
1083
|
|
|
if not self.cdata: |
|
1084
|
|
|
self._init(*args, **kargs) |
|
1085
|
|
|
|
|
1086
|
|
|
def _init(self, dimentions, algorithm=2, implementation=NOISE_IMP_SIMPLE, |
|
|
|
|
|
|
1087
|
|
|
hurst=0.5, lacunarity=2.0, octaves=4, rand=None): |
|
1088
|
|
|
self._cdata_random = _cdata(rand) |
|
1089
|
|
|
self.implementation = implementation |
|
1090
|
|
|
self._dimentions = dimentions |
|
1091
|
|
|
self._hurst = hurst |
|
1092
|
|
|
self._lacunarity = lacunarity |
|
1093
|
|
|
self.octaves = octaves |
|
1094
|
|
|
self.cdata = ffi.gc(lib.TCOD_noise_new(self._dimentions, self._hurst, |
|
|
|
|
|
|
1095
|
|
|
self._lacunarity, |
|
1096
|
|
|
self._cdata_random), |
|
1097
|
|
|
lib.TCOD_noise_delete) |
|
|
|
|
|
|
1098
|
|
|
self.algorithm = algorithm |
|
1099
|
|
|
|
|
1100
|
|
|
@property |
|
1101
|
|
|
def algorithm(self): |
|
|
|
|
|
|
1102
|
|
|
return self._algorithm |
|
1103
|
|
|
@algorithm.setter |
|
1104
|
|
|
def algorithm(self, value): |
|
|
|
|
|
|
1105
|
|
|
self._algorithm = value |
|
1106
|
|
|
lib.TCOD_noise_set_type(self.cdata, value) |
|
|
|
|
|
|
1107
|
|
|
|
|
1108
|
|
|
@property |
|
1109
|
|
|
def dimentions(self): |
|
|
|
|
|
|
1110
|
|
|
return self._dimentions |
|
1111
|
|
|
|
|
1112
|
|
|
@property |
|
1113
|
|
|
def hurst(self): |
|
|
|
|
|
|
1114
|
|
|
return self._hurst |
|
1115
|
|
|
|
|
1116
|
|
|
@property |
|
1117
|
|
|
def lacunarity(self): |
|
|
|
|
|
|
1118
|
|
|
return self._lacunarity |
|
1119
|
|
|
|
|
1120
|
|
|
def get_point(self, x=0, y=0, z=0, w=0): |
|
1121
|
|
|
"""Return the noise value at the (x, y, z, w) point. |
|
1122
|
|
|
|
|
1123
|
|
|
Args: |
|
1124
|
|
|
x (float): The position on the 1st axis. |
|
1125
|
|
|
y (float): The position on the 2nd axis. |
|
1126
|
|
|
z (float): The position on the 3rd axis. |
|
1127
|
|
|
w (float): The position on the 4th axis. |
|
1128
|
|
|
""" |
|
1129
|
|
|
if self.implementation == NOISE_IMP_SIMPLE: |
|
1130
|
|
|
return lib.TCOD_noise_get(self.cdata, (x, y, z, w)) |
|
|
|
|
|
|
1131
|
|
|
elif self.implementation == NOISE_IMP_FBM: |
|
1132
|
|
|
return lib.TCOD_noise_get_fbm(self.cdata, (x, y, z, w), |
|
|
|
|
|
|
1133
|
|
|
self.octaves) |
|
1134
|
|
|
elif self.implementation == NOISE_IMP_TURBULENCE: |
|
1135
|
|
|
return lib.TCOD_noise_get_turbulence(self.cdata, (x, y, z, w), |
|
|
|
|
|
|
1136
|
|
|
self.octaves) |
|
1137
|
|
|
raise RuntimeError('implementation must be one of tcod.NOISE_IMP_*') |
|
1138
|
|
|
|
|
1139
|
|
|
class Map(_CDataWrapper): |
|
1140
|
|
|
""" |
|
1141
|
|
|
.. versionadded:: 2.0 |
|
1142
|
|
|
|
|
1143
|
|
|
Args: |
|
1144
|
|
|
width (int): Width of the new Map. |
|
1145
|
|
|
height (int): Height of the new Map. |
|
1146
|
|
|
|
|
1147
|
|
|
Attributes: |
|
1148
|
|
|
width (int): Read only width of this Map. |
|
1149
|
|
|
height (int): Read only height of this Map. |
|
1150
|
|
|
""" |
|
1151
|
|
|
|
|
1152
|
|
|
def __init__(self, *args, **kargs): |
|
1153
|
|
|
super(Map, self).__init__(*args, **kargs) |
|
1154
|
|
|
if not self.cdata: |
|
1155
|
|
|
self._init(*args, **kargs) |
|
1156
|
|
|
|
|
1157
|
|
|
self.width = lib.TCOD_map_get_width(self.cdata) |
|
|
|
|
|
|
1158
|
|
|
self.height = lib.TCOD_map_get_width(self.cdata) |
|
|
|
|
|
|
1159
|
|
|
|
|
1160
|
|
|
def _init(self, width, height): |
|
|
|
|
|
|
1161
|
|
|
self.cdata = ffi.gc(lib.TCOD_map_new(width, height), |
|
|
|
|
|
|
1162
|
|
|
lib.TCOD_map_delete) |
|
|
|
|
|
|
1163
|
|
|
|
|
1164
|
|
|
def set_properties(self, x, y, transparent, walkable): |
|
|
|
|
|
|
1165
|
|
|
lib.TCOD_map_set_properties(self.cdata, x, y, transparent, walkable) |
|
|
|
|
|
|
1166
|
|
|
|
|
1167
|
|
|
def clear(self, transparent, walkable): |
|
|
|
|
|
|
1168
|
|
|
lib.TCOD_map_clear(self.cdata, transparent, walkable) |
|
|
|
|
|
|
1169
|
|
|
|
|
1170
|
|
|
def compute_fov(self, x, y, radius=0, light_walls=True, |
|
1171
|
|
|
algorithm=lib.FOV_RESTRICTIVE): |
|
|
|
|
|
|
1172
|
|
|
""" |
|
1173
|
|
|
|
|
1174
|
|
|
Args: |
|
1175
|
|
|
x (int): |
|
1176
|
|
|
y (int): |
|
1177
|
|
|
radius (int): |
|
1178
|
|
|
light_walls (bool): |
|
1179
|
|
|
algorithm (int): Defaults to FOV_RESTRICTIVE |
|
1180
|
|
|
""" |
|
1181
|
|
|
lib.TCOD_map_compute_fov(self.cdata, x, y, radius, light_walls, |
|
|
|
|
|
|
1182
|
|
|
algorithm) |
|
1183
|
|
|
|
|
1184
|
|
|
def is_fov(self, x, y): |
|
|
|
|
|
|
1185
|
|
|
return lib.TCOD_map_is_in_fov(self.cdata, x, y) |
|
|
|
|
|
|
1186
|
|
|
|
|
1187
|
|
|
def is_transparent(self, x, y): |
|
|
|
|
|
|
1188
|
|
|
return lib.TCOD_map_is_transparent(self.cdata, x, y) |
|
|
|
|
|
|
1189
|
|
|
|
|
1190
|
|
|
def is_walkable(self, x, y): |
|
|
|
|
|
|
1191
|
|
|
return lib.TCOD_map_is_walkable(self.cdata, x, y) |
|
|
|
|
|
|
1192
|
|
|
|
|
1193
|
|
|
|
|
1194
|
|
|
class _PathFinder(object): |
|
1195
|
|
|
""" |
|
1196
|
|
|
.. versionadded:: 2.0 |
|
1197
|
|
|
""" |
|
1198
|
|
|
|
|
1199
|
|
|
def __init__(self, map_data, diagonal_cost=1.41): |
|
1200
|
|
|
self._cdata = None |
|
1201
|
|
|
self._map_data = map_data |
|
1202
|
|
|
self._diagonal_cost = diagonal_cost |
|
1203
|
|
|
self._handle = None |
|
1204
|
|
|
self._callback_args = () |
|
1205
|
|
|
self._propagator = _PropagateException() |
|
1206
|
|
|
self._width = None |
|
1207
|
|
|
self._height = None |
|
1208
|
|
|
|
|
1209
|
|
|
_path_new_using_map = lib.TCOD_path_new_using_map |
|
|
|
|
|
|
1210
|
|
|
_path_new_using_function = lib.TCOD_path_new_using_function |
|
|
|
|
|
|
1211
|
|
|
_path_delete = lib.TCOD_path_delete |
|
|
|
|
|
|
1212
|
|
|
|
|
1213
|
|
|
@property |
|
1214
|
|
|
def cdata(self): |
|
|
|
|
|
|
1215
|
|
|
if self._cdata is None: |
|
1216
|
|
|
self._cdata = self._init_cdata() |
|
1217
|
|
|
return self._cdata |
|
1218
|
|
|
|
|
1219
|
|
|
def _init_cdata(self): |
|
|
|
|
|
|
1220
|
|
|
if callable(self._map_data): |
|
1221
|
|
|
self._handle = ffi.new_handle(self) |
|
|
|
|
|
|
1222
|
|
|
return ffi.gc(self._path_new_using_function( |
|
|
|
|
|
|
1223
|
|
|
self._width, self._height, |
|
1224
|
|
|
lib._pycall_path_func, self._handle, |
|
|
|
|
|
|
1225
|
|
|
self._diagonal_cost), |
|
1226
|
|
|
self._path_delete) |
|
1227
|
|
|
|
|
1228
|
|
|
if isinstance(self._map_data, Map): |
|
1229
|
|
|
self._width = self._map_data.width |
|
1230
|
|
|
self._height = self._map_data.height |
|
1231
|
|
|
return ffi.gc(self._path_new_using_map(self._map_data.cdata, |
|
|
|
|
|
|
1232
|
|
|
self._diagonal_cost), |
|
1233
|
|
|
self._path_delete) |
|
1234
|
|
|
|
|
1235
|
|
|
raise RuntimeError('map_data must be a callable/Map, not %r' % |
|
1236
|
|
|
(self._map_data,)) |
|
1237
|
|
|
|
|
1238
|
|
|
|
|
1239
|
|
|
class AStar(_PathFinder): |
|
1240
|
|
|
""" |
|
1241
|
|
|
.. versionadded:: 2.0 |
|
1242
|
|
|
""" |
|
1243
|
|
|
|
|
1244
|
|
|
def get_path(self, start_x, start_y, goal_x, goal_y): |
|
1245
|
|
|
"""Return a list of (x, y) steps to reach the goal point, if possible. |
|
1246
|
|
|
|
|
1247
|
|
|
Args: |
|
1248
|
|
|
start_x (int): Starting X position. |
|
1249
|
|
|
start_y (int): Starting Y position. |
|
1250
|
|
|
goal_x (int): Destination X position. |
|
1251
|
|
|
goal_y (int): Destination Y position. |
|
1252
|
|
|
Returns: |
|
1253
|
|
|
List[Tuple[int, int]]: |
|
1254
|
|
|
A list of points, or an empty list if there is no valid path. |
|
1255
|
|
|
""" |
|
1256
|
|
|
with self._propagator: |
|
1257
|
|
|
lib.TCOD_path_compute(self.cdata, start_x, start_y, goal_x, goal_y) |
|
|
|
|
|
|
1258
|
|
|
path = [] |
|
1259
|
|
|
x = ffi.new('int[2]') |
|
|
|
|
|
|
1260
|
|
|
y = x + 1 |
|
1261
|
|
|
while lib.TCOD_path_walk(self.cdata, x, y, False): |
|
|
|
|
|
|
1262
|
|
|
path.append((x[0], y[0])) |
|
1263
|
|
|
return path |
|
1264
|
|
|
|
|
1265
|
|
|
|
|
1266
|
|
|
class Dijkstra(_PathFinder): |
|
1267
|
|
|
""" |
|
1268
|
|
|
.. versionadded:: 2.0 |
|
1269
|
|
|
""" |
|
1270
|
|
|
|
|
1271
|
|
|
_path_new_using_map = lib.TCOD_dijkstra_new |
|
|
|
|
|
|
1272
|
|
|
_path_new_using_function = lib.TCOD_dijkstra_new_using_function |
|
|
|
|
|
|
1273
|
|
|
_path_delete = lib.TCOD_dijkstra_delete |
|
|
|
|
|
|
1274
|
|
|
|
|
1275
|
|
|
def set_goal(self, x, y): |
|
1276
|
|
|
"""Set the goal point and recompute the Dijkstra path-finder. |
|
1277
|
|
|
""" |
|
1278
|
|
|
with self._propagator: |
|
1279
|
|
|
lib.TCOD_dijkstra_compute(self.cdata, x, y) |
|
|
|
|
|
|
1280
|
|
|
|
|
1281
|
|
|
def get_path(self, x, y): |
|
1282
|
|
|
"""Return a list of (x, y) steps to reach the goal point, if possible. |
|
1283
|
|
|
""" |
|
1284
|
|
|
lib.TCOD_dijkstra_path_set(self.cdata, x, y) |
|
|
|
|
|
|
1285
|
|
|
path = [] |
|
1286
|
|
|
pointer_x = ffi.new('int[2]') |
|
|
|
|
|
|
1287
|
|
|
pointer_y = x + 1 |
|
1288
|
|
|
while lib.TCOD_dijkstra_path_walk(self.cdata, pointer_x, pointer_y): |
|
|
|
|
|
|
1289
|
|
|
path.append(pointer_x[0], pointer_y[0]) |
|
1290
|
|
|
return path |
|
1291
|
|
|
|
|
1292
|
|
|
def clipboard_set(string): |
|
1293
|
|
|
"""Set the clipboard contents to string. |
|
1294
|
|
|
|
|
1295
|
|
|
Args: |
|
1296
|
|
|
string (AnyStr): A Unicode or UTF-8 encoded string. |
|
1297
|
|
|
|
|
1298
|
|
|
.. versionadded:: 2.0 |
|
1299
|
|
|
""" |
|
1300
|
|
|
lib.TCOD_sys_clipboard_set(_bytes(string)) |
|
|
|
|
|
|
1301
|
|
|
|
|
1302
|
|
|
def clipboard_get(): |
|
1303
|
|
|
"""Return the current contents of the clipboard. |
|
1304
|
|
|
|
|
1305
|
|
|
Returns: |
|
1306
|
|
|
Text: The clipboards current contents. |
|
1307
|
|
|
|
|
1308
|
|
|
.. versionadded:: 2.0 |
|
1309
|
|
|
""" |
|
1310
|
|
|
return _unpack_char_p(lib.TCOD_sys_clipboard_get()) |
|
|
|
|
|
|
1311
|
|
|
|
|
1312
|
|
|
@ffi.def_extern() |
|
1313
|
|
|
def _pycall_bsp_callback(node, handle): |
|
1314
|
|
|
"""static bool _pycall_bsp_callback(TCOD_bsp_t *node, void *userData);""" |
|
1315
|
|
|
func, userData, propagate = ffi.from_handle(handle) |
|
|
|
|
|
|
1316
|
|
|
try: |
|
1317
|
|
|
return func(BSP(node), userData) |
|
|
|
|
|
|
1318
|
|
|
except BaseException: |
|
1319
|
|
|
propagate(*_sys.exc_info()) |
|
1320
|
|
|
return False |
|
1321
|
|
|
|
|
1322
|
|
|
__all__ = [_name for _name in list(globals()) if _name[0] != '_'] |
|
1323
|
|
|
|