eniallator

Tree Farmer

Dec 30th, 2020 (edited)
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.69 KB | None | 0 0
  1. local sideLength = 7
  2. local inspectAttempts = 5
  3. local desiredFuelPercent = 0.5
  4.  
  5. local move = {
  6.     forward = function()
  7.         while not turtle.forward() do
  8.             turtle.dig()
  9.         end
  10.     end,
  11.     up = function()
  12.         while not turtle.up() do
  13.             turtle.digUp()
  14.         end
  15.     end,
  16.     down = function()
  17.         while not turtle.down() do
  18.             turtle.digDown()
  19.         end
  20.     end
  21. }
  22.  
  23. local checkBlock = {
  24.     forward = function(name)
  25.         local i
  26.         for i = 1, inspectAttempts do
  27.             local success, blockData = turtle.inspect()
  28.             if success then
  29.                 return blockData.name:find(name) ~= nil
  30.             end
  31.         end
  32.     end,
  33.     up = function(name)
  34.         local i
  35.         for i = 1, inspectAttempts do
  36.             local success, blockData = turtle.inspectUp()
  37.             if success then
  38.                 return blockData.name:find(name) ~= nil
  39.             end
  40.         end
  41.     end,
  42.     down = function(name)
  43.         local i
  44.         for i = 1, inspectAttempts do
  45.             local success, blockData = turtle.inspectDown()
  46.             if success then
  47.                 return blockData.name:find(name) ~= nil
  48.             end
  49.         end
  50.     end
  51. }
  52.  
  53. local function checkItem(name, slot)
  54.     local item = turtle.getItemDetail(slot)
  55.     return item and item.name:find(name)
  56. end
  57.  
  58. local function selectItem(name)
  59.     local i
  60.     for i = 1, 16 do
  61.         if checkItem(name, i) then
  62.             turtle.select(i)
  63.             return
  64.         end
  65.     end
  66. end
  67.  
  68. local function chopTreeIfExists()
  69.     if checkBlock.down('log') then
  70.         turtle.digDown()
  71.         selectItem('sapling')
  72.         turtle.placeDown()
  73.     end
  74.     local height, i = 0
  75.     while checkBlock.up('log') do
  76.         move.up()
  77.         height = height + 1
  78.     end
  79.     for i = 1, height do
  80.         move.down()
  81.     end
  82. end
  83.  
  84. local function transferItem(name, slot)
  85.     local i
  86.     if not checkItem(name, slot) and turtle.getItemCount(slot) > 0 then
  87.         turtle.select(slot)
  88.         for i = 1, 16 do
  89.             if i ~= slot and turtle.getItemCount(i) == 0 then
  90.                 turtle.transferTo(i)
  91.                 break
  92.             end
  93.         end
  94.     end
  95.     for i = 1, 16 do
  96.         if checkItem(name, i) then
  97.             turtle.select(i)
  98.             turtle.transferTo(slot)
  99.         end
  100.     end
  101. end
  102.  
  103. while true do
  104.     move.forward()
  105.     chopTreeIfExists()
  106.  
  107.     local i, j
  108.     for i = 1, sideLength do
  109.         for j = 1, sideLength - 1 do
  110.             move.forward()
  111.             chopTreeIfExists()
  112.         end
  113.         if i < sideLength then
  114.             if i % 2 == 1 then
  115.                 turtle.turnRight()
  116.                 move.forward()
  117.                 chopTreeIfExists()
  118.                 turtle.turnRight()
  119.             else
  120.                 turtle.turnLeft()
  121.                 move.forward()
  122.                 chopTreeIfExists()
  123.                 turtle.turnLeft()
  124.             end
  125.         end
  126.     end
  127.  
  128.     move.forward()
  129.     transferItem('sapling', 1)
  130.     if checkBlock.down('barrel') then
  131.         for i = 2, 16 do
  132.             if turtle.getItemCount(i) > 0 then
  133.                 turtle.select(i)
  134.                 turtle.dropDown()
  135.             end
  136.         end
  137.     end
  138.     if checkBlock.forward('pedestal') then
  139.         turtle.select(1)
  140.         turtle.suck(turtle.getItemSpace(1))
  141.     end
  142.     turtle.turnRight()
  143.     if
  144.         turtle.getFuelLevel() ~= 'unlimited' and checkBlock.forward('pedestal') and
  145.             turtle.getFuelLevel() < turtle.getFuelLimit() * desiredFuelPercent
  146.      then
  147.         turtle.select(16)
  148.         turtle.suck()
  149.         turtle.refuel()
  150.     end
  151.     turtle.turnRight()
  152.     sleep(60)
  153. end
  154.  
Advertisement
Add Comment
Please, Sign In to add comment