techage/logic/lua_logic.lua

253 lines
6.4 KiB
Lua
Raw Normal View History

--[[
TechAge
=======
2020-01-31 10:55:10 -08:00
Copyright (C) 2017-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Lua Logic Block
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
-- mem.inp_tbl = {
-- n001 = true, -- "on" received
-- n002 = false, -- "off" received
-- inp = true, -- last input
-- outp = false, -- last output
-- }
local ValidSymbols = {
["if"] = true,
["then"] = true,
["else"] = true,
["end"] = true,
["return"] = true,
["not"] = true,
["and"] = true,
["or"] = true,
["inp"] = true,
["outp"] = true,
["true"] = true,
["false"] = true,
["nil"] = true,
["=="] = true,
["~="] = true,
["("] = true,
[")"] = true,
}
local function check(expression)
for _, sym in ipairs(string.split(expression, " ")) do
if not ValidSymbols[sym] and string.find(sym, '^[n0-9]+$') == nil then
return false, "Error: Invalid symbol '"..sym.."'"
end
end
return true, "ok"
end
2020-01-31 10:55:10 -08:00
local function compile(nvm, expression)
local res, err = check(expression)
if res then
local code, err = loadstring(expression, "")
if code then
2020-01-31 10:55:10 -08:00
nvm.code = code
nvm.error = "ok"
else
2020-01-31 10:55:10 -08:00
nvm.code = nil
nvm.error = err
end
else
2020-01-31 10:55:10 -08:00
nvm.code = nil
nvm.error = err
end
end
2020-01-31 10:55:10 -08:00
local function get_code(pos, nvm)
local meta = M(pos)
local if_expr = meta:get_string("if_expr") or ""
local then_expr = meta:get_string("then_expr") or ""
local else_expr = meta:get_string("else_expr") or ""
local expr = "if "..if_expr.." then return "..then_expr.." else return "..else_expr.." end"
2020-01-31 10:55:10 -08:00
compile(nvm, expr)
return nvm.code
end
2020-01-31 10:55:10 -08:00
local function eval(pos, nvm)
nvm.code = nvm.code or get_code(pos, nvm)
if nvm.code then
setfenv(nvm.code, nvm.inp_tbl)
local res, sts = pcall(nvm.code)
if res then
2020-01-31 10:55:10 -08:00
nvm.error = "ok"
if sts == true and nvm.inp_tbl.outp ~= true then
nvm.inp_tbl.outp = sts
return "on"
2020-01-31 10:55:10 -08:00
elseif sts == false and nvm.inp_tbl.outp ~= false then
nvm.inp_tbl.outp = sts
return "off"
end
else
2020-01-31 10:55:10 -08:00
nvm.error = "Error: "..sts
end
end
end
2020-01-31 10:55:10 -08:00
local function data(nvm)
local tbl = {"inp = "..dump(nvm.inp_tbl.inp), "outp = "..dump(nvm.inp_tbl.outp)}
for k,v in pairs(nvm.inp_tbl) do
if k ~= "inp" and k ~= "outp" then
tbl[#tbl+1] = k.." = "..dump(v)
end
end
return table.concat(tbl, ", ")
end
local function formspec(pos, meta)
2020-01-31 10:55:10 -08:00
local nvm = techage.get_nvm(pos)
local numbers = meta:get_string("numbers") or ""
local if_expr = meta:get_string("if_expr") or ""
local then_expr = meta:get_string("then_expr") or ""
local else_expr = meta:get_string("else_expr") or ""
2020-01-31 10:55:10 -08:00
local err = nvm.error or "ok"
if err ~= "ok" then
err = string.sub(err, 15)
end
err = minetest.formspec_escape(err)
2020-01-31 10:55:10 -08:00
nvm.inp_tbl = nvm.inp_tbl or {inp = false, outp = false}
local data = data(nvm)
return "size[9,8]"..
"background[0,0;9,1.3;techage_formspec_bg.png]"..
"field[0.5,0.2;8.5,2;numbers;"..S("Insert destination node number(s)")..";"..numbers.."]" ..
"label[0,1.4;Variables: "..data.."]"..
"label[0,2;Valid symbols: not and or true false nil == ~= ( )]"..
"background[0,2.6;9,4;techage_formspec_bg.png]"..
"label[0.1,2.8;if]"..
"field[0.8,2.9;7,1;if_expr;;"..if_expr.."]" ..
"label[7.6,2.8;then]"..
"label[0.6,3.8;return]"..
"field[2,3.9;7,1;then_expr;;"..then_expr.."]" ..
"label[0.1,4.5;else]"..
"label[0.6,5.2;return]"..
"field[2,5.3;7,1;else_expr;;"..else_expr.."]" ..
"label[0.1,6;end]"..
"label[0,6.8;Result: "..err.."]"..
"button[2,7.3;2.5,1;update;"..S("Update").."]"..
"button[5,7.3;2.5,1;store;"..S("Store").."]"
end
minetest.register_node("techage:ta3_logic", {
description = S("TA3 Logic Block"),
tiles = {
-- up, down, right, left, back, front
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3_top.png",
"techage_filling_ta3.png^techage_frame_ta3.png^techage_appl_logic.png",
},
after_place_node = function(pos, placer)
local meta = M(pos)
2020-01-31 10:55:10 -08:00
local nvm = techage.get_nvm(pos)
nvm.inp_tbl = {inp = false, outp = false}
logic.after_place_node(pos, placer, "techage:ta3_logic", S("TA3 Logic Block"))
logic.infotext(meta, S("TA3 Logic Block"))
meta:set_string("formspec", formspec(pos, meta))
end,
on_receive_fields = function(pos, formname, fields, player)
if minetest.is_protected(pos, player:get_player_name()) then
return
end
local meta = M(pos)
2020-01-31 10:55:10 -08:00
local nvm = techage.get_nvm(pos)
if fields.numbers and fields.numbers ~= "" then
if techage.check_numbers(fields.numbers, player:get_player_name()) then
meta:set_string("numbers", fields.numbers)
logic.infotext(M(pos), S("TA3 Logic Block"))
end
end
if fields.if_expr and fields.if_expr ~= "" then
meta:set_string("if_expr", fields.if_expr)
end
if fields.then_expr and fields.then_expr ~= "" then
meta:set_string("then_expr", fields.then_expr)
end
if fields.else_expr and fields.else_expr ~= "" then
meta:set_string("else_expr", fields.else_expr)
end
if fields.store then
2020-01-31 10:55:10 -08:00
get_code(pos, nvm)
end
meta:set_string("formspec", formspec(pos, meta))
end,
on_timer = function(pos,elapsed)
2020-01-31 10:55:10 -08:00
local nvm = techage.get_nvm(pos)
local topic = eval(pos, nvm)
if topic then
local meta = M(pos)
local own_num = meta:get_string("node_number") or ""
local numbers = meta:get_string("numbers") or ""
techage.send_multi(own_num, numbers, topic)
end
return false
end,
techage_set_numbers = function(pos, numbers, player_name)
local meta = M(pos)
local res = logic.set_numbers(pos, numbers, player_name, S("TA3 Logic Block"))
meta:set_string("formspec", formspec(pos, meta))
return res
end,
after_dig_node = function(pos)
techage.remove_node(pos)
2020-01-31 10:55:10 -08:00
techage.del_mem(pos)
end,
paramtype2 = "facedir",
groups = {choppy=2, cracky=2, crumbly=2},
is_ground_content = false,
sounds = default.node_sound_wood_defaults(),
})
minetest.register_craft({
output = "techage:ta3_logic",
recipe = {
{"", "group:wood", ""},
{"", "default:copper_ingot", "techage:vacuum_tube"},
{"", "group:wood", ""},
},
})
techage.register_node({"techage:ta3_logic"}, {
on_recv_message = function(pos, src, topic, payload)
2020-01-31 10:55:10 -08:00
local nvm = techage.get_nvm(pos)
nvm.inp_tbl = nvm.inp_tbl or {outp = false}
if topic == "on" then
2020-01-31 10:55:10 -08:00
nvm.inp_tbl.inp = true
nvm.inp_tbl["n"..src] = true
elseif topic == "off" then
2020-01-31 10:55:10 -08:00
nvm.inp_tbl.inp = false
nvm.inp_tbl["n"..src] = false
else
2019-08-29 12:43:00 -07:00
return "unsupported"
end
minetest.get_node_timer(pos):start(0.1)
end,
on_node_load = function(pos)
end,
})