1 Commits

Author SHA1 Message Date
7d8fd7a8df Fix issue #135 2014-01-05 19:55:45 +01:00
169 changed files with 657 additions and 763 deletions

View File

@ -1,114 +0,0 @@
mesecon.queue.actions={} -- contains all ActionQueue actions
function mesecon.queue:add_function(name, func)
mesecon.queue.funcs[name] = func
end
-- If add_action with twice the same overwritecheck and same position are called, the first one is overwritten
-- use overwritecheck nil to never overwrite, but just add the event to the queue
-- priority specifies the order actions are executed within one globalstep, highest first
-- should be between 0 and 1
function mesecon.queue:add_action(pos, func, params, time, overwritecheck, priority)
-- Create Action Table:
time = time or 0 -- time <= 0 --> execute, time > 0 --> wait time until execution
priority = priority or 1
local action = { pos=mesecon:tablecopy(pos),
func=func,
params=mesecon:tablecopy(params),
time=time,
owcheck=(overwritecheck and mesecon:tablecopy(overwritecheck)) or nil,
priority=priority}
local toremove = nil
-- Otherwise, add the action to the queue
if overwritecheck then -- check if old action has to be overwritten / removed:
for i, ac in ipairs(mesecon.queue.actions) do
if(mesecon:cmpPos(pos, ac.pos)
and mesecon:cmpAny(overwritecheck, ac.owcheck)) then
toremove = i
break
end
end
end
if (toremove ~= nil) then
table.remove(mesecon.queue.actions, toremove)
end
table.insert(mesecon.queue.actions, action)
end
-- execute the stored functions on a globalstep
-- if however, the pos of a function is not loaded (get_node_or_nil == nil), do NOT execute the function
-- this makes sure that resuming mesecons circuits when restarting minetest works fine
-- However, even that does not work in some cases, that's why we delay the time the globalsteps
-- start to be execute by 5 seconds
local get_highest_priority = function (actions)
local highestp = -1, highesti
for i, ac in ipairs(actions) do
if ac.priority > highestp then
highestp = ac.priority
highesti = i
end
end
return highesti
end
local m_time = 0
minetest.register_globalstep(function (dtime)
m_time = m_time + dtime
if (m_time < MESECONS_RESUMETIME) then return end -- don't even try if server has not been running for XY seconds
local actions = mesecon:tablecopy(mesecon.queue.actions)
local actions_now={}
mesecon.queue.actions = {}
-- sort actions into two categories:
-- those toexecute now (actions_now) and those to execute later (mesecon.queue.actions)
for i, ac in ipairs(actions) do
if ac.time > 0 then
ac.time = ac.time - dtime -- executed later
table.insert(mesecon.queue.actions, ac)
else
table.insert(actions_now, ac)
end
end
while(#actions_now > 0) do -- execute highest priorities first, until all are executed
local hp = get_highest_priority(actions_now)
mesecon.queue:execute(actions_now[hp])
table.remove(actions_now, hp)
end
end)
function mesecon.queue:execute(action)
mesecon.queue.funcs[action.func](action.pos, unpack(action.params))
end
-- Store and read the ActionQueue to / from a file
-- so that upcoming actions are remembered when the game
-- is restarted
local wpath = minetest.get_worldpath()
local function file2table(filename)
local f = io.open(filename, "r")
if f==nil then return {} end
local t = f:read("*all")
f:close()
if t=="" or t==nil then return {} end
return minetest.deserialize(t)
end
local function table2file(filename, table)
local f = io.open(filename, "w")
f:write(minetest.serialize(table))
f:close()
end
mesecon.queue.actions = file2table(wpath.."/mesecon_actionqueue")
minetest.register_on_shutdown(function()
mesecon.queue.actions = table2file(wpath.."/mesecon_actionqueue", mesecon.queue.actions)
end)

View File

@ -39,10 +39,40 @@
-- } -- }
--} --}
-- PUBLIC VARIABLES -- PUBLIC VARIABLES
mesecon={} -- contains all functions and all global variables mesecon={} -- contains all functions and all global variables
mesecon.queue={} -- contains the ActionQueue mesecon.actions_on={} -- Saves registered function callbacks for mesecon on | DEPRECATED
mesecon.queue.funcs={} -- contains all ActionQueue functions mesecon.actions_off={} -- Saves registered function callbacks for mesecon off | DEPRECATED
mesecon.actions_change={} -- Saves registered function callbacks for mesecon change | DEPRECATED
mesecon.receptors={} -- saves all information about receptors | DEPRECATED
mesecon.effectors={} -- saves all information about effectors | DEPRECATED
mesecon.conductors={} -- saves all information about conductors | DEPRECATED
local wpath = minetest.get_worldpath()
local function read_file(fn)
local f = io.open(fn, "r")
if f==nil then return {} end
local t = f:read("*all")
f:close()
if t=="" or t==nil then return {} end
return minetest.deserialize(t)
end
local function write_file(fn, tbl)
local f = io.open(fn, "w")
f:write(minetest.serialize(tbl))
f:close()
end
mesecon.to_update = read_file(wpath.."/mesecon_to_update")
mesecon.r_to_update = read_file(wpath.."/mesecon_r_to_update")
minetest.register_on_shutdown(function()
write_file(wpath.."/mesecon_to_update",mesecon.to_update)
write_file(wpath.."/mesecon_r_to_update",mesecon.r_to_update)
end)
-- Settings -- Settings
dofile(minetest.get_modpath("mesecons").."/settings.lua") dofile(minetest.get_modpath("mesecons").."/settings.lua")
@ -56,10 +86,6 @@ dofile(minetest.get_modpath("mesecons").."/presets.lua");
-- mostly things that make the source look cleaner -- mostly things that make the source look cleaner
dofile(minetest.get_modpath("mesecons").."/util.lua"); dofile(minetest.get_modpath("mesecons").."/util.lua");
-- The ActionQueue
-- Saves all the actions that have to be execute in the future
dofile(minetest.get_modpath("mesecons").."/actionqueue.lua");
-- Internal stuff -- Internal stuff
-- This is the most important file -- This is the most important file
-- it handles signal transmission and basically everything else -- it handles signal transmission and basically everything else
@ -75,20 +101,9 @@ dofile(minetest.get_modpath("mesecons").."/legacy.lua");
-- API -- API
-- these are the only functions you need to remember -- these are the only functions you need to remember
mesecon.queue:add_function("receptor_on", function (pos, rules) function mesecon:receptor_on_i(pos, rules)
rules = rules or mesecon.rules.default rules = rules or mesecon.rules.default
-- if area (any of the rule targets) is not loaded, keep trying and call this again later
for _, rule in ipairs(mesecon:flattenrules(rules)) do
local np = mesecon:addPosRule(pos, rule)
-- if area is not loaded, keep trying
if minetest.get_node_or_nil(np) == nil then
mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
return
end
end
-- execute action
for _, rule in ipairs(mesecon:flattenrules(rules)) do for _, rule in ipairs(mesecon:flattenrules(rules)) do
local np = mesecon:addPosRule(pos, rule) local np = mesecon:addPosRule(pos, rule)
local rulenames = mesecon:rules_link_rule_all(pos, rule) local rulenames = mesecon:rules_link_rule_all(pos, rule)
@ -96,24 +111,19 @@ mesecon.queue:add_function("receptor_on", function (pos, rules)
mesecon:turnon(np, rulename) mesecon:turnon(np, rulename)
end end
end end
end)
function mesecon:receptor_on(pos, rules)
mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
end end
mesecon.queue:add_function("receptor_off", function (pos, rules) function mesecon:receptor_on(pos, rules)
rules = rules or mesecon.rules.default if MESECONS_GLOBALSTEP then
rules = rules or mesecon.rules.default
-- if area (any of the rule targets) is not loaded, keep trying and call this again later mesecon.r_to_update[#mesecon.r_to_update+1]={pos=pos, rules=rules, action="on"}
for _, rule in ipairs(mesecon:flattenrules(rules)) do else
local np = mesecon:addPosRule(pos, rule) mesecon:receptor_on_i(pos, rules)
if minetest.get_node_or_nil(np) == nil then
mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
return
end
end end
end
function mesecon:receptor_off_i(pos, rules)
rules = rules or mesecon.rules.default
for _, rule in ipairs(mesecon:flattenrules(rules)) do for _, rule in ipairs(mesecon:flattenrules(rules)) do
local np = mesecon:addPosRule(pos, rule) local np = mesecon:addPosRule(pos, rule)
local rulenames = mesecon:rules_link_rule_all(pos, rule) local rulenames = mesecon:rules_link_rule_all(pos, rule)
@ -121,14 +131,19 @@ mesecon.queue:add_function("receptor_off", function (pos, rules)
if not mesecon:connected_to_receptor(np, mesecon:invertRule(rule)) then if not mesecon:connected_to_receptor(np, mesecon:invertRule(rule)) then
mesecon:turnoff(np, rulename) mesecon:turnoff(np, rulename)
else else
mesecon:changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2) mesecon:changesignal(np, minetest.get_node(np), rulename, mesecon.state.off)
end end
end end
end end
end) end
function mesecon:receptor_off(pos, rules) function mesecon:receptor_off(pos, rules)
mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules) if MESECONS_GLOBALSTEP then
rules = rules or mesecon.rules.default
mesecon.r_to_update[#mesecon.r_to_update+1]={pos=pos, rules=rules, action="off"}
else
mesecon:receptor_off_i(pos, rules)
end
end end

View File

@ -22,9 +22,9 @@
-- mesecon:effector_get_rules(node) --> Returns the input rules of the effector (mesecon.rules.default if none specified) -- mesecon:effector_get_rules(node) --> Returns the input rules of the effector (mesecon.rules.default if none specified)
-- SIGNALS -- SIGNALS
-- mesecon:activate(pos, node, recdepth) --> Activates the effector node at the specific pos (calls nodedef.mesecons.effector.action_on), higher recdepths are executed later -- mesecon:activate(pos, node) --> Activates the effector node at the specific pos (calls nodedef.mesecons.effector.action_on)
-- mesecon:deactivate(pos, node, recdepth) --> Deactivates the effector node at the specific pos (calls nodedef.mesecons.effector.action_off), " -- mesecon:deactivate(pos, node) --> Deactivates the effector node at the specific pos (calls nodedef.mesecons.effector.action_off)
-- mesecon:changesignal(pos, node, rulename, newstate) --> Changes the effector node at the specific pos (calls nodedef.mesecons.effector.action_change), " -- mesecon:changesignal(pos, node, rulename, newstate) --> Changes the effector node at the specific pos (calls nodedef.mesecons.effector.action_change)
-- RULES -- RULES
-- mesecon:add_rules(name, rules) | deprecated? --> Saves rules table by name -- mesecon:add_rules(name, rules) | deprecated? --> Saves rules table by name
@ -41,8 +41,8 @@
-- HIGH-LEVEL Internals -- HIGH-LEVEL Internals
-- mesecon:is_power_on(pos) --> Returns true if pos emits power in any way -- mesecon:is_power_on(pos) --> Returns true if pos emits power in any way
-- mesecon:is_power_off(pos) --> Returns true if pos does not emit power in any way -- mesecon:is_power_off(pos) --> Returns true if pos does not emit power in any way
-- mesecon:turnon(pos, rulename) --> Returns true whatever there is at pos. Calls itself for connected nodes (if pos is a conductor) --> recursive, the rulename is the name of the input rule that caused calling turnon; Uses third parameter recdepth internally to determine how far away the current node is from the initial pos as it uses recursion -- mesecon:turnon(pos, rulename) --> Returns true whatever there is at pos. Calls itself for connected nodes (if pos is a conductor) --> recursive, the rulename is the name of the input rule that caused calling turnon
-- mesecon:turnoff(pos, rulename) --> Turns off whatever there is at pos. Calls itself for connected nodes (if pos is a conductor) --> recursive, the rulename is the name of the input rule that caused calling turnoff; Uses third parameter recdepth internally to determine how far away the current node is from the initial pos as it uses recursion -- mesecon:turnoff(pos, rulename) --> Turns off whatever there is at pos. Calls itself for connected nodes (if pos is a conductor) --> recursive, the rulename is the name of the input rule that caused calling turnoff
-- mesecon:connected_to_receptor(pos) --> Returns true if pos is connected to a receptor directly or via conductors; calls itself if pos is a conductor --> recursive -- mesecon:connected_to_receptor(pos) --> Returns true if pos is connected to a receptor directly or via conductors; calls itself if pos is a conductor --> recursive
-- mesecon:rules_link(output, input, dug_outputrules) --> Returns true if outputposition + outputrules = inputposition and inputposition + inputrules = outputposition (if the two positions connect) -- mesecon:rules_link(output, input, dug_outputrules) --> Returns true if outputposition + outputrules = inputposition and inputposition + inputrules = outputposition (if the two positions connect)
-- mesecon:rules_link_anydir(outp., inp., d_outpr.) --> Same as rules mesecon:rules_link but also returns true if output and input are swapped -- mesecon:rules_link_anydir(outp., inp., d_outpr.) --> Same as rules mesecon:rules_link but also returns true if output and input are swapped
@ -177,76 +177,116 @@ function mesecon:effector_get_rules(node)
return mesecon.rules.default return mesecon.rules.default
end end
-- ####################### --Signals
-- # Signals (effectors) #
-- #######################
-- Activation: function mesecon:activate(pos, node, rulename)
mesecon.queue:add_function("activate", function (pos, rulename) if MESECONS_GLOBALSTEP then
node = minetest.get_node(pos) if rulename == nil then
effector = mesecon:get_effector(node.name) for _,rule in ipairs(mesecon:effector_get_rules(node)) do
mesecon:activate(pos, node, rule)
if effector and effector.action_on then end
effector.action_on(pos, node, rulename) return
end end
end) add_action(pos, "on", rulename)
else
function mesecon:activate(pos, node, rulename, recdepth) local effector = mesecon:get_effector(node.name)
if rulename == nil then if effector and effector.action_on then
for _,rule in ipairs(mesecon:effector_get_rules(node)) do effector.action_on (pos, node, rulename)
mesecon:activate(pos, node, rule, recdepth + 1)
end end
return
end end
mesecon.queue:add_action(pos, "activate", {rulename}, nil, rulename, 1 / recdepth)
end end
function mesecon:deactivate(pos, node, rulename)
-- Deactivation if MESECONS_GLOBALSTEP then
mesecon.queue:add_function("deactivate", function (pos, rulename) if rulename == nil then
node = minetest.get_node(pos) for _,rule in ipairs(mesecon:effector_get_rules(node)) do
effector = mesecon:get_effector(node.name) mesecon:deactivate(pos, node, rule)
end
if effector and effector.action_off then return
effector.action_off(pos, node, rulename) end
end add_action(pos, "off", rulename)
end) else
local effector = mesecon:get_effector(node.name)
function mesecon:deactivate(pos, node, rulename, recdepth) if effector and effector.action_off then
if rulename == nil then effector.action_off (pos, node, rulename)
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
mesecon:deactivate(pos, node, rule, recdepth + 1)
end end
return
end end
mesecon.queue:add_action(pos, "deactivate", {rulename}, nil, rulename, 1 / recdepth)
end end
function mesecon:changesignal(pos, node, rulename, newstate)
-- Change
mesecon.queue:add_function("change", function (pos, rulename, changetype) newstate = newstate or "on"
node = minetest.get_node(pos) --rulename = rulename or mesecon.rules.default
effector = mesecon:get_effector(node.name) if MESECONS_GLOBALSTEP then
if rulename == nil then
if effector and effector.action_change then for _,rule in ipairs(mesecon:effector_get_rules(node)) do
effector.action_change(pos, node, rulename, changetype) mesecon:changesignal(pos, node, rule, newstate)
end end
end)
function mesecon:changesignal(pos, node, rulename, newstate, recdepth)
if rulename == nil then
for _,rule in ipairs(mesecon:effector_get_rules(node)) do
mesecon:changesignal(pos, node, rule, newstate, recdepth + 1)
end
return return
end
add_action(pos, "c"..newstate, rulename)
else
local effector = mesecon:get_effector(node.name)
if effector and effector.action_change then
effector.action_change (pos, node, rulename, newstate)
end
end end
mesecon.queue:add_action(pos, "change", {rulename, newstate}, nil, rulename, 1 / recdepth)
end end
-- ######### function execute_actions(dtime)
-- # Rules # "Database" for rulenames local nactions = mesecon.to_update
-- ######### mesecon.to_update = {}
for _,i in ipairs(nactions) do
node = minetest.get_node(i.pos)
if node.name=="ignore" then
add_action(i.pos, i.action, i.rname)
else
effector = mesecon:get_effector(node.name)
if i.action == "on" then
if effector and effector.action_on then
effector.action_on(i.pos, node, i.rname)
end
elseif i.action == "off" then
if effector and effector.action_off then
effector.action_off(i.pos, node, i.rname)
end
elseif i.action == "con" then
if effector and effector.action_change then
effector.action_change(i.pos, node, i.rname, "on")
end
elseif i.action == "coff" then
if effector and effector.action_change then
effector.action_change(i.pos, node, i.rname, "off")
end
end
end
end
local nactions = mesecon.r_to_update
mesecon.r_to_update = {}
for _,i in ipairs(nactions) do
if i.action == "on" then
mesecon:receptor_on_i(i.pos, i.rules)
else
mesecon:receptor_off_i(i.pos,i.rules)
end
end
end
minetest.register_globalstep(execute_actions)
function add_action(pos, action, rname)
for i, update in ipairs(mesecon.to_update) do
-- check if action for this node already exist, if so correct it:
if mesecon:cmpPos(pos, update.pos) and mesecon:cmpPos(update.rname, rname) then
mesecon.to_update[i].action = action
return -- action added (as correction), so return now
end
end
table.insert(mesecon.to_update, {pos = pos, action = action, rname = rname})
end
--Rules
function mesecon:add_rules(name, rules) function mesecon:add_rules(name, rules)
mesecon.rules[name] = rules mesecon.rules[name] = rules
@ -365,15 +405,8 @@ function mesecon:is_power_off(pos, rulename)
return false return false
end end
function mesecon:turnon(pos, rulename, recdepth) function mesecon:turnon(pos, rulename)
recdepth = recdepth or 2
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if(node.name == "ignore") then
-- try turning on later again
mesecon.queue:add_action(
pos, "turnon", {rulename, recdepth + 1}, nil, true)
end
if mesecon:is_conductor_off(node, rulename) then if mesecon:is_conductor_off(node, rulename) then
local rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
@ -381,7 +414,7 @@ function mesecon:turnon(pos, rulename, recdepth)
if not rulename then if not rulename then
for _, rule in ipairs(mesecon:flattenrules(rules)) do for _, rule in ipairs(mesecon:flattenrules(rules)) do
if mesecon:connected_to_receptor(pos, rule) then if mesecon:connected_to_receptor(pos, rule) then
mesecon:turnon(pos, rule, recdepth + 1) mesecon:turnon(pos, rule)
end end
end end
return return
@ -391,71 +424,54 @@ function mesecon:turnon(pos, rulename, recdepth)
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
local np = mesecon:addPosRule(pos, rule) local np = mesecon:addPosRule(pos, rule)
if(minetest.get_node(np).name == "ignore") then local rulenames = mesecon:rules_link_rule_all(pos, rule)
-- try turning on later again
mesecon.queue:add_action(
np, "turnon", {rulename, recdepth + 1}, nil, true)
else
local rulenames = mesecon:rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do for _, rulename in ipairs(rulenames) do
mesecon:turnon(np, rulename, recdepth + 1) mesecon:turnon(np, rulename)
end
end end
end end
elseif mesecon:is_effector(node.name) then elseif mesecon:is_effector(node.name) then
mesecon:changesignal(pos, node, rulename, mesecon.state.on, recdepth) mesecon:changesignal(pos, node, rulename, mesecon.state.on)
if mesecon:is_effector_off(node.name) then if mesecon:is_effector_off(node.name) then
mesecon:activate(pos, node, rulename, recdepth) mesecon:activate(pos, node, rulename)
end end
end end
end end
mesecon.queue:add_function("turnon", function (pos, rulename, recdepth) function mesecon:turnoff(pos, rulename)
mesecon:turnon(pos, rulename, recdepth)
end)
function mesecon:turnoff(pos, rulename, recdepth)
recdepth = recdepth or 2
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
if(node.name == "ignore") then
-- try turning on later again
mesecon.queue:add_action(
pos, "turnoff", {rulename, recdepth + 1}, nil, true)
end
if mesecon:is_conductor_on(node, rulename) then if mesecon:is_conductor_on(node, rulename) then
local rules = mesecon:conductor_get_rules(node) local rules = mesecon:conductor_get_rules(node)
--[[
if not rulename then
for _, rule in ipairs(mesecon:flattenrules(rules)) do
if mesecon:is_powered(pos, rule) then
mesecon:turnoff(pos, rule)
end
end
return
end
--]]
minetest.swap_node(pos, {name = mesecon:get_conductor_off(node, rulename), param2 = node.param2}) minetest.swap_node(pos, {name = mesecon:get_conductor_off(node, rulename), param2 = node.param2})
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
local np = mesecon:addPosRule(pos, rule) local np = mesecon:addPosRule(pos, rule)
if(minetest.get_node(np).name == "ignore") then local rulenames = mesecon:rules_link_rule_all(pos, rule)
-- try turning on later again
mesecon.queue:add_action(
np, "turnoff", {rulename, recdepth + 1}, nil, true)
else
local rulenames = mesecon:rules_link_rule_all(pos, rule)
for _, rulename in ipairs(rulenames) do for _, rulename in ipairs(rulenames) do
mesecon:turnoff(np, rulename, recdepth + 1) mesecon:turnoff(np, rulename)
end
end end
end end
elseif mesecon:is_effector(node.name) then elseif mesecon:is_effector(node.name) then
mesecon:changesignal(pos, node, rulename, mesecon.state.off, recdepth) mesecon:changesignal(pos, node, rulename, mesecon.state.off)
if mesecon:is_effector_on(node.name) if mesecon:is_effector_on(node.name)
and not mesecon:is_powered(pos) then and not mesecon:is_powered(pos) then
mesecon:deactivate(pos, node, rulename, recdepth + 1) mesecon:deactivate(pos, node, rulename)
end end
end end
end end
mesecon.queue:add_function("turnoff", function (pos, rulename, recdepth)
mesecon:turnoff(pos, rulename, recdepth)
end)
function mesecon:connected_to_receptor(pos, rulename) function mesecon:connected_to_receptor(pos, rulename)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -465,10 +481,9 @@ function mesecon:connected_to_receptor(pos, rulename)
if not rules then return false end if not rules then return false end
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule) local np = mesecon:addPosRule(pos, rule)
for _, rname in ipairs(rulenames) do if mesecon:rules_link(np, pos) then
local np = mesecon:addPosRule(pos, rname) if mesecon:find_receptor_on(np, {}, mesecon:invertRule(rule)) then
if mesecon:find_receptor_on(np, {}, mesecon:invertRule(rname)) then
return true return true
end end
end end
@ -498,10 +513,9 @@ function mesecon:find_receptor_on(pos, checked, rulename)
-- add current position to checked -- add current position to checked
table.insert(checked, {x=pos.x, y=pos.y, z=pos.z, metaindex = metaindex}) table.insert(checked, {x=pos.x, y=pos.y, z=pos.z, metaindex = metaindex})
for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule) local np = mesecon:addPosRule(pos, rule)
for _, rname in ipairs(rulenames) do if mesecon:rules_link(np, pos) then
local np = mesecon:addPosRule(pos, rname) if mesecon:find_receptor_on(np, checked, mesecon:invertRule(rule)) then
if mesecon:find_receptor_on(np, checked, mesecon:invertRule(rname)) then
return true return true
end end
end end
@ -564,26 +578,6 @@ function mesecon:rules_link_rule_all(output, rule) --output/input are positions
return rules return rules
end end
function mesecon:rules_link_rule_all_inverted(input, rule)
--local irule = mesecon:invertRule(rule)
local output = mesecon:addPosRule(input, rule)
local outputnode = minetest.get_node(output)
local outputrules = mesecon:get_any_outputrules (outputnode)
if not outputrules then
return {}
end
local rules = {}
for _, outputrule in ipairs(mesecon:flattenrules(outputrules)) do
if mesecon:cmpPos(mesecon:addPosRule(output, outputrule), input) then
if outputrule.sx == nil or rule.sx == nil or mesecon:cmpSpecial(outputrule, rule) then
rules[#rules+1] = mesecon:invertRule(outputrule)
end
end
end
return rules
end
function mesecon:rules_link_anydir(pos1, pos2) function mesecon:rules_link_anydir(pos1, pos2)
return mesecon:rules_link(pos1, pos2) or mesecon:rules_link(pos2, pos1) return mesecon:rules_link(pos1, pos2) or mesecon:rules_link(pos2, pos1)
end end
@ -595,23 +589,21 @@ function mesecon:is_powered(pos, rule)
if not rule then if not rule then
for _, rule in ipairs(mesecon:flattenrules(rules)) do for _, rule in ipairs(mesecon:flattenrules(rules)) do
local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule) local np = mesecon:addPosRule(pos, rule)
for _, rname in ipairs(rulenames) do local nn = minetest.get_node(np)
local np = mesecon:addPosRule(pos, rname)
local nn = minetest.get_node(np) if (mesecon:is_conductor_on (nn, mesecon:invertRule(rule)) or mesecon:is_receptor_on (nn.name))
if (mesecon:is_conductor_on (nn, mesecon:invertRule(rname)) or mesecon:is_receptor_on (nn.name)) then and mesecon:rules_link(np, pos) then
return true return true
end
end end
end end
else else
local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule) local np = mesecon:addPosRule(pos, rule)
for _, rname in ipairs(rulenames) do local nn = minetest.get_node(np)
local np = mesecon:addPosRule(pos, rname)
local nn = minetest.get_node(np) if (mesecon:is_conductor_on (nn, mesecon:invertRule(rule)) or mesecon:is_receptor_on (nn.name))
if (mesecon:is_conductor_on (nn, mesecon:invertRule(rname)) or mesecon:is_receptor_on (nn.name)) then and mesecon:rules_link(np, pos) then
return true return true
end
end end
end end

