summaryrefslogtreecommitdiff
path: root/internal.lua
diff options
context:
space:
mode:
authorAuke Kok <sofar+github@foo-projects.org>2016-05-27 16:08:53 -0700
committerAuke Kok <sofar+github@foo-projects.org>2016-05-27 16:08:53 -0700
commit7ecb29e87f1f272f92d0fec871dd525a80a9537c (patch)
tree9c57001d333fc36a0561a3e8bfdac0df0108532b /internal.lua
parent4c743f9c4d1d263c5c36edb6a5dbaab7e2bdc6e4 (diff)
Convert digilines to a mod (not modpack). (#32)
Digilines is probably used by most people in its entirety. I've retained the ability to disable inventory, rtc, lightsensor and LCD by the minetest settings "diglines_enable_rtc" etc.. If set to "false", these components will not be loaded. It is assumed by default that these are enabled. In the conversion the digilines_lcd:lcd node was renamed to digilines:lcd (same for all the other nodes). To retain backwards compatibility I've provided aliases for each of these nodes.
Diffstat (limited to 'internal.lua')
-rw-r--r--internal.lua90
1 files changed, 90 insertions, 0 deletions
diff --git a/internal.lua b/internal.lua
new file mode 100644
index 0000000..2319c16
--- /dev/null
+++ b/internal.lua
@@ -0,0 +1,90 @@
+function digiline:getspec(node)
+ if not minetest.registered_nodes[node.name] then return false end
+ return minetest.registered_nodes[node.name].digiline
+end
+
+function digiline:importrules(spec, node)
+ if type(spec) == 'function' then
+ return spec(node)
+ elseif spec then
+ return spec
+ else
+ return digiline.rules.default
+ end
+end
+
+function digiline:getAnyInputRules(pos)
+ local node = minetest.get_node(pos)
+ local spec = digiline:getspec(node)
+ if not spec then return end
+
+ if spec.wire then
+ return digiline:importrules(spec.wire.rules, node)
+ end
+ if spec.effector then
+ return digiline:importrules(spec.effector.rules, node)
+ end
+end
+
+function digiline:getAnyOutputRules(pos)
+ local node = minetest.get_node(pos)
+ local spec = digiline:getspec(node)
+ if not spec then return end
+
+ if spec.wire then
+ return digiline:importrules(spec.wire.rules, node)
+ end
+ if spec.receptor then
+ return digiline:importrules(spec.receptor.rules, node)
+ end
+end
+
+function digiline:rules_link(output, input)
+ local outputrules = digiline:getAnyOutputRules(output)
+ local inputrules = digiline:getAnyInputRules (input)
+
+ if not outputrules or not inputrules then return false end
+
+
+ for _, orule in ipairs(outputrules) do
+ if digiline:cmpPos(digiline:addPosRule(output, orule), input) then
+ for _, irule in ipairs(inputrules) do
+ if digiline:cmpPos(digiline:addPosRule(input, irule), output) then
+ return true
+ end
+ end
+ end
+ end
+ return false
+end
+
+function digiline:rules_link_anydir(output, input)
+ return digiline:rules_link(output, input)
+ or digiline:rules_link(input, output)
+end
+
+function digiline:transmit(pos, channel, msg, checked)
+ local checkedid = tostring(pos.x).."_"..tostring(pos.y).."_"..tostring(pos.z)
+ if checked[checkedid] then return end
+ checked[checkedid] = true
+
+ local node = minetest.get_node(pos)
+ local spec = digiline:getspec(node)
+ if not spec then return end
+
+
+ -- Effector actions --> Receive
+ if spec.effector then
+ spec.effector.action(pos, node, channel, msg)
+ end
+
+ -- Cable actions --> Transmit
+ if spec.wire then
+ local rules = digiline:importrules(spec.wire.rules, node)
+ for _,rule in ipairs(rules) do
+ if digiline:rules_link(pos, digiline:addPosRule(pos, rule)) then
+ digiline:transmit(digiline:addPosRule(pos, rule), channel, msg, checked)
+ end
+ end
+ end
+end