Birog

tree

Sep 15th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. -- Move up for given amount and check for every move if there is enough fuel for this opreation
  2. function moveUp(amount)
  3. for n = 1, amount, 1 do
  4. checkFuel(0)
  5. turtle.up()
  6. end
  7. end
  8.  
  9. -- Move down for given amount and check for every move if there is enough fuel for this opreation
  10. function moveDown(amount)
  11. for n = 1, amount, 1 do
  12. checkFuel(0)
  13. turtle.down()
  14. end
  15. end
  16.  
  17. -- if there is more fuel then requested, otherweise refuel from slot 5
  18. -- if this fails then wait for userinput
  19. function checkFuel(value)
  20. fuel = turtle.getFuelLevel()
  21. if fuel <= value then
  22. print("Need FUEL!, press any key when ready")
  23. -- move wood to refuel slot and refuel (we could refuel from 5 but this is logically cleaner ;) )
  24. turtle.select(5)
  25. turtle.transferTo(16 ,32)
  26. turtle.select(16)
  27. turtle.refuel()
  28. fuel = turtle.getFuelLevel()
  29. print("Refueled - Fuel Level"..fuel)
  30.  
  31. -- refuel failed for some reason? wait for player to refuel
  32. if fuel == 0 then
  33. print("Refuel failed, please refuel manually!!")
  34. local event, param1 = os.pullEvent("char")
  35. turtle.select(16)
  36. turtle.refuel()
  37. end
  38. end
  39. end
  40.  
  41. -- checks if there is something in front of turtel
  42. -- if yes : it must be a tree and turtel will chop it down
  43. -- maybe there are just leaves but we think of them as a tree and clear the way ;)
  44. function detectAndCutTree()
  45. -- is something in front of turtel?
  46. if turtle.detect() then
  47. -- dig block in front place it in slot 5 and move to digged pos, chop 1 below and chop everything above
  48. turtle.select(5)
  49. turtle.dig()
  50. moveForward(1)
  51. turtle.digDown()
  52. while turtle.detectUp() do
  53. turtle.digUp()
  54. moveUp(1)
  55. end
  56.  
  57. -- move down to ground
  58. while not turtle.detectDown() do
  59. moveDown(1)
  60. end
  61.  
  62. -- move to original postion, pre chopping
  63. moveUp(1)
  64. turtle.back()
  65. end
  66. end
  67.  
  68.  
  69. -- moves turtel forwar for given amount, check for fuel and possible tree on every move
  70. function moveForward(amount)
  71. for n = 1, amount, 1 do
  72. checkFuel(0)
  73. detectAndCutTree()
  74. turtle.forward()
  75. end
  76. end
  77.  
  78. -- plant a sappling, checks for saps in slot 1 and 2. if empty waits for player input
  79. function plantSap()
  80. gotSap = 1
  81.  
  82. turtle.select(1)
  83. sapLevel = turtle.getItemCount(1)
  84. -- if slot 1 is empty try slot 2
  85. if sapLevel == 0 then
  86. turtle.select(2)
  87. sapLevel2 = turtle.getItemCount(2)
  88. -- if slot 2 is empty try slot 3
  89. if sapLevel2 == 0 then
  90. turtle.select(3)
  91. sapLevel3 = turtle.getItemCount(3)
  92. -- if slot 3 is empty try slot 4
  93. if sapLevel3 == 0 then
  94. turtle.select(4)
  95. sapLevel4 = turtle.getItemCount(4)
  96. -- if slot 4 is empty just continue in felling mode
  97. if sapLevel4 == 0 then
  98. gotSap = 0
  99. print("No Saplings! just felling now")
  100. end
  101. end
  102. end
  103. end
  104. -- if gotSap == 1 we cant plant something and have the right slot selected
  105. if gotSap == 1 then
  106. turtle.placeDown()
  107. end
  108.  
  109. end
  110.  
  111. -- move in straight line and planting a sappling on every second field
  112. function plantOrCut(dist)
  113. plant = 0
  114. while dist > 0 do
  115. dist = dist - 1
  116. if plant == 0 then
  117. -- just move and cut no plating
  118. moveForward(1)
  119. plant = 1
  120. else
  121. -- try planting then move and cut
  122. plantSap()
  123. moveForward(1)
  124. plant = 0
  125. end
  126. end
  127. end
  128.  
  129. -- method for moving the turtel through the grid
  130. function moveTurtel()
  131. dist = 10
  132. turn = 0
  133. moveUp(1)
  134. turtle.select(1)
  135.  
  136. for n = 1, dist, 1 do
  137. -- move turtel
  138. plantOrCut(dist)
  139. -- turn right
  140. if turn == 0 then
  141. turtle.turnRight()
  142. moveForward(2)
  143. turtle.turnRight()
  144. turn = 1
  145. -- turn left
  146. else
  147. turtle.turnLeft()
  148. moveForward(2)
  149. turtle.turnLeft()
  150. turn = 0
  151. end
  152. end
  153. end
  154.  
  155.  
  156. -- returns back to chest, refuels and drops loot
  157. function goHome()
  158. turtle.turnLeft()
  159. moveForward(20)
  160. turtle.turnRight()
  161. --sit on chest
  162. moveDown(1)
  163. -- move one wood to another place to make sure the slot will be filled with wood again
  164. turtle.select(5)
  165. turtle.transferTo(15 ,4)
  166. checkFuel(800)
  167.  
  168. -- drop loot to chest
  169. turtle.select(5)
  170. turtle.dropDown()
  171. turtle.select(6)
  172. turtle.dropDown()
  173. turtle.select(7)
  174. turtle.dropDown()
  175. turtle.select(8)
  176. turtle.dropDown()
  177.  
  178. -- move one wood back to 5 to ensure we have wood there to refuel from
  179. -- reserves slot
  180. turtle.select(15)
  181. turtle.transferTo(5 ,1)
  182. turtle.transferTo(6 ,1)
  183. turtle.transferTo(7 ,1)
  184. turtle.transferTo(8 ,1)
  185. end
  186.  
  187. -- Main Routine, endless repeating
  188. -- fuels itself than starts
  189. function gogoTurtel()
  190. turtle.select(16)
  191. turtle.refuel()
  192.  
  193. while 1 > 0 do
  194. fuel = turtle.getFuelLevel()
  195. print("Going with Fuel Level"..fuel)
  196. moveTurtel()
  197. goHome()
  198. end
  199.  
  200. end
  201.  
  202. gogoTurtel()
Add Comment
Please, Sign In to add comment