Biggest stacks first, smallest as late as possible
Split stacks ~evenly Fast skip occupied slots
This commit is contained in:
parent
bcb96d6caf
commit
e9b4b52dc1
20
register.lua
20
register.lua
@ -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,10 +457,12 @@ 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()
|
||||||
|
if max_found >= amount then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
else
|
else
|
||||||
if not craft_item:is_empty() and
|
if not craft_item:is_empty() and
|
||||||
craft_item:get_name() ~= needed_item then
|
craft_item:get_name() ~= needed_item then
|
||||||
@ -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,6 +523,11 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
width = 3
|
width = 3
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- To spread the items evenly
|
||||||
|
local STEPSIZE = math.ceil(math.sqrt(amount) / 5) * 5
|
||||||
|
local current_count = 0
|
||||||
|
repeat
|
||||||
|
current_count = math.min(current_count + STEPSIZE, amount)
|
||||||
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
|
||||||
@ -524,7 +535,9 @@ 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 = craftguide_move_stacks(player_inv, craft_item, needed_item, amount)
|
local newitem = craftguide_move_stacks(player_inv,
|
||||||
|
craft_item, needed_item, current_count)
|
||||||
|
|
||||||
if newitem then
|
if newitem then
|
||||||
craft_list[craft_index] = newitem
|
craft_list[craft_index] = newitem
|
||||||
end
|
end
|
||||||
@ -532,6 +545,7 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
index = index + 1
|
index = index + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
until current_count == amount
|
||||||
|
|
||||||
player_inv:set_list("craft", craft_list)
|
player_inv:set_list("craft", craft_list)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user