Merge pull request #62 from oversword/issue-61

Perform warehouse push action on unpull, and stop removing things from the cache on unpull
This commit is contained in:
Joachim Stolberg 2020-12-18 12:33:30 +01:00 committed by GitHub
commit 4798ba472f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 45 deletions

@ -132,7 +132,7 @@ tubelib.register_node(NODE_NAME,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
meta:set_string("push_dir", wh.Turn180[side]) meta:set_string("push_dir", wh.Turn180[side])
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
item:set_count(num) item:set_count(num)
return tubelib.put_item(meta, "shift", item) return tubelib.put_item(meta, "shift", item)
@ -146,7 +146,14 @@ tubelib.register_node(NODE_NAME,
return tubelib.get_item(M(pos), "main") return tubelib.get_item(M(pos), "main")
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
return tubelib.put_item(M(pos), "main", item) local meta = M(pos)
local num = wh.inv_add_item(Box, meta, item)
if num > 0 then
-- this should never happen, but better safe than sorry
item:set_count(num)
return tubelib.put_item(meta, "shift", item)
end
return true
end, end,
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)

@ -132,7 +132,7 @@ tubelib.register_node(NODE_NAME,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
meta:set_string("push_dir", wh.Turn180[side]) meta:set_string("push_dir", wh.Turn180[side])
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
item:set_count(num) item:set_count(num)
return tubelib.put_item(meta, "shift", item) return tubelib.put_item(meta, "shift", item)
@ -146,7 +146,14 @@ tubelib.register_node(NODE_NAME,
return tubelib.get_item(M(pos), "main") return tubelib.get_item(M(pos), "main")
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
return tubelib.put_item(M(pos), "main", item) local meta = M(pos)
local num = wh.inv_add_item(Box, meta, item)
if num > 0 then
-- this should never happen, but better safe than sorry
item:set_count(num)
return tubelib.put_item(meta, "shift", item)
end
return true
end, end,
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)

@ -132,7 +132,7 @@ tubelib.register_node(NODE_NAME,
on_push_item = function(pos, side, item) on_push_item = function(pos, side, item)
local meta = M(pos) local meta = M(pos)
meta:set_string("push_dir", wh.Turn180[side]) meta:set_string("push_dir", wh.Turn180[side])
local num = wh.numbers_to_shift(Box, meta, item) local num = wh.inv_add_item(Box, meta, item)
if num > 0 then if num > 0 then
item:set_count(num) item:set_count(num)
return tubelib.put_item(meta, "shift", item) return tubelib.put_item(meta, "shift", item)
@ -146,7 +146,14 @@ tubelib.register_node(NODE_NAME,
return tubelib.get_item(M(pos), "main") return tubelib.get_item(M(pos), "main")
end, end,
on_unpull_item = function(pos, side, item) on_unpull_item = function(pos, side, item)
return tubelib.put_item(M(pos), "main", item) local meta = M(pos)
local num = wh.inv_add_item(Box, meta, item)
if num > 0 then
-- this should never happen, but better safe than sorry
item:set_count(num)
return tubelib.put_item(meta, "shift", item)
end
return true
end, end,
on_recv_message = function(pos, topic, payload) on_recv_message = function(pos, topic, payload)

@ -24,8 +24,6 @@ local COUNTDOWN_TICKS = 2
local CYCLE_TIME = 2 local CYCLE_TIME = 2
local Cache = {}
techpack_warehouse.Box = {} techpack_warehouse.Box = {}
techpack_warehouse.Turn180 = {F="B", L="R", B="F", R="L", U="D", D="U"} techpack_warehouse.Turn180 = {F="B", L="R", B="F", R="L", U="D", D="U"}
@ -169,39 +167,33 @@ function techpack_warehouse.Box:new(attr)
return o return o
end end
function techpack_warehouse.numbers_to_shift(self, meta, item) -- We can't use the standard function "inv:add_item()" because this function
-- check cache -- would not allow to add more than the default 99 items per stack.
local number = meta:get_string("tubelib_number") function techpack_warehouse.inv_add_item(self, meta, item)
local item_name = item:get_name()
if not Cache[number] then
local inv = meta:get_inventory()
Cache[number] = {}
for idx,items in ipairs(inv:get_list("filter")) do
Cache[number][idx] = items:get_name()
end
end
-- determine number to shift
local num_items = item:get_count() local num_items = item:get_count()
local inv_size = meta:get_int("inv_size") local item_name = item:get_name()
local inv = meta:get_inventory() local inv = meta:get_inventory()
local main_list = inv:get_list("main")
for idx, name in ipairs(Cache[number]) do for idx, stack in ipairs(main_list) do
if item_name == name then -- If item configured
local stack_size = inv:get_stack("main", idx):get_count() if item_name == inv:get_stack("filter", idx):get_name() then
if stack_size == self.inv_size then -- full? local stack_size = stack:get_count()
Cache[number][idx] = "" -- delete for searching -- If there is some space for further items
elseif (stack_size + num_items) > self.inv_size then -- limit will be reached? if stack_size < self.inv_size then
inv:set_stack("main", idx, ItemStack({name = item_name, count = self.inv_size})) local new_stack_size = math.min(self.inv_size, stack_size + num_items)
Cache[number][idx] = "" -- delete for searching main_list[idx] = ItemStack({name = item_name, count = new_stack_size})
-- search with the rest for further slots -- calc new number of items
num_items = num_items - (self.inv_size - stack_size) num_items = num_items - (new_stack_size - stack_size)
else -- If everything is distributed
inv:set_stack("main", idx, ItemStack({name = item_name, count = stack_size + num_items})) if num_items == 0 then
return 0 break
end
end end
end end
end end
inv:set_list("main", main_list)
return num_items return num_items
end end
@ -219,8 +211,6 @@ function techpack_warehouse.allow_metadata_inventory_put(self, pos, listname, in
if listname == "input" and item_name == stack:get_name() then if listname == "input" and item_name == stack:get_name() then
return math.min(stack:get_count(), self.inv_size - main_stack:get_count()) return math.min(stack:get_count(), self.inv_size - main_stack:get_count())
elseif listname == "filter" and item_name == main_stack:get_name() then elseif listname == "filter" and item_name == main_stack:get_name() then
local number = M(pos):get_string("tubelib_number")
Cache[number] = nil
return 1 return 1
elseif listname == "shift" then elseif listname == "shift" then
return stack:get_count() return stack:get_count()
@ -230,8 +220,6 @@ end
function techpack_warehouse.on_metadata_inventory_put(pos, listname, index, stack, player) function techpack_warehouse.on_metadata_inventory_put(pos, listname, index, stack, player)
if listname == "input" then if listname == "input" then
local number = M(pos):get_string("tubelib_number")
Cache[number] = nil
minetest.after(0.5, move_to_main, pos, index) minetest.after(0.5, move_to_main, pos, index)
end end
end end
@ -242,16 +230,12 @@ function techpack_warehouse.allow_metadata_inventory_take(pos, listname, index,
end end
local inv = M(pos):get_inventory() local inv = M(pos):get_inventory()
local main_stack = inv:get_stack("main", index) local main_stack = inv:get_stack("main", index)
local number = M(pos):get_string("tubelib_number")
if listname == "main" then if listname == "main" then
Cache[number] = nil
minetest.after(0.1, move_to_player_inv, player:get_player_name(), pos, index) minetest.after(0.1, move_to_player_inv, player:get_player_name(), pos, index)
return 0 return 0
elseif listname == "filter" and main_stack:is_empty() then elseif listname == "filter" and main_stack:is_empty() then
Cache[number] = nil
return 1 return 1
elseif listname == "shift" then elseif listname == "shift" then
Cache[number] = nil
return stack:get_count() return stack:get_count()
end end
return 0 return 0
@ -265,8 +249,6 @@ function techpack_warehouse.on_receive_fields(self, pos, formname, fields, playe
if minetest.is_protected(pos, player:get_player_name()) then if minetest.is_protected(pos, player:get_player_name()) then
return return
end end
local number = M(pos):get_string("tubelib_number")
Cache[number] = nil
self.State:state_button_event(pos, fields) self.State:state_button_event(pos, fields)
end end