DeltaDaedalus

Untitled

Aug 15th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. --NOTE: currently only works for Oak, Birch, or any other kind of tree that only generates with simple pillars as trunks
  2. --NOTE: currently only plants trees in a grid with 3 blocks in between, beginning at position 1,1
  3.  
  4. print("Beginning Lumberjack v0.1")
  5.  
  6. --Setup parameters
  7. direction = 0 --Current Direction
  8. location = {0, 0} --Current Location
  9. fuelLoc = nil --Location to be in to reful
  10. fuelDir = nil --Direction to face to refuel
  11. outLoc = nil --Location to be in to output
  12. outDir = nil --Direction to face to output
  13. boxW = 0 --Width of the bounding box
  14. boxH = 0 --Height of the bounding box
  15. saplingSlot = 1 --Slot in which to store saplings
  16. fuelSlot = 2 --Slot in which to store fuel
  17. treeGridW = 0 --Number of trees across X
  18. treeGridH = 0 --Number of trees across Y
  19.  
  20. --Vector functions
  21.  
  22. function vectorCopy(v)
  23. local u = {}
  24. for i = 1, #v do
  25. u[i] = v[i]
  26. end
  27. return u
  28. end
  29.  
  30. function vectorEq(u, v)
  31. for i = 1, #u do
  32. if u[i] ~= v[i] then return false end
  33. end
  34. return true
  35. end
  36.  
  37. function vectorAdd(u, v)
  38. local w = {}
  39. for i = 1, #u do
  40. w[i] = u[i] + v[i]
  41. end
  42. end
  43.  
  44. function vectorSub(u, v)
  45. local w = {}
  46. for i = 1, #u do
  47. w[i] = u[i] - v[i]
  48. end
  49. end
  50.  
  51. --Utility functions
  52.  
  53. --Same as turtle.forward, but will wait for entities in the way to move, and will register movement in location variable.
  54. function smartForward()
  55. if turtle.detect() then return false end
  56. repeat until turtle.forward()
  57. if direction == 0 then location[1] = location[1] + 1
  58. elseif direction == 1 then location[2] = location[2] + 1
  59. elseif direction == 2 then location[1] = location[1] - 1
  60. elseif direction == 3 then location[2] = location[2] - 1 end
  61. return true
  62. end
  63.  
  64. function smartBack()
  65. repeat until turtle.back()
  66. if direction == 0 then location[1] = location[1] + 1
  67. elseif direction == 1 then location[2] = location[2] + 1
  68. elseif direction == 2 then location[1] = location[1] - 1
  69. elseif direction == 3 then location[2] = location[2] - 1 end
  70. return true
  71. end
  72.  
  73. function aggressiveForward()
  74. function smartForward()
  75. if turtle.detect() and not turtle.dig() then return false end
  76. repeat until turtle.forward()
  77. if direction == 0 then location[1] = location[1] + 1
  78. elseif direction == 1 then location[2] = location[2] + 1
  79. elseif direction == 2 then location[1] = location[1] - 1
  80. elseif direction == 3 then location[2] = location[2] - 1 end
  81. return true
  82. end
  83.  
  84. --Same as turtle.turnLeft, but registers rotation in direction variable
  85. function smartTurnLeft()
  86. turtle.turnLeft()
  87. direction = (direction+1) % 4
  88. end
  89.  
  90. --Same as turtle.turnRight, but registers rotation in direction variable
  91. function smartTurnRight()
  92. turtle.turnRight()
  93. direction = (direction+3) % 4
  94. end
  95.  
  96. --Action functions
  97.  
  98. --Startup procedure by which the turtle finds the area to which it is assigned.
  99. function findBounds() --finds boundaries at startup,
  100. while not turtle.detect() do turtle.turnLeft() end --rotate to face "east" from the "southwest" corner of the enclosure
  101. while turtle.detect() do turtle.turnLeft() end
  102.  
  103. repeat
  104. --Check for chests
  105. if fuelLoc == nil or outLoc == nil then
  106. turtle.select(2)
  107. smartTurnRight() --Check edge
  108. local test, data = turtle.inspect()
  109. if test and data.name == "minecraft:chest" then
  110. if turtle.suck() then fuelLoc = vectorCopy(location); fuelDir = direction else outLoc = vectorCopy(location); outDir = direction end
  111. end
  112. smartTurnLeft() --Turn back forward
  113. end
  114.  
  115. if direction == 0 then boxW = boxW + 1 elseif direction == 1 then boxH = boxH + 1 end
  116. if not smartForward() then smartTurnLeft() end
  117. until vectorEq(location, {0,0})
  118.  
  119. treeGridW = math.ceil((boxW - 2) / 4)
  120. treeGridH = math.ceil((boxH - 2) / 4)
  121. end
  122.  
  123. function chopTree()
  124. turtle.dig()
  125. smartForward()
  126. while turtle.digUp() do
  127. turtle.up()
  128. end
  129. repeat until not turtle.down()
  130. smartBack()
  131. turtle.select(saplingSlot)
  132. turtle.place()
  133. end
  134.  
  135. --Assumes turtle starts in a corner facing along the left edge
  136. function gather() --gathering process thought loop
  137. while true do
  138. repeat turtle.suck() until not smartForward() end
  139. smartTurnRight()
  140. if not smartForward() then return end
  141. if not smartForward() then return end
  142. smartTurnRight()
  143. end
  144. end
  145.  
  146. function harvest() --harvesting process thought loop
  147. pathTo({2,1})
  148. turnTo(2)
  149. for i = 1, treeGridW do
  150. for j = 1, treeGridH do
  151. chopTree()
  152. if j < treeGridW then
  153. smartTurnLeft()
  154. smartForward()
  155. smartForward()
  156. smartForward()
  157. smartForward()
  158. smartTurnRight()
  159. end
  160. end
  161.  
  162. if i < treeGridW then
  163. if i % 2 == 1 then
  164. smartTurnLeft()
  165. smartTurnLeft()
  166. smartForward()
  167. smartForward()
  168. else
  169. smartTurnRight()
  170. smartForward()
  171. smartTurnLeft()
  172. smartForward()
  173. smartForward()
  174. smartForward()
  175. smartForward()
  176. smartForward()
  177. smartForward()
  178. smartTurnLeft()
  179. smartForward()
  180. smartLeft()
  181. end
  182. end
  183. end
  184. end
  185.  
  186. function output()
  187. pathTo(outLoc)
  188. turnTo(outDir)
  189.  
  190. for i = 1, 16 do
  191. if i ~= saplingSlot then
  192. turtle.drop()
  193. end
  194. end
  195. end
  196.  
  197. function refuel()
  198. pathTo(fuelLoc)
  199. turnTo(fuelDir)
  200.  
  201. turtle.select(fuelSlot)
  202. turtle.suck()
  203. turtle.refuel(turtle.getItemCount())
  204. turtle.select(fuelSlot)
  205. turtle.drop()
  206. end
  207.  
  208. function turnTo(target)
  209. repeat smartTurnLeft() until direction == target
  210. end
  211.  
  212. --Simple pathing uses presumed locations of trees to go where it wants. Cannot reliably path onto tree locations
  213. function pathTo(target)
  214. --Ensure turtle starts off the tree grid
  215. if location[1] % 4 == 1 then --Turtle has a tree straight North/South
  216. if location[1] < target[1] then turtTo(0) else turnTo(2) end
  217. smartForward()
  218. elseif location[2] % 4 == 1 then --Turtle has a tree straight East/West
  219. if location[2] < target[2] then turtTo(1) else turnTo(3) end
  220. smartForward()
  221. end
  222.  
  223. --Walk Either Vertical->Horizontal or Horizontal->Vertical depending on where target is on the tree grid.
  224. if target[1] % 4 == 1 then --Target has a tree straight North/South
  225. if location[1] < target[1] then turtTo(0) else turnTo(2) end
  226. for i = 1, math.abs(target[1] - location[1]) do smartForward() end
  227. for i = 1, math.abs(target[2] - location[2]) do smartForward() end
  228. elseif target[2] % 4 == 1 then --Target has a tree straight East/West
  229. if location[2] < target[2] then turtTo(1) else turnTo(3) end
  230. for i = 1, math.abs(target[2] - location[2]) do smartForward() end
  231. for i = 1, math.abs(target[1] - location[1]) do smartForward() end
  232. else
  233. for i = 1, math.abs(target[1] - location[1]) do smartForward() end
  234. for i = 1, math.abs(target[2] - location[2]) do smartForward() end
  235. end
  236. end
  237.  
  238. --Begin execution
  239. print("Please ensure the turtle has a comfortable amount of fuel to complete the setup process")
  240. read()
  241. print("Please ensure the turtle is in the corner of a flat, clear, rectangular area bound at the corners by blocks.")
  242. read()
  243. print("Please ensure the rectangular area has a fuel chest somewhere on its edge with fuel inside.")
  244. read()
  245. print("Please ensure the rectangular area has an empty output chest somewhere on its edge, and has saplings in slot 1")
  246. read()
  247. print("Please stand outside the rectangular area. Once the turtle has finished detecting boundaries, you may remove them.")
  248.  
  249. pathTo({2, 0})
  250.  
  251. print(boxW .. " by " .. boxH)
  252. if boxW < 3 or boxH < 3 then error("Error, area must be at least 3x3") end
  253.  
  254. if fuelLoc == nil then
  255. error("Error, no fuel chest found, please ensure your intended fuel chest contains some fuel")
  256. else print("Fuel at " .. fuelLoc[1] .. "," .. fuelLoc[2] .. " facing " .. fuelDir) end
  257.  
  258. if outLoc == nil then
  259. error("Error, no output chest found, please ensure your intended output chest is empty")
  260. else print("Output at " .. outLoc[1] .. "," .. outLoc[2] .. " facing " .. outDir) end
  261.  
  262. if treeGridH * treeGridW > 64 then
  263. error("Error, space given is for " .. treeGridH * treeGridW .. " trees, 64 is the max number allowed!")
  264. else print("Enough space for " .. treeGridH * treeGridW .. " trees.") end
  265.  
  266. while true do --basic thought loop
  267. harvest()
  268.  
  269. pathTo({boxW-1, boxH-1})
  270. turnTo(3)
  271. gather()
  272.  
  273. output()
  274.  
  275. refuel()
  276.  
  277. sleep(30)
  278. end
Advertisement
Add Comment
Please, Sign In to add comment