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