Merge pull request #291 from MihailRis/update-regions-format

Update regions format
This commit is contained in:
MihailRis
2024-08-11 21:01:43 +03:00
committed by GitHub
14 changed files with 146 additions and 115 deletions
+10 -1
View File
@@ -7,6 +7,8 @@
#include "files/files.hpp" #include "files/files.hpp"
#include "items/ItemDef.hpp" #include "items/ItemDef.hpp"
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "world/World.hpp"
#include "files/WorldFiles.hpp"
#include "Content.hpp" #include "Content.hpp"
ContentLUT::ContentLUT( ContentLUT::ContentLUT(
@@ -24,8 +26,15 @@ static constexpr size_t get_entries_count(
} }
std::shared_ptr<ContentLUT> ContentLUT::create( std::shared_ptr<ContentLUT> ContentLUT::create(
const fs::path& filename, const Content* content const std::shared_ptr<WorldFiles>& worldFiles,
const fs::path& filename,
const Content* content
) { ) {
auto worldInfo = worldFiles->readWorldInfo();
if (!worldInfo.has_value()) {
return nullptr;
}
auto root = files::read_json(filename); auto root = files::read_json(filename);
auto blocklist = root->list("blocks"); auto blocklist = root->list("blocks");
auto itemlist = root->list("items"); auto itemlist = root->list("items");
+5 -1
View File
@@ -17,6 +17,8 @@ struct contententry {
std::string name; std::string name;
}; };
class WorldFiles;
template <typename T, class U> template <typename T, class U>
class ContentUnitLUT { class ContentUnitLUT {
std::vector<T> indices; std::vector<T> indices;
@@ -100,7 +102,9 @@ public:
ContentLUT(const ContentIndices* indices, size_t blocks, size_t items); ContentLUT(const ContentIndices* indices, size_t blocks, size_t items);
static std::shared_ptr<ContentLUT> create( static std::shared_ptr<ContentLUT> create(
const fs::path& filename, const Content* content const std::shared_ptr<WorldFiles>& worldFiles,
const fs::path& filename,
const Content* content
); );
inline bool hasContentReorder() const { inline bool hasContentReorder() const {
+4 -4
View File
@@ -32,11 +32,11 @@ public:
}; };
WorldConverter::WorldConverter( WorldConverter::WorldConverter(
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
std::shared_ptr<ContentLUT> lut std::shared_ptr<ContentLUT> lut
) )
: wfile(std::make_unique<WorldFiles>(folder)), : wfile(worldFiles),
lut(std::move(lut)), lut(std::move(lut)),
content(content) { content(content) {
fs::path regionsFolder = fs::path regionsFolder =
@@ -56,13 +56,13 @@ WorldConverter::~WorldConverter() {
} }
std::shared_ptr<Task> WorldConverter::startTask( std::shared_ptr<Task> WorldConverter::startTask(
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
const std::shared_ptr<ContentLUT>& lut, const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone, const runnable& onDone,
bool multithreading bool multithreading
) { ) {
auto converter = std::make_shared<WorldConverter>(folder, content, lut); auto converter = std::make_shared<WorldConverter>(worldFiles, content, lut);
if (!multithreading) { if (!multithreading) {
converter->setOnComplete([=]() { converter->setOnComplete([=]() {
converter->write(); converter->write();
+3 -3
View File
@@ -22,7 +22,7 @@ struct convert_task {
}; };
class WorldConverter : public Task { class WorldConverter : public Task {
std::unique_ptr<WorldFiles> wfile; std::shared_ptr<WorldFiles> wfile;
std::shared_ptr<ContentLUT> const lut; std::shared_ptr<ContentLUT> const lut;
const Content* const content; const Content* const content;
std::queue<convert_task> tasks; std::queue<convert_task> tasks;
@@ -33,7 +33,7 @@ class WorldConverter : public Task {
void convertRegion(const fs::path& file) const; void convertRegion(const fs::path& file) const;
public: public:
WorldConverter( WorldConverter(
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
std::shared_ptr<ContentLUT> lut std::shared_ptr<ContentLUT> lut
); );
@@ -52,7 +52,7 @@ public:
uint getWorkDone() const override; uint getWorkDone() const override;
static std::shared_ptr<Task> startTask( static std::shared_ptr<Task> startTask(
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
const std::shared_ptr<ContentLUT>& lut, const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone, const runnable& onDone,
+8 -7
View File
@@ -76,7 +76,7 @@ fs::path WorldFiles::getPacksFile() const {
void WorldFiles::write(const World* world, const Content* content) { void WorldFiles::write(const World* world, const Content* content) {
if (world) { if (world) {
writeWorldInfo(world); writeWorldInfo(world->getInfo());
if (!fs::exists(getPacksFile())) { if (!fs::exists(getPacksFile())) {
writePacks(world->getPacks()); writePacks(world->getPacks());
} }
@@ -116,19 +116,20 @@ void WorldFiles::writeIndices(const ContentIndices* indices) {
files::write_json(getIndicesFile(), &root); files::write_json(getIndicesFile(), &root);
} }
void WorldFiles::writeWorldInfo(const World* world) { void WorldFiles::writeWorldInfo(const WorldInfo& info) {
files::write_json(getWorldFile(), world->serialize().get()); files::write_json(getWorldFile(), info.serialize().get());
} }
bool WorldFiles::readWorldInfo(World* world) { std::optional<WorldInfo> WorldFiles::readWorldInfo() {
fs::path file = getWorldFile(); fs::path file = getWorldFile();
if (!fs::is_regular_file(file)) { if (!fs::is_regular_file(file)) {
logger.warning() << "world.json does not exists"; logger.warning() << "world.json does not exists";
return false; return std::nullopt;
} }
auto root = files::read_json(file); auto root = files::read_json(file);
world->deserialize(root.get()); WorldInfo info {};
return true; info.deserialize(root.get());
return info;
} }
static void read_resources_data( static void read_resources_data(
+5 -3
View File
@@ -2,6 +2,7 @@
#include <filesystem> #include <filesystem>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <optional>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -20,6 +21,7 @@ class Player;
class Content; class Content;
class ContentIndices; class ContentIndices;
class World; class World;
struct WorldInfo;
struct DebugSettings; struct DebugSettings;
namespace fs = std::filesystem; namespace fs = std::filesystem;
@@ -32,10 +34,9 @@ class WorldFiles {
bool doWriteLights = true; bool doWriteLights = true;
fs::path getWorldFile() const; fs::path getWorldFile() const;
fs::path getIndicesFile() const;
fs::path getPacksFile() const; fs::path getPacksFile() const;
void writeWorldInfo(const World* world); void writeWorldInfo(const WorldInfo& info);
void writeIndices(const ContentIndices* indices); void writeIndices(const ContentIndices* indices);
public: public:
WorldFiles(const fs::path& directory); WorldFiles(const fs::path& directory);
@@ -43,10 +44,11 @@ public:
~WorldFiles(); ~WorldFiles();
fs::path getPlayerFile() const; fs::path getPlayerFile() const;
fs::path getIndicesFile() const;
fs::path getResourcesFile() const; fs::path getResourcesFile() const;
void createDirectories(); void createDirectories();
bool readWorldInfo(World* world); std::optional<WorldInfo> readWorldInfo();
bool readResourcesData(const Content* content); bool readResourcesData(const Content* content);
/// @brief Write all unsaved data to world files /// @brief Write all unsaved data to world files
+2 -2
View File
@@ -20,12 +20,12 @@ regfile::regfile(fs::path filename) : file(std::move(filename)) {
file.read(header, REGION_HEADER_SIZE); file.read(header, REGION_HEADER_SIZE);
// avoid of use strcmp_s // avoid of use strcmp_s
if (std::string(header, strlen(REGION_FORMAT_MAGIC)) != if (std::string(header, std::strlen(REGION_FORMAT_MAGIC)) !=
REGION_FORMAT_MAGIC) { REGION_FORMAT_MAGIC) {
throw std::runtime_error("invalid region file magic number"); throw std::runtime_error("invalid region file magic number");
} }
version = header[8]; version = header[8];
if (uint(version) > REGION_FORMAT_VERSION) { if (static_cast<uint>(version) > REGION_FORMAT_VERSION) {
throw illegal_region_format( throw illegal_region_format(
"region format " + std::to_string(version) + " is not supported" "region format " + std::to_string(version) + " is not supported"
); );
+7 -6
View File
@@ -160,9 +160,10 @@ std::shared_ptr<UINode> create_debug_panel(
sub->add(box, glm::vec2(20, 0)); sub->add(box, glm::vec2(20, 0));
panel->add(sub); panel->add(sub);
} }
panel->add(create_label([=](){ auto& worldInfo = level->getWorld()->getInfo();
panel->add(create_label([&](){
int hour, minute, second; int hour, minute, second;
timeutil::from_value(level->getWorld()->daytime, hour, minute, second); timeutil::from_value(worldInfo.daytime, hour, minute, second);
std::wstring timeString = std::wstring timeString =
util::lfill(std::to_wstring(hour), 2, L'0') + L":" + util::lfill(std::to_wstring(hour), 2, L'0') + L":" +
@@ -171,14 +172,14 @@ std::shared_ptr<UINode> create_debug_panel(
})); }));
{ {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8); auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 1.0f, 0.005f, 8);
bar->setSupplier([=]() {return level->getWorld()->daytime;}); bar->setSupplier([&]() {return worldInfo.daytime;});
bar->setConsumer([=](double val) {level->getWorld()->daytime = val;}); bar->setConsumer([&](double val) {worldInfo.daytime = val;});
panel->add(bar); panel->add(bar);
} }
{ {
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8); auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->setSupplier([=]() {return level->getWorld()->fog;}); bar->setSupplier([&]() {return worldInfo.fog;});
bar->setConsumer([=](double val) {level->getWorld()->fog = val;}); bar->setConsumer([&](double val) {worldInfo.fog = val;});
panel->add(bar); panel->add(bar);
} }
{ {
+7 -5
View File
@@ -161,7 +161,7 @@ void WorldRenderer::setupWorldShader(
shader->uniform1f("u_gamma", settings.graphics.gamma.get()); shader->uniform1f("u_gamma", settings.graphics.gamma.get());
shader->uniform1f("u_fogFactor", fogFactor); shader->uniform1f("u_fogFactor", fogFactor);
shader->uniform1f("u_fogCurve", settings.graphics.fogCurve.get()); shader->uniform1f("u_fogCurve", settings.graphics.fogCurve.get());
shader->uniform1f("u_dayTime", level->getWorld()->daytime); shader->uniform1f("u_dayTime", level->getWorld()->getInfo().daytime);
shader->uniform3f("u_cameraPos", camera->position); shader->uniform3f("u_cameraPos", camera->position);
shader->uniform1i("u_cubemap", 1); shader->uniform1i("u_cubemap", 1);
@@ -338,8 +338,10 @@ void WorldRenderer::draw(
const Viewport& vp = pctx.getViewport(); const Viewport& vp = pctx.getViewport();
camera->aspect = vp.getWidth() / static_cast<float>(vp.getHeight()); camera->aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
const EngineSettings& settings = engine->getSettings(); const auto& settings = engine->getSettings();
skybox->refresh(pctx, world->daytime, 1.0f + world->fog * 2.0f, 4); const auto& worldInfo = world->getInfo();
skybox->refresh(pctx, worldInfo.daytime, 1.0f + worldInfo.fog * 2.0f, 4);
auto assets = engine->getAssets(); auto assets = engine->getAssets();
auto linesShader = assets->get<Shader>("lines"); auto linesShader = assets->get<Shader>("lines");
@@ -352,7 +354,7 @@ void WorldRenderer::draw(
Window::clearDepth(); Window::clearDepth();
// Drawing background sky plane // Drawing background sky plane
skybox->draw(pctx, camera, assets, world->daytime, world->fog); skybox->draw(pctx, camera, assets, worldInfo.daytime, worldInfo.fog);
// Actually world render with depth buffer on // Actually world render with depth buffer on
{ {
@@ -375,7 +377,7 @@ void WorldRenderer::draw(
auto screenShader = assets->get<Shader>("screen"); auto screenShader = assets->get<Shader>("screen");
screenShader->use(); screenShader->use();
screenShader->uniform1f("u_timer", timer); screenShader->uniform1f("u_timer", timer);
screenShader->uniform1f("u_dayTime", level->getWorld()->daytime); screenShader->uniform1f("u_dayTime", worldInfo.daytime);
postProcessing->render(pctx, screenShader); postProcessing->render(pctx, screenShader);
} }
+13 -13
View File
@@ -44,13 +44,13 @@ void EngineController::deleteWorld(const std::string& name) {
std::shared_ptr<Task> create_converter( std::shared_ptr<Task> create_converter(
Engine* engine, Engine* engine,
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content, const Content* content,
const std::shared_ptr<ContentLUT>& lut, const std::shared_ptr<ContentLUT>& lut,
const runnable& postRunnable const runnable& postRunnable
) { ) {
return WorldConverter::startTask( return WorldConverter::startTask(
folder, worldFiles,
content, content,
lut, lut,
[=]() { [=]() {
@@ -67,7 +67,7 @@ void show_convert_request(
Engine* engine, Engine* engine,
const Content* content, const Content* content,
const std::shared_ptr<ContentLUT>& lut, const std::shared_ptr<ContentLUT>& lut,
const fs::path& folder, const std::shared_ptr<WorldFiles>& worldFiles,
const runnable& postRunnable const runnable& postRunnable
) { ) {
guiutil::confirm( guiutil::confirm(
@@ -75,7 +75,7 @@ void show_convert_request(
langs::get(L"world.convert-request"), langs::get(L"world.convert-request"),
[=]() { [=]() {
auto converter = auto converter =
create_converter(engine, folder, content, lut, postRunnable); create_converter(engine, worldFiles, content, lut, postRunnable);
menus::show_process_panel( menus::show_process_panel(
engine, converter, L"Converting world..." engine, converter, L"Converting world..."
); );
@@ -106,13 +106,13 @@ static bool loadWorldContent(Engine* engine, const fs::path& folder) {
}); });
} }
static void loadWorld(Engine* engine, const fs::path& folder) { static void loadWorld(Engine* engine, const std::shared_ptr<WorldFiles>& worldFiles) {
try { try {
auto content = engine->getContent(); auto content = engine->getContent();
auto& packs = engine->getContentPacks(); auto& packs = engine->getContentPacks();
auto& settings = engine->getSettings(); auto& settings = engine->getSettings();
auto level = World::load(folder, settings, content, packs); auto level = World::load(worldFiles, settings, content, packs);
engine->setScreen( engine->setScreen(
std::make_shared<LevelScreen>(engine, std::move(level)) std::make_shared<LevelScreen>(engine, std::move(level))
); );
@@ -133,9 +133,9 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
} }
auto* content = engine->getContent(); auto* content = engine->getContent();
auto worldFiles = std::make_shared<WorldFiles>(
std::shared_ptr<ContentLUT> lut(World::checkIndices(folder, content)); folder, engine->getSettings().debug);
if (lut) { if (auto lut = World::checkIndices(worldFiles, content)) {
if (lut->hasMissingContent()) { if (lut->hasMissingContent()) {
engine->setScreen(std::make_shared<MenuScreen>(engine)); engine->setScreen(std::make_shared<MenuScreen>(engine));
show_content_missing(engine, lut); show_content_missing(engine, lut);
@@ -145,7 +145,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
engine, engine,
create_converter( create_converter(
engine, engine,
folder, worldFiles,
content, content,
lut, lut,
[=]() { openWorld(name, false); } [=]() { openWorld(name, false); }
@@ -153,14 +153,14 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
L"Converting world..." L"Converting world..."
); );
} else { } else {
show_convert_request(engine, content, lut, folder, [=]() { show_convert_request(engine, content, lut, std::move(worldFiles), [=]() {
openWorld(name, false); openWorld(name, false);
}); });
} }
} }
} else { return;
loadWorld(engine, folder);
} }
loadWorld(engine, std::move(worldFiles));
} }
inline uint64_t str2seed(const std::string& seedstr) { inline uint64_t str2seed(const std::string& seedstr) {
+7 -7
View File
@@ -42,27 +42,27 @@ static int l_world_get_list(lua::State* L) {
} }
static int l_world_get_total_time(lua::State* L) { static int l_world_get_total_time(lua::State* L) {
return lua::pushnumber(L, level->getWorld()->totalTime); return lua::pushnumber(L, level->getWorld()->getInfo().totalTime);
} }
static int l_world_get_day_time(lua::State* L) { static int l_world_get_day_time(lua::State* L) {
return lua::pushnumber(L, level->getWorld()->daytime); return lua::pushnumber(L, level->getWorld()->getInfo().daytime);
} }
static int l_world_set_day_time(lua::State* L) { static int l_world_set_day_time(lua::State* L) {
auto value = lua::tonumber(L, 1); auto value = lua::tonumber(L, 1);
level->getWorld()->daytime = fmod(value, 1.0); level->getWorld()->getInfo().daytime = std::fmod(value, 1.0);
return 0; return 0;
} }
static int l_world_set_day_time_speed(lua::State* L) { static int l_world_set_day_time_speed(lua::State* L) {
auto value = lua::tonumber(L, 1); auto value = lua::tonumber(L, 1);
level->getWorld()->daytimeSpeed = std::abs(value); level->getWorld()->getInfo().daytimeSpeed = std::abs(value);
return 0; return 0;
} }
static int l_world_get_day_time_speed(lua::State* L) { static int l_world_get_day_time_speed(lua::State* L) {
return lua::pushnumber(L, level->getWorld()->daytimeSpeed); return lua::pushnumber(L, level->getWorld()->getInfo().daytimeSpeed);
} }
static int l_world_get_seed(lua::State* L) { static int l_world_get_seed(lua::State* L) {
@@ -76,12 +76,12 @@ static int l_world_exists(lua::State* L) {
} }
static int l_world_is_day(lua::State* L) { static int l_world_is_day(lua::State* L) {
auto daytime = level->getWorld()->daytime; auto daytime = level->getWorld()->getInfo().daytime;
return lua::pushboolean(L, daytime >= 0.2 && daytime <= 0.8); return lua::pushboolean(L, daytime >= 0.2 && daytime <= 0.8);
} }
static int l_world_is_night(lua::State* L) { static int l_world_is_night(lua::State* L) {
auto daytime = level->getWorld()->daytime; auto daytime = level->getWorld()->getInfo().daytime;
return lua::pushboolean(L, daytime < 0.2 || daytime > 0.8); return lua::pushboolean(L, daytime < 0.2 || daytime > 0.8);
} }
+3 -2
View File
@@ -29,6 +29,7 @@ Level::Level(
events(std::make_unique<LevelEvents>()), events(std::make_unique<LevelEvents>()),
entities(std::make_unique<Entities>(this)), entities(std::make_unique<Entities>(this)),
settings(settings) { settings(settings) {
auto& worldInfo = world->getInfo();
auto& cameraIndices = content->getIndices(ResourceType::CAMERA); auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) { for (size_t i = 0; i < cameraIndices.size(); i++) {
auto camera = std::make_shared<Camera>(); auto camera = std::make_shared<Camera>();
@@ -46,8 +47,8 @@ Level::Level(
cameras.push_back(std::move(camera)); cameras.push_back(std::move(camera));
} }
if (world->nextEntityId) { if (worldInfo.nextEntityId) {
entities->setNextID(world->nextEntityId); entities->setNextID(worldInfo.nextEntityId);
} }
auto inv = std::make_shared<Inventory>( auto inv = std::make_shared<Inventory>(
world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
+39 -41
View File
@@ -26,29 +26,22 @@ world_load_error::world_load_error(const std::string& message)
} }
World::World( World::World(
std::string name, WorldInfo info,
std::string generator, const std::shared_ptr<WorldFiles>& worldFiles,
const fs::path& directory,
uint64_t seed,
EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
) ) : info(std::move(info)),
: name(std::move(name)), content(content),
generator(std::move(generator)), packs(packs),
seed(seed), wfile(std::move(worldFiles)) {}
content(content),
packs(packs),
wfile(std::make_unique<WorldFiles>(directory, settings.debug)) {
}
World::~World() { World::~World() {
} }
void World::updateTimers(float delta) { void World::updateTimers(float delta) {
daytime += delta * daytimeSpeed * DAYIME_SPECIFIC_SPEED; info.daytime += delta * info.daytimeSpeed * DAYIME_SPECIFIC_SPEED;
daytime = fmod(daytime, 1.0f); info.daytime = std::fmod(info.daytime, 1.0f);
totalTime += delta; info.totalTime += delta;
} }
void World::writeResources(const Content* content) { void World::writeResources(const Content* content) {
@@ -71,7 +64,7 @@ void World::writeResources(const Content* content) {
void World::write(Level* level) { void World::write(Level* level) {
const Content* content = level->content; const Content* content = level->content;
level->chunks->saveAll(); level->chunks->saveAll();
nextEntityId = level->entities->peekNextID(); info.nextEntityId = level->entities->peekNextID();
wfile->write(this, content); wfile->write(this, content);
auto playerFile = dynamic::Map(); auto playerFile = dynamic::Map();
@@ -95,32 +88,39 @@ std::unique_ptr<Level> World::create(
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
) { ) {
WorldInfo info {};
info.name = name;
info.generator = generator;
info.seed = seed;
auto world = std::make_unique<World>( auto world = std::make_unique<World>(
name, generator, directory, seed, settings, content, packs info,
std::make_unique<WorldFiles>(directory, settings.debug),
content,
packs
); );
return std::make_unique<Level>(std::move(world), content, settings); return std::make_unique<Level>(std::move(world), content, settings);
} }
std::unique_ptr<Level> World::load( std::unique_ptr<Level> World::load(
const fs::path& directory, const std::shared_ptr<WorldFiles>& worldFilesPtr,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
) { ) {
auto worldFiles = worldFilesPtr.get();
auto info = worldFiles->readWorldInfo();
if (!info.has_value()) {
throw world_load_error("could not to find world.json");
}
logger.info() << "world version: " << info->major << "." << info->minor;
auto world = std::make_unique<World>( auto world = std::make_unique<World>(
".", info.value(),
WorldGenerators::getDefaultGeneratorID(), std::move(worldFilesPtr),
directory,
0,
settings,
content, content,
packs packs
); );
auto& wfile = world->wfile; auto& wfile = world->wfile;
if (!wfile->readWorldInfo(world.get())) {
throw world_load_error("could not to find world.json");
}
wfile->readResourcesData(content); wfile->readResourcesData(content);
auto level = std::make_unique<Level>(std::move(world), content, settings); auto level = std::make_unique<Level>(std::move(world), content, settings);
@@ -155,21 +155,21 @@ std::unique_ptr<Level> World::load(
} }
std::shared_ptr<ContentLUT> World::checkIndices( std::shared_ptr<ContentLUT> World::checkIndices(
const fs::path& directory, const Content* content const std::shared_ptr<WorldFiles>& worldFiles, const Content* content
) { ) {
fs::path indicesFile = directory / fs::path("indices.json"); fs::path indicesFile = worldFiles->getIndicesFile();
if (fs::is_regular_file(indicesFile)) { if (fs::is_regular_file(indicesFile)) {
return ContentLUT::create(indicesFile, content); return ContentLUT::create(worldFiles, indicesFile, content);
} }
return nullptr; return nullptr;
} }
void World::setName(const std::string& name) { void World::setName(const std::string& name) {
this->name = name; this->info.name = name;
} }
void World::setGenerator(const std::string& generator) { void World::setGenerator(const std::string& generator) {
this->generator = generator; this->info.generator = generator;
} }
bool World::hasPack(const std::string& id) const { bool World::hasPack(const std::string& id) const {
@@ -180,26 +180,26 @@ bool World::hasPack(const std::string& id) const {
} }
void World::setSeed(uint64_t seed) { void World::setSeed(uint64_t seed) {
this->seed = seed; this->info.seed = seed;
} }
std::string World::getName() const { std::string World::getName() const {
return name; return info.name;
} }
uint64_t World::getSeed() const { uint64_t World::getSeed() const {
return seed; return info.seed;
} }
std::string World::getGenerator() const { std::string World::getGenerator() const {
return generator; return info.generator;
} }
const std::vector<ContentPack>& World::getPacks() const { const std::vector<ContentPack>& World::getPacks() const {
return packs; return packs;
} }
void World::deserialize(dynamic::Map* root) { void WorldInfo::deserialize(dynamic::Map* root) {
name = root->get("name", name); name = root->get("name", name);
generator = root->get("generator", generator); generator = root->get("generator", generator);
seed = root->get("seed", seed); seed = root->get("seed", seed);
@@ -208,10 +208,8 @@ void World::deserialize(dynamic::Map* root) {
generator = WorldGenerators::getDefaultGeneratorID(); generator = WorldGenerators::getDefaultGeneratorID();
} }
if (auto verobj = root->map("version")) { if (auto verobj = root->map("version")) {
int major = 0, minor = -1;
verobj->num("major", major); verobj->num("major", major);
verobj->num("minor", minor); verobj->num("minor", minor);
logger.info() << "world version: " << major << "." << minor;
} }
if (auto timeobj = root->map("time")) { if (auto timeobj = root->map("time")) {
timeobj->num("day-time", daytime); timeobj->num("day-time", daytime);
@@ -225,7 +223,7 @@ void World::deserialize(dynamic::Map* root) {
nextEntityId = root->get("next-entity-id", 1); nextEntityId = root->get("next-entity-id", 1);
} }
std::unique_ptr<dynamic::Map> World::serialize() const { std::unique_ptr<dynamic::Map> WorldInfo::serialize() const {
auto root = std::make_unique<dynamic::Map>(); auto root = std::make_unique<dynamic::Map>();
auto& versionobj = root->putMap("version"); auto& versionobj = root->putMap("version");
+33 -20
View File
@@ -24,20 +24,12 @@ public:
world_load_error(const std::string& message); world_load_error(const std::string& message);
}; };
/// @brief holds all world data except the level (chunks and objects) struct WorldInfo : public Serializable {
class World : Serializable {
std::string name; std::string name;
std::string generator; std::string generator;
uint64_t seed; uint64_t seed;
const Content* const content;
std::vector<ContentPack> packs;
int64_t nextInventoryId = 0; int64_t nextInventoryId = 0;
void writeResources(const Content* content);
public:
std::unique_ptr<WorldFiles> wfile;
/// @brief Day/night loop timer in range 0..1 where /// @brief Day/night loop timer in range 0..1 where
/// 0.0 - is midnight and /// 0.0 - is midnight and
/// 0.5 - is noon /// 0.5 - is noon
@@ -54,12 +46,28 @@ public:
entityid_t nextEntityId = 0; entityid_t nextEntityId = 0;
int major = 0, minor = -1;
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map* src) override;
};
/// @brief holds all world data except the level (chunks and objects)
class World {
WorldInfo info {};
const Content* const content;
std::vector<ContentPack> packs;
int64_t nextInventoryId = 0;
void writeResources(const Content* content);
public:
std::shared_ptr<WorldFiles> wfile;
World( World(
std::string name, WorldInfo info,
std::string generator, const std::shared_ptr<WorldFiles>& worldFiles,
const fs::path& directory,
uint64_t seed,
EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
); );
@@ -78,7 +86,7 @@ public:
/// @param content current Content instance /// @param content current Content instance
/// @return ContentLUT if world convert required else nullptr /// @return ContentLUT if world convert required else nullptr
static std::shared_ptr<ContentLUT> checkIndices( static std::shared_ptr<ContentLUT> checkIndices(
const fs::path& directory, const Content* content const std::shared_ptr<WorldFiles>& worldFiles, const Content* content
); );
/// @brief Create new world /// @brief Create new world
@@ -102,7 +110,7 @@ public:
); );
/// @brief Load an existing world /// @brief Load an existing world
/// @param directory root world directory /// @param worldFiles world files manager
/// @param settings current engine settings /// @param settings current engine settings
/// @param content current engine Content instance /// @param content current engine Content instance
/// with all world content-packs applied /// with all world content-packs applied
@@ -110,7 +118,7 @@ public:
/// @return Level instance containing World instance /// @return Level instance containing World instance
/// @throws world_load_error on world.json load error /// @throws world_load_error on world.json load error
static std::unique_ptr<Level> load( static std::unique_ptr<Level> load(
const fs::path& directory, const std::shared_ptr<WorldFiles>& worldFiles,
EngineSettings& settings, EngineSettings& settings,
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
@@ -134,6 +142,14 @@ public:
/// @brief Get world generator id /// @brief Get world generator id
std::string getGenerator() const; std::string getGenerator() const;
WorldInfo& getInfo() {
return info;
}
const WorldInfo& getInfo() const {
return info;
}
/// @brief Get vector of all content-packs installed in world /// @brief Get vector of all content-packs installed in world
const std::vector<ContentPack>& getPacks() const; const std::vector<ContentPack>& getPacks() const;
@@ -147,7 +163,4 @@ public:
const Content* getContent() const { const Content* getContent() const {
return content; return content;
} }
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map* src) override;
}; };