Pressure plates and the object detector will send power to vertical

wires 2 nodes below them, allows to hide circuitry powered by them.
Fixes #179
Rewrite pressure plates + vertical wires using mesecon.register_node.
This commit is contained in:
Jeija 2014-11-22 12:28:45 +01:00
parent 194155fff8
commit b5cc933287
6 changed files with 93 additions and 144 deletions

View File

@ -47,15 +47,14 @@ mesecon.queue.funcs={} -- contains all ActionQueue functions
-- Settings -- Settings
dofile(minetest.get_modpath("mesecons").."/settings.lua") dofile(minetest.get_modpath("mesecons").."/settings.lua")
-- Presets (eg default rules)
dofile(minetest.get_modpath("mesecons").."/presets.lua");
-- Utilities like comparing positions, -- Utilities like comparing positions,
-- adding positions and rules, -- adding positions and rules,
-- mostly things that make the source look cleaner -- mostly things that make the source look cleaner
dofile(minetest.get_modpath("mesecons").."/util.lua"); dofile(minetest.get_modpath("mesecons").."/util.lua");
-- Presets (eg default rules)
dofile(minetest.get_modpath("mesecons").."/presets.lua");
-- The ActionQueue -- The ActionQueue
-- Saves all the actions that have to be execute in the future -- Saves all the actions that have to be execute in the future
dofile(minetest.get_modpath("mesecons").."/actionqueue.lua"); dofile(minetest.get_modpath("mesecons").."/actionqueue.lua");

View File

@ -15,6 +15,9 @@ mesecon.rules.default =
{x=0, y=1, z=-1}, {x=0, y=1, z=-1},
{x=0, y=-1, z=-1}} {x=0, y=-1, z=-1}}
mesecon.rules.pplate = {{x=0, y=-2, z=0}}
mesecon.mergetable(mesecon.rules.default, mesecon.rules.pplate)
mesecon.rules.buttonlike = mesecon.rules.buttonlike =
{{x = 1, y = 0, z = 0}, {{x = 1, y = 0, z = 0},
{x = 1, y = 1, z = 0}, {x = 1, y = 1, z = 0},

View File

@ -199,6 +199,9 @@ mesecon.mergetable = function(source, dest)
for k, v in pairs(source) do for k, v in pairs(source) do
dest[k] = dest[k] or v dest[k] = dest[k] or v
end end
for i, v in ipairs(source) do
dest[i] = dest[i] or v
end
end end
mesecon.register_node = function(name, spec_common, spec_off, spec_on) mesecon.register_node = function(name, spec_common, spec_off, spec_on)

View File

@ -55,7 +55,8 @@ minetest.register_node("mesecons_detector:object_detector_off", {
groups = {cracky=3}, groups = {cracky=3},
description="Player Detector", description="Player Detector",
mesecons = {receptor = { mesecons = {receptor = {
state = mesecon.state.off state = mesecon.state.off,
rules = mesecon.rules.pplate
}}, }},
on_construct = object_detector_make_formspec, on_construct = object_detector_make_formspec,
on_receive_fields = object_detector_on_receive_fields, on_receive_fields = object_detector_on_receive_fields,
@ -70,7 +71,8 @@ minetest.register_node("mesecons_detector:object_detector_on", {
groups = {cracky=3,not_in_creative_inventory=1}, groups = {cracky=3,not_in_creative_inventory=1},
drop = 'mesecons_detector:object_detector_off', drop = 'mesecons_detector:object_detector_off',
mesecons = {receptor = { mesecons = {receptor = {
state = mesecon.state.on state = mesecon.state.on,
rules = mesecon.rules.pplate
}}, }},
on_construct = object_detector_make_formspec, on_construct = object_detector_make_formspec,
on_receive_fields = object_detector_on_receive_fields, on_receive_fields = object_detector_on_receive_fields,
@ -94,7 +96,7 @@ minetest.register_abm(
action = function(pos) action = function(pos)
if object_detector_scan(pos) then if object_detector_scan(pos) then
minetest.swap_node(pos, {name = "mesecons_detector:object_detector_on"}) minetest.swap_node(pos, {name = "mesecons_detector:object_detector_on"})
mesecon:receptor_on(pos) mesecon:receptor_on(pos, mesecon.rules.pplate)
end end
end, end,
}) })
@ -106,7 +108,7 @@ minetest.register_abm(
action = function(pos) action = function(pos)
if not object_detector_scan(pos) then if not object_detector_scan(pos) then
minetest.swap_node(pos, {name = "mesecons_detector:object_detector_off"}) minetest.swap_node(pos, {name = "mesecons_detector:object_detector_off"})
mesecon:receptor_off(pos) mesecon:receptor_off(pos, mesecon.rules.pplate)
end end
end, end,
}) })

