2014-08-07 04:54:17 +02:00
|
|
|
-- ______
|
|
|
|
-- |
|
|
|
|
-- |
|
|
|
|
-- | __ ___ _ __ _ _
|
|
|
|
-- | | | | | |\ | | |_| | | | | |_ |_|
|
|
|
|
-- |___| |______ |__| | \| | | \ |__| |_ |_ |_ |\
|
|
|
|
-- |
|
|
|
|
-- |
|
|
|
|
--
|
|
|
|
|
2013-01-13 00:18:25 +01:00
|
|
|
-- Reference
|
2014-08-07 04:54:17 +02:00
|
|
|
-- ports = get_real_port_states(pos): gets if inputs are powered from outside
|
|
|
|
-- newport = merge_port_states(state1, state2): just does result = state1 or state2 for every port
|
|
|
|
-- set_port(pos, rule, state): activates/deactivates the mesecons according to the port states
|
|
|
|
-- set_port_states(pos, ports): Applies new port states to a LuaController at pos
|
|
|
|
-- run(pos): runs the code in the controller at pos
|
|
|
|
-- reset_meta(pos, code, errmsg): performs a software-reset, installs new code and prints error messages
|
|
|
|
-- resetn(pos): performs a hardware reset, turns off all ports
|
2013-01-13 00:18:25 +01:00
|
|
|
--
|
|
|
|
-- 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"
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local rules = {
|
|
|
|
a = {x = -1, y = 0, z = 0, name="A"},
|
|
|
|
b = {x = 0, y = 0, z = 1, name="B"},
|
|
|
|
c = {x = 1, y = 0, z = 0, name="C"},
|
|
|
|
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 --
|
|
|
|
------------------
|
2014-08-07 04:54:17 +02:00
|
|
|
-- These helpers are required to set the port states of the luacontroller
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function update_real_port_states(pos, rule_name, new_state)
|
2014-01-10 16:38:02 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2014-08-07 04:54:17 +02:00
|
|
|
if rule_name == nil then
|
2014-01-10 16:38:02 +01:00
|
|
|
meta:set_int("real_portstates", 1)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local n = meta:get_int("real_portstates") - 1
|
|
|
|
local L = {}
|
|
|
|
for i = 1, 4 do
|
2014-08-07 04:54:17 +02:00
|
|
|
L[i] = n % 2
|
|
|
|
n = math.floor(n / 2)
|
2014-01-10 16:38:02 +01:00
|
|
|
end
|
2014-08-07 04:54:17 +02:00
|
|
|
-- (0,-1) (-1,0) (1,0) (0,1)
|
|
|
|
local pos_to_side = { 4, 1, nil, 3, 2 }
|
|
|
|
if rule_name.x == nil then
|
|
|
|
for _, rname in ipairs(rule_name) do
|
|
|
|
local port = pos_to_side[rname.x + (2 * rname.z) + 3]
|
2014-01-10 16:38:02 +01:00
|
|
|
L[port] = (newstate == "on") and 1 or 0
|
|
|
|
end
|
|
|
|
else
|
2014-08-07 04:54:17 +02:00
|
|
|
local port = pos_to_side[rule_name.x + (2 * rule_name.z) + 3]
|
|
|
|
L[port] = (new_state == "on") and 1 or 0
|
2014-01-10 16:38:02 +01:00
|
|
|
end
|
2014-08-07 04:54:17 +02:00
|
|
|
meta:set_int("real_portstates",
|
|
|
|
1 +
|
|
|
|
1 * L[1] +
|
|
|
|
2 * L[2] +
|
|
|
|
4 * L[3] +
|
|
|
|
8 * L[4])
|
2014-01-10 16:38:02 +01:00
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local port_names = {"a", "b", "c", "d"}
|
|
|
|
|
|
|
|
local function get_real_port_states(pos)
|
|
|
|
-- Determine if ports are powered (by itself or from outside)
|
2014-01-10 16:38:02 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local L = {}
|
|
|
|
local n = meta:get_int("real_portstates") - 1
|
2014-08-07 04:54:17 +02:00
|
|
|
for _, name in ipairs(port_names) do
|
|
|
|
L[name] = ((n % 2) == 1)
|
|
|
|
n = math.floor(n / 2)
|
2014-01-10 16:38:02 +01:00
|
|
|
end
|
|
|
|
return L
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local function merge_port_states(ports, vports)
|
|
|
|
return {
|
|
|
|
a = ports.a or vports.a,
|
|
|
|
b = ports.b or vports.b,
|
|
|
|
c = ports.c or vports.c,
|
|
|
|
d = ports.d or vports.d,
|
|
|
|
}
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function generate_name(ports)
|
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
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local function set_port(pos, rule, state)
|
2013-01-22 18:26:27 +01:00
|
|
|
if state then
|
2014-11-22 15:42:22 +01:00
|
|
|
mesecon.receptor_on(pos, {rule})
|
2013-01-22 18:26:27 +01:00
|
|
|
else
|
2014-11-22 15:42:22 +01:00
|
|
|
mesecon.receptor_off(pos, {rule})
|
2013-01-22 18:26:27 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local function clean_port_states(ports)
|
|
|
|
ports.a = ports.a and true or false
|
|
|
|
ports.b = ports.b and true or false
|
|
|
|
ports.c = ports.c and true or false
|
|
|
|
ports.d = ports.d and true or false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function set_port_states(pos, ports)
|
2013-12-01 02:20:01 +01:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local name = node.name
|
2014-08-07 04:54:17 +02:00
|
|
|
clean_port_states(ports)
|
2013-01-13 17:33:16 +01:00
|
|
|
local vports = minetest.registered_nodes[name].virtual_portstates
|
2014-08-07 04:54:17 +02:00
|
|
|
local new_name = generate_name(ports)
|
2013-01-22 18:26:27 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
if name ~= new_name and vports then
|
2014-11-29 10:56:09 +01:00
|
|
|
-- Problem:
|
|
|
|
-- We need to place the new node first so that when turning
|
|
|
|
-- off some port, it won't stay on because the rules indicate
|
|
|
|
-- there is an onstate output port there.
|
|
|
|
-- When turning the output off then, it will however cause feedback
|
|
|
|
-- so that the luacontroller will receive an "off" event by turning
|
|
|
|
-- its output off.
|
|
|
|
-- Solution / Workaround:
|
|
|
|
-- Remember which output was turned off and ignore next "off" event.
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local ign = minetest.deserialize(meta:get_string("ignore_offevents")) or {}
|
|
|
|
if ports.a and not vports.a and not mesecon.is_powered(pos, rules.a) then ign.A = true end
|
|
|
|
if ports.b and not vports.b and not mesecon.is_powered(pos, rules.b) then ign.B = true end
|
|
|
|
if ports.c and not vports.c and not mesecon.is_powered(pos, rules.c) then ign.C = true end
|
|
|
|
if ports.d and not vports.d and not mesecon.is_powered(pos, rules.d) then ign.D = true end
|
|
|
|
meta:set_string("ignore_offevents", minetest.serialize(ign))
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
minetest.swap_node(pos, {name = new_name, param2 = node.param2})
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
if ports.a ~= vports.a then set_port(pos, rules.a, ports.a) end
|
|
|
|
if ports.b ~= vports.b then set_port(pos, rules.b, ports.b) end
|
|
|
|
if ports.c ~= vports.c then set_port(pos, rules.c, ports.c) end
|
|
|
|
if ports.d ~= vports.d then set_port(pos, rules.d, ports.d) end
|
2013-04-01 22:24:01 +02:00
|
|
|
end
|
2013-02-10 23:08:59 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
-----------------
|
|
|
|
-- Overheating --
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
local function overheat_off(pos)
|
2014-11-22 15:42:22 +01:00
|
|
|
mesecon.receptor_off(pos, mesecon.rules.flat)
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function overheat(pos, meta)
|
|
|
|
if mesecon.do_overheat(pos) then -- If too hot
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
node.name = BASENAME.."_burnt"
|
|
|
|
minetest.swap_node(pos, node)
|
|
|
|
-- Wait for pending operations
|
|
|
|
minetest.after(0.2, overheat_off, pos)
|
|
|
|
return true
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
|
|
|
|
2014-11-29 10:56:09 +01:00
|
|
|
------------------------
|
|
|
|
-- Ignored off events --
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
local function ignore_event(event, meta)
|
|
|
|
if event.type ~= "off" then return false end
|
|
|
|
local ignore_offevents = minetest.deserialize(meta:get_string("ignore_offevents")) or {}
|
|
|
|
if ignore_offevents[event.pin.name] then
|
|
|
|
ignore_offevents[event.pin.name] = nil
|
|
|
|
meta:set_string("ignore_offevents", minetest.serialize(ignore_offevents))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
-------------------------
|
|
|
|
-- Parsing and running --
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
local function safe_print(param)
|
2013-01-13 11:05:04 +01:00
|
|
|
print(dump(param))
|
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function remove_functions(x)
|
|
|
|
local tp = type(x)
|
|
|
|
if tp == "table" then
|
|
|
|
for key, value in pairs(x) do
|
|
|
|
local key_t, val_t = type(key), type(value)
|
|
|
|
if key_t == "function" or val_t == "function" then
|
|
|
|
x[key] = nil
|
|
|
|
else
|
|
|
|
if key_t == "table" then
|
|
|
|
remove_functions(key)
|
|
|
|
end
|
|
|
|
if val_t == "table" then
|
|
|
|
remove_functions(value)
|
|
|
|
end
|
|
|
|
end
|
2013-06-06 22:38:40 +02:00
|
|
|
end
|
2014-08-07 04:54:17 +02:00
|
|
|
elseif tp == "function" then
|
2013-06-06 22:38:40 +02:00
|
|
|
return nil
|
|
|
|
end
|
2014-08-07 04:54:17 +02:00
|
|
|
return x
|
2013-06-06 22:38:40 +02:00
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function get_interrupt(pos)
|
|
|
|
-- iid = interrupt id
|
|
|
|
local function interrupt(time, iid)
|
2013-01-14 17:58:14 +01:00
|
|
|
if type(time) ~= "number" then return end
|
2014-11-23 10:59:51 +01:00
|
|
|
local luac_id = minetest.get_meta(pos):get_int("luac_id")
|
|
|
|
mesecon.queue:add_action(pos, "lc_interrupt", {luac_id, iid}, time, iid, 1)
|
2013-01-13 17:33:16 +01:00
|
|
|
end
|
|
|
|
return interrupt
|
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local function get_digiline_send(pos)
|
2014-03-19 09:09:39 +01:00
|
|
|
if not digiline then return end
|
|
|
|
return function(channel, msg)
|
|
|
|
minetest.after(0, function()
|
|
|
|
digiline:receptor_send(pos, digiline.rules.default, channel, msg)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local safe_globals = {
|
|
|
|
"assert", "error", "ipairs", "next", "pairs", "pcall", "select",
|
|
|
|
"tonumber", "tostring", "type", "unpack", "_VERSION", "xpcall",
|
|
|
|
}
|
|
|
|
local function create_environment(pos, mem, event)
|
2013-01-13 00:18:25 +01:00
|
|
|
-- Gather variables for the environment
|
2013-12-01 04:13:00 +01:00
|
|
|
local vports = minetest.registered_nodes[minetest.get_node(pos).name].virtual_portstates
|
2014-08-07 04:54:17 +02:00
|
|
|
local vports_copy = {}
|
|
|
|
for k, v in pairs(vports) do vports_copy[k] = v end
|
|
|
|
local rports = get_real_port_states(pos)
|
|
|
|
|
|
|
|
-- Create new library tables on each call to prevent one LuaController
|
|
|
|
-- from breaking a library and messing up other LuaControllers.
|
|
|
|
local env = {
|
|
|
|
pin = merge_port_states(vports, rports),
|
|
|
|
port = vports_copy,
|
|
|
|
event = event,
|
|
|
|
mem = mem,
|
|
|
|
heat = minetest.get_meta(pos):get_int("heat"),
|
2014-11-22 23:04:34 +01:00
|
|
|
heat_max = mesecon.setting("overheat_max", 20),
|
2014-08-07 04:54:17 +02:00
|
|
|
print = safe_print,
|
|
|
|
interrupt = get_interrupt(pos),
|
|
|
|
digiline_send = get_digiline_send(pos),
|
|
|
|
string = {
|
|
|
|
byte = string.byte,
|
|
|
|
char = string.char,
|
|
|
|
format = string.format,
|
|
|
|
gsub = string.gsub,
|
|
|
|
len = string.len,
|
|
|
|
lower = string.lower,
|
|
|
|
upper = string.upper,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
table = {
|
|
|
|
concat = table.concat,
|
|
|
|
insert = table.insert,
|
|
|
|
maxn = table.maxn,
|
|
|
|
remove = table.remove,
|
|
|
|
sort = table.sort,
|
|
|
|
},
|
|
|
|
os = {
|
|
|
|
clock = os.clock,
|
|
|
|
difftime = os.difftime,
|
|
|
|
time = os.time,
|
|
|
|
},
|
2013-03-23 23:41:44 +01:00
|
|
|
}
|
2014-08-07 04:54:17 +02:00
|
|
|
env._G = env
|
|
|
|
|
|
|
|
for _, name in pairs(safe_globals) do
|
|
|
|
env[name] = _G[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
return env
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local function timeout()
|
|
|
|
debug.sethook() -- Clear hook
|
|
|
|
error("Code timed out!")
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function code_prohibited(code)
|
|
|
|
-- LuaJIT doesn't increment the instruction counter when running
|
|
|
|
-- loops, so we have to sanitize inputs if we're using LuaJIT.
|
|
|
|
if not jit then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local prohibited = {"while", "for", "do", "repeat", "until", "goto"}
|
|
|
|
code = " "..code.." "
|
|
|
|
for _, p in ipairs(prohibited) do
|
|
|
|
if string.find(code, "[^%w_]"..p.."[^%w_]") then
|
|
|
|
return "Prohibited command: "..p
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function create_sandbox(code, env)
|
2013-01-13 00:18:25 +01:00
|
|
|
if code:byte(1) == 27 then
|
2014-08-07 04:54:17 +02:00
|
|
|
return nil, "Binary code prohibited."
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
2014-11-22 14:47:18 +01:00
|
|
|
local f, msg = loadstring(code)
|
2014-08-07 04:54:17 +02:00
|
|
|
if not f then return nil, msg end
|
2013-01-13 00:18:25 +01:00
|
|
|
setfenv(f, env)
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
return function(...)
|
|
|
|
debug.sethook(timeout, "", 10000)
|
|
|
|
local ok, ret = pcall(f, ...)
|
|
|
|
debug.sethook() -- Clear hook
|
|
|
|
if not ok then error(ret) end
|
|
|
|
return ret
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
2013-01-13 11:05:04 +01:00
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
|
|
|
|
local function load_memory(meta)
|
2013-01-13 11:05:04 +01:00
|
|
|
return minetest.deserialize(meta:get_string("lc_memory")) or {}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function save_memory(meta, mem)
|
|
|
|
meta:set_string("lc_memory",
|
|
|
|
minetest.serialize(
|
|
|
|
remove_functions(mem)
|
|
|
|
)
|
|
|
|
)
|
2013-01-14 17:58:14 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function run(pos, event)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2014-08-07 04:54:17 +02:00
|
|
|
if overheat(pos) then return end
|
2014-11-29 10:56:09 +01:00
|
|
|
if ignore_event(event, meta) then return end
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
-- Load code & mem from meta
|
2013-01-13 11:05:04 +01:00
|
|
|
local mem = load_memory(meta)
|
|
|
|
local code = meta:get_string("code")
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local err = code_prohibited(code)
|
|
|
|
if err then return err end
|
|
|
|
|
|
|
|
-- Create environment
|
2013-01-13 17:33:16 +01:00
|
|
|
local env = create_environment(pos, mem, event)
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
-- Create the sandbox and execute code
|
|
|
|
local f, msg = create_sandbox(code, env)
|
|
|
|
if not f 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
|
2014-08-07 04:54:17 +02:00
|
|
|
if type(env.port) ~= "table" then
|
|
|
|
return "Ports set are invalid."
|
|
|
|
end
|
2013-01-13 11:05:04 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
save_memory(meta, env.mem)
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-03-19 09:09:39 +01:00
|
|
|
-- Actually set the ports
|
2014-08-07 04:54:17 +02:00
|
|
|
set_port_states(pos, env.port)
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2014-11-25 17:20:05 +01:00
|
|
|
mesecon.queue:add_function("lc_interrupt", function (pos, luac_id, iid)
|
|
|
|
-- There is no luacontroller anymore / it has been reprogrammed / replaced
|
|
|
|
if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
|
|
|
|
run(pos, {type="interrupt", iid = iid})
|
|
|
|
end)
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function reset_meta(pos, code, errmsg)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2013-03-15 22:46:59 +01:00
|
|
|
meta:set_string("code", code)
|
2013-12-01 04:13:00 +01:00
|
|
|
code = minetest.formspec_escape(code or "")
|
|
|
|
errmsg = minetest.formspec_escape(errmsg or "")
|
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)
|
2014-08-07 04:54:17 +02:00
|
|
|
meta:set_int("luac_id", math.random(1, 65535))
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local function reset(pos)
|
|
|
|
set_port_states(pos, {a=false, b=false, c=false, d=false})
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
|
2013-01-13 11:05:04 +01:00
|
|
|
|
|
|
|
-----------------------
|
|
|
|
-- Node Registration --
|
|
|
|
-----------------------
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local output_rules = {}
|
|
|
|
local input_rules = {}
|
2013-01-13 17:33:16 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local node_box = {
|
|
|
|
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
|
2013-01-19 21:45:39 +01:00
|
|
|
}
|
2014-08-07 04:54:17 +02:00
|
|
|
}
|
2013-01-19 21:45:39 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local selection_box = {
|
|
|
|
type = "fixed",
|
|
|
|
fixed = { -8/16, -8/16, -8/16, 8/16, -5/16, 8/16 },
|
|
|
|
}
|
2013-01-19 21:45:39 +01:00
|
|
|
|
|
|
|
local digiline = {
|
|
|
|
receptor = {},
|
|
|
|
effector = {
|
2014-08-07 04:54:17 +02:00
|
|
|
action = function(pos, node, channel, msg)
|
|
|
|
run(pos, {type = "digiline", channel = channel, msg = msg})
|
2013-01-19 21:45:39 +01:00
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
2014-08-07 04:54:17 +02:00
|
|
|
local function on_receive_fields(pos, form_name, fields)
|
|
|
|
if not fields.program then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
reset(pos)
|
|
|
|
reset_meta(pos, fields.code)
|
|
|
|
local err = run(pos, {type="program"})
|
|
|
|
if err then
|
|
|
|
print(err)
|
|
|
|
reset_meta(pos, fields.code, err)
|
|
|
|
end
|
|
|
|
end
|
2013-01-19 21:45:39 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
for a = 0, 1 do -- 0 = off 1 = on
|
2013-04-01 22:24:01 +02:00
|
|
|
for b = 0, 1 do
|
|
|
|
for c = 0, 1 do
|
|
|
|
for d = 0, 1 do
|
2014-08-07 04:54:17 +02:00
|
|
|
local cid = tostring(d)..tostring(c)..tostring(b)..tostring(a)
|
|
|
|
local node_name = BASENAME..cid
|
|
|
|
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
|
2013-01-20 17:48:43 +01:00
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
local groups
|
|
|
|
if a + b + c + d ~= 0 then
|
|
|
|
groups = {dig_immediate=2, not_in_creative_inventory=1, overheat = 1}
|
|
|
|
else
|
|
|
|
groups = {dig_immediate=2, overheat = 1}
|
|
|
|
end
|
2013-01-13 00:18:25 +01:00
|
|
|
|
2014-08-07 04:54:17 +02: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],
|
|
|
|
action_change = function (pos, _, rule_name, new_state)
|
|
|
|
update_real_port_states(pos, rule_name, new_state)
|
2014-11-29 10:56:09 +01:00
|
|
|
run(pos, {type=new_state, pin=rule_name})
|
2014-08-07 04:54:17 +02:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
receptor = {
|
|
|
|
state = mesecon.state.on,
|
|
|
|
rules = output_rules[cid]
|
|
|
|
}
|
2013-01-13 00:18:25 +01:00
|
|
|
}
|
|
|
|
|
2014-08-07 04:54:17 +02:00
|
|
|
minetest.register_node(node_name, {
|
|
|
|
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-01-13 00:18:25 +01:00
|
|
|
},
|
2014-08-07 04:54:17 +02:00
|
|
|
inventory_image = top,
|
|
|
|
paramtype = "light",
|
|
|
|
groups = groups,
|
|
|
|
drop = BASENAME.."0000",
|
|
|
|
sunlight_propagates = true,
|
|
|
|
selection_box = selection_box,
|
|
|
|
node_box = node_box,
|
|
|
|
on_construct = reset_meta,
|
|
|
|
on_receive_fields = on_receive_fields,
|
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
mesecons = mesecons,
|
|
|
|
digiline = digiline,
|
|
|
|
-- Virtual portstates are the ports that
|
|
|
|
-- the node shows as powered up (light up).
|
|
|
|
virtual_portstates = {
|
|
|
|
a = a == 1,
|
|
|
|
b = b == 1,
|
|
|
|
c = c == 1,
|
|
|
|
d = d == 1,
|
|
|
|
},
|
|
|
|
after_dig_node = function (pos, node)
|
2014-11-22 23:04:34 +01:00
|
|
|
mesecon.receptor_off(pos, output_rules)
|
2014-08-07 04:54:17 +02:00
|
|
|
end,
|
|
|
|
is_luacontroller = true,
|
|
|
|
})
|
2013-01-13 00:18:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-20 21:44:58 +02:00
|
|
|
------------------------------
|
2014-08-07 04:54:17 +02:00
|
|
|
-- Overheated LuaController --
|
2014-04-20 21:44:58 +02:00
|
|
|
------------------------------
|
|
|
|
|
2013-05-16 03:36:16 +02:00
|
|
|
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,
|
2014-12-20 10:11:12 +01:00
|
|
|
selection_box = selection_box,
|
2014-11-22 23:14:45 +01:00
|
|
|
node_box = node_box,
|
2013-05-16 03:36:16 +02:00
|
|
|
on_construct = reset_meta,
|
2014-08-07 04:54:17 +02:00
|
|
|
on_receive_fields = on_receive_fields,
|
2013-05-16 03:36:16 +02:00
|
|
|
sounds = default.node_sound_stone_defaults(),
|
|
|
|
virtual_portstates = {a = false, b = false, c = false, d = false},
|
2014-08-07 04:54:17 +02:00
|
|
|
mesecons = {
|
|
|
|
effector = {
|
|
|
|
rules = mesecon.rules.flat,
|
|
|
|
action_change = function(pos, _, rule_name, new_state)
|
2014-11-22 23:14:45 +01:00
|
|
|
update_real_port_states(pos, rule_name, new_state)
|
2014-08-07 04:54:17 +02:00
|
|
|
end,
|
|
|
|
},
|
|
|
|
},
|
2013-05-16 03:36:16 +02:00
|
|
|
})
|
|
|
|
|
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
|
|
|
|