-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
bsp.py
276 lines (230 loc) · 8.24 KB
/
bsp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
r"""Libtcod's Binary Space Partitioning.
The following example shows how to traverse the BSP tree using Python. This
assumes `create_room` and `connect_rooms` will be replaced by custom code.
Example::
import tcod.bsp
bsp = tcod.bsp.BSP(x=0, y=0, width=80, height=60)
bsp.split_recursive(
depth=5,
min_width=3,
min_height=3,
max_horizontal_ratio=1.5,
max_vertical_ratio=1.5,
)
# In pre order, leaf nodes are visited before the nodes that connect them.
for node in bsp.pre_order():
if node.children:
node1, node2 = node.children
print('Connect the rooms:\n%s\n%s' % (node1, node2))
else:
print('Dig a room for %s.' % node)
"""
from __future__ import annotations
from typing import Any, Iterator
import tcod.random
from tcod._internal import deprecate
from tcod.cffi import ffi, lib
class BSP:
"""A binary space partitioning tree which can be used for simple dungeon generation.
Attributes:
x (int): Rectangle left coordinate.
y (int): Rectangle top coordinate.
width (int): Rectangle width.
height (int): Rectangle height.
level (int): This nodes depth.
position (int): The integer of where the node was split.
horizontal (bool): This nodes split orientation.
parent (Optional[BSP]): This nodes parent or None
children (Union[Tuple[()], Tuple[BSP, BSP]]):
A tuple of (left, right) BSP instances, or
an empty tuple if this BSP has no children.
Args:
x (int): Rectangle left coordinate.
y (int): Rectangle top coordinate.
width (int): Rectangle width.
height (int): Rectangle height.
"""
def __init__(self, x: int, y: int, width: int, height: int) -> None:
"""Initialize a root node of a BSP tree."""
self.x = x
self.y = y
self.width = width
self.height = height
self.level = 0
self.position = 0
self.horizontal = False
self.parent: BSP | None = None
self.children: tuple[()] | tuple[BSP, BSP] = ()
@property
@deprecate("This attribute has been renamed to `width`.", FutureWarning)
def w(self) -> int: # noqa: D102
return self.width
@w.setter
def w(self, value: int) -> None:
self.width = value
@property
@deprecate("This attribute has been renamed to `height`.", FutureWarning)
def h(self) -> int: # noqa: D102
return self.height
@h.setter
def h(self, value: int) -> None:
self.height = value
def _as_cdata(self) -> Any: # noqa: ANN401
cdata = ffi.gc(
lib.TCOD_bsp_new_with_size(self.x, self.y, self.width, self.height),
lib.TCOD_bsp_delete,
)
cdata.level = self.level
return cdata
def __repr__(self) -> str:
"""Provide a useful readout when printed."""
status = "leaf"
if self.children:
status = "split at position=%i,horizontal=%r" % (
self.position,
self.horizontal,
)
return "<%s(x=%i,y=%i,width=%i,height=%i) level=%i %s>" % (
self.__class__.__name__,
self.x,
self.y,
self.width,
self.height,
self.level,
status,
)
def _unpack_bsp_tree(self, cdata: Any) -> None: # noqa: ANN401
self.x = cdata.x
self.y = cdata.y
self.width = cdata.w
self.height = cdata.h
self.level = cdata.level
self.position = cdata.position
self.horizontal = bool(cdata.horizontal)
if lib.TCOD_bsp_is_leaf(cdata):
return
self.children = (BSP(0, 0, 0, 0), BSP(0, 0, 0, 0))
self.children[0].parent = self
self.children[0]._unpack_bsp_tree(lib.TCOD_bsp_left(cdata))
self.children[1].parent = self
self.children[1]._unpack_bsp_tree(lib.TCOD_bsp_right(cdata))
def split_once(self, horizontal: bool, position: int) -> None:
"""Split this partition into 2 sub-partitions.
Args:
horizontal (bool): If True then the sub-partition is split into an upper and bottom half.
position (int): The position of where to put the divider relative to the current node.
"""
cdata = self._as_cdata()
lib.TCOD_bsp_split_once(cdata, horizontal, position)
self._unpack_bsp_tree(cdata)
def split_recursive( # noqa: PLR0913
self,
depth: int,
min_width: int,
min_height: int,
max_horizontal_ratio: float,
max_vertical_ratio: float,
seed: tcod.random.Random | None = None,
) -> None:
"""Divide this partition recursively.
Args:
depth (int): The maximum depth to divide this object recursively.
min_width (int): The minimum width of any individual partition.
min_height (int): The minimum height of any individual partition.
max_horizontal_ratio (float):
Prevent creating a horizontal ratio more extreme than this.
max_vertical_ratio (float):
Prevent creating a vertical ratio more extreme than this.
seed (Optional[tcod.random.Random]):
The random number generator to use.
"""
cdata = self._as_cdata()
lib.TCOD_bsp_split_recursive(
cdata,
seed or ffi.NULL,
depth,
min_width,
min_height,
max_horizontal_ratio,
max_vertical_ratio,
)
self._unpack_bsp_tree(cdata)
@deprecate("Use pre_order method instead of walk.")
def walk(self) -> Iterator[BSP]:
"""Iterate over this BSP's hierarchy in pre order.
.. deprecated:: 2.3
Use :any:`pre_order` instead.
"""
return self.post_order()
def pre_order(self) -> Iterator[BSP]:
"""Iterate over this BSP's hierarchy in pre order.
.. versionadded:: 8.3
"""
yield self
for child in self.children:
yield from child.pre_order()
def in_order(self) -> Iterator[BSP]:
"""Iterate over this BSP's hierarchy in order.
.. versionadded:: 8.3
"""
if self.children:
yield from self.children[0].in_order()
yield self
yield from self.children[1].in_order()
else:
yield self
def post_order(self) -> Iterator[BSP]:
"""Iterate over this BSP's hierarchy in post order.
.. versionadded:: 8.3
"""
for child in self.children:
yield from child.post_order()
yield self
def level_order(self) -> Iterator[BSP]:
"""Iterate over this BSP's hierarchy in level order.
.. versionadded:: 8.3
"""
next = [self]
while next:
level = next
next = []
yield from level
for node in level:
next.extend(node.children)
def inverted_level_order(self) -> Iterator[BSP]:
"""Iterate over this BSP's hierarchy in inverse level order.
.. versionadded:: 8.3
"""
levels: list[list[BSP]] = []
next = [self]
while next:
levels.append(next)
level = next
next = []
for node in level:
next.extend(node.children)
while levels:
yield from levels.pop()
def contains(self, x: int, y: int) -> bool:
"""Return True if this node contains these coordinates.
Args:
x (int): X position to check.
y (int): Y position to check.
Returns:
bool: True if this node contains these coordinates.
Otherwise False.
"""
return self.x <= x < self.x + self.width and self.y <= y < self.y + self.height
def find_node(self, x: int, y: int) -> BSP | None:
"""Return the deepest node which contains these coordinates.
Returns:
BSP object or None.
"""
if not self.contains(x, y):
return None
for child in self.children:
found = child.find_node(x, y)
if found:
return found
return self
__all__ = ["BSP"]