Commit 50d88f67 authored by Fedja Beader's avatar Fedja Beader
Browse files

rewrite setup_move_to_hotbar to be lazier and more performant

parent 8f801626
......@@ -163,40 +163,59 @@ end
-- true on success, false on error
-- Attempts to ensure that item_name is present in hotbar. If there are actions
-- that will make this happen, then they will be appended to the work queue and
-- this function will return the ItemLoc of the hotbar slot. Else it will return nil.
function setup_move_to_hotbar(my_inv, item_name)
-- skip if already in hotbar
local index = find_item_index(my_inv["main"], item_name)
if index and index < 9 then
return ItemLoc:new(nil, "main", index)
end
-- search whole inventory for requested item
local loc = find_item(my_inv, item_name)
if not loc then
l:err("Unable to move item to hotbar, " .. item_name .. " not found in inventory!")
return false
elseif loc.list_name == "main" and 1 < loc.index and loc.index < 9 then
return true -- nothing to do
l:err("Unable to move " .. item_name .. " to hotbar. It was not found in your personal inventory!")
return nil
end
local wanted_stack = my_inv[loc.list_name][loc.index]
local from = { inv_loc = nil, list_name = loc.list_name, list_index = loc.index }
local hotbar = { inv_loc = nil, list_name = "main", list_index = 8 }
-- move existing item out of hotbar
local stack = my_inv["main"][8]
local to = nil
if stack:get_count() > 0 then -- find empty spot to move this item to
local empty_index = find_empty_index(my_inv["main"])
if not empty_index then
l:err("Unable to move item to hotbar, no empty spot left")
return false
local from_stack = my_inv[loc.list_name][loc.index]
local from_loc = ItemLoc:new(nil, loc.list_name, loc.index)
local hotbar_loc = ItemLoc:new(nil, "main", 8)
-- try to find empty slot in hotbar, prioritising main list.
local empty_index_in_main = find_item_index(my_inv["main"], "")
local empty_loc = nil
if not empty_index_in_main then
empty_loc = find_item(my_inv, "")
if not empty_loc then
l:err("Unable to move " .. item_name .. " to hotbar. There was no empty stack to move old hotbar contents out.")
return nil
end
to = { inv_loc = nil, list_name = "main", list_index = empty_index }
empty_loc = ItemLoc:new(nil, empty_loc.list_name, empty_loc.index)
elseif empty_index_in_main > 8 then
empty_loc = ItemLoc:new(nil, "main", empty_index_in_main)
else
-- empty slot is in hotbar, use it as slot for item_name
hotbar_loc = ItemLoc:new(nil, "main", empty_index_in_main)
end
queue:prepend(function() return queued_ensure_stack(hotbar, wanted_stack:get_name(), wanted_stack:get_count(), 5) end)
queue:prepend(function() return queued_move_stack(from, hotbar, wanted_stack:get_count()) end)
if stack:get_count() > 0 then
queue:prepend(function() return queued_ensure_stack(hotbar, "", 0, 5) end)
queue:prepend(function() return queued_move_stack(hotbar, to, stack:get_count()) end)
-- move existing item out of hotbar, if needed
local stack = my_inv[hotbar_loc.list_name][hotbar_loc.list_index]
if empty_loc then -- find empty spot to move this item to, if needed
queue:append(function() return queued_move_stack(hotbar_loc, empty_loc, stack:get_count()) end)
queue:append(function() return queued_ensure_stack(hotbar_loc, "", 0, 5) end)
end
return true
-- actually move items to hotbar
queue:append(function() return queued_move_stack(from_loc, hotbar_loc, from_stack:get_count()) end)
queue:append(function() return queued_ensure_stack(hotbar_loc, from_stack:get_name(), from_stack:get_count(), 5) end)
return hotbar_loc
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment