Advertisement
Guest User

Untitled

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