1
|
|
|
"""This module handles backward compatibility with the ctypes libtcodpy module. |
2
|
|
|
""" |
3
|
|
|
|
4
|
|
|
from __future__ import absolute_import as _ |
5
|
|
|
|
6
|
|
|
import os |
7
|
|
|
import sys |
8
|
|
|
|
9
|
|
|
import threading as _threading |
10
|
|
|
|
11
|
|
|
import numpy as _np |
|
|
|
|
12
|
|
|
|
13
|
|
|
from tcod.libtcod import * |
|
|
|
|
14
|
|
|
|
15
|
|
|
from tcod.tcod import _int, _unpack_char_p |
16
|
|
|
from tcod.tcod import _bytes, _unicode, _fmt_bytes, _fmt_unicode |
17
|
|
|
from tcod.tcod import _CDataWrapper |
18
|
|
|
from tcod.tcod import _PropagateException |
19
|
|
|
from tcod.tcod import _console |
20
|
|
|
|
21
|
|
|
import tcod.bsp |
22
|
|
|
from tcod.color import * |
|
|
|
|
23
|
|
|
import tcod.console |
24
|
|
|
import tcod.image |
25
|
|
|
import tcod.map |
26
|
|
|
import tcod.noise |
27
|
|
|
import tcod.path |
28
|
|
|
import tcod.random |
29
|
|
|
|
30
|
|
|
Bsp = tcod.bsp.BSP |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class ConsoleBuffer(object): |
34
|
|
|
"""Simple console that allows direct (fast) access to cells. simplifies |
35
|
|
|
use of the "fill" functions. |
36
|
|
|
|
37
|
|
|
Args: |
38
|
|
|
width (int): Width of the new ConsoleBuffer. |
39
|
|
|
height (int): Height of the new ConsoleBuffer. |
40
|
|
|
back_r (int): Red background color, from 0 to 255. |
41
|
|
|
back_g (int): Green background color, from 0 to 255. |
42
|
|
|
back_b (int): Blue background color, from 0 to 255. |
43
|
|
|
fore_r (int): Red foreground color, from 0 to 255. |
44
|
|
|
fore_g (int): Green foreground color, from 0 to 255. |
45
|
|
|
fore_b (int): Blue foreground color, from 0 to 255. |
46
|
|
|
char (AnyStr): A single character str or bytes object. |
47
|
|
|
""" |
48
|
|
|
def __init__(self, width, height, back_r=0, back_g=0, back_b=0, fore_r=0, fore_g=0, fore_b=0, char=' '): |
|
|
|
|
49
|
|
|
"""initialize with given width and height. values to fill the buffer |
50
|
|
|
are optional, defaults to black with no characters. |
51
|
|
|
""" |
52
|
|
|
self.width = width |
53
|
|
|
self.height = height |
54
|
|
|
self.clear(back_r, back_g, back_b, fore_r, fore_g, fore_b, char) |
55
|
|
|
|
56
|
|
|
def clear(self, back_r=0, back_g=0, back_b=0, fore_r=0, fore_g=0, fore_b=0, char=' '): |
|
|
|
|
57
|
|
|
"""Clears the console. Values to fill it with are optional, defaults |
58
|
|
|
to black with no characters. |
59
|
|
|
|
60
|
|
|
Args: |
61
|
|
|
back_r (int): Red background color, from 0 to 255. |
62
|
|
|
back_g (int): Green background color, from 0 to 255. |
63
|
|
|
back_b (int): Blue background color, from 0 to 255. |
64
|
|
|
fore_r (int): Red foreground color, from 0 to 255. |
65
|
|
|
fore_g (int): Green foreground color, from 0 to 255. |
66
|
|
|
fore_b (int): Blue foreground color, from 0 to 255. |
67
|
|
|
char (AnyStr): A single character str or bytes object. |
68
|
|
|
""" |
69
|
|
|
n = self.width * self.height |
|
|
|
|
70
|
|
|
self.back_r = [back_r] * n |
71
|
|
|
self.back_g = [back_g] * n |
72
|
|
|
self.back_b = [back_b] * n |
73
|
|
|
self.fore_r = [fore_r] * n |
74
|
|
|
self.fore_g = [fore_g] * n |
75
|
|
|
self.fore_b = [fore_b] * n |
76
|
|
|
self.char = [ord(char)] * n |
77
|
|
|
|
78
|
|
|
def copy(self): |
79
|
|
|
"""Returns a copy of this ConsoleBuffer. |
80
|
|
|
|
81
|
|
|
Returns: |
82
|
|
|
ConsoleBuffer: A new ConsoleBuffer copy. |
83
|
|
|
""" |
84
|
|
|
other = ConsoleBuffer(0, 0) |
85
|
|
|
other.width = self.width |
86
|
|
|
other.height = self.height |
87
|
|
|
other.back_r = list(self.back_r) # make explicit copies of all lists |
|
|
|
|
88
|
|
|
other.back_g = list(self.back_g) |
|
|
|
|
89
|
|
|
other.back_b = list(self.back_b) |
|
|
|
|
90
|
|
|
other.fore_r = list(self.fore_r) |
|
|
|
|
91
|
|
|
other.fore_g = list(self.fore_g) |
|
|
|
|
92
|
|
|
other.fore_b = list(self.fore_b) |
|
|
|
|
93
|
|
|
other.char = list(self.char) |
|
|
|
|
94
|
|
|
return other |
95
|
|
|
|
96
|
|
|
def set_fore(self, x, y, r, g, b, char): |
|
|
|
|
97
|
|
|
"""Set the character and foreground color of one cell. |
98
|
|
|
|
99
|
|
|
Args: |
100
|
|
|
x (int): X position to change. |
101
|
|
|
y (int): Y position to change. |
102
|
|
|
r (int): Red foreground color, from 0 to 255. |
103
|
|
|
g (int): Green foreground color, from 0 to 255. |
104
|
|
|
b (int): Blue foreground color, from 0 to 255. |
105
|
|
|
char (AnyStr): A single character str or bytes object. |
106
|
|
|
""" |
107
|
|
|
i = self.width * y + x |
108
|
|
|
self.fore_r[i] = r |
109
|
|
|
self.fore_g[i] = g |
110
|
|
|
self.fore_b[i] = b |
111
|
|
|
self.char[i] = ord(char) |
112
|
|
|
|
113
|
|
|
def set_back(self, x, y, r, g, b): |
|
|
|
|
114
|
|
|
"""Set the background color of one cell. |
115
|
|
|
|
116
|
|
|
Args: |
117
|
|
|
x (int): X position to change. |
118
|
|
|
y (int): Y position to change. |
119
|
|
|
r (int): Red background color, from 0 to 255. |
120
|
|
|
g (int): Green background color, from 0 to 255. |
121
|
|
|
b (int): Blue background color, from 0 to 255. |
122
|
|
|
char (AnyStr): A single character str or bytes object. |
123
|
|
|
""" |
124
|
|
|
i = self.width * y + x |
125
|
|
|
self.back_r[i] = r |
126
|
|
|
self.back_g[i] = g |
127
|
|
|
self.back_b[i] = b |
128
|
|
|
|
129
|
|
|
def set(self, x, y, back_r, back_g, back_b, fore_r, fore_g, fore_b, char): |
|
|
|
|
130
|
|
|
"""Set the background color, foreground color and character of one cell. |
131
|
|
|
|
132
|
|
|
Args: |
133
|
|
|
x (int): X position to change. |
134
|
|
|
y (int): Y position to change. |
135
|
|
|
back_r (int): Red background color, from 0 to 255. |
136
|
|
|
back_g (int): Green background color, from 0 to 255. |
137
|
|
|
back_b (int): Blue background color, from 0 to 255. |
138
|
|
|
fore_r (int): Red foreground color, from 0 to 255. |
139
|
|
|
fore_g (int): Green foreground color, from 0 to 255. |
140
|
|
|
fore_b (int): Blue foreground color, from 0 to 255. |
141
|
|
|
char (AnyStr): A single character str or bytes object. |
142
|
|
|
""" |
143
|
|
|
i = self.width * y + x |
144
|
|
|
self.back_r[i] = back_r |
145
|
|
|
self.back_g[i] = back_g |
146
|
|
|
self.back_b[i] = back_b |
147
|
|
|
self.fore_r[i] = fore_r |
148
|
|
|
self.fore_g[i] = fore_g |
149
|
|
|
self.fore_b[i] = fore_b |
150
|
|
|
self.char[i] = ord(char) |
151
|
|
|
|
152
|
|
|
def blit(self, dest, fill_fore=True, fill_back=True): |
153
|
|
|
"""Use libtcod's "fill" functions to write the buffer to a console. |
154
|
|
|
|
155
|
|
|
Args: |
156
|
|
|
dest (Console): Console object to modify. |
157
|
|
|
fill_fore (bool): |
158
|
|
|
If True, fill the foreground color and characters. |
159
|
|
|
fill_back (bool): |
160
|
|
|
If True, fill the background color. |
161
|
|
|
""" |
162
|
|
|
if not dest: |
163
|
|
|
dest = tcod.console.Console._from_cdata(ffi.NULL) |
164
|
|
|
if (dest.width != self.width or |
165
|
|
|
dest.height != self.height): |
166
|
|
|
raise ValueError('ConsoleBuffer.blit: Destination console has an incorrect size.') |
|
|
|
|
167
|
|
|
|
168
|
|
|
if fill_back: |
169
|
|
|
bg = dest.bg.ravel() |
|
|
|
|
170
|
|
|
bg[0::3] = self.back_r |
171
|
|
|
bg[1::3] = self.back_g |
172
|
|
|
bg[2::3] = self.back_b |
173
|
|
|
|
174
|
|
|
if fill_fore: |
175
|
|
|
fg = dest.fg.ravel() |
|
|
|
|
176
|
|
|
fg[0::3] = self.fore_r |
177
|
|
|
fg[1::3] = self.fore_g |
178
|
|
|
fg[2::3] = self.fore_b |
179
|
|
|
dest.ch.ravel()[:] = self.char |
180
|
|
|
|
181
|
|
|
class Dice(_CDataWrapper): |
182
|
|
|
""" |
183
|
|
|
|
184
|
|
|
Args: |
185
|
|
|
nb_dices (int): Number of dice. |
186
|
|
|
nb_faces (int): Number of sides on a die. |
187
|
|
|
multiplier (float): Multiplier. |
188
|
|
|
addsub (float): Addition. |
189
|
|
|
|
190
|
|
|
.. deprecated:: 2.0 |
191
|
|
|
You should make your own dice functions instead of using this class |
192
|
|
|
which is tied to a CData object. |
193
|
|
|
""" |
194
|
|
|
|
195
|
|
|
def __init__(self, *args, **kargs): |
196
|
|
|
super(Dice, self).__init__(*args, **kargs) |
197
|
|
|
if self.cdata == ffi.NULL: |
198
|
|
|
self._init(*args, **kargs) |
199
|
|
|
|
200
|
|
|
def _init(self, nb_dices=0, nb_faces=0, multiplier=0, addsub=0): |
|
|
|
|
201
|
|
|
self.cdata = ffi.new('TCOD_dice_t*') |
202
|
|
|
self.nb_dices = nb_dices |
203
|
|
|
self.nb_faces = nb_faces |
204
|
|
|
self.multiplier = multiplier |
205
|
|
|
self.addsub = addsub |
206
|
|
|
|
207
|
|
|
def _get_nb_dices(self): |
|
|
|
|
208
|
|
|
return self.nb_rolls |
209
|
|
|
def _set_nb_dices(self, value): |
|
|
|
|
210
|
|
|
self.nb_rolls = value |
|
|
|
|
211
|
|
|
nb_dices = property(_get_nb_dices, _set_nb_dices) |
212
|
|
|
|
213
|
|
|
def __str__(self): |
214
|
|
|
add = '+(%s)' % self.addsub if self.addsub != 0 else '' |
215
|
|
|
return '%id%ix%s%s' % (self.nb_dices, self.nb_faces, |
216
|
|
|
self.multiplier, add) |
217
|
|
|
|
218
|
|
|
def __repr__(self): |
219
|
|
|
return ('%s(nb_dices=%r,nb_faces=%r,multiplier=%r,addsub=%r)' % |
220
|
|
|
(self.__class__.__name__, self.nb_dices, self.nb_faces, |
221
|
|
|
self.multiplier, self.addsub)) |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
# reverse lookup table for KEY_X attributes, used by Key.__repr__ |
225
|
|
|
_LOOKUP_VK = { |
226
|
|
|
value:'KEY_%s' % key[6:] for key,value in lib.__dict__.items() |
|
|
|
|
227
|
|
|
if key.startswith('TCODK') |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
class Key(_CDataWrapper): |
231
|
|
|
"""Key Event instance |
232
|
|
|
|
233
|
|
|
Attributes: |
234
|
|
|
vk (int): TCOD_keycode_t key code |
235
|
|
|
c (int): character if vk == TCODK_CHAR else 0 |
236
|
|
|
text (Text): text[TCOD_KEY_TEXT_SIZE]; text if vk == TCODK_TEXT else text[0] == '\0' |
|
|
|
|
237
|
|
|
pressed (bool): does this correspond to a key press or key release event ? |
|
|
|
|
238
|
|
|
lalt (bool): True when left alt is held. |
239
|
|
|
lctrl (bool): True when left control is held. |
240
|
|
|
lmeta (bool): True when left meta key is held. |
241
|
|
|
ralt (bool): True when right alt is held. |
242
|
|
|
rctrl (bool): True when right control is held. |
243
|
|
|
rmeta (bool): True when right meta key is held. |
244
|
|
|
shift (bool): True when any shift is held. |
245
|
|
|
""" |
246
|
|
|
|
247
|
|
|
_BOOL_ATTRIBUTES = ('lalt', 'lctrl', 'lmeta', |
248
|
|
|
'ralt', 'rctrl', 'rmeta', 'pressed', 'shift') |
249
|
|
|
|
250
|
|
|
def __init__(self, *args, **kargs): |
251
|
|
|
super(Key, self).__init__(*args, **kargs) |
252
|
|
|
if self.cdata == ffi.NULL: |
253
|
|
|
self.cdata = ffi.new('TCOD_key_t*') |
254
|
|
|
|
255
|
|
|
def __getattr__(self, attr): |
256
|
|
|
if attr in self._BOOL_ATTRIBUTES: |
257
|
|
|
return bool(getattr(self.cdata, attr)) |
258
|
|
|
if attr == 'c': |
259
|
|
|
return ord(getattr(self.cdata, attr)) |
260
|
|
|
if attr == 'text': |
261
|
|
|
return _unpack_char_p(getattr(self.cdata, attr)) |
262
|
|
|
return super(Key, self).__getattr__(attr) |
263
|
|
|
|
264
|
|
|
def __repr__(self): |
265
|
|
|
"""Return a representation of this Key object.""" |
266
|
|
|
params = [] |
267
|
|
|
params.append('pressed=%r, vk=tcod.%s' % |
268
|
|
|
(self.pressed, _LOOKUP_VK[self.vk])) |
269
|
|
|
if self.c: |
270
|
|
|
params.append('c=ord(%r)' % chr(self.c)) |
271
|
|
|
if self.text: |
272
|
|
|
params.append('text=%r' % self.text) |
273
|
|
|
for attr in ['shift', 'lalt', 'lctrl', 'lmeta', |
274
|
|
|
'ralt', 'rctrl', 'rmeta']: |
275
|
|
|
if getattr(self, attr): |
276
|
|
|
params.append('%s=%r' % (attr, getattr(self, attr))) |
277
|
|
|
return 'tcod.Key(%s)' % ', '.join(params) |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
class Mouse(_CDataWrapper): |
281
|
|
|
"""Mouse event instance |
282
|
|
|
|
283
|
|
|
Attributes: |
284
|
|
|
x (int): Absolute mouse position at pixel x. |
285
|
|
|
y (int): |
286
|
|
|
dx (int): Movement since last update in pixels. |
287
|
|
|
dy (int): |
288
|
|
|
cx (int): Cell coordinates in the root console. |
289
|
|
|
cy (int): |
290
|
|
|
dcx (int): Movement since last update in console cells. |
291
|
|
|
dcy (int): |
292
|
|
|
lbutton (bool): Left button status. |
293
|
|
|
rbutton (bool): Right button status. |
294
|
|
|
mbutton (bool): Middle button status. |
295
|
|
|
lbutton_pressed (bool): Left button pressed event. |
296
|
|
|
rbutton_pressed (bool): Right button pressed event. |
297
|
|
|
mbutton_pressed (bool): Middle button pressed event. |
298
|
|
|
wheel_up (bool): Wheel up event. |
299
|
|
|
wheel_down (bool): Wheel down event. |
300
|
|
|
""" |
301
|
|
|
|
302
|
|
|
def __init__(self, *args, **kargs): |
303
|
|
|
super(Mouse, self).__init__(*args, **kargs) |
304
|
|
|
if self.cdata == ffi.NULL: |
305
|
|
|
self.cdata = ffi.new('TCOD_mouse_t*') |
306
|
|
|
|
307
|
|
|
def __repr__(self): |
308
|
|
|
"""Return a representation of this Mouse object.""" |
309
|
|
|
params = [] |
310
|
|
|
for attr in ['x', 'y', 'dx', 'dy', 'cx', 'cy', 'dcx', 'dcy']: |
311
|
|
|
if getattr(self, attr) == 0: |
312
|
|
|
continue |
313
|
|
|
params.append('%s=%r' % (attr, getattr(self, attr))) |
314
|
|
|
for attr in ['lbutton', 'rbutton', 'mbutton', |
315
|
|
|
'lbutton_pressed', 'rbutton_pressed', 'mbutton_pressed', |
316
|
|
|
'wheel_up', 'wheel_down']: |
317
|
|
|
if getattr(self, attr): |
318
|
|
|
params.append('%s=%r' % (attr, getattr(self, attr))) |
319
|
|
|
return 'tcod.Mouse(%s)' % ', '.join(params) |
320
|
|
|
|
321
|
|
|
|
322
|
|
|
def bsp_new_with_size(x, y, w, h): |
|
|
|
|
323
|
|
|
"""Create a new BSP instance with the given rectangle. |
324
|
|
|
|
325
|
|
|
Args: |
326
|
|
|
x (int): Rectangle left coordinate. |
327
|
|
|
y (int): Rectangle top coordinate. |
328
|
|
|
w (int): Rectangle width. |
329
|
|
|
h (int): Rectangle height. |
330
|
|
|
|
331
|
|
|
Returns: |
332
|
|
|
BSP: A new BSP instance. |
333
|
|
|
|
334
|
|
|
.. deprecated:: 2.0 |
335
|
|
|
Call the :any:`BSP` class instead. |
336
|
|
|
""" |
337
|
|
|
return Bsp(x, y, w, h) |
338
|
|
|
|
339
|
|
|
def bsp_split_once(node, horizontal, position): |
340
|
|
|
""" |
341
|
|
|
.. deprecated:: 2.0 |
342
|
|
|
Use :any:`BSP.split_once` instead. |
343
|
|
|
""" |
344
|
|
|
node.split_once(horizontal, position) |
345
|
|
|
|
346
|
|
|
def bsp_split_recursive(node, randomizer, nb, minHSize, minVSize, maxHRatio, |
|
|
|
|
347
|
|
|
maxVRatio): |
348
|
|
|
""" |
349
|
|
|
.. deprecated:: 2.0 |
350
|
|
|
Use :any:`BSP.split_recursive` instead. |
351
|
|
|
""" |
352
|
|
|
node.split_recursive(nb, minHSize, minVSize, |
353
|
|
|
maxHRatio, maxVRatio, randomizer) |
354
|
|
|
|
355
|
|
|
def bsp_resize(node, x, y, w, h): |
|
|
|
|
356
|
|
|
""" |
357
|
|
|
.. deprecated:: 2.0 |
358
|
|
|
Assign directly to :any:`BSP` attributes instead. |
359
|
|
|
""" |
360
|
|
|
node.x = x |
361
|
|
|
node.y = y |
362
|
|
|
node.width = w |
363
|
|
|
node.height = h |
364
|
|
|
|
365
|
|
|
def bsp_left(node): |
366
|
|
|
""" |
367
|
|
|
.. deprecated:: 2.0 |
368
|
|
|
Use :any:`BSP.children` instead. |
369
|
|
|
""" |
370
|
|
|
return None if not node.children else node.children[0] |
371
|
|
|
|
372
|
|
|
def bsp_right(node): |
373
|
|
|
""" |
374
|
|
|
.. deprecated:: 2.0 |
375
|
|
|
Use :any:`BSP.children` instead. |
376
|
|
|
""" |
377
|
|
|
return None if not node.children else node.children[1] |
378
|
|
|
|
379
|
|
|
def bsp_father(node): |
380
|
|
|
""" |
381
|
|
|
.. deprecated:: 2.0 |
382
|
|
|
Use :any:`BSP.parent` instead. |
383
|
|
|
""" |
384
|
|
|
return node.parent |
385
|
|
|
|
386
|
|
|
def bsp_is_leaf(node): |
387
|
|
|
""" |
388
|
|
|
.. deprecated:: 2.0 |
389
|
|
|
Use :any:`BSP.children` instead. |
390
|
|
|
""" |
391
|
|
|
return not node.children |
392
|
|
|
|
393
|
|
|
def bsp_contains(node, cx, cy): |
|
|
|
|
394
|
|
|
""" |
395
|
|
|
.. deprecated:: 2.0 |
396
|
|
|
Use :any:`BSP.contains` instead. |
397
|
|
|
""" |
398
|
|
|
return node.contains(cx, cy) |
399
|
|
|
|
400
|
|
|
def bsp_find_node(node, cx, cy): |
|
|
|
|
401
|
|
|
""" |
402
|
|
|
.. deprecated:: 2.0 |
403
|
|
|
Use :any:`BSP.find_node` instead. |
404
|
|
|
""" |
405
|
|
|
return node.find_node(cx, cy) |
406
|
|
|
|
407
|
|
|
def _bsp_traverse(node_iter, callback, userData): |
|
|
|
|
408
|
|
|
"""pack callback into a handle for use with the callback |
409
|
|
|
_pycall_bsp_callback |
410
|
|
|
""" |
411
|
|
|
for node in node_iter: |
412
|
|
|
callback(node, userData) |
413
|
|
|
|
414
|
|
|
def bsp_traverse_pre_order(node, callback, userData=0): |
|
|
|
|
415
|
|
|
"""Traverse this nodes hierarchy with a callback. |
416
|
|
|
|
417
|
|
|
.. deprecated:: 2.0 |
418
|
|
|
Use :any:`BSP.walk` instead. |
419
|
|
|
""" |
420
|
|
|
_bsp_traverse(node._iter_pre_order(), callback, userData) |
421
|
|
|
|
422
|
|
|
def bsp_traverse_in_order(node, callback, userData=0): |
|
|
|
|
423
|
|
|
"""Traverse this nodes hierarchy with a callback. |
424
|
|
|
|
425
|
|
|
.. deprecated:: 2.0 |
426
|
|
|
Use :any:`BSP.walk` instead. |
427
|
|
|
""" |
428
|
|
|
_bsp_traverse(node._iter_in_order(), callback, userData) |
429
|
|
|
|
430
|
|
|
def bsp_traverse_post_order(node, callback, userData=0): |
|
|
|
|
431
|
|
|
"""Traverse this nodes hierarchy with a callback. |
432
|
|
|
|
433
|
|
|
.. deprecated:: 2.0 |
434
|
|
|
Use :any:`BSP.walk` instead. |
435
|
|
|
""" |
436
|
|
|
_bsp_traverse(node._iter_post_order(), callback, userData) |
437
|
|
|
|
438
|
|
|
def bsp_traverse_level_order(node, callback, userData=0): |
|
|
|
|
439
|
|
|
"""Traverse this nodes hierarchy with a callback. |
440
|
|
|
|
441
|
|
|
.. deprecated:: 2.0 |
442
|
|
|
Use :any:`BSP.walk` instead. |
443
|
|
|
""" |
444
|
|
|
_bsp_traverse(node._iter_level_order(), callback, userData) |
445
|
|
|
|
446
|
|
|
def bsp_traverse_inverted_level_order(node, callback, userData=0): |
|
|
|
|
447
|
|
|
"""Traverse this nodes hierarchy with a callback. |
448
|
|
|
|
449
|
|
|
.. deprecated:: 2.0 |
450
|
|
|
Use :any:`BSP.walk` instead. |
451
|
|
|
""" |
452
|
|
|
_bsp_traverse(node._iter_inverted_level_order(), callback, userData) |
453
|
|
|
|
454
|
|
|
def bsp_remove_sons(node): |
455
|
|
|
"""Delete all children of a given node. Not recommended. |
456
|
|
|
|
457
|
|
|
.. note:: |
458
|
|
|
This function will add unnecessary complexity to your code. |
459
|
|
|
Don't use it. |
460
|
|
|
|
461
|
|
|
.. deprecated:: 2.0 |
462
|
|
|
BSP deletion is automatic. |
463
|
|
|
""" |
464
|
|
|
node.children = () |
465
|
|
|
|
466
|
|
|
def bsp_delete(node): |
|
|
|
|
467
|
|
|
"""Exists for backward compatibility. Does nothing. |
468
|
|
|
|
469
|
|
|
BSP's created by this library are automatically garbage collected once |
470
|
|
|
there are no references to the tree. |
471
|
|
|
This function exists for backwards compatibility. |
472
|
|
|
|
473
|
|
|
.. deprecated:: 2.0 |
474
|
|
|
BSP deletion is automatic. |
475
|
|
|
""" |
476
|
|
|
pass |
477
|
|
|
|
478
|
|
|
def color_lerp(c1, c2, a): |
|
|
|
|
479
|
|
|
"""Return the linear interpolation between two colors. |
480
|
|
|
|
481
|
|
|
``a`` is the interpolation value, with 0 returing ``c1``, |
482
|
|
|
1 returning ``c2``, and 0.5 returing a color halfway between both. |
483
|
|
|
|
484
|
|
|
Args: |
485
|
|
|
c1 (Union[Tuple[int, int, int], Sequence[int]]): |
486
|
|
|
The first color. At a=0. |
487
|
|
|
c2 (Union[Tuple[int, int, int], Sequence[int]]): |
488
|
|
|
The second color. At a=1. |
489
|
|
|
a (float): The interpolation value, |
490
|
|
|
|
491
|
|
|
Returns: |
492
|
|
|
Color: The interpolated Color. |
493
|
|
|
""" |
494
|
|
|
return Color._new_from_cdata(lib.TCOD_color_lerp(c1, c2, a)) |
495
|
|
|
|
496
|
|
|
def color_set_hsv(c, h, s, v): |
|
|
|
|
497
|
|
|
"""Set a color using: hue, saturation, and value parameters. |
498
|
|
|
|
499
|
|
|
Does not return a new Color. ``c`` is modified inplace. |
500
|
|
|
|
501
|
|
|
Args: |
502
|
|
|
c (Union[Color, List[Any]]): A Color instance, or a list of any kind. |
503
|
|
|
h (float): Hue, from 0 to 360. |
504
|
|
|
s (float): Saturation, from 0 to 1. |
505
|
|
|
v (float): Value, from 0 to 1. |
506
|
|
|
""" |
507
|
|
|
new_color = ffi.new('TCOD_color_t*') |
508
|
|
|
lib.TCOD_color_set_HSV(new_color, h, s, v) |
509
|
|
|
c[:] = new_color.r, new_color.g, new_color.b |
510
|
|
|
|
511
|
|
|
def color_get_hsv(c): |
|
|
|
|
512
|
|
|
"""Return the (hue, saturation, value) of a color. |
513
|
|
|
|
514
|
|
|
Args: |
515
|
|
|
c (Union[Tuple[int, int, int], Sequence[int]]): |
516
|
|
|
An (r, g, b) sequence or Color instance. |
517
|
|
|
|
518
|
|
|
Returns: |
519
|
|
|
Tuple[float, float, float]: |
520
|
|
|
A tuple with (hue, saturation, value) values, from 0 to 1. |
521
|
|
|
""" |
522
|
|
|
hsv = ffi.new('float [3]') |
523
|
|
|
lib.TCOD_color_get_HSV(c, hsv, hsv + 1, hsv + 2) |
524
|
|
|
return hsv[0], hsv[1], hsv[2] |
525
|
|
|
|
526
|
|
|
def color_scale_HSV(c, scoef, vcoef): |
|
|
|
|
527
|
|
|
"""Scale a color's saturation and value. |
528
|
|
|
|
529
|
|
|
Does not return a new Color. ``c`` is modified inplace. |
530
|
|
|
|
531
|
|
|
Args: |
532
|
|
|
c (Union[Color, List[int]]): A Color instance, or an [r, g, b] list. |
533
|
|
|
scoef (float): Saturation multiplier, from 0 to 1. |
534
|
|
|
Use 1 to keep current saturation. |
535
|
|
|
vcoef (float): Value multiplier, from 0 to 1. |
536
|
|
|
Use 1 to keep current value. |
537
|
|
|
""" |
538
|
|
|
color_p = ffi.new('TCOD_color_t*') |
539
|
|
|
color_p.r, color_p.g, color_p.b = c.r, c.g, c.b |
540
|
|
|
lib.TCOD_color_scale_HSV(color_p, scoef, vcoef) |
541
|
|
|
c[:] = color_p.r, color_p.g, color_p.b |
542
|
|
|
|
543
|
|
|
def color_gen_map(colors, indexes): |
544
|
|
|
"""Return a smoothly defined scale of colors. |
545
|
|
|
|
546
|
|
|
If ``indexes`` is [0, 3, 9] for example, the first color from ``colors`` |
547
|
|
|
will be returned at 0, the 2nd will be at 3, and the 3rd will be at 9. |
548
|
|
|
All in-betweens will be filled with a gradient. |
549
|
|
|
|
550
|
|
|
Args: |
551
|
|
|
colors (Iterable[Union[Tuple[int, int, int], Sequence[int]]]): |
552
|
|
|
Array of colors to be sampled. |
553
|
|
|
indexes (Iterable[int]): A list of indexes. |
554
|
|
|
|
555
|
|
|
Returns: |
556
|
|
|
List[Color]: A list of Color instances. |
557
|
|
|
|
558
|
|
|
Example: |
559
|
|
|
>>> tcod.color_gen_map([(0, 0, 0), (255, 128, 0)], [0, 5]) |
560
|
|
|
[Color(0,0,0), Color(51,25,0), Color(102,51,0), Color(153,76,0), \ |
561
|
|
|
Color(204,102,0), Color(255,128,0)] |
562
|
|
|
""" |
563
|
|
|
ccolors = ffi.new('TCOD_color_t[]', colors) |
564
|
|
|
cindexes = ffi.new('int[]', indexes) |
565
|
|
|
cres = ffi.new('TCOD_color_t[]', max(indexes) + 1) |
566
|
|
|
lib.TCOD_color_gen_map(cres, len(colors), ccolors, cindexes) |
567
|
|
|
return [Color._new_from_cdata(cdata) for cdata in cres] |
568
|
|
|
|
569
|
|
|
|
570
|
|
|
def console_init_root(w, h, title=None, fullscreen=False, |
|
|
|
|
571
|
|
|
renderer=RENDERER_SDL, order='C'): |
|
|
|
|
572
|
|
|
"""Set up the primary display and return the root console. |
573
|
|
|
|
574
|
|
|
.. versionchanged:: 4.3 |
575
|
|
|
Added `order` parameter. |
576
|
|
|
`title` parameter is now optional. |
577
|
|
|
|
578
|
|
|
Args: |
579
|
|
|
w (int): Width in character tiles for the root console. |
580
|
|
|
h (int): Height in character tiles for the root console. |
581
|
|
|
title (Optional[AnyStr]): |
582
|
|
|
This string will be displayed on the created windows title bar. |
583
|
|
|
renderer: Rendering mode for libtcod to use. |
584
|
|
|
order (str): Which numpy memory order to use. |
585
|
|
|
|
586
|
|
|
Returns: |
587
|
|
|
Console: |
588
|
|
|
Returns a special Console instance representing the root console. |
589
|
|
|
""" |
590
|
|
|
if title is None: |
591
|
|
|
# Use the scripts filename as the title. |
592
|
|
|
title = os.path.basename(sys.argv[0]) |
593
|
|
|
lib.TCOD_console_init_root(w, h, _bytes(title), fullscreen, renderer) |
594
|
|
|
return tcod.console.Console._from_cdata(ffi.NULL, order) |
595
|
|
|
|
596
|
|
|
|
597
|
|
|
def console_set_custom_font(fontFile, flags=FONT_LAYOUT_ASCII_INCOL, |
|
|
|
|
598
|
|
|
nb_char_horiz=0, nb_char_vertic=0): |
599
|
|
|
"""Load a custom font file. |
600
|
|
|
|
601
|
|
|
Call this before function before calling :any:`tcod.console_init_root`. |
602
|
|
|
|
603
|
|
|
Flags can be a mix of the following: |
604
|
|
|
|
605
|
|
|
* tcod.FONT_LAYOUT_ASCII_INCOL |
606
|
|
|
* tcod.FONT_LAYOUT_ASCII_INROW |
607
|
|
|
* tcod.FONT_TYPE_GREYSCALE |
608
|
|
|
* tcod.FONT_TYPE_GRAYSCALE |
609
|
|
|
* tcod.FONT_LAYOUT_TCOD |
610
|
|
|
|
611
|
|
|
Args: |
612
|
|
|
fontFile (AnyStr): Path to a font file. |
613
|
|
|
flags (int): |
614
|
|
|
nb_char_horiz (int): |
615
|
|
|
nb_char_vertic (int): |
616
|
|
|
""" |
617
|
|
|
lib.TCOD_console_set_custom_font(_bytes(fontFile), flags, |
618
|
|
|
nb_char_horiz, nb_char_vertic) |
619
|
|
|
|
620
|
|
|
|
621
|
|
|
def console_get_width(con): |
622
|
|
|
"""Return the width of a console. |
623
|
|
|
|
624
|
|
|
Args: |
625
|
|
|
con (Console): Any Console instance. |
626
|
|
|
|
627
|
|
|
Returns: |
628
|
|
|
int: The width of a Console. |
629
|
|
|
|
630
|
|
|
.. deprecated:: 2.0 |
631
|
|
|
Use `Console.get_width` instead. |
632
|
|
|
""" |
633
|
|
|
return lib.TCOD_console_get_width(_console(con)) |
634
|
|
|
|
635
|
|
|
def console_get_height(con): |
636
|
|
|
"""Return the height of a console. |
637
|
|
|
|
638
|
|
|
Args: |
639
|
|
|
con (Console): Any Console instance. |
640
|
|
|
|
641
|
|
|
Returns: |
642
|
|
|
int: The height of a Console. |
643
|
|
|
|
644
|
|
|
.. deprecated:: 2.0 |
645
|
|
|
Use `Console.get_hright` instead. |
646
|
|
|
""" |
647
|
|
|
return lib.TCOD_console_get_height(_console(con)) |
648
|
|
|
|
649
|
|
|
def console_map_ascii_code_to_font(asciiCode, fontCharX, fontCharY): |
|
|
|
|
650
|
|
|
"""Set a character code to new coordinates on the tile-set. |
651
|
|
|
|
652
|
|
|
`asciiCode` must be within the bounds created during the initialization of |
653
|
|
|
the loaded tile-set. For example, you can't use 255 here unless you have a |
654
|
|
|
256 tile tile-set loaded. This applies to all functions in this group. |
655
|
|
|
|
656
|
|
|
Args: |
657
|
|
|
asciiCode (int): The character code to change. |
658
|
|
|
fontCharX (int): The X tile coordinate on the loaded tileset. |
659
|
|
|
0 is the leftmost tile. |
660
|
|
|
fontCharY (int): The Y tile coordinate on the loaded tileset. |
661
|
|
|
0 is the topmost tile. |
662
|
|
|
""" |
663
|
|
|
lib.TCOD_console_map_ascii_code_to_font(_int(asciiCode), fontCharX, |
664
|
|
|
fontCharY) |
665
|
|
|
|
666
|
|
|
def console_map_ascii_codes_to_font(firstAsciiCode, nbCodes, fontCharX, |
|
|
|
|
667
|
|
|
fontCharY): |
668
|
|
|
"""Remap a contiguous set of codes to a contiguous set of tiles. |
669
|
|
|
|
670
|
|
|
Both the tile-set and character codes must be contiguous to use this |
671
|
|
|
function. If this is not the case you may want to use |
672
|
|
|
:any:`console_map_ascii_code_to_font`. |
673
|
|
|
|
674
|
|
|
Args: |
675
|
|
|
firstAsciiCode (int): The starting character code. |
676
|
|
|
nbCodes (int): The length of the contiguous set. |
677
|
|
|
fontCharX (int): The starting X tile coordinate on the loaded tileset. |
678
|
|
|
0 is the leftmost tile. |
679
|
|
|
fontCharY (int): The starting Y tile coordinate on the loaded tileset. |
680
|
|
|
0 is the topmost tile. |
681
|
|
|
|
682
|
|
|
""" |
683
|
|
|
lib.TCOD_console_map_ascii_codes_to_font(_int(firstAsciiCode), nbCodes, |
684
|
|
|
fontCharX, fontCharY) |
685
|
|
|
|
686
|
|
|
def console_map_string_to_font(s, fontCharX, fontCharY): |
|
|
|
|
687
|
|
|
"""Remap a string of codes to a contiguous set of tiles. |
688
|
|
|
|
689
|
|
|
Args: |
690
|
|
|
s (AnyStr): A string of character codes to map to new values. |
691
|
|
|
The null character `'\x00'` will prematurely end this |
692
|
|
|
function. |
693
|
|
|
fontCharX (int): The starting X tile coordinate on the loaded tileset. |
694
|
|
|
0 is the leftmost tile. |
695
|
|
|
fontCharY (int): The starting Y tile coordinate on the loaded tileset. |
696
|
|
|
0 is the topmost tile. |
697
|
|
|
""" |
698
|
|
|
lib.TCOD_console_map_string_to_font_utf(_unicode(s), fontCharX, fontCharY) |
699
|
|
|
|
700
|
|
|
def console_is_fullscreen(): |
701
|
|
|
"""Returns True if the display is fullscreen. |
702
|
|
|
|
703
|
|
|
Returns: |
704
|
|
|
bool: True if the display is fullscreen, otherwise False. |
705
|
|
|
""" |
706
|
|
|
return bool(lib.TCOD_console_is_fullscreen()) |
707
|
|
|
|
708
|
|
|
def console_set_fullscreen(fullscreen): |
709
|
|
|
"""Change the display to be fullscreen or windowed. |
710
|
|
|
|
711
|
|
|
Args: |
712
|
|
|
fullscreen (bool): Use True to change to fullscreen. |
713
|
|
|
Use False to change to windowed. |
714
|
|
|
""" |
715
|
|
|
lib.TCOD_console_set_fullscreen(fullscreen) |
716
|
|
|
|
717
|
|
|
def console_is_window_closed(): |
718
|
|
|
"""Returns True if the window has received and exit event.""" |
719
|
|
|
return lib.TCOD_console_is_window_closed() |
720
|
|
|
|
721
|
|
|
def console_set_window_title(title): |
722
|
|
|
"""Change the current title bar string. |
723
|
|
|
|
724
|
|
|
Args: |
725
|
|
|
title (AnyStr): A string to change the title bar to. |
726
|
|
|
""" |
727
|
|
|
lib.TCOD_console_set_window_title(_bytes(title)) |
728
|
|
|
|
729
|
|
|
def console_credits(): |
|
|
|
|
730
|
|
|
lib.TCOD_console_credits() |
731
|
|
|
|
732
|
|
|
def console_credits_reset(): |
|
|
|
|
733
|
|
|
lib.TCOD_console_credits_reset() |
734
|
|
|
|
735
|
|
|
def console_credits_render(x, y, alpha): |
|
|
|
|
736
|
|
|
return lib.TCOD_console_credits_render(x, y, alpha) |
737
|
|
|
|
738
|
|
|
def console_flush(): |
739
|
|
|
"""Update the display to represent the root consoles current state.""" |
740
|
|
|
lib.TCOD_console_flush() |
741
|
|
|
|
742
|
|
|
# drawing on a console |
743
|
|
|
def console_set_default_background(con, col): |
744
|
|
|
"""Change the default background color for a console. |
745
|
|
|
|
746
|
|
|
Args: |
747
|
|
|
con (Console): Any Console instance. |
748
|
|
|
col (Union[Tuple[int, int, int], Sequence[int]]): |
749
|
|
|
An (r, g, b) sequence or Color instance. |
750
|
|
|
""" |
751
|
|
|
lib.TCOD_console_set_default_background(_console(con), col) |
752
|
|
|
|
753
|
|
|
def console_set_default_foreground(con, col): |
754
|
|
|
"""Change the default foreground color for a console. |
755
|
|
|
|
756
|
|
|
Args: |
757
|
|
|
con (Console): Any Console instance. |
758
|
|
|
col (Union[Tuple[int, int, int], Sequence[int]]): |
759
|
|
|
An (r, g, b) sequence or Color instance. |
760
|
|
|
""" |
761
|
|
|
lib.TCOD_console_set_default_foreground(_console(con), col) |
762
|
|
|
|
763
|
|
|
def console_clear(con): |
764
|
|
|
"""Reset a console to its default colors and the space character. |
765
|
|
|
|
766
|
|
|
Args: |
767
|
|
|
con (Console): Any Console instance. |
768
|
|
|
|
769
|
|
|
.. seealso:: |
770
|
|
|
:any:`console_set_default_background` |
771
|
|
|
:any:`console_set_default_foreground` |
772
|
|
|
""" |
773
|
|
|
return lib.TCOD_console_clear(_console(con)) |
774
|
|
|
|
775
|
|
|
def console_put_char(con, x, y, c, flag=BKGND_DEFAULT): |
|
|
|
|
776
|
|
|
"""Draw the character c at x,y using the default colors and a blend mode. |
777
|
|
|
|
778
|
|
|
Args: |
779
|
|
|
con (Console): Any Console instance. |
780
|
|
|
x (int): Character x position from the left. |
781
|
|
|
y (int): Character y position from the top. |
782
|
|
|
c (Union[int, AnyStr]): Character to draw, can be an integer or string. |
783
|
|
|
flag (int): Blending mode to use, defaults to BKGND_DEFAULT. |
784
|
|
|
""" |
785
|
|
|
lib.TCOD_console_put_char(_console(con), x, y, _int(c), flag) |
786
|
|
|
|
787
|
|
|
def console_put_char_ex(con, x, y, c, fore, back): |
|
|
|
|
788
|
|
|
"""Draw the character c at x,y using the colors fore and back. |
789
|
|
|
|
790
|
|
|
Args: |
791
|
|
|
con (Console): Any Console instance. |
792
|
|
|
x (int): Character x position from the left. |
793
|
|
|
y (int): Character y position from the top. |
794
|
|
|
c (Union[int, AnyStr]): Character to draw, can be an integer or string. |
795
|
|
|
fore (Union[Tuple[int, int, int], Sequence[int]]): |
796
|
|
|
An (r, g, b) sequence or Color instance. |
797
|
|
|
back (Union[Tuple[int, int, int], Sequence[int]]): |
798
|
|
|
An (r, g, b) sequence or Color instance. |
799
|
|
|
""" |
800
|
|
|
lib.TCOD_console_put_char_ex(_console(con), x, y, _int(c), fore, back) |
801
|
|
|
|
802
|
|
|
def console_set_char_background(con, x, y, col, flag=BKGND_SET): |
|
|
|
|
803
|
|
|
"""Change the background color of x,y to col using a blend mode. |
804
|
|
|
|
805
|
|
|
Args: |
806
|
|
|
con (Console): Any Console instance. |
807
|
|
|
x (int): Character x position from the left. |
808
|
|
|
y (int): Character y position from the top. |
809
|
|
|
col (Union[Tuple[int, int, int], Sequence[int]]): |
810
|
|
|
An (r, g, b) sequence or Color instance. |
811
|
|
|
flag (int): Blending mode to use, defaults to BKGND_SET. |
812
|
|
|
""" |
813
|
|
|
lib.TCOD_console_set_char_background(_console(con), x, y, col, flag) |
814
|
|
|
|
815
|
|
|
def console_set_char_foreground(con, x, y, col): |
|
|
|
|
816
|
|
|
"""Change the foreground color of x,y to col. |
817
|
|
|
|
818
|
|
|
Args: |
819
|
|
|
con (Console): Any Console instance. |
820
|
|
|
x (int): Character x position from the left. |
821
|
|
|
y (int): Character y position from the top. |
822
|
|
|
col (Union[Tuple[int, int, int], Sequence[int]]): |
823
|
|
|
An (r, g, b) sequence or Color instance. |
824
|
|
|
""" |
825
|
|
|
lib.TCOD_console_set_char_foreground(_console(con), x, y, col) |
826
|
|
|
|
827
|
|
|
def console_set_char(con, x, y, c): |
|
|
|
|
828
|
|
|
"""Change the character at x,y to c, keeping the current colors. |
829
|
|
|
|
830
|
|
|
Args: |
831
|
|
|
con (Console): Any Console instance. |
832
|
|
|
x (int): Character x position from the left. |
833
|
|
|
y (int): Character y position from the top. |
834
|
|
|
c (Union[int, AnyStr]): Character to draw, can be an integer or string. |
835
|
|
|
""" |
836
|
|
|
lib.TCOD_console_set_char(_console(con), x, y, _int(c)) |
837
|
|
|
|
838
|
|
|
def console_set_background_flag(con, flag): |
839
|
|
|
"""Change the default blend mode for this console. |
840
|
|
|
|
841
|
|
|
Args: |
842
|
|
|
con (Console): Any Console instance. |
843
|
|
|
flag (int): Blend mode to use by default. |
844
|
|
|
""" |
845
|
|
|
lib.TCOD_console_set_background_flag(_console(con), flag) |
846
|
|
|
|
847
|
|
|
def console_get_background_flag(con): |
848
|
|
|
"""Return this consoles current blend mode. |
849
|
|
|
|
850
|
|
|
Args: |
851
|
|
|
con (Console): Any Console instance. |
852
|
|
|
""" |
853
|
|
|
return lib.TCOD_console_get_background_flag(_console(con)) |
854
|
|
|
|
855
|
|
|
def console_set_alignment(con, alignment): |
856
|
|
|
"""Change this consoles current alignment mode. |
857
|
|
|
|
858
|
|
|
* tcod.LEFT |
859
|
|
|
* tcod.CENTER |
860
|
|
|
* tcod.RIGHT |
861
|
|
|
|
862
|
|
|
Args: |
863
|
|
|
con (Console): Any Console instance. |
864
|
|
|
alignment (int): |
865
|
|
|
""" |
866
|
|
|
lib.TCOD_console_set_alignment(_console(con), alignment) |
867
|
|
|
|
868
|
|
|
def console_get_alignment(con): |
869
|
|
|
"""Return this consoles current alignment mode. |
870
|
|
|
|
871
|
|
|
Args: |
872
|
|
|
con (Console): Any Console instance. |
873
|
|
|
""" |
874
|
|
|
return lib.TCOD_console_get_alignment(_console(con)) |
875
|
|
|
|
876
|
|
|
def console_print(con, x, y, fmt): |
|
|
|
|
877
|
|
|
"""Print a color formatted string on a console. |
878
|
|
|
|
879
|
|
|
Args: |
880
|
|
|
con (Console): Any Console instance. |
881
|
|
|
x (int): Character x position from the left. |
882
|
|
|
y (int): Character y position from the top. |
883
|
|
|
fmt (AnyStr): A unicode or bytes string optionaly using color codes. |
884
|
|
|
""" |
885
|
|
|
lib.TCOD_console_print_utf(_console(con), x, y, _fmt_unicode(fmt)) |
886
|
|
|
|
887
|
|
|
def console_print_ex(con, x, y, flag, alignment, fmt): |
|
|
|
|
888
|
|
|
"""Print a string on a console using a blend mode and alignment mode. |
889
|
|
|
|
890
|
|
|
Args: |
891
|
|
|
con (Console): Any Console instance. |
892
|
|
|
x (int): Character x position from the left. |
893
|
|
|
y (int): Character y position from the top. |
894
|
|
|
""" |
895
|
|
|
lib.TCOD_console_print_ex_utf(_console(con), |
896
|
|
|
x, y, flag, alignment, _fmt_unicode(fmt)) |
897
|
|
|
|
898
|
|
|
def console_print_rect(con, x, y, w, h, fmt): |
|
|
|
|
899
|
|
|
"""Print a string constrained to a rectangle. |
900
|
|
|
|
901
|
|
|
If h > 0 and the bottom of the rectangle is reached, |
902
|
|
|
the string is truncated. If h = 0, |
903
|
|
|
the string is only truncated if it reaches the bottom of the console. |
904
|
|
|
|
905
|
|
|
|
906
|
|
|
|
907
|
|
|
Returns: |
908
|
|
|
int: The number of lines of text once word-wrapped. |
909
|
|
|
""" |
910
|
|
|
return lib.TCOD_console_print_rect_utf( |
911
|
|
|
_console(con), x, y, w, h, _fmt_unicode(fmt)) |
912
|
|
|
|
913
|
|
|
def console_print_rect_ex(con, x, y, w, h, flag, alignment, fmt): |
|
|
|
|
914
|
|
|
"""Print a string constrained to a rectangle with blend and alignment. |
915
|
|
|
|
916
|
|
|
Returns: |
917
|
|
|
int: The number of lines of text once word-wrapped. |
918
|
|
|
""" |
919
|
|
|
return lib.TCOD_console_print_rect_ex_utf( |
920
|
|
|
_console(con), x, y, w, h, flag, alignment, _fmt_unicode(fmt)) |
921
|
|
|
|
922
|
|
|
def console_get_height_rect(con, x, y, w, h, fmt): |
|
|
|
|
923
|
|
|
"""Return the height of this text once word-wrapped into this rectangle. |
924
|
|
|
|
925
|
|
|
Returns: |
926
|
|
|
int: The number of lines of text once word-wrapped. |
927
|
|
|
""" |
928
|
|
|
return lib.TCOD_console_get_height_rect_utf( |
929
|
|
|
_console(con), x, y, w, h, _fmt_unicode(fmt)) |
930
|
|
|
|
931
|
|
|
def console_rect(con, x, y, w, h, clr, flag=BKGND_DEFAULT): |
|
|
|
|
932
|
|
|
"""Draw a the background color on a rect optionally clearing the text. |
933
|
|
|
|
934
|
|
|
If clr is True the affected tiles are changed to space character. |
935
|
|
|
""" |
936
|
|
|
lib.TCOD_console_rect(_console(con), x, y, w, h, clr, flag) |
937
|
|
|
|
938
|
|
|
def console_hline(con, x, y, l, flag=BKGND_DEFAULT): |
|
|
|
|
939
|
|
|
"""Draw a horizontal line on the console. |
940
|
|
|
|
941
|
|
|
This always uses the character 196, the horizontal line character. |
942
|
|
|
""" |
943
|
|
|
lib.TCOD_console_hline(_console(con), x, y, l, flag) |
944
|
|
|
|
945
|
|
|
def console_vline(con, x, y, l, flag=BKGND_DEFAULT): |
|
|
|
|
946
|
|
|
"""Draw a vertical line on the console. |
947
|
|
|
|
948
|
|
|
This always uses the character 179, the vertical line character. |
949
|
|
|
""" |
950
|
|
|
lib.TCOD_console_vline(_console(con), x, y, l, flag) |
951
|
|
|
|
952
|
|
|
def console_print_frame(con, x, y, w, h, clear=True, flag=BKGND_DEFAULT, fmt=b''): |
|
|
|
|
953
|
|
|
"""Draw a framed rectangle with optinal text. |
954
|
|
|
|
955
|
|
|
This uses the default background color and blend mode to fill the |
956
|
|
|
rectangle and the default foreground to draw the outline. |
957
|
|
|
|
958
|
|
|
fmt will be printed on the inside of the rectangle, word-wrapped. |
959
|
|
|
""" |
960
|
|
|
lib.TCOD_console_print_frame( |
961
|
|
|
_console(con), x, y, w, h, clear, flag, _fmt_bytes(fmt)) |
962
|
|
|
|
963
|
|
|
def console_set_color_control(con, fore, back): |
964
|
|
|
"""Configure :any:`color controls`. |
965
|
|
|
|
966
|
|
|
Args: |
967
|
|
|
con (int): :any:`Color control` constant to modify. |
968
|
|
|
fore (Union[Tuple[int, int, int], Sequence[int]]): |
969
|
|
|
An (r, g, b) sequence or Color instance. |
970
|
|
|
back (Union[Tuple[int, int, int], Sequence[int]]): |
971
|
|
|
An (r, g, b) sequence or Color instance. |
972
|
|
|
""" |
973
|
|
|
lib.TCOD_console_set_color_control(con, fore, back) |
974
|
|
|
|
975
|
|
|
def console_get_default_background(con): |
976
|
|
|
"""Return this consoles default background color.""" |
977
|
|
|
return Color._new_from_cdata( |
978
|
|
|
lib.TCOD_console_get_default_background(_console(con))) |
979
|
|
|
|
980
|
|
|
def console_get_default_foreground(con): |
981
|
|
|
"""Return this consoles default foreground color.""" |
982
|
|
|
return Color._new_from_cdata( |
983
|
|
|
lib.TCOD_console_get_default_foreground(_console(con))) |
984
|
|
|
|
985
|
|
|
def console_get_char_background(con, x, y): |
|
|
|
|
986
|
|
|
"""Return the background color at the x,y of this console.""" |
987
|
|
|
return Color._new_from_cdata( |
988
|
|
|
lib.TCOD_console_get_char_background(_console(con), x, y)) |
989
|
|
|
|
990
|
|
|
def console_get_char_foreground(con, x, y): |
|
|
|
|
991
|
|
|
"""Return the foreground color at the x,y of this console.""" |
992
|
|
|
return Color._new_from_cdata( |
993
|
|
|
lib.TCOD_console_get_char_foreground(_console(con), x, y)) |
994
|
|
|
|
995
|
|
|
def console_get_char(con, x, y): |
|
|
|
|
996
|
|
|
"""Return the character at the x,y of this console.""" |
997
|
|
|
return lib.TCOD_console_get_char(_console(con), x, y) |
998
|
|
|
|
999
|
|
|
def console_set_fade(fade, fadingColor): |
|
|
|
|
1000
|
|
|
lib.TCOD_console_set_fade(fade, fadingColor) |
1001
|
|
|
|
1002
|
|
|
def console_get_fade(): |
|
|
|
|
1003
|
|
|
return lib.TCOD_console_get_fade() |
1004
|
|
|
|
1005
|
|
|
def console_get_fading_color(): |
|
|
|
|
1006
|
|
|
return Color._new_from_cdata(lib.TCOD_console_get_fading_color()) |
1007
|
|
|
|
1008
|
|
|
# handling keyboard input |
1009
|
|
|
def console_wait_for_keypress(flush): |
1010
|
|
|
"""Block until the user presses a key, then returns a new Key. |
1011
|
|
|
|
1012
|
|
|
Args: |
1013
|
|
|
flush bool: If True then the event queue is cleared before waiting |
1014
|
|
|
for the next event. |
1015
|
|
|
|
1016
|
|
|
Returns: |
1017
|
|
|
Key: A new Key instance. |
1018
|
|
|
""" |
1019
|
|
|
k=Key() |
|
|
|
|
1020
|
|
|
lib.TCOD_console_wait_for_keypress_wrapper(k.cdata, flush) |
1021
|
|
|
return k |
1022
|
|
|
|
1023
|
|
|
def console_check_for_keypress(flags=KEY_RELEASED): |
|
|
|
|
1024
|
|
|
k=Key() |
|
|
|
|
1025
|
|
|
lib.TCOD_console_check_for_keypress_wrapper(k.cdata, flags) |
1026
|
|
|
return k |
1027
|
|
|
|
1028
|
|
|
def console_is_key_pressed(key): |
|
|
|
|
1029
|
|
|
return lib.TCOD_console_is_key_pressed(key) |
1030
|
|
|
|
1031
|
|
|
# using offscreen consoles |
1032
|
|
|
def console_new(w, h): |
|
|
|
|
1033
|
|
|
"""Return an offscreen console of size: w,h.""" |
1034
|
|
|
return tcod.console.Console(w, h) |
1035
|
|
|
|
1036
|
|
|
def console_from_file(filename): |
1037
|
|
|
"""Return a new console object from a filename. |
1038
|
|
|
|
1039
|
|
|
The file format is automactially determined. This can load REXPaint `.xp`, |
1040
|
|
|
ASCII Paint `.apf`, or Non-delimited ASCII `.asc` files. |
1041
|
|
|
|
1042
|
|
|
Args: |
1043
|
|
|
filename (Text): The path to the file, as a string. |
1044
|
|
|
|
1045
|
|
|
Returns: A new :any`Console` instance. |
1046
|
|
|
""" |
1047
|
|
|
return tcod.console.Console._from_cdata( |
1048
|
|
|
lib.TCOD_console_from_file(filename.encode('utf-8'))) |
1049
|
|
|
|
1050
|
|
|
def console_blit(src, x, y, w, h, dst, xdst, ydst, ffade=1.0,bfade=1.0): |
|
|
|
|
1051
|
|
|
"""Blit the console src from x,y,w,h to console dst at xdst,ydst.""" |
1052
|
|
|
lib.TCOD_console_blit( |
1053
|
|
|
_console(src), x, y, w, h, |
1054
|
|
|
_console(dst), xdst, ydst, ffade, bfade) |
1055
|
|
|
|
1056
|
|
|
def console_set_key_color(con, col): |
1057
|
|
|
"""Set a consoles blit transparent color.""" |
1058
|
|
|
lib.TCOD_console_set_key_color(_console(con), col) |
1059
|
|
|
if hasattr(con, 'set_key_color'): |
1060
|
|
|
con.set_key_color(col) |
1061
|
|
|
|
1062
|
|
|
def console_delete(con): |
|
|
|
|
1063
|
|
|
con = _console(con) |
1064
|
|
|
if con == ffi.NULL: |
1065
|
|
|
lib.TCOD_console_delete(con) |
1066
|
|
|
|
1067
|
|
|
# fast color filling |
1068
|
|
View Code Duplication |
def console_fill_foreground(con, r, g, b): |
|
|
|
|
1069
|
|
|
"""Fill the foregound of a console with r,g,b. |
1070
|
|
|
|
1071
|
|
|
Args: |
1072
|
|
|
con (Console): Any Console instance. |
1073
|
|
|
r (Sequence[int]): An array of integers with a length of width*height. |
1074
|
|
|
g (Sequence[int]): An array of integers with a length of width*height. |
1075
|
|
|
b (Sequence[int]): An array of integers with a length of width*height. |
1076
|
|
|
""" |
1077
|
|
|
if len(r) != len(g) or len(r) != len(b): |
1078
|
|
|
raise TypeError('R, G and B must all have the same size.') |
1079
|
|
|
if (isinstance(r, _np.ndarray) and isinstance(g, _np.ndarray) and |
1080
|
|
|
isinstance(b, _np.ndarray)): |
1081
|
|
|
#numpy arrays, use numpy's ctypes functions |
1082
|
|
|
r = _np.ascontiguousarray(r, dtype=_np.intc) |
1083
|
|
|
g = _np.ascontiguousarray(g, dtype=_np.intc) |
1084
|
|
|
b = _np.ascontiguousarray(b, dtype=_np.intc) |
1085
|
|
|
cr = ffi.cast('int *', r.ctypes.data) |
|
|
|
|
1086
|
|
|
cg = ffi.cast('int *', g.ctypes.data) |
|
|
|
|
1087
|
|
|
cb = ffi.cast('int *', b.ctypes.data) |
|
|
|
|
1088
|
|
|
else: |
1089
|
|
|
# otherwise convert using ffi arrays |
1090
|
|
|
cr = ffi.new('int[]', r) |
|
|
|
|
1091
|
|
|
cg = ffi.new('int[]', g) |
|
|
|
|
1092
|
|
|
cb = ffi.new('int[]', b) |
|
|
|
|
1093
|
|
|
|
1094
|
|
|
lib.TCOD_console_fill_foreground(_console(con), cr, cg, cb) |
1095
|
|
|
|
1096
|
|
View Code Duplication |
def console_fill_background(con, r, g, b): |
|
|
|
|
1097
|
|
|
"""Fill the backgound of a console with r,g,b. |
1098
|
|
|
|
1099
|
|
|
Args: |
1100
|
|
|
con (Console): Any Console instance. |
1101
|
|
|
r (Sequence[int]): An array of integers with a length of width*height. |
1102
|
|
|
g (Sequence[int]): An array of integers with a length of width*height. |
1103
|
|
|
b (Sequence[int]): An array of integers with a length of width*height. |
1104
|
|
|
""" |
1105
|
|
|
if len(r) != len(g) or len(r) != len(b): |
1106
|
|
|
raise TypeError('R, G and B must all have the same size.') |
1107
|
|
|
if (isinstance(r, _np.ndarray) and isinstance(g, _np.ndarray) and |
1108
|
|
|
isinstance(b, _np.ndarray)): |
1109
|
|
|
#numpy arrays, use numpy's ctypes functions |
1110
|
|
|
r = _np.ascontiguousarray(r, dtype=_np.intc) |
1111
|
|
|
g = _np.ascontiguousarray(g, dtype=_np.intc) |
1112
|
|
|
b = _np.ascontiguousarray(b, dtype=_np.intc) |
1113
|
|
|
cr = ffi.cast('int *', r.ctypes.data) |
|
|
|
|
1114
|
|
|
cg = ffi.cast('int *', g.ctypes.data) |
|
|
|
|
1115
|
|
|
cb = ffi.cast('int *', b.ctypes.data) |
|
|
|
|
1116
|
|
|
else: |
1117
|
|
|
# otherwise convert using ffi arrays |
1118
|
|
|
cr = ffi.new('int[]', r) |
|
|
|
|
1119
|
|
|
cg = ffi.new('int[]', g) |
|
|
|
|
1120
|
|
|
cb = ffi.new('int[]', b) |
|
|
|
|
1121
|
|
|
|
1122
|
|
|
lib.TCOD_console_fill_background(_console(con), cr, cg, cb) |
1123
|
|
|
|
1124
|
|
|
def console_fill_char(con,arr): |
|
|
|
|
1125
|
|
|
"""Fill the character tiles of a console with an array. |
1126
|
|
|
|
1127
|
|
|
Args: |
1128
|
|
|
con (Console): Any Console instance. |
1129
|
|
|
arr (Sequence[int]): An array of integers with a length of width*height. |
1130
|
|
|
""" |
1131
|
|
|
if isinstance(arr, _np.ndarray): |
1132
|
|
|
#numpy arrays, use numpy's ctypes functions |
1133
|
|
|
arr = _np.ascontiguousarray(arr, dtype=_np.intc) |
1134
|
|
|
carr = ffi.cast('int *', arr.ctypes.data) |
1135
|
|
|
else: |
1136
|
|
|
#otherwise convert using the ffi module |
1137
|
|
|
carr = ffi.new('int[]', arr) |
1138
|
|
|
|
1139
|
|
|
lib.TCOD_console_fill_char(_console(con), carr) |
1140
|
|
|
|
1141
|
|
|
def console_load_asc(con, filename): |
1142
|
|
|
"""Update a console from a non-delimited ASCII `.asc` file.""" |
1143
|
|
|
return lib.TCOD_console_load_asc(_console(con), filename.encode('utf-8')) |
1144
|
|
|
|
1145
|
|
|
def console_save_asc(con, filename): |
1146
|
|
|
"""Save a console to a non-delimited ASCII `.asc` file.""" |
1147
|
|
|
return lib.TCOD_console_save_asc(_console(con), filename.encode('utf-8')) |
1148
|
|
|
|
1149
|
|
|
def console_load_apf(con, filename): |
1150
|
|
|
"""Update a console from an ASCII Paint `.apf` file.""" |
1151
|
|
|
return lib.TCOD_console_load_apf(_console(con), filename.encode('utf-8')) |
1152
|
|
|
|
1153
|
|
|
def console_save_apf(con, filename): |
1154
|
|
|
"""Save a console to an ASCII Paint `.apf` file.""" |
1155
|
|
|
return lib.TCOD_console_save_apf(_console(con), filename.encode('utf-8')) |
1156
|
|
|
|
1157
|
|
|
def console_load_xp(con, filename): |
1158
|
|
|
"""Update a console from a REXPaint `.xp` file.""" |
1159
|
|
|
return lib.TCOD_console_load_xp(_console(con), filename.encode('utf-8')) |
1160
|
|
|
|
1161
|
|
|
def console_save_xp(con, filename, compress_level=9): |
1162
|
|
|
"""Save a console to a REXPaint `.xp` file.""" |
1163
|
|
|
return lib.TCOD_console_save_xp( |
1164
|
|
|
_console(con), |
1165
|
|
|
filename.encode('utf-8'), |
1166
|
|
|
compress_level, |
1167
|
|
|
) |
1168
|
|
|
|
1169
|
|
|
def console_from_xp(filename): |
1170
|
|
|
"""Return a single console from a REXPaint `.xp` file.""" |
1171
|
|
|
return tcod.console.Console._from_cdata( |
1172
|
|
|
lib.TCOD_console_from_xp(filename.encode('utf-8'))) |
1173
|
|
|
|
1174
|
|
|
def console_list_load_xp(filename): |
1175
|
|
|
"""Return a list of consoles from a REXPaint `.xp` file.""" |
1176
|
|
|
tcod_list = lib.TCOD_console_list_from_xp(filename.encode('utf-8')) |
1177
|
|
|
if tcod_list == ffi.NULL: |
1178
|
|
|
return None |
1179
|
|
|
try: |
1180
|
|
|
python_list = [] |
1181
|
|
|
lib.TCOD_list_reverse(tcod_list) |
1182
|
|
|
while not lib.TCOD_list_is_empty(tcod_list): |
1183
|
|
|
python_list.append( |
1184
|
|
|
tcod.console.Console._from_cdata(lib.TCOD_list_pop(tcod_list)), |
1185
|
|
|
) |
1186
|
|
|
return python_list |
1187
|
|
|
finally: |
1188
|
|
|
lib.TCOD_list_delete(tcod_list) |
1189
|
|
|
|
1190
|
|
|
def console_list_save_xp(console_list, filename, compress_level=9): |
1191
|
|
|
"""Save a list of consoles to a REXPaint `.xp` file.""" |
1192
|
|
|
tcod_list = lib.TCOD_list_new() |
1193
|
|
|
try: |
1194
|
|
|
for console in console_list: |
1195
|
|
|
lib.TCOD_list_push(tcod_list, _console(console)) |
1196
|
|
|
return lib.TCOD_console_list_save_xp( |
1197
|
|
|
tcod_list, filename.encode('utf-8'), compress_level |
1198
|
|
|
) |
1199
|
|
|
finally: |
1200
|
|
|
lib.TCOD_list_delete(tcod_list) |
1201
|
|
|
|
1202
|
|
|
def path_new_using_map(m, dcost=1.41): |
|
|
|
|
1203
|
|
|
"""Return a new AStar using the given Map. |
1204
|
|
|
|
1205
|
|
|
Args: |
1206
|
|
|
m (Map): A Map instance. |
1207
|
|
|
dcost (float): The path-finding cost of diagonal movement. |
1208
|
|
|
Can be set to 0 to disable diagonal movement. |
1209
|
|
|
Returns: |
1210
|
|
|
AStar: A new AStar instance. |
1211
|
|
|
""" |
1212
|
|
|
return tcod.path.AStar(m, dcost) |
1213
|
|
|
|
1214
|
|
|
def path_new_using_function(w, h, func, userData=0, dcost=1.41): |
|
|
|
|
1215
|
|
|
"""Return a new AStar using the given callable function. |
1216
|
|
|
|
1217
|
|
|
Args: |
1218
|
|
|
w (int): Clipping width. |
1219
|
|
|
h (int): Clipping height. |
1220
|
|
|
func (Callable[[int, int, int, int, Any], float]): |
1221
|
|
|
userData (Any): |
1222
|
|
|
dcost (float): A multiplier for the cost of diagonal movement. |
1223
|
|
|
Can be set to 0 to disable diagonal movement. |
1224
|
|
|
Returns: |
1225
|
|
|
AStar: A new AStar instance. |
1226
|
|
|
""" |
1227
|
|
|
return tcod.path.AStar( |
1228
|
|
|
tcod.path._EdgeCostFunc((func, userData), w, h), |
1229
|
|
|
dcost, |
1230
|
|
|
) |
1231
|
|
|
|
1232
|
|
|
def path_compute(p, ox, oy, dx, dy): |
|
|
|
|
1233
|
|
|
"""Find a path from (ox, oy) to (dx, dy). Return True if path is found. |
1234
|
|
|
|
1235
|
|
|
Args: |
1236
|
|
|
p (AStar): An AStar instance. |
1237
|
|
|
ox (int): Starting x position. |
1238
|
|
|
oy (int): Starting y position. |
1239
|
|
|
dx (int): Destination x position. |
1240
|
|
|
dy (int): Destination y position. |
1241
|
|
|
Returns: |
1242
|
|
|
bool: True if a valid path was found. Otherwise False. |
1243
|
|
|
""" |
1244
|
|
|
return lib.TCOD_path_compute(p._path_c, ox, oy, dx, dy) |
1245
|
|
|
|
1246
|
|
|
def path_get_origin(p): |
|
|
|
|
1247
|
|
|
"""Get the current origin position. |
1248
|
|
|
|
1249
|
|
|
This point moves when :any:`path_walk` returns the next x,y step. |
1250
|
|
|
|
1251
|
|
|
Args: |
1252
|
|
|
p (AStar): An AStar instance. |
1253
|
|
|
Returns: |
1254
|
|
|
Tuple[int, int]: An (x, y) point. |
1255
|
|
|
""" |
1256
|
|
|
x = ffi.new('int *') |
|
|
|
|
1257
|
|
|
y = ffi.new('int *') |
|
|
|
|
1258
|
|
|
lib.TCOD_path_get_origin(p._path_c, x, y) |
1259
|
|
|
return x[0], y[0] |
1260
|
|
|
|
1261
|
|
|
def path_get_destination(p): |
|
|
|
|
1262
|
|
|
"""Get the current destination position. |
1263
|
|
|
|
1264
|
|
|
Args: |
1265
|
|
|
p (AStar): An AStar instance. |
1266
|
|
|
Returns: |
1267
|
|
|
Tuple[int, int]: An (x, y) point. |
1268
|
|
|
""" |
1269
|
|
|
x = ffi.new('int *') |
|
|
|
|
1270
|
|
|
y = ffi.new('int *') |
|
|
|
|
1271
|
|
|
lib.TCOD_path_get_destination(p._path_c, x, y) |
1272
|
|
|
return x[0], y[0] |
1273
|
|
|
|
1274
|
|
|
def path_size(p): |
|
|
|
|
1275
|
|
|
"""Return the current length of the computed path. |
1276
|
|
|
|
1277
|
|
|
Args: |
1278
|
|
|
p (AStar): An AStar instance. |
1279
|
|
|
Returns: |
1280
|
|
|
int: Length of the path. |
1281
|
|
|
""" |
1282
|
|
|
return lib.TCOD_path_size(p._path_c) |
1283
|
|
|
|
1284
|
|
|
def path_reverse(p): |
|
|
|
|
1285
|
|
|
"""Reverse the direction of a path. |
1286
|
|
|
|
1287
|
|
|
This effectively swaps the origin and destination points. |
1288
|
|
|
|
1289
|
|
|
Args: |
1290
|
|
|
p (AStar): An AStar instance. |
1291
|
|
|
""" |
1292
|
|
|
lib.TCOD_path_reverse(p._path_c) |
1293
|
|
|
|
1294
|
|
|
def path_get(p, idx): |
|
|
|
|
1295
|
|
|
"""Get a point on a path. |
1296
|
|
|
|
1297
|
|
|
Args: |
1298
|
|
|
p (AStar): An AStar instance. |
1299
|
|
|
idx (int): Should be in range: 0 <= inx < :any:`path_size` |
1300
|
|
|
""" |
1301
|
|
|
x = ffi.new('int *') |
|
|
|
|
1302
|
|
|
y = ffi.new('int *') |
|
|
|
|
1303
|
|
|
lib.TCOD_path_get(p._path_c, idx, x, y) |
1304
|
|
|
return x[0], y[0] |
1305
|
|
|
|
1306
|
|
|
def path_is_empty(p): |
|
|
|
|
1307
|
|
|
"""Return True if a path is empty. |
1308
|
|
|
|
1309
|
|
|
Args: |
1310
|
|
|
p (AStar): An AStar instance. |
1311
|
|
|
Returns: |
1312
|
|
|
bool: True if a path is empty. Otherwise False. |
1313
|
|
|
""" |
1314
|
|
|
return lib.TCOD_path_is_empty(p._path_c) |
1315
|
|
|
|
1316
|
|
|
def path_walk(p, recompute): |
|
|
|
|
1317
|
|
|
"""Return the next (x, y) point in a path, or (None, None) if it's empty. |
1318
|
|
|
|
1319
|
|
|
When ``recompute`` is True and a previously valid path reaches a point |
1320
|
|
|
where it is now blocked, a new path will automatically be found. |
1321
|
|
|
|
1322
|
|
|
Args: |
1323
|
|
|
p (AStar): An AStar instance. |
1324
|
|
|
recompute (bool): Recompute the path automatically. |
1325
|
|
|
Returns: |
1326
|
|
|
Union[Tuple[int, int], Tuple[None, None]]: |
1327
|
|
|
A single (x, y) point, or (None, None) |
1328
|
|
|
""" |
1329
|
|
|
x = ffi.new('int *') |
|
|
|
|
1330
|
|
|
y = ffi.new('int *') |
|
|
|
|
1331
|
|
|
if lib.TCOD_path_walk(p._path_c, x, y, recompute): |
1332
|
|
|
return x[0], y[0] |
1333
|
|
|
return None,None |
|
|
|
|
1334
|
|
|
|
1335
|
|
|
def path_delete(p): |
|
|
|
|
1336
|
|
|
"""Does nothing.""" |
1337
|
|
|
pass |
1338
|
|
|
|
1339
|
|
|
def dijkstra_new(m, dcost=1.41): |
|
|
|
|
1340
|
|
|
return tcod.path.Dijkstra(m, dcost) |
1341
|
|
|
|
1342
|
|
|
def dijkstra_new_using_function(w, h, func, userData=0, dcost=1.41): |
|
|
|
|
1343
|
|
|
return tcod.path.Dijkstra( |
1344
|
|
|
tcod.path._EdgeCostFunc((func, userData), w, h), |
1345
|
|
|
dcost, |
1346
|
|
|
) |
1347
|
|
|
|
1348
|
|
|
def dijkstra_compute(p, ox, oy): |
|
|
|
|
1349
|
|
|
lib.TCOD_dijkstra_compute(p._path_c, ox, oy) |
1350
|
|
|
|
1351
|
|
|
def dijkstra_path_set(p, x, y): |
|
|
|
|
1352
|
|
|
return lib.TCOD_dijkstra_path_set(p._path_c, x, y) |
1353
|
|
|
|
1354
|
|
|
def dijkstra_get_distance(p, x, y): |
|
|
|
|
1355
|
|
|
return lib.TCOD_dijkstra_get_distance(p._path_c, x, y) |
1356
|
|
|
|
1357
|
|
|
def dijkstra_size(p): |
|
|
|
|
1358
|
|
|
return lib.TCOD_dijkstra_size(p._path_c) |
1359
|
|
|
|
1360
|
|
|
def dijkstra_reverse(p): |
|
|
|
|
1361
|
|
|
lib.TCOD_dijkstra_reverse(p._path_c) |
1362
|
|
|
|
1363
|
|
|
def dijkstra_get(p, idx): |
|
|
|
|
1364
|
|
|
x = ffi.new('int *') |
|
|
|
|
1365
|
|
|
y = ffi.new('int *') |
|
|
|
|
1366
|
|
|
lib.TCOD_dijkstra_get(p._path_c, idx, x, y) |
1367
|
|
|
return x[0], y[0] |
1368
|
|
|
|
1369
|
|
|
def dijkstra_is_empty(p): |
|
|
|
|
1370
|
|
|
return lib.TCOD_dijkstra_is_empty(p._path_c) |
1371
|
|
|
|
1372
|
|
|
def dijkstra_path_walk(p): |
|
|
|
|
1373
|
|
|
x = ffi.new('int *') |
|
|
|
|
1374
|
|
|
y = ffi.new('int *') |
|
|
|
|
1375
|
|
|
if lib.TCOD_dijkstra_path_walk(p._path_c, x, y): |
1376
|
|
|
return x[0], y[0] |
1377
|
|
|
return None,None |
|
|
|
|
1378
|
|
|
|
1379
|
|
|
def dijkstra_delete(p): |
|
|
|
|
1380
|
|
|
pass |
1381
|
|
|
|
1382
|
|
|
def _heightmap_cdata(array): |
1383
|
|
|
"""Return a new TCOD_heightmap_t instance using an array. |
1384
|
|
|
|
1385
|
|
|
Formatting is verified during this function. |
1386
|
|
|
""" |
1387
|
|
|
if not array.flags['C_CONTIGUOUS']: |
1388
|
|
|
raise ValueError('array must be a C-style contiguous segment.') |
1389
|
|
|
if array.dtype != _np.float32: |
1390
|
|
|
raise ValueError('array dtype must be float32, not %r' % array.dtype) |
1391
|
|
|
width, height = array.shape |
1392
|
|
|
pointer = ffi.cast('float *', array.ctypes.data) |
1393
|
|
|
return ffi.new('TCOD_heightmap_t *', (width, height, pointer)) |
1394
|
|
|
|
1395
|
|
|
def heightmap_new(w, h): |
|
|
|
|
1396
|
|
|
"""Return a new numpy.ndarray formatted for use with heightmap functions. |
1397
|
|
|
|
1398
|
|
|
You can pass a numpy array to any heightmap function as long as all the |
1399
|
|
|
following are true:: |
1400
|
|
|
* The array is 2 dimentional. |
1401
|
|
|
* The array has the C_CONTIGUOUS flag. |
1402
|
|
|
* The array's dtype is :any:`dtype.float32`. |
1403
|
|
|
|
1404
|
|
|
Args: |
1405
|
|
|
w (int): The width of the new HeightMap. |
1406
|
|
|
h (int): The height of the new HeightMap. |
1407
|
|
|
|
1408
|
|
|
Returns: |
1409
|
|
|
numpy.ndarray: A C-contiguous mapping of float32 values. |
1410
|
|
|
""" |
1411
|
|
|
return _np.ndarray((h, w), _np.float32) |
1412
|
|
|
|
1413
|
|
|
def heightmap_set_value(hm, x, y, value): |
|
|
|
|
1414
|
|
|
"""Set the value of a point on a heightmap. |
1415
|
|
|
|
1416
|
|
|
Args: |
1417
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1418
|
|
|
x (int): The x position to change. |
1419
|
|
|
y (int): The y position to change. |
1420
|
|
|
value (float): The value to set. |
1421
|
|
|
|
1422
|
|
|
.. deprecated:: 2.0 |
1423
|
|
|
Do ``hm[y, x] = value`` instead. |
1424
|
|
|
""" |
1425
|
|
|
hm[y, x] = value |
1426
|
|
|
|
1427
|
|
|
def heightmap_add(hm, value): |
|
|
|
|
1428
|
|
|
"""Add value to all values on this heightmap. |
1429
|
|
|
|
1430
|
|
|
Args: |
1431
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1432
|
|
|
value (float): A number to add to this heightmap. |
1433
|
|
|
|
1434
|
|
|
.. deprecated:: 2.0 |
1435
|
|
|
Do ``hm[:] += value`` instead. |
1436
|
|
|
""" |
1437
|
|
|
hm[:] += value |
1438
|
|
|
|
1439
|
|
|
def heightmap_scale(hm, value): |
|
|
|
|
1440
|
|
|
"""Multiply all items on this heightmap by value. |
1441
|
|
|
|
1442
|
|
|
Args: |
1443
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1444
|
|
|
value (float): A number to scale this heightmap by. |
1445
|
|
|
|
1446
|
|
|
.. deprecated:: 2.0 |
1447
|
|
|
Do ``hm[:] *= value`` instead. |
1448
|
|
|
""" |
1449
|
|
|
hm[:] *= value |
1450
|
|
|
|
1451
|
|
|
def heightmap_clear(hm): |
|
|
|
|
1452
|
|
|
"""Add value to all values on this heightmap. |
1453
|
|
|
|
1454
|
|
|
Args: |
1455
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1456
|
|
|
|
1457
|
|
|
.. deprecated:: 2.0 |
1458
|
|
|
Do ``hm.array[:] = 0`` instead. |
1459
|
|
|
""" |
1460
|
|
|
hm[:] = 0 |
1461
|
|
|
|
1462
|
|
|
def heightmap_clamp(hm, mi, ma): |
|
|
|
|
1463
|
|
|
"""Clamp all values on this heightmap between ``mi`` and ``ma`` |
1464
|
|
|
|
1465
|
|
|
Args: |
1466
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1467
|
|
|
mi (float): The lower bound to clamp to. |
1468
|
|
|
ma (float): The upper bound to clamp to. |
1469
|
|
|
|
1470
|
|
|
.. deprecated:: 2.0 |
1471
|
|
|
Do ``hm.clip(mi, ma)`` instead. |
1472
|
|
|
""" |
1473
|
|
|
hm.clip(mi, ma) |
1474
|
|
|
|
1475
|
|
|
def heightmap_copy(hm1, hm2): |
1476
|
|
|
"""Copy the heightmap ``hm1`` to ``hm2``. |
1477
|
|
|
|
1478
|
|
|
Args: |
1479
|
|
|
hm1 (numpy.ndarray): The source heightmap. |
1480
|
|
|
hm2 (numpy.ndarray): The destination heightmap. |
1481
|
|
|
|
1482
|
|
|
.. deprecated:: 2.0 |
1483
|
|
|
Do ``hm2[:] = hm1[:]`` instead. |
1484
|
|
|
""" |
1485
|
|
|
hm2[:] = hm1[:] |
1486
|
|
|
|
1487
|
|
|
def heightmap_normalize(hm, mi=0.0, ma=1.0): |
|
|
|
|
1488
|
|
|
"""Normalize heightmap values between ``mi`` and ``ma``. |
1489
|
|
|
|
1490
|
|
|
Args: |
1491
|
|
|
mi (float): The lowest value after normalization. |
1492
|
|
|
ma (float): The highest value after normalization. |
1493
|
|
|
""" |
1494
|
|
|
lib.TCOD_heightmap_normalize(_heightmap_cdata(hm), mi, ma) |
1495
|
|
|
|
1496
|
|
|
def heightmap_lerp_hm(hm1, hm2, hm3, coef): |
1497
|
|
|
"""Perform linear interpolation between two heightmaps storing the result |
1498
|
|
|
in ``hm3``. |
1499
|
|
|
|
1500
|
|
|
This is the same as doing ``hm3[:] = hm1[:] + (hm2[:] - hm1[:]) * coef`` |
1501
|
|
|
|
1502
|
|
|
Args: |
1503
|
|
|
hm1 (numpy.ndarray): The first heightmap. |
1504
|
|
|
hm2 (numpy.ndarray): The second heightmap to add to the first. |
1505
|
|
|
hm3 (numpy.ndarray): A destination heightmap to store the result. |
1506
|
|
|
coef (float): The linear interpolation coefficient. |
1507
|
|
|
""" |
1508
|
|
|
lib.TCOD_heightmap_lerp_hm(_heightmap_cdata(hm1), _heightmap_cdata(hm2), |
1509
|
|
|
_heightmap_cdata(hm3), coef) |
1510
|
|
|
|
1511
|
|
|
def heightmap_add_hm(hm1, hm2, hm3): |
1512
|
|
|
"""Add two heightmaps together and stores the result in ``hm3``. |
1513
|
|
|
|
1514
|
|
|
Args: |
1515
|
|
|
hm1 (numpy.ndarray): The first heightmap. |
1516
|
|
|
hm2 (numpy.ndarray): The second heightmap to add to the first. |
1517
|
|
|
hm3 (numpy.ndarray): A destination heightmap to store the result. |
1518
|
|
|
|
1519
|
|
|
.. deprecated:: 2.0 |
1520
|
|
|
Do ``hm3[:] = hm1[:] + hm2[:]`` instead. |
1521
|
|
|
""" |
1522
|
|
|
hm3[:] = hm1[:] + hm2[:] |
1523
|
|
|
|
1524
|
|
|
def heightmap_multiply_hm(hm1, hm2, hm3): |
1525
|
|
|
"""Multiplies two heightmap's together and stores the result in ``hm3``. |
1526
|
|
|
|
1527
|
|
|
Args: |
1528
|
|
|
hm1 (numpy.ndarray): The first heightmap. |
1529
|
|
|
hm2 (numpy.ndarray): The second heightmap to multiply with the first. |
1530
|
|
|
hm3 (numpy.ndarray): A destination heightmap to store the result. |
1531
|
|
|
|
1532
|
|
|
.. deprecated:: 2.0 |
1533
|
|
|
Do ``hm3[:] = hm1[:] * hm2[:]`` instead. |
1534
|
|
|
Alternatively you can do ``HeightMap(hm1.array[:] * hm2.array[:])``. |
1535
|
|
|
""" |
1536
|
|
|
hm3[:] = hm1[:] * hm2[:] |
1537
|
|
|
|
1538
|
|
|
def heightmap_add_hill(hm, x, y, radius, height): |
|
|
|
|
1539
|
|
|
"""Add a hill (a half spheroid) at given position. |
1540
|
|
|
|
1541
|
|
|
If height == radius or -radius, the hill is a half-sphere. |
1542
|
|
|
|
1543
|
|
|
Args: |
1544
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1545
|
|
|
x (float): The x position at the center of the new hill. |
1546
|
|
|
y (float): The y position at the center of the new hill. |
1547
|
|
|
radius (float): The size of the new hill. |
1548
|
|
|
height (float): The height or depth of the new hill. |
1549
|
|
|
""" |
1550
|
|
|
lib.TCOD_heightmap_add_hill(_heightmap_cdata(hm), x, y, radius, height) |
1551
|
|
|
|
1552
|
|
|
def heightmap_dig_hill(hm, x, y, radius, height): |
|
|
|
|
1553
|
|
|
""" |
1554
|
|
|
|
1555
|
|
|
This function takes the highest value (if height > 0) or the lowest |
1556
|
|
|
(if height < 0) between the map and the hill. |
1557
|
|
|
|
1558
|
|
|
It's main goal is to carve things in maps (like rivers) by digging hills along a curve. |
|
|
|
|
1559
|
|
|
|
1560
|
|
|
Args: |
1561
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1562
|
|
|
x (float): The x position at the center of the new carving. |
1563
|
|
|
y (float): The y position at the center of the new carving. |
1564
|
|
|
radius (float): The size of the carving. |
1565
|
|
|
height (float): The height or depth of the hill to dig out. |
1566
|
|
|
""" |
1567
|
|
|
lib.TCOD_heightmap_dig_hill(_heightmap_cdata(hm), x, y, radius, height) |
1568
|
|
|
|
1569
|
|
|
def heightmap_rain_erosion(hm, nbDrops, erosionCoef, sedimentationCoef, rnd=None): |
|
|
|
|
1570
|
|
|
"""Simulate the effect of rain drops on the terrain, resulting in erosion. |
1571
|
|
|
|
1572
|
|
|
``nbDrops`` should be at least hm.size. |
1573
|
|
|
|
1574
|
|
|
Args: |
1575
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1576
|
|
|
nbDrops (int): Number of rain drops to simulate. |
1577
|
|
|
erosionCoef (float): Amount of ground eroded on the drop's path. |
1578
|
|
|
sedimentationCoef (float): Amount of ground deposited when the drops |
1579
|
|
|
stops to flow. |
1580
|
|
|
rnd (Optional[Random]): A tcod.Random instance, or None. |
1581
|
|
|
""" |
1582
|
|
|
lib.TCOD_heightmap_rain_erosion(_heightmap_cdata(hm), nbDrops, erosionCoef, |
1583
|
|
|
sedimentationCoef, rnd.random_c if rnd else ffi.NULL) |
|
|
|
|
1584
|
|
|
|
1585
|
|
|
def heightmap_kernel_transform(hm, kernelsize, dx, dy, weight, minLevel, |
|
|
|
|
1586
|
|
|
maxLevel): |
1587
|
|
|
"""Apply a generic transformation on the map, so that each resulting cell |
1588
|
|
|
value is the weighted sum of several neighbour cells. |
1589
|
|
|
|
1590
|
|
|
This can be used to smooth/sharpen the map. |
1591
|
|
|
|
1592
|
|
|
Args: |
1593
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1594
|
|
|
kernelsize (int): Should be set to the length of the parameters:: |
1595
|
|
|
dx, dy, and weight. |
1596
|
|
|
dx (Sequence[int]): A sequence of x coorinates. |
1597
|
|
|
dy (Sequence[int]): A sequence of y coorinates. |
1598
|
|
|
weight (Sequence[float]): A sequence of kernelSize cells weight. |
1599
|
|
|
The value of each neighbour cell is scaled by |
1600
|
|
|
its corresponding weight |
1601
|
|
|
minLevel (float): No transformation will apply to cells |
1602
|
|
|
below this value. |
1603
|
|
|
maxLevel (float): No transformation will apply to cells |
1604
|
|
|
above this value. |
1605
|
|
|
|
1606
|
|
|
See examples below for a simple horizontal smoothing kernel : |
1607
|
|
|
replace value(x,y) with |
1608
|
|
|
0.33*value(x-1,y) + 0.33*value(x,y) + 0.33*value(x+1,y). |
1609
|
|
|
To do this, you need a kernel of size 3 |
1610
|
|
|
(the sum involves 3 surrounding cells). |
1611
|
|
|
The dx,dy array will contain |
1612
|
|
|
* dx=-1, dy=0 for cell (x-1, y) |
1613
|
|
|
* dx=1, dy=0 for cell (x+1, y) |
1614
|
|
|
* dx=0, dy=0 for cell (x, y) |
1615
|
|
|
* The weight array will contain 0.33 for each cell. |
1616
|
|
|
|
1617
|
|
|
Example: |
1618
|
|
|
>>> import numpy as np |
1619
|
|
|
>>> heightmap = np.zeros((3, 3), dtype=np.float32) |
1620
|
|
|
>>> heightmap[:,1] = 1 |
1621
|
|
|
>>> dx = [-1, 1, 0] |
1622
|
|
|
>>> dy = [0, 0, 0] |
1623
|
|
|
>>> weight = [0.33, 0.33, 0.33] |
1624
|
|
|
>>> tcod.heightmap_kernel_transform(heightmap, 3, dx, dy, weight, |
1625
|
|
|
... 0.0, 1.0) |
1626
|
|
|
""" |
1627
|
|
|
cdx = ffi.new('int[]', dx) |
1628
|
|
|
cdy = ffi.new('int[]', dy) |
1629
|
|
|
cweight = ffi.new('float[]', weight) |
1630
|
|
|
lib.TCOD_heightmap_kernel_transform(_heightmap_cdata(hm), kernelsize, |
1631
|
|
|
cdx, cdy, cweight, minLevel, maxLevel) |
1632
|
|
|
|
1633
|
|
|
def heightmap_add_voronoi(hm, nbPoints, nbCoef, coef, rnd=None): |
|
|
|
|
1634
|
|
|
"""Add values from a Voronoi diagram to the heightmap. |
1635
|
|
|
|
1636
|
|
|
Args: |
1637
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1638
|
|
|
nbPoints (Any): Number of Voronoi sites. |
1639
|
|
|
nbCoef (int): The diagram value is calculated from the nbCoef |
1640
|
|
|
closest sites. |
1641
|
|
|
coef (Sequence[float]): The distance to each site is scaled by the |
1642
|
|
|
corresponding coef. |
1643
|
|
|
Closest site : coef[0], |
1644
|
|
|
second closest site : coef[1], ... |
1645
|
|
|
rnd (Optional[Random]): A Random instance, or None. |
1646
|
|
|
""" |
1647
|
|
|
nbPoints = len(coef) |
1648
|
|
|
ccoef = ffi.new('float[]', coef) |
1649
|
|
|
lib.TCOD_heightmap_add_voronoi(_heightmap_cdata(hm), nbPoints, |
1650
|
|
|
nbCoef, ccoef, rnd.random_c if rnd else ffi.NULL) |
|
|
|
|
1651
|
|
|
|
1652
|
|
|
def heightmap_add_fbm(hm, noise, mulx, muly, addx, addy, octaves, delta, scale): |
|
|
|
|
1653
|
|
|
"""Add FBM noise to the heightmap. |
1654
|
|
|
|
1655
|
|
|
The noise coordinate for each map cell is |
1656
|
|
|
`((x + addx) * mulx / width, (y + addy) * muly / height)`. |
1657
|
|
|
|
1658
|
|
|
The value added to the heightmap is `delta + noise * scale`. |
1659
|
|
|
|
1660
|
|
|
Args: |
1661
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1662
|
|
|
noise (Noise): A Noise instance. |
1663
|
|
|
mulx (float): Scaling of each x coordinate. |
1664
|
|
|
muly (float): Scaling of each y coordinate. |
1665
|
|
|
addx (float): Translation of each x coordinate. |
1666
|
|
|
addy (float): Translation of each y coordinate. |
1667
|
|
|
octaves (float): Number of octaves in the FBM sum. |
1668
|
|
|
delta (float): The value added to all heightmap cells. |
1669
|
|
|
scale (float): The noise value is scaled with this parameter. |
1670
|
|
|
""" |
1671
|
|
|
noise = noise.noise_c if noise is not None else ffi.NULL |
1672
|
|
|
lib.TCOD_heightmap_add_fbm(_heightmap_cdata(hm), noise, |
1673
|
|
|
mulx, muly, addx, addy, octaves, delta, scale) |
1674
|
|
|
|
1675
|
|
|
def heightmap_scale_fbm(hm, noise, mulx, muly, addx, addy, octaves, delta, |
|
|
|
|
1676
|
|
|
scale): |
1677
|
|
|
"""Multiply the heighmap values with FBM noise. |
1678
|
|
|
|
1679
|
|
|
Args: |
1680
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1681
|
|
|
noise (Noise): A Noise instance. |
1682
|
|
|
mulx (float): Scaling of each x coordinate. |
1683
|
|
|
muly (float): Scaling of each y coordinate. |
1684
|
|
|
addx (float): Translation of each x coordinate. |
1685
|
|
|
addy (float): Translation of each y coordinate. |
1686
|
|
|
octaves (float): Number of octaves in the FBM sum. |
1687
|
|
|
delta (float): The value added to all heightmap cells. |
1688
|
|
|
scale (float): The noise value is scaled with this parameter. |
1689
|
|
|
""" |
1690
|
|
|
noise = noise.noise_c if noise is not None else ffi.NULL |
1691
|
|
|
lib.TCOD_heightmap_scale_fbm(_heightmap_cdata(hm), noise, |
1692
|
|
|
mulx, muly, addx, addy, octaves, delta, scale) |
1693
|
|
|
|
1694
|
|
|
def heightmap_dig_bezier(hm, px, py, startRadius, startDepth, endRadius, |
|
|
|
|
1695
|
|
|
endDepth): |
1696
|
|
|
"""Carve a path along a cubic Bezier curve. |
1697
|
|
|
|
1698
|
|
|
Both radius and depth can vary linearly along the path. |
1699
|
|
|
|
1700
|
|
|
Args: |
1701
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1702
|
|
|
px (Sequence[int]): The 4 `x` coordinates of the Bezier curve. |
1703
|
|
|
py (Sequence[int]): The 4 `y` coordinates of the Bezier curve. |
1704
|
|
|
startRadius (float): The starting radius size. |
1705
|
|
|
startDepth (float): The starting depth. |
1706
|
|
|
endRadius (float): The ending radius size. |
1707
|
|
|
endDepth (float): The ending depth. |
1708
|
|
|
""" |
1709
|
|
|
lib.TCOD_heightmap_dig_bezier(_heightmap_cdata(hm), px, py, startRadius, |
1710
|
|
|
startDepth, endRadius, |
1711
|
|
|
endDepth) |
1712
|
|
|
|
1713
|
|
|
def heightmap_get_value(hm, x, y): |
|
|
|
|
1714
|
|
|
"""Return the value at ``x``, ``y`` in a heightmap. |
1715
|
|
|
|
1716
|
|
|
Args: |
1717
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1718
|
|
|
x (int): The x position to pick. |
1719
|
|
|
y (int): The y position to pick. |
1720
|
|
|
|
1721
|
|
|
Returns: |
1722
|
|
|
float: The value at ``x``, ``y``. |
1723
|
|
|
|
1724
|
|
|
.. deprecated:: 2.0 |
1725
|
|
|
Do ``value = hm[y, x]`` instead. |
1726
|
|
|
""" |
1727
|
|
|
# explicit type conversion to pass test, (test should have been better.) |
1728
|
|
|
return float(hm[y, x]) |
1729
|
|
|
|
1730
|
|
|
def heightmap_get_interpolated_value(hm, x, y): |
|
|
|
|
1731
|
|
|
"""Return the interpolated height at non integer coordinates. |
1732
|
|
|
|
1733
|
|
|
Args: |
1734
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1735
|
|
|
x (float): A floating point x coordinate. |
1736
|
|
|
y (float): A floating point y coordinate. |
1737
|
|
|
|
1738
|
|
|
Returns: |
1739
|
|
|
float: The value at ``x``, ``y``. |
1740
|
|
|
""" |
1741
|
|
|
return lib.TCOD_heightmap_get_interpolated_value(_heightmap_cdata(hm), |
1742
|
|
|
x, y) |
1743
|
|
|
|
1744
|
|
|
def heightmap_get_slope(hm, x, y): |
|
|
|
|
1745
|
|
|
"""Return the slope between 0 and (pi / 2) at given coordinates. |
1746
|
|
|
|
1747
|
|
|
Args: |
1748
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1749
|
|
|
x (int): The x coordinate. |
1750
|
|
|
y (int): The y coordinate. |
1751
|
|
|
|
1752
|
|
|
Returns: |
1753
|
|
|
float: The steepness at ``x``, ``y``. From 0 to (pi / 2) |
1754
|
|
|
""" |
1755
|
|
|
return lib.TCOD_heightmap_get_slope(_heightmap_cdata(hm), x, y) |
1756
|
|
|
|
1757
|
|
|
def heightmap_get_normal(hm, x, y, waterLevel): |
|
|
|
|
1758
|
|
|
"""Return the map normal at given coordinates. |
1759
|
|
|
|
1760
|
|
|
Args: |
1761
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1762
|
|
|
x (float): The x coordinate. |
1763
|
|
|
y (float): The y coordinate. |
1764
|
|
|
waterLevel (float): The heightmap is considered flat below this value. |
1765
|
|
|
|
1766
|
|
|
Returns: |
1767
|
|
|
Tuple[float, float, float]: An (x, y, z) vector normal. |
1768
|
|
|
""" |
1769
|
|
|
cn = ffi.new('float[3]') |
|
|
|
|
1770
|
|
|
lib.TCOD_heightmap_get_normal(_heightmap_cdata(hm), x, y, cn, waterLevel) |
1771
|
|
|
return tuple(cn) |
1772
|
|
|
|
1773
|
|
|
def heightmap_count_cells(hm, mi, ma): |
|
|
|
|
1774
|
|
|
"""Return the number of map cells which value is between ``mi`` and ``ma``. |
1775
|
|
|
|
1776
|
|
|
Args: |
1777
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1778
|
|
|
mi (float): The lower bound. |
1779
|
|
|
ma (float): The upper bound. |
1780
|
|
|
|
1781
|
|
|
Returns: |
1782
|
|
|
int: The count of values which fall between ``mi`` and ``ma``. |
1783
|
|
|
""" |
1784
|
|
|
return lib.TCOD_heightmap_count_cells(_heightmap_cdata(hm), mi, ma) |
1785
|
|
|
|
1786
|
|
|
def heightmap_has_land_on_border(hm, waterlevel): |
|
|
|
|
1787
|
|
|
"""Returns True if the map edges are below ``waterlevel``, otherwise False. |
1788
|
|
|
|
1789
|
|
|
Args: |
1790
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1791
|
|
|
waterLevel (float): The water level to use. |
1792
|
|
|
|
1793
|
|
|
Returns: |
1794
|
|
|
bool: True if the map edges are below ``waterlevel``, otherwise False. |
1795
|
|
|
""" |
1796
|
|
|
return lib.TCOD_heightmap_has_land_on_border(_heightmap_cdata(hm), |
1797
|
|
|
waterlevel) |
1798
|
|
|
|
1799
|
|
|
def heightmap_get_minmax(hm): |
|
|
|
|
1800
|
|
|
"""Return the min and max values of this heightmap. |
1801
|
|
|
|
1802
|
|
|
Args: |
1803
|
|
|
hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. |
1804
|
|
|
|
1805
|
|
|
Returns: |
1806
|
|
|
Tuple[float, float]: The (min, max) values. |
1807
|
|
|
|
1808
|
|
|
.. deprecated:: 2.0 |
1809
|
|
|
Do ``hm.min()`` or ``hm.max()`` instead. |
1810
|
|
|
""" |
1811
|
|
|
mi = ffi.new('float *') |
|
|
|
|
1812
|
|
|
ma = ffi.new('float *') |
|
|
|
|
1813
|
|
|
lib.TCOD_heightmap_get_minmax(_heightmap_cdata(hm), mi, ma) |
1814
|
|
|
return mi[0], ma[0] |
1815
|
|
|
|
1816
|
|
|
def heightmap_delete(hm): |
|
|
|
|
1817
|
|
|
"""Does nothing. |
1818
|
|
|
|
1819
|
|
|
.. deprecated:: 2.0 |
1820
|
|
|
libtcod-cffi deletes heightmaps automatically. |
1821
|
|
|
""" |
1822
|
|
|
pass |
1823
|
|
|
|
1824
|
|
|
def image_new(width, height): |
|
|
|
|
1825
|
|
|
return tcod.image.Image(width, height) |
1826
|
|
|
|
1827
|
|
|
def image_clear(image, col): |
|
|
|
|
1828
|
|
|
image.clear(col) |
1829
|
|
|
|
1830
|
|
|
def image_invert(image): |
|
|
|
|
1831
|
|
|
image.invert() |
1832
|
|
|
|
1833
|
|
|
def image_hflip(image): |
|
|
|
|
1834
|
|
|
image.hflip() |
1835
|
|
|
|
1836
|
|
|
def image_rotate90(image, num=1): |
|
|
|
|
1837
|
|
|
image.rotate90(num) |
1838
|
|
|
|
1839
|
|
|
def image_vflip(image): |
|
|
|
|
1840
|
|
|
image.vflip() |
1841
|
|
|
|
1842
|
|
|
def image_scale(image, neww, newh): |
|
|
|
|
1843
|
|
|
image.scale(neww, newh) |
1844
|
|
|
|
1845
|
|
|
def image_set_key_color(image, col): |
|
|
|
|
1846
|
|
|
image.set_key_color(col) |
1847
|
|
|
|
1848
|
|
|
def image_get_alpha(image, x, y): |
|
|
|
|
1849
|
|
|
image.get_alpha(x, y) |
1850
|
|
|
|
1851
|
|
|
def image_is_pixel_transparent(image, x, y): |
|
|
|
|
1852
|
|
|
lib.TCOD_image_is_pixel_transparent(image.image_c, x, y) |
1853
|
|
|
|
1854
|
|
|
def image_load(filename): |
1855
|
|
|
"""Load an image file into an Image instance and return it. |
1856
|
|
|
|
1857
|
|
|
Args: |
1858
|
|
|
filename (AnyStr): Path to a .bmp or .png image file. |
1859
|
|
|
""" |
1860
|
|
|
return tcod.image.Image._from_cdata( |
1861
|
|
|
ffi.gc(lib.TCOD_image_load(_bytes(filename)), |
1862
|
|
|
lib.TCOD_image_delete) |
1863
|
|
|
) |
1864
|
|
|
|
1865
|
|
|
def image_from_console(console): |
1866
|
|
|
"""Return an Image with a Consoles pixel data. |
1867
|
|
|
|
1868
|
|
|
This effectively takes a screen-shot of the Console. |
1869
|
|
|
|
1870
|
|
|
Args: |
1871
|
|
|
console (Console): Any Console instance. |
1872
|
|
|
""" |
1873
|
|
|
return tcod.image.Image._from_cdata( |
1874
|
|
|
ffi.gc( |
1875
|
|
|
lib.TCOD_image_from_console(_console(console)), |
1876
|
|
|
lib.TCOD_image_delete, |
1877
|
|
|
) |
1878
|
|
|
) |
1879
|
|
|
|
1880
|
|
|
def image_refresh_console(image, console): |
|
|
|
|
1881
|
|
|
image.refresh_console(console) |
1882
|
|
|
|
1883
|
|
|
def image_get_size(image): |
|
|
|
|
1884
|
|
|
return image.width, image.height |
1885
|
|
|
|
1886
|
|
|
def image_get_pixel(image, x, y): |
|
|
|
|
1887
|
|
|
return image.get_pixel(x, y) |
1888
|
|
|
|
1889
|
|
|
def image_get_mipmap_pixel(image, x0, y0, x1, y1): |
|
|
|
|
1890
|
|
|
return image.get_mipmap_pixel(x0, y0, x1, y1) |
1891
|
|
|
|
1892
|
|
|
def image_put_pixel(image, x, y, col): |
|
|
|
|
1893
|
|
|
image.put_pixel(x, y, col) |
1894
|
|
|
|
1895
|
|
|
def image_blit(image, console, x, y, bkgnd_flag, scalex, scaley, angle): |
|
|
|
|
1896
|
|
|
image.blit(console, x, y, bkgnd_flag, scalex, scaley, angle) |
1897
|
|
|
|
1898
|
|
|
def image_blit_rect(image, console, x, y, w, h, bkgnd_flag): |
|
|
|
|
1899
|
|
|
image.blit_rect(console, x, y, w, h, bkgnd_flag) |
1900
|
|
|
|
1901
|
|
|
def image_blit_2x(image, console, dx, dy, sx=0, sy=0, w=-1, h=-1): |
|
|
|
|
1902
|
|
|
image.blit_2x(console, dx, dy, sx, sy, w, h) |
1903
|
|
|
|
1904
|
|
|
def image_save(image, filename): |
|
|
|
|
1905
|
|
|
image.save_as(filename) |
1906
|
|
|
|
1907
|
|
|
def image_delete(image): |
|
|
|
|
1908
|
|
|
pass |
1909
|
|
|
|
1910
|
|
|
def line_init(xo, yo, xd, yd): |
|
|
|
|
1911
|
|
|
"""Initilize a line whose points will be returned by `line_step`. |
1912
|
|
|
|
1913
|
|
|
This function does not return anything on its own. |
1914
|
|
|
|
1915
|
|
|
Does not include the origin point. |
1916
|
|
|
|
1917
|
|
|
Args: |
1918
|
|
|
xo (int): X starting point. |
1919
|
|
|
yo (int): Y starting point. |
1920
|
|
|
xd (int): X destination point. |
1921
|
|
|
yd (int): Y destination point. |
1922
|
|
|
|
1923
|
|
|
.. deprecated:: 2.0 |
1924
|
|
|
Use `line_iter` instead. |
1925
|
|
|
""" |
1926
|
|
|
lib.TCOD_line_init(xo, yo, xd, yd) |
1927
|
|
|
|
1928
|
|
|
def line_step(): |
1929
|
|
|
"""After calling line_init returns (x, y) points of the line. |
1930
|
|
|
|
1931
|
|
|
Once all points are exhausted this function will return (None, None) |
1932
|
|
|
|
1933
|
|
|
Returns: |
1934
|
|
|
Union[Tuple[int, int], Tuple[None, None]]: |
1935
|
|
|
The next (x, y) point of the line setup by line_init, |
1936
|
|
|
or (None, None) if there are no more points. |
1937
|
|
|
|
1938
|
|
|
.. deprecated:: 2.0 |
1939
|
|
|
Use `line_iter` instead. |
1940
|
|
|
""" |
1941
|
|
|
x = ffi.new('int *') |
|
|
|
|
1942
|
|
|
y = ffi.new('int *') |
|
|
|
|
1943
|
|
|
ret = lib.TCOD_line_step(x, y) |
1944
|
|
|
if not ret: |
1945
|
|
|
return x[0], y[0] |
1946
|
|
|
return None,None |
|
|
|
|
1947
|
|
|
|
1948
|
|
|
|
1949
|
|
|
def line(xo, yo, xd, yd, py_callback): |
|
|
|
|
1950
|
|
|
""" Iterate over a line using a callback function. |
1951
|
|
|
|
1952
|
|
|
Your callback function will take x and y parameters and return True to |
1953
|
|
|
continue iteration or False to stop iteration and return. |
1954
|
|
|
|
1955
|
|
|
This function includes both the start and end points. |
1956
|
|
|
|
1957
|
|
|
Args: |
1958
|
|
|
xo (int): X starting point. |
1959
|
|
|
yo (int): Y starting point. |
1960
|
|
|
xd (int): X destination point. |
1961
|
|
|
yd (int): Y destination point. |
1962
|
|
|
py_callback (Callable[[int, int], bool]): |
1963
|
|
|
A callback which takes x and y parameters and returns bool. |
1964
|
|
|
|
1965
|
|
|
Returns: |
1966
|
|
|
bool: False if the callback cancels the line interation by |
1967
|
|
|
returning False or None, otherwise True. |
1968
|
|
|
|
1969
|
|
|
.. deprecated:: 2.0 |
1970
|
|
|
Use `line_iter` instead. |
1971
|
|
|
""" |
1972
|
|
|
for x, y in line_iter(xo, yo, xd, yd): |
|
|
|
|
1973
|
|
|
if not py_callback(x, y): |
1974
|
|
|
break |
1975
|
|
|
else: |
1976
|
|
|
return True |
1977
|
|
|
return False |
1978
|
|
|
|
1979
|
|
|
|
1980
|
|
|
def line_iter(xo, yo, xd, yd): |
|
|
|
|
1981
|
|
|
""" returns an iterator |
1982
|
|
|
|
1983
|
|
|
This iterator does not include the origin point. |
1984
|
|
|
|
1985
|
|
|
Args: |
1986
|
|
|
xo (int): X starting point. |
1987
|
|
|
yo (int): Y starting point. |
1988
|
|
|
xd (int): X destination point. |
1989
|
|
|
yd (int): Y destination point. |
1990
|
|
|
|
1991
|
|
|
Returns: |
1992
|
|
|
Iterator[Tuple[int,int]]: An iterator of (x,y) points. |
1993
|
|
|
""" |
1994
|
|
|
data = ffi.new('TCOD_bresenham_data_t *') |
1995
|
|
|
lib.TCOD_line_init_mt(xo, yo, xd, yd, data) |
1996
|
|
|
x = ffi.new('int *') |
|
|
|
|
1997
|
|
|
y = ffi.new('int *') |
|
|
|
|
1998
|
|
|
yield xo, yo |
1999
|
|
|
while not lib.TCOD_line_step_mt(x, y, data): |
2000
|
|
|
yield (x[0], y[0]) |
2001
|
|
|
|
2002
|
|
|
FOV_BASIC = 0 |
2003
|
|
|
FOV_DIAMOND = 1 |
2004
|
|
|
FOV_SHADOW = 2 |
2005
|
|
|
FOV_PERMISSIVE_0 = 3 |
2006
|
|
|
FOV_PERMISSIVE_1 = 4 |
2007
|
|
|
FOV_PERMISSIVE_2 = 5 |
2008
|
|
|
FOV_PERMISSIVE_3 = 6 |
2009
|
|
|
FOV_PERMISSIVE_4 = 7 |
2010
|
|
|
FOV_PERMISSIVE_5 = 8 |
2011
|
|
|
FOV_PERMISSIVE_6 = 9 |
2012
|
|
|
FOV_PERMISSIVE_7 = 10 |
2013
|
|
|
FOV_PERMISSIVE_8 = 11 |
2014
|
|
|
FOV_RESTRICTIVE = 12 |
2015
|
|
|
NB_FOV_ALGORITHMS = 13 |
2016
|
|
|
|
2017
|
|
|
def map_new(w, h): |
|
|
|
|
2018
|
|
|
return tcod.map.Map(w, h) |
2019
|
|
|
|
2020
|
|
|
def map_copy(source, dest): |
|
|
|
|
2021
|
|
|
return lib.TCOD_map_copy(source.map_c, dest.map_c) |
2022
|
|
|
|
2023
|
|
|
def map_set_properties(m, x, y, isTrans, isWalk): |
|
|
|
|
2024
|
|
|
lib.TCOD_map_set_properties(m.map_c, x, y, isTrans, isWalk) |
2025
|
|
|
|
2026
|
|
|
def map_clear(m,walkable=False,transparent=False): |
|
|
|
|
2027
|
|
|
# walkable/transparent looks incorrectly ordered here. |
2028
|
|
|
# TODO: needs test. |
|
|
|
|
2029
|
|
|
lib.TCOD_map_clear(m.map_c, walkable, transparent) |
2030
|
|
|
|
2031
|
|
|
def map_compute_fov(m, x, y, radius=0, light_walls=True, algo=FOV_RESTRICTIVE ): |
|
|
|
|
2032
|
|
|
lib.TCOD_map_compute_fov(m.map_c, x, y, radius, light_walls, algo) |
2033
|
|
|
|
2034
|
|
|
def map_is_in_fov(m, x, y): |
|
|
|
|
2035
|
|
|
return lib.TCOD_map_is_in_fov(m.map_c, x, y) |
2036
|
|
|
|
2037
|
|
|
def map_is_transparent(m, x, y): |
|
|
|
|
2038
|
|
|
return lib.TCOD_map_is_transparent(m.map_c, x, y) |
2039
|
|
|
|
2040
|
|
|
def map_is_walkable(m, x, y): |
|
|
|
|
2041
|
|
|
return lib.TCOD_map_is_walkable(m.map_c, x, y) |
2042
|
|
|
|
2043
|
|
|
def map_delete(m): |
|
|
|
|
2044
|
|
|
pass |
2045
|
|
|
|
2046
|
|
|
def map_get_width(map): |
|
|
|
|
2047
|
|
|
return map.width |
2048
|
|
|
|
2049
|
|
|
def map_get_height(map): |
|
|
|
|
2050
|
|
|
return map.height |
2051
|
|
|
|
2052
|
|
|
def mouse_show_cursor(visible): |
|
|
|
|
2053
|
|
|
lib.TCOD_mouse_show_cursor(visible) |
2054
|
|
|
|
2055
|
|
|
def mouse_is_cursor_visible(): |
|
|
|
|
2056
|
|
|
return lib.TCOD_mouse_is_cursor_visible() |
2057
|
|
|
|
2058
|
|
|
def mouse_move(x, y): |
|
|
|
|
2059
|
|
|
lib.TCOD_mouse_move(x, y) |
2060
|
|
|
|
2061
|
|
|
def mouse_get_status(): |
|
|
|
|
2062
|
|
|
return Mouse(lib.TCOD_mouse_get_status()) |
2063
|
|
|
|
2064
|
|
|
def namegen_parse(filename,random=None): |
|
|
|
|
2065
|
|
|
lib.TCOD_namegen_parse(_bytes(filename), random or ffi.NULL) |
2066
|
|
|
|
2067
|
|
|
def namegen_generate(name): |
|
|
|
|
2068
|
|
|
return _unpack_char_p(lib.TCOD_namegen_generate(_bytes(name), False)) |
2069
|
|
|
|
2070
|
|
|
def namegen_generate_custom(name, rule): |
|
|
|
|
2071
|
|
|
return _unpack_char_p(lib.TCOD_namegen_generate(_bytes(name), |
2072
|
|
|
_bytes(rule), False)) |
2073
|
|
|
|
2074
|
|
|
def namegen_get_sets(): |
|
|
|
|
2075
|
|
|
sets = lib.TCOD_namegen_get_sets() |
2076
|
|
|
try: |
2077
|
|
|
lst = [] |
2078
|
|
|
while not lib.TCOD_list_is_empty(sets): |
2079
|
|
|
lst.append(_unpack_char_p(ffi.cast('char *', lib.TCOD_list_pop(sets)))) |
|
|
|
|
2080
|
|
|
finally: |
2081
|
|
|
lib.TCOD_list_delete(sets) |
2082
|
|
|
return lst |
2083
|
|
|
|
2084
|
|
|
def namegen_destroy(): |
|
|
|
|
2085
|
|
|
lib.TCOD_namegen_destroy() |
2086
|
|
|
|
2087
|
|
|
def noise_new(dim, h=NOISE_DEFAULT_HURST, l=NOISE_DEFAULT_LACUNARITY, |
|
|
|
|
2088
|
|
|
random=None): |
2089
|
|
|
"""Return a new Noise instance. |
2090
|
|
|
|
2091
|
|
|
Args: |
2092
|
|
|
dim (int): Number of dimensions. From 1 to 4. |
2093
|
|
|
h (float): The hurst exponent. Should be in the 0.0-1.0 range. |
2094
|
|
|
l (float): The noise lacunarity. |
2095
|
|
|
random (Optional[Random]): A Random instance, or None. |
2096
|
|
|
|
2097
|
|
|
Returns: |
2098
|
|
|
Noise: The new Noise instance. |
2099
|
|
|
""" |
2100
|
|
|
return tcod.noise.Noise(dim, hurst=h, lacunarity=l, seed=random) |
2101
|
|
|
|
2102
|
|
|
def noise_set_type(n, typ): |
|
|
|
|
2103
|
|
|
"""Set a Noise objects default noise algorithm. |
2104
|
|
|
|
2105
|
|
|
Args: |
2106
|
|
|
typ (int): Any NOISE_* constant. |
2107
|
|
|
""" |
2108
|
|
|
n.algorithm = typ |
2109
|
|
|
|
2110
|
|
|
def noise_get(n, f, typ=NOISE_DEFAULT): |
|
|
|
|
2111
|
|
|
"""Return the noise value sampled from the ``f`` coordinate. |
2112
|
|
|
|
2113
|
|
|
``f`` should be a tuple or list with a length matching |
2114
|
|
|
:any:`Noise.dimensions`. |
2115
|
|
|
If ``f`` is shoerter than :any:`Noise.dimensions` the missing coordinates |
2116
|
|
|
will be filled with zeros. |
2117
|
|
|
|
2118
|
|
|
Args: |
2119
|
|
|
n (Noise): A Noise instance. |
2120
|
|
|
f (Sequence[float]): The point to sample the noise from. |
2121
|
|
|
typ (int): The noise algorithm to use. |
2122
|
|
|
|
2123
|
|
|
Returns: |
2124
|
|
|
float: The sampled noise value. |
2125
|
|
|
""" |
2126
|
|
|
return lib.TCOD_noise_get_ex(n.noise_c, ffi.new('float[4]', f), typ) |
2127
|
|
|
|
2128
|
|
|
def noise_get_fbm(n, f, oc, typ=NOISE_DEFAULT): |
|
|
|
|
2129
|
|
|
"""Return the fractal Brownian motion sampled from the ``f`` coordinate. |
2130
|
|
|
|
2131
|
|
|
Args: |
2132
|
|
|
n (Noise): A Noise instance. |
2133
|
|
|
f (Sequence[float]): The point to sample the noise from. |
2134
|
|
|
typ (int): The noise algorithm to use. |
2135
|
|
|
octaves (float): The level of level. Should be more than 1. |
2136
|
|
|
|
2137
|
|
|
Returns: |
2138
|
|
|
float: The sampled noise value. |
2139
|
|
|
""" |
2140
|
|
|
return lib.TCOD_noise_get_fbm_ex(n.noise_c, ffi.new('float[4]', f), |
2141
|
|
|
oc, typ) |
2142
|
|
|
|
2143
|
|
|
def noise_get_turbulence(n, f, oc, typ=NOISE_DEFAULT): |
|
|
|
|
2144
|
|
|
"""Return the turbulence noise sampled from the ``f`` coordinate. |
2145
|
|
|
|
2146
|
|
|
Args: |
2147
|
|
|
n (Noise): A Noise instance. |
2148
|
|
|
f (Sequence[float]): The point to sample the noise from. |
2149
|
|
|
typ (int): The noise algorithm to use. |
2150
|
|
|
octaves (float): The level of level. Should be more than 1. |
2151
|
|
|
|
2152
|
|
|
Returns: |
2153
|
|
|
float: The sampled noise value. |
2154
|
|
|
""" |
2155
|
|
|
return lib.TCOD_noise_get_turbulence_ex(n.noise_c, ffi.new('float[4]', f), |
2156
|
|
|
oc, typ) |
2157
|
|
|
|
2158
|
|
|
def noise_delete(n): |
|
|
|
|
2159
|
|
|
"""Does nothing.""" |
2160
|
|
|
pass |
2161
|
|
|
|
2162
|
|
|
_chr = chr |
|
|
|
|
2163
|
|
|
try: |
2164
|
|
|
_chr = unichr # Python 2 |
|
|
|
|
2165
|
|
|
except NameError: |
|
|
|
|
2166
|
|
|
pass |
2167
|
|
|
|
2168
|
|
|
def _unpack_union(type_, union): |
2169
|
|
|
''' |
2170
|
|
|
unpack items from parser new_property (value_converter) |
2171
|
|
|
''' |
2172
|
|
|
if type_ == lib.TCOD_TYPE_BOOL: |
2173
|
|
|
return bool(union.b) |
2174
|
|
|
elif type_ == lib.TCOD_TYPE_CHAR: |
2175
|
|
|
return _unicode(union.c) |
2176
|
|
|
elif type_ == lib.TCOD_TYPE_INT: |
2177
|
|
|
return union.i |
2178
|
|
|
elif type_ == lib.TCOD_TYPE_FLOAT: |
2179
|
|
|
return union.f |
2180
|
|
|
elif (type_ == lib.TCOD_TYPE_STRING or |
2181
|
|
|
lib.TCOD_TYPE_VALUELIST15 >= type_ >= lib.TCOD_TYPE_VALUELIST00): |
2182
|
|
|
return _unpack_char_p(union.s) |
|
|
|
|
2183
|
|
|
elif type_ == lib.TCOD_TYPE_COLOR: |
2184
|
|
|
return Color._new_from_cdata(union.col) |
2185
|
|
|
elif type_ == lib.TCOD_TYPE_DICE: |
2186
|
|
|
return Dice(union.dice) |
2187
|
|
|
elif type_ & lib.TCOD_TYPE_LIST: |
2188
|
|
|
return _convert_TCODList(union.list, type_ & 0xFF) |
2189
|
|
|
else: |
2190
|
|
|
raise RuntimeError('Unknown libtcod type: %i' % type_) |
2191
|
|
|
|
2192
|
|
|
def _convert_TCODList(clist, type_): |
|
|
|
|
2193
|
|
|
return [_unpack_union(type_, lib.TDL_list_get_union(clist, i)) |
2194
|
|
|
for i in range(lib.TCOD_list_size(clist))] |
2195
|
|
|
|
2196
|
|
|
def parser_new(): |
|
|
|
|
2197
|
|
|
return ffi.gc(lib.TCOD_parser_new(), lib.TCOD_parser_delete) |
2198
|
|
|
|
2199
|
|
|
def parser_new_struct(parser, name): |
|
|
|
|
2200
|
|
|
return lib.TCOD_parser_new_struct(parser, name) |
2201
|
|
|
|
2202
|
|
|
# prevent multiple threads from messing with def_extern callbacks |
2203
|
|
|
_parser_callback_lock = _threading.Lock() |
|
|
|
|
2204
|
|
|
_parser_listener = None # temporary global pointer to a listener instance |
|
|
|
|
2205
|
|
|
|
2206
|
|
|
@ffi.def_extern() |
2207
|
|
|
def _pycall_parser_new_struct(struct, name): |
|
|
|
|
2208
|
|
|
return _parser_listener.end_struct(struct, _unpack_char_p(name)) |
2209
|
|
|
|
2210
|
|
|
@ffi.def_extern() |
2211
|
|
|
def _pycall_parser_new_flag(name): |
|
|
|
|
2212
|
|
|
return _parser_listener.new_flag(_unpack_char_p(name)) |
2213
|
|
|
|
2214
|
|
|
@ffi.def_extern() |
2215
|
|
|
def _pycall_parser_new_property(propname, type, value): |
|
|
|
|
2216
|
|
|
return _parser_listener.new_property(_unpack_char_p(propname), type, |
2217
|
|
|
_unpack_union(type, value)) |
2218
|
|
|
|
2219
|
|
|
@ffi.def_extern() |
2220
|
|
|
def _pycall_parser_end_struct(struct, name): |
|
|
|
|
2221
|
|
|
return _parser_listener.end_struct(struct, _unpack_char_p(name)) |
2222
|
|
|
|
2223
|
|
|
@ffi.def_extern() |
2224
|
|
|
def _pycall_parser_error(msg): |
|
|
|
|
2225
|
|
|
_parser_listener.error(_unpack_char_p(msg)) |
2226
|
|
|
|
2227
|
|
|
def parser_run(parser, filename, listener=None): |
|
|
|
|
2228
|
|
|
global _parser_listener |
|
|
|
|
2229
|
|
|
if not listener: |
2230
|
|
|
lib.TCOD_parser_run(parser, _bytes(filename), ffi.NULL) |
2231
|
|
|
return |
2232
|
|
|
|
2233
|
|
|
propagate_manager = _PropagateException() |
2234
|
|
|
propagate = propagate_manager.propagate |
2235
|
|
|
|
2236
|
|
|
clistener = ffi.new( |
2237
|
|
|
'TCOD_parser_listener_t *', |
2238
|
|
|
{ |
2239
|
|
|
'new_struct': lib._pycall_parser_new_struct, |
2240
|
|
|
'new_flag': lib._pycall_parser_new_flag, |
2241
|
|
|
'new_property': lib._pycall_parser_new_property, |
2242
|
|
|
'end_struct': lib._pycall_parser_end_struct, |
2243
|
|
|
'error': lib._pycall_parser_error, |
2244
|
|
|
}, |
2245
|
|
|
) |
2246
|
|
|
|
2247
|
|
|
with _parser_callback_lock: |
2248
|
|
|
_parser_listener = listener |
2249
|
|
|
with propagate_manager: |
2250
|
|
|
lib.TCOD_parser_run(parser, _bytes(filename), clistener) |
2251
|
|
|
|
2252
|
|
|
def parser_delete(parser): |
|
|
|
|
2253
|
|
|
pass |
2254
|
|
|
|
2255
|
|
|
def parser_get_bool_property(parser, name): |
|
|
|
|
2256
|
|
|
return bool(lib.TCOD_parser_get_bool_property(parser, _bytes(name))) |
2257
|
|
|
|
2258
|
|
|
def parser_get_int_property(parser, name): |
|
|
|
|
2259
|
|
|
return lib.TCOD_parser_get_int_property(parser, _bytes(name)) |
2260
|
|
|
|
2261
|
|
|
def parser_get_char_property(parser, name): |
|
|
|
|
2262
|
|
|
return _chr(lib.TCOD_parser_get_char_property(parser, _bytes(name))) |
2263
|
|
|
|
2264
|
|
|
def parser_get_float_property(parser, name): |
|
|
|
|
2265
|
|
|
return lib.TCOD_parser_get_float_property(parser, _bytes(name)) |
2266
|
|
|
|
2267
|
|
|
def parser_get_string_property(parser, name): |
|
|
|
|
2268
|
|
|
return _unpack_char_p( |
2269
|
|
|
lib.TCOD_parser_get_string_property(parser, _bytes(name))) |
2270
|
|
|
|
2271
|
|
|
def parser_get_color_property(parser, name): |
|
|
|
|
2272
|
|
|
return Color._new_from_cdata( |
2273
|
|
|
lib.TCOD_parser_get_color_property(parser, _bytes(name))) |
2274
|
|
|
|
2275
|
|
|
def parser_get_dice_property(parser, name): |
|
|
|
|
2276
|
|
|
d = ffi.new('TCOD_dice_t *') |
|
|
|
|
2277
|
|
|
lib.TCOD_parser_get_dice_property_py(parser, _bytes(name), d) |
2278
|
|
|
return Dice(d) |
2279
|
|
|
|
2280
|
|
|
def parser_get_list_property(parser, name, type): |
|
|
|
|
2281
|
|
|
clist = lib.TCOD_parser_get_list_property(parser, _bytes(name), type) |
2282
|
|
|
return _convert_TCODList(clist, type) |
2283
|
|
|
|
2284
|
|
|
RNG_MT = 0 |
2285
|
|
|
RNG_CMWC = 1 |
2286
|
|
|
|
2287
|
|
|
DISTRIBUTION_LINEAR = 0 |
2288
|
|
|
DISTRIBUTION_GAUSSIAN = 1 |
2289
|
|
|
DISTRIBUTION_GAUSSIAN_RANGE = 2 |
2290
|
|
|
DISTRIBUTION_GAUSSIAN_INVERSE = 3 |
2291
|
|
|
DISTRIBUTION_GAUSSIAN_RANGE_INVERSE = 4 |
2292
|
|
|
|
2293
|
|
|
def random_get_instance(): |
2294
|
|
|
"""Return the default Random instance. |
2295
|
|
|
|
2296
|
|
|
Returns: |
2297
|
|
|
Random: A Random instance using the default random number generator. |
2298
|
|
|
""" |
2299
|
|
|
return tcod.random.Random._new_from_cdata( |
2300
|
|
|
ffi.cast('mersenne_data_t*', lib.TCOD_random_get_instance())) |
2301
|
|
|
|
2302
|
|
|
def random_new(algo=RNG_CMWC): |
2303
|
|
|
"""Return a new Random instance. Using ``algo``. |
2304
|
|
|
|
2305
|
|
|
Args: |
2306
|
|
|
algo (int): The random number algorithm to use. |
2307
|
|
|
|
2308
|
|
|
Returns: |
2309
|
|
|
Random: A new Random instance using the given algorithm. |
2310
|
|
|
""" |
2311
|
|
|
return tcod.random.Random(algo) |
2312
|
|
|
|
2313
|
|
|
def random_new_from_seed(seed, algo=RNG_CMWC): |
2314
|
|
|
"""Return a new Random instance. Using the given ``seed`` and ``algo``. |
2315
|
|
|
|
2316
|
|
|
Args: |
2317
|
|
|
seed (Hashable): The RNG seed. Should be a 32-bit integer, but any |
2318
|
|
|
hashable object is accepted. |
2319
|
|
|
algo (int): The random number algorithm to use. |
2320
|
|
|
|
2321
|
|
|
Returns: |
2322
|
|
|
Random: A new Random instance using the given algorithm. |
2323
|
|
|
""" |
2324
|
|
|
return tcod.random.Random(algo, seed) |
2325
|
|
|
|
2326
|
|
|
def random_set_distribution(rnd, dist): |
2327
|
|
|
"""Change the distribution mode of a random number generator. |
2328
|
|
|
|
2329
|
|
|
Args: |
2330
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2331
|
|
|
dist (int): The distribution mode to use. Should be DISTRIBUTION_*. |
2332
|
|
|
""" |
2333
|
|
|
lib.TCOD_random_set_distribution(rnd.random_c if rnd else ffi.NULL, dist) |
2334
|
|
|
|
2335
|
|
|
def random_get_int(rnd, mi, ma): |
|
|
|
|
2336
|
|
|
"""Return a random integer in the range: ``mi`` <= n <= ``ma``. |
2337
|
|
|
|
2338
|
|
|
The result is affacted by calls to :any:`random_set_distribution`. |
2339
|
|
|
|
2340
|
|
|
Args: |
2341
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2342
|
|
|
low (int): The lower bound of the random range, inclusive. |
2343
|
|
|
high (int): The upper bound of the random range, inclusive. |
2344
|
|
|
|
2345
|
|
|
Returns: |
2346
|
|
|
int: A random integer in the range ``mi`` <= n <= ``ma``. |
2347
|
|
|
""" |
2348
|
|
|
return lib.TCOD_random_get_int(rnd.random_c if rnd else ffi.NULL, mi, ma) |
2349
|
|
|
|
2350
|
|
|
def random_get_float(rnd, mi, ma): |
|
|
|
|
2351
|
|
|
"""Return a random float in the range: ``mi`` <= n <= ``ma``. |
2352
|
|
|
|
2353
|
|
|
The result is affacted by calls to :any:`random_set_distribution`. |
2354
|
|
|
|
2355
|
|
|
Args: |
2356
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2357
|
|
|
low (float): The lower bound of the random range, inclusive. |
2358
|
|
|
high (float): The upper bound of the random range, inclusive. |
2359
|
|
|
|
2360
|
|
|
Returns: |
2361
|
|
|
float: A random double precision float |
2362
|
|
|
in the range ``mi`` <= n <= ``ma``. |
2363
|
|
|
""" |
2364
|
|
|
return lib.TCOD_random_get_double( |
2365
|
|
|
rnd.random_c if rnd else ffi.NULL, mi, ma) |
2366
|
|
|
|
2367
|
|
|
def random_get_double(rnd, mi, ma): |
|
|
|
|
2368
|
|
|
"""Return a random float in the range: ``mi`` <= n <= ``ma``. |
2369
|
|
|
|
2370
|
|
|
.. deprecated:: 2.0 |
2371
|
|
|
Use :any:`random_get_float` instead. |
2372
|
|
|
Both funtions return a double precision float. |
2373
|
|
|
""" |
2374
|
|
|
return lib.TCOD_random_get_double( |
2375
|
|
|
rnd.random_c if rnd else ffi.NULL, mi, ma) |
2376
|
|
|
|
2377
|
|
|
def random_get_int_mean(rnd, mi, ma, mean): |
|
|
|
|
2378
|
|
|
"""Return a random weighted integer in the range: ``mi`` <= n <= ``ma``. |
2379
|
|
|
|
2380
|
|
|
The result is affacted by calls to :any:`random_set_distribution`. |
2381
|
|
|
|
2382
|
|
|
Args: |
2383
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2384
|
|
|
low (int): The lower bound of the random range, inclusive. |
2385
|
|
|
high (int): The upper bound of the random range, inclusive. |
2386
|
|
|
mean (int): The mean return value. |
2387
|
|
|
|
2388
|
|
|
Returns: |
2389
|
|
|
int: A random weighted integer in the range ``mi`` <= n <= ``ma``. |
2390
|
|
|
""" |
2391
|
|
|
return lib.TCOD_random_get_int_mean( |
2392
|
|
|
rnd.random_c if rnd else ffi.NULL, mi, ma, mean) |
2393
|
|
|
|
2394
|
|
|
def random_get_float_mean(rnd, mi, ma, mean): |
|
|
|
|
2395
|
|
|
"""Return a random weighted float in the range: ``mi`` <= n <= ``ma``. |
2396
|
|
|
|
2397
|
|
|
The result is affacted by calls to :any:`random_set_distribution`. |
2398
|
|
|
|
2399
|
|
|
Args: |
2400
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2401
|
|
|
low (float): The lower bound of the random range, inclusive. |
2402
|
|
|
high (float): The upper bound of the random range, inclusive. |
2403
|
|
|
mean (float): The mean return value. |
2404
|
|
|
|
2405
|
|
|
Returns: |
2406
|
|
|
float: A random weighted double precision float |
2407
|
|
|
in the range ``mi`` <= n <= ``ma``. |
2408
|
|
|
""" |
2409
|
|
|
return lib.TCOD_random_get_double_mean( |
2410
|
|
|
rnd.random_c if rnd else ffi.NULL, mi, ma, mean) |
2411
|
|
|
|
2412
|
|
|
def random_get_double_mean(rnd, mi, ma, mean): |
|
|
|
|
2413
|
|
|
"""Return a random weighted float in the range: ``mi`` <= n <= ``ma``. |
2414
|
|
|
|
2415
|
|
|
.. deprecated:: 2.0 |
2416
|
|
|
Use :any:`random_get_float_mean` instead. |
2417
|
|
|
Both funtions return a double precision float. |
2418
|
|
|
""" |
2419
|
|
|
return lib.TCOD_random_get_double_mean( |
2420
|
|
|
rnd.random_c if rnd else ffi.NULL, mi, ma, mean) |
2421
|
|
|
|
2422
|
|
|
def random_save(rnd): |
2423
|
|
|
"""Return a copy of a random number generator. |
2424
|
|
|
|
2425
|
|
|
Args: |
2426
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2427
|
|
|
|
2428
|
|
|
Returns: |
2429
|
|
|
Random: A Random instance with a copy of the random generator. |
2430
|
|
|
""" |
2431
|
|
|
return tcod.random.Random._new_from_cdata( |
2432
|
|
|
ffi.gc( |
2433
|
|
|
ffi.cast('mersenne_data_t*', |
2434
|
|
|
lib.TCOD_random_save(rnd.random_c if rnd else ffi.NULL)), |
2435
|
|
|
lib.TCOD_random_delete), |
2436
|
|
|
) |
2437
|
|
|
|
2438
|
|
|
def random_restore(rnd, backup): |
2439
|
|
|
"""Restore a random number generator from a backed up copy. |
2440
|
|
|
|
2441
|
|
|
Args: |
2442
|
|
|
rnd (Optional[Random]): A Random instance, or None to use the default. |
2443
|
|
|
backup (Random): The Random instance which was used as a backup. |
2444
|
|
|
""" |
2445
|
|
|
lib.TCOD_random_restore(rnd.random_c if rnd else ffi.NULL, |
2446
|
|
|
backup.random_c) |
2447
|
|
|
|
2448
|
|
|
def random_delete(rnd): |
|
|
|
|
2449
|
|
|
"""Does nothing.""" |
2450
|
|
|
pass |
2451
|
|
|
|
2452
|
|
|
def struct_add_flag(struct, name): |
|
|
|
|
2453
|
|
|
lib.TCOD_struct_add_flag(struct, name) |
2454
|
|
|
|
2455
|
|
|
def struct_add_property(struct, name, typ, mandatory): |
|
|
|
|
2456
|
|
|
lib.TCOD_struct_add_property(struct, name, typ, mandatory) |
2457
|
|
|
|
2458
|
|
|
def struct_add_value_list(struct, name, value_list, mandatory): |
|
|
|
|
2459
|
|
|
CARRAY = c_char_p * (len(value_list) + 1) |
|
|
|
|
2460
|
|
|
cvalue_list = CARRAY() |
2461
|
|
|
for i, value in enumerate(value_list): |
2462
|
|
|
cvalue_list[i] = cast(value, c_char_p) |
|
|
|
|
2463
|
|
|
cvalue_list[len(value_list)] = 0 |
2464
|
|
|
lib.TCOD_struct_add_value_list(struct, name, cvalue_list, mandatory) |
2465
|
|
|
|
2466
|
|
|
def struct_add_list_property(struct, name, typ, mandatory): |
|
|
|
|
2467
|
|
|
lib.TCOD_struct_add_list_property(struct, name, typ, mandatory) |
2468
|
|
|
|
2469
|
|
|
def struct_add_structure(struct, sub_struct): |
|
|
|
|
2470
|
|
|
lib.TCOD_struct_add_structure(struct, sub_struct) |
2471
|
|
|
|
2472
|
|
|
def struct_get_name(struct): |
|
|
|
|
2473
|
|
|
return _unpack_char_p(lib.TCOD_struct_get_name(struct)) |
2474
|
|
|
|
2475
|
|
|
def struct_is_mandatory(struct, name): |
|
|
|
|
2476
|
|
|
return lib.TCOD_struct_is_mandatory(struct, name) |
2477
|
|
|
|
2478
|
|
|
def struct_get_type(struct, name): |
|
|
|
|
2479
|
|
|
return lib.TCOD_struct_get_type(struct, name) |
2480
|
|
|
|
2481
|
|
|
# high precision time functions |
2482
|
|
|
def sys_set_fps(fps): |
2483
|
|
|
"""Set the maximum frame rate. |
2484
|
|
|
|
2485
|
|
|
You can disable the frame limit again by setting fps to 0. |
2486
|
|
|
|
2487
|
|
|
Args: |
2488
|
|
|
fps (int): A frame rate limit (i.e. 60) |
2489
|
|
|
""" |
2490
|
|
|
lib.TCOD_sys_set_fps(fps) |
2491
|
|
|
|
2492
|
|
|
def sys_get_fps(): |
2493
|
|
|
"""Return the current frames per second. |
2494
|
|
|
|
2495
|
|
|
This the actual frame rate, not the frame limit set by |
2496
|
|
|
:any:`tcod.sys_set_fps`. |
2497
|
|
|
|
2498
|
|
|
This number is updated every second. |
2499
|
|
|
|
2500
|
|
|
Returns: |
2501
|
|
|
int: The currently measured frame rate. |
2502
|
|
|
""" |
2503
|
|
|
return lib.TCOD_sys_get_fps() |
2504
|
|
|
|
2505
|
|
|
def sys_get_last_frame_length(): |
2506
|
|
|
"""Return the delta time of the last rendered frame in seconds. |
2507
|
|
|
|
2508
|
|
|
Returns: |
2509
|
|
|
float: The delta time of the last rendered frame. |
2510
|
|
|
""" |
2511
|
|
|
return lib.TCOD_sys_get_last_frame_length() |
2512
|
|
|
|
2513
|
|
|
def sys_sleep_milli(val): |
2514
|
|
|
"""Sleep for 'val' milliseconds. |
2515
|
|
|
|
2516
|
|
|
Args: |
2517
|
|
|
val (int): Time to sleep for in milliseconds. |
2518
|
|
|
|
2519
|
|
|
.. deprecated:: 2.0 |
2520
|
|
|
Use :any:`time.sleep` instead. |
2521
|
|
|
""" |
2522
|
|
|
lib.TCOD_sys_sleep_milli(val) |
2523
|
|
|
|
2524
|
|
|
def sys_elapsed_milli(): |
2525
|
|
|
"""Get number of milliseconds since the start of the program. |
2526
|
|
|
|
2527
|
|
|
Returns: |
2528
|
|
|
int: Time since the progeam has started in milliseconds. |
2529
|
|
|
|
2530
|
|
|
.. deprecated:: 2.0 |
2531
|
|
|
Use :any:`time.clock` instead. |
2532
|
|
|
""" |
2533
|
|
|
return lib.TCOD_sys_elapsed_milli() |
2534
|
|
|
|
2535
|
|
|
def sys_elapsed_seconds(): |
2536
|
|
|
"""Get number of seconds since the start of the program. |
2537
|
|
|
|
2538
|
|
|
Returns: |
2539
|
|
|
float: Time since the progeam has started in seconds. |
2540
|
|
|
|
2541
|
|
|
.. deprecated:: 2.0 |
2542
|
|
|
Use :any:`time.clock` instead. |
2543
|
|
|
""" |
2544
|
|
|
return lib.TCOD_sys_elapsed_seconds() |
2545
|
|
|
|
2546
|
|
|
def sys_set_renderer(renderer): |
2547
|
|
|
"""Change the current rendering mode to renderer. |
2548
|
|
|
|
2549
|
|
|
.. deprecated:: 2.0 |
2550
|
|
|
RENDERER_GLSL and RENDERER_OPENGL are not currently available. |
2551
|
|
|
""" |
2552
|
|
|
lib.TCOD_sys_set_renderer(renderer) |
2553
|
|
|
|
2554
|
|
|
def sys_get_renderer(): |
2555
|
|
|
"""Return the current rendering mode. |
2556
|
|
|
|
2557
|
|
|
""" |
2558
|
|
|
return lib.TCOD_sys_get_renderer() |
2559
|
|
|
|
2560
|
|
|
# easy screenshots |
2561
|
|
|
def sys_save_screenshot(name=None): |
2562
|
|
|
"""Save a screenshot to a file. |
2563
|
|
|
|
2564
|
|
|
By default this will automatically save screenshots in the working |
2565
|
|
|
directory. |
2566
|
|
|
|
2567
|
|
|
The automatic names are formatted as screenshotNNN.png. For example: |
2568
|
|
|
screenshot000.png, screenshot001.png, etc. Whichever is available first. |
2569
|
|
|
|
2570
|
|
|
Args: |
2571
|
|
|
file Optional[AnyStr]: File path to save screenshot. |
2572
|
|
|
""" |
2573
|
|
|
if name is not None: |
2574
|
|
|
name = _bytes(name) |
2575
|
|
|
lib.TCOD_sys_save_screenshot(name or ffi.NULL) |
2576
|
|
|
|
2577
|
|
|
# custom fullscreen resolution |
2578
|
|
|
def sys_force_fullscreen_resolution(width, height): |
2579
|
|
|
"""Force a specific resolution in fullscreen. |
2580
|
|
|
|
2581
|
|
|
Will use the smallest available resolution so that: |
2582
|
|
|
|
2583
|
|
|
* resolution width >= width and |
2584
|
|
|
resolution width >= root console width * font char width |
2585
|
|
|
* resolution height >= height and |
2586
|
|
|
resolution height >= root console height * font char height |
2587
|
|
|
|
2588
|
|
|
Args: |
2589
|
|
|
width (int): The desired resolution width. |
2590
|
|
|
height (int): The desired resolution height. |
2591
|
|
|
""" |
2592
|
|
|
lib.TCOD_sys_force_fullscreen_resolution(width, height) |
2593
|
|
|
|
2594
|
|
|
def sys_get_current_resolution(): |
2595
|
|
|
"""Return the current resolution as (width, height) |
2596
|
|
|
|
2597
|
|
|
Returns: |
2598
|
|
|
Tuple[int,int]: The current resolution. |
2599
|
|
|
""" |
2600
|
|
|
w = ffi.new('int *') |
|
|
|
|
2601
|
|
|
h = ffi.new('int *') |
|
|
|
|
2602
|
|
|
lib.TCOD_sys_get_current_resolution(w, h) |
2603
|
|
|
return w[0], h[0] |
2604
|
|
|
|
2605
|
|
|
def sys_get_char_size(): |
2606
|
|
|
"""Return the current fonts character size as (width, height) |
2607
|
|
|
|
2608
|
|
|
Returns: |
2609
|
|
|
Tuple[int,int]: The current font glyph size in (width, height) |
2610
|
|
|
""" |
2611
|
|
|
w = ffi.new('int *') |
|
|
|
|
2612
|
|
|
h = ffi.new('int *') |
|
|
|
|
2613
|
|
|
lib.TCOD_sys_get_char_size(w, h) |
2614
|
|
|
return w[0], h[0] |
2615
|
|
|
|
2616
|
|
|
# update font bitmap |
2617
|
|
|
def sys_update_char(asciiCode, fontx, fonty, img, x, y): |
|
|
|
|
2618
|
|
|
"""Dynamically update the current frot with img. |
2619
|
|
|
|
2620
|
|
|
All cells using this asciiCode will be updated |
2621
|
|
|
at the next call to :any:`tcod.console_flush`. |
2622
|
|
|
|
2623
|
|
|
Args: |
2624
|
|
|
asciiCode (int): Ascii code corresponding to the character to update. |
2625
|
|
|
fontx (int): Left coordinate of the character |
2626
|
|
|
in the bitmap font (in tiles) |
2627
|
|
|
fonty (int): Top coordinate of the character |
2628
|
|
|
in the bitmap font (in tiles) |
2629
|
|
|
img (Image): An image containing the new character bitmap. |
2630
|
|
|
x (int): Left pixel of the character in the image. |
2631
|
|
|
y (int): Top pixel of the character in the image. |
2632
|
|
|
""" |
2633
|
|
|
lib.TCOD_sys_update_char(_int(asciiCode), fontx, fonty, img, x, y) |
2634
|
|
|
|
2635
|
|
|
def sys_register_SDL_renderer(callback): |
|
|
|
|
2636
|
|
|
"""Register a custom randering function with libtcod. |
2637
|
|
|
|
2638
|
|
|
Note: |
2639
|
|
|
This callback will only be called by the SDL renderer. |
2640
|
|
|
|
2641
|
|
|
The callack will receive a :any:`CData <ffi-cdata>` void* to an |
2642
|
|
|
SDL_Surface* struct. |
2643
|
|
|
|
2644
|
|
|
The callback is called on every call to :any:`tcod.console_flush`. |
2645
|
|
|
|
2646
|
|
|
Args: |
2647
|
|
|
callback Callable[[CData], None]: |
2648
|
|
|
A function which takes a single argument. |
2649
|
|
|
""" |
2650
|
|
|
with _PropagateException() as propagate: |
2651
|
|
|
@ffi.def_extern(onerror=propagate) |
2652
|
|
|
def _pycall_sdl_hook(sdl_surface): |
|
|
|
|
2653
|
|
|
callback(sdl_surface) |
2654
|
|
|
lib.TCOD_sys_register_SDL_renderer(lib._pycall_sdl_hook) |
2655
|
|
|
|
2656
|
|
|
def sys_check_for_event(mask, k, m): |
|
|
|
|
2657
|
|
|
"""Check for and return an event. |
2658
|
|
|
|
2659
|
|
|
Args: |
2660
|
|
|
mask (int): :any:`Event types` to wait for. |
2661
|
|
|
k (Optional[Key]): A tcod.Key instance which might be updated with |
2662
|
|
|
an event. Can be None. |
2663
|
|
|
m (Optional[Mouse]): A tcod.Mouse instance which might be updated |
2664
|
|
|
with an event. Can be None. |
2665
|
|
|
""" |
2666
|
|
|
return lib.TCOD_sys_check_for_event( |
2667
|
|
|
mask, k.cdata if k else ffi.NULL, m.cdata if m else ffi.NULL) |
2668
|
|
|
|
2669
|
|
|
def sys_wait_for_event(mask, k, m, flush): |
|
|
|
|
2670
|
|
|
"""Wait for an event then return. |
2671
|
|
|
|
2672
|
|
|
If flush is True then the buffer will be cleared before waiting. Otherwise |
2673
|
|
|
each available event will be returned in the order they're recieved. |
2674
|
|
|
|
2675
|
|
|
Args: |
2676
|
|
|
mask (int): :any:`Event types` to wait for. |
2677
|
|
|
k (Optional[Key]): A tcod.Key instance which might be updated with |
2678
|
|
|
an event. Can be None. |
2679
|
|
|
m (Optional[Mouse]): A tcod.Mouse instance which might be updated |
2680
|
|
|
with an event. Can be None. |
2681
|
|
|
flush (bool): Clear the event buffer before waiting. |
2682
|
|
|
""" |
2683
|
|
|
return lib.TCOD_sys_wait_for_event( |
2684
|
|
|
mask, k.cdata if k else ffi.NULL, m.cdata if m else ffi.NULL, flush) |
2685
|
|
|
|
2686
|
|
|
def sys_clipboard_set(text): |
|
|
|
|
2687
|
|
|
return lib.TCOD_sys_clipboard_set(text.encode('utf-8')) |
2688
|
|
|
|
2689
|
|
|
def sys_clipboard_get(): |
|
|
|
|
2690
|
|
|
return ffi.string(lib.TCOD_sys_clipboard_get()).decode('utf-8') |
2691
|
|
|
|
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.