libtcod
Loading...
Searching...
No Matches
bsp.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34// clang-format off
35#pragma once
36#ifndef TCOD_BSP_HPP_
37#define TCOD_BSP_HPP_
38
39#include <utility>
40
41#include "bsp.h"
42#include "list.hpp"
43#include "mersenne.hpp"
44#include "tree.hpp"
45
48
49class TCODBsp;
50
51class TCODLIB_API ITCODBspCallback {
52public :
53 virtual ~ITCODBspCallback() {}
54 virtual bool visitNode(TCODBsp *node, void *userData) = 0;
55};
56
63
64// clang-format on
65class TCODLIB_API TCODBsp : public TCODTree {
66 public:
67 int x{}, y{}, w{}, h{}; //
68 int position{}; // position of splitting
69 bool horizontal{}; // horizontal splitting ?
70 uint8_t level{}; // level in the tree
71
87 TCODBsp() : level{0} {}
88 TCODBsp(int x, int y, int w, int h) : x{x}, y{y}, w{w}, h{h}, level{0} {}
89
90 TCODBsp(const TCODBsp&) = delete;
91 TCODBsp& operator=(const TCODBsp&) = delete;
92 TCODBsp(TCODBsp&& rhs) noexcept { (*this) = std::move(rhs); };
93 TCODBsp& operator=(TCODBsp&& rhs) noexcept {
94 swap(*this, rhs);
95 return *this;
96 };
97
98 friend void swap(TCODBsp& lhs, TCODBsp& rhs) noexcept {
99 std::swap(lhs.x, rhs.x);
100 std::swap(lhs.y, rhs.y);
101 std::swap(lhs.w, rhs.w);
102 std::swap(lhs.h, rhs.h);
103 std::swap(lhs.position, rhs.position);
104 std::swap(lhs.horizontal, rhs.horizontal);
105 std::swap(lhs.level, rhs.level);
106 std::swap(lhs.next, rhs.next);
107 std::swap(lhs.father, rhs.father);
108 std::swap(lhs.sons, rhs.sons);
109 };
110
111
130 virtual ~TCODBsp();
131
132 // clang-format off
161 void removeSons();
162
188 void splitOnce(bool horizontal, int position);
189
214 void splitRecursive(TCODRandom *randomizer, int nb, int minHSize, int minVSize, float maxHRatio, float maxVRatio);
215
245 void resize(int x,int y, int w, int h);
246
285
308 TCODBsp *getLeft() const {
309 return static_cast<TCODBsp*>(sons);
310 }
311 TCODBsp *getRight() const {
312 return sons ? static_cast<TCODBsp*>(sons->next) : NULL;
313 }
314 TCODBsp *getFather() const {
315 return static_cast<TCODBsp*>(father);
316 }
317
327 bool isLeaf() const { return sons == NULL ; }
328
340 bool contains(int x, int y) const;
341
353 TCODBsp *findNode(int x, int y);
354
429 bool traversePreOrder(ITCODBspCallback* listener, void* userData);
430 bool traverseInOrder(ITCODBspCallback* listener, void* userData);
431 bool traversePostOrder(ITCODBspCallback* listener, void* userData);
432 bool traverseLevelOrder(ITCODBspCallback* listener, void* userData);
433 bool traverseInvertedLevelOrder(ITCODBspCallback* listener, void* userData);
434 // clang-format on
435
445 template <typename VisitorFunc>
446 bool traversePreOrder(VisitorFunc&& visitor) {
447 auto visitor_instance = LambdaVisitor<VisitorFunc>{};
448 return traversePreOrder(&visitor_instance, static_cast<void*>(&visitor));
449 }
450
460 template <typename VisitorFunc>
461 bool traverseInOrder(VisitorFunc&& visitor) {
462 auto visitor_instance = LambdaVisitor<VisitorFunc>{};
463 return traverseInOrder(&visitor_instance, static_cast<void*>(&visitor));
464 }
465
475 template <typename VisitorFunc>
476 bool traversePostOrder(VisitorFunc&& visitor) {
477 auto visitor_instance = LambdaVisitor<VisitorFunc>{};
478 return traversePostOrder(&visitor_instance, static_cast<void*>(&visitor));
479 }
480
490 template <typename VisitorFunc>
491 bool traverseLevelOrder(VisitorFunc&& visitor) {
492 auto visitor_instance = LambdaVisitor<VisitorFunc>{};
493 return traverseLevelOrder(&visitor_instance, static_cast<void*>(&visitor));
494 }
495
505 template <typename VisitorFunc>
506 bool traverseInvertedLevelOrder(VisitorFunc&& visitor) {
507 auto visitor_instance = LambdaVisitor<VisitorFunc>{};
508 return traverseInvertedLevelOrder(&visitor_instance, static_cast<void*>(&visitor));
509 }
510
511 protected:
512 TCODBsp(TCODBsp* father, bool left);
513
514 private:
515
520 template <typename VisitorFunc>
521 class LambdaVisitor final : public ITCODBspCallback {
522
525 bool visitNode(TCODBsp* node, void* callback) override {
526 if constexpr (std::is_same_v<typename std::invoke_result<VisitorFunc, TCODBsp&>::type, void>) {
527 (*static_cast<VisitorFunc*>(callback))(*node);
528 return true;
529 } else {
530 return ((*static_cast<VisitorFunc*>(callback))(*node));
531 }
532 }
533 };
534};
536#endif // TCOD_BSP_HPP_
Binary space partitioning module.
Definition bsp.hpp:51
Definition tree.hpp:43
Deprecated TCODList class.
Random number generator C++ module.
Internal tree container.