feat: add app.save_world() function and update documentation
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "constants.hpp"
|
||||
#include "lighting/Lightmap.hpp"
|
||||
#include "util/SmallHeap.hpp"
|
||||
#include "maths/aabb.hpp"
|
||||
#include "voxel.hpp"
|
||||
|
||||
inline constexpr int CHUNK_DATA_LEN = CHUNK_VOL * 4;
|
||||
@@ -75,4 +76,11 @@ public:
|
||||
bool decode(const ubyte* data);
|
||||
|
||||
static void convert(ubyte* data, const ContentReport* report);
|
||||
|
||||
AABB getAABB() const {
|
||||
return AABB(
|
||||
glm::vec3(x * CHUNK_W, -INFINITY, z * CHUNK_D),
|
||||
glm::vec3((x + 1) * CHUNK_W, INFINITY, (z + 1) * CHUNK_D)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -24,6 +24,10 @@ GlobalChunks::GlobalChunks(Level* level)
|
||||
chunksMap.max_load_factor(CHUNKS_MAP_MAX_LOAD_FACTOR);
|
||||
}
|
||||
|
||||
void GlobalChunks::setOnUnload(consumer<Chunk&> onUnload) {
|
||||
this->onUnload = std::move(onUnload);
|
||||
}
|
||||
|
||||
std::shared_ptr<Chunk> GlobalChunks::fetch(int x, int z) {
|
||||
const auto& found = chunksMap.find(keyfrom(x, z));
|
||||
if (found == chunksMap.end()) {
|
||||
@@ -160,6 +164,9 @@ void GlobalChunks::decref(Chunk* chunk) {
|
||||
ekey.pos[0] = chunk->x;
|
||||
ekey.pos[1] = chunk->z;
|
||||
|
||||
if (onUnload) {
|
||||
onUnload(*chunk);
|
||||
}
|
||||
save(chunk);
|
||||
chunksMap.erase(ekey.key);
|
||||
refCounters.erase(found);
|
||||
@@ -170,17 +177,11 @@ void GlobalChunks::save(Chunk* chunk) {
|
||||
if (chunk == nullptr) {
|
||||
return;
|
||||
}
|
||||
AABB aabb(
|
||||
glm::vec3(chunk->x * CHUNK_W, -INFINITY, chunk->z * CHUNK_D),
|
||||
glm::vec3(
|
||||
(chunk->x + 1) * CHUNK_W, INFINITY, (chunk->z + 1) * CHUNK_D
|
||||
)
|
||||
);
|
||||
AABB aabb = chunk->getAABB();
|
||||
auto entities = level->entities->getAllInside(aabb);
|
||||
auto root = dv::object();
|
||||
root["data"] = level->entities->serialize(entities);
|
||||
if (!entities.empty()) {
|
||||
level->entities->despawn(std::move(entities));
|
||||
chunk->flags.entities = true;
|
||||
}
|
||||
level->getWorld()->wfile->getRegions().put(
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <glm/gtx/hash.hpp>
|
||||
|
||||
#include "voxel.hpp"
|
||||
#include "delegates.hpp"
|
||||
|
||||
class Chunk;
|
||||
class Level;
|
||||
@@ -30,10 +31,14 @@ class GlobalChunks {
|
||||
std::unordered_map<uint64_t, std::shared_ptr<Chunk>> chunksMap;
|
||||
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> pinnedChunks;
|
||||
std::unordered_map<ptrdiff_t, int> refCounters;
|
||||
|
||||
consumer<Chunk&> onUnload;
|
||||
public:
|
||||
GlobalChunks(Level* level);
|
||||
~GlobalChunks() = default;
|
||||
|
||||
void setOnUnload(consumer<Chunk&> onUnload);
|
||||
|
||||
std::shared_ptr<Chunk> fetch(int x, int z);
|
||||
std::shared_ptr<Chunk> create(int x, int z);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user