Advertisement
kktangent

quarrytest1

Feb 28th, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. -- Mining starts from lower right corner of quarry heading forward
  2.  
  3. -- Turtle facing rotations
  4. -- 0 ^
  5. -- 1 3 |
  6. -- 2 <---+
  7.  
  8. x = 1 -- Turtle position X, Y, Z
  9. y = 1
  10. z = 1
  11. rot = 0 -- 0:+Z, 1:+X, 2:-Z, 3: -X
  12. ix = 1 -- Increase X
  13. ty = 1 -- Target Y
  14. bedrock = false
  15. resuming = false
  16.  
  17. function writeStatus()
  18. local f = fs.open("_status", "wb")
  19. f.write(1) -- Orequarry ID
  20. f.write(x)
  21. f.write(y)
  22. f.write(z)
  23. f.write(rot)
  24. f.write(ix+1) -- Byte is limited to 0, so -1 would loop over to 255
  25. f.write(ty)
  26. f.close()
  27. end
  28.  
  29. function quarry(sx, sy, sz)
  30. local fuelDelay = 260
  31. local oldix = ix
  32. if (sx < 1) or (sy < 1) or (sz < 1) then
  33. return
  34. end
  35.  
  36. fuel(1000, 10000)
  37. empty()
  38. if resuming then
  39. local f = fs.open("_status", "rb")
  40. f.read() -- Orequarry ID
  41. x = f.read()
  42. y = f.read()
  43. z = f.read()
  44. rot = f.read()
  45. ix = f.read()-1
  46. ty = f.read()
  47. f.close()
  48. print(string.format("Status: %d %d %d %d %d", x, y, z, rot, ix))
  49. else
  50. if sx*sy*sz == 1 then return
  51. elseif sy > 2 then ty = 2
  52. end
  53. if sx > 255 then sx = 255 end
  54. if sy > 255 then sy = 255 end
  55. if sz > 255 then sz = 255 end
  56. local f = fs.open("_area", "wb")
  57. f.write(sx)
  58. f.write(sy)
  59. f.write(sz)
  60. f.close()
  61. end
  62.  
  63. -- Start quarry loop
  64. sdelay = 0
  65. while true do
  66. if sdelay == 0 then sleep(1) end
  67. sdelay = (sdelay+1) % 10
  68.  
  69. oldix = ix
  70. if y < ty then
  71. turtle.digDown()
  72. if turtle.down() then
  73. y = y+1 writeStatus()
  74. empty()
  75. else
  76. bedrock = true
  77. ty = y
  78. end
  79. elseif rot == 0 then -- Going forward
  80. if z < sz then
  81. if dig(sy) then
  82. z = z+1 writeStatus()
  83. end
  84. else -- Reached edge
  85. if ix > 0 then turnTo(1)
  86. else turnTo(3)
  87. end
  88. end
  89. elseif rot == 2 then -- Going backwards
  90. if z > 1 then
  91. if dig(sy) then
  92. z = z-1 writeStatus()
  93. end
  94. else -- Reached edge
  95. if ix > 0 then turnTo(1)
  96. else turnTo(3)
  97. end
  98. end
  99. elseif rot == 1 then -- Going left
  100. if x < sx then
  101. if dig(sy) then
  102. x = x+1 writeStatus()
  103. end
  104. else
  105. ty = math.min(y+3, sy)
  106. ix = -ix writeStatus()
  107. while turtle.detectUp() do
  108. if not turtle.digUp() then
  109. break
  110. end
  111. end
  112. if (math.abs(ty-y)<1.5) or bedrock then break end
  113. end
  114. if z < 2 then turnTo(0)
  115. else turnTo(2)
  116. end
  117. elseif rot == 3 then -- Going right
  118. if x > 1 then
  119. if dig(sy) then
  120. x = x-1 writeStatus()
  121. end
  122. else
  123. ty = math.min(y+3, sy)
  124. ix = -ix writeStatus()
  125. while turtle.detectUp() do
  126. if not turtle.digUp() then
  127. break
  128. end
  129. end
  130. if (math.abs(ty-y)<1.5) or bedrock then break end
  131. end
  132. if z < 2 then turnTo(0)
  133. else turnTo(2)
  134. end
  135. end
  136.  
  137. fuelDelay = fuelDelay-1
  138. if fuelDelay < 0 then
  139. fuelDelay = 260
  140. if turtle.getFuelLevel() < 600 then
  141. fuel(10000, 10000)
  142. end
  143. end
  144. end
  145.  
  146. fs.delete("_status")
  147. fs.delete("_area")
  148.  
  149. -- Return to surface
  150.  
  151. -- Come up half way
  152. while y > 1+sy/2 do
  153. y = y-1
  154. turtle.digUp() turtle.up()
  155. end
  156.  
  157. -- Come to X = 1
  158. -- Turn to left
  159. turnTo(3, true) -- Turn but don't save
  160. while x > 1 do
  161. x = x-1
  162. turtle.dig() turtle.forward()
  163. end
  164.  
  165. -- Come to Z = 1
  166. -- Turn to backwards
  167. rot = 2
  168. turtle.turnRight()
  169. while z > 1 do
  170. z = z-1
  171. turtle.dig() turtle.forward()
  172. end
  173.  
  174. -- Come up all the way
  175. while y > 1 do
  176. y = y-1
  177. turtle.digUp() turtle.up()
  178. end
  179. -- Finally empty items to chest
  180. doEmpty()
  181. print("Mining trip finished!")
  182. end
  183.  
  184. function dig(sy)
  185. while turtle.detectUp() do
  186. if not turtle.digUp() then
  187. break
  188. end
  189. end
  190. if (y < sy) and (not bedrock) and turtle.detectDown() then
  191. if not turtle.digDown() then
  192. bedrock = true
  193. end
  194. end
  195. while turtle.detect() do
  196. if not turtle.dig() then
  197. repeat
  198. empty()
  199. until not turtle.suck()
  200. break
  201. end
  202. empty()
  203. end
  204. return turtle.forward()
  205. end
  206.  
  207. function doEmpty()
  208. print("Emptying inventory...")
  209. while turtle.detectUp() do
  210. if not turtle.digUp() then
  211. break
  212. end
  213. end
  214. turtle.select(16) -- Select ender chest for placing
  215. if not turtle.placeUp() then
  216. print("Error placing ender chest")
  217. return
  218. end
  219. for i=1,14 do
  220. turtle.select(i)
  221. turtle.dropUp()
  222. end
  223. turtle.select(16)
  224. turtle.digUp()
  225. sleep(1)
  226. print("Emptying finished")
  227. end
  228.  
  229. function empty()
  230. if (turtle.getItemCount(13) > 0) or (turtle.getItemCount(14) > 0) then
  231. doEmpty()
  232. end
  233. end
  234.  
  235. function fuel(minfuel, maxfuel)
  236. empty() -- Make sure slot 14 is empty
  237. write("Checking fuel...")
  238. if turtle.getFuelLevel() > minfuel then
  239. print("Fuel tank already full")
  240. return
  241. end
  242. while turtle.detectUp() do
  243. if not turtle.digUp() then break end
  244. end
  245. local chest = false
  246. turtle.select(14)
  247. while turtle.getFuelLevel() < maxfuel do
  248. turtle.refuel()
  249. if not chest then
  250. turtle.select(15)
  251. if not turtle.placeUp() then
  252. print("Error placing ender chest")
  253. break
  254. end
  255. chest = true
  256. turtle.select(14)
  257. end
  258. turtle.suckUp()
  259. print("Fuel "..turtle.getFuelLevel())
  260. end
  261. if chest then
  262. turtle.select(15)
  263. turtle.digUp()
  264. end
  265. print("Fuel tank filled")
  266. end
  267.  
  268. function turnTo(newRot, nosave)
  269. function capRot(r)
  270. if r < -0.5 then r = r+4
  271. elseif r > 3.5 then r = r-4
  272. end
  273. return r
  274. end
  275.  
  276. if rot ~= newRot then
  277. if capRot(rot-1) == newRot then
  278. turtle.turnRight()
  279. elseif capRot(rot+1) == newRot then
  280. turtle.turnLeft()
  281. else
  282. turtle.turnRight() turtle.turnRight()
  283. end
  284. rot = newRot
  285. if nosave == nil then writeStatus() end
  286. end
  287. end
  288.  
  289. -- Main program begins
  290.  
  291. param = {...}
  292.  
  293. if #param == 1 then
  294. fs.delete("_status")
  295. fs.delete("_area")
  296. end
  297.  
  298. if fs.exists("_status") and fs.exists("_area") then
  299. local f = fs.open("_status", "rb")
  300. m = f.read()
  301. f.close()
  302. if m == 1 then
  303. resuming = true
  304. local f = fs.open("_area", "rb")
  305. local sx = f.read()
  306. local sy = f.read()
  307. local sz = f.read()
  308. f.close()
  309. print(string.format("Resuming quarry (%d x %d x %d)", sx, sy, sz))
  310. quarry(sx, sy, sz)
  311. fs.delete("_status")
  312. else
  313. print("Resume is already in use for other program")
  314. end
  315. else
  316. if turtle.getItemCount(16) == 0 then
  317. print("Missing enderchest (slot 16) for emptying")
  318. print("Continue anyway? [y/n] ")
  319. if read() ~= "y" then return end
  320. end
  321. if turtle.getItemCount(15) == 0 then
  322. print("Missing enderchest (slot 15) for fuel")
  323. print("Continue anyway? [y/n] ")
  324. if read() ~= "y" then return end
  325. end
  326.  
  327. if #param == 3 then
  328. quarry(0+param[1], 0+param[2], 0+param[3])
  329. elseif #param == 2 then
  330. quarry(0+param[1], 255, 0+param[2])
  331. else
  332. print("Usage: quarry [width] ([height] optional) [depth]")
  333. print("Example: quarry 30 99 30")
  334. print(" or: quarry 32 64")
  335. print("Meeting bedrock will stop the turtle.")
  336. print("Turtle will advance forward and left")
  337. end
  338. fs.delete("_status")
  339. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement