New effector system, effectors have to be registered with mesecon:register_effector(onstate, offstate, rules, function get_rules) rules and function get_rules are optional. You don't have to specify both onstate and offstate (required for e.g. removestone)

This commit is contained in:
Jeija 2012-08-13 20:17:45 +02:00
parent 24c50571f7
commit 00ce2540c2
14 changed files with 206 additions and 163 deletions

View File

@ -154,7 +154,7 @@ function mesecon:receptor_on(pos, rules)
np.x = pos.x + rules[i].x
np.y = pos.y + rules[i].y
np.z = pos.z + rules[i].z
mesecon:turnon(np)
mesecon:turnon(np, pos)
i=i+1
end
end
@ -164,7 +164,6 @@ function mesecon:receptor_off(pos, rules)
rules = mesecon:get_rules("default")
end
local checked = {}
local connected = false
local i = 1
while rules[i]~=nil do
@ -172,9 +171,9 @@ function mesecon:receptor_off(pos, rules)
np.x = pos.x + rules[i].x
np.y = pos.y + rules[i].y
np.z = pos.z + rules[i].z
connected, checked = mesecon:connected_to_pw_src(np, checked)
if not connected then
mesecon:turnoff(np)
connected = mesecon:connected_to_pw_src(np)
if connected == false then
mesecon:turnoff(np, pos)
end
i=i+1
end

View File

@ -1,5 +1,6 @@
-- INTERNAL API
-- INTERNAL
--Receptors
function mesecon:is_receptor_node(nodename)
local i = 1
while mesecon.receptors[i] ~= nil do
@ -53,6 +54,33 @@ function mesecon:receptor_get_rules(node)
return nil
end
-- Effectors
function mesecon:is_effector_on(nodename)
local i = 1
while mesecon.effectors[i] ~= nil do
if mesecon.effectors[i].onstate == nodename then
return true
end
i = i + 1
end
return false
end
function mesecon:is_effector_off(nodename)
local i = 1
while mesecon.effectors[i] ~= nil do
if mesecon.effectors[i].offstate == nodename then
return true
end
i = i + 1
end
return false
end
function mesecon:is_effector(nodename)
return mesecon:is_effector_on(nodename) or mesecon:is_effector_off(nodename)
end
function mesecon:effector_get_input_rules(node)
local i = 1
while(mesecon.effectors[i] ~= nil) do
@ -60,7 +88,7 @@ function mesecon:effector_get_input_rules(node)
or mesecon.effectors[i].offstate == node.name then
if mesecon.effectors[i].get_input_rules ~= nil then
return mesecon.effectors[i].get_input_rules(node.param2)
elseif mesecon.receptors[i].input_rules ~=nil then
elseif mesecon.effectors[i].input_rules ~=nil then
return mesecon.effectors[i].input_rules
else
return mesecon:get_rules("default")
@ -89,16 +117,16 @@ function mesecon:receptor_outputs (cpos, rpos) --cpos = conductor pos, rpos = re
return false
end
function mesecon:effector_inputs (cpos, rpos) --cpos = conductor pos, rpos = receptor pos
local rnode = minetest.env:get_node (rpos)
local rules = mesecon:effector_get_input_rules (rnode)
function mesecon:effector_inputs (srcpos, destpos)
local destnode = minetest.env:get_node (destpos)
local rules = mesecon:effector_get_input_rules (destnode)
if rules == nil then return false end
local i = 1
while rules[i] ~= nil do
if rpos.x + rules[i].x == cpos.x
and rpos.y + rules[i].y == cpos.y
and rpos.z + rules[i].z == cpos.z then
if destpos.x + rules[i].x == srcpos.x
and destpos.y + rules[i].y == srcpos.y
and destpos.z + rules[i].z == srcpos.z then
return true
end
i = i + 1
@ -214,59 +242,6 @@ function mesecon:is_conductor_off(name)
return false
end
--Rules rotation Functions:
function mesecon:rotate_rules_right(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].z=rules[i].x
nr[i].x=-rules[i].z
nr[i].y=rules[i].y
i=i+1
end
return nr
end
function mesecon:rotate_rules_left(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].z=-rules[i].x
nr[i].x=rules[i].z
nr[i].y=rules[i].y
i=i+1
end
return nr
end
function mesecon:rotate_rules_down(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].y=rules[i].x
nr[i].x=-rules[i].y
nr[i].z=rules[i].z
i=i+1
end
return nr
end
function mesecon:rotate_rules_up(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].y=-rules[i].x
nr[i].x=rules[i].y
nr[i].z=rules[i].z
i=i+1
end
return nr
end
function mesecon:is_power_on(pos)
local node = minetest.env:get_node(pos)
if mesecon:is_conductor_on(node.name) or mesecon:is_receptor_node(node.name) then
@ -278,60 +253,62 @@ end
function mesecon:is_power_off(pos)
local node = minetest.env:get_node(pos)
if mesecon:is_conductor_off(node.name) or mesecon:is_receptor_node_off(node.name) then
return 1
return true
end
return 0
return false
end
function mesecon:turnon(pos)
function mesecon:turnon(pos, sourcepos)
local node = minetest.env:get_node(pos)
local rules = mesecon:get_rules("default") --TODO: Use rules of conductor
local i = 1
if mesecon:is_conductor_off(node.name) then
minetest.env:add_node(pos, {name=mesecon:get_conductor_on(node.name)})
nodeupdate(pos)
rules = mesecon:get_rules("default") --TODO: Use rules of conductor
local i=1
while rules[i]~=nil do
local np = {}
np.x = pos.x + rules[i].x
np.y = pos.y + rules[i].y
np.z = pos.z + rules[i].z
mesecon:turnon(np)
mesecon:turnon(np, pos)
i=i+1
end
end
if mesecon:is_effector(node.name) then
if mesecon:effector_inputs(sourcepos, pos) then
mesecon:changesignal(pos)
if minetest.get_item_group(node.name, "mesecon_effector_off") == 1 then
mesecon:activate(pos)
if mesecon:is_effector_off(node.name) then mesecon:activate(pos) end
end
end
end
function mesecon:turnoff(pos)
function mesecon:turnoff(pos, sourcepos)
local node = minetest.env:get_node(pos)
rules = mesecon:get_rules("default") --TODO: Use rules of conductor
local i = 1
if mesecon:is_conductor_on(node.name) then
minetest.env:add_node(pos, {name=mesecon:get_conductor_off(node.name)})
nodeupdate(pos)
rules = mesecon:get_rules("default") --TODO: Use ruels of conductor
local i = 1
while rules[i]~=nil do
local np = {}
np.x = pos.x + rules[i].x
np.y = pos.y + rules[i].y
np.z = pos.z + rules[i].z
mesecon:turnoff(np)
mesecon:turnoff(np, pos)
i = i + 1
end
end
mesecon:changesignal(pos) --Changesignal is always thrown because nodes can be both receptors and effectors
if minetest.get_item_group(node.name, "mesecon_effector_on") == 1 and
not mesecon:is_powered(pos) then --Check if the signal comes from another source
--Send Signals to effectors:
mesecon:deactivate(pos)
if mesecon:is_effector(node.name) then
if mesecon:effector_inputs(sourcepos, pos) then
mesecon:changesignal(pos)
if mesecon:is_effector_on(node.name) then mesecon:deactivate(pos) end
end
end
end
@ -456,3 +433,56 @@ end
function compare_pos(pos1, pos2)
return pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z
end
--Rules rotation Functions:
function mesecon:rotate_rules_right(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].z=rules[i].x
nr[i].x=-rules[i].z
nr[i].y=rules[i].y
i=i+1
end
return nr
end
function mesecon:rotate_rules_left(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].z=-rules[i].x
nr[i].x=rules[i].z
nr[i].y=rules[i].y
i=i+1
end
return nr
end
function mesecon:rotate_rules_down(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].y=rules[i].x
nr[i].x=-rules[i].y
nr[i].z=rules[i].z
i=i+1
end
return nr
end
function mesecon:rotate_rules_up(rules)
local i=1
local nr={};
while rules[i]~=nil do
nr[i]={}
nr[i].y=-rules[i].x
nr[i].x=rules[i].y
nr[i].z=rules[i].z
i=i+1
end
return nr
end

View File

