From 436bfd24bacba7a99014896a29c1a72d1d59f3bf Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 11 Dec 2024 16:30:19 +0300 Subject: [PATCH] update ChunksStorage --- dev/tests/example.lua | 21 +-------------------- dev/tests/world.lua | 23 +++++++++++++++++++++++ src/frontend/hud.cpp | 2 +- src/voxels/ChunksStorage.cpp | 29 +++++++++++++++++------------ src/voxels/ChunksStorage.hpp | 11 ++++------- src/world/Level.cpp | 4 ---- 6 files changed, 46 insertions(+), 44 deletions(-) create mode 100644 dev/tests/world.lua diff --git a/dev/tests/example.lua b/dev/tests/example.lua index a1a0c369..8cde7829 100644 --- a/dev/tests/example.lua +++ b/dev/tests/example.lua @@ -1,20 +1 @@ --- Create/close/open/close world - --- Open -test.new_world("demo", "2019", "core:default") -assert(world.is_open()) -assert(world.get_generator() == "core:default") -test.sleep(1) -assert(world.get_total_time() > 0.0) -print(world.get_total_time()) - --- Close -test.close_world(true) -assert(not world.is_open()) - --- Reopen -test.open_world("demo") -assert(world.is_open()) -assert(world.get_total_time() > 0.0) -test.tick() -test.close_world(true) +print("hello world") diff --git a/dev/tests/world.lua b/dev/tests/world.lua new file mode 100644 index 00000000..3e7353e3 --- /dev/null +++ b/dev/tests/world.lua @@ -0,0 +1,23 @@ +-- Create/close/open/close world + +-- Open +test.new_world("demo", "2019", "core:default") +assert(world.is_open()) +assert(world.get_generator() == "core:default") +test.sleep(1) +assert(world.get_total_time() > 0.0) +print(world.get_total_time()) + +-- Close +test.close_world(true) +assert(not world.is_open()) + +-- Reopen +test.open_world("demo") +assert(world.is_open()) +assert(world.get_total_time() > 0.0) +assert(world.get_seed() == 2019) +test.tick() + +-- Close +test.close_world(true) diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 21111e18..20199f0a 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -295,7 +295,7 @@ void Hud::updateWorldGenDebugVisualization() { data[(flippedZ * width + x) * 4 + 1] = level.chunks->getChunk(ax + ox, az + oz) ? 255 : 0; data[(flippedZ * width + x) * 4 + 0] = - level.chunksStorage->get(ax + ox, az + oz) ? 255 : 0; + level.chunksStorage->fetch(ax + ox, az + oz) ? 255 : 0; if (ax < 0 || az < 0 || ax >= areaWidth || az >= areaHeight) { diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index 410448c0..26b4fbd3 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -20,23 +20,18 @@ static debug::Logger logger("chunks-storage"); ChunksStorage::ChunksStorage(Level* level) : level(level) { } -void ChunksStorage::store(const std::shared_ptr& chunk) { - chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk; -} +std::shared_ptr ChunksStorage::fetch(int x, int z) { + std::lock_guard lock(mutex); -std::shared_ptr ChunksStorage::get(int x, int z) const { auto found = chunksMap.find(glm::ivec2(x, z)); if (found == chunksMap.end()) { return nullptr; } - return found->second; -} - -void ChunksStorage::remove(int x, int z) { - auto found = chunksMap.find(glm::ivec2(x, z)); - if (found != chunksMap.end()) { - chunksMap.erase(found->first); + auto ptr = found->second.lock(); + if (ptr == nullptr) { + chunksMap.erase(found); } + return ptr; } static void check_voxels(const ContentIndices& indices, Chunk* chunk) { @@ -63,11 +58,21 @@ static void check_voxels(const ContentIndices& indices, Chunk* chunk) { } std::shared_ptr ChunksStorage::create(int x, int z) { + std::lock_guard lock(mutex); + + auto found = chunksMap.find(glm::ivec2(x, z)); + if (found != chunksMap.end()) { + auto chunk = found->second.lock(); + if (chunk) { + return chunk; + } + } + World* world = level->getWorld(); auto& regions = world->wfile.get()->getRegions(); auto chunk = std::make_shared(x, z); - store(chunk); + chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk; if (auto data = regions.getVoxels(chunk->x, chunk->z)) { const auto& indices = *level->content->getIndices(); diff --git a/src/voxels/ChunksStorage.hpp b/src/voxels/ChunksStorage.hpp index 35fff6c1..78bc03d0 100644 --- a/src/voxels/ChunksStorage.hpp +++ b/src/voxels/ChunksStorage.hpp @@ -1,11 +1,9 @@ #pragma once +#include #include #include -#include "typedefs.hpp" -#include "voxel.hpp" - #define GLM_ENABLE_EXPERIMENTAL #include @@ -14,13 +12,12 @@ class Level; class ChunksStorage { Level* level; - std::unordered_map> chunksMap; + std::mutex mutex; + std::unordered_map> chunksMap; public: ChunksStorage(Level* level); ~ChunksStorage() = default; - std::shared_ptr get(int x, int z) const; - void store(const std::shared_ptr& chunk); - void remove(int x, int y); + std::shared_ptr fetch(int x, int z); std::shared_ptr create(int x, int z); }; diff --git a/src/world/Level.cpp b/src/world/Level.cpp index ed370d08..402c8e5f 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -62,10 +62,6 @@ Level::Level( ); lighting = std::make_unique(content, chunks.get()); - events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type, Chunk* chunk) { - this->chunksStorage->remove(chunk->x, chunk->z); - }); - inventories = std::make_unique(*this); }