Advertisement
Myros27

Automatic Rubber & Birch Tree Farmer for Robots by Myros

Nov 17th, 2019 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1.  --- Automatic Rubber & Birch Tree Farmer for Robots by Myros
  2. -- credits to SubThread, ...
  3.  
  4.  -- Checks if everything is ready to Start
  5. local robot = require("robot")
  6. local term = require("term")
  7. local computer = require("computer")
  8. local component = require("component")
  9. local sides = require("sides")
  10. local exp = component.experience.level()
  11. local row = 0
  12. local rowminus =0
  13. print("Robot tree farm started")
  14. -- Change these to change the tree farm grid size or the distance between each tree in the grid. Specialrow is for your 2. Sapling. if no other sapling needed, set to 0.
  15. local treesX = 9
  16. local treesZ = 3
  17. local distanceBetweenTrees = 5
  18. local specialrow = 2
  19. term.clear()
  20.  
  21.  -- Displays the current Status
  22. local function Status()
  23.     term.clear()
  24.     print(robot.name())
  25.     print("Current EXP:")
  26.     print(component.experience.level())
  27.     print("Current Batt:")
  28.     print(computer.energy())
  29. end
  30.  
  31.  -- Sucks everything up
  32. local function Sucky()
  33.     component.tractor_beam.suck()
  34.     Status()
  35. end
  36.  
  37. -- Goes forward eventually, no matter if something is blocking the path at the moment.
  38. local function GoForward()
  39.     while true do
  40.         local movedSuccessfuly = robot.forward()
  41.         if movedSuccessfuly then
  42.             break
  43.         end
  44.     end
  45.     Sucky()
  46. end
  47.  
  48. -- Goes down eventually, no matter if something is blocking the path at the moment.
  49. local function GoDown()
  50.     while true do
  51.         local movedSuccessfuly = robot.down()
  52.         if movedSuccessfuly then
  53.             break
  54.         end
  55.     end
  56. end
  57.  
  58. -- Goes up eventually, no matter if something is blocking the path at the moment.
  59. local function GoUp()
  60.     while true do
  61.         local movedSuccessfuly = robot.up()
  62.         if movedSuccessfuly then
  63.             break
  64.         else
  65.         robot.swingUp()
  66.         end
  67.     end
  68. end
  69.  
  70. -- Check if Charge or Inventory are critical
  71. local function CheckMaintenance()
  72.     if robot.space(robot.inventorySize()) > 60 then
  73.         if computer.energy() > 10000 then
  74.             return true
  75.         end
  76.     else
  77.     end
  78.     return false
  79. end
  80.  
  81. -- Goes Forward until it collides
  82. local function ForwardUntilWall()
  83.     while true do
  84.         local blockFound = robot.detect()
  85.         if blockFound then
  86.             break
  87.         else
  88.             robot.forward()
  89.         end
  90.     end
  91. end
  92.  
  93. -- Goes Down until it collides
  94. local function DownUntilWall()
  95.     while true do
  96.         local blockFound = robot.detectDown()
  97.         if blockFound then
  98.             break
  99.         else
  100.             robot.down()   
  101.         end
  102.     end
  103. end
  104.  
  105. -- Fells a Tree
  106. local function Treefeller()
  107.     Sucky()
  108.     robot.turnRight()
  109.     local treeFound = robot.detect()
  110.     if treeFound then
  111.         local saplinggrowing = robot.compare()
  112.         if saplinggrowing then
  113.             robot.turnLeft()
  114.         else
  115.             if row == specialrow then
  116.                 robot.select(2)
  117.                 robot.swingUp()
  118.                 GoUp()
  119.                 local speztreeFound = robot.detect()
  120.                     if speztreeFound then
  121.                         robot.down()
  122.                         robot.swing()
  123.                         GoForward()
  124.                         for i = 1, 6 do
  125.                             robot.swingUp()
  126.                             GoUp()
  127.                         end
  128.                         robot.swingUp()
  129.                         DownUntilWall()
  130.                         robot.turnAround()
  131.                         GoForward()
  132.                         robot.turnAround()
  133.                         robot.place()
  134.                         robot.turnLeft()
  135.                         Sucky()
  136.                     else
  137.                         robot.down()
  138.                         robot.turnLeft()
  139.                     end
  140.             else
  141.                 robot.swing()
  142.                 GoForward()
  143.                 for i = 1, 6 do
  144.                     robot.swingUp()
  145.                     GoUp()
  146.                 end
  147.                 robot.swingUp()
  148.                 DownUntilWall()
  149.                 robot.turnAround()
  150.                 GoForward()
  151.                 robot.turnAround()
  152.                 robot.place()
  153.                 robot.turnLeft()
  154.                 Sucky()
  155.             end
  156.         end
  157.     else
  158.     robot.place()
  159.     robot.turnLeft()
  160.     end
  161. end
  162.  
  163. -- Move from tree to tree
  164. local function MoveToTree()
  165.     Treefeller()
  166.     for x = 2, treesX do
  167.         for i = 1, distanceBetweenTrees do
  168.             GoForward()
  169.         end
  170.         Treefeller()
  171.     end
  172.     robot.turnAround()
  173.     for x =2, treesX do
  174.         for i = 1, distanceBetweenTrees do
  175.             GoForward()
  176.             Sucky()
  177.         end
  178.     end
  179.     GoForward()
  180.     robot.turnRight()
  181. end
  182.  
  183. -- Handels the Maintenance
  184. local function Maintenance()
  185.     row= 0
  186.     Status()
  187.     while robot.detectUp() do
  188.         os.sleep(1)
  189.         Status()
  190.     end
  191.     for i = 3, robot.inventorySize() do
  192.         robot.select(i)
  193.         robot.drop()
  194.     end
  195.     robot.turnAround()
  196.     while computer.energy() < 20000 do
  197.         os.sleep(20)
  198.     end
  199.     GoForward()
  200.     GoForward()
  201.     robot.turnLeft()
  202. end
  203.  
  204. -- Moves to the correct Row
  205. local function RowZ()
  206.     while CheckMaintenance() do
  207.         row = row + 1
  208.         if row > treesZ then
  209.             ForwardUntilWall()
  210.             row = 1
  211.         else
  212.         end
  213.         if row == 1 then
  214.             robot.turnRight()
  215.             robot.select(1)
  216.             GoForward()
  217.             MoveToTree()
  218.         else
  219.             if row == specialrow then
  220.                 robot.select(2)
  221.             else
  222.                 robot.select(1)
  223.             end
  224.             robot.turnAround()
  225.             for i = 1, distanceBetweenTrees do
  226.                 GoForward()
  227.             end
  228.             robot.turnLeft()
  229.             GoForward()
  230.             MoveToTree()           
  231.         end
  232.     end
  233. end
  234.  
  235. -- Robot starts here
  236. -- Do the complete cycle.
  237. while true do
  238.     Maintenance()
  239.     RowZ()
  240.     ForwardUntilWall()
  241.     robot.turnLeft()
  242.     GoForward()
  243.     GoForward()
  244. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement