2013-01-13 00:18:25 +01:00
|
|
|
-- Reference
|
|
|
|
-- ports = get_real_portstates(pos): gets if inputs are powered from outside
|
|
|
|
-- newport = merge_portstates(state1, state2): just does result = state1 or state2 for every port
|
2013-04-01 22:24:01 +02:00
|
|
|
-- action_setports(pos, rule, state): activates/deactivates the mesecons according to the portstates (helper for action)
|
2013-01-13 00:18:25 +01:00
|
|
|
-- action(pos, ports): Applies new portstates to a luacontroller at pos
|
2013-01-13 17:33:16 +01:00
|
|
|
-- lc_update(pos): updates the controller at pos by executing the code
|
2013-01-13 00:18:25 +01:00
|
|
|
-- reset_meta (pos, code, errmsg): performs a software-reset, installs new code and prints error messages
|
|
|
|
-- reset (pos): performs a hardware reset, turns off all ports
|
|
|
|
--
|
|
|
|
-- The Sandbox
|
|
|
|
-- The whole code of the controller runs in a sandbox,
|
|
|
|
-- a very restricted environment.
|
|
|
|
-- However, as this does not prevent you from using e.g. loops,
|
|
|
|
-- we need to check for these prohibited commands first.
|
|
|
|
-- Actually the only way to damage the server is to
|
|
|
|
-- use too much memory from the sandbox.
|
|
|
|
-- You can add more functions to the environment
|
|
|
|
-- (see where local env is defined)
|
|
|
|
-- Something nice to play is is appending minetest.env to it.
|
|
|
|
|
|
|
|
local BASENAME = "mesecons_luacontroller:luacontroller"
|
|
|
|
|
|
|
|
local rules = {}
|
2013-01-13 17:33:16 +01:00
|
|
|
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"}
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
------------------
|
|
|
|
-- Action stuff --
|
|
|
|
------------------
|
|
|
|
-- These helpers are required to set the portstates of the luacontroller
|
|
|
|
|
2013-01-13 00:18:25 +01:00
|
|
|
local get_real_portstates = function(pos) -- determine if ports are powered (by itself or from outside)
|
|
|
|
ports = {
|
2013-06-20 04:38:34 +02:00
|
|
|
a = mesecon:is_power_on(mesecon:addPosRule(pos, rules.a), mesecon:invertRule(rules.a))
|
2013-01-13 00:18:25 +01:00
|
|
|
and mesecon:rules_link(mesecon:addPosRule(pos, rules.a), pos),
|
2013-06-20 04:38:34 +02:00
|
|
|
b = mesecon:is_power_on(mesecon:addPosRule(pos, rules.b), mesecon:invertRule(rules.b))
|
2013-01-13 00:18:25 +01:00
|
|
|
and mesecon:rules_link(mesecon:addPosRule(pos, rules.b), pos),
|
2013-06-20 04:38:34 +02:00
|
|
|
c = mesecon:is_power_on(mesecon:addPosRule(pos, rules.c), mesecon:invertRule(rules.c))
|
2013-01-13 00:18:25 +01:00
|
|
|
and mesecon:rules_link(mesecon:addPosRule(pos, rules.c), pos),
|
2013-06-20 04:38:34 +02:00
|
|
|
d = mesecon:is_power_on(mesecon:addPosRule(pos, rules.d), mesecon:invertRule(rules.d))
|
2013-01-13 00:18:25 +01:00
|
|
|
and mesecon:rules_link(mesecon:addPosRule(pos, rules.d), pos),
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
end
|
|
|
|
|
|
|
|
local merge_portstates = function (ports, vports)
|
|
|
|
local npo = {a=false, b=false, c=false, d=false}
|
|
|
|
npo.a = vports.a or ports.a
|
|
|
|
npo.b = vports.b or ports.b
|
|
|
|
npo.c = vports.c or ports.c
|
|
|
|
npo.d = vports.d or ports.d
|
|
|
|
return npo
|
|
|
|
end
|
|
|
|
|
2013-04-01 22:24:01 +02:00
|
|
|
local generate_name = function (ports)
|
2013-01-20 17:48:43 +01:00
|
|
|
local overwrite = overwrite or {}
|
2013-04-01 22:24:01 +02:00
|
|
|
local d = ports.d and 1 or 0
|
|
|
|
local c = ports.c and 1 or 0
|
|
|
|
local b = ports.b and 1 or 0
|
|
|
|
local a = ports.a and 1 or 0
|
2013-01-20 17:48:43 +01:00
|
|
|
return BASENAME..d..c..b..a
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2013-04-01 22:24:01 +02:00
|
|
|
local setport = function (pos, rule, state)
|
2013-01-22 18:26:27 +01:00
|
|
|
if state then
|
|
|
|
mesecon:receptor_on(pos, {rule})
|
|
|
|
else
|
|
|
|
mesecon:receptor_off(pos, {rule})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-01 22:24:01 +02:00
|
|
|
local action = function (pos, ports)
|
2013-01-13 17:33:16 +01:00
|
|
|
local name = minetest.env:get_node(pos).name
|
|
|
|
local vports = minetest.registered_nodes[name].virtual_portstates
|
2013-01-20 17:48:43 +01:00
|
|
|
local newname = generate_name(ports)
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
if name ~= newname and vports then
|
2013-01-20 17:48:43 +01:00
|
|
|
local rules_on = {}
|
|
|
|
local rules_off = {}
|
2013-01-22 18:26:27 +01:00
|
|
|
|
|
|
|
mesecon:swap_node(pos, newname)
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-04-01 22:24:01 +02:00
|
|
|
if ports.a ~= vports.a then setport(pos, rules.a, ports.a) end
|
|
|
|
if ports.b ~= vports.b then setport(pos, rules.b, ports.b) end
|
|
|
|
if ports.c ~= vports.c then setport(pos, rules.c, ports.c) end
|
|
|
|
if ports.d ~= vports.d then setport(pos, rules.d, ports.d) end
|
|
|
|
end
|
2013-02-10 23:08:59 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
--------------------
|
|
|
|
-- Overheat stuff --
|
|
|
|
--------------------
|
|
|
|
|
2013-01-13 00:18:25 +01:00
|
|
|
local heat = function (meta) -- warm up
|
|
|
|
h = meta:get_int("heat")
|
|
|
|
if h ~= nil then
|
|
|
|
meta:set_int("heat", h + 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-05 06:45:19 +02:00
|
|
|
--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
|
2013-01-13 00:18:25 +01:00
|
|
|
|
|
|
|
local overheat = function (meta) -- determine if too hot
|
|
|
|
h = meta:get_int("heat")
|
|
|
|
if h == nil then return true end -- if nil then overheat
|
2013-06-05 06:45:19 +02:00
|
|
|
if h > 40 then
|
2013-01-13 00:18:25 +01:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local overheat_off = function(pos)
|
2013-01-13 11:05:04 +01:00
|
|
|
mesecon:receptor_off(pos, mesecon.rules.flat)
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
-------------------
|
|
|
|
-- Parsing stuff --
|
|
|
|
-------------------
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
local code_prohibited = function(code)
|
2013-01-13 00:18:25 +01:00
|
|
|
-- Clean code
|
2013-06-10 22:40:34 +02:00
|
|
|
local prohibited = {"while", "for", "repeat", "until", "function", "goto"}
|
2013-01-13 00:18:25 +01:00
|
|
|
for _, p in ipairs(prohibited) do
|
|
|
|
if string.find(code, p) then
|
|
|
|
return "Prohibited command: "..p
|
|
|
|
end
|
|
|
|
end
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
|
|
|
|
2013-06-06 22:38:40 +02:00
|
|
|
local safe_print = function(param)
|
2013-01-13 11:05:04 +01:00
|
|
|
print(dump(param))
|
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-06-10 22:40:34 +02:00
|
|
|
deep_copy = function(original, visited) --deep copy that removes functions
|
|
|
|
visited = visited or {}
|
|
|
|
if visited[original] ~= nil then --already visited this node
|
|
|
|
return visited[original]
|
|
|
|
end
|
2013-06-06 22:38:40 +02:00
|
|
|
if type(original) == 'table' then --nested table
|
|
|
|
local copy = {}
|
2013-06-10 22:40:34 +02:00
|
|
|
visited[original] = copy
|
2013-06-06 22:38:40 +02:00
|
|
|
for key, value in next, original, nil do
|
2013-06-10 22:40:34 +02:00
|
|
|
copy[deep_copy(key, visited)] = deep_copy(value, visited)
|
2013-06-06 22:38:40 +02:00
|
|
|
end
|
2013-06-10 22:40:34 +02:00
|
|
|
setmetatable(copy, deep_copy(getmetatable(original), visited))
|
2013-06-06 22:38:40 +02:00
|
|
|
return copy
|
|
|
|
elseif type(original) == 'function' then --ignore functions
|
|
|
|
return nil
|
|
|
|
else --by-value type
|
|
|
|
return original
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local safe_serialize = function(value)
|
|
|
|
return minetest.serialize(deep_copy(value))
|
|
|
|
end
|
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
local interrupt = function(params)
|
|
|
|
lc_update(params.pos, {type="interrupt", iid = params.iid})
|
|
|
|
end
|
|
|
|
|
|
|
|
local getinterrupt = function(pos)
|
|
|
|
local interrupt = function (time, iid) -- iid = interrupt id
|
2013-01-14 17:58:14 +01:00
|
|
|
if type(time) ~= "number" then return end
|
2013-02-12 10:25:24 +01:00
|
|
|
local iid = iid or math.random()
|
2013-01-13 17:33:16 +01:00
|
|
|
local meta = minetest.env:get_meta(pos)
|
|
|
|
local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}
|
2013-05-16 03:36:16 +02:00
|
|
|
local found = false
|
2013-06-06 22:38:40 +02:00
|
|
|
local search = safe_serialize(iid)
|
2013-05-16 03:36:16 +02:00
|
|
|
for _, i in ipairs(interrupts) do
|
2013-06-06 22:38:40 +02:00
|
|
|
if safe_serialize(i) == search then
|
2013-05-16 03:36:16 +02:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not found then
|
|
|
|
table.insert(interrupts, iid)
|
2013-06-06 22:38:40 +02:00
|
|
|
meta:set_string("lc_interrupts", safe_serialize(interrupts))
|
2013-05-16 03:36:16 +02:00
|
|
|
end
|
2013-01-13 17:33:16 +01:00
|
|
|
minetest.after(time, interrupt, {pos=pos, iid = iid})
|
|
|
|
end
|
|
|
|
return interrupt
|
|
|
|
end
|
|
|
|
|
2013-01-19 21:45:39 +01:00
|
|
|
local getdigiline_send = function (pos)
|
|
|
|
local digiline_send = function (channel, msg)
|
|
|
|
if digiline then
|
2013-01-19 22:18:28 +01:00
|
|
|
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
|
2013-01-19 21:45:39 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return digiline_send
|
|
|
|
end
|
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
local create_environment = function(pos, mem, event)
|
2013-01-13 00:18:25 +01:00
|
|
|
-- Gather variables for the environment
|
|
|
|
local vports = minetest.registered_nodes[minetest.env:get_node(pos).name].virtual_portstates
|
|
|
|
vports = {a = vports.a, b = vports.b, c = vports.c, d = vports.d}
|
|
|
|
local rports = get_real_portstates(pos)
|
|
|
|
|
2013-03-23 23:41:44 +01:00
|
|
|
return {
|
2013-06-06 22:38:40 +02:00
|
|
|
print = safe_print,
|
2013-01-13 11:05:04 +01:00
|
|
|
pin = merge_portstates(vports, rports),
|
|
|
|
port = vports,
|
2013-01-13 17:33:16 +01:00
|
|
|
interrupt = getinterrupt(pos),
|
2013-01-19 21:45:39 +01:00
|
|
|
digiline_send = getdigiline_send(pos),
|
2013-01-13 17:33:16 +01:00
|
|
|
mem = mem,
|
2013-01-22 21:15:49 +01:00
|
|
|
tostring = tostring,
|
|
|
|
tonumber = tonumber,
|
2013-03-23 23:41:44 +01:00
|
|
|
string = {
|
|
|
|
byte = string.byte,
|
|
|
|
char = string.char,
|
|
|
|
find = string.find,
|
|
|
|
format = string.format,
|
|
|
|
gmatch = string.gmatch,
|
|
|
|
gsub = string.gsub,
|
|
|
|
len = string.len,
|
|
|
|
lower = string.lower,
|
|
|
|
match = string.match,
|
|
|
|
rep = string.rep,
|
|
|
|
reverse = string.reverse,
|
|
|
|
sub = string.sub,
|
|
|
|
},
|
|
|
|
math = {
|
|
|
|
abs = math.abs,
|
|
|
|
acos = math.acos,
|
|
|
|
asin = math.asin,
|
|
|
|
atan = math.atan,
|
|
|
|
atan2 = math.atan2,
|
|
|
|
ceil = math.ceil,
|
|
|
|
cos = math.cos,
|
|
|
|
cosh = math.cosh,
|
|
|
|
deg = math.deg,
|
|
|
|
exp = math.exp,
|
|
|
|
floor = math.floor,
|
|
|
|
fmod = math.fmod,
|
|
|
|
frexp = math.frexp,
|
|
|
|
huge = math.huge,
|
|
|
|
ldexp = math.ldexp,
|
|
|
|
log = math.log,
|
|
|
|
log10 = math.log10,
|
|
|
|
max = math.max,
|
|
|
|
min = math.min,
|
|
|
|
modf = math.modf,
|
|
|
|
pi = math.pi,
|
|
|
|
pow = math.pow,
|
|
|
|
rad = math.rad,
|
|
|
|
random = math.random,
|
|
|
|
sin = math.sin,
|
|
|
|
sinh = math.sinh,
|
|
|
|
sqrt = math.sqrt,
|
|
|
|
tan = math.tan,
|
|
|
|
tanh = math.tanh,
|
|
|
|
},
|
2013-03-26 15:12:56 +01:00
|
|
|
table = {
|
|
|
|
insert = table.insert,
|
|
|
|
maxn = table.maxn,
|
|
|
|
remove = table.remove,
|
|
|
|
sort = table.sort
|
|
|
|
},
|
2013-03-23 23:41:44 +01:00
|
|
|
event = event,
|
|
|
|
}
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
local create_sandbox = function (code, env)
|
2013-01-13 00:18:25 +01:00
|
|
|
-- Create Sandbox
|
|
|
|
if code:byte(1) == 27 then
|
2013-01-13 11:05:04 +01:00
|
|
|
return _, "You Hacker You! Don't use binary code!"
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
2013-01-13 11:05:04 +01:00
|
|
|
f, msg = loadstring(code)
|
|
|
|
if not f then return _, msg end
|
2013-01-13 00:18:25 +01:00
|
|
|
setfenv(f, env)
|
2013-01-13 11:05:04 +01:00
|
|
|
return f
|
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
local do_overheat = function (pos, meta)
|
2013-01-13 00:18:25 +01:00
|
|
|
-- Overheat protection
|
|
|
|
heat(meta)
|
2013-06-05 06:45:19 +02:00
|
|
|
--minetest.after(0.5, cool, meta)
|
2013-01-13 00:18:25 +01:00
|
|
|
if overheat(meta) then
|
2013-05-16 03:36:16 +02:00
|
|
|
mesecon:swap_node(pos, BASENAME.."_burnt")
|
|
|
|
minetest.env:get_meta(pos):set_string("lc_interrupts", "")
|
2013-01-13 00:18:25 +01:00
|
|
|
minetest.after(0.2, overheat_off, pos) -- wait for pending operations
|
2013-01-20 17:48:43 +01:00
|
|
|
return true
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local load_memory = function(meta)
|
|
|
|
return minetest.deserialize(meta:get_string("lc_memory")) or {}
|
|
|
|
end
|
|
|
|
|
|
|
|
local save_memory = function(meta, mem)
|
2013-06-06 22:38:40 +02:00
|
|
|
meta:set_string("lc_memory", safe_serialize(mem))
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
local interrupt_allow = function (meta, event)
|
|
|
|
if event.type ~= "interrupt" then return true end
|
|
|
|
|
|
|
|
local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}
|
2013-06-06 22:38:40 +02:00
|
|
|
local search = safe_serialize(event.iid)
|
2013-01-13 17:33:16 +01:00
|
|
|
for _, i in ipairs(interrupts) do
|
2013-06-06 22:38:40 +02:00
|
|
|
if safe_serialize(i) == search then
|
2013-01-13 17:33:16 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2013-01-14 17:58:14 +01:00
|
|
|
local ports_invalid = function (var)
|
|
|
|
if type(var) == "table" then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return "The ports you set are invalid"
|
|
|
|
end
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
----------------------
|
|
|
|
-- Parsing function --
|
|
|
|
----------------------
|
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
lc_update = function (pos, event)
|
2013-01-13 11:05:04 +01:00
|
|
|
local meta = minetest.env:get_meta(pos)
|
2013-01-13 17:33:16 +01:00
|
|
|
if not interrupt_allow(meta, event) then return end
|
2013-01-20 17:48:43 +01:00
|
|
|
if do_overheat(pos, meta) then return end
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
-- load code & mem from memory
|
2013-01-13 11:05:04 +01:00
|
|
|
local mem = load_memory(meta)
|
|
|
|
local code = meta:get_string("code")
|
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
-- make sure code is ok and create environment
|
2013-01-13 11:05:04 +01:00
|
|
|
local prohibited = code_prohibited(code)
|
|
|
|
if prohibited then return prohibited end
|
2013-01-13 17:33:16 +01:00
|
|
|
local env = create_environment(pos, mem, event)
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
-- create the sandbox and execute code
|
2013-01-13 11:05:04 +01:00
|
|
|
local chunk, msg = create_sandbox (code, env)
|
|
|
|
if not chunk then return msg end
|
2013-01-14 17:58:14 +01:00
|
|
|
local success, msg = pcall(f)
|
2013-01-13 11:05:04 +01:00
|
|
|
if not success then return msg end
|
2013-01-14 17:58:14 +01:00
|
|
|
if ports_invalid(env.port) then return ports_invalid(env.port) end
|
2013-01-13 11:05:04 +01:00
|
|
|
|
|
|
|
save_memory(meta, mem)
|
2013-01-13 00:18:25 +01:00
|
|
|
|
|
|
|
-- Actually set the ports
|
2013-04-01 22:24:01 +02:00
|
|
|
minetest.after(0, action, pos, env.port)
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local reset_meta = function(pos, code, errmsg)
|
|
|
|
local meta = minetest.env:get_meta(pos)
|
2013-03-15 22:46:59 +01:00
|
|
|
meta:set_string("code", code)
|
2013-03-15 22:21:16 +01:00
|
|
|
if minetest.formspec_escape then
|
|
|
|
code = minetest.formspec_escape(code or "")
|
|
|
|
errmsg = minetest.formspec_escape(errmsg or "")
|
|
|
|
else
|
|
|
|
code = string.gsub(code or "", "%[", "(") -- would otherwise
|
|
|
|
code = string.gsub(code, "%]", ")") -- corrupt formspec
|
|
|
|
errmsg = string.gsub(errmsg or "", "%[", "(") -- would otherwise
|
|
|
|
errmsg = string.gsub(errmsg, "%]", ")") -- corrupt formspec
|
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
meta:set_string("formspec", "size[10,8]"..
|
2013-01-19 12:03:27 +01:00
|
|
|
"background[-0.2,-0.25;10.4,8.75;jeija_luac_background.png]"..
|
|
|
|
"textarea[0.2,0.6;10.2,5;code;;"..code.."]"..
|
|
|
|
"image_button[3.75,6;2.5,1;jeija_luac_runbutton.png;program;]"..
|
|
|
|
"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
|
2013-01-20 17:48:43 +01:00
|
|
|
"label[0.1,5;"..errmsg.."]")
|
2013-01-13 00:18:25 +01:00
|
|
|
meta:set_int("heat", 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
local reset = function (pos)
|
2013-02-12 10:25:24 +01:00
|
|
|
minetest.env:get_meta(pos):set_string("lc_interrupts", "")
|
2013-01-22 18:26:27 +01:00
|
|
|
action(pos, {a=false, b=false, c=false, d=false}, true)
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- ______
|
|
|
|
-- |
|
2013-06-22 18:11:06 +02:00
|
|
|
-- |
|
|
|
|
-- | __ ___ _ __ _ _
|
|
|
|
-- | | | | | |\ | | |_| | | | | |_ |_|
|
|
|
|
-- |___| |______ |__| | \| | | \ |__| |_ |_ |_ |\
|
|
|
|
-- |
|
|
|
|
-- |
|
2013-01-13 00:18:25 +01:00
|
|
|
--
|
2013-01-13 11:05:04 +01:00
|
|
|
|
|
|
|
-----------------------
|
|
|
|
-- Node Registration --
|
|
|
|
-----------------------
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
local output_rules={}
|
|
|
|
local input_rules={}
|
|
|
|
|
2013-01-19 21:45:39 +01:00
|
|
|
local nodebox = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = {
|
|
|
|
{ -8/16, -8/16, -8/16, 8/16, -7/16, 8/16 }, -- bottom slab
|
|
|
|
{ -5/16, -7/16, -5/16, 5/16, -6/16, 5/16 }, -- circuit board
|
|
|
|
{ -3/16, -6/16, -3/16, 3/16, -5/16, 3/16 }, -- IC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local selectionbox = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = { -8/16, -8/16, -8/16, 8/16, -5/16, 8/16 },
|
|
|
|
}
|
|
|
|
|
|
|
|
local digiline = {
|
|
|
|
receptor = {},
|
|
|
|
effector = {
|
|
|
|
action = function (pos, node, channel, msg)
|
2013-01-20 17:48:43 +01:00
|
|
|
lc_update (pos, {type = "digiline", channel = channel, msg = msg})
|
2013-01-19 21:45:39 +01:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 22:24:01 +02:00
|
|
|
for a = 0, 1 do -- 0 = off; 1 = on
|
|
|
|
for b = 0, 1 do
|
|
|
|
for c = 0, 1 do
|
|
|
|
for d = 0, 1 do
|
2013-01-20 17:48:43 +01:00
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
local cid = tostring(d)..tostring(c)..tostring(b)..tostring(a)
|
|
|
|
local nodename = BASENAME..cid
|
2013-01-13 00:18:25 +01:00
|
|
|
local top = "jeija_luacontroller_top.png"
|
|
|
|
if a == 1 then
|
|
|
|
top = top.."^jeija_luacontroller_LED_A.png"
|
|
|
|
end
|
|
|
|
if b == 1 then
|
|
|
|
top = top.."^jeija_luacontroller_LED_B.png"
|
|
|
|
end
|
|
|
|
if c == 1 then
|
|
|
|
top = top.."^jeija_luacontroller_LED_C.png"
|
|
|
|
end
|
|
|
|
if d == 1 then
|
|
|
|
top = top.."^jeija_luacontroller_LED_D.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
if a + b + c + d ~= 0 then
|
2013-06-05 06:45:19 +02:00
|
|
|
groups = {dig_immediate=2, not_in_creative_inventory=1, overheat = 1}
|
2013-01-13 00:18:25 +01:00
|
|
|
else
|
2013-06-05 06:45:19 +02:00
|
|
|
groups = {dig_immediate=2, overheat = 1}
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 17:33:16 +01:00
|
|
|
output_rules[cid] = {}
|
|
|
|
input_rules[cid] = {}
|
|
|
|
if (a == 1) then table.insert(output_rules[cid], rules.a) end
|
|
|
|
if (b == 1) then table.insert(output_rules[cid], rules.b) end
|
|
|
|
if (c == 1) then table.insert(output_rules[cid], rules.c) end
|
|
|
|
if (d == 1) then table.insert(output_rules[cid], rules.d) end
|
|
|
|
|
|
|
|
if (a == 0) then table.insert(input_rules[cid], rules.a) end
|
|
|
|
if (b == 0) then table.insert(input_rules[cid], rules.b) end
|
|
|
|
if (c == 0) then table.insert(input_rules[cid], rules.c) end
|
|
|
|
if (d == 0) then table.insert(input_rules[cid], rules.d) end
|
|
|
|
|
|
|
|
local mesecons = {
|
|
|
|
effector =
|
|
|
|
{
|
|
|
|
rules = input_rules[cid],
|
2013-01-19 22:18:28 +01:00
|
|
|
action_change = function (pos, _, rulename, newstate)
|
|
|
|
lc_update(pos, {type=newstate, pin=rulename})
|
2013-01-13 17:33:16 +01:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
receptor =
|
|
|
|
{
|
2013-01-13 00:18:25 +01:00
|
|
|
state = mesecon.state.on,
|
2013-01-13 17:33:16 +01:00
|
|
|
rules = output_rules[cid]
|
2013-01-13 00:18:25 +01:00
|
|
|
}
|
2013-01-13 17:33:16 +01:00
|
|
|
}
|
2013-01-13 00:18:25 +01:00
|
|
|
|
|
|
|
minetest.register_node(nodename, {
|
|
|
|
description = "Luacontroller",
|
|
|
|
drawtype = "nodebox",
|
|
|
|
tiles = {
|
|
|
|
top,
|
|
|
|
"jeija_microcontroller_bottom.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png"
|
|
|
|
},
|
|
|
|
|
2013-02-19 16:58:17 +01:00
|
|
|
inventory_image = top,
|
2013-01-13 00:18:25 +01:00
|
|
|
paramtype = "light",
|
|
|
|
groups = groups,
|
|
|
|
drop = BASENAME.."0000",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
selection_box = selectionbox,
|
|
|
|
node_box = nodebox,
|
|
|
|
on_construct = reset_meta,
|
|
|
|
on_receive_fields = function(pos, formname, fields)
|
|
|
|
reset(pos)
|
|
|
|
reset_meta(pos, fields.code)
|
2013-01-13 17:33:16 +01:00
|
|
|
local err = lc_update(pos, {type="program"})
|
2013-01-13 00:18:25 +01:00
|
|
|
if err then print(err) end
|
|
|
|
reset_meta(pos, fields.code, err)
|
|
|
|
end,
|
2013-03-07 02:51:57 +01:00
|
|
|
sounds = default.node_sound_stone_defaults(),
|
2013-01-13 00:18:25 +01:00
|
|
|
mesecons = mesecons,
|
2013-01-19 21:45:39 +01:00
|
|
|
digiline = digiline,
|
2013-01-13 17:33:16 +01:00
|
|
|
is_luacontroller = true,
|
2013-01-13 00:18:25 +01:00
|
|
|
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
|
|
|
|
after_dig_node = function (pos, node)
|
|
|
|
mesecon:receptor_off(pos, output_rules)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-16 03:36:16 +02:00
|
|
|
--overheated luacontroller
|
|
|
|
minetest.register_node(BASENAME .. "_burnt", {
|
|
|
|
drawtype = "nodebox",
|
|
|
|
tiles = {
|
|
|
|
"jeija_luacontroller_burnt_top.png",
|
|
|
|
"jeija_microcontroller_bottom.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png"
|
|
|
|
},
|
|
|
|
inventory_image = "jeija_luacontroller_burnt_top.png",
|
|
|
|
paramtype = "light",
|
|
|
|
groups = {dig_immediate=2, not_in_creative_inventory=1},
|
|
|
|
drop = BASENAME.."0000",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
selection_box = selectionbox,
|
|
|
|
node_box = nodebox,
|
|
|
|
on_construct = reset_meta,
|
|
|
|
on_receive_fields = function(pos, formname, fields)
|
|
|
|
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)
|
|
|
|
end,
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
is_luacontroller = true,
|
|
|
|
virtual_portstates = {a = false, b = false, c = false, d = false},
|
|
|
|
})
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
------------------------
|
|
|
|
-- Craft Registration --
|
|
|
|
------------------------
|
|
|
|
|
2013-01-13 00:18:25 +01:00
|
|
|
minetest.register_craft({
|
|
|
|
output = BASENAME.."0000 2",
|
|
|
|
recipe = {
|
|
|
|
{'mesecons_materials:silicon', 'mesecons_materials:silicon', 'group:mesecon_conductor_craftable'},
|
|
|
|
{'mesecons_materials:silicon', 'mesecons_materials:silicon', 'group:mesecon_conductor_craftable'},
|
|
|
|
{'group:mesecon_conductor_craftable', 'group:mesecon_conductor_craftable', ''},
|
|
|
|
}
|
|
|
|
})
|
2013-01-13 11:05:04 +01:00
|
|
|
|