12.2. Splitting the tree
Splitting a node once
Once you have the root node, you can split it into two smaller non-overlapping nodes.
void TCODBsp::splitOnce(bool horizontal, int position)
void TCOD_bsp_split_once(TCOD_bsp_t *node, bool horizontal, int position)
bsp_split_once(node, horizontal, position)
| Parameter | Description |
|---|---|
| node | In the C version, the root node created with TCOD_bsp_new_with_size, or a node obtained by
splitting. |
| horizontal | If true, the node will be split horizontally, else, vertically. |
| position | Coordinate of the splitting position. If horizontal is true, x <= position < x+w Else, y <= position < y+h |
Example:
TCODBsp *myBSP = new TCODBsp(0,0,50,50);
myBSP->splitOnce(true,20);
TCOD_bsp_t *my_bsp=TCOD_bsp_new_with_size(0,0,50,50);
TCOD_bsp_split_once(my_bsp,false,20);
my_bsp=libtcod.bsp_new_with_size(0,0,50,50)
libtcod.bsp_split_once(my_bsp,False,20)
Recursively splitting a node
You can also recursively split the bsp. At each step, a random orientation (horizontal/vertical) and position
are chosen :
void TCODBsp::splitRecursive(TCODRandom *randomizer, int nb, int minHSize, int minVSize, float maxHRatio, float maxVRatio);
void TCOD_bsp_split_recursive(TCOD_bsp_t *node, TCOD_random_t randomizer, int nb, int minHSize, int minVSize, float maxHRatio, float maxVRatio)
bsp_split_recursive(node, randomizer, nb, minHSize, minVSize, maxHRatio, maxVRatio)
| Parameter | Description |
|---|---|
| node | In the C version, the root node created with TCOD_bsp_new_with_size, or a node obtained by
splitting. |
| randomizer | The random number generator to use. Use NULL for the default one. |
| nb | Number of recursion levels. |
| minHSize, | minVSize minimum values of w and h for a node. A node is split only if the resulting sub-nodes are
bigger than minHSize x minVSize |
| maxHRatio, | maxVRation maximum 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 split into a maximum of
2*2*2*2 sub-regions).
TCODBsp *myBSP
= new TCODBsp(0,0,50,50);
myBSP->splitRecursive(NULL,4,5,5,1.5f,1.5f);
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);
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)