]> git.localhorst.tv Git - blank.git/blobdiff - src/chunk.cpp
fixed light propagation
[blank.git] / src / chunk.cpp
index 81c8698db803dbcc3a1e99a4ebe1a9226bf15976..cc53e912683cb99e576219709fd2151bf867019a 100644 (file)
@@ -194,7 +194,6 @@ void work_dark() {
                        }
                }
        }
-       work_light();
 }
 
 }
@@ -207,17 +206,27 @@ void Chunk::SetBlock(int index, const Block &block) {
 
        if (&old_type == &new_type) return;
 
-       if (new_type.luminosity > 0) {
-               if (GetLight(index) < new_type.luminosity) {
-                       SetLight(index, new_type.luminosity);
-                       light_queue.emplace(this, ToPos(index));
-                       work_light();
-               }
-       } else if (new_type.block_light && GetLight(index) != 0) {
+       if (new_type.luminosity > old_type.luminosity) {
+               // light added
+               SetLight(index, new_type.luminosity);
+               light_queue.emplace(this, ToPos(index));
+               work_light();
+       } else if (new_type.luminosity < old_type.luminosity) {
+               // light removed
+               dark_queue.emplace(this, ToPos(index));
                SetLight(index, 0);
+               work_dark();
+               SetLight(index, new_type.luminosity);
+               light_queue.emplace(this, ToPos(index));
+               work_light();
+       } else if (new_type.block_light && !old_type.block_light) {
+               // obstacle added
                dark_queue.emplace(this, ToPos(index));
+               SetLight(index, 0);
                work_dark();
-       } else if (old_type.block_light && !new_type.block_light) {
+               work_light();
+       } else if (!new_type.block_light && old_type.block_light) {
+               // obstacle removed
                int level = 0;
                for (int face = 0; face < Block::FACE_COUNT; ++face) {
                        Pos next_pos(ToPos(index) + Block::FaceNormal(Block::Face(face)));
@@ -263,6 +272,21 @@ int Chunk::GetLight(int index) const {
 }
 
 
+bool Chunk::IsSurface(const Pos &pos) const {
+       const Block &block = BlockAt(pos);
+       if (!Type(block).visible) {
+               return false;
+       }
+       for (int face = 0; face < Block::FACE_COUNT; ++face) {
+               const Block *next = FindNext(pos, Block::Face(face));
+               if (!next || !Type(*next).visible) {
+                       return true;
+               }
+       }
+       return false;
+}
+
+
 void Chunk::Allocate() {
        blocks.resize(Size(), Block(0));
        light.resize(Size(), 0);
@@ -455,15 +479,15 @@ glm::mat4 Chunk::ToTransform(int idx) const {
 }
 
 
-ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
+ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, const Generator &gen)
 : base(0, 0, 0)
 , reg(reg)
 , gen(gen)
 , loaded()
 , to_generate()
 , to_free()
-, load_dist(6)
-, unload_dist(8) {
+, load_dist(config.load_dist)
+, unload_dist(config.unload_dist) {
 
 }
 
@@ -603,8 +627,12 @@ void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
                }
        }
        // add missing new chunks
+       GenerateSurrounding(base);
+}
+
+void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
        const Chunk::Pos offset(load_dist, load_dist, load_dist);
-       Generate(base - offset, base + offset);
+       Generate(pos - offset, pos + offset);
 }
 
 void ChunkLoader::Update() {