Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor quadtree: Extracted into tfs::map::quadtree namespace with code improvements and tests #4798

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0065b9e
Rework quadtree
ramon-bernardo Oct 5, 2024
35e0ee0
Use stl array
ramon-bernardo Oct 5, 2024
eee435c
Use inline instead static constexpr
ramon-bernardo Oct 5, 2024
e9d4f5f
Use int32_t instead of fast int32_t and auto when possible; move east…
ramon-bernardo Oct 5, 2024
30976c9
Remove remove erase check and fix comparison qt::find param
ramon-bernardo Oct 5, 2024
b7c3159
Fix use map quadtree
ramon-bernardo Oct 5, 2024
081114f
Rename namespace tfs::quadtree
ramon-bernardo Oct 5, 2024
18e1ea4
Add wildtree doc and some fix in namespace
ramon-bernardo Oct 5, 2024
cc7d235
Add quadtree doc
ramon-bernardo Oct 5, 2024
fe6172e
Simplify namespace doc
ramon-bernardo Oct 5, 2024
50b41c9
Add quadtree doc
ramon-bernardo Oct 5, 2024
d7cf93e
Rename push method, some doc fix
ramon-bernardo Oct 5, 2024
05f887e
Fix quadtree
ramon-bernardo Oct 5, 2024
e8c8d4d
Fix quadtree
ramon-bernardo Oct 6, 2024
ade4eb8
Remove unused z position
ramon-bernardo Oct 6, 2024
10594d5
Impl destructors
ramon-bernardo Oct 6, 2024
657d756
Impl destructors
ramon-bernardo Oct 6, 2024
26420d3
Use generator isntead comparasion
ramon-bernardo Oct 6, 2024
6c301d9
Remove const
ramon-bernardo Oct 6, 2024
33f3b2a
Fix nullptr check
ramon-bernardo Oct 6, 2024
9ad4755
Fix the creation of quadtree leaf nodes
ramon-bernardo Oct 6, 2024
fce5839
Add quadtree test
ramon-bernardo Oct 6, 2024
ab8a668
Fix quadtree function
ramon-bernardo Oct 6, 2024
94f660a
Fix quadtree
ramon-bernardo Oct 6, 2024
8877e35
Add tfs::generator
ramon-bernardo Oct 6, 2024
1ef0a43
Bump cmake C++23
ramon-bernardo Oct 6, 2024
d083eac
Add tfs::generator
ramon-bernardo Oct 6, 2024
01ed2d8
Add tfs::generator
ramon-bernardo Oct 6, 2024
9ff7d47
Add tfs::generator
ramon-bernardo Oct 6, 2024
33f00bd
Test bump gh os
ramon-bernardo Oct 6, 2024
20b994b
Revert "Test bump gh os"
ramon-bernardo Oct 6, 2024
bc23c52
Force use experimental generator
ramon-bernardo Oct 25, 2024
98daa5e
Use SpectatorVec instead generators
ramon-bernardo Oct 26, 2024
8272891
Use explicit const.
ramon-bernardo Oct 26, 2024
1061f10
Fix tests
ramon-bernardo Oct 26, 2024
228911c
Fix tests
ramon-bernardo Oct 26, 2024
010fa48
Fix tests
ramon-bernardo Oct 26, 2024
819c7b6
Fix tests
ramon-bernardo Oct 26, 2024
13caf97
Add new tests
ramon-bernardo Oct 26, 2024
63cd064
Fix tests
ramon-bernardo Oct 26, 2024
1ca68f5
Fix tests
ramon-bernardo Oct 26, 2024
d8d7df7
Fix tests
ramon-bernardo Oct 26, 2024
14c00d3
Create leaf node if not exists
ramon-bernardo Oct 26, 2024
fd9e407
Add method to reset quadtree and fix tests
ramon-bernardo Oct 26, 2024
938396a
Add method to reset quadtree ad remove precompiled header
ramon-bernardo Oct 26, 2024
9c920bf
Add method to reset quadtree
ramon-bernardo Oct 26, 2024
c3a5e0d
Remove quadtree test
ramon-bernardo Oct 26, 2024
9d8fedd
Merge branch 'master' into quadtree-rework-and-tests
ramon-bernardo Nov 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add quadtree doc
  • Loading branch information
ramon-bernardo committed Oct 5, 2024
commit 50b41c97328279ff9efe8adcdad4afa3fbf2a2cb
149 changes: 75 additions & 74 deletions src/quadtree.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "otpch.h"

#include "creature.h"
#include "map.h"
#include "spectators.h"

