Advertisement
Guest User

Untitled

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