1
|
|
|
""" |
2
|
|
|
Rogue-like map utilitys such as line-of-sight, field-of-view, and path-finding. |
|
|
|
|
3
|
|
|
|
4
|
|
|
.. deprecated:: 3.2 |
5
|
|
|
The features provided here are better realized in the |
6
|
|
|
:any:`tcod.map` and :any:`tcod.path` modules. |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from __future__ import absolute_import |
10
|
|
|
|
11
|
|
|
import itertools as _itertools |
12
|
|
|
import math as _math |
13
|
|
|
|
14
|
|
|
from tcod import ffi as _ffi |
15
|
|
|
from tcod import lib as _lib |
16
|
|
|
|
17
|
|
|
import tdl as _tdl |
18
|
|
|
from . import style as _style |
19
|
|
|
|
20
|
|
|
_FOVTYPES = {'BASIC' : 0, 'DIAMOND': 1, 'SHADOW': 2, 'RESTRICTIVE': 12, 'PERMISSIVE': 11} |
|
|
|
|
21
|
|
|
|
22
|
|
|
def _get_fov_type(fov): |
23
|
|
|
"Return a FOV from a string" |
24
|
|
|
oldFOV = fov |
|
|
|
|
25
|
|
|
fov = str(fov).upper() |
26
|
|
|
if fov in _FOVTYPES: |
27
|
|
|
return _FOVTYPES[fov] |
28
|
|
|
if fov[:10] == 'PERMISSIVE' and fov[10].isdigit() and fov[10] != '9': |
29
|
|
|
return 4 + int(fov[10]) |
30
|
|
|
raise _tdl.TDLError('No such fov option as %s' % oldFOV) |
31
|
|
|
|
32
|
|
|
class Map(object): |
33
|
|
|
"""Fast field-of-view and path-finding on stored data. |
34
|
|
|
|
35
|
|
|
.. deprecated:: 3.2 |
36
|
|
|
:any:`tcod.map.Map` should be used instead. |
37
|
|
|
|
38
|
|
|
Set map conditions with the walkable and transparency attributes, this |
39
|
|
|
object can be iterated and checked for containment similar to consoles. |
40
|
|
|
|
41
|
|
|
For example, you can set all tiles and transparent and walkable with the |
42
|
|
|
following code: |
43
|
|
|
|
44
|
|
|
Example: |
45
|
|
|
>>> import tdl.map |
46
|
|
|
>>> map_ = tdl.map.Map(80, 60) |
47
|
|
|
>>> for x,y in map_: |
48
|
|
|
... map_.transparent[x,y] = True |
49
|
|
|
... map_.walkable[x,y] = True |
50
|
|
|
|
51
|
|
|
Attributes: |
52
|
|
|
transparent: Map transparency |
53
|
|
|
|
54
|
|
|
Access this attribute with ``map.transparent[x,y]`` |
55
|
|
|
|
56
|
|
|
Set to True to allow field-of-view rays, False will |
57
|
|
|
block field-of-view. |
58
|
|
|
|
59
|
|
|
Transparent tiles only affect field-of-view. |
60
|
|
|
walkable: Map accessibility |
61
|
|
|
|
62
|
|
|
Access this attribute with ``map.walkable[x,y]`` |
63
|
|
|
|
64
|
|
|
Set to True to allow path-finding through that tile, |
65
|
|
|
False will block passage to that tile. |
66
|
|
|
|
67
|
|
|
Walkable tiles only affect path-finding. |
68
|
|
|
|
69
|
|
|
fov: Map tiles touched by a field-of-view computation. |
70
|
|
|
|
71
|
|
|
Access this attribute with ``map.fov[x,y]`` |
72
|
|
|
|
73
|
|
|
Is True if a the tile is if view, otherwise False. |
74
|
|
|
|
75
|
|
|
You can set to this attribute if you want, but you'll typically |
76
|
|
|
be using it to read the field-of-view of a :any:`compute_fov` call. |
77
|
|
|
""" |
78
|
|
|
|
79
|
|
|
class _MapAttribute(object): |
|
|
|
|
80
|
|
|
def __init__(self, map, bit_index): |
|
|
|
|
81
|
|
|
self.map = map |
82
|
|
|
self.bit_index = bit_index |
83
|
|
|
self.bit = 1 << bit_index |
84
|
|
|
self.bit_inverse = 0xFF ^ self.bit |
85
|
|
|
|
86
|
|
|
def __getitem__(self, key): |
87
|
|
|
return bool(self.map._array_cdata[key[1]][key[0]] & self.bit) |
88
|
|
|
|
89
|
|
|
def __setitem__(self, key, value): |
90
|
|
|
self.map._array_cdata[key[1]][key[0]] = ( |
91
|
|
|
(self.map._array_cdata[key[1]][key[0]] & self.bit_inverse) | |
92
|
|
|
(self.bit * bool(value)) |
93
|
|
|
) |
94
|
|
|
|
95
|
|
|
def __init__(self, width, height): |
96
|
|
|
"""Create a new Map with width and height.""" |
97
|
|
|
self.width = width |
98
|
|
|
self.height = height |
99
|
|
|
self._map_cdata = _lib.TCOD_map_new(width, height) |
100
|
|
|
# cast array into cdata format: uint8[y][x] |
101
|
|
|
# for quick Python access |
102
|
|
|
self._array_cdata = _ffi.new('uint8_t[%i][%i]' % (height, width)) |
103
|
|
|
# flat array to pass to TDL's C helpers |
104
|
|
|
self._array_cdata_flat = _ffi.cast('uint8_t *', self._array_cdata) |
105
|
|
|
self.transparent = self._MapAttribute(self, 0) |
106
|
|
|
self.walkable = self._MapAttribute(self, 1) |
107
|
|
|
self.fov = self._MapAttribute(self, 2) |
108
|
|
|
|
109
|
|
|
def compute_fov(self, x, y, fov='PERMISSIVE', radius=None, light_walls=True, |
|
|
|
|
110
|
|
|
sphere=True, cumulative=False): |
|
|
|
|
111
|
|
|
"""Compute the field-of-view of this Map and return an iterator of the |
112
|
|
|
points touched. |
113
|
|
|
|
114
|
|
|
Args: |
115
|
|
|
x (int): Point of view, x-coordinate. |
116
|
|
|
y (int): Point of view, y-coordinate. |
117
|
|
|
fov (Text): The type of field-of-view to be used. |
118
|
|
|
|
119
|
|
|
Available types are: |
120
|
|
|
'BASIC', 'DIAMOND', 'SHADOW', 'RESTRICTIVE', 'PERMISSIVE', |
121
|
|
|
'PERMISSIVE0', 'PERMISSIVE1', ..., 'PERMISSIVE8' |
122
|
|
|
radius (Optional[int]): Maximum view distance from the point of |
123
|
|
|
view. |
124
|
|
|
|
125
|
|
|
A value of 0 will give an infinite distance. |
126
|
|
|
light_walls (bool): Light up walls, or only the floor. |
127
|
|
|
sphere (bool): If True the lit area will be round instead of |
128
|
|
|
square. |
129
|
|
|
cumulative (bool): If True the lit cells will accumulate instead |
130
|
|
|
of being cleared before the computation. |
131
|
|
|
|
132
|
|
|
Returns: |
133
|
|
|
Iterator[Tuple[int, int]]: An iterator of (x, y) points of tiles |
134
|
|
|
touched by the field-of-view. |
135
|
|
|
|
136
|
|
|
Unexpected behaviour can happen if you modify the Map while |
137
|
|
|
using the iterator. |
138
|
|
|
|
139
|
|
|
You can use the Map's fov attribute as an alternative to this |
140
|
|
|
iterator. |
141
|
|
|
""" |
142
|
|
|
# refresh cdata |
143
|
|
|
_lib.TDL_map_data_from_buffer(self._map_cdata, |
144
|
|
|
self._array_cdata_flat) |
145
|
|
|
if radius is None: # infinite radius |
146
|
|
|
radius = max(self.width, self.height) |
147
|
|
|
_lib.TCOD_map_compute_fov(self._map_cdata, x, y, radius, light_walls, |
148
|
|
|
_get_fov_type(fov)) |
149
|
|
|
_lib.TDL_map_fov_to_buffer(self._map_cdata, |
150
|
|
|
self._array_cdata_flat, cumulative) |
151
|
|
|
def iterate_fov(): |
|
|
|
|
152
|
|
|
_array_cdata = self._array_cdata |
153
|
|
|
for y in range(self.height): |
|
|
|
|
154
|
|
|
for x in range(self.width): |
|
|
|
|
155
|
|
|
if(_array_cdata[y][x] & 4): |
|
|
|
|
156
|
|
|
yield (x, y) |
157
|
|
|
return iterate_fov() |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
def compute_path(self, start_x, start_y, dest_x, dest_y, |
162
|
|
|
diagonal_cost=_math.sqrt(2)): |
163
|
|
|
"""Get the shortest path between two points. |
164
|
|
|
|
165
|
|
|
Args: |
166
|
|
|
start_x (int): Starting x-position. |
167
|
|
|
start_y (int): Starting y-position. |
168
|
|
|
dest_x (int): Destination x-position. |
169
|
|
|
dest_y (int): Destination y-position. |
170
|
|
|
diagonal_cost (float): Multiplier for diagonal movement. |
171
|
|
|
|
172
|
|
|
Can be set to zero to disable diagonal movement entirely. |
173
|
|
|
|
174
|
|
|
Returns: |
175
|
|
|
List[Tuple[int, int]]: The shortest list of points to the |
176
|
|
|
destination position from the starting position. |
177
|
|
|
|
178
|
|
|
The start point is not included in this list. |
179
|
|
|
""" |
180
|
|
|
# refresh cdata |
181
|
|
|
_lib.TDL_map_data_from_buffer(self._map_cdata, |
182
|
|
|
self._array_cdata_flat) |
183
|
|
|
path_cdata = _lib.TCOD_path_new_using_map(self._map_cdata, diagonal_cost) |
|
|
|
|
184
|
|
|
try: |
185
|
|
|
_lib.TCOD_path_compute(path_cdata, start_x, start_y, dest_x, dest_y) |
186
|
|
|
x = _ffi.new('int *') |
|
|
|
|
187
|
|
|
y = _ffi.new('int *') |
|
|
|
|
188
|
|
|
length = _lib.TCOD_path_size(path_cdata) |
189
|
|
|
path = [None] * length |
190
|
|
|
for i in range(length): |
191
|
|
|
_lib.TCOD_path_get(path_cdata, i, x, y) |
192
|
|
|
path[i] = ((x[0], y[0])) |
193
|
|
|
finally: |
194
|
|
|
_lib.TCOD_path_delete(path_cdata) |
195
|
|
|
return path |
196
|
|
|
|
197
|
|
|
def __iter__(self): |
198
|
|
|
return _itertools.product(range(self.width), range(self.height)) |
199
|
|
|
|
200
|
|
|
def __contains__(self, position): |
201
|
|
|
x, y = position |
|
|
|
|
202
|
|
|
return (0 <= x < self.width) and (0 <= y < self.height) |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
206
|
|
|
class AStar(object): |
207
|
|
|
"""An A* pathfinder using a callback. |
208
|
|
|
|
209
|
|
|
.. deprecated:: 3.2 |
210
|
|
|
See :any:`tcod.path`. |
211
|
|
|
|
212
|
|
|
Before crating this instance you should make one of two types of |
213
|
|
|
callbacks: |
214
|
|
|
|
215
|
|
|
- A function that returns the cost to move to (x, y) |
216
|
|
|
- A function that returns the cost to move between |
217
|
|
|
(destX, destY, sourceX, sourceY) |
218
|
|
|
|
219
|
|
|
If path is blocked the function should return zero or None. |
220
|
|
|
When using the second type of callback be sure to set advanced=True |
221
|
|
|
|
222
|
|
|
Args: |
223
|
|
|
width (int): Width of the pathfinding area (in tiles.) |
224
|
|
|
height (int): Height of the pathfinding area (in tiles.) |
225
|
|
|
callback (Union[Callable[[int, int], float], |
226
|
|
|
Callable[[int, int, int, int], float]]): A callback |
227
|
|
|
returning the cost of a tile or edge. |
228
|
|
|
|
229
|
|
|
A callback taking parameters depending on the setting |
230
|
|
|
of 'advanced' and returning the cost of |
231
|
|
|
movement for an open tile or zero for a |
232
|
|
|
blocked tile. |
233
|
|
|
diagnalCost (float): Multiplier for diagonal movement. |
234
|
|
|
|
235
|
|
|
Can be set to zero to disable diagonal movement entirely. |
236
|
|
|
advanced (bool): Give 2 additional parameters to the callback. |
237
|
|
|
|
238
|
|
|
A simple callback with 2 positional parameters may not |
239
|
|
|
provide enough information. Setting this to True will |
240
|
|
|
call the callback with 2 additional parameters giving |
241
|
|
|
you both the destination and the source of movement. |
242
|
|
|
|
243
|
|
|
When True the callback will need to accept |
244
|
|
|
(destX, destY, sourceX, sourceY) as parameters. |
245
|
|
|
Instead of just (destX, destY). |
246
|
|
|
""" |
247
|
|
|
|
248
|
|
|
__slots__ = ('_as_parameter_', '_callback', '__weakref__') |
249
|
|
|
|
250
|
|
|
def __init__(self, width, height, callback, |
251
|
|
|
diagnalCost=_math.sqrt(2), advanced=False): |
252
|
|
|
if not diagnalCost: # set None or False to zero |
253
|
|
|
diagnalCost = 0.0 |
254
|
|
|
if advanced: |
255
|
|
|
def newCallback(sourceX, sourceY, destX, destY, null): |
|
|
|
|
256
|
|
|
pathCost = callback(destX, destY, sourceX, sourceY) |
|
|
|
|
257
|
|
|
if pathCost: |
258
|
|
|
return pathCost |
259
|
|
|
return 0.0 |
260
|
|
|
else: |
261
|
|
|
def newCallback(sourceX, sourceY, destX, destY, null): |
|
|
|
|
262
|
|
|
pathCost = callback(destX, destY) # expecting a float or 0 |
|
|
|
|
263
|
|
|
if pathCost: |
264
|
|
|
return pathCost |
265
|
|
|
return 0.0 |
266
|
|
|
# float(int, int, int, int, void*) |
267
|
|
|
self._callback = _ffi.callback('TCOD_path_func_t')(newCallback) |
268
|
|
|
|
269
|
|
|
self._as_parameter_ = _lib.TCOD_path_new_using_function(width, height, |
270
|
|
|
self._callback, _ffi.NULL, diagnalCost) |
271
|
|
|
|
272
|
|
|
def __del__(self): |
273
|
|
|
if self._as_parameter_: |
274
|
|
|
_lib.TCOD_path_delete(self._as_parameter_) |
275
|
|
|
self._as_parameter_ = None |
276
|
|
|
|
277
|
|
|
def get_path(self, origX, origY, destX, destY): |
|
|
|
|
278
|
|
|
""" |
279
|
|
|
Get the shortest path from origXY to destXY. |
280
|
|
|
|
281
|
|
|
Returns: |
282
|
|
|
List[Tuple[int, int]]: Returns a list walking the path from orig |
283
|
|
|
to dest. |
284
|
|
|
|
285
|
|
|
This excludes the starting point and includes the destination. |
286
|
|
|
|
287
|
|
|
If no path is found then an empty list is returned. |
288
|
|
|
""" |
289
|
|
|
found = _lib.TCOD_path_compute(self._as_parameter_, origX, origY, destX, destY) |
|
|
|
|
290
|
|
|
if not found: |
291
|
|
|
return [] # path not found |
292
|
|
|
x, y = _ffi.new('int *'), _ffi.new('int *') |
|
|
|
|
293
|
|
|
recalculate = True |
294
|
|
|
path = [] |
295
|
|
|
while _lib.TCOD_path_walk(self._as_parameter_, x, y, recalculate): |
296
|
|
|
path.append((x[0], y[0])) |
297
|
|
|
return path |
298
|
|
|
|
299
|
|
|
def quick_fov(x, y, callback, fov='PERMISSIVE', radius=7.5, lightWalls=True, sphere=True): |
|
|
|
|
300
|
|
|
"""All field-of-view functionality in one call. |
301
|
|
|
|
302
|
|
|
Before using this call be sure to make a function, lambda, or method that takes 2 |
|
|
|
|
303
|
|
|
positional parameters and returns True if light can pass through the tile or False |
|
|
|
|
304
|
|
|
for light-blocking tiles and for indexes that are out of bounds of the |
305
|
|
|
dungeon. |
306
|
|
|
|
307
|
|
|
This function is 'quick' as in no hassle but can quickly become a very slow |
308
|
|
|
function call if a large radius is used or the callback provided itself |
309
|
|
|
isn't optimized. |
310
|
|
|
|
311
|
|
|
Always check if the index is in bounds both in the callback and in the |
312
|
|
|
returned values. These values can go into the negatives as well. |
313
|
|
|
|
314
|
|
|
Args: |
315
|
|
|
x (int): x center of the field-of-view |
316
|
|
|
y (int): y center of the field-of-view |
317
|
|
|
callback (Callable[[int, int], bool]): |
318
|
|
|
|
319
|
|
|
This should be a function that takes two positional arguments x,y |
320
|
|
|
and returns True if the tile at that position is transparent |
321
|
|
|
or False if the tile blocks light or is out of bounds. |
322
|
|
|
fov (Text): The type of field-of-view to be used. |
323
|
|
|
|
324
|
|
|
Available types are: |
325
|
|
|
'BASIC', 'DIAMOND', 'SHADOW', 'RESTRICTIVE', 'PERMISSIVE', |
326
|
|
|
'PERMISSIVE0', 'PERMISSIVE1', ..., 'PERMISSIVE8' |
327
|
|
|
radius (float) Radius of the field-of-view. |
328
|
|
|
|
329
|
|
|
When sphere is True a floating point can be used to fine-tune |
330
|
|
|
the range. Otherwise the radius is just rounded up. |
331
|
|
|
|
332
|
|
|
Be careful as a large radius has an exponential affect on |
333
|
|
|
how long this function takes. |
334
|
|
|
lightWalls (bool): Include or exclude wall tiles in the field-of-view. |
335
|
|
|
sphere (bool): True for a spherical field-of-view. |
336
|
|
|
False for a square one. |
337
|
|
|
|
338
|
|
|
Returns: |
339
|
|
|
Set[Tuple[int, int]]: A set of (x, y) points that are within the |
340
|
|
|
field-of-view. |
341
|
|
|
""" |
342
|
|
|
trueRadius = radius |
|
|
|
|
343
|
|
|
radius = int(_math.ceil(radius)) |
344
|
|
|
mapSize = radius * 2 + 1 |
|
|
|
|
345
|
|
|
fov = _get_fov_type(fov) |
346
|
|
|
|
347
|
|
|
setProp = _lib.TCOD_map_set_properties # make local |
|
|
|
|
348
|
|
|
inFOV = _lib.TCOD_map_is_in_fov |
|
|
|
|
349
|
|
|
|
350
|
|
|
tcodMap = _lib.TCOD_map_new(mapSize, mapSize) |
|
|
|
|
351
|
|
|
try: |
352
|
|
|
# pass no.1, write callback data to the tcodMap |
353
|
|
|
for x_, y_ in _itertools.product(range(mapSize), range(mapSize)): |
|
|
|
|
354
|
|
|
pos = (x_ + x - radius, |
355
|
|
|
y_ + y - radius) |
356
|
|
|
transparent = bool(callback(*pos)) |
357
|
|
|
setProp(tcodMap, x_, y_, transparent, False) |
358
|
|
|
|
359
|
|
|
# pass no.2, compute fov and build a list of points |
360
|
|
|
_lib.TCOD_map_compute_fov(tcodMap, radius, radius, radius, lightWalls, fov) |
|
|
|
|
361
|
|
|
touched = set() # points touched by field of view |
362
|
|
|
for x_, y_ in _itertools.product(range(mapSize), range(mapSize)): |
|
|
|
|
363
|
|
|
if sphere and _math.hypot(x_ - radius, y_ - radius) > trueRadius: |
364
|
|
|
continue |
365
|
|
|
if inFOV(tcodMap, x_, y_): |
366
|
|
|
touched.add((x_ + x - radius, y_ + y - radius)) |
367
|
|
|
finally: |
368
|
|
|
_lib.TCOD_map_delete(tcodMap) |
369
|
|
|
return touched |
370
|
|
|
|
371
|
|
|
def bresenham(x1, y1, x2, y2): |
|
|
|
|
372
|
|
|
""" |
373
|
|
|
Return a list of points in a bresenham line. |
374
|
|
|
|
375
|
|
|
Implementation hastily copied from RogueBasin. |
376
|
|
|
|
377
|
|
|
Returns: |
378
|
|
|
List[Tuple[int, int]]: A list of (x, y) points, |
379
|
|
|
including both the start and end-points. |
380
|
|
|
""" |
381
|
|
|
points = [] |
382
|
|
|
issteep = abs(y2-y1) > abs(x2-x1) |
383
|
|
|
if issteep: |
384
|
|
|
x1, y1 = y1, x1 |
385
|
|
|
x2, y2 = y2, x2 |
386
|
|
|
rev = False |
387
|
|
|
if x1 > x2: |
388
|
|
|
x1, x2 = x2, x1 |
389
|
|
|
y1, y2 = y2, y1 |
390
|
|
|
rev = True |
391
|
|
|
deltax = x2 - x1 |
392
|
|
|
deltay = abs(y2-y1) |
393
|
|
|
error = int(deltax / 2) |
394
|
|
|
y = y1 |
|
|
|
|
395
|
|
|
ystep = None |
396
|
|
|
if y1 < y2: |
397
|
|
|
ystep = 1 |
398
|
|
|
else: |
399
|
|
|
ystep = -1 |
400
|
|
|
for x in range(x1, x2 + 1): |
|
|
|
|
401
|
|
|
if issteep: |
402
|
|
|
points.append((y, x)) |
403
|
|
|
else: |
404
|
|
|
points.append((x, y)) |
405
|
|
|
error -= deltay |
406
|
|
|
if error < 0: |
407
|
|
|
y += ystep |
|
|
|
|
408
|
|
|
error += deltax |
409
|
|
|
# Reverse the list if the coordinates were reversed |
410
|
|
|
if rev: |
411
|
|
|
points.reverse() |
412
|
|
|
return points |
413
|
|
|
|
414
|
|
|
|
415
|
|
|
__all__ = [_var for _var in locals().keys() if _var[0] != '_'] |
416
|
|
|
|
417
|
|
|
quickFOV = _style.backport(quick_fov) |
|
|
|
|
418
|
|
|
AStar.getPath = _style.backport(AStar.get_path) |
419
|
|
|
|
This check looks for lines that are too long. You can specify the maximum line length.