Forks:Lua Examples

From LinuxWorks Minetest Server Wiki
Jump to navigation Jump to search

On this page you will find an introduction to programming a Lua Controller and some example Lua programs.

About the Lua Language

This is not an introduction to programming or Lua. For all informations about Lua please note that Minetest uses the version 5.1 of the Lua language.

A short introduction to the Lua Controller is avaiable at http://mesecons.net/luacontroller/ .

A good starting to point to get familiar with the Lua language is the page http://lua-users.org/wiki/LuaDirectory in the official wiki of the Lua language.

For easy reference, the Lua 5.1 Reference manual is excellent.

Fundamentals about Lua Controllers

The introduction at http://mesecons.net/luacontroller/ describes all important limitations of the Lua Controller compared to the use of Lua outside of Minetest.

  • the code in the Lua Controller is (more of less) only some sort of interrupt handler, not a complete program
  • write as efficient and simple as possible
  • interrupts with less than 1s are not allowed
  • hiding the code is not possible
  • Lua Controllers only run in loaded/active areas
  • commands must be in if..then code blocks with event-related conditions
  • your code will be audited by moderators, any problematic code will be disabled (see also "coding style")
  • LuaATC tracks and Operation Panels from advtrains are similar to code, but remember that they run always - even in unloaded areas

Coding Style

  • make your code easy to read
    • use indenting
    • no complex/long conditions for if
    • use descriptive variable names
    • add comments
  • don't recalculate values, save them in variables
  • put some thoughts into your code, make it as efficient as possible

Examples

Blinking Lights

In case you need a configurable blinking light or a configurable timer for feeding any machines with items:

local time_on = 2   --  2 seconds on  
local time_off = 6  --  6 seconds off
  
if event.iid == "on" then
     port.a = true
     interrupt(time_on,"off")
     return
end

if event.iid == "off" then
     port.a = false
     interrupt(time_off,"on")
     return
end

if event.type == "program" then
     interrupt(1,"on")
     return
end

Code with comments.

Display the server time

To display the current time and date of the server add an LCD (name: "lcd") to the Lua Controller and use this program:

local  lcd_name = "lcd"  

if event.iid == "d" then
     dt = os.datetable()
     tz_name = ((dt.isdst and "CEST") or "CET")
     outputtext = string.format("SERVER TIME | %4i-%02i-%02i | %02i:%02i %s",dt.year,dt.month,dt.day,dt.hour,dt.min,tz_name)
     digiline_send(lcd_name,outputtext)
     diff = 60 - dt.sec
     if diff<1 then diff=1 end
     if diff>30 then diff = 30 end
     interrupt(diff,"d")
     return
end

if event.type == "program" then
     digiline_send(lcd_name,"SERVER TIME | STARTING") 
     interrupt(1,"d")
     return
end

Code with comments.

Simple Uranium Enrichment

For an easy enrichment of uranium dust is only one centrifuge needed. This is not fast but a steady and reliable system.

local delay = 2
local injector = "eject"

if event.iid == "c" then
     digiline_send(injector,{ count = 2, exmatch = true, slotseq = "rotation" })
     interrupt(delay,"c")
     return
end

if event.type == "program" then
     interrupt(1,"c")
     return
end

Code with comments and a picture of the machine setup.


More Examples

  • Simple Tree Machine
  • ...
  • ...

Advtrains Examples

  • Enforce a minimal distance between trains
  • ...
  • ...