Improve craftguide_craft() speed: Move stacks

This commit is contained in:
SmallJoker 2019-03-02 08:56:14 +01:00 committed by SmallJoker
parent 850ee9cbc0
commit bcb96d6caf

View File

@ -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 return
end end
else
-- abort if no item fits onto it -- Take matching group from the inventory (biggest stack)
if craft_item:get_count() >= craft_item:get_definition().stack_max then local main = inv:get_list("main")
return local max_found = 0
for i, stack in ipairs(main) do
if stack:get_count() > max_found and
get_item_group(stack:get_name(), group) ~= 0 then
needed_item = stack:get_name()
max_found = stack:get_count()
break
end 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
end end
needed_item = ciname end
need_group = false else
if not craft_item:is_empty() and
craft_item:get_name() ~= needed_item then
return -- Item must be identical
end end
end end
if need_group then needed_item = ItemStack(needed_item)
-- search an item of the specific group local to_take = math.min(amount, needed_item:get_stack_max())
for i,item in pairs(player_inv:get_list("main")) do to_take = to_take - craft_item:get_count()
if not item:is_empty() if to_take <= 0 then
and minetest.get_item_group(item:get_name(), need_group) > 0 then return -- Nothing to do
return i
end
end end
needed_item:set_count(to_take)
-- no index found local taken = inv:remove_item("main", needed_item)
return local leftover = taken:add_item(craft_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
-- 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
-- no index found
end
-- modifies the player inventory and returns the changed craft_item if possible
local function move_item(player_inv, craft_item, needed_item)
local stackid = item_fits(player_inv, craft_item, needed_item)
if not stackid then
return
end
local wanted_stack = player_inv:get_stack("main", stackid)
local taken_item = wanted_stack:take_item()
player_inv:set_stack("main", stackid, wanted_stack)
if not craft_item
or craft_item:is_empty() then
return taken_item
end
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,24 +511,12 @@ 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
--[[
if amount == "max" then
amount = 99 -- Arbitrary; need better way to do this.
else
amount = tonumber(amount)
end--]]
for iter = 1, amount do
local index = 1 local index = 1
for y = 1, 3 do for y = 1, 3 do
for x = 1, width do for x = 1, width do
@ -551,7 +524,7 @@ local function craftguide_craft(player, formname, fields)
if needed_item then if needed_item then
local craft_index = ((y - 1) * 3) + x local craft_index = ((y - 1) * 3) + x
local craft_item = craft_list[craft_index] local craft_item = craft_list[craft_index]
local newitem = move_item(player_inv, craft_item, needed_item) local newitem = craftguide_move_stacks(player_inv, craft_item, needed_item, amount)
if newitem then if newitem then
craft_list[craft_index] = newitem craft_list[craft_index] = newitem
end end
@ -559,7 +532,6 @@ local function craftguide_craft(player, formname, fields)
index = index + 1 index = index + 1
end end
end end
end
player_inv:set_list("craft", craft_list) player_inv:set_list("craft", craft_list)