#define BLANK_WORLD_CHUNKLOADER_HPP_
#include "Chunk.hpp"
+#include "../app/IntervalTimer.hpp"
#include <list>
struct Config {
int load_dist = 6;
int unload_dist = 8;
+ int gen_limit = 16;
};
ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept;
bool Known(const Chunk::Pos &) noexcept;
Chunk &ForceLoad(const Chunk::Pos &);
+ bool OutOfRange(const Chunk &c) const noexcept { return OutOfRange(c.Position()); }
+ bool OutOfRange(const Chunk::Pos &) const noexcept;
+
void Rebase(const Chunk::Pos &);
- void Update();
+ void Update(int dt);
private:
Chunk &Generate(const Chunk::Pos &pos);
std::list<Chunk::Pos> to_generate;
std::list<Chunk> to_free;
+ IntervalTimer gen_timer;
+
int load_dist;
int unload_dist;
, 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;
}
}