Finish new mesecon-in-nodedef for conductors and receptors. Make wires and switch use it for reference.

This commit is contained in:
Jeija 2012-12-08 17:50:25 +01:00
parent e0aa5b1d3d
commit 472650f099
5 changed files with 102 additions and 111 deletions

View File

@ -2,7 +2,8 @@
--Receptors --Receptors
function mesecon:is_receptor_node(nodename) function mesecon:is_receptor_node(nodename)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.receptor and minetest.registered_nodes[nodename].mesecons.receptor
and minetest.registered_nodes[nodename].mesecons.receptor.state == mesecon.state.on then and minetest.registered_nodes[nodename].mesecons.receptor.state == mesecon.state.on then
return true return true
@ -16,7 +17,8 @@ function mesecon:is_receptor_node(nodename)
end end
function mesecon:is_receptor_node_off(nodename, pos, ownpos) function mesecon:is_receptor_node_off(nodename, pos, ownpos)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.receptor and minetest.registered_nodes[nodename].mesecons.receptor
and minetest.registered_nodes[nodename].mesecons.receptor.state == mesecon.state.off then and minetest.registered_nodes[nodename].mesecons.receptor.state == mesecon.state.off then
return true return true
@ -33,7 +35,9 @@ function mesecon:receptor_get_rules(node)
if minetest.registered_nodes[node.name].mesecons if minetest.registered_nodes[node.name].mesecons
and minetest.registered_nodes[node.name].mesecons.receptor then and minetest.registered_nodes[node.name].mesecons.receptor then
local rules = minetest.registered_nodes[node.name].mesecons.receptor.rules local rules = minetest.registered_nodes[node.name].mesecons.receptor.rules
if type(rules) == 'function' then if not rules then
return mesecon.rules.default
elseif type(rules) == 'function' then
return rules(node) return rules(node)
else else
return rules return rules
@ -60,7 +64,8 @@ function mesecon:is_effector_on(nodename)
return true return true
end end
end end
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.effector and minetest.registered_nodes[nodename].mesecons.effector
and minetest.registered_nodes[nodename].mesecons.effector.state == mesecon.state.on then and minetest.registered_nodes[nodename].mesecons.effector.state == mesecon.state.on then
return true return true
@ -74,7 +79,8 @@ function mesecon:is_effector_off(nodename)
return true return true
end end
end end
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.effector and minetest.registered_nodes[nodename].mesecons.effector
and minetest.registered_nodes[nodename].mesecons.effector.state == mesecon.state.off then and minetest.registered_nodes[nodename].mesecons.effector.state == mesecon.state.off then
return true return true
@ -83,7 +89,8 @@ function mesecon:is_effector_off(nodename)
end end
function mesecon:is_effector(nodename) function mesecon:is_effector(nodename)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.effector then and minetest.registered_nodes[nodename].mesecons.effector then
return true return true
end end
@ -94,7 +101,9 @@ function mesecon:effector_get_input_rules(node)
if minetest.registered_nodes[node.name].mesecons if minetest.registered_nodes[node.name].mesecons
and minetest.registered_nodes[node.name].mesecons.effector then and minetest.registered_nodes[node.name].mesecons.effector then
local rules = minetest.registered_nodes[node.name].mesecons.effector.rules local rules = minetest.registered_nodes[node.name].mesecons.effector.rules
if type(rules) == 'function' then if not rules then
return mesecon.rules.default
elseif type(rules) == 'function' then
return rules(node) return rules(node)
else else
return rules return rules
@ -117,7 +126,7 @@ end
--Signals --Signals
function mesecon:activate(pos) --TODO function mesecon:activate(pos) --TODO
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
for i, action in ipairs(mesecon.actions_on) do for i, action in ipairs(mesecon.actions_on) do
action(pos, node) action(pos, node)
end end
@ -154,9 +163,10 @@ end
--Conductor system stuff --Conductor system stuff
function mesecon:get_conductor_on(offstate) function mesecon:get_conductor_on(offstate)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[offstate]
and minetest.registered_nodes[nodename].mesecons.condcutor then and minetest.registered_nodes[offstate].mesecons
return minetest.registered_nodes[nodename].mesecons.conductor.onstate and minetest.registered_nodes[offstate].mesecons.conductor then
return minetest.registered_nodes[offstate].mesecons.conductor.onstate
end end
for i, conductor in ipairs(mesecon.conductors) do --TODO for i, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.offstate == offstate then if conductor.offstate == offstate then
@ -167,9 +177,10 @@ function mesecon:get_conductor_on(offstate)
end end
function mesecon:get_conductor_off(onstate) function mesecon:get_conductor_off(onstate)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[onstate]
and minetest.registered_nodes[nodename].mesecons.condcutor then and minetest.registered_nodes[onstate].mesecons
return minetest.registered_nodes[nodename].mesecons.conductor.offstate and minetest.registered_nodes[onstate].mesecons.conductor then
return minetest.registered_nodes[onstate].mesecons.conductor.offstate
end end
for i, conductor in ipairs(mesecon.conductors) do --TODO for i, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.onstate == onstate then if conductor.onstate == onstate then
@ -179,44 +190,49 @@ function mesecon:get_conductor_off(onstate)
return false return false
end end
function mesecon:is_conductor_on(name) function mesecon:is_conductor_on(nodename)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons.condcutor and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.condcutor.state == mesecon.state.on then and minetest.registered_nodes[nodename].mesecons.conductor
and minetest.registered_nodes[nodename].mesecons.conductor.state == mesecon.state.on then
return true return true
end end
for i, conductor in ipairs(mesecon.conductors) do --TODO for _, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.onstate == name then if conductor.onstate == nodename then
return true return true
end end
end end
return false return false
end end
function mesecon:is_conductor_off(name) function mesecon:is_conductor_off(nodename)
if minetest.registered_nodes[nodename].mesecons if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons.condcutor and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.condcutor.state == mesecon.state.off then and minetest.registered_nodes[nodename].mesecons.conductor
and minetest.registered_nodes[nodename].mesecons.conductor.state == mesecon.state.off then
return true return true
end end
for i, conductor in ipairs(mesecon.conductors) do --TODO for _, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.offstate == name then if conductor.offstate == nodename then
return true return true
end end
end end
return false return false
end end
function mesecon:is_conductor(name) function mesecon:is_conductor(nodename)
--TODO: simplify --TODO: simplify
return mesecon:is_conductor_on(name) or mesecon:is_conductor_off(name) return mesecon:is_conductor_on(nodename) or mesecon:is_conductor_off(nodename)
end end
function mesecon:conductor_get_rules(node) function mesecon:conductor_get_rules(node)
if minetest.registered_nodes[node.name].mesecons if minetest.registered_nodes[node.name]
and minetest.registered_nodes[node.name].mesecons.condcutor then and minetest.registered_nodes[node.name].mesecons
local rules = minetest.registered_nodes[node.name].mesecons.condcutor.rules and minetest.registered_nodes[node.name].mesecons.conductor then
if type(rules) == 'function' then local rules = minetest.registered_nodes[node.name].mesecons.conductor.rules
if not rules then
return mesecon.rules.default
elseif type(rules) == 'function' then
return rules(node) return rules(node)
else else
return rules return rules
@ -272,17 +288,17 @@ function mesecon:turnon(pos)
if mesecon:is_effector(node.name) then if mesecon:is_effector(node.name) then
mesecon:changesignal(pos) mesecon:changesignal(pos)
if mesecon:is_effector_off(node.name) then mesecon:activate(pos) end if mesecon:is_effector_off(node.name) then
mesecon:activate(pos)
end
end end
end end
function mesecon:turnoff(pos) --receptor rules used because output could have been dug function mesecon:turnoff(pos) --receptor rules used because output could have been dug
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local rules
if mesecon:is_conductor_on(node.name) then if mesecon:is_conductor_on(node.name) then
rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
minetest.env:add_node(pos, {name=mesecon:get_conductor_off(node.name), param2 = node.param2}) minetest.env:add_node(pos, {name=mesecon:get_conductor_off(node.name), param2 = node.param2})
for i, rule in ipairs(rules) do for i, rule in ipairs(rules) do
@ -299,7 +315,10 @@ function mesecon:turnoff(pos) --receptor rules used because output could have be
if mesecon:is_effector(node.name) then if mesecon:is_effector(node.name) then
mesecon:changesignal(pos) mesecon:changesignal(pos)
if mesecon:is_effector_on(node.name) and not mesecon:is_powered(pos) then mesecon:deactivate(pos) end if mesecon:is_effector_on(node.name)
and not mesecon:is_powered(pos) then
mesecon:deactivate(pos)
end
end end
end end
@ -347,9 +366,6 @@ function mesecon:connected_to_pw_src(pos, checked)
end end
function mesecon:rules_link(output, input, dug_outputrules) --output/input are positions (outputrules optional, used if node has been dug) function mesecon:rules_link(output, input, dug_outputrules) --output/input are positions (outputrules optional, used if node has been dug)
local k = 1
local l = 1
local outputnode = minetest.env:get_node(output) local outputnode = minetest.env:get_node(output)
local inputnode = minetest.env:get_node(input) local inputnode = minetest.env:get_node(input)
@ -375,12 +391,11 @@ function mesecon:rules_link(output, input, dug_outputrules) --output/input are p
end end
for k, outputrule in ipairs(outputrules) do for _, outputrule in ipairs(outputrules) do
if outputrule.x + output.x == input.x if outputrule.x + output.x == input.x
and outputrule.y + output.y == input.y and outputrule.y + output.y == input.y
and outputrule.z + output.z == input.z then -- Check if output sends to input and outputrule.z + output.z == input.z then -- Check if output sends to input
l = 1 for _, inputrule in ipairs(inputrules) do
for k, inputrule in ipairs(inputrules) do
if inputrule.x + input.x == output.x if inputrule.x + input.x == output.x
and inputrule.y + input.y == output.y and inputrule.y + input.y == output.y
and inputrule.z + input.z == output.z then --Check if input accepts from output and inputrule.z + input.z == output.z then --Check if input accepts from output

