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 Coordinate2D _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 Coordinate2D& loc1, const Coordinate2D& loc2,
23 const std::vector<int16_t>& heights);
24 ~HeightMap() = default;
25
26 HeightMap(const HeightMap& other)
27 : _base_pt(other._base_pt), _x_len(other._x_len), _z_len(other._z_len) {
28 size_t size = _x_len * _z_len;
29 // Allocate memory and copy the heights
30 _raw_heights.reset(new int16_t[size]);
31 std::copy(other._raw_heights.get(), other._raw_heights.get() + size, _raw_heights.get());
32 }
33
34 HeightMap(HeightMap&& other) noexcept = default;
36 HeightMap& operator=(HeightMap&& other) = default;
37
45 int16_t get(int x, int z) const;
46
52 int16_t get_worldspace(const Coordinate2D& loc) const;
53
59 void fill_coord(Coordinate& out) const;
60
65 uint16_t x_len() const;
66
71 uint16_t z_len() const;
72
78
85 struct Iterator {
86 using iterator_category = std::forward_iterator_tag;
87 using difference_type = std::ptrdiff_t;
88 using value_type = int16_t;
89 using pointer = int16_t*;
90 using reference = int16_t&;
91
97 Iterator(pointer ptr) : m_ptr(ptr) {}
98
105 reference operator*() const { return *m_ptr; }
106
112 pointer operator->() { return m_ptr; }
113
121 m_ptr++;
122 return *this;
123 }
124
134 Iterator tmp = *this;
135 ++(*this);
136 return tmp;
137 }
138
147 friend bool operator==(const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; };
148
157 friend bool operator!=(const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; };
158
159 private:
160 pointer m_ptr;
161 };
162
170 using iterator_category = std::forward_iterator_tag;
171 using difference_type = std::ptrdiff_t;
172 using value_type = int16_t;
173 using pointer = const int16_t*;
174 using reference = const int16_t&;
175
181 ConstIterator(pointer ptr) : m_ptr(ptr) {}
182
189 reference operator*() const { return *m_ptr; }
190
196 pointer operator->() { return m_ptr; }
197
205 m_ptr++;
206 return *this;
207 }
208
218 ConstIterator tmp = *this;
219 ++(*this);
220 return tmp;
221 }
222
231 friend bool operator==(const ConstIterator& a, const ConstIterator& b) {
232 return a.m_ptr == b.m_ptr;
233 };
234
243 friend bool operator!=(const ConstIterator& a, const ConstIterator& b) {
244 return a.m_ptr != b.m_ptr;
245 };
246
247 private:
248 pointer m_ptr;
249 };
250
251 Iterator begin() { return Iterator(&_raw_heights[0]); }
252 Iterator end() { return Iterator(&_raw_heights[_x_len * _z_len]); }
253 ConstIterator begin() const { return ConstIterator(&_raw_heights[0]); }
254 ConstIterator end() const { return ConstIterator(&_raw_heights[_x_len * _z_len]); }
255};
256
257} // namespace mcpp
Coordinate class.
Namespace containing all the the mcpp library classes.
Definition block.h:10
Height-agnostic coordinate class.
Definition coordinate.h:107
Definition coordinate.h:17
An iterator for the const HeightMap structure.
Definition heightmap.h:169
const int16_t & reference
Definition heightmap.h:174
ConstIterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition heightmap.h:181
ConstIterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition heightmap.h:217
int16_t value_type
Definition heightmap.h:172
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition heightmap.h:189
ConstIterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition heightmap.h:204
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
Equality comparison operator.
Definition heightmap.h:231
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
Inequality comparison operator.
Definition heightmap.h:243
std::forward_iterator_tag iterator_category
Definition heightmap.h:170
const int16_t * pointer
Definition heightmap.h:173
pointer operator->()
Access the pointer to the current element.
Definition heightmap.h:196
std::ptrdiff_t difference_type
Definition heightmap.h:171
An iterator for the HeightMap structure.
Definition heightmap.h:85
int16_t value_type
Definition heightmap.h:88
int16_t & reference
Definition heightmap.h:90
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition heightmap.h:157
int16_t * pointer
Definition heightmap.h:89
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition heightmap.h:120
pointer operator->()
Access the pointer to the current element.
Definition heightmap.h:112
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition heightmap.h:133
std::forward_iterator_tag iterator_category
Definition heightmap.h:86
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition heightmap.h:105
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition heightmap.h:97
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition heightmap.h:147
std::ptrdiff_t difference_type
Definition heightmap.h:87
Definition heightmap.h:13
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:251
ConstIterator end() const
Definition heightmap.h:254
~HeightMap()=default
HeightMap(const Coordinate2D &loc1, const Coordinate2D &loc2, const std::vector< int16_t > &heights)
ConstIterator begin() const
Definition heightmap.h:253
Coordinate2D base_pt() const
int16_t get(int x, int z) const
Iterator end()
Definition heightmap.h:252
HeightMap & operator=(HeightMap &&other)=default
int16_t get_worldspace(const Coordinate2D &loc) const
HeightMap(const HeightMap &other)
Definition heightmap.h:26
HeightMap & operator=(const HeightMap &other)