Advertisement
HarvDad

quarry

Apr 12th, 2014
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. -- quarry
  2. -- Mines a quarry to the dimensions supplied by the user
  3. -- Turtle drops the spoils on coveyer belts by 3 types
  4. -- Turtle returns to its starting point when mission is completed or fuel runs low
  5. -- Written by HarvDad, April 2014
  6.  
  7. args = {...}
  8. nArgs = #args
  9.  
  10. print("quarry: Rev 3.0")
  11. x = 0
  12. y = 0
  13. z = 0
  14. face = 0
  15. minimumFuel = 100
  16. missionMessage = "Mission complete."
  17. notDone = true
  18. abort = false
  19. local currentFuelLevel = turtle.getFuelLevel()
  20. rockBottom = 0
  21.  
  22. -- The following 'face' directions are relative to the starting position of the turtle in this program
  23. north = 0
  24. west = 1
  25. south = 2
  26. east = 3
  27.  
  28. -- Status data for marking a spot to return to after going 'home'
  29.  
  30. markX = 0
  31. markY = 0
  32. markZ = 0
  33. markFace = 0
  34.  
  35. if nArgs == 0 or (nArgs == 1 and args[1]== "help") then
  36. print("Quarry an area specified by the given length, width, and height")
  37. print("Usage: Quarry <length> <width> [height]")
  38. return
  39. end
  40.  
  41. if nArgs ~= 3 then
  42. print("Usage: Quarry <length> <width> [height]")
  43. return
  44. end
  45.  
  46. length = tonumber(args[1])
  47. if length == nil then
  48. print("\"", args[1], "\" is not a valid length")
  49. return
  50. end
  51. if length < 1 then
  52. print("length must be a positive number greater than zero")
  53. end
  54.  
  55. width = tonumber(args[2])
  56. if width == nil then
  57. print("\"", args[2], "\" is not a valid width")
  58. return
  59. end
  60. if width < 1 then
  61. print("width must be a positive number greater than zero")
  62. end
  63.  
  64. height = tonumber(args[3])
  65. if height == nil then
  66. print("\"", args[3], "\" is not a valid height")
  67. return
  68. end
  69. if height < 1 then
  70. print("height must be a positive number greater than zero")
  71. end
  72.  
  73. targetArea = length * width
  74. areaCovered = 1;
  75.  
  76. function chomp()
  77. while turtle.detect() do
  78. turtle.dig()
  79. end
  80. areaCovered = areaCovered+1
  81. end
  82.  
  83. direction = "left"
  84.  
  85. local clock = os.clock
  86. function sleep(n) -- seconds
  87. local t0 = clock()
  88. while clock() - t0 <= n do end
  89. end
  90.  
  91. function left()
  92. if face == 0 then face = 1 turtle.turnLeft() return end
  93. if face == 1 then face = 2 turtle.turnLeft() return end
  94. if face == 2 then face = 3 turtle.turnLeft() return end
  95. if face == 3 then face = 0 turtle.turnLeft() return end
  96. print("function left\(\): Bad face value: ", face)
  97. end
  98.  
  99. function right()
  100. if face == 0 then face = 3 turtle.turnRight() return end
  101. if face == 1 then face = 0 turtle.turnRight() return end
  102. if face == 2 then face = 1 turtle.turnRight() return end
  103. if face == 3 then face = 2 turtle.turnRight() return end
  104. print("function right\(\): Bad face value: ", face)
  105. end
  106.  
  107. function up()
  108. while turtle.detectUp() do -- This loop added in case of falling sand or whatever
  109. turtle.digUp()
  110. end
  111. while not turtle.up() do
  112. turtle.attackUp()
  113. sleep(2)
  114. end
  115. y = y+1
  116. end
  117.  
  118. function rise(nBlocks)
  119. local i
  120. for i=1,nBlocks do
  121. up()
  122. end
  123. end
  124.  
  125. function down()
  126. if turtle.detectDown() then
  127. if not turtle.digDown() then
  128. print("Unable to dig down. Assuming bedrock")
  129. rockBottom = y
  130. return
  131. end
  132. end
  133. while not turtle.down() do
  134. turtle.attackDown()
  135. sleep(2)
  136. end
  137. y = y-1
  138. end
  139.  
  140. function turn()
  141. if direction == "left" then
  142. left()
  143. else
  144. right()
  145. end
  146. -- chomp()
  147. forward(true)
  148. if direction == "left" then
  149. left()
  150. direction = "right"
  151. else
  152. right()
  153. direction = "left"
  154. end
  155. end
  156.  
  157. function startNextRow()
  158. if direction == "left" then
  159. left()
  160. else
  161. right()
  162. end
  163. forward(true)
  164. turtle.digUp()
  165. turtle.digDown()
  166. if direction == "left" then
  167. left()
  168. direction = "right"
  169. else
  170. right()
  171. direction = "left"
  172. end
  173. end
  174.  
  175. function forward(keepTrackOfArea)
  176. while turtle.detect() do -- This loop added in case of falling sand or whatever
  177. turtle.dig()
  178. end
  179. for i=1,10 do
  180. if turtle.forward() then
  181. if keepTrackOfArea then
  182. areaCovered = areaCovered + 1
  183. end
  184. break
  185. end
  186. turtle.attack()
  187. sleep(2)
  188. end
  189. if face == 0 then z = z+1 return end
  190. if face == 1 then x = x-1 return end
  191. if face == 2 then z = z-1 return end
  192. if face == 3 then x = x+1 return end
  193. end
  194.  
  195. function setFace(f)
  196. if f == 0 then
  197. if face == 0 then return end
  198. if face == 1 then right() return end
  199. if face == 2 then right() right() return end
  200. if face == 3 then left() return end
  201. end
  202.  
  203. if f == 1 then
  204. if face == 0 then left() return end
  205. if face == 1 then return end
  206. if face == 2 then right() return end
  207. if face == 3 then right() right() return end
  208. end
  209.  
  210. if f == 2 then
  211. if face == 0 then left() left() return end
  212. if face == 1 then left() return end
  213. if face == 2 then return end
  214. if face == 3 then right() return end
  215. end
  216.  
  217. if f == 3 then
  218. if face == 0 then right() return end
  219. if face == 1 then left() left() return end
  220. if face == 2 then left() return end
  221. if face == 3 then return end
  222. end
  223. end
  224.  
  225. function home(targetY)
  226. -- print("home:face ", face, ", x = ", x, ", z = ", z)
  227. if x < 0 then
  228. setFace(3)
  229. while x < 0 do
  230. forward()
  231. end
  232. else
  233. if x > 0 then
  234. setFace(1)
  235. while x > 0 do
  236. forward()
  237. end
  238. end
  239. end
  240.  
  241. if z < 0 then
  242. setFace(0)
  243. while z < 0 do
  244. forward()
  245. end
  246. else
  247. if z > 0 then
  248. setFace(2)
  249. while z > 0 do
  250. forward()
  251. end
  252. end
  253. end
  254. setFace(0)
  255.  
  256. if (targetY == 0) then
  257. while y < 0 do
  258. up()
  259. end
  260. end
  261. end
  262.  
  263. function strip(nBlocks)
  264. local i
  265. for i=1,nBlocks do
  266. forward(true)
  267. turtle.digUp()
  268. turtle.digDown()
  269. end
  270. end
  271.  
  272.  
  273. function checkFuel()
  274. if currentFuelLevel == "unlimited" then
  275. return true
  276. end
  277.  
  278. if currentFuelLevel < minimumFuel then
  279. if not turtle.refuel() then
  280. areaCovered = targetArea
  281. missionMessage = "Mission aborted due to low fuel."
  282. abort = true
  283. return false
  284. end
  285. end
  286. return true
  287. end
  288.  
  289. cobbleSlot = 2
  290. dirtSlot = 1
  291. dirtConveyor = -4
  292. cobbleConveyor = -3
  293. oresConveyor = -2
  294. dumpLevel = 1
  295.  
  296.  
  297. function goEmptyInventoryToChestAndReturn()
  298. saveAreaCovered = areaCovered
  299. markX = x
  300. markY = y
  301. markZ = z
  302. markFace = face
  303.  
  304. home()
  305. setFace(east)
  306. while y > 0 do
  307. down()
  308. end
  309. dump()
  310. while y < markY do
  311. up()
  312. end
  313. returnToMark()
  314. areaCovered = saveAreaCovered
  315. end
  316.  
  317. function returnToMark()
  318. if markZ < z then
  319. setFace(south)
  320. while z > markZ do
  321. forward(false)
  322. end
  323. else
  324. if markZ > z then
  325. setFace(north)
  326. while z < markZ do
  327. forward(false)
  328. end
  329. end
  330. end
  331.  
  332. if markX < 0 then
  333. setFace(west)
  334. while x > markX do
  335. forward(false)
  336. end
  337. else
  338. if markX > 0 then
  339. setFace(east)
  340. while x < markX do
  341. forward(false)
  342. end
  343. end
  344. end
  345.  
  346. while y > markY do
  347. down()
  348. end
  349.  
  350. setFace(markFace)
  351. end
  352.  
  353. function dumpItemType(typeSlot)
  354. if typeSlot > 0 then
  355. turtle.select(typeSlot)
  356. dropCount = turtle.getItemCount(typeSlot)
  357. if dropCount > 1 then
  358. turtle.dropDown(dropCount - 1)
  359. end
  360. for i = 3,16 do
  361. turtle.select(i)
  362. if turtle.compareTo(typeSlot) then
  363. turtle.dropDown()
  364. end
  365. end
  366. else
  367. for i = 3,16 do
  368. if turtle.getItemCount(i) > 0 then
  369. turtle.select(i)
  370. turtle.dropDown()
  371. end
  372. end
  373. end
  374. end
  375.  
  376. function dumpAll()
  377. saveAreaCovered = areaCovered
  378. markX = x
  379. markY = y
  380. markZ = z
  381. markFace = face
  382.  
  383. setFace (south)
  384. while y < dumpLevel do
  385. up()
  386. end
  387. while z > dirtConveyor do
  388. forward(false)
  389. end
  390. dumpItemType(dirtSlot)
  391.  
  392. setFace(north)
  393. forward(false)
  394. dumpItemType(cobbleSlot)
  395. forward(false)
  396. dumpItemType(0)
  397. turtle.select(dirtSlot)
  398.  
  399. returnToMark()
  400. end
  401.  
  402. currentLevel = 255
  403.  
  404.  
  405. -- MAIN PROGRAM
  406.  
  407. print("fuelLevel = ", currentFuelLevel)
  408. print("length = ", length)
  409. print("width = ", width)
  410. print("height = ", height)
  411. print("face = ", face)
  412.  
  413. print("Current Fuel Level: ", currentFuelLevel)
  414.  
  415. while notDone do
  416.  
  417. dumpLevel = 1
  418. turtle.select(1)
  419.  
  420. if currentFuelLevel ~= "unlimited" then
  421. if currentFuelLevel < minimumFuel then
  422. if not turtle.refuel() then
  423. print("No fuel")
  424. return
  425. end
  426. end
  427. end
  428.  
  429. if currentLevel == rockBottom then
  430. missionMessage = "Hit rock bottom. "
  431. break
  432. end
  433.  
  434. down()
  435. down()
  436. turtle.digDown()
  437.  
  438. for w=1,width do
  439. strip(length-1)
  440. if z == 0 and x ~= 0 then
  441. currentLevel = y
  442. dumpAll()
  443. while y > currentLevel do
  444. down()
  445. end
  446. end
  447. if areaCovered < targetArea then
  448. startNextRow()
  449. end
  450. if abort then
  451. home(0)
  452. end
  453. end
  454. home(99)
  455. down()
  456. areaCovered = 1
  457. direction = "left"
  458. end
  459.  
  460. print(missionMessage, " Current fuel level is ", turtle.getFuelLevel())
  461. home(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement