Lorc520

wood_round

Apr 24th, 2025 (edited)
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 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. -- Chopping and replanting logic goes here
  28. end
  29. end
  30.  
  31. -- Return to original orientation
  32. if side == "left" then
  33. turtle.turnRight()
  34. else
  35. turtle.turnLeft()
  36. end
  37. end
  38.  
  39. -- Move to the next row depending on which side we just checked
  40. function moveToNextRow(side)
  41. -- Step 1: Move 1 block forward (past the last sapling row)
  42. turtle.forward()
  43.  
  44. -- Step 2: Turn and move 5 blocks up to next sapling line
  45. if side == "left" then
  46. turtle.turnRight()
  47. for i = 1, 5 do
  48. turtle.forward()
  49. end
  50. turtle.turnRight()
  51. elseif side == "right" then
  52. turtle.turnLeft()
  53. for i = 1, 5 do
  54. turtle.forward()
  55. end
  56. turtle.turnLeft()
  57. end
  58.  
  59. -- Step 3: Move 1 forward to align with the next sapling row
  60. turtle.forward()
  61. end
  62.  
  63. -- Return to original starting position
  64. function returnToStart(length, width, side)
  65. -- Turn around
  66. turtle.turnLeft()
  67. turtle.turnLeft()
  68.  
  69. if side == "right" then
  70. turtle.forward()
  71. turtle.turnLeft()
  72. for i = 1, 5 * (width - 1) do
  73. turtle.forward()
  74. end
  75. elseif side == "left" then
  76. for i = 1, (5 * length) - 6 do
  77. turtle.forward()
  78. end
  79. turtle.turnRight()
  80. for i = 1, 5 * (width - 1) do
  81. turtle.forward()
  82. end
  83. end
  84.  
  85. -- Final adjustments for original direction, but facing the chest
  86. turtle.turnLeft()
  87. turtle.forward()
  88. end
  89.  
  90. -- Refuel until 1000 using all slots except 1 and 2, then deposit leftovers in chest behind
  91. function refuelAndDeposit()
  92. local fuelNeeded = 1000 - turtle.getFuelLevel()
  93.  
  94. -- Try to refuel from slots 3-16
  95. for i = 3, 16 do
  96. if fuelNeeded <= 0 then break end
  97. turtle.select(i)
  98. while turtle.refuel(1) do
  99. fuelNeeded = fuelNeeded - 1
  100. if fuelNeeded <= 0 then break end
  101. end
  102. end
  103.  
  104. -- Deposit remaining items in slots 3-16
  105. for i = 3, 16 do
  106. turtle.select(i)
  107. turtle.drop() -- Drops behind
  108. end
  109.  
  110. -- Reselect slot 1 and reset to original facing to keep things clean
  111. turtle.select(1)
  112. turtle.turnLeft()
  113. turtle.turnLeft()
  114. end
  115.  
  116. -- Main traversal logic through the sapling grid
  117. function checkTrees(length, width)
  118. local finalSide = "left" -- We'll update this as we go
  119. for row = 1, width do
  120. local side = (row % 2 == 1) and "left" or "right"
  121. finalSide = side -- Remember the last side for return logic
  122.  
  123. for x = 1, length do
  124. checkForTree(side)
  125. if x < length then
  126. for i = 1, 5 do
  127. turtle.forward()
  128. end
  129. end
  130. end
  131.  
  132. if row < width then
  133. moveToNextRow(side)
  134. end
  135. end
  136.  
  137. returnToStart(length, width, finalSide)
  138. end
  139.  
  140. -- Prompt the user for the grid size
  141. local length = getInput("Enter the number of trees per row (length):")
  142. local width = getInput("Enter the number of rows (width):")
  143.  
  144. -- Start repeating the process every minute
  145. while true do
  146. -- Begin traversal
  147. checkTrees(length, width)
  148.  
  149. -- Refuel and deposit at the end
  150. refuelAndDeposit()
  151.  
  152. -- Wait for 1 minute before repeating
  153. print("Waiting for 1 minute before the next round...")
  154. os.sleep(60) -- Wait 60 seconds (1 minute)
  155. end
  156.  
Add Comment
Please, Sign In to add comment