Compare commits
	
		
			45 Commits
		
	
	
		
			patch_#135
			...
			zefram_doo
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					77b8f6514a | ||
| 
						 | 
					b64fea4f70 | ||
| 
						 | 
					a6916191aa | ||
| 
						 | 
					f1eaee2281 | ||
| 
						 | 
					300abcb587 | ||
| 
						 | 
					1f66687580 | ||
| 
						 | 
					1852e967a9 | ||
| 
						 | 
					a9427d267b | ||
| 
						 | 
					2cab6aa5ef | ||
| 
						 | 
					37405e5a06 | ||
| 
						 | 
					3d2cfeace8 | ||
| 
						 | 
					df6829e553 | ||
| 
						 | 
					39a0e56c18 | ||
| 
						 | 
					b50721c701 | ||
| 
						 | 
					38ff900274 | ||
| 
						 | 
					9eda62df7b | ||
| 
						 | 
					8440d05e8c | ||
| 
						 | 
					5d3cba0bd4 | ||
| 
						 | 
					5002315ec9 | ||
| 
						 | 
					a59f53d71a | ||
| 
						 | 
					c240d399fb | ||
| 
						 | 
					ee3797746f | ||
| 
						 | 
					8a71f51b26 | ||
| 
						 | 
					a632a8abc8 | ||
| 
						 | 
					a6bd955449 | ||
| 
						 | 
					6c979a6ebb | ||
| 
						 | 
					fe50e87da1 | ||
| 
						 | 
					c8ef37f522 | ||
| 
						 | 
					1a492feb7a | ||
| 
						 | 
					eea4dbbea8 | ||
| 
						 | 
					cd30aed807 | ||
| 
						 | 
					76b9198717 | ||
| 
						 | 
					d066b91632 | ||
| 
						 | 
					1083539e9b | ||
| 
						 | 
					6afded8284 | ||
| 
						 | 
					ff5e315325 | ||
| 
						 | 
					f1211f7dae | ||
| 
						 | 
					93fb489bdb | ||
| 
						 | 
					f1ae54ed12 | ||
| 
						 | 
					7517cc4af1 | ||
| 
						 | 
					c067e52714 | ||
| 
						 | 
					2d004b19ea | ||
| 
						 | 
					de6dd30745 | ||
| 
						 | 
					3f76b77001 | ||
| 
						 | 
					5e02b3beef | 
							
								
								
									
										114
									
								
								mesecons/actionqueue.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,114 @@
 | 
			
		||||
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)
 | 
			
		||||
@@ -39,40 +39,10 @@
 | 
			
		||||
--	}
 | 
			
		||||
--}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
-- PUBLIC VARIABLES
 | 
			
		||||
mesecon={} -- contains all functions and all global variables
 | 
			
		||||
mesecon.actions_on={} -- Saves registered function callbacks for mesecon on | DEPRECATED
 | 
			
		||||
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)
 | 
			
		||||
mesecon.queue={} -- contains the ActionQueue
 | 
			
		||||
mesecon.queue.funcs={} -- contains all ActionQueue functions
 | 
			
		||||
 | 
			
		||||
-- Settings
 | 
			
		||||
dofile(minetest.get_modpath("mesecons").."/settings.lua")
 | 
			
		||||
@@ -86,6 +56,10 @@ dofile(minetest.get_modpath("mesecons").."/presets.lua");
 | 
			
		||||
-- mostly things that make the source look cleaner
 | 
			
		||||
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
 | 
			
		||||
-- This is the most important file
 | 
			
		||||
-- it handles signal transmission and basically everything else
 | 
			
		||||
@@ -101,9 +75,20 @@ dofile(minetest.get_modpath("mesecons").."/legacy.lua");
 | 
			
		||||
-- API
 | 
			
		||||
-- these are the only functions you need to remember
 | 
			
		||||
 | 
			
		||||
function mesecon:receptor_on_i(pos, rules)
 | 
			
		||||
