From Minetest Wiki
Jump to: navigation, search

Robot (basic robot).png
A mod
Type Learning (might find a better category latter)
Author rnd
Latest version 2017-08-03
Forum topic 15850
Source code GitHub
Technical name basic_robot

This is the user-documentation about the mod basic_robot (Forum, github) by rnd.

This page is also intended as an introduction to programming, using the programming-language lua with the robot.

Also, there is a 'live' tutorial-parcour about robots-programs, at the old ROBOTS-server,, and now at the new ROBOTS_SKYBLOCK,



basic_robot is a lightweight programmable robot for minetest, usable in multiplayer.

'lightweight': the mod only has 2 objects: robot and remotecontrol

The robot can move around, sense the blocks, dig, build, and much more.

Since version 12/21a, there is also a remote control (but this is still WIP).

Users can write programs for the bot in Lua.

The system uses a spawner ("cpu-box") that the player has to place on the ground.
This spawner presents a form for controlling, programming and running the robot ("worker").
Only this worker moves around, and it has a limited range (about 23 nodes from player and spawner).

A keypad from the mod basic_machines can be set up to start a robot
with a button-push by other players, and for keyboard-entry.

This is useful for robots that perform a task for other players, e.g. as a shop, mailbox, chatbot, game, etc.

How to get

To obtain a spawner in creative mode, use the command "/giveme basic_robot:spawner".
For a remote control, use the command "/giveme basic_robot:control".


Name Ingredients Input → Output Description
Robot spawner Mese Crystals
+ Steel Ingot
+ Stone
Mese Crystal.png
Mese Crystal.png
Mese Crystal.png
Mese Crystal.png
Mese Crystal.png
Mese Crystal.png
Steel Ingot.png
Robot spawner (basic robot).png
Used to spawn a robot-worker.
Robot remote control Stick
+ Mese Crystal
Mese Crystal.png
Robot remote control (basic robot).png
Used to control a robot.

Controlling Form

Rightclicking the spawner opens a form with buttons and a textbox for writing a program.

controlling form for basic_robot
  • Button "Save" saves the program. This must be done before starting a program.
Note: Saving is not permanent - there is no file to save to, just a quick syntax-check.
Also, picking up the spawner clears the textarea with the code.
So, use cut&paste to save your code to a texteditor, such as notepad.
  • Button "Start" creates a robot-worker (on top of the spawner) that starts executing the program.
The robot looks like a simple box, with a face on its frontside, and an arrow on top.
  • Button "Stop" stops the program and removes the robot-worker.
  • Entryfield id for controlling several robots.
Players without the priv 'robot' can only use the ids 1 and 2.
This means they are only allowed to run 2 robots at the same time.
  • Button "Inventory" opens the inventory of the bot.
The inventory has 8*4 slots, same size as a chest.
  • Button "Library" opens a "bookshelf". The robot can read and write books here.
This can be used for programcode, input and output.
  • Button "Help" shows some helptext.
  • Press ESC to exit the form.


Each spawner can only run one bot at a time.
To pick up the spawner, its inventory and library must be empty (like a chest).

While the robots is running, rightclicking it will also open the form
(without the Start-button), so you can stop it, and access its inventory.

The code of a program is executed repeatedly, about once every second.



There is a server dedicated to robots:

Address[:Port] MaxPlayers Version/Subgame/Mapgen Name Description Flags 15 0.4.15/minetest/v7 ROBOTS program your own robot Ded Dmg PvP


  • Cre - Creative mode
  • Ded - Dedicated Server
  • Dmg - Damage enabled
  • Far - Players can see far away names
  • Liq - Finite Liquids
  • Pvp - Players can damage other players
  • Pwd - Players need to set a password at first connect
  • Rol - Rollback enabled

See also: Serverlist and Servers

Robot-commands - Part 1

List of simple commands, to start learning how to use the robot:

  • say("Hi") - output text to chat.
  • self.label("Hugo") - assign a name to the robot.
  • turn.left() - rotates bot 90°.
  • turn.right()
  • move.forward() - move one step in a given direction, if the path is clear.
  • move.backward()
  • move.up()
  • move.down()
  • move.left()
  • move.right()
  • dig.forward() - get/mine the block in the given direction.
!! Caution, the robot can dig stuff like its own spawner, chests owned by the player etc. !!
  • place.backward("default:dirt") - build a block of the specifed stuff in a given direction.
  • p = self.pos() - returns the position of the bot, as a table p with entries for x, y, z.
Note: in an earlier version of basic_robot, this command was "selfpos()"
  • n = read_node.forward() - returns a string with the name of the block in a given direction.


Example programs - simple

First, some simple programs with explanations, as an introduction to show how the robot and programming works.



The robot will repeatedly say "Hi", while spinning atop the spawner.
To stop it, rightclick the robot or spawner, and press the Stop-button.

Changing the program while the robot is running has no effect.


if (i == nil) then 
   say("Hi !")
   i = 0 
i = i+1

Outputs "Hi !" (only once), then start counting and turning.

== is the check to compare the values on both sides, i.e. "if i is-equal-to nil".
= is an assignment, i.e. "i=0" means "put the value 0 into the variable i".

To calculate i+1, the variable i must have a value assigned first.
And because all the code is executed again every second,
this first assignment must be done only once.

(Otherwise, we couldn't count to 2)

At the start, no variables exist yet, so we can test for nil ("no-value").

Note: nil is different from any string or number !


 if i==nil then i="Hi !"; say(i); i=0 end
 i=i+1  say(i)

Same as above, but with multiple statements in one line,
and some variations (e.g. braces and ";" are optional).

In lua, the value of a variables can be of any type.
Here, i first is assigned the string "Hi !", and later a number.

A quick way to clear the code in the textarea is control-a, then delete or backspace.
Also, picking up the spawner, and placing it again will clear the code.


This shows some program that works perfectly well, but still shoots the player's foot :-)


The robot keeps digging down, until stopped or until it finds a cave, or an unloaded map-block.
With the first dig, it will also destroy its spawner.
So, to get at the stop-button, the user has to jump into the hole to get at the robot.
(Quickly, otherwise the player also gets falling damage !)

Leaving the game, and entering again probably also stops the bot...

(Maybe a bug: without the spawner, the inventory of the robot is not accessible)


 p = self.pos()  -- returns a table, with elements x, y, and z
 say("Position: " .. p.x .. "," ..  p.y .. "," ..  p.z)

Now we go up, instead of down.
After stopping the robot, and pressing start again,
there will be a block of dirt above the bot from the first go.
We need to dig away this block so that the bot can move up again.

! Caution, check the correct spelling of the blocks you want to build/place !

The command "say()" outputs a single string, but several strings can be concatenated with '..'.

Numbers are automatically converted to strings for output (e.g. the number in p.x).

With the robot announcing its position, the player can use a command like "/teleport 11,12,13" to get at the top of the column that was built.

Note: y is used as the height here in minetest.

Clouds fly at a height of about 120.

Use left-shift to sneak to the edge of the block to look down without falling.
Use the command "/home" to get down again.

Make sure to "/sethome" before teleporting up.

Also, read the documentation, i.e. about "fly", "noclip", and "bones".


 if (i==nil) then say("Demo1"); i=0 end
 if read_node.forward()=="air" then

If nothing ("air" mean nothing solid) is in front of it, the robot moves forward, otherwise it turns.
The robot can "fly" one block above the ground, like the player can reach by jumping up. The bot just doesn't need to bounce up & down.

That means, if the robot reaches a place where the ground one step ahead is more then one block lower, he cannot move forward.

With the above program, he just stops moving, because there are no instructions in the code to handle that case.


 local function report_pos(msg)
   p = self.pos()
   say(msg .. " position x=" .. p.x .. " y=" .. p.y .. " z=" .. p.z)
 if (i==nil) then say("\nDemo2"); i=0 end
 n = read_node.forward()
 if n~="air" then
   report_pos("Now at") 
   say("found "..n..", turning")
   if move.forward() then 
     i=i+1  -- move was ok
     report_pos("cannot move at")
     --self.remove()  -- stop program, and remove robot


  • ".." is the lua-command for concatenating strings.
  • "\n" is newline.
  • "~=" is the check for not-equal.
  • "--" starts a comment. Text after this until the end of the line is ignored.
Here, we have also commented-out the statement "self.remove()",
so that it doesn't get executed.

This is essentially the same program as 'Run1' above, but with more checking and reporting:
now there is a check if the move was successful, and output to report the position.

Because we want to report the current position of the bot at more then one place in the code,
the common instructions for that have been moved into a function.
When called, the function gets a string as a parameter, that is used in the message that is generated.
There is no return-statement at the end of the function, because we don't need a result.

New pages




Spawner and basic_robot, remote control with colored buttons

A remote control (rc for short) can be used to give orders to a robot.

It has 2 modes:

  • manual (leftclick) - without any programming, it shows a form with buttons for moving, turning and digging.
  • programming (rightclick) - shows a small textarea for entering a few lines of code, and a SAVE-button.

Those remote controller are very useful, see the extra page Mods/basic_robot/RC for more.

But now, for a short demonstration, get one spawner ("/giveme basic_robot:spawner" ) and 2-3 remotes ("/giveme basic_robot:control 2").
Place the spawner on the ground, and the remotes on the hotbar.

Rightclick the spawner, and enter this program:


 if (i==nil) then say("RC-demo"); i=0; talk=1 end
 i = i+1
 if talk>0 then say(i) end

Press SAVE, then press START.
The robot appears on top of the spawner, greets, and starts to count.

Now select the first rc and leftclick: the form for 'manual'-mode appears, with buttons to steer the robot around.
Try it. To stop, press ESC.

Note: "Left" means move-left, "TLeft" means turn-left. "Go" means move-forward.

Now rightclick the rc, without pointing at some node.

The programming form pops up, with a textarea and a SAVE-button.

Enter a simple program, like this line of code:



Then press SAVE, and (with this rc still selected) leftclick.
Instead of the manual-mode-form with buttons popping up as before,
the robot executes the line of code entered into the form, once.
In the chat-text a line with "Hi" should appear between all that counting.

Rightclick the rc again, and change the code in the textarea to:

RC2 - ReportPosition

p  = self.pos()
say("Step="..i.." Position x="..p.x.." y=".. p.y.." z=".. p.z)

Save, then leftclick - the robot should report its position, as well as the running counter from the program RC0 above.

(... Todo: rightclick with remote on a node ...)

With another 'empty' rc you can steer the robot via the manual-buttons,
and by selecting and clicking the rc with the code, you get the report.

Now we use another rc to to deactivate the "say"-instruction in RC0, to stop cluttering the chat:

RC3 - Talk OnOff

if talk>0 then talk=0 else talk=1 end

This switches the contents of variable talk with each leftclick,
and so controls the output from RC0.

RC4 - Paving/bridgebuilding

Paving one step in front of the robot:


This also works on water, or thru the air, to build bridges.
The robot needs building-material in its inventory.

Example programs - not-so-simple

Now, we show some programs that might be actually useful.

Build1 - Fence

This program builds a small, simple fences around the spawner, with a gate on one side.
Here, the idea is to divide the whole work ("build a complete fence")
into a single, simpler task ("build one corner") that just needs to be repeated.

 local function init(progname)
  if (stat==nil) then
   i=0  stat="ok" w=0
 local function work()
   w=w+1  say("working on part #"..w)
 if w==3 then
 if w>=4 then
   stat="Done."  say(stat)  self.remove()

The instructions in work() do one corner of the fence. After executing it four times, the fence is done.
The fence-gate is placed after the 3rd corner, replacing one of the fenceposts.

The move&turn-instructions in init() get the bot to a proper starting position.

Build2 - Fence2

More fences, and a program more organized.

 local function init(progname)
   if (stat==nil) then 
     i=0  stat="ok"  w=0  debug=0
     s1=0  stuff1="default:stone"
     s2=0  stuff2="default:wood"
     s3=0  stuff3="doors:door_wood"
     s1=0  stuff1="default:fence_wood"
     s2=0  stuff2="doors:gate_wood_closed"
     s3=0  stuff3="flowers:rose"
 local function report_pos(msg) 
    p = self.pos()   -- table p with entries for x,y,z
    say(msg.." Position x="..p.x.." y=".. p.y.." z=".. p.z) 
 local function _say(msg) 
   if debug>0 then say(msg) end
 local function work(s)
   if (stat~="ok") then return end
   _say("work#"..w.." : "..s)
   while #s>0 do
     c1= string.sub(s,1,1)  -- get first char from workstring
     if (c1=="f") then move.forward() end
     if (c1=="u") then move.up()   end
     if (c1=="d") then move.down() end
     if (c1=="r") then turn.right()   end
     if (c1=="l") then turn.left()    end
     if (c1=="-") then
       n = read_node.down()  _say("dig: "..n)
     n = read_node.down()  _say(i.." : "..n)
     if n == "air" then 
       if (c1=="1") then place.down(stuff1) s1=s1+1 end
       if (c1=="2") then place.down(stuff2) s2=s2+1 end 
       if (c1=="3") then place.down(stuff3) s3=s3+1 end 
       if c1=="3" then _say("! cannot place : "..c1.." over "..n) end
     s = string.sub(s,2)  -- remove first char from workstring
   end --while
 init("UniBuilder v0.4")
 --work("F-LF-")  --cleanup
 work("F1LF1 ")  --build
 if w==2 then  work("-2")  end
 --if w==2 then  work("-")  end  --cleanup
 if w>4  then  work("FRF") w=0 end
 _say("w="..w.. " s1="..s1.." s2="..s2)
 if s1>=28 and s2>=4 then  
   say("Done.")  self.remove()  


This needs at least 7x7 clear spaces of flat terrain. The program builds some fences and gates.
It is a variation of the above program "Build1", building the same fence 4 times at different places.

The code is organized into several functions:

  • init() - set up variables, assign values, announce the program-name
  • report_pos() - report the position of the bot
  • _say() - a wrapper around say(), for easy switching on/off printing messages while testing
  • work() - defines one-letter-shortcuts for moving, digging, building etc.
Note that this work()-function has no fixed workload as in the previous Build1,
and gets a string with commands as a parameter.

After the functions follows the actual logic for building the fences.

There are some commented-out "cleanup"-statements to undo the built, and remove the fence.
(You need to stop the robot by hand when running a cleanup)

Fence-gates don't align themselves with the nearby fenceposts, and the robot cannot change that.
So the player has to use a screwdriver to finish this (or dig & rebuild the mis-aligned gates).

Build3 - Instant shelter

A small, safe place.

The robot builds a shelter around the spawner, like a phone-booth.

 if not s then s="Instant shelter v1.6"  say(s)
 --m0="default:dirt"  m1="default:glass" m2="default:stone"
 --m0="default:dirt"  m1="default:glass" m2="default:wood"
   m0="default:dirt"  m1="default:glass" m2="default:cobble"
   m8="default:sand"  m9="default:torch"  
   mS=m0  mC=m2   -- material for sides and corners
 dig.forward()  place.forward(mS)  turn.angle(45)  i=i+1  say(i)
 dig.forward()  place.forward(mC)  turn.angle(45)  i=i+1  say(i)
 if i== 8 then move.up()  place.down(m8)  mS=m1 end
 if i==16 then move.up()  mS=m0 end
 if i>=24 then
   say(s .." done.")  self.remove()  say("Bye")

Usage: if you need some protection NOW, set the spawner on the ground,
stand on it, put the building-materials into the spawner, start the program
and the robot builds the shelter around you and the spawner.

In the picture, the side are built with dirt, to make it easier to dig out.

But you could also use the robot for digging out.

Note: the robot can also turn at an angle, not just left and right.
This is used here to built the blocks at the corners, without moving the robot.

Build4 - House1

Now we try to build a house.
A hut. Ok, it's a really small hut :-)

local function init(progname)
 if (i==nil) then 
  i=0  stat="ok"
local function work(s)
  say("work: "..s)
  while #s>0 do
    c1= string.sub(s,1,1)  i=i+1
    if (c1==".") then say("Stop.") self.remove() end
    if (c1=="-") then draw=0 end
    if (c1=="+") then draw=1 end
    if (c1=="u") then move.up()    end
    if (c1=="d") then move.down()  end
    if (c1=="l") then move.left()  end
    if (c1=="r") then move.right() end
    if (c1=="<") then turn.left()  end
    if (c1==">") then turn.right() end
    if (c1=="f") then 
      if draw>0 then place.backward(stuff) say(stuff) end
    s = string.sub(s,2)
init("UniBuilder v0.7")
-- ?? execution limit

BTW, to see some 'happy accidents', replace the last lines with this:



Essentially, we use the functions from the above program "Build2",
and write some new logic for building a house instead of a fence.

The procedure is quite similar: the robot moves around like the shape of the walls, and builds a block after each move.
But now it also needs to move up, and repeat this for each layer of the wall.
Finally, some extra processing to close the roof.

Build5 - another housebuilder

'Hut-Builder 3x4, v0.2' at work


 local function init()
   if i~=nil then return end
   progname="Build hut 1x3, v0.1"
   prg = "!<fd>"
   prg = prg .. "+fff>FF>fff>uFF>"
   prg = prg .. "+fff>FF>ffuf>F,F>"
   prg = prg .. "+f&f&f>FF>fu-."
   progname="Hut-Builder 3x4, v0.2"
   prg = "!<fd+f>"
   prg = prg .. "FFFFF>ffff>"
   prg = prg .. "FFFFF>fuf-F+f>"
   prg = prg .. "FFFFF>ffff>"
   prg = prg .. "FFFFF>"
   prg = prg .. "uffff>"  .. "F&F&F&F&F>"
   prg = prg .. "ff&f&f>" .. "F&F&F&F&u"
   prg = prg .. "->f_f>"  .. "_f_."
   i=0  cs=0  stat="ok" 
   draw=0 raze=0
 local function report_pos(msg) 
    p = self.pos()   -- table p with entries for x,y,z
    say(msg.." Position x="..p.x.." y=".. p.y.." z=".. p.z) 
 -- get first char from prg, and do 1 step
 local function work1()
  if #s>0 then
    c1= string.sub(s,1,1)  
    say(i.." work1: "..s.." : "..c1)
    if (c1==".") then say("Stop.") self.remove() end
    if (c1=="!") then 
      report_pos(progname.." now at") 
    if (c1=="+") then draw=1 end
    if (c1=="-") then draw=0 end
    if (c1=="(") then raze=1 end
    if (c1==")") then raze=0 end
 --$%& ^|_
    if (c1=="$") then place.front(stuff) cs=cs+1 end
    if (c1=="%") then place.left( stuff) cs=cs+1 end
    if (c1=="&") then place.right(stuff) cs=cs+1 end
    if (c1=="^") then place.up(   stuff) cs=cs+1 end
    if (c1=="|") then place.down( stuff) cs=cs+1 end
    if (c1=="_") then place.forward_down(stuff) cs=cs+1 end
 -- /() =?  * #' ,;.:
    if (c1=="'") then dig.up()      end
    if (c1==",") then dig.down()    end
    if (c1==":") then dig.right()   end
    if (c1=="*") then dig.forward() end
    if (c1==";") then dig.forward_down() end
    if (c1=="l") then move.left()  end
    if (c1=="r") then move.right() end
    if (c1=="<") then turn.left()  end
    if (c1==">") then turn.right() end
    if (c1=="f") then 
      if raze>0 then dig.forward() end
      if draw>0 then place.backward(stuff) cs=cs+1 end
    if (c1=="u") then 
      if raze>0 then dig.up() end
      if draw>0 then place.down(stuff) cs=cs+1 end
    if (c1=="d") then 
      if raze>0 then dig.down() end
      if draw>0 then place.up(stuff) cs=cs+1 end
    prg = string.sub(s,2)


Running Build3 revealed some problems with the sandbox of basic_robot.
So, this is an alternative approach, with a simplified, restructured program:

  • init() -- only runs once, to do all the setup.
  • work1() -- runs once every tick, and does one step of the program in prg.

We still use the basic idea of a 'turtle' / l-system, that is used as a pen/brush for drawing,
that is controlled with a program consisting of 1-character-commands.

But now, we process only one command per 'tick'.
This could be optimized a bit, e.g. some commands don't need time,
so a second command could be done during the same tick.


Upper- and lowercase are considered the same,

so both "ffffffffff" and "fffFFfffFF" do the same.
  • F,B, U,D, R,L - Move forward,backward, up,down, right,left
  • <,> - Turn left, right
  • +,- - Switch building on/off
  • (,) - Switch digging on/off
  •  ! - Announce position
  • . - End program
  • ' , * ; - Dig up, down, forward, forward-down
  • $ % & ^ | _ - Build forward,left,right, up,down,forward-down
  • 0,1,2, etc. - Select material to build (not used yet)
  •  :,[,],/ - reserved for future use (e.g. subroutines)

Building the house

The entrance will be at the position of the spawner,
and the house/hut will be built in the direction the robot spawns.

Building the program

The easy way to develop a building-program is by using a remote, and copy/paste.
We start with a short program in init(), like

prg = "!<fd+"

that lets the bot just climb down from the spawner, and turns on 'drawing' = building.

As this program is run, chars are removed from the front, and the corresponding commands executed.
When all chars have been used (and the program has not ended with "."), the bot just sits there, waiting.
Now we use a remote to give the bot the next few instructions to build:
Rightclick the remote (without pointing at the bot or spawner), and enter into the textbox


and click the SAVE-button.

Then leftclick the bot with the remote, to let him execute the commands from the textbox.
Watch the bot, correct any errors using an 'empty' remote.

Continue with the next few building-instructions, e.g.


Etc. until the building is complete.

Copy&paste all the snippets into an editor, make a single string from it, e.g.

  prg = "!<fd+f>"
  prg = prg .. "FFFFF>ffff>" .. "FFFFF>fuf-F+f>"
  prg = prg .. "FFFFF>ffff>" .. "FFFFF>" .. "uffff>"  
  prg = prg .. "F&F&F&F&F>"  .. "ff&f&f>" .. "F&F&F&F&u"
  prg = prg .. "->f_f>"      .. "_f_."

and finally put that finished building-string into init().

Example programs - useful

Now some programs that might be actually useful during play.


Programs that help with farming.


When a player picks up leaves, or a leave decays,
there is a small chance (about 5%) that it drops a sapling.
So, when harvesting a tree, a player can normally expect to find 1-2 saplings.

This program just repeats placing and picking up leaves,
until it has no more leaves in its inventory.

 -- Mike's sapling-mill v1.0
 -- Turn leaves into saplings.
 leaves = "default:jungleleaves"
 leaves = "default:pine_needles";
 leaves = "default:leaves"
 dig.forward()  -- first dig & collect
 pickup(8)      -- collect saplings that dropped on the ground
 if not place.forward(leaves) then say("empty !") self.remove() end

The mill stops when the place-command fails, i.e. it has run out of leaves.

But that command can also fail for other reasons.

Note: For proper operation, put some leaves into the robot's inventory before starting.
and set/uncomment the matching statement 'leaves="..."'.

(When running as admin or in singleplayer, it might work even without leaves:)

Sapling-Mill 2

A more advanced version of the sapling-mill:

 if not s then s="Sapling-mill v1.2c" -- say(s)
 --leaves = "default:grass"        --> wheat-seeds / server-error ??
 --leaves = "default:grass_1"      --> wheat-seeds 
 --leaves = "default:junglegrass"  --> cotton-seeds
 --leaves = "default:acacia_leaves" --> acacia-tree
 --leaves = "default:aspen_leaves"  --> aspen-tree
 --leaves = "default:pine_needles"  --> pine-tree
 --leaves = "default:jungleleaves"  --> jungle-trees
   leaves = "default:leaves"        --> sappling for apple-trees
 dig.forward()	-- first dig & collect, then place
 pickup(8)     	-- collect saplings on ground that dropped after dig
 if place.forward(leaves) then 
 if fail>4 then
   say(s.." done with "..leaves)  self.remove() 

Here we have a more complete list of leave-types,
and the mill keeps running until all placed leaves have been collected again,
and tried again, until they all have been used up.



Harvesting and re-planting wheat, cotton etc. gets tedicious pretty fast, so let's automate that.

First, we try a simple 'remote lawnmover'-mode.
This uses one robot, with no program, and two remote-controllers.

  • RC#1 is 'empty', and is used to steer the robot around.
  • RC#2 gets programmed (rightclick) with this code :
 ripe="farming:wheat_8"; seed="farming:seed_wheat" 
 if read_node.forward() == ripe  then dig.forward() end
 if read_node.forward() == "air" then place.forward(seed) end
Note: on the ROBOTS-server, farming also needs fertilizer.
Placing fertilizer is not included in this program !

After manoevering the robot to the first ripe fruit with RC#1,
we use RC#2 (with leftclick) to move the bot forward while harvesting & planting.

This will only harvest ripe fruits, and only plant seeds on empty spaces.
But it does not check if that space has wet farming soil.

The advantage is, harvesting is now somewhat automated, we can use any terrain,
and we don't need to build walls or fences around the field.

The disadvantage is, this is still a lot of work, and gets tedicious again.


For a more 'hands-off' mode of harvesting, we need to automate the movement of the robot.

One way is to build a wall or fence around the field, and the robot turns around if it hits that wall.




Harvesting wheat, cotton etc. all works the same, because the plants grow directly on the ground,
and can be harvested with just one action.

Harvesting trees is harder, because the robot has to move in the 3rd dimension to get at all parts.
Also, different trees grow in different forms.




Programs that help with mining.

Mining101 - simple digger1x1

This program digs a 1x1 tunnel straight ahead.
The drops of the mined nodes (i.e. the resulting ore) go to the storage of the robot.

Place the spawner at the place where you want to start digging, and press the start-button.

 -- simple digger1x1
 if not i then  -- Init:
   turn.left()  -- initital positioning: aim robot in a different direction
 -- Work:
 i=i+1  say(i)
 if i<=20 then

The robot spawns facing in a fixed direction, to have him dig in some other direction
we need to turn him in the init-part of the program.

Or use a remote-control for that.

The counter is just to ensure that the bot doesn't run away too far.

To get a walkable tunnel, just run the program a 2nd time,
with the command "move.up()" uncommented.


 -- simple digger2x1
 if not i then  -- Init:
   turn.left()  -- initital positioning: aim robot in a different direction
 -- Work:
 i=i+1  say(i)
 if i<=20 then
   -- ??
   dig.up()       -- !! doesn't work with setting 'maxdig=1'
   say("stop")  self.remove()

Same as above, but now we dig a walkable 2x1 tunnel in one pass.


Mining103 - tunnel 2x1

Digging a 2x1 tunnel forward.

This is another variant with turtle-like movement, using a string of 1-character-commands.

 -- rnd 2017-01-23 / hajo 2017-01-26
 if not commands then
      --turn.left()  -- aim robot in a different direction
 script = "DuDdf"  -- Variant 1 - uses only dig-forward
 	script = "^Df"    -- Variant 2: DigUp, DigForward, MoveForward
	commands = {
	["f"] = function() move.forward()  end,
	["b"] = function() move.backward() end,
	["l"] = function() move.left()  end,
	["r"] = function() move.right() end,
	["u"] = function() move.up()   end,
	["d"] = function() move.down() end,
	["<"] = function() turn.left()  end,
	[">"] = function() turn.right() end,
	["D"] = function() dig.forward() end,
	["^"] = function() dig.up() end,
	i=1; n=string.len(script)
 c=string.sub(script,i,i); commands[c]();
 if i>=n then i=1 else i=i+1 end

commands is an array containing functions, indexed by characters.
These functions contain the lua-code to execute for that (turtlecommand-)character.

script is a string that contains the (turtle-)instructions for the bot.

With each tick, one char from the script is extracted,
and the corresponding function from commands is executed.


Programs that build something.

BoxHouse - BasicHousing

This is a house-builder that uses the keyboard-blocks of the robot.
The advantage is, the house doesn't need any materials.
The disadvantage is, the result looks like a plastic toyhouse :)

 if not pn then pn="BoxHouse 0.5b"  -- hajo, 2017-03-03
  pos = self.spawnpos();
  n=2; h=2               -- !! set size here !!
  c=2 -- 1=white 2=grey 3=red 4=green 5=blue 6=yellow
  y= h; c=3; -- roof
  for z=-n,n do  for x=-n,n do 
    keyboard.set({x=pos.x+x,y=pos.y+y,z=pos.z+z}, c)
  end  end 
  y= -1; c=2; -- base/floor
  for z=-n,n do  for x=-n,n do  keyboard.set({x=pos.x+x,y=pos.y+y,z=pos.z+z}, c) end end
 function wall(x,y,z)
  if x==0 and y==0 and z==0 then return -1 end  -- don't overwrite spawner !!
  if y<0        then return 4 end -- 2:grey, base
  if y>=h       then return 3 end -- 3:red, roof
  a=math.abs(x); b=math.abs(z)
  if y==1 and (a==0 or b==0) then return 0 end  -- door/window
  if a==n and b==n then return 5 end  -- 5:blue, corners
  if a==n then return 6 end -- 6:yellow, E,W walls
  if b==n then return 1 end -- 1:white,  N,S walls
  return w
  for y=0,h do  for z=-n,n do  for x=-n,n do 
    w=wall(x,y,z); c=w;  --say(x.."/"..y.."/"..z.. " --> "..w)  
 --c=0  -- 0:air, for cleanup
  if w>0 then keyboard.set({x=pos.x+x,y=pos.y+y,z=pos.z+z}, c) end
  end  end  end
  say("done");  self.remove()

Put the spawner at the intended center of the house,
set the size (n=1 .. n=4), and start.

Any material at the building-site gets overwritten with the key-blocks,
so if you need some of that dirt, dig it out before starting.

If you uncomment the 'cleanup'-line, a (misbuild) house will be removed.

Note: this works correctly in singleplayer, and for admin.
Otherwise, it stops after some blocks with 'execution limit exceeded'.

h is the height of the roof. The spawner is at y=0, the floor at y=-1. n is the size, and is used as -n .. +n, with the spawner at x=0, z=0.

n inner size outer size remarks
1 1x1 3x3 see also 'Instant shelter'
2 3x3 5x5 'Basic housing'
3 5x5 7x7 'Decent housing'
4 7x7 9x9 'Roomy housing'

There is a big x/y/z - loop over the whole size of the house,
and the function wall(x,y,z) returns a code that decides
what type/color of block to build at every location.

Each wall has one hole/window at the center, and you can decide yourself
where to dig another block, to make it into a door.


Misc. useful programs.

Simple Shop

This is a simple shop, to be used on a server in multiplayer.
The shop would be set up in a secured area (e.g. in range of a protector),
and we also need a normal chest and a keypad (from the mod basic_machines).
Also, some lights, and placing a sign for announcing the shop should be considered.

 --say("Shop out of order ... check back soon !"); self.remove()
 if not pn then pn="SimpleShop v0.2" -- Hajo, 2017-03-03+
  if p==nil then s="Hello !" else s="Hello "..p[1].." !" end
  -- m1="default:torch";  m2="default:dirt" --Test
  m1="default:sword_steel"  -- Example 1: sell 1 sword for 1 gold
  --m1="basic_machines:keypad"  -- Example 2: sell 1 keypad for 2 steel
  --m2="default:steel_ingot 2"
  ok1 = check_inventory.self(m1,"main") -- look at robot's inventory
  if not ok1 then say("Out of stock: "..m1); self.remove() end
  ok2 = check_inventory.up(m2,"main")   -- look at chest above robot
  if not ok2 then say("No payment in chest ("..m2..")"); self.remove() end
  say("Selling "..m1.." for "..m2) 
  say("Thanks for your purchase !");

The robot's inventory stores the items to be sold, as well as the payments.
The chest is for the items to be exchanged,
and the keypad-button activates the shop.

This shop simply exchanges one item from the chest for one other item from the robot's inventory.

But it would be easy to change the logic to any combination of items.

Robot-commands - Part 2

Date- and time commands:

  • os.time() -- returns the current date and time, coded as the number of seconds since the epoch.
  • ([format [, time]]) -- Returns a string or a table containing date and time, formatted according to the given string format.
See manual at or
  • os.clock() -- returns cpu-time used
  • os.difftime (t2, t1) -- Returns the difference, in seconds, from time t1 to time t2 (where the times are values returned by os.time)

Time, date & clock          -- default-format
 say(""..s)"%Y-%m-%d %H:%M")  -- Year, month, day, Hour, Minute
 say(" "..s)
 -- from
 function duration (secs)
    local units, dur = {"wk", "d", "hr", "min"}, ""
    for i, v in ipairs({604800, 86400, 3600, 60}) do
        if secs >= v then
            dur = dur .. math.floor(secs / v) .. " " .. units[i] .. ", "
            secs = secs % v
    if secs == 0 then 
        return dur:sub(1, -3)
        return dur .. secs .. " sec"
 s=os.time()  -- time since jan 1, 1970
 say("--> "..d)
 s=os.clock()  -- time since program started
 say("==>".. duration(s) )

This program outputs lines like 13:14
os.clock=123.456 --> 2 min, 3.456 sec

The function duration() converts these numbers into weeks, days, hour, minutes.

Robot-commands - Part 3

Commands for inventory-access :

  • pickup(8) - pick up items from the ground within range
e.g. saplings that dropped while harvesting trees
  • take.forward(item) - take items from a chest in the given direction
  • insert.forward(item) - put item into a chest
  • check_inventory.forward() - check an inventory for an item:
    • ok=check_inventory.forward(item,inventory) - returns true if item exists
    • item=check_inventory.self("",inventory,index) - returns item at index-position


This program can list the robot's own inventory (inside the spawner),
or the contents of a chest.

 if not pn then pn="Inventory-Lister1"; say(pn)
   move.forward() move.down() --turn.left() -- move into position, to look at chest
   inv="main"  -- furnace also has an inventory-slot "fuel"
 i=i+1  --say(i)
 m=check_inventory.forward("",inv,i) -- look at chest in front of robot
 --m=check_inventory.self("",inv,i)      -- look at robot's own inventory
 if m=="" then
   say(i..": "..m) 
 if i>=32 then
   say(free.." free slots")

This version is quite slow, because it checks one slot per tick.

Inventory-Lister2 - Fast

 if not pn then pn="Inventory-Lister2"; say(pn)
 --move.forward() move.down() --turn.left() -- move into position, to look at chest
  for i=1,32 do
    m=check_inventory.self("",inv,i)      -- look at robot's own inventory
    m=check_inventory.forward("",inv,i)   -- look at chest in front
    if m=="" then free=free+1 else say(i..": "..m) end
  say(free.." free slots")

