AndyE7

Mine 8.0

Sep 30th, 2025 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.50 KB | None | 0 0
  1. ----------------Save And Load---------------
  2.  
  3. local save = function(data, name)
  4. if not fs.exists('/data') then
  5. fs.makeDir('/data')
  6. end
  7. local file = fs.open('/data/'..name, 'w')
  8. file.write(textutils.serialize(data))
  9. file.close()
  10. end
  11.  
  12. local load = function(name)
  13. if fs.exists('/data/'..name) then
  14. local file = fs.open('/data/'..name, 'r')
  15. data = textutils.unserialize(file.readAll())
  16. file.close()
  17. end
  18. return data
  19. end
  20.  
  21. --------------------------------------------
  22.  
  23. local tArgs = {...}
  24.  
  25. local length
  26. local height
  27. local width
  28.  
  29. if not fs.exists('/data/mineSize') then
  30. if #tArgs ~= 3 then
  31. print("Required Length, Height and Width")
  32. return
  33. end
  34.  
  35. length = tonumber(tArgs[1])
  36. height = tonumber(tArgs[2])
  37. width = tonumber(tArgs[3])
  38. else
  39. savedData = load('mineSize')
  40. continueFromLast = true
  41.  
  42. length = tonumber(savedData[1])
  43. height = tonumber(savedData[2])
  44. width = tonumber(savedData[3])
  45. end
  46.  
  47.  
  48.  
  49. local currentDirectionFacing
  50. local lastKnownDirectionFacing
  51.  
  52. local currentXValue
  53. local currentYValue
  54. local currentZValue
  55.  
  56. local lastKnownXValue
  57. local lastKnownYValue
  58. local lastKnownZValue
  59.  
  60. local minXValue
  61. local minYValue
  62. local minZValue
  63.  
  64. local maxXValue
  65. local maxYValue
  66. local maxZValue
  67.  
  68. if length == nil or height == nil or width == nil then
  69. print("Invalid Dimensions")
  70. return
  71. end
  72.  
  73. if length < 3 or height < 3 or width < 3 then
  74. if width < 3 then
  75. print("Length must be 3 or more")
  76. end
  77. if height < 3 then
  78. print("Height must be 3 or more")
  79. end
  80. if length < 3 then
  81. print("Width must be 3 or more")
  82. end
  83.  
  84. return
  85. end
  86.  
  87. if width % 2 == 0 then
  88. print("Width must be an odd number")
  89. return
  90. end
  91.  
  92. if height % 2 == 0 then
  93. print("Current Bug Detected:")
  94. print("Height set to Even Number")
  95. print("The turtle will return after the Second Last Layer")
  96. end
  97.  
  98. ---------------Misc Functions---------------
  99. --22.03
  100.  
  101. local calcDirection = function(d)
  102. if d > 3 then
  103. d = 0
  104. end
  105. if d < 0 then
  106. d = 3
  107. end
  108. return d
  109. end
  110.  
  111. local updateVariable = function(x, y, z, d)
  112. if x ~= nil and x ~= 0 then
  113. currentXValue = currentXValue+x
  114. end
  115. if y ~= nil and y ~= 0 then
  116. currentYValue = currentYValue+y
  117. end
  118. if z ~= nil and z ~= 0 then
  119. currentZValue = currentZValue+z
  120. end
  121. if d ~= nil then
  122. currentDirectionFacing = calcDirection(currentDirectionFacing+d)
  123. end
  124.  
  125. save({currentXValue,currentYValue,currentZValue,currentDirectionFacing},"currentLocation")
  126.  
  127. end
  128.  
  129. local dig = function(digFront, digTop, digBottom)
  130.  
  131. if digFront ~= false then
  132. turtle.dig()
  133. end
  134.  
  135. if digTop ~= false then
  136. turtle.digUp()
  137. end
  138.  
  139. if digBottom ~= false then
  140. turtle.digDown()
  141. end
  142.  
  143. end
  144.  
  145. local inventoryCheck = function()
  146. if turtle.getItemCount(16) >= 1 then
  147. return true
  148. else
  149. return false
  150. end
  151. end
  152.  
  153. local refuelCheck = function()
  154. while turtle.getFuelLevel() <= 1 do
  155. turtle.select(1)
  156. turtle.refuel(1)
  157. end
  158. end
  159.  
  160. local emptyInventory = function()
  161. for i = 2, 16 do
  162. turtle.select(i)
  163. turtle.drop()
  164. end
  165. turtle.select(1)
  166. end
  167.  
  168. --------------------------------------------
  169.  
  170.  
  171.  
  172.  
  173. ------------------Movement------------------
  174.  
  175. -- Moving
  176.  
  177. local forwardX = function(digFront, digTop, digBottom)
  178. refuelCheck()
  179. local continueLoop = false
  180. while continueLoop == false do
  181. while turtle.detect() == true do
  182. dig(digFront, digTop, digBottom)
  183. end
  184. continueLoop = turtle.forward()
  185. end
  186. updateVariable(1,0,0)
  187. end
  188.  
  189. local backX = function(digFront, digTop, digBottom)
  190. refuelCheck()
  191. local continueLoop = false
  192. while continueLoop == false do
  193. while turtle.detect() == true do
  194. dig(digFront, digTop, digBottom)
  195. end
  196. continueLoop = turtle.forward()
  197. end
  198. updateVariable(-1,0,0)
  199. end
  200.  
  201. local forwardZ = function(digFront, digTop, digBottom)
  202. refuelCheck()
  203. local continueLoop = false
  204. while continueLoop == false do
  205. while turtle.detect() == true do
  206. dig(digFront, digTop, digBottom)
  207. end
  208. continueLoop = turtle.forward()
  209. end
  210. updateVariable(0,0,1)
  211. end
  212.  
  213. local backZ = function(digFront, digTop, digBottom)
  214. refuelCheck()
  215. local continueLoop = false
  216. while continueLoop == false do
  217. while turtle.detect() == true do
  218. dig(digFront, digTop, digBottom)
  219. end
  220. continueLoop = turtle.forward()
  221. end
  222. updateVariable(0,0,-1)
  223. end
  224.  
  225. local up = function(digFront, digTop, digBottom)
  226. refuelCheck()
  227. local continueLoop = false
  228. while continueLoop == false do
  229. while turtle.detectUp() == true do
  230. dig(digFront, digTop, digBottom)
  231. end
  232. continueLoop = turtle.up()
  233. end
  234. updateVariable(0,1,0)
  235. end
  236.  
  237. local down = function(digFront, digTop, digBottom)
  238. refuelCheck()
  239. local continueLoop = false
  240. while continueLoop == false do
  241. while turtle.detectDown() == true do
  242. dig(digFront, digTop, digBottom)
  243. end
  244. continueLoop = turtle.down()
  245. end
  246. updateVariable(0,-1,0)
  247. end
  248.  
  249. local moveToCenterX = function()
  250. while currentXValue ~= maxXValue/2 do
  251. if currentDirectionFacing == 1 then
  252. forwardX()
  253. elseif currentDirectionFacing == 3 then
  254. backX()
  255. end
  256. end
  257. end
  258.  
  259. local moveToMaxY = function()
  260. while currentYValue ~= maxYValue do
  261. up()
  262. end
  263. end
  264.  
  265. local moveToMinX = function()
  266. while currentXValue ~= minXValue do
  267. backX()
  268. end
  269. end
  270.  
  271. local moveToMinZ = function()
  272. while currentZValue ~= minZValue do
  273. backZ()
  274. end
  275. end
  276.  
  277. local moveToLastZ = function()
  278. while currentZValue ~= lastKnownZValue do
  279. if currentZValue < lastKnownZValue then
  280. forwardZ()
  281. else
  282. backZ()
  283. end
  284. end
  285. end
  286.  
  287. local moveToLastX = function()
  288. while currentXValue ~= lastKnownXValue do
  289. if currentXValue < lastKnownXValue then
  290. forwardX()
  291. else
  292. backX()
  293. end
  294. end
  295. end
  296.  
  297. local moveToLastY = function()
  298. while currentYValue ~= lastKnownYValue do
  299. if currentYValue > lastKnownYValue then
  300. down()
  301. elseif currentYValue < lastKnownYValue then
  302. up()
  303. end
  304. end
  305. end
  306.  
  307. -- Turning
  308.  
  309. local left = function()
  310. turtle.turnLeft()
  311. updateVariable(0,0,0,-1)
  312. end
  313.  
  314. local right = function()
  315. turtle.turnRight()
  316. updateVariable(0,0,0,1)
  317. end
  318.  
  319. local turnToCenterX = function()
  320. if currentXValue > maxXValue/2 then
  321. while currentDirectionFacing ~= 3 do
  322. left()
  323. end
  324. else
  325. while currentDirectionFacing ~= 1 do
  326. right()
  327. end
  328. end
  329. end
  330.  
  331. local turnToMinX = function()
  332. while currentDirectionFacing ~= 3 do
  333. if currentDirectionFacing == 0 then
  334. left()
  335. else
  336. right()
  337. end
  338. end
  339. end
  340.  
  341. local turnToMaxX = function()
  342. while currentDirectionFacing ~= 1 do
  343. if currentDirectionFacing == 2 then
  344. left()
  345. else
  346. right()
  347. end
  348. end
  349. end
  350.  
  351. local turnToMinZ = function()
  352. while currentDirectionFacing ~= 2 do
  353. if currentDirectionFacing == 3 then
  354. left()
  355. else
  356. right()
  357. end
  358. end
  359. end
  360.  
  361. local turnToMaxZ = function()
  362. while currentDirectionFacing ~= 0 do
  363. if currentDirectionFacing == 1 then
  364. left()
  365. else
  366. right()
  367. end
  368. end
  369. end
  370.  
  371. local turnToLastX = function()
  372. if currentXValue > lastKnownXValue then
  373. turnToMinX()
  374. elseif currentXValue < lastKnownXValue then
  375. turnToMaxX()
  376. end
  377. end
  378.  
  379. local turnToLastDirection = function()
  380. while currentDirectionFacing ~= lastKnownDirectionFacing do
  381. left()
  382. end
  383. end
  384.  
  385. local moveToLastMinedZ = function()
  386. while currentZValue ~= lastKnownMiningZValue do
  387. if currentZValue < lastKnownMiningZValue then
  388. forwardZ()
  389. else
  390. backZ()
  391. end
  392. end
  393. end
  394.  
  395. local moveToLastMinedX = function()
  396. while currentXValue ~= lastKnownMiningXValue do
  397. if currentXValue < lastKnownMiningXValue then
  398. forwardX()
  399. else
  400. backX()
  401. end
  402. end
  403. end
  404.  
  405. local moveToLastMinedY = function()
  406. while currentYValue ~= lastKnownMiningYValue do
  407. if currentYValue > lastKnownMiningYValue then
  408. down()
  409. elseif currentYValue < lastKnownMiningYValue then
  410. up()
  411. end
  412. end
  413. end
  414.  
  415. local turnToLastMinedDirection = function()
  416. while currentDirectionFacing ~= lastKnownMiningDirectionFacing do
  417. left()
  418. end
  419. end
  420.  
  421. --------------------------------------------
  422.  
  423.  
  424.  
  425.  
  426.  
  427. ----------------GPS Function----------------
  428.  
  429. local locateLastPosition = function()
  430. local savedLocation = load('currentLocation')
  431. local miningLoc = load('miningLocation')
  432.  
  433. minXValue = 0
  434. minYValue = 1
  435. minZValue = 0
  436.  
  437. maxXValue = width-1
  438. maxYValue = height
  439. maxZValue = length-1
  440.  
  441. currentXValue = tonumber(savedLocation[1])
  442. currentYValue = tonumber(savedLocation[2])
  443. currentZValue = tonumber(savedLocation[3])
  444.  
  445. lastKnownMiningXValue = tonumber(miningLoc[1])
  446. lastKnownMiningYValue = tonumber(miningLoc[2])
  447. lastKnownMiningZValue = tonumber(miningLoc[3])
  448. lastKnownMiningDirectionFacing = tonumber(miningLoc[4])
  449.  
  450. lastKnownXValue = currentXValue
  451. lastKnownYValue = currentYValue
  452. lastKnownZValue = currentZValue
  453.  
  454. currentDirectionFacing = tonumber(savedLocation[4])
  455. lastKnownDirectionFacing = currentDirectionFacing
  456.  
  457. end
  458.  
  459. local locateInitialPosition = function()
  460.  
  461. minXValue = 0
  462. minYValue = 1
  463. minZValue = 0
  464.  
  465. maxXValue = width-1
  466. maxYValue = height
  467. maxZValue = length-1
  468.  
  469. currentXValue = maxXValue/2
  470. currentYValue = maxYValue
  471. currentZValue = minZValue
  472.  
  473. lastKnownXValue = currentXValue
  474. lastKnownYValue = currentYValue
  475. lastKnownZValue = currentZValue
  476.  
  477. currentDirectionFacing = 0
  478.  
  479. end
  480.  
  481. --------------------------------------------
  482.  
  483.  
  484. --------------Return Functions--------------
  485.  
  486. local returnToFirstCorner = function()
  487.  
  488. turnToMinZ()
  489. moveToMinZ()
  490.  
  491. turnToMinX()
  492. moveToMinX()
  493.  
  494. end
  495.  
  496. local returnToLastPosition = function()
  497.  
  498. turnToMaxZ()
  499. moveToLastZ()
  500.  
  501. turnToLastX()
  502. moveToLastX()
  503.  
  504. moveToLastY()
  505.  
  506. turnToLastDirection()
  507.  
  508. end
  509.  
  510.  
  511. local returnToLastMinedPosition = function()
  512.  
  513. turnToMaxZ()
  514. moveToLastMinedZ()
  515.  
  516. turnToLastX()
  517. moveToLastMinedX()
  518.  
  519. moveToLastMinedY()
  520.  
  521. turnToLastMinedDirection()
  522.  
  523. end
  524.  
  525.  
  526. local returnToChest = function()
  527. save({currentXValue, currentYValue, currentZValue, currentDirectionFacing}, "miningLocation")
  528.  
  529. lastKnownDirectionFacing = currentDirectionFacing
  530. lastKnownXValue = currentXValue
  531. lastKnownYValue = currentYValue
  532. lastKnownZValue = currentZValue
  533.  
  534. moveToMaxY()
  535. turnToCenterX()
  536. moveToCenterX()
  537. turnToMinZ()
  538. moveToMinZ()
  539.  
  540. emptyInventory()
  541. end
  542.  
  543.  
  544.  
  545. --------------------------------------------
  546.  
  547.  
  548. ----------------Mine Function---------------
  549.  
  550. local mineStripForwardZ = function()
  551. turnToMaxZ()
  552. while currentZValue ~= maxZValue do
  553. if inventoryCheck() == true then
  554. returnToChest()
  555. returnToLastPosition()
  556. end
  557. dig()
  558. forwardZ()
  559. end
  560. end
  561.  
  562. local mineStripBackZ = function()
  563. turnToMinZ()
  564. while currentZValue ~= minZValue do
  565. if inventoryCheck() == true then
  566. returnToChest()
  567. returnToLastPosition()
  568. end
  569. dig()
  570. backZ()
  571. end
  572. end
  573.  
  574. local nextStrip = function()
  575. turnToMaxX()
  576. dig()
  577. forwardX()
  578. end
  579.  
  580. local nextLayer = function()
  581. local currentYValueClone = currentYValue
  582. if currentYValue == minYValue+1 then
  583. down(false,false,false)
  584. elseif currentYValue-2 <= minYValue then
  585. while currentYValue ~= minYValue+1 do
  586. down(false,false,true)
  587. end
  588. else
  589. while currentYValue ~= currentYValueClone-2 do
  590. down(false,true,true)
  591. end
  592. end
  593. end
  594.  
  595. --------------------------------------------
  596.  
  597.  
  598. ----------------Main Function---------------
  599.  
  600.  
  601. local mineLayerHandler = function()
  602.  
  603. save({length,height,width},"mineSize")
  604.  
  605. turnToMinX()
  606. moveToMinX()
  607.  
  608. while currentYValue ~= minYValue+1 do
  609. while currentXValue < maxXValue do
  610. mineStripForwardZ()
  611. nextStrip()
  612. mineStripBackZ()
  613. nextStrip()
  614. end
  615. if currentXValue == maxXValue then
  616. mineStripForwardZ()
  617. turtle.digUp()
  618. turtle.digDown()
  619. end
  620. returnToFirstCorner()
  621. nextLayer()
  622. end
  623. returnToChest()
  624. right()
  625. right()
  626. fs.delete("/data/mineSize")
  627. fs.delete("/data/currentLocation")
  628. end
  629.  
  630. -- turning to max z
  631. -- moving to max z
  632. -- turning to max x
  633. -- moving forward one
  634. -- turning to min z
  635. -- continuing mining
  636.  
  637. if continueFromLast ~= true then
  638. locateInitialPosition()
  639. mineLayerHandler()
  640. else
  641. locateLastPosition()
  642. returnToLastMinedPosition()
  643. mineLayerHandler()
  644. end
  645.  
  646.  
  647.  
  648.  
  649. --------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment