From 3aa7d6ac28cf870bb81642ddd9b3160f7939ec50 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 18 Feb 2025 01:03:34 +0300 Subject: [PATCH] feat: support items data in base pack --- res/content/base/modules/util.lua | 3 ++- res/content/base/scripts/components/drop.lua | 11 ++++++----- res/content/base/scripts/hud.lua | 3 ++- src/items/ItemStack.cpp | 3 +++ src/items/ItemStack.hpp | 4 ++++ src/logic/scripting/lua/libs/libinventory.cpp | 5 ++++- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/res/content/base/modules/util.lua b/res/content/base/modules/util.lua index d12337fb..b1a97ad0 100644 --- a/res/content/base/modules/util.lua +++ b/res/content/base/modules/util.lua @@ -1,12 +1,13 @@ local util = {} -function util.drop(ppos, itemid, count, pickup_delay) +function util.drop(ppos, itemid, count, data, pickup_delay) if itemid == 0 or not itemid then return nil end return entities.spawn("base:drop", ppos, {base__drop={ id=itemid, count=count, + data=data, pickup_delay=pickup_delay }}) end diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 7aa8babb..cf57f72f 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -14,6 +14,7 @@ end if SAVED_DATA.item then dropitem.id = item.index(SAVED_DATA.item) dropitem.count = SAVED_DATA.count + dropitem.data = SAVED_DATA.data end local DROP_SCALE = 0.3 @@ -25,6 +26,7 @@ local rotation = mat4.rotate({ function on_save() SAVED_DATA.item = item.name(dropitem.id) SAVED_DATA.count = dropitem.count + SAVED_DATA.data = dropitem.data end do -- setup visuals @@ -59,11 +61,10 @@ function on_sensor_enter(index, oid) if pid == -1 then -- other is base:drop too if index == 0 and other:def_index() == def_index then - local odrop = other:get_component("base:drop") - if odrop.dropitem.id == dropitem.id then - -- // TODO: replace combination logic with item.* function + local odrop = other:get_component("base:drop").dropitem + if odrop.id == dropitem.id and not odrop.data then local stack = item.stack_size(dropitem.id) - local sum = dropitem.count + odrop.dropitem.count + local sum = dropitem.count + odrop.count if sum <= stack then dropitem.count = sum other:despawn() @@ -75,7 +76,7 @@ function on_sensor_enter(index, oid) if timer < 0.0 and index == 0 then entity:despawn() - inventory.add(player.get_inventory(pid), dropitem.id, dropitem.count) + inventory.add(player.get_inventory(pid), dropitem.id, dropitem.count, dropitem.data) audio.play_sound_2d("events/pickup", 0.5, 0.8 + math.random() * 0.4, "regular") end if index == 1 then diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 31125631..00f6bdc6 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -14,12 +14,13 @@ function on_hud_open() if itemid == 0 then return end + local data = inventory.get_all_data(invid, slot) inventory.set(invid, slot, itemid, itemcount-1) local pvel = {player.get_vel(pid)} local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) local throw_force = vec3.mul(player.get_dir(pid), DROP_FORCE) - local drop = base_util.drop(ppos, itemid, 1, 1.5) + local drop = base_util.drop(ppos, itemid, 1, data, 1.5) local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)) drop.rigidbody:set_vel(velocity) end) diff --git a/src/items/ItemStack.cpp b/src/items/ItemStack.cpp index 3e63371a..f99d8700 100644 --- a/src/items/ItemStack.cpp +++ b/src/items/ItemStack.cpp @@ -57,6 +57,9 @@ void ItemStack::setField(std::string_view name, dv::value value) { } if (value == nullptr) { fields.erase(std::string(name)); + if (fields.empty()) { + fields = nullptr; + } return; } fields[std::string(name)] = std::move(value); diff --git a/src/items/ItemStack.hpp b/src/items/ItemStack.hpp index c7ff7e0c..91f2d3e7 100644 --- a/src/items/ItemStack.hpp +++ b/src/items/ItemStack.hpp @@ -54,4 +54,8 @@ public: const dv::value& getFields() const { return fields; } + + bool hasFields() const { + return fields != nullptr; + } }; diff --git a/src/logic/scripting/lua/libs/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp index c0921f96..3e2d390e 100644 --- a/src/logic/scripting/lua/libs/libinventory.cpp +++ b/src/logic/scripting/lua/libs/libinventory.cpp @@ -208,7 +208,10 @@ static int l_get_all_data(lua::State* L, ItemStack& stack) { } static int l_has_data(lua::State* L, ItemStack& stack) { - auto key = lua::require_string(L, 3); + auto key = lua::tostring(L, 3); + if (key == nullptr) { + return lua::pushboolean(L, stack.hasFields()); + } return lua::pushboolean(L, stack.getField(key) != nullptr); }