mesecon.queue:add_function("receptor_on", function (pos, rules)
 | 
			
		||||
	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
 | 
			
		||||
		local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
		local rulenames = mesecon:rules_link_rule_all(pos, rule)
 | 
			
		||||
@@ -111,19 +96,24 @@ function mesecon:receptor_on_i(pos, rules)
 | 
			
		||||
			mesecon:turnon(np, rulename)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function mesecon:receptor_on(pos, 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="on"}
 | 
			
		||||
	else
 | 
			
		||||
		mesecon:receptor_on_i(pos, rules)
 | 
			
		||||
	end
 | 
			
		||||
	mesecon.queue:add_action(pos, "receptor_on", {rules}, nil, rules)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function mesecon:receptor_off_i(pos, rules)
 | 
			
		||||
mesecon.queue:add_function("receptor_off", function (pos, rules)
 | 
			
		||||
	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 minetest.get_node_or_nil(np) == nil then
 | 
			
		||||
			mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
 | 
			
		||||
			return
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	for _, rule in ipairs(mesecon:flattenrules(rules)) do
 | 
			
		||||
		local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
		local rulenames = mesecon:rules_link_rule_all(pos, rule)
 | 
			
		||||
@@ -131,19 +121,14 @@ function mesecon:receptor_off_i(pos, rules)
 | 
			
		||||
			if not mesecon:connected_to_receptor(np, mesecon:invertRule(rule)) then
 | 
			
		||||
				mesecon:turnoff(np, rulename)
 | 
			
		||||
			else
 | 
			
		||||
				mesecon:changesignal(np, minetest.get_node(np), rulename, mesecon.state.off)
 | 
			
		||||
				mesecon:changesignal(np, minetest.get_node(np), rulename, mesecon.state.off, 2)
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function mesecon:receptor_off(pos, 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
 | 
			
		||||
	mesecon.queue:add_action(pos, "receptor_off", {rules}, nil, rules)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,9 +22,9 @@
 | 
			
		||||
-- mesecon:effector_get_rules(node)  --> Returns the input rules of the effector (mesecon.rules.default if none specified)
 | 
			
		||||
 | 
			
		||||
-- SIGNALS
 | 
			
		||||
-- mesecon:activate(pos, node)     --> Activates   the effector node at the specific pos (calls nodedef.mesecons.effector.action_on)
 | 
			
		||||
-- 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:activate(pos, node, recdepth)		--> Activates   the effector node at the specific pos (calls nodedef.mesecons.effector.action_on), higher recdepths are executed later
 | 
			
		||||
-- mesecon:deactivate(pos, node, recdepth)		--> 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), "
 | 
			
		||||
 | 
			
		||||
-- RULES
 | 
			
		||||
-- mesecon:add_rules(name, rules) | deprecated? --> Saves rules table by name
 | 
			
		||||
@@ -41,8 +41,8 @@
 | 
			
		||||
-- HIGH-LEVEL Internals
 | 
			
		||||
-- 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: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
 | 
			
		||||
-- 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: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: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_anydir(outp., inp., d_outpr.)   --> Same as rules mesecon:rules_link but also returns true if output and input are swapped
 | 
			
		||||
@@ -177,121 +177,76 @@ function mesecon:effector_get_rules(node)
 | 
			
		||||
	return mesecon.rules.default
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
--Signals
 | 
			
		||||
-- #######################
 | 
			
		||||
-- # Signals (effectors) #
 | 
			
		||||
-- #######################
 | 
			
		||||
 | 
			
		||||
function mesecon:activate(pos, node, rulename)
 | 
			
		||||
	if MESECONS_GLOBALSTEP then
 | 
			
		||||
		if rulename == nil then
 | 
			
		||||
			for _,rule in ipairs(mesecon:effector_get_rules(node)) do
 | 
			
		||||
				mesecon:activate(pos, node, rule)
 | 
			
		||||
			end
 | 
			
		||||
			return
 | 
			
		||||
		end
 | 
			
		||||
		add_action(pos, "on", rulename)
 | 
			
		||||
	else
 | 
			
		||||
		local effector = mesecon:get_effector(node.name)
 | 
			
		||||
		if effector and effector.action_on then
 | 
			
		||||
			effector.action_on (pos, node, rulename)
 | 
			
		||||
		end
 | 
			
		||||
-- Activation:
 | 
			
		||||
mesecon.queue:add_function("activate", function (pos, rulename)
 | 
			
		||||
	node = minetest.get_node(pos)
 | 
			
		||||
	effector = mesecon:get_effector(node.name)
 | 
			
		||||
 | 
			
		||||
	if effector and effector.action_on then
 | 
			
		||||
		effector.action_on(pos, node, rulename)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function mesecon:deactivate(pos, node, rulename)
 | 
			
		||||
	if MESECONS_GLOBALSTEP then
 | 
			
		||||
		if rulename == nil then
 | 
			
		||||
			for _,rule in ipairs(mesecon:effector_get_rules(node)) do
 | 
			
		||||
				mesecon:deactivate(pos, node, rule)
 | 
			
		||||
			end
 | 
			
		||||
			return
 | 
			
		||||
function mesecon:activate(pos, node, rulename, recdepth)
 | 
			
		||||
	if rulename == nil then
 | 
			
		||||
		for _,rule in ipairs(mesecon:effector_get_rules(node)) do
 | 
			
		||||
			mesecon:activate(pos, node, rule, recdepth + 1)
 | 
			
		||||
		end
 | 
			
		||||
		add_action(pos, "off", rulename)
 | 
			
		||||
	else
 | 
			
		||||
		local effector = mesecon:get_effector(node.name)
 | 
			
		||||
		if effector and effector.action_off then
 | 
			
		||||
			effector.action_off (pos, node, rulename)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function mesecon:changesignal(pos, node, rulename, newstate)
 | 
			
		||||
	
 | 
			
		||||
	newstate = newstate or "on"
 | 
			
		||||
	--rulename = rulename or mesecon.rules.default
 | 
			
		||||
	if MESECONS_GLOBALSTEP then
 | 
			
		||||
		if rulename == nil then
 | 
			
		||||
			for _,rule in ipairs(mesecon:effector_get_rules(node)) do
 | 
			
		||||
				mesecon:changesignal(pos, node, rule, newstate)
 | 
			
		||||
			end
 | 
			
		||||
		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
 | 
			
		||||
	mesecon.queue:add_action(pos, "activate", {rulename}, nil, rulename, 1 / recdepth)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function execute_actions(dtime)
 | 
			
		||||
	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
 | 
			
		||||
 | 
			
		||||
-- Deactivation
 | 
			
		||||
mesecon.queue:add_function("deactivate", function (pos, rulename)
 | 
			
		||||
	node = minetest.get_node(pos)
 | 
			
		||||
	effector = mesecon:get_effector(node.name)
 | 
			
		||||
 | 
			
		||||
	if effector and effector.action_off then
 | 
			
		||||
		effector.action_off(pos, node, rulename)
 | 
			
		||||
	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)
 | 
			
		||||
 | 
			
		||||
function mesecon:deactivate(pos, node, rulename, recdepth)
 | 
			
		||||
	if rulename == nil then
 | 
			
		||||
		for _,rule in ipairs(mesecon:effector_get_rules(node)) do
 | 
			
		||||
			mesecon:deactivate(pos, node, rule, recdepth + 1)
 | 
			
		||||
		end
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
	mesecon.queue:add_action(pos, "deactivate", {rulename}, nil, rulename, 1 / recdepth)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
minetest.register_globalstep(execute_actions)
 | 
			
		||||
 | 
			
		||||
function add_action(pos, action, rname)
 | 
			
		||||
	for _,i in ipairs(mesecon.to_update) do
 | 
			
		||||
		if i.pos.x == pos.x and i.pos.y == pos.y and i.pos.z == pos.z and i.rname.x == rname.x and i.rname.y == rname.y and i.rname.z == rname.z then
 | 
			
		||||
			if (i.action == "on" and action == "on") or (i.action == "off" and action == "off") then
 | 
			
		||||
				--nothing
 | 
			
		||||
			elseif i.action == "coff" and action == "on" then i.action = "on"
 | 
			
		||||
			elseif i.action == "con" and action == "off" then i.action = "off"
 | 
			
		||||
			else
 | 
			
		||||
				if action == "on" or action == "con" then i.action = "con" end
 | 
			
		||||
				if action == "off" or action == "coff" then i.action = "coff" end
 | 
			
		||||
			end
 | 
			
		||||
			break
 | 
			
		||||
		end
 | 
			
		||||
-- Change
 | 
			
		||||
mesecon.queue:add_function("change", function (pos, rulename, changetype)
 | 
			
		||||
	node = minetest.get_node(pos)
 | 
			
		||||
	effector = mesecon:get_effector(node.name)
 | 
			
		||||
 | 
			
		||||
	if effector and effector.action_change then
 | 
			
		||||
		effector.action_change(pos, node, rulename, changetype)
 | 
			
		||||
	end
 | 
			
		||||
	mesecon.to_update[#mesecon.to_update+1] = {pos = pos, action = action, rname = rname}
 | 
			
		||||
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
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	mesecon.queue:add_action(pos, "change", {rulename, newstate}, nil, rulename, 1 / recdepth)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
--Rules
 | 
			
		||||
-- #########
 | 
			
		||||
-- # Rules # "Database" for rulenames
 | 
			
		||||
-- #########
 | 
			
		||||
 | 
			
		||||
function mesecon:add_rules(name, rules)
 | 
			
		||||
	mesecon.rules[name] = rules
 | 
			
		||||
@@ -410,8 +365,15 @@ function mesecon:is_power_off(pos, rulename)
 | 
			
		||||
	return false
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function mesecon:turnon(pos, rulename)
 | 
			
		||||
function mesecon:turnon(pos, rulename, recdepth)
 | 
			
		||||
	recdepth = recdepth or 2
 | 
			
		||||
	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
 | 
			
		||||
		local rules = mesecon:conductor_get_rules(node)
 | 
			
		||||
@@ -419,7 +381,7 @@ function mesecon:turnon(pos, rulename)
 | 
			
		||||
		if not rulename then
 | 
			
		||||
			for _, rule in ipairs(mesecon:flattenrules(rules)) do
 | 
			
		||||
				if mesecon:connected_to_receptor(pos, rule) then
 | 
			
		||||
					mesecon:turnon(pos, rule)
 | 
			
		||||
					mesecon:turnon(pos, rule, recdepth + 1)
 | 
			
		||||
				end
 | 
			
		||||
			end
 | 
			
		||||
			return
 | 
			
		||||
@@ -429,54 +391,71 @@ function mesecon:turnon(pos, rulename)
 | 
			
		||||
 | 
			
		||||
		for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
 | 
			
		||||
			local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
			local rulenames = mesecon:rules_link_rule_all(pos, rule)
 | 
			
		||||
			if(minetest.get_node(np).name == "ignore") then
 | 
			
		||||
				-- 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
 | 
			
		||||
				mesecon:turnon(np, rulename)
 | 
			
		||||
				for _, rulename in ipairs(rulenames) do
 | 
			
		||||
					mesecon:turnon(np, rulename, recdepth + 1)
 | 
			
		||||
				end
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
	elseif mesecon:is_effector(node.name) then
 | 
			
		||||
		mesecon:changesignal(pos, node, rulename, mesecon.state.on)
 | 
			
		||||
		mesecon:changesignal(pos, node, rulename, mesecon.state.on, recdepth)
 | 
			
		||||
		if mesecon:is_effector_off(node.name) then
 | 
			
		||||
			mesecon:activate(pos, node, rulename)
 | 
			
		||||
			mesecon:activate(pos, node, rulename, recdepth)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function mesecon:turnoff(pos, rulename)
 | 
			
		||||
mesecon.queue:add_function("turnon", function (pos, rulename, recdepth)
 | 
			
		||||
	mesecon:turnon(pos, rulename, recdepth)
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
function mesecon:turnoff(pos, rulename, recdepth)
 | 
			
		||||
	recdepth = recdepth or 2
 | 
			
		||||
	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
 | 
			
		||||
		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})
 | 
			
		||||
 | 
			
		||||
		for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
 | 
			
		||||
			local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
			local rulenames = mesecon:rules_link_rule_all(pos, rule)
 | 
			
		||||
			if(minetest.get_node(np).name == "ignore") then
 | 
			
		||||
				-- 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
 | 
			
		||||
				mesecon:turnoff(np, rulename)
 | 
			
		||||
				for _, rulename in ipairs(rulenames) do
 | 
			
		||||
					mesecon:turnoff(np, rulename, recdepth + 1)
 | 
			
		||||
				end
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
	elseif mesecon:is_effector(node.name) then
 | 
			
		||||
		mesecon:changesignal(pos, node, rulename, mesecon.state.off)
 | 
			
		||||
		mesecon:changesignal(pos, node, rulename, mesecon.state.off, recdepth)
 | 
			
		||||
		if mesecon:is_effector_on(node.name)
 | 
			
		||||
		and not mesecon:is_powered(pos) then
 | 
			
		||||
			mesecon:deactivate(pos, node, rulename)
 | 
			
		||||
			mesecon:deactivate(pos, node, rulename, recdepth + 1)
 | 
			
		||||
		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)
 | 
			
		||||
	local node = minetest.get_node(pos)
 | 
			
		||||
@@ -486,9 +465,10 @@ function mesecon:connected_to_receptor(pos, rulename)
 | 
			
		||||
	if not rules then return false end
 | 
			
		||||
 | 
			
		||||
	for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
 | 
			
		||||
		local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
		if mesecon:rules_link(np, pos) then
 | 
			
		||||
			if mesecon:find_receptor_on(np, {}, mesecon:invertRule(rule)) then
 | 
			
		||||
		local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule)
 | 
			
		||||
		for _, rname in ipairs(rulenames) do
 | 
			
		||||
			local np = mesecon:addPosRule(pos, rname)
 | 
			
		||||
			if mesecon:find_receptor_on(np, {}, mesecon:invertRule(rname)) then
 | 
			
		||||
				return true
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
@@ -518,9 +498,10 @@ function mesecon:find_receptor_on(pos, checked, rulename)
 | 
			
		||||
		-- add current position to checked
 | 
			
		||||
		table.insert(checked, {x=pos.x, y=pos.y, z=pos.z, metaindex = metaindex})
 | 
			
		||||
		for _, rule in ipairs(mesecon:rule2meta(rulename, rules)) do
 | 
			
		||||
			local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
			if mesecon:rules_link(np, pos) then
 | 
			
		||||
				if mesecon:find_receptor_on(np, checked, mesecon:invertRule(rule)) then
 | 
			
		||||
			local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule)
 | 
			
		||||
			for _, rname in ipairs(rulenames) do
 | 
			
		||||
				local np = mesecon:addPosRule(pos, rname)
 | 
			
		||||
				if mesecon:find_receptor_on(np, checked, mesecon:invertRule(rname)) then
 | 
			
		||||
					return true
 | 
			
		||||
				end
 | 
			
		||||
			end
 | 
			
		||||
@@ -583,6 +564,26 @@ function mesecon:rules_link_rule_all(output, rule) --output/input are positions
 | 
			
		||||
	return rules
 | 
			
		||||
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)
 | 
			
		||||
	return mesecon:rules_link(pos1, pos2) or mesecon:rules_link(pos2, pos1)
 | 
			
		||||
