Advertisement
mathiaas

lumberjack

Apr 9th, 2024 (edited)
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. local args = {...}
  2. local scriptName = "lumberjack"
  3.  
  4. if #args < 3 then
  5.     print("Usage: " .. scriptName.. " <rows> <number of trees> <sleep interval>")
  6.     return
  7. end
  8.  
  9. local rowsToChop = tonumber(args[1])
  10. local treesPerRow = tonumber(args[2])
  11. local sleepInterval = tonumber(args[3])
  12. local treeCount = rowsToChop * treesPerRow * 2
  13. local logTally = 0
  14. local depositErrors = 0
  15.  
  16. dofile("turtle_bot")
  17.  
  18. local t = TurtleBot.new(scriptName, "West")
  19. t:connect()
  20.  
  21.  
  22. local function plant()
  23.     if turtle.detect() then return end
  24.     if t:selectItem(ITEMS.sapling.name) then return turtle.place() end
  25.     error("Ran out of saplings to plant", 2)
  26. end
  27.  
  28.  
  29. local function isLog(direction)
  30.     direction = direction or "front"
  31.     return t:detectBlock({BLOCKS.log.name}, direction)
  32. end
  33.  
  34.  
  35. local function isChoppable(direction)
  36.     return t:detectBlock({BLOCKS.log.name, BLOCKS.leaves.name}, direction)
  37. end
  38.  
  39.  
  40. local function goDown()
  41.     while true do
  42.         local success, _ = turtle.inspectDown()
  43.         if success then
  44.             if isChoppable("down") then
  45.                 t:dig("down")
  46.             else
  47.                 return true
  48.             end
  49.         end
  50.         t:down()
  51.     end
  52. end
  53.  
  54.  
  55. local function chopLog(direction)
  56.     direction = direction or "front"
  57.     if not isChoppable(direction) then return false end
  58.     if isLog(direction) then logTally = logTally + 1 end
  59.     t:dig(direction)
  60. end
  61.  
  62.  
  63. local function chopLayer(iteration)
  64.     chopLog("up")
  65.     for i = 1, 4 do
  66.         chopLog()
  67.         if iteration ~= 1 then
  68.             turtle.turnRight()
  69.             turtle.suck()
  70.         end
  71.     end
  72. end
  73.  
  74.  
  75. local function chopTree()
  76.     local i = 1
  77.     while isLog() do
  78.         chopLayer(i)
  79.         t:up()
  80.         i = i + 1
  81.     end
  82.  
  83.     goDown()
  84. end
  85.  
  86.  
  87. local function chopRow()
  88.     for tree=1, treesPerRow do
  89.         chopTree()
  90.         turtle.suck()
  91.         plant()
  92.         t:right()
  93.         turtle.suck()
  94.         if tree ~= treesPerRow then
  95.             t:forward()
  96.             t:left()
  97.         else
  98.             t:right()
  99.             t:forward()
  100.         end
  101.     end
  102.     t:updateData()
  103.     for tree=1, treesPerRow do
  104.         chopTree()
  105.         turtle.suck()
  106.         plant()
  107.         t:right()
  108.         turtle.suck()
  109.         if tree ~= treesPerRow then
  110.             t:forward()
  111.             t:left()
  112.         end
  113.     end
  114.     t:updateData()
  115. end
  116.  
  117. local function switchRow()
  118.     t:forward()
  119.     t:left()
  120.     t:forward(3)
  121.     t:left()
  122.     t:forward()
  123.     t:left()
  124. end
  125.  
  126.  
  127. local function printInfo()
  128.     shell.run("clear")
  129.     print("Deposit errors: " .. depositErrors .. ".")
  130.     print("Logs chopped: " .. logTally .. ".")
  131.     print("Trees in my farm: " .. treeCount .. "!")
  132.     print("Current fuel level: " .. turtle.getFuelLevel() .. ".")
  133.     print("Sleeping for " .. sleepInterval .. " seconds...")
  134. end
  135.  
  136.  
  137. local function reset()
  138.     local blocks_to_path = (rowsToChop - 1) * 4 + 1
  139.     t:forward()
  140.     t:right()
  141.     t:forward(blocks_to_path)
  142.     t:right()
  143.     t:combine()
  144.     t:enderDeposit(COLOR_CHANNELS.farmingDepot, {})
  145.     t:enderRefuel(2000, 5000, ITEMS.charcoal)
  146. end
  147.  
  148.  
  149. local function main()
  150.     while true do
  151.         t:enderCollect(COLOR_CHANNELS.sapling, treeCount)
  152.         t:forward()
  153.         t:left()
  154.         for row = 1, rowsToChop do
  155.             chopRow()
  156.             if row ~= rowsToChop then
  157.                 switchRow()
  158.             end
  159.         end
  160.         reset()
  161.         printInfo()
  162.         sleep(sleepInterval)
  163.     end
  164. end
  165.  
  166.  
  167. t:execute(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement