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
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.state == mesecon.state.on then
return true
@ -16,7 +17,8 @@ function mesecon:is_receptor_node(nodename)
end
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.state == mesecon.state.off then
return true
@ -33,7 +35,9 @@ function mesecon:receptor_get_rules(node)
if minetest.registered_nodes[node.name].mesecons
and minetest.registered_nodes[node.name].mesecons.receptor then
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)
else
return rules
@ -60,7 +64,8 @@ function mesecon:is_effector_on(nodename)
return true
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.state == mesecon.state.on then
return true
@ -74,7 +79,8 @@ function mesecon:is_effector_off(nodename)
return true
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.state == mesecon.state.off then
return true
@ -83,7 +89,8 @@ function mesecon:is_effector_off(nodename)
end
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
return true
end
@ -94,7 +101,9 @@ function mesecon:effector_get_input_rules(node)
if minetest.registered_nodes[node.name].mesecons
and minetest.registered_nodes[node.name].mesecons.effector then
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)
else
return rules
@ -117,7 +126,7 @@ end
--Signals
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
action(pos, node)
end
@ -154,9 +163,10 @@ end
--Conductor system stuff
function mesecon:get_conductor_on(offstate)
if minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.condcutor then
return minetest.registered_nodes[nodename].mesecons.conductor.onstate
if minetest.registered_nodes[offstate]
and minetest.registered_nodes[offstate].mesecons
and minetest.registered_nodes[offstate].mesecons.conductor then
return minetest.registered_nodes[offstate].mesecons.conductor.onstate
end
for i, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.offstate == offstate then
@ -167,9 +177,10 @@ function mesecon:get_conductor_on(offstate)
end
function mesecon:get_conductor_off(onstate)
if minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.condcutor then
return minetest.registered_nodes[nodename].mesecons.conductor.offstate
if minetest.registered_nodes[onstate]
and minetest.registered_nodes[onstate].mesecons
and minetest.registered_nodes[onstate].mesecons.conductor then
return minetest.registered_nodes[onstate].mesecons.conductor.offstate
end
for i, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.onstate == onstate then
@ -179,44 +190,49 @@ function mesecon:get_conductor_off(onstate)
return false
end
function mesecon:is_conductor_on(name)
if minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.condcutor
and minetest.registered_nodes[nodename].mesecons.condcutor.state == mesecon.state.on then
function mesecon:is_conductor_on(nodename)
if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.conductor
and minetest.registered_nodes[nodename].mesecons.conductor.state == mesecon.state.on then
return true
end
for i, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.onstate == name then
for _, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.onstate == nodename then
return true
end
end
return false
end
function mesecon:is_conductor_off(name)
if minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.condcutor
and minetest.registered_nodes[nodename].mesecons.condcutor.state == mesecon.state.off then
function mesecon:is_conductor_off(nodename)
if minetest.registered_nodes[nodename]
and minetest.registered_nodes[nodename].mesecons
and minetest.registered_nodes[nodename].mesecons.conductor
and minetest.registered_nodes[nodename].mesecons.conductor.state == mesecon.state.off then
return true
end
for i, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.offstate == name then
for _, conductor in ipairs(mesecon.conductors) do --TODO
if conductor.offstate == nodename then
return true
end
end
return false
end
function mesecon:is_conductor(name)
function mesecon:is_conductor(nodename)
--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
function mesecon:conductor_get_rules(node)
if minetest.registered_nodes[node.name].mesecons
and minetest.registered_nodes[node.name].mesecons.condcutor then
local rules = minetest.registered_nodes[node.name].mesecons.condcutor.rules
if type(rules) == 'function' then
if minetest.registered_nodes[node.name]
and minetest.registered_nodes[node.name].mesecons
and minetest.registered_nodes[node.name].mesecons.conductor 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)
else
return rules
@ -272,17 +288,17 @@ function mesecon:turnon(pos)
if mesecon:is_effector(node.name) then
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
function mesecon:turnoff(pos) --receptor rules used because output could have been dug
local node = minetest.env:get_node(pos)
local rules
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})
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
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
@ -347,9 +366,6 @@ function mesecon:connected_to_pw_src(pos, checked)
end
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 inputnode = minetest.env:get_node(input)
@ -375,12 +391,11 @@ function mesecon:rules_link(output, input, dug_outputrules) --output/input are p
end
for k, outputrule in ipairs(outputrules) do
for _, outputrule in ipairs(outputrules) do
if outputrule.x + output.x == input.x
and outputrule.y + output.y == input.y
and outputrule.z + output.z == input.z then -- Check if output sends to input
l = 1
for k, inputrule in ipairs(inputrules) do
for _, inputrule in ipairs(inputrules) do
if inputrule.x + input.x == output.x
and inputrule.y + input.y == output.y
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)
if get_rules == nil and rules == nil then
rules = mesecon:get_rules("default")
rules=mesecon:get_rules("default")
end
table.insert(mesecon.receptors,
{onstate = onstate,
offstate = offstate,
rules = rules,
rules = input_rules,
get_rules = get_rules})
end

