mcpp
C++ Minecraft Library
Loading...
Searching...
No Matches
chunk.h
Go to the documentation of this file.
1#pragma once
2
3#include "block.h"
4#include "coordinate.h"
5#include <memory>
6#include <vector>
7
8namespace mcpp {
13struct Chunk {
14private:
15 Coordinate _base_pt;
16 uint16_t _x_len;
17 uint16_t _y_len;
18 uint16_t _z_len;
19 std::unique_ptr<BlockType[]> _raw_data;
20
21public:
22 // Constructors and assignment
23 Chunk(const Coordinate& loc1, const Coordinate& loc2, const std::vector<BlockType>& block_list);
24 ~Chunk() = default;
25
26 Chunk(const Chunk& other)
27 : _base_pt(other._base_pt), _x_len(other._x_len), _y_len(other._y_len), _z_len(other._z_len) {
28 size_t size = _x_len * _y_len * _z_len;
29 _raw_data.reset(new BlockType[size]);
30 std::copy(other._raw_data.get(), other._raw_data.get() + size, _raw_data.get());
31 }
32
33 Chunk(Chunk&& other) noexcept = default;
34 Chunk& operator=(const Chunk& other);
35 Chunk& operator=(Chunk&& other) = default;
36
45
54 BlockType get(int x, int y, int z) const;
55
60 uint16_t x_len() const;
61
66 uint16_t y_len() const;
67
72 uint16_t z_len() const;
73
79
88 struct Iterator {
89 using iterator_category = std::forward_iterator_tag;
90 using difference_type = std::ptrdiff_t;
94
100 Iterator(pointer ptr) : m_ptr(ptr) {}
101
108 reference operator*() const { return *m_ptr; }
109
115 pointer operator->() { return m_ptr; }
116
124 m_ptr++;
125 return *this;
126 }
127
137 Iterator tmp = *this;
138 ++(*this);
139 return tmp;
140 }
141
150 friend bool operator==(const Iterator& a, const Iterator& b) { return a.m_ptr == b.m_ptr; };
151
160 friend bool operator!=(const Iterator& a, const Iterator& b) { return a.m_ptr != b.m_ptr; };
161
162 private:
163 pointer m_ptr;
164 };
165
175 using iterator_category = std::forward_iterator_tag;
176 using difference_type = std::ptrdiff_t;
178 using pointer = const BlockType*;
179 using reference = const BlockType&;
180
186 ConstIterator(pointer ptr) : m_ptr(ptr) {}
187
194 reference operator*() const { return *m_ptr; }
195
201 pointer operator->() { return m_ptr; }
202
210 m_ptr++;
211 return *this;
212 }
213
223 ConstIterator tmp = *this;
224 ++(*this);
225 return tmp;
226 }
227
236 friend bool operator==(const ConstIterator& a, const ConstIterator& b) {
237 return a.m_ptr == b.m_ptr;
238 };
239
248 friend bool operator!=(const ConstIterator& a, const ConstIterator& b) {
249 return a.m_ptr != b.m_ptr;
250 };
251
252 private:
253 pointer m_ptr;
254 };
255
256 // Iterators
257 Iterator begin() { return Iterator(&_raw_data[0]); }
258 Iterator end() { return Iterator(&_raw_data[_x_len * _y_len * _z_len]); }
259 ConstIterator begin() const { return ConstIterator(&_raw_data[0]); }
260 ConstIterator end() const { return ConstIterator(&_raw_data[_x_len * _y_len * _z_len]); }
261};
262} // namespace mcpp
BlockType class.
Definition block.h:11
Coordinate class.
Namespace containing all the the mcpp library classes.
Definition block.h:10
An iterator for the const Chunk's 3D block data.
Definition chunk.h:174
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition chunk.h:194
pointer operator->()
Access the pointer to the current element.
Definition chunk.h:201
const BlockType * pointer
Definition chunk.h:178
ConstIterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition chunk.h:222
ConstIterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition chunk.h:209
ConstIterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition chunk.h:186
std::ptrdiff_t difference_type
Definition chunk.h:176
friend bool operator==(const ConstIterator &a, const ConstIterator &b)
Equality comparison operator.
Definition chunk.h:236
friend bool operator!=(const ConstIterator &a, const ConstIterator &b)
Inequality comparison operator.
Definition chunk.h:248
std::forward_iterator_tag iterator_category
Definition chunk.h:175
An iterator for the Chunk's 3D block data.
Definition chunk.h:88
BlockType * pointer
Definition chunk.h:92
Iterator(pointer ptr)
Constructs an iterator at the given pointer position.
Definition chunk.h:100
friend bool operator!=(const Iterator &a, const Iterator &b)
Inequality comparison operator.
Definition chunk.h:160
std::ptrdiff_t difference_type
Definition chunk.h:90
pointer operator->()
Access the pointer to the current element.
Definition chunk.h:115
Iterator & operator++()
Pre-increment operator. Advances the iterator to the next position.
Definition chunk.h:123
Iterator operator++(int)
Post-increment operator. Advances the iterator to the next position.
Definition chunk.h:136
std::forward_iterator_tag iterator_category
Definition chunk.h:89
friend bool operator==(const Iterator &a, const Iterator &b)
Equality comparison operator.
Definition chunk.h:150
reference operator*() const
Dereference the iterator to access the value at the current position.
Definition chunk.h:108
Definition chunk.h:13
ConstIterator end() const
Definition chunk.h:260
Coordinate base_pt() const
BlockType get_worldspace(const Coordinate &pos) const
Chunk(Chunk &&other) noexcept=default
uint16_t y_len() const
BlockType get(int x, int y, int z) const
Iterator begin()
Definition chunk.h:257
Iterator end()
Definition chunk.h:258
Chunk & operator=(Chunk &&other)=default
Chunk & operator=(const Chunk &other)
Chunk(const Coordinate &loc1, const Coordinate &loc2, const std::vector< BlockType > &block_list)
Chunk(const Chunk &other)
Definition chunk.h:26
uint16_t z_len() const
uint16_t x_len() const
~Chunk()=default
ConstIterator begin() const
Definition chunk.h:259
Definition coordinate.h:14