init.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. local EEPROM_SIZE = 255
  2. local microc_rules = {}
  3. local yc = {}
  4. for a = 0, 1 do
  5. for b = 0, 1 do
  6. for c = 0, 1 do
  7. for d = 0, 1 do
  8. local nodename = "mesecons_microcontroller:microcontroller"..tostring(d)..tostring(c)..tostring(b)..tostring(a)
  9. local top = "jeija_microcontroller_top.png"
  10. if tostring(a) == "1" then
  11. top = top.."^jeija_microcontroller_LED_A.png"
  12. end
  13. if tostring(b) == "1" then
  14. top = top.."^jeija_microcontroller_LED_B.png"
  15. end
  16. if tostring(c) == "1" then
  17. top = top.."^jeija_microcontroller_LED_C.png"
  18. end
  19. if tostring(d) == "1" then
  20. top = top.."^jeija_microcontroller_LED_D.png"
  21. end
  22. local groups
  23. if tostring(d)..tostring(c)..tostring(b)..tostring(a) ~= "0000" then
  24. groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 3, overheat = 1}
  25. else
  26. groups = {dig_immediate=2, mesecon = 3, overheat = 1}
  27. end
  28. local rules={}
  29. if (a == 1) then table.insert(rules, {x = -1, y = 0, z = 0}) end
  30. if (b == 1) then table.insert(rules, {x = 0, y = 0, z = 1}) end
  31. if (c == 1) then table.insert(rules, {x = 1, y = 0, z = 0}) end
  32. if (d == 1) then table.insert(rules, {x = 0, y = 0, z = -1}) end
  33. local input_rules={}
  34. if (a == 0) then table.insert(input_rules, {x = -1, y = 0, z = 0, name = "A"}) end
  35. if (b == 0) then table.insert(input_rules, {x = 0, y = 0, z = 1, name = "B"}) end
  36. if (c == 0) then table.insert(input_rules, {x = 1, y = 0, z = 0, name = "C"}) end
  37. if (d == 0) then table.insert(input_rules, {x = 0, y = 0, z = -1, name = "D"}) end
  38. microc_rules[nodename] = rules
  39. local mesecons = {effector =
  40. {
  41. rules = input_rules,
  42. action_change = function (pos, node, rulename, newstate)
  43. yc.update_real_portstates(pos, node, rulename, newstate)
  44. yc.update(pos)
  45. end
  46. }}
  47. if nodename ~= "mesecons_microcontroller:microcontroller0000" then
  48. mesecons.receptor = {
  49. state = mesecon.state.on,
  50. rules = rules
  51. }
  52. end
  53. minetest.register_node(nodename, {
  54. description = "Microcontroller",
  55. drawtype = "nodebox",
  56. tiles = {
  57. top,
  58. "jeija_microcontroller_bottom.png",
  59. "jeija_microcontroller_sides.png",
  60. "jeija_microcontroller_sides.png",
  61. "jeija_microcontroller_sides.png",
  62. "jeija_microcontroller_sides.png"
  63. },
  64. sunlight_propagates = true,
  65. paramtype = "light",
  66. is_ground_content = false,
  67. walkable = true,
  68. groups = groups,
  69. drop = "mesecons_microcontroller:microcontroller0000 1",
  70. selection_box = {
  71. type = "fixed",
  72. fixed = { -8/16, -8/16, -8/16, 8/16, -5/16, 8/16 },
  73. },
  74. node_box = {
  75. type = "fixed",
  76. fixed = {
  77. { -8/16, -8/16, -8/16, 8/16, -7/16, 8/16 }, -- bottom slab
  78. { -5/16, -7/16, -5/16, 5/16, -6/16, 5/16 }, -- circuit board
  79. { -3/16, -6/16, -3/16, 3/16, -5/16, 3/16 }, -- IC
  80. }
  81. },
  82. on_construct = function(pos)
  83. local meta = minetest.get_meta(pos)
  84. meta:set_string("code", "")
  85. meta:set_string("formspec", "size[9,2.5]"..
  86. "field[0.256,-0.2;9,2;code;Code:;]"..
  87. "button[0 ,0.2;1.5,3;band;AND]"..
  88. "button[1.5,0.2;1.5,3;bxor;XOR]"..
  89. "button[3 ,0.2;1.5,3;bnot;NOT]"..
  90. "button[4.5,0.2;1.5,3;bnand;NAND]"..
  91. "button[6 ,0.2;1.5,3;btflop;T-Flop]"..
  92. "button[7.5,0.2;1.5,3;brsflop;RS-Flop]"..
  93. "button_exit[3.5,1;2,3;program;Program]")
  94. meta:set_string("infotext", "Unprogrammed Microcontroller")
  95. local r = ""
  96. for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
  97. meta:set_string("eeprom", r)
  98. end,
  99. on_receive_fields = function(pos, formanme, fields, sender)
  100. local meta = minetest.get_meta(pos)
  101. if fields.band then
  102. fields.code = "sbi(C, A&B) :A and B are inputs, C is output"
  103. elseif fields.bxor then
  104. fields.code = "sbi(C, A~B) :A and B are inputs, C is output"
  105. elseif fields.bnot then
  106. fields.code = "sbi(B, !A) :A is input, B is output"
  107. elseif fields.bnand then
  108. fields.code = "sbi(C, !A|!B) :A and B are inputs, C is output"
  109. elseif fields.btflop then
  110. fields.code = "if(A)sbi(1,1);if(!A&#1)sbi(B,!B)sbi(1,0); if(C)off(B,1); :A is input, B is output (Q), C is reset, toggles with falling edge"
  111. elseif fields.brsflop then
  112. fields.code = "if(A)on(C);if(B)off(C); :A is S (Set), B is R (Reset), C is output (R dominates)"
  113. end
  114. if fields.code == nil then return end
  115. meta:set_string("code", fields.code)
  116. meta:set_string("formspec", "size[9,2.5]"..
  117. "field[0.256,-0.2;9,2;code;Code:;"..minetest.formspec_escape(fields.code).."]"..
  118. "button[0 ,0.2;1.5,3;band;AND]"..
  119. "button[1.5,0.2;1.5,3;bxor;XOR]"..
  120. "button[3 ,0.2;1.5,3;bnot;NOT]"..
  121. "button[4.5,0.2;1.5,3;bnand;NAND]"..
  122. "button[6 ,0.2;1.5,3;btflop;T-Flop]"..
  123. "button[7.5,0.2;1.5,3;brsflop;RS-Flop]"..
  124. "button_exit[3.5,1;2,3;program;Program]")
  125. meta:set_string("infotext", "Programmed Microcontroller")
  126. yc.reset (pos)
  127. yc.update(pos)
  128. end,
  129. sounds = default.node_sound_stone_defaults(),
  130. mesecons = mesecons,
  131. after_dig_node = function (pos, node)
  132. rules = microc_rules[node.name]
  133. mesecon.receptor_off(pos, rules)
  134. end,
  135. on_blast = mesecon.on_blastnode,
  136. })
  137. end
  138. end
  139. end
  140. end
  141. if minetest.get_modpath("mesecons_luacontroller") then
  142. minetest.register_craft({
  143. type = "shapeless",
  144. output = "mesecons_microcontroller:microcontroller0000",
  145. recipe = {"mesecons_luacontroller:luacontroller0000"},
  146. })
  147. minetest.register_craft({
  148. type = "shapeless",
  149. output = "mesecons_luacontroller:luacontroller0000",
  150. recipe = {"mesecons_microcontroller:microcontroller0000"},
  151. })
  152. else
  153. minetest.register_craft({
  154. output = 'craft "mesecons_microcontroller:microcontroller0000" 2',
  155. recipe = {
  156. {'mesecons_materials:silicon', 'mesecons_materials:silicon', 'group:mesecon_conductor_craftable'},
  157. {'mesecons_materials:silicon', 'mesecons_materials:silicon', 'group:mesecon_conductor_craftable'},
  158. {'group:mesecon_conductor_craftable', 'group:mesecon_conductor_craftable', ''},
  159. }
  160. })
  161. end
  162. yc.reset = function(pos)
  163. yc.action(pos, {a=false, b=false, c=false, d=false})
  164. local meta = minetest.get_meta(pos)
  165. meta:set_int("afterid", 0)
  166. local r = ""
  167. for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
  168. meta:set_string("eeprom", r)
  169. end
  170. yc.update = function(pos)
  171. local meta = minetest.get_meta(pos)
  172. if (mesecon.do_overheat(pos)) then
  173. minetest.remove_node(pos)
  174. minetest.after(0.2, function (pos)
  175. mesecon.receptor_off(pos, mesecon.rules.flat)
  176. end , pos) -- wait for pending parsings
  177. minetest.add_item(pos, "mesecons_microcontroller:microcontroller0000")
  178. end
  179. local code = meta:get_string("code")
  180. code = yc.code_remove_commentary(code)
  181. code = string.gsub(code, " ", "") --Remove all spaces
  182. code = string.gsub(code, " ", "") --Remove all tabs
  183. if yc.parsecode(code, pos) == nil then
  184. meta:set_string("infotext", "Code not valid!\n"..code)
  185. else
  186. meta:set_string("infotext", "Working Microcontroller\n"..code)
  187. end
  188. end
  189. --Code Parsing
  190. yc.code_remove_commentary = function(code)
  191. local is_string = false
  192. for i = 1, #code do
  193. if code:sub(i, i) == '"' then
  194. is_string = not is_string --toggle is_string
  195. elseif code:sub(i, i) == ":" and not is_string then
  196. return code:sub(1, i-1)
  197. end
  198. end
  199. return code
  200. end
  201. yc.parsecode = function(code, pos)
  202. local meta = minetest.get_meta(pos)
  203. local endi = 1
  204. local Lreal = yc.get_real_portstates(pos)
  205. local Lvirtual = yc.get_virtual_portstates(pos)
  206. if Lvirtual == nil then return nil end
  207. local c
  208. local eeprom = meta:get_string("eeprom")
  209. while true do
  210. local command, params
  211. command, endi = yc.parse_get_command(code, endi)
  212. if command == nil then return nil end
  213. if command == true then break end --end of code
  214. if command == "if" then
  215. local r
  216. r, endi = yc.command_if(code, endi, yc.merge_portstates(Lreal, Lvirtual), eeprom)
  217. if r == nil then return nil end
  218. if r == true then -- nothing
  219. elseif r == false then
  220. local endi_new = yc.skip_to_else (code, endi)
  221. if endi_new == nil then --else > not found
  222. endi = yc.skip_to_endif(code, endi)
  223. else
  224. endi = endi_new
  225. end
  226. if endi == nil then return nil end
  227. end
  228. else
  229. params, endi = yc.parse_get_params(code, endi)
  230. if not params then return nil end
  231. end
  232. if command == "on" then
  233. L = yc.command_on (params, Lvirtual)
  234. elseif command == "off" then
  235. L = yc.command_off(params, Lvirtual)
  236. elseif command == "print" then
  237. local su = yc.command_print(params, eeprom, yc.merge_portstates(Lreal, Lvirtual))
  238. if su ~= true then return nil end
  239. elseif command == "after" then
  240. local su = yc.command_after(params, pos)
  241. if su == nil then return nil end
  242. elseif command == "sbi" then
  243. local new_eeprom
  244. new_eeprom, Lvirtual = yc.command_sbi (params, eeprom, yc.merge_portstates(Lreal, Lvirtual), Lvirtual)
  245. if new_eeprom == nil then return nil
  246. else eeprom = new_eeprom end
  247. elseif command == "if" then --nothing
  248. else
  249. return nil
  250. end
  251. if Lvirtual == nil then return nil end
  252. if eeprom == nil then return nil else
  253. minetest.get_meta(pos):set_string("eeprom", eeprom) end
  254. end
  255. yc.action(pos, Lvirtual)
  256. return true
  257. end
  258. yc.parse_get_command = function(code, starti)
  259. i = starti
  260. local s
  261. while s ~= "" do
  262. s = string.sub(code, i, i)
  263. if s == "(" then
  264. return string.sub(code, starti, i-1), i + 1 -- i: ( i+1 after (
  265. end
  266. if s == ";" and starti == i then
  267. starti = starti + 1
  268. i = starti
  269. elseif s == ">" then
  270. starti = yc.skip_to_endif(code, starti)
  271. if starti == nil then return nil end
  272. i = starti
  273. else
  274. i = i + 1
  275. end
  276. end
  277. if starti == i-1 then
  278. return true, true
  279. end
  280. return nil, nil
  281. end
  282. yc.parse_get_params = function(code, starti)
  283. i = starti
  284. local s
  285. local params = {}
  286. local is_string = false
  287. while s ~= "" do
  288. s = string.sub(code, i, i)
  289. if code:sub(i, i) == '"' then
  290. is_string = (is_string==false) --toggle is_string
  291. end
  292. if s == ")" and is_string == false then
  293. table.insert(params, string.sub(code, starti, i-1)) -- i: ) i+1 after )
  294. return params, i + 1
  295. end
  296. if s == "," and is_string == false then
  297. table.insert(params, string.sub(code, starti, i-1)) -- i: ) i+1 after )
  298. starti = i + 1
  299. end
  300. i = i + 1
  301. end
  302. return nil, nil
  303. end
  304. yc.parse_get_eeprom_param = function(cond, starti)
  305. i = starti
  306. local s
  307. local addr
  308. while s ~= "" do
  309. s = string.sub(cond, i, i)
  310. if string.find("0123456789", s) == nil or s == "" then
  311. addr = string.sub(cond, starti, i-1) -- i: last number i+1 after last number
  312. return addr, i
  313. end
  314. if s == "," then return nil, nil end
  315. i = i + 1
  316. end
  317. return nil, nil
  318. end
  319. yc.skip_to_endif = function(code, starti)
  320. local i = starti
  321. local s = false
  322. local open_ifs = 1
  323. while s ~= nil and s~= "" do
  324. s = code:sub(i, i)
  325. if s == "i" and code:sub(i+1, i+1) == "f" then --if in µCScript
  326. open_ifs = open_ifs + 1
  327. end
  328. if s == ";" then
  329. open_ifs = open_ifs - 1
  330. end
  331. if open_ifs == 0 then
  332. return i + 1
  333. end
  334. i = i + 1
  335. end
  336. return nil
  337. end
  338. yc.skip_to_else = function(code, starti)
  339. local i = starti
  340. local s = false
  341. local open_ifs = 1
  342. while s ~= nil and s~= "" do
  343. s = code:sub(i, i)
  344. if s == "i" and code:sub(i+1, i+1) == "f" then --if in µCScript
  345. open_ifs = open_ifs + 1
  346. end
  347. if s == ";" then
  348. open_ifs = open_ifs - 1
  349. end
  350. if open_ifs == 1 and s == ">" then
  351. return i + 1
  352. end
  353. i = i + 1
  354. end
  355. return nil
  356. end
  357. --Commands
  358. yc.command_on = function(params, L)
  359. local rules = {}
  360. for i, port in ipairs(params) do
  361. L = yc.set_portstate (port, true, L)
  362. end
  363. return L
  364. end
  365. yc.command_off = function(params, L)
  366. local rules = {}
  367. for i, port in ipairs(params) do
  368. L = yc.set_portstate (port, false, L)
  369. end
  370. return L
  371. end
  372. yc.command_print = function(params, eeprom, L)
  373. local s = ""
  374. for i, param in ipairs(params) do
  375. if param:sub(1,1) == '"' and param:sub(#param, #param) == '"' then
  376. s = s..param:sub(2, #param-1)
  377. else
  378. r = yc.command_parsecondition(param, L, eeprom)
  379. if r == "1" or r == "0" then
  380. s = s..r
  381. else return nil end
  382. end
  383. end
  384. print(s) --don't remove
  385. return true
  386. end
  387. yc.command_sbi = function(params, eeprom, L, Lv)
  388. if params[1]==nil or params[2]==nil or params[3] ~=nil then return nil end
  389. local status = yc.command_parsecondition(params[2], L, eeprom)
  390. if status == nil then return nil, nil end
  391. if string.find("ABCD", params[1])~=nil and #params[1]==1 then --is a port
  392. if status == "1" then
  393. Lv = yc.set_portstate (params[1], true, Lv)
  394. else
  395. Lv = yc.set_portstate (params[1], false, Lv)
  396. end
  397. return eeprom, Lv;
  398. end
  399. --is an eeprom address
  400. local new_eeprom = "";
  401. for i=1, #eeprom do
  402. if tonumber(params[1])==i then
  403. new_eeprom = new_eeprom..status
  404. else
  405. new_eeprom = new_eeprom..eeprom:sub(i, i)
  406. end
  407. end
  408. return new_eeprom, Lv
  409. end
  410. -- after (delay)
  411. yc.command_after = function(params, pos)
  412. if params[1] == nil or params[2] == nil or params[3] ~= nil then return nil end
  413. --get time (maximum time is 200)
  414. local time = tonumber(params[1])
  415. if time == nil or time > 200 then
  416. return nil
  417. end
  418. --get code in quotes "code"
  419. if string.sub(params[2], 1, 1) ~= '"' or string.sub(params[2], #params[2], #params[2]) ~= '"' then return nil end
  420. local code = string.sub(params[2], 2, #params[2] - 1)
  421. local afterid = math.random(10000)
  422. local meta = minetest.get_meta(pos)
  423. meta:set_int("afterid", afterid)
  424. minetest.after(time, yc.command_after_execute, {pos = pos, code = code, afterid = afterid})
  425. return true
  426. end
  427. yc.command_after_execute = function(params)
  428. local meta = minetest.get_meta(params.pos)
  429. if meta:get_int("afterid") == params.afterid then --make sure the node has not been changed
  430. if yc.parsecode(params.code, params.pos) == nil then
  431. meta:set_string("infotext", "Code in after() not valid!")
  432. else
  433. if code ~= nil then
  434. meta:set_string("infotext", "Working Microcontroller\n"..code)
  435. else
  436. meta:set_string("infotext", "Working Microcontroller")
  437. end
  438. end
  439. end
  440. end
  441. --If
  442. yc.command_if = function(code, starti, L, eeprom)
  443. local cond, endi = yc.command_if_getcondition(code, starti)
  444. if cond == nil then return nil end
  445. cond = yc.command_parsecondition(cond, L, eeprom)
  446. local result
  447. if cond == "0" then result = false
  448. elseif cond == "1" then result = true end
  449. if not result then end
  450. return result, endi --endi from local cond, endi = yc.command_if_getcondition(code, starti)
  451. end
  452. --Condition parsing
  453. yc.command_if_getcondition = function(code, starti)
  454. i = starti
  455. local s
  456. local brackets = 1 --1 Bracket to close
  457. while s ~= "" do
  458. s = string.sub(code, i, i)
  459. if s == ")" then
  460. brackets = brackets - 1
  461. end
  462. if s == "(" then
  463. brackets = brackets + 1
  464. end
  465. if brackets == 0 then
  466. return string.sub(code, starti, i-1), i + 1 -- i: ( i+1 after (
  467. end
  468. i = i + 1
  469. end
  470. return nil, nil
  471. end
  472. yc.command_parsecondition = function(cond, L, eeprom)
  473. cond = string.gsub(cond, "A", tonumber(L.a and 1 or 0))
  474. cond = string.gsub(cond, "B", tonumber(L.b and 1 or 0))
  475. cond = string.gsub(cond, "C", tonumber(L.c and 1 or 0))
  476. cond = string.gsub(cond, "D", tonumber(L.d and 1 or 0))
  477. local i = 1
  478. local l = string.len(cond)
  479. while i<=l do
  480. local s = cond:sub(i,i)
  481. if s == "#" then
  482. local addr, endi = yc.parse_get_eeprom_param(cond, i+1)
  483. local buf = yc.eeprom_read(tonumber(addr), eeprom)
  484. if buf == nil then return nil end
  485. local call = cond:sub(i, endi-1)
  486. cond = string.gsub(cond, call, buf)
  487. i = 0
  488. l = string.len(cond)
  489. end
  490. i = i + 1
  491. end
  492. cond = string.gsub(cond, "!0", "1")
  493. cond = string.gsub(cond, "!1", "0")
  494. local i = 2
  495. local l = string.len(cond)
  496. while i<=l do
  497. local s = cond:sub(i,i)
  498. local b = tonumber(cond:sub(i-1, i-1))
  499. local a = tonumber(cond:sub(i+1, i+1))
  500. if cond:sub(i+1, i+1) == nil then break end
  501. if s == "=" then
  502. if a==nil then return nil end
  503. if b==nil then return nil end
  504. if a == b then buf = "1" end
  505. if a ~= b then buf = "0" end
  506. cond = string.gsub(cond, b..s..a, buf)
  507. i = 1
  508. l = string.len(cond)
  509. end
  510. i = i + 1
  511. end
  512. local i = 2
  513. local l = string.len(cond)
  514. while i<=l do
  515. local s = cond:sub(i,i)
  516. local b = tonumber(cond:sub(i-1, i-1))
  517. local a = tonumber(cond:sub(i+1, i+1))
  518. if cond:sub(i+1, i+1) == nil then break end
  519. if s == "&" then
  520. if a==nil then return nil end
  521. local buf = ((a==1) and (b==1))
  522. if buf == true then buf = "1" end
  523. if buf == false then buf = "0" end
  524. cond = string.gsub(cond, b..s..a, buf)
  525. i = 1
  526. l = string.len(cond)
  527. end
  528. if s == "|" then
  529. if a==nil then return nil end
  530. local buf = ((a == 1) or (b == 1))
  531. if buf == true then buf = "1" end
  532. if buf == false then buf = "0" end
  533. cond = string.gsub(cond, b..s..a, buf)
  534. i = 1
  535. l = string.len(cond)
  536. end
  537. if s == "~" then
  538. if a==nil then return nil end
  539. local buf = (((a == 1) or (b == 1)) and not((a==1) and (b==1)))
  540. if buf == true then buf = "1" end
  541. if buf == false then buf = "0" end
  542. cond = string.gsub(cond, b..s..a, buf)
  543. i = 1
  544. l = string.len(cond)
  545. end
  546. i = i + 1
  547. end
  548. return cond
  549. end
  550. --Virtual-Hardware functions
  551. yc.eeprom_read = function(number, eeprom)
  552. if not number then return end
  553. return eeprom:sub(number, number)
  554. end
  555. --Real I/O functions
  556. yc.action = function(pos, L) --L-->Lvirtual
  557. local Lv = yc.get_virtual_portstates(pos)
  558. local name = "mesecons_microcontroller:microcontroller"
  559. ..tonumber(L.d and 1 or 0)
  560. ..tonumber(L.c and 1 or 0)
  561. ..tonumber(L.b and 1 or 0)
  562. ..tonumber(L.a and 1 or 0)
  563. local node = minetest.get_node(pos)
  564. minetest.swap_node(pos, {name = name, param2 = node.param2})
  565. yc.action_setports(pos, L, Lv)
  566. end
  567. yc.action_setports = function(pos, L, Lv)
  568. local name = "mesecons_microcontroller:microcontroller"
  569. local rules
  570. if Lv.a ~= L.a then
  571. rules = microc_rules[name.."0001"]
  572. if L.a == true then mesecon.receptor_on(pos, rules)
  573. else mesecon.receptor_off(pos, rules) end
  574. end
  575. if Lv.b ~= L.b then
  576. rules = microc_rules[name.."0010"]
  577. if L.b == true then mesecon.receptor_on(pos, rules)
  578. else mesecon.receptor_off(pos, rules) end
  579. end
  580. if Lv.c ~= L.c then
  581. rules = microc_rules[name.."0100"]
  582. if L.c == true then mesecon.receptor_on(pos, rules)
  583. else mesecon.receptor_off(pos, rules) end
  584. end
  585. if Lv.d ~= L.d then
  586. rules = microc_rules[name.."1000"]
  587. if L.d == true then mesecon.receptor_on(pos, rules)
  588. else mesecon.receptor_off(pos, rules) end
  589. end
  590. end
  591. yc.set_portstate = function(port, state, L)
  592. if port == "A" then L.a = state
  593. elseif port == "B" then L.b = state
  594. elseif port == "C" then L.c = state
  595. elseif port == "D" then L.d = state
  596. else return nil end
  597. return L
  598. end
  599. yc.update_real_portstates = function(pos, node, rulename, newstate)
  600. local meta = minetest.get_meta(pos)
  601. if rulename == nil then
  602. meta:set_int("real_portstates", 1)
  603. return
  604. end
  605. local n = meta:get_int("real_portstates") - 1
  606. local L = {}
  607. for i = 1, 4 do
  608. L[i] = n%2
  609. n = math.floor(n/2)
  610. end
  611. if rulename.x == nil then
  612. for _, rname in ipairs(rulename) do
  613. local port = ({4, 1, nil, 3, 2})[rname.x+2*rname.z+3]
  614. L[port] = (newstate == "on") and 1 or 0
  615. end
  616. else
  617. local port = ({4, 1, nil, 3, 2})[rulename.x+2*rulename.z+3]
  618. L[port] = (newstate == "on") and 1 or 0
  619. end
  620. meta:set_int("real_portstates", 1 + L[1] + 2*L[2] + 4*L[3] + 8*L[4])
  621. end
  622. yc.get_real_portstates = function(pos) -- determine if ports are powered (by itself or from outside)
  623. local meta = minetest.get_meta(pos)
  624. local L = {}
  625. local n = meta:get_int("real_portstates") - 1
  626. for _, index in ipairs({"a", "b", "c", "d"}) do
  627. L[index] = ((n%2) == 1)
  628. n = math.floor(n/2)
  629. end
  630. return L
  631. end
  632. yc.get_virtual_portstates = function(pos) -- portstates according to the name
  633. local name = minetest.get_node(pos).name
  634. local b, a = string.find(name, ":microcontroller")
  635. if a == nil then return nil end
  636. a = a + 1
  637. local Lvirtual = {a=false, b=false, c=false, d=false}
  638. if name:sub(a , a ) == "1" then Lvirtual.d = true end
  639. if name:sub(a+1, a+1) == "1" then Lvirtual.c = true end
  640. if name:sub(a+2, a+2) == "1" then Lvirtual.b = true end
  641. if name:sub(a+3, a+3) == "1" then Lvirtual.a = true end
  642. return Lvirtual
  643. end
  644. yc.merge_portstates = function(Lreal, Lvirtual)
  645. local L = {a=false, b=false, c=false, d=false}
  646. if Lvirtual.a or Lreal.a then L.a = true end
  647. if Lvirtual.b or Lreal.b then L.b = true end
  648. if Lvirtual.c or Lreal.c then L.c = true end
  649. if Lvirtual.d or Lreal.d then L.d = true end
  650. return L
  651. end