forked from rozukke/mcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheightmap.cpp
More file actions
51 lines (41 loc) · 1.56 KB
/
heightmap.cpp
File metadata and controls
51 lines (41 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "../include/mcpp/heightmap.h"
#include <cstdint>
namespace mcpp {
HeightMap::HeightMap(const Coordinate2D& loc1, const Coordinate2D& loc2,
const std::vector<int16_t>& heights) {
_base_pt = Coordinate{
std::min(loc1.x, loc2.x),
0,
std::min(loc1.z, loc2.z),
};
_x_len = std::abs(loc1.x - loc2.x) + 1;
_z_len = std::abs(loc1.z - loc2.z) + 1;
_raw_heights = std::make_unique<int16_t[]>(heights.size());
std::copy(heights.begin(), heights.end(), _raw_heights.get());
}
HeightMap& HeightMap::operator=(const HeightMap& other) {
if (this != &other) {
// Copy data from the other object
_base_pt = other._base_pt;
_x_len = other._x_len;
_z_len = other._z_len;
}
return *this;
}
int16_t HeightMap::get(int x, int z) const {
if ((x < 0 || x >= _x_len) || (z < 0 || z >= _z_len)) {
throw std::out_of_range("Out of range access of heightmap at " + std::to_string(x) + "," +
std::to_string(z) + " (worldspace x=" + std::to_string(_base_pt.x + x) +
",z=" + std::to_string(_base_pt.z + z));
}
// Get 2D from flat vector
return _raw_heights[(x * _z_len) + z];
}
int16_t HeightMap::get_worldspace(const Coordinate2D& loc) const {
return get(loc.x - _base_pt.x, loc.z - _base_pt.z);
}
void HeightMap::fill_coord(Coordinate& out) const { out.y = get_worldspace(out); }
uint16_t HeightMap::x_len() const { return _x_len; }
uint16_t HeightMap::z_len() const { return _z_len; }
Coordinate2D HeightMap::base_pt() const { return _base_pt; }
} // namespace mcpp