View File

@ -2,31 +2,4 @@ minetest.swap_node = minetest.swap_node or function(pos, node)
local data = minetest.get_meta(pos):to_table() local data = minetest.get_meta(pos):to_table()
minetest.add_node(pos, node) minetest.add_node(pos, node)
minetest.get_meta(pos):from_table(data) minetest.get_meta(pos):from_table(data)
end end
local rules = {}
rules.a = {x = -1, y = 0, z = 0, name="A"}
rules.b = {x = 0, y = 0, z = 1, name="B"}
rules.c = {x = 1, y = 0, z = 0, name="C"}
rules.d = {x = 0, y = 0, z = -1, name="D"}
function legacy_update_ports(pos)
local meta = minetest.get_meta(pos)
L = {
a = mesecon:is_power_on(mesecon:addPosRule(pos, rules.a),
mesecon:invertRule(rules.a)) and
mesecon:rules_link(mesecon:addPosRule(pos, rules.a), pos),
b = mesecon:is_power_on(mesecon:addPosRule(pos, rules.b),
mesecon:invertRule(rules.b)) and
mesecon:rules_link(mesecon:addPosRule(pos, rules.b), pos),
c = mesecon:is_power_on(mesecon:addPosRule(pos, rules.c),
mesecon:invertRule(rules.c)) and
mesecon:rules_link(mesecon:addPosRule(pos, rules.c), pos),
d = mesecon:is_power_on(mesecon:addPosRule(pos, rules.d),
mesecon:invertRule(rules.d)) and
mesecon:rules_link(mesecon:addPosRule(pos, rules.d), pos),
}
local n = (L.a and 1 or 0) + (L.b and 2 or 0) + (L.c and 4 or 0) + (L.d and 8 or 0) + 1
meta:set_int("real_portstates", n)
return L
end

