Advertisement
HarvDad

trees

Apr 1st, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. -- trees
  2. -- Harvests trees laid out in rows
  3. -- Trees must be 1x1 trunks with no branches
  4. -- If supplied, saplings will be replanted
  5. -- Program assumes turtle is pre-fueled
  6. -- Turtle returns to its starting point when mission is completed or fuel runs low
  7. -- Written by HarvDad, April 2014
  8.  
  9. args = {...}
  10. nArgs = #args
  11.  
  12. version = "trees: Rev 1.2"
  13. mission = "Harvest trees laid out in rows"
  14.  
  15. usage = "Usage: trees <rows>"
  16.  
  17. x = 0
  18. y = 0
  19. z = 0
  20. face = 0
  21. missionMessage = "Mission complete."
  22. abort = false
  23. logSlot = 1
  24. saplingSlot = 16
  25. markerSlot = 15
  26. local currentFuelLevel = turtle.getFuelLevel()
  27.  
  28. -- The following 'face' directions are relative to the starting position of the turtle in this program
  29. north = 0
  30. west = 1
  31. south = 2
  32. east = 3
  33.  
  34. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  35. print(version)
  36. print(mission)
  37. print("Place sample log in slot ", logSlot)
  38. print("Place sapling supply in slot ", saplingSlot)
  39. print("Place marker block in slot ", markerSlot)
  40. print(usage)
  41. return
  42. end
  43.  
  44. if nArgs ~= 1 then
  45. print(usage)
  46. return
  47. end
  48.  
  49. rows = tonumber(args[1])
  50. if rows == nil then
  51. print("\"", args[1], "\" is not a valid row count.")
  52. return
  53. end
  54. if rows < 1 then
  55. print("Number of rows must be positive number.")
  56. end
  57.  
  58. areaCovered = 1
  59. currentSlot = 2
  60. materialSlot = 1
  61. maxSlot = 16
  62.  
  63. nextTurn = "left"
  64.  
  65. function setFace(f)
  66. if f == 0 then
  67. if face == 0 then return end
  68. if face == 1 then right() return end
  69. if face == 2 then right() right() return end
  70. if face == 3 then left() return end
  71. end
  72.  
  73. if f == 1 then
  74. if face == 0 then left() return end
  75. if face == 1 then return end
  76. if face == 2 then right() return end
  77. if face == 3 then right() right() return end
  78. end
  79.  
  80. if f == 2 then
  81. if face == 0 then left() left() return end
  82. if face == 1 then left() return end
  83. if face == 2 then return end
  84. if face == 3 then right() return end
  85. end
  86.  
  87. if f == 3 then
  88. if face == 0 then right() return end
  89. if face == 1 then left() left() return end
  90. if face == 2 then left() return end
  91. if face == 3 then return end
  92. end
  93. end
  94.  
  95. function forward()
  96. for i=1,10 do -- This loop trys to handle pests (mob) that might be in the way
  97. if turtle.forward() then
  98. break
  99. end
  100. turtle.attack()
  101. sleep(2)
  102. end
  103. areaCovered = areaCovered + 1
  104.  
  105. if face == 0 then z = z+1 return end
  106. if face == 1 then x = x-1 return end
  107. if face == 2 then z = z-1 return end
  108. if face == 3 then x = x+1 return end
  109. end
  110.  
  111. function left()
  112. if face == 0 then face = 1 turtle.turnLeft() return end
  113. if face == 1 then face = 2 turtle.turnLeft() return end
  114. if face == 2 then face = 3 turtle.turnLeft() return end
  115. if face == 3 then face = 0 turtle.turnLeft() return end
  116. print("function left\(\): Bad face value: ", face)
  117. end
  118.  
  119. function right()
  120. if face == 0 then face = 3 turtle.turnRight() return end
  121. if face == 1 then face = 0 turtle.turnRight() return end
  122. if face == 2 then face = 1 turtle.turnRight() return end
  123. if face == 3 then face = 2 turtle.turnRight() return end
  124. print("function right\(\): Bad face value: ", face)
  125. end
  126.  
  127. function up()
  128. for i=1,10 do
  129. if not turtle.up() then
  130. if turtle.detectUp() then
  131. turtle.digUp()
  132. else
  133. turtle.attackUp()
  134. sleep(2)
  135. end
  136. else
  137. break
  138. end
  139. end
  140. y = y+1
  141. end
  142.  
  143. function down()
  144. for i=1,10 do
  145. if not turtle.down() then
  146. if turtle.detectDown() then
  147. turtle.digDown()
  148. else
  149. turtle.attackDown()
  150. sleep(2)
  151. end
  152. else
  153. break
  154. end
  155. end
  156. y = y-1
  157. end
  158.  
  159. function focusSaplings()
  160. local i
  161. saplingCount = turtle.getItemCount(saplingSlot)
  162.  
  163. if saplingCount < 2 then
  164. for i=2,15 do
  165. turtle.select(i)
  166. if turtle.compareTo(saplingSlot) then
  167. turtle.transferTo(saplingSlot)
  168. end
  169. end
  170. end
  171.  
  172. turtle.select(saplingSlot)
  173. end
  174.  
  175. function nextTree()
  176. success = false
  177.  
  178. while not success do
  179. if not turtle.forward() then
  180. turtle.select(logSlot)
  181. if turtle.compare() then
  182. success = true
  183. else
  184. turtle.select(saplingSlot)
  185. if turtle.compare() then
  186. success = true
  187. else
  188. if turtle.detect() then
  189. missionMessage = "Encountered unexpected block."
  190. sleep(5)
  191. abort = true
  192. return success
  193. else
  194. for i=1,10 do
  195. success = turtle.forward()
  196. if not success then
  197. turtle.attack()
  198. sleep(2)
  199. else
  200. if z >= lastZ or x <= lastX then
  201. return
  202. end
  203. updateLocation()
  204. end
  205. end
  206. missionMessage = "Could not get past unknown entity"
  207. abort = true
  208. return success
  209. end
  210. end
  211. end
  212. else
  213. if z >= lastZ or x <= lastX then
  214. return success
  215. end
  216. updateLocation()
  217. end
  218. -- print("nextTree: x = ", x, ", z = ", z)
  219. end
  220. return success
  221. end
  222.  
  223. function updateLocation()
  224. if face == 0 then z = z+1 return end
  225. if face == 1 then x = x-1 return end
  226. if face == 2 then z = z-1 return end
  227. if face == 3 then x = x+1 return end
  228. end
  229.  
  230. function harvestSapling()
  231. turtle.dig()
  232. forward()
  233. up()
  234. turtle.select(saplingSlot)
  235. turtle.placeDown()
  236. turtle.select(logSlot)
  237. end
  238.  
  239. function harvestTree()
  240. turtle.dig()
  241. forward()
  242. while turtle.detectUp() do
  243. turtle.digUp()
  244. up()
  245. end
  246.  
  247. while y > 1 do
  248. down()
  249. end
  250. turtle.select(saplingSlot)
  251. turtle.placeDown()
  252. turtle.select(logSlot)
  253. end
  254.  
  255. function chopTree()
  256. turtle.select(logSlot)
  257. if turtle.compare() then
  258. turtle.dig()
  259. forward()
  260. while turtle.detectUp() do
  261. turtle.digUp()
  262. turtle.up()
  263. end
  264.  
  265. while not turtle.detectDown() do
  266. turtle.suckDown()
  267. turtle.down()
  268. end
  269. else
  270. turtle.dig()
  271. forward()
  272. end
  273. forward()
  274. totalCut = totalCut + 1
  275. end
  276.  
  277. function replantTree()
  278. focusSaplings()
  279. if turtle.getItemCount(saplingSlot) > 0 then
  280. left()
  281. left()
  282. turtle.select(saplingSlot)
  283. turtle.place()
  284. left()
  285. left()
  286. end
  287. end
  288.  
  289. function _home()
  290. setFace(east)
  291. forward()
  292.  
  293. if z < 0 then
  294. setFace(north)
  295. while z < 0 do
  296. forward()
  297. end
  298. else
  299. if z > 0 then
  300. setFace(south)
  301. while z > 0 do
  302. forward()
  303. end
  304. end
  305. end
  306.  
  307. if x < 0 then
  308. setFace(west)
  309. while x < 0 do
  310. forward()
  311. end
  312. else
  313. if x > 0 then
  314. setFace(east)
  315. while x > 0 do
  316. forward()
  317. end
  318. end
  319. end
  320.  
  321. setFace(north)
  322. nextTurn = "left"
  323. end
  324.  
  325. function nextRow()
  326. if nextTurn == "left" then
  327. left()
  328. for i=1,interval do
  329. forward()
  330. end
  331. left()
  332. nextTurn = "right"
  333. else
  334. right()
  335. for i=1,interval do
  336. forward()
  337. end
  338. right()
  339. nextTurn = "left"
  340. end
  341. end
  342.  
  343. function facingTree()
  344. turtle.select(logSlot)
  345. return turtle.compare()
  346. end
  347.  
  348. function facingSapling() -- WARNING!!! turtles cannot dependably compare to saplings (BUG)
  349. turtle.select(saplingSlot)
  350. return turtle.compare()
  351. end
  352.  
  353. function facingMarker()
  354. turtle.select(markerSlot)
  355. return turtle.compare()
  356. end
  357.  
  358. spanSet = false
  359.  
  360. function harvestForward()
  361. if y > 0 then
  362. turtle.dig()
  363. forward()
  364. down()
  365. else
  366. if facingTree() then
  367. harvestTree()
  368. else
  369. if facingMarker() and x == 0 then
  370. spanZ = z
  371. spanSet = true
  372. else
  373. if turtle.detect() then -- sadly, we must assume it's a sapling
  374. harvestSapling()
  375. else
  376. forward()
  377. end
  378. end
  379. end
  380. end
  381. end
  382.  
  383. function home()
  384. if z == 0 then
  385. left()
  386. while x < 0 do
  387. forward()
  388. end
  389. else
  390. right()
  391. while x < 0 do
  392. forward()
  393. end
  394. forward()
  395. right()
  396. while z > 0 do
  397. forward()
  398. end
  399. right()
  400. forward()
  401. end
  402. setFace(north)
  403. end
  404.  
  405. -- Main program
  406.  
  407. totalCut = 0
  408. rowCount = 0
  409. columnCount = 0
  410. firstZ = 0
  411. lastZ = 0
  412. lastX = 0
  413. interval = 5
  414.  
  415.  
  416. setFace(north)
  417. nextTurn = "left"
  418.  
  419. --[[
  420. for i=1,30 do
  421. if facingTree() then
  422. break
  423. end
  424. forward()
  425. end
  426. --]]
  427.  
  428. firstZ = z
  429. lastZ = firstZ + ((rows-1) * interval)
  430. spanZ = lastZ - firstZ
  431.  
  432. spanX = -12
  433. spanZ = 100
  434. lastJ = 0
  435.  
  436. for i = 1,rows do
  437. for j = 1,spanZ do
  438. -- forward()
  439. harvestForward()
  440. if x == 0 and spanSet then
  441. break
  442. end
  443. end
  444. if i < rows then
  445. nextRow()
  446. end
  447. end
  448.  
  449. if y > 0 then
  450. turtle.dig()
  451. forward()
  452. down()
  453. end
  454.  
  455. print("Heading home...")
  456.  
  457. home()
  458.  
  459. if abort then
  460. print("Mission aborted")
  461. end
  462. print(missionMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement