mcpp
C++ Minecraft Library
util.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "block.h"
4 #include <cstddef>
5 #include <iterator>
6 #include <ostream>
7 #include <vector>
8 
13 namespace mcpp {
18 struct Coordinate {
26  constexpr Coordinate(int x, int y, int z) : x(x), y(y), z(z) {}
27 
31  constexpr Coordinate() : x(0), y(0), z(0) {}
32 
40  constexpr Coordinate(double x, double y, double z)
41  : x(static_cast<int>(x)), y(static_cast<int>(y)),
42  z(static_cast<int>(z)) {}
43 
51  Coordinate operator+(const Coordinate& obj) const;
52 
59  bool operator==(const Coordinate& obj) const;
60 
67  bool operator!=(const Coordinate& obj) const;
68 
76  Coordinate operator-(const Coordinate& obj) const;
77 
83  [[nodiscard]] Coordinate clone() const;
84 
92  friend std::ostream& operator<<(std::ostream& out, const Coordinate& coord);
93 
94  int x;
95  int y;
96  int z;
97 };
98 
103 struct Chunk {
112  struct Iterator {
113  using iterator_category = std::forward_iterator_tag;
114  using difference_type = std::ptrdiff_t;
116  using pointer = BlockType*;
118 
124  Iterator(pointer ptr) : m_ptr(ptr) {}
125 
132  reference operator*() const { return *m_ptr; }
133 
139  pointer operator->() { return m_ptr; }
140 
148  m_ptr++;
149  return *this;
150  }
151 
161  Iterator tmp = *this;
162  ++(*this);
163  return tmp;
164  }
165 
174  friend bool operator==(const Iterator& a, const Iterator& b) {
175  return a.m_ptr == b.m_ptr;
176  };
177 
186  friend bool operator!=(const Iterator& a, const Iterator& b) {
187  return a.m_ptr != b.m_ptr;
188  };
189 
190  private:
191  pointer m_ptr;
192  };
193 
202  struct ConstIterator {
203  using iterator_category = std::forward_iterator_tag;
204  using difference_type = std::ptrdiff_t;
206  using pointer = const BlockType*;
207  using reference = const BlockType&;
208 
214  ConstIterator(pointer ptr) : m_ptr(ptr) {}
215 
222  reference operator*() const { return *m_ptr; }
223 
229  pointer operator->() { return m_ptr; }
230 
238  m_ptr++;
239  return *this;
240  }
241 
251  ConstIterator tmp = *this;
252  ++(*this);
253  return tmp;
254  }
255 
264  friend bool operator==(const ConstIterator& a, const ConstIterator& b) {
265  return a.m_ptr == b.m_ptr;
266  };
267 
276  friend bool operator!=(const ConstIterator& a, const ConstIterator& b) {
277  return a.m_ptr != b.m_ptr;
278  };
279 
280  private:
281  pointer m_ptr;
282  };
283 
284  Iterator begin() { return Iterator(&raw_data[0]); }
285  Iterator end() { return Iterator(&raw_data[_x_len * _y_len * _z_len]); }
286  ConstIterator begin() const { return ConstIterator(&raw_data[0]); }
287  ConstIterator end() const {
288  return ConstIterator(&raw_data[_x_len * _y_len * _z_len]);
289  }
290 
294  Chunk(const Coordinate& loc1, const Coordinate& loc2,
295  const std::vector<BlockType>& block_list);
296 
298 
299  Chunk& operator=(const Chunk& other) noexcept;
300 
309 
318  BlockType get(int x, int y, int z) const;
319 
324  int x_len() const;
325 
330  int y_len() const;
331 
336  int z_len() const;
337 
343 
344  private:
345  Coordinate _base_pt;
346  int _y_len;
347  int _x_len;
348  int _z_len;
349  BlockType* raw_data;
350 };
351 
356 struct HeightMap {
363  struct Iterator {
364  using iterator_category = std::forward_iterator_tag;
365  using difference_type = std::ptrdiff_t;
366  using value_type = int;
367  using pointer = int*;
368  using reference = int&;
369 
375  Iterator(pointer ptr) : m_ptr(ptr) {}
376 
383  reference operator*() const { return *m_ptr; }
384 
390  pointer operator->() { return m_ptr; }
391 
399  m_ptr++;
400  return *this;
401  }
402 
412  Iterator tmp = *this;
413  ++(*this);
414  return tmp;
415  }
416 
425  friend bool operator==(const Iterator& a, const Iterator& b) {
426  return a.m_ptr == b.m_ptr;
427  };
428 
437  friend bool operator!=(const Iterator& a, const Iterator& b) {
438  return a.m_ptr != b.m_ptr;
439  };
440 
441  private:
442  pointer m_ptr;
443  };
444 
451  struct ConstIterator {
452  using iterator_category = std::forward_iterator_tag;
453  using difference_type = std::ptrdiff_t;
454  using value_type = int;
455  using pointer = const int*;
456  using reference = const int&;
457 
463  ConstIterator(pointer ptr) : m_ptr(ptr) {}
464 
471  reference operator*() const { return *m_ptr; }
472 
478  pointer operator->() { return m_ptr; }
479 
487  m_ptr++;
488  return *this;
489  }
490 
500  ConstIterator tmp = *this;
501  ++(*this);
502  return tmp;
503  }
504 
513  friend bool operator==(const ConstIterator& a, const ConstIterator& b) {
514  return a.m_ptr == b.m_ptr;
515  };
516 
525  friend bool operator!=(const ConstIterator& a, const ConstIterator& b) {
526  return a.m_ptr != b.m_ptr;
527  };
528 
529  private:
530  pointer m_ptr;
531  };
532 
533  Iterator begin() { return Iterator(&raw_heights[0]); }
534  Iterator end() { return Iterator(&raw_heights[_x_len * _z_len]); }
535  ConstIterator begin() const { return ConstIterator(&raw_heights[0]); }
536  ConstIterator end() const {
537  return ConstIterator(&raw_heights[_x_len * _z_len]);
538  }
539 
540  HeightMap(const Coordinate& loc1, const Coordinate& loc2,
541  const std::vector<int>& heights);
542 
544 
545  HeightMap& operator=(const HeightMap& other) noexcept;
546 
554  int get(int x, int z) const;
555 
561  int get_worldspace(const Coordinate& loc) const;
562 
568  void fill_coord(Coordinate& out) const;
569 
574  int x_len() const;
575 
580  int z_len() const;
581 
587 
588  private:
589  Coordinate _base_pt;
590  int _x_len;
591  int _z_len;
592  int* raw_heights;
593 };
594 
595 } // namespace mcpp
BlockType class.
Definition: block.h:10
Namespace containing all the the mcpp library classes.
Definition: block.h:9
An iterator for the const Chunk's 3D block data.
Definition: util.h:202
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition: util.h:222
pointer operator->()
Access the pointer to the current element.
Definition: util.h:229
const BlockType * pointer
Definition: util.h:206
ConstIterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition: util.h:250
ConstIterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition: util.h:214
std::ptrdiff_t difference_type
Definition: util.h:204
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
Equality comparison operator.
Definition: util.h:264
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
Inequality comparison operator.
Definition: util.h:276
std::forward_iterator_tag iterator_category
Definition: util.h:203
ConstIterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition: util.h:237
An iterator for the Chunk's 3D block data.
Definition: util.h:112
BlockType * pointer
Definition: util.h:116
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition: util.h:124
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition: util.h:186
std::ptrdiff_t difference_type
Definition: util.h:114
pointer operator->()
Access the pointer to the current element.
Definition: util.h:139
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition: util.h:160
std::forward_iterator_tag iterator_category
Definition: util.h:113
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition: util.h:147
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition: util.h:174
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition: util.h:132
Definition: util.h:103
ConstIterator end() const
Definition: util.h:287
Coordinate base_pt() const
BlockType get_worldspace(const Coordinate &pos) const
int x_len() const
BlockType get(int x, int y, int z) const
Iterator begin()
Definition: util.h:284
Iterator end()
Definition: util.h:285
int y_len() const
Chunk(const Coordinate &loc1, const Coordinate &loc2, const std::vector< BlockType > &block_list)
Chunk & operator=(const Chunk &other) noexcept
int z_len() const
ConstIterator begin() const
Definition: util.h:286
Definition: util.h:18
Coordinate operator+(const Coordinate &obj) const
Adds two Coordinate objects.
int y
Definition: util.h:95
Coordinate operator-(const Coordinate &obj) const
Subtracts one Coordinate object from another.
constexpr Coordinate(int x, int y, int z)
Constructs a Coordinate object with integer values.
Definition: util.h:26
int z
Definition: util.h:96
bool operator!=(const Coordinate &obj) const
Checks if two Coordinate objects are not equal.
Coordinate clone() const
Creates a copy of the Coordinate object.
friend std::ostream & operator<<(std::ostream &out, const Coordinate &coord)
Outputs the Coordinate object to an ostream.
constexpr Coordinate()
Constructs a Coordinate object with zero values.
Definition: util.h:31
constexpr Coordinate(double x, double y, double z)
Constructs a Coordinate object with double values.
Definition: util.h:40
bool operator==(const Coordinate &obj) const
Checks if two Coordinate objects are equal.
int x
Definition: util.h:94
An iterator for the const HeightMap structure.
Definition: util.h:451
ConstIterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition: util.h:463
int value_type
Definition: util.h:454
const int & reference
Definition: util.h:456
ConstIterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition: util.h:486
const int * pointer
Definition: util.h:455
ConstIterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition: util.h:499
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition: util.h:471
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
Equality comparison operator.
Definition: util.h:513
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
Inequality comparison operator.
Definition: util.h:525
std::forward_iterator_tag iterator_category
Definition: util.h:452
pointer operator->()
Access the pointer to the current element.
Definition: util.h:478
std::ptrdiff_t difference_type
Definition: util.h:453
An iterator for the HeightMap structure.
Definition: util.h:363
int & reference
Definition: util.h:368
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition: util.h:437
pointer operator->()
Access the pointer to the current element.
Definition: util.h:390
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition: util.h:411
int value_type
Definition: util.h:366
std::forward_iterator_tag iterator_category
Definition: util.h:364
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition: util.h:383
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition: util.h:375
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition: util.h:398
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition: util.h:425
int * pointer
Definition: util.h:367
std::ptrdiff_t difference_type
Definition: util.h:365
Definition: util.h:356
int get_worldspace(const Coordinate &loc) const
void fill_coord(Coordinate &out) const
Iterator begin()
Definition: util.h:533
int z_len() const
ConstIterator end() const
Definition: util.h:536
ConstIterator begin() const
Definition: util.h:535
Coordinate base_pt() const
HeightMap & operator=(const HeightMap &other) noexcept
Iterator end()
Definition: util.h:534
HeightMap(const Coordinate &loc1, const Coordinate &loc2, const std::vector< int > &heights)
int get(int x, int z) const
int x_len() const