Advertisement
Guest User

Untitled

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