This version runs faster, but might crash because of the sandbox-restrictions.


Get all items from a chest, and put them into another chest.
For reorganizing, when you have many chests filled with stuff.


Part1: move to chest#1, and take out all items.

That is, move them into the robot's own inventory.
 if not pn then pn="Chest mover1"; say(pn)
 --get robot next to chest#1:
   move.forward()  move.down()
   move.forward()  turn.right()
   inv="main"  free=0 c=0  i=0
 i=i+1  --say(i)
 if m=="" then
   take.forward(m); c=c+1
   --say(i..": "..m)
 if i>32 then
   say("Taken:"..c..", free:"


Part2: move to chest#2, and put all items from the robot's inventory into that chest.

 if not pn then pn="Chest mover2"; say(pn)
 --get robot next to chest#2:
   move.forward()  move.down()
   move.forward()  turn.left() -- move.forward()
   inv="main"; free=0; c=0; i=0
 i=i+1  --say(i)
 if m=="" then
  if ok then
   say(i..": "..m) 
   say(i.." error: "..m); self.remove()
 if i>32 then
   say("Done:"..c..", free:"

Obviously, the move-commands need to be adjusted for the locations of the chests.

Chest-Mover3 / DropBox

Swap contents of box with robot's inventory

 if not pn then pn="DropBox v0.3"; say(pn)
  m=check_inventory.forward("",inv,1)  -- look at slot1 of chest in front
  if m=="" then 
    say("chest is empty")  -- move robot's inventory to chest
    for i=1,32 do
      if m=="" then
        -- say(i..": "..m) 
   say("1: "..m) 
    say("getting chest")
    for i=1,32 do
      m=check_inventory.forward("",inv,i)  -- look at chest in front
      if m=="" then 
   	--say(i..": "..m) 

This could be used as a shared chest in multiplayer, to exchange stuff between groupmembers.
The robot would be activated by a password-protected keypad.


  • write a book of the chest/inventory-contents.
  • make a version of the mover that uses the remote control.
  • filter while moving
  • count total / stats (e.g. "default:stone : 3000 items in 31 stacks")

Robot-commands - Part 4

Commands about books :

  • title, text = -- returns the title and contents of the book at i-th position in the library,
with i in the range 1..32
  • book.write(i,text) -- writes text into the book at i-th position in the library.
Those books get the title "program book".
  • display_text(text,linelen,size) -- show text on the "face" of the robot
size=1 means the robot stays its normal size of 1 cube.
With size=2, the robot shows the display at double size.
  • read_text.forward(s) -- reads text of signs, chests and other blocks.
s is optional, defaults to "infotext", to be used for other meta.
It is possible to use the text read as code to execute (see next chapter).


Lists the books in the library of the spawner.

  if i==nil then
   t="title T"
   c="contents C"
   book.write(9,t,c)   -- creates/overwrites the book in slot 9 of the library
   t="(unknown title)"
  if i>32 then say("done.") self.remove() end
  t, c =
  if c==nil then c="(empty)" end
--say("Book #"..i.." : "..c)
  say("Book #"..i.." : "..t.." - "..c)

To access books in a spawner/bookshelf at "home", you need to put in the coordinates.

Big news-display

Show text from the book in slot#1 on a big screen.

"\n" is a newline.
if not msg then  -- Write news-text to book
  msg="*** NEW ***\n\n"
  msg=msg.."Really big news-\n display !\n"
  msg=msg.."Never seen before !\n"
  msg=msg.."Now available !\n"
  msg=msg.."Low cost !\n"
  msg=msg.."Big display !\n"
  msg=msg.."High quality !\n"
  msg=msg.."Unbelievable !\n"
  msg=msg.."Come and see it !\n"
  msg=msg.."<Your ad here>\n"
  text = "News:".."\n""%Y-%m-%d %H:%M") .. "\n\n" .. msg; 
if not pn then pn="BigDisplay"  -- show news on big display
  title,text =;
  if not text then  say("no text"); self.remove(); goto ex  end"")
  self.display_text(text,20,3)  --triple-size, 20 char per line
