Advertisement
LaniusFNV

Tree Farm

Jan 24th, 2022
943
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This is currently slightly broken: the loop runs correctly once,
  2. -- but doesn't return home correctly.
  3.  
  4. local SAPLING = "minecraft:birch_sapling"
  5. local LOG = "minecraft:birch_log"
  6.  
  7. local function findItem(item_name)
  8.   for slot = 1,16,1 do
  9.     local details = turtle.getItemDetail(slot)
  10.  
  11.     if details ~= nil then
  12.       if details.name == item_name then
  13.         return slot
  14.       end
  15.     end
  16.   end
  17.  
  18.   return nil
  19. end
  20.  
  21. local function ensureFueled(width, height)
  22.   local current_fuel = turtle.getFuelLevel()
  23.   local needed_fuel =
  24.     3 + -- moveToStart()
  25.     1 + -- fellTree()
  26.     width * 3 + -- moveToNextTree()
  27.     1 + height * 3 + -- moveToNextColumn() 1
  28.     height * 3 -- moveToNextColumn() 2
  29.  
  30.   local has_enough_fuel = current_fuel >= needed_fuel
  31.  
  32.   if has_enough_fuel then
  33.     return true
  34.   else
  35.     local slot = findItem(LOG)
  36.     turtle.select(slot)
  37.  
  38.     turtle.refuel()
  39.  
  40.     return ensureFueled(width, height)
  41.   end
  42. end
  43.  
  44. local function turnRight()
  45.   turtle.suck()
  46.   turtle.turnRight()
  47.   turtle.suck()
  48. end
  49. local function turnLeft()
  50.   turtle.suck()
  51.   turtle.turnLeft()
  52.   turtle.suck()
  53. end
  54.  
  55. local function forward()
  56.   turtle.suck()
  57.   turtle.forward()
  58.   turtle.suck()
  59. end
  60.  
  61. local function returnToChest(width)
  62.   turnRight()
  63.   forward()
  64.   turnLeft()
  65.   for _ = 1,3 * width,1 do
  66.     turtle.forward()
  67.   end
  68.   forward()
  69.   forward()
  70.   turnRight()
  71.   turnRight()
  72. end
  73.  
  74. local function depositItems()
  75.   local sapling_slot = findItem(SAPLING)
  76.   local log_slot = findItem(LOG)
  77.   for slot = 1,16,1 do
  78.     -- the log slot is for fuel
  79.     -- this works, because findItem
  80.     -- only ever returns the first slot
  81.     -- where an item is found
  82.     if slot ~= sapling_slot and slot ~= log_slot then
  83.       turtle.select(slot)
  84.       turtle.dropDown()
  85.     end
  86.   end
  87. end
  88.  
  89. local function placeSapling()
  90.   local slot = findItem(SAPLING)
  91.   if slot ~= nil then
  92.     turtle.select(slot)
  93.  
  94.     turtle.place()
  95.   end
  96. end
  97.  
  98. -- This assumes that the tree will be felled in one swoop
  99. local function fellTree()
  100.   local blocked, data = turtle.inspect()
  101.   if blocked and data.name == LOG then
  102.     turtle.dig()
  103.     os.sleep(1)
  104.     turtle.suck()
  105.     placeSapling()
  106.   end
  107.   turnLeft()
  108. end
  109.  
  110. local function moveToStart()
  111.   forward()
  112.   forward()
  113.   turnRight()
  114.   forward()
  115.   turnRight()
  116. end
  117.  
  118. local function moveToNextColumn(height)
  119.   turnLeft()
  120.   turnLeft()
  121.  
  122.   for _ = 1,height * 2,1 do
  123.     forward()
  124.   end
  125.   forward()
  126.  
  127.   turnRight()
  128.   forward()
  129.   moveToStart()
  130. end
  131.  
  132. local function moveToNextTree()
  133.   forward()
  134.   forward()
  135.   forward()
  136.   turnRight()
  137. end
  138.  
  139. local function farm(width, height)
  140.   moveToStart()
  141.   for col = 1,width,1 do
  142.     for row = 1,height - 1,1 do
  143.       fellTree()
  144.       moveToNextTree()
  145.     end
  146.     fellTree()
  147.     moveToNextColumn(height)
  148.   end
  149. end
  150.  
  151. local function sleep(seconds)
  152.   while seconds > 0 do
  153.     term.setCursorPos(1, 2)
  154.     term.clearLine()
  155.     term.write("Sleeping for " .. seconds .. " s")
  156.     os.sleep(1)
  157.     seconds = seconds - 1
  158.   end
  159. end
  160.  
  161. local function initialMessage()
  162.   term.clear()
  163.   term.setCursorPos(1, 1)
  164.   term.write("farmTrees online!")
  165. end
  166.  
  167. local function main(width, height)
  168.   initialMessage()
  169.   while true do
  170.     assert(ensureFueled(width, height))
  171.     farm(width, height)
  172.     returnToChest(width)
  173.     depositItems()
  174.     sleep(60)
  175.   end
  176. end
  177.  
  178. main(3, 3)
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement