From cf2faa4a0109678be3ddef12a514aeb52060de8a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 5 Jul 2024 23:41:55 +0300 Subject: [PATCH] implement nextEntityID save/load --- src/frontend/debug_panel.cpp | 3 ++- src/objects/Entities.hpp | 8 ++++++++ src/world/Level.cpp | 1 + src/world/World.cpp | 5 +++++ src/world/World.hpp | 2 ++ 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/frontend/debug_panel.cpp b/src/frontend/debug_panel.cpp index 6960a6e9..59741526 100644 --- a/src/frontend/debug_panel.cpp +++ b/src/frontend/debug_panel.cpp @@ -85,7 +85,8 @@ std::shared_ptr create_debug_panel( L" visible: "+std::to_wstring(level->chunks->visible); })); panel->add(create_label([=]() { - return L"entities: "+std::to_wstring(level->entities->size()); + return L"entities: "+std::to_wstring(level->entities->size())+L" next: "+ + std::to_wstring(level->entities->peekNextID()); })); panel->add(create_label([=](){ const auto& vox = player->selection.vox; diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 8492186c..579c1ec0 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -192,9 +192,17 @@ public: void despawn(entityid_t id); dynamic::Value serialize(const Entity& entity); + void setNextID(entityid_t id) { + nextID = id; + } + inline size_t size() const { return entities.size(); } + + inline entityid_t peekNextID() const { + return nextID; + } }; #endif // OBJECTS_ENTITIES_HPP_ diff --git a/src/world/Level.cpp b/src/world/Level.cpp index e057708d..fc0549ab 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -26,6 +26,7 @@ Level::Level( entities(std::make_unique(this)), settings(settings) { + entities->setNextID(this->world->nextEntityId); auto inv = std::make_shared( this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE ); diff --git a/src/world/World.cpp b/src/world/World.cpp index 0b10dc33..9c85e38b 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -9,10 +9,12 @@ #include "../files/WorldFiles.hpp" #include "../items/Inventories.hpp" #include "../objects/Player.hpp" +#include "../objects/Entities.hpp" #include "../voxels/Chunk.hpp" #include "../voxels/Chunks.hpp" #include "../voxels/ChunksStorage.hpp" #include "../world/WorldGenerators.hpp" +#include "Level.hpp" #include #include @@ -52,6 +54,7 @@ void World::updateTimers(float delta) { void World::write(Level* level) { const Content* content = level->content; level->chunks->saveAll(); + nextEntityId = level->entities->peekNextID(); wfile->write(this, content); auto playerFile = dynamic::Map(); @@ -193,6 +196,7 @@ void World::deserialize(dynamic::Map* root) { weatherobj->num("fog", fog); } nextInventoryId = root->get("next-inventory-id", 2); + nextEntityId = root->get("next-entity-id", 1); } std::unique_ptr World::serialize() const { @@ -215,5 +219,6 @@ std::unique_ptr World::serialize() const { weatherobj.put("fog", fog); root->put("next-inventory-id", nextInventoryId); + root->put("next-entity-id", nextEntityId); return root; } diff --git a/src/world/World.hpp b/src/world/World.hpp index 904ed027..6926dcf6 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -51,6 +51,8 @@ public: /// @brief will be replaced with weather in future float fog = 0.0f; + entityid_t nextEntityId = 0; + World( std::string name, std::string generator,