View File

@ -18,7 +18,7 @@ local bottom_box = {
local vertical_rules = { local vertical_rules = {
{x=0, y=1, z=0}, {x=0, y=1, z=0},
{x=0, y=-1, z=0}, {x=0, y=-1, z=0}
} }
local top_rules = { local top_rules = {
@ -26,7 +26,7 @@ local top_rules = {
{x=-1,y=0, z=0}, {x=-1,y=0, z=0},
{x=0,y=0, z=1}, {x=0,y=0, z=1},
{x=0,y=0, z=-1}, {x=0,y=0, z=-1},
{x=0,y=-1, z=0}, {x=0,y=-1, z=0}
} }
local bottom_rules = { local bottom_rules = {
@ -35,26 +35,31 @@ local bottom_rules = {
{x=0, y=0, z=1}, {x=0, y=0, z=1},
{x=0, y=0, z=-1}, {x=0, y=0, z=-1},
{x=0, y=1, z=0}, {x=0, y=1, z=0},
{x=0, y=2, z=0} -- receive power from pressure plate / detector / ... 2 nodes above
} }
local vertical_updatepos = function (pos) local vertical_updatepos = function (pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if minetest.registered_nodes[node.name] and minetest.registered_nodes[node.name].is_vertical_conductor then if minetest.registered_nodes[node.name]
and minetest.registered_nodes[node.name].is_vertical_conductor then
local node_above = minetest.get_node(mesecon:addPosRule(pos, vertical_rules[1])) local node_above = minetest.get_node(mesecon:addPosRule(pos, vertical_rules[1]))
local node_below = minetest.get_node(mesecon:addPosRule(pos, vertical_rules[2])) local node_below = minetest.get_node(mesecon:addPosRule(pos, vertical_rules[2]))
local namestate = minetest.registered_nodes[node.name].vertical_conductor_state local namestate = minetest.registered_nodes[node.name].vertical_conductor_state
local above = minetest.registered_nodes[node_above.name] and minetest.registered_nodes[node_above.name].is_vertical_conductor local above = minetest.registered_nodes[node_above.name]
local below = minetest.registered_nodes[node_below.name] and minetest.registered_nodes[node_below.name].is_vertical_conductor and minetest.registered_nodes[node_above.name].is_vertical_conductor
local below = minetest.registered_nodes[node_below.name]
and minetest.registered_nodes[node_below.name].is_vertical_conductor
local basename = "mesecons_extrawires:vertical_"
if above and below then -- above and below: vertical mesecon if above and below then -- above and below: vertical mesecon
minetest.add_node(pos, {name = "mesecons_extrawires:vertical_" .. namestate}) minetest.add_node(pos, {name = basename .. namestate})
elseif above and not below then -- above only: bottom elseif above and not below then -- above only: bottom
minetest.add_node(pos, {name = "mesecons_extrawires:vertical_bottom_" .. namestate}) minetest.add_node(pos, {name = basename .. "bottom_" .. namestate})
elseif not above and below then -- below only: top elseif not above and below then -- below only: top
minetest.add_node(pos, {name = "mesecons_extrawires:vertical_top_" .. namestate}) minetest.add_node(pos, {name = basename .. "top_" .. namestate})
else -- no vertical wire above, no vertical wire below: use default wire else -- no vertical wire above, no vertical wire below: use bottom
minetest.add_node(pos, {name = "mesecons_extrawires:vertical_" .. namestate}) minetest.add_node(pos, {name = basename .. "bottom_" .. namestate})
end end
end end
end end
@ -66,76 +71,42 @@ local vertical_update = function (pos, node)
end end
-- Vertical wire -- Vertical wire
minetest.register_node("mesecons_extrawires:vertical_on", { mesecon.register_node("mesecons_extrawires:vertical", {
description = "Vertical mesecon", description = "Vertical mesecon",
drawtype = "nodebox", drawtype = "nodebox",
tiles = {"mesecons_wire_on.png"},
walkable = false, walkable = false,
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
groups = {dig_immediate=3, not_in_creative_inventory=1},
selection_box = vertical_box, selection_box = vertical_box,
node_box = vertical_box, node_box = vertical_box,
is_vertical_conductor = true, is_vertical_conductor = true,
vertical_conductor_state = "on",
mesecons = {conductor = {
state = mesecon.state.on,
offstate = "mesecons_extrawires:vertical_off",
rules = vertical_rules,
}},
drop = "mesecons_extrawires:vertical_off", drop = "mesecons_extrawires:vertical_off",
after_place_node = vertical_update, after_place_node = vertical_update,
after_dig_node = vertical_update, after_dig_node = vertical_update
}) },{
minetest.register_node("mesecons_extrawires:vertical_off", {
description = "Vertical mesecon",
drawtype = "nodebox",
tiles = {"mesecons_wire_off.png"}, tiles = {"mesecons_wire_off.png"},
walkable = false,
paramtype = "light",
sunlight_propagates = true,
groups = {dig_immediate=3}, groups = {dig_immediate=3},
selection_box = vertical_box,
node_box = vertical_box,
is_vertical_conductor = true,
vertical_conductor_state = "off", vertical_conductor_state = "off",
mesecons = {conductor = { mesecons = {conductor = {
state = mesecon.state.off, state = mesecon.state.off,
onstate = "mesecons_extrawires:vertical_on", onstate = "mesecons_extrawires:vertical_on",
rules = vertical_rules, rules = vertical_rules,
}}, }}
after_place_node = vertical_update, },{
after_dig_node = vertical_update,
})
-- Vertical wire top
minetest.register_node("mesecons_extrawires:vertical_top_on", {
description = "Vertical mesecon",
drawtype = "nodebox",
tiles = {"mesecons_wire_on.png"}, tiles = {"mesecons_wire_on.png"},
walkable = false,
paramtype = "light",
sunlight_propagates = true,
groups = {dig_immediate=3, not_in_creative_inventory=1}, groups = {dig_immediate=3, not_in_creative_inventory=1},
selection_box = top_box,
node_box = top_box,
is_vertical_conductor = true,
vertical_conductor_state = "on", vertical_conductor_state = "on",
mesecons = {conductor = { mesecons = {conductor = {
state = mesecon.state.on, state = mesecon.state.on,
offstate = "mesecons_extrawires:vertical_top_off", offstate = "mesecons_extrawires:vertical_off",
rules = top_rules, rules = vertical_rules,
}}, }}
drop = "mesecons_extrawires:vertical_off",
after_place_node = vertical_update,
after_dig_node = vertical_update,
}) })
minetest.register_node("mesecons_extrawires:vertical_top_off", { -- Vertical wire top
mesecon.register_node("mesecons_extrawires:vertical_top", {
description = "Vertical mesecon", description = "Vertical mesecon",
drawtype = "nodebox", drawtype = "nodebox",
tiles = {"mesecons_wire_off.png"},
walkable = false, walkable = false,
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
@ -143,43 +114,31 @@ minetest.register_node("mesecons_extrawires:vertical_top_off", {
selection_box = top_box, selection_box = top_box,
node_box = top_box, node_box = top_box,
is_vertical_conductor = true, is_vertical_conductor = true,
drop = "mesecons_extrawires:vertical_off",
after_place_node = vertical_update,
after_dig_node = vertical_update
},{
tiles = {"mesecons_wire_off.png"},
vertical_conductor_state = "off", vertical_conductor_state = "off",
mesecons = {conductor = { mesecons = {conductor = {
state = mesecon.state.off, state = mesecon.state.off,
onstate = "mesecons_extrawires:vertical_top_on", onstate = "mesecons_extrawires:vertical_top_on",
rules = top_rules, rules = top_rules,
}}, }}
drop = "mesecons_extrawires:vertical_off", },{
after_place_node = vertical_update, tiles = {"mesecons_wire_on.png"},
after_dig_node = vertical_update, vertical_conductor_state = "on",
mesecons = {conductor = {
state = mesecon.state.on,
offstate = "mesecons_extrawires:vertical_top_off",
rules = top_rules,
}}
}) })
-- Vertical wire bottom -- Vertical wire bottom
minetest.register_node("mesecons_extrawires:vertical_bottom_on", { mesecon.register_node("mesecons_extrawires:vertical_bottom", {
description = "Vertical mesecon", description = "Vertical mesecon",
drawtype = "nodebox", drawtype = "nodebox",
tiles = {"mesecons_wire_on.png"},
walkable = false,
paramtype = "light",
sunlight_propagates = true,
vertical_conductor_state = "on",
groups = {dig_immediate = 3, not_in_creative_inventory = 1},
selection_box = bottom_box,
node_box = bottom_box,
mesecons = {conductor = {
state = mesecon.state.on,
offstate = "mesecons_extrawires:vertical_bottom_off",
rules = bottom_rules,
}},
drop = "mesecons_extrawires:vertical_off",
after_place_node = vertical_update,
after_dig_node = vertical_update,
})
minetest.register_node("mesecons_extrawires:vertical_bottom_off", {
description = "Vertical mesecon",
drawtype = "nodebox",
tiles = {"mesecons_wire_off.png"},
walkable = false, walkable = false,
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
@ -187,15 +146,25 @@ minetest.register_node("mesecons_extrawires:vertical_bottom_off", {
selection_box = bottom_box, selection_box = bottom_box,
node_box = bottom_box, node_box = bottom_box,
is_vertical_conductor = true, is_vertical_conductor = true,
drop = "mesecons_extrawires:vertical_off",
after_place_node = vertical_update,
after_dig_node = vertical_update
},{
tiles = {"mesecons_wire_off.png"},
vertical_conductor_state = "off", vertical_conductor_state = "off",
mesecons = {conductor = { mesecons = {conductor = {
state = mesecon.state.off, state = mesecon.state.off,
onstate = "mesecons_extrawires:vertical_bottom_on", onstate = "mesecons_extrawires:vertical_bottom_on",
rules = bottom_rules, rules = bottom_rules,
}}, }}
drop = "mesecons_extrawires:vertical_off", },{
after_place_node = vertical_update, tiles = {"mesecons_wire_on.png"},
after_dig_node = vertical_update, vertical_conductor_state = "on",
mesecons = {conductor = {
state = mesecon.state.on,
offstate = "mesecons_extrawires:vertical_bottom_off",
rules = bottom_rules,
}}
}) })
minetest.register_craft({ minetest.register_craft({

View File

@ -9,19 +9,19 @@ local pp_box_on = {
} }
pp_on_timer = function (pos, elapsed) pp_on_timer = function (pos, elapsed)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
local ppspec = minetest.registered_nodes[node.name].pressureplate local basename = minetest.registered_nodes[node.name].pressureplate_basename
-- This is a workaround for a strange bug that occurs when the server is started -- This is a workaround for a strange bug that occurs when the server is started
-- For some reason the first time on_timer is called, the pos is wrong -- For some reason the first time on_timer is called, the pos is wrong
if not ppspec then return end if not basename then return end
local objs = minetest.get_objects_inside_radius(pos, 1) local objs = minetest.get_objects_inside_radius(pos, 1)
local two_below = mesecon:addPosRule(pos, {x = 0, y = -2, z = 0}) local two_below = mesecon:addPosRule(pos, {x = 0, y = -2, z = 0})
if objs[1] == nil and node.name == ppspec.onstate then if objs[1] == nil and node.name == basename .. "_on" then
minetest.add_node(pos, {name = ppspec.offstate}) minetest.add_node(pos, {name = basename .. "_off"})
mesecon:receptor_off(pos) mesecon:receptor_off(pos, mesecon.rules.pplate)
-- force deactivation of mesecon two blocks below (hacky) -- force deactivation of mesecon two blocks below (hacky)
if not mesecon:connected_to_receptor(two_below) then if not mesecon:connected_to_receptor(two_below) then
mesecon:turnoff(two_below) mesecon:turnoff(two_below)
@ -30,8 +30,8 @@ pp_on_timer = function (pos, elapsed)
for k, obj in pairs(objs) do for k, obj in pairs(objs) do
local objpos = obj:getpos() local objpos = obj:getpos()
if objpos.y > pos.y-1 and objpos.y < pos.y then if objpos.y > pos.y-1 and objpos.y < pos.y then
minetest.add_node(pos, {name=ppspec.onstate}) minetest.add_node(pos, {name = basename .. "_on"})
mesecon:receptor_on(pos) mesecon:receptor_on(pos, mesecon.rules.pplate )
-- force activation of mesecon two blocks below (hacky) -- force activation of mesecon two blocks below (hacky)
mesecon:turnon(two_below) mesecon:turnon(two_below)
end end
@ -49,66 +49,40 @@ end
-- image: inventory and wield image of the pressure plate -- image: inventory and wield image of the pressure plate
-- recipe: crafting recipe of the pressure plate -- recipe: crafting recipe of the pressure plate
function mesecon:register_pressure_plate(offstate, onstate, description, textures_off, textures_on, image_w, image_i, recipe) function mesecon:register_pressure_plate(basename, description, textures_off, textures_on, image_w, image_i, recipe)
local ppspec = { mesecon.register_node(basename, {
offstate = offstate,
onstate = onstate
}
minetest.register_node(offstate, {
drawtype = "nodebox", drawtype = "nodebox",
tiles = textures_off,
inventory_image = image_i, inventory_image = image_i,
wield_image = image_w, wield_image = image_w,
paramtype = "light", paramtype = "light",
selection_box = pp_box_off,
node_box = pp_box_off,
groups = {snappy = 2, oddly_breakable_by_hand = 3},
description = description, description = description,
pressureplate = ppspec, pressureplate_basename = basename,
on_timer = pp_on_timer, on_timer = pp_on_timer,
mesecons = {receptor = {
state = mesecon.state.off
}},
on_construct = function(pos) on_construct = function(pos)
minetest.get_node_timer(pos):start(PRESSURE_PLATE_INTERVAL) minetest.get_node_timer(pos):start(PRESSURE_PLATE_INTERVAL)
end, end,
}) },{
mesecons = {receptor = { state = mesecon.state.off, rules = mesecon.rules.pplate }},
minetest.register_node(onstate, { node_box = pp_box_off,
drawtype = "nodebox", selection_box = pp_box_off,
tiles = textures_on, groups = {snappy = 2, oddly_breakable_by_hand = 3},
paramtype = "light", tiles = textures_off
selection_box = pp_box_on, },{
mesecons = {receptor = { state = mesecon.state.on, rules = mesecon.rules.pplate }},
node_box = pp_box_on, node_box = pp_box_on,
selection_box = pp_box_on,
groups = {snappy = 2, oddly_breakable_by_hand = 3, not_in_creative_inventory = 1}, groups = {snappy = 2, oddly_breakable_by_hand = 3, not_in_creative_inventory = 1},
drop = offstate, tiles = textures_on
pressureplate = ppspec,
on_timer = pp_on_timer,
sounds = default.node_sound_wood_defaults(),
mesecons = {receptor = {
state = mesecon.state.on
}},
on_construct = function(pos)
minetest.get_node_timer(pos):start(PRESSURE_PLATE_INTERVAL)
end,
after_dig_node = function(pos)
local two_below = mesecon:addPosRule(pos, {x = 0, y = -2, z = 0})
if not mesecon:connected_to_receptor(two_below) then
mesecon:turnoff(two_below)
end
end
}) })
minetest.register_craft({ minetest.register_craft({
output = offstate, output = basename .. "_off",
recipe = recipe, recipe = recipe,
}) })
end end
mesecon:register_pressure_plate( mesecon:register_pressure_plate(
"mesecons_pressureplates:pressure_plate_wood_off", "mesecons_pressureplates:pressure_plate_wood",
"mesecons_pressureplates:pressure_plate_wood_on",
"Wooden Pressure Plate", "Wooden Pressure Plate",
{"jeija_pressure_plate_wood_off.png","jeija_pressure_plate_wood_off.png","jeija_pressure_plate_wood_off_edges.png"}, {"jeija_pressure_plate_wood_off.png","jeija_pressure_plate_wood_off.png","jeija_pressure_plate_wood_off_edges.png"},
{"jeija_pressure_plate_wood_on.png","jeija_pressure_plate_wood_on.png","jeija_pressure_plate_wood_on_edges.png"}, {"jeija_pressure_plate_wood_on.png","jeija_pressure_plate_wood_on.png","jeija_pressure_plate_wood_on_edges.png"},
@ -117,8 +91,7 @@ mesecon:register_pressure_plate(
{{"group:wood", "group:wood"}}) {{"group:wood", "group:wood"}})
mesecon:register_pressure_plate( mesecon:register_pressure_plate(
"mesecons_pressureplates:pressure_plate_stone_off", "mesecons_pressureplates:pressure_plate_stone",
"mesecons_pressureplates:pressure_plate_stone_on",
"Stone Pressure Plate", "Stone Pressure Plate",
{"jeija_pressure_plate_stone_off.png","jeija_pressure_plate_stone_off.png","jeija_pressure_plate_stone_off_edges.png"}, {"jeija_pressure_plate_stone_off.png","jeija_pressure_plate_stone_off.png","jeija_pressure_plate_stone_off_edges.png"},
{"jeija_pressure_plate_stone_on.png","jeija_pressure_plate_stone_on.png","jeija_pressure_plate_stone_on_edges.png"}, {"jeija_pressure_plate_stone_on.png","jeija_pressure_plate_stone_on.png","jeija_pressure_plate_stone_on_edges.png"},