View File

@ -1,76 +1,38 @@
-- Dig and place services
mesecon.on_placenode = function (pos, node) mesecon.on_placenode = function (pos, node)
-- Receptors: Send on signal when active
if mesecon:is_receptor_on(node.name) then if mesecon:is_receptor_on(node.name) then
mesecon:receptor_on(pos, mesecon:receptor_get_rules(node)) mesecon:receptor_on(pos, mesecon:receptor_get_rules(node))
end elseif mesecon:is_powered(pos) then
if mesecon:is_conductor(node.name) then
-- Conductors: Send turnon signal when powered or replace by respective offstate conductor
-- if placed conductor is an onstate one
if mesecon:is_conductor(node.name) then
if mesecon:is_powered(pos) then
-- also call receptor_on if itself is powered already, so that neighboring
-- conductors will be activated (when pushing an on-conductor with a piston)
mesecon:turnon (pos) mesecon:turnon (pos)
mesecon:receptor_on (pos, mesecon:conductor_get_rules(node)) --mesecon:receptor_on (pos, mesecon:conductor_get_rules(node))
elseif mesecon:is_conductor_off(node.name) then
minetest.swap_node(pos, {name = mesecon:get_conductor_off(node)})
end
end
-- Effectors: Send changesignal and activate or deactivate
if mesecon:is_effector(node.name) then
if mesecon:is_powered(pos) then
mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on", 1)
mesecon:activate(pos, node, nil, 1)
else else
mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "off", 1) mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on")
mesecon:deactivate(pos, node, nil, 1) mesecon:activate(pos, node)
end end
elseif mesecon:is_conductor_on(node) then
minetest.swap_node(pos, {name = mesecon:get_conductor_off(node)})
elseif mesecon:is_effector_on (node.name) then
mesecon:deactivate(pos, node)
end end
end end
mesecon.on_dignode = function (pos, node) mesecon.on_dignode = function (pos, node)
if mesecon:is_conductor_on(node) then if mesecon:is_conductor_on(node) then
mesecon:receptor_off(pos, mesecon:conductor_get_rules(node)) mesecon:receptor_off_i(pos, mesecon:conductor_get_rules(node))
elseif mesecon:is_receptor_on(node.name) then elseif mesecon:is_receptor_on(node.name) then
mesecon:receptor_off(pos, mesecon:receptor_get_rules(node)) mesecon:receptor_off(pos, mesecon:receptor_get_rules(node))
end end
end end
minetest.register_abm({
nodenames = {"group:overheat"},
interval = 1.0,
chance = 1,
action = function(pos, node, active_object_count, active_object_count_wider)
local meta = minetest.get_meta(pos)
meta:set_int("heat",0)
end,
})
minetest.register_on_placenode(mesecon.on_placenode) minetest.register_on_placenode(mesecon.on_placenode)
minetest.register_on_dignode(mesecon.on_dignode) minetest.register_on_dignode(mesecon.on_dignode)
-- Overheating service for fast circuits
-- returns true if heat is too high
mesecon.do_overheat = function(pos)
local meta = minetest.get_meta(pos)
local heat = meta:get_int("heat") or 0
heat = heat + 1
meta:set_int("heat", heat)
if heat < OVERHEAT_MAX then
mesecon.queue:add_action(pos, "cooldown", {}, 1, nil, 0)
else
return true
end
return false
end
mesecon.queue:add_function("cooldown", function (pos)
if minetest.get_item_group(minetest.get_node(pos).name, "overheat") == 0 then
return -- node has been moved, this one does not use overheating - ignore
end
local meta = minetest.get_meta(pos)
local heat = meta:get_int("heat")
if (heat > 0) then
meta:set_int("heat", heat - 1)
end
end)

View File

