From d0803fce0c16d6c026d81b80e3736e093772bc80 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 14 Oct 2026 12:15:32 +0300 Subject: [PATCH] add world.get_logic, set_logic 'block_ticks', 'random_ticks' runtime world parameters --- res/modules/internal/lifetime_events.lua | 4 +- src/logic/BlocksController.cpp | 24 +++++++--- src/logic/scripting/lua/libs/libworld.cpp | 13 +++++ src/world/World.cpp | 58 ++++++++++++++++++++++- src/world/World.hpp | 20 ++++++++ 5 files changed, 110 insertions(+), 9 deletions(-) diff --git a/res/modules/internal/lifetime_events.lua b/res/modules/internal/lifetime_events.lua index d1750e4cb..670f92561 100644 --- a/res/modules/internal/lifetime_events.lua +++ b/res/modules/internal/lifetime_events.lua @@ -197,7 +197,9 @@ local function __process_post_runnables() network.__process_events() if not hud or not hud.is_paused() then internals.process_block_register_events() - internals.tick_updating_blocks(time.delta()) + if world.is_open() and world.get_logic("block_ticks") then + internals.tick_updating_blocks(time.delta()) + end end end diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 58594f6ee..cf6f5564d 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -130,14 +130,26 @@ void BlocksController::updateBlock(int x, int y, int z) { } void BlocksController::update(float delta, uint padding) { - if (int parts = randTickClock.update(delta)) { - for (int i = 0; i < parts; i++) { - randomTick(randTickClock.convertPart(i), randTickClock.getParts(), padding); + auto& worldInfo = level.getWorld().getInfo(); + + if (worldInfo.getLogicFlag("random_ticks", true)) { + if (int parts = randTickClock.update(delta)) { + for (int i = 0; i < parts; i++) { + randomTick( + randTickClock.convertPart(i), + randTickClock.getParts(), + padding + ); + } } } - if (int parts = blocksTickClock.update(delta)) { - for (int i = 0; i < parts; i++) { - onBlocksTick(blocksTickClock.convertPart(i), blocksTickClock.getParts()); + if (worldInfo.getLogicFlag("block_ticks", true)) { + if (int parts = blocksTickClock.update(delta)) { + for (int i = 0; i < parts; i++) { + onBlocksTick( + blocksTickClock.convertPart(i), blocksTickClock.getParts() + ); + } } } if (worldTickClock.update(delta)) { diff --git a/src/logic/scripting/lua/libs/libworld.cpp b/src/logic/scripting/lua/libs/libworld.cpp index c45b7bd35..b2257d45e 100644 --- a/src/logic/scripting/lua/libs/libworld.cpp +++ b/src/logic/scripting/lua/libs/libworld.cpp @@ -108,6 +108,17 @@ static int l_get_seed(lua::State* L) { return lua::pushinteger(L, require_world_info().seed); } +static int l_get_logic(lua::State* L) { + auto name = lua::require_string(L, 1); + return lua::pushvalue(L, require_world_info().getLogicValue(name)); +} + +static int l_set_logic(lua::State* L) { + auto name = lua::require_string(L, 1); + require_world_info().setLogicValue(name, lua::tovalue(L, 2)); + return 0; +} + static int l_exists(lua::State* L) { auto name = lua::require_string(L, 1); auto worldsDir = engine->getPaths().getWorldFolderByName(name); @@ -250,6 +261,8 @@ const luaL_Reg worldlib[] = { {"set_day_time_speed", lua::wrap}, {"get_day_time_speed", lua::wrap}, {"get_seed", lua::wrap}, + {"get_logic", lua::wrap}, + {"set_logic", lua::wrap}, {"get_generator", lua::wrap}, {"is_day", lua::wrap}, {"is_night", lua::wrap}, diff --git a/src/world/World.cpp b/src/world/World.cpp index d65dd2636..bf74ed990 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include "content/Content.hpp" @@ -14,6 +15,7 @@ #include "objects/Player.hpp" #include "objects/Players.hpp" #include "settings.hpp" +#include "util/stringutil.hpp" #include "voxels/Chunk.hpp" #include "voxels/Chunks.hpp" #include "voxels/GlobalChunks.hpp" @@ -24,6 +26,7 @@ static debug::Logger logger("world"); const float DAYIME_SPECIFIC_SPEED = 1.0f / 1440.0f; // 1.0f/60.0f/24.0f; + world_load_error::world_load_error(const std::string& message) : std::runtime_error(message) { } @@ -38,8 +41,7 @@ World::World( packs(packs), wfile(std::move(worldFiles)) {} -World::~World() { -} +World::~World() = default; void World::updateTimers(float delta) { info.daytime += delta * info.daytimeSpeed * DAYIME_SPECIFIC_SPEED; @@ -76,6 +78,49 @@ void World::write(Level& level) { writeResources(content); } +dv::value WorldInfo::getLogicValue(const std::string& name, + const dv::value& def) const { + if (logic.isObject()) { + if (auto field = logic.at(name)) { + return *field; + } + } + const auto& defaults = defaultLogic(); + if (defaults.isObject()) { + if (auto field = defaults.at(name)) { + return *field; + } + } + return def; +} + +void WorldInfo::setLogicValue(const std::string& name, dv::value value) { + if (!WorldInfo::isLogicKey(name)) { + throw std::runtime_error("unknown logic parameter " + util::quote(name)); + } + if (!logic.isObject()) { + logic = dv::object(); + } + logic[name] = std::move(value); +} + +bool WorldInfo::getLogicFlag(const std::string& name, bool def) const { + auto value = getLogicValue(name, dv::value(def)); + if (value.isBoolean()) { + return value.asBoolean(); + } + if (value.isInteger()) { + return value.asInteger() != 0; + } + if (value.isNumber()) { + return value.asNumber() != 0.0; + } + if (value.isString()) { + return !value.asString().empty(); + } + return def; +} + std::unique_ptr World::create( const std::string& name, const std::string& environment, @@ -216,6 +261,14 @@ void WorldInfo::deserialize(const dv::value& root) { daytimeSpeed = timeobj["day-time-speed"].asNumber(); totalTime = timeobj["total-time"].asNumber(); } + if (root.has("logic")) { + logic = root["logic"]; + } else { + logic = dv::object({ + {"random_ticks", true}, + {"block_ticks", true} + }); + } nextInventoryId = root["next-inventory-id"].asInteger(2); nextEntityId = root["next-entity-id"].asInteger(1); root.at("next-player-id").get(nextPlayerId); @@ -244,5 +297,6 @@ dv::value WorldInfo::serialize() const { root["next-inventory-id"] = nextInventoryId; root["next-entity-id"] = nextEntityId; root["next-player-id"] = nextPlayerId; + root["logic"] = logic; return root; } diff --git a/src/world/World.hpp b/src/world/World.hpp index dc9439906..51a9bb236 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -22,6 +22,18 @@ public: }; struct WorldInfo : public Serializable { + static const dv::value& defaultLogic() { + static const dv::value logic = dv::object({ + {"random_ticks", true}, + {"block_ticks", true} + }); + return logic; + } + + static bool isLogicKey(const std::string& name) { + return defaultLogic().has(name); + } + std::string name; std::string environment; std::string explicitGenerator; @@ -29,6 +41,8 @@ struct WorldInfo : public Serializable { int64_t nextInventoryId = 1; int64_t nextPlayerId = 0; + dv::value logic = defaultLogic(); + /// @brief Day/night loop timer in range 0..1 where /// 0.0 - is midnight and /// 0.5 - is noon @@ -45,6 +59,12 @@ struct WorldInfo : public Serializable { bool isLoaded = false; + dv::value getLogicValue( + const std::string& name, const dv::value& def = nullptr + ) const; + void setLogicValue(const std::string& name, dv::value value); + bool getLogicFlag(const std::string& name, bool def = true) const; + dv::value serialize() const override; void deserialize(const dv::value& src) override; };