Skip to content

Commit

Permalink
Use Coordinate2D for height-related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy committed Oct 31, 2024
1 parent 6c67d4a commit ce1c51d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
16 changes: 8 additions & 8 deletions include/mcpp/mcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,30 @@ 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
* considerable performance gains.
*
* \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
14 changes: 7 additions & 7 deletions include/mcpp/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>& heights);

~HeightMap();
Expand All @@ -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
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/mcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>& heights) {
_base_pt = Coordinate{
std::min(loc1.x, loc2.x),
Expand Down Expand Up @@ -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);
}

Expand All @@ -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

0 comments on commit ce1c51d

Please sign in to comment.