]> git.localhorst.tv Git - blank.git/blob - src/generator.cpp
update light levels from border on neighbor change
[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)
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 {
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 }