]> git.localhorst.tv Git - blank.git/blob - src/world/Generator.cpp
save and load world seed
[blank.git] / src / world / Generator.cpp
1 #include "Generator.hpp"
2
3 #include "Chunk.hpp"
4 #include "../rand/OctaveNoise.hpp"
5
6 #include <glm/glm.hpp>
7
8
9 namespace blank {
10
11 Generator::Generator(const Config &config) noexcept
12 : solidNoise(config.seed)
13 , typeNoise(config.seed)
14 , stretch(1.0f/config.stretch)
15 , solid_threshold(config.solid_threshold)
16 , space(0)
17 , light(0)
18 , solids() {
19
20 }
21
22
23 void Generator::operator ()(Chunk &chunk) const noexcept {
24         Chunk::Pos pos(chunk.Position());
25         glm::vec3 coords(pos * Chunk::Extent());
26         for (int z = 0; z < Chunk::depth; ++z) {
27                 for (int y = 0; y < Chunk::height; ++y) {
28                         for (int x = 0; x < Chunk::width; ++x) {
29                                 Block::Pos block_pos(x, y, z);
30                                 glm::vec3 gen_pos = (coords + block_pos) * stretch;
31                                 float val = OctaveNoise(solidNoise, coords + block_pos, 3, 0.5f, stretch, 2.0f);
32                                 if (val > solid_threshold) {
33                                         int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size();
34                                         chunk.SetBlock(block_pos, Block(solids[type_val]));
35                                 } else {
36                                         chunk.SetBlock(block_pos, Block(space));
37                                 }
38                         }
39                 }
40         }
41         unsigned int random = 263167 * pos.x + 2097593 * pos.y + 426389 * pos.z;
42         for (int index = 0; index < Chunk::size; ++index) {
43                 if (chunk.IsSurface(index)) {
44                         random = random * 666649 + 7778777;
45                         if ((random % 32) == 0) {
46                                 chunk.SetBlock(index, Block(light));
47                         }
48                 }
49         }
50         chunk.Invalidate();
51         chunk.CheckUpdate();
52 }
53
54 }