From 9164edf9714d929a106e15627d950dc66f0eb351 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 23 Dec 2024 12:37:26 +0300 Subject: [PATCH] move 'player.pick' binding handler to Lua --- res/content/base/scripts/hud.lua | 26 ++++++++++++++++++++++++++ res/scripts/stdlib.lua | 1 - src/core_defs.hpp | 1 - src/items/Inventory.cpp | 7 +++++-- src/items/Inventory.hpp | 4 +++- src/logic/PlayerController.cpp | 26 -------------------------- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/res/content/base/scripts/hud.lua b/res/content/base/scripts/hud.lua index 31125631..a3df0849 100644 --- a/res/content/base/scripts/hud.lua +++ b/res/content/base/scripts/hud.lua @@ -23,4 +23,30 @@ function on_hud_open() local velocity = vec3.add(throw_force, vec3.add(pvel, DROP_INIT_VEL)) drop.rigidbody:set_vel(velocity) end) + input.add_callback("player.pick", function () + if hud.is_paused() or hud.is_inventory_open() then + return + end + local pid = hud.get_player() + local x, y, z = player.get_selected_block(pid) + if x == nil then + return + end + local id = block.get_picking_item(block.get(x, y, z)) + local inv, cur_slot = player.get_inventory(pid) + local slot = inventory.find_by_item(inv, id, 0, 9) + if slot then + player.set_selected_slot(pid, slot) + return + end + if not rules.get("allow-content-access") then + return + end + slot = inventory.find_by_item(inv, 0, 0, 9) + if slot then + cur_slot = slot + end + player.set_selected_slot(pid, cur_slot) + inventory.set(inv, cur_slot, id, 1) + end) end diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index aa56bc31..0a49ccd1 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -320,7 +320,6 @@ function __vc_on_hud_open() _rules.create("allow-content-access", hud._is_content_access(), function(value) hud._set_content_access(value) - input.set_enabled("player.pick", value) end) _rules.create("allow-flight", true, function(value) input.set_enabled("player.flight", value) diff --git a/src/core_defs.hpp b/src/core_defs.hpp index 38160dac..60f9ec79 100644 --- a/src/core_defs.hpp +++ b/src/core_defs.hpp @@ -26,7 +26,6 @@ inline const std::string BIND_PLAYER_FLIGHT = "player.flight"; inline const std::string BIND_PLAYER_ATTACK = "player.attack"; inline const std::string BIND_PLAYER_DESTROY = "player.destroy"; inline const std::string BIND_PLAYER_BUILD = "player.build"; -inline const std::string BIND_PLAYER_PICK = "player.pick"; inline const std::string BIND_PLAYER_FAST_INTERACTOIN = "player.fast_interaction"; inline const std::string BIND_HUD_INVENTORY = "hud.inventory"; diff --git a/src/items/Inventory.cpp b/src/items/Inventory.cpp index 00c5589e..9797a15e 100644 --- a/src/items/Inventory.cpp +++ b/src/items/Inventory.cpp @@ -23,10 +23,13 @@ size_t Inventory::findEmptySlot(size_t begin, size_t end) const { return npos; } -size_t Inventory::findSlotByItem(itemid_t id, size_t begin, size_t end) { +size_t Inventory::findSlotByItem( + itemid_t id, size_t begin, size_t end, size_t minCount +) { end = std::min(slots.size(), end); for (size_t i = begin; i < end; i++) { - if (slots[i].getItemId() == id) { + const auto& stack = slots[i]; + if (stack.getItemId() == id && stack.getCount() >= minCount) { return i; } } diff --git a/src/items/Inventory.hpp b/src/items/Inventory.hpp index 176ad7ee..49560523 100644 --- a/src/items/Inventory.hpp +++ b/src/items/Inventory.hpp @@ -22,7 +22,9 @@ public: ItemStack& getSlot(size_t index); size_t findEmptySlot(size_t begin = 0, size_t end = -1) const; - size_t findSlotByItem(itemid_t id, size_t begin = 0, size_t end = -1); + size_t findSlotByItem( + itemid_t id, size_t begin = 0, size_t end = -1, size_t minCount = 1 + ); inline size_t size() const { return slots.size(); diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 92face2b..f26b5395 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -341,28 +341,6 @@ static int determine_rotation( return 0; } -static void pick_block( - ContentIndices* indices, - const Block& block, - Player& player, - int x, - int y, - int z -) { - itemid_t id = block.rt.pickingItem; - auto inventory = player.getInventory(); - size_t slotid = inventory->findSlotByItem(id, 0, 10); - if (slotid == Inventory::npos) { - slotid = player.getChosenSlot(); - } else { - player.setChosenSlot(slotid); - } - ItemStack& stack = inventory->getSlot(slotid); - if (stack.getItemId() != id) { - stack.set(ItemStack(id, 1)); - } -} - voxel* PlayerController::updateSelection(float maxDistance) { auto indices = level.content->getIndices(); auto& chunks = *player.chunks; @@ -568,10 +546,6 @@ void PlayerController::updateInteraction(float delta) { if (def && rclick) { processRightClick(*def, target); } - if (Events::jactive(BIND_PLAYER_PICK)) { - auto coord = selection.actualPosition; - pick_block(indices, target, player, coord.x, coord.y, coord.z); - } } Player* PlayerController::getPlayer() {