@ -1,7 +1,7 @@
minetest.register_on_dignode(
function(pos, oldnode, digger)
if mesecon:is_conductor_on(oldnode.name) then
local i = 1
print("receptor_off")
mesecon:receptor_off(pos)
end
@ -19,7 +19,7 @@ minetest.register_on_placenode(
if mesecon:is_powered(pos) then
if mesecon:is_conductor_off(node.name) then
mesecon:turnon(pos)
mesecon:turnon(pos) -- in this case we don't need a source as the destination certainly is a conductor and not a receptor
else
mesecon:changesignal(pos)
mesecon:activate(pos)

View File

@ -19,10 +19,9 @@ for i = 1, 5 do
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
groups = {dig_immediate=2, mesecon = 2, mesecon_effector_on = 1},
groups = {dig_immediate=2, mesecon = 2},
description="Battery",
})
mesecon:add_receptor_node_off("mesecons_battery:battery_charging_"..i)
end
for i = 1, 5 do
@ -46,10 +45,11 @@ for i = 1, 5 do
type = "fixed",
fixed = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
},
groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 2, mesecon_effector_off = 1},
groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 2},
description="Battery",
})
mesecon:add_receptor_node("mesecons_battery:battery_discharging_"..i)
mesecon:register_effector("mesecons_battery:battery_discharging_"..i, "mesecons_battery:battery_charging_"..i)
end
minetest.register_on_placenode(function (pos, newnode, placer)

View File

@ -115,10 +115,6 @@ end)
mesecon.delayer_update = function(pos, node)
if string.find(node.name, "mesecons_delayer:delayer_off")~=nil then
local input_rules = mesecon.delayer_get_input_rules(node.param2)[1]
np = {x = pos.x + input_rules.x, y = pos.y + input_rules.y, z = pos.z + input_rules.z}
if mesecon:is_power_on(np) then
local time = 0
if node.name=="mesecons_delayer:delayer_off_1" then
minetest.env:add_node(pos, {name="mesecons_delayer:delayer_on_1", param2=node.param2})
@ -138,13 +134,8 @@ mesecon.delayer_update = function(pos, node)
end
minetest.after(time, mesecon.delayer_turnon, {pos=pos, param2=node.param2})
end
end
if string.find(node.name, "mesecons_delayer:delayer_on")~=nil then
local input_rules = mesecon.delayer_get_input_rules(node.param2)[1]
np = {x = pos.x + input_rules.x, y = pos.y + input_rules.y, z = pos.z + input_rules.z}
if not mesecon:is_power_on(np) then
local time = 0
if node.name=="mesecons_delayer:delayer_on_1" then
minetest.env:add_node(pos, {name="mesecons_delayer:delayer_off_1", param2=node.param2})
@ -165,7 +156,6 @@ mesecon.delayer_update = function(pos, node)
minetest.after(time, mesecon.delayer_turnoff, {pos=pos, param2=node.param2})
end
end
end
mesecon:register_on_signal_change(mesecon.delayer_update)

View File

@ -100,6 +100,7 @@ for i = 1, 4 do
},
on_punch = mesecons_door_transform
})
mesecon:register_effector("mesecons_door:door_"..i.."_"..j, "mesecons_door:door_"..i.."_"..j)
end
end
@ -125,6 +126,7 @@ minetest.register_node("mesecons_door:door", {
minetest.env:add_node(node_pos, {name = "mesecons_door:door_"..best_number.."_1"})
end
})
mesecon:register_effector("mesecons_door:door")
minetest.register_on_placenode(function(pos, newnode, placer)
local b_pos = {x = pos.x, y = pos.y - 1, z = pos.z}

View File

@ -71,3 +71,5 @@ mesecon:register_on_signal_off(function(pos, node)
nodeupdate(pos)
end
end)
mesecon:register_effector("mesecons_lamp:lamp_on", "mesecons_lamp:lamp_off")

View File

@ -8,7 +8,7 @@ function mesecon:lightstone_add(name, base_item, texture_off, texture_on)
minetest.register_node("mesecons_lightstone:lightstone_" .. name .. "_on", {
tile_images = {texture_on},
inventory_image = minetest.inventorycube(texture_on),
groups = {cracky=2,not_in_creative_inventory=1, mesecon_effector_on = 1, mesecon = 2},
groups = {cracky=2,not_in_creative_inventory=1, mesecon = 2},
drop = "node mesecons_lightstone:lightstone_" .. name .. "_off 1",
light_source = LIGHT_MAX-2,
description=name.." Lightstone",
@ -33,6 +33,7 @@ function mesecon:lightstone_add(name, base_item, texture_off, texture_on)
{'','group:mesecon_conductor_craftable',''},
}
})
mesecon:register_effector("mesecons_lightstone:lightstone_" .. name .. "_on", "mesecons_lightstone:lightstone_" .. name .. "_off")
end

