Advertisement
Guest User

Untitled

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