Code cleanup

This commit is contained in:
Jeija 2012-09-05 23:52:09 +02:00
parent 69c6f1a440
commit 7e7e1de725
2 changed files with 113 additions and 278 deletions

View File

@ -11,71 +11,7 @@
-- --
-- See the documentation on the forum for additional information, especially about crafting -- See the documentation on the forum for additional information, especially about crafting
-- --
--Quick Developer documentation for the mesecon API -- For developer documentation see the Developers' section on mesecons.tk
--=================================================
--
--RECEPTORS
--
--A receptor is a node that emits power, e.g. a solar panel, a switch or a power plant.
--Usually you create two blocks per receptor that have to be switched when switching the on/off state:
-- # An off-state node (e.g. mesecons:mesecon_switch_off"
-- # An on-state node (e.g. mesecons:mesecon_switch_on"
--The on-state and off-state nodes should be registered in the mesecon api,
--so that the Mesecon circuit can be recalculated. This can be done using
--
--mesecon:add_receptor_node(nodename) -- for on-state node
--mesecon:add_receptor_node_off(nodename) -- for off-state node
--example: mesecon:add_receptor_node("mesecons:mesecon_switch_on")
--
--Turning receptors on and off
--Usually the receptor has to turn on and off. For this, you have to
-- # Remove the node and replace it with the node in the other state (e.g. replace on by off)
-- # Send the event to the mesecon circuit by using the api functions
-- mesecon:receptor_on (pos, rules) } These functions take the position of your receptor
-- mesecon:receptor_off(pos, rules) } as their parameter.
--
--You can specify the rules using the rules parameter. If you don't want special rules, just leave it out
--e.g. if you want to use the "pressureplate" rules, you use this command:
--mesecon:receptor_on (pos, mesecon:get_rules("pressureplate"))
--The rules can be manipulated by several rotate functions:
--rules=mesecon:rotate_rules_right/left/up/down(rules)
--
--
--
--EFFECTORS
--
--A receptor is a node that uses power and transfers the signal to a mechanical, optical whatever
--event. e.g. the meselamp, the movestone or the removestone.
--
--There are two callback functions for receptors.
-- # function mesecon:register_on_signal_on (action)
-- # function mesecon:register_on_signal_off(action)
--
--These functions will be called for each block next to a mesecon conductor.
--
--Example: The removestone
--The removestone only uses one callback: The mesecon:register_on_signal_on function
--
--mesecon:register_on_signal_on(function(pos, node) -- As the action prameter you have to use a function
-- if node.name=="mesecons:removestone" then -- Check if it really is removestone. If you wouldn't use this, every node next to mesecons would be removed
-- minetest.env:remove_node(pos) -- The action: The removestone is removed
-- end -- end of if
--end) -- end of the function, )=end of the parameters of mesecon:register_on_signal_on
--
--CONDUCTORS: (new feature!! yay)
--You can specify your custom conductors using
--# mesecon:register_conductor(onstate, offstate)
-- onstate=the conductor's nodename when it is turned on
-- offstate=the conductor's nodename when it is turned off
--
--As you can see, conductors need an offstate and an onstate node, just like receptors
--mesecons:mesecon_on / mesecons:mesecon_off are the default conductors
--Other conductors connect to other conductors. It's always "the same energy"
--! As there is no special drawtype, conductors don't connect to others visually,
--but it works in fact.
--
--The function # mesecon:register_conductor(onstate, offstate) is the only thing you need to do,
--the mod does everything else for you (turn the conductor on and off...)
-- PUBLIC VARIABLES -- PUBLIC VARIABLES
@ -97,50 +33,25 @@ dofile(minetest.get_modpath("mesecons").."/internal.lua");
-- API API API API API API API API API API API API API API API API API API -- API API API API API API API API API API API API API API API API API API
function mesecon:add_receptor_node(nodename, rules, get_rules) --rules table is optional; if rules depend on param2 pass (nodename, nil, function get_rules) function mesecon:add_receptor_node(name, rules, get_rules) --rules table is optional; if rules depend on param2 pass (nodename, nil, function get_rules)
local i=1
repeat
if mesecon.receptors[i]==nil then break end
i=i+1
until false
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
mesecon.receptors[i]={} table.insert(mesecon.receptors, {name = name, rules = rules, get_rules = get_rules})
mesecon.receptors[i].name = nodename
mesecon.receptors[i].rules = rules
mesecon.receptors[i].get_rules = get_rules
end end
function mesecon:add_receptor_node_off(nodename, rules, get_rules) function mesecon:add_receptor_node_off(name, rules, get_rules)
local i=1
repeat
if mesecon.receptors_off[i]==nil then break end
i=i+1
until false
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
mesecon.receptors_off[i]={} table.insert(mesecon.receptors_off, {name = name, rules = rules, get_rules = get_rules})
mesecon.receptors_off[i].name = nodename
mesecon.receptors_off[i].rules = rules
mesecon.receptors_off[i].get_rules = get_rules
end end
function mesecon:register_effector(onstate, offstate, input_rules, get_input_rules) function mesecon:register_effector(onstate, offstate, input_rules, get_input_rules)
local i=1
repeat
if mesecon.effectors[i]==nil then break end
i=i+1
until false
if get_input_rules==nil and input_rules==nil then if get_input_rules==nil and input_rules==nil then
rules=mesecon:get_rules("default") rules=mesecon:get_rules("default")
end end
mesecon.effectors[i]={} table.insert(mesecon.effectors, {onstate = onstate, offstate = offstate, input_rules = input_rules, get_input_rules = get_input_rules})
mesecon.effectors[i].onstate = onstate
mesecon.effectors[i].offstate = offstate
mesecon.effectors[i].input_rules = input_rules
mesecon.effectors[i].get_input_rules = get_input_rules
end end
function mesecon:receptor_on(pos, rules) function mesecon:receptor_on(pos, rules)
@ -148,12 +59,11 @@ function mesecon:receptor_on(pos, rules)
rules = mesecon:get_rules("default") rules = mesecon:get_rules("default")
end end
local i = 1 for i, rule in ipairs(rules) do
while rules[i]~=nil do
local np = {} local np = {}
np.x = pos.x + rules[i].x np.x = pos.x + rule.x
np.y = pos.y + rules[i].y np.y = pos.y + rule.y
np.z = pos.z + rules[i].z np.z = pos.z + rule.z
if mesecon:rules_link(pos, np, rules) then if mesecon:rules_link(pos, np, rules) then
mesecon:turnon(np, pos) mesecon:turnon(np, pos)
end end
@ -167,12 +77,12 @@ function mesecon:receptor_off(pos, rules)
end end
local connected = false local connected = false
local i = 1
while rules[i]~=nil do for i, rule in ipairs(rules) do
local np = {} local np = {}
np.x = pos.x + rules[i].x np.x = pos.x + rule.x
np.y = pos.y + rules[i].y np.y = pos.y + rule.y
np.z = pos.z + rules[i].z np.z = pos.z + rule.z
if mesecon:rules_link(pos, np, rules) and mesecon:connected_to_pw_src(np) == false then if mesecon:rules_link(pos, np, rules) and mesecon:connected_to_pw_src(np) == false then
mesecon:turnoff(np, pos) mesecon:turnoff(np, pos)
end end
@ -181,45 +91,22 @@ function mesecon:receptor_off(pos, rules)
end end
function mesecon:register_on_signal_on(action) function mesecon:register_on_signal_on(action)
local i = 1 table.insert(mesecon.actions_on, action)
repeat
i=i+1
if mesecon.actions_on[i]==nil then break end
until false
mesecon.actions_on[i]=action
end end
function mesecon:register_on_signal_off(action) function mesecon:register_on_signal_off(action)
local i = 1 table.insert(mesecon.actions_off, action)
repeat
i=i+1
if mesecon.actions_off[i]==nil then break end
until false
mesecon.actions_off[i]=action
end end
function mesecon:register_on_signal_change(action) function mesecon:register_on_signal_change(action)
local i = 1 table.insert(mesecon.actions_change, action)
repeat
i=i+1
if mesecon.actions_change[i]==nil then break end
until false
mesecon.actions_change[i]=action
end end
function mesecon:register_conductor (onstate, offstate, rules, get_rules) function mesecon:register_conductor (onstate, offstate, rules, get_rules)
local i = 1
while mesecon.conductors[i]~=nil do
i = i + 1
end
if rules == nil then if rules == nil then
rules = mesecon:get_rules("default") rules = mesecon:get_rules("default")
end end
mesecon.conductors[i]={} table.insert(mesecon.conductors, {onstate = onstate, offstate = offstate, rules = rules, get_rules = get_rules})
mesecon.conductors[i].onstate = onstate
mesecon.conductors[i].offstate = offstate
mesecon.conductors[i].rules = rules
mesecon.conductors[i].get_rules = get_rules
end end
mesecon:add_rules("default", mesecon:add_rules("default",

View File

@ -3,8 +3,8 @@
--Receptors --Receptors
function mesecon:is_receptor_node(nodename) function mesecon:is_receptor_node(nodename)
local i = 1 local i = 1
while mesecon.receptors[i] ~= nil do for i, receptor in ipairs(mesecon.receptors) do
if mesecon.receptors[i].name == nodename then if receptor.name == nodename then
return true return true
end end
i = i + 1 i = i + 1
@ -14,8 +14,8 @@ end
function mesecon:is_receptor_node_off(nodename, pos, ownpos) function mesecon:is_receptor_node_off(nodename, pos, ownpos)
local i = 1 local i = 1
while mesecon.receptors_off[i] ~= nil do for i, receptor in ipairs(mesecon.receptors_off) do
if mesecon.receptors_off[i].name == nodename then if receptor.name == nodename then
return true return true
end end
i = i + 1 i = i + 1
@ -24,55 +24,47 @@ function mesecon:is_receptor_node_off(nodename, pos, ownpos)
end end
function mesecon:receptor_get_rules(node) function mesecon:receptor_get_rules(node)
local i = 1 for i, receptor in ipairs(mesecon.receptors) do
while(mesecon.receptors[i] ~= nil) do if receptor.name == node.name then
if mesecon.receptors[i].name == node.name then if receptor.get_rules ~= nil then
if mesecon.receptors[i].get_rules ~= nil then return receptor.get_rules(node.param2)
return mesecon.receptors[i].get_rules(node.param2)
elseif mesecon.receptors[i].rules ~=nil then elseif mesecon.receptors[i].rules ~=nil then
return mesecon.receptors[i].rules return receptor.rules
else else
return mesecon:get_rules("default") return mesecon:get_rules("default")
end end
end end
i = i + 1
end end
local i = 1 for i, receptor in ipairs(mesecon.receptors_off) do
while(mesecon.receptors_off[i] ~= nil) do if receptor.name == node.name then
if mesecon.receptors_off[i].name == node.name then if receptor.get_rules ~= nil then
if mesecon.receptors_off[i].get_rules ~= nil then return receptor.get_rules(node.param2)
return mesecon.receptors_off[i].get_rules(node.param2)
elseif mesecon.receptors_off[i].rules ~=nil then elseif mesecon.receptors_off[i].rules ~=nil then
return mesecon.receptors_off[i].rules return receptor.rules
else else
return mesecon:get_rules("default") return mesecon:get_rules("default")
end end
end end
i = i + 1
end end
return nil return nil
end end
-- Effectors -- Effectors
function mesecon:is_effector_on(nodename) function mesecon:is_effector_on(nodename)
local i = 1 for i, effector in ipairs(mesecon.effectors) do
while mesecon.effectors[i] ~= nil do if effector.onstate == nodename then
if mesecon.effectors[i].onstate == nodename then
return true return true
end end
i = i + 1
end end
return false return false
end end
function mesecon:is_effector_off(nodename) function mesecon:is_effector_off(nodename)
local i = 1 for i, effector in ipairs(mesecon.effectors) do
while mesecon.effectors[i] ~= nil do if effector.offstate == nodename then
if mesecon.effectors[i].offstate == nodename then
return true return true
end end
i = i + 1
end end
return false return false
end end
@ -82,19 +74,17 @@ function mesecon:is_effector(nodename)
end end
function mesecon:effector_get_input_rules(node) function mesecon:effector_get_input_rules(node)
local i = 1 for i, effector in ipairs(mesecon.effectors) do
while(mesecon.effectors[i] ~= nil) do if effector.onstate == node.name
if mesecon.effectors[i].onstate == node.name or effector.offstate == node.name then
or mesecon.effectors[i].offstate == node.name then if effector.get_input_rules ~= nil then
if mesecon.effectors[i].get_input_rules ~= nil then return effector.get_input_rules(node.param2)
return mesecon.effectors[i].get_input_rules(node.param2)
elseif mesecon.effectors[i].input_rules ~=nil then elseif mesecon.effectors[i].input_rules ~=nil then
return mesecon.effectors[i].input_rules return effector.input_rules
else else
return mesecon:get_rules("default") return mesecon:get_rules("default")
end end
end end
i = i + 1
end end
end end
@ -102,101 +92,75 @@ end
function mesecon:activate(pos) function mesecon:activate(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1 for i, action in ipairs(mesecon.actions_on) do
repeat action(pos, node)
i=i+1
if mesecon.actions_on[i]~=nil then mesecon.actions_on[i](pos, node)
else break
end end
until false
end end
function mesecon:deactivate(pos) function mesecon:deactivate(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1 local i = 1
repeat for i, action in ipairs(mesecon.actions_off) do
i=i+1 action(pos, node)
if mesecon.actions_off[i]~=nil then mesecon.actions_off[i](pos, node)
else break
end end
until false
end end
function mesecon:changesignal(pos) function mesecon:changesignal(pos)
local node = minetest.env:get_node(pos) local node = minetest.env:get_node(pos)
local i = 1 local i = 1
repeat for i, action in ipairs(mesecon.actions_change) do
i=i+1 action(pos, node)
if mesecon.actions_change[i]~=nil then mesecon.actions_change[i](pos, node)
else break
end end
until false
end end
--Rules --Rules
function mesecon:add_rules(name, rules) function mesecon:add_rules(name, rules)
local i = 1 table.insert(mesecon.rules, {name = name, rules = rules})
while mesecon.rules[i]~=nil do
i=i+1
end
mesecon.rules[i]={}
mesecon.rules[i].name=name
mesecon.rules[i].rules=rules
end end
function mesecon:get_rules(name) function mesecon:get_rules(name)
local i = 1 for i, rule in ipairs(mesecon.rules) do
while mesecon.rules[i]~=nil do if rule.name==name then
if mesecon.rules[i].name==name then return rule.rules
return mesecon.rules[i].rules
end end
i=i+1
end end
end end
--Conductor system stuff --Conductor system stuff
function mesecon:get_conductor_on(offstate) function mesecon:get_conductor_on(offstate)
local i = 1 for i, conductor in ipairs(mesecon.conductors) do
while mesecon.conductors[i]~=nil do if conductor.offstate == offstate then
if mesecon.conductors[i].offstate == offstate then return conductor.onstate
return mesecon.conductors[i].onstate
end end
i=i+1
end end
return false return false
end end
function mesecon:get_conductor_off(onstate) function mesecon:get_conductor_off(onstate)
local i = 1 for i, conductor in ipairs(mesecon.conductors) do
while mesecon.conductors[i]~=nil do if conductor.onstate == onstate then
if mesecon.conductors[i].onstate == onstate then return conductor.offstate
return mesecon.conductors[i].offstate
end end
i=i+1
end end
return false return false
end end
function mesecon:is_conductor_on(name) function mesecon:is_conductor_on(name)
local i = 1 for i, conductor in ipairs(mesecon.conductors) do
while mesecon.conductors[i]~=nil do if conductor.onstate == name then
if mesecon.conductors[i].onstate == name then
return true return true
end end
i=i+1
end end
return false return false
end end
function mesecon:is_conductor_off(name) function mesecon:is_conductor_off(name)
local i = 1 for i, conductor in ipairs(mesecon.conductors) do
while mesecon.conductors[i]~=nil do if conductor.offstate == name then
if mesecon.conductors[i].offstate == name then
return true return true
end end
i=i+1
end end
return false return false
end end
@ -206,17 +170,15 @@ function mesecon:is_conductor(name)
end end
function mesecon:conductor_get_rules(node) function mesecon:conductor_get_rules(node)
local i = 1 for i, conductor in ipairs(mesecon.conductors) do
while mesecon.conductors[i] ~= nil do if conductor.onstate == node.name
if mesecon.conductors[i].onstate == node.name or conductor.offstate == node.name then
or mesecon.conductors[i].offstate == node.name then if conductor.get_rules ~= nil then
if mesecon.conductors[i].get_rules ~= nil then return conductor.get_rules(node.param2)
return mesecon.conductors[i].get_rules(node.param2)
else else
return mesecon.conductors[i].rules return conductor.rules
end end
end end
i = i + 1
end end
end end
@ -245,16 +207,15 @@ function mesecon:turnon(pos)
local rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
minetest.env:add_node(pos, {name=mesecon:get_conductor_on(node.name), param2 = node.param2}) minetest.env:add_node(pos, {name=mesecon:get_conductor_on(node.name), param2 = node.param2})
while rules[i]~=nil do for i, rule in ipairs(rules) do
local np = {} local np = {}
np.x = pos.x + rules[i].x np.x = pos.x + rule.x
np.y = pos.y + rules[i].y np.y = pos.y + rule.y
np.z = pos.z + rules[i].z np.z = pos.z + rule.z
if mesecon:rules_link(pos, np) then if mesecon:rules_link(pos, np) then
mesecon:turnon(np) mesecon:turnon(np)
end end
i=i+1
end end
end end
@ -274,17 +235,15 @@ function mesecon:turnoff(pos) --receptor rules used because output could have be
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})
while rules[i]~=nil do for i, rule in ipairs(rules) do
local np = { local np = {
x = pos.x + rules[i].x, x = pos.x + rule.x,
y = pos.y + rules[i].y, y = pos.y + rule.y,
z = pos.z + rules[i].z,} z = pos.z + rule.z,}
if mesecon:rules_link(pos, np) then if mesecon:rules_link(pos, np) then
mesecon:turnoff(np) mesecon:turnoff(np)
end end
i = i + 1
end end
end end
@ -319,18 +278,17 @@ function mesecon:connected_to_pw_src(pos, checked)
local rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
local i = 1 local i = 1
while rules[i] ~= nil do for i, rule in ipairs(rules) do
local np = {} local np = {}
np.x = pos.x + rules[i].x np.x = pos.x + rule.x
np.y = pos.y + rules[i].y np.y = pos.y + rule.y
np.z = pos.z + rules[i].z np.z = pos.z + rule.z
if mesecon:rules_link(pos, np) then if mesecon:rules_link(pos, np) then
connected, checked = mesecon:connected_to_pw_src(np, checked) connected, checked = mesecon:connected_to_pw_src(np, checked)
if connected then if connected then
return true return true
end end
end end
i=i+1
end end
return false, checked return false, checked
end end
@ -364,21 +322,19 @@ function mesecon:rules_link(output, input, dug_outputrules) --output/input are p
end end
while outputrules[k] ~= nil do for k, outputrule in ipairs(outputrules) do
if outputrules[k].x + output.x == input.x if outputrule.x + output.x == input.x
and outputrules[k].y + output.y == input.y and outputrule.y + output.y == input.y
and outputrules[k].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 l = 1
while inputrules[l] ~= nil do for k, inputrule in ipairs(inputrules) do
if inputrules[l].x + input.x == output.x if inputrule.x + input.x == output.x
and inputrules[l].y + input.y == output.y and inputrule.y + input.y == output.y
and inputrules[l].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
return true return true
end end
l = l + 1
end end
end end
k = k + 1
end end
return false return false
end end
@ -405,18 +361,17 @@ function mesecon:is_powered_by_conductor(pos)
return false return false
end end
while rules[j] ~= nil do for i, rule in ipairs(rules) do
local con_pos = { local con_pos = {
x = pos.x + rules[j].x, x = pos.x + rule.x,
y = pos.y + rules[j].y, y = pos.y + rule.y,
z = pos.z + rules[j].z} z = pos.z + rule.z}
con_node = minetest.env:get_node(con_pos) con_node = minetest.env:get_node(con_pos)
if mesecon:is_conductor_on(con_node.name) and mesecon:rules_link(con_pos, pos) then if mesecon:is_conductor_on(con_node.name) and mesecon:rules_link(con_pos, pos) then
return true return true
end end
j = j + 1
end end
return false return false
@ -440,18 +395,17 @@ function mesecon:is_powered_by_receptor(pos)
return false return false
end end
while rules[j] ~= nil do for i, rule in ipairs(rules) do
local rcpt_pos = { local rcpt_pos = {
x = pos.x + rules[j].x, x = pos.x + rule.x,
y = pos.y + rules[j].y, y = pos.y + rule.y,
z = pos.z + rules[j].z} z = pos.z + rule.z}
rcpt_node = minetest.env:get_node(rcpt_pos) rcpt_node = minetest.env:get_node(rcpt_pos)
if mesecon:is_receptor_node(rcpt_node.name) and mesecon:rules_link(rcpt_pos, pos) then if mesecon:is_receptor_node(rcpt_node.name) and mesecon:rules_link(rcpt_pos, pos) then
return true return true
end end
j = j + 1
end end
return false return false
@ -477,12 +431,11 @@ end
function mesecon:rotate_rules_right(rules) function mesecon:rotate_rules_right(rules)
local i=1 local i=1
local nr={}; local nr={};
while rules[i]~=nil do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
nr[i].z=rules[i].x nr[i].z=rule.x
nr[i].x=-rules[i].z nr[i].x=-rule.z
nr[i].y=rules[i].y nr[i].y=rule.y
i=i+1
end end
return nr return nr
end end
@ -490,12 +443,11 @@ end
function mesecon:rotate_rules_left(rules) function mesecon:rotate_rules_left(rules)
local i=1 local i=1
local nr={}; local nr={};
while rules[i]~=nil do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
nr[i].z=-rules[i].x nr[i].z=-rules[i].x
nr[i].x=rules[i].z nr[i].x=rules[i].z
nr[i].y=rules[i].y nr[i].y=rules[i].y
i=i+1
end end
return nr return nr
end end
@ -503,12 +455,11 @@ end
function mesecon:rotate_rules_down(rules) function mesecon:rotate_rules_down(rules)
local i=1 local i=1
local nr={}; local nr={};
while rules[i]~=nil do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
nr[i].y=rules[i].x nr[i].y=rule.x
nr[i].x=-rules[i].y nr[i].x=-rule.y
nr[i].z=rules[i].z nr[i].z=rule.z
i=i+1
end end
return nr return nr
end end
@ -516,14 +467,11 @@ end
function mesecon:rotate_rules_up(rules) function mesecon:rotate_rules_up(rules)
local i=1 local i=1
local nr={}; local nr={};
while rules[i]~=nil do for i, rule in ipairs(rules) do
nr[i]={} nr[i]={}
nr[i].y=-rules[i].x nr[i].y=-rule.x
nr[i].x=rules[i].y nr[i].x=rule.y
nr[i].z=rules[i].z nr[i].z=rule.z
i=i+1
end end
return nr return nr
end end
--TODO: is_powered returns the position (see services.lua!!!)