DeltaDaedalus

Untitled

Aug 15th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.71 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. smart = {
  54. --Same as turtle.forward, but will wait for entities in the way to move, and will register movement in location variable.
  55. forward = function()
  56. if turtle.detect() then return false end
  57. repeat until turtle.forward()
  58. if direction == 0 then location[1] = location[1] + 1
  59. elseif direction == 1 then location[2] = location[2] + 1
  60. elseif direction == 2 then location[1] = location[1] - 1
  61. elseif direction == 3 then location[2] = location[2] - 1 end
  62. --print(location[1], " ", location[2])
  63. return true
  64. end,
  65.  
  66. back = function()
  67. repeat until turtle.back()
  68. if direction == 0 then location[1] = location[1] - 1
  69. elseif direction == 1 then location[2] = location[2] - 1
  70. elseif direction == 2 then location[1] = location[1] + 1
  71. elseif direction == 3 then location[2] = location[2] + 1 end
  72. return true
  73. end,
  74.  
  75. --Same as turtle.turnLeft, but registers rotation in direction variable
  76. turnLeft = function()
  77. turtle.turnLeft()
  78. direction = (direction+1) % 4
  79. --print(direction)
  80. end,
  81.  
  82. --Same as turtle.turnRight, but registers rotation in direction variable
  83. turnRight = function()
  84. turtle.turnRight()
  85. direction = (direction+3) % 4
  86. --print(direction)
  87. end,
  88. }
  89.  
  90. --Action functions
  91. function chopTree()
  92. local _, data = turtle.inspect()
  93. if data.name == "minecraft:sapling" then return end
  94. turtle.dig()
  95. smart.forward()
  96. while turtle.digUp() do
  97. turtle.up()
  98. end
  99. repeat until not turtle.down()
  100. smart.back()
  101. turtle.select(saplingSlot)
  102. turtle.place()
  103. end
  104.  
  105. --Assumes turtle starts in a corner facing along the left edge
  106. function gather() --gathering process thought loop
  107. local phase = true
  108. turtle.select(1)
  109. repeat
  110. while (phase and location[2] < boxH-1) or (location[2] > 0 and not phase) do
  111. turtle.suck()
  112. smart.forward()
  113. end
  114. if phase then smart.turnRight() else smart.turnLeft() end
  115. smart.forward()
  116. smart.forward()
  117. if phase then smart.turnRight() else smart.turnLeft() end
  118. phase = not phase
  119. until location[1] >= boxW-1
  120. end
  121.  
  122. function harvest() --harvesting process thought loop
  123. pathTo({2,1})
  124. turnTo(2)
  125. turtle.select(1)
  126. for i = 1, treeGridW do
  127. for j = 1, treeGridH do
  128. chopTree()
  129. if j < treeGridH then
  130. smart.turnRight()
  131. for i = 1, 4 do smart.forward() end
  132. smart.turnLeft()
  133. end
  134. end
  135.  
  136. if i < treeGridW then
  137. if i % 2 == 1 then
  138. smart.turnRight()
  139. smart.turnRight()
  140. smart.forward()
  141. smart.forward()
  142. else
  143. smart.turnRight()
  144. smart.forward()
  145. smart.turnLeft()
  146. for i = 1, 6 do smart.forward() end
  147. smart.turnLeft()
  148. smart.forward()
  149. smart.turnLeft()
  150. end
  151. end
  152. end
  153. end
  154.  
  155. function output()
  156. pathTo(outLoc)
  157. turnTo(outDir)
  158.  
  159. for i = 1, 16 do
  160. if i ~= saplingSlot then
  161. turtle.select(i)
  162. turtle.drop()
  163. end
  164. end
  165. end
  166.  
  167. function refuel()
  168. pathTo(fuelLoc)
  169. turnTo(fuelDir)
  170.  
  171. turtle.select(fuelSlot)
  172. turtle.suck()
  173. turtle.refuel(turtle.getItemCount())
  174. turtle.select(fuelSlot)
  175. turtle.drop()
  176. end
  177.  
  178. function turnTo(target)
  179. while direction ~= target do smart.turnLeft() end
  180. end
  181.  
  182. --Simple pathing uses presumed locations of trees to go where it wants. Cannot reliably path onto tree locations
  183. function pathTo(target)
  184. if vectorEq(location, target) then return end
  185. --Ensure turtle starts off the tree grid
  186. if location[1] % 4 == 1 then --Turtle has a tree straight North/South
  187. if location[1] < target[1] then turnTo(0) else turnTo(2) end
  188. smart.forward()
  189. elseif location[2] % 4 == 1 then --Turtle has a tree straight East/West
  190. if location[2] < target[2] then turnTo(1) else turnTo(3) end
  191. smart.forward()
  192. end
  193.  
  194. --Walk Either Vertical->Horizontal or Horizontal->Vertical depending on where target is on the tree grid.
  195. if target[1] % 4 == 1 then --Target has a tree straight North/South
  196. if location[2] < target[2] then turnTo(1) else turnTo(3) end
  197. for i = 1, math.abs(target[2] - location[2]) do smart.forward() end
  198. if location[1] < target[1] then turnTo(0) else turnTo(2) end
  199. for i = 1, math.abs(target[1] - location[1]) do smart.forward() end
  200. elseif target[2] % 4 == 1 then --Target has a tree straight East/West
  201. if location[1] < target[1] then turnTo(0) else turnTo(2) end
  202. for i = 1, math.abs(target[1] - location[1]) do smart.forward() end
  203. if location[2] < target[2] then turnTo(1) else turnTo(3) end
  204. for i = 1, math.abs(target[2] - location[2]) do smart.forward() end
  205. else
  206. if location[1] < target[1] then turnTo(0) else turnTo(2) end
  207. for i = 1, math.abs(target[1] - location[1]) do smart.forward() end
  208. if location[2] < target[2] then turnTo(1) else turnTo(3) end
  209. for i = 1, math.abs(target[2] - location[2]) do smart.forward() end
  210. end
  211. end
  212.  
  213. --Startup procedure by which the turtle finds the area to which it is assigned.
  214. function findBounds() --finds boundaries at startup,
  215. while not turtle.detect() do turtle.turnLeft() end --rotate to face "east" from the "southwest" corner of the enclosure
  216. while turtle.detect() do turtle.turnLeft() end
  217.  
  218. repeat
  219. --Check for chests
  220. if fuelLoc == nil or outLoc == nil then
  221. turtle.select(2)
  222. smart.turnRight() --Check edge
  223. local test, data = turtle.inspect()
  224. if test and data.name == "minecraft:chest" then
  225. if turtle.suck() then fuelLoc = vectorCopy(location); fuelDir = direction else outLoc = vectorCopy(location); outDir = direction end
  226. end
  227. smart.turnLeft() --turn back forward
  228. end
  229.  
  230. if direction == 0 then boxW = boxW + 1 elseif direction == 1 then boxH = boxH + 1 end
  231. if not smart.forward() then smart.turnLeft() end
  232. until vectorEq(location, {0,0})
  233.  
  234. treeGridW = math.ceil((boxW - 2) / 4)
  235. treeGridH = math.ceil((boxH - 2) / 4)
  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. findBounds()
  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 > 16 then
  263. error("Error, space given is for " .. treeGridH * treeGridW .. " trees, 16 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. sleep()
  270. pathTo({0, 0})
  271. turnTo(1)
  272. gather()
  273. sleep(60)
  274. pathTo({0, 0})
  275. turnTo(1)
  276. gather()
  277.  
  278.  
  279. output()
  280.  
  281. refuel()
  282.  
  283. sleep(60)
  284. end
Add Comment
Please, Sign In to add comment