update ChunksStorage
This commit is contained in:
parent
91fa2838d6
commit
436bfd24ba
@ -1,20 +1 @@
|
|||||||
-- Create/close/open/close world
|
print("hello 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)
|
|
||||||
|
|||||||
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] =
|
data[(flippedZ * width + x) * 4 + 1] =
|
||||||
level.chunks->getChunk(ax + ox, az + oz) ? 255 : 0;
|
level.chunks->getChunk(ax + ox, az + oz) ? 255 : 0;
|
||||||
data[(flippedZ * width + x) * 4 + 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 ||
|
if (ax < 0 || az < 0 ||
|
||||||
ax >= areaWidth || az >= areaHeight) {
|
ax >= areaWidth || az >= areaHeight) {
|
||||||
|
|||||||
@ -20,23 +20,18 @@ static debug::Logger logger("chunks-storage");
|
|||||||
ChunksStorage::ChunksStorage(Level* level) : level(level) {
|
ChunksStorage::ChunksStorage(Level* level) : level(level) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ChunksStorage::store(const std::shared_ptr<Chunk>& chunk) {
|
std::shared_ptr<Chunk> ChunksStorage::fetch(int x, int z) {
|
||||||
chunksMap[glm::ivec2(chunk->x, chunk->z)] = chunk;
|
std::lock_guard lock(mutex);
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Chunk> ChunksStorage::get(int x, int z) const {
|
|
||||||
auto found = chunksMap.find(glm::ivec2(x, z));
|
auto found = chunksMap.find(glm::ivec2(x, z));
|
||||||
if (found == chunksMap.end()) {
|
if (found == chunksMap.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
return found->second;
|
auto ptr = found->second.lock();
|
||||||
}
|
if (ptr == nullptr) {
|
||||||
|
chunksMap.erase(found);
|
||||||
void ChunksStorage::remove(int x, int z) {
|
|
||||||
auto found = chunksMap.find(glm::ivec2(x, z));
|
|
||||||
if (found != chunksMap.end()) {
|
|
||||||
chunksMap.erase(found->first);
|
|
||||||
}
|
}
|
||||||
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check_voxels(const ContentIndices& indices, Chunk* chunk) {
|
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::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();
|
World* world = level->getWorld();
|
||||||
auto& regions = world->wfile.get()->getRegions();
|
auto& regions = world->wfile.get()->getRegions();
|
||||||
|
|
||||||
auto chunk = std::make_shared<Chunk>(x, z);
|
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)) {
|
if (auto data = regions.getVoxels(chunk->x, chunk->z)) {
|
||||||
const auto& indices = *level->content->getIndices();
|
const auto& indices = *level->content->getIndices();
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "typedefs.hpp"
|
|
||||||
#include "voxel.hpp"
|
|
||||||
|
|
||||||
#define GLM_ENABLE_EXPERIMENTAL
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
#include <glm/gtx/hash.hpp>
|
#include <glm/gtx/hash.hpp>
|
||||||
|
|
||||||
@ -14,13 +12,12 @@ class Level;
|
|||||||
|
|
||||||
class ChunksStorage {
|
class ChunksStorage {
|
||||||
Level* level;
|
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:
|
public:
|
||||||
ChunksStorage(Level* level);
|
ChunksStorage(Level* level);
|
||||||
~ChunksStorage() = default;
|
~ChunksStorage() = default;
|
||||||
|
|
||||||
std::shared_ptr<Chunk> get(int x, int z) const;
|
std::shared_ptr<Chunk> fetch(int x, int z);
|
||||||
void store(const std::shared_ptr<Chunk>& chunk);
|
|
||||||
void remove(int x, int y);
|
|
||||||
std::shared_ptr<Chunk> create(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());
|
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);
|
inventories = std::make_unique<Inventories>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user