From 57356e1d64d6d9d7e8d59b078543b290e998ad00 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 01:27:53 +0300 Subject: [PATCH 01/12] fix lamp material --- res/content/base/blocks/lamp.json | 1 + 1 file changed, 1 insertion(+) diff --git a/res/content/base/blocks/lamp.json b/res/content/base/blocks/lamp.json index 4a1f1ce71..b8f911008 100644 --- a/res/content/base/blocks/lamp.json +++ b/res/content/base/blocks/lamp.json @@ -2,5 +2,6 @@ "texture": "lamp", "emission": [15, 14, 13], "shadeless": true, + "material": "base:glass", "base:durability": 0.3 } From e247902cc6ffdaa6beab391fcfdaea7f021ab063 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 02:30:59 +0300 Subject: [PATCH 02/12] fix data_buffer:put_number --- res/modules/bit_converter.lua | 4 ++-- res/modules/data_buffer.lua | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/res/modules/bit_converter.lua b/res/modules/bit_converter.lua index c2ada355f..b72dc3114 100644 --- a/res/modules/bit_converter.lua +++ b/res/modules/bit_converter.lua @@ -313,8 +313,8 @@ function bit_converter.bytes_to_uint16(bytes, order) return bit.bor( - bit.lshift(bytes[1], 8), - bytes[2], 0) + bit.lshift(bytes[2], 8), + bytes[1], 0) end function bit_converter.bytes_to_int64(bytes, order) diff --git a/res/modules/data_buffer.lua b/res/modules/data_buffer.lua index 91dce29c7..ad9ad4867 100644 --- a/res/modules/data_buffer.lua +++ b/res/modules/data_buffer.lua @@ -144,31 +144,31 @@ function data_buffer:put_number(num) if math.floor(num) ~= num then type = TYPE_FLOAT64 - bytes = bit_converter.float64_to_bytes(num) + bytes = bit_converter.float64_to_bytes(num, self.order) elseif num == 0 then type = TYPE_ZERO bytes = { } elseif num > 0 then if num <= MAX_UINT16 then type = TYPE_UINT16 - bytes = bit_converter.uint16_to_bytes(num) + bytes = bit_converter.uint16_to_bytes(num, self.order) elseif num <= MAX_UINT32 then type = TYPE_UINT32 - bytes = bit_converter.uint32_to_bytes(num) + bytes = bit_converter.uint32_to_bytes(num, self.order) elseif num <= MAX_INT64 then type = TYPE_INT64 - bytes = bit_converter.int64_to_bytes(num) + bytes = bit_converter.int64_to_bytes(num, self.order) end elseif num < 0 then if num >= MIN_INT16 then type = TYPE_SINT16 - bytes = bit_converter.sint16_to_bytes(num) + bytes = bit_converter.sint16_to_bytes(num, self.order) elseif num >= MIN_INT32 then type = TYPE_SINT32 - bytes = bit_converter.sint32_to_bytes(num) + bytes = bit_converter.sint32_to_bytes(num, self.order) elseif num >= MIN_INT64 then type = TYPE_INT64 - bytes = bit_converter.int64_to_bytes(num) + bytes = bit_converter.int64_to_bytes(num, self.order) end end From 266f3059b4ae56a0aed852e942688f85671d81a3 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 04:17:29 +0300 Subject: [PATCH 03/12] fix --- src/graphics/ui/GUI.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 2cc280244..08485201f 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -202,7 +202,7 @@ void GUI::act(float delta, const Viewport& vp) { void GUI::postAct() { while (!postRunnables.empty()) { - runnable callback = postRunnables.back(); + runnable callback = postRunnables.front(); postRunnables.pop(); callback(); } From 85600eafea0980acb8ec779a7859495d92ff1073 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 04:31:33 +0300 Subject: [PATCH 04/12] fix in-game chat --- res/layouts/ingame_chat.xml.lua | 19 ++++++------------- src/engine/Engine.cpp | 2 +- src/graphics/ui/elements/Panel.cpp | 1 + 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/res/layouts/ingame_chat.xml.lua b/res/layouts/ingame_chat.xml.lua index ec4e12b09..dce629887 100644 --- a/res/layouts/ingame_chat.xml.lua +++ b/res/layouts/ingame_chat.xml.lua @@ -7,18 +7,11 @@ local initialized = false local max_lines = 15 local animation_fps = 30 -local function remove_line(line) - document[line[1]]:destruct() - time.post_runnable(function() - if world.is_open() then document.root:reposition() end - end) -end - local function update_line(line, uptime) local diff = uptime - line[2] if diff > timeout then - remove_line(line) - table.insert(dead_lines, i) + document[line[1]]:destruct() + table.insert(dead_lines, table.index(lines, line)) elseif diff > timeout-fadeout then local opacity = (timeout - diff) / fadeout document[line[1]].color = {0, 0, 0, opacity * 80} @@ -27,16 +20,16 @@ local function update_line(line, uptime) end events.on("core:chat", function(message) + while #lines >= max_lines do + document[lines[1][1]]:destruct() + table.remove(lines, 1) + end local current_time = time.uptime() local id = 'l'..tostring(nextid) document.root:add(gui.template("chat_line", {id=id})) document.root:reposition() document[id.."L"].text = message nextid = nextid + 1 - if #lines == max_lines then - remove_line(lines[1]) - table.remove(lines, 1) - end table.insert(lines, {id, current_time}) end) diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index ce29e8c17..c9de5a64f 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -199,6 +199,7 @@ void Engine::updateFrontend() { audio::update(delta); gui->act(delta, Viewport(Window::width, Window::height)); screen->update(delta); + gui->postAct(); } void Engine::nextFrame() { @@ -217,7 +218,6 @@ void Engine::renderFrame() { Viewport viewport(Window::width, Window::height); DrawContext ctx(nullptr, viewport, nullptr); gui->draw(ctx, *assets); - gui->postAct(); } void Engine::saveSettings() { diff --git a/src/graphics/ui/elements/Panel.cpp b/src/graphics/ui/elements/Panel.cpp index d90b8830c..d76866f08 100644 --- a/src/graphics/ui/elements/Panel.cpp +++ b/src/graphics/ui/elements/Panel.cpp @@ -53,6 +53,7 @@ void Panel::cropToContent() { void Panel::fullRefresh() { refresh(); cropToContent(); + reposition(); Container::fullRefresh(); } From 13fde2116d095b9393c4f5804ba23071e5f56ad6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 04:42:08 +0300 Subject: [PATCH 05/12] fix textbox horizontal scroll & fix console log width --- res/layouts/console.xml | 4 ++-- src/graphics/ui/elements/TextBox.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/res/layouts/console.xml b/res/layouts/console.xml index 43e72dd8d..18d72163c 100644 --- a/res/layouts/console.xml +++ b/res/layouts/console.xml @@ -12,7 +12,7 @@ + size-func="unpack(vec2.add(gui.get_viewport(), {-350,-100}))"> diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index f1ed66087..12f262f1c 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -826,7 +826,7 @@ void TextBox::setCaret(size_t position) { scrolled(-glm::ceil(offset/static_cast(scrollStep)+0.5f)); } uint lcaret = caret - label->getTextLineOffset(line); - int realoffset = font->calcWidth(input, lcaret)-int(textOffset)+2; + int realoffset = font->calcWidth(input, lcaret)-int(textOffset) - padding.x; if (realoffset-width > 0) { setTextOffset(textOffset + realoffset-width); } else if (realoffset < 0) { From f2101f6504a45ea6639c7842760e09a9ac377e73 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 05:31:07 +0300 Subject: [PATCH 06/12] add entity:set_enabled(...) --- doc/en/scripting/builtins/librules.md | 2 +- doc/en/scripting/ecs.md | 3 +++ doc/ru/scripting/ecs.md | 3 +++ res/modules/internal/stdcomp.lua | 20 ++++++++++++++++++-- src/data/dv.cpp | 3 +++ src/logic/scripting/scripting.cpp | 4 ++++ 6 files changed, 32 insertions(+), 3 deletions(-) diff --git a/doc/en/scripting/builtins/librules.md b/doc/en/scripting/builtins/librules.md index c4b1c3779..acdf11d86 100644 --- a/doc/en/scripting/builtins/librules.md +++ b/doc/en/scripting/builtins/librules.md @@ -18,7 +18,7 @@ Creates a rule. If a handler is specified, returns the id for deletion. > Rules that have not been created can be used, but resetting via rules.reset will result in setting the value to nil. ```lua - rules.listen( +rules.listen( -- rule name name: str, -- value change handler function diff --git a/doc/en/scripting/ecs.md b/doc/en/scripting/ecs.md index 8763d9819..90a648285 100644 --- a/doc/en/scripting/ecs.md +++ b/doc/en/scripting/ecs.md @@ -26,6 +26,9 @@ entity:get_uid() -> int entity:get_component(name: str) -> component or nil -- Checks for the presence of a component by name entity:has_component(name: str) -> bool + +-- Enables/disables the component +entity:set_enabled(name: str, enable: bool) ``` ## Built-in components diff --git a/doc/ru/scripting/ecs.md b/doc/ru/scripting/ecs.md index 3e8e00e54..db44a1ac5 100644 --- a/doc/ru/scripting/ecs.md +++ b/doc/ru/scripting/ecs.md @@ -26,6 +26,9 @@ entity:get_uid() -> int entity:get_component(name: str) -> компонент или nil -- Проверяет наличие компонента по имени entity:has_component(name: str) -> bool + +-- Включает/выключает компонент по имени +entity:set_enabled(name: str, enable: bool) ``` ## Встроенные компоненты diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index bdb2468fc..a62989a99 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -68,6 +68,22 @@ local Entity = {__index={ def_index=function(self) return entities.get_def(self.eid) end, def_name=function(self) return entities.def_name(entities.get_def(self.eid)) end, get_player=function(self) return entities.get_player(self.eid) end, + set_enabled=function(self, name, flag) + local comp = self.components[name] + if comp then + if flag then + if comp.__disabled and comp.on_enable then + comp.on_enable() + end + comp.__disabled = nil + else + if not comp.__disabled and comp.on_disable then + comp.on_disable() + end + comp.__disabled = true + end + end + end, }} local entities = {} @@ -99,7 +115,7 @@ return { end for _, component in pairs(entity.components) do local callback = component.on_update - if callback then + if not component.__disabled and callback then local result, err = pcall(callback, tps) if err then debug.error(err) @@ -113,7 +129,7 @@ return { for _,entity in pairs(entities) do for _, component in pairs(entity.components) do local callback = component.on_render - if callback then + if not component.__disabled and callback then local result, err = pcall(callback, delta) if err then debug.error(err) diff --git a/src/data/dv.cpp b/src/data/dv.cpp index 5709fd847..cccf348e4 100644 --- a/src/data/dv.cpp +++ b/src/data/dv.cpp @@ -104,6 +104,9 @@ namespace dv { } boolean_t value::asBoolean() const { + if (type == value_type::none) { + return false; + } check_type(type, value_type::boolean); return val.boolean; } diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index d58879d32..d9b154903 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -614,6 +614,10 @@ static void process_entity_callback( ) { auto L = lua::get_main_state(); lua::pushenv(L, *env); + if (lua::hasfield(L, "__disabled")) { + lua::pop(L); + return; + } if (lua::getfield(L, name)) { if (args) { lua::call_nothrow(L, args(L), 0); From 23c66654a21c60a0bf22ae87530fde26165bb8d1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 05:56:14 +0300 Subject: [PATCH 07/12] add ENTITY_NONE, ENTITY_AUTO reserved entity id & update player.set_entity(...) --- src/constants.hpp | 1 + src/logic/scripting/lua/libs/libplayer.cpp | 4 +++- src/objects/Player.cpp | 4 ++-- src/objects/Player.hpp | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/constants.hpp b/src/constants.hpp index 6f4a6ce6e..8755331d2 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -27,6 +27,7 @@ inline constexpr blockid_t BLOCK_OBSTACLE = 1; inline constexpr blockid_t BLOCK_STRUCT_AIR = 2; inline constexpr itemid_t ITEM_EMPTY = 0; inline constexpr entityid_t ENTITY_NONE = 0; +inline constexpr entityid_t ENTITY_AUTO = std::numeric_limits::max(); inline constexpr int CHUNK_W = 16; inline constexpr int CHUNK_H = 256; diff --git a/src/logic/scripting/lua/libs/libplayer.cpp b/src/logic/scripting/lua/libs/libplayer.cpp index 12b78a97f..d4e55b777 100644 --- a/src/logic/scripting/lua/libs/libplayer.cpp +++ b/src/logic/scripting/lua/libs/libplayer.cpp @@ -229,7 +229,9 @@ static int l_set_entity(lua::State* L) { if (player == nullptr) { return 0; } - if (auto entity = get_entity(L, 2)) { + if (lua::isnumber(L, 2)) { + player->setEntity(lua::tointeger(L, 2)); + } else if (auto entity = get_entity(L, 2)) { player->setEntity(entity->getUID()); } return 0; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 777fd8ed8..888fce96c 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -62,7 +62,7 @@ Player::Player( Player::~Player() = default; void Player::updateEntity() { - if (eid == 0) { + 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)) { @@ -73,7 +73,7 @@ void Player::updateEntity() { if (auto entity = level.entities->get(eid)) { entity->setPlayer(id); } - } else if (chunks->getChunkByVoxel(position)) { + } else if (chunks->getChunkByVoxel(position) && eid != ENTITY_NONE) { logger.error() << "player entity despawned or deleted; " "will be respawned"; eid = 0; diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index b695c75eb..4b9f24491 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -54,7 +54,7 @@ class Player : public Serializable { bool infiniteItems = true; bool instantDestruction = true; bool loadingChunks = true; - entityid_t eid; + entityid_t eid = ENTITY_AUTO; entityid_t selectedEid = 0; glm::vec3 rotation {}; From 94438924067d3a8da81fc5500eb5025e5a5e018c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 06:51:29 +0300 Subject: [PATCH 08/12] fixes --- src/objects/Player.cpp | 2 +- src/objects/Players.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 888fce96c..9058717c6 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -76,7 +76,7 @@ void Player::updateEntity() { } else if (chunks->getChunkByVoxel(position) && eid != ENTITY_NONE) { logger.error() << "player entity despawned or deleted; " "will be respawned"; - eid = 0; + eid = ENTITY_AUTO; } } diff --git a/src/objects/Players.cpp b/src/objects/Players.cpp index e200a20b7..9c0e2603d 100644 --- a/src/objects/Players.cpp +++ b/src/objects/Players.cpp @@ -37,7 +37,7 @@ Player* Players::create(int64_t id) { glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, level.inventories->create(DEF_PLAYER_INVENTORY_SIZE), - 0 + ENTITY_AUTO ); auto player = playerPtr.get(); add(std::move(playerPtr)); @@ -92,7 +92,7 @@ void Players::deserialize(const dv::value& src) { glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, level.inventories->create(DEF_PLAYER_INVENTORY_SIZE), - 0 + ENTITY_AUTO ); auto player = playerPtr.get(); player->deserialize(playerMap); From b12982655de6f991852e99330a868afd33ae14ac Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 21 Jan 2025 09:39:06 +0300 Subject: [PATCH 09/12] fix gui.alert --- res/scripts/stdlib.lua | 18 ++++++++++++++++-- src/graphics/ui/gui_util.cpp | 19 ++++++++++--------- src/logic/scripting/lua/lua_wrapper.hpp | 3 +++ src/logic/scripting/scripting.cpp | 13 ++++++++----- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 9ee20241d..67cdd0fe8 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -37,7 +37,10 @@ local function complete_app_lib(app) app.tick = coroutine.yield app.get_version = core.get_version app.get_setting_info = core.get_setting_info - app.load_content = core.load_content + app.load_content = function() + core.load_content() + app.tick() + end app.reset_content = core.reset_content app.is_content_loaded = core.is_content_loaded @@ -416,7 +419,18 @@ end function start_coroutine(chunk, name) local co = coroutine.create(function() - local status, error = xpcall(chunk, __vc__error) + local status, error = xpcall(chunk, function(...) + gui.alert(debug.traceback(), function() + if world.is_open() then + __vc_app.close_world() + else + __vc_app.reset_content() + menu:reset() + menu.page = "main" + end + end) + return ... + end) if not status then debug.error(error) end diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index b84dcfd0e..04026132c 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -34,13 +34,14 @@ void guiutil::alert( auto panel = std::make_shared(glm::vec2(500, 300), glm::vec4(4.0f), 4.0f); panel->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f)); - auto menu = engine.getGUI()->getMenu(); - runnable on_hidden_final = [on_hidden, menu, &engine]() { - menu->removePage(""); + auto menuPtr = engine.getGUI()->getMenu(); + auto& menu = *menuPtr; + runnable on_hidden_final = [on_hidden, &menu, &engine]() { + menu.removePage(""); if (on_hidden) { on_hidden(); } else { - menu->back(); + menu.back(); } }; @@ -50,21 +51,21 @@ void guiutil::alert( panel->add(label); panel->add(std::make_shared