@ -1,12 +1,9 @@
-- SETTINGS -- SETTINGS
BLINKY_PLANT_INTERVAL = 3 BLINKY_PLANT_INTERVAL = 3
NEW_STYLE_WIRES = true -- true = new nodebox wires, false = old raillike wires NEW_STYLE_WIRES = true -- true = new nodebox wires, false = old raillike wires
PRESSURE_PLATE_INTERVAL = 0.1 PRESSURE_PLATE_INTERVAL = 0.1
OBJECT_DETECTOR_RADIUS = 6 OBJECT_DETECTOR_RADIUS = 6
PISTON_MAXIMUM_PUSH = 15 PISTON_MAXIMUM_PUSH = 15
MOVESTONE_MAXIMUM_PUSH = 100 MOVESTONE_MAXIMUM_PUSH = 100
MESECONS_RESUMETIME = 4 -- time to wait when starting the server before MESECONS_GLOBALSTEP = true -- true = receptors/effectors won't be updated
-- processing the ActionQueue, don't set this too low -- until next globalstep, decreases server load
OVERHEAT_MAX = 20 -- maximum heat of any component that directly sends an output
-- signal when the input changes (e.g. luacontroller, gates)
-- Unit: actions per second, checks are every 1 second

View File

@ -169,7 +169,6 @@ function mesecon:cmpSpecial(r1, r2)
end end
function mesecon:tablecopy(table) -- deep table copy function mesecon:tablecopy(table) -- deep table copy
if type(table) ~= "table" then return table end -- no need to copy
local newtable = {} local newtable = {}
for idx, item in pairs(table) do for idx, item in pairs(table) do
@ -182,14 +181,3 @@ function mesecon:tablecopy(table) -- deep table copy
return newtable return newtable
end end
function mesecon:cmpAny(t1, t2)
if type(t1) ~= type(t2) then return false end
if type(t1) ~= "table" and type(t2) ~= "table" then return t1 == t2 end
for i, e in pairs(t1) do
if not mesecon:cmpAny(e, t2[i]) then return false end
end
return true
end

View File