end
 | 
			
		||||
@@ -594,21 +595,23 @@ function mesecon:is_powered(pos, rule)
 | 
			
		||||
 | 
			
		||||
	if not rule then
 | 
			
		||||
		for _, rule in ipairs(mesecon:flattenrules(rules)) do
 | 
			
		||||
			local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
			local nn = minetest.get_node(np)
 | 
			
		||||
	
 | 
			
		||||
			if (mesecon:is_conductor_on (nn, mesecon:invertRule(rule)) or mesecon:is_receptor_on (nn.name))
 | 
			
		||||
			and mesecon:rules_link(np, pos) then
 | 
			
		||||
				return true
 | 
			
		||||
			local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule)
 | 
			
		||||
			for _, rname in ipairs(rulenames) do
 | 
			
		||||
				local np = mesecon:addPosRule(pos, rname)
 | 
			
		||||
				local nn = minetest.get_node(np)
 | 
			
		||||
				if (mesecon:is_conductor_on (nn, mesecon:invertRule(rname)) or mesecon:is_receptor_on (nn.name)) then
 | 
			
		||||
					return true
 | 
			
		||||
				end
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
	else
 | 
			
		||||
		local np = mesecon:addPosRule(pos, rule)
 | 
			
		||||
		local nn = minetest.get_node(np)
 | 
			
		||||
 | 
			
		||||
		if (mesecon:is_conductor_on (nn, mesecon:invertRule(rule)) or mesecon:is_receptor_on (nn.name))
 | 
			
		||||
		and mesecon:rules_link(np, pos) then
 | 
			
		||||
			return true
 | 
			
		||||
		local rulenames = mesecon:rules_link_rule_all_inverted(pos, rule)
 | 
			
		||||
		for _, rname in ipairs(rulenames) do
 | 
			
		||||
			local np = mesecon:addPosRule(pos, rname)
 | 
			
		||||
			local nn = minetest.get_node(np)
 | 
			
		||||
			if (mesecon:is_conductor_on (nn, mesecon:invertRule(rname)) or mesecon:is_receptor_on (nn.name)) then
 | 
			
		||||
				return true
 | 
			
		||||
			end
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -2,4 +2,31 @@ minetest.swap_node = minetest.swap_node or function(pos, node)
 | 
			
		||||
	local data = minetest.get_meta(pos):to_table()
 | 
			
		||||
	minetest.add_node(pos, node)
 | 
			
		||||
	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
 | 
			
		||||
 
 | 
			
		||||
@@ -1,38 +1,76 @@
 | 
			
		||||
-- Dig and place services
 | 
			
		||||
 | 
			
		||||
