Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. local robot = require("robot")
  2. print("Robot tree farm started")
  3.  
  4. -- Select the first slot, which is supposed to have a sapling.
  5. robot.select(1)
  6.  
  7. -- Change these to change the tree farm grid size or the distance between each tree in the grid.
  8. local treesX = 6
  9. local treesZ = 6
  10. local distanceBetweenTrees = 3
  11.  
  12. -- Goes forward eventually, no matter if something is blocking the path at the moment.
  13. local function GoForward()
  14.     while true do
  15.         local movedSuccessfuly = robot.forward()
  16.         if movedSuccessfuly then
  17.             break
  18.         end
  19.     end
  20.     robot.suck()
  21. end
  22.  
  23. -- Goes down eventually, no matter if something is blocking the path at the moment.
  24. local function GoDown()
  25.     while true do
  26.         local movedSuccessfuly = robot.down()
  27.         if movedSuccessfuly then
  28.             break
  29.         end
  30.     end
  31. end
  32.  
  33. -- Checks for a tree
  34. local function CheckForTree()
  35.     -- Check for a block
  36.     if robot.detect() then
  37.         robot.up()
  38.        
  39.         -- Attempt to detect a block above which will determine if the tree has grown.
  40.         local blockFound = robot.detect()
  41.         robot.down()
  42.        
  43.         -- Check tree has grown and if so then go up.
  44.         if blockFound then
  45.             for blocksToMoveUp = 1, 8 do
  46.                 -- Destroy the wood in front of the robot
  47.                 robot.swing()
  48.                
  49.                 -- Check if there is a block above and if so then destroy the block.
  50.                 if robot.detectUp() then
  51.                     robot.swingUp()
  52.                 end
  53.                
  54.                 -- Go up
  55.                 robot.up()
  56.             end
  57.            
  58.             -- Move back down again
  59.             for blocksToMoveDown = 1, 8 do
  60.                 GoDown()
  61.             end
  62.            
  63.             -- Suck up stuff and go forward
  64.             robot.suck()
  65.             robot.suckUp()
  66.             robot.suckDown()
  67.             GoForward()
  68.            
  69.             -- Suck up stuff
  70.             for rotation = 1, 4 do
  71.                 robot.turnLeft()
  72.                 robot.suck()
  73.             end
  74.            
  75.             -- Go back
  76.             robot.turnAround()
  77.             GoForward()
  78.             robot.turnAround()
  79.            
  80.             -- Place the new sapling here
  81.             robot.place()
  82.         end
  83.     else
  84.         -- There is no block here, place the sapling
  85.         robot.place()
  86.     end
  87. end
  88.  
  89. -- Scans a row of trees.
  90. local function CheckRowOfTrees()
  91.     -- Check for trees in the X row.
  92.     for treeX = 1, treesX do
  93.         CheckForTree()
  94.        
  95.         -- If this isn't the last tree in the row then move to the next tree in the row.
  96.         if treeX < treesX then
  97.             robot.turnRight()
  98.             for blocksToMove = 1, distanceBetweenTrees do
  99.                 GoForward()
  100.             end
  101.             robot.turnLeft()
  102.         end
  103.     end
  104.    
  105.     -- Go back to the first tree in the row.
  106.     robot.turnLeft()
  107.     for blocksToMove = 1, distanceBetweenTrees*(treesX - 1) do
  108.         GoForward()
  109.     end
  110.     robot.turnRight()
  111. end
  112.  
  113. -- Do the complete cycle.
  114. while true do
  115.     -- Go to each X row in the grid.
  116.     for treeZ = 1, treesZ do
  117.         CheckRowOfTrees()
  118.        
  119.         -- If this isn't the last X row in the grid then go to the next X row in the grid.
  120.         if treeZ < treesZ then
  121.             robot.turnRight()
  122.             GoForward()
  123.             robot.turnLeft()
  124.             for blocksToMove = 1, distanceBetweenTrees do
  125.                 GoForward()
  126.             end
  127.             robot.turnLeft()
  128.             GoForward()
  129.             robot.turnRight()
  130.         end
  131.     end
  132.    
  133.     -- Go back to the starting position.
  134.     robot.turnRight()
  135.     GoForward()
  136.     robot.turnRight()
  137.     for blocksToMove = 1, distanceBetweenTrees*(treesZ - 1) do
  138.         GoForward()
  139.         robot.suck()
  140.     end
  141.     robot.turnRight()
  142.     GoForward()
  143.     robot.turnRight()
  144.    
  145.     -- Sleep for five seconds.
  146.     os.sleep(5)
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement