inventory regions, minor refactor
This commit is contained in:
+48
-40
@@ -13,6 +13,7 @@
|
||||
#include "../world/World.h"
|
||||
#include "../lighting/Lightmap.h"
|
||||
|
||||
#include "../coders/byte_utils.h"
|
||||
#include "../util/data_io.h"
|
||||
#include "../coders/json.h"
|
||||
#include "../constants.h"
|
||||
@@ -189,7 +190,7 @@ void WorldFiles::put(Chunk* chunk){
|
||||
int localX = chunk->x - (regionX * REGION_SIZE);
|
||||
int localZ = chunk->z - (regionZ * REGION_SIZE);
|
||||
|
||||
/* Writing Voxels */ {
|
||||
/* Writing voxels */ {
|
||||
size_t compressedSize;
|
||||
std::unique_ptr<ubyte[]> chunk_data (chunk->encode());
|
||||
ubyte* data = compress(chunk_data.get(), CHUNK_DATA_LEN, compressedSize);
|
||||
@@ -198,15 +199,39 @@ void WorldFiles::put(Chunk* chunk){
|
||||
region->setUnsaved(true);
|
||||
region->put(localX, localZ, data, compressedSize);
|
||||
}
|
||||
/* Writing lights cache */
|
||||
if (doWriteLights && chunk->isLighted()) {
|
||||
size_t compressedSize;
|
||||
std::unique_ptr<ubyte[]> light_data (chunk->lightmap->encode());
|
||||
std::unique_ptr<ubyte[]> light_data (chunk->lightmap.encode());
|
||||
ubyte* data = compress(light_data.get(), LIGHTMAP_DATA_LEN, compressedSize);
|
||||
|
||||
WorldRegion* region = getOrCreateRegion(lights, regionX, regionZ);
|
||||
region->setUnsaved(true);
|
||||
region->put(localX, localZ, data, compressedSize);
|
||||
}
|
||||
/* Writing block inventories */
|
||||
if (!chunk->inventories.empty()){
|
||||
auto& inventories = chunk->inventories;
|
||||
ByteBuilder builder;
|
||||
builder.putInt32(inventories.size());
|
||||
for (auto& entry : inventories) {
|
||||
builder.putInt32(entry.first);
|
||||
auto map = entry.second->serialize();
|
||||
auto bytes = json::to_binary(map.get(), true);
|
||||
builder.putInt32(bytes.size());
|
||||
builder.put(bytes.data(), bytes.size());
|
||||
}
|
||||
WorldRegion* region = getOrCreateRegion(storages, regionX, regionZ);
|
||||
region->setUnsaved(true);
|
||||
|
||||
auto datavec = builder.data();
|
||||
uint datasize = builder.size();
|
||||
auto data = std::make_unique<ubyte[]>(builder.size());
|
||||
for (uint i = 0; i < builder.size(); i++) {
|
||||
data[i] = datavec[i];
|
||||
}
|
||||
region->put(localX, localZ, data.release(), datasize);
|
||||
}
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getRegionsFolder() const {
|
||||
@@ -217,6 +242,10 @@ fs::path WorldFiles::getLightsFolder() const {
|
||||
return directory/fs::path("lights");
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getInventoriesFolder() const {
|
||||
return directory/fs::path("inventories");
|
||||
}
|
||||
|
||||
fs::path WorldFiles::getRegionFilename(int x, int z) const {
|
||||
return fs::path(std::to_string(x) + "_" + std::to_string(z) + ".bin");
|
||||
}
|
||||
@@ -436,20 +465,24 @@ void WorldFiles::writeRegions(regionsmap& regions, const fs::path& folder, int l
|
||||
void WorldFiles::write(const World* world, const Content* content) {
|
||||
fs::path regionsFolder = getRegionsFolder();
|
||||
fs::path lightsFolder = getLightsFolder();
|
||||
fs::path inventoriesFolder = getInventoriesFolder();
|
||||
|
||||
fs::create_directories(regionsFolder);
|
||||
fs::create_directories(inventoriesFolder);
|
||||
fs::create_directories(lightsFolder);
|
||||
|
||||
if (world) {
|
||||
writeWorldInfo(world);
|
||||
writePacks(world);
|
||||
}
|
||||
if (generatorTestMode)
|
||||
if (generatorTestMode) {
|
||||
return;
|
||||
}
|
||||
|
||||
writeIndices(content->getIndices());
|
||||
writeRegions(regions, regionsFolder, REGION_LAYER_VOXELS);
|
||||
writeRegions(lights, lightsFolder, REGION_LAYER_LIGHTS);
|
||||
writeRegions(storages, inventoriesFolder, REGION_LAYER_STORAGES);
|
||||
}
|
||||
|
||||
void WorldFiles::writePacks(const World* world) {
|
||||
@@ -488,21 +521,7 @@ void WorldFiles::writeIndices(const ContentIndices* indices) {
|
||||
}
|
||||
|
||||
void WorldFiles::writeWorldInfo(const World* world) {
|
||||
dynamic::Map root;
|
||||
|
||||
auto& versionobj = root.putMap("version");
|
||||
versionobj.put("major", ENGINE_VERSION_MAJOR);
|
||||
versionobj.put("minor", ENGINE_VERSION_MINOR);
|
||||
|
||||
root.put("name", world->getName());
|
||||
root.put("seed", world->getSeed());
|
||||
|
||||
auto& timeobj = root.putMap("time");
|
||||
timeobj.put("day-time", world->daytime);
|
||||
timeobj.put("day-time-speed", world->daytimeSpeed);
|
||||
timeobj.put("total-time", world->totalTime);
|
||||
|
||||
files::write_json(getWorldFile(), &root);
|
||||
files::write_json(getWorldFile(), world->serialize().get());
|
||||
}
|
||||
|
||||
bool WorldFiles::readWorldInfo(World* world) {
|
||||
@@ -513,25 +532,7 @@ bool WorldFiles::readWorldInfo(World* world) {
|
||||
}
|
||||
|
||||
auto root = files::read_json(file);
|
||||
|
||||
world->setName(root->getStr("name", world->getName()));
|
||||
world->setSeed(root->getInt("seed", world->getSeed()));
|
||||
|
||||
auto verobj = root->map("version");
|
||||
if (verobj) {
|
||||
int major=0, minor=-1;
|
||||
verobj->num("major", major);
|
||||
verobj->num("minor", minor);
|
||||
std::cout << "world version: " << major << "." << minor << std::endl;
|
||||
}
|
||||
|
||||
auto timeobj = root->map("time");
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", world->daytime);
|
||||
timeobj->num("day-time-speed", world->daytimeSpeed);
|
||||
timeobj->num("total-time", world->totalTime);
|
||||
}
|
||||
|
||||
world->deserialize(root.get());
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -550,8 +551,15 @@ bool WorldFiles::readPlayer(Player* player) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void WorldFiles::addPack(const std::string& id) {
|
||||
auto packs = files::read_list(getPacksFile());
|
||||
void WorldFiles::addPack(const World* world, const std::string& id) {
|
||||
fs::path file = getPacksFile();
|
||||
if (!fs::is_regular_file(file)) {
|
||||
if (!fs::is_directory(directory)) {
|
||||
fs::create_directories(directory);
|
||||
}
|
||||
writePacks(world);
|
||||
}
|
||||
auto packs = files::read_list(file);
|
||||
packs.push_back(id);
|
||||
|
||||
std::stringstream ss;
|
||||
@@ -559,5 +567,5 @@ void WorldFiles::addPack(const std::string& id) {
|
||||
for (const auto& pack : packs) {
|
||||
ss << pack << "\n";
|
||||
}
|
||||
files::write_string(getPacksFile(), ss.str());
|
||||
files::write_string(file, ss.str());
|
||||
}
|
||||
|
||||
@@ -16,8 +16,11 @@
|
||||
#include "../settings.h"
|
||||
|
||||
const uint REGION_HEADER_SIZE = 10;
|
||||
|
||||
const uint REGION_LAYER_VOXELS = 0;
|
||||
const uint REGION_LAYER_LIGHTS = 1;
|
||||
const uint REGION_LAYER_STORAGES = 2;
|
||||
|
||||
const uint REGION_SIZE_BIT = 5;
|
||||
const uint REGION_SIZE = (1 << (REGION_SIZE_BIT));
|
||||
const uint REGION_CHUNKS_COUNT = ((REGION_SIZE) * (REGION_SIZE));
|
||||
@@ -74,8 +77,7 @@ class WorldFiles {
|
||||
std::unordered_map<glm::ivec3, std::unique_ptr<regfile>> openRegFiles;
|
||||
|
||||
void writeWorldInfo(const World* world);
|
||||
fs::path getLightsFolder() const;
|
||||
fs::path getRegionFilename(int x, int y) const;
|
||||
fs::path getRegionFilename(int x, int y) const;
|
||||
fs::path getWorldFile() const;
|
||||
fs::path getIndicesFile() const;
|
||||
fs::path getPacksFile() const;
|
||||
@@ -110,12 +112,16 @@ class WorldFiles {
|
||||
int x, int z, int layer);
|
||||
|
||||
regfile* getRegFile(glm::ivec3 coord, const fs::path& folder);
|
||||
|
||||
fs::path getLightsFolder() const;
|
||||
fs::path getInventoriesFolder() const;
|
||||
public:
|
||||
static bool parseRegionFilename(const std::string& name, int& x, int& y);
|
||||
fs::path getRegionsFolder() const;
|
||||
fs::path getPlayerFile() const;
|
||||
|
||||
regionsmap regions;
|
||||
regionsmap storages;
|
||||
regionsmap lights;
|
||||
fs::path directory;
|
||||
std::unique_ptr<ubyte[]> compressionBuffer;
|
||||
@@ -147,7 +153,7 @@ public:
|
||||
void writePacks(const World* world);
|
||||
void writeIndices(const ContentIndices* indices);
|
||||
/* Append pack to packs.list without duplicate check */
|
||||
void addPack(const std::string& id);
|
||||
void addPack(const World* world, const std::string& id);
|
||||
|
||||
static const char* WORLD_FILE;
|
||||
};
|
||||
|
||||
+1
-4
@@ -99,10 +99,7 @@ bool files::write_json(fs::path filename, const dynamic::Map* obj, bool nice) {
|
||||
}
|
||||
|
||||
bool files::write_binary_json(fs::path filename, const dynamic::Map* obj, bool compression) {
|
||||
auto bytes = json::to_binary(obj);
|
||||
if (compression) {
|
||||
bytes = gzip::compress(bytes.data(), bytes.size());
|
||||
}
|
||||
auto bytes = json::to_binary(obj, compression);
|
||||
return files::write_bytes(filename, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user