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  explicit Coordinate(int x = 0, int y = 0, int z = 0);
27 
35  Coordinate(double x, double y, double z);
36 
44  Coordinate operator+(const Coordinate& obj) const;
45 
52  bool operator==(const Coordinate& obj) const;
53 
60  bool operator!=(const Coordinate& obj) const;
61 
69  Coordinate operator-(const Coordinate& obj) const;
70 
76  [[nodiscard]] Coordinate clone() const;
77 
85  friend std::ostream& operator<<(std::ostream& out, const Coordinate& coord);
86 
87  int x;
88  int y;
89  int z;
90 };
91 
96 struct Chunk {
105  struct Iterator {
106  using iterator_category = std::forward_iterator_tag;
107  using difference_type = std::ptrdiff_t;
109  using pointer = BlockType*;
111 
117  Iterator(pointer ptr) : m_ptr(ptr) {}
118 
125  reference operator*() const { return *m_ptr; }
126 
132  pointer operator->() { return m_ptr; }
133 
141  m_ptr++;
142  return *this;
143  }
144 
154  Iterator tmp = *this;
155  ++(*this);
156  return tmp;
157  }
158 
167  friend bool operator==(const Iterator& a, const Iterator& b) {
168  return a.m_ptr == b.m_ptr;
169  };
170 
179  friend bool operator!=(const Iterator& a, const Iterator& b) {
180  return a.m_ptr != b.m_ptr;
181  };
182 
183  private:
184  pointer m_ptr;
185  };
186 
187  Iterator begin() { return Iterator(&raw_data[0]); }
188  Iterator end() { return Iterator(&raw_data[_x_len * _y_len * _z_len]); }
189 
193  Chunk(const Coordinate& loc1, const Coordinate& loc2,
194  const std::vector<BlockType>& block_list);
195 
197 
198  Chunk& operator=(const Chunk& other) noexcept;
199 
208 
217  BlockType get(int x, int y, int z);
218 
223  int x_len() const;
224 
229  int y_len() const;
230 
235  int z_len() const;
236 
242 
243  private:
244  Coordinate _base_pt;
245  int _y_len;
246  int _x_len;
247  int _z_len;
248  BlockType* raw_data;
249 };
250 
255 struct HeightMap {
262  struct Iterator {
263  using iterator_category = std::forward_iterator_tag;
264  using difference_type = std::ptrdiff_t;
265  using value_type = int;
266  using pointer = int*;
267  using reference = int&;
268 
274  Iterator(pointer ptr) : m_ptr(ptr) {}
275 
282  reference operator*() const { return *m_ptr; }
283 
289  pointer operator->() { return m_ptr; }
290 
298  m_ptr++;
299  return *this;
300  }
301 
311  Iterator tmp = *this;
312  ++(*this);
313  return tmp;
314  }
315 
324  friend bool operator==(const Iterator& a, const Iterator& b) {
325  return a.m_ptr == b.m_ptr;
326  };
327 
336  friend bool operator!=(const Iterator& a, const Iterator& b) {
337  return a.m_ptr != b.m_ptr;
338  };
339 
340  private:
341  pointer m_ptr;
342  };
343 
344  Iterator begin() { return Iterator(&raw_heights[0]); }
345  Iterator end() { return Iterator(&raw_heights[_x_len * _z_len]); }
346  HeightMap(const Coordinate& loc1, const Coordinate& loc2,
347  const std::vector<int>& heights);
348 
350 
351  HeightMap& operator=(const HeightMap& other) noexcept;
352 
360  int get(int x, int z) const;
361 
367  int get_worldspace(const Coordinate& loc) const;
368 
374  void fill_coord(Coordinate& out);
375 
380  int x_len() const;
381 
386  int z_len() const;
387 
393 
394  private:
395  Coordinate _base_pt;
396  int _x_len;
397  int _z_len;
398  int* raw_heights;
399 };
400 
401 } // namespace mcpp
BlockType class.
Definition: block.h:10
Namespace containing all the the mcpp library classes.
Definition: block.h:9
An iterator for the Chunk's 3D block data.
Definition: util.h:105
BlockType * pointer
Definition: util.h:109
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition: util.h:117
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition: util.h:179
std::ptrdiff_t difference_type
Definition: util.h:107
pointer operator->()
Access the pointer to the current element.
Definition: util.h:132
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition: util.h:153
std::forward_iterator_tag iterator_category
Definition: util.h:106
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition: util.h:140
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition: util.h:167
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition: util.h:125
Definition: util.h:96
Coordinate base_pt() const
int x_len() const
BlockType get_worldspace(const Coordinate &pos)
BlockType get(int x, int y, int z)
Iterator begin()
Definition: util.h:187
Iterator end()
Definition: util.h:188
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
Definition: util.h:18
Coordinate operator+(const Coordinate &obj) const
Adds two Coordinate objects.
Coordinate(double x, double y, double z)
Constructs a Coordinate object with double values.
int y
Definition: util.h:88
Coordinate operator-(const Coordinate &obj) const
Subtracts one Coordinate object from another.
int z
Definition: util.h:89
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.
bool operator==(const Coordinate &obj) const
Checks if two Coordinate objects are equal.
int x
Definition: util.h:87
Coordinate(int x=0, int y=0, int z=0)
Constructs a Coordinate object with integer values.
An iterator for the HeightMap structure.
Definition: util.h:262
int & reference
Definition: util.h:267
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition: util.h:336
pointer operator->()
Access the pointer to the current element.
Definition: util.h:289
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition: util.h:310
int value_type
Definition: util.h:265
std::forward_iterator_tag iterator_category
Definition: util.h:263
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition: util.h:282
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition: util.h:274
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition: util.h:297
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition: util.h:324
int * pointer
Definition: util.h:266
std::ptrdiff_t difference_type
Definition: util.h:264
Definition: util.h:255
int get_worldspace(const Coordinate &loc) const
Iterator begin()
Definition: util.h:344
int z_len() const
Coordinate base_pt() const
HeightMap & operator=(const HeightMap &other) noexcept
Iterator end()
Definition: util.h:345
HeightMap(const Coordinate &loc1, const Coordinate &loc2, const std::vector< int > &heights)
int get(int x, int z) const
int x_len() const
void fill_coord(Coordinate &out)