]> git.localhorst.tv Git - blank.git/blobdiff - src/world/chunk.cpp
figure out depth and normal in box/box test
[blank.git] / src / world / chunk.cpp
index 569bb11c9f8e240ff25864cf7323ce8aed579504..7591266be5433ecb6b7a0e6c200e1eb3026a68e7 100644 (file)
@@ -5,6 +5,7 @@
 #include "Generator.hpp"
 
 #include <algorithm>
+#include <iostream>
 #include <limits>
 #include <queue>
 
@@ -481,6 +482,33 @@ bool Chunk::Intersection(
        }
 }
 
+bool Chunk::Intersection(
+       const AABB &box,
+       const glm::mat4 &Mbox,
+       const glm::mat4 &Mchunk
+) const noexcept {
+       float penetration;
+       glm::vec3 normal;
+
+       if (!blank::Intersection(box, Mbox, Bounds(), Mchunk, penetration, normal)) {
+               return false;
+       }
+       for (int idx = 0, z = 0; z < depth; ++z) {
+               for (int y = 0; y < height; ++y) {
+                       for (int x = 0; x < width; ++x, ++idx) {
+                               const BlockType &type = Type(idx);
+                               if (!type.visible) {
+                                       continue;
+                               }
+                               if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox)) {
+                                       return true;
+                               }
+                       }
+               }
+       }
+       return false;
+}
+
 
 namespace {
 
@@ -627,9 +655,10 @@ ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry &reg, con
 , loaded()
 , to_generate()
 , to_free()
+, gen_timer(config.gen_limit)
 , load_dist(config.load_dist)
 , unload_dist(config.unload_dist) {
-
+       gen_timer.Start();
 }
 
 namespace {
@@ -774,6 +803,12 @@ Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
        return Generate(pos);
 }
 
+bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept {
+       return std::abs(base.x - pos.x) > unload_dist
+                       || std::abs(base.y - pos.y) > unload_dist
+                       || std::abs(base.z - pos.z) > unload_dist;
+}
+
 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
        if (new_base == base) {
                return;
@@ -782,9 +817,7 @@ void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
 
        // unload far away chunks
        for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
-               if (std::abs(base.x - iter->Position().x) > unload_dist
-                               || std::abs(base.y - iter->Position().y) > unload_dist
-                               || std::abs(base.z - iter->Position().z) > unload_dist) {
+               if (OutOfRange(*iter)) {
                        auto saved = iter;
                        Remove(*saved);
                        ++iter;
@@ -795,9 +828,7 @@ void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
        }
        // abort far away queued chunks
        for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
-               if (std::abs(base.x - iter->x) > unload_dist
-                               || std::abs(base.y - iter->y) > unload_dist
-                               || std::abs(base.z - iter->z) > unload_dist) {
+               if (OutOfRange(*iter)) {
                        iter = to_generate.erase(iter);
                } else {
                        ++iter;
@@ -812,8 +843,9 @@ void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) {
        Generate(pos - offset, pos + offset);
 }
 
-void ChunkLoader::Update() {
-       if (to_generate.empty()) {
+void ChunkLoader::Update(int dt) {
+       gen_timer.Update(dt);
+       if (!gen_timer.Hit() || to_generate.empty()) {
                return;
        }
 
@@ -824,7 +856,6 @@ void ChunkLoader::Update() {
                if (iter->Position() == pos) {
                        iter->Relink();
                        loaded.splice(loaded.end(), to_free, iter);
-                       return;
                }
        }