From 3430e5cd6ff772bc4653f3d437e45102f1acb587 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 12 Feb 2025 16:32:21 +0300 Subject: [PATCH] add 'player-entity' to defaults.toml --- res/config/defaults.toml | 1 + res/content/base/config/defaults.toml | 1 + src/content/Content.cpp | 4 +++- src/content/Content.hpp | 8 +++++++- src/content/ContentBuilder.cpp | 3 ++- src/content/ContentBuilder.hpp | 1 + src/content/ContentLoader.cpp | 5 +++++ src/io/engine_paths.hpp | 3 +-- src/logic/scripting/lua/libs/libgeneration.cpp | 7 +++++-- src/objects/Player.cpp | 12 ++++++++---- 10 files changed, 34 insertions(+), 11 deletions(-) diff --git a/res/config/defaults.toml b/res/config/defaults.toml index 53de523f..dd2bd314 100644 --- a/res/config/defaults.toml +++ b/res/config/defaults.toml @@ -1 +1,2 @@ generator = "core:default" +player-entity = "" diff --git a/res/content/base/config/defaults.toml b/res/content/base/config/defaults.toml index d292d2ce..a099c198 100644 --- a/res/content/base/config/defaults.toml +++ b/res/content/base/config/defaults.toml @@ -1 +1,2 @@ generator = "base:demo" +player-entity = "base:player" diff --git a/src/content/Content.cpp b/src/content/Content.cpp index 68e9cec3..1cc2fdd7 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -34,12 +34,14 @@ Content::Content( UptrsMap packs, UptrsMap blockMaterials, UptrsMap skeletons, - ResourceIndicesSet resourceIndices + ResourceIndicesSet resourceIndices, + dv::value defaults ) : indices(std::move(indices)), packs(std::move(packs)), blockMaterials(std::move(blockMaterials)), skeletons(std::move(skeletons)), + defaults(std::move(defaults)), blocks(std::move(blocks)), items(std::move(items)), entities(std::move(entities)), diff --git a/src/content/Content.hpp b/src/content/Content.hpp index 3a836c4e..b95ef247 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -201,6 +201,7 @@ class Content { UptrsMap packs; UptrsMap blockMaterials; UptrsMap skeletons; + dv::value defaults = nullptr; public: ContentUnitDefs blocks; ContentUnitDefs items; @@ -219,7 +220,8 @@ public: UptrsMap packs, UptrsMap blockMaterials, UptrsMap skeletons, - ResourceIndicesSet resourceIndices + ResourceIndicesSet resourceIndices, + dv::value defaults ); ~Content(); @@ -231,6 +233,10 @@ public: return resourceIndices[static_cast(type)]; } + inline const dv::value& getDefaults() const { + return defaults; + } + const rigging::SkeletonConfig* getSkeleton(const std::string& id) const; const BlockMaterial* findBlockMaterial(const std::string& id) const; const ContentPackRuntime* getPackRuntime(const std::string& id) const; diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp index 49433dfd..c57a7ad8 100644 --- a/src/content/ContentBuilder.cpp +++ b/src/content/ContentBuilder.cpp @@ -84,7 +84,8 @@ std::unique_ptr ContentBuilder::build() { std::move(packs), std::move(blockMaterials), std::move(skeletons), - std::move(resourceIndices) + std::move(resourceIndices), + std::move(defaults) ); // Now, it's time to resolve foreign keys diff --git a/src/content/ContentBuilder.hpp b/src/content/ContentBuilder.hpp index cc35838c..7d3df1f6 100644 --- a/src/content/ContentBuilder.hpp +++ b/src/content/ContentBuilder.hpp @@ -73,6 +73,7 @@ public: ContentUnitBuilder entities {allNames, ContentType::ENTITY}; ContentUnitBuilder generators {allNames, ContentType::GENERATOR}; ResourceIndicesSet resourceIndices {}; + dv::value defaults = nullptr; ~ContentBuilder(); diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index d3df18f1..52f4ffa0 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -23,6 +23,7 @@ #include "data/dv_util.hpp" #include "data/StructLayout.hpp" #include "presets/ParticlesPreset.hpp" +#include "io/engine_paths.hpp" namespace fs = std::filesystem; using namespace data; @@ -760,6 +761,10 @@ void ContentLoader::load() { auto folder = pack->folder; + builder.defaults = paths.readCombinedObject( + EnginePaths::CONFIG_DEFAULTS.string() + ); + // Load world generators io::path generatorsDir = folder / "generators"; foreach_file(generatorsDir, [this](const io::path& file) { diff --git a/src/io/engine_paths.hpp b/src/io/engine_paths.hpp index 65e5e067..6bf62010 100644 --- a/src/io/engine_paths.hpp +++ b/src/io/engine_paths.hpp @@ -39,8 +39,7 @@ public: static std::tuple parsePath(std::string_view view); - static inline auto CONFIG_DEFAULTS = - std::filesystem::u8path("config/defaults.toml"); + static inline io::path CONFIG_DEFAULTS = "config/defaults.toml"; private: std::filesystem::path userFilesFolder {"."}; std::filesystem::path resourcesFolder {"res"}; diff --git a/src/logic/scripting/lua/libs/libgeneration.cpp b/src/logic/scripting/lua/libs/libgeneration.cpp index 855700a9..721803f3 100644 --- a/src/logic/scripting/lua/libs/libgeneration.cpp +++ b/src/logic/scripting/lua/libs/libgeneration.cpp @@ -6,6 +6,7 @@ #include "world/Level.hpp" #include "world/generator/VoxelFragment.hpp" #include "content/ContentLoader.hpp" +#include "content/Content.hpp" #include "engine/Engine.hpp" #include "../lua_custom_types.hpp" @@ -69,8 +70,9 @@ static int l_get_generators(lua::State* L) { /// @brief Get the default world generator /// @return The ID of the default world generator static int l_get_default_generator(lua::State* L) { + // content is not initialized yet auto combined = engine->getResPaths()->readCombinedObject( - EnginePaths::CONFIG_DEFAULTS.u8string() + EnginePaths::CONFIG_DEFAULTS.string() ); return lua::pushstring(L, combined["generator"].asString()); } @@ -81,4 +83,5 @@ const luaL_Reg generationlib[] = { {"load_fragment", lua::wrap}, {"get_generators", lua::wrap}, {"get_default_generator", lua::wrap}, - {NULL, NULL}}; + {NULL, NULL} +}; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 9058717c..cbedd676 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -63,10 +63,14 @@ Player::~Player() = default; void Player::updateEntity() { if (eid == ENTITY_AUTO) { - auto& def = level.content.entities.require("base:player"); - eid = level.entities->spawn(def, getPosition()); - if (auto entity = level.entities->get(eid)) { - entity->setPlayer(id); + const auto& defaults = level.content.getDefaults(); + const auto& defName = defaults["player-entity"].asString(); + if (!defName.empty()) { + auto& def = level.content.entities.require(defName); + eid = level.entities->spawn(def, getPosition()); + if (auto entity = level.entities->get(eid)) { + entity->setPlayer(id); + } } } else if (auto entity = level.entities->get(eid)) { position = entity->getTransform().pos;