Merge pull request #235 from MihailRis/blocks_meta

WorldRegions refactor
This commit is contained in:
MihailRis
2024-06-07 14:54:45 +03:00
committed by GitHub
8 changed files with 78 additions and 83 deletions
+41 -41
View File
@@ -50,17 +50,12 @@ std::unique_ptr<ubyte[]> regfile::read(int index, uint32_t& length) {
return data; return data;
} }
WorldRegion::WorldRegion() { WorldRegion::WorldRegion()
chunksData = new ubyte*[REGION_CHUNKS_COUNT]{}; : chunksData(std::make_unique<std::unique_ptr<ubyte[]>[]>(REGION_CHUNKS_COUNT)),
sizes = new uint32_t[REGION_CHUNKS_COUNT]{}; sizes(std::make_unique<uint32_t[]>(REGION_CHUNKS_COUNT))
} {}
WorldRegion::~WorldRegion() { WorldRegion::~WorldRegion() {
for (uint i = 0; i < REGION_CHUNKS_COUNT; i++) {
delete[] chunksData[i];
}
delete[] sizes;
delete[] chunksData;
} }
void WorldRegion::setUnsaved(bool unsaved) { void WorldRegion::setUnsaved(bool unsaved) {
@@ -70,23 +65,22 @@ bool WorldRegion::isUnsaved() const {
return unsaved; return unsaved;
} }
ubyte** WorldRegion::getChunks() const { std::unique_ptr<ubyte[]>* WorldRegion::getChunks() const {
return chunksData; return chunksData.get();
} }
uint32_t* WorldRegion::getSizes() const { uint32_t* WorldRegion::getSizes() const {
return sizes; return sizes.get();
} }
void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) { void WorldRegion::put(uint x, uint z, ubyte* data, uint32_t size) {
size_t chunk_index = z * REGION_SIZE + x; size_t chunk_index = z * REGION_SIZE + x;
delete[] chunksData[chunk_index]; chunksData[chunk_index].reset(data);
chunksData[chunk_index] = data;
sizes[chunk_index] = size; sizes[chunk_index] = size;
} }
ubyte* WorldRegion::getChunkData(uint x, uint z) { ubyte* WorldRegion::getChunkData(uint x, uint z) {
return chunksData[z * REGION_SIZE + x]; return chunksData[z * REGION_SIZE + x].get();
} }
uint WorldRegion::getChunkDataSize(uint x, uint z) { uint WorldRegion::getChunkDataSize(uint x, uint z) {
@@ -109,19 +103,21 @@ WorldRegion* WorldRegions::getRegion(int x, int z, int layer) {
RegionsLayer& regions = layers[layer]; RegionsLayer& regions = layers[layer];
std::lock_guard lock(regions.mutex); std::lock_guard lock(regions.mutex);
auto found = regions.regions.find(glm::ivec2(x, z)); auto found = regions.regions.find(glm::ivec2(x, z));
if (found == regions.regions.end()) if (found == regions.regions.end()) {
return nullptr; return nullptr;
}
return found->second.get(); return found->second.get();
} }
WorldRegion* WorldRegions::getOrCreateRegion(int x, int z, int layer) { WorldRegion* WorldRegions::getOrCreateRegion(int x, int z, int layer) {
WorldRegion* region = getRegion(x, z, layer); if (auto region = getRegion(x, z, layer)) {
if (region == nullptr) { return region;
}
RegionsLayer& regions = layers[layer]; RegionsLayer& regions = layers[layer];
std::lock_guard lock(regions.mutex); std::lock_guard lock(regions.mutex);
region = new WorldRegion(); auto region_ptr = std::make_unique<WorldRegion>();
regions.regions[glm::ivec2(x, z)].reset(region); auto region = region_ptr.get();
} regions.regions[{x, z}] = std::move(region_ptr);
return region; return region;
} }
@@ -153,10 +149,7 @@ inline void calc_reg_coords(
} }
std::unique_ptr<ubyte[]> WorldRegions::readChunkData( std::unique_ptr<ubyte[]> WorldRegions::readChunkData(
int x, int x, int z, uint32_t& length, regfile* rfile
int z,
uint32_t& length,
regfile* rfile
){ ){
int regionX, regionZ, localX, localZ; int regionX, regionZ, localX, localZ;
calc_reg_coords(x, z, regionX, regionZ, localX, localZ); calc_reg_coords(x, z, regionX, regionZ, localX, localZ);
@@ -166,14 +159,14 @@ std::unique_ptr<ubyte[]> WorldRegions::readChunkData(
/// @brief Read missing chunks data (null pointers) from region file /// @brief Read missing chunks data (null pointers) from region file
void WorldRegions::fetchChunks(WorldRegion* region, int x, int z, regfile* file) { void WorldRegions::fetchChunks(WorldRegion* region, int x, int z, regfile* file) {
ubyte** chunks = region->getChunks(); auto* chunks = region->getChunks();
uint32_t* sizes = region->getSizes(); uint32_t* sizes = region->getSizes();
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE; int chunk_x = (i % REGION_SIZE) + x * REGION_SIZE;
int chunk_z = (i / REGION_SIZE) + z * REGION_SIZE; int chunk_z = (i / REGION_SIZE) + z * REGION_SIZE;
if (chunks[i] == nullptr) { if (chunks[i] == nullptr) {
chunks[i] = readChunkData(chunk_x, chunk_z, sizes[i], file).release(); chunks[i] = readChunkData(chunk_x, chunk_z, sizes[i], file);
} }
} }
} }
@@ -295,18 +288,18 @@ void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry){
char intbuf[4]{}; char intbuf[4]{};
uint offsets[REGION_CHUNKS_COUNT]{}; uint offsets[REGION_CHUNKS_COUNT]{};
ubyte** region = entry->getChunks(); auto* region = entry->getChunks();
uint32_t* sizes = entry->getSizes(); uint32_t* sizes = entry->getSizes();
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
ubyte* chunk = region[i]; ubyte* chunk = region[i].get();
if (chunk == nullptr){ if (chunk == nullptr){
offsets[i] = 0; offsets[i] = 0;
} else { } else {
offsets[i] = offset; offsets[i] = offset;
size_t compressedSize = sizes[i]; size_t compressedSize = sizes[i];
dataio::write_int32_big(compressedSize, (ubyte*)intbuf, 0); dataio::write_int32_big(compressedSize, reinterpret_cast<ubyte*>(intbuf), 0);
offset += 4 + compressedSize; offset += 4 + compressedSize;
file.write(intbuf, 4); file.write(intbuf, 4);
@@ -314,7 +307,7 @@ void WorldRegions::writeRegion(int x, int z, int layer, WorldRegion* entry){
} }
} }
for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) { for (size_t i = 0; i < REGION_CHUNKS_COUNT; i++) {
dataio::write_int32_big(offsets[i], (ubyte*)intbuf, 0); dataio::write_int32_big(offsets[i], reinterpret_cast<ubyte*>(intbuf), 0);
file.write(intbuf, 4); file.write(intbuf, 4);
} }
} }
@@ -358,15 +351,20 @@ static std::unique_ptr<ubyte[]> write_inventories(Chunk* chunk, uint& datasize)
auto datavec = builder.data(); auto datavec = builder.data();
datasize = builder.size(); datasize = builder.size();
auto data = std::make_unique<ubyte[]>(datasize); auto data = std::make_unique<ubyte[]>(datasize);
for (uint i = 0; i < datasize; i++) { std::memcpy(data.get(), datavec, datasize);
data[i] = datavec[i];
}
return data; return data;
} }
/// @brief Store chunk (voxels and lights) in region (existing or new) /// @brief Store chunk data (voxels and lights) in region (existing or new)
void WorldRegions::put(Chunk* chunk){ void WorldRegions::put(Chunk* chunk){
assert(chunk != nullptr); assert(chunk != nullptr);
if (!chunk->flags.lighted) {
return;
}
bool lightsUnsaved = !chunk->flags.loadedLights && doWriteLights;
if (!chunk->flags.unsaved && !lightsUnsaved) {
return;
}
int regionX, regionZ, localX, localZ; int regionX, regionZ, localX, localZ;
calc_reg_coords(chunk->x, chunk->z, regionX, regionZ, localX, localZ); calc_reg_coords(chunk->x, chunk->z, regionX, regionZ, localX, localZ);
@@ -409,11 +407,12 @@ std::unique_ptr<light_t[]> WorldRegions::getLights(int x, int z) {
} }
chunk_inventories_map WorldRegions::fetchInventories(int x, int z) { chunk_inventories_map WorldRegions::fetchInventories(int x, int z) {
chunk_inventories_map inventories; chunk_inventories_map meta;
uint32_t bytesSize; uint32_t bytesSize;
const ubyte* data = getData(x, z, REGION_LAYER_INVENTORIES, bytesSize); const ubyte* data = getData(x, z, REGION_LAYER_INVENTORIES, bytesSize);
if (data == nullptr) if (data == nullptr) {
return inventories; return meta;
}
ByteReader reader(data, bytesSize); ByteReader reader(data, bytesSize);
int count = reader.getInt32(); int count = reader.getInt32();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@@ -423,9 +422,9 @@ chunk_inventories_map WorldRegions::fetchInventories(int x, int z) {
reader.skip(size); reader.skip(size);
auto inv = std::make_shared<Inventory>(0, 0); auto inv = std::make_shared<Inventory>(0, 0);
inv->deserialize(map.get()); inv->deserialize(map.get());
inventories[index] = inv; meta[index] = inv;
} }
return inventories; return meta;
} }
void WorldRegions::processRegionVoxels(int x, int z, const regionproc& func) { void WorldRegions::processRegionVoxels(int x, int z, const regionproc& func) {
@@ -466,8 +465,9 @@ void WorldRegions::write() {
bool WorldRegions::parseRegionFilename(const std::string& name, int& x, int& z) { bool WorldRegions::parseRegionFilename(const std::string& name, int& x, int& z) {
size_t sep = name.find('_'); size_t sep = name.find('_');
if (sep == std::string::npos || sep == 0 || sep == name.length()-1) if (sep == std::string::npos || sep == 0 || sep == name.length()-1) {
return false; return false;
}
try { try {
x = std::stoi(name.substr(0, sep)); x = std::stoi(name.substr(0, sep));
z = std::stoi(name.substr(sep+1)); z = std::stoi(name.substr(sep+1));
+3 -3
View File
@@ -38,8 +38,8 @@ public:
}; };
class WorldRegion { class WorldRegion {
ubyte** chunksData; std::unique_ptr<std::unique_ptr<ubyte[]>[]> chunksData;
uint32_t* sizes; std::unique_ptr<uint32_t[]> sizes;
bool unsaved = false; bool unsaved = false;
public: public:
WorldRegion(); WorldRegion();
@@ -52,7 +52,7 @@ public:
void setUnsaved(bool unsaved); void setUnsaved(bool unsaved);
bool isUnsaved() const; bool isUnsaved() const;
ubyte** getChunks() const; std::unique_ptr<ubyte[]>* getChunks() const;
uint32_t* getSizes() const; uint32_t* getSizes() const;
}; };
+7 -4
View File
@@ -113,8 +113,11 @@ bool Chunk::decode(const ubyte* data) {
ubyte bst1 = data[CHUNK_VOL*2 + i]; ubyte bst1 = data[CHUNK_VOL*2 + i];
ubyte bst2 = data[CHUNK_VOL*3 + i]; ubyte bst2 = data[CHUNK_VOL*3 + i];
vox.id = (blockid_t(bid1) << 8) | (blockid_t(bid2)); vox.id = (static_cast<blockid_t>(bid1) << 8) |
vox.state = int2blockstate((blockstate_t(bst1) << 8) | (blockstate_t(bst2))); static_cast<blockid_t>(bid2);
vox.state = int2blockstate(
(static_cast<blockstate_t>(bst1) << 8) |
static_cast<blockstate_t>(bst2));
} }
return true; return true;
} }
@@ -122,8 +125,8 @@ bool Chunk::decode(const ubyte* data) {
void Chunk::convert(ubyte* data, const ContentLUT* lut) { void Chunk::convert(ubyte* data, const ContentLUT* lut) {
for (uint i = 0; i < CHUNK_VOL; i++) { for (uint i = 0; i < CHUNK_VOL; i++) {
// see encode method to understand what the hell is going on here // see encode method to understand what the hell is going on here
blockid_t id = ((blockid_t(data[i]) << 8) | blockid_t id = ((static_cast<blockid_t>(data[i]) << 8) |
blockid_t(data[CHUNK_VOL+i])); static_cast<blockid_t>(data[CHUNK_VOL+i]));
blockid_t replacement = lut->getBlockId(id); blockid_t replacement = lut->getBlockId(id);
data[i] = replacement >> 8; data[i] = replacement >> 8;
data[CHUNK_VOL+i] = replacement & 0xFF; data[CHUNK_VOL+i] = replacement & 0xFF;
+14 -10
View File
@@ -5,8 +5,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <unordered_map> #include <unordered_map>
#include "../constants.hpp"
#include "voxel.hpp" #include "voxel.hpp"
#include "../constants.hpp"
#include "../lighting/Lightmap.hpp" #include "../lighting/Lightmap.hpp"
inline constexpr int CHUNK_DATA_LEN = CHUNK_VOL*4; inline constexpr int CHUNK_DATA_LEN = CHUNK_VOL*4;
@@ -15,6 +16,10 @@ class Lightmap;
class ContentLUT; class ContentLUT;
class Inventory; class Inventory;
namespace dynamic {
class Map;
}
using chunk_inventories_map = std::unordered_map<uint, std::shared_ptr<Inventory>>; using chunk_inventories_map = std::unordered_map<uint, std::shared_ptr<Inventory>>;
class Chunk { class Chunk {
@@ -32,7 +37,7 @@ public:
bool loadedLights: 1; bool loadedLights: 1;
} flags {}; } flags {};
/* Block inventories map where key is index of block in voxels array */ /// @brief Block inventories map where key is index of block in voxels array
chunk_inventories_map inventories; chunk_inventories_map inventories;
Chunk(int x, int z); Chunk(int x, int z);
@@ -44,14 +49,15 @@ public:
// unused // unused
std::unique_ptr<Chunk> clone() const; std::unique_ptr<Chunk> clone() const;
/* Creates new block inventory given size /// @brief Creates new block inventory given size
@return inventory id or 0 if block does not exists */ /// @return inventory id or 0 if block does not exists
void addBlockInventory(std::shared_ptr<Inventory> inventory, void addBlockInventory(
uint x, uint y, uint z); std::shared_ptr<Inventory> inventory, uint x, uint y, uint z
);
void removeBlockInventory(uint x, uint y, uint z); void removeBlockInventory(uint x, uint y, uint z);
void setBlockInventories(chunk_inventories_map map); void setBlockInventories(chunk_inventories_map map);
/* @return inventory bound to the given block or nullptr */ /// @return inventory bound to the given block or nullptr
std::shared_ptr<Inventory> getBlockInventory(uint x, uint y, uint z) const; std::shared_ptr<Inventory> getBlockInventory(uint x, uint y, uint z) const;
inline void setModifiedAndUnsaved() { inline void setModifiedAndUnsaved() {
@@ -61,9 +67,7 @@ public:
std::unique_ptr<ubyte[]> encode() const; std::unique_ptr<ubyte[]> encode() const;
/** /// @return true if all is fine
* @return true if all is fine
**/
bool decode(const ubyte* data); bool decode(const ubyte* data);
static void convert(ubyte* data, const ContentLUT* lut); static void convert(ubyte* data, const ContentLUT* lut);
+2 -7
View File
@@ -497,14 +497,9 @@ void Chunks::saveAndClear(){
for (size_t i = 0; i < volume; i++){ for (size_t i = 0; i < volume; i++){
Chunk* chunk = chunks[i].get(); Chunk* chunk = chunks[i].get();
chunks[i] = nullptr; chunks[i] = nullptr;
if (chunk == nullptr || !chunk->flags.lighted) if (chunk) {
continue;
bool lightsUnsaved = !chunk->flags.loadedLights &&
worldFiles->doesWriteLights();
if (!chunk->flags.unsaved && !lightsUnsaved)
continue;
regions.put(chunk); regions.put(chunk);
} }
}
chunksCount = 0; chunksCount = 0;
} }
+2
View File
@@ -59,8 +59,10 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
auto data = regions.getChunk(chunk->x, chunk->z); auto data = regions.getChunk(chunk->x, chunk->z);
if (data) { if (data) {
chunk->decode(data.get()); chunk->decode(data.get());
auto invs = regions.fetchInventories(chunk->x, chunk->z); auto invs = regions.fetchInventories(chunk->x, chunk->z);
chunk->setBlockInventories(std::move(invs)); chunk->setBlockInventories(std::move(invs));
chunk->flags.loaded = true; chunk->flags.loaded = true;
for(auto& entry : chunk->inventories) { for(auto& entry : chunk->inventories) {
level->inventories->store(entry.second); level->inventories->store(entry.second);
+5 -13
View File
@@ -35,12 +35,10 @@ World::World(
) : name(std::move(name)), ) : name(std::move(name)),
generator(std::move(generator)), generator(std::move(generator)),
seed(seed), seed(seed),
settings(settings),
content(content), content(content),
packs(packs) packs(packs),
{ wfile(std::make_unique<WorldFiles>(directory, settings.debug))
wfile = std::make_unique<WorldFiles>(directory, settings.debug); {}
}
World::~World(){ World::~World(){
} }
@@ -57,16 +55,10 @@ void World::write(Level* level) {
auto& regions = wfile->getRegions(); auto& regions = wfile->getRegions();
for (size_t i = 0; i < chunks->volume; i++) { for (size_t i = 0; i < chunks->volume; i++) {
auto chunk = chunks->chunks[i]; if (auto chunk = chunks->chunks[i]) {
if (chunk == nullptr || !chunk->flags.lighted)
continue;
bool lightsUnsaved = !chunk->flags.loadedLights &&
settings.debug.doWriteLights.get();
if (!chunk->flags.unsaved && !lightsUnsaved)
continue;
regions.put(chunk.get()); regions.put(chunk.get());
} }
}
wfile->write(this, content); wfile->write(this, content);
auto playerFile = dynamic::Map(); auto playerFile = dynamic::Map();
-1
View File
@@ -30,7 +30,6 @@ class World : Serializable {
std::string name; std::string name;
std::string generator; std::string generator;
uint64_t seed; uint64_t seed;
EngineSettings& settings;
const Content* const content; const Content* const content;
std::vector<ContentPack> packs; std::vector<ContentPack> packs;