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