From 667ff6e59e564ad3491e93fc268c39ede3068b2f Mon Sep 17 00:00:00 2001 From: Artemis Rosman <73006620+rozukke@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:17:02 +1000 Subject: [PATCH] Add getters to Chunk DS --- include/mcpp/util.h | 32 ++++++++++++++++++++++++++++---- src/util.cpp | 29 +++++++++++++++++++---------- test/minecraft_tests.cpp | 14 ++++++++++++++ 3 files changed, 61 insertions(+), 14 deletions(-) diff --git a/include/mcpp/util.h b/include/mcpp/util.h index 72c1fcf8..9dddbf2c 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -119,12 +119,36 @@ struct Chunk { */ BlockType get(int x, int y, int z); + /** + * Gets the x length of the Chunk. + * @return x length of the Chunk + */ + int x_len() const; + + /** + * Gets the y length of the Chunk. + * @return y length of the Chunk + */ + int y_len() const; + + /** + * Gets the z length of the Chunk. + * @return z length of the Chunk + */ + int z_len() const; + + /** + * Gets the minimum coordinate in the Chunk. + * @return the minimum coordinate in the Chunk + */ + Coordinate base_pt() const; + private: - Coordinate base_pt; + Coordinate _base_pt; BlockType* raw_data; - int y_len; - int x_len; - int z_len; + int _y_len; + int _x_len; + int _z_len; }; /** diff --git a/src/util.cpp b/src/util.cpp index 8d5ccd00..15406c0d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -60,12 +60,12 @@ Chunk::Chunk(const Coordinate& loc1, const Coordinate& loc2, const std::vector& block_list) { Coordinate min{std::min(loc1.x, loc2.x), std::min(loc1.y, loc2.y), std::min(loc1.z, loc2.z)}; - this->base_pt = min.clone(); + this->_base_pt = min.clone(); Coordinate dim = loc1 - loc2; - x_len = std::abs(dim.x) + 1; - y_len = std::abs(dim.y) + 1; - z_len = std::abs(dim.z) + 1; + _x_len = std::abs(dim.x) + 1; + _y_len = std::abs(dim.y) + 1; + _z_len = std::abs(dim.z) + 1; this->raw_data = new BlockType[block_list.size()]; std::copy(block_list.begin(), block_list.end(), raw_data); @@ -75,25 +75,34 @@ Chunk::~Chunk() { delete[] raw_data; } BlockType Chunk::get(int x, int y, int z) { if ((x < 0 || y < 0 || z < 0) || - (x > x_len - 1 || y > y_len - 1 || z > z_len - 1)) { + (x > _x_len - 1 || y > _y_len - 1 || z > _z_len - 1)) { throw std::out_of_range("Out of bounds Chunk access at " + to_string(Coordinate(x, y, z))); } - return raw_data[z + z_len * (x + y_len * y)]; + return raw_data[z + _z_len * (x + _y_len * y)]; } BlockType Chunk::get_worldspace(const Coordinate& pos) { - Coordinate array_pos = pos - base_pt; + Coordinate array_pos = pos - _base_pt; if ((array_pos.x < 0 || array_pos.y < 0 || array_pos.z < 0) || - (array_pos.x > x_len - 1 || array_pos.y > y_len - 1 || - array_pos.z > z_len - 1)) { + (array_pos.x > _x_len - 1 || array_pos.y > _y_len - 1 || + array_pos.z > _z_len - 1)) { throw std::out_of_range("Out of bounds Chunk access at " + to_string(array_pos) + " (world coordinate " + to_string(pos) + " )"); } - return raw_data[array_pos.z + z_len * (array_pos.x + y_len * array_pos.y)]; + return raw_data[array_pos.z + + _z_len * (array_pos.x + _y_len * array_pos.y)]; } +int Chunk::x_len() const { return this->_x_len; } + +int Chunk::y_len() const { return this->_y_len; } + +int Chunk::z_len() const { return this->_z_len; } + +Coordinate Chunk::base_pt() const { return this->_base_pt.clone(); } + HeightMap::HeightMap(const Coordinate& loc1, const Coordinate& loc2, const std::vector& heights) { _base_pt = Coordinate{ diff --git a/test/minecraft_tests.cpp b/test/minecraft_tests.cpp index 00adb36a..48b887ab 100644 --- a/test/minecraft_tests.cpp +++ b/test/minecraft_tests.cpp @@ -140,6 +140,20 @@ TEST_CASE("getBlocks and Chunk operations") { mc.setBlock(loc1 + Coordinate{1, 2, 3}, Blocks::IRON_BLOCK); Chunk res = mc.getBlocks(loc1, loc2); + SUBCASE("getters") { + Chunk data = mc.getBlocks(loc1, loc2); + + CHECK_EQ(data.base_pt(), Coordinate{100, 100, 100}); + CHECK_EQ(data.x_len(), 11); + CHECK_EQ(data.z_len(), 11); + + data = mc.getBlocks(loc2, loc1); + + CHECK_EQ(data.base_pt(), Coordinate{100, 100, 100}); + CHECK_EQ(data.x_len(), 11); + CHECK_EQ(data.z_len(), 11); + } + SUBCASE("Block accessing returns correct block using get()") { CHECK_EQ(res.get(0, 0, 0), Blocks::GOLD_BLOCK); CHECK_EQ(res.get(1, 1, 1), Blocks::BRICKS);