View File

@ -1,3 +1,9 @@
mesecon:add_rules("microcontroller_all", { --flat rules (looks better with nodebox wires connection)
{x = 1, y = 0, z = 0 },
{x = 0, y = 0, z = 1 },
{x = -1, y = 0, z = 0},
{x = 0, y = 0, z = -1}})
EEPROM_SIZE = 255
for a = 0, 1 do
@ -6,9 +12,9 @@ for c = 0, 1 do
for d = 0, 1 do
local nodename = "mesecons_microcontroller:microcontroller"..tostring(d)..tostring(c)..tostring(b)..tostring(a)
if tostring(d)..tostring(c)..tostring(b)..tostring(a) ~= "0000" then
groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon_effector_on = 1, mesecon_effector_off = 0, mesecon = 2}
groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 3}
else
groups = {dig_immediate=2, mesecon_effector_on = 1, mesecon_effector_off = 0, mesecon = 2}
groups = {dig_immediate=2, mesecon = 3}
end
minetest.register_node(nodename, {
description = "Microcontroller",
@ -17,7 +23,6 @@ minetest.register_node(nodename, {
"jeija_microcontroller_top_"..tostring(d)..tostring(c)..tostring(b)..tostring(a)..".png",
"jeija_microcontroller_sides.png",
},
--inventory_image = "jeija_microcontroller_top_0000.png",
sunlight_propagates = true,
paramtype = "light",
@ -88,16 +93,17 @@ minetest.register_node(nodename, {
update_yc(pos)
end,
})
local rules={}
if (a == 1) then table.insert(rules, {x = -1, y = 0, z = 0}) end
if (b == 1) then table.insert(rules, {x = 0, y = 0, z = 1}) end
if (c == 1) then table.insert(rules, {x = 1, y = 0, z = 0}) end
if (d == 1) then table.insert(rules, {x = 0, y = 0, z = -1}) end
mesecon:add_rules(nodename, rules)
mesecon:register_effector(nodename, nodename, mesecon:get_rules("microcontroller_all"))
if nodename ~= "mesecons_microcontroller:microcontroller0000" then
mesecon:add_receptor_node(nodename, rules)
else
mesecon:add_receptor_node_off(nodename)
end
end
end

View File

@ -39,9 +39,10 @@ minetest.register_node("mesecons_movestones:movestone", {
tile_images = {"jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_side.png", "jeija_movestone_arrows.png", "jeija_movestone_arrows.png"},
paramtype2 = "facedir",
legacy_facedir_simple = true,
groups = {cracky=3, mesecon_effector_on = 1, mesecon_effector_off = 1},
groups = {cracky=3},
description="Movestone",
})
mesecon:register_effector("mesecons_movestones:movestone", "mesecons_movestones:movestone")
minetest.register_entity("mesecons_movestones:movestone_entity", {
physical = false,
@ -116,9 +117,10 @@ minetest.register_node("mesecons_movestones:sticky_movestone", {
inventory_image = minetest.inventorycube("jeija_sticky_movestone.png", "jeija_movestone_side.png", "jeija_movestone_side.png"),
paramtype2 = "facedir",
legacy_facedir_simple = true,
groups = {cracky=3, mesecon_effector_on = 1, mesecon_effector_off = 1},
groups = {cracky=3},
description="Sticky Movestone",
})
mesecon:register_effector("mesecons_movestones:sticky_movestone", "mesecons_movestones:sticky_movestone")
minetest.register_craft({
output = '"mesecons_movestones:sticky_movestone" 2',

View File

@ -1,7 +1,7 @@
minetest.register_node("mesecons_noteblock:noteblock", {
description = "Noteblock",
tile_images = {"mesecons_noteblock.png"},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon_effector_off = 1, mesecon = 2},
groups = {snappy=2,choppy=2,oddly_breakable_by_hand=2, mesecon = 2},
drawtype = "allfaces_optional",
visual_scale = 1.3,
paramtype="light",
@ -9,6 +9,7 @@ minetest.register_node("mesecons_noteblock:noteblock", {
minetest.env:add_node(pos, {name="mesecons_noteblock:noteblock", param2=0})
end
})
mesecon:register_effector("mesecons_noteblock:noteblock", "mesecons_noteblock:noteblock")
minetest.register_craft({
output = '"mesecons_noteblock:noteblock" 1',

View File

@ -3,7 +3,7 @@
minetest.register_node("mesecons_pistons:piston_normal", {
description = "Piston",
tiles = {"jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_side.png"},
groups = {cracky=3, mesecon_effector_on = 1, mesecon_effector_off = 1, mesecon = 2},
groups = {cracky=3, mesecon = 2},
paramtype2 = "facedir",
after_dig_node = function(pos, oldnode)
local dir = mesecon:piston_get_direction(oldnode)
@ -18,12 +18,13 @@ minetest.register_node("mesecons_pistons:piston_normal", {
end
end,
})
mesecon:register_effector("mesecons_pistons:piston_normal", "mesecons_pistons:piston_normal")
--registration sticky one:
minetest.register_node("mesecons_pistons:piston_sticky", {
description = "Sticky Piston",
tiles = {"jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_tb.png", "jeija_piston_sticky_side.png"},
groups = {cracky=3, mesecon_effector_on = 1, mesecon_effector_off = 1, mesecon = 2},
groups = {cracky=3, mesecon = 2},
paramtype2 = "facedir",
after_dig_node = function(pos, oldnode)
local dir = mesecon:piston_get_direction(oldnode)
@ -38,6 +39,7 @@ minetest.register_node("mesecons_pistons:piston_sticky", {
end
end,
})
mesecon:register_effector("mesecons_pistons:piston_sticky", "mesecons_pistons:piston_sticky")
minetest.register_craft({
output = '"mesecons_pistons:piston_normal" 2',

View File

@ -4,10 +4,12 @@ minetest.register_node("mesecons_random:removestone", {
tile_images = {"jeija_removestone.png"},
inventory_image = minetest.inventorycube("jeija_removestone_inv.png"),
material = minetest.digprop_stonelike(1.0),
groups = {cracky=3, mesecon_effector_off = 1, mesecon = 2},
groups = {cracky=3, mesecon = 2},
description="Removestone",
})
mesecon:register_effector(nil, "mesecons_random:removestone")
minetest.register_craft({
output = '"mesecons_random:removestone" 4',
recipe = {

View File

@ -53,22 +53,8 @@ minetest.register_abm({
interval = 1,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local pa = {x=0, y=0, z=0}
local rules=mesecon.torch_get_rules(minetest.env:get_node(pos).param2)
if node.param2 == 4 then
pa.z = -2
elseif node.param2 == 2 then
pa.x = -2
elseif node.param2 == 5 then
pa.z = 2
elseif node.param2 == 3 then
pa.x = 2
elseif node.param2 == 1 then
pa.y = 2
elseif node.param2 == 0 then
pa.y = -2
end
local pa = mesecon.torch_get_input_rules(node.param2)
local postc = {x=pos.x-pa.x, y=pos.y-pa.y, z=pos.z-pa.z}
if mesecon:is_power_on(postc) then
@ -101,6 +87,25 @@ mesecon.torch_get_rules = function(param2)
return rules
end
mesecon.torch_get_input_rules = function(param2)
local rules = {x=0, y=0, z=0}
if param2 == 4 then
rules.z = -2
elseif param2 == 2 then
rules.x = -2
elseif param2 == 5 then
rules.z = 2
elseif param2 == 3 then
rules.x = 2
elseif param2 == 1 then
rules.y = 2
elseif param2 == 0 then
rules.y = -2
end
return rules
end
mesecon:add_rules("mesecontorch",
{{x=1, y=0, z=0},
{x=0, y=0, z=1},
@ -110,6 +115,7 @@ mesecon:add_rules("mesecontorch",
mesecon:add_receptor_node("mesecons_torch:mesecon_torch_on", nil, mesecon.torch_get_rules)
mesecon:add_receptor_node_off("mesecons_torch:mesecon_torch_off", nil, mesecon.torch_get_rules)
mesecon:register_effector("mesecons_torch:mesecon_torch_on","mesecons_torch:mesecon_torch_off", nil,mesecon.torch_get_input_rules)
-- Param2 Table (Block Attached To)
-- 5 = z-1