-
Notifications
You must be signed in to change notification settings - Fork 17
Examples
- Simple Counter
- Hello World
- For Loop with range(from, to)
- Monitoring Chest & Furnace
- Harvester Fuel Control
- Idle Function
- Stopwatch
- Balancer
Very simple example with output on the Controller menu.
init() code:
a = 1
loop() code:
a = a + 1
$print("a = ", a)
end
"Hello world" example with output on the Display.
init() code:
a = Array("Hello", "world", "of", "Minetest")
$clear_screen("0669")
for i,text in a.next() do
$display("0669", i+2, text)
end
Second "Hello world" example with output on the Display, implemented by means of a for/range loop.
init() code:
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)
end
More realistic example to output Pusher states on the Display
init() code:
DISPLAY = "1234"
min = 0
loop() code:
-- call code every 60 sec
if ticks % 60 == 0 then
-- output time in minutes
min = min + 1
$display(DISPLAY, 1, min, " min")
-- Cactus chest overrun
sts = $get_status("1034") -- read pusher status
if sts == "blocked" then $display(DISPLAY, 2, "Cactus full") end
-- Tree chest overrun
sts = $get_status("1089") -- read pusher status
if sts == "blocked" then $display(DISPLAY, 3, "Tree full") end
-- Furnace fuel empty
sts = $get_status("2895") -- read pusher status
if sts == "standby" then $display(DISPLAY, 4, "Furnace fuel") end
end
To pump only the necessary amount of fuel into the Harvester (or Quarry), the following script can be used:
init() code:
PUSHR = "0899"
HARVR = "0897"
-- to be on the safe side
$send_cmnd(PUSHR, "off")
pusher_on = false
loop() code:
- called every 30 second
if ticks % 30 == 0 then
if pusher_on then
pusher_on = false
-- stop pusher
$send_cmnd(PUSHR, "off")
-- restart harvester
$send_cmnd(HARVR, "on")
end
sts = $get_fuel_status(HARVR)
if sts == "empty" then
-- start pusher
$send_cmnd(PUSHR, "on")
pusher_on = true
end
end
This example shows, how Lua functions can be used. The function 'idle()' checks if a Pusher counter has changed since the last call. If not (Pusher is idle) the function returns true. This is normaly an indication for any fault in the TechPack plant.
init() code:
-- every 60 sec
$loopcycle(60)
DISPLAY = "0299"
-- define variable stores for the 3 pusher idle functions
s1 = Store()
s2 = Store()
s3 = Store()
function section:
-- 'number' is the Pusher number
-- 's' is a Store for the function variables
function idle(number, s)
cnt = $get_counter(number)
if cnt ~= s.get("cnt") then
-- store new counter value
s.set("cnt", cnt)
return false -- not idle
end
return true -- pusher is idle
end
loop() code:
-- clear row
$display(DISPLAY, 1, "")
if idle("0280", s1) then
$display(DISPLAY, 1, "Harv 1 is dle")
end
if idle("0285", s2) then
$display(DISPLAY, 1, "Harv 2 is idle")
end
if idle("0286", s3) then
$display(DISPLAY, 1, "Harv 3 is idle")
end
Stopwatch with highscore list, stored on the Server. You need the following nodes:
- SaferLua Controller
- SaferLua Battery
- SaferLua Server
- SmartLine Display
- SmartLine Player Detector
- SmartLine Button/Switch
You have to configure the Button/Switch as "switch" and configure the number of the SaferLua Controller to "connect" the switch with the Controller. When you turn on the switch, the time and the player name are taken. When you turn off the switch again, the time is calculated and the times of the best three players are shown on the Display.
init() code:
-- node numbers
DISPLAY = "0669"
SWITCH = "0670"
DETECTOR = "0671"
SERVER = "0674"
$events(true)
$loopcycle(0)
$clear_screen(DISPLAY)
-- read highscore data from Server, if available
-- otherwise generate a new 'Store'
highscore = $server_read(SERVER, "highscore") or Store()
loop() code:
if event then
if $get_input(SWITCH) == "on" then
time = $get_ms_time()
name = $playerdetector(DETECTOR)
$display(DISPLAY, 1, "running")
elseif $get_input(SWITCH) == "off" then
$display(DISPLAY, 1, "stopped")
time = $get_ms_time() - time
$display(DISPLAY, 2, "Time: ", time)
-- store the best (smallest) time only
time = math.min(highscore.get(name) or 999999, time)
highscore.set(name, time)
keys = highscore.keys("up")
-- output the top three with name and time
for i, name in keys.next() do
if i == 4 then break end
$display(DISPLAY, i+3, name, ": ", highscore.get(name) / 1000.0)
end
-- store results on the Server
$server_write(SERVER, "highscore", highscore)
end
end
The Autocrafter need one or more items depending on the recipe to craft the output. For example the rail recipe need 2 wood and 6 steel items to generate 18 rail items. This unequal ratio of items could lead to the situation that the Autocrafter inventory is full of wood but has no steel.
To prevent inventory overloads, the following "balancer" example can be used.
It reads the counters of the input Pushers to control the item flow ratio, which
in this case has to be 2:6
init() code:
-- tubelib numbers
IN_STEEL = "0761"
IN_WOOD = "0762"
-- Recipe: 6 steel + 2 wood gives 18 rails
NUM_STEEL = 6
NUM_WOOD = 2
MAX_DIFF = 10
$loopcycle(10)
-- Reset counter
$clear_counter(IN_STEEL)
$clear_counter(IN_WOOD)
loop() code:
-- read counters
cnt_steel = $get_counter(IN_STEEL)
cnt_wood = $get_counter(IN_WOOD)
$print("steel "..cnt_steel..", wood "..cnt_wood)
-- control ratio
if cnt_steel/NUM_STEEL > cnt_wood/NUM_WOOD + MAX_DIFF then
$send_cmnd(IN_STEEL, "off")
elseif cnt_wood/NUM_WOOD > cnt_steel/NUM_STEEL + MAX_DIFF then
$send_cmnd(IN_WOOD, "off")
else
$send_cmnd(IN_STEEL, "on")
$send_cmnd(IN_WOOD, "on")
end
For an email system you need a Central Server and a SaferLua Controller with Terminal per player. The Central Server serves as database for player name/node 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
- To send a message, enter the receiver name and the text message like
Tom: hello
into the Terminal - The Lua script will determine the destination number and send the message to the destination Controller
- All players who should be able to take part in the email system have to be entered into the Server form
init() code:
$loopcycle(0)
$events(true)
-- Start: update to your conditions
TERM = "27309"
CONTROLLER = "27310"
NAME = "Tom"
SERVER = "27312"
-- End: update to your conditions
$server_write(SERVER, NAME, CONTROLLER)
$server_write(SERVER, CONTROLLER, NAME)
loop() code:
-- read from Terminal and send the message
s = $get_term()
if s then
name,text = unpack(string.split(s, ":", false, 1))
num = $server_read(SERVER, name)
if num then
$send_msg(num, text)
$put_term(TERM, "message sent")
end
end
-- read message and output to terminal
num,text = $get_msg()
if num then
name = $server_read(SERVER, num)
if name then
$put_term(TERM, name..": "..text)
end
end