Improve logic block

This commit is contained in:
Joachim Stolberg 2021-02-25 17:23:50 +01:00
parent 86ace1405c
commit ae6bdc93ac
2 changed files with 40 additions and 21 deletions

View File

@ -124,7 +124,8 @@ end
local function on_rightclick_off(pos, node, clicker) local function on_rightclick_off(pos, node, clicker)
local meta = M(pos) local meta = M(pos)
local numbers = meta:get_string("numbers") local numbers = meta:get_string("numbers")
if numbers ~= "" and numbers ~= nil then local cycle_time = meta:get_int("cycle_time") or 0
if numbers ~= "" and numbers ~= nil and cycle_time == 0 then
if meta:get_string("public") == "true" or if meta:get_string("public") == "true" or
clicker:get_player_name() == meta:get_string("owner") then clicker:get_player_name() == meta:get_string("owner") then
switch_off(pos) switch_off(pos)

View File

@ -32,10 +32,11 @@ local HELP = S("Send an 'on'/'off' command if the\nexpression becomes true.\n")
-- mem.io_tbl = { -- mem.io_tbl = {
-- i123 = true, -- "on" received -- i123 = true, -- "on" received
-- i124 = false, -- "off" received -- i124 = false, -- "off" received
-- o456 = false, -- last output val -- o456 = false, -- last output val
-- } -- }
local ValidSymbols = { local ValidSymbols = {
["me"] = true,
["and"] = true, ["and"] = true,
["or"] = true, ["or"] = true,
["true"] = true, ["true"] = true,
@ -74,17 +75,17 @@ local function check_expr(expr)
end end
local function check_num(num, player_name) local function check_num(num, player_name)
if not techage.check_numbers(num, player_name) then if num ~= "me" and not techage.check_numbers(num, player_name) then
return "Invalid node number '"..num.."'" return "Invalid node number '"..num.."'"
end end
end end
local function send(pos, num, val) local function send(pos, num, val)
local own_num = M(pos):get_string("node_number")
local nvm = techage.get_nvm(pos) local nvm = techage.get_nvm(pos)
nvm.own_num = nvm.own_num or M(pos):get_string("node_number")
nvm.io_tbl = nvm.io_tbl or {} nvm.io_tbl = nvm.io_tbl or {}
nvm.io_tbl["o" .. num] = val == "on" and true or false nvm.io_tbl["o" .. num] = val == "on" and true or false
techage.send_single(own_num, num, val) techage.send_single(nvm.own_num, num, val)
end end
local function check_syntax(line, owner, outp, expr) local function check_syntax(line, owner, outp, expr)
@ -115,6 +116,9 @@ local function get_code(pos, nvm)
local mem = techage.get_mem(pos) local mem = techage.get_mem(pos)
local tbl = {} local tbl = {}
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
nvm.own_num = nvm.own_num or M(pos):get_string("node_number")
nvm.io_tbl.send = send
nvm.io_tbl.pos = pos
for i = 1,NUM_RULES do for i = 1,NUM_RULES do
local outp = meta:get_string("outp" .. i) local outp = meta:get_string("outp" .. i)
@ -127,6 +131,8 @@ local function get_code(pos, nvm)
val = val == "true" and "on" or "off" val = val == "true" and "on" or "off"
-- add prefix 'i' to all numbers -- add prefix 'i' to all numbers
expr = string.gsub(expr, '([0-9]+)', "i%1") expr = string.gsub(expr, '([0-9]+)', "i%1")
expr = string.gsub(expr, 'me', "i" .. nvm.own_num)
outp = string.gsub(outp, 'me', nvm.own_num)
tbl[#tbl + 1] = "if "..expr.." then send(pos, "..outp..", '"..val.."') end" tbl[#tbl + 1] = "if "..expr.." then send(pos, "..outp..", '"..val.."') end"
else else
nvm.error = err nvm.error = err
@ -138,17 +144,17 @@ local function get_code(pos, nvm)
local str = table.concat(tbl, "\n") local str = table.concat(tbl, "\n")
local code = compile(nvm, str) local code = compile(nvm, str)
nvm.io_tbl.send = send
nvm.io_tbl.pos = pos
setfenv(code, nvm.io_tbl)
return code return code
end end
local function execute(pos) local function execute(pos)
local nvm = techage.get_nvm(pos) local nvm = techage.get_nvm(pos)
local mem = techage.get_mem(pos) local mem = techage.get_mem(pos)
nvm.own_num = nvm.own_num or M(pos):get_string("node_number")
nvm.io_tbl["i" .. nvm.own_num] = nvm.from_myself or false
mem.code = mem.code or get_code(pos, nvm) mem.code = mem.code or get_code(pos, nvm)
if mem.code then if mem.code then
setfenv(mem.code, nvm.io_tbl)
local res, _ = pcall(mem.code) local res, _ = pcall(mem.code)
if not res then if not res then
nvm.error = "Unknown runtime error" nvm.error = "Unknown runtime error"
@ -162,9 +168,13 @@ local function data(nvm)
for k,v in pairs(nvm.io_tbl) do for k,v in pairs(nvm.io_tbl) do
if k ~= "send" and k ~= "pos" then if k ~= "send" and k ~= "pos" then
if k:byte(1) == 105 then -- 'i' if k:byte(1) == 105 then -- 'i'
inp[#inp+1] = k:sub(2) .. " = " .. dump(v) local num = k:sub(2)
if num == nvm.own_num then num = "me" end
inp[#inp+1] = num .. " = " .. dump(v)
else else
outp[#outp+1] = k:sub(2) .. " = " .. dump(v) local num = k:sub(2)
if num == nvm.own_num then num = "me" end
outp[#outp+1] = num .. " = " .. dump(v)
end end
end end
end end
@ -194,7 +204,7 @@ local function rules(meta)
tbl[#tbl + 1] = "label[1.4," .. y2 .. ";=]" tbl[#tbl + 1] = "label[1.4," .. y2 .. ";=]"
tbl[#tbl + 1] = "dropdown[1.8," .. y3 .. ";1.6,1;val" .. i ..";,true,false;" .. val .. "]" tbl[#tbl + 1] = "dropdown[1.8," .. y3 .. ";1.6,1;val" .. i ..";,true,false;" .. val .. "]"
tbl[#tbl + 1] = "label[3.5," .. y2 .. ";if]" tbl[#tbl + 1] = "label[3.5," .. y2 .. ";if]"
tbl[#tbl + 1] = "field[4.2," .. y1 .. ";5,1;expr" .. i ..";;" .. expr .. "]" tbl[#tbl + 1] = "field[4.2," .. y1 .. ";5.6,1;expr" .. i ..";;" .. expr .. "]"
end end
return table.concat(tbl, "") return table.concat(tbl, "")
end end
@ -205,7 +215,7 @@ local function formspec(pos, meta)
err = minetest.formspec_escape(err) err = minetest.formspec_escape(err)
nvm.io_tbl = nvm.io_tbl or {} nvm.io_tbl = nvm.io_tbl or {}
local inputs, outputs = data(nvm) local inputs, outputs = data(nvm)
return "size[9.4,7.7]" .. return "size[10,7.7]" ..
"tabheader[0,0;tab;"..S("Rules") .. "," .. S("Help")..";1;;true]" .. "tabheader[0,0;tab;"..S("Rules") .. "," .. S("Help")..";1;;true]" ..
"container[0.4,0.1]" .. "container[0.4,0.1]" ..
rules(meta) .. rules(meta) ..
@ -217,13 +227,13 @@ local function formspec(pos, meta)
"label[0,5.7;" .. S("Syntax") .. ":]" .. "label[0,5.7;" .. S("Syntax") .. ":]" ..
"label[2,5.7;" .. err .. "]" .. "label[2,5.7;" .. err .. "]" ..
"button[1.5,7.0;3,1;update;" .. S("Update") .. "]" .. "button[1.5,7.0;3,1;update;" .. S("Update") .. "]" ..
"button[5,7.0;3,1;store;" .. S("Store") .. "]" "button[5.6,7.0;3,1;store;" .. S("Store") .. "]"
end end
local function formspec_help() local function formspec_help()
return "size[9.4,7.7]" .. return "size[10,7.7]" ..
"tabheader[0,0;tab;"..S("Rules") .. "," .. S("Help")..";2;;true]" .. "tabheader[0,0;tab;"..S("Rules") .. "," .. S("Help")..";2;;true]" ..
"textarea[0.3,0.3;9.3,8;;;"..minetest.formspec_escape(HELP).."]" "textarea[0.3,0.3;9.9,8;;;"..minetest.formspec_escape(HELP).."]"
end end
minetest.register_node("techage:ta3_logic2", { minetest.register_node("techage:ta3_logic2", {
@ -258,6 +268,9 @@ minetest.register_node("techage:ta3_logic2", {
meta:set_string("val" .. i, fields["val" .. i] or "") meta:set_string("val" .. i, fields["val" .. i] or "")
meta:set_string("expr" .. i, fields["expr" .. i] or "") meta:set_string("expr" .. i, fields["expr" .. i] or "")
end end
local nvm = techage.get_nvm(pos)
nvm.own_num = nvm.own_num or M(pos):get_string("node_number")
nvm.io_tbl = {["i" .. nvm.own_num] = false}
end end
if fields.tab == "2" then if fields.tab == "2" then
@ -309,16 +322,21 @@ minetest.register_craft({
techage.register_node({"techage:ta3_logic2"}, { techage.register_node({"techage:ta3_logic2"}, {
on_recv_message = function(pos, src, topic, payload) on_recv_message = function(pos, src, topic, payload)
local nvm = techage.get_nvm(pos) local nvm = techage.get_nvm(pos)
nvm.own_num = nvm.own_num or M(pos):get_string("node_number")
nvm.io_tbl = nvm.io_tbl or {} nvm.io_tbl = nvm.io_tbl or {}
if topic == "on" then if src ~= nvm.own_num then -- from other node
nvm.io_tbl["i" .. src] = true if topic == "on" then
elseif topic == "off" then nvm.io_tbl["i" .. src] = true
nvm.io_tbl["i" .. src] = false elseif topic == "off" then
nvm.io_tbl["i" .. src] = false
else
return "unsupported"
end
minetest.get_node_timer(pos):start(0.1)
else else
return "unsupported" nvm.from_myself = (topic == "on") and true or false
end end
minetest.get_node_timer(pos):start(0.1)
end, end,
}) })