mesecon.on_placenode = function (pos, node)
 | 
			
		||||
	-- Receptors: Send on signal when active
 | 
			
		||||
	if mesecon:is_receptor_on(node.name) then
 | 
			
		||||
		mesecon:receptor_on(pos, mesecon:receptor_get_rules(node))
 | 
			
		||||
	elseif mesecon:is_powered(pos) then
 | 
			
		||||
		if mesecon:is_conductor(node.name) then
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	-- 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:receptor_on (pos, mesecon:conductor_get_rules(node))
 | 
			
		||||
		else
 | 
			
		||||
			mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "on")
 | 
			
		||||
			mesecon:activate(pos, 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
 | 
			
		||||
			mesecon:changesignal(pos, node, mesecon:effector_get_rules(node), "off", 1)
 | 
			
		||||
			mesecon:deactivate(pos, node, nil, 1)
 | 
			
		||||
		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
 | 
			
		||||
 | 
			
		||||
mesecon.on_dignode = function (pos, node)
 | 
			
		||||
	if mesecon:is_conductor_on(node) then
 | 
			
		||||
		mesecon:receptor_off_i(pos, mesecon:conductor_get_rules(node))
 | 
			
		||||
		mesecon:receptor_off(pos, mesecon:conductor_get_rules(node))
 | 
			
		||||
	elseif mesecon:is_receptor_on(node.name) then
 | 
			
		||||
		mesecon:receptor_off(pos, mesecon:receptor_get_rules(node))
 | 
			
		||||
	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_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)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,12 @@
 | 
			
		||||
-- SETTINGS
 | 
			
		||||
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
 | 
			
		||||
OBJECT_DETECTOR_RADIUS = 6
 | 
			
		||||
PISTON_MAXIMUM_PUSH = 15
 | 
			
		||||
MOVESTONE_MAXIMUM_PUSH = 100
 | 
			
		||||
MESECONS_GLOBALSTEP = true	-- true = receptors/effectors won't be updated
 | 
			
		||||
				-- until next globalstep, decreases server load
 | 
			
		||||
MESECONS_RESUMETIME = 4		-- time to wait when starting the server before
 | 
			
		||||
				-- processing the ActionQueue, don't set this too low
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 592 B After Width: | Height: | Size: 592 B  | 
| 
		 Before Width: | Height: | Size: 487 B After Width: | Height: | Size: 487 B  | 
| 
		 Before Width: | Height: | Size: 341 B After Width: | Height: | Size: 341 B  | 
| 
		 Before Width: | Height: | Size: 340 B After Width: | Height: | Size: 340 B  | 
| 
		 Before Width: | Height: | Size: 307 B After Width: | Height: | Size: 307 B  | 
| 
		 Before Width: | Height: | Size: 307 B After Width: | Height: | Size: 307 B  | 
| 
		 Before Width: | Height: | Size: 743 B After Width: | Height: | Size: 743 B  | 
| 
		 Before Width: | Height: | Size: 725 B After Width: | Height: | Size: 725 B  | 
| 
		 Before Width: | Height: | Size: 204 B After Width: | Height: | Size: 204 B  | 
| 
		 Before Width: | Height: | Size: 196 B After Width: | Height: | Size: 196 B  | 
| 
		 Before Width: | Height: | Size: 713 B After Width: | Height: | Size: 713 B  | 
| 
		 Before Width: | Height: | Size: 751 B After Width: | Height: | Size: 751 B  | 
| 
		 Before Width: | Height: | Size: 737 B After Width: | Height: | Size: 737 B  | 
| 
		 Before Width: | Height: | Size: 598 B After Width: | Height: | Size: 598 B  | 
| 
		 Before Width: | Height: | Size: 692 B After Width: | Height: | Size: 692 B  | 
| 
		 Before Width: | Height: | Size: 553 B After Width: | Height: | Size: 553 B  | 
| 
		 Before Width: | Height: | Size: 330 B After Width: | Height: | Size: 330 B  | 
| 
		 Before Width: | Height: | Size: 319 B After Width: | Height: | Size: 319 B  | 
| 
		 Before Width: | Height: | Size: 867 B After Width: | Height: | Size: 867 B  | 
| 
		 Before Width: | Height: | Size: 347 B After Width: | Height: | Size: 347 B  | 
| 
		 Before Width: | Height: | Size: 386 B After Width: | Height: | Size: 386 B  | 
| 
		 Before Width: | Height: | Size: 465 B After Width: | Height: | Size: 465 B  | 
| 
		 Before Width: | Height: | Size: 464 B After Width: | Height: | Size: 464 B  | 
| 
		 Before Width: | Height: | Size: 167 B After Width: | Height: | Size: 167 B  | 
| 
		 Before Width: | Height: | Size: 454 B After Width: | Height: | Size: 454 B  | 
| 
		 Before Width: | Height: | Size: 492 B After Width: | Height: | Size: 492 B  | 
| 
		 Before Width: | Height: | Size: 373 B After Width: | Height: | Size: 373 B  | 
| 
		 Before Width: | Height: | Size: 396 B After Width: | Height: | Size: 396 B  | 
@@ -169,6 +169,7 @@ function mesecon:cmpSpecial(r1, r2)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function mesecon:tablecopy(table) -- deep table copy
 | 
			
		||||
	if type(table) ~= "table" then return table end -- no need to copy
 | 
			
		||||
	local newtable = {}
 | 
			
		||||
 | 
			
		||||
	for idx, item in pairs(table) do
 | 
			
		||||
@@ -181,3 +182,14 @@ function mesecon:tablecopy(table) -- deep table copy
 | 
			
		||||
 | 
			
		||||
	return newtable
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 454 B After Width: | Height: | Size: 454 B  | 
| 
		 Before Width: | Height: | Size: 463 B After Width: | Height: | Size: 463 B  | 
| 
		 Before Width: | Height: | Size: 411 B After Width: | Height: | Size: 411 B  | 
| 
		 Before Width: | Height: | Size: 449 B After Width: | Height: | Size: 449 B  | 
| 
		 Before Width: | Height: | Size: 434 B After Width: | Height: | Size: 434 B  | 
