refactor & add structures.json

This commit is contained in:
MihailRis
2024-09-24 04:05:13 +03:00
parent 519ebc05f1
commit 323c2f2935
20 changed files with 153 additions and 43 deletions
+74
View File
@@ -0,0 +1,74 @@
#include "../ContentLoader.hpp"
#include "../ContentPack.hpp"
#include "files/files.hpp"
#include "logic/scripting/scripting.hpp"
#include "world/generator/GeneratorDef.hpp"
#include "world/generator/VoxelFragment.hpp"
#include "debug/Logger.hpp"
static debug::Logger logger("generator-loader");
static VoxelStructureMeta load_structure_meta(
const std::string& name, const dv::value& config
) {
VoxelStructureMeta meta;
meta.name = name;
return meta;
}
static std::vector<std::unique_ptr<GeneratingVoxelStructure>> load_structures(
const fs::path& structuresFile
) {
auto structuresDir = structuresFile.parent_path();
auto map = files::read_json(structuresFile);
std::vector<std::unique_ptr<GeneratingVoxelStructure>> structures;
for (auto& [name, config] : map.asObject()) {
auto structFile = structuresDir / fs::u8path(name + ".vox");
logger.debug() << "loading voxel fragment " << structFile.u8string();
if (!fs::exists(structFile)) {
throw std::runtime_error("structure file does not exist (" +
structFile.u8string());
}
auto fragment = std::make_unique<VoxelFragment>();
fragment->deserialize(files::read_binary_json(structFile));
structures.push_back(std::make_unique<GeneratingVoxelStructure>(
load_structure_meta(name, config),
std::move(fragment)
));
}
return structures;
}
static void load_structures(GeneratorDef& def, const fs::path& structuresFile) {
def.structures = load_structures(structuresFile);
// build indices map
for (size_t i = 0; i < def.structures.size(); i++) {
auto& structure = def.structures[i];
def.structuresIndices[structure->meta.name] = i;
}
}
static inline const auto STRUCTURES_FILE = fs::u8path("structures.json");
static inline const auto GENERATORS_DIR = fs::u8path("generators");
void ContentLoader::loadGenerator(
GeneratorDef& def, const std::string& full, const std::string& name
) {
auto packDir = pack->folder;
auto generatorsDir = packDir / GENERATORS_DIR;
auto folder = generatorsDir / fs::u8path(name);
auto generatorFile = generatorsDir / fs::u8path(name + ".lua");
if (!fs::exists(generatorFile)) {
return;
}
auto structuresFile = folder / STRUCTURES_FILE;
if (fs::exists(structuresFile)) {
load_structures(def, structuresFile);
}
def.script = scripting::load_generator(generatorFile);
}