2018-08-28 22:24:43 +02:00
|
|
|
--[[
|
|
|
|
|
|
|
|
ICTA Controller
|
|
|
|
===============
|
|
|
|
|
|
|
|
Part of the SmartLine mod
|
|
|
|
|
|
|
|
Copyright (C) 2018 Joachim Stolberg
|
|
|
|
|
|
|
|
LGPLv2.1+
|
|
|
|
See LICENSE.txt for more information
|
|
|
|
|
|
|
|
command.lua:
|
|
|
|
|
|
|
|
Register all controller commands
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local sl = smartline
|
|
|
|
|
|
|
|
function sl.operand(s)
|
|
|
|
if s == "is" then
|
|
|
|
return "== "
|
|
|
|
else
|
|
|
|
return "~= "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-08 18:17:43 +02:00
|
|
|
function sl.fmt_number(num)
|
|
|
|
local mtch = num:match('^(%d+).*')
|
|
|
|
if mtch and num ~= mtch then
|
|
|
|
return mtch.."..."
|
|
|
|
end
|
|
|
|
return num
|
|
|
|
end
|
|
|
|
|
2018-08-28 22:24:43 +02:00
|
|
|
-- '#' is used as placeholder for rule numbers and has to be escaped
|
|
|
|
function smartline.escape(s)
|
2018-08-29 12:43:21 +02:00
|
|
|
s = tostring(s)
|
2018-08-28 22:24:43 +02:00
|
|
|
return s:gsub("#", '"..string.char(35).."')
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-09-08 18:17:43 +02:00
|
|
|
smartline.icta_register_condition("initial", {
|
|
|
|
title = "initial",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Condition is true only after\ncontroller start.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
-- Return two chunks of executable Lua code for the controller, according:
|
|
|
|
-- return <read condition>, <expected result>
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'env.ticks', '== 1'
|
|
|
|
end,
|
2018-09-08 18:17:43 +02:00
|
|
|
button = function(data, environ) return "Initial after start" end,
|
2018-08-28 22:24:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_condition("true", {
|
|
|
|
title = "true",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Condition is always true.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
code = function(data, environ)
|
|
|
|
return '"true"', '== "true"'
|
|
|
|
end,
|
|
|
|
button = function(data, environ) return "true" end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_condition("condition", {
|
2018-09-08 18:17:43 +02:00
|
|
|
title = "condition",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "condition",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "condition row number",
|
|
|
|
choices = "1,2,3,4,5,6,7,8",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "operand",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "condition",
|
2018-08-28 22:24:43 +02:00
|
|
|
choices = "was true, was not true",
|
|
|
|
default = "was true",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Used to execute two or several\nactions based on one condition.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
code = function(data, environ)
|
|
|
|
local idx = data.condition:byte(-1) - 0x30
|
|
|
|
local expected_result = "== false"
|
|
|
|
if data.operand == "was true" then
|
|
|
|
expected_result = "== true"
|
|
|
|
end
|
|
|
|
return "env.condition["..idx.."]", expected_result
|
|
|
|
end,
|
2018-09-08 18:17:43 +02:00
|
|
|
button = function(data, environ) return "cond("..data.condition:sub(-1,-1)..","..data.operand..")" end,
|
2018-08-28 22:24:43 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_condition("input", {
|
|
|
|
title = "inputs",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "node number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "operand",
|
|
|
|
choices = "is,is not",
|
|
|
|
default = "is",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "value",
|
|
|
|
choices = "on,off,false",
|
|
|
|
default = "on",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "An input is only available,\nif a nodes sends on/off\ncommands to the controller.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ) -- default button label
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'inp('..sl.fmt_number(data.number)..','..data.operand.." "..data.value..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'env.input["'..data.number..'"]',
|
|
|
|
sl.operand(data.operand)..'"'..data.value..'"'
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_condition("state", {
|
|
|
|
title = "node state request",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "node number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "operand",
|
|
|
|
label = "",
|
|
|
|
choices = "is,is not",
|
|
|
|
default = "is",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "value",
|
|
|
|
label = "",
|
|
|
|
choices = "stopped,running,standby,blocked,fault",
|
|
|
|
default = "stopped",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Read the state from a node.\nWorks for Pusher, Harvester, Quarry,\n"..
|
|
|
|
"and other similar nodes.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ) -- default button label
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'sts('..sl.fmt_number(data.number)..","..data.operand..' '..data.value..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'tubelib.send_request("'..data.number..'", "state", "")',
|
|
|
|
sl.operand(data.operand)..'"'..data.value..'"'
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_condition("fuel", {
|
|
|
|
title = "fuel state request",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "node number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "operand",
|
|
|
|
label = "",
|
|
|
|
choices = "is,is not",
|
|
|
|
default = "is",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "value",
|
|
|
|
label = "",
|
|
|
|
choices = "full,loaded,empty",
|
|
|
|
default = "full"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Read the fuel state from a node.\nWorks for Harvester, Quarry,\n"..
|
|
|
|
"and other fuel consuming nodes.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'fuel('..sl.fmt_number(data.number)..","..data.operand..' '..data.value..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'tubelib.send_request("'..data.number..'", "fuel", "")',
|
|
|
|
sl.operand(data.operand)..'"'..data.value..'"'
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_condition("signaltower", {
|
|
|
|
title = "Signal Tower state request",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Signal Tower number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "operand",
|
|
|
|
choices = "is,is not",
|
|
|
|
default = "is",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "value",
|
|
|
|
choices = "off,green,amber,red",
|
|
|
|
default = "off",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Read the color state\nfrom a Signal Tower.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ) -- default button label
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'tower('..sl.fmt_number(data.number)..","..data.operand..' '..data.value..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'tubelib.send_request("'..data.number..'", "state", "")',
|
|
|
|
sl.operand(data.operand)..'"'..data.value..'"'
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("signaltower", {
|
|
|
|
title = "Signal Tower command",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Signal Tower number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "value",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "lamp color",
|
2018-08-28 22:24:43 +02:00
|
|
|
choices = "off,green,amber,red",
|
|
|
|
default = "red",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Turn on/off a Signal Tower lamp.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'tower('..sl.fmt_number(data.number)..","..data.value..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
local s = 'tubelib.send_message("%s", "%s", nil, "%s", nil)'
|
|
|
|
return string.format(s, data.number, environ.owner, data.value)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("switch", {
|
2018-09-08 18:17:43 +02:00
|
|
|
title = "turn node on/off",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "node number(s)",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "value",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "state",
|
2018-08-28 22:24:43 +02:00
|
|
|
choices = "on,off",
|
|
|
|
default = "on",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Used for pushers, lamps, machines, gates,...",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'turn('..sl.fmt_number(data.number)..","..data.value..')'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
code = function(data, environ)
|
|
|
|
local s = 'tubelib.send_message("%s", "%s", nil, "%s", nil)'
|
|
|
|
return string.format(s, data.number, environ.owner, data.value)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("display", {
|
|
|
|
title = "Display: overwrite one line",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Display number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "row",
|
|
|
|
label = "Display line",
|
|
|
|
choices = "1,2,3,4,5,6,7,8,9",
|
|
|
|
default = "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "ascii",
|
|
|
|
name = "text",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "text",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Use a '*' character as reference\nto any condition result",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
code = function(data, environ)
|
2018-08-29 18:32:53 +02:00
|
|
|
local s1 = string.format('local text = string.gsub("%s", "*", tostring(env.result[#]))', smartline.escape(data.text))
|
2018-08-28 22:24:43 +02:00
|
|
|
local s2 = string.format('local payload = {row = %s, str = text}', data.row)
|
|
|
|
local s3 = string.format('tubelib.send_message("%s", "%s", nil, "row", payload)', data.number, environ.owner)
|
|
|
|
return s1.."\n\t"..s2.."\n\t"..s3
|
|
|
|
end,
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return "lcd("..sl.fmt_number(data.number)..","..data.row..',"'..data.text..'")'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("cleardisplay", {
|
|
|
|
title = "Display: Clear screen",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "numbers",
|
|
|
|
name = "number",
|
|
|
|
label = "Display number",
|
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'tubelib.send_message("'..data.number..'", "'..environ.owner..'", nil, "clear", nil)'
|
|
|
|
end,
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return "clear lcd("..sl.fmt_number(data.number)..")"
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
smartline.icta_register_action("chat", {
|
|
|
|
title = "chat send",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "ascii",
|
|
|
|
name = "text",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "message",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "The chat message is send to the\nController owner, only.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'minetest.chat_send_player("'..environ.owner..'", "[SmartLine Controller] '..data.text..'")'
|
|
|
|
end,
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'chat("'..data.text:sub(1,12)..'")'
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
function smartline.icta_door_toggle(pos, owner, state)
|
|
|
|
pos = minetest.string_to_pos("("..pos..")")
|
|
|
|
if pos then
|
|
|
|
local door = doors.get(pos)
|
|
|
|
if door then
|
|
|
|
local player = {
|
|
|
|
get_player_name = function() return owner end,
|
|
|
|
is_player = function() return true end,
|
|
|
|
}
|
|
|
|
if state == "open" then
|
|
|
|
door:open(player)
|
|
|
|
elseif state == "close" then
|
|
|
|
door:close(player)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
smartline.icta_register_action("door", {
|
|
|
|
title = "doors open/close",
|
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "pos",
|
|
|
|
label = "door position like: 123,7,-1200",
|
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "textlist",
|
|
|
|
name = "door_state",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "door state",
|
2018-08-28 22:24:43 +02:00
|
|
|
choices = "open,close",
|
|
|
|
default = "open",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
2018-09-08 18:17:43 +02:00
|
|
|
name = "lbl",
|
|
|
|
label = "For standard doors like the Steel Doors.\n"..
|
|
|
|
"Use the Tubelib Programmer to\neasily determine a door position.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'smartline.icta_door_toggle("'..data.pos..'", "'..environ.owner..'", "'..data.door_state..'")'
|
|
|
|
end,
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return 'door("'..data.pos..'",'..data.door_state..")"
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
function smartline.icta_player_detect(number, name)
|
|
|
|
local state = tubelib.send_request(number, "name", nil)
|
2018-09-08 18:17:43 +02:00
|
|
|
if (name == "*" and state ~= "") or string.find(name, state) then
|
2018-08-28 22:24:43 +02:00
|
|
|
return state
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
smartline.icta_register_condition("playerdetector", {
|
2018-09-08 18:17:43 +02:00
|
|
|
title = "Player Detector name request",
|
2018-08-28 22:24:43 +02:00
|
|
|
formspec = {
|
|
|
|
{
|
|
|
|
type = "digits",
|
|
|
|
name = "number",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Player Detector number",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "ascii",
|
|
|
|
name = "name",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "player name(s) or * for all",
|
2018-08-28 22:24:43 +02:00
|
|
|
default = "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type = "label",
|
|
|
|
name = "lbl",
|
2018-09-08 18:17:43 +02:00
|
|
|
label = "Read and check the name\nfrom a Player Detector.\n Use a '*' character for all player names.",
|
2018-08-28 22:24:43 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
code = function(data, environ)
|
|
|
|
return 'smartline.icta_player_detect("'..data.number..'", "'..data.name..'")', "~= nil"
|
|
|
|
end,
|
|
|
|
button = function(data, environ)
|
2018-09-08 18:17:43 +02:00
|
|
|
return "detector("..sl.fmt_number(data.number)..","..data.name:sub(1,8)..")"
|
2018-08-28 22:24:43 +02:00
|
|
|
end,
|
|
|
|
})
|