Lua controller added

master
Joachim Stolberg 2020-03-14 23:02:13 +01:00
parent cf6410dcf6
commit d9ee957dad
21 changed files with 523 additions and 443 deletions

View File

@ -22,5 +22,6 @@ Highly recommended: signs_bot, hyperloop
- 2019-06-16 V0.01 * First upload
- 2019-09-28 V0.02 * TA3 finished
- 2020-02-29 V0.04 * TA4 ICTA controller added
- 2020-03-14 V0.05 * TA4 Lua controller added

View File

@ -214,7 +214,7 @@ techage.register_node({"techage:generator", "techage:generator_on"}, {
if topic == "trigger" then
nvm.firebox_trigger = 3
if nvm.running then
return math.max((nvm.provided or PWR_CAPA) / PWR_CAPA, 0.1)
return math.max((nvm.provided or PWR_CAPA) / PWR_CAPA, 0.02)
else
return 0
end

View File

@ -137,5 +137,9 @@ techage.Items = {
ta4_battery = "techage:ta4_battery",
ta4_display = "techage:ta4_display",
ta4_signaltower = "techage:ta4_signaltower",
ta4_lua_controller = "techage:ta4_lua_controller",
ta4_lua_server = "techage:ta4_server",
ta4_sensor_chest = "techage:ta4_sensor_chest",
ta4_terminal = "techage:ta4_terminal",
--ta4_ "",
}

View File

@ -146,6 +146,10 @@ techage.manual_DE.aTitel = {
"3,Batterie",
"3,TA4 Display",
"3,TA4 Signal Tower",
"2,TA4 Lua Controller",
"3,TA4 Lua Server",
"3,TA4 Sensor Kiste/Chest",
"3,TA4 Lua Controller Terminal",
"2,Weitere TA4 Blöcke",
"3,TA4 Tank / TA4 Tank",
"3,TA4 Pumpe / TA4 Pump",
@ -650,7 +654,7 @@ techage.manual_DE.aText = {
"\n"..
"Ein Tank kann auch von Hand gefüllt oder geleert werden\\, indem mit einem vollen oder leeren Flüssigkeitsbehälter (Fass\\, Kanister) auf den Tank geklickt wird. Dabei ist zu beachten\\, dass Fässer nur komplett gefüllt oder entleert werden können. Sind bspw. weniger als 10 Einheiten im Tank\\, muss dieser Rest mit Kanistern entnommen oder leergepumpt werden.\n"..
"\n"..
"In einen TA3 Tank passen 500 Einheiten oder 50 Fässer einer Flüssigkeit.\n"..
"In einen TA3 Tank passen 1000 Einheiten oder 100 Fässer einer Flüssigkeit.\n"..
"\n"..
"\n"..
"\n",
@ -714,7 +718,7 @@ techage.manual_DE.aText = {
"\n",
"Der Öltank ist die große Ausführung des TA3 Tanks (siehe Flüssigkeiten -> TA3 Tank).\n"..
"\n"..
"Der große Tank kann 2000 Einheiten Öl aufnehmen.\n"..
"Der große Tank kann 4000 Einheiten Öl aufnehmen.\n"..
"\n"..
"\n"..
"\n",
@ -1209,10 +1213,30 @@ techage.manual_DE.aText = {
"\n"..
"\n"..
"\n",
"Der Lua Controller muss\\, wie der Name schon sagt\\, in der Programmiersprache Lua programmiert werden. Außerdem sollte man etwas Englisch können (oder Google bemühen)\\, denn die Anleitung dazu gibt es nur in Englisch:\n"..
"\n"..
"https://github.com/joe7575/techage/blob/master/manuals/ta4_lua_controller_EN.md\n"..
"\n"..
"Auch der Lua Controller benötigt eine Batterie. Die Batterie muss in unmittelbarer Nähe zum Controller platziert werden\\, also an einer der 26 Positionen um den Controller herum.\n"..
"\n"..
"\n"..
"\n",
"Der Server dient zur zentralen Speicherung von Daten von mehreren Lua Controllern. Es speichert auch die Daten über einen Server-Neustart hinweg.\n"..
"\n"..
"\n"..
"\n",
"Die TA4 Sensor Kiste dient zum Aufbau von Automatischen Lagern oder Verkaufsautomaten. Sie hat erweitere Kommandos zur Fernsteuerung.\n"..
"\n"..
"\n"..
"\n",
"Das Terminal dient zur Ein-/Ausgabe für den Lua Controller.\n"..
"\n"..
"\n"..
"\n",
"",
"Siehe TA3 Tank.\n"..
"\n"..
"In einen TA4 Tank passen 1000 Einheiten oder 100 Fässer einer Flüssigkeit.\n"..
"In einen TA4 Tank passen 2000 Einheiten oder 200 Fässer einer Flüssigkeit.\n"..
"\n"..
"\n"..
"\n",
@ -1380,6 +1404,10 @@ techage.manual_DE.aItemName = {
"ta4_battery",
"ta4_display",
"ta4_signaltower",
"ta4_lua_controller",
"ta4_lua_server",
"ta4_sensor_chest",
"ta4_terminal",
"",
"ta4_tank",
"ta4_pump",
@ -1538,5 +1566,9 @@ techage.manual_DE.aPlanTable = {
"",
"",
"",
"",
"",
"",
"",
}

View File

@ -16,7 +16,7 @@
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
local BATTERY_CAPACITY = 5000000
local BATTERY_CAPACITY = 10000000
local function calc_percent(content)
local val = (BATTERY_CAPACITY - math.min(content or 0, BATTERY_CAPACITY))

View File

@ -108,7 +108,7 @@ local function add_line(meta, payload)
else
rows = string.split(text, "|")
end
if #rows >= NUM_ROWS then
while #rows >= NUM_ROWS do
table.remove(rows, 1)
end
table.insert(rows, payload)

View File

@ -1,132 +0,0 @@
--[[
TechAge
=======
Copyright (C) 2019-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
ICTA Controller - Stopwatch
Start/stop the watch with an on/off commands.
The player name clicking the stop is stored in addition.
]]--
-- for lazy programmers
local M = minetest.get_meta
local S = techage.S
local logic = techage.logic
local function retrieve_clicker_name(number)
local pos = techage.get_node_info(number).pos
if pos then
local meta = minetest.get_meta(pos)
return meta:get_string("clicker_name") or "<unknown>"
end
return "<error>"
end
-- env = {
-- event = <bool>,
-- last_event = <number> -- last event time
-- ticks = <number,
-- pos = <pos>,
-- timer = gen_table(8, 0),
-- blocked = gen_table(8, false),
-- result = gen_table(8, false),
-- condition = gen_table(8, false),
-- input = <table>, -- node number is key
-- number = <number>,
-- owner = <string>,
-- },
--
-- return cond_result, trigger_action
function techage.stopwatch(env, data)
if env.input[data.number] == "on" then
env.time = env.last_event
if not env.highscore then
env.highscore = 99999
end
return nil, false
else
local time = (env.last_event - env.time) / 1000000
local name = retrieve_clicker_name(data.number)
env.highscore = math.min(time, env.highscore)
local s1 = string.format("%2.1f s", time)
local s2 = string.format("%2.1f s", env.highscore)
env.stopwatch_result = {s1, s2, name}
return nil, true
end
end
techage.icta_register_condition("stopwatch", {
title = "stopwatch",
formspec = {
{
type = "number",
name = "number",
label = "Switch number",
default = "",
},
{
type = "label",
name = "lbl",
label = "Hint: Stop the time between switching on\nand switching off of the connected switch.",
},
},
code = function(data, environ)
return "techage.stopwatch"
end,
button = function(data, environ)
return 'stopwatch('..sl.fmt_number(data.number)..')'
end,
})
techage.icta_register_action("stopwatch", {
title = "stopwatch",
formspec = {
{
type = "number",
name = "number",
label = "Display number",
default = "",
},
{
type = "textlist",
name = "row",
label = "Display line",
choices = "1,2,3,4,5,6,7,8,9",
default = "1",
},
{
type = "ascii",
name = "text",
label = "label",
default = "",
},
{
type = "textlist",
name = "type",
label = "type",
choices = "time,highscore,name",
default = "time",
},
{
type = "label",
name = "lbl",
label = "Hint: Display number for the output\nof time, highscore and player name.",
},
},
button = function(data, environ)
return "lcd("..sl.fmt_number(data.number)..","..data.row..","..data.type..')'
end,
code = function(data, environ)
local idx = ({time=1, highscore= 2, name=3})[data.type]
local s1 = string.format('local payload = {row = %s, str = "%s "..env.stopwatch_result['..idx..']}', data.row, techage.escape(data.text))
local s2 = string.format('techage.send_single("%s", "%s", "row", payload)', environ.number, data.number)
return s1.."\n\t"..s2
end,
})

View File

@ -218,17 +218,15 @@ else
dofile(MP.."/icta_controller/commands.lua")
dofile(MP.."/icta_controller/edit.lua")
dofile(MP.."/icta_controller/battery.lua")
--dofile(MP.."/icta_controller/stopwatch.lua")
--dofile(MP.."/icta_controller/expression.lua")
dofile(MP.."/icta_controller/display.lua")
dofile(MP.."/icta_controller/signaltower.lua")
-- Lua Controller
-- dofile(MP.."/lua_controller/controller.lua")
-- dofile(MP.."/lua_controller/commands.lua")
-- dofile(MP.."/lua_controller/server.lua")
-- dofile(MP.."/lua_controller/sensorchest.lua")
-- dofile(MP.."/lua_controller/terminal.lua")
dofile(MP.."/lua_controller/controller.lua")
dofile(MP.."/lua_controller/commands.lua")
dofile(MP.."/lua_controller/server.lua")
dofile(MP.."/lua_controller/sensorchest.lua")
dofile(MP.."/lua_controller/terminal.lua")
-- Items
dofile(MP.."/items/barrel.lua")

View File

@ -230,7 +230,7 @@ minetest.register_craft({
output = "techage:ta3_tank 2",
recipe = {
{"techage:iron_ingot", "techage:ta3_barrel_empty", "group:wood"},
{"techage:tubeS", "techage:ta3_barrel_empty", "techage:ta3_pipeS"},
{"group:wood", "techage:ta3_barrel_empty", "techage:ta3_pipeS"},
{"group:wood", "techage:ta3_barrel_empty", "techage:iron_ingot"},
},
})

View File

@ -325,7 +325,7 @@ Water Barrel=Wasserfass
Water Boiler=Wasserboiler
Water Pump=Wasserpumpe
[Bucket] Lava can only be placed below sea level!=Lava kann nur unterhalb der Meerehöhe platziert werden!
[TA Oil] No oil exploration possible at this depth! = [TA Oil] Keine Ölsuche in dieser Tiefe möglich!
[TA Oil] No oil exploration possible at this depth! = [TA Oil] Keine Ölsuche in dieser Tiefe möglich!
[TA4 Wind Turbine]=[TA4 Windkraftanlage]
[TA] Area is protected!=[TA] Bereich ist geschützt
[TA] Derrick is being built!=[TA] Bohrturm wird errichtet

View File

@ -17,11 +17,14 @@
-- store protection data locally
local LocalRef = {}
local function not_protected(owner, numbers)
LocalRef[owner] = LocalRef[owner] or {}
if LocalRef[owner][numbers] == nil then
LocalRef[owner][numbers] = techage.check_numbers(numbers, owner)
if owner and numbers then
LocalRef[owner] = LocalRef[owner] or {}
if LocalRef[owner][numbers] == nil then
LocalRef[owner][numbers] = techage.check_numbers(numbers, owner)
end
return LocalRef[owner][numbers]
end
return LocalRef[owner][numbers]
return false
end
techage.lua_ctlr.register_function("get_input", {
@ -35,69 +38,18 @@ techage.lua_ctlr.register_function("get_input", {
" The device has to be connected with the controller."
})
techage.lua_ctlr.register_function("get_status", {
cmnd = function(self, num)
techage.lua_ctlr.register_function("read_data", {
cmnd = function(self, num, ident)
num = tostring(num or "")
return techage.send_single(self.meta.number, num, "state", nil)
return techage.send_single(self.meta.number, num, ident, nil)
end,
help = " $get_status(num) ,\n"..
" Read status string from a remote device.\n"..
' example: sts = $get_status("1234")'
help = " $read_data(num, ident)\n"..
" Read any kind of data from another block.\n"..
' "num" is the block number\n'..
' "ident" specifies the data to be read\n'..
' example: sts = $read_data("1234", "state")'
})
techage.lua_ctlr.register_function("get_player_action", {
cmnd = function(self, num)
num = tostring(num or "")
return unpack(techage.send_single(self.meta.number, num, "player_action", nil) or {"","",""})
end,
help = " $get_player_action(num) ,\n"..
" Read player action status from a Sensor Chest.\n"..
' example: player, action, item = $get_player_action("1234")'
})
techage.lua_ctlr.register_function("get_fuel_value", {
cmnd = function(self, num)
num = tostring(num or "")
return techage.send_single(self.meta.number, num, "fuel", nil)
end,
help = " $get_fuel_value(num)\n"..
" Read the fuel value from fuel consuming blocks.\n"..
' example: val = $get_fuel_value("1234")'
})
techage.lua_ctlr.register_function("get_load_value", {
cmnd = function(self, num)
num = tostring(num or "")
return techage.send_single(self.meta.number, num, "load", nil)
end,
help = " $get_load_value(num)\n"..
" Read the load value (0..100) from a tank/storage block.\n"..
' example: val = $get_load_value("1234")'
})
techage.lua_ctlr.register_function("get_delivered_value", {
cmnd = function(self, num)
num = tostring(num or "")
return techage.send_single(self.meta.number, num, "delivered", nil)
end,
help = " $get_delivered_value(num)\n"..
" Read the delivered power value from a generator block.\n"..
" Power consuming blocks like accus\nalso provide a negative value.\n"..
' example: val = $get_delivered_value("1234")'
})
--techage.lua_ctlr.register_function("get_num_items", {
-- cmnd = function(self, num, idx)
-- num = tostring(num or "")
-- idx = tonumber(idx)
-- return techage.send_single(self.meta.number, num, "num_items", idx)
-- end,
-- help = " $get_num_items(num)\n"..
-- " Read number of stored items in one\n"..
-- " storage (1..8) from a Warehouse Box.\n"..
-- ' example: cnt = $get_num_items("1234", 4)\n'
--})
techage.lua_ctlr.register_function("time_as_str", {
cmnd = function(self)
local t = minetest.get_timeofday()
@ -122,28 +74,18 @@ techage.lua_ctlr.register_function("time_as_num", {
' example: time = $time_as_num()'
})
techage.lua_ctlr.register_function("playerdetector", {
cmnd = function(self, num)
num = tostring(num or "")
if not_protected(self.meta.owner, num) then
return techage.send_single(self.meta.number, num, "name", nil)
end
end,
help = ' $playerdetector(num) --> e.g. "Joe"\n'..
' "" is returned if no player is nearby.\n'..
' example: name = $playerdetector("1234")'
})
techage.lua_ctlr.register_action("send_cmnd", {
cmnd = function(self, num, text)
cmnd = function(self, num, cmnd, data)
num = tostring(num or "")
text = tostring(text or "")
cmnd = tostring(cmnd or "")
if not_protected(self.meta.owner, num) then
techage.send_single(self.meta.number, num, text, nil)
techage.send_single(self.meta.number, num, cmnd, data)
end
end,
help = " $send_cmnd(num, text)\n"..
help = " $send_cmnd(num, cmnd, data)\n"..
' Send a command to the device with number "num".\n'..
'"cmnd" is the command as text string\n'..
'"data" is additional data (optional)\n'..
' example: $send_cmnd("1234", "on")'
})
@ -163,20 +105,30 @@ techage.lua_ctlr.register_action("set_filter", {
techage.lua_ctlr.register_action("display", {
cmnd = function(self, num, row, text1, text2, text3)
cmnd = function(self, num, row, text)
num = tostring(num or "")
text1 = tostring(text1 or "")
text2 = tostring(text2 or "")
text3 = tostring(text3 or "")
row = tonumber(row or 1) or 1
text = tostring(text or "")
if not_protected(self.meta.owner, num) then
techage.send_single(self.meta.number, num, "set", {row = row, str = text1..text2..text3})
if text:byte(1) ~= 32 then -- left aligned?
text = "<"..text -- use the '<' lcdlib control char for left-aligned
else
text = text:sub(2) -- delete blank for centered
end
if row == 0 then -- add line?
techage.send_single(self.meta.number, num, "add", text)
else
techage.send_single(self.meta.number, num, "set", {row = row, str = text})
end
end
end,
help = " $display(num, row, text,...)\n"..
help = " $display(num, row, text)\n"..
' Send a text line to the display with number "num".\n'..
" 'row' is a value from 1..5\n"..
" The function accepts up to 3 text parameters\n"..
' example: $display("123", 1, "Hello ", name, " !")'
" 'row' is a value from 1..5, or 0 for scroll screen\n"..
" and add a new line. If the first char of the string\n"..
" is a blank, the text will be horizontally centered.\n"..
' example: $display("123", 1, "Hello "..name)'
})
techage.lua_ctlr.register_action("clear_screen", {

View File

@ -32,12 +32,13 @@ local sHELP = [[TA4 Lua Controller
techage.lua_ctlr = {}
local BATTERY_CAPA = 5000000
local BATTERY_CAPA = 10000000
local Cache = {}
local STATE_STOPPED = 0
local STATE_RUNNING = 1
local CYCLE_TIME = 1
local tCommands = {}
local tFunctions = {" Overview", " Data structures"}
@ -82,17 +83,14 @@ local function merge(dest, keys, values)
end
techage.lua_ctlr.register_action("print", {
cmnd = function(self, text1, text2, text3)
cmnd = function(self, text)
local pos = self.meta.pos
text1 = tostring(text1 or "")
text2 = tostring(text2 or "")
text3 = tostring(text3 or "")
output(pos, text1..text2..text3)
text = tostring(text or "")
output(pos, text)
end,
help = " $print(text,...)\n"..
help = " $print(text)\n"..
" Send a text line to the output window.\n"..
" The function accepts up to 3 text strings\n"..
' e.g. $print("Hello ", name, " !")'
' e.g. $print("Hello "..name)'
})
techage.lua_ctlr.register_action("loopcycle", {
@ -329,7 +327,7 @@ local function start_controller(pos)
return false
end
meta:set_string("output", "<press update>")
meta:set_string("output", "")
meta:set_int("cycletime", 1)
meta:set_int("cyclecount", 0)
meta:set_int("cpu", 0)
@ -337,7 +335,7 @@ local function start_controller(pos)
if compile(pos, meta, number) then
meta:set_int("state", techage.RUNNING)
meta:set_int("running", STATE_RUNNING)
minetest.get_node_timer(pos):start(1)
minetest.get_node_timer(pos):start(CYCLE_TIME)
meta:set_string("formspec", formspec4(meta))
meta:set_string("infotext", "Controller "..number..": running")
return true
@ -416,6 +414,10 @@ local function on_timer(pos, elapsed)
end
meta:set_int("cyclecount", 0)
if techage.is_activeformspec(pos) then
local meta = minetest.get_meta(pos)
meta:set_string("formspec", formspec4(meta))
end
return call_loop(pos, meta, elapsed)
end
@ -444,6 +446,7 @@ local function on_receive_fields(pos, formname, fields, player)
if fields.update then
meta:set_string("formspec", formspec4(meta))
techage.set_activeformspec(pos, player)
elseif fields.clear then
meta:set_string("output", "<press update>")
meta:set_string("formspec", formspec4(meta))
@ -517,6 +520,14 @@ minetest.register_node("techage:ta4_lua_controller", {
on_receive_fields = on_receive_fields,
on_rightclick = function(pos, node, clicker)
local meta = M(pos)
if meta:get_int("running") == STATE_RUNNING then
techage.set_activeformspec(pos, clicker)
meta:set_string("formspec", formspec4(meta))
end
end,
on_dig = function(pos, node, puncher, pointed_thing)
if minetest.is_protected(pos, puncher:get_player_name()) then
return
@ -596,14 +607,14 @@ function techage.lua_ctlr.get_msg(number)
end
techage.register_node({"techage:ta4_lua_controller"}, {
on_recv_message = function(pos, topic, payload)
on_recv_message = function(pos, src, topic, payload)
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
if topic == "on" then
set_input(pos, number, payload, topic)
set_input(pos, number, src, topic)
elseif topic == "off" then
set_input(pos, number, payload, topic)
set_input(pos, number, src, topic)
elseif topic == "term" then
set_input(pos, number, "term", payload)
elseif topic == "msg" then

View File

@ -20,39 +20,53 @@ local PlayerActions = {}
local InventoryState = {}
local function store_action(pos, player, action, stack)
local function store_action(pos, player, action)
local meta = minetest.get_meta(pos)
local name = player and player:get_player_name() or ""
local number = meta:get_string("node_number")
local item = stack:get_name().." "..stack:get_count()
PlayerActions[number] = {name, action, item}
PlayerActions[number] = {name, action}
end
local function send_off_command(pos)
local meta = minetest.get_meta(pos)
local numbers = meta:get_string("numbers") or ""
if numbers ~= "" then
local number = meta:get_string("number")
if number ~= "" then
local own_num = meta:get_string("node_number")
techage.send_multi(own_num, numbers, "off")
techage.send_single(own_num, number, "off")
end
end
local function send_command(pos)
local meta = minetest.get_meta(pos)
local numbers = meta:get_string("numbers") or ""
if numbers ~= "" then
local number = meta:get_string("number")
if number ~= "" then
local own_num = meta:get_string("node_number")
techage.send_multi(own_num, numbers, "on")
techage.send_single(own_num, number, "on")
minetest.after(0.2, send_off_command, pos)
end
end
local function get_stacks(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local a = safer_lua.Array()
for idx = 1,4 do
local stack = inv:get_stack("main", idx)
local s = safer_lua.Store()
if stack:get_count() > 0 then
s.set("name", stack:get_name())
s.set("count", stack:get_count())
a.add(s)
end
end
return a
end
local function allow_metadata_inventory_put(pos, listname, index, stack, player)
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
store_action(pos, player, "put", stack)
store_action(pos, player, "put")
send_command(pos)
return stack:get_count()
end
@ -61,7 +75,7 @@ local function allow_metadata_inventory_take(pos, listname, index, stack, player
if minetest.is_protected(pos, player:get_player_name()) then
return 0
end
store_action(pos, player, "take", stack)
store_action(pos, player, "take")
send_command(pos)
return stack:get_count()
end
@ -78,7 +92,16 @@ local function after_dig_node(pos, oldnode, oldmetadata, digger)
techage.remove_node(pos)
end
local function formspec(pos)
local function formspec1()
return "size[6,4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"field[0.5,1;5,1;number;TA4 Lua Controller number:;]" ..
"button_exit[1.5,2.5;2,1;exit;Save]"
end
local function formspec2(pos)
local text = M(pos):get_string("text")
return "size[8,6]"..
default.gui_bg..
@ -117,22 +140,30 @@ minetest.register_node("techage:ta4_sensor_chest", {
meta:set_string("node_number", number)
meta:set_string("owner", placer:get_player_name())
meta:set_string("text", "Text to be changed\nby command.")
meta:set_string("formspec", formspec(pos))
meta:set_string("formspec", formspec1())
meta:set_string("infotext", S("TA4 Sensor Chest").." "..number)
end,
on_receive_fields = function(pos, formname, fields, player)
local meta = M(pos)
local nvm = techage.get_nvm(pos)
if fields.f1 then
if fields.number and fields.number ~= "" then
local owner = meta:get_string("owner")
if techage.check_numbers(fields.number, owner) then
meta:set_string("number", fields.number)
local node_number = meta:get_string("node_number")
meta:set_string("infotext", S("TA4 Sensor Chest").." "..node_number..": "..S("connected with").." "..fields.number)
meta:set_string("formspec", formspec2(pos))
end
elseif fields.f1 then
store_action(pos, player, "f1")
send_command(pos)
end
if fields.f2 then
meta:set_string("formspec", formspec2(pos))
elseif fields.f2 then
store_action(pos, player, "f2")
send_command(pos)
meta:set_string("formspec", formspec2(pos))
end
meta:set_string("formspec", formspec(pos, meta))
end,
techage_set_numbers = function(pos, numbers, player_name)
@ -150,7 +181,7 @@ minetest.register_node("techage:ta4_sensor_chest", {
sounds = default.node_sound_wood_defaults(),
})
techage.register_node({"ta4_sensor_chest"}, {
techage.register_node({"techage:ta4_sensor_chest"}, {
on_pull_item = function(pos, in_dir, num)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
@ -172,13 +203,16 @@ techage.register_node({"ta4_sensor_chest"}, {
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
return techage.get_inv_state(inv, "main")
elseif topic == "player_action" then
elseif topic == "action" then
local meta = minetest.get_meta(pos)
local number = meta:get_string("node_number")
return PlayerActions[number]
return PlayerActions[number][1], PlayerActions[number][2]
elseif topic == "stacks" then
return get_stacks(pos)
elseif topic == "text" then
local meta = minetest.get_meta(pos)
meta:set_string("text", tostring(payload))
meta:set_string("formspec", formspec2(pos))
else
return "unsupported"
end

View File

@ -12,39 +12,24 @@
]]--
local SERVER_CAPA = 5000
local DEFAULT_MEM = {
size=0,
data={
version = 1,
info = "SaferLua key/value Server",
}
}
-- for lazy programmers
local S = function(pos) if pos then return minetest.pos_to_string(pos) end end
local M = minetest.get_meta
local function formspec(meta)
local names = meta:get_string("names") or ""
local SERVER_CAPA = 5000
local function formspec(nvm)
local names = table.concat(nvm.names or {}, " ")
return "size[9,4]"..
default.gui_bg..
default.gui_bg_img..
default.gui_slots..
"field[0.2,1;9,1;names;Allowed user names (spaces separated):;"..names.."]" ..
"field[0.2,1;9,1;names;Allowed user names (space separated):;"..names.."]" ..
"button_exit[3.5,2.5;2,1;exit;Save]"
end
local function on_time(pos, elasped)
local meta = minetest.get_meta(pos)
local nvm = techage.get_nvm(pos)
if next(nvm) == nil then
nvm = table.copy(DEFAULT_MEM)
end
local number = meta:get_string("number")
meta:set_string("infotext", "Server "..number..": ("..(nvm.size or 0).."/"..SERVER_CAPA..")")
return true
end
minetest.register_node("techage:ta4_server", {
description = "Central Server",
description = "TA4 Lua Server",
tiles = {
-- up, down, right, left, back, front
"techage_server_top.png",
@ -73,23 +58,25 @@ minetest.register_node("techage:ta4_server", {
},
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
local meta = M(pos)
local nvm = techage.get_nvm(pos)
local number = techage.add_node(pos, "techage:ta4_server")
meta:set_string("owner", placer:get_player_name())
meta:set_string("number", number)
meta:set_string("formspec", formspec(meta))
on_time(pos, 0)
meta:set_string("formspec", formspec(nvm))
nvm.size = 0
meta:set_string("infotext", "Server "..number..": ("..nvm.size.."/"..SERVER_CAPA..")")
minetest.get_node_timer(pos):start(20)
end,
on_receive_fields = function(pos, formname, fields, player)
local meta = minetest.get_meta(pos)
local meta = M(pos)
local nvm = techage.get_nvm(pos)
local owner = meta:get_string("owner")
if player:get_player_name() == owner then
if fields.names and fields.names ~= "" then
local names = string.gsub(fields.names, " +", " ")
meta:set_string("names", names)
meta:set_string("formspec", formspec(meta))
nvm.names = string.split(fields.names, " ")
meta:set_string("formspec", formspec(nvm))
end
end
end,
@ -98,14 +85,18 @@ minetest.register_node("techage:ta4_server", {
if minetest.is_protected(pos, puncher:get_player_name()) then
return
end
local meta = minetest.get_meta(pos)
local number = meta:get_string("number")
techage.del_mem(pos)
minetest.node_dig(pos, node, puncher, pointed_thing)
techage.remove_node(pos)
end,
on_timer = on_time,
on_timer = function(pos, elasped)
local meta = M(pos)
local nvm = techage.get_nvm(pos)
nvm.size = nvm.size or 0
local number = meta:get_string("number")
meta:set_string("infotext", "Server "..number..": ("..nvm.size.."/"..SERVER_CAPA..")")
return true
end,
paramtype = "light",
sunlight_propagates = true,
@ -138,25 +129,14 @@ local function calc_size(v)
end
end
local function get_memory(pos, num, name)
local function get_memory(num, name)
local info = techage.get_node_info(num)
if info and info.name == "techage:ta4_server" then
local meta = minetest.get_meta(info.pos)
local owner = meta:get_string("owner")
if name == owner then
local nvm = techage.get_nvm(pos)
if next(nvm) == nil then
nvm = table.copy(DEFAULT_MEM)
end
return nvm
end
local names = meta:get_string("names")
for _,n in ipairs(string.split(names, " ")) do
local nvm = techage.get_nvm(pos)
if info and info.pos then
local nvm = techage.get_nvm(info.pos)
nvm.names = nvm.names or {}
for _,n in ipairs(nvm.names) do
if name == n then
if next(nvm) == nil then
nvm = table.copy(DEFAULT_MEM)
end
nvm.data = nvm.data or {}
return nvm
end
end
@ -173,7 +153,9 @@ local function write_value(nvm, key, item)
end
nvm.size = nvm.size + calc_size(item)
nvm.data[key] = item
return true
end
return false
end
local function read_value(nvm, key)
@ -185,7 +167,7 @@ local function read_value(nvm, key)
end
techage.register_node({"techage:ta4_server"}, {
on_recv_message = function(pos, topic, payload)
on_recv_message = function(pos, src, topic, payload)
return "unsupported"
end,
on_node_load = function(pos)
@ -197,7 +179,7 @@ techage.register_node({"techage:ta4_server"}, {
techage.lua_ctlr.register_function("server_read", {
cmnd = function(self, num, key)
if type(key) == "string" then
local nvm = get_memory(self.meta.pos, num, self.meta.owner)
local nvm = get_memory(num, self.meta.owner)
if nvm then
return read_value(nvm, key)
end
@ -208,15 +190,15 @@ techage.lua_ctlr.register_function("server_read", {
help = " $server_read(num, key)\n"..
" Read a value from the server.\n"..
" 'key' must be a string.\n"..
' example: state = $server_read("0123", "state")'
' example: state = $server_read("123", "state")'
})
techage.lua_ctlr.register_action("server_write", {
cmnd = function(self, num, key, value)
if type(key) == "string" then
local nvm = get_memory(self.meta.pos, num, self.meta.owner)
local nvm = get_memory(num, self.meta.owner)
if nvm then
write_value(nvm, key, value)
return write_value(nvm, key, value)
end
else
self.error("Invalid server_write parameter")
@ -226,7 +208,8 @@ techage.lua_ctlr.register_action("server_write", {
" Store a value on the server under the key 'key'.\n"..
" 'key' must be a string. 'value' can be either a\n"..
" number, string, boolean, nil or data structure.\n"..
' example: $server_write("0123", "state", state)'
" return value: true if successful or false\n"..
' example: res = $server_write("123", "state", state)'
})

View File

@ -80,11 +80,13 @@ local function command(pos, cmnd, player)
meta:set_string("formspec", formspec2(meta))
elseif cmnd == "pub" and owner == player then
meta:set_int("public", 1)
output(pos, player..":$ "..cmnd)
--output(pos, player..":$ "..cmnd)
output(pos, "> "..cmnd)
output(pos, "Switched to public use!")
elseif cmnd == "priv" and owner == player then
meta:set_int("public", 0)
output(pos, player..":$ "..cmnd)
--output(pos, player..":$ "..cmnd)
output(pos, "> "..cmnd)
output(pos, "Switched to private use!")
elseif meta:get_int("public") == 1 or owner == player then
-- send <num> on/off
@ -92,7 +94,8 @@ local function command(pos, cmnd, player)
if num and topic then
local own_number = meta:get_string("own_number")
if techage.lua_ctlr.not_protected(owner, num) then
output(pos, player..":$ send "..num.." "..topic)
--output(pos, player..":$ send "..num.." "..topic)
output(pos, "> send "..num.." "..topic)
techage.send_single(own_number, num, topic, nil)
return
end
@ -102,15 +105,17 @@ local function command(pos, cmnd, player)
if num and text then
local own_number = meta:get_string("own_number")
if techage.lua_ctlr.not_protected(owner, num) then
output(pos, player..":$ msg "..num.." "..text)
--output(pos, player..":$ msg "..num.." "..text)
output(pos, "> msg "..num.." "..text)
techage.send_single(own_number, num, "msg", {src=own_number, text=text})
return
end
end
local number = meta:get_string("number")
local own_number = meta:get_string("own_number")
if techage.lua_ctlr.not_protected(owner, num) then
output(pos, player..":$ "..cmnd)
if techage.lua_ctlr.not_protected(owner, number) then
--output(pos, player..":$ "..cmnd)
output(pos, "> "..cmnd)
techage.send_single(own_number, number, "term", cmnd)
end
end
@ -157,9 +162,7 @@ minetest.register_node("techage:ta4_terminal", {
local meta = minetest.get_meta(pos)
if fields.number and fields.number ~= "" then
local owner = meta:get_string("owner")
print(fields.number, owner)
if techage.check_numbers(fields.number, owner) then
print(1)
meta:set_string("number", fields.number)
local own_number = meta:get_string("own_number")
meta:set_string("infotext", "TA4 Lua Controller Terminal "..own_number..": connected with "..fields.number)
@ -192,7 +195,7 @@ minetest.register_craft({
})
techage.register_node({"techage:ta4_terminal"}, {
on_recv_message = function(pos, topic, payload)
on_recv_message = function(pos, src, topic, payload)
if topic == "term" then
output(pos, payload)
return true

View File

@ -254,7 +254,7 @@ In einem Tank können Flüssigkeiten gespeichert werden. Ein Tank kann über ein
Ein Tank kann auch von Hand gefüllt oder geleert werden, indem mit einem vollen oder leeren Flüssigkeitsbehälter (Fass, Kanister) auf den Tank geklickt wird. Dabei ist zu beachten, dass Fässer nur komplett gefüllt oder entleert werden können. Sind bspw. weniger als 10 Einheiten im Tank, muss dieser Rest mit Kanistern entnommen oder leergepumpt werden.
In einen TA3 Tank passen 500 Einheiten oder 50 Fässer einer Flüssigkeit.
In einen TA3 Tank passen 1000 Einheiten oder 100 Fässer einer Flüssigkeit.
[ta3_tank|image]
@ -342,7 +342,7 @@ Das Bohrgestänge wird für die Bohrung benötigt. Es werden so viele Bohrgestä
Der Öltank ist die große Ausführung des TA3 Tanks (siehe Flüssigkeiten -> TA3 Tank).
Der große Tank kann 2000 Einheiten Öl aufnehmen.
Der große Tank kann 4000 Einheiten Öl aufnehmen.
[oiltank|image]

View File

@ -351,6 +351,36 @@ Der Signal Tower kann rot, grün und orange anzeigen. Eine Kombination der 3 Far
## TA4 Lua Controller
Der Lua Controller muss, wie der Name schon sagt, in der Programmiersprache Lua programmiert werden. Außerdem sollte man etwas Englisch können (oder Google bemühen), denn die Anleitung dazu gibt es nur in Englisch:
https://github.com/joe7575/techage/blob/master/manuals/ta4_lua_controller_EN.md
Auch der Lua Controller benötigt eine Batterie. Die Batterie muss in unmittelbarer Nähe zum Controller platziert werden, also an einer der 26 Positionen um den Controller herum.
[ta4_lua_controller|image]
### TA4 Lua Server
Der Server dient zur zentralen Speicherung von Daten von mehreren Lua Controllern. Es speichert auch die Daten über einen Server-Neustart hinweg.
[ta4_lua_server|image]
### TA4 Sensor Kiste/Chest
Die TA4 Sensor Kiste dient zum Aufbau von Automatischen Lagern oder Verkaufsautomaten. Sie hat erweitere Kommandos zur Fernsteuerung.
[ta4_sensor_chest|image]
### TA4 Lua Controller Terminal
Das Terminal dient zur Ein-/Ausgabe für den Lua Controller.
[ta4_terminal|image]
## Weitere TA4 Blöcke
@ -358,7 +388,7 @@ Der Signal Tower kann rot, grün und orange anzeigen. Eine Kombination der 3 Far
Siehe TA3 Tank.
In einen TA4 Tank passen 1000 Einheiten oder 100 Fässer einer Flüssigkeit.
In einen TA4 Tank passen 2000 Einheiten oder 200 Fässer einer Flüssigkeit.
[ta4_tank|image]

View File

@ -30,6 +30,16 @@ lItemName = []
lPlanTable = []
lTocLinks = []
def reset():
global lTitel, lText, lItemName, lPlanTable, lTocLinks
lTitel = []
lText = []
lItemName = []
lPlanTable = []
lTocLinks = []
def lua_table(name, lData):
lOut = []
lOut.append("%s = {" % name)
@ -209,16 +219,26 @@ def gen_lua_file(dest_name):
lOut.append(lua_table("%s.%s.aPlanTable" % (mod, manual), lPlanTable))
file(dest_name, "w").write("".join(lOut))
def gen_toc_md_file(dest_name, titel):
def gen_toc_md_file(dest_name, titel, level_range=[1,6]):
print("Write MD file '%s'" % dest_name)
lOut = ["# "+ titel]
lOut.append("")
for item in lTocLinks:
list_item = " " * (item["level"] - 1) + "-"
link = "%s#%s" % (item["link"], header_escsape(item["header"]))
lOut.append("%s [%s](%s)" % (list_item, item["header"], link))
if item["level"] in range(*level_range):
list_item = " " * (item["level"] - level_range[0]) + "-"
link = "%s#%s" % (item["link"], header_escsape(item["header"]))
lOut.append("%s [%s](%s)" % (list_item, item["header"], link))
file(dest_name, "w").write("\n".join(lOut))
def gen_file_local_toc(dest_name, level_range=[1,6]):
lOut = []
for item in lTocLinks:
if item["level"] in range(*level_range):
list_item = " " * (item["level"] - level_range[0]) + "-"
link = "#%s" % (item["header"].replace(" ", "-").replace("\\", ""))
lOut.append("%s [%s](%s)" % (list_item, item["header"].replace("\\", ""), link))
file(dest_name, "w").write("\n".join(lOut))
mod = "techage"
manual = "manual_DE"
parse_md_file("./manual_DE.md", mod, manual)
@ -228,3 +248,7 @@ parse_md_file("./manual_ta3_DE.md", mod, manual)
parse_md_file("./manual_ta4_DE.md", mod, manual)
gen_lua_file("../doc/manual_DE.lua")
gen_toc_md_file("./toc_DE.md", "Inhaltsverzeichnis")
reset()
parse_md_file("./ta4_lua_controller_EN.md", mod, manual)
gen_file_local_toc("toc.txt", level_range=[2,4])

View File

@ -3,13 +3,13 @@
![Lua Controller](https://github.com/joe7575/techage/blob/master/textures/techage_lua_controller_inventory.png)
The TA4 Lua Controller is a small computer, programmable in Lua to control your machinery.
In contrast to the ICTA Controller this controller allows to implement larger and smarter control and monitoring tasks.
In contrast to the ICTA Controller this controller allows to implement larger and more complex programs.
But to write Lua scripts, some knowledge with the programming language Lua is required.
Minetest uses Lua 5.1. The reference document for Lua 5.1 is [here](https://www.lua.org/manual/5.1/). The book [Programming in Lua (first edition)](https://www.lua.org/pil/contents.html) is also a perfect source for learning Lua.
This manual is also available as PDF:
This TA4 Lua Controller manual is also available as PDF:
https://github.com/joe7575/techage/blob/master/manuals/ta4_lua_controller_EN.pdf
@ -18,21 +18,31 @@ https://github.com/joe7575/techage/blob/master/manuals/ta4_lua_controller_EN.pdf
## Table of Contents
- [TA4 Lua Controller Blocks](#TA4-Lua-Controller-Blocks)
- [TA4 Lua Controller](#TA4-Lua-Controller)
- [Battery](#Battery)
- [Central Server](#Central-Server)
- [ TA4 Lua Controller Terminal](#TA4-Lua-Controller-Terminal)
- [TA4 Sensor Chest ](#TA4-Sensor-Chest )
- [TA4 Lua Controller](#TA4-Lua-Controller)
- [Battery](#Battery)
- [TA4 Lua Server](#TA4-Lua-Server)
- [TA4 Lua Controller Terminal](#TA4-Lua-Controller-Terminal)
- [TA4 Sensor Chest](#TA4-Sensor-Chest)
- [Lua Functions and Environment](#Lua-Functions-and-Environment)
- [Lua Functions and Limitations](#Lua-Functions-and-Limitations)
- [Arrays, Stores, and Sets](#Arrays,-Stores,-and-Sets)
- [Initialization, Loops, and Events](#Initialization,-Loops,-and-Events)
- [Techage Commands](#Techage-Commands)
- [Controller local commands](#Controller-local-commands)
- [Techage commands](#Techage-commands)
- [Server and Terminal commands](#Server-and-Terminal-commands)
- [Further commands](#Further-commands)
- [Lua Functions and Limitations](#Lua-Functions-and-Limitations)
- [Arrays, Stores, and Sets](#Arrays,-Stores,-and-Sets)
- [Initialization, Cyclic Task, and Events](#Initialization,-Cyclic-Task,-and-Events)
- [Lua Controller Functions](#Lua-Controller-Functions)
- [Controller local Functions](#Controller-local-Functions)
- [Techage Command Functions](#Techage-Command-Functions)
- [Server and Terminal Functions](#Server-and-Terminal-Functions)
- [Further Functions](#Further-Functions)
- [Example Scripts](#Example-Scripts)
- [Simple Counter](#Simple-Counter)
- [Hello World](#Hello-World)
- [For Loop with range(from, to)](#For-Loop-with-range(from,-to))
- [Monitoring Chest & Furnace](#Monitoring-Chest-&-Furnace)
- [Simple Calculator](#Simple-Calculator)
- [Welcome Display](#Welcome-Display)
- [Sensor Chest](#Sensor-Chest)
- [Emails](#Emails)
## TA4 Lua Controller Blocks
@ -40,14 +50,14 @@ https://github.com/joe7575/techage/blob/master/manuals/ta4_lua_controller_EN.pdf
The controller block has a menu form with the following tabs:
- the `init` tab for the initialization code block (see "Initialization, Loops, and Events")
- the `func` tab for the Lua functions (see "Initialization, Loops, and Events")
- the `loop` tab for the main code block (see "Initialization, Loops, and Events")
- the `init` tab for the initialization code block
- the `func` tab for the Lua functions
- the `loop` tab for the main code block
- the `outp` tab for debugging outputs via `$print()`
- the `notes` tab for your code snippets or other notes (like a clipboard)
- the `help` tab with information to the available commands (see "Techage Commands")
- the `help` tab with information to the available functions
The controller needs power to work. A battery pack has to be placed neadby.
The controller needs power to work. A battery pack has to be placed nearby.
### Battery
@ -56,14 +66,14 @@ The needed battery power is directly dependent on the CPU time the controller co
Because of that, it is important to optimize the execution time of the code (which helps the admin to keep server lags down :))
The controller will be restarted (init() is called) every time the Minetest server starts again.
To store data non-volatile (to pass a server restart), the Central Server has to be used.
To store data non-volatile (to pass a server restart), the "TA4 Lua Server" block has to be used.
### Central Server
### TA4 Lua Server
The Server block is used to store data from the controllers nonvolatile. It can also be used for communication purposes between several Controllers.
The Server has a form to enter valid usernames for server access.
The Server block is used to store data from Lua Controllers nonvolatile. It can also be used for communication purposes between several Lua Controllers.
Only configured players have access to the server. Therefore, the server has a menu to enter player names.
For special Server commands, see "Server and Terminal commands"
For special Server functions, see "Server and Terminal Functions"
### TA4 Lua Controller Terminal
@ -78,7 +88,7 @@ The Terminal has a help system for internal commands. Its supports the following
- `send <num> on/off` = send on/off event to e. g. lamps (for testing purposes)
- `msg <num> <text>` = send a text message to another Controller (for testing purposes)
For special Terminal commands for the TA4 Lua Controller, see "Terminal Commands"
For special Terminal functions for the TA4 Lua Controller, see "Server and Terminal Functions"
### TA4 Sensor Chest
@ -149,15 +159,15 @@ It is not possible to easily control the memory usage of a Lua table at runtime.
_Arrays_ are lists of elements, which can be addressed by means of an index. An index must be an integer number. The first element in an _array_ has the index value 1. _Arrays_ have the following methods:
- add(value) - add a new element at the end of the array
- set(idx, value) - overwrite an existing array element on index `idx`
- get(idx) - return the value of the array element on index `idx`
- remove(idx) - remove the array element on index `idx`
- add(value) - add a new element at the end of the array
- set(idx, value) - overwrite an existing array element on index `idx`
- get(idx) - return the value of the array element on index `idx`
- remove(idx) - remove the array element on index `idx`
- insert(idx, val) - insert a new element at index `idx` (the array becomes one element longer)
- size() - return the number of _array_ elements
- memsize() - return the needed _array_ memory space
- next() - `for` loop iterator function, returning `idx,val`
- sort(reverse) - sort the _array_ elements in place. If _reverse_ is `true`, sort in descending order.
- size() - return the number of _array_ elements
- memsize() - return the needed _array_ memory space
- next() - `for` loop iterator function, returning `idx,val`
- sort(reverse) - sort the _array_ elements in place. If _reverse_ is `true`, sort in descending order.
Example:
@ -182,10 +192,10 @@ end
Unlike _arrays_, which are indexed by a range of numbers, _stores_ are indexed by keys, which can be a string or a number. The main operations on a _store_ are storing a value with some key and extracting the value given the key.
The _store_ has the following methods:
- set(key, val) - store/overwrite the value `val` behind the keyword `key`
- set(key, val) - store/overwrite the value `val` behind the keyword `key`
- get(key) - read the value behind `key`
- del(key) - delete a value
- size() - return the number of _store_ elements
- size() - return the number of _store_ elements
- memsize() - return the needed _store_ memory space
- next() - `for` loop iterator function, returning `key,val`
- keys(order) - return an _array_ with the keys. If _order_ is `"up"` or `"down"`, return the keys as sorted _array_, in order of the _store_ values.
@ -251,13 +261,13 @@ The configured limit can be determined via `memsize()`:
memsize() --> function returns 1000 (example)
```
### Initialization, Loops, and Events
### Initialization, Cyclic Task, and Events
The TA4 Lua Controller distinguishes between the initialization phase (just after the controller was started) and the continuous operational phase, in which the normal code is executed.
#### Initialization
During the initialization phase the function `init()` is executed once. The `init()` function is typically used to initialize variables, e.g. clean the display, or reset other blocks:
During the initialization phase the function `init()` is executed once. The `init()` function is typically used to initialize variables, clean the display, or reset other blocks:
```lua
-- initialize variables
@ -266,21 +276,21 @@ table = Store()
player_name = "unknown"
# reset blocks
$clear_screen("0123") -- "0123" is the number of the display
$clear_screen("123") -- "123" is the number of the display
$send_cmnd("2345", "off") -- turn off the blocks with the number "2345"
```
#### Loops
#### Cyclic Task
During the continuous operational phase the `loop()` function is typically called every second.
During the continuous operational phase the `loop()` function is cyclically called.
Code witch should be executed cyclically has to be placed here.
The cycle frequency is per default once per second, but can be changed via:
The cycle frequency is per default once per second but can be changed via:
```lua
$loopcycle(0) -- no loop cyle any more
$loopcycle(1) -- call the loop function every second
$loopcycle(10) -- call the loop function only every 10 seconds
$loopcycle(10) -- call the loop function every 10 seconds
```
The provided number must be an integer value.
@ -289,90 +299,112 @@ The cycle frequency can be changed in the `init()` function, but also in the `lo
#### Events
To be able to react directly on received commands, the TA4 Lua Controller supports events.
Events are usually turned off, but can be activated with the command `events()`:
Events are usually turned off, but can be activated with the function `events()`:
```lua
$events(true) -- enable events
$events(false) -- disable events
```
If an event occurs (a command was received from another block), the `loop()` is executed (in addition to the
normal loop cycle). In this case the system variable 'event' is set:
If an event occurs (a command was received from another block), the `loop()` is executed (in addition to the normal loop cycle). In this case the system variable 'event' is set:
```lua
if event then
-- event has occurred
if $input("3456") == "on" then -- check input from block with the number "3456"
if $get_input("3456") == "on" then -- check input from block "3456"
-- do some action...
end
end
```
The first occurred event will directly processed, further events may be delayed. The TA4 Lua Controller
allows a maximum of one event every 100 ms.
The first occurred event will directly be processed, further events may be delayed. The TA4 Lua Controller allows a maximum of one event every 100 ms.
## Techage Commands
## Lua Controller Functions
For the communication with other blocks the controller supports the following commands:
In addition to Lua standard function the Lua Controller provides the following functions:
### Controller local commands
### Controller local Functions
- `$print(text, text, text)` - Output a text string on the 'outp' tab of the controller menu. The function accepts up to three text arguments. E.g.: `$print("Hello ", name, " !")`
- `$loopcycle(seconds)` - This function allows to change the call frequency of the controllers loop() function, witch is per default one second. For more info, see "Loops and Events".
- `$events(bool)` - Enable/disable event handling. For more info, see "Loops and Events"
- `$get_ms_time()` - Returns time with millisecond precision.
- `$time_as_str()` - Read the time of day (ingame) als text string in 24h format, like "18:45".
- `$time_as_num()` - Read the time of day (ingame) als integer number in 24h format, like 1845.
- `$get_input(num)` - Read one input value provided by an external block with the given number _num_. The block has to be configured with the number of the controller to be able to send status messages (on/off commands) to the controller. _num_ is the number of the remote block, like "1234".
- `$print(text)` - Output a text string on the 'outp' tab of the controller menu.
E.g.: `$print("Hello "..name)`
- `$loopcycle(seconds)` - This function allows to change the call frequency of the controller loop() function, witch is per default one second. For more info, see "Cyclic Task"
- `$events(bool)` - Enable/disable event handling. For more info, see "Events"
- `$get_ms_time()` - Returns time with millisecond precision
- `$time_as_str()` - Read the time of day (ingame) as text string in 24h format, like "18:45"
- `$time_as_num()` - Read the time of day (ingame) as integer number in 24h format, like 1845
- `$get_input(num)` - Read an input value provided by an external block with the given number _num_. The block has to be configured with the number of the controller to be able to send status messages (on/off commands) to the controller. _num_ is the number of the remote block, like "1234".
#### Input Example
- a Player Detector with number "0001" is configured to send on/off commands to a block with number "0002".
- The TA4 Lua Controller with number "0002" will receive these commands as input messages.
- The program on the SaferLua Controller can always read the last input message from block with number "0001" by means of:
- A Player Detector with number "456" is configured to send on/off commands to the TA4 Lua Controller with number "345".
- The TA4 Lua Controller will receive these commands as input value.
- The program on the SaferLua Controller can always read the last input value from the Player Detector with number "456" by means of:
`sts = $get_input("0001")`
`sts = $get_input("456")`
### Techage commands
### Techage Command Functions
* `$get_status(num)` - Read the status from an external block with the given number _num_. Standard blocks return a status string like: 'running', 'stopped', 'blocked', 'standby', 'fault', or "unloaded".
* `$get_player_action(num)` - Read the player action status from a TA4 Sensor Chest with the given number _num_. The function returns three values: player-name, action, item-name.
* `$get_fuel_value(num)` - Read fuel value from fuel consuming blocks. The block returns a number value. _num_ is the number of the remote block, like "1234".
* `$get_load_value(num)` - Read the load value from a tank/storage block. The block returns a number (0..100). _num_ is the number of the remote block, like "1234".
* `$get_delivered_value(num)` - Read the delivered power value from a generator block. The block returns a positive or negative number. Blocks like accus provide a negative value while charging. _num_ is the number of the remote block, like "1234".
* `$playerdetector(num)` - Read the name status from a Player Detector with the number _num_. If no player is nearby, the detector returns an empty string `""`.
* `$send_cmnd(num, text)` - Send a command to another block. _num_ is the number of the remote block, like "1234". _text_ is the command string like "on".
* `set_filter(num, slot, val)` - Enable/disable a Distributor filter slot. _num_ is the number of the Distributor block. _slot_ is a color and is used to select one of the Distributor sides: "red", "green", "blue", and "yellow". _val_ is either "on" (enable filter) or "off" (disable filter).
* `$display(num, row, text,...)` Send a text string to the display with number _num_. _row_ is the display row, a value from 1 to 5. _text_ is the string to be displayed. This function allows up to 3 text strings.
* `$read_data(num, ident)` - Read any kind of data from another block with the given number _num_. _ident_ specifies the data to be read. The result is block dependent (see table below):
| ident | returned data | comment |
| ----------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
| "state" | one of: "running", "stopped", "blocked", "standby", "fault", or "unloaded" | Techage machine state, used by many machines |
| "state" | one of: "red", "amber", "green", "off" | Signal Tower state |
| "state" | one of: "empty", "loaded", "full" | State of a chest or Sensor Chest |
| "fuel" | number | fuel value from fuel consuming block |
| "load" | number | Read the load value in percent (0..100) from a tank/storage block |
| "delivered" | number | Read the current delivered power value from a generator block. A power consuming block (accu) provides a negative value |
| "action" | player-name, action-string | only for Sensor Chests |
| "stacks" | Array with up to 4 Stores with the inventory content (see example) | only for Sensor Chests |
* `$send_cmnd(num, cmnd, data)` - Send a command to another block. _num_ is the number of the remote block, like "1234". _cmnd_ is the command, _data_ is additional data (see table below):
| cmnd | data | comment |
| -------------------------------- | ----------- | ----------------------------------------- |
| "on", "off" | nil | turn a node on/off (machine, lamp,...) |
| "red, "amber", "green", "off" | nil | set Signal Tower color |
| "red", "green", "blue", "yellow" | "on", "off" | Enable/disable a Distributor filter slot. |
| "text" | text string | Text to be used for the Sensor Chest menu |
* `$display(num, row, text)` Send a text string to the display with number _num_. _row_ is the display row, a value from 1 to 5, or 0 to add the text string at the bottom (scroll screen mode). _text_ is the string to be displayed. If the first char of the string is a blank, the text will be horizontally centered.
* `$clear_screen(num)` Clear the screen of the display with number _num_.
* `$position(num)` Returns the position '(x,y,z)' of the device with the given _num_.
* `$position(num)` Returns the position as string "'(x,y,z)" of the device with the given _num_.
### Server and Terminal commands
### Server and Terminal Functions
The Server is used to store data permanently/non-volatile. It can also be used to share data between several Controllers.
- `$server_write(num, key, value)` - Store a value on the server under the key _key_. _key_ must be a string. _value_ can be either a number, string, boolean, nil or data structure. **But this command does not allow nested data structures**. _num_ is the number of the Server, like "1234". Example: `$server_write("0123", "state", state)`
- `$server_write(num, key, value)` - Store a value on the server under the key _key_. _key_ must be a string. _value_ can be either a number, string, boolean, nil or data structure.
**This function does not allow nested data structures**.
_num_ is the number of the Server.
Example: `$server_write("0123", "state", state)`
- `$server_read(num, key)` - Read a value from the server. _key_ must be a string. _num_ is the number of the Server, like "1234".
The Terminal can send text strings as events to the Controller.
In contrast the Controller can send text strings to the terminal.
- `$get_term()` - Read a text command received from the Terminal
- `$put_term(num, text)` - Send a text string to the Terminal. _num_ is the number of the Terminal.
### Further commands
### Further Functions
Messages are used to transport data between Controllers. Messages are text strings or any other data plus the sender number.
Incoming messages are stored in a message queue (up to 10) and can be read one after the other.
* `$get_msg()` - Read a received message. The function returns the sender number and the message.
* `$send_msg(num, msg)` - Send a message to another Controller. _num_ is the destination number.
Messages are used to transport data between Controllers. Messages are text strings or any other data. Incoming messages are stored in order (up to 10) and can be read one after the other.
* `$get_msg()` - Read a received message. The function returns the sender number and the message. (see example "Emails")
* `$send_msg(num, msg)` - Send a message to another Controller. _num_ is the destination number. (see example "Emails")
* `$chat(text,...)` - Send yourself a chat message. This function allows up to 3 text strings.
* `$door(pos, text)` - Open/Close a door at position "pos".
Example: `$door("123,7,-1200", "close")`.
Hint: Use the Techage Programmer or Info Tool to easily determine the door position.
Hint: Use the Techage Info Tool to determine the door position.
## Example Scripts
@ -391,10 +423,11 @@ loop() code:
```lua
a = a + 1
$print("a = ", a)
$print("a = "..a)
```
### Hello World
"Hello world" example with output on the Display.
@ -407,11 +440,12 @@ a = Array("Hello", "world", "of", "Minetest")
$clear_screen("0669")
for i,text in a.next() do
$display("0669", i+2, text)
$display("0669", i, text)
end
```
### For Loop with range(from, to)
Second "Hello world" example with output on the Display,
@ -425,19 +459,21 @@ a = Array("Hello", "world", "of", "Minetest")
$clear_screen("0669")
for i in range(1, 4) do
text = a.get(i)
$display("0669", i+2, text)
text = a.get(i)
$display("0669", i, text)
end
```
### Monitoring Chest & Furnace
More realistic example to output Pusher states on the Display
More realistic example to read Pusher states and output them on a display:
init() code:
```lua
DISPLAY = "1234"
DISPLAY = "1234" -- adapt this to your display number
min = 0
```
@ -448,27 +484,127 @@ loop() code:
if ticks % 60 == 0 then
-- output time in minutes
min = min + 1
$display(DISPLAY, 1, min, " min")
$display(DISPLAY, 1, min.." min")
-- Cactus chest overrun
sts = $get_status("1034") -- read pusher status
sts = $read_data("1034", "state") -- read pusher status
if sts == "blocked" then $display(DISPLAY, 2, "Cactus full") end
-- Tree chest overrun
sts = $get_status("1089") -- read pusher status
sts = $read_data("1065", "state") -- read pusher status
if sts == "blocked" then $display(DISPLAY, 3, "Tree full") end
-- Furnace fuel empty
sts = $get_status("2895") -- read pusher status
sts = $read_data("1544", "state") -- read pusher status
if sts == "standby" then $display(DISPLAY, 4, "Furnace fuel") end
end
```
### Simple Calculator
A simple calculator (adds entered numbers) by means of a Lua Controller and a Terminal.
init() code:
```lua
$events(true)
$loopcycle(0)
TERM = "360" -- terminal number, to be adapted!
sum = 0
$put_term(TERM, "sum = "..sum)
```
loop() code:
```lua
s = $get_term() -- read text from terminal
if s then
val = tonumber(s) or 0 -- convert to number
sum = sum + val
text = string.format("+%d = %d", val, sum) -- format output string
$put_term(TERM, text) -- output to terminal
end
```
### Welcome Display
In addition to the controller, you also need a player detector and a display.
When the Player Detector detects a player the player name is shown on the display:
init() code:
```lua
$events(true)
$loopcycle(0)
SENSOR = "365" -- player detector number, to be adapted!
DISPLAY = "367" -- display number, to be adapted!
$clear_screen(DISPLAY)
```
loop() code:
```lua
if event then
name = $read_data(SENSOR, "name")
if name == "" then -- no player arround
$clear_screen(DISPLAY)
else
$display(DISPLAY, 2, " Welcome")
$display(DISPLAY, 3, " "..name)
end
end
```
### Sensor Chest
The following example shows the functions/commands to be used with the Sensor Chest:
init() code:
```lua
$events(true)
$loopcycle(0)
SENSOR = "372" -- sensor chest number, to be adapted!
$send_cmnd(SENSOR, "text", "press both buttons and\nput something into the chest")
```
loop() code:
```lua
if event and $get_input(SENSOR) == "on" then
-- read inventory state
state = $read_data(SENSOR, "state")
$print("state: "..state)
-- read player name and action
name, action = $read_data(SENSOR, "action")
$print("action"..": "..name.." "..action)
-- read inventory content
stacks = $read_data(SENSOR, "stacks")
for i,stack in stacks.next() do
$print("stack: "..stack.get("name").." "..stack.get("count"))
end
$print("")
end
```
### Emails
For an email system you need a Central Server and a TA4 Lua Controller with Terminal per player.
The Central Server serves as database for player name/block number resolution.
For an email system you need a TA4 Lua Server and a TA4 Lua Controller with Terminal per player.
The TA4 Lua Server serves as database for player name/block number resolution.
* Each Player needs its own Terminal and Controller. The Terminal has to be connected with the Controller
* Each Controller runs the same Lua Script, only the numbers and the owner names are different
@ -483,14 +619,14 @@ $loopcycle(0)
$events(true)
-- Start: update to your conditions
TERM = "27309"
CONTROLLER = "27310"
TERM = "360"
CONTROLLER = "359"
NAME = "Tom"
SERVER = "27312"
SERVER = "363"
-- End: update to your conditions
$server_write(SERVER, NAME, CONTROLLER)
$server_write(SERVER, CONTROLLER, NAME)
$print($server_write(SERVER, NAME, CONTROLLER))
$print($server_write(SERVER, CONTROLLER, NAME))
```
loop() code:

Binary file not shown.

View File

@ -145,6 +145,10 @@
- [Batterie](./manual_ta4_DE.md#batterie)
- [TA4 Display](./manual_ta4_DE.md#ta4-display)
- [TA4 Signal Tower](./manual_ta4_DE.md#ta4-signal-tower)
- [TA4 Lua Controller](./manual_ta4_DE.md#ta4-lua-controller)
- [TA4 Lua Server](./manual_ta4_DE.md#ta4-lua-server)
- [TA4 Sensor Kiste/Chest](./manual_ta4_DE.md#ta4-sensor-kistechest)
- [TA4 Lua Controller Terminal](./manual_ta4_DE.md#ta4-lua-controller-terminal)
- [Weitere TA4 Blöcke](./manual_ta4_DE.md#weitere-ta4-blöcke)
- [TA4 Tank / TA4 Tank](./manual_ta4_DE.md#ta4-tank--ta4-tank)
- [TA4 Pumpe / TA4 Pump](./manual_ta4_DE.md#ta4-pumpe--ta4-pump)