View File

@ -69,10 +69,10 @@ for zmy=0, 1 do
tostring(xpy)..tostring(zpy)..tostring(xmy)..tostring(zmy)
if nodeid == "00000000" then
groups = {dig_immediate = 3, mesecon = 2, mesecon_conductor_craftable=1}
groups = {dig_immediate = 3, mesecon_conductor_craftable=1}
wiredesc = "Mesecon"
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..")"
end
@ -153,7 +153,11 @@ for zmy=0, 1 do
groups = groups,
walkable = false,
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", {
@ -174,9 +178,12 @@ for zmy=0, 1 do
groups = {dig_immediate = 3, mesecon = 2, not_in_creative_inventory = 1},
walkable = false,
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
@ -187,13 +194,13 @@ end
end
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)
end
end)
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)
end
end)
@ -234,48 +241,20 @@ function mesecon:update_autoconnect(pos, secondcall, replace_old)
nodename = minetest.env:get_node(pos).name
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
local zmg = minetest.get_item_group(minetest.env:get_node(zmpos ).name, "mesecon")
local zmymg = minetest.get_item_group(minetest.env:get_node(zmympos).name, "mesecon")
local xmg = minetest.get_item_group(minetest.env:get_node(xmpos ).name, "mesecon")
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_bothdir(pos, xpympos) then xp = 1 end
if mesecon:rules_link_bothdir(pos, xmympos) then xm = 1 end
if mesecon:rules_link_bothdir(pos, zpympos) then zp = 1 end
if mesecon:rules_link_bothdir(pos, zmympos) then zm = 1 end
local xpyg = minetest.get_item_group(minetest.env:get_node(xpypos).name, "mesecon")
local zpyg = minetest.get_item_group(minetest.env:get_node(zpypos).name, "mesecon")
local xmyg = minetest.get_item_group(minetest.env:get_node(xmypos).name, "mesecon")
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
if mesecon:rules_link(pos, xpypos) then xpy = 1 else xpy = 0 end
if mesecon:rules_link(pos, zpypos) then zpy = 1 else zpy = 0 end
if mesecon:rules_link(pos, xmypos) then xmy = 1 else xmy = 0 end
if mesecon:rules_link(pos, zmypos) then zmy = 1 else zmy = 0 end
-- Backward compatibility
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},
description = "Button",
})
minetest.register_node("mesecons_button:button_on", {
drawtype = "nodebox",
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("mesecons_button:button_on", nil, mesecon.button_get_rules)

View File

@ -3,32 +3,30 @@
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"},
paramtype2="facedir",
groups = {dig_immediate=2, mesecon = 2},
description="Switch",
groups = {dig_immediate=2},
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", {
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",
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',
description="Switch",
})
mesecon:register_receptor("mesecons_switch:mesecon_switch_on", "mesecons_switch:mesecon_switch_off")
minetest.register_on_punchnode(function(pos, node, puncher)
if node.name == "mesecons_switch:mesecon_switch_on" then
mesecons = {receptor = {
state = mesecon.state.on
}},
on_punch = function(pos, node)
minetest.env:add_node(pos, {name="mesecons_switch:mesecon_switch_off", param2=node.param2})
nodeupdate(pos)
mesecon:receptor_off(pos)
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({
output = '"mesecons_switch:mesecon_switch_off" 2',