--turn.left()  --turn into the direction of the player

Robot-commands - Part 5

Commands to look at the world :

  • f = find_nodes("default:stone", 3) -- returns true/false if a block of the specified type is found within the given distance.
The maximum range for this search is 8 blocks.


Lookout - we found a torch
 if rg==nil then
   s="default:torch_wall"  -- also: torch_ceiling
   say("Looking for "..s)
 f = find_nodes(s, rg) 
 if f then
   msg="Found "
   msg="No "
 say(msg..s.." within "..rg.." blocks")
 if not f then  say("done.")  self.remove()  end

The program starts with the maximum allowed range, counts down, and stops when nothing is found.


This program checks if blocks of ore are in range:

 if (rg==nil) then s="Ore-detector v0.3"
   rg=9  i=1  next=0
   ore={'diamond', 'mese', 'gold', 'copper', 'iron', 'coal', '-'}  --Table of valueable stuff
 if (ore[i]=="-") then next=1  i=0 end
 if next>0 then
   i=i+1  rg=9  next=0
   say("Seaching: "..ore[i])
 if (rg>8) then rg=8 end
 f = find_nodes("default:stone_with_"..ore[i], rg)  -- returns true/false
 if f then
   say("Found "..ore[i].." within range "..rg) 
   --say("no "..i.." @ rg"..rg)

