jig487

treeFarm

Jun 16th, 2021 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.34 KB | None | 0 0
  1. local tArgs = {...}
  2. local gps = {0, 0}--Y axis and rotation for turtle
  3.  
  4. --Goes back and forth between two blocks. detects when it hits end and goes back other way. Maybe waits a bit before restarting
  5. --Checks to see if a tree has grown. If it has then cut it down and replant, then continue.
  6. --when it resets it dumps its inventory other than fuel and 1 stack of saplings.
  7.  
  8. local function chop() --Cuts down tree and resets position
  9.     while turtle.detect() do
  10.         turtle.dig()
  11.         while not turtle.up() do
  12.             turtle.digUp()
  13.         end
  14.         gps[1] = gps[1] + 1
  15.     end
  16.     for i = 1, gps[1] do
  17.         turtle.down()
  18.     end
  19.     gps[1] = 0
  20. end
  21.  
  22. local function replant() --replants tree
  23.     turtle.select(2)
  24.     turtle.place()
  25.     turtle.select(1)
  26. end
  27.  
  28. local function check() --turns, checks for a tree. If ok then chops and replants. then turns back
  29.     if gps[2] == 0 then
  30.         turtle.turnLeft()
  31.     else
  32.         turtle.turnRight()
  33.     end
  34.     local ok, data = turtle.inspect()
  35.     if ok and data.name == "rustic:log" then
  36.         chop()
  37.         replant()
  38.     end
  39.     if gps[2] == 0 then
  40.         turtle.turnRight()
  41.     else
  42.         turtle.turnLeft()
  43.     end
  44. end
  45.  
  46. --crate is (17 x 9) 117 slots large
  47. local function dump() --dumps inventory except for fuel and 1 stack of saplings
  48.     local ok, data = turtle.getItemDetail()
  49.     if data and data.name == "actuallyadditions:block_giant_chest" then
  50.         for i = 3, 16 do
  51.             turtle.select(i)
  52.             turtle.dropDown()
  53.         end
  54.     end
  55.     turtle.select(1)
  56. end
  57.  
  58. local function patrol() --goes forward until it hits a block, then turns around and does it again.
  59.     check()
  60.     while not turtle.forward() do
  61.         if turtle.detect() then
  62.             turtle.turnRight()
  63.             turtle.turnRight()
  64.             gps[2] = gps[2] + 180
  65.             if gps[2] >= 360 then
  66.                 gps[2] = 0
  67.             end
  68.         else
  69.             turtle.attack()
  70.         end
  71.         dump()
  72.         check()
  73.     end
  74. end
  75.  
  76. local function refuel() -- checks if fuel is below 5000. True = goes to slot 1 and refuels. If no fuel, spits out an error and goes to get more
  77.     if turtle.getFuelLevel() < 5000 then
  78.       turtle.select(1)
  79.       turtle.refuel(10)
  80.    end
  81. end
  82.  
  83. --Main code
  84.  
  85. for t = 1, tArgs[1] * 5 do
  86.     refuel()
  87.     patrol()
  88. end
Add Comment
Please, Sign In to add comment