-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathutil.h
More file actions
404 lines (352 loc) · 11.4 KB
/
util.h
File metadata and controls
404 lines (352 loc) · 11.4 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#pragma once
#include "block.h"
#include <cstddef>
#include <iterator>
#include <ostream>
#include <vector>
/** @file
* @brief Coordinate class.
*
*/
namespace mcpp {
/**
* Represented using integers since sub-unit coordinates are not of particular
* relevance. Allows for operations such as addition between coordinates.
*/
struct Coordinate {
/**
* @brief Constructs a Coordinate object with integer values.
*
* @param x The x-coordinate. Default is 0.
* @param y The y-coordinate. Default is 0.
* @param z The z-coordinate. Default is 0.
*/
explicit Coordinate(int x = 0, int y = 0, int z = 0);
/**
* @brief Constructs a Coordinate object with double values.
*
* @param x The x-coordinate as a double.
* @param y The y-coordinate as a double.
* @param z The z-coordinate as a double.
*/
Coordinate(double x, double y, double z);
/**
* @brief Adds two Coordinate objects.
*
* @param obj The Coordinate object to add.
* @return A new Coordinate object representing the sum of the two
* coordinates.
*/
Coordinate operator+(const Coordinate& obj) const;
/**
* @brief Checks if two Coordinate objects are equal.
*
* @param obj The Coordinate object to compare with.
* @return True if the coordinates are equal, false otherwise.
*/
bool operator==(const Coordinate& obj) const;
/**
* @brief Checks if two Coordinate objects are not equal.
*
* @param obj The Coordinate object to compare with.
* @return True if the coordinates are not equal, false otherwise.
*/
bool operator!=(const Coordinate& obj) const;
/**
* @brief Subtracts one Coordinate object from another.
*
* @param obj The Coordinate object to subtract.
* @return A new Coordinate object representing the difference between the
* two coordinates.
*/
Coordinate operator-(const Coordinate& obj) const;
/**
* @brief Creates a copy of the Coordinate object.
*
* @return A new Coordinate object that is a copy of the current object.
*/
[[nodiscard]] Coordinate clone() const;
std::size_t operator()(const Coordinate& obj) const;
/**
* @brief Outputs the Coordinate object to an ostream.
*
* @param out The output stream.
* @param coord The Coordinate object to output.
* @return The output stream with the Coordinate object's values.
*/
friend std::ostream& operator<<(std::ostream& out, const Coordinate& coord);
int x;
int y;
int z;
};
/**
* Stores a 3D cuboid of BlockTypes while preserving their relative location to
* the base point they were gathered at and each other.
*/
struct Chunk {
/**
* @brief An iterator for the Chunk's 3D block data.
*
* This iterator allows for range-based for loops and standard iterator
* operations over the 3D block data stored within a Chunk. It provides a
* linear interface to traverse the 3D grid of blocks, enabling sequential
* access to the elements stored in the chunk.
*/
struct Iterator {
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = BlockType;
using pointer = BlockType*;
using reference = BlockType&;
/**
* @brief Constructs an iterator at the given pointer position.
*
* @param ptr Pointer to the position in the height array.
*/
Iterator(pointer ptr) : m_ptr(ptr) {}
/**
* @brief Dereference the iterator to access the value at the current
* position.
*
* @return Reference to the current element.
*/
reference operator*() const { return *m_ptr; }
/**
* @brief Access the pointer to the current element.
*
* @return Pointer to the current element.
*/
pointer operator->() { return m_ptr; }
/**
* @brief Pre-increment operator. Advances the iterator to the next
* position.
*
* @return Reference to the updated iterator.
*/
Iterator& operator++() {
m_ptr++;
return *this;
}
/**
* @brief Post-increment operator. Advances the iterator to the next
* position.
*
* @param int Unused dummy parameter to differentiate from prefix
* increment.
* @return Iterator to the original position before incrementing.
*/
Iterator operator++(int) {
Iterator tmp = *this;
++(*this);
return tmp;
}
/**
* @brief Equality comparison operator.
*
* @param a First iterator to compare.
* @param b Second iterator to compare.
* @return true if both iterators point to the same position, false
* otherwise.
*/
friend bool operator==(const Iterator& a, const Iterator& b) {
return a.m_ptr == b.m_ptr;
};
/**
* @brief Inequality comparison operator.
*
* @param a First iterator to compare.
* @param b Second iterator to compare.
* @return true if iterators point to different positions, false
* otherwise.
*/
friend bool operator!=(const Iterator& a, const Iterator& b) {
return a.m_ptr != b.m_ptr;
};
private:
pointer m_ptr;
};
Iterator begin() { return Iterator(&raw_data[0]); }
Iterator end() { return Iterator(&raw_data[_x_len * _y_len * _z_len]); }
/**
* Initialized by copying from a flat vector of blocks
*/
Chunk(const Coordinate& loc1, const Coordinate& loc2,
const std::vector<BlockType>& block_list);
~Chunk();
Chunk& operator=(const Chunk& other) noexcept;
/**
* Accesses the Minecraft block at absolute position pos and returns its
* BlockType if it is in the included area.
* @param pos: Abolute position in the Minecraft world to query BlockType
* for
* @return BlockType at specified location
*/
BlockType get_worldspace(const Coordinate& pos);
/**
* Local equivalent of get_worldspace, equivalent to a 3D array access of
* the internal data.
* @param x: x element of array access
* @param y: y element of array access
* @param z: z element of array access
* @return BlockType at specified location
*/
BlockType get(int x, int y, int z);
/**
* Gets the x length of the Chunk.
* @return x length of the Chunk
*/
int x_len() const;
/**
* Gets the y length of the Chunk.
* @return y length of the Chunk
*/
int y_len() const;
/**
* Gets the z length of the Chunk.
* @return z length of the Chunk
*/
int z_len() const;
/**
* Gets the minimum coordinate in the Chunk.
* @return the minimum coordinate in the Chunk
*/
Coordinate base_pt() const;
private:
Coordinate _base_pt;
int _y_len;
int _x_len;
int _z_len;
BlockType* raw_data;
};
/**
* Represents a 2D area of the world with the y coordinates of the highest
* non-air blocks at each (x,z)
*/
struct HeightMap {
/**
* @brief An iterator for the HeightMap structure.
*
* This iterator allows for range-based for loops and standard iterator
* operations over the height data stored within a HeightMap.
*/
struct Iterator {
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = int;
using pointer = int*;
using reference = int&;
/**
* @brief Constructs an iterator at the given pointer position.
*
* @param ptr Pointer to the position in the height array.
*/
Iterator(pointer ptr) : m_ptr(ptr) {}
/**
* @brief Dereference the iterator to access the value at the current
* position.
*
* @return Reference to the current element.
*/
reference operator*() const { return *m_ptr; }
/**
* @brief Access the pointer to the current element.
*
* @return Pointer to the current element.
*/
pointer operator->() { return m_ptr; }
/**
* @brief Pre-increment operator. Advances the iterator to the next
* position.
*
* @return Reference to the updated iterator.
*/
Iterator& operator++() {
m_ptr++;
return *this;
}
/**
* @brief Post-increment operator. Advances the iterator to the next
* position.
*
* @param int Unused dummy parameter to differentiate from prefix
* increment.
* @return Iterator to the original position before incrementing.
*/
Iterator operator++(int) {
Iterator tmp = *this;
++(*this);
return tmp;
}
/**
* @brief Equality comparison operator.
*
* @param a First iterator to compare.
* @param b Second iterator to compare.
* @return true if both iterators point to the same position, false
* otherwise.
*/
friend bool operator==(const Iterator& a, const Iterator& b) {
return a.m_ptr == b.m_ptr;
};
/**
* @brief Inequality comparison operator.
*
* @param a First iterator to compare.
* @param b Second iterator to compare.
* @return true if iterators point to different positions, false
* otherwise.
*/
friend bool operator!=(const Iterator& a, const Iterator& b) {
return a.m_ptr != b.m_ptr;
};
private:
pointer m_ptr;
};
Iterator begin() { return Iterator(&raw_heights[0]); }
Iterator end() { return Iterator(&raw_heights[_x_len * _z_len]); }
HeightMap(const Coordinate& loc1, const Coordinate& loc2,
const std::vector<int>& heights);
~HeightMap();
HeightMap& operator=(const HeightMap& other) noexcept;
/**
* Get the height using an offset from the origin/base point of the heights
* area
* @param x: x offset to access underlying array
* @param z: z offset to access underlying array
* @return: height at specified offset
*/
int get(int x, int z) const;
/**
* Get the height at a Minecraft coordinate if saved inside the height map
* @param loc: Coordinate in Minecraft world to access in the map
* @return: height at specified coordinate
*/
int get_worldspace(const Coordinate& loc) const;
/**
* Fill a coordinate inplace with the highest y coordinate at the `loc`'s x
* and z components.
* @param loc: Coordinate to fill y value for
*/
void fill_coord(Coordinate& out);
/**
* Gets the x length of the HeightMap.
* @return x length of the HeightMap
*/
int x_len() const;
/**
* Gets the z length of the HeightMap.
* @return z length of the HeightMap
*/
int z_len() const;
/**
* Gets the minimum coordinate in the HeightMap.
* @return the minimum coordinate in the HeightMap.
*/
Coordinate base_pt() const;
private:
Coordinate _base_pt;
int _x_len;
int _z_len;
int* raw_heights;
};
} // namespace mcpp