1- r"""
1+ r"""Libtcod's Binary Space Partitioning.
2+
23The following example shows how to traverse the BSP tree using Python. This
34assumes `create_room` and `connect_rooms` will be replaced by custom code.
45
56Example::
67
7- import tcod
8+ import tcod.bsp
89
910 bsp = tcod.bsp.BSP(x=0, y=0, width=80, height=60)
1011 bsp.split_recursive(
2526"""
2627from __future__ import annotations
2728
28- from typing import Any , Iterator , List , Optional , Tuple , Union # noqa: F401
29+ from typing import Any , Iterator
2930
3031import tcod .random
3132from tcod ._internal import deprecate
3233from tcod .loader import ffi , lib
3334
3435
35- class BSP ( object ) :
36+ class BSP :
3637 """A binary space partitioning tree which can be used for simple dungeon generation.
3738
3839 Attributes:
@@ -55,7 +56,8 @@ class BSP(object):
5556 height (int): Rectangle height.
5657 """
5758
58- def __init__ (self , x : int , y : int , width : int , height : int ):
59+ def __init__ (self , x : int , y : int , width : int , height : int ) -> None :
60+ """Initialize a root node of a BSP tree."""
5961 self .x = x
6062 self .y = y
6163 self .width = width
@@ -65,19 +67,21 @@ def __init__(self, x: int, y: int, width: int, height: int):
6567 self .position = 0
6668 self .horizontal = False
6769
68- self .parent : Optional [ BSP ] = None
69- self .children : Union [ Tuple [ ()], Tuple [BSP , BSP ] ] = ()
70+ self .parent : BSP | None = None
71+ self .children : tuple [ ()] | tuple [BSP , BSP ] = ()
7072
7173 @property
72- def w (self ) -> int :
74+ @deprecate ("This attribute has been renamed to `width`." , FutureWarning )
75+ def w (self ) -> int : # noqa: D102
7376 return self .width
7477
7578 @w .setter
7679 def w (self , value : int ) -> None :
7780 self .width = value
7881
7982 @property
80- def h (self ) -> int :
83+ @deprecate ("This attribute has been renamed to `height`." , FutureWarning )
84+ def h (self ) -> int : # noqa: D102
8185 return self .height
8286
8387 @h .setter
@@ -92,7 +96,7 @@ def _as_cdata(self) -> Any:
9296 cdata .level = self .level
9397 return cdata
9498
95- def __str__ (self ) -> str :
99+ def __repr__ (self ) -> str :
96100 """Provide a useful readout when printed."""
97101 status = "leaf"
98102 if self .children :
@@ -101,7 +105,7 @@ def __str__(self) -> str:
101105 self .horizontal ,
102106 )
103107
104- return "<%s(x=%i,y=%i,width=%i,height=%i)level=%i, %s>" % (
108+ return "<%s(x=%i,y=%i,width=%i,height=%i) level=%i %s>" % (
105109 self .__class__ .__name__ ,
106110 self .x ,
107111 self .y ,
@@ -131,21 +135,21 @@ def split_once(self, horizontal: bool, position: int) -> None:
131135 """Split this partition into 2 sub-partitions.
132136
133137 Args:
134- horizontal (bool):
135- position (int):
138+ horizontal (bool): If True then the sub-partition is split into an upper and bottom half.
139+ position (int): The position of where to put the divider relative to the current node.
136140 """
137141 cdata = self ._as_cdata ()
138142 lib .TCOD_bsp_split_once (cdata , horizontal , position )
139143 self ._unpack_bsp_tree (cdata )
140144
141- def split_recursive (
145+ def split_recursive ( # noqa: PLR0913
142146 self ,
143147 depth : int ,
144148 min_width : int ,
145149 min_height : int ,
146150 max_horizontal_ratio : float ,
147151 max_vertical_ratio : float ,
148- seed : Optional [ tcod .random .Random ] = None ,
152+ seed : tcod .random .Random | None = None ,
149153 ) -> None :
150154 """Divide this partition recursively.
151155
@@ -229,7 +233,7 @@ def inverted_level_order(self) -> Iterator[BSP]:
229233
230234 .. versionadded:: 8.3
231235 """
232- levels : List [ List [BSP ]] = []
236+ levels : list [ list [BSP ]] = []
233237 next = [self ]
234238 while next :
235239 levels .append (next )
@@ -253,16 +257,16 @@ def contains(self, x: int, y: int) -> bool:
253257 """
254258 return self .x <= x < self .x + self .width and self .y <= y < self .y + self .height
255259
256- def find_node (self , x : int , y : int ) -> Optional [ BSP ] :
260+ def find_node (self , x : int , y : int ) -> BSP | None :
257261 """Return the deepest node which contains these coordinates.
258262
259263 Returns:
260- Optional[BSP]: BSP object or None.
264+ BSP object or None.
261265 """
262266 if not self .contains (x , y ):
263267 return None
264268 for child in self .children :
265- found : Optional [ BSP ] = child .find_node (x , y )
269+ found = child .find_node (x , y )
266270 if found :
267271 return found
268272 return self
0 commit comments