< index
< 14. BSP toolkit
< 14.1 Creating a BSP tree

=====================================
14.2 Splitting a BSP tree
=====================================

> 14.3 Resizing a BSP tree
Once you have the root node, you can split it into two smaller non-overlapping nodes.

C++ : void TCODBsp::splitOnce(bool horizontal, int position)
C   : void TCOD_bsp_split_once(TCOD_bsp_t *node, bool horizontal, int position)
Py  : bsp_split_once(node, horizontal, position)

ParameterDescription
nodeIn the C version, the root node created with TCOD_bsp_new_with_size, or a node obtained by splitting.
horizontalIf true, the node will be splitted horizontally, else, vertically.
positionCoordinate of the splitting position.
If horizontal is true, x <= position < x+w
Else, y <= position < y+h

Example :

C++ : TCODBsp *myBSP = new TCODBsp(0,0,50,50);
      myBSP->splitOnce(true,20); // horizontal split into two nodes : (0,0,50,20) and (0,20,50,30)
C   : TCOD_bsp_t *my_bsp=TCOD_bsp_new_with_size(0,0,50,50);
      TCOD_bsp_split_once(my_bsp,false,20); /* vertical split into two nodes : (0,0,20,50) and (20,0,30,50)
Py  : my_bsp=libtcod.bsp_new_with_size(0,0,50,50)
      libtcod.bsp_split_once(my_bsp,False,20) # vertical split into two nodes : (0,0,20,50) and (20,0,30,50)


You can also recursively split the bsp. At each step, a random orientation (horizontal/vertical) and position are choosen :

C++ : void TCODBsp::splitRecursive(TCODRandom *randomizer, int nb, int minHSize, int maxHRatio, int minVSize, int maxVRatio)
C   : void TCOD_bsp_split_recursive(TCOD_bsp_t *node, TCOD_random_t randomizer, int nb, int minHSize, int minVSize, float maxHRatio, float maxVRatio)
Py  : bsp_split_recursive(node, randomizer, nb, minHSize, minVSize, maxHRatio, maxVRatio)

ParameterDescription
nodeIn the C version, the root node created with TCOD_bsp_new_with_size, or a node obtained by splitting.
randomizerThe random number generator to use. Use NULL for the default one.
nbNumber of recursion levels.
minHSize, minVSizeminimum values of w and h for a node. A node is splitted only if the resulting sub-nodes are bigger than minHSize x minVSize
maxHRatio, maxVRationmaximum values of w/h and h/w for a node. If a node does not conform, the splitting orientation is forced to reduce either the w/h or the h/w ratio. Use values near 1.0 to promote square nodes.

Example :
Do a 4 levels BSP tree (the region is splitted into a maximum of 2*2*2*2 sub-regions).

C++ : TCODBsp *myBSP = new TCODBsp(0,0,50,50);
      myBSP->splitRecursive(NULL,4,5,5,1.5f,1.5f); 
C   : TCOD_bsp_t *my_bsp=TCOD_bsp_new_with_size(0,0,50,50);
      TCOD_bsp_split_recursive(my_bsp,NULL,4,5,5,1.5f,1.5f);
Py  : my_bsp=libtcod.bsp_new_with_size(0,0,50,50)
      libtcod.bsp_split_recursive(my_bsp,0,4,5,5,1.5,1.5)

insert a comment