From ce1c51d0855f1d92cc8ac049ca32bedfe5bf6e47 Mon Sep 17 00:00:00 2001 From: darcy Date: Thu, 31 Oct 2024 12:59:17 +1100 Subject: [PATCH] Use `Coordinate2D` for height-related functions --- include/mcpp/mcpp.h | 16 ++++++++-------- include/mcpp/util.h | 14 +++++++------- src/mcpp.cpp | 9 +++++---- src/util.cpp | 6 +++--- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/include/mcpp/mcpp.h b/include/mcpp/mcpp.h index a6ecbce6..bb3e9b90 100644 --- a/include/mcpp/mcpp.h +++ b/include/mcpp/mcpp.h @@ -125,19 +125,18 @@ class MinecraftConnection { Chunk getBlocks(const Coordinate& loc1, const Coordinate& loc2); /** - * @brief Returns the height of the specific provided x and z coordinate + * @brief Returns the height of the specific provided 2D coordinate * * ***IMPORTANT:*** * DO NOT USE FOR LARGE AREAS, IT WILL BE VERY SLOW * USE getHeights() INSTEAD * - * Gets the y-value of the highest non-air block at the specified (x, z) + * Gets the y-value of the highest non-air block at the specified 2D * coordinate. - * @param x - * @param z + * @param loc 2D coordinate * @return Returns the integer y-height at the requested coordinate. */ - int getHeight(int x, int z); + int getHeight(Coordinate2D loc); /** * @brief Provides a scaled option of the getHeight call to allow for @@ -145,10 +144,11 @@ class MinecraftConnection { * * \par USE THIS instead of getHeight in a for loop. * - * @param loc1 - * @param loc2 + * @param loc1 1st corner of rectangle + * @param loc2 2nd corner of rectangle * @return Returns a vector of integers representing the 2D area of heights. */ - const HeightMap getHeights(const Coordinate& loc1, const Coordinate& loc2); + const HeightMap getHeights(const Coordinate2D& loc1, + const Coordinate2D& loc2); }; } // namespace mcpp diff --git a/include/mcpp/util.h b/include/mcpp/util.h index e738d8c8..6a819ff4 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -451,7 +451,7 @@ struct HeightMap { Iterator begin() { return Iterator(&raw_heights[0]); } Iterator end() { return Iterator(&raw_heights[_x_len * _z_len]); } - HeightMap(const Coordinate& loc1, const Coordinate& loc2, + HeightMap(const Coordinate2D& loc1, const Coordinate2D& loc2, const std::vector& heights); ~HeightMap(); @@ -469,10 +469,10 @@ struct HeightMap { /** * Get the height at a Minecraft coordinate if saved inside the height map - * @param loc: Coordinate in Minecraft world to access in the map + * @param loc: 2D coordinate in Minecraft world to access in the map * @return: height at specified coordinate */ - int get_worldspace(const Coordinate& loc) const; + int get_worldspace(const Coordinate2D& loc) const; /** * Fill a coordinate inplace with the highest y coordinate at the `loc`'s x @@ -494,13 +494,13 @@ struct HeightMap { int z_len() const; /** - * Gets the minimum coordinate in the HeightMap. - * @return the minimum coordinate in the HeightMap. + * Gets the minimum 2D coordinate in the HeightMap. + * @return the minimum 2D coordinate in the HeightMap. */ - Coordinate base_pt() const; + Coordinate2D base_pt() const; private: - Coordinate _base_pt; + Coordinate2D _base_pt; int _x_len; int _z_len; int* raw_heights; diff --git a/src/mcpp.cpp b/src/mcpp.cpp index cacad722..4bf6cdb0 100644 --- a/src/mcpp.cpp +++ b/src/mcpp.cpp @@ -112,13 +112,14 @@ Chunk MinecraftConnection::getBlocks(const Coordinate& loc1, return Chunk{loc1, loc2, result}; } -int MinecraftConnection::getHeight(int x, int z) { - std::string returnValue = conn->sendReceiveCommand("world.getHeight", x, z); +int MinecraftConnection::getHeight(Coordinate2D loc) { + std::string returnValue = + conn->sendReceiveCommand("world.getHeight", loc.x, loc.z); return stoi(returnValue); } -const HeightMap MinecraftConnection::getHeights(const Coordinate& loc1, - const Coordinate& loc2) { +const HeightMap MinecraftConnection::getHeights(const Coordinate2D& loc1, + const Coordinate2D& loc2) { std::string returnValue = conn->sendReceiveCommand( "world.getHeights", loc1.x, loc1.z, loc2.x, loc2.z); diff --git a/src/util.cpp b/src/util.cpp index 9c47d884..e1c0f757 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -156,7 +156,7 @@ 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, +HeightMap::HeightMap(const Coordinate2D& loc1, const Coordinate2D& loc2, const std::vector& heights) { _base_pt = Coordinate{ std::min(loc1.x, loc2.x), @@ -203,7 +203,7 @@ int HeightMap::get(int x, int z) const { return raw_heights[x * _z_len + z]; } -int HeightMap::get_worldspace(const Coordinate& loc) const { +int HeightMap::get_worldspace(const Coordinate2D& loc) const { return get(loc.x - _base_pt.x, loc.z - _base_pt.z); } @@ -215,6 +215,6 @@ int HeightMap::x_len() const { return this->_x_len; } int HeightMap::z_len() const { return this->_z_len; } -Coordinate HeightMap::base_pt() const { return this->_base_pt.clone(); } +Coordinate2D HeightMap::base_pt() const { return this->_base_pt.clone(); } } // namespace mcpp