test / test (push) Successful in 55s
- New strata_crafting/inventory.lua. Smart tool select: when a dig starts, the inventory item that digs the node fastest (core.get_dig_params) is swapped into the wielded slot, and swapped back 0.4 s after dig is released if both slots are unchanged. The server can't change the selected hotbar slot, so it swaps stacks. On by default; "Auto-select tools" checkbox on the inventory screen. - Quick stack: backpack items (slots 11-40) go into chests within 6 nodes that already hold the same item. Sort: merges and orders the backpack (tools, blocks, other items; by name). The hotbar is kept. - Buttons and checkbox added to the inventory/crafting formspec. - 5 selftest cases in tests/inventory.lua. Closes #19 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
8.2 KiB
Lua
251 lines
8.2 KiB
Lua
-- Strata inventory helpers: smart tool select, quick stack and sort.
|
|
--
|
|
-- * Smart tool select (on by default, a checkbox on the inventory screen):
|
|
-- when a player starts digging a node, the best tool for it in their
|
|
-- inventory is swapped into the wielded slot. Once dig has been released
|
|
-- for SWAP_BACK_DELAY the two slots are swapped back, if they still hold
|
|
-- the same items. The server can't change the selected hotbar slot, so it
|
|
-- swaps the stacks instead.
|
|
-- * Quick stack moves items from the backpack (slots after the hotbar) into
|
|
-- chests within QUICK_STACK_RANGE that already hold the same item.
|
|
-- * Sort merges and orders the backpack; the hotbar stays as it is.
|
|
|
|
local S = strata.S
|
|
|
|
local HOTBAR_SIZE = 10
|
|
local QUICK_STACK_RANGE = 6 -- nodes from the avatar, on the plane
|
|
local SWAP_BACK_DELAY = 0.4 -- seconds after dig is released
|
|
local SMART_META = "strata:smart_tool"
|
|
local NO_DIG = math.huge
|
|
|
|
strata.inventory = {
|
|
HOTBAR_SIZE = HOTBAR_SIZE,
|
|
QUICK_STACK_RANGE = QUICK_STACK_RANGE,
|
|
}
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Smart tool select
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Seconds it takes `stack` to dig `nodename`, or math.huge if it can't.
|
|
local function dig_time(stack, nodename)
|
|
local ndef = core.registered_nodes[nodename]
|
|
if not ndef or ndef.diggable == false then
|
|
return NO_DIG
|
|
end
|
|
local caps = ItemStack(stack):get_tool_capabilities()
|
|
local params = core.get_dig_params(ndef.groups or {}, caps)
|
|
return params.diggable and params.time or NO_DIG
|
|
end
|
|
strata.dig_time = dig_time
|
|
|
|
--- Index of the item in list "main" that digs nodename fastest, and its
|
|
-- time. Ties keep the lowest index; nil if nothing can dig it.
|
|
function strata.best_tool(inv, nodename)
|
|
local best, best_t
|
|
for i, stack in ipairs(inv:get_list("main")) do
|
|
if not stack:is_empty() then
|
|
local t = dig_time(stack, nodename)
|
|
if t < NO_DIG and (not best_t or t < best_t) then
|
|
best, best_t = i, t
|
|
end
|
|
end
|
|
end
|
|
return best, best_t
|
|
end
|
|
|
|
--- If a better tool for nodename is in the inventory, swap it into slot
|
|
-- `wield`. Returns a record for strata.smart_tool_restore, or nil.
|
|
function strata.smart_tool_swap(inv, wield, nodename)
|
|
local best, best_t = strata.best_tool(inv, nodename)
|
|
local current = inv:get_stack("main", wield)
|
|
if not best or best == wield or best_t >= dig_time(current, nodename) then
|
|
return nil
|
|
end
|
|
local tool = inv:get_stack("main", best)
|
|
inv:set_stack("main", wield, tool)
|
|
inv:set_stack("main", best, current)
|
|
return {wield = wield, other = best, tool = tool:get_name(), item = current:get_name()}
|
|
end
|
|
|
|
--- Undo a smart_tool_swap, if both slots still hold what it left there.
|
|
function strata.smart_tool_restore(inv, rec)
|
|
local a, b = inv:get_stack("main", rec.wield), inv:get_stack("main", rec.other)
|
|
if a:get_name() ~= rec.tool or b:get_name() ~= rec.item then
|
|
return false
|
|
end
|
|
inv:set_stack("main", rec.wield, b)
|
|
inv:set_stack("main", rec.other, a)
|
|
return true
|
|
end
|
|
|
|
function strata.smart_tool_enabled(player)
|
|
return player:get_meta():get_string(SMART_META) ~= "off"
|
|
end
|
|
|
|
local swaps = {} -- player name -> {rec = record, idle = seconds since dig was held}
|
|
|
|
core.register_on_punchnode(function(pos, node, puncher)
|
|
if not puncher or not puncher:is_player() or not strata.smart_tool_enabled(puncher) then
|
|
return
|
|
end
|
|
local name = puncher:get_player_name()
|
|
if swaps[name] then
|
|
return -- already holding a swapped tool
|
|
end
|
|
local rec = strata.smart_tool_swap(puncher:get_inventory(), puncher:get_wield_index(), node.name)
|
|
if rec then
|
|
swaps[name] = {rec = rec, idle = 0}
|
|
end
|
|
end)
|
|
|
|
core.register_globalstep(function(dtime)
|
|
for name, s in pairs(swaps) do
|
|
local player = core.get_player_by_name(name)
|
|
if not player then
|
|
swaps[name] = nil
|
|
elseif player:get_player_control().dig then
|
|
s.idle = 0
|
|
else
|
|
s.idle = s.idle + dtime
|
|
if s.idle >= SWAP_BACK_DELAY then
|
|
strata.smart_tool_restore(player:get_inventory(), s.rec)
|
|
swaps[name] = nil
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
|
|
core.register_on_leaveplayer(function(player)
|
|
local name = player:get_player_name()
|
|
if swaps[name] then
|
|
strata.smart_tool_restore(player:get_inventory(), swaps[name].rec)
|
|
swaps[name] = nil
|
|
end
|
|
end)
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Quick stack and sort
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Chest positions on the plane within QUICK_STACK_RANGE of pos, nearest first.
|
|
function strata.chests_near(pos)
|
|
local c = vector.round(pos)
|
|
local r = QUICK_STACK_RANGE
|
|
local found = core.find_nodes_in_area(
|
|
vector.new(c.x - r, c.y - r, strata.PLANE_Z), vector.new(c.x + r, c.y + r, strata.PLANE_Z), "group:chest")
|
|
table.sort(found, function(a, b)
|
|
return vector.distance(a, pos) < vector.distance(b, pos)
|
|
end)
|
|
return found
|
|
end
|
|
|
|
--- Move backpack items of inv into chests near pos that already hold the
|
|
-- same item. Returns the number of items moved.
|
|
function strata.quick_stack(inv, pos)
|
|
local moved = 0
|
|
local chests = {}
|
|
for _, cpos in ipairs(strata.chests_near(pos)) do
|
|
chests[#chests + 1] = core.get_meta(cpos):get_inventory()
|
|
end
|
|
for i = HOTBAR_SIZE + 1, inv:get_size("main") do
|
|
local stack = inv:get_stack("main", i)
|
|
if not stack:is_empty() then
|
|
local probe = ItemStack(stack:get_name())
|
|
for _, cinv in ipairs(chests) do
|
|
if cinv:contains_item("main", probe) then
|
|
local count = stack:get_count()
|
|
stack = cinv:add_item("main", stack)
|
|
moved = moved + count - stack:get_count()
|
|
if stack:is_empty() then
|
|
break
|
|
end
|
|
end
|
|
end
|
|
inv:set_stack("main", i, stack)
|
|
end
|
|
end
|
|
return moved
|
|
end
|
|
|
|
-- Sort order: tools, then placeable nodes, then other items; by name.
|
|
local function category(name)
|
|
local def = core.registered_items[name]
|
|
if not def or def.type == "tool" then
|
|
return 1
|
|
elseif def.type == "node" then
|
|
return 2
|
|
end
|
|
return 3
|
|
end
|
|
|
|
--- Merge and sort the backpack part of list "main". The hotbar is kept.
|
|
function strata.sort_inventory(inv)
|
|
local size = inv:get_size("main")
|
|
local stacks = {}
|
|
for i = HOTBAR_SIZE + 1, size do
|
|
local stack = inv:get_stack("main", i)
|
|
if not stack:is_empty() then
|
|
stacks[#stacks + 1] = stack
|
|
end
|
|
end
|
|
table.sort(stacks, function(a, b)
|
|
local ca, cb = category(a:get_name()), category(b:get_name())
|
|
if ca ~= cb then
|
|
return ca < cb
|
|
elseif a:get_name() ~= b:get_name() then
|
|
return a:get_name() < b:get_name()
|
|
end
|
|
return a:get_count() > b:get_count()
|
|
end)
|
|
-- merge partial stacks of the same item into the slots before them
|
|
local out = {}
|
|
for _, stack in ipairs(stacks) do
|
|
local last = out[#out]
|
|
if last and last:get_name() == stack:get_name() then
|
|
stack = last:add_item(stack)
|
|
end
|
|
if not stack:is_empty() then
|
|
out[#out + 1] = stack
|
|
end
|
|
end
|
|
for i = HOTBAR_SIZE + 1, size do
|
|
inv:set_stack("main", i, out[i - HOTBAR_SIZE] or ItemStack())
|
|
end
|
|
end
|
|
|
|
--------------------------------------------------------------------------
|
|
-- Inventory screen controls (added by strata_crafting's formspec)
|
|
--------------------------------------------------------------------------
|
|
|
|
--- Formspec elements for the controls row at height y.
|
|
function strata.inventory_controls(player, y)
|
|
return table.concat({
|
|
("checkbox[3,%.2f;inv_smart;%s;%s]"):format(y + 0.3, core.formspec_escape(S("Auto-select tools")),
|
|
tostring(strata.smart_tool_enabled(player))),
|
|
("button[8.25,%.2f;2.25,0.6;inv_stack;%s]"):format(y, core.formspec_escape(S("Quick stack"))),
|
|
("tooltip[inv_stack;%s]"):format(core.formspec_escape(
|
|
S("Put backpack items into nearby chests that already hold them"))),
|
|
("button[10.75,%.2f;2,0.6;inv_sort;%s]"):format(y, core.formspec_escape(S("Sort"))),
|
|
("tooltip[inv_sort;%s]"):format(core.formspec_escape(S("Sort the backpack (the hotbar stays)"))),
|
|
})
|
|
end
|
|
|
|
--- Handle the controls' fields. Returns true if one was used.
|
|
function strata.inventory_fields(player, fields)
|
|
if fields.inv_smart then
|
|
player:get_meta():set_string(SMART_META, fields.inv_smart == "true" and "" or "off")
|
|
return true
|
|
elseif fields.inv_stack then
|
|
local pos = strata.get_avatar_pos(player:get_player_name())
|
|
if pos then
|
|
strata.quick_stack(player:get_inventory(), pos)
|
|
end
|
|
return true
|
|
elseif fields.inv_sort then
|
|
strata.sort_inventory(player:get_inventory())
|
|
return true
|
|
end
|
|
return false
|
|
end
|