refactor World

This commit is contained in:
MihailRis
2024-08-11 16:30:58 +03:00
parent bcdff0f816
commit 3d3da1cdcd
8 changed files with 102 additions and 83 deletions
+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(
+4 -2
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;
@@ -35,7 +37,7 @@ class WorldFiles {
fs::path getIndicesFile() 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);
@@ -46,7 +48,7 @@ public:
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
+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);
} }
+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
+36 -37
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, std::unique_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,8 +88,15 @@ 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);
} }
@@ -107,20 +107,21 @@ std::unique_ptr<Level> World::load(
const Content* content, const Content* content,
const std::vector<ContentPack>& packs const std::vector<ContentPack>& packs
) { ) {
auto worldFilesPtr = std::make_unique<WorldFiles>(directory, settings.debug);
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);
@@ -165,11 +166,11 @@ std::shared_ptr<ContentLUT> World::checkIndices(
} }
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 +181,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 +209,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 +224,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");
+30 -17
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::unique_ptr<WorldFiles> wfile;
World( World(
std::string name, WorldInfo info,
std::string generator, std::unique_ptr<WorldFiles> wfile,
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
); );
@@ -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;
}; };