VOX
A little voxel engine
Loading...
Searching...
No Matches
Chunk.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "hashes.hpp"
4#include <array>
5#include <glm/vec3.hpp>
6#include <mutex>
7#include <condition_variable>
8#include <unordered_map>
9#include <unordered_set>
10#include "Block.hpp"
11#include "Status.hpp"
12#include "define.hpp"
13#include "Tracy.hpp"
14
15#define CHUNK_Y_SIZE 512
16#define CHUNK_X_SIZE 16
17#define CHUNK_Z_SIZE 16
18#define BLOCKS_PER_CHUNK CHUNK_Y_SIZE * CHUNK_X_SIZE * CHUNK_Z_SIZE
19#define CHUNK_SIZE_IVEC3 glm::ivec3(CHUNK_X_SIZE, CHUNK_Y_SIZE, CHUNK_Z_SIZE)
20#define CHUNK_SIZE_VEC3 glm::vec3(CHUNK_X_SIZE, CHUNK_Y_SIZE, CHUNK_Z_SIZE)
21
22glm::ivec3 getChunkPos(const glm::ivec3 & block_pos);
23glm::ivec3 getBlockChunkPos(const glm::ivec3 & block_pos);
24
25class ChunkData;
26class Chunk
27{
28public:
29 enum class genLevel : uint16_t
30 {
31 LIGHT,
33 CAVE,
34 EMPTY,
35 };
36 enum class e_biome : uint8_t
37 {
38 FOREST,
39 PLAIN,
41 OCEAN,
42 COAST,
43 DESERT,
44 RIVER,
45 NONE
46 };
47 struct biomeInfo
48 {
49 biomeInfo() = default;
50 biomeInfo(const biomeInfo & other) = default;
51 biomeInfo(biomeInfo && other) = default;
52 biomeInfo & operator=(const biomeInfo & other) = default;
53 biomeInfo & operator=(biomeInfo && other) = default;
54
55 bool operator==(const biomeInfo & other)const = default;
56 bool isLand = false;
57 bool isOcean = false;
58 bool isCoast = false;
59 float continentalness = 0;
60 float erosion = 0;
61 float humidity = 0;
62 float temperature = 0;
63 float weirdness = 0;
64 float PV = 0;
66 };
67
68 typedef std::array<BlockInfo::Type, BLOCKS_PER_CHUNK> BlockArray;
69 typedef std::array<uint8_t, BLOCKS_PER_CHUNK> LightArray;
70 typedef std::array<uint8_t, CHUNK_X_SIZE * CHUNK_Z_SIZE> HeightArray;
71
72 // one biome info per 4 blocks
73 typedef std::array<biomeInfo, CHUNK_X_SIZE * CHUNK_Z_SIZE> BiomeArray;
74
75 constexpr static size_t DATA_SIZE = sizeof(BlockArray) + sizeof(LightArray) + sizeof(BiomeArray) + sizeof(genLevel);
76
77 Chunk(glm::ivec3 position);
78 // Chunk(const glm::ivec3 & position, const BlockArray & blocks);
79 Chunk(const glm::ivec3 & position, const BlockArray & blocks, const LightArray & light, const BiomeArray & biome);
80 Chunk(const ChunkData & data);
81
82 Chunk(const Chunk & other) = delete;
83 Chunk(Chunk && other) = delete;
84 Chunk & operator=(const Chunk & other) = delete;
85 Chunk & operator=(const Chunk && other) = delete;
86 ~Chunk();
87
89 const BlockArray & getBlocks() const;
90 BlockInfo::Type getBlock(const int & x, const int & y, const int & z) const;
91 BlockInfo::Type getBlock(const glm::ivec3 & position) const;
92 void setBlock(const int & x, const int & y, const int & z, BlockInfo::Type block);
93 void setBlock(const glm::ivec3 & position, BlockInfo::Type block);
94 void setBlockColumn(const int & x, const int & z, const std::array<BlockInfo::Type, CHUNK_Y_SIZE> & column);
95 void setBlockColumn(const glm::ivec2 & pos, const std::array<BlockInfo::Type, CHUNK_Y_SIZE> & column);
96
98 const LightArray & getLight() const;
99 uint8_t getLight(const int & x, const int & y, const int & z) const;
100 uint8_t getLight(const glm::ivec3 & position) const;
101 void setLight(const int & x, const int & y, const int & z, uint8_t light);
102 void setLight(const glm::ivec3 & position, uint8_t light);
103
104 uint8_t getSkyLight(const int & x, const int & y, const int & z) const;
105 uint8_t getSkyLight(const glm::ivec3 & position) const;
106 void setSkyLight(const int & x, const int & y, const int & z, uint8_t light);
107 void setSkyLight(const glm::ivec3 & position, uint8_t light);
108
109 uint8_t getBlockLight(const int & x, const int & y, const int & z) const;
110 uint8_t getBlockLight(const glm::ivec3 & position) const;
111 void setBlockLight(const int & x, const int & y, const int & z, uint8_t light);
112 void setBlockLight(const glm::ivec3 & position, uint8_t light);
113
115 const BiomeArray & getBiomes() const;
116 biomeInfo getBiome(const int & x, const int & z) const;
117 biomeInfo getBiome(const glm::ivec2 & position) const;
118 void setBiome(const int & x, const int & z, const biomeInfo & biome);
119 void setBiome(const glm::ivec2 & position, const biomeInfo & biome);
120 static int toBiomeIndex(int x, int z);
121 static int toBiomeIndex(const glm::ivec2 & position);
122 static glm::ivec2 toBiomeCoord(int index);
123
125 const HeightArray & getHeights() const;
126 uint8_t getHeight(const int & x, const int & z) const;
127 uint8_t getHeight(const glm::ivec2 & position) const;
128 void setHeight(const int & x, const int & z, uint8_t height);
129 void setHeight(const glm::ivec2 & position, uint8_t height);
130 static int toHeightIndex(const int & x, const int & z);
131 static int toHeightIndex(const glm::ivec2 & position);
132
133
134 const glm::ivec3 & getPosition() const;
135 void setPosition(const glm::ivec3 & position);
136
137 const int & x()const {return position.x;};
138 const int & y()const {return position.y;};
139 const int & z()const {return position.z;};
140
141 uint64_t getMeshID() const;
142 void setMeshID(const uint64_t & mesh_id);
143
144 bool isMeshed() const;
145 void setMeshed(bool meshed);
146
147 int getLoadLevel() const;
148 int getHighestLoadLevel() const;
149 void setLoadLevel(const int & load_level);
150
151 genLevel getGenLevel() const;
152 void setGenLevel(const genLevel & level);
153
154 static int toIndex(const int & x, const int & y, const int & z);
155 static glm::ivec3 toCoord(const int & index);
156
157
158 // TracySharedLockableN (std::shared_mutex, , "Chunk Status");
160 std::unordered_set<uint64_t> entity_ids;
161 std::unordered_set<uint64_t> observing_player_ids;
162private:
163 std::atomic<bool> meshed = false;
164 glm::ivec3 position;
165 std::atomic<uint64_t> m_mesh_id;
166 mutable TracyLockableN (std::mutex, m_mesh_id_mutex, "Mesh ID");
167 BlockArray m_blocks;
168 // 4 left bits for block light, 4 right bits for sky light
169 LightArray m_light;
170 BiomeArray m_biomes;
171 HeightArray m_heights;
172 int load_level = std::numeric_limits<int>::max();
173 int highest_load_level = 0;
174 genLevel m_gen_level = genLevel::EMPTY;
175};
176
177typedef std::unordered_map<glm::ivec3, std::shared_ptr<Chunk>> ChunkMap;
179namespace std
180{
181 template<>
182 struct hash<Chunk::genLevel>
183 {
184 std::size_t operator()(const Chunk::genLevel & level) const
185 {
186 return std::hash<uint16_t>()(static_cast<uint16_t>(level));
187 }
188 };
189
190 template <>
191 struct hash<std::pair<glm::ivec3, Chunk::genLevel>>
192 {
193 std::size_t operator()(const std::pair<glm::ivec3, Chunk::genLevel> & pair) const
194 {
195 return std::hash<glm::ivec3>()(pair.first) ^ std::hash<Chunk::genLevel>()(pair.second);
196 }
197 };
198}
199
201{
202 ChunkData() = default;
203 ChunkData(const Chunk & chunk);
204
205 ChunkData(ChunkData && other) = default;
206 ChunkData & operator=(ChunkData && other) = default;
207 ChunkData(const ChunkData & other) = default;
208 ChunkData & operator=(const ChunkData & other) = default;
209
210 std::vector<char> serialize() const;
211 void deserialize(const char * data, const size_t & size);
212
218 glm::ivec3 position;
219 static constexpr size_t DATA_SIZE = sizeof(blocks) + sizeof(light) + sizeof(biome) + sizeof(height) + sizeof(gen_level) + sizeof(position);
220};
glm::ivec3 getChunkPos(const glm::ivec3 &block_pos)
Definition: Chunk.cpp:374
Chunk::e_biome BiomeType
Definition: Chunk.hpp:178
std::unordered_map< glm::ivec3, std::shared_ptr< Chunk > > ChunkMap
Definition: Chunk.hpp:177
glm::ivec3 getBlockChunkPos(const glm::ivec3 &block_pos)
Definition: Chunk.cpp:393
Type
Definition: Block.hpp:33
Definition: Chunk.hpp:27
void setBlockLight(const int &x, const int &y, const int &z, uint8_t light)
Definition: Chunk.cpp:206
std::unordered_set< uint64_t > observing_player_ids
Definition: Chunk.hpp:161
const int & x() const
Definition: Chunk.hpp:137
LightArray & getLight()
Definition: Chunk.cpp:137
static glm::ivec2 toBiomeCoord(int index)
Definition: Chunk.cpp:247
std::array< uint8_t, BLOCKS_PER_CHUNK > LightArray
Definition: Chunk.hpp:69
const int & y() const
Definition: Chunk.hpp:138
static glm::ivec3 toCoord(const int &index)
Definition: Chunk.cpp:366
uint8_t getSkyLight(const int &x, const int &y, const int &z) const
Definition: Chunk.cpp:172
Chunk & operator=(const Chunk &other)=delete
Chunk(const Chunk &other)=delete
BlockArray & getBlocks()
Definition: Chunk.cpp:89
std::array< uint8_t, CHUNK_X_SIZE *CHUNK_Z_SIZE > HeightArray
Definition: Chunk.hpp:70
genLevel
Definition: Chunk.hpp:30
static constexpr size_t DATA_SIZE
Definition: Chunk.hpp:75
void setSkyLight(const int &x, const int &y, const int &z, uint8_t light)
Definition: Chunk.cpp:183
static int toIndex(const int &x, const int &y, const int &z)
Definition: Chunk.cpp:361
HeightArray & getHeights()
Definition: Chunk.cpp:265
std::array< biomeInfo, CHUNK_X_SIZE *CHUNK_Z_SIZE > BiomeArray
Definition: Chunk.hpp:73
Status status
Definition: Chunk.hpp:159
BiomeArray & getBiomes()
Definition: Chunk.cpp:217
void setBiome(const int &x, const int &z, const biomeInfo &biome)
Definition: Chunk.cpp:237
void setGenLevel(const genLevel &level)
Definition: Chunk.cpp:356
uint8_t getHeight(const int &x, const int &z) const
Definition: Chunk.cpp:275
bool isMeshed() const
Definition: Chunk.cpp:325
void setMeshID(const uint64_t &mesh_id)
Definition: Chunk.cpp:320
biomeInfo getBiome(const int &x, const int &z) const
Definition: Chunk.cpp:227
int getHighestLoadLevel() const
Definition: Chunk.cpp:340
void setBlock(const int &x, const int &y, const int &z, BlockInfo::Type block)
Definition: Chunk.cpp:111
uint64_t getMeshID() const
Definition: Chunk.cpp:315
void setHeight(const int &x, const int &z, uint8_t height)
Definition: Chunk.cpp:285
genLevel getGenLevel() const
Definition: Chunk.cpp:351
Chunk(Chunk &&other)=delete
void setPosition(const glm::ivec3 &position)
Definition: Chunk.cpp:310
void setLoadLevel(const int &load_level)
Definition: Chunk.cpp:344
BlockInfo::Type getBlock(const int &x, const int &y, const int &z) const
Definition: Chunk.cpp:99
std::unordered_set< uint64_t > entity_ids
Definition: Chunk.hpp:160
Chunk & operator=(const Chunk &&other)=delete
std::array< BlockInfo::Type, BLOCKS_PER_CHUNK > BlockArray
Definition: Chunk.hpp:68
e_biome
Definition: Chunk.hpp:37
void setBlockColumn(const int &x, const int &z, const std::array< BlockInfo::Type, CHUNK_Y_SIZE > &column)
Definition: Chunk.cpp:124
const glm::ivec3 & getPosition() const
Definition: Chunk.cpp:305
void setLight(const int &x, const int &y, const int &z, uint8_t light)
Definition: Chunk.cpp:159
~Chunk()
Definition: Chunk.cpp:84
static int toBiomeIndex(int x, int z)
Definition: Chunk.cpp:255
int getLoadLevel() const
Definition: Chunk.cpp:335
void setMeshed(bool meshed)
Definition: Chunk.cpp:330
uint8_t getBlockLight(const int &x, const int &y, const int &z) const
Definition: Chunk.cpp:195
static int toHeightIndex(const int &x, const int &z)
Definition: Chunk.cpp:295
const int & z() const
Definition: Chunk.hpp:139
Definition: Status.hpp:10
Definition: ObjLoader.hpp:65
Definition: Chunk.hpp:201
Chunk::LightArray light
Definition: Chunk.hpp:214
Chunk::genLevel gen_level
Definition: Chunk.hpp:217
ChunkData & operator=(ChunkData &&other)=default
std::vector< char > serialize() const
Definition: ChunkData.cpp:15
Chunk::BiomeArray biome
Definition: Chunk.hpp:215
Chunk::HeightArray height
Definition: Chunk.hpp:216
ChunkData(const ChunkData &other)=default
void deserialize(const char *data, const size_t &size)
Definition: ChunkData.cpp:48
static constexpr size_t DATA_SIZE
Definition: Chunk.hpp:219
ChunkData(ChunkData &&other)=default
ChunkData()=default
glm::ivec3 position
Definition: Chunk.hpp:218
Chunk::BlockArray blocks
Definition: Chunk.hpp:213
ChunkData & operator=(const ChunkData &other)=default
Definition: Chunk.hpp:48
bool operator==(const biomeInfo &other) const =default
float continentalness
Definition: Chunk.hpp:59
biomeInfo(const biomeInfo &other)=default
float PV
Definition: Chunk.hpp:64
e_biome biome
Definition: Chunk.hpp:65
float weirdness
Definition: Chunk.hpp:63
bool isCoast
Definition: Chunk.hpp:58
biomeInfo & operator=(const biomeInfo &other)=default
float temperature
Definition: Chunk.hpp:62
float erosion
Definition: Chunk.hpp:60
biomeInfo(biomeInfo &&other)=default
biomeInfo()=default
biomeInfo & operator=(biomeInfo &&other)=default
float humidity
Definition: Chunk.hpp:61
bool isOcean
Definition: Chunk.hpp:57
bool isLand
Definition: Chunk.hpp:56
std::size_t operator()(const Chunk::genLevel &level) const
Definition: Chunk.hpp:184
std::size_t operator()(const std::pair< glm::ivec3, Chunk::genLevel > &pair) const
Definition: Chunk.hpp:193