2020-07-10 17:49:16 +00:00
|
|
|
#include "World.hpp"
|
|
|
|
|
2020-07-18 15:42:45 +00:00
|
|
|
#include "../contouring/Dummy.hpp"
|
2020-07-18 12:54:07 +00:00
|
|
|
#include <Remotery.h>
|
2020-07-10 17:49:16 +00:00
|
|
|
|
2020-07-18 15:42:45 +00:00
|
|
|
World::World(const World::options &options): contouring(std::make_shared<contouring::Dummy>()) {
|
2020-07-10 17:49:16 +00:00
|
|
|
setOptions(options);
|
|
|
|
}
|
2020-07-18 15:42:45 +00:00
|
|
|
World::~World() {
|
|
|
|
contouring = NULL;
|
|
|
|
}
|
2020-07-10 17:49:16 +00:00
|
|
|
|
|
|
|
void World::update(const camera_pos& pos, World::report& rep) {
|
|
|
|
const chunk_pos newPos = glm::divide(pos, chunk_voxel_pos(CHUNK_LENGTH));
|
|
|
|
const auto chunkChange = newPos != last_pos;
|
|
|
|
last_pos = newPos;
|
2020-07-18 12:54:07 +00:00
|
|
|
rmt_ScopedCPUSample(World, 0);
|
2020-07-10 17:49:16 +00:00
|
|
|
|
|
|
|
// Update alive chunks
|
|
|
|
{
|
2020-07-18 12:54:07 +00:00
|
|
|
rmt_ScopedCPUSample(Update, 0);
|
2020-07-10 17:49:16 +00:00
|
|
|
for(auto [chunkPos, chunk]: chunks) {
|
2020-07-18 15:42:45 +00:00
|
|
|
if (glm::length2(last_pos - chunkPos) > keepDistance * keepDistance
|
2020-07-10 17:49:16 +00:00
|
|
|
&& unloadQueue.push(chunkPos)) {
|
|
|
|
//TODO: unloadCount++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-07-18 15:42:45 +00:00
|
|
|
if (chunk->update()) { // MAYBE: also contour joints
|
|
|
|
contouring->onUpdate(chunkPos, chunks); //TODO: get update update_type(simple(pos), complex)
|
|
|
|
} else if (chunkChange) { //NOTE: must be solved before octrees
|
|
|
|
contouring->onNotify(chunkPos, chunks);
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 15:42:45 +00:00
|
|
|
contouring->update(pos);
|
2020-07-10 17:49:16 +00:00
|
|
|
|
|
|
|
rep.chunk_unload.push(unloadQueue.size());
|
|
|
|
// Unload dead chunks
|
2020-07-18 12:54:07 +00:00
|
|
|
{
|
|
|
|
rmt_ScopedCPUSample(Unload, 0);
|
|
|
|
for (size_t i = 0; i < 8 && !unloadQueue.empty(); i++) {
|
|
|
|
chunks.extract(unloadQueue.pop());
|
|
|
|
//TODO: save to file
|
|
|
|
}
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find missing chunks (~240ms from max loadDistance)
|
|
|
|
if(chunkChange) {
|
2020-07-18 12:54:07 +00:00
|
|
|
rmt_ScopedCPUSample(ToLoad, 0);
|
2020-07-10 17:49:16 +00:00
|
|
|
std::vector<chunk_pos> to_load;
|
|
|
|
for (int x = -loadDistance; x <= loadDistance; x++) {
|
|
|
|
for (int y = -loadDistance; y <= loadDistance; y++) {
|
|
|
|
for (int z = -loadDistance; z <= loadDistance; z++) {
|
|
|
|
if (x * x + y * y + z * z <= loadDistance * loadDistance) {
|
|
|
|
const chunk_pos p = last_pos + glm::ivec3(x, y, z);
|
|
|
|
if (chunks.find(p) == chunks.end()) {
|
|
|
|
to_load.push_back(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}}
|
|
|
|
std::sort(to_load.begin(), to_load.end(), [](const chunk_pos &a, const chunk_pos &b) {
|
|
|
|
return glm::length2(a) < glm::length2(b);
|
|
|
|
});
|
|
|
|
for(auto p: to_load) {
|
|
|
|
loadQueue.push(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rep.chunk_load.push(loadQueue.size());
|
|
|
|
// Load chunks
|
2020-07-18 12:54:07 +00:00
|
|
|
{
|
|
|
|
rmt_ScopedCPUSample(Load, 0);
|
|
|
|
for (size_t i = 0; i < 8 && !loadQueue.empty(); i++) {
|
|
|
|
const auto pos = loadQueue.pop();
|
|
|
|
const auto chunk = std::make_shared<Chunk>(pos, generator);
|
|
|
|
chunks.insert({pos, chunk});
|
|
|
|
//trigger surronding render
|
2020-07-18 15:42:45 +00:00
|
|
|
//MAYBE: contouring dependant
|
|
|
|
for (size_t i = 0; i < 6; i++) {
|
2020-07-18 12:54:07 +00:00
|
|
|
const auto it = chunks.find(pos + g_face_offsets[i]);
|
|
|
|
if (it != chunks.end())
|
|
|
|
it->second->invalidate();
|
|
|
|
}
|
2020-07-14 17:03:32 +00:00
|
|
|
}
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
rep.chunk_count.push(chunks.size());
|
|
|
|
}
|
|
|
|
void World::setOptions(const World::options& options) {
|
|
|
|
loadDistance = options.loadDistance;
|
|
|
|
keepDistance = options.keepDistance;
|
|
|
|
}
|
|
|
|
|
2020-07-18 15:42:45 +00:00
|
|
|
void World::setContouring(std::shared_ptr<contouring::Abstract> ct) {
|
|
|
|
contouring = ct;
|
|
|
|
last_pos = chunk_pos(INT_MAX); // trigger chunkChange on next update
|
2020-07-10 19:37:49 +00:00
|
|
|
}
|