Move double chest neighbor function to mcl_util

Wuzzy 2017-08-03 23:27:22 +02:00
parent 3b1c0765d6
commit a29091e23c
2 changed files with 49 additions and 50 deletions

@ -97,6 +97,36 @@ function mcl_util.rotate_axis(itemstack, placer, pointed_thing)
return itemstack
end
-- Returns position of the neighbor of a double chest node
-- or nil if node is invalid.
-- This function assumes that the large chest is actually intact
-- * pos: Position of the node to investigate
-- * param2: param2 of that node
-- * side: Which "half" the investigated node is. "left" or "right"
function mcl_util.get_double_container_neighbor_pos(pos, param2, side)
if side == "right" then
if param2 == 0 then
return {x=pos.x-1, y=pos.y, z=pos.z}
elseif param2 == 1 then
return {x=pos.x, y=pos.y, z=pos.z+1}
elseif param2 == 2 then
return {x=pos.x+1, y=pos.y, z=pos.z}
elseif param2 == 3 then
return {x=pos.x, y=pos.y, z=pos.z-1}
end
else
if param2 == 0 then
return {x=pos.x+1, y=pos.y, z=pos.z}
elseif param2 == 1 then
return {x=pos.x, y=pos.y, z=pos.z-1}
elseif param2 == 2 then
return {x=pos.x-1, y=pos.y, z=pos.z}
elseif param2 == 3 then
return {x=pos.x, y=pos.y, z=pos.z+1}
end
end
end
-- Moves a single item from one inventory to another.
--- source_inventory: Inventory to take the item from
--- source_list: List name of the source inventory from which to take the item
@ -147,7 +177,7 @@ function mcl_util.move_item_container(source_pos, source_list, source_stack_id,
-- Normalize double container by forcing to always use the left segment first
if dctype == 6 then
dpos = mcl_chests.get_large_chest_neighbor_pos(destination_pos, dnode.param2, "right")
dpos = mcl_util.get_double_container_neighbor_pos(destination_pos, dnode.param2, "right")
if not dpos then
return false
end
@ -198,7 +228,7 @@ function mcl_util.move_item_container(source_pos, source_list, source_stack_id,
-- Try transfer to neighbor node if transfer failed and double container
if not ok and dctype == 5 then
dpos = mcl_chests.get_large_chest_neighbor_pos(dpos, dnode.param2, "left")
dpos = mcl_util.get_double_container_neighbor_pos(dpos, dnode.param2, "left")
dmeta = minetest.get_meta(dpos)
dinv = dmeta:get_inventory()

@ -1,34 +1,3 @@
mcl_chests = {}
-- Returns position of the neighbor of a large chest node.
-- This function assumes that the large chest is actually intact
-- * pos: Position of the node to investigate
-- * param2: param2 of that node
-- * side: Which "half" the investigated node is. "right" or "left"
function mcl_chests.get_large_chest_neighbor_pos(pos, param2, side)
if side == "right" then
if param2 == 0 then
return {x=pos.x-1, y=pos.y, z=pos.z}
elseif param2 == 1 then
return {x=pos.x, y=pos.y, z=pos.z+1}
elseif param2 == 2 then
return {x=pos.x+1, y=pos.y, z=pos.z}
elseif param2 == 3 then
return {x=pos.x, y=pos.y, z=pos.z-1}
end
else
if param2 == 0 then
return {x=pos.x+1, y=pos.y, z=pos.z}
elseif param2 == 1 then
return {x=pos.x, y=pos.y, z=pos.z-1}
elseif param2 == 2 then
return {x=pos.x-1, y=pos.y, z=pos.z}
elseif param2 == 3 then
return {x=pos.x, y=pos.y, z=pos.z+1}
end
end
end
-- This is a helper function to register both chests and trapped chests. Trapped chests will make use of the additional parameters
local register_chest = function(basename, desc, longdesc, usagehelp, hidden, mesecons, on_rightclick_addendum, on_rightclick_addendum_left, on_rightclick_addendum_right, drop)
@ -77,13 +46,13 @@ minetest.register_node("mcl_chests:"..basename, {
-- BEGIN OF LISTRING WORKAROUND
inv:set_size("input", 1)
-- END OF LISTRING WORKAROUND
if minetest.get_node(mcl_chests.get_large_chest_neighbor_pos(pos, param2, "right")).name == "mcl_chests:"..basename then
if minetest.get_node(mcl_util.get_double_container_neighbor_pos(pos, param2, "right")).name == "mcl_chests:"..basename then
minetest.swap_node(pos, {name="mcl_chests:"..basename.."_right",param2=param2})
local p = mcl_chests.get_large_chest_neighbor_pos(pos, param2, "right")
local p = mcl_util.get_double_container_neighbor_pos(pos, param2, "right")
minetest.swap_node(p, { name = "mcl_chests:"..basename.."_left", param2 = param2 })
elseif minetest.get_node(mcl_chests.get_large_chest_neighbor_pos(pos, param2, "left")).name == "mcl_chests:"..basename then
elseif minetest.get_node(mcl_util.get_double_container_neighbor_pos(pos, param2, "left")).name == "mcl_chests:"..basename then
minetest.swap_node(pos, {name="mcl_chests:"..basename.."_left",param2=param2})
local p = mcl_chests.get_large_chest_neighbor_pos(pos, param2, "left")
local p = mcl_util.get_double_container_neighbor_pos(pos, param2, "left")
minetest.swap_node(p, { name = "mcl_chests:"..basename.."_right", param2 = param2 })
end
end,
@ -156,7 +125,7 @@ minetest.register_node("mcl_chests:"..basename.."_left", {
return
end
local param2 = n.param2
local p = mcl_chests.get_large_chest_neighbor_pos(pos, param2, "left")
local p = mcl_util.get_double_container_neighbor_pos(pos, param2, "left")
if not p or minetest.get_node(p).name ~= "mcl_chests:"..basename.."_right" then
return
end
@ -183,7 +152,7 @@ minetest.register_node("mcl_chests:"..basename.."_left", {
if inv:room_for_item("main", stack) then
return -1
else
local other_pos = mcl_chests.get_large_chest_neighbor_pos(pos, minetest.get_node(pos).param2, "left")
local other_pos = mcl_util.get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, "left")
local other_inv = minetest.get_inventory({type="node", pos=other_pos})
if other_inv:room_for_item("main", stack) then
return -1
@ -208,7 +177,7 @@ minetest.register_node("mcl_chests:"..basename.."_left", {
local inv = minetest.get_inventory({type="node", pos=pos})
local leftover = inv:add_item("main", stack)
if not leftover:is_empty() then
local other_pos = mcl_chests.get_large_chest_neighbor_pos(pos, minetest.get_node(pos).param2, "left")
local other_pos = mcl_util.get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, "left")
local other_inv = minetest.get_inventory({type="node", pos=other_pos})
other_inv:add_item("main", leftover)
end
@ -223,7 +192,7 @@ minetest.register_node("mcl_chests:"..basename.."_left", {
_mcl_hardness = 2.5,
on_rightclick = function(pos, node, clicker)
local pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "left")
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "left")
minetest.show_formspec(clicker:get_player_name(),
"mcl_chests:"..basename.."_"..pos.x.."_"..pos.y.."_"..pos.z,
@ -264,7 +233,7 @@ minetest.register_node("mcl_chests:"..basename.."_right", {
return
end
local param2 = n.param2
local p = mcl_chests.get_large_chest_neighbor_pos(pos, param2, "right")
local p = mcl_util.get_double_container_neighbor_pos(pos, param2, "right")
if not p or minetest.get_node(p).name ~= "mcl_chests:"..basename.."_left" then
return
end
@ -287,7 +256,7 @@ minetest.register_node("mcl_chests:"..basename.."_right", {
-- BEGIN OF LISTRING WORKAROUND
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if listname == "input" then
local other_pos = mcl_chests.get_large_chest_neighbor_pos(pos, minetest.get_node(pos).param2, "right")
local other_pos = mcl_util.get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, "right")
local other_inv = minetest.get_inventory({type="node", pos=other_pos})
if other_inv:room_for_item("main", stack) then
return -1
@ -313,7 +282,7 @@ minetest.register_node("mcl_chests:"..basename.."_right", {
" moves stuff to chest at "..minetest.pos_to_string(pos))
-- BEGIN OF LISTRING WORKAROUND
if listname == "input" then
local other_pos = mcl_chests.get_large_chest_neighbor_pos(pos, minetest.get_node(pos).param2, "right")
local other_pos = mcl_util.get_double_container_neighbor_pos(pos, minetest.get_node(pos).param2, "right")
local other_inv = minetest.get_inventory({type="node", pos=other_pos})
local leftover = other_inv:add_item("main", stack)
if not leftover:is_empty() then
@ -331,7 +300,7 @@ minetest.register_node("mcl_chests:"..basename.."_right", {
_mcl_hardness = 2.5,
on_rightclick = function(pos, node, clicker)
local pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "right")
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "right")
minetest.show_formspec(clicker:get_player_name(),
"mcl_chests:"..basename.."_"..pos.x.."_"..pos.y.."_"..pos.z,
@ -403,12 +372,12 @@ register_chest("trapped_chest",
minetest.swap_node(pos, {name="mcl_chests:trapped_chest_on_left", param2 = node.param2})
mesecon:receptor_on(pos, trapped_chest_mesecons_rules)
local pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "left")
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "left")
minetest.swap_node(pos_other, {name="mcl_chests:trapped_chest_on_right", param2 = node.param2})
mesecon:receptor_on(pos_other, trapped_chest_mesecons_rules)
end,
function(pos, node, clicker)
local pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "right")
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "right")
-- Save number of players in left part of the chest only
local meta = minetest.get_meta(pos_other)
@ -441,7 +410,7 @@ register_chest("trapped_chest_on",
meta:set_int("players", players)
end,
function(pos, node, clicker)
local pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "right")
local pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "right")
local meta = minetest.get_meta(pos_other)
local players = meta:get_int("players")
players = players + 1
@ -464,7 +433,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
players = meta:get_int("players")
players = players - 1
elseif node.name == "mcl_chests:trapped_chest_on_right" then
pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "right")
pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "right")
meta = minetest.get_meta(pos_other)
players = meta:get_int("players")
players = players - 1
@ -484,7 +453,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
minetest.swap_node(pos, {name="mcl_chests:trapped_chest_left", param2 = node.param2})
mesecon:receptor_off(pos, trapped_chest_mesecons_rules)
pos_other = mcl_chests.get_large_chest_neighbor_pos(pos, node.param2, "left")
pos_other = mcl_util.get_double_container_neighbor_pos(pos, node.param2, "left")
minetest.swap_node(pos_other, {name="mcl_chests:trapped_chest_right", param2 = node.param2})
mesecon:receptor_off(pos_other, trapped_chest_mesecons_rules)
else