update ChunksStorage
This commit is contained in:
parent
91fa2838d6
commit
436bfd24ba
@ -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")
|
||||
|
||||
23
dev/tests/world.lua
Normal file
23
dev/tests/world.lua
Normal file
@ -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)
|
||||
@ -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) {
|
||||
|
||||
@ -20,23 +20,18 @@ static debug::Logger logger("chunks-storage");
|
||||
ChunksStorage::ChunksStorage(Level* level) : level(level) {
|
||||
}
|
||||
|
||||
void ChunksStorage::store(const std::shared_ptr<Chunk>& chunk) {
|
||||
chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk;
|
||||
}
|
||||
std::shared_ptr<Chunk> ChunksStorage::fetch(int x, int z) {
|
||||
std::lock_guard lock(mutex);
|
||||
|
||||
std::shared_ptr<Chunk> 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<Chunk> 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<Chunk>(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();
|
||||
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "typedefs.hpp"
|
||||
#include "voxel.hpp"
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/gtx/hash.hpp>
|
||||
|
||||
@ -14,13 +12,12 @@ class Level;
|
||||
|
||||
class ChunksStorage {
|
||||
Level* level;
|
||||
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> chunksMap;
|
||||
std::mutex mutex;
|
||||
std::unordered_map<glm::ivec2, std::weak_ptr<Chunk>> chunksMap;
|
||||
public:
|
||||
ChunksStorage(Level* level);
|
||||
~ChunksStorage() = default;
|
||||
|
||||
std::shared_ptr<Chunk> get(int x, int z) const;
|
||||
void store(const std::shared_ptr<Chunk>& chunk);
|
||||
void remove(int x, int y);
|
||||
std::shared_ptr<Chunk> fetch(int x, int z);
|
||||
std::shared_ptr<Chunk> create(int x, int z);
|
||||
};
|
||||
|
||||
@ -62,10 +62,6 @@ Level::Level(
|
||||
);
|
||||
lighting = std::make_unique<Lighting>(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<Inventories>(*this);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user