View File

@ -18,13 +18,12 @@ end
function mesecon:register_receptor(onstate, offstate, rules, get_rules) function mesecon:register_receptor(onstate, offstate, rules, get_rules)
if get_rules == nil and rules == nil then if get_rules == nil and rules == nil then
rules = mesecon:get_rules("default") rules=mesecon:get_rules("default")
end end
table.insert(mesecon.receptors, table.insert(mesecon.receptors,
{onstate = onstate, {onstate = onstate,
offstate = offstate, offstate = offstate,
rules = rules, rules = input_rules,
get_rules = get_rules}) get_rules = get_rules})
end end

View File

@ -69,10 +69,10 @@ for zmy=0, 1 do
tostring(xpy)..tostring(zpy)..tostring(xmy)..tostring(zmy) tostring(xpy)..tostring(zpy)..tostring(xmy)..tostring(zmy)
if nodeid == "00000000" then if nodeid == "00000000" then
groups = {dig_immediate = 3, mesecon = 2, mesecon_conductor_craftable=1} groups = {dig_immediate = 3, mesecon_conductor_craftable=1}
wiredesc = "Mesecon" wiredesc = "Mesecon"
else else
groups = {dig_immediate = 3, mesecon = 2, not_in_creative_inventory = 1} groups = {dig_immediate = 3, not_in_creative_inventory = 1}
wiredesc = "Mesecons Wire (ID: "..nodeid..")" wiredesc = "Mesecons Wire (ID: "..nodeid..")"
end end
@ -153,7 +153,11 @@ for zmy=0, 1 do
groups = groups, groups = groups,
walkable = false, walkable = false,
stack_max = 99, stack_max = 99,
drop = "mesecons:wire_00000000_off" drop = "mesecons:wire_00000000_off",
mesecons = {conductor={
state = mesecon.state.off,
onstate = "mesecons:wire_"..nodeid.."_on"
}}
}) })
minetest.register_node("mesecons:wire_"..nodeid.."_on", { minetest.register_node("mesecons:wire_"..nodeid.."_on", {
@ -174,9 +178,12 @@ for zmy=0, 1 do
groups = {dig_immediate = 3, mesecon = 2, not_in_creative_inventory = 1}, groups = {dig_immediate = 3, mesecon = 2, not_in_creative_inventory = 1},
walkable = false, walkable = false,
stack_max = 99, stack_max = 99,
drop = "mesecons:wire_00000000_off" drop = "mesecons:wire_00000000_off",
mesecons = {conductor={
state = mesecon.state.on,
offstate = "mesecons:wire_"..nodeid.."_off"
}}
}) })
mesecon:register_conductor("mesecons:wire_"..nodeid.."_on", "mesecons:wire_"..nodeid.."_off")
end end
end end
end end
@ -187,13 +194,13 @@ end
end end
minetest.register_on_placenode(function(pos, node) minetest.register_on_placenode(function(pos, node)
if minetest.get_item_group(node.name, "mesecon") > 1 then if minetest.registered_nodes[node.name].mesecons then
mesecon:update_autoconnect(pos) mesecon:update_autoconnect(pos)
end end
end) end)
minetest.register_on_dignode(function(pos, node) minetest.register_on_dignode(function(pos, node)
if minetest.get_item_group(node.name, "mesecon") > 1 then if minetest.registered_nodes[node.name].mesecons then
mesecon:update_autoconnect(pos) mesecon:update_autoconnect(pos)
end end
end) end)
@ -234,48 +241,20 @@ function mesecon:update_autoconnect(pos, secondcall, replace_old)
nodename = minetest.env:get_node(pos).name nodename = minetest.env:get_node(pos).name
if string.find(nodename, "mesecons:wire_") == nil and not replace_old then return nil end if string.find(nodename, "mesecons:wire_") == nil and not replace_old then return nil end
if mesecon:rules_link_bothdir(pos, xppos) then xp = 1 else xp = 0 end
if mesecon:rules_link_bothdir(pos, xmpos) then xm = 1 else xm = 0 end
if mesecon:rules_link_bothdir(pos, zppos) then zp = 1 else zp = 0 end
if mesecon:rules_link_bothdir(pos, zmpos) then zm = 1 else zm = 0 end
--if the groups mesecon == 1 then wires won't connect to it if mesecon:rules_link_bothdir(pos, xpympos) then xp = 1 end
local zmg = minetest.get_item_group(minetest.env:get_node(zmpos ).name, "mesecon") if mesecon:rules_link_bothdir(pos, xmympos) then xm = 1 end
local zmymg = minetest.get_item_group(minetest.env:get_node(zmympos).name, "mesecon") if mesecon:rules_link_bothdir(pos, zpympos) then zp = 1 end
local xmg = minetest.get_item_group(minetest.env:get_node(xmpos ).name, "mesecon") if mesecon:rules_link_bothdir(pos, zmympos) then zm = 1 end
local xmymg = minetest.get_item_group(minetest.env:get_node(xmympos).name, "mesecon")
local zpg = minetest.get_item_group(minetest.env:get_node(zppos ).name, "mesecon")
local zpymg = minetest.get_item_group(minetest.env:get_node(zpympos).name, "mesecon")
local xpg = minetest.get_item_group(minetest.env:get_node(xppos ).name, "mesecon")
local xpymg = minetest.get_item_group(minetest.env:get_node(xpympos).name, "mesecon")
if mesecon:rules_link(pos, xpypos) then xpy = 1 else xpy = 0 end
local xpyg = minetest.get_item_group(minetest.env:get_node(xpypos).name, "mesecon") if mesecon:rules_link(pos, zpypos) then zpy = 1 else zpy = 0 end
local zpyg = minetest.get_item_group(minetest.env:get_node(zpypos).name, "mesecon") if mesecon:rules_link(pos, xmypos) then xmy = 1 else xmy = 0 end
local xmyg = minetest.get_item_group(minetest.env:get_node(xmypos).name, "mesecon") if mesecon:rules_link(pos, zmypos) then zmy = 1 else zmy = 0 end
local zmyg = minetest.get_item_group(minetest.env:get_node(zmypos).name, "mesecon")
if ((zmg == 2) or (zmymg == 2)) == true then zm = 1 else zm = 0 end
if ((xmg == 2) or (xmymg == 2)) == true then xm = 1 else xm = 0 end
if ((zpg == 2) or (zpymg == 2)) == true then zp = 1 else zp = 0 end
if ((xpg == 2) or (xpymg == 2)) == true then xp = 1 else xp = 0 end
if xpyg == 2 then xpy = 1 else xpy = 0 end
if zpyg == 2 then zpy = 1 else zpy = 0 end
if xmyg == 2 then xmy = 1 else xmy = 0 end
if zmyg == 2 then zmy = 1 else zmy = 0 end
-- If group == 3 then the mesecon only connects to input and output ports
if xpg == 3 and mesecon:rules_link_bothdir(pos, xppos) then xp = 1 end
if xmg == 3 and mesecon:rules_link_bothdir(pos, xmpos) then xm = 1 end
if zpg == 3 and mesecon:rules_link_bothdir(pos, zppos) then zp = 1 end
if zmg == 3 and mesecon:rules_link_bothdir(pos, zmpos) then zm = 1 end
if xpymg == 3 and mesecon:rules_link_bothdir(pos, xpympos) then xp = 1 end
if xmymg == 3 and mesecon:rules_link_bothdir(pos, xmympos) then xm = 1 end
if zpymg == 3 and mesecon:rules_link_bothdir(pos, zpympos) then zp = 1 end
if zmymg == 3 and mesecon:rules_link_bothdir(pos, zmympos) then zm = 1 end
if xpyg == 3 then if mesecon:rules_link(pos, xpypos) then xpy = 1 end end
if zpyg == 3 then if mesecon:rules_link(pos, zpypos) then zpy = 1 end end
if xmyg == 3 then if mesecon:rules_link(pos, xmypos) then xmy = 1 end end
if zmyg == 3 then if mesecon:rules_link(pos, zmypos) then zmy = 1 end end
-- Backward compatibility -- Backward compatibility
if replace_old then if replace_old then

