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