VOX
A little voxel engine
Loading...
Searching...
No Matches
World.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "List.hpp"
4#include "Player.hpp"
5#include "WorldGenerator.hpp"
6#include "logger.hpp"
8#include "tasks.hpp"
9#include "Mob.hpp"
10#include "hashes.hpp"
11#include "Tracy.hpp"
12#include "Structures.hpp"
13#include "Save.hpp"
14
15#define GLM_ENABLE_EXPERIMENTAL
16#include <glm/glm.hpp>
17
18
19#include <unordered_map>
20#include <shared_mutex>
21#include <unordered_set>
22
23class World
24{
25public:
27 {
28 using enum Chunk::genLevel;
29 public:
30 typedef std::unordered_set<glm::ivec3> ChunkGenList;
31
32
33 // void drawNoises(RenderAPI & vk);
34 // struct genInfo
35 // {
36 // struct zone
37 // {
38 // Chunk::genLevel level;
39 // Chunk::genLevel oldLevel;
40 // glm::ivec3 size;
41 // glm::ivec3 start;
42 // };
43 // std::vector<zone> zones;
44 // };
45
50 // constexpr static std::array<glm::ivec3, 3> ZONE_SIZES = {
51 // glm::ivec3(1, 0, 1),
52 // glm::ivec3(1, 0, 1),
53 // glm::ivec3(1, 0, 1)
54 // };
55 // constexpr static int MAX_TICKET_LEVEL = TICKET_LEVEL_INACTIVE + 2;
56
57 //gen level 0 if full chunkGeneration
58
59
60 WorldGenerator(World & world);
61
63
72 // static genInfo getGenInfo(Chunk::genLevel gen_level, Chunk::genLevel old_level, glm::ivec3 chunkPos3D);
73
74 // static Chunk::genLevel ticketToGenLevel(int ticket_level);
75
84 // void generate(genInfo::zone info, ChunkMap & chunkGenGrid);
85
92 std::shared_ptr<task::TaskGraph> getGenerationGraph(ChunkGenList & chunks_to_gen);
93
94
95 // double m_avg = 0;
96 // int m_called = 0;
97 // float m_max = -1;
98 // float m_min = 1;
99 private:
100 constexpr static int FOREST_TREE_FREQUENCY = 12;
101 constexpr static int PLAIN_TREE_FREQUENCY = 120;
102 uint64_t seed = 0;
103 struct genStruct
104 {
105 genStruct()
106 :
107 graph(task::TaskGraph::create()),
108 light_graph(task::TaskGraph::create()),
109 relief_graph(task::TaskGraph::create()),
110 decorate_graph(task::TaskGraph::create())
111 {
112 task::Task light_task = graph->emplace(light_graph).Name("Light passes");
113 task::Task relief_task = graph->emplace(relief_graph).Name("Relief passes");
114 task::Task decorate_task = graph->emplace(decorate_graph).Name("Decoration passes");
115 relief_task.precede(decorate_task);
116 decorate_task.precede(light_task);
117 light_graph->emplace([]{}); // dummy task
118 relief_graph->emplace([]{}); // dummy task
119 decorate_graph->emplace([]{}); // dummy task
120 graph->emplace([]{});
121 }
122 std::shared_ptr<task::TaskGraph> graph;
123 std::shared_ptr<task::TaskGraph> light_graph;
124 std::shared_ptr<task::TaskGraph> relief_graph;
125 std::shared_ptr<task::TaskGraph> decorate_graph;
126 std::unordered_set<std::pair<glm::ivec3, Chunk::genLevel>> generated_chunk;
127 };
128 /*
129 * PERLINS
130 */
131 Perlin m_relief_perlin;
132 Perlin m_cave_perlin;
133
134
135 // BIOMES PERLINS
136 Perlin m_continentalness_perlin;
137 Perlin m_erosion_perlin;
138 Perlin m_weirdness_perlin;
139 Perlin m_temperature_perlin;
140 Perlin m_humidity_perlin;
141
142 World & m_world;
143
144
145
146 /*********************\
147 * NOISE MANIPULATION *
148 \*********************/
149 float calculatePeaksAndValleys(const float & weirdness);
150 float calculateHeightBias(const float & erosion, const float & PV);
151 float calculateBaseHeight(
152 const float & relief,
153 const float & continentalness,
154 const float & pv,
155 const float & erosion);
156 int temperatureLevel(const float & temperature);
157 int humidityLevel(const float & humidity);
158 int continentalnessLevel(const float & continentalness);
159 int erosionLevel(const float & erosion);
160 int weirdnessLevel(const float & weirdness);
161 int PVLevel(const float & PV);
162
163 BiomeType getBiomeType(
164 const int & continentalness,
165 const int & erosion,
166 const int & humidity,
167 const int & temperature,
168 const int & weirdness,
169 const int & PV,
170 const int & relief);
171 std::array<BlockType, CHUNK_Y_SIZE> getBlockColumn(int baseHeight, BiomeType biome);
172 void carve(std::array<BlockType, CHUNK_Y_SIZE> & blocks, glm::ivec3 pos);
173
174
175
176
177
178 /****************************************\
179 *
180 * GENERATION PASSES
181 *
182 \****************************************/
183 void setupPass(genStruct & genData,const glm::ivec3 & chunk_pos, Chunk::genLevel gen_level);
184 void addPassToGraph(genStruct & genData, glm::ivec3 chunk_pos, Chunk::genLevel gen_level);
185
186 void lightPass(const glm::ivec3 & pos);
187
188 void reliefPass(const glm::ivec3 & pos);
189
190 void newReliefPass(const glm::ivec3 & pos);
191 /********************\
192 * RELIEF PASS UTILS
193 \********************/
194 BlockInfo::Type generateCaveBlock(glm::ivec3 position);
195 float generateReliefValue(glm::ivec2 position);
196
197 void decoratePass(const glm::ivec3 & pos);
198 /********************\
199 * DECORATE PASS UTILS
200 \********************/
201 void placeStructure(ChunkMap & chunkGrid, const glm::ivec3 & start_pos, const StructureInfo & structure);
202 /*
203 * BLOCK PLACING UTILS
204 */
205 };
206
207 World(bool save = false);
208 ~World();
209
210 World(World & other) = delete;
211 World(World && other) = delete;
212 World & operator=(World & other) = delete;
213 World & operator=(World && other) = delete;
214
221 static glm::vec3 getBlockChunkPosition(const glm::vec3 & position);
222
229 static glm::vec3 getChunkPosition(const glm::vec3 & position);
230
238 std::shared_ptr<Chunk> getChunk(const glm::ivec3 & position);
239 void insertChunk(const glm::ivec3 & position, std::shared_ptr<Chunk> chunk);
240
247 void unloadRegion(const glm::ivec2 region_pos);
248
251protected:
252
253 std::unordered_map<uint64_t, std::shared_ptr<Player>> m_players;
254 TracyLockableN (std::mutex, m_players_mutex, "Players");
255
256 std::unordered_map<uint64_t, std::shared_ptr<Mob>> m_mobs;
257 TracyLockableN (std::mutex, m_mobs_mutex, "Mobs");
258 uint64_t m_mob_id = 0;
259
260 std::unordered_map<glm::ivec3, std::shared_ptr<Chunk>> m_chunks;
261 mutable TracyLockableN (std::mutex, m_chunks_mutex, "Chunks");
262
264
265
267
268 std::unique_ptr<Save> m_save;
269
278 std::shared_ptr<Chunk> getChunkNoLock(const glm::ivec3 & position);
279
280
281 void insertChunkNoLock(const glm::ivec3 & position, std::shared_ptr<Chunk> chunk);
282 /*************************************
283 * FUTURES
284 *************************************/
285 // void waitForFinishedFutures();
286 // void waitForFutures();
287
288 /*************************************
289 * LIGHTS
290 *************************************/
291
299 std::unordered_set<glm::ivec3> updateSkyLight(const glm::ivec3 & block_position);
300
308 std::unordered_set<glm::ivec3> updateBlockLight(const glm::ivec3 & block_position);
309};
310
std::unordered_map< glm::ivec3, std::shared_ptr< Chunk > > ChunkMap
Definition: Chunk.hpp:177
Type
Definition: Block.hpp:33
genLevel
Definition: Chunk.hpp:30
e_biome
Definition: Chunk.hpp:37
Definition: ExecutorAccessor.hpp:10
Perlin noise generator.
Definition: Perlin.hpp:23
a class to save and load chunks from the disk
Definition: Save.hpp:18
Definition: Structures.hpp:13
Definition: World.hpp:27
std::unordered_set< glm::ivec3 > ChunkGenList
Definition: World.hpp:30
~WorldGenerator()
Definition: WorldGenerator.cpp:37
std::shared_ptr< task::TaskGraph > getGenerationGraph(ChunkGenList &chunks_to_gen)
get the generation info for the given gen level and chunk position
Definition: WorldGenerator.cpp:362
Definition: World.hpp:24
WorldGenerator & getWorldGenerator()
Definition: World.cpp:14
ExecutorAccessor m_executor
Definition: World.hpp:263
Save & getSave()
TracyLockableN(std::mutex, m_chunks_mutex, "Chunks")
World & operator=(World &other)=delete
std::shared_ptr< Chunk > getChunkNoLock(const glm::ivec3 &position)
Get a chunk ptr without locking the chunks mutex.
Definition: World.cpp:47
TracyLockableN(std::mutex, m_players_mutex, "Players")
TracyLockableN(std::mutex, m_mobs_mutex, "Mobs")
WorldGenerator m_world_generator
Definition: World.hpp:266
void insertChunkNoLock(const glm::ivec3 &position, std::shared_ptr< Chunk > chunk)
Definition: World.cpp:72
std::unordered_set< glm::ivec3 > updateSkyLight(const glm::ivec3 &block_position)
Update the sky light of the chunk containing the block.
Definition: World.cpp:114
std::unique_ptr< Save > m_save
Definition: World.hpp:268
uint64_t m_mob_id
Definition: World.hpp:258
std::shared_ptr< Chunk > getChunk(const glm::ivec3 &position)
Get the Chunk object.
Definition: World.cpp:40
~World()
Definition: World.cpp:10
World(World &other)=delete
World(World &&other)=delete
std::unordered_set< glm::ivec3 > updateBlockLight(const glm::ivec3 &block_position)
Update the block light of the chunk containing the block.
Definition: World.cpp:328
std::unordered_map< uint64_t, std::shared_ptr< Player > > m_players
Definition: World.hpp:253
std::unordered_map< uint64_t, std::shared_ptr< Mob > > m_mobs
Definition: World.hpp:256
static glm::vec3 getBlockChunkPosition(const glm::vec3 &position)
will return the block position relative to the chunk
Definition: World.cpp:19
void unloadRegion(const glm::ivec2 region_pos)
Unloads all chunk inside a region, if save is enabled, will save the region in a region file.
Definition: World.cpp:88
void insertChunk(const glm::ivec3 &position, std::shared_ptr< Chunk > chunk)
Definition: World.cpp:66
static glm::vec3 getChunkPosition(const glm::vec3 &position)
will return the chunk position containing the block
Definition: World.cpp:32
World & operator=(World &&other)=delete
std::unordered_map< glm::ivec3, std::shared_ptr< Chunk > > m_chunks
Definition: World.hpp:260
Definition: Task.hpp:10
Task & precede(Task &t)
add a precedence link from this task to
Definition: Task.hpp:28
Task & Name(const std::string &name)
Definition: Task.hpp:55
Definition: Executor.cpp:5