Advertisement
Paxon57

Untitled

Jun 19th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. args = {...}
  2.  
  3. -- Init variables
  4. local pos = {
  5. x = 0,
  6. y = 0,
  7. z = 0,
  8. f = 0
  9. }
  10.  
  11. local xSize = args[1]
  12. local ySize = args[2]
  13.  
  14. -- Functions
  15. local function moveForward()
  16.  
  17. turtle.suck()
  18.  
  19. while turtle.detect() do turtle.dig() end
  20. turtle.forward()
  21.  
  22. if pos.f == 0 then pos.x = pos.x + 1 end
  23. if pos.f == 2 then pos.x = pos.x - 1 end
  24. if pos.f == 1 then pos.y = pos.y + 1 end
  25. if pos.f == 3 then pos.y = pos.y - 1 end
  26. end
  27.  
  28. local function moveUp()
  29. while turtle.detectUp() do turtle.digUp() end
  30. turtle.up()
  31. pos.z = pos.z + 1
  32. end
  33.  
  34. local function moveDown()
  35. while turtle.detectDown() do turtle.digDown() end
  36. turtle.down()
  37. pos.z = pos.z - 1
  38. end
  39.  
  40. local function turnLeft()
  41. turtle.turnLeft()
  42. pos.f = (pos.f - 1) % 4
  43. end
  44.  
  45. local function turnRight()
  46. turtle.turnRight()
  47. pos.f = (pos.f + 1) % 4
  48. end
  49.  
  50. local function refuel()
  51. if turtle.getFuelLevel() > turtle.getFuelLimit() * 0.01 then return end
  52. turtle.select(16)
  53. while turtle.refuel(0) and turtle.getFuelLevel() < turtle.getFuelLimit() * 0.01 do
  54. turtle.refuel(1)
  55. end
  56. turtle.select(1)
  57. end
  58.  
  59. local function isItem(itemName, name)
  60. if string.find(itemName, name) then
  61. return true
  62. else
  63. return false
  64. end
  65. end
  66.  
  67. local function goAround()
  68. turnRight()
  69. moveForward()
  70. turnLeft()
  71. moveForward()
  72. moveForward()
  73. turnLeft()
  74. moveForward()
  75. turnRight()
  76. end
  77.  
  78. local function chopTheTree()
  79. moveUp()
  80. moveUp()
  81. while turtle.detectUp() do
  82. moveUp()
  83. end
  84. moveForward()
  85. for i = 1, math.abs(pos.z) do
  86. for i = 1, 4 do
  87. turtle.dig()
  88. turnRight()
  89. end
  90. moveDown()
  91. end
  92. moveForward()
  93. turnRight()
  94. turnRight()
  95. turtle.select(15)
  96. turtle.place()
  97. turnLeft()
  98. turnLeft()
  99. end
  100.  
  101. -- Main Code
  102. print("Insert saplings into slot 15")
  103. turtle.select(15)
  104. while turtle.getItemDetail() == nil do end
  105. while not isItem(turtle.getItemDetail().name, "sapling") do end
  106. print("Saplings detected")
  107. print("Please insert fuel into slot 16 (not wood)")
  108. turtle.select(16)
  109. while not turtle.refuel(0) do end
  110. print("Fuel detected, beginning the work")
  111. turtle.select(15)
  112.  
  113. -- Main Loop
  114. while true do
  115. refuel()
  116. if turtle.getFuelLevel() > 20 then
  117.  
  118. moveForward()
  119.  
  120. if pos.f == 0 and pos.x >= (3 * xSize) then
  121. if pos.y == (3 * (ySize - 1)) then
  122. turnLeft()
  123. moveForward()
  124. turnLeft()
  125. for i = 1, pos.x do
  126. moveForward()
  127. end
  128. turnRight()
  129. for i = 1, pos.y do
  130. moveForward()
  131. end
  132. turnLeft()
  133. for i = 1, 14 do
  134. turtle.select(i)
  135. turtle.drop()
  136. end
  137. turtle.select(15)
  138. turnRight()
  139. turnRight()
  140. else
  141. turnRight()
  142. for i = 1, 3 do
  143. moveForward()
  144. end
  145. turnRight()
  146. end
  147. elseif pos.f == 2 and pos.x == 0 then
  148. if pos.y == (3 * (ySize - 1)) then
  149. turnRight()
  150. for i = 1, pos.y do
  151. moveForward()
  152. end
  153. turnLeft()
  154. for i = 1, 14 do
  155. turtle.select(i)
  156. turtle.drop()
  157. end
  158. turtle.select(15)
  159. turnRight()
  160. turnRight()
  161. else
  162. turnLeft()
  163. for i = 1, 3 do
  164. moveForward()
  165. end
  166. turnLeft()
  167. end
  168. end
  169.  
  170. local b, data = turtle.inspect()
  171. if b ~= false then
  172. if isItem(data.name, "sapling") then
  173. goAround()
  174. elseif isItem(data.name, "log") then
  175. chopTheTree()
  176. end
  177. end
  178. else
  179. print("Waiting for fuel in slot 16")
  180. end
  181. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement