Add NanoBasic to TA3 Terminal

This commit is contained in:
Joachim Stolberg 2024-12-29 20:20:01 +01:00
parent c61b8dd47a
commit abd2c4a0be
3 changed files with 68 additions and 55 deletions

View File

@ -95,21 +95,6 @@ local function get_number(pos, new)
end end
end end
local function not_protected(pos, placer_name, clicker_name)
local meta = minetest.get_meta(pos)
if meta then
if placer_name and not minetest.is_protected(pos, placer_name) then
if clicker_name == nil or placer_name == clicker_name then
return true
end
if not minetest.is_protected(pos, clicker_name) then
return true
end
end
end
return false
end
local function register_lbm(name, nodenames) local function register_lbm(name, nodenames)
minetest.register_lbm({ minetest.register_lbm({
label = "[TechAge] Node update", label = "[TechAge] Node update",
@ -351,10 +336,10 @@ end
-- Send message functions -- Send message functions
------------------------------------------------------------------- -------------------------------------------------------------------
function techage.not_protected(number, placer_name, clicker_name) function techage.not_protected(number, player_name)
local ninfo = NodeInfoCache[number] or update_nodeinfo(number) local ninfo = NodeInfoCache[number] or update_nodeinfo(number)
if ninfo and ninfo.pos then if ninfo and ninfo.pos then
return not_protected(ninfo.pos, placer_name, clicker_name) return not minetest.is_protected(ninfo.pos, player_name)
end end
return false return false
end end
@ -364,7 +349,7 @@ end
-- and the node is not protected for the given player_name. -- and the node is not protected for the given player_name.
function techage.check_number(number, placer_name) function techage.check_number(number, placer_name)
if number then if number then
if not techage.not_protected(number, placer_name, nil) then if not techage.not_protected(number, placer_name) then
return false return false
end end
return true return true
@ -378,7 +363,7 @@ end
function techage.check_numbers(numbers, placer_name) function techage.check_numbers(numbers, placer_name)
if numbers then if numbers then
for _,num in ipairs(string_split(numbers, " ")) do for _,num in ipairs(string_split(numbers, " ")) do
if not techage.not_protected(num, placer_name, nil) then if not techage.not_protected(num, placer_name) then
return false return false
end end
end end
@ -471,7 +456,11 @@ function techage.beduino_request_data(src, number, topic, payload)
local ndef = NodeDef[ninfo.name] local ndef = NodeDef[ninfo.name]
if ndef and ndef.on_beduino_request_data then if ndef and ndef.on_beduino_request_data then
techage_counting_hit() techage_counting_hit()
return ndef.on_beduino_request_data(ninfo.pos, src, topic, payload or {}) if topic == 128 then
return 0, techage.get_node_lvm(ninfo.pos).name
else
return ndef.on_beduino_request_data(ninfo.pos, src, topic, payload or {})
end
end end
end end
return 1, "" return 1, ""

View File

@ -110,12 +110,12 @@ local function input_panel(nvm, x, y)
end end
local function get_action(nvm, fields) local function get_action(nvm, fields)
local keys = {"Edit", "Save", "Renum", "Cancel", "Run", "Stop", "Continue", "List", "Enter"} local keys = {"Edit", "Save", "Renum", "Cancel", "Run", "Stop", "Continue", "List", "Enter", "smaller", "larger"}
nvm.status = nvm.status or "init" nvm.status = nvm.status or "init"
if nvm.status == "" then if nvm.status == "" then
nvm.status = "init" nvm.status = "init"
end end
--print("get_action", nvm.status, dump(fields)) print("get_action", nvm.status, dump(fields))
for _,key in ipairs(keys) do for _,key in ipairs(keys) do
if fields[key] and Actions[nvm.status] and Actions[nvm.status][key] then if fields[key] and Actions[nvm.status] and Actions[nvm.status][key] then
print("get_action", nvm.status, key) print("get_action", nvm.status, key)
@ -147,14 +147,21 @@ end
-- Lines have line numbers at the beginning, like: "10 PRINT "Hello World" -- Lines have line numbers at the beginning, like: "10 PRINT "Hello World"
-- This function sorts the lines by the line numbers -- This function sorts the lines by the line numbers
local function sort_lines(code) local function sort_lines(pos, nvm, code)
local lines = {} local lines = {}
local keys = {} local keys = {}
for line in code:gmatch("[^\r\n]+") do for line in code:gmatch("[^\r\n]+") do
local num = tonumber(line:match("^%s*(%d+)")) local num = tonumber(line:match("^%s*(%d+)"))
if num then if num then
if lines[num] then
nanobasic.print(pos, "Line number " .. num .. " is already used\n")
return
end
lines[num] = line lines[num] = line
keys[#keys + 1] = num keys[#keys + 1] = num
else
nanobasic.print(pos, "Line number missing in line '" .. line .. "'\n")
return
end end
end end
@ -200,32 +207,21 @@ local function replace_all_goto_refs(lines, new_nums)
end end
end end
local function renumber_lines(code) local function renumber_lines(pos, nvm, code)
local lines = {} local lines = {}
local keys = {}
local new_nums = {} local new_nums = {}
local num = 10 local num = 10
for line in code:gmatch("[^\r\n]+") do for line in code:gmatch("[^\r\n]+") do
local s = line:match("^%s*(%d+)") local s = line:match("^%s*(%d+)")
if s then if s then
lines[num] = line:sub(s:len() + 1) lines[#lines + 1] = num .. line:sub(s:len() + 1)
new_nums[s] = num new_nums[s] = num
keys[#keys + 1] = num
num = num + 10
else
lines[num] = line
keys[#keys + 1] = num
num = num + 10 num = num + 10
end end
end end
replace_all_goto_refs(lines, new_nums) replace_all_goto_refs(lines, new_nums)
return table.concat(lines, "\n")
local sorted = {}
for i,num in ipairs(keys) do
sorted[i] = num .. lines[num]
end
return table.concat(sorted, "\n")
end end
minetest.register_node("techage:basic_terminal", { minetest.register_node("techage:basic_terminal", {
@ -265,6 +261,8 @@ minetest.register_node("techage:basic_terminal", {
local text = poweron_message(pos) local text = poweron_message(pos)
nvm.trm_ttl = 0 nvm.trm_ttl = 0
nvm.status = "init" nvm.status = "init"
nvm.bttns = Buttons
nvm.input = ""
meta:set_int("public", 0) meta:set_int("public", 0)
meta:set_string("formspec", formspec(pos, text)) meta:set_string("formspec", formspec(pos, text))
if placer then if placer then
@ -283,7 +281,8 @@ minetest.register_node("techage:basic_terminal", {
fields.Enter = fields.Enter or fields.key_enter_field fields.Enter = fields.Enter or fields.key_enter_field
local action = get_action(nvm, fields) local action = get_action(nvm, fields)
local text = action(pos, nvm, fields) local text = action(pos, nvm, fields)
if text and text ~= "" then techage.set_activeformspec(pos, player)
if text then
meta:set_string("formspec", formspec(pos, text)) meta:set_string("formspec", formspec(pos, text))
end end
end end
@ -323,11 +322,12 @@ minetest.register_node("techage:basic_terminal", {
local text = nanobasic.get_screen_buffer(pos) local text = nanobasic.get_screen_buffer(pos)
M(pos):set_string("formspec", formspec(pos, text)) M(pos):set_string("formspec", formspec(pos, text))
elseif res >= nanobasic.NB_XFUNC then elseif res >= nanobasic.NB_XFUNC then
if Functions[res] then local res = Functions[res] and Functions[res](pos, nvm) or false
return Functions[res](pos, nvm) if techage.is_activeformspec(pos) then
local text = nanobasic.get_screen_buffer(pos)
M(pos):set_string("formspec", formspec(pos, text))
end end
print("Oops, error") return res
print(res, dump(Functions))
else else
print("res = ", res) print("res = ", res)
return false return false
@ -345,6 +345,7 @@ minetest.register_node("techage:basic_terminal", {
else else
text = nanobasic.get_screen_buffer(pos) or "" text = nanobasic.get_screen_buffer(pos) or ""
end end
techage.set_activeformspec(pos, clicker)
M(pos):set_string("formspec", formspec(pos, text)) M(pos):set_string("formspec", formspec(pos, text))
end, end,
@ -424,7 +425,7 @@ register_ext_function("cmd$", {nanobasic.NB_NUM, nanobasic.NB_STR, nanobasic.NB_
local num = tostring(nanobasic.pop_num(pos) or 0) local num = tostring(nanobasic.pop_num(pos) or 0)
local own_num = M(pos):get_string("node_number") local own_num = M(pos):get_string("node_number")
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
if techage.not_protected(num, owner) then if techage.not_protected(tostring(num), owner) then
techage.counting_add(owner, 1) techage.counting_add(owner, 1)
local resp = techage.send_single(own_num, num, cmnd, payload) local resp = techage.send_single(own_num, num, cmnd, payload)
if type(resp) == "string" then if type(resp) == "string" then
@ -446,7 +447,7 @@ register_ext_function("bcmd", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_
local num = nanobasic.pop_num(pos) or 0 local num = nanobasic.pop_num(pos) or 0
local own_num = M(pos):get_string("node_number") local own_num = M(pos):get_string("node_number")
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
if techage.not_protected(num, owner) then if techage.not_protected(tostring(num), owner) then
techage.counting_add(owner, 1) techage.counting_add(owner, 1)
local resp = techage.beduino_send_cmnd(own_num, num, cmnd, payload) local resp = techage.beduino_send_cmnd(own_num, num, cmnd, payload)
nanobasic.push_num(pos, resp) nanobasic.push_num(pos, resp)
@ -464,7 +465,8 @@ register_ext_function("breq", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_
local num = nanobasic.pop_num(pos) or 0 local num = nanobasic.pop_num(pos) or 0
local own_num = M(pos):get_string("node_number") local own_num = M(pos):get_string("node_number")
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
if techage.not_protected(num, owner) then --print("breq", own_num, num, cmnd, owner)
if techage.not_protected(tostring(num), owner) then
techage.counting_add(owner, 1) techage.counting_add(owner, 1)
local sts, resp = techage.beduino_request_data(own_num, num, cmnd, payload) local sts, resp = techage.beduino_request_data(own_num, num, cmnd, payload)
if type(resp) == "table" then if type(resp) == "table" then
@ -487,16 +489,19 @@ register_ext_function("breq$", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB
local num = nanobasic.pop_num(pos) or 0 local num = nanobasic.pop_num(pos) or 0
local own_num = M(pos):get_string("node_number") local own_num = M(pos):get_string("node_number")
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
if techage.not_protected(num, owner) then if techage.not_protected(tostring(num), owner) then
techage.counting_add(owner, 1) techage.counting_add(owner, 1)
local sts, resp = techage.beduino_request_data(own_num, num, cmnd, payload) local sts, resp = techage.beduino_request_data(own_num, num, cmnd, payload)
print("breq$", sts, dump(resp))
if type(resp) == "string" and sts == 0 then if type(resp) == "string" and sts == 0 then
nanobasic.push_str(pos, resp) nanobasic.push_str(pos, resp)
elseif sts > 0 then
nanobasic.push_str(pos, "<" .. tostring(sts) .. ">")
elseif type(resp) ~= "string" then elseif type(resp) ~= "string" then
nanobasic.push_str(pos, "<5>") nanobasic.push_str(pos, "<5>")
end end
else else
nanobasic.push_str(pos, "<" .. tostring(sts) .. ">") nanobasic.push_str(pos, "<4>")
end end
return true return true
end) end)
@ -516,12 +521,11 @@ register_ext_function("dputs", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB
local num = nanobasic.pop_num(pos) or 0 local num = nanobasic.pop_num(pos) or 0
local own_num = M(pos):get_string("node_number") local own_num = M(pos):get_string("node_number")
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
if techage.not_protected(num, owner) then if techage.not_protected(tostring(num), owner) then
techage.counting_add(owner, 1) techage.counting_add(owner, 1)
if row == 0 then -- add line if row == 0 then -- add line
techage.send_single(own_num, num, "add", text) techage.send_single(own_num, num, "add", text)
else else
print("dputs", row, text)
local payload = safer_lua.Store() local payload = safer_lua.Store()
payload.set("row", row) payload.set("row", row)
payload.set("str", text) payload.set("str", text)
@ -536,7 +540,7 @@ register_ext_function("dclr", {nanobasic.NB_NUM}, nanobasic.NB_NONE, function(po
local num = nanobasic.pop_num(pos) or 0 local num = nanobasic.pop_num(pos) or 0
local own_num = M(pos):get_string("node_number") local own_num = M(pos):get_string("node_number")
local owner = M(pos):get_string("owner") local owner = M(pos):get_string("owner")
if techage.not_protected(num, owner) then if techage.not_protected(tostring(num), owner) then
techage.counting_add(owner, 1) techage.counting_add(owner, 1)
techage.send_single(own_num, num, "clear", nil) techage.send_single(own_num, num, "clear", nil)
end end
@ -570,6 +574,7 @@ end)
-- str: iname(str: item_name) -- str: iname(str: item_name)
register_ext_function("iname$", {nanobasic.NB_STR}, nanobasic.NB_STR, function(pos, nvm) register_ext_function("iname$", {nanobasic.NB_STR}, nanobasic.NB_STR, function(pos, nvm)
local item_name = nanobasic.pop_str(pos) or "" local item_name = nanobasic.pop_str(pos) or ""
print("iname", item_name)
local item = minetest.registered_items[item_name] local item = minetest.registered_items[item_name]
if item and item.description then if item and item.description then
local s = minetest.get_translated_string("en", item.description) local s = minetest.get_translated_string("en", item.description)
@ -591,13 +596,29 @@ register_action({"init", "stopped", "error", "break"}, "Edit", function(pos, nvm
end) end)
register_action({"edit"}, "Save", function(pos, nvm, fields) register_action({"edit"}, "Save", function(pos, nvm, fields)
code = sort_lines(fields.code) local code = sort_lines(pos, nvm, fields.code)
if code == nil then
nvm.status = "error"
nvm.bttns = {"Edit", "", "", "", "", "Stop", "", ""}
nvm.input = ""
M(pos):set_string("code", fields.code)
return nanobasic.get_screen_buffer(pos) or ""
end
M(pos):set_string("code", code) M(pos):set_string("code", code)
return code return code
end) end)
register_action({"edit"}, "Renum", function(pos, nvm, fields) register_action({"edit"}, "Renum", function(pos, nvm, fields)
code = renumber_lines(fields.code) code = sort_lines(pos, nvm, fields.code)
if code == nil then
nvm.status = "error"
nvm.bttns = {"Edit", "", "", "", "", "Stop", "", ""}
nvm.input = ""
M(pos):set_string("code", fields.code)
return nanobasic.get_screen_buffer(pos) or ""
end
code = renumber_lines(pos, nvm, code)
print("Renum", code)
M(pos):set_string("code", code) M(pos):set_string("code", code)
return code return code
end) end)
@ -678,6 +699,7 @@ register_action({"stopped", "init"}, "Stop", function(pos, nvm, fields)
nanobasic.print(pos, "\nProgram stopped.\n") nanobasic.print(pos, "\nProgram stopped.\n")
nvm.bttns = Buttons nvm.bttns = Buttons
nvm.input = "" nvm.input = ""
nanobasic.clear_screen(pos)
return poweron_message(pos) return poweron_message(pos)
end) end)
@ -699,11 +721,13 @@ end)
register_action(States, "larger", function(pos, nvm, fields) register_action(States, "larger", function(pos, nvm, fields)
nvm.trm_text_size = math.min((nvm.trm_text_size or 0) + 1, 8) nvm.trm_text_size = math.min((nvm.trm_text_size or 0) + 1, 8)
print("larger", nvm.trm_text_size)
return fields.code or nanobasic.get_screen_buffer(pos) or "" return fields.code or nanobasic.get_screen_buffer(pos) or ""
end) end)
register_action(States, "smaller", function(pos, nvm, fields) register_action(States, "smaller", function(pos, nvm, fields)
nvm.trm_text_size = math.max((nvm.trm_text_size or 0) - 1, -8) nvm.trm_text_size = math.max((nvm.trm_text_size or 0) - 1, -8)
print("smaller", nvm.trm_text_size)
return fields.code or nanobasic.get_screen_buffer(pos) or "" return fields.code or nanobasic.get_screen_buffer(pos) or ""
end) end)

View File

@ -210,7 +210,7 @@ local function command(pos, command, player, is_ta4)
output(pos, "Techage version = " .. techage.version) output(pos, "Techage version = " .. techage.version)
elseif cmnd == "connect" and data then elseif cmnd == "connect" and data then
output(pos, "$ "..command) output(pos, "$ "..command)
if techage.not_protected(data, owner, owner) then if techage.not_protected(data, owner) then
local own_num = meta:get_string("node_number") local own_num = meta:get_string("node_number")
local resp = techage.send_single(own_num, data, cmnd) local resp = techage.send_single(own_num, data, cmnd)
if resp then if resp then
@ -231,7 +231,7 @@ local function command(pos, command, player, is_ta4)
num, cmnd, payload = command:match('^cmd%s+([0-9]+)%s+(%w+)%s*(.*)$') num, cmnd, payload = command:match('^cmd%s+([0-9]+)%s+(%w+)%s*(.*)$')
if num and cmnd then if num and cmnd then
if techage.not_protected(num, owner, owner) then if techage.not_protected(num, owner) then
local resp = techage.send_single(own_num, num, cmnd, payload) local resp = techage.send_single(own_num, num, cmnd, payload)
if type(resp) == "string" then if type(resp) == "string" then
output(pos, resp) output(pos, resp)
@ -244,7 +244,7 @@ local function command(pos, command, player, is_ta4)
num, cmnd = command:match('^turn%s+([0-9]+)%s+([onf]+)$') num, cmnd = command:match('^turn%s+([0-9]+)%s+([onf]+)$')
if num and (cmnd == "on" or cmnd == "off") then if num and (cmnd == "on" or cmnd == "off") then
if techage.not_protected(num, owner, owner) then if techage.not_protected(num, owner) then
local resp = techage.send_single(own_num, num, cmnd) local resp = techage.send_single(own_num, num, cmnd)
output(pos, dump(resp)) output(pos, dump(resp))
end end