Lorc520

woodfarm

Apr 24th, 2025 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. -- Function to get valid number input from user
  2. function getInput(prompt)
  3. while true do
  4. print(prompt)
  5. local input = read()
  6. local num = tonumber(input)
  7. if num then
  8. return num
  9. else
  10. print("Invalid input. Please enter a number.")
  11. end
  12. end
  13. end
  14.  
  15. -- Function to check for a tree on the given side (left/right)
  16. function checkForTree(side)
  17. if side == "left" then
  18. turtle.turnLeft()
  19. else
  20. turtle.turnRight()
  21. end
  22.  
  23. if turtle.detect() then
  24. turtle.select(2) -- Select log for comparison
  25. if turtle.compare() then
  26. print("Tree detected on the " .. side .. ". Chopping and replanting.")
  27.  
  28. -- Chopping the tree logic
  29. -- Break the block in front and move forward, repeat until the block above is not the same as slot 2
  30. if turtle.detect() then
  31. turtle.dig() -- Dig the block in front
  32. turtle.forward() -- Move forward to the next block
  33.  
  34. -- Now check and dig the block above
  35. while turtle.detectUp() do
  36. turtle.digUp() -- Dig the block above
  37. turtle.up() -- Move up to the next level
  38. end
  39. end
  40.  
  41. -- Now the turtle is at the top, move down until it meets a block
  42. while not turtle.detectDown() do
  43. turtle.down() -- Move down until a block is encountered
  44. end
  45.  
  46. -- Turn around to face the original position
  47. turtle.turnLeft()
  48. turtle.turnLeft()
  49.  
  50. -- Move forward to the original position
  51. turtle.forward()
  52.  
  53. -- Turn to the left to plant the sapling
  54. turtle.turnLeft()
  55. turtle.turnLeft()
  56. turtle.select(1) -- Select sapling in slot 1
  57. turtle.place() -- Place the sapling
  58.  
  59. -- Suck items that may have fallen (if any)
  60. turtle.suck()
  61. end
  62. end
  63.  
  64. -- Return to original orientation
  65. if side == "left" then
  66. turtle.turnRight()
  67. else
  68. turtle.turnLeft()
  69. end
  70. end
  71.  
  72. -- Move to the next row depending on which side we just checked
  73. function moveToNextRow(side)
  74. -- Step 1: Move 1 block forward (past the last sapling row)
  75. turtle.forward()
  76.  
  77. -- Step 2: Turn and move 5 blocks up to next sapling line
  78. if side == "left" then
  79. turtle.turnRight()
  80. for i = 1, 5 do
  81. turtle.forward()
  82. end
  83. turtle.turnRight()
  84. elseif side == "right" then
  85. turtle.turnLeft()
  86. for i = 1, 5 do
  87. turtle.forward()
  88. end
  89. turtle.turnLeft()
  90. end
  91.  
  92. -- Step 3: Move 1 forward to align with the next sapling row
  93. turtle.forward()
  94. end
  95.  
  96. -- Return to original starting position
  97. function returnToStart(length, width, side)
  98. -- Turn around
  99. turtle.turnLeft()
  100. turtle.turnLeft()
  101.  
  102. if side == "right" then
  103. turtle.forward()
  104. turtle.turnLeft()
  105. for i = 1, 5 * (width - 1) do
  106. turtle.forward()
  107. end
  108. elseif side == "left" then
  109. for i = 1, (5 * length) - 6 do
  110. turtle.forward()
  111. end
  112. turtle.turnRight()
  113. for i = 1, 5 * (width - 1) do
  114. turtle.forward()
  115. end
  116. end
  117.  
  118. -- Final adjustments for original direction, but facing the chest
  119. turtle.turnLeft()
  120. turtle.forward()
  121. end
  122.  
  123. -- Refuel until 1000 using all slots except 1 and 2, then deposit leftovers in chest behind
  124. function refuelAndDeposit()
  125. local fuelNeeded = 1000 - turtle.getFuelLevel()
  126.  
  127. -- Try to refuel from slots 3-16
  128. for i = 3, 16 do
  129. if fuelNeeded <= 0 then break end
  130. turtle.select(i)
  131. while turtle.refuel(1) do
  132. fuelNeeded = fuelNeeded - 1
  133. if fuelNeeded <= 0 then break end
  134. end
  135. end
  136.  
  137. -- Deposit remaining items in slots 3-16
  138. for i = 3, 16 do
  139. turtle.select(i)
  140. turtle.drop() -- Drops behind
  141. end
  142.  
  143. -- Reselect slot 1 and reset to original facing to keep things clean
  144. turtle.select(1)
  145. turtle.turnLeft()
  146. turtle.turnLeft()
  147. end
  148.  
  149. -- Main traversal logic through the sapling grid
  150. function checkTrees(length, width)
  151. local finalSide = "left" -- We'll update this as we go
  152. for row = 1, width do
  153. local side = (row % 2 == 1) and "left" or "right"
  154. finalSide = side -- Remember the last side for return logic
  155.  
  156. for x = 1, length do
  157. checkForTree(side)
  158. if x < length then
  159. for i = 1, 5 do
  160. turtle.forward()
  161. end
  162. end
  163. end
  164.  
  165. if row < width then
  166. moveToNextRow(side)
  167. end
  168. end
  169.  
  170. returnToStart(length, width, finalSide)
  171. end
  172.  
  173. -- Prompt the user for the grid size
  174. local length = getInput("Enter the number of trees per row (length):")
  175. local width = getInput("Enter the number of rows (width):")
  176.  
  177. -- Start repeating the process every minute
  178. while true do
  179. -- Begin traversal
  180. checkTrees(length, width)
  181.  
  182. -- Refuel and deposit at the end
  183. refuelAndDeposit()
  184.  
  185. -- Wait for 1 minute before repeating
  186. print("Waiting for 1 minute before the next round...")
  187. os.sleep(60) -- Wait 60 seconds (1 minute)
  188. end
  189.  
Add Comment
Please, Sign In to add comment