Run this detector on the robot, and use an 'empty' remote controller to move the bot around to find the good stuff.


This program is a faster, optimized version of the above OreDetector
giving only one output-line for each type of ore:

 if not s then s="Ore-detector2 v0.4f"
   rg=9  i=1  next=0  msg=""
   --Table of stuff to search:
   ore={'@diamond', '@mese', '@gold', 
  '@copper', '@iron', '@coal', 
 --'!torch', '!torch_wall',
  '!lava_source',  '!lava_flowing',
  '!water_source', '!water_flowing',
  '-'}   --todo: moreores, mobs
   say(#ore) --
 if (ore[i]=="-") then next=1  i=0  
   --say("done.")  self.remove()
 if next>0 then
   i=i+1  rg=9  next=0
   --msg="Seaching: "..ore[i]  say(msg)  msg=""
 if (rg>8) then rg=8 end
 s  = ore[i]
 c1 = string.sub(s,1,1)
 s  = string.sub(s,2)
 if c1=="@" then s="default:stone_with_"..s end
 if c1=="!" then s="default:"           ..s end
 f = find_nodes(s, rg)  -- returns true/false
 if f then
   msg="Found "..s.." within range "..rg
 --say(i.." no "..s.." @ rg"..rg)  --debug
   if #msg>0 then say("--> "..msg)  end

It also searches for water and lava, to prevent surprises during mining.

Todo / Debug

see Mods/basic_robot/Todo