Advertisement
AndyE7

Mine 7.0

Mar 22nd, 2022
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.76 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. --------------------------------------------
  386.  
  387.  
  388.  
  389.  
  390.  
  391. ----------------GPS Function----------------
  392.  
  393. local locateLastPosition = function()
  394. savedLocation = load('currentLocation')
  395.  
  396. minXValue = 0
  397. minYValue = 1
  398. minZValue = 0
  399.  
  400. maxXValue = width-1
  401. maxYValue = height
  402. maxZValue = length-1
  403.  
  404. currentXValue = tonumber(savedLocation[1])
  405. currentYValue = tonumber(savedLocation[2])
  406. currentZValue = tonumber(savedLocation[3])
  407.  
  408. lastKnownXValue = currentXValue
  409. lastKnownYValue = currentYValue
  410. lastKnownZValue = currentZValue
  411.  
  412. currentDirectionFacing = tonumber(savedLocation[4])
  413. lastKnownDirectionFacing = currentDirectionFacing
  414.  
  415. end
  416.  
  417. local locateInitialPosition = function()
  418.  
  419. minXValue = 0
  420. minYValue = 1
  421. minZValue = 0
  422.  
  423. maxXValue = width-1
  424. maxYValue = height
  425. maxZValue = length-1
  426.  
  427. currentXValue = maxXValue/2
  428. currentYValue = maxYValue
  429. currentZValue = minZValue
  430.  
  431. lastKnownXValue = currentXValue
  432. lastKnownYValue = currentYValue
  433. lastKnownZValue = currentZValue
  434.  
  435. currentDirectionFacing = 0
  436.  
  437. end
  438.  
  439. --------------------------------------------
  440.  
  441.  
  442. --------------Return Functions--------------
  443.  
  444. local returnToFirstCorner = function()
  445.  
  446. turnToMinZ()
  447. moveToMinZ()
  448.  
  449. turnToMinX()
  450. moveToMinX()
  451.  
  452. end
  453.  
  454. local returnToLastPosition = function()
  455.  
  456. turnToMaxZ()
  457. moveToLastZ()
  458.  
  459. turnToLastX()
  460. moveToLastX()
  461.  
  462. moveToLastY()
  463.  
  464. turnToLastDirection()
  465.  
  466. end
  467.  
  468. local returnToChest = function()
  469.  
  470. lastKnownDirectionFacing = currentDirectionFacing
  471. lastKnownXValue = currentXValue
  472. lastKnownYValue = currentYValue
  473. lastKnownZValue = currentZValue
  474.  
  475. moveToMaxY()
  476.  
  477. turnToCenterX()
  478. moveToCenterX()
  479.  
  480. turnToMinZ()
  481. moveToMinZ()
  482.  
  483. emptyInventory()
  484.  
  485. end
  486.  
  487.  
  488. --------------------------------------------
  489.  
  490.  
  491. ----------------Mine Function---------------
  492.  
  493. local mineStripForwardZ = function()
  494. turnToMaxZ()
  495. while currentZValue ~= maxZValue do
  496. if inventoryCheck() == true then
  497. returnToChest()
  498. returnToLastPosition()
  499. end
  500. dig()
  501. forwardZ()
  502. end
  503. end
  504.  
  505. local mineStripBackZ = function()
  506. turnToMinZ()
  507. while currentZValue ~= minZValue do
  508. if inventoryCheck() == true then
  509. returnToChest()
  510. returnToLastPosition()
  511. end
  512. dig()
  513. backZ()
  514. end
  515. end
  516.  
  517. local nextStrip = function()
  518. turnToMaxX()
  519. dig()
  520. forwardX()
  521. end
  522.  
  523. local nextLayer = function()
  524. local currentYValueClone = currentYValue
  525. if currentYValue == minYValue+1 then
  526. down(false,false,false)
  527. elseif currentYValue-2 <= minYValue then
  528. while currentYValue ~= minYValue+1 do
  529. down(false,false,true)
  530. end
  531. else
  532. while currentYValue ~= currentYValueClone-2 do
  533. down(false,true,true)
  534. end
  535. end
  536. end
  537.  
  538. --------------------------------------------
  539.  
  540.  
  541. ----------------Main Function---------------
  542.  
  543.  
  544. local mineLayerHandler = function()
  545.  
  546. save({length,height,width},"mineSize")
  547.  
  548. turnToMinX()
  549. moveToMinX()
  550.  
  551. while currentYValue ~= minYValue+1 do
  552. while currentXValue < maxXValue do
  553. mineStripForwardZ()
  554. nextStrip()
  555. mineStripBackZ()
  556. nextStrip()
  557. end
  558. if currentXValue == maxXValue then
  559. mineStripForwardZ()
  560. turtle.digUp()
  561. turtle.digDown()
  562. end
  563. returnToFirstCorner()
  564. nextLayer()
  565. end
  566. returnToChest()
  567. right()
  568. right()
  569. fs.delete("/data/mineSize")
  570. fs.delete("/data/currentLocation")
  571. end
  572.  
  573. -- turning to max z
  574. -- moving to max z
  575. -- turning to max x
  576. -- moving forward one
  577. -- turning to min z
  578. -- continuing mining
  579.  
  580. local mineLayerHandlerContinueFromLast = function()
  581. while currentYValue ~= minYValue+1 do
  582. if currentDirectionFacing == 2 then
  583. mineStripBackZ()
  584. nextStrip()
  585. end
  586. while currentXValue < maxXValue do
  587. mineStripForwardZ()
  588. nextStrip()
  589. mineStripBackZ()
  590. nextStrip()
  591. end
  592. if currentXValue == maxXValue then
  593. mineStripForwardZ()
  594. turtle.digUp()
  595. turtle.digDown()
  596. end
  597. returnToFirstCorner()
  598. nextLayer()
  599. end
  600. returnToChest()
  601. right()
  602. right()
  603. fs.delete("/data/mineSize")
  604. fs.delete("/data/currentLocation")
  605. end
  606.  
  607. if continueFromLast ~= true then
  608. locateInitialPosition()
  609. mineLayerHandler()
  610. else
  611. locateLastPosition()
  612. mineLayerHandlerContinueFromLast()
  613. end
  614.  
  615.  
  616.  
  617.  
  618. --------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement