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
commit 690f7ff724
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 146 additions and 115 deletions

View File

@ -7,6 +7,8 @@
#include "files/files.hpp"
#include "items/ItemDef.hpp"
#include "voxels/Block.hpp"
#include "world/World.hpp"
#include "files/WorldFiles.hpp"
#include "Content.hpp"
ContentLUT::ContentLUT(
@ -24,8 +26,15 @@ static constexpr size_t get_entries_count(
}
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 blocklist = root->list("blocks");
auto itemlist = root->list("items");

View File

@ -17,6 +17,8 @@ struct contententry {
std::string name;
};
class WorldFiles;
template <typename T, class U>
class ContentUnitLUT {
std::vector<T> indices;
@ -100,7 +102,9 @@ public:
ContentLUT(const ContentIndices* indices, size_t blocks, size_t items);
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 {

View File

@ -32,11 +32,11 @@ public:
};
WorldConverter::WorldConverter(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
std::shared_ptr<ContentLUT> lut
)
: wfile(std::make_unique<WorldFiles>(folder)),
: wfile(worldFiles),
lut(std::move(lut)),
content(content) {
fs::path regionsFolder =
@ -56,13 +56,13 @@ WorldConverter::~WorldConverter() {
}
std::shared_ptr<Task> WorldConverter::startTask(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone,
bool multithreading
) {
auto converter = std::make_shared<WorldConverter>(folder, content, lut);
auto converter = std::make_shared<WorldConverter>(worldFiles, content, lut);
if (!multithreading) {
converter->setOnComplete([=]() {
converter->write();

View File

@ -22,7 +22,7 @@ struct convert_task {
};
class WorldConverter : public Task {
std::unique_ptr<WorldFiles> wfile;
std::shared_ptr<WorldFiles> wfile;
std::shared_ptr<ContentLUT> const lut;
const Content* const content;
std::queue<convert_task> tasks;
@ -33,7 +33,7 @@ class WorldConverter : public Task {
void convertRegion(const fs::path& file) const;
public:
WorldConverter(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
std::shared_ptr<ContentLUT> lut
);
@ -52,7 +52,7 @@ public:
uint getWorkDone() const override;
static std::shared_ptr<Task> startTask(
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& onDone,

View File

@ -76,7 +76,7 @@ fs::path WorldFiles::getPacksFile() const {
void WorldFiles::write(const World* world, const Content* content) {
if (world) {
writeWorldInfo(world);
writeWorldInfo(world->getInfo());
if (!fs::exists(getPacksFile())) {
writePacks(world->getPacks());
}
@ -116,19 +116,20 @@ void WorldFiles::writeIndices(const ContentIndices* indices) {
files::write_json(getIndicesFile(), &root);
}
void WorldFiles::writeWorldInfo(const World* world) {
files::write_json(getWorldFile(), world->serialize().get());
void WorldFiles::writeWorldInfo(const WorldInfo& info) {
files::write_json(getWorldFile(), info.serialize().get());
}
bool WorldFiles::readWorldInfo(World* world) {
std::optional<WorldInfo> WorldFiles::readWorldInfo() {
fs::path file = getWorldFile();
if (!fs::is_regular_file(file)) {
logger.warning() << "world.json does not exists";
return false;
return std::nullopt;
}
auto root = files::read_json(file);
world->deserialize(root.get());
return true;
WorldInfo info {};
info.deserialize(root.get());
return info;
}
static void read_resources_data(

View File

@ -2,6 +2,7 @@
#include <filesystem>
#include <glm/glm.hpp>
#include <optional>
#include <memory>
#include <string>
#include <vector>
@ -20,6 +21,7 @@ class Player;
class Content;
class ContentIndices;
class World;
struct WorldInfo;
struct DebugSettings;
namespace fs = std::filesystem;
@ -32,10 +34,9 @@ class WorldFiles {
bool doWriteLights = true;
fs::path getWorldFile() const;
fs::path getIndicesFile() const;
fs::path getPacksFile() const;
void writeWorldInfo(const World* world);
void writeWorldInfo(const WorldInfo& info);
void writeIndices(const ContentIndices* indices);
public:
WorldFiles(const fs::path& directory);
@ -43,10 +44,11 @@ public:
~WorldFiles();
fs::path getPlayerFile() const;
fs::path getIndicesFile() const;
fs::path getResourcesFile() const;
void createDirectories();
bool readWorldInfo(World* world);
std::optional<WorldInfo> readWorldInfo();
bool readResourcesData(const Content* content);
/// @brief Write all unsaved data to world files

View File

@ -20,12 +20,12 @@ regfile::regfile(fs::path filename) : file(std::move(filename)) {
file.read(header, REGION_HEADER_SIZE);
// 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) {
throw std::runtime_error("invalid region file magic number");
}
version = header[8];
if (uint(version) > REGION_FORMAT_VERSION) {
if (static_cast<uint>(version) > REGION_FORMAT_VERSION) {
throw illegal_region_format(
"region format " + std::to_string(version) + " is not supported"
);

View File

@ -160,9 +160,10 @@ std::shared_ptr<UINode> create_debug_panel(
sub->add(box, glm::vec2(20, 0));
panel->add(sub);
}
panel->add(create_label([=](){
auto& worldInfo = level->getWorld()->getInfo();
panel->add(create_label([&](){
int hour, minute, second;
timeutil::from_value(level->getWorld()->daytime, hour, minute, second);
timeutil::from_value(worldInfo.daytime, hour, minute, second);
std::wstring timeString =
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);
bar->setSupplier([=]() {return level->getWorld()->daytime;});
bar->setConsumer([=](double val) {level->getWorld()->daytime = val;});
bar->setSupplier([&]() {return worldInfo.daytime;});
bar->setConsumer([&](double val) {worldInfo.daytime = val;});
panel->add(bar);
}
{
auto bar = std::make_shared<TrackBar>(0.0f, 1.0f, 0.0f, 0.005f, 8);
bar->setSupplier([=]() {return level->getWorld()->fog;});
bar->setConsumer([=](double val) {level->getWorld()->fog = val;});
bar->setSupplier([&]() {return worldInfo.fog;});
bar->setConsumer([&](double val) {worldInfo.fog = val;});
panel->add(bar);
}
{

View File

@ -161,7 +161,7 @@ void WorldRenderer::setupWorldShader(
shader->uniform1f("u_gamma", settings.graphics.gamma.get());
shader->uniform1f("u_fogFactor", fogFactor);
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->uniform1i("u_cubemap", 1);
@ -338,8 +338,10 @@ void WorldRenderer::draw(
const Viewport& vp = pctx.getViewport();
camera->aspect = vp.getWidth() / static_cast<float>(vp.getHeight());
const EngineSettings& settings = engine->getSettings();
skybox->refresh(pctx, world->daytime, 1.0f + world->fog * 2.0f, 4);
const auto& settings = engine->getSettings();
const auto& worldInfo = world->getInfo();
skybox->refresh(pctx, worldInfo.daytime, 1.0f + worldInfo.fog * 2.0f, 4);
auto assets = engine->getAssets();
auto linesShader = assets->get<Shader>("lines");
@ -352,7 +354,7 @@ void WorldRenderer::draw(
Window::clearDepth();
// 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
{
@ -375,7 +377,7 @@ void WorldRenderer::draw(
auto screenShader = assets->get<Shader>("screen");
screenShader->use();
screenShader->uniform1f("u_timer", timer);
screenShader->uniform1f("u_dayTime", level->getWorld()->daytime);
screenShader->uniform1f("u_dayTime", worldInfo.daytime);
postProcessing->render(pctx, screenShader);
}

View File

@ -44,13 +44,13 @@ void EngineController::deleteWorld(const std::string& name) {
std::shared_ptr<Task> create_converter(
Engine* engine,
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const runnable& postRunnable
) {
return WorldConverter::startTask(
folder,
worldFiles,
content,
lut,
[=]() {
@ -67,7 +67,7 @@ void show_convert_request(
Engine* engine,
const Content* content,
const std::shared_ptr<ContentLUT>& lut,
const fs::path& folder,
const std::shared_ptr<WorldFiles>& worldFiles,
const runnable& postRunnable
) {
guiutil::confirm(
@ -75,7 +75,7 @@ void show_convert_request(
langs::get(L"world.convert-request"),
[=]() {
auto converter =
create_converter(engine, folder, content, lut, postRunnable);
create_converter(engine, worldFiles, content, lut, postRunnable);
menus::show_process_panel(
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 {
auto content = engine->getContent();
auto& packs = engine->getContentPacks();
auto& settings = engine->getSettings();
auto level = World::load(folder, settings, content, packs);
auto level = World::load(worldFiles, settings, content, packs);
engine->setScreen(
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();
std::shared_ptr<ContentLUT> lut(World::checkIndices(folder, content));
if (lut) {
auto worldFiles = std::make_shared<WorldFiles>(
folder, engine->getSettings().debug);
if (auto lut = World::checkIndices(worldFiles, content)) {
if (lut->hasMissingContent()) {
engine->setScreen(std::make_shared<MenuScreen>(engine));
show_content_missing(engine, lut);
@ -145,7 +145,7 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
engine,
create_converter(
engine,
folder,
worldFiles,
content,
lut,
[=]() { openWorld(name, false); }
@ -153,14 +153,14 @@ void EngineController::openWorld(const std::string& name, bool confirmConvert) {
L"Converting world..."
);
} else {
show_convert_request(engine, content, lut, folder, [=]() {
show_convert_request(engine, content, lut, std::move(worldFiles), [=]() {
openWorld(name, false);
});
}
}
} else {
loadWorld(engine, folder);
return;
}
loadWorld(engine, std::move(worldFiles));
}
inline uint64_t str2seed(const std::string& seedstr) {

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) {
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) {
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) {
auto value = lua::tonumber(L, 1);
level->getWorld()->daytime = fmod(value, 1.0);
level->getWorld()->getInfo().daytime = std::fmod(value, 1.0);
return 0;
}
static int l_world_set_day_time_speed(lua::State* L) {
auto value = lua::tonumber(L, 1);
level->getWorld()->daytimeSpeed = std::abs(value);
level->getWorld()->getInfo().daytimeSpeed = std::abs(value);
return 0;
}
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) {
@ -76,12 +76,12 @@ static int l_world_exists(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);
}
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);
}

View File

@ -29,6 +29,7 @@ Level::Level(
events(std::make_unique<LevelEvents>()),
entities(std::make_unique<Entities>(this)),
settings(settings) {
auto& worldInfo = world->getInfo();
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {
auto camera = std::make_shared<Camera>();
@ -46,8 +47,8 @@ Level::Level(
cameras.push_back(std::move(camera));
}
if (world->nextEntityId) {
entities->setNextID(world->nextEntityId);
if (worldInfo.nextEntityId) {
entities->setNextID(worldInfo.nextEntityId);
}
auto inv = std::make_shared<Inventory>(
world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE

View File

@ -26,29 +26,22 @@ world_load_error::world_load_error(const std::string& message)
}
World::World(
std::string name,
std::string generator,
const fs::path& directory,
uint64_t seed,
EngineSettings& settings,
WorldInfo info,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::vector<ContentPack>& packs
)
: name(std::move(name)),
generator(std::move(generator)),
seed(seed),
content(content),
packs(packs),
wfile(std::make_unique<WorldFiles>(directory, settings.debug)) {
}
) : info(std::move(info)),
content(content),
packs(packs),
wfile(std::move(worldFiles)) {}
World::~World() {
}
void World::updateTimers(float delta) {
daytime += delta * daytimeSpeed * DAYIME_SPECIFIC_SPEED;
daytime = fmod(daytime, 1.0f);
totalTime += delta;
info.daytime += delta * info.daytimeSpeed * DAYIME_SPECIFIC_SPEED;
info.daytime = std::fmod(info.daytime, 1.0f);
info.totalTime += delta;
}
void World::writeResources(const Content* content) {
@ -71,7 +64,7 @@ void World::writeResources(const Content* content) {
void World::write(Level* level) {
const Content* content = level->content;
level->chunks->saveAll();
nextEntityId = level->entities->peekNextID();
info.nextEntityId = level->entities->peekNextID();
wfile->write(this, content);
auto playerFile = dynamic::Map();
@ -95,32 +88,39 @@ std::unique_ptr<Level> World::create(
const Content* content,
const std::vector<ContentPack>& packs
) {
WorldInfo info {};
info.name = name;
info.generator = generator;
info.seed = seed;
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);
}
std::unique_ptr<Level> World::load(
const fs::path& directory,
const std::shared_ptr<WorldFiles>& worldFilesPtr,
EngineSettings& settings,
const Content* content,
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>(
".",
WorldGenerators::getDefaultGeneratorID(),
directory,
0,
settings,
info.value(),
std::move(worldFilesPtr),
content,
packs
);
auto& wfile = world->wfile;
if (!wfile->readWorldInfo(world.get())) {
throw world_load_error("could not to find world.json");
}
wfile->readResourcesData(content);
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(
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)) {
return ContentLUT::create(indicesFile, content);
return ContentLUT::create(worldFiles, indicesFile, content);
}
return nullptr;
}
void World::setName(const std::string& name) {
this->name = name;
this->info.name = name;
}
void World::setGenerator(const std::string& generator) {
this->generator = generator;
this->info.generator = generator;
}
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) {
this->seed = seed;
this->info.seed = seed;
}
std::string World::getName() const {
return name;
return info.name;
}
uint64_t World::getSeed() const {
return seed;
return info.seed;
}
std::string World::getGenerator() const {
return generator;
return info.generator;
}
const std::vector<ContentPack>& World::getPacks() const {
return packs;
}
void World::deserialize(dynamic::Map* root) {
void WorldInfo::deserialize(dynamic::Map* root) {
name = root->get("name", name);
generator = root->get("generator", generator);
seed = root->get("seed", seed);
@ -208,10 +208,8 @@ void World::deserialize(dynamic::Map* root) {
generator = WorldGenerators::getDefaultGeneratorID();
}
if (auto verobj = root->map("version")) {
int major = 0, minor = -1;
verobj->num("major", major);
verobj->num("minor", minor);
logger.info() << "world version: " << major << "." << minor;
}
if (auto timeobj = root->map("time")) {
timeobj->num("day-time", daytime);
@ -225,7 +223,7 @@ void World::deserialize(dynamic::Map* root) {
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& versionobj = root->putMap("version");

View File

@ -24,20 +24,12 @@ public:
world_load_error(const std::string& message);
};
/// @brief holds all world data except the level (chunks and objects)
class World : Serializable {
struct WorldInfo : public Serializable {
std::string name;
std::string generator;
uint64_t seed;
const Content* const content;
std::vector<ContentPack> packs;
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
/// 0.0 - is midnight and
/// 0.5 - is noon
@ -54,12 +46,28 @@ public:
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(
std::string name,
std::string generator,
const fs::path& directory,
uint64_t seed,
EngineSettings& settings,
WorldInfo info,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const std::vector<ContentPack>& packs
);
@ -78,7 +86,7 @@ public:
/// @param content current Content instance
/// @return ContentLUT if world convert required else nullptr
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
@ -102,7 +110,7 @@ public:
);
/// @brief Load an existing world
/// @param directory root world directory
/// @param worldFiles world files manager
/// @param settings current engine settings
/// @param content current engine Content instance
/// with all world content-packs applied
@ -110,7 +118,7 @@ public:
/// @return Level instance containing World instance
/// @throws world_load_error on world.json load error
static std::unique_ptr<Level> load(
const fs::path& directory,
const std::shared_ptr<WorldFiles>& worldFiles,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs
@ -134,6 +142,14 @@ public:
/// @brief Get world generator id
std::string getGenerator() const;
WorldInfo& getInfo() {
return info;
}
const WorldInfo& getInfo() const {
return info;
}
/// @brief Get vector of all content-packs installed in world
const std::vector<ContentPack>& getPacks() const;
@ -147,7 +163,4 @@ public:
const Content* getContent() const {
return content;
}
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map* src) override;
};