Biggest stacks first, smallest as late as possible

Split stacks ~evenly
Fast skip occupied slots
This commit is contained in:
SmallJoker 2019-03-02 09:27:23 +01:00 committed by SmallJoker
parent bcb96d6caf
commit e9b4b52dc1
1 changed files with 29 additions and 15 deletions

View File

@ -432,6 +432,10 @@ end
-- Takes any stack from "main" where the `amount` of `needed_item` may fit -- Takes any stack from "main" where the `amount` of `needed_item` may fit
-- into the given crafting stack (`craft_item`) -- into the given crafting stack (`craft_item`)
local function craftguide_move_stacks(inv, craft_item, needed_item, amount) local function craftguide_move_stacks(inv, craft_item, needed_item, amount)
if craft_item:get_count() >= amount then
return
end
local get_item_group = minetest.get_item_group local get_item_group = minetest.get_item_group
local group = needed_item:match("^group:(.+)") local group = needed_item:match("^group:(.+)")
if group then if group then
@ -453,7 +457,9 @@ local function craftguide_move_stacks(inv, craft_item, needed_item, amount)
get_item_group(stack:get_name(), group) ~= 0 then get_item_group(stack:get_name(), group) ~= 0 then
needed_item = stack:get_name() needed_item = stack:get_name()
max_found = stack:get_count() max_found = stack:get_count()
break if max_found >= amount then
break
end
end end
end end
end end
@ -476,8 +482,8 @@ local function craftguide_move_stacks(inv, craft_item, needed_item, amount)
local leftover = taken:add_item(craft_item) local leftover = taken:add_item(craft_item)
if not leftover:is_empty() then if not leftover:is_empty() then
-- Somehow failed to add the existing "craft" item. Undo the action. -- Somehow failed to add the existing "craft" item. Undo the action.
inv:add_item("main", taken) inv:add_item("main", leftover)
return -- No change return taken
end end
return taken return taken
end end
@ -517,21 +523,29 @@ local function craftguide_craft(player, formname, fields)
width = 3 width = 3
end end
local index = 1 -- To spread the items evenly
for y = 1, 3 do local STEPSIZE = math.ceil(math.sqrt(amount) / 5) * 5
for x = 1, width do local current_count = 0
local needed_item = needed[index] repeat
if needed_item then current_count = math.min(current_count + STEPSIZE, amount)
local craft_index = ((y - 1) * 3) + x local index = 1
local craft_item = craft_list[craft_index] for y = 1, 3 do
local newitem = craftguide_move_stacks(player_inv, craft_item, needed_item, amount) for x = 1, width do
if newitem then local needed_item = needed[index]
craft_list[craft_index] = newitem if needed_item then
local craft_index = ((y - 1) * 3) + x
local craft_item = craft_list[craft_index]
local newitem = craftguide_move_stacks(player_inv,
craft_item, needed_item, current_count)
if newitem then
craft_list[craft_index] = newitem
end
end end
index = index + 1
end end
index = index + 1
end end
end until current_count == amount
player_inv:set_list("craft", craft_list) player_inv:set_list("craft", craft_list)