mcpp
C++ Minecraft Library
Loading...
Searching...
No Matches
heightmap.h
Go to the documentation of this file.
1#pragma once
2
3#include "coordinate.h"
4#include <cstdint>
5#include <memory>
6#include <vector>
7
8namespace mcpp {
13struct HeightMap {
14private:
15 Coordinate _base_pt;
16 uint16_t _x_len;
17 uint16_t _z_len;
18 std::unique_ptr<int16_t[]> _raw_heights;
19
20public:
21 // Constructors and assignment
22 HeightMap(const Coordinate& loc1, const Coordinate& loc2, const std::vector<int16_t>& heights);
23 ~HeightMap() = default;
24
25 HeightMap(const HeightMap& other)
26 : _base_pt(other._base_pt), _x_len(other._x_len), _z_len(other._z_len) {
27 size_t size = _x_len * _z_len;
28 // Allocate memory and copy the heights
29 _raw_heights.reset(new int16_t[size]);
30 std::copy(other._raw_heights.get(), other._raw_heights.get() + size, _raw_heights.get());
31 }
32
33 HeightMap(HeightMap&& other) noexcept = default;
35 HeightMap& operator=(HeightMap&& other) = default;
36
44 int16_t get(int x, int z) const;
45
51 int16_t get_worldspace(const Coordinate& loc) const;
52
58 void fill_coord(Coordinate& out) const;
59
64 uint16_t x_len() const;
65
70 uint16_t z_len() const;
71
77
84 struct Iterator {
85 using iterator_category = std::forward_iterator_tag;
86 using difference_type = std::ptrdiff_t;
87 using value_type = int16_t;
88 using pointer = int16_t*;
89 using reference = int16_t&;
90
96 Iterator(pointer ptr) : m_ptr(ptr) {}
97
104 reference operator*() const { return *m_ptr; }
105
111 pointer operator->() { return m_ptr; }
112
120 m_ptr++;
121 return *this;
122 }
123
133 Iterator tmp = *this;
134 ++(*this);
135 return tmp;
136 }
137
146 friend bool operator==(const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; };
147
156 friend bool operator!=(const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; };
157
158 private:
159 pointer m_ptr;
160 };
161
169 using iterator_category = std::forward_iterator_tag;
170 using difference_type = std::ptrdiff_t;
171 using value_type = int16_t;
172 using pointer = const int16_t*;
173 using reference = const int16_t&;
174
180 ConstIterator(pointer ptr) : m_ptr(ptr) {}
181
188 reference operator*() const { return *m_ptr; }
189
195 pointer operator->() { return m_ptr; }
196
204 m_ptr++;
205 return *this;
206 }
207
217 ConstIterator tmp = *this;
218 ++(*this);
219 return tmp;
220 }
221
230 friend bool operator==(const ConstIterator& a, const ConstIterator& b) {
231 return a.m_ptr == b.m_ptr;
232 };
233
242 friend bool operator!=(const ConstIterator& a, const ConstIterator& b) {
243 return a.m_ptr != b.m_ptr;
244 };
245
246 private:
247 pointer m_ptr;
248 };
249
250 Iterator begin() { return Iterator(&_raw_heights[0]); }
251 Iterator end() { return Iterator(&_raw_heights[_x_len * _z_len]); }
252 ConstIterator begin() const { return ConstIterator(&_raw_heights[0]); }
253 ConstIterator end() const { return ConstIterator(&_raw_heights[_x_len * _z_len]); }
254};
255
256} // namespace mcpp
Coordinate class.
Namespace containing all the the mcpp library classes.
Definition block.h:10
Definition coordinate.h:14
An iterator for the const HeightMap structure.
Definition heightmap.h:168
const int16_t & reference
Definition heightmap.h:173
ConstIterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition heightmap.h:180
ConstIterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition heightmap.h:216
int16_t value_type
Definition heightmap.h:171
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition heightmap.h:188
ConstIterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition heightmap.h:203
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
Equality comparison operator.
Definition heightmap.h:230
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
Inequality comparison operator.
Definition heightmap.h:242
std::forward_iterator_tag iterator_category
Definition heightmap.h:169
const int16_t * pointer
Definition heightmap.h:172
pointer operator->()
Access the pointer to the current element.
Definition heightmap.h:195
std::ptrdiff_t difference_type
Definition heightmap.h:170
An iterator for the HeightMap structure.
Definition heightmap.h:84
int16_t value_type
Definition heightmap.h:87
int16_t & reference
Definition heightmap.h:89
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition heightmap.h:156
int16_t * pointer
Definition heightmap.h:88
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition heightmap.h:119
pointer operator->()
Access the pointer to the current element.
Definition heightmap.h:111
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition heightmap.h:132
std::forward_iterator_tag iterator_category
Definition heightmap.h:85
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition heightmap.h:104
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition heightmap.h:96
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition heightmap.h:146
std::ptrdiff_t difference_type
Definition heightmap.h:86
Definition heightmap.h:13
HeightMap(const Coordinate &loc1, const Coordinate &loc2, const std::vector< int16_t > &heights)
int16_t get_worldspace(const Coordinate &loc) const
HeightMap(HeightMap &&other) noexcept=default
uint16_t x_len() const
uint16_t z_len() const
void fill_coord(Coordinate &out) const
Iterator begin()
Definition heightmap.h:250
ConstIterator end() const
Definition heightmap.h:253
~HeightMap()=default
ConstIterator begin() const
Definition heightmap.h:252
Coordinate base_pt() const
int16_t get(int x, int z) const
Iterator end()
Definition heightmap.h:251
HeightMap & operator=(HeightMap &&other)=default
HeightMap(const HeightMap &other)
Definition heightmap.h:25
HeightMap & operator=(const HeightMap &other)