< index < 14. BSP toolkit < 14.3 Resizing a BSP tree | ===================================== 14.4 Reading information from the tree ===================================== | > 14.5 Traversing the tree

Once you have built a BSP tree, you can retrieve information from any node. The node gives you free access to its fields :

    C++ : class TCODBsp {
    public :
            int x,y,w,h; //
            int position; // position of splitting
            bool horizontal; // horizontal splitting ?
            uint8 level; // level in the tree
            ...
          }
    C   : typedef struct {
            int x,y,w,h;
            int position;
            bool horizontal;
            uint8 level;
            ...
          } TCOD_bsp_t;

Parameter | Description
--- | ---
x,y,w,h | Rectangular region covered by this node.
position | If this node is not a leaf, splitting position.
horizontal | If this node is not a leaf, splitting orientation.
level | Level in the BSP tree (0 for the root, 1 for the root's sons, ...).

You can navigate from a node to its sons or its parent using one of those functions. Each function returns NULL if the corresponding node does not exists (if the node is not splitted for getLeft and getRight, and if the node is the root node for getFather).

    C++ : TCODBsp *TCODBsp::getLeft() const
          TCODBsp *TCODBsp::getRight() const
          TCODBsp *TCODBsp::getFather() const
    C   : TCOD_bsp_t * TCOD_bsp_left(TCOD_bsp_t *node)
          TCOD_bsp_t * TCOD_bsp_right(TCOD_bsp_t *node)
          TCOD_bsp_t * TCOD_bsp_father(TCOD_bsp_t *node)
    Py  : bsp_left(node)
          bsp_right(node)
          bsp_father(node)

Parameter | Description
--- | ---
node | In the C version, the node reference.

You can know if a node is a leaf (not splitted, no sons) with this function :

    C++ : bool TCODBsp::isLeaf() const
    C   : bool TCOD_bsp_is_leaf(TCOD_bsp_t *node)
    Py  : bsp_is_leaf(node)

You can check if a map cell is inside a node.

    C++ : bool TCODBsp::contains(int cx, int cy) const
    C   : bool TCOD_bsp_contains(TCOD_bsp_t *node, int cx, int cy)
    Py  : bsp_contains(node, cx, cy)

Parameter | Description
--- | ---
node | In the C version, the node reference.
cx,cy | Map cell coordinates.

You can search the tree for the smallest node containing a map cell. If the cell is outside the tree, the function returns NULL :

    C++ : TCODBsp *TCODBsp::findNode(int cx, int cy)
    C   : TCOD_bsp_t * TCOD_bsp_find_node(TCOD_bsp_t *node, int cx, int cy)
    Py  : bsp_find_node(node, cx, cy)

Parameter | Description
--- | ---
node | In the C version, the node reference.
cx,cy | Map cell coordinates.