2022-12-01 18:33:19 +01:00
|
|
|
local S = minetest.get_translator(minetest.get_current_modname())
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
local EEPROM_SIZE = 255
|
2012-08-08 23:35:36 +02:00
|
|
|
|
2014-11-22 11:37:47 +01:00
|
|
|
local microc_rules = {}
|
2017-01-15 23:12:13 +01:00
|
|
|
local yc = {}
|
2014-11-22 11:37:47 +01:00
|
|
|
|
2012-08-09 12:54:31 +02:00
|
|
|
for a = 0, 1 do
|
|
|
|
for b = 0, 1 do
|
|
|
|
for c = 0, 1 do
|
|
|
|
for d = 0, 1 do
|
|
|
|
local nodename = "mesecons_microcontroller:microcontroller"..tostring(d)..tostring(c)..tostring(b)..tostring(a)
|
2012-08-25 08:23:44 +02:00
|
|
|
local top = "jeija_microcontroller_top.png"
|
|
|
|
if tostring(a) == "1" then
|
2024-06-08 13:43:47 +02:00
|
|
|
top = top.."^jeija_luacontroller_LED_A.png"
|
2012-08-25 08:23:44 +02:00
|
|
|
end
|
|
|
|
if tostring(b) == "1" then
|
2024-06-08 13:43:47 +02:00
|
|
|
top = top.."^jeija_luacontroller_LED_B.png"
|
2012-08-25 08:23:44 +02:00
|
|
|
end
|
|
|
|
if tostring(c) == "1" then
|
2024-06-08 13:43:47 +02:00
|
|
|
top = top.."^jeija_luacontroller_LED_C.png"
|
2012-08-25 08:23:44 +02:00
|
|
|
end
|
|
|
|
if tostring(d) == "1" then
|
2024-06-08 13:43:47 +02:00
|
|
|
top = top.."^jeija_luacontroller_LED_D.png"
|
2012-08-25 08:23:44 +02:00
|
|
|
end
|
2017-01-15 23:12:13 +01:00
|
|
|
local groups
|
2012-08-09 13:34:10 +02:00
|
|
|
if tostring(d)..tostring(c)..tostring(b)..tostring(a) ~= "0000" then
|
2013-06-05 06:45:19 +02:00
|
|
|
groups = {dig_immediate=2, not_in_creative_inventory=1, mesecon = 3, overheat = 1}
|
2012-08-09 13:34:10 +02:00
|
|
|
else
|
2013-06-05 06:45:19 +02:00
|
|
|
groups = {dig_immediate=2, mesecon = 3, overheat = 1}
|
2012-08-09 13:34:10 +02:00
|
|
|
end
|
2012-12-09 00:42:30 +01:00
|
|
|
local rules={}
|
|
|
|
if (a == 1) then table.insert(rules, {x = -1, y = 0, z = 0}) end
|
|
|
|
if (b == 1) then table.insert(rules, {x = 0, y = 0, z = 1}) end
|
|
|
|
if (c == 1) then table.insert(rules, {x = 1, y = 0, z = 0}) end
|
|
|
|
if (d == 1) then table.insert(rules, {x = 0, y = 0, z = -1}) end
|
|
|
|
|
|
|
|
local input_rules={}
|
2012-12-19 17:34:05 +01:00
|
|
|
if (a == 0) then table.insert(input_rules, {x = -1, y = 0, z = 0, name = "A"}) end
|
|
|
|
if (b == 0) then table.insert(input_rules, {x = 0, y = 0, z = 1, name = "B"}) end
|
|
|
|
if (c == 0) then table.insert(input_rules, {x = 1, y = 0, z = 0, name = "C"}) end
|
|
|
|
if (d == 0) then table.insert(input_rules, {x = 0, y = 0, z = -1, name = "D"}) end
|
2014-11-22 11:37:47 +01:00
|
|
|
microc_rules[nodename] = rules
|
2012-12-09 00:42:30 +01:00
|
|
|
|
|
|
|
local mesecons = {effector =
|
|
|
|
{
|
|
|
|
rules = input_rules,
|
2014-01-10 16:38:02 +01:00
|
|
|
action_change = function (pos, node, rulename, newstate)
|
2022-02-27 21:12:37 +01:00
|
|
|
if yc.update_real_portstates(pos, node, rulename, newstate) then
|
|
|
|
yc.update(pos)
|
|
|
|
end
|
2012-12-09 00:42:30 +01:00
|
|
|
end
|
|
|
|
}}
|
|
|
|
if nodename ~= "mesecons_microcontroller:microcontroller0000" then
|
|
|
|
mesecons.receptor = {
|
|
|
|
state = mesecon.state.on,
|
|
|
|
rules = rules
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-08-09 12:54:31 +02:00
|
|
|
minetest.register_node(nodename, {
|
2022-12-01 18:33:19 +01:00
|
|
|
description = S("Microcontroller"),
|
2012-08-08 13:26:32 +02:00
|
|
|
drawtype = "nodebox",
|
2012-08-09 10:13:05 +02:00
|
|
|
tiles = {
|
2012-08-25 08:23:44 +02:00
|
|
|
top,
|
2012-08-25 07:32:24 +02:00
|
|
|
"jeija_microcontroller_bottom.png",
|
2012-08-09 10:13:05 +02:00
|
|
|
"jeija_microcontroller_sides.png",
|
2012-08-25 07:32:24 +02:00
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png",
|
|
|
|
"jeija_microcontroller_sides.png"
|
2012-08-09 10:13:05 +02:00
|
|
|
},
|
2012-08-09 21:40:25 +02:00
|
|
|
|
2012-08-08 13:26:32 +02:00
|
|
|
sunlight_propagates = true,
|
|
|
|
paramtype = "light",
|
2017-10-31 22:50:39 +01:00
|
|
|
is_ground_content = false,
|
2012-08-08 13:26:32 +02:00
|
|
|
walkable = true,
|
2012-08-09 13:34:10 +02:00
|
|
|
groups = groups,
|
2013-04-28 12:40:08 +02:00
|
|
|
drop = "mesecons_microcontroller:microcontroller0000 1",
|
2012-08-08 13:26:32 +02:00
|
|
|
selection_box = {
|
|
|
|
type = "fixed",
|
2012-08-12 03:04:05 +02:00
|
|
|
fixed = { -8/16, -8/16, -8/16, 8/16, -5/16, 8/16 },
|
2012-08-08 13:26:32 +02:00
|
|
|
},
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
2012-08-09 10:13:05 +02:00
|
|
|
fixed = {
|
2012-08-12 03:04:05 +02:00
|
|
|
{ -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
|
2012-08-09 10:13:05 +02:00
|
|
|
}
|
2012-08-08 13:26:32 +02:00
|
|
|
},
|
|
|
|
on_construct = function(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-08-08 13:26:32 +02:00
|
|
|
meta:set_string("code", "")
|
2012-08-09 22:39:54 +02:00
|
|
|
meta:set_string("formspec", "size[9,2.5]"..
|
|
|
|
"field[0.256,-0.2;9,2;code;Code:;]"..
|
|
|
|
"button[0 ,0.2;1.5,3;band;AND]"..
|
|
|
|
"button[1.5,0.2;1.5,3;bxor;XOR]"..
|
|
|
|
"button[3 ,0.2;1.5,3;bnot;NOT]"..
|
|
|
|
"button[4.5,0.2;1.5,3;bnand;NAND]"..
|
|
|
|
"button[6 ,0.2;1.5,3;btflop;T-Flop]"..
|
|
|
|
"button[7.5,0.2;1.5,3;brsflop;RS-Flop]"..
|
|
|
|
"button_exit[3.5,1;2,3;program;Program]")
|
2012-08-08 13:26:32 +02:00
|
|
|
meta:set_string("infotext", "Unprogrammed Microcontroller")
|
2012-08-08 23:35:36 +02:00
|
|
|
local r = ""
|
|
|
|
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
|
|
|
|
meta:set_string("eeprom", r)
|
2012-08-08 13:26:32 +02:00
|
|
|
end,
|
2022-02-12 20:12:12 +01:00
|
|
|
on_receive_fields = function(pos, _, fields, sender)
|
2018-07-23 13:00:59 +02:00
|
|
|
local player_name = sender:get_player_name()
|
|
|
|
if minetest.is_protected(pos, player_name) and
|
|
|
|
not minetest.check_player_privs(player_name, {protection_bypass=true}) then
|
|
|
|
minetest.record_protection_violation(pos, player_name)
|
|
|
|
return
|
|
|
|
end
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-08-09 22:39:54 +02:00
|
|
|
if fields.band then
|
2012-08-10 09:44:49 +02:00
|
|
|
fields.code = "sbi(C, A&B) :A and B are inputs, C is output"
|
2012-08-09 22:39:54 +02:00
|
|
|
elseif fields.bxor then
|
2012-08-10 09:44:49 +02:00
|
|
|
fields.code = "sbi(C, A~B) :A and B are inputs, C is output"
|
2012-08-09 22:39:54 +02:00
|
|
|
elseif fields.bnot then
|
2012-08-10 09:44:49 +02:00
|
|
|
fields.code = "sbi(B, !A) :A is input, B is output"
|
2012-08-09 22:39:54 +02:00
|
|
|
elseif fields.bnand then
|
2012-08-10 09:44:49 +02:00
|
|
|
fields.code = "sbi(C, !A|!B) :A and B are inputs, C is output"
|
2012-08-09 22:39:54 +02:00
|
|
|
elseif fields.btflop then
|
2012-10-07 19:08:01 +02:00
|
|
|
fields.code = "if(A)sbi(1,1);if(!A)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"
|
2012-08-09 22:39:54 +02:00
|
|
|
elseif fields.brsflop then
|
|
|
|
fields.code = "if(A)on(C);if(B)off(C); :A is S (Set), B is R (Reset), C is output (R dominates)"
|
2014-04-25 12:54:39 +02:00
|
|
|
end
|
|
|
|
if fields.code == nil then return end
|
2012-08-09 22:39:54 +02:00
|
|
|
|
|
|
|
meta:set_string("code", fields.code)
|
|
|
|
meta:set_string("formspec", "size[9,2.5]"..
|
2013-08-29 23:14:04 +02:00
|
|
|
"field[0.256,-0.2;9,2;code;Code:;"..minetest.formspec_escape(fields.code).."]"..
|
2012-08-09 22:39:54 +02:00
|
|
|
"button[0 ,0.2;1.5,3;band;AND]"..
|
|
|
|
"button[1.5,0.2;1.5,3;bxor;XOR]"..
|
|
|
|
"button[3 ,0.2;1.5,3;bnot;NOT]"..
|
|
|
|
"button[4.5,0.2;1.5,3;bnand;NAND]"..
|
|
|
|
"button[6 ,0.2;1.5,3;btflop;T-Flop]"..
|
|
|
|
"button[7.5,0.2;1.5,3;brsflop;RS-Flop]"..
|
|
|
|
"button_exit[3.5,1;2,3;program;Program]")
|
2012-08-09 23:09:02 +02:00
|
|
|
meta:set_string("infotext", "Programmed Microcontroller")
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.reset (pos)
|
2017-01-19 16:31:11 +01:00
|
|
|
yc.update(pos)
|
2012-08-09 21:40:25 +02:00
|
|
|
end,
|
2022-05-05 16:57:53 +02:00
|
|
|
sounds = mesecon.node_sound.stone,
|
2012-12-09 00:42:30 +01:00
|
|
|
mesecons = mesecons,
|
2012-12-09 15:32:29 +01:00
|
|
|
after_dig_node = function (pos, node)
|
2014-11-22 11:37:47 +01:00
|
|
|
rules = microc_rules[node.name]
|
2014-11-22 15:42:22 +01:00
|
|
|
mesecon.receptor_off(pos, rules)
|
2012-12-09 00:42:30 +01:00
|
|
|
end,
|
2017-10-07 00:44:49 +02:00
|
|
|
on_blast = mesecon.on_blastnode,
|
2012-08-08 13:26:32 +02:00
|
|
|
})
|
2012-08-09 12:54:31 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-08 13:26:32 +02:00
|
|
|
|
2017-09-11 21:26:37 +02:00
|
|
|
if minetest.get_modpath("mesecons_luacontroller") then
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "shapeless",
|
|
|
|
output = "mesecons_microcontroller:microcontroller0000",
|
|
|
|
recipe = {"mesecons_luacontroller:luacontroller0000"},
|
|
|
|
})
|
|
|
|
minetest.register_craft({
|
|
|
|
type = "shapeless",
|
|
|
|
output = "mesecons_luacontroller:luacontroller0000",
|
|
|
|
recipe = {"mesecons_microcontroller:microcontroller0000"},
|
|
|
|
})
|
|
|
|
else
|
|
|
|
minetest.register_craft({
|
|
|
|
output = 'craft "mesecons_microcontroller:microcontroller0000" 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', ''},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
2012-08-08 13:26:32 +02:00
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.reset = function(pos)
|
|
|
|
yc.action(pos, {a=false, b=false, c=false, d=false})
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-08-13 22:55:14 +02:00
|
|
|
meta:set_int("afterid", 0)
|
2012-08-08 23:35:36 +02:00
|
|
|
local r = ""
|
|
|
|
for i=1, EEPROM_SIZE+1 do r=r.."0" end --Generate a string with EEPROM_SIZE*"0"
|
|
|
|
meta:set_string("eeprom", r)
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.update = function(pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2014-04-20 21:44:58 +02:00
|
|
|
|
|
|
|
if (mesecon.do_overheat(pos)) then
|
2013-12-01 04:13:00 +01:00
|
|
|
minetest.remove_node(pos)
|
2014-04-20 21:44:58 +02:00
|
|
|
minetest.after(0.2, function (pos)
|
2014-11-22 15:42:22 +01:00
|
|
|
mesecon.receptor_off(pos, mesecon.rules.flat)
|
2014-04-20 21:44:58 +02:00
|
|
|
end , pos) -- wait for pending parsings
|
2013-12-01 04:13:00 +01:00
|
|
|
minetest.add_item(pos, "mesecons_microcontroller:microcontroller0000")
|
2012-08-09 16:25:30 +02:00
|
|
|
end
|
|
|
|
|
2012-08-08 13:26:32 +02:00
|
|
|
local code = meta:get_string("code")
|
2017-01-15 23:12:13 +01:00
|
|
|
code = yc.code_remove_commentary(code)
|
2012-08-08 13:26:32 +02:00
|
|
|
code = string.gsub(code, " ", "") --Remove all spaces
|
|
|
|
code = string.gsub(code, " ", "") --Remove all tabs
|
2017-01-15 23:12:13 +01:00
|
|
|
if yc.parsecode(code, pos) == nil then
|
2012-08-21 19:35:34 +02:00
|
|
|
meta:set_string("infotext", "Code not valid!\n"..code)
|
2012-08-08 13:26:32 +02:00
|
|
|
else
|
2012-08-21 19:35:34 +02:00
|
|
|
meta:set_string("infotext", "Working Microcontroller\n"..code)
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-09 21:40:25 +02:00
|
|
|
|
|
|
|
--Code Parsing
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.code_remove_commentary = function(code)
|
2014-11-22 15:42:22 +01:00
|
|
|
local is_string = false
|
2012-08-09 08:27:47 +02:00
|
|
|
for i = 1, #code do
|
2012-08-10 10:47:40 +02:00
|
|
|
if code:sub(i, i) == '"' then
|
2012-10-07 19:08:01 +02:00
|
|
|
is_string = not is_string --toggle is_string
|
|
|
|
elseif code:sub(i, i) == ":" and not is_string then
|
2012-08-09 08:27:47 +02:00
|
|
|
return code:sub(1, i-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return code
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.parsecode = function(code, pos)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-08-08 13:26:32 +02:00
|
|
|
local endi = 1
|
2017-01-15 23:12:13 +01:00
|
|
|
local Lreal = yc.get_real_portstates(pos)
|
|
|
|
local Lvirtual = yc.get_virtual_portstates(pos)
|
2012-08-09 21:40:25 +02:00
|
|
|
if Lvirtual == nil then return nil end
|
2012-09-08 15:10:31 +02:00
|
|
|
local eeprom = meta:get_string("eeprom")
|
2012-08-08 13:26:32 +02:00
|
|
|
while true do
|
2014-11-22 15:42:22 +01:00
|
|
|
local command, params
|
2017-01-15 23:12:13 +01:00
|
|
|
command, endi = yc.parse_get_command(code, endi)
|
2012-08-08 13:26:32 +02:00
|
|
|
if command == nil then return nil end
|
2012-08-10 10:19:33 +02:00
|
|
|
if command == true then break end --end of code
|
2012-08-08 13:26:32 +02:00
|
|
|
if command == "if" then
|
2014-11-22 15:42:22 +01:00
|
|
|
local r
|
2017-01-15 23:12:13 +01:00
|
|
|
r, endi = yc.command_if(code, endi, yc.merge_portstates(Lreal, Lvirtual), eeprom)
|
2012-08-08 13:26:32 +02:00
|
|
|
if r == nil then return nil end
|
2012-08-09 13:34:10 +02:00
|
|
|
if r == true then -- nothing
|
2012-08-08 13:26:32 +02:00
|
|
|
elseif r == false then
|
2017-01-15 23:12:13 +01:00
|
|
|
local endi_new = yc.skip_to_else (code, endi)
|
2012-08-10 10:19:33 +02:00
|
|
|
if endi_new == nil then --else > not found
|
2017-01-15 23:12:13 +01:00
|
|
|
endi = yc.skip_to_endif(code, endi)
|
2012-08-10 10:19:33 +02:00
|
|
|
else
|
|
|
|
endi = endi_new
|
|
|
|
end
|
2012-08-08 13:26:32 +02:00
|
|
|
if endi == nil then return nil end
|
|
|
|
end
|
|
|
|
else
|
2017-01-15 23:12:13 +01:00
|
|
|
params, endi = yc.parse_get_params(code, endi)
|
2014-11-22 15:42:22 +01:00
|
|
|
if not params then return nil end
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
if command == "on" then
|
2022-02-12 20:12:12 +01:00
|
|
|
Lvirtual = yc.command_on (params, Lvirtual)
|
2012-08-08 13:26:32 +02:00
|
|
|
elseif command == "off" then
|
2022-02-12 20:12:12 +01:00
|
|
|
Lvirtual = yc.command_off(params, Lvirtual)
|
2012-08-10 10:47:40 +02:00
|
|
|
elseif command == "print" then
|
2017-01-15 23:12:13 +01:00
|
|
|
local su = yc.command_print(params, eeprom, yc.merge_portstates(Lreal, Lvirtual))
|
2012-08-10 10:47:40 +02:00
|
|
|
if su ~= true then return nil end
|
2012-08-13 22:33:25 +02:00
|
|
|
elseif command == "after" then
|
2017-01-15 23:12:13 +01:00
|
|
|
local su = yc.command_after(params, pos)
|
2012-08-13 22:33:25 +02:00
|
|
|
if su == nil then return nil end
|
2012-08-08 23:35:36 +02:00
|
|
|
elseif command == "sbi" then
|
2014-11-22 15:42:22 +01:00
|
|
|
local new_eeprom
|
2017-01-15 23:12:13 +01:00
|
|
|
new_eeprom, Lvirtual = yc.command_sbi (params, eeprom, yc.merge_portstates(Lreal, Lvirtual), Lvirtual)
|
2012-08-09 09:03:22 +02:00
|
|
|
if new_eeprom == nil then return nil
|
|
|
|
else eeprom = new_eeprom end
|
2012-08-08 13:26:32 +02:00
|
|
|
elseif command == "if" then --nothing
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2012-08-09 13:34:10 +02:00
|
|
|
if Lvirtual == nil then return nil end
|
2012-08-08 23:35:36 +02:00
|
|
|
if eeprom == nil then return nil else
|
2013-12-01 04:13:00 +01:00
|
|
|
minetest.get_meta(pos):set_string("eeprom", eeprom) end
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.action(pos, Lvirtual)
|
2012-08-08 13:26:32 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.parse_get_command = function(code, starti)
|
2018-07-23 13:00:59 +02:00
|
|
|
local i = starti
|
2014-11-22 15:42:22 +01:00
|
|
|
local s
|
2012-08-08 13:26:32 +02:00
|
|
|
while s ~= "" do
|
|
|
|
s = string.sub(code, i, i)
|
2012-08-10 10:19:33 +02:00
|
|
|
if s == "(" then
|
|
|
|
return string.sub(code, starti, i-1), i + 1 -- i: ( i+1 after (
|
|
|
|
end
|
2015-10-04 13:30:34 +02:00
|
|
|
if s == ";" and starti == i then
|
2012-08-08 13:26:32 +02:00
|
|
|
starti = starti + 1
|
|
|
|
i = starti
|
2012-08-10 10:19:33 +02:00
|
|
|
elseif s == ">" then
|
2017-01-15 23:12:13 +01:00
|
|
|
starti = yc.skip_to_endif(code, starti)
|
2012-08-10 10:19:33 +02:00
|
|
|
if starti == nil then return nil end
|
|
|
|
i = starti
|
|
|
|
else
|
|
|
|
i = i + 1
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-10 10:19:33 +02:00
|
|
|
|
2012-08-08 13:26:32 +02:00
|
|
|
if starti == i-1 then
|
|
|
|
return true, true
|
|
|
|
end
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.parse_get_params = function(code, starti)
|
2018-07-23 13:00:59 +02:00
|
|
|
local i = starti
|
2014-11-22 15:42:22 +01:00
|
|
|
local s
|
2012-08-08 13:26:32 +02:00
|
|
|
local params = {}
|
2012-08-10 10:47:40 +02:00
|
|
|
local is_string = false
|
2012-08-08 13:26:32 +02:00
|
|
|
while s ~= "" do
|
|
|
|
s = string.sub(code, i, i)
|
2012-08-10 10:47:40 +02:00
|
|
|
if code:sub(i, i) == '"' then
|
|
|
|
is_string = (is_string==false) --toggle is_string
|
|
|
|
end
|
|
|
|
if s == ")" and is_string == false then
|
2012-08-08 13:26:32 +02:00
|
|
|
table.insert(params, string.sub(code, starti, i-1)) -- i: ) i+1 after )
|
|
|
|
return params, i + 1
|
|
|
|
end
|
2012-08-10 10:47:40 +02:00
|
|
|
if s == "," and is_string == false then
|
2012-08-08 13:26:32 +02:00
|
|
|
table.insert(params, string.sub(code, starti, i-1)) -- i: ) i+1 after )
|
|
|
|
starti = i + 1
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.parse_get_eeprom_param = function(cond, starti)
|
2018-07-23 13:00:59 +02:00
|
|
|
local i = starti
|
2014-11-22 15:42:22 +01:00
|
|
|
local s
|
2012-08-09 08:06:23 +02:00
|
|
|
local addr
|
2012-08-08 23:35:36 +02:00
|
|
|
while s ~= "" do
|
2012-08-09 08:06:23 +02:00
|
|
|
s = string.sub(cond, i, i)
|
2022-08-01 17:27:05 +02:00
|
|
|
local b = s:byte()
|
|
|
|
if s == "" or 48 > b or b > 57 then
|
2012-08-09 08:06:23 +02:00
|
|
|
addr = string.sub(cond, starti, i-1) -- i: last number i+1 after last number
|
|
|
|
return addr, i
|
2012-08-08 23:35:36 +02:00
|
|
|
end
|
|
|
|
if s == "," then return nil, nil end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.skip_to_endif = function(code, starti)
|
2012-08-09 21:40:25 +02:00
|
|
|
local i = starti
|
|
|
|
local s = false
|
2012-08-10 10:19:33 +02:00
|
|
|
local open_ifs = 1
|
|
|
|
while s ~= nil and s~= "" do
|
|
|
|
s = code:sub(i, i)
|
|
|
|
if s == "i" and code:sub(i+1, i+1) == "f" then --if in µCScript
|
|
|
|
open_ifs = open_ifs + 1
|
|
|
|
end
|
|
|
|
if s == ";" then
|
|
|
|
open_ifs = open_ifs - 1
|
|
|
|
end
|
|
|
|
if open_ifs == 0 then
|
|
|
|
return i + 1
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.skip_to_else = function(code, starti)
|
2012-08-10 10:19:33 +02:00
|
|
|
local i = starti
|
|
|
|
local s = false
|
|
|
|
local open_ifs = 1
|
2012-08-09 21:40:25 +02:00
|
|
|
while s ~= nil and s~= "" do
|
|
|
|
s = code:sub(i, i)
|
2012-08-10 10:19:33 +02:00
|
|
|
if s == "i" and code:sub(i+1, i+1) == "f" then --if in µCScript
|
|
|
|
open_ifs = open_ifs + 1
|
|
|
|
end
|
2012-08-09 21:40:25 +02:00
|
|
|
if s == ";" then
|
2012-08-10 10:19:33 +02:00
|
|
|
open_ifs = open_ifs - 1
|
|
|
|
end
|
|
|
|
if open_ifs == 1 and s == ">" then
|
2012-08-09 21:40:25 +02:00
|
|
|
return i + 1
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--Commands
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_on = function(params, L)
|
2012-08-08 13:26:32 +02:00
|
|
|
for i, port in ipairs(params) do
|
2017-01-15 23:12:13 +01:00
|
|
|
L = yc.set_portstate (port, true, L)
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
return L
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_off = function(params, L)
|
2012-08-08 13:26:32 +02:00
|
|
|
for i, port in ipairs(params) do
|
2017-01-15 23:12:13 +01:00
|
|
|
L = yc.set_portstate (port, false, L)
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
return L
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_print = function(params, eeprom, L)
|
2012-08-10 10:47:40 +02:00
|
|
|
local s = ""
|
|
|
|
for i, param in ipairs(params) do
|
|
|
|
if param:sub(1,1) == '"' and param:sub(#param, #param) == '"' then
|
|
|
|
s = s..param:sub(2, #param-1)
|
|
|
|
else
|
2022-02-12 20:12:12 +01:00
|
|
|
local r = yc.command_parsecondition(param, L, eeprom)
|
2012-08-10 10:47:40 +02:00
|
|
|
if r == "1" or r == "0" then
|
|
|
|
s = s..r
|
|
|
|
else return nil end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print(s) --don't remove
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_sbi = function(params, eeprom, L, Lv)
|
2012-08-10 09:44:49 +02:00
|
|
|
if params[1]==nil or params[2]==nil or params[3] ~=nil then return nil end
|
2017-01-15 23:12:13 +01:00
|
|
|
local status = yc.command_parsecondition(params[2], L, eeprom)
|
2012-08-10 09:44:49 +02:00
|
|
|
|
|
|
|
if status == nil then return nil, nil end
|
|
|
|
|
2022-08-01 17:27:05 +02:00
|
|
|
if #params[1]==1 then
|
|
|
|
local b = params[1]:byte()
|
|
|
|
if 65 <= b and b <= 68 then -- is a port
|
|
|
|
if status == "1" then
|
|
|
|
Lv = yc.set_portstate (params[1], true, Lv)
|
|
|
|
else
|
|
|
|
Lv = yc.set_portstate (params[1], false, Lv)
|
|
|
|
end
|
|
|
|
return eeprom, Lv;
|
2012-08-10 09:44:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--is an eeprom address
|
2014-11-22 15:42:22 +01:00
|
|
|
local new_eeprom = "";
|
2012-08-08 23:35:36 +02:00
|
|
|
for i=1, #eeprom do
|
2015-10-04 13:30:34 +02:00
|
|
|
if tonumber(params[1])==i then
|
2012-08-09 09:03:22 +02:00
|
|
|
new_eeprom = new_eeprom..status
|
2012-08-08 23:35:36 +02:00
|
|
|
else
|
|
|
|
new_eeprom = new_eeprom..eeprom:sub(i, i)
|
|
|
|
end
|
|
|
|
end
|
2012-08-10 09:44:49 +02:00
|
|
|
return new_eeprom, Lv
|
2012-08-08 23:35:36 +02:00
|
|
|
end
|
|
|
|
|
2012-08-13 22:33:25 +02:00
|
|
|
-- after (delay)
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_after = function(params, pos)
|
2012-08-13 22:33:25 +02:00
|
|
|
if params[1] == nil or params[2] == nil or params[3] ~= nil then return nil end
|
|
|
|
|
|
|
|
--get time (maximum time is 200)
|
|
|
|
local time = tonumber(params[1])
|
|
|
|
if time == nil or time > 200 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--get code in quotes "code"
|
|
|
|
if string.sub(params[2], 1, 1) ~= '"' or string.sub(params[2], #params[2], #params[2]) ~= '"' then return nil end
|
|
|
|
local code = string.sub(params[2], 2, #params[2] - 1)
|
|
|
|
|
2012-08-13 22:55:14 +02:00
|
|
|
local afterid = math.random(10000)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
2012-08-13 22:55:14 +02:00
|
|
|
meta:set_int("afterid", afterid)
|
2017-01-15 23:12:13 +01:00
|
|
|
minetest.after(time, yc.command_after_execute, {pos = pos, code = code, afterid = afterid})
|
2012-08-13 22:33:25 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_after_execute = function(params)
|
2013-12-01 04:13:00 +01:00
|
|
|
local meta = minetest.get_meta(params.pos)
|
2012-08-13 22:55:14 +02:00
|
|
|
if meta:get_int("afterid") == params.afterid then --make sure the node has not been changed
|
2017-01-15 23:12:13 +01:00
|
|
|
if yc.parsecode(params.code, params.pos) == nil then
|
2012-08-13 23:20:29 +02:00
|
|
|
meta:set_string("infotext", "Code in after() not valid!")
|
|
|
|
else
|
2022-02-12 20:12:12 +01:00
|
|
|
if params.code ~= nil then
|
|
|
|
meta:set_string("infotext", "Working Microcontroller\n"..params.code)
|
2012-09-01 23:42:42 +02:00
|
|
|
else
|
|
|
|
meta:set_string("infotext", "Working Microcontroller")
|
|
|
|
end
|
2012-08-13 23:20:29 +02:00
|
|
|
end
|
2012-08-13 22:33:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-09 21:40:25 +02:00
|
|
|
--If
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_if = function(code, starti, L, eeprom)
|
|
|
|
local cond, endi = yc.command_if_getcondition(code, starti)
|
2012-08-08 13:26:32 +02:00
|
|
|
if cond == nil then return nil end
|
2012-08-08 15:09:05 +02:00
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
cond = yc.command_parsecondition(cond, L, eeprom)
|
2012-08-08 15:09:05 +02:00
|
|
|
|
2014-11-22 15:42:22 +01:00
|
|
|
local result
|
2012-08-08 15:09:05 +02:00
|
|
|
if cond == "0" then result = false
|
2014-11-22 15:42:22 +01:00
|
|
|
elseif cond == "1" then result = true end
|
|
|
|
if not result then end
|
2017-01-15 23:12:13 +01:00
|
|
|
return result, endi --endi from local cond, endi = yc.command_if_getcondition(code, starti)
|
2012-08-08 15:09:05 +02:00
|
|
|
end
|
|
|
|
|
2012-08-09 21:40:25 +02:00
|
|
|
--Condition parsing
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_if_getcondition = function(code, starti)
|
2018-07-23 13:00:59 +02:00
|
|
|
local i = starti
|
2014-11-22 15:42:22 +01:00
|
|
|
local s
|
2012-08-08 23:35:36 +02:00
|
|
|
local brackets = 1 --1 Bracket to close
|
2012-08-08 15:09:05 +02:00
|
|
|
while s ~= "" do
|
|
|
|
s = string.sub(code, i, i)
|
2015-10-04 13:30:34 +02:00
|
|
|
|
2012-08-08 15:09:05 +02:00
|
|
|
if s == ")" then
|
2012-08-08 23:35:36 +02:00
|
|
|
brackets = brackets - 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if s == "(" then
|
|
|
|
brackets = brackets + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if brackets == 0 then
|
|
|
|
return string.sub(code, starti, i-1), i + 1 -- i: ( i+1 after (
|
2012-08-08 15:09:05 +02:00
|
|
|
end
|
2012-08-08 23:35:36 +02:00
|
|
|
|
2012-08-08 15:09:05 +02:00
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
return nil, nil
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.command_parsecondition = function(cond, L, eeprom)
|
2012-08-09 12:54:31 +02:00
|
|
|
cond = string.gsub(cond, "A", tonumber(L.a and 1 or 0))
|
2012-08-08 13:26:32 +02:00
|
|
|
cond = string.gsub(cond, "B", tonumber(L.b and 1 or 0))
|
|
|
|
cond = string.gsub(cond, "C", tonumber(L.c and 1 or 0))
|
|
|
|
cond = string.gsub(cond, "D", tonumber(L.d and 1 or 0))
|
|
|
|
|
2012-08-09 16:25:30 +02:00
|
|
|
|
2012-08-08 23:35:36 +02:00
|
|
|
local i = 1
|
|
|
|
local l = string.len(cond)
|
|
|
|
while i<=l do
|
|
|
|
local s = cond:sub(i,i)
|
2012-08-09 08:06:23 +02:00
|
|
|
if s == "#" then
|
2017-01-15 23:12:13 +01:00
|
|
|
local addr, endi = yc.parse_get_eeprom_param(cond, i+1)
|
|
|
|
local buf = yc.eeprom_read(tonumber(addr), eeprom)
|
2012-08-08 23:35:36 +02:00
|
|
|
if buf == nil then return nil end
|
|
|
|
local call = cond:sub(i, endi-1)
|
|
|
|
cond = string.gsub(cond, call, buf)
|
|
|
|
i = 0
|
|
|
|
l = string.len(cond)
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
2012-08-09 09:05:00 +02:00
|
|
|
|
2012-08-09 08:06:23 +02:00
|
|
|
cond = string.gsub(cond, "!0", "1")
|
|
|
|
cond = string.gsub(cond, "!1", "0")
|
2012-08-09 09:05:00 +02:00
|
|
|
|
2022-02-12 20:12:12 +01:00
|
|
|
i = 2
|
|
|
|
l = string.len(cond)
|
2012-08-08 15:09:05 +02:00
|
|
|
while i<=l do
|
|
|
|
local s = cond:sub(i,i)
|
|
|
|
local b = tonumber(cond:sub(i-1, i-1))
|
|
|
|
local a = tonumber(cond:sub(i+1, i+1))
|
2012-08-08 20:13:07 +02:00
|
|
|
if cond:sub(i+1, i+1) == nil then break end
|
2012-08-08 15:09:05 +02:00
|
|
|
if s == "=" then
|
2012-08-09 08:06:23 +02:00
|
|
|
if a==nil then return nil end
|
2012-09-01 23:28:04 +02:00
|
|
|
if b==nil then return nil end
|
2022-02-12 20:12:12 +01:00
|
|
|
local buf = a == b and "1" or "0"
|
2012-08-08 15:33:34 +02:00
|
|
|
cond = string.gsub(cond, b..s..a, buf)
|
2012-08-08 15:09:05 +02:00
|
|
|
i = 1
|
|
|
|
l = string.len(cond)
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
2012-08-09 09:05:00 +02:00
|
|
|
|
2022-02-12 20:12:12 +01:00
|
|
|
i = 2
|
|
|
|
l = string.len(cond)
|
2012-08-08 13:26:32 +02:00
|
|
|
while i<=l do
|
|
|
|
local s = cond:sub(i,i)
|
|
|
|
local b = tonumber(cond:sub(i-1, i-1))
|
|
|
|
local a = tonumber(cond:sub(i+1, i+1))
|
2012-08-08 20:13:07 +02:00
|
|
|
if cond:sub(i+1, i+1) == nil then break end
|
2012-08-08 13:26:32 +02:00
|
|
|
if s == "&" then
|
2012-08-09 08:06:23 +02:00
|
|
|
if a==nil then return nil end
|
2018-11-06 09:48:44 +01:00
|
|
|
if b==nil then return nil end
|
2012-08-08 13:26:32 +02:00
|
|
|
local buf = ((a==1) and (b==1))
|
|
|
|
if buf == true then buf = "1" end
|
|
|
|
if buf == false then buf = "0" end
|
|
|
|
cond = string.gsub(cond, b..s..a, buf)
|
|
|
|
i = 1
|
|
|
|
l = string.len(cond)
|
|
|
|
end
|
|
|
|
if s == "|" then
|
2012-08-09 08:06:23 +02:00
|
|
|
if a==nil then return nil end
|
2018-11-06 09:48:44 +01:00
|
|
|
if b==nil then return nil end
|
2012-08-08 13:26:32 +02:00
|
|
|
local buf = ((a == 1) or (b == 1))
|
|
|
|
if buf == true then buf = "1" end
|
|
|
|
if buf == false then buf = "0" end
|
|
|
|
cond = string.gsub(cond, b..s..a, buf)
|
|
|
|
i = 1
|
|
|
|
l = string.len(cond)
|
|
|
|
end
|
|
|
|
if s == "~" then
|
2012-08-09 08:06:23 +02:00
|
|
|
if a==nil then return nil end
|
2018-11-06 09:48:44 +01:00
|
|
|
if b==nil then return nil end
|
2012-08-08 13:26:32 +02:00
|
|
|
local buf = (((a == 1) or (b == 1)) and not((a==1) and (b==1)))
|
|
|
|
if buf == true then buf = "1" end
|
|
|
|
if buf == false then buf = "0" end
|
|
|
|
cond = string.gsub(cond, b..s..a, buf)
|
|
|
|
i = 1
|
|
|
|
l = string.len(cond)
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
2012-08-09 09:05:00 +02:00
|
|
|
|
2012-08-08 15:09:05 +02:00
|
|
|
return cond
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
|
2012-08-09 21:40:25 +02:00
|
|
|
--Virtual-Hardware functions
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.eeprom_read = function(number, eeprom)
|
2014-11-22 15:42:22 +01:00
|
|
|
if not number then return end
|
|
|
|
return eeprom:sub(number, number)
|
2012-08-08 23:35:36 +02:00
|
|
|
end
|
|
|
|
|
2012-08-09 21:40:25 +02:00
|
|
|
--Real I/O functions
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.action = function(pos, L) --L-->Lvirtual
|
|
|
|
local Lv = yc.get_virtual_portstates(pos)
|
2012-08-09 12:54:31 +02:00
|
|
|
local name = "mesecons_microcontroller:microcontroller"
|
|
|
|
..tonumber(L.d and 1 or 0)
|
|
|
|
..tonumber(L.c and 1 or 0)
|
|
|
|
..tonumber(L.b and 1 or 0)
|
|
|
|
..tonumber(L.a and 1 or 0)
|
2013-12-01 02:20:01 +01:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
minetest.swap_node(pos, {name = name, param2 = node.param2})
|
2012-08-09 16:25:30 +02:00
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.action_setports(pos, L, Lv)
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.action_setports = function(pos, L, Lv)
|
2012-08-09 16:25:30 +02:00
|
|
|
local name = "mesecons_microcontroller:microcontroller"
|
2012-08-09 21:40:25 +02:00
|
|
|
local rules
|
|
|
|
if Lv.a ~= L.a then
|
2014-11-22 11:37:47 +01:00
|
|
|
rules = microc_rules[name.."0001"]
|
2014-11-22 15:42:22 +01:00
|
|
|
if L.a == true then mesecon.receptor_on(pos, rules)
|
|
|
|
else mesecon.receptor_off(pos, rules) end
|
2012-08-09 21:40:25 +02:00
|
|
|
end
|
2012-09-08 15:10:31 +02:00
|
|
|
if Lv.b ~= L.b then
|
2014-11-22 11:37:47 +01:00
|
|
|
rules = microc_rules[name.."0010"]
|
2014-11-22 15:42:22 +01:00
|
|
|
if L.b == true then mesecon.receptor_on(pos, rules)
|
|
|
|
else mesecon.receptor_off(pos, rules) end
|
2012-08-09 21:40:25 +02:00
|
|
|
end
|
2015-10-04 13:30:34 +02:00
|
|
|
if Lv.c ~= L.c then
|
2014-11-22 11:37:47 +01:00
|
|
|
rules = microc_rules[name.."0100"]
|
2014-11-22 15:42:22 +01:00
|
|
|
if L.c == true then mesecon.receptor_on(pos, rules)
|
|
|
|
else mesecon.receptor_off(pos, rules) end
|
2012-08-09 21:40:25 +02:00
|
|
|
end
|
2015-10-04 13:30:34 +02:00
|
|
|
if Lv.d ~= L.d then
|
2014-11-22 11:37:47 +01:00
|
|
|
rules = microc_rules[name.."1000"]
|
2014-11-22 15:42:22 +01:00
|
|
|
if L.d == true then mesecon.receptor_on(pos, rules)
|
|
|
|
else mesecon.receptor_off(pos, rules) end
|
2012-08-08 13:26:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.set_portstate = function(port, state, L)
|
2012-08-08 13:26:32 +02:00
|
|
|
if port == "A" then L.a = state
|
|
|
|
elseif port == "B" then L.b = state
|
|
|
|
elseif port == "C" then L.c = state
|
|
|
|
elseif port == "D" then L.d = state
|
|
|
|
else return nil end
|
|
|
|
return L
|
|
|
|
end
|
|
|
|
|
2022-02-27 21:12:37 +01:00
|
|
|
-- Updates the real port states according to the signal change.
|
|
|
|
-- Returns whether the real port states actually changed.
|
2022-02-12 20:12:12 +01:00
|
|
|
yc.update_real_portstates = function(pos, _, rulename, newstate)
|
2014-01-10 16:38:02 +01:00
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if rulename == nil then
|
|
|
|
meta:set_int("real_portstates", 1)
|
2022-02-27 21:12:37 +01:00
|
|
|
return true
|
2014-01-10 16:38:02 +01:00
|
|
|
end
|
2022-02-27 21:12:37 +01:00
|
|
|
local real_portstates = meta:get_int("real_portstates")
|
|
|
|
local n = real_portstates - 1
|
2014-01-10 16:38:02 +01:00
|
|
|
local L = {}
|
|
|
|
for i = 1, 4 do
|
|
|
|
L[i] = n%2
|
|
|
|
n = math.floor(n/2)
|
|
|
|
end
|
|
|
|
if rulename.x == nil then
|
|
|
|
for _, rname in ipairs(rulename) do
|
|
|
|
local port = ({4, 1, nil, 3, 2})[rname.x+2*rname.z+3]
|
|
|
|
L[port] = (newstate == "on") and 1 or 0
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local port = ({4, 1, nil, 3, 2})[rulename.x+2*rulename.z+3]
|
|
|
|
L[port] = (newstate == "on") and 1 or 0
|
|
|
|
end
|
2022-02-27 21:12:37 +01:00
|
|
|
local new_portstates = 1 + L[1] + 2*L[2] + 4*L[3] + 8*L[4]
|
|
|
|
if new_portstates ~= real_portstates then
|
|
|
|
meta:set_int("real_portstates", new_portstates)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2014-01-10 16:38:02 +01:00
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.get_real_portstates = function(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
|
|
|
|
for _, index in ipairs({"a", "b", "c", "d"}) do
|
|
|
|
L[index] = ((n%2) == 1)
|
|
|
|
n = math.floor(n/2)
|
|
|
|
end
|
2012-08-08 13:26:32 +02:00
|
|
|
return L
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.get_virtual_portstates = function(pos) -- portstates according to the name
|
2014-11-22 15:42:22 +01:00
|
|
|
local name = minetest.get_node(pos).name
|
2022-02-12 20:12:12 +01:00
|
|
|
local _, a = string.find(name, ":microcontroller")
|
2012-08-09 12:54:31 +02:00
|
|
|
if a == nil then return nil end
|
|
|
|
a = a + 1
|
|
|
|
|
2014-11-22 15:42:22 +01:00
|
|
|
local Lvirtual = {a=false, b=false, c=false, d=false}
|
2012-08-09 12:54:31 +02:00
|
|
|
if name:sub(a , a ) == "1" then Lvirtual.d = true end
|
|
|
|
if name:sub(a+1, a+1) == "1" then Lvirtual.c = true end
|
|
|
|
if name:sub(a+2, a+2) == "1" then Lvirtual.b = true end
|
2012-08-09 16:25:30 +02:00
|
|
|
if name:sub(a+3, a+3) == "1" then Lvirtual.a = true end
|
2012-08-09 12:54:31 +02:00
|
|
|
return Lvirtual
|
|
|
|
end
|
|
|
|
|
2017-01-15 23:12:13 +01:00
|
|
|
yc.merge_portstates = function(Lreal, Lvirtual)
|
2012-08-09 16:25:30 +02:00
|
|
|
local L = {a=false, b=false, c=false, d=false}
|
|
|
|
if Lvirtual.a or Lreal.a then L.a = true end
|
|
|
|
if Lvirtual.b or Lreal.b then L.b = true end
|
|
|
|
if Lvirtual.c or Lreal.c then L.c = true end
|
|
|
|
if Lvirtual.d or Lreal.d then L.d = true end
|
|
|
|
return L
|
2012-08-09 12:54:31 +02:00
|
|
|
end
|