Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.91 KB | None | 0 0
  1. --Created by makeo. All rights reserved.
  2.  
  3. --DO NOT EDIT
  4. local FUEL_SLOT = 1
  5. local CHEST_FUEL_SLOT = 1
  6. local ENDER_CHEST_SLOT = 2
  7. local DIG_SLOT = 3
  8.  
  9. --save movments
  10. function move(move, detect, dig, attack)
  11. if needsPitstop() then
  12. doPitstop()
  13. end
  14.  
  15. local first = true
  16. while not move() do
  17. if detect() then
  18. turtle.select(DIG_SLOT)
  19. dig()
  20. else
  21. attack()
  22. end
  23.  
  24. if not first then
  25. sleep(0.5)
  26. end
  27. first = false
  28. end
  29. end
  30.  
  31. function moveForward()
  32. move(turtle.forward, turtle.detect, turtle.dig, turtle.attack)
  33. end
  34.  
  35. function moveBack()
  36. if not turtle.back() then
  37. turn180()
  38. moveForward()
  39. turn180()
  40. end
  41. end
  42.  
  43. function moveUp()
  44. move(turtle.up, turtle.detectUp, turtle.digUp, turtle.attackUp)
  45. end
  46.  
  47. function moveDown()
  48. move(turtle.down, turtle.detectDown, turtle.digDown, turtle.attackDown)
  49. end
  50.  
  51. function turn180()
  52. turtle.turnLeft()
  53. turtle.turnLeft()
  54. end
  55.  
  56. --pitstop
  57. function doPitstop()
  58. if turtle.getItemDetail(ENDER_CHEST_SLOT) == nil then
  59. if peripheral.getType("bottom") ~= "ender_chest" then
  60. error("No ender chest found.")
  61. end
  62. else
  63. if turtle.detectDown() then
  64. turtle.select(DIG_SLOT)
  65. turtle.digDown()
  66. end
  67.  
  68. turtle.select(ENDER_CHEST_SLOT)
  69. turtle.placeDown()
  70. sleep(0.5)
  71. end
  72.  
  73. if (not hasSpace()) or getMovingHome() then
  74. for i = 3, 16, 1 do
  75. while turtle.getItemCount(i) > 0 do
  76. turtle.select(i)
  77. turtle.drop()
  78. sleep(0.5)
  79.  
  80. if not moved then
  81. sleep(2)
  82. end
  83. end
  84. end
  85. end
  86.  
  87. turtle.select(ENDER_CHEST_SLOT)
  88. turtle.digDown()
  89. end
  90.  
  91. function needsPitstop()
  92. return (not hasSpace()) or turtle.getFuelLevel() < 5
  93. end
  94.  
  95. function hasSpace()
  96. local count = 0
  97. for i = 3, 16, 1 do
  98. if turtle.getItemCount(i) == 0 then
  99. count = count + 1
  100. end
  101. end
  102. return count > 1
  103. end
  104.  
  105. function readNumber(minVal)
  106. local num = -1
  107. repeat
  108. if num ~= -1 then
  109. print("Text is not a number.")
  110. end
  111. num = tonumber(read())
  112.  
  113. if num ~= nil and num < minVal then
  114. print("Number is out of bound.")
  115. num = nil
  116. end
  117.  
  118. until num ~= nil
  119. return num
  120. end
  121.  
  122. --ore stuff
  123. function isOre()
  124. local success, data = turtle.inspect()
  125. return checkOre(success, data)
  126. end
  127.  
  128. function isOreUp()
  129. local success, data = turtle.inspectUp()
  130. return checkOre(success, data)
  131. end
  132.  
  133. function isOreDown()
  134. local success, data = turtle.inspectDown()
  135. return checkOre(success, data)
  136. end
  137.  
  138. function checkOre(success, data)
  139. if success then
  140. local name = data["name"]
  141. if name ~= nil then
  142. --Alternative name bypass
  143. if name == "Forestry:resources" or name == "TConstruct:SearedBrick"
  144. or name == "denseores:block0" or name == "rftools:dimensionalShardBlock"
  145. or name == "harvestcraft:salt" then
  146. return true
  147. end
  148.  
  149. local index = string.find(name, ":")
  150. if index ~= nil then
  151. local part = string.lower(string.sub(name, index))
  152. local isOre = string.find(part, "ore")
  153. return isOre ~= nil
  154. end
  155. end
  156. end
  157. return false
  158. end
  159.  
  160. --vein stuff
  161. function mineVein()
  162. while true do
  163. if isOre() then
  164. veinMoveForward()
  165. elseif isOreUp() then
  166. veinMoveUp()
  167. elseif isOreDown() then
  168. veinMoveDown()
  169. else
  170. local success = false
  171. for i = 1, 3, 1 do
  172. veinMoveLeft()
  173. if isOre() then
  174. veinMoveForward()
  175. success = true
  176. break
  177. end
  178. end
  179.  
  180. if not success then
  181. if not popVeinMovment() then
  182. return
  183. end
  184. end
  185. end
  186. sleep(0.5)
  187. end
  188. end
  189.  
  190. function veinMoveForward()
  191. moveForward()
  192. pushToVeinStack(1)
  193. end
  194.  
  195. function veinMoveUp()
  196. moveUp()
  197. pushToVeinStack(2)
  198. end
  199.  
  200. function veinMoveDown()
  201. moveDown()
  202. pushToVeinStack(3)
  203. end
  204.  
  205. function veinMoveLeft()
  206. turtle.turnLeft()
  207. pushToVeinStack(4)
  208. end
  209.  
  210. function veinMoveRight()
  211. turtle.turnRight()
  212. pushToVeinStack(5)
  213. end
  214.  
  215. function veinMoveBack()
  216. moveBack()
  217. pushToVeinStack(6)
  218. end
  219.  
  220. function popVeinMovment()
  221. local direction = getFromVeinStack()
  222.  
  223. if direction == nil then
  224. return false
  225. end
  226.  
  227. if direction == 1 then
  228. moveBack()
  229. elseif direction == 2 then
  230. moveDown()
  231. elseif direction == 3 then
  232. moveUp()
  233. elseif direction == 4 then
  234. turtle.turnRight()
  235. removeLastVeinStack()
  236. return popVeinMovment()
  237. elseif direction == 5 then
  238. turtle.turnLeft()
  239. removeLastVeinStack()
  240. return popVeinMovment()
  241. elseif direction == 6 then
  242. moveForward()
  243. end
  244.  
  245. removeLastVeinStack()
  246.  
  247. return true;
  248. end
  249.  
  250. function pushToVeinStack(direction)
  251. local stack = getVeinStack()
  252.  
  253. if stack == nil then
  254. stack = {}
  255. end
  256.  
  257. stack[#stack + 1] = direction
  258.  
  259. stack = optimizeVeinStack(stack)
  260. saveVeinStack(stack, #stack)
  261. end
  262.  
  263. function getFromVeinStack()
  264. local data = getVeinStack()
  265.  
  266. if data ~= nil then
  267. return data[#data]
  268. end
  269. return nil
  270. end
  271.  
  272. function optimizeVeinStack(data)
  273. local lastAction = 0
  274. local actionCount = 0
  275.  
  276. local i = 1
  277. while i <= #data do
  278. --turn right and then left is somewhat useless
  279. if (data[i] == 4 and lastAction == 5) or
  280. (data[i] == 5 and lastAction == 4) then
  281. data = moveArrayContent(data, i + 1, 2)
  282. if #data < 1 then
  283. break
  284. end
  285.  
  286. i = 1
  287. lastAction = 0
  288. actionCount = 0
  289. end
  290.  
  291. if data[i] ~= lastAction then
  292. lastAction = 0
  293. actionCount = 0
  294. end
  295.  
  296. if data[i] == 4 then
  297. lastAction = 4
  298. actionCount = actionCount + 1
  299. elseif data[i] == 5 then
  300. lastAction = 5
  301. actionCount = actionCount + 1
  302. end
  303.  
  304. if actionCount == 3 then
  305. local newAction = 4
  306. if lastAction == 4 then
  307. newAction = 5
  308. end
  309. data = moveArrayContent(data, i + 1, 2)
  310. data[i - 2] = newAction
  311.  
  312. i = 0
  313. lastAction = 0
  314. actionCount = 0
  315. end
  316. i = i + 1
  317. end
  318. return data
  319. end
  320.  
  321. function moveArrayContent(array, startIndex, amount)
  322. local size = #array
  323. for i = startIndex, size + amount, 1 do
  324. array[i - amount] = array[i]
  325. array[i] = nil
  326. end
  327. return array
  328. end
  329.  
  330. function removeLastVeinStack()
  331. local data = getVeinStack()
  332. saveVeinStack(data, #data - 1)
  333. end
  334.  
  335. function saveVeinStack(data, length)
  336. if data ~= nil then
  337. local dataLeng = #data
  338. for i = length + 1, dataLeng, 1 do
  339. data[i] = nil
  340. end
  341. end
  342.  
  343. if #data < 1 then
  344. data = nil
  345. end
  346. setVeinStack(data)
  347. end
  348.  
  349. function getVeinStack()
  350. return getVariable("veinStack")
  351. end
  352.  
  353. function setVeinStack(value)
  354. setVariable("veinStack", value)
  355. end
  356.  
  357. function getVariable(name)
  358. local vars = getVariables()
  359. if vars ~= nil then
  360. return vars[name]
  361. end
  362. return nil
  363. end
  364.  
  365. function setVariable(name, value)
  366. local vars = getVariables()
  367.  
  368. if vars == nil then
  369. vars = {}
  370. end
  371.  
  372. vars[name] = value
  373.  
  374. local handle = fs.open("Vars.dat", "w")
  375. handle.writeLine(textutils.serialize(vars))
  376. handle.close()
  377. end
  378.  
  379. function getVariables()
  380. local handle = fs.open("Vars.dat", "r")
  381.  
  382. if handle ~= nil then
  383. local raw = handle.readAll()
  384. handle.close()
  385.  
  386. if raw ~= nil then
  387. return textutils.unserialize(raw)
  388. end
  389. end
  390. return nil
  391. end
  392.  
  393. function getStripLength()
  394. return getVariable("stripLength")
  395. end
  396.  
  397. function setStripLength(value)
  398. setVariable("stripLength", value)
  399. end
  400.  
  401. function getStripPos()
  402. return getVariable("stripPos")
  403. end
  404.  
  405. function setStripPos(value)
  406. setVariable("stripPos", value)
  407. end
  408.  
  409. function getStripOnGround()
  410. return getVariable("stripGround")
  411. end
  412.  
  413. function setStripOnGround(value)
  414. return setVariable("stripGround", value)
  415. end
  416.  
  417. function getStripHasDug()
  418. return getVariable("stripHasDug")
  419. end
  420.  
  421. function setStripHasDug(value)
  422. return setVariable("stripHasDug", value)
  423. end
  424.  
  425. function getStripHasFinished()
  426. return getVariable("stripHasFinished")
  427. end
  428.  
  429. function setStripHasFinished(value)
  430. return setVariable("stripHasFinished", value)
  431. end
  432.  
  433. function digStrip(gotoStart)
  434. local length = getStripLength()
  435.  
  436. if not getStripHasFinished() then
  437. while getStripPos() <= length or (not getStripHasDug()) do
  438. mineVein()
  439.  
  440. if getStripHasDug() then
  441.  
  442. moveForward()
  443. setStripPos(getStripPos() + 1)
  444. setStripHasDug(false)
  445. else
  446. if getStripOnGround() then
  447. moveUp()
  448. setStripOnGround(false)
  449. else
  450. moveDown()
  451. setStripOnGround(true)
  452. end
  453. setStripHasDug(true)
  454. end
  455. end
  456. setStripHasFinished(true)
  457. end
  458.  
  459. mineVein()
  460.  
  461. if not getStripOnGround() then
  462. moveDown()
  463. setStripOnGround(true)
  464. mineVein()
  465. end
  466.  
  467. if gotoStart then
  468. while getStripPos() > 1 do
  469. moveBack()
  470. setStripPos(getStripPos() - 1)
  471. sleep(0.5)
  472. end
  473. end
  474. end
  475.  
  476. function clearStripParams()
  477. setStripLength(0)
  478. setStripPos(1)
  479. setStripOnGround(true)
  480. setStripHasDug(false)
  481. setStripHasFinished(false)
  482. end
  483.  
  484. function getIsPreparing()
  485. return getVariable("isPreparing")
  486. end
  487.  
  488. function setIsPreparing(value)
  489. return setVariable("isPreparing", value)
  490. end
  491.  
  492. function getSpacing()
  493. return getVariable("spacing")
  494. end
  495.  
  496. function setSpacing(value)
  497. return setVariable("spacing", value)
  498. end
  499.  
  500. function getDepth()
  501. return getVariable("depth")
  502. end
  503.  
  504. function setDepth(value)
  505. return setVariable("depth", value)
  506. end
  507.  
  508. function getStripCount()
  509. return getVariable("stripCount")
  510. end
  511.  
  512. function setStripCount(value)
  513. return setVariable("stripCount", value)
  514. end
  515.  
  516. function getRemainingStripCount()
  517. return getVariable("remainStripCount")
  518. end
  519.  
  520. function setRemainingStripCount(value)
  521. return setVariable("remainStripCount", value)
  522. end
  523.  
  524. function getMovingHome()
  525. return getVariable("movHome")
  526. end
  527.  
  528. function setMovingHome(value)
  529. return setVariable("movHome", value)
  530. end
  531.  
  532. function prepareNextStrip()
  533. if not getIsPreparing() then
  534. turtle.turnRight()
  535. setIsPreparing(true)
  536.  
  537. clearStripParams()
  538. end
  539.  
  540. setStripLength(getSpacing() + 1)
  541. digStrip(false)
  542.  
  543. clearStripParams()
  544.  
  545. turtle.turnLeft()
  546. setIsPreparing(false)
  547. setStripHasFinished(false)
  548. end
  549.  
  550. function goHome()
  551. if not getMovingHome() then
  552. turtle.turnLeft()
  553. setMovingHome(true)
  554. setStripPos(1)
  555. end
  556.  
  557. local length = ((getStripCount() - 1) * (getSpacing() + 1))
  558.  
  559. while getStripPos() <= length do
  560. moveForward()
  561. setStripPos(getStripPos() + 1)
  562. end
  563.  
  564. doPitstop()
  565.  
  566. turtle.turnRight()
  567.  
  568. fs.delete("Vars.dat")
  569.  
  570. print("All strips cleared.")
  571. error()
  572. end
  573.  
  574. function initVars()
  575. clearStripParams()
  576. setIsPreparing(false)
  577. setMovingHome(false)
  578.  
  579. print("Enter the spacing between the strips.")
  580. setSpacing(readNumber(0))
  581.  
  582. print("Enter the depth of each strip.")
  583. setDepth(readNumber(1))
  584.  
  585. print("Enter the amount of strips.")
  586. local stripCount = readNumber(1)
  587. setStripCount(stripCount)
  588. setRemainingStripCount(stripCount)
  589. end
  590.  
  591. if getSpacing() == nil then
  592. initVars()
  593. end
  594.  
  595. while true do
  596. if getMovingHome() then
  597. goHome()
  598. end
  599.  
  600. if getIsPreparing() then
  601. prepareNextStrip()
  602. elseif not getStripHasFinished() then
  603. setStripLength(getDepth())
  604. digStrip(true)
  605. clearStripParams()
  606.  
  607. local remStrips = getRemainingStripCount() - 1
  608. setRemainingStripCount(remStrips)
  609. if remStrips < 1 then
  610. goHome()
  611. end
  612.  
  613. prepareNextStrip()
  614. end
  615. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement