init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. -- ______
  2. -- |
  3. -- |
  4. -- | __ ___ _ __ _ _
  5. -- | | | | | |\ | | |_| | | | | |_ |_|
  6. -- |___| |______ |__| | \| | | \ |__| |_ |_ |_ |\
  7. -- |
  8. -- |
  9. --
  10. -- Reference
  11. -- ports = get_real_port_states(pos): gets if inputs are powered from outside
  12. -- newport = merge_port_states(state1, state2): just does result = state1 or state2 for every port
  13. -- set_port(pos, rule, state): activates/deactivates the mesecons according to the port states
  14. -- set_port_states(pos, ports): Applies new port states to a LuaController at pos
  15. -- run(pos): runs the code in the controller at pos
  16. -- reset_meta(pos, code, errmsg): performs a software-reset, installs new code and prints error messages
  17. -- resetn(pos): performs a hardware reset, turns off all ports
  18. --
  19. -- The Sandbox
  20. -- The whole code of the controller runs in a sandbox,
  21. -- a very restricted environment.
  22. -- However, as this does not prevent you from using e.g. loops,
  23. -- we need to check for these prohibited commands first.
  24. -- Actually the only way to damage the server is to
  25. -- use too much memory from the sandbox.
  26. -- You can add more functions to the environment
  27. -- (see where local env is defined)
  28. -- Something nice to play is is appending minetest.env to it.
  29. local BASENAME = "mesecons_luacontroller:luacontroller"
  30. local rules = {
  31. a = {x = -1, y = 0, z = 0, name="A"},
  32. b = {x = 0, y = 0, z = 1, name="B"},
  33. c = {x = 1, y = 0, z = 0, name="C"},
  34. d = {x = 0, y = 0, z = -1, name="D"},
  35. }
  36. ------------------
  37. -- Action stuff --
  38. ------------------
  39. -- These helpers are required to set the port states of the luacontroller
  40. local function update_real_port_states(pos, rule_name, new_state)
  41. local meta = minetest.get_meta(pos)
  42. if rule_name == nil then
  43. meta:set_int("real_portstates", 1)
  44. return
  45. end
  46. local n = meta:get_int("real_portstates") - 1
  47. local L = {}
  48. for i = 1, 4 do
  49. L[i] = n % 2
  50. n = math.floor(n / 2)
  51. end
  52. -- (0,-1) (-1,0) (1,0) (0,1)
  53. local pos_to_side = { 4, 1, nil, 3, 2 }
  54. if rule_name.x == nil then
  55. for _, rname in ipairs(rule_name) do
  56. local port = pos_to_side[rname.x + (2 * rname.z) + 3]
  57. L[port] = (newstate == "on") and 1 or 0
  58. end
  59. else
  60. local port = pos_to_side[rule_name.x + (2 * rule_name.z) + 3]
  61. L[port] = (new_state == "on") and 1 or 0
  62. end
  63. meta:set_int("real_portstates",
  64. 1 +
  65. 1 * L[1] +
  66. 2 * L[2] +
  67. 4 * L[3] +
  68. 8 * L[4])
  69. end
  70. local port_names = {"a", "b", "c", "d"}
  71. local function get_real_port_states(pos)
  72. -- Determine if ports are powered (by itself or from outside)
  73. local meta = minetest.get_meta(pos)
  74. local L = {}
  75. local n = meta:get_int("real_portstates") - 1
  76. for _, name in ipairs(port_names) do
  77. L[name] = ((n % 2) == 1)
  78. n = math.floor(n / 2)
  79. end
  80. return L
  81. end
  82. local function merge_port_states(ports, vports)
  83. return {
  84. a = ports.a or vports.a,
  85. b = ports.b or vports.b,
  86. c = ports.c or vports.c,
  87. d = ports.d or vports.d,
  88. }
  89. end
  90. local function generate_name(ports)
  91. local d = ports.d and 1 or 0
  92. local c = ports.c and 1 or 0
  93. local b = ports.b and 1 or 0
  94. local a = ports.a and 1 or 0
  95. return BASENAME..d..c..b..a
  96. end
  97. local function set_port(pos, rule, state)
  98. if state then
  99. mesecon.receptor_on(pos, {rule})
  100. else
  101. mesecon.receptor_off(pos, {rule})
  102. end
  103. end
  104. local function clean_port_states(ports)
  105. ports.a = ports.a and true or false
  106. ports.b = ports.b and true or false
  107. ports.c = ports.c and true or false
  108. ports.d = ports.d and true or false
  109. end
  110. local function set_port_states(pos, ports)
  111. local node = minetest.get_node(pos)
  112. local name = node.name
  113. clean_port_states(ports)
  114. local vports = minetest.registered_nodes[name].virtual_portstates
  115. local new_name = generate_name(ports)
  116. if name ~= new_name and vports then
  117. -- Problem:
  118. -- We need to place the new node first so that when turning
  119. -- off some port, it won't stay on because the rules indicate
  120. -- there is an onstate output port there.
  121. -- When turning the output off then, it will however cause feedback
  122. -- so that the luacontroller will receive an "off" event by turning
  123. -- its output off.
  124. -- Solution / Workaround:
  125. -- Remember which output was turned off and ignore next "off" event.
  126. local meta = minetest.get_meta(pos)
  127. local ign = minetest.deserialize(meta:get_string("ignore_offevents")) or {}
  128. if ports.a and not vports.a and not mesecon.is_powered(pos, rules.a) then ign.A = true end
  129. if ports.b and not vports.b and not mesecon.is_powered(pos, rules.b) then ign.B = true end
  130. if ports.c and not vports.c and not mesecon.is_powered(pos, rules.c) then ign.C = true end
  131. if ports.d and not vports.d and not mesecon.is_powered(pos, rules.d) then ign.D = true end
  132. meta:set_string("ignore_offevents", minetest.serialize(ign))
  133. minetest.swap_node(pos, {name = new_name, param2 = node.param2})
  134. if ports.a ~= vports.a then set_port(pos, rules.a, ports.a) end
  135. if ports.b ~= vports.b then set_port(pos, rules.b, ports.b) end
  136. if ports.c ~= vports.c then set_port(pos, rules.c, ports.c) end
  137. if ports.d ~= vports.d then set_port(pos, rules.d, ports.d) end
  138. end
  139. end
  140. -----------------
  141. -- Overheating --
  142. -----------------
  143. local function overheat_off(pos)
  144. mesecon.receptor_off(pos, mesecon.rules.flat)
  145. end
  146. local function overheat(pos, meta)
  147. if mesecon.do_overheat(pos) then -- If too hot
  148. local node = minetest.get_node(pos)
  149. node.name = BASENAME.."_burnt"
  150. minetest.swap_node(pos, node)
  151. -- Wait for pending operations
  152. minetest.after(0.2, overheat_off, pos)
  153. return true
  154. end
  155. end
  156. ------------------------
  157. -- Ignored off events --
  158. ------------------------
  159. local function ignore_event(event, meta)
  160. if event.type ~= "off" then return false end
  161. local ignore_offevents = minetest.deserialize(meta:get_string("ignore_offevents")) or {}
  162. if ignore_offevents[event.pin.name] then
  163. ignore_offevents[event.pin.name] = nil
  164. meta:set_string("ignore_offevents", minetest.serialize(ignore_offevents))
  165. return true
  166. end
  167. end
  168. -------------------------
  169. -- Parsing and running --
  170. -------------------------
  171. local function safe_print(param)
  172. print(dump(param))
  173. end
  174. local function remove_functions(x)
  175. local tp = type(x)
  176. if tp == "table" then
  177. for key, value in pairs(x) do
  178. local key_t, val_t = type(key), type(value)
  179. if key_t == "function" or val_t == "function" then
  180. x[key] = nil
  181. else
  182. if key_t == "table" then
  183. remove_functions(key)
  184. end
  185. if val_t == "table" then
  186. remove_functions(value)
  187. end
  188. end
  189. end
  190. elseif tp == "function" then
  191. return nil
  192. end
  193. return x
  194. end
  195. local function get_interrupt(pos)
  196. -- iid = interrupt id
  197. local function interrupt(time, iid)
  198. if type(time) ~= "number" then return end
  199. local luac_id = minetest.get_meta(pos):get_int("luac_id")
  200. mesecon.queue:add_action(pos, "lc_interrupt", {luac_id, iid}, time, iid, 1)
  201. end
  202. return interrupt
  203. end
  204. local function get_digiline_send(pos)
  205. if not digiline then return end
  206. return function(channel, msg)
  207. minetest.after(0, function()
  208. digiline:receptor_send(pos, digiline.rules.default, channel, msg)
  209. end)
  210. end
  211. end
  212. local safe_globals = {
  213. "assert", "error", "ipairs", "next", "pairs", "pcall", "select",
  214. "tonumber", "tostring", "type", "unpack", "_VERSION", "xpcall",
  215. }
  216. local function create_environment(pos, mem, event)
  217. -- Gather variables for the environment
  218. local vports = minetest.registered_nodes[minetest.get_node(pos).name].virtual_portstates
  219. local vports_copy = {}
  220. for k, v in pairs(vports) do vports_copy[k] = v end
  221. local rports = get_real_port_states(pos)
  222. -- Create new library tables on each call to prevent one LuaController
  223. -- from breaking a library and messing up other LuaControllers.
  224. local env = {
  225. pin = merge_port_states(vports, rports),
  226. port = vports_copy,
  227. event = event,
  228. mem = mem,
  229. heat = minetest.get_meta(pos):get_int("heat"),
  230. heat_max = mesecon.setting("overheat_max", 20),
  231. print = safe_print,
  232. interrupt = get_interrupt(pos),
  233. digiline_send = get_digiline_send(pos),
  234. string = {
  235. byte = string.byte,
  236. char = string.char,
  237. format = string.format,
  238. gsub = string.gsub,
  239. len = string.len,
  240. lower = string.lower,
  241. upper = string.upper,
  242. rep = string.rep,
  243. reverse = string.reverse,
  244. sub = string.sub,
  245. },
  246. math = {
  247. abs = math.abs,
  248. acos = math.acos,
  249. asin = math.asin,
  250. atan = math.atan,
  251. atan2 = math.atan2,
  252. ceil = math.ceil,
  253. cos = math.cos,
  254. cosh = math.cosh,
  255. deg = math.deg,
  256. exp = math.exp,
  257. floor = math.floor,
  258. fmod = math.fmod,
  259. frexp = math.frexp,
  260. huge = math.huge,
  261. ldexp = math.ldexp,
  262. log = math.log,
  263. log10 = math.log10,
  264. max = math.max,
  265. min = math.min,
  266. modf = math.modf,
  267. pi = math.pi,
  268. pow = math.pow,
  269. rad = math.rad,
  270. random = math.random,
  271. sin = math.sin,
  272. sinh = math.sinh,
  273. sqrt = math.sqrt,
  274. tan = math.tan,
  275. tanh = math.tanh,
  276. },
  277. table = {
  278. concat = table.concat,
  279. insert = table.insert,
  280. maxn = table.maxn,
  281. remove = table.remove,
  282. sort = table.sort,
  283. },
  284. os = {
  285. clock = os.clock,
  286. difftime = os.difftime,
  287. time = os.time,
  288. },
  289. }
  290. env._G = env
  291. for _, name in pairs(safe_globals) do
  292. env[name] = _G[name]
  293. end
  294. return env
  295. end
  296. local function timeout()
  297. debug.sethook() -- Clear hook
  298. error("Code timed out!")
  299. end
  300. local function code_prohibited(code)
  301. -- LuaJIT doesn't increment the instruction counter when running
  302. -- loops, so we have to sanitize inputs if we're using LuaJIT.
  303. if not jit then
  304. return false
  305. end
  306. local prohibited = {"while", "for", "do", "repeat", "until", "goto"}
  307. code = " "..code.." "
  308. for _, p in ipairs(prohibited) do
  309. if string.find(code, "[^%w_]"..p.."[^%w_]") then
  310. return "Prohibited command: "..p
  311. end
  312. end
  313. end
  314. local function create_sandbox(code, env)
  315. if code:byte(1) == 27 then
  316. return nil, "Binary code prohibited."
  317. end
  318. local f, msg = loadstring(code)
  319. if not f then return nil, msg end
  320. setfenv(f, env)
  321. return function(...)
  322. debug.sethook(timeout, "", 10000)
  323. local ok, ret = pcall(f, ...)
  324. debug.sethook() -- Clear hook
  325. if not ok then error(ret) end
  326. return ret
  327. end
  328. end
  329. local function load_memory(meta)
  330. return minetest.deserialize(meta:get_string("lc_memory")) or {}
  331. end
  332. local function save_memory(meta, mem)
  333. meta:set_string("lc_memory",
  334. minetest.serialize(
  335. remove_functions(mem)
  336. )
  337. )
  338. end
  339. local function run(pos, event)
  340. local meta = minetest.get_meta(pos)
  341. if overheat(pos) then return end
  342. if ignore_event(event, meta) then return end
  343. -- Load code & mem from meta
  344. local mem = load_memory(meta)
  345. local code = meta:get_string("code")
  346. local err = code_prohibited(code)
  347. if err then return err end
  348. -- Create environment
  349. local env = create_environment(pos, mem, event)
  350. -- Create the sandbox and execute code
  351. local f, msg = create_sandbox(code, env)
  352. if not f then return msg end
  353. local success, msg = pcall(f)
  354. if not success then return msg end
  355. if type(env.port) ~= "table" then
  356. return "Ports set are invalid."
  357. end
  358. save_memory(meta, env.mem)
  359. -- Actually set the ports
  360. set_port_states(pos, env.port)
  361. end
  362. mesecon.queue:add_function("lc_interrupt", function (pos, luac_id, iid)
  363. -- There is no luacontroller anymore / it has been reprogrammed / replaced
  364. if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
  365. run(pos, {type="interrupt", iid = iid})
  366. end)
  367. local function reset_meta(pos, code, errmsg)
  368. local meta = minetest.get_meta(pos)
  369. meta:set_string("code", code)
  370. code = minetest.formspec_escape(code or "")
  371. errmsg = minetest.formspec_escape(errmsg or "")
  372. meta:set_string("formspec", "size[10,8]"..
  373. "background[-0.2,-0.25;10.4,8.75;jeija_luac_background.png]"..
  374. "textarea[0.2,0.6;10.2,5;code;;"..code.."]"..
  375. "image_button[3.75,6;2.5,1;jeija_luac_runbutton.png;program;]"..
  376. "image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
  377. "label[0.1,5;"..errmsg.."]")
  378. meta:set_int("heat", 0)
  379. meta:set_int("luac_id", math.random(1, 65535))
  380. end
  381. local function reset(pos)
  382. set_port_states(pos, {a=false, b=false, c=false, d=false})
  383. end
  384. -----------------------
  385. -- Node Registration --
  386. -----------------------
  387. local output_rules = {}
  388. local input_rules = {}
  389. local node_box = {
  390. type = "fixed",
  391. fixed = {
  392. {-8/16, -8/16, -8/16, 8/16, -7/16, 8/16}, -- Bottom slab
  393. {-5/16, -7/16, -5/16, 5/16, -6/16, 5/16}, -- Circuit board
  394. {-3/16, -6/16, -3/16, 3/16, -5/16, 3/16}, -- IC
  395. }
  396. }
  397. local selection_box = {
  398. type = "fixed",
  399. fixed = { -8/16, -8/16, -8/16, 8/16, -5/16, 8/16 },
  400. }
  401. local digiline = {
  402. receptor = {},
  403. effector = {
  404. action = function(pos, node, channel, msg)
  405. run(pos, {type = "digiline", channel = channel, msg = msg})
  406. end
  407. }
  408. }
  409. local function on_receive_fields(pos, form_name, fields)
  410. if not fields.program then
  411. return
  412. end
  413. reset(pos)
  414. reset_meta(pos, fields.code)
  415. local err = run(pos, {type="program"})
  416. if err then
  417. print(err)
  418. reset_meta(pos, fields.code, err)
  419. end
  420. end
  421. for a = 0, 1 do -- 0 = off 1 = on
  422. for b = 0, 1 do
  423. for c = 0, 1 do
  424. for d = 0, 1 do
  425. local cid = tostring(d)..tostring(c)..tostring(b)..tostring(a)
  426. local node_name = BASENAME..cid
  427. local top = "jeija_luacontroller_top.png"
  428. if a == 1 then
  429. top = top.."^jeija_luacontroller_LED_A.png"
  430. end
  431. if b == 1 then
  432. top = top.."^jeija_luacontroller_LED_B.png"
  433. end
  434. if c == 1 then
  435. top = top.."^jeija_luacontroller_LED_C.png"
  436. end
  437. if d == 1 then
  438. top = top.."^jeija_luacontroller_LED_D.png"
  439. end
  440. local groups
  441. if a + b + c + d ~= 0 then
  442. groups = {dig_immediate=2, not_in_creative_inventory=1, overheat = 1}
  443. else
  444. groups = {dig_immediate=2, overheat = 1}
  445. end
  446. output_rules[cid] = {}
  447. input_rules[cid] = {}
  448. if a == 1 then table.insert(output_rules[cid], rules.a) end
  449. if b == 1 then table.insert(output_rules[cid], rules.b) end
  450. if c == 1 then table.insert(output_rules[cid], rules.c) end
  451. if d == 1 then table.insert(output_rules[cid], rules.d) end
  452. if a == 0 then table.insert( input_rules[cid], rules.a) end
  453. if b == 0 then table.insert( input_rules[cid], rules.b) end
  454. if c == 0 then table.insert( input_rules[cid], rules.c) end
  455. if d == 0 then table.insert( input_rules[cid], rules.d) end
  456. local mesecons = {
  457. effector = {
  458. rules = input_rules[cid],
  459. action_change = function (pos, _, rule_name, new_state)
  460. update_real_port_states(pos, rule_name, new_state)
  461. run(pos, {type=new_state, pin=rule_name})
  462. end,
  463. },
  464. receptor = {
  465. state = mesecon.state.on,
  466. rules = output_rules[cid]
  467. }
  468. }
  469. minetest.register_node(node_name, {
  470. description = "LuaController",
  471. drawtype = "nodebox",
  472. tiles = {
  473. top,
  474. "jeija_microcontroller_bottom.png",
  475. "jeija_microcontroller_sides.png",
  476. "jeija_microcontroller_sides.png",
  477. "jeija_microcontroller_sides.png",
  478. "jeija_microcontroller_sides.png"
  479. },
  480. inventory_image = top,
  481. paramtype = "light",
  482. groups = groups,
  483. drop = BASENAME.."0000",
  484. sunlight_propagates = true,
  485. selection_box = selection_box,
  486. node_box = node_box,
  487. on_construct = reset_meta,
  488. on_receive_fields = on_receive_fields,
  489. sounds = default.node_sound_stone_defaults(),
  490. mesecons = mesecons,
  491. digiline = digiline,
  492. -- Virtual portstates are the ports that
  493. -- the node shows as powered up (light up).
  494. virtual_portstates = {
  495. a = a == 1,
  496. b = b == 1,
  497. c = c == 1,
  498. d = d == 1,
  499. },
  500. after_dig_node = function (pos, node)
  501. mesecon.receptor_off(pos, output_rules)
  502. end,
  503. is_luacontroller = true,
  504. })
  505. end
  506. end
  507. end
  508. end
  509. ------------------------------
  510. -- Overheated LuaController --
  511. ------------------------------
  512. minetest.register_node(BASENAME .. "_burnt", {
  513. drawtype = "nodebox",
  514. tiles = {
  515. "jeija_luacontroller_burnt_top.png",
  516. "jeija_microcontroller_bottom.png",
  517. "jeija_microcontroller_sides.png",
  518. "jeija_microcontroller_sides.png",
  519. "jeija_microcontroller_sides.png",
  520. "jeija_microcontroller_sides.png"
  521. },
  522. inventory_image = "jeija_luacontroller_burnt_top.png",
  523. paramtype = "light",
  524. groups = {dig_immediate=2, not_in_creative_inventory=1},
  525. drop = BASENAME.."0000",
  526. sunlight_propagates = true,
  527. selection_box = selection_box,
  528. node_box = node_box,
  529. on_construct = reset_meta,
  530. on_receive_fields = on_receive_fields,
  531. sounds = default.node_sound_stone_defaults(),
  532. virtual_portstates = {a = false, b = false, c = false, d = false},
  533. mesecons = {
  534. effector = {
  535. rules = mesecon.rules.flat,
  536. action_change = function(pos, _, rule_name, new_state)
  537. update_real_port_states(pos, rule_name, new_state)
  538. end,
  539. },
  540. },
  541. })
  542. ------------------------
  543. -- Craft Registration --
  544. ------------------------
  545. minetest.register_craft({
  546. output = BASENAME.."0000 2",
  547. recipe = {
  548. {'mesecons_materials:silicon', 'mesecons_materials:silicon', 'group:mesecon_conductor_craftable'},
  549. {'mesecons_materials:silicon', 'mesecons_materials:silicon', 'group:mesecon_conductor_craftable'},
  550. {'group:mesecon_conductor_craftable', 'group:mesecon_conductor_craftable', ''},
  551. }
  552. })