namespace tfs::map::quadtree {
Expand Down Expand Up @@ -31,20 +32,20 @@
*
* This class defines the interface for a QuadTree node.
* The class is non-copyable to prevent accidental copying of nodes.
*/
*/
class QuadTree

Check failure on line 36 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

redefinition of ‘class QuadTree’

Check failure on line 36 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=on

redefinition of ‘class QuadTree’

Check failure on line 36 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘class QuadTree’
{
public:
/// @brief Default constructor for QuadTree.
explicit QuadTree() = default;

/**
* @brief Virtual destructor for QuadTree.
*
* The virtual destructor allows for proper cleanup
* @brief Virtual destructor for QuadTree.
*
* The virtual destructor allows for proper cleanup
* of derived classes
* that may be allocated dynamically.
*/
* that may be allocated dynamically.
*/
virtual ~QuadTree() = default;

/// Deleted copy constructor to ensure QuadTree is non-copyable.
Expand All @@ -53,49 +54,49 @@
QuadTree& operator=(const QuadTree&) = delete;

/**
* @brief Check if the node is a leaf node.
* @return true if the node is a leaf, false otherwise.
* @brief Check if the node is a leaf node.
* @return true if the node is a leaf, false otherwise.
*/
virtual bool is_leaf() const = 0;

/**
* @brief Set a child node at a specified index.
*
* This method assigns a child node to the
* @brief Set a child node at a specified index.
*
* This method assigns a child node to the
* current node at the given
* index. The index must be within valid bounds for child nodes (0 to 3).
* index. The index must be within valid bounds for child nodes (0 to 3).
* @param {index} The index at which to set the child node.
* @param {node} A pointer to the child node to be
* set.
*/
*/
virtual void set_child(uint8_t index, QuadTree* node) = 0;

/**
* @brief Get a child node at a specified index.
* @param {index} The index of the child node to
* @brief Get a child node at a specified index.
* @param {index} The index of the child node to
* retrieve (0 to 3).
* @return A pointer to the child node, or nullptr if no child exists.
*/
* @return A pointer to the child node, or nullptr if no child exists.
*/
virtual QuadTree* get_child(uint8_t index) const = 0;
};

/// The number of bits used to represent the dimensions of a tile grid.
inline constexpr int32_t TILE_GRID_BITS = 3;

Check failure on line 84 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

redefinition of ‘constexpr const int32_t TILE_GRID_BITS’

Check failure on line 84 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=on

redefinition of ‘constexpr const int32_t TILE_GRID_BITS’

Check failure on line 84 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘constexpr const int32_t TILE_GRID_BITS’
/// The size of the tile grid, calculated as 2 ^ TILE_GRID_BITS.
/// This value indicates the number of tiles that can fit along one dimension of the grid.
inline constexpr int32_t TILE_GRID_SIZE = (1 << TILE_GRID_BITS);

Check failure on line 87 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

redefinition of ‘constexpr const int32_t TILE_GRID_SIZE’

Check failure on line 87 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘constexpr const int32_t TILE_GRID_SIZE’
/// A mask to isolate the tile index within the bounds of the grid size.
/// This mask is used to ensure tile indices remain valid and wrap around correctly.
inline constexpr int32_t TILE_INDEX_MASK = (TILE_GRID_SIZE - 1);

Check failure on line 90 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

redefinition of ‘constexpr const int32_t TILE_INDEX_MASK’

Check failure on line 90 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘constexpr const int32_t TILE_INDEX_MASK’

/**
* @class Node
* @brief Represents a node in a QuadTree.
*
* This class extends the QuadTree interface and
* implements a specific type of node that can have up to four child nodes.
*/
* @class Node
* @brief Represents a node in a QuadTree.
*
* This class extends the QuadTree interface and
* implements a specific type of node that can have up to four child nodes.
*/
class Node final : public QuadTree

Check failure on line 99 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

redefinition of ‘class Node’

Check failure on line 99 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘class Node’
{
public:
/// @brief Default constructor for QuadTree.
Expand All @@ -107,36 +108,36 @@
Node& operator=(const Node&) = delete;

/**
* @brief Check if the node is a leaf node.
*
* This implementation always returns false, as Node
* @brief Check if the node is a leaf node.
*
* This implementation always returns false, as Node
* objects are designed to have children.
* @return false, indicating that the node is not a leaf.
* @return false, indicating that the node is not a leaf.
*/
bool is_leaf() const override { return false; }

/**
* @brief Set a child node at a specified index.
*
* This method assigns a child node to the
* @brief Set a child node at a specified index.
*
* This method assigns a child node to the
* current node at the given index. The index must be in the range of 0 to 3.
*
* @param {index} The index
*
* @param {index} The index
* at which to set the child node (0 to 3).
* @param {node} A pointer to the child QuadTree node to be set.
* @param {node} A pointer to the child QuadTree node to be set.
*/
void set_child(uint8_t index, QuadTree* node) override { nodes[index] = node; }

/**
* @brief Get a child node at a specified index.
*
* This method retrieves the child node at the
* @brief Get a child node at a specified index.
*
* This method retrieves the child node at the
* specified index.
*
* @param {index} The index of the child node to retrieve (0 to 3).
* @return A
*
* @param {index} The index of the child node to retrieve (0 to 3).
* @return A
* pointer to the child QuadTree node, or nullptr if no child exists.
*/
*/
QuadTree* get_child(uint8_t index) const override { return nodes[index]; };

private:
Expand All @@ -147,19 +148,19 @@
/**
* @class Leaf
* @brief Represents a leaf node in a QuadTree.
*
*
* This class extends the QuadTree interface and
* is designed to hold data specific to a leaf node, such as creatures and tile information.
*/
class Leaf final : public QuadTree

Check failure on line 155 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

redefinition of ‘class Leaf’

Check failure on line 155 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

redefinition of ‘class Leaf’
{
public:
/**
* @brief Constructor for Leaf.
*
* This constructor initializes the leaf node with the specified
* @brief Constructor for Leaf.
*
* This constructor initializes the leaf node with the specified
* coordinates.
*/
*/
explicit Leaf(uint16_t x, uint16_t y);

/// Deleted copy constructor to ensure Leaf is non-copyable.
Expand All @@ -168,62 +169,62 @@
Leaf& operator=(const Leaf&) = delete;

/**
* @brief Check if the node is a leaf node.
*
* This implementation always returns true,
* @brief Check if the node is a leaf node.
*
* This implementation always returns true,
* indicating that this object is a leaf.
* @return true, indicating that the node is a Leaf.
*/
* @return true, indicating that the node is a Leaf.
*/
bool is_leaf() const override { return true; }

/**
* @brief Set a child node at a specified index.
*
* This method does nothing, as leaf nodes
* @brief Set a child node at a specified index.
*
* This method does nothing, as leaf nodes
* cannot have children.
* @param {index} The index at which to set the child node (not used).
* @param
* @param {index} The index at which to set the child node (not used).
* @param
* {node} A pointer to the child node to be set (not used).
*/
void set_child(uint8_t index, QuadTree* node) override {}
*/
void set_child(uint8_t, QuadTree*) override {}

/**
* @brief Get a child node at a specified index.
*
* This method always returns nullptr, as leaf
* @brief Get a child node at a specified index.
*
* This method always returns nullptr, as leaf
* nodes do not have children.
* @param {index} The index of the child node to retrieve (not used).
* @param {index} The index of the child node to retrieve (not used).
* @return Always returns nullptr.
*/
QuadTree* get_child(uint8_t index) const override { return nullptr; };
*/
QuadTree* get_child(uint8_t) const override { return nullptr; };

/**
* @brief Add a creature to the leaf.
*
* This method adds the specified creature to the Leaf
* @brief Add a creature to the leaf.
*
* This method adds the specified creature to the Leaf
* node.
* @param {creature} A pointer to the creature to be added.
*/
* @param {creature} A pointer to the creature to be added.
*/
void push_creature(Creature* creature);

/**
* @brief Remove a creature from the leaf.
*
* This method removes the specified creature from
* @brief Remove a creature from the leaf.
*
* This method removes the specified creature from
* the leaf node.
* @param {creature} A pointer to the creature to be removed.
* @param {creature} A pointer to the creature to be removed.
*/
void remove_creature(Creature* creature);

/**
* @brief A 3D array of pointers to tiles for the leaf across multiple layers.
* @brief A 3D array of pointers to tiles for the leaf across multiple layers.
*
* This array holds pointers to `Tile` objects, organized in a three-dimensional
* structure. The first dimension represents different layers, while the second
* and third dimensions represent the x and y coordinates of the tiles within
* each layer. Each layer can contain a grid of tiles.
*/
*/
std::array<std::array<std::array<Tile*, TILE_GRID_SIZE>, TILE_GRID_SIZE>, MAP_MAX_LAYERS> tiles = {};

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

‘MAP_MAX_LAYERS’ was not declared in this scope

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

template argument 2 is invalid

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

‘<expression error>’ in namespace ‘std’ does not name a type

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=off

extra ‘;’ [-Werror=pedantic]

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=on

‘MAP_MAX_LAYERS’ was not declared in this scope

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=on

template argument 2 is invalid

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=on

‘<expression error>’ in namespace ‘std’ does not name a type

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / ubuntu-Release-luajit=on

extra ‘;’ [-Werror=pedantic]

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

‘MAP_MAX_LAYERS’ was not declared in this scope

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

template argument 2 is invalid

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

‘<expression error>’ in namespace ‘std’ does not name a type

Check failure on line 227 in src/quadtree.h

View workflow job for this annotation

GitHub Actions / test

extra ‘;’ [-Werror=pedantic]

/// @brief A set of creatures (monsters, NPCs and players) present in this leaf node.
std::set<Creature*> creatures;
Expand Down
Loading