View File

@ -28,6 +28,7 @@ minetest.register_node("mesecons_button:button_off", {
groups = {dig_immediate=2, mesecon = 3, mesecon_needs_receiver = 1}, groups = {dig_immediate=2, mesecon = 3, mesecon_needs_receiver = 1},
description = "Button", description = "Button",
}) })
minetest.register_node("mesecons_button:button_on", { minetest.register_node("mesecons_button:button_on", {
drawtype = "nodebox", drawtype = "nodebox",
tiles = { tiles = {
@ -108,4 +109,3 @@ mesecon:add_rules("button", {
mesecon:add_receptor_node_off("mesecons_button:button_off", nil, mesecon.button_get_rules) mesecon:add_receptor_node_off("mesecons_button:button_off", nil, mesecon.button_get_rules)
mesecon:add_receptor_node("mesecons_button:button_on", nil, mesecon.button_get_rules) mesecon:add_receptor_node("mesecons_button:button_on", nil, mesecon.button_get_rules)

View File

@ -3,32 +3,30 @@
minetest.register_node("mesecons_switch:mesecon_switch_off", { minetest.register_node("mesecons_switch:mesecon_switch_off", {
tiles = {"jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_off.png"}, tiles = {"jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_off.png"},
paramtype2="facedir", paramtype2="facedir",
groups = {dig_immediate=2, mesecon = 2}, groups = {dig_immediate=2},
description="Switch", description="Switch",
mesecons = {receptor = {
state = mesecon.state.off
}},
on_punch = function(pos, node)
minetest.env:add_node(pos, {name="mesecons_switch:mesecon_switch_on", param2=node.param2})
mesecon:receptor_on(pos)
end
}) })
minetest.register_node("mesecons_switch:mesecon_switch_on", { minetest.register_node("mesecons_switch:mesecon_switch_on", {
tiles = {"jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_on.png"}, tiles = {"jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_side.png", "jeija_mesecon_switch_on.png"},
paramtype2="facedir", paramtype2="facedir",
groups = {dig_immediate=2,not_in_creative_inventory=1, mesecon = 2}, groups = {dig_immediate=2,not_in_creative_inventory=1},
drop='"mesecons_switch:mesecon_switch_off" 1', drop='"mesecons_switch:mesecon_switch_off" 1',
description="Switch", mesecons = {receptor = {
}) state = mesecon.state.on
}},
mesecon:register_receptor("mesecons_switch:mesecon_switch_on", "mesecons_switch:mesecon_switch_off") on_punch = function(pos, node)
minetest.register_on_punchnode(function(pos, node, puncher)
if node.name == "mesecons_switch:mesecon_switch_on" then
minetest.env:add_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2}) minetest.env:add_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2})
nodeupdate(pos)
mesecon:receptor_off(pos) mesecon:receptor_off(pos)
end end
if node.name == "mesecons_switch:mesecon_switch_off" then })
minetest.env:add_node(pos, {name="mesecons_switch:mesecon_switch_on", param2=node.param2})
nodeupdate(pos)
mesecon:receptor_on(pos)
end
end)
minetest.register_craft({ minetest.register_craft({
output = '"mesecons_switch:mesecon_switch_off" 2', output = '"mesecons_switch:mesecon_switch_off" 2',