2021-08-22 18:59:49 +02:00
|
|
|
-- |\ /| ____ ____ ____ _____ ____ _____
|
|
|
|
-- | \ / | | | | | | | |\ | |
|
|
|
|
-- | \/ | |___ ____ |___ | | | | \ | |____
|
|
|
|
-- | | | | | | | | | \ | |
|
|
|
|
-- | | |___ ____| |___ |____ |____| | \| ____|
|
|
|
|
-- by Jeija, Uberi (Temperest), sfan5, VanessaE, Hawk777 and contributors
|
|
|
|
--
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- This mod adds mesecons[=minecraft redstone] and different receptors/effectors to minetest.
|
|
|
|
-- See the documentation on the forum for additional information, especially about crafting
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- For basic development resources, see http://mesecons.net/developers.html
|
|
|
|
--
|
|
|
|
--
|
|
|
|
--
|
|
|
|
--Quick draft for the mesecons array in the node's definition
|
|
|
|
--mesecons =
|
|
|
|
--{
|
|
|
|
-- receptor =
|
|
|
|
-- {
|
|
|
|
-- state = mesecon.state.on/off
|
|
|
|
-- rules = rules/get_rules
|
|
|
|
-- },
|
|
|
|
-- effector =
|
|
|
|
-- {
|
|
|
|
-- action_on = function
|
|
|
|
-- action_off = function
|
|
|
|
-- action_change = function
|
|
|
|
-- rules = rules/get_rules
|
|
|
|
-- },
|
|
|
|
-- conductor =
|
|
|
|
-- {
|
|
|
|
-- state = mesecon.state.on/off
|
|
|
|
-- offstate = opposite state (for state = on only)
|
|
|
|
-- onstate = opposite state (for state = off only)
|
|
|
|
-- rules = rules/get_rules
|
|
|
|
-- }
|
|
|
|
--}
|
|
|
|
|
|
|
|
-- PUBLIC VARIABLES
|
|
|
|
mesecon={} -- contains all functions and all global variables
|
|
|
|
mesecon.queue={} -- contains the ActionQueue
|
|
|
|
mesecon.queue.funcs={} -- contains all ActionQueue functions
|
|
|
|
|
|
|
|
-- Settings
|
|
|
|
dofile(minetest.get_modpath("mesecons").."/settings.lua")
|
|
|
|
|
|
|
|
-- Utilities like comparing positions,
|
|
|
|
-- adding positions and rules,
|
|
|
|
-- mostly things that make the source look cleaner
|
|
|
|
dofile(minetest.get_modpath("mesecons").."/util.lua");
|
|
|
|
|
|
|
|
-- Presets (eg default rules)
|
|
|
|
dofile(minetest.get_modpath("mesecons").."/presets.lua");
|
|
|
|
|
|
|
|
-- The ActionQueue
|
|
|
|
-- Saves all the actions that have to be execute in the future
|
|
|
|
dofile(minetest.get_modpath("mesecons").."/actionqueue.lua");
|
|
|
|
|
|
|
|
-- Internal stuff
|
|
|
|
-- This is the most important file
|
|
|
|
-- it handles signal transmission and basically everything else
|
|
|
|
-- It is also responsible for managing the nodedef things,
|
|
|
|
-- like calling action_on/off/change
|
|
|
|
dofile(minetest.get_modpath("mesecons").."/internal.lua");
|
|
|
|
|
|
|
|
-- API
|
|
|
|
-- these are the only functions you need to remember
|
|
|
|
|
2021-08-28 10:14:32 +02:00
|
|
|
mesecon.queue:add_function("receptor_on", function (pos, rules, voltage, opaquespread)
|
2021-08-22 18:59:49 +02:00
|
|
|
mesecon.vm_begin()
|
|
|
|
|
|
|
|
rules = rules or mesecon.rules.default
|
|
|
|
|
2021-08-28 10:14:32 +02:00
|
|
|
local node = mesecon.get_node_force(pos)
|
|
|
|
local os=opaquespread or mesecon.is_receptor_opaquespread(node.name)
|
|
|
|
|
2021-08-22 18:59:49 +02:00
|
|
|
-- Call turnon on all linking positions
|
|
|
|
for _, rule in pairs(mesecon.flattenrules(rules)) do
|
|
|
|
local np = vector.add(pos, rule)
|
|
|
|
local rulenames = mesecon.rules_link_rule_all(pos, rule)
|
|
|
|
for _, rulename in pairs(rulenames) do
|
2021-08-28 10:14:32 +02:00
|
|
|
if os then rulename.opaquespread=os end
|
|
|
|
minetest.log("action", mesecon.postostring(pos) .. "-->" .. "receptor_on-> ".."np="..mesecon.postostring(np).." link="..mesecon.tabletostring(rulename).. " v=" .. (voltage or "nil"))
|
2021-08-22 19:10:58 +02:00
|
|
|
mesecon.turnon(np, rulename, voltage)
|
2021-08-22 18:59:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mesecon.vm_commit()
|
|
|
|
end)
|
|
|
|
|
2021-08-28 10:14:32 +02:00
|
|
|
function mesecon.receptor_on(pos, rules, voltage, opaquespread)
|
|
|
|
mesecon.queue:add_action(pos, "receptor_on", {rules, voltage, opaquespread}, nil, rules)
|
2021-08-22 18:59:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
mesecon.queue:add_function("receptor_off", function (pos, rules)
|
|
|
|
rules = rules or mesecon.rules.default
|
2021-09-03 08:08:00 +02:00
|
|
|
minetest.log("action", mesecon.postostring(pos) .. "-->" .. "receptor_off-> ".." rules="..mesecon.tabletostring(rules))
|
2021-08-22 18:59:49 +02:00
|
|
|
-- Call turnoff on all linking positions
|
|
|
|
for _, rule in ipairs(mesecon.flattenrules(rules)) do
|
|
|
|
local np = vector.add(pos, rule)
|
|
|
|
local rulenames = mesecon.rules_link_rule_all(pos, rule)
|
|
|
|
for _, rulename in ipairs(rulenames) do
|
|
|
|
mesecon.vm_begin()
|
|
|
|
mesecon.changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
|
|
|
|
|
|
|
|
-- Turnoff returns true if turnoff process was successful, no onstate receptor
|
|
|
|
-- was found along the way. Commit changes that were made in voxelmanip. If turnoff
|
|
|
|
-- returns true, an onstate receptor was found, abort voxelmanip transaction.
|
2021-08-22 19:10:58 +02:00
|
|
|
local res, rec_on = mesecon.turnoff(np, rulename)
|
2021-09-03 08:08:00 +02:00
|
|
|
minetest.log("action", mesecon.postostring(np) .. "-->" .. "receptor_off.turnoff-> ".." rulename="..mesecon.tabletostring(rulename).." res="..mesecon.tabletostring(res).." rec_on="..mesecon.tabletostring(rec_on))
|
2021-08-22 19:10:58 +02:00
|
|
|
if (res) then
|
2021-08-22 18:59:49 +02:00
|
|
|
mesecon.vm_commit()
|
|
|
|
else
|
|
|
|
mesecon.vm_abort()
|
2021-08-22 19:10:58 +02:00
|
|
|
for _, rec in pairs(rec_on) do
|
|
|
|
--rules??? -> mesecon.receptor_get_rules(node)
|
|
|
|
local node = mesecon.get_node_force(rec)
|
2021-08-22 19:16:46 +02:00
|
|
|
|
|
|
|
local meta = minetest.get_meta(rec)
|
|
|
|
local voltage = meta:get_string("mesecon_voltage")~="" and meta:get_int("mesecon_voltage") or 15
|
|
|
|
mesecon.receptor_on(rec, mesecon.receptor_get_rules(node), voltage)
|
2021-08-22 19:10:58 +02:00
|
|
|
end
|
2021-08-22 18:59:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
function mesecon.receptor_off(pos, rules)
|
2021-09-03 08:08:00 +02:00
|
|
|
minetest.log("action", mesecon.postostring(pos) .. "-->" .. "receptor_off-> ".." rules="..mesecon.tabletostring(rules) .. "trace="..debug.traceback())
|
2021-08-22 18:59:49 +02:00
|
|
|
mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
|
|
|
|
end
|
|
|
|
|
|
|
|
--Services like turnoff receptor on dignode and so on
|
|
|
|
dofile(minetest.get_modpath("mesecons").."/services.lua");
|