forked from minetest-mods/mesecons
Code cleanup
This commit is contained in:
@ -11,71 +11,7 @@
|
||||
--
|
||||
-- See the documentation on the forum for additional information, especially about crafting
|
||||
--
|
||||
--Quick Developer documentation for the mesecon API
|
||||
--=================================================
|
||||
--
|
||||
--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...)
|
||||
-- For developer documentation see the Developers' section on mesecons.tk
|
||||
|
||||
|
||||
-- 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
|
||||
|
||||
function mesecon:add_receptor_node(nodename, 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
|
||||
function mesecon:add_receptor_node(name, rules, get_rules) --rules table is optional; if rules depend on param2 pass (nodename, nil, function get_rules)
|
||||
if get_rules==nil and rules==nil then
|
||||
rules=mesecon:get_rules("default")
|
||||
end
|
||||
mesecon.receptors[i]={}
|
||||
mesecon.receptors[i].name = nodename
|
||||
mesecon.receptors[i].rules = rules
|
||||
mesecon.receptors[i].get_rules = get_rules
|
||||
table.insert(mesecon.receptors, {name = name, rules = rules, get_rules = get_rules})
|
||||
end
|
||||
|
||||
function mesecon:add_receptor_node_off(nodename, rules, get_rules)
|
||||
local i=1
|
||||
repeat
|
||||
if mesecon.receptors_off[i]==nil then break end
|
||||
i=i+1
|
||||
until false
|
||||
function mesecon:add_receptor_node_off(name, rules, get_rules)
|
||||
if get_rules==nil and rules==nil then
|
||||
rules=mesecon:get_rules("default")
|
||||
end
|
||||
mesecon.receptors_off[i]={}
|
||||
mesecon.receptors_off[i].name = nodename
|
||||
mesecon.receptors_off[i].rules = rules
|
||||
mesecon.receptors_off[i].get_rules = get_rules
|
||||
table.insert(mesecon.receptors_off, {name = name, rules = rules, get_rules = get_rules})
|
||||
end
|
||||
|
||||
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
|
||||
rules=mesecon:get_rules("default")
|
||||
end
|
||||
mesecon.effectors[i]={}
|
||||
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
|
||||
table.insert(mesecon.effectors, {onstate = onstate, offstate = offstate, input_rules = input_rules, get_input_rules = get_input_rules})
|
||||
end
|
||||
|
||||
function mesecon:receptor_on(pos, rules)
|
||||
@ -148,12 +59,11 @@ function mesecon:receptor_on(pos, rules)
|
||||
rules = mesecon:get_rules("default")
|
||||
end
|
||||
|
||||
local i = 1
|
||||
while rules[i]~=nil do
|
||||
for i, rule in ipairs(rules) do
|
||||
local np = {}
|
||||
np.x = pos.x + rules[i].x
|
||||
np.y = pos.y + rules[i].y
|
||||
np.z = pos.z + rules[i].z
|
||||
np.x = pos.x + rule.x
|
||||
np.y = pos.y + rule.y
|
||||
np.z = pos.z + rule.z
|
||||
if mesecon:rules_link(pos, np, rules) then
|
||||
mesecon:turnon(np, pos)
|
||||
end
|
||||
@ -167,12 +77,12 @@ function mesecon:receptor_off(pos, rules)
|
||||
end
|
||||
|
||||
local connected = false
|
||||
local i = 1
|
||||
while rules[i]~=nil do
|
||||
|
||||
for i, rule in ipairs(rules) do
|
||||
local np = {}
|
||||
np.x = pos.x + rules[i].x
|
||||
np.y = pos.y + rules[i].y
|
||||
np.z = pos.z + rules[i].z
|
||||
np.x = pos.x + rule.x
|
||||
np.y = pos.y + rule.y
|
||||
np.z = pos.z + rule.z
|
||||
if mesecon:rules_link(pos, np, rules) and mesecon:connected_to_pw_src(np) == false then
|
||||
mesecon:turnoff(np, pos)
|
||||
end
|
||||
@ -181,45 +91,22 @@ function mesecon:receptor_off(pos, rules)
|
||||
end
|
||||
|
||||
function mesecon:register_on_signal_on(action)
|
||||
local i = 1
|
||||
repeat
|
||||
i=i+1
|
||||
if mesecon.actions_on[i]==nil then break end
|
||||
until false
|
||||
mesecon.actions_on[i]=action
|
||||
table.insert(mesecon.actions_on, action)
|
||||
end
|
||||
|
||||
function mesecon:register_on_signal_off(action)
|
||||
local i = 1
|
||||
repeat
|
||||
i=i+1
|
||||
if mesecon.actions_off[i]==nil then break end
|
||||
until false
|
||||
mesecon.actions_off[i]=action
|
||||
table.insert(mesecon.actions_off, action)
|
||||
end
|
||||
|
||||
function mesecon:register_on_signal_change(action)
|
||||
local i = 1
|
||||
repeat
|
||||
i=i+1
|
||||
if mesecon.actions_change[i]==nil then break end
|
||||
until false
|
||||
mesecon.actions_change[i]=action
|
||||
table.insert(mesecon.actions_change, action)
|
||||
end
|
||||
|
||||
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
|
||||
rules = mesecon:get_rules("default")
|
||||
end
|
||||
mesecon.conductors[i]={}
|
||||
mesecon.conductors[i].onstate = onstate
|
||||
mesecon.conductors[i].offstate = offstate
|
||||
mesecon.conductors[i].rules = rules
|
||||
mesecon.conductors[i].get_rules = get_rules
|
||||
table.insert(mesecon.conductors, {onstate = onstate, offstate = offstate, rules = rules, get_rules = get_rules})
|
||||
end
|
||||
|
||||
mesecon:add_rules("default",
|
||||
|
Reference in New Issue
Block a user