#include "ChunkLoader.hpp"
#include "Generator.hpp"
+#include "WorldCollision.hpp"
#include <algorithm>
+#include <iostream>
#include <limits>
#include <queue>
}
}
+namespace {
+
+// propagate light from a's edge to b
+void edge_light(
+ Chunk &a, const Chunk::Pos &a_pos,
+ Chunk &b, const Chunk::Pos &b_pos
+) noexcept {
+ if (a.GetLight(a_pos) > 1) {
+ const BlockType &b_type = b.Type(Chunk::ToIndex(b_pos));
+ if (!b_type.block_light) {
+ light_queue.emplace(&a, a_pos);
+ }
+ if (b_type.visible) {
+ b.Invalidate();
+ }
+ }
+}
+
+}
+
void Chunk::SetNeighbor(Chunk &other) noexcept {
if (other.position == position + Pos(-1, 0, 0)) {
if (neighbor[Block::FACE_LEFT] != &other) {
for (int y = 0; y < height; ++y) {
Pos my_pos(0, y, z);
Pos other_pos(width - 1, y, z);
- if (GetLight(my_pos) > 0) {
- light_queue.emplace(this, my_pos);
- }
- if (other.GetLight(other_pos) > 0) {
- light_queue.emplace(&other, other_pos);
- }
+ edge_light(*this, my_pos, other, other_pos);
+ edge_light(other, other_pos, *this, my_pos);
}
}
work_light();
for (int y = 0; y < height; ++y) {
Pos my_pos(width - 1, y, z);
Pos other_pos(0, y, z);
- if (GetLight(my_pos) > 0) {
- light_queue.emplace(this, my_pos);
- }
- if (other.GetLight(other_pos) > 0) {
- light_queue.emplace(&other, other_pos);
- }
+ edge_light(*this, my_pos, other, other_pos);
+ edge_light(other, other_pos, *this, my_pos);
}
}
work_light();
for (int x = 0; x < width; ++x) {
Pos my_pos(x, 0, z);
Pos other_pos(x, height - 1, z);
- if (GetLight(my_pos) > 0) {
- light_queue.emplace(this, my_pos);
- }
- if (other.GetLight(other_pos) > 0) {
- light_queue.emplace(&other, other_pos);
- }
+ edge_light(*this, my_pos, other, other_pos);
+ edge_light(other, other_pos, *this, my_pos);
}
}
work_light();
for (int x = 0; x < width; ++x) {
Pos my_pos(x, height - 1, z);
Pos other_pos(x, 0, z);
- if (GetLight(my_pos) > 0) {
- light_queue.emplace(this, my_pos);
- }
- if (other.GetLight(other_pos) > 0) {
- light_queue.emplace(&other, other_pos);
- }
+ edge_light(*this, my_pos, other, other_pos);
+ edge_light(other, other_pos, *this, my_pos);
}
}
work_light();
for (int x = 0; x < width; ++x) {
Pos my_pos(x, y, 0);
Pos other_pos(x, y, depth - 1);
- if (GetLight(my_pos) > 0) {
- light_queue.emplace(this, my_pos);
- }
- if (other.GetLight(other_pos) > 0) {
- light_queue.emplace(&other, other_pos);
- }
+ edge_light(*this, my_pos, other, other_pos);
+ edge_light(other, other_pos, *this, my_pos);
}
}
work_light();
for (int x = 0; x < width; ++x) {
Pos my_pos(x, y, depth - 1);
Pos other_pos(x, y, 0);
- if (GetLight(my_pos) > 0) {
- light_queue.emplace(this, my_pos);
- }
- if (other.GetLight(other_pos) > 0) {
- light_queue.emplace(&other, other_pos);
- }
+ edge_light(*this, my_pos, other, other_pos);
+ edge_light(other, other_pos, *this, my_pos);
}
}
work_light();
}
}
+bool Chunk::Intersection(
+ const AABB &box,
+ const glm::mat4 &Mbox,
+ const glm::mat4 &Mchunk,
+ std::vector<WorldCollision> &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.collision) {
+ 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 {
, loaded()
, to_generate()
, to_free()
+, gen_timer(config.gen_limit)
, load_dist(config.load_dist)
, unload_dist(config.unload_dist) {
-
+ gen_timer.Start();
}
namespace {
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;
// 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;
}
// 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;
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;
}
if (iter->Position() == pos) {
iter->Relink();
loaded.splice(loaded.end(), to_free, iter);
- return;
}
}