2020-07-25 16:45:03 +00:00
|
|
|
#include "Universe.hpp"
|
2020-07-10 17:49:16 +00:00
|
|
|
|
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-25 16:45:03 +00:00
|
|
|
using namespace world;
|
|
|
|
|
|
|
|
Universe::Universe(const Universe::options &options): loadPool(2), contouring(std::make_shared<contouring::Dummy>()) {
|
2020-07-10 17:49:16 +00:00
|
|
|
setOptions(options);
|
|
|
|
}
|
2020-07-25 16:45:03 +00:00
|
|
|
Universe::~Universe() {
|
2020-07-18 15:42:45 +00:00
|
|
|
contouring = NULL;
|
|
|
|
}
|
2020-07-10 17:49:16 +00:00
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
Universe::LoadPool::LoadPool(size_t count) {
|
2020-07-22 20:55:13 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
workers.push_back(std::thread([&] {
|
|
|
|
while (running) {
|
|
|
|
chunk_pos ctx;
|
|
|
|
loadQueue.wait();
|
|
|
|
if (loadQueue.pop(ctx)) {
|
|
|
|
rmt_ScopedCPUSample(ProcessGenerate, 0);
|
|
|
|
loadedQueue.push({ctx, std::make_shared<Chunk>(ctx, generator)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
2020-07-25 16:45:03 +00:00
|
|
|
Universe::LoadPool::~LoadPool() {
|
2020-07-22 20:55:13 +00:00
|
|
|
running = false;
|
|
|
|
loadQueue.notify();
|
|
|
|
|
|
|
|
for (auto &worker : workers) {
|
|
|
|
if (worker.joinable())
|
|
|
|
worker.join();
|
|
|
|
}
|
|
|
|
}
|
2020-07-25 16:45:03 +00:00
|
|
|
inline void Universe::LoadPool::push(const chunk_pos &pos, int weight) { loadQueue.push(pos, weight); }
|
|
|
|
inline bool Universe::LoadPool::pop(robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>> &out) { return loadedQueue.pop(out); }
|
|
|
|
inline size_t Universe::LoadPool::size() { return loadQueue.size(); }
|
2020-07-22 20:55:13 +00:00
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
void Universe::update(const camera_pos& pos, Universe::report& rep) {
|
2020-07-10 17:49:16 +00:00
|
|
|
const chunk_pos newPos = glm::divide(pos, chunk_voxel_pos(CHUNK_LENGTH));
|
|
|
|
const auto chunkChange = newPos != last_pos;
|
|
|
|
last_pos = newPos;
|
2020-07-25 16:45:03 +00:00
|
|
|
rmt_ScopedCPUSample(Universe, 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-22 20:55:13 +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-25 14:29:05 +00:00
|
|
|
if (const auto neighbors = chunk->update()) {
|
|
|
|
contouring->onUpdate(chunkPos, chunks, neighbors.value()); //TODO: get update update_type(simple(pos), complex)
|
2020-07-18 15:42:45 +00:00
|
|
|
} else if (chunkChange) { //NOTE: must be solved before octrees
|
|
|
|
contouring->onNotify(chunkPos, chunks);
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-22 20:55:13 +00:00
|
|
|
{
|
|
|
|
rmt_ScopedCPUSample(Contouring, 0);
|
|
|
|
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);
|
2020-07-24 19:42:47 +00:00
|
|
|
for (size_t i = 0; i < 256 && !unloadQueue.empty(); i++) {
|
2020-07-23 19:39:08 +00:00
|
|
|
chunks.erase(unloadQueue.pop());
|
2020-07-18 12:54:07 +00:00
|
|
|
//TODO: save to file
|
|
|
|
}
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:55:13 +00:00
|
|
|
// Find missing chunks
|
2020-07-10 17:49:16 +00:00
|
|
|
if(chunkChange) {
|
2020-07-18 12:54:07 +00:00
|
|
|
rmt_ScopedCPUSample(ToLoad, 0);
|
2020-07-22 20:55:13 +00:00
|
|
|
//TODO: circle point algo
|
2020-07-10 17:49:16 +00:00
|
|
|
for (int x = -loadDistance; x <= loadDistance; x++) {
|
|
|
|
for (int y = -loadDistance; y <= loadDistance; y++) {
|
|
|
|
for (int z = -loadDistance; z <= loadDistance; z++) {
|
2020-07-22 20:55:13 +00:00
|
|
|
const auto dist2 = x * x + y * y + z * z;
|
|
|
|
if (dist2 <= loadDistance * loadDistance) {
|
2020-07-10 17:49:16 +00:00
|
|
|
const chunk_pos p = last_pos + glm::ivec3(x, y, z);
|
|
|
|
if (chunks.find(p) == chunks.end()) {
|
2020-07-22 20:55:13 +00:00
|
|
|
loadPool.push(p, -dist2);
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}}}
|
|
|
|
}
|
|
|
|
|
2020-07-22 20:55:13 +00:00
|
|
|
rep.chunk_load.push(loadPool.size());
|
|
|
|
// Loaded chunks
|
2020-07-18 12:54:07 +00:00
|
|
|
{
|
|
|
|
rmt_ScopedCPUSample(Load, 0);
|
2020-07-23 19:39:08 +00:00
|
|
|
robin_hood::pair<chunk_pos, std::shared_ptr<Chunk>> loaded;
|
2020-07-22 20:55:13 +00:00
|
|
|
while (loadPool.pop(loaded)) {
|
|
|
|
chunks.insert(loaded);
|
|
|
|
contouring->onUpdate(loaded.first, chunks, Faces::All);
|
2020-07-14 17:03:32 +00:00
|
|
|
}
|
2020-07-10 17:49:16 +00:00
|
|
|
}
|
|
|
|
rep.chunk_count.push(chunks.size());
|
|
|
|
}
|
2020-07-25 16:45:03 +00:00
|
|
|
void Universe::setOptions(const Universe::options& options) {
|
2020-07-10 17:49:16 +00:00
|
|
|
loadDistance = options.loadDistance;
|
|
|
|
keepDistance = options.keepDistance;
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
void Universe::setContouring(std::shared_ptr<contouring::Abstract> ct) {
|
2020-07-18 15:42:45 +00:00
|
|
|
contouring = ct;
|
|
|
|
last_pos = chunk_pos(INT_MAX); // trigger chunkChange on next update
|
2020-07-10 19:37:49 +00:00
|
|
|
}
|
2020-07-25 14:29:05 +00:00
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
std::optional<std::pair<voxel_pos, Voxel>> Universe::raycast(const Ray &ray) const {
|
2020-07-25 14:29:05 +00:00
|
|
|
std::vector<voxel_pos> points;
|
|
|
|
ray.grid(points);
|
|
|
|
std::shared_ptr<Chunk> chunk = NULL;
|
|
|
|
chunk_pos chunk_pos(INT_MAX);
|
|
|
|
for(auto point: points) {
|
|
|
|
const auto pos = glm::divide(point, glm::ivec3(CHUNK_LENGTH));
|
|
|
|
if(pos != chunk_pos) {
|
|
|
|
if(const auto& newChunk = at(pos)) {
|
|
|
|
chunk = newChunk.value();
|
|
|
|
chunk_pos = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(chunk != NULL) {
|
|
|
|
const auto voxel = chunk->getAt(glm::modulo(point, glm::uvec3(CHUNK_LENGTH)));
|
|
|
|
if(voxel.Density > 0)
|
|
|
|
return {{point, voxel}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-07-25 16:45:03 +00:00
|
|
|
std::optional<Item> Universe::set(const voxel_pos& pos, const Voxel& val) {
|
2020-07-25 14:29:05 +00:00
|
|
|
const auto chunkPos = glm::divide(pos, glm::ivec3(CHUNK_LENGTH));
|
|
|
|
if(const auto& chunk = at(chunkPos)) {
|
|
|
|
return {chunk.value()->breakAt(glm::modulo(pos, glm::ivec3(CHUNK_LENGTH)), val)};
|
|
|
|
} else {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2020-07-25 16:45:03 +00:00
|
|
|
ItemList Universe::setCube(const voxel_pos& pos, const Voxel& val, int radius) {
|
2020-07-25 14:29:05 +00:00
|
|
|
ItemList list;
|
|
|
|
for (int z = -radius; z <= radius; z++) {
|
|
|
|
for (int y = -radius; y <= radius; y++) {
|
|
|
|
for (int x = -radius; x <= radius; x++) {
|
|
|
|
//TODO: list.pop(val)
|
|
|
|
list.add(set(pos + glm::lvec3(x, y, z), val));
|
|
|
|
}}}
|
|
|
|
return list;
|
|
|
|
}
|