Syldarion

SylLumberjack -- ComputerCraft

Dec 6th, 2020 (edited)
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. -- SylLumberjack
  2.  
  3. local monitorWaitTime = 30
  4. local nextTurn = "left"
  5. local shouldStop = false
  6. local shouldStopThreshold = 10
  7.  
  8. local function outOfSaplings()
  9. return turtle.getItemCount(3) == 0
  10. end
  11.  
  12. local function tryUp()
  13. if turtle.getFuelLevel() == 0 then
  14. shouldStop = true
  15. else
  16. turtle.up()
  17. end
  18. end
  19.  
  20. local function tryDown()
  21. if turtle.getFuelLevel() == 0 then
  22. shouldStop = true
  23. else
  24. turtle.down()
  25. end
  26. end
  27.  
  28. local function tryForward()
  29. if turtle.getFuelLevel() == 0 then
  30. shouldStop = true
  31. else
  32. turtle.forward()
  33. end
  34. end
  35.  
  36. local function chopTree()
  37. -- assumes we are facing the base of the tree
  38. local stepsUp = 0
  39. turtle.dig()
  40. tryForward()
  41. while turtle.detectUp() do
  42. turtle.digUp()
  43. tryUp()
  44. stepsUp = stepsUp + 1
  45. end
  46.  
  47. for i=1,stepsUp do
  48. turtle.digDown()
  49. tryDown()
  50. end
  51.  
  52. if turtle.getFuelLevel() < shouldStopThreshold then
  53. shouldStop = true
  54. end
  55. end
  56.  
  57. local function plantTree()
  58. turtle.select(3)
  59. turtle.place()
  60. end
  61.  
  62. local function checkTreeLeft()
  63. turtle.select(3)
  64. turtle.turnLeft()
  65. if turtle.detect() and not turtle.compare() then
  66. chopTree()
  67. turtle.turnRight()
  68. turtle.turnRight()
  69. tryForward()
  70. turtle.turnLeft()
  71. return true
  72. else
  73. turtle.turnRight()
  74. return false
  75. end
  76. end
  77.  
  78. local function checkTreeRight()
  79. turtle.select(3)
  80. turtle.turnRight()
  81. if turtle.detect() and not turtle.compare() then
  82. chopTree()
  83. turtle.turnRight()
  84. turtle.turnRight()
  85. tryForward()
  86. turtle.turnRight()
  87. return true
  88. else
  89. turtle.turnLeft()
  90. return false
  91. end
  92. end
  93.  
  94. local function checkTreeRow()
  95. while not turtle.detect() and not shouldStop do
  96. turtle.forward()
  97. if checkTreeLeft() then
  98. turtle.turnLeft()
  99. plantTree()
  100. turtle.turnRight()
  101. end
  102. if checkTreeRight() then
  103. turtle.turnRight()
  104. plantTree()
  105. turtle.turnLeft()
  106. end
  107. end
  108. end
  109.  
  110. local function toggleNextTurn()
  111. if nextTurn == "left" then
  112. nextTurn = "right"
  113. else
  114. nextTurn = "left"
  115. end
  116. end
  117.  
  118. local function goToNextRow()
  119. if nextTurn == "left" then
  120. turtle.turnLeft()
  121. for i=1,4 do
  122. tryForward()
  123. end
  124. turtle.turnLeft()
  125. else
  126. turtle.turnRight()
  127. for i=1,4 do
  128. tryForward()
  129. end
  130. turtle.turnRight()
  131. end
  132. toggleNextTurn()
  133. end
  134.  
  135. local function unload()
  136. -- assumes the turtle is over a chest
  137. for n=4,16 do
  138. local nCount = turtle.getItemCount(n)
  139. if nCount > 0 then
  140. turtle.select(n)
  141. turtle.dropDown()
  142. end
  143. end
  144. end
  145.  
  146. local function runTreePath()
  147. local done = false
  148.  
  149. while not done do
  150. checkTreeRow()
  151.  
  152. if shouldStop then
  153. return
  154. end
  155.  
  156. -- once we get here, something is in front of us
  157. turtle.select(1)
  158. if turtle.compare() then
  159. -- Stop marker hit
  160. done = true
  161. end
  162.  
  163. turtle.select(2)
  164. if turtle.compare() then
  165. -- Turn marker hit
  166. goToNextRow()
  167. end
  168. end
  169.  
  170. unload()
  171. turtle.turnRight()
  172. turtle.turnRight()
  173. end
  174.  
  175. local function run()
  176. -- fuel for run
  177. turtle.select(4)
  178. turtle.refuel()
  179.  
  180. while not shouldStop do
  181. -- assumes we start on the row
  182. -- that ends with a left turn
  183. runTreePath()
  184. sleep(monitorWaitTime)
  185. end
  186. end
  187.  
  188. term.clear()
  189. textutils.slowPrint("SylLumberjack v0.1")
  190. print("Slots: 1 - Stop Marker | 2 - Turn Marker | 3 - Saplings | 4 - Fuel")
  191.  
  192. local input = "none"
  193. while (input ~= "left") and (input ~= "right") do
  194. print("Enter first turn direction (left/right): ")
  195. input = read()
  196. end
  197. nextTurn = input
  198.  
  199. run()
  200.  
Add Comment
Please, Sign In to add comment