Improve craftguide_craft() speed: Move stacks
This commit is contained in:
parent
850ee9cbc0
commit
bcb96d6caf
152
register.lua
152
register.lua
@ -429,78 +429,57 @@ local function craftguide_giveme(player, formname, fields)
|
|||||||
player_inv:add_item("main", {name = output, count = amount})
|
player_inv:add_item("main", {name = output, count = amount})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- tells if an item can be moved and returns an index if so
|
-- Takes any stack from "main" where the `amount` of `needed_item` may fit
|
||||||
local function item_fits(player_inv, craft_item, needed_item)
|
-- into the given crafting stack (`craft_item`)
|
||||||
local need_group = string.sub(needed_item, 1, 6) == "group:"
|
local function craftguide_move_stacks(inv, craft_item, needed_item, amount)
|
||||||
if need_group then
|
local get_item_group = minetest.get_item_group
|
||||||
need_group = string.sub(needed_item, 7)
|
local group = needed_item:match("^group:(.+)")
|
||||||
end
|
if group then
|
||||||
if craft_item
|
if not craft_item:is_empty() then
|
||||||
and not craft_item:is_empty() then
|
-- Source item must be the same to fill
|
||||||
local ciname = craft_item:get_name()
|
if get_item_group(craft_item:get_name(), group) ~= 0 then
|
||||||
|
needed_item = craft_item:get_name()
|
||||||
-- abort if the item there isn't usable
|
else
|
||||||
if ciname ~= needed_item
|
-- TODO: Maybe swap unmatching "craft" items
|
||||||
and not need_group then
|
-- !! Would conflict with recursive function call
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- abort if no item fits onto it
|
|
||||||
if craft_item:get_count() >= craft_item:get_definition().stack_max then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- use the item there if it's in the right group and a group item is needed
|
|
||||||
if need_group then
|
|
||||||
if minetest.get_item_group(ciname, need_group) == 0 then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
needed_item = ciname
|
else
|
||||||
need_group = false
|
-- Take matching group from the inventory (biggest stack)
|
||||||
end
|
local main = inv:get_list("main")
|
||||||
end
|
local max_found = 0
|
||||||
|
for i, stack in ipairs(main) do
|
||||||
if need_group then
|
if stack:get_count() > max_found and
|
||||||
-- search an item of the specific group
|
get_item_group(stack:get_name(), group) ~= 0 then
|
||||||
for i,item in pairs(player_inv:get_list("main")) do
|
needed_item = stack:get_name()
|
||||||
if not item:is_empty()
|
max_found = stack:get_count()
|
||||||
and minetest.get_item_group(item:get_name(), need_group) > 0 then
|
break
|
||||||
return i
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
else
|
||||||
-- no index found
|
if not craft_item:is_empty() and
|
||||||
return
|
craft_item:get_name() ~= needed_item then
|
||||||
end
|
return -- Item must be identical
|
||||||
|
|
||||||
-- search an item with a the name needed_item
|
|
||||||
for i,item in pairs(player_inv:get_list("main")) do
|
|
||||||
if not item:is_empty()
|
|
||||||
and item:get_name() == needed_item then
|
|
||||||
return i
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- no index found
|
needed_item = ItemStack(needed_item)
|
||||||
end
|
local to_take = math.min(amount, needed_item:get_stack_max())
|
||||||
|
to_take = to_take - craft_item:get_count()
|
||||||
-- modifies the player inventory and returns the changed craft_item if possible
|
if to_take <= 0 then
|
||||||
local function move_item(player_inv, craft_item, needed_item)
|
return -- Nothing to do
|
||||||
local stackid = item_fits(player_inv, craft_item, needed_item)
|
|
||||||
if not stackid then
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
local wanted_stack = player_inv:get_stack("main", stackid)
|
needed_item:set_count(to_take)
|
||||||
local taken_item = wanted_stack:take_item()
|
|
||||||
player_inv:set_stack("main", stackid, wanted_stack)
|
|
||||||
|
|
||||||
if not craft_item
|
local taken = inv:remove_item("main", needed_item)
|
||||||
or craft_item:is_empty() then
|
local leftover = taken:add_item(craft_item)
|
||||||
return taken_item
|
if not leftover:is_empty() then
|
||||||
|
-- Somehow failed to add the existing "craft" item. Undo the action.
|
||||||
|
inv:add_item("main", taken)
|
||||||
|
return -- No change
|
||||||
end
|
end
|
||||||
|
return taken
|
||||||
craft_item:add_item(taken_item)
|
|
||||||
return craft_item
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function craftguide_craft(player, formname, fields)
|
local function craftguide_craft(player, formname, fields)
|
||||||
@ -510,15 +489,21 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
if amount then break end
|
if amount then break end
|
||||||
end
|
end
|
||||||
if not amount then return end
|
if not amount then return end
|
||||||
|
|
||||||
|
amount = tonumber(amount) or 99 -- fallback for "all"
|
||||||
|
if amount <= 0 or amount > 99 then return end
|
||||||
|
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
|
|
||||||
local output = unified_inventory.current_item[player_name]
|
local output = unified_inventory.current_item[player_name] or ""
|
||||||
if (not output) or (output == "") then return end
|
if output == "" then return end
|
||||||
|
|
||||||
local player_inv = player:get_inventory()
|
local player_inv = player:get_inventory()
|
||||||
|
local craft_list = player_inv:get_list("craft")
|
||||||
|
|
||||||
local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][output]
|
local crafts = unified_inventory.crafts_for[
|
||||||
if (not crafts) or (#crafts == 0) then return end
|
unified_inventory.current_craft_direction[player_name]][output] or {}
|
||||||
|
if #crafts == 0 then return end
|
||||||
|
|
||||||
local alternate = unified_inventory.alternate[player_name]
|
local alternate = unified_inventory.alternate[player_name]
|
||||||
|
|
||||||
@ -526,38 +511,25 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
if craft.width > 3 then return end
|
if craft.width > 3 then return end
|
||||||
|
|
||||||
local needed = craft.items
|
local needed = craft.items
|
||||||
|
|
||||||
local craft_list = player_inv:get_list("craft")
|
|
||||||
|
|
||||||
local width = craft.width
|
local width = craft.width
|
||||||
if width == 0 then
|
if width == 0 then
|
||||||
-- Shapeless recipe
|
-- Shapeless recipe
|
||||||
width = 3
|
width = 3
|
||||||
end
|
end
|
||||||
|
|
||||||
amount = tonumber(amount) or 99
|
local index = 1
|
||||||
--[[
|
for y = 1, 3 do
|
||||||
if amount == "max" then
|
for x = 1, width do
|
||||||
amount = 99 -- Arbitrary; need better way to do this.
|
local needed_item = needed[index]
|
||||||
else
|
if needed_item then
|
||||||
amount = tonumber(amount)
|
local craft_index = ((y - 1) * 3) + x
|
||||||
end--]]
|
local craft_item = craft_list[craft_index]
|
||||||
|
local newitem = craftguide_move_stacks(player_inv, craft_item, needed_item, amount)
|
||||||
for iter = 1, amount do
|
if newitem then
|
||||||
local index = 1
|
craft_list[craft_index] = newitem
|
||||||
for y = 1, 3 do
|
|
||||||
for x = 1, width do
|
|
||||||
local needed_item = needed[index]
|
|
||||||
if needed_item then
|
|
||||||
local craft_index = ((y - 1) * 3) + x
|
|
||||||
local craft_item = craft_list[craft_index]
|
|
||||||
local newitem = move_item(player_inv, craft_item, needed_item)
|
|
||||||
if newitem then
|
|
||||||
craft_list[craft_index] = newitem
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
index = index + 1
|
|
||||||
end
|
end
|
||||||
|
index = index + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user