Splizard wrote:Although I would like to be able to open both gates if connected with one click,
any idea how I would go about this?
Somthing similar to how in mesecons, sending power to one item will activate all items attached to the first.
I've done it:
Code: Select all
-- Local constants
-- This _has_ to be set to 1
local GATE_OPENED = 2
-- This has to be != from GATE_OPENED and is coded on 4 bits
local GATE_CLOSED = 1
-- Local Functions
local on_gate_punched = function(pos, node, puncher)
if (node.name ~= 'gates:gate_closed')
and (node.name ~= 'gates:gate_opened') then
return
end
local state = node.param2
local toggle_gate = function(pos, state)
local nodename = ""
local param2 = ""
-- Switch the gate state when hit
if state == GATE_OPENED then
nodename = 'gates:gate_closed'
param2 = GATE_CLOSED
elseif state == GATE_CLOSED then
nodename = 'gates:gate_opened'
param2 = GATE_OPENED
else
print('Uninitialized node: ' .. state)
return
end
minetest.env:add_node(pos, {
name = nodename,
param2 = param2,
})
end
toggle_gate(pos, state)
--handle gates above this one
local lpos = {x=pos.x, y=pos.y, z=pos.z}
while true do
lpos.y = lpos.y+1
local lnode = minetest.env:get_node(lpos)
if (lnode.name ~= "gates:gate_closed" and lnode.name ~= "gates:gate_opened") then
break
end
toggle_gate(lpos, state)
end
--handle gates below this one
local lpos = {x=pos.x, y=pos.y, z=pos.z}
while true do
lpos.y = lpos.y-1
local lnode = minetest.env:get_node(lpos)
if (lnode.name ~= "gates:gate_closed" and lnode.name ~= "gates:gate_opened") then
break
end
toggle_gate(lpos,state)
end
end
local on_gate_placed = function(pos, node, placer)
if node.name ~= 'gates:gate_closed' then
return
end
minetest.env:add_node(pos, {
name = node.name,
param2 = GATE_OPENED,
})
end
-- Nodes
-- As long as param2 is set to 1 for open gates, it doesn't matter to
-- use drawtype = 'signlike'
minetest.register_node('gates:gate_opened', {
drawtype = 'plantlike',
visual_scale = 1.5,
tile_images = {'gate_open.png'},
sunlight_propagates = true,
paramtype = 'light',
walkable = false,
material = minetest.digprop_constanttime(1.0),
drop = "gates:gate_closed",
})
minetest.register_node('gates:gate_closed', {
tile_images = {'gate_top.png','gate_top.png','gate.png'},
sunlight_propagates = true,
paramtype = 'light',
walkable = true,
material = minetest.digprop_constanttime(1.0),
})
-- Crafts
minetest.register_craft({
output = '"gates:gate_closed" 2',
recipe = {
{"default:stick", "default:wood", "default:stick"},
{"default:stick", "default:wood", "default:stick"},
},
})
-- Change the gate state
minetest.register_on_punchnode(on_gate_punched)
-- Reset param2 for open gates
minetest.register_on_placenode(on_gate_placed)
-- Mesecon Stuff:
--mesecon:register_on_signal_on(on_gate_punched)
--mesecon:register_on_signal_off(on_gate_punched)
print("[Gates] Loaded!")
Punching a gate node now opens or closes all nodes above or below it. Also fixed a bug with the on_placenode routine.
Just place two or more gates an top of each other and punching any of them will toggle the gate state.
Speaking of mesecons, I've tried to add mesecon actuation (commented out at the bottom), but it fails because the name "mesecons" is nil. It's unusual since the hatches mod had worked just fine.