Advertisement
Guest User

Untitled

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