@@ -23,14 +23,6 @@ minetest.register_chatcommand("tell", {
 | 
			
		||||
	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", {
 | 
			
		||||
	params = "<name> <value>",
 | 
			
		||||
	description = "Set health of <name> to <value> hitpoints",
 | 
			
		||||
@@ -50,16 +42,13 @@ minetest.register_chatcommand("hp", {
 | 
			
		||||
	end
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
local initialize_data = function(meta, player, command, param)
 | 
			
		||||
local function initialize_data(meta)
 | 
			
		||||
	local commands = meta:get_string("commands")
 | 
			
		||||
	meta:set_string("formspec",
 | 
			
		||||
		"invsize[9,6;]" ..
 | 
			
		||||
		"field[1,1;7.5,1;player;Player;" .. player .. "]" ..
 | 
			
		||||
		"button[1.3,2;2,1;nearest;Nearest]" ..
 | 
			
		||||
		"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]")
 | 
			
		||||
		"invsize[9,5;]" ..
 | 
			
		||||
		"textarea[0.5,0.5;8.5,4;commands;Commands;"..commands.."]" ..
 | 
			
		||||
		"label[1,3.8;@nearest, @farthest, and @random are replaced by the respective player names]" ..
 | 
			
		||||
		"button_exit[3.3,4.5;2,1;submit;Submit]")
 | 
			
		||||
	local owner = meta:get_string("owner")
 | 
			
		||||
	if owner == "" then
 | 
			
		||||
		owner = "not owned"
 | 
			
		||||
@@ -68,81 +57,64 @@ local initialize_data = function(meta, player, command, param)
 | 
			
		||||
	end
 | 
			
		||||
	meta:set_string("infotext", "Command Block\n" ..
 | 
			
		||||
		"(" .. owner .. ")\n" ..
 | 
			
		||||
		"Command: /" .. command .. " " .. param)
 | 
			
		||||
		"Commands: "..commands)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local construct = function(pos)
 | 
			
		||||
local function construct(pos)
 | 
			
		||||
	local meta = minetest.get_meta(pos)
 | 
			
		||||
 | 
			
		||||
	meta:set_string("player", "@nearest")
 | 
			
		||||
	meta:set_string("command", "time")
 | 
			
		||||
	meta:set_string("param", "7000")
 | 
			
		||||
	meta:set_string("commands", "tell @nearest Commandblock unconfigured")
 | 
			
		||||
 | 
			
		||||
	meta:set_string("owner", "")
 | 
			
		||||
 | 
			
		||||
	initialize_data(meta, "@nearest", "time", "7000")
 | 
			
		||||
	initialize_data(meta)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local after_place = function(pos, placer)
 | 
			
		||||
local function after_place(pos, placer)
 | 
			
		||||
	if placer then
 | 
			
		||||
		local meta = minetest.get_meta(pos)
 | 
			
		||||
		meta:set_string("owner", placer:get_player_name())
 | 
			
		||||
		initialize_data(meta, "@nearest", "time", "7000")
 | 
			
		||||
		initialize_data(meta)
 | 
			
		||||
	end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local receive_fields = function(pos, formname, fields, sender)
 | 
			
		||||
local function receive_fields(pos, formname, fields, sender)
 | 
			
		||||
	if fields.quit then
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
	local meta = minetest.get_meta(pos)
 | 
			
		||||
	if fields.nearest then
 | 
			
		||||
		initialize_data(meta, "@nearest", fields.command, fields.param)
 | 
			
		||||
	elseif fields.farthest then
 | 
			
		||||
		initialize_data(meta, "@farthest", fields.command, fields.param)
 | 
			
		||||
	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, fields.player, fields.command, fields.param)
 | 
			
		||||
	local owner = meta:get_string("owner")
 | 
			
		||||
	if owner ~= "" and sender:get_player_name() ~= owner then
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
	meta:set_string("commands", fields.commands)
 | 
			
		||||
 | 
			
		||||
	initialize_data(meta)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local resolve_player = function(name, pos)
 | 
			
		||||
	local get_distance = function(pos1, pos2)
 | 
			
		||||
		return math.sqrt((pos1.x - pos2.x) ^ 2 + (pos1.y - pos2.y) ^ 2 + (pos1.z - pos2.z) ^ 2)
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	if name == "@nearest" then
 | 
			
		||||
		local min_distance = math.huge
 | 
			
		||||
		for index, player in ipairs(minetest.get_connected_players()) do
 | 
			
		||||
			local distance = get_distance(pos, player:getpos())
 | 
			
		||||
			if distance < min_distance then
 | 
			
		||||
				min_distance = distance
 | 
			
		||||
				name = player:get_player_name()
 | 
			
		||||
			end
 | 
			
		||||
local function resolve_commands(commands, pos)
 | 
			
		||||
	local nearest, farthest = nil, nil
 | 
			
		||||
	local min_distance, max_distance = math.huge, -1
 | 
			
		||||
	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
 | 
			
		||||
	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
 | 
			
		||||
		if distance > max_distance then
 | 
			
		||||
			max_distance = distance
 | 
			
		||||
			farthest = player:get_player_name()
 | 
			
		||||
		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
 | 
			
		||||
	local random = players[math.random(#players)]:get_player_name()
 | 
			
		||||
	commands = commands:gsub("@nearest", nearest)
 | 
			
		||||
	commands = commands:gsub("@farthest", farthest)
 | 
			
		||||
	commands = commands:gsub("@random", random)
 | 
			
		||||
	return commands
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local commandblock_action_on = function(pos, node)
 | 
			
		||||
local function commandblock_action_on(pos, node)
 | 
			
		||||
	if node.name ~= "mesecons_commandblock:commandblock_off" then
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
@@ -150,29 +122,48 @@ local commandblock_action_on = function(pos, node)
 | 
			
		||||
	minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_on"})
 | 
			
		||||
 | 
			
		||||
	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")
 | 
			
		||||
	if owner == "" then
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
	local has_privs, missing_privs = minetest.check_player_privs(owner, command.privs)
 | 
			
		||||
	if not has_privs then
 | 
			
		||||
		minetest.chat_send_player(owner, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")")
 | 
			
		||||
		return
 | 
			
		||||
 | 
			
		||||
	local commands = resolve_commands(meta:get_string("commands"), pos)
 | 
			
		||||
	for _, command in pairs(commands:split("\n")) do
 | 
			
		||||
		local pos = command:find(" ")
 | 
			
		||||
		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
 | 
			
		||||
	local player = resolve_player(meta:get_string("player"), pos)
 | 
			
		||||
	command.func(player, meta:get_string("param"))
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local commandblock_action_off = function(pos, node)
 | 
			
		||||
local function commandblock_action_off(pos, node)
 | 
			
		||||
	if node.name == "mesecons_commandblock:commandblock_on" then
 | 
			
		||||
		minetest.swap_node(pos, {name = "mesecons_commandblock:commandblock_off"})
 | 
			
		||||
	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", {
 | 
			
		||||
	description = "Command Block",
 | 
			
		||||
	tiles = {"jeija_commandblock_off.png"},
 | 
			
		||||
@@ -181,10 +172,7 @@ minetest.register_node("mesecons_commandblock:commandblock_off", {
 | 
			
		||||
	on_construct = construct,
 | 
			
		||||
	after_place_node = after_place,
 | 
			
		||||
	on_receive_fields = receive_fields,
 | 
			
		||||
	can_dig = function(pos,player)
 | 
			
		||||
		local owner = minetest.get_meta(pos):get_string("owner")
 | 
			
		||||
		return owner == "" or owner == player:get_player_name()
 | 
			
		||||
	end,
 | 
			
		||||
	can_dig = can_dig,
 | 
			
		||||
	sounds = default.node_sound_stone_defaults(),
 | 
			
		||||
	mesecons = {effector = {
 | 
			
		||||
		action_on = commandblock_action_on
 | 
			
		||||
@@ -199,10 +187,7 @@ minetest.register_node("mesecons_commandblock:commandblock_on", {
 | 
			
		||||
	on_construct = construct,
 | 
			
		||||
	after_place_node = after_place,
 | 
			
		||||
	on_receive_fields = receive_fields,
 | 
			
		||||
	can_dig = function(pos,player)
 | 
			
		||||
		local owner = minetest.get_meta(pos):get_string("owner")
 | 
			
		||||
		return owner == "" or owner == player:get_player_name()
 | 
			
		||||
	end,
 | 
			
		||||
	can_dig = can_dig,
 | 
			
		||||
	sounds = default.node_sound_stone_defaults(),
 | 
			
		||||
	mesecons = {effector = {
 | 
			
		||||
		action_off = commandblock_action_off
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 323 B After Width: | Height: | Size: 323 B  | 
| 
		 Before Width: | Height: | Size: 282 B After Width: | Height: | Size: 282 B  | 
| 
		 Before Width: | Height: | Size: 278 B After Width: | Height: | Size: 278 B  | 
@@ -1,167 +0,0 @@
 | 
			
		||||
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(),
 | 
			
		||||
})
 | 
			
		||||
@@ -17,28 +17,18 @@ end
 | 
			
		||||
 | 
			
		||||
-- 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 def = minetest.registered_nodes[node.name]
 | 
			
		||||
	local time = def.delayer_time
 | 
			
		||||
	minetest.swap_node(pos, {name = def.delayer_onstate, param2=node.param2})
 | 
			
		||||
	minetest.after(time, delayer_turnon , {pos = pos, node = node})
 | 
			
		||||
	mesecon.queue:add_action(pos, "receptor_on", {delayer_get_output_rules(node)}, time, nil)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local delayer_deactivate = function(pos, node)
 | 
			
		||||
	local def = minetest.registered_nodes[node.name]
 | 
			
		||||
	local time = def.delayer_time
 | 
			
		||||
	minetest.swap_node(pos, {name = def.delayer_offstate, param2=node.param2})
 | 
			
		||||
	minetest.after(time, delayer_turnoff, {pos = pos, node = node})
 | 
			
		||||
	mesecon.queue:add_action(pos, "receptor_off", {delayer_get_output_rules(node)}, time, nil)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
-- Register the 2 (states) x 4 (delay times) delayers
 | 
			
		||||
 
 | 
			
		||||
@@ -6,13 +6,13 @@ local object_detector_make_formspec = function (pos)
 | 
			
		||||
	local meta = minetest.get_meta(pos)
 | 
			
		||||
	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,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
 | 
			
		||||
 | 
			
		||||
local object_detector_on_receive_fields = function(pos, formname, fields)
 | 
			
		||||
	if fields.quit then
 | 
			
		||||
		return
 | 
			
		||||
	end
 | 
			
		||||
	if not fields.scanname or not fields.digiline_channel then return end;
 | 
			
		||||
 | 
			
		||||
	local meta = minetest.get_meta(pos)
 | 
			
		||||
	meta:set_string("scanname", fields.scanname)
 | 
			
		||||
	meta:set_string("digiline_channel", fields.digiline_channel)
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 712 B After Width: | Height: | Size: 712 B  | 
| 
		 Before Width: | Height: | Size: 735 B After Width: | Height: | Size: 735 B  | 
@@ -1,2 +1,2 @@
 | 
			
		||||
mesecons
 | 
			
		||||
doors
 | 
			
		||||
mesecons
 | 
			
		||||
							
								
								
									
										130
									
								
								mesecons_doors/init.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,130 @@
 | 
			
		||||
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
 | 
			
		||||
@@ -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 },
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	groups = {dig_immediate=3, mesecon=3, mesecon_conductor_craftable=1, not_in_creative_inventory=1},
 | 
			
		||||
	groups = {dig_immediate=3, mesecon=3, not_in_creative_inventory=1},
 | 
			
		||||
	mesecons = {
 | 
			
		||||
		conductor = {
 | 
			
		||||
			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 },
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	groups = {dig_immediate=3, mesecon=3, mesecon_conductor_craftable=1, not_in_creative_inventory=1},
 | 
			
		||||
	groups = {dig_immediate=3, mesecon=3, not_in_creative_inventory=1},
 | 
			
		||||
	mesecons = {
 | 
			
		||||
		conductor = {
 | 
			
		||||
			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 },
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	groups = {dig_immediate=3, mesecon=3, mesecon_conductor_craftable=1, not_in_creative_inventory=1},
 | 
			
		||||
	groups = {dig_immediate=3, mesecon=3, not_in_creative_inventory=1},
 | 
			
		||||
	mesecons = {
 | 
			
		||||
		conductor = {
 | 
			
		||||
			states = crossover_states,
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,7 @@ minetest.register_node("mesecons_extrawires:tjunction_on", {
 | 
			
		||||
	sunlight_propagates = true,
 | 
			
		||||
	selection_box = tjunction_selectionbox,
 | 
			
		||||
	node_box = tjunction_nodebox,
 | 
			
		||||
	groups = {dig_immediate = 3, mesecon_conductor_craftable=1, not_in_creative_inventory = 1},
 | 
			
		||||
	groups = {dig_immediate = 3, not_in_creative_inventory = 1},
 | 
			
		||||
	drop = "mesecons_extrawires:tjunction_off",
 | 
			
		||||
	mesecons = {conductor = 
 | 
			
		||||
	{
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,8 @@ function gate_get_input_rules_twoinputs(node)
 | 
			
		||||
	return gate_rotate_rules(node)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function update_gate(pos)
 | 
			
		||||
function update_gate(pos, node, rulename, newstate)
 | 
			
		||||
	yc_update_real_portstates(pos, node, rulename, newstate)
 | 
			
		||||
	gate = get_gate(pos)
 | 
			
		||||
	L = rotate_ports(
 | 
			
		||||
		yc_get_real_portstates(pos),
 | 
			
		||||
@@ -46,9 +47,7 @@ function set_gate(pos, on)
 | 
			
		||||
	gate = get_gate(pos)
 | 
			
		||||
	local meta = minetest.get_meta(pos)
 | 
			
		||||
	if on ~= gate_state(pos) then
 | 
			
		||||
		yc_heat(meta)
 | 
			
		||||
		--minetest.after(0.5, yc_cool, meta)
 | 
			
		||||
		if yc_overheat(meta) then
 | 
			
		||||
		if mesecon.do_overheat(pos) then
 | 
			
		||||
			pop_gate(pos)
 | 
			
		||||
		else
 | 
			
		||||
			local node = minetest.get_node(pos)
 | 
			
		||||
@@ -77,7 +76,9 @@ end
 | 
			
		||||
function pop_gate(pos)
 | 
			
		||||
	gate = get_gate(pos)
 | 
			
		||||
	minetest.remove_node(pos)
 | 
			
		||||
	minetest.after(0.2, yc_overheat_off, pos)
 | 
			
		||||
	minetest.after(0.2, function (pos)
 | 
			
		||||
		mesecon:receptor_off(pos, mesecon.rules.flat)
 | 
			
		||||
	end , pos) -- wait for pending parsings
 | 
			
		||||
	minetest.add_item(pos, "mesecons_gates:"..gate.."_off")
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@@ -152,7 +153,6 @@ for _, gate in ipairs(gates) do
 | 
			
		||||
			walkable = true,
 | 
			
		||||
			on_construct = function(pos)
 | 
			
		||||
				local meta = minetest.get_meta(pos)
 | 
			
		||||
				meta:set_int("heat", 0)
 | 
			
		||||
				update_gate(pos)
 | 
			
		||||
			end,
 | 
			
		||||
			groups = groups,
 | 
			
		||||
@@ -220,4 +220,3 @@ minetest.register_craft({
 | 
			
		||||
		{'mesecons:mesecon', '', ''},
 | 
			
		||||
	},
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 233 B After Width: | Height: | Size: 233 B  | 
| 
		 Before Width: | Height: | Size: 231 B After Width: | Height: | Size: 231 B  | 
| 
		 Before Width: | Height: | Size: 251 B After Width: | Height: | Size: 251 B  | 
| 
		 Before Width: | Height: | Size: 241 B After Width: | Height: | Size: 241 B  | 
| 
		 Before Width: | Height: | Size: 195 B After Width: | Height: | Size: 195 B  | 
| 
		 Before Width: | Height: | Size: 195 B After Width: | Height: | Size: 195 B  | 
| 
		 Before Width: | Height: | Size: 245 B After Width: | Height: | Size: 245 B  | 
| 
		 Before Width: | Height: | Size: 835 B After Width: | Height: | Size: 835 B  | 
| 
		 Before Width: | Height: | Size: 817 B After Width: | Height: | Size: 817 B  | 
| 
		 Before Width: | Height: | Size: 305 B After Width: | Height: | Size: 305 B  | 
| 
		 Before Width: | Height: | Size: 270 B After Width: | Height: | Size: 270 B  | 
| 
		 Before Width: | Height: | Size: 293 B After Width: | Height: | Size: 293 B  | 
| 
		 Before Width: | Height: | Size: 209 B After Width: | Height: | Size: 209 B  | 
| 
		 Before Width: | Height: | Size: 253 B After Width: | Height: | Size: 253 B  | 
| 
		 Before Width: | Height: | Size: 196 B After Width: | Height: | Size: 196 B  | 
| 
		 Before Width: | Height: | Size: 246 B After Width: | Height: | Size: 246 B  | 
| 
		 Before Width: | Height: | Size: 252 B After Width: | Height: | Size: 252 B  | 
| 
		 Before Width: | Height: | Size: 238 B After Width: | Height: | Size: 238 B  | 
| 
		 Before Width: | Height: | Size: 261 B After Width: | Height: | Size: 261 B  | 
| 
		 Before Width: | Height: | Size: 142 B After Width: | Height: | Size: 142 B  | 
| 
		 Before Width: | Height: | Size: 126 B After Width: | Height: | Size: 126 B  | 
| 
		 Before Width: | Height: | Size: 200 B After Width: | Height: | Size: 200 B  | 
| 
		 Before Width: | Height: | Size: 169 B After Width: | Height: | Size: 169 B  | 
| 
		 Before Width: | Height: | Size: 244 B After Width: | Height: | Size: 244 B  | 
| 
		 Before Width: | Height: | Size: 207 B After Width: | Height: | Size: 207 B  | 
| 
		 Before Width: | Height: | Size: 260 B After Width: | Height: | Size: 260 B  | 
| 
		 Before Width: | Height: | Size: 253 B After Width: | Height: | Size: 253 B  | 
| 
		 Before Width: | Height: | Size: 307 B After Width: | Height: | Size: 307 B  | 
| 
		 Before Width: | Height: | Size: 260 B After Width: | Height: | Size: 260 B  | 
| 
		 Before Width: | Height: | Size: 545 B After Width: | Height: | Size: 545 B  | 
| 
		 Before Width: | Height: | Size: 447 B After Width: | Height: | Size: 447 B  | 
| 
		 Before Width: | Height: | Size: 667 B After Width: | Height: | Size: 667 B  | 
| 
		 Before Width: | Height: | Size: 452 B After Width: | Height: | Size: 452 B  | 
| 
		 Before Width: | Height: | Size: 662 B After Width: | Height: | Size: 662 B  | 
| 
		 Before Width: | Height: | Size: 446 B After Width: | Height: | Size: 446 B  | 
| 
		 Before Width: | Height: | Size: 705 B After Width: | Height: | Size: 705 B  | 
| 
		 Before Width: | Height: | Size: 408 B After Width: | Height: | Size: 408 B  | 
| 
		 Before Width: | Height: | Size: 650 B After Width: | Height: | Size: 650 B  | 
| 
		 Before Width: | Height: | Size: 291 B After Width: | Height: | Size: 291 B  | 
| 
		 Before Width: | Height: | Size: 486 B After Width: | Height: | Size: 486 B  | 
@@ -31,18 +31,46 @@ rules.d = {x =  0, y = 0, z = -1, name="D"}
 | 
			
		||||
------------------
 | 
			
		||||
-- 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)
 | 
			
		||||
	ports = {
 | 
			
		||||
		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),
 | 
			
		||||
	}
 | 
			
		||||
	return ports
 | 
			
		||||
	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
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local merge_portstates = function (ports, vports)
 | 
			
		||||
@@ -94,30 +122,6 @@ end
 | 
			
		||||
-- 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)
 | 
			
		||||
	mesecon:receptor_off(pos, mesecon.rules.flat)
 | 
			
		||||
end
 | 
			
		||||
@@ -164,40 +168,29 @@ local safe_serialize = function(value)
 | 
			
		||||
	return minetest.serialize(deep_copy(value))
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local interrupt = function(params)
 | 
			
		||||
	lc_update(params.pos, {type="interrupt", iid = params.iid})
 | 
			
		||||
end
 | 
			
		||||
mesecon.queue:add_function("lc_interrupt", function (pos, iid, luac_id)
 | 
			
		||||
	-- There is no luacontroller anymore / it has been reprogrammed / replaced
 | 
			
		||||
	if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
 | 
			
		||||
	lc_update(pos, {type="interrupt", iid = iid})
 | 
			
		||||
end)
 | 
			
		||||
 | 
			
		||||
local getinterrupt = function(pos)
 | 
			
		||||
	local interrupt = function (time, iid) -- iid = interrupt id
 | 
			
		||||
		if type(time) ~= "number" then return end
 | 
			
		||||
		local iid = iid or math.random()
 | 
			
		||||
		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})
 | 
			
		||||
		luac_id = minetest.get_meta(pos):get_int("luac_id")
 | 
			
		||||
		mesecon.queue:add_action(pos, "lc_interrupt", {iid, luac_id}, time, iid, 1)
 | 
			
		||||
	end
 | 
			
		||||
	return interrupt
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local getdigiline_send = function (pos)
 | 
			
		||||
	local digiline_send = function (channel, msg)
 | 
			
		||||
		if digiline then
 | 
			
		||||
local getdigiline_send = function(pos)
 | 
			
		||||
	if not digiline then return end
 | 
			
		||||
	-- Send messages on next serverstep
 | 
			
		||||
	return function(channel, msg)
 | 
			
		||||
		minetest.after(0, function()
 | 
			
		||||
			digiline:receptor_send(pos, digiline.rules.default, channel, msg)
 | 
			
		||||
		end
 | 
			
		||||
		end)
 | 
			
		||||
	end
 | 
			
		||||
	return digiline_send
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local create_environment = function(pos, mem, event)
 | 
			
		||||
@@ -215,6 +208,8 @@ local create_environment = function(pos, mem, event)
 | 
			
		||||
			mem = mem,
 | 
			
		||||
			tostring = tostring,
 | 
			
		||||
			tonumber = tonumber,
 | 
			
		||||
			heat = minetest.get_meta(pos):get_int("heat"),
 | 
			
		||||
			heat_max = OVERHEAT_MAX,
 | 
			
		||||
			string = {
 | 
			
		||||
				byte = string.byte,
 | 
			
		||||
				char = string.char,
 | 
			
		||||
@@ -224,6 +219,7 @@ local create_environment = function(pos, mem, event)
 | 
			
		||||
				gsub = string.gsub,
 | 
			
		||||
				len = string.len,
 | 
			
		||||
				lower = string.lower,
 | 
			
		||||
				upper = string.upper,
 | 
			
		||||
				match = string.match,
 | 
			
		||||
				rep = string.rep,
 | 
			
		||||
				reverse = string.reverse,
 | 
			
		||||
@@ -281,14 +277,10 @@ local create_sandbox = function (code, env)
 | 
			
		||||
	return f
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local do_overheat = function (pos, meta)
 | 
			
		||||
	-- Overheat protection
 | 
			
		||||
	heat(meta)
 | 
			
		||||
	--minetest.after(0.5, cool, meta)
 | 
			
		||||
	if overheat(meta) then
 | 
			
		||||
local lc_overheat = function (pos, meta)
 | 
			
		||||
	if mesecon.do_overheat(pos) then -- if too hot
 | 
			
		||||
		local node = minetest.get_node(pos)
 | 
			
		||||
		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
 | 
			
		||||
		return true
 | 
			
		||||
	end
 | 
			
		||||
@@ -302,20 +294,6 @@ local save_memory = function(meta, mem)
 | 
			
		||||
	meta:set_string("lc_memory", safe_serialize(mem))
 | 
			
		||||
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)
 | 
			
		||||
	if type(var) == "table" then
 | 
			
		||||
		return false
 | 
			
		||||
@@ -329,8 +307,7 @@ end
 | 
			
		||||
 | 
			
		||||
lc_update = function (pos, event)
 | 
			
		||||
	local meta = minetest.get_meta(pos)
 | 
			
		||||
	if not interrupt_allow(meta, event) then return end
 | 
			
		||||
	if do_overheat(pos, meta) then return end
 | 
			
		||||
	if lc_overheat(pos) then return end
 | 
			
		||||
 | 
			
		||||
	-- load code & mem from memory
 | 
			
		||||
	local mem  = load_memory(meta)
 | 
			
		||||
@@ -338,7 +315,7 @@ lc_update = function (pos, event)
 | 
			
		||||
 | 
			
		||||
	-- make sure code is ok and create environment
 | 
			
		||||
	local prohibited = code_prohibited(code)
 | 
			
		||||
	if 	prohibited then return prohibited end
 | 
			
		||||
	if prohibited then return prohibited end
 | 
			
		||||
	local env = create_environment(pos, mem, event)
 | 
			
		||||
 | 
			
		||||
	-- create the sandbox and execute code
 | 
			
		||||
@@ -351,7 +328,7 @@ lc_update = function (pos, event)
 | 
			
		||||
	save_memory(meta, mem)
 | 
			
		||||
 | 
			
		||||
	-- Actually set the ports
 | 
			
		||||
	minetest.after(0, action, pos, env.port)
 | 
			
		||||
	action(pos, env.port)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local reset_meta = function(pos, code, errmsg)
 | 
			
		||||
@@ -366,11 +343,11 @@ local reset_meta = function(pos, code, errmsg)
 | 
			
		||||
		"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
 | 
			
		||||
		"label[0.1,5;"..errmsg.."]")
 | 
			
		||||
	meta:set_int("heat", 0)
 | 
			
		||||
	meta:set_int("luac_id", math.random(1, 1000000))
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
local reset = function (pos)
 | 
			
		||||
	minetest.get_meta(pos):set_string("lc_interrupts", "")
 | 
			
		||||
	action(pos, {a=false, b=false, c=false, d=false}, true)
 | 
			
		||||
	action(pos, {a=false, b=false, c=false, d=false})
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
--        ______
 | 
			
		||||
@@ -457,6 +434,7 @@ local mesecons = {
 | 
			
		||||
	{
 | 
			
		||||
		rules = input_rules[cid],
 | 
			
		||||
		action_change = function (pos, _, rulename, newstate)
 | 
			
		||||
			lc_update_real_portstates(pos, rulename, newstate)
 | 
			
		||||
			lc_update(pos, {type=newstate,  pin=rulename})
 | 
			
		||||
		end,
 | 
			
		||||
	},
 | 
			
		||||
@@ -488,33 +466,50 @@ minetest.register_node(nodename, {
 | 
			
		||||
	node_box = nodebox,
 | 
			
		||||
	on_construct = reset_meta,
 | 
			
		||||
	on_receive_fields = function(pos, formname, fields)
 | 
			
		||||
		if fields.quit then
 | 
			
		||||
		if not fields.program then
 | 
			
		||||
			return
 | 
			
		||||
		end
 | 
			
		||||
		reset(pos)
 | 
			
		||||
		reset_meta(pos, fields.code)
 | 
			
		||||
		local err = lc_update(pos, {type="program"})
 | 
			
		||||
		if err then print(err) end
 | 
			
		||||
		reset_meta(pos, fields.code, err)
 | 
			
		||||
		if err then
 | 
			
		||||
			print(err)
 | 
			
		||||
			reset_meta(pos, fields.code, err)
 | 
			
		||||
		end
 | 
			
		||||
	end,
 | 
			
		||||
	on_timer = handle_timer,
 | 
			
		||||
	sounds = default.node_sound_stone_defaults(),
 | 
			
		||||
	mesecons = mesecons,
 | 
			
		||||
	digiline = digiline,
 | 
			
		||||
	is_luacontroller = true,
 | 
			
		||||
	virtual_portstates = {	a = a == 1, -- virtual portstates are
 | 
			
		||||
					b = b == 1, -- the ports the the
 | 
			
		||||
					c = c == 1, -- controller powers itself
 | 
			
		||||
					d = d == 1},-- so those that light up
 | 
			
		||||
				b = b == 1, -- the ports the the
 | 
			
		||||
				c = c == 1, -- controller powers itself
 | 
			
		||||
				d = d == 1},-- so those that light up
 | 
			
		||||
	after_dig_node = function (pos, node)
 | 
			
		||||
		mesecon:receptor_off(pos, output_rules)
 | 
			
		||||
	end,
 | 
			
		||||
	is_luacontroller = true,
 | 
			
		||||
})
 | 
			
		||||
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", {
 | 
			
		||||
	drawtype = "nodebox",
 | 
			
		||||
	tiles = {
 | 
			
		||||
@@ -540,12 +535,14 @@ minetest.register_node(BASENAME .. "_burnt", {
 | 
			
		||||
		reset(pos)
 | 
			
		||||
		reset_meta(pos, fields.code)
 | 
			
		||||
		local err = lc_update(pos, {type="program"})
 | 
			
		||||
		if err then print(err) end
 | 
			
		||||
		reset_meta(pos, fields.code, err)
 | 
			
		||||
		if err then
 | 
			
		||||
			print(err)
 | 
			
		||||
			reset_meta(pos, fields.code, err)
 | 
			
		||||
		end
 | 
			
		||||
	end,
 | 
			
		||||
	sounds = default.node_sound_stone_defaults(),
 | 
			
		||||
	is_luacontroller = true,
 | 
			
		||||
	virtual_portstates = {a = false, b = false, c = false, d = false},
 | 
			
		||||
	mesecons = mesecons_burnt,
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
------------------------
 | 
			
		||||
 
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB  | 
| 
		 Before Width: | Height: | Size: 4.2 KiB After Width: | Height: | Size: 4.2 KiB  | 
| 
		 Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB  | 
| 
		 Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB  | 
| 
		 Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB  |