@ -23,6 +23,14 @@ minetest.register_chatcommand("tell", {
end end
}) })
minetest.register_chatcommand("tellme", {
params = "<text>",
description = "Say <text> to yourself",
func = function(name, param)
minetest.chat_send_player(name, param, false)
end
})
minetest.register_chatcommand("hp", { minetest.register_chatcommand("hp", {
params = "<name> <value>", params = "<name> <value>",
description = "Set health of <name> to <value> hitpoints", description = "Set health of <name> to <value> hitpoints",
@ -42,13 +50,16 @@ minetest.register_chatcommand("hp", {
end end
}) })
local function initialize_data(meta) local initialize_data = function(meta, player, command, param)
local commands = meta:get_string("commands")
meta:set_string("formspec", meta:set_string("formspec",
"invsize[9,5;]" .. "invsize[9,6;]" ..
"textarea[0.5,0.5;8.5,4;commands;Commands;"..commands.."]" .. "field[1,1;7.5,1;player;Player;" .. player .. "]" ..
"label[1,3.8;@nearest, @farthest, and @random are replaced by the respective player names]" .. "button[1.3,2;2,1;nearest;Nearest]" ..
"button_exit[3.3,4.5;2,1;submit;Submit]") "button[3.3,2;2,1;farthest;Farthest]" ..
"button[5.3,2;2,1;random;Random]" ..
"field[1,4;2,1;command;Command;" .. command .. "]" ..
"field[3,4;5.5,1;param;Parameter;" .. param .. "]" ..
"button_exit[3.3,5;2,1;submit;Submit]")
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
if owner == "" then if owner == "" then
owner = "not owned" owner = "not owned"
@ -57,64 +68,81 @@ local function initialize_data(meta)
end end
meta:set_string("infotext", "Command Block\n" .. meta:set_string("infotext", "Command Block\n" ..
"(" .. owner .. ")\n" .. "(" .. owner .. ")\n" ..
"Commands: "..commands) "Command: /" .. command .. " " .. param)
end end
local function construct(pos) local construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("commands", "tell @nearest Commandblock unconfigured") meta:set_string("player", "@nearest")
meta:set_string("command", "time")
meta:set_string("param", "7000")
meta:set_string("owner", "") meta:set_string("owner", "")
initialize_data(meta) initialize_data(meta, "@nearest", "time", "7000")
end end
local function after_place(pos, placer) local after_place = function(pos, placer)
if placer then if placer then
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name()) meta:set_string("owner", placer:get_player_name())
initialize_data(meta) initialize_data(meta, "@nearest", "time", "7000")
end end
end end
local function receive_fields(pos, formname, fields, sender) local receive_fields = function(pos, formname, fields, sender)
if fields.quit then if fields.quit then
return return
end end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner") if fields.nearest then
if owner ~= "" and sender:get_player_name() ~= owner then initialize_data(meta, "@nearest", fields.command, fields.param)
return elseif fields.farthest then
end initialize_data(meta, "@farthest", fields.command, fields.param)
meta:set_string("commands", fields.commands) elseif fields.random then
initialize_data(meta, "@random", fields.command, fields.param)
else --fields.submit or pressed enter
meta:set_string("player", fields.player)
meta:set_string("command", fields.command)
meta:set_string("param", fields.param)
initialize_data(meta) initialize_data(meta, fields.player, fields.command, fields.param)
end
end end
local function resolve_commands(commands, pos) local resolve_player = function(name, pos)
local nearest, farthest = nil, nil local get_distance = function(pos1, pos2)
local min_distance, max_distance = math.huge, -1 return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2)
local players = minetest.get_connected_players()
for index, player in pairs(players) do
local distance = vector.distance(pos, player:getpos())
if distance < min_distance then
min_distance = distance
nearest = player:get_player_name()
end
if distance > max_distance then
max_distance = distance
farthest = player:get_player_name()
end
end end
local random = players[math.random(#players)]:get_player_name()
commands = commands:gsub("@nearest", nearest) if name == "@nearest" then
commands = commands:gsub("@farthest", farthest) local min_distance = math.huge
commands = commands:gsub("@random", random) for index, player in ipairs(minetest.get_connected_players()) do
return commands local distance = get_distance(pos, player:getpos())
if distance < min_distance then
min_distance = distance
name = player:get_player_name()
end
end
elseif name == "@farthest" then
local max_distance = -1
for index, player in ipairs(minetest.get_connected_players()) do
local distance = get_distance(pos, player:getpos())
if distance > max_distance then
max_distance = distance
name = player:get_player_name()
end
end
elseif name == "@random" then
local players = minetest.get_connected_players()
local player = players[math.random(#players)]
name = player:get_player_name()
end
return name
end end
local function commandblock_action_on(pos, node) local commandblock_action_on = function(pos, node)
if node.name ~= "mesecons_commandblock:commandblock_off" then if node.name ~= "mesecons_commandblock:commandblock_off" then
return return
end end
@ -122,48 +150,29 @@ local function commandblock_action_on(pos, node)
minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"}) minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"})
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local command = minetest.chatcommands[meta:get_string("command")]
if command == nil then
return
end
local owner = meta:get_string("owner") local owner = meta:get_string("owner")
if owner == "" then if owner == "" then
return return
end end
local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs)
local commands = resolve_commands(meta:get_string("commands"), pos) if not has_privs then
for _, command in pairs(commands:split("\n")) do minetest.chat_send_player(owner, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")")
local pos = command:find(" ") return
local cmd, param = command, ""
if pos then
cmd = command:sub(1, pos - 1)
param = command:sub(pos + 1)
end
local cmddef = minetest.chatcommands[cmd]
if not cmddef then
minetest.chat_send_player(owner, "The command "..cmd.." does not exist")
return
end
local has_privs, missing_privs = minetest.check_player_privs(owner, cmddef.privs)
if not has_privs then
minetest.chat_send_player(owner, "You don't have permission "
.."to run "..cmd
.." (missing privileges: "
..table.concat(missing_privs, ", ")..")")
return
end
cmddef.func(owner, param)
end end
local player = resolve_player(meta:get_string("player"), pos)
command.func(player, meta:get_string("param"))
end end
local function commandblock_action_off(pos, node) local commandblock_action_off = function(pos, node)
if node.name == "mesecons_commandblock:commandblock_on" then if node.name == "mesecons_commandblock:commandblock_on" then
minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"}) minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"})
end end
end end
local function can_dig(pos, player)
local meta = minetest.get_meta(pos)
local owner = meta:get_string("owner")
return owner == "" or owner == player:get_player_name()
end
minetest.register_node("mesecons_commandblock:commandblock_off", { minetest.register_node("mesecons_commandblock:commandblock_off", {
description = "Command Block", description = "Command Block",
tiles = {"jeija_commandblock_off.png"}, tiles = {"jeija_commandblock_off.png"},
@ -172,7 +181,10 @@ minetest.register_node("mesecons_commandblock:commandblock_off", {
on_construct = construct, on_construct = construct,
after_place_node = after_place, after_place_node = after_place,
on_receive_fields = receive_fields, on_receive_fields = receive_fields,
can_dig = can_dig, can_dig = function(pos,player)
local owner = minetest.get_meta(pos):get_string("owner")
return owner == "" or owner == player:get_player_name()
end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
mesecons = {effector = { mesecons = {effector = {
action_on = commandblock_action_on action_on = commandblock_action_on
@ -187,7 +199,10 @@ minetest.register_node("mesecons_commandblock:commandblock_on", {
on_construct = construct, on_construct = construct,
after_place_node = after_place, after_place_node = after_place,
on_receive_fields = receive_fields, on_receive_fields = receive_fields,
can_dig = can_dig, can_dig = function(pos,player)
local owner = minetest.get_meta(pos):get_string("owner")
return owner == "" or owner == player:get_player_name()
end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
mesecons = {effector = { mesecons = {effector = {
action_off = commandblock_action_off action_off = commandblock_action_off

View File

@ -1,2 +1,2 @@
doors
mesecons mesecons
doors

View File

@ -0,0 +1,167 @@
doors = {}
-- Registers a door - REDEFINITION ONLY | DOORS MOD MUST HAVE BEEN LOADED BEFORE
-- name: The name of the door
-- def: a table with the folowing fields:
-- description
-- inventory_image
-- groups
-- tiles_bottom: the tiles of the bottom part of the door {front, side}
-- tiles_top: the tiles of the bottom part of the door {front, side}
-- If the following fields are not defined the default values are used
-- node_box_bottom
-- node_box_top
-- selection_box_bottom
-- selection_box_top
-- only_placer_can_open: if true only the player who placed the door can
-- open it
function doors:register_door(name, def)
def.groups.not_in_creative_inventory = 1
local box = {{-0.5, -0.5, -0.5, 0.5, 0.5, -0.5+1.5/16}}
if not def.node_box_bottom then
def.node_box_bottom = box
end
if not def.node_box_top then
def.node_box_top = box
end
if not def.selection_box_bottom then
def.selection_box_bottom= box
end
if not def.selection_box_top then
def.selection_box_top = box
end
local tt = def.tiles_top
local tb = def.tiles_bottom
local function after_dig_node(pos, name)
if minetest.get_node(pos).name == name then
minetest.remove_node(pos)
end
end
local function on_rightclick(pos, dir, check_name, replace, replace_dir, params)
pos.y = pos.y+dir
if not minetest.get_node(pos).name == check_name then
return
end
local p2 = minetest.get_node(pos).param2
p2 = params[p2+1]
local meta = minetest.get_meta(pos):to_table()
minetest.set_node(pos, {name=replace_dir, param2=p2})
minetest.get_meta(pos):from_table(meta)
pos.y = pos.y-dir
meta = minetest.get_meta(pos):to_table()
minetest.set_node(pos, {name=replace, param2=p2})
minetest.get_meta(pos):from_table(meta)
end
local function on_mesecons_signal_open (pos, node)
on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
end
local function on_mesecons_signal_close (pos, node)
on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
end
local function check_player_priv(pos, player)
if not def.only_placer_can_open then
return true
end
local meta = minetest.get_meta(pos)
local pn = player:get_player_name()
return meta:get_string("doors_owner") == pn
end
minetest.register_node(":"..name.."_b_1", {
tiles = {tb[2], tb[2], tb[2], tb[2], tb[1], tb[1].."^[transformfx"},
paramtype = "light",
paramtype2 = "facedir",
drop = name,
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = def.node_box_bottom
},
selection_box = {
type = "fixed",
fixed = def.selection_box_bottom
},
groups = def.groups,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
pos.y = pos.y+1
after_dig_node(pos, name.."_t_1")
end,
on_rightclick = function(pos, node, puncher)
if check_player_priv(pos, puncher) then
on_rightclick(pos, 1, name.."_t_1", name.."_b_2", name.."_t_2", {1,2,3,0})
end
end,
mesecons = {effector = {
action_on = on_mesecons_signal_open
}},
can_dig = check_player_priv,
})
minetest.register_node(":"..name.."_b_2", {
tiles = {tb[2], tb[2], tb[2], tb[2], tb[1].."^[transformfx", tb[1]},
paramtype = "light",
paramtype2 = "facedir",
drop = name,
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = def.node_box_bottom
},
selection_box = {
type = "fixed",
fixed = def.selection_box_bottom
},
groups = def.groups,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
pos.y = pos.y+1
after_dig_node(pos, name.."_t_2")
end,
on_rightclick = function(pos, node, puncher)
if check_player_priv(pos, puncher) then
on_rightclick(pos, 1, name.."_t_2", name.."_b_1", name.."_t_1", {3,0,1,2})
end
end,
mesecons = {effector = {
action_off = on_mesecons_signal_close
}},
can_dig = check_player_priv,
})
end
doors:register_door("doors:door_wood", {
description = "Wooden Door",
inventory_image = "door_wood.png",
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=2,door=1},
tiles_bottom = {"door_wood_b.png", "door_brown.png"},
tiles_top = {"door_wood_a.png", "door_brown.png"},
sounds = default.node_sound_wood_defaults(),
})
doors:register_door("doors:door_steel", {
description = "Steel Door",
inventory_image = "door_steel.png",
groups = {snappy=1,bendy=2,cracky=1,melty=2,level=2,door=1},
tiles_bottom = {"door_steel_b.png", "door_grey.png"},
tiles_top = {"door_steel_a.png", "door_grey.png"},
only_placer_can_open = true,
sounds = default.node_sound_stone_defaults(),
})

View File

@ -17,18 +17,28 @@ end
-- Functions that are called after the delay time -- Functions that are called after the delay time
local delayer_turnon = function(params)
local rules = delayer_get_output_rules(params.node)
mesecon:receptor_on(params.pos, rules)
end
local delayer_turnoff = function(params)
local rules = delayer_get_output_rules(params.node)
mesecon:receptor_off(params.pos, rules)
end
local delayer_activate = function(pos, node) local delayer_activate = function(pos, node)
local def = minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
local time = def.delayer_time local time = def.delayer_time
minetest.swap_node(pos, {name = def.delayer_onstate, param2=node.param2}) minetest.swap_node(pos, {name = def.delayer_onstate, param2=node.param2})
mesecon.queue:add_action(pos, "receptor_on", {delayer_get_output_rules(node)}, time, nil) minetest.after(time, delayer_turnon , {pos = pos, node = node})
end end
local delayer_deactivate = function(pos, node) local delayer_deactivate = function(pos, node)
local def = minetest.registered_nodes[node.name] local def = minetest.registered_nodes[node.name]
local time = def.delayer_time local time = def.delayer_time
minetest.swap_node(pos, {name = def.delayer_offstate, param2=node.param2}) minetest.swap_node(pos, {name = def.delayer_offstate, param2=node.param2})
mesecon.queue:add_action(pos, "receptor_off", {delayer_get_output_rules(node)}, time, nil) minetest.after(time, delayer_turnoff, {pos = pos, node = node})
end end
-- Register the 2 (states) x 4 (delay times) delayers -- Register the 2 (states) x 4 (delay times) delayers

View File

@ -6,13 +6,13 @@ local object_detector_make_formspec = function (pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("formspec", "size[9,2.5]" .. meta:set_string("formspec", "size[9,2.5]" ..
"field[0.3, 0;9,2;scanname;Name of player to scan for (empty for any):;${scanname}]".. "field[0.3, 0;9,2;scanname;Name of player to scan for (empty for any):;${scanname}]"..
"field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]".. "field[0.3,1.5;4,2;digiline_channel;Digiline Channel (optional):;${digiline_channel}]")
"button_exit[7,0.75;2,3;;Save]")
end end
local object_detector_on_receive_fields = function(pos, formname, fields) local object_detector_on_receive_fields = function(pos, formname, fields)
if not fields.scanname or not fields.digiline_channel then return end; if fields.quit then
return
end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_string("scanname", fields.scanname) meta:set_string("scanname", fields.scanname)
meta:set_string("digiline_channel", fields.digiline_channel) meta:set_string("digiline_channel", fields.digiline_channel)

View File

@ -1,130 +0,0 @@
local other_state_node = {}
for _, material in ipairs({
{ id = "wood", desc = "Wooden", color = "brown" },
{ id = "steel", desc = "Steel", color = "grey" },
}) do
doors:register_door("mesecons_doors:op_door_"..material.id, {
description = "Mesecon-operated "..material.desc.." Door",
inventory_image = minetest.registered_items["doors:door_"..material.id].inventory_image,
groups = minetest.registered_nodes["doors:door_"..material.id.."_b_1"].groups,
tiles_bottom = {"door_"..material.id.."_b.png", "door_"..material.color..".png"},
tiles_top = {"door_"..material.id.."_a.png", "door_"..material.color..".png"},
})
local groups_plus_mesecon = { mesecon = 2 }
for k, v in pairs(minetest.registered_nodes["doors:door_"..material.id.."_b_1"].groups) do
groups_plus_mesecon[k] = v
end
doors:register_door("mesecons_doors:sig_door_"..material.id, {
description = "Mesecon-signalling "..material.desc.." Door",
inventory_image = minetest.registered_items["doors:door_"..material.id].inventory_image,
groups = groups_plus_mesecon,
tiles_bottom = {"door_"..material.id.."_b.png", "door_"..material.color..".png"},
tiles_top = {"door_"..material.id.."_a.png", "door_"..material.color..".png"},
})
for _, thishalf in ipairs({ "t", "b" }) do
local otherhalf = thishalf == "t" and "b" or "t"
local otherdir = thishalf == "t" and -1 or 1
for orientation = 1, 2 do
local thissuffix = material.id.."_"..thishalf.."_"..orientation
local othersuffix = material.id.."_"..otherhalf.."_"..orientation
local thisopname = "mesecons_doors:op_door_"..thissuffix
local otheropname = "mesecons_doors:op_door_"..othersuffix
local oponr = minetest.registered_nodes[thisopname].on_rightclick
local function handle_mesecon_signal (thispos, thisnode, signal)
local thismeta = minetest.get_meta(thispos)
if signal == thismeta:get_int("sigstate") then return end
thismeta:set_int("sigstate", signal)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
if minetest.get_node(otherpos).name ~= otheropname then return end
local othermeta = minetest.get_meta(otherpos)
local newdoorstate = math.max(thismeta:get_int("sigstate"), othermeta:get_int("sigstate"))
if newdoorstate == thismeta:get_int("doorstate") then return end
oponr(thispos, thisnode, nil)
thismeta:set_int("doorstate", newdoorstate)
othermeta:set_int("doorstate", newdoorstate)
end
minetest.override_item(thisopname, {
on_construct = function (pos)
if mesecon:is_powered(pos) then
local node = minetest.get_node(pos)
mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on", 1)
mesecon:activate(pos, node, nil, 1)
end
end,
on_rightclick = function (pos, node, clicker) end,
mesecons = {
effector = {
action_on = function (pos, node)
handle_mesecon_signal(pos, node, 1)
end,
action_off = function (pos, node)
handle_mesecon_signal(pos, node, 0)
end,
},
},
})
local thissigname = "mesecons_doors:sig_door_"..thissuffix
local othersigname = "mesecons_doors:sig_door_"..othersuffix
local sigonr = minetest.registered_nodes[thissigname].on_rightclick
minetest.override_item(thissigname, {
on_rightclick = function (thispos, thisnode, clicker)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
print("open: otherpos.name="..minetest.get_node(otherpos).name..", othersigname="..othersigname)
if minetest.get_node(otherpos).name ~= othersigname then return end
sigonr(thispos, thisnode, clicker)
for _, pos in ipairs({ thispos, otherpos }) do
local node = minetest.get_node(pos)
node.name = other_state_node[node.name]
minetest.swap_node(pos, node)
mesecon:receptor_on(pos)
end
end,
mesecons = { receptor = { state = mesecon.state.off } },
})
other_state_node[thissigname] = thissigname.."_on"
local ondef = {}
for k, v in pairs(minetest.registered_nodes[thissigname]) do
ondef[k] = v
end
ondef.on_rightclick = function (thispos, thisnode, clicker)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
print("close: otherpos.name="..minetest.get_node(otherpos).name..", othersigname="..othersigname)
if minetest.get_node(otherpos).name ~= othersigname.."_on" then return end
for _, pos in ipairs({ thispos, otherpos }) do
local node = minetest.get_node(pos)
node.name = other_state_node[node.name]
minetest.swap_node(pos, node)
mesecon:receptor_off(pos)
end
sigonr(thispos, thisnode, clicker)
end
ondef.mesecons = { receptor = { state = mesecon.state.on } }
ondef.after_destruct = function (thispos, thisnode)
local otherpos = { x = thispos.x, y = thispos.y + otherdir, z = thispos.z }
if minetest.get_node(otherpos).name == othersigname.."_on" then
minetest.remove_node(otherpos)
mesecon:receptor_off(otherpos)
end
end
other_state_node[thissigname.."_on"] = thissigname
ondef.mesecon_other_state_node = thissigname
minetest.register_node(thissigname.."_on", ondef)
end
end
minetest.register_craft({
output = "mesecons_doors:op_door_"..material.id,
recipe = {
{ "group:mesecon_conductor_craftable", "", "" },
{ "", "doors:door_"..material.id, "group:mesecon_conductor_craftable" },
{ "group:mesecon_conductor_craftable", "", "" },
},
})
minetest.register_craft({
output = "mesecons_doors:sig_door_"..material.id,
recipe = {
{ "", "", "group:mesecon_conductor_craftable" },
{ "group:mesecon_conductor_craftable", "doors:door_"..material.id, "" },
{ "", "", "group:mesecon_conductor_craftable" },
},
})
end

View File

@ -77,7 +77,7 @@ minetest.register_node("mesecons_extrawires:crossover_01", {
{ -3/32, -17/32, 6/32, 3/32, -13/32, 16/32+0.001 }, { -3/32, -17/32, 6/32, 3/32, -13/32, 16/32+0.001 },
}, },
}, },
groups = {dig_immediate=3, mesecon=3, not_in_creative_inventory=1}, groups = {dig_immediate=3, mesecon=3, mesecon_conductor_craftable=1, not_in_creative_inventory=1},
mesecons = { mesecons = {
conductor = { conductor = {
states = crossover_states, states = crossover_states,
@ -113,7 +113,7 @@ minetest.register_node("mesecons_extrawires:crossover_10", {
{ -3/32, -17/32, 6/32, 3/32, -13/32, 16/32+0.001 }, { -3/32, -17/32, 6/32, 3/32, -13/32, 16/32+0.001 },
}, },
}, },
groups = {dig_immediate=3, mesecon=3, not_in_creative_inventory=1}, groups = {dig_immediate=3, mesecon=3, mesecon_conductor_craftable=1, not_in_creative_inventory=1},
mesecons = { mesecons = {
conductor = { conductor = {
states = crossover_states, states = crossover_states,
@ -149,7 +149,7 @@ minetest.register_node("mesecons_extrawires:crossover_on", {
{ -3/32, -17/32, 6/32, 3/32, -13/32, 16/32+0.001 }, { -3/32, -17/32, 6/32, 3/32, -13/32, 16/32+0.001 },
}, },
}, },
groups = {dig_immediate=3, mesecon=3, not_in_creative_inventory=1}, groups = {dig_immediate=3, mesecon=3, mesecon_conductor_craftable=1, not_in_creative_inventory=1},
mesecons = { mesecons = {
conductor = { conductor = {
states = crossover_states, states = crossover_states,

View File

@ -38,7 +38,7 @@ minetest.register_node("mesecons_extrawires:tjunction_on", {
sunlight_propagates = true, sunlight_propagates = true,
selection_box = tjunction_selectionbox, selection_box = tjunction_selectionbox,
node_box = tjunction_nodebox, node_box = tjunction_nodebox,
groups = {dig_immediate = 3, not_in_creative_inventory = 1}, groups = {dig_immediate = 3, mesecon_conductor_craftable=1, not_in_creative_inventory = 1},
drop = "mesecons_extrawires:tjunction_off", drop = "mesecons_extrawires:tjunction_off",
mesecons = {conductor = mesecons = {conductor =
{ {

View File

@ -23,8 +23,7 @@ function gate_get_input_rules_twoinputs(node)
return gate_rotate_rules(node) return gate_rotate_rules(node)
end end
function update_gate(pos, node, rulename, newstate) function update_gate(pos)
yc_update_real_portstates(pos, node, rulename, newstate)
gate = get_gate(pos) gate = get_gate(pos)
L = rotate_ports( L = rotate_ports(
yc_get_real_portstates(pos), yc_get_real_portstates(pos),
@ -47,7 +46,9 @@ function set_gate(pos, on)
gate = get_gate(pos) gate = get_gate(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if on ~= gate_state(pos) then if on ~= gate_state(pos) then
if mesecon.do_overheat(pos) then yc_heat(meta)
--minetest.after(0.5, yc_cool, meta)
if yc_overheat(meta) then
pop_gate(pos) pop_gate(pos)
else else
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
@ -76,9 +77,7 @@ end
function pop_gate(pos) function pop_gate(pos)
gate = get_gate(pos) gate = get_gate(pos)
minetest.remove_node(pos) minetest.remove_node(pos)
minetest.after(0.2, function (pos) minetest.after(0.2, yc_overheat_off, pos)
mesecon:receptor_off(pos, mesecon.rules.flat)
end , pos) -- wait for pending parsings
minetest.add_item(pos, "mesecons_gates:"..gate.."_off") minetest.add_item(pos, "mesecons_gates:"..gate.."_off")
end end
@ -153,6 +152,7 @@ for _, gate in ipairs(gates) do
walkable = true, walkable = true,
on_construct = function(pos) on_construct = function(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_int("heat", 0)
update_gate(pos) update_gate(pos)
end, end,
groups = groups, groups = groups,
@ -220,3 +220,4 @@ minetest.register_craft({
{'mesecons:mesecon', '', ''}, {'mesecons:mesecon', '', ''},
}, },
}) })

View File

@ -31,46 +31,18 @@ rules.d = {x = 0, y = 0, z = -1, name="D"}
------------------ ------------------
-- These helpers are required to set the portstates of the luacontroller -- These helpers are required to set the portstates of the luacontroller
function lc_update_real_portstates(pos, rulename, newstate)
local meta = minetest.get_meta(pos)
if rulename == nil then
meta:set_int("real_portstates", 1)
return
end
local n = meta:get_int("real_portstates") - 1
if n < 0 then
legacy_update_ports(pos)
n = meta:get_int("real_portstates") - 1
end
local L = {}
for i = 1, 4 do
L[i] = n%2
n = math.floor(n/2)
end
if rulename.x == nil then
for _, rname in ipairs(rulename) do
local port = ({4, 1, nil, 3, 2})[rname.x+2*rname.z+3]
L[port] = (newstate == "on") and 1 or 0
end
else
local port = ({4, 1, nil, 3, 2})[rulename.x+2*rulename.z+3]
L[port] = (newstate == "on") and 1 or 0
end
meta:set_int("real_portstates", 1 + L[1] + 2*L[2] + 4*L[3] + 8*L[4])
end
local get_real_portstates = function(pos) -- determine if ports are powered (by itself or from outside) local get_real_portstates = function(pos) -- determine if ports are powered (by itself or from outside)
local meta = minetest.get_meta(pos) ports = {
local L = {} a = mesecon:is_power_on(mesecon:addPosRule(pos, rules.a), mesecon:invertRule(rules.a))
local n = meta:get_int("real_portstates") - 1 and mesecon:rules_link(mesecon:addPosRule(pos, rules.a), pos),
if n < 0 then b = mesecon:is_power_on(mesecon:addPosRule(pos, rules.b), mesecon:invertRule(rules.b))
return legacy_update_ports(pos) and mesecon:rules_link(mesecon:addPosRule(pos, rules.b), pos),
end c = mesecon:is_power_on(mesecon:addPosRule(pos, rules.c), mesecon:invertRule(rules.c))
for _, index in ipairs({"a", "b", "c", "d"}) do and mesecon:rules_link(mesecon:addPosRule(pos, rules.c), pos),
L[index] = ((n%2) == 1) d = mesecon:is_power_on(mesecon:addPosRule(pos, rules.d), mesecon:invertRule(rules.d))
n = math.floor(n/2) and mesecon:rules_link(mesecon:addPosRule(pos, rules.d), pos),
end }
return L return ports
end end
local merge_portstates = function (ports, vports) local merge_portstates = function (ports, vports)
@ -122,6 +94,30 @@ end
-- Overheat stuff -- -- Overheat stuff --
-------------------- --------------------
local heat = function (meta) -- warm up
h = meta:get_int("heat")
if h ~= nil then
meta:set_int("heat", h + 1)
end
end
--local cool = function (meta) -- cool down after a while
-- h = meta:get_int("heat")
-- if h ~= nil then
-- meta:set_int("heat", h - 1)
-- end
--end
local overheat = function (meta) -- determine if too hot
h = meta:get_int("heat")
if h == nil then return true end -- if nil then overheat
if h > 40 then
return true
else
return false
end
end
local overheat_off = function(pos) local overheat_off = function(pos)
mesecon:receptor_off(pos, mesecon.rules.flat) mesecon:receptor_off(pos, mesecon.rules.flat)
end end
@ -168,29 +164,40 @@ local safe_serialize = function(value)
return minetest.serialize(deep_copy(value)) return minetest.serialize(deep_copy(value))
end end
mesecon.queue:add_function("lc_interrupt", function (pos, iid, luac_id) local interrupt = function(params)
-- There is no luacontroller anymore / it has been reprogrammed / replaced lc_update(params.pos, {type="interrupt", iid = params.iid})
if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end end
lc_update(pos, {type="interrupt", iid = iid})
end)
local getinterrupt = function(pos) local getinterrupt = function(pos)
local interrupt = function (time, iid) -- iid = interrupt id local interrupt = function (time, iid) -- iid = interrupt id
if type(time) ~= "number" then return end if type(time) ~= "number" then return end
luac_id = minetest.get_meta(pos):get_int("luac_id") local iid = iid or math.random()
mesecon.queue:add_action(pos, "lc_interrupt", {iid, luac_id}, time, iid, 1) local meta = minetest.get_meta(pos)
local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}
local found = false
local search = safe_serialize(iid)
for _, i in ipairs(interrupts) do
if safe_serialize(i) == search then
found = true
break
end
end
if not found then
table.insert(interrupts, iid)
meta:set_string("lc_interrupts", safe_serialize(interrupts))
end
minetest.after(time, interrupt, {pos=pos, iid = iid})
end end
return interrupt return interrupt
end end
local getdigiline_send = function(pos) local getdigiline_send = function (pos)
if not digiline then return end local digiline_send = function (channel, msg)
-- Send messages on next serverstep if digiline then
return function(channel, msg)
minetest.after(0, function()
digiline:receptor_send(pos, digiline.rules.default, channel, msg) digiline:receptor_send(pos, digiline.rules.default, channel, msg)
end) end
end end
return digiline_send
end end
local create_environment = function(pos, mem, event) local create_environment = function(pos, mem, event)
@ -208,8 +215,6 @@ local create_environment = function(pos, mem, event)
mem = mem, mem = mem,
tostring = tostring, tostring = tostring,
tonumber = tonumber, tonumber = tonumber,
heat = minetest.get_meta(pos):get_int("heat"),
heat_max = OVERHEAT_MAX,
string = { string = {
byte = string.byte, byte = string.byte,
char = string.char, char = string.char,
@ -219,7 +224,6 @@ local create_environment = function(pos, mem, event)
gsub = string.gsub, gsub = string.gsub,
len = string.len, len = string.len,
lower = string.lower, lower = string.lower,
upper = string.upper,
match = string.match, match = string.match,
rep = string.rep, rep = string.rep,
reverse = string.reverse, reverse = string.reverse,
@ -277,10 +281,14 @@ local create_sandbox = function (code, env)
return f return f
end end
local lc_overheat = function (pos, meta) local do_overheat = function (pos, meta)
if mesecon.do_overheat(pos) then -- if too hot -- Overheat protection
heat(meta)
--minetest.after(0.5, cool, meta)
if overheat(meta) then
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
minetest.swap_node(pos, {name = BASENAME.."_burnt", param2 = node.param2}) minetest.swap_node(pos, {name = BASENAME.."_burnt", param2 = node.param2})
minetest.get_meta(pos):set_string("lc_interrupts", "")
minetest.after(0.2, overheat_off, pos) -- wait for pending operations minetest.after(0.2, overheat_off, pos) -- wait for pending operations
return true return true
end end
@ -294,6 +302,20 @@ local save_memory = function(meta, mem)
meta:set_string("lc_memory", safe_serialize(mem)) meta:set_string("lc_memory", safe_serialize(mem))
end end
local interrupt_allow = function (meta, event)
if event.type ~= "interrupt" then return true end
local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}
local search = safe_serialize(event.iid)
for _, i in ipairs(interrupts) do
if safe_serialize(i) == search then
return true
end
end
return false
end
local ports_invalid = function (var) local ports_invalid = function (var)
if type(var) == "table" then if type(var) == "table" then
return false return false
@ -307,7 +329,8 @@ end
lc_update = function (pos, event) lc_update = function (pos, event)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if lc_overheat(pos) then return end if not interrupt_allow(meta, event) then return end
if do_overheat(pos, meta) then return end
-- load code & mem from memory -- load code & mem from memory
local mem = load_memory(meta) local mem = load_memory(meta)
@ -315,7 +338,7 @@ lc_update = function (pos, event)
-- make sure code is ok and create environment -- make sure code is ok and create environment
local prohibited = code_prohibited(code) local prohibited = code_prohibited(code)
if prohibited then return prohibited end if prohibited then return prohibited end
local env = create_environment(pos, mem, event) local env = create_environment(pos, mem, event)
-- create the sandbox and execute code -- create the sandbox and execute code
@ -328,7 +351,7 @@ lc_update = function (pos, event)
save_memory(meta, mem) save_memory(meta, mem)
-- Actually set the ports -- Actually set the ports
action(pos, env.port) minetest.after(0, action, pos, env.port)
end end
local reset_meta = function(pos, code, errmsg) local reset_meta = function(pos, code, errmsg)
@ -343,11 +366,11 @@ local reset_meta = function(pos, code, errmsg)
"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]".. "image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
"label[0.1,5;"..errmsg.."]") "label[0.1,5;"..errmsg.."]")
meta:set_int("heat", 0) meta:set_int("heat", 0)
meta:set_int("luac_id", math.random(1, 1000000))
end end
local reset = function (pos) local reset = function (pos)
action(pos, {a=false, b=false, c=false, d=false}) minetest.get_meta(pos):set_string("lc_interrupts", "")
action(pos, {a=false, b=false, c=false, d=false}, true)
end end
-- ______ -- ______
@ -434,7 +457,6 @@ local mesecons = {
{ {
rules = input_rules[cid], rules = input_rules[cid],
action_change = function (pos, _, rulename, newstate) action_change = function (pos, _, rulename, newstate)
lc_update_real_portstates(pos, rulename, newstate)
lc_update(pos, {type=newstate, pin=rulename}) lc_update(pos, {type=newstate, pin=rulename})
end, end,
}, },
@ -466,50 +488,33 @@ minetest.register_node(nodename, {
node_box = nodebox, node_box = nodebox,
on_construct = reset_meta, on_construct = reset_meta,
on_receive_fields = function(pos, formname, fields) on_receive_fields = function(pos, formname, fields)
if not fields.program then if fields.quit then
return return
end end
reset(pos) reset(pos)
reset_meta(pos, fields.code) reset_meta(pos, fields.code)
local err = lc_update(pos, {type="program"}) local err = lc_update(pos, {type="program"})
if err then if err then print(err) end
print(err) reset_meta(pos, fields.code, err)
reset_meta(pos, fields.code, err)
end
end, end,
on_timer = handle_timer,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
mesecons = mesecons, mesecons = mesecons,
digiline = digiline, digiline = digiline,
is_luacontroller = true,
virtual_portstates = { a = a == 1, -- virtual portstates are virtual_portstates = { a = a == 1, -- virtual portstates are
b = b == 1, -- the ports the the b = b == 1, -- the ports the the
c = c == 1, -- controller powers itself c = c == 1, -- controller powers itself
d = d == 1},-- so those that light up d = d == 1},-- so those that light up
after_dig_node = function (pos, node) after_dig_node = function (pos, node)
mesecon:receptor_off(pos, output_rules) mesecon:receptor_off(pos, output_rules)
end, end,
is_luacontroller = true,
}) })
end end
end end
end end
end end
------------------------------ --overheated luacontroller
-- overheated luacontroller --
------------------------------
local mesecons_burnt = {
effector =
{
rules = mesecon.rules.flat,
action_change = function (pos, _, rulename, newstate)
-- only update portstates when changes are triggered
lc_update_real_portstates(pos, rulename, newstate)
end
}
}
minetest.register_node(BASENAME .. "_burnt", { minetest.register_node(BASENAME .. "_burnt", {
drawtype = "nodebox", drawtype = "nodebox",
tiles = { tiles = {
@ -535,14 +540,12 @@ minetest.register_node(BASENAME .. "_burnt", {
reset(pos) reset(pos)
reset_meta(pos, fields.code) reset_meta(pos, fields.code)
local err = lc_update(pos, {type="program"}) local err = lc_update(pos, {type="program"})
if err then if err then print(err) end
print(err) reset_meta(pos, fields.code, err)
reset_meta(pos, fields.code, err)
end
end, end,
sounds = default.node_sound_stone_defaults(), sounds = default.node_sound_stone_defaults(),
is_luacontroller = true,
virtual_portstates = {a = false, b = false, c = false, d = false}, virtual_portstates = {a = false, b = false, c = false, d = false},
mesecons = mesecons_burnt,
}) })
------------------------ ------------------------

View File

@ -39,8 +39,7 @@ mesecon:add_rules(nodename, rules)
local mesecons = {effector = local mesecons = {effector =
{ {
rules = input_rules, rules = input_rules,
action_change = function (pos, node, rulename, newstate) action_change = function (pos, node, rulename)
yc_update_real_portstates(pos, node, rulename, newstate)
update_yc(pos) update_yc(pos)
end end
}} }}
@ -93,11 +92,15 @@ minetest.register_node(nodename, {
"button[7.5,0.2;1.5,3;brsflop;RS-Flop]".. "button[7.5,0.2;1.5,3;brsflop;RS-Flop]"..
"button_exit[3.5,1;2,3;program;Program]") "button_exit[3.5,1;2,3;program;Program]")
meta:set_string("infotext", "Unprogrammed Microcontroller") meta:set_string("infotext", "Unprogrammed Microcontroller")
meta:set_int("heat", 0)
local r = "" local r = ""
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0" for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
meta:set_string("eeprom", r) meta:set_string("eeprom", r)
end, end,
on_receive_fields = function(pos, formanme, fields, sender) on_receive_fields = function(pos, formanme, fields, sender)
if fields.quit then
return
end
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
if fields.band then if fields.band then
fields.code = "sbi(C, A&B) :A and B are inputs, C is output" fields.code = "sbi(C, A&B) :A and B are inputs, C is output"
@ -111,8 +114,8 @@ minetest.register_node(nodename, {
fields.code = "if(A)sbi(1,1);if(!A&#1)sbi(B,!B)sbi(1,0); if(C)off(B,1); :A is input, B is output (Q), C is reset, toggles with falling edge" fields.code = "if(A)sbi(1,1);if(!A&#1)sbi(B,!B)sbi(1,0); if(C)off(B,1); :A is input, B is output (Q), C is reset, toggles with falling edge"
elseif fields.brsflop then elseif fields.brsflop then
fields.code = "if(A)on(C);if(B)off(C); :A is S (Set), B is R (Reset), C is output (R dominates)" fields.code = "if(A)on(C);if(B)off(C); :A is S (Set), B is R (Reset), C is output (R dominates)"
end elseif fields.program or fields.code then --nothing
if fields.code == nil then return end else return nil end
meta:set_string("code", fields.code) meta:set_string("code", fields.code)
meta:set_string("formspec", "size[9,2.5]".. meta:set_string("formspec", "size[9,2.5]"..
@ -152,6 +155,7 @@ minetest.register_craft({
function yc_reset(pos) function yc_reset(pos)
yc_action(pos, {a=false, b=false, c=false, d=false}) yc_action(pos, {a=false, b=false, c=false, d=false})
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
meta:set_int("heat", 0)
meta:set_int("afterid", 0) meta:set_int("afterid", 0)
local r = "" local r = ""
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0" for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
@ -160,12 +164,11 @@ end
function update_yc(pos) function update_yc(pos)
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
yc_heat(meta)
if (mesecon.do_overheat(pos)) then --minetest.after(0.5, yc_cool, meta)
if (yc_overheat(meta)) then
minetest.remove_node(pos) minetest.remove_node(pos)
minetest.after(0.2, function (pos) minetest.after(0.2, yc_overheat_off, pos) --wait for pending parsings
mesecon:receptor_off(pos, mesecon.rules.flat)
end , pos) -- wait for pending parsings
minetest.add_item(pos, "mesecons_microcontroller:microcontroller0000") minetest.add_item(pos, "mesecons_microcontroller:microcontroller0000")
end end
@ -630,45 +633,25 @@ function yc_set_portstate(port, state, L)
return L return L
end end
function yc_update_real_portstates(pos, node, rulename, newstate) function yc_get_real_portstates(pos) -- port powered or not (by itself or from outside)?
local meta = minetest.get_meta(pos) rulesA = mesecon:get_rules("mesecons_microcontroller:microcontroller0001")
if rulename == nil then rulesB = mesecon:get_rules("mesecons_microcontroller:microcontroller0010")
meta:set_int("real_portstates", 1) rulesC = mesecon:get_rules("mesecons_microcontroller:microcontroller0100")
return rulesD = mesecon:get_rules("mesecons_microcontroller:microcontroller1000")
end L = {
local n = meta:get_int("real_portstates") - 1 a = mesecon:is_power_on(mesecon:addPosRule(pos, rulesA[1]),
if n < 0 then mesecon:invertRule(rulesA[1])) and
legacy_update_ports(pos) mesecon:rules_link(mesecon:addPosRule(pos, rulesA[1]), pos),
n = meta:get_int("real_portstates") - 1 b = mesecon:is_power_on(mesecon:addPosRule(pos, rulesB[1]),
end mesecon:invertRule(rulesB[1])) and
local L = {} mesecon:rules_link(mesecon:addPosRule(pos, rulesB[1]), pos),
for i = 1, 4 do c = mesecon:is_power_on(mesecon:addPosRule(pos, rulesC[1]),
L[i] = n%2 mesecon:invertRule(rulesC[1])) and
n = math.floor(n/2) mesecon:rules_link(mesecon:addPosRule(pos, rulesC[1]), pos),
end d = mesecon:is_power_on(mesecon:addPosRule(pos, rulesD[1]),
if rulename.x == nil then mesecon:invertRule(rulesD[1])) and
for _, rname in ipairs(rulename) do mesecon:rules_link(mesecon:addPosRule(pos, rulesD[1]), pos),
local port = ({4, 1, nil, 3, 2})[rname.x+2*rname.z+3] }
L[port] = (newstate == "on") and 1 or 0
end
else
local port = ({4, 1, nil, 3, 2})[rulename.x+2*rulename.z+3]
L[port] = (newstate == "on") and 1 or 0
end
meta:set_int("real_portstates", 1 + L[1] + 2*L[2] + 4*L[3] + 8*L[4])
end
function yc_get_real_portstates(pos) -- determine if ports are powered (by itself or from outside)
local meta = minetest.get_meta(pos)
local L = {}
local n = meta:get_int("real_portstates") - 1
if n < 0 then
return legacy_update_ports(pos)
end
for _, index in ipairs({"a", "b", "c", "d"}) do
L[index] = ((n%2) == 1)
n = math.floor(n/2)
end
return L return L
end end
@ -694,3 +677,34 @@ function yc_merge_portstates(Lreal, Lvirtual)
if Lvirtual.d or Lreal.d then L.d = true end if Lvirtual.d or Lreal.d then L.d = true end
return L return L
end end
--"Overheat" protection
function yc_heat(meta)
h = meta:get_int("heat")
if h ~= nil then
meta:set_int("heat", h + 1)
end
end
--function yc_cool(meta)
-- h = meta:get_int("heat")
-- if h ~= nil then
-- meta:set_int("heat", h - 1)
-- end
--end
function yc_overheat(meta)
if MESECONS_GLOBALSTEP then return false end
h = meta:get_int("heat")
if h == nil then return true end -- if nil the overheat
if h>60 then
return true
else
return false
end
end
function yc_overheat_off(pos)
rules = mesecon:get_rules("mesecons_microcontroller:microcontroller1111")
mesecon:receptor_off(pos, rules)
end

View File

@ -0,0 +1 @@
-- place texture packs for mesecons into the textures folder here

Binary file not shown.

After

Width:  |  Height:  |  Size: 497 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 418 B

View File

Before

Width:  |  Height:  |  Size: 454 B

After

Width:  |  Height:  |  Size: 454 B

View File

Before

Width:  |  Height:  |  Size: 463 B

After

Width:  |  Height:  |  Size: 463 B

View File

Before

Width:  |  Height:  |  Size: 323 B

After

Width:  |  Height:  |  Size: 323 B

View File

Before

Width:  |  Height:  |  Size: 282 B

After

Width:  |  Height:  |  Size: 282 B

View File

Before

Width:  |  Height:  |  Size: 278 B

After

Width:  |  Height:  |  Size: 278 B

View File

Before

Width:  |  Height:  |  Size: 592 B

After

Width:  |  Height:  |  Size: 592 B

View File

Before

Width:  |  Height:  |  Size: 233 B

After

Width:  |  Height:  |  Size: 233 B

View File

Before

Width:  |  Height:  |  Size: 231 B

After

Width:  |  Height:  |  Size: 231 B

View File

Before

Width:  |  Height:  |  Size: 251 B

After

Width:  |  Height:  |  Size: 251 B

View File

Before

Width:  |  Height:  |  Size: 241 B

After

Width:  |  Height:  |  Size: 241 B

View File

Before

Width:  |  Height:  |  Size: 195 B

After

Width:  |  Height:  |  Size: 195 B

View File

Before

Width:  |  Height:  |  Size: 195 B

After

Width:  |  Height:  |  Size: 195 B

View File

Before

Width:  |  Height:  |  Size: 245 B

After

Width:  |  Height:  |  Size: 245 B

View File

Before

Width:  |  Height:  |  Size: 743 B

After

Width:  |  Height:  |  Size: 743 B

View File

Before

Width:  |  Height:  |  Size: 777 B

After

Width:  |  Height:  |  Size: 777 B

View File

Before

Width:  |  Height:  |  Size: 487 B

After

Width:  |  Height:  |  Size: 487 B

View File

Before

Width:  |  Height:  |  Size: 835 B

After

Width:  |  Height:  |  Size: 835 B

View File

Before

Width:  |  Height:  |  Size: 817 B

After

Width:  |  Height:  |  Size: 817 B

View File

Before

Width:  |  Height:  |  Size: 305 B

After

Width:  |  Height:  |  Size: 305 B

View File

Before

Width:  |  Height:  |  Size: 270 B

After

Width:  |  Height:  |  Size: 270 B

View File

Before

Width:  |  Height:  |  Size: 209 B

After

Width:  |  Height:  |  Size: 209 B

View File

Before

Width:  |  Height:  |  Size: 253 B

After

Width:  |  Height:  |  Size: 253 B

View File

Before

Width:  |  Height:  |  Size: 196 B

After

Width:  |  Height:  |  Size: 196 B

View File

Before

Width:  |  Height:  |  Size: 246 B

After

Width:  |  Height:  |  Size: 246 B

View File

Before

Width:  |  Height:  |  Size: 252 B

After

Width:  |  Height:  |  Size: 252 B

View File

Before

Width:  |  Height:  |  Size: 238 B

After

Width:  |  Height:  |  Size: 238 B

View File

Before

Width:  |  Height:  |  Size: 261 B

After

Width:  |  Height:  |  Size: 261 B

View File

Before

Width:  |  Height:  |  Size: 142 B

After

Width:  |  Height:  |  Size: 142 B

View File

Before

Width:  |  Height:  |  Size: 126 B

After

Width:  |  Height:  |  Size: 126 B

View File

Before

Width:  |  Height:  |  Size: 200 B

After

Width:  |  Height:  |  Size: 200 B

View File

Before

Width:  |  Height:  |  Size: 169 B

After

Width:  |  Height:  |  Size: 169 B

View File

Before

Width:  |  Height:  |  Size: 260 B

After

Width:  |  Height:  |  Size: 260 B

View File

Before

Width:  |  Height:  |  Size: 545 B

After

Width:  |  Height:  |  Size: 545 B

View File

Before

Width:  |  Height:  |  Size: 447 B

After

Width:  |  Height:  |  Size: 447 B

View File

Before

Width:  |  Height:  |  Size: 667 B

After

Width:  |  Height:  |  Size: 667 B

View File

Before

Width:  |  Height:  |  Size: 452 B

After

Width:  |  Height:  |  Size: 452 B

View File

Before

Width:  |  Height:  |  Size: 662 B

After

Width:  |  Height:  |  Size: 662 B

View File

Before

Width:  |  Height:  |  Size: 446 B

After

Width:  |  Height:  |  Size: 446 B

View File

Before

Width:  |  Height:  |  Size: 705 B

After

Width:  |  Height:  |  Size: 705 B

View File

Before

Width:  |  Height:  |  Size: 408 B

After

Width:  |  Height:  |  Size: 408 B

View File

Before

Width:  |  Height:  |  Size: 650 B

After

Width:  |  Height:  |  Size: 650 B

View File

Before

Width:  |  Height:  |  Size: 291 B

After

Width:  |  Height:  |  Size: 291 B

View File

Before

Width:  |  Height:  |  Size: 486 B

After

Width:  |  Height:  |  Size: 486 B

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

View File

Before

Width:  |  Height:  |  Size: 8.5 KiB

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 341 B

After

Width:  |  Height:  |  Size: 341 B

View File

Before

Width:  |  Height:  |  Size: 340 B

After

Width:  |  Height:  |  Size: 340 B

View File

Before

Width:  |  Height:  |  Size: 307 B

After

Width:  |  Height:  |  Size: 307 B

View File

Before

Width:  |  Height:  |  Size: 307 B

After

Width:  |  Height:  |  Size: 307 B

View File

Before

Width:  |  Height:  |  Size: 743 B

After

Width:  |  Height:  |  Size: 743 B

View File

Before

Width:  |  Height:  |  Size: 725 B

After

Width:  |  Height:  |  Size: 725 B

View File

Before

Width:  |  Height:  |  Size: 204 B

After

Width:  |  Height:  |  Size: 204 B

View File

Before

Width:  |  Height:  |  Size: 196 B

After

Width:  |  Height:  |  Size: 196 B

View File

Before

Width:  |  Height:  |  Size: 713 B

After

Width:  |  Height:  |  Size: 713 B

View File

Before

Width:  |  Height:  |  Size: 751 B

After

Width:  |  Height:  |  Size: 751 B

View File

Before

Width:  |  Height:  |  Size: 737 B

After

Width:  |  Height:  |  Size: 737 B

View File

Before

Width:  |  Height:  |  Size: 598 B

After

Width:  |  Height:  |  Size: 598 B

View File

Before

Width:  |  Height:  |  Size: 692 B

After

Width:  |  Height:  |  Size: 692 B

View File

Before

Width:  |  Height:  |  Size: 553 B

After

Width:  |  Height:  |  Size: 553 B

View File

Before

Width:  |  Height:  |  Size: 330 B

After

Width:  |  Height:  |  Size: 330 B

View File

Before

Width:  |  Height:  |  Size: 319 B

After

Width:  |  Height:  |  Size: 319 B

View File

Before

Width:  |  Height:  |  Size: 260 B

After

Width:  |  Height:  |  Size: 260 B

View File

Before

Width:  |  Height:  |  Size: 253 B

After

Width:  |  Height:  |  Size: 253 B

View File

Before

Width:  |  Height:  |  Size: 307 B

After

Width:  |  Height:  |  Size: 307 B

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 550 B

After

Width:  |  Height:  |  Size: 550 B

View File

Before

Width:  |  Height:  |  Size: 613 B

After

Width:  |  Height:  |  Size: 613 B

Some files were not shown because too many files have changed in this diff Show More