From 5b154b57b3f4eefed1e7cd87e11fbbf3cdc6fe49 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 2 Jul 2024 23:36:38 +0300 Subject: [PATCH] implement actual items dropping --- res/content/base/scripts/components/drop.lua | 3 +- res/content/base/scripts/hud.lua | 29 +++++++++++--------- src/items/ItemStack.cpp | 3 ++ src/logic/scripting/lua/libinventory.cpp | 2 +- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 8b85b871..6093a747 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -5,6 +5,7 @@ local rig = entity.modeltree inair = true ready = false +local item = ARGS.item local rotation = mat4.rotate({0, 1, 0}, math.random() * 360) mat4.rotate(rotation, {1, 0, 0}, math.random() * 360, rotation) @@ -24,7 +25,7 @@ end function on_trigger_enter(index, oid) if ready and oid == 0 then entity:despawn() - inventory.add(player.get_inventory(oid), item.index("base:stone.item"), 1) + inventory.add(player.get_inventory(oid), item.id, item.count) audio.play_sound_2d("events/pickup", 0.5, 0.8+math.random()*0.4, "regular") end end diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 4ad429b5..4a3af9cb 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -3,19 +3,22 @@ local DROP_INIT_VEL = {0, 3, 0} function on_hud_open() input.add_callback("player.drop", function () - for i=1,80 do - local pid = hud.get_player() - local pvel = {player.get_vel(pid)} - local ppos = vec3.add({player.get_pos(pid)}, {0, 0.7, 0}) - local throw_force = vec3.mul(vec3.add(player.get_dir(pid), - { - (math.random() - 0.5) * 1, - (math.random() - 0.5) * 1, - (math.random() - 0.5) * 1 - }), DROP_FORCE) - - local drop = entities.spawn("base:drop", ppos) - drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) + local pid = hud.get_player() + local invid, slot = player.get_inventory(pid) + local itemid, itemcount = inventory.get(invid, slot) + if itemid == 0 then + return end + 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 = entities.spawn("base:drop", ppos, {item={ + id=itemid, + count=1 + }}) + drop.rigidbody:set_vel(vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL))) end) end diff --git a/src/items/ItemStack.cpp b/src/items/ItemStack.cpp index d60246c3..4c715064 100644 --- a/src/items/ItemStack.cpp +++ b/src/items/ItemStack.cpp @@ -12,6 +12,9 @@ ItemStack::ItemStack(itemid_t item, itemcount_t count) : item(item), count(count void ItemStack::set(const ItemStack& item) { this->item = item.item; this->count = item.count; + if (count == 0) { + this->item = 0; + } } bool ItemStack::accepts(const ItemStack& other) const { diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index 717583b6..67f4e6f1 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -32,7 +32,7 @@ static std::shared_ptr get_inventory(int64_t id, int arg) { } static void validate_slotid(int slotid, Inventory* inv) { - if (slotid < 0 || uint64_t(slotid) >= inv->size()) { + if (static_cast(slotid) >= inv->size()) { throw std::runtime_error("slot index is out of range [0..inventory.size(invid)]"); } }