Advertisement
Houshalter

ComputerCraft Tree Harverster and Planter

Feb 26th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. --todo list
  2. --make the turtle return to it's origin when done. Possibly start planting again
  3. --let it refill saplings and maybe coal from chests
  4. --let it drop it's load of wood off into the water when full
  5. --maybe make it burn logs when out of fuel
  6.  
  7. --asign each turtle their own row or something like that for speed.
  8. --at least more than one turtle at a time
  9.  
  10. --maybe make it cook the coal. Could be done easier with pipes. hmm
  11.  
  12. function restack()
  13. if turtle.getItemCount(2) > 1 then
  14. turtle.select(2)
  15. turtle.transferTo(1, (turtle.getItemCount(2) - 1))
  16. end
  17.  
  18. if turtle.getItemCount(2) <= 1 then
  19. turtle.select(3)
  20. turtle.transferTo(2, (turtle.getItemCount(3) - 1))
  21. end
  22. turtle.select(1)
  23. end
  24. --moves the turtle forward, digging through blocks if necessary.
  25. --if it fails for some reason it keeps trying until it gets through
  26. --also keeps count of the amount it's moved for location purposes
  27. function digforward(n)
  28. if turtle.getItemCount(1) <= 1 then restack() end
  29. if turtle.getItemCount(1) > 1 then turtle.placeDown() end
  30. for i = 1, n do
  31. repeat
  32. if turtle.detect() then turtle.dig() end
  33. until turtle.forward()
  34.  
  35. movedcount = movedcount + 1
  36. end
  37. end
  38.  
  39. --turn right
  40. --move forward
  41. --for slots 4, 16 drop everything
  42. --turn left twice
  43. --go forward
  44. --turn right
  45. --select the first slot again
  46.  
  47. function dumpinventory()
  48. print("Dumping inventory.")
  49. turtle.turnRight()
  50. turtle.dig()
  51. turtle.forward()
  52. turtle.dig()
  53. for i = 4, 16 do
  54. turtle.select(i)
  55. turtle.drop()
  56. end
  57. turtle.select(1)
  58. turtle.turnLeft()
  59. turtle.turnLeft()
  60. turtle.forward()
  61. turtle.turnRight()
  62. end
  63.  
  64. --start count at 0
  65. --if there is no block above, exit loop and decend back down
  66. --break block above, move up until sucessful, add 1 to height
  67.  
  68. --DECENDING BACK DOWN
  69. --if height =< 0 exit loop
  70. --break block below, move down until sucessful, subtract 1 from height
  71.  
  72. function treeharvest()
  73. height = 0
  74. turtle.digDown()
  75. while true do
  76. if not turtle.detectUp() then break end
  77. repeat
  78. if turtle.detectUp() then turtle.digUp() end
  79. until turtle.up()
  80. height = height + 1
  81. end
  82.  
  83. while true do
  84. if height <= 0 then break end
  85. repeat
  86. if turtle.detectDown() then turtle.digDown() end
  87. until turtle.down()
  88. height = height - 1
  89. end
  90. if turtle.getItemCount(16) >= 32 then dumpinventory() end
  91. if turtle.getFuelLevel() < 300 then refuel() end
  92. end
  93.  
  94. --turn the turtle right
  95. --add 1 to the direction (cyclying back to 1 if greater than 4
  96.  
  97. function turnright()
  98. turtle.turnRight()
  99. direction = direction + 1
  100. if direction > 4 then direction = 0 end
  101. end
  102.  
  103. function turnleft()
  104. turtle.turnLeft()
  105. direction = direction - 1
  106. if direction < 1 then direction = 4 end
  107. end
  108.  
  109.  
  110. --if facing north or east, turn right
  111. --else turn left
  112.  
  113.  
  114. function turn()
  115. print(rotation)
  116. if rotation < 2 then turtle.turnRight() end
  117. if rotation >= 2 then turtle.turnLeft() end
  118. rotation = rotation + 1
  119. if rotation >= 4 then rotation = 0 end
  120. end
  121.  
  122. --refuels the turtle with anything consumable in it's inventory
  123. --goes back to selecting slot 1
  124.  
  125. function refuel()
  126. print("Refueling turtle.")
  127. for i = 4, 16 do
  128. turtle.select(i)
  129. turtle.refuel()
  130. end
  131.  
  132. turtle.select(1)
  133. print("Turtle has " .. turtle.getFuelLevel() .. " fuel left.")
  134. end
  135.  
  136. function iseven(n)
  137. if math.modf(n, 2) == 0 then return true
  138. else return false end
  139. end
  140.  
  141. --GoHome(numberofrows)
  142. --Go to the origin
  143. --turn right
  144. --calculate the number of spaces to go forward
  145. -- the number of rows plus ((the number of rows - 1) * 4)
  146. --go that many spaces forward minus one (you are on that row)
  147. --turn right
  148.  
  149. function GoHome(numberofrows)
  150. print("Returning to origin.")
  151. if iseven(numberofrows) then turtle.turnRight()
  152. else turtle.turnLeft() end
  153. spacestohome = numberofrows + ((numberofrows - 1) * 4)
  154. digforward(spacestohome - 1)
  155. turtle.turnRight()
  156. end
  157.  
  158.  
  159. --rows = 0, travelled = 0
  160. --dig forward
  161. --if spot with a block above, perform treeharvest()
  162. --add one to count of distance travelled.
  163. --if distance travelled is equal to or greater than distance, exit loop
  164. --goto 3
  165. --add 1 to the number of rows done
  166. --if rowsdone is equal to or greater than rows, return(), end program
  167. --turn()
  168. --digforward(5)
  169. --turn()
  170. -- travelled = 0
  171. --goto line 3
  172.  
  173. function harvest(distance, rows)
  174. rowsdone, travelled, rotation = 0, 1, 0
  175. while true do
  176. while true do
  177. if turtle.detectUp() then treeharvest() end
  178. digforward(1)
  179. travelled = travelled + 1
  180. if travelled >= distance then break end
  181. end
  182.  
  183. if turtle.detectUp() then treeharvest() end
  184. rowsdone = rowsdone + 1
  185. if rowsdone >= rows then break end
  186. turn()
  187. digforward(5)
  188. turn()
  189. travelled = 1
  190. end
  191. GoHome(rows)
  192. end
  193.  
  194. print("Number of rows? ")
  195. rowstodo = tonumber(io.read())
  196. print("Number of times? ")
  197. loops = tonumber(io.read())
  198. direction = 1
  199. rotation = 0
  200. movedcount = 0
  201. refuel()
  202. print("Beginning harvest.")
  203. for z = 1, loops do
  204. harvest(30, rowstodo)
  205. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement