Skip to content

Examples

Joachim Stolberg edited this page Jul 1, 2018 · 35 revisions

THIS IS WORK IN PROGRESS

Simple Counter

init() code:

a = 1

loop() code:

a = a + 1
$print("a = ", a)

end

Hello world

Hello world init() code:

a = Array("Hello", "world", "of", "Minetest")

$clear_screen("0669")

for i,text in a.next() do
    $display("0669", i+2, text)
end

Monitoring chest & furnace

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

$get_status("1034") is the command to read the status string from a tubelib compatible node, like Pushers, Harvester, Quarry, and so on.

$display("0665", 3, "Tree full") is the command,to write the text line "Tree full" in row 3 of the display with number "0665".

Stopwatch with highscore list

Stopwatch

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