-
Notifications
You must be signed in to change notification settings - Fork 17
Examples
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
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
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 disproportion 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 "flow control" example can be used.
It reads the item counters of the Pushers to control the item flow.
Before it goes into operation, the Autocrafter are flushed and all counters are reset.
init() code:
--------------------------------------------------------------------------------
-- Item flow control to prevent autostarter overloads
-- 2018-07-22 - v1.0 - first draft
--------------------------------------------------------------------------------
-- tubelib numbers
IN_STEEL = "0761"
IN_WOOD = "0762"
OUT_RAIL = "0763"
AUTOCRAFTER = "0760"
DISPLAY = "0765"
-- states
FLUSHING = 0
RUNNING = 1
$loopcycle(10)
-- flush autocrafter
state = FLUSHING
$send_cmnd(IN_STEEL, "off")
$send_cmnd(IN_WOOD, "off")
$send_cmnd(OUT_RAIL, "on")
$send_cmnd(AUTOCRAFTER, "on")
$clear_screen(DISPLAY)
loop() code:
-- wait until autocrafter goes standby
if state == FLUSHING then
$display(DISPLAY, 1, "flushing...")
sts = $get_status(AUTOCRAFTER)
if sts == "standby" then
-- start the flow control
state = RUNNING
$send_cmnd(IN_STEEL, "on")
$send_cmnd(IN_WOOD, "on")
$clear_counter(IN_STEEL)
$clear_counter(IN_WOOD)
$clear_counter(OUT_RAIL)
$display(DISPLAY, 1, "running...")
end
end
-- monitor IN/OUT pushers
if state == RUNNING then
cnt_steel = $get_counter(IN_STEEL)
cnt_wood = $get_counter(IN_WOOD)
cnt_rail = $get_counter(OUT_RAIL)
$display(DISPLAY, 2, "steel: ", cnt_steel)
$display(DISPLAY, 3, "wood: ", cnt_wood)
$display(DISPLAY, 4, "rails: ", cnt_rail)
-- add some offset for rails + material in the autocrafter
cnt_rail = cnt_rail + 50
-- 6 steel + 2 wood gives 18 rails
if cnt_steel/6 > cnt_rail/18 then
$send_cmnd(IN_STEEL, "off")
$display(DISPLAY, 6, "steel: ", "off")
else
$send_cmnd(IN_STEEL, "on")
$display(DISPLAY, 6, "steel: ", "on")
end
if cnt_wood/2 > cnt_rail/18 then
$send_cmnd(IN_WOOD, "off")
$display(DISPLAY, 7, "wood: ", "off")
else
$send_cmnd(IN_WOOD, "on")
$display(DISPLAY, 7, "wood: ", "on")
end
end