mesecons/mesecons_doors/init.lua
Zefram 77b8f6514a Better mesecon-enabled doors
The mesecons_compatibility doors erred in making steel doors, which
are meant to be locked, openable by anyone using a mesecon signal.
They also didn't handle mirror-paired doors, and nastily duplicated
lots of the standard door code rather than using it and adding to it.
Replace mesecons_compatibility with a new system, in which standard
doors are left alone and new types of door are added that have mesecon
behaviour.  The new door types are each available in both wood and steel,
using the standard door textures.

The mesecon-operated doors open and close according to the mesecon
signal they receive: open when the signal is on and closed when off.
Unlike the old mesecons_compatibility doors, which only accepted the
signal to the bottom half, these accept the signal to either half of
the door.  A convenient kind of control therefore is a wall-mounted
button just above the doorway: the signal flows diagonally down to the
top half of the door.  The door cannot be operated manually.

The mesecon-signalling doors are opened and closed manually, and generate
a mesecon signal indicating whether they're open, on when open and off
when closed.  Thus opening the door can trigger automatic activity.
Pairing a mesecon-signalling door with a mesecon-operated door results
in a door pair where right-clicking on one door operates both.

By making use of the pairing behaviour built into the standard doors mod,
which is inherited by the mesecon doors, and placing doors from sideways
angles, it is possible to effectively get mesecon doors with the opposite
signal sense.  For example, a mesecon-signalling door that sends an on
signal when closed, turning the signal off when opened.
2014-05-04 16:47:47 +02:00

131 lines
5.6 KiB
Lua

local other_state_node = {}
for _, material in ipairs({
{ id = "wood", desc = "Wooden", color = "brown" },
{ id = "steel", desc = "Steel", color = "grey" },
}) do
doors:register_door("mesecons_doors:op_door_"..material.id, {
description = "Mesecon-operated "..material.desc.." Door",
inventory_image = minetest.registered_items["doors:door_"..material.id].inventory_image,
groups = minetest.registered_nodes["doors:door_"..material.id.."_b_1"].groups,
tiles_bottom = {"door_"..material.id.."_b.png", "door_"..material.color..".png"},
tiles_top = {"door_"..material.id.."_a.png", "door_"..material.color..".png"},
})
local groups_plus_mesecon = { mesecon = 2 }
for k, v in pairs(minetest.registered_nodes["doors:door_"..material.id.."_b_1"].groups) do
groups_plus_mesecon[k] = v
end
doors:register_door("mesecons_doors:sig_door_"..material.id, {
description = "Mesecon-signalling "..material.desc.." Door",
inventory_image = minetest.registered_items["doors:door_"..material.id].inventory_image,
groups = groups_plus_mesecon,
tiles_bottom = {"door_"..material.id.."_b.png", "door_"..material.color..".png"},
tiles_top = {"door_"..material.id.."_a.png", "door_"..material.color..".png"},
})
for _, thishalf in ipairs({ "t", "b" }) do
local otherhalf = thishalf == "t" and "b" or "t"
local otherdir = thishalf == "t" and -1 or 1
for orientation = 1, 2 do
local thissuffix = material.id.."_"..thishalf.."_"..orientation
local othersuffix = material.id.."_"..otherhalf.."_"..orientation
local thisopname = "mesecons_doors:op_door_"..thissuffix
local otheropname = "mesecons_doors:op_door_"..othersuffix
local oponr = minetest.registered_nodes[thisopname].on_rightclick
local function handle_mesecon_signal (thispos, thisnode, signal)
local thismeta = minetest.get_meta(thispos)
if signal == thismeta:get_int("sigstate") then return end
thismeta:set_int("sigstate", signal)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
if minetest.get_node(otherpos).name ~= otheropname then return end
local othermeta = minetest.get_meta(otherpos)
local newdoorstate = math.max(thismeta:get_int("sigstate"), othermeta:get_int("sigstate"))
if newdoorstate == thismeta:get_int("doorstate") then return end
oponr(thispos, thisnode, nil)
thismeta:set_int("doorstate", newdoorstate)
othermeta:set_int("doorstate", newdoorstate)
end
minetest.override_item(thisopname, {
on_construct = function (pos)
if mesecon:is_powered(pos) then
local node = minetest.get_node(pos)
mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on", 1)
mesecon:activate(pos, node, nil, 1)
end
end,
on_rightclick = function (pos, node, clicker) end,
mesecons = {
effector = {
action_on = function (pos, node)
handle_mesecon_signal(pos, node, 1)
end,
action_off = function (pos, node)
handle_mesecon_signal(pos, node, 0)
end,
},
},
})
local thissigname = "mesecons_doors:sig_door_"..thissuffix
local othersigname = "mesecons_doors:sig_door_"..othersuffix
local sigonr = minetest.registered_nodes[thissigname].on_rightclick
minetest.override_item(thissigname, {
on_rightclick = function (thispos, thisnode, clicker)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
print("open: otherpos.name="..minetest.get_node(otherpos).name..", othersigname="..othersigname)
if minetest.get_node(otherpos).name ~= othersigname then return end
sigonr(thispos, thisnode, clicker)
for _, pos in ipairs({ thispos, otherpos }) do
local node = minetest.get_node(pos)
node.name = other_state_node[node.name]
minetest.swap_node(pos, node)
mesecon:receptor_on(pos)
end
end,
mesecons = { receptor = { state = mesecon.state.off } },
})
other_state_node[thissigname] = thissigname.."_on"
local ondef = {}
for k, v in pairs(minetest.registered_nodes[thissigname]) do
ondef[k] = v
end
ondef.on_rightclick = function (thispos, thisnode, clicker)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
print("close: otherpos.name="..minetest.get_node(otherpos).name..", othersigname="..othersigname)
if minetest.get_node(otherpos).name ~= othersigname.."_on" then return end
for _, pos in ipairs({ thispos, otherpos }) do
local node = minetest.get_node(pos)
node.name = other_state_node[node.name]
minetest.swap_node(pos, node)
mesecon:receptor_off(pos)
end
sigonr(thispos, thisnode, clicker)
end
ondef.mesecons = { receptor = { state = mesecon.state.on } }
ondef.after_destruct = function (thispos, thisnode)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
if minetest.get_node(otherpos).name == othersigname.."_on" then
minetest.remove_node(otherpos)
mesecon:receptor_off(otherpos)
end
end
other_state_node[thissigname.."_on"] = thissigname
ondef.mesecon_other_state_node = thissigname
minetest.register_node(thissigname.."_on", ondef)
end
end
minetest.register_craft({
output = "mesecons_doors:op_door_"..material.id,
recipe = {
{ "group:mesecon_conductor_craftable", "", "" },
{ "", "doors:door_"..material.id, "group:mesecon_conductor_craftable" },
{ "group:mesecon_conductor_craftable", "", "" },
},
})
minetest.register_craft({
output = "mesecons_doors:sig_door_"..material.id,
recipe = {
{ "", "", "group:mesecon_conductor_craftable" },
{ "group:mesecon_conductor_craftable", "doors:door_"..material.id, "" },
{ "", "", "group:mesecon_conductor_craftable" },
},
})
end