X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2Fchunk.cpp;h=2c5e7ec4ebad297570c5ef71fd2799822e5e30cc;hb=32909aa3224ec0ed5656721178eb6ad31cd047df;hp=fe4f80267ab00d1f4bf2c572c7f278168cf03915;hpb=57ff8c89c8e172b0988a09490a2bc19d740d1c79;p=blank.git diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index fe4f802..2c5e7ec 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -3,8 +3,10 @@ #include "ChunkLoader.hpp" #include "Generator.hpp" +#include "WorldCollision.hpp" #include +#include #include #include @@ -169,15 +171,16 @@ void Chunk::SetBlock(int index, const Block &block) noexcept { } else if (!new_type.block_light && old_type.block_light) { // obstacle removed int level = 0; + Pos pos(ToPos(index)); for (int face = 0; face < Block::FACE_COUNT; ++face) { - BlockLookup next_block(this, ToPos(index), Block::Face(face)); + BlockLookup next_block(this, pos, Block::Face(face)); if (next_block) { - level = std::min(level, next_block.GetLight()); + level = std::max(level, next_block.GetLight()); } } if (level > 1) { SetLight(index, level - 1); - light_queue.emplace(this, ToPos(index)); + light_queue.emplace(this, pos); work_light(); } } @@ -449,7 +452,6 @@ bool Chunk::Intersection( float &dist, glm::vec3 &normal ) const noexcept { - // TODO: should be possible to heavily optimize this int idx = 0; blkid = -1; dist = std::numeric_limits::infinity(); @@ -481,6 +483,36 @@ bool Chunk::Intersection( } } +bool Chunk::Intersection( + const AABB &box, + const glm::mat4 &Mbox, + const glm::mat4 &Mchunk, + std::vector &col +) const noexcept { + bool any = false; + 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, penetration, normal)) { + col.emplace_back(this, idx, penetration, normal); + any = true; + } + } + } + } + return any; +} + namespace { @@ -627,9 +659,10 @@ ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, 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 +807,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 +821,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 +832,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 +847,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 +860,6 @@ void ChunkLoader::Update() { if (iter->Position() == pos) { iter->Relink(); loaded.splice(loaded.end(), to_free, iter); - return; } }