jironpaste

Turtle Miner minecraft computercraft

Apr 16th, 2020 (edited)
4,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.51 KB | None | 0 0
  1. -- https://i.imgur.com/WCia4xK.png (Basic setup)
  2. -- Auto refules during operations
  3. -- Auto storage and sorting (ores go in ore container, stones and misc goes in stone container)
  4. -- Auto "repairs damage done to environment"
  5. -- If desiered you can add other items to be filtered in the "detectDirt" method, ex included.
  6. -- Set and forget, easy to use.
  7.  
  8. -- to Do: make 2 commands, one for Mining and one for Filling,
  9. -- Currently executes mining operations. When mining has concluded, it will
  10. -- restock on fule and comence "repairing damage to the environment" by
  11. -- refilling the mined area with stone and other materials.
  12.  
  13. print("+-------------------------------------+")
  14. print("|    MinerTurtle 1.0, by Jiron       |")
  15. print("+-------------------------------------+")
  16. print("| Farm set-up: Place down 3 chests    |")
  17. print("| place the mining turtle on the      |")
  18. print("| chest to the left and facing chest. |")
  19. print("| Put fuel in the right chest.        |")
  20. print("| Overflow chest can be placed unless |")
  21. print("| you have no needed of it.           |")
  22. print("|  Put some fuel in the turtle and    |")
  23. print("|  lets define the size of the mine.  |")
  24. print("+-------------------------------------+")
  25. print("Press enter to continue..")
  26. getready = io.read()
  27. print("+-------------------------------------+")
  28. print("+----------------Setup----------------+")
  29. print("| O = overflow C = chest  T = turtle  |")
  30. print("|          ^ Mine Area  ^             |")
  31. print("|                O    T               |")
  32. print("|           C    C    C               |")
  33. print("|           L    M    R               |")
  34. print("| note: O is placed 1 block above T   |")
  35. print("|  Remember: Turtle should be facing  |")
  36. print("|  the chest.                         |")
  37. print("+-------------------------------------+")
  38. print("Press enter to continue..")
  39. print("https://i.imgur.com/WCia4xK.png")
  40. print("Shows the basic setup")
  41. print("The mine will be filled after if there")
  42. print("are materials in middel chest.")
  43. getsett = io.read()
  44. print("Width of the mine; in ODD numbers only!")
  45. re = io.read()
  46.  
  47. width = tonumber(re)
  48. print("Length of mine?")
  49. rel = io.read()
  50. print("Start height? check y=Height")
  51. sh = io.read()
  52. startHeight = tonumber(sh)
  53. length = tonumber(rel)
  54. print("Farm size set to " .. length .. "x" .. width .. "x" .. startHeight)
  55. --print("Cycle time? in minutes")
  56. --redir = io.read()
  57. --Farm
  58. --waitTime = tonumber(redir * 60)
  59. tRight = false
  60. tLeft = true
  61. start = true
  62. turtleDepth = 0
  63. done = false
  64. flip = false
  65. gotItem = false
  66. flipTurtle = false
  67. banked = false
  68.  
  69. function noFaithInUser(number)
  70.     if (number % 2 == 0) then
  71.         print("Error: Width is not odd in size!")
  72.         return true
  73.     end
  74.     start = false
  75.     return flase
  76. end
  77.  
  78.  
  79.  
  80. function turnLeft()
  81.     -- print("left")
  82.     block()
  83.     turtle.turnLeft()
  84.     block()
  85.     turtle.forward()
  86.     block()
  87.     turtle.turnLeft()
  88.     tLeft = true
  89.     tRight = false
  90.     block()
  91. end
  92. function turnLeftFill()
  93.     -- print("left")
  94.     turtle.place()
  95.     turtle.turnLeft()
  96.     turtle.dig()
  97.     stonePlaceDown()
  98.     turtle.forward()
  99.    
  100.     turtle.turnLeft()
  101.     turtle.dig()
  102.     tLeft = true
  103.     tRight = false
  104.     stonePlaceDown()
  105. end
  106.  
  107. function turnRight()
  108.     --print("Right")
  109.     block()
  110.     turtle.turnRight()
  111.     block()
  112.    
  113.     turtle.forward()
  114.     block()
  115.     turtle.turnRight()
  116.     tRight = true
  117.     tLeft = false
  118.     block()
  119. end
  120.  
  121. function turnRightFill()
  122.     --print("Right")
  123.     turtle.place()
  124.     turtle.turnRight()
  125.     turtle.dig()
  126.     stonePlaceDown()
  127.    
  128.     turtle.forward()
  129.    
  130.     turtle.turnRight()
  131.     turtle.dig()
  132.     tRight = true
  133.     tLeft = false
  134.     stonePlaceDown()
  135. end
  136.  
  137. function fallingBlock()
  138.     local success, data = turtle.inspect()
  139.     local fBlocks =
  140.         {
  141.             sand = "minecraft:sand",
  142.             gravel = "minecraft:gravel",
  143.             anvil = "minecraft:anvil",
  144.             dragon_egg = "minecraft:dragon_egg"
  145.         }
  146.     if data.name == fBlocks.sand or data.name == fBlocks.gravel or data.name == fBlocks.anvil or data.name == fBlocks.dragon_egg then
  147.         return true
  148.     else
  149.         return false
  150.     end
  151. end
  152.  
  153.  
  154. function block()
  155.    
  156.     while fallingBlock() == true do
  157.         turtle.dig()
  158.         sleep(1)
  159.     end
  160.    
  161.     if turtle.detect() == true then
  162.        
  163.         turtle.dig()
  164.         turtle.suck()
  165.         turtle.suckDown()
  166.    
  167.    
  168.    
  169.     end
  170.  
  171. end
  172. function detectStone(s)
  173.     local res = detectType(s, "stone")
  174.     local res2 = detectType(s, "red")
  175.     if res == "yes" and res2 == "no" then
  176.         return "yes";
  177.     else
  178.         return "no"
  179.     end
  180. end
  181. function detectDirt(s)
  182.     local res = detectType(s, "dirt")
  183.     local res2 = detectType(s, "gravel")
  184.     local res3 = detectType(s, "sand")
  185.     local res4 = detectType(s, "chisel")
  186.     local res5 = detectType(s, "quark")
  187.     --if you want to filter out use the blockname, above items are 'Minecraft:dirt', 'Minecraft:gravel' <- => itemname = gravel
  188.     --local res6 = detectType(s,"itemname")
  189.    
  190.     if res == "yes" or res2 == "yes" or res3 == "yes" or res4 == "yes" or res5 == "yes" then
  191.        
  192.         return "yes";
  193.     else
  194.         return "no"
  195.    
  196.     end
  197. end
  198.  
  199. function detectType(s, type)
  200.     res = string.find(s .. "", type)
  201.     if res ~= nil then
  202.         return "yes";
  203.     else
  204.         return "no"
  205.     end
  206. end
  207.  
  208.  
  209. function evenOrOdd(number)
  210.     if number % 2 == 0 then
  211.         return true
  212.     else
  213.         return false
  214.     end
  215. end
  216.  
  217.  
  218.  
  219. function digForward()
  220.    
  221.     while fallingBlock() == true do
  222.         turtle.dig()
  223.         sleep(1)
  224.     end
  225.     turtle.dig()
  226.     turtle.suck()
  227.     local t = turtle.forward()
  228.     while t == false do
  229.         turtle.dig()
  230.         turtle.suck()
  231.         t = turtle.forward()
  232.     end
  233. end
  234.  
  235.  
  236. function digDown()
  237.     turtle.digDown()
  238.     turtle.suckDown()
  239.     turtle.suck()
  240.     turtle.down()
  241.  
  242. end
  243.  
  244. function goDown(hs)
  245.     if hs > 1 then
  246.         turtle.digDown()
  247.         turtle.suckDown()
  248.         turtle.down()
  249.         hs = hs - 1
  250.         goDown(hs)
  251.     end
  252. end
  253.  
  254.  
  255. function move(length, first)-- MOVE
  256.     if length >= 0 then
  257.         digForward()
  258.        
  259.         length = length - 1
  260.         --print("move")
  261.         move(length, false)
  262.    
  263.    
  264.     end
  265. end
  266.  
  267.  
  268. function checkSlotForStone()
  269.     local suc, space = turtle.getItemSpace()
  270.     if suc then
  271.         local data = turtle.getItemDetail()
  272.         if data then
  273.             -- print(data.name)
  274.             local name = data.name
  275.             return detectStone(name)
  276.         end
  277.     else return "no"
  278.     end
  279. end
  280.  
  281.  
  282. function checkSlotForDirt()
  283.     local suc, space = turtle.getItemSpace()
  284.     if suc then
  285.         local data = turtle.getItemDetail()
  286.         if data then
  287.             -- print(data.name)
  288.             local name = data.name
  289.             return detectDirt(name)
  290.         end
  291.     else return "no"
  292.     end
  293. end
  294.  
  295.  
  296. function refuel(f)
  297.     for i = f, 16 do -- loop through the slots
  298.         turtle.select(i)-- change to the slot
  299.         if turtle.refuel(0) then -- if it's valid fuel
  300.             turtle.refuel()-- consume half the stack as fuel
  301.         end
  302.     end
  303.     turtle.select(1)
  304. end
  305.  
  306.  
  307. function refuelTurtle()
  308.     turtle.forward()
  309.    
  310.     turtle.turnLeft()
  311.     local suc, fuelLevel = turtle.getFuelLevel()
  312.     if turtle.getFuelLevel() ~= "unlimited" then
  313.        
  314.         for i = 1, 16 do -- loop through the slots
  315.             turtle.select(i)-- change to the slot
  316.             turtle.suck()
  317.        
  318.         end
  319.     end
  320.    
  321.     refuel(1)
  322.     storeUnusedFuel(1)
  323.     turtle.turnLeft()
  324.     turtle.forward()
  325.     turtle.forward()
  326.     turtle.turnLeft()
  327. end
  328.  
  329.  
  330.  
  331. function storeUnusedFuel(f)
  332.     for i = f, 16 do -- loop through the slots
  333.         turtle.select(i)-- change to the slot
  334.         if turtle.refuel(0) then
  335.             turtle.drop()
  336.         end
  337.     end
  338.     turtle.select(1)
  339. end
  340.  
  341. function storeOre()
  342.     for i = 1, 16 do -- loop through the slots
  343.         turtle.select(i)-- change to the slot
  344.         local validFuelSorce = turtle.refuel(0)
  345.         if checkSlotForStone() == "no" and checkSlotForDirt() == "no" and validFuelSorce == false then
  346.             turtle.drop()
  347.         end
  348.     end
  349.     turtle.select(1)
  350. end
  351. function storeCobbel()
  352.    
  353.     turtle.turnRight()
  354.     turtle.forward()
  355.     turtle.turnLeft()
  356.    
  357.     for i = 1, 16 do -- loop through the slots
  358.         turtle.select(i)-- change to the slot
  359.         local validFuelSorce = turtle.refuel(0)
  360.         local d1, d2 = false
  361.         if checkSlotForStone() ~= "no" and validFuelSorce == false then
  362.             d1 = turtle.drop()
  363.             if d1 ~= true then
  364.                 turtle.dropUp()
  365.             end
  366.        
  367.         end
  368.         if checkSlotForDirt() ~= "no" and validFuelSorce == false then
  369.             d2 = turtle.drop()
  370.             if d2 ~= true then
  371.                 turtle.dropUp()
  372.             end
  373.         end
  374.    
  375.     end
  376.     turtle.select(1)
  377.     turtle.turnRight()
  378.  
  379. end
  380.  
  381. function fillCobbel()
  382.    
  383.     turtle.turnRight()
  384.     turtle.forward()
  385.     turtle.turnLeft()
  386.     for i = 2, 16 do -- loop through the slots
  387.         turtle.select(i)-- change to the slot
  388.        
  389.         turtle.suck()
  390.    
  391.     end
  392.     turtle.select(2)
  393.     x = turtle.getItemCount()
  394.     if x == 0 then
  395.         print("OutOfMaterials")
  396.         turtleDepth = -1
  397.         banked = true
  398.     -- stop it!
  399.     end
  400.     turtle.turnRight()
  401.     gotItem = true
  402.  
  403.  
  404. end
  405.  
  406.  
  407.  
  408.  
  409. function stonePlaceDown()
  410.    
  411.     if gotItem then
  412.         if turtle.getItemCount() > 0 then
  413.             turtle.placeDown()
  414.         else
  415.             for i = 2, 16 do -- loop through the slots
  416.                 turtle.select(i)-- change to the slot
  417.                 item = turtle.getItemCount()
  418.                
  419.                 if item > 0 then
  420.                     gotItem = true
  421.                     turtle.transferTo(2)
  422.                     turtle.select(2)
  423.                     turtle.placeDown()
  424.                     break
  425.                 end
  426.                
  427.                 if i == 16 then
  428.                     gotItem = false
  429.                 end
  430.            
  431.            
  432.             end
  433.        
  434.        
  435.        
  436.         end
  437.     end
  438.  
  439.  
  440. end
  441.  
  442. function moveFill(length)
  443.     if length >= 0 then
  444.         turtle.dig()
  445.         stonePlaceDown()
  446.         local t = turtle.forward()
  447.         while t == false do
  448.             turtle.dig()
  449.             t = turtle.forward()
  450.         end
  451.        
  452.        
  453.         length = length - 1
  454.         --print("move")
  455.         moveFill(length)
  456.    
  457.    
  458.     end
  459. end
  460.  
  461. function minerStoreAndResupply(newWidth, miner, distance)
  462.     print("Store&Resupply progress..")
  463.     turtle.turnLeft()
  464.     goUp(turtleDepth + 1)
  465.     block()
  466.     move(distance, false)
  467.     turtle.turnLeft()
  468.     block()
  469.     move(length - 1, false)
  470.     if miner == true then
  471.         storeOre()
  472.         storeCobbel()
  473.     else
  474.         fillCobbel()
  475.     end
  476.     refuelTurtle()
  477.     turtle.select(2)
  478.     move(length - 1, false)
  479.     turtle.turnRight()
  480.     move(distance, false)
  481.     turtle.turnLeft()
  482.     goDown(turtleDepth + 1)
  483. end
  484.  
  485. function turtleFill()
  486.    
  487.     for newWidth = width, 1, -1 do
  488.        
  489.         if turtleDepth < 0 then
  490.             break
  491.         end
  492.        
  493.        
  494.         flip = not flip
  495.         if newWidth == width then
  496.             moveFill(length - 1)
  497.         else if newWidth < width and newWidth > 1 then
  498.             moveFill(length - 1)
  499.         else
  500.             moveFill(length - 1)-- set to -1 for mirror setup
  501.         end
  502.         end
  503.        
  504.         turtle.select(2)
  505.         --print(flip)
  506.         local countItem = turtle.getItemCount()
  507.         local number = 0
  508.         if evenOrOdd(startHeight) == false then
  509.             number = turtleDepth - 1
  510.         else
  511.             number = turtleDepth
  512.         end
  513.        
  514.        
  515.        
  516.        
  517.         if countItem < 1 and flip == true and gotItem == false then
  518.             if evenOrOdd(number) == false then
  519.                 minerStoreAndResupply(newWidth, false, (width - newWidth) - 1)
  520.             else
  521.                 minerStoreAndResupply(newWidth, false, (newWidth - 2))
  522.             end
  523.        
  524.        
  525.         end
  526.        
  527.         if turtle.getFuelLevel() < 6000 and flip == true then
  528.             if evenOrOdd(number) == false then
  529.                 minerStoreAndResupply(newWidth, false, (width - newWidth) - 1)
  530.             else
  531.                 minerStoreAndResupply(newWidth, false, (newWidth - 2))
  532.             end
  533.        
  534.         end
  535.        
  536.        
  537.        
  538.         if newWidth > 1 then
  539.             if tRight == false then
  540.                 turnRightFill()
  541.            
  542.             else
  543.                 turnLeftFill()
  544.            
  545.             end
  546.         end
  547.        
  548.         if newWidth == 1 then
  549.             return newWidth
  550.         end
  551.     end
  552.  
  553. end
  554.  
  555.  
  556.  
  557. function turtleRun()
  558.    
  559.     for newWidth = width, 1, -1 do
  560.         flipTurtle = not flipTurtle
  561.         -- print("Doin a run")
  562.         if newWidth == width then
  563.             move(length - 1, true)
  564.         else if newWidth < width and newWidth > 1 then
  565.             move(length - 1, false)
  566.         else
  567.             move(length - 1, false)-- set to -1 for mirror setup
  568.         end
  569.         end
  570.        
  571.         turtle.select(16)
  572.        
  573.        
  574.         local countItem = turtle.getItemCount()
  575.         local number = turtleDepth + 1
  576.        
  577.        
  578.        
  579.        
  580.         if countItem > 0 and flipTurtle == true then
  581.             if evenOrOdd(number) == false then
  582.                 minerStoreAndResupply(newWidth, true, (width - newWidth) - 1)
  583.             else
  584.                 minerStoreAndResupply(newWidth, true, (newWidth - 2))
  585.             end
  586.         end
  587.        
  588.        
  589.         if turtle.getFuelLevel() < 6000 and flipTurtle == true then
  590.             if evenOrOdd(number) == false then
  591.                 minerStoreAndResupply(newWidth, true, (width - newWidth) - 1)
  592.             else
  593.                 minerStoreAndResupply(newWidth, true, (newWidth - 2))
  594.             end
  595.        
  596.         end
  597.        
  598.        
  599.         turtle.select(1)
  600.        
  601.         if newWidth > 1 then
  602.             if tRight == false then
  603.                 turnRight()
  604.             else
  605.                 turnLeft()
  606.             end
  607.         end
  608.        
  609.         if newWidth == 1 then
  610.             return newWidth
  611.         end
  612.     end
  613.  
  614. end
  615.  
  616. function moveHome(length)
  617.     if length >= 0 then
  618.         turtle.forward()
  619.        
  620.         length = length - 1
  621.         moveHome(length)
  622.     end
  623. end
  624.  
  625. function returnToBegining()
  626.     turtle.turnLeft()
  627.     block()
  628.     move(width - 2)
  629.     turtle.turnLeft()
  630.     block()
  631.     move(length - 1)
  632. end
  633.  
  634. function continueMiningOdd(h)
  635.     block()
  636.     moveHome(length - 1)
  637.     turtle.turnRight()
  638.     block()
  639.     moveHome(width - 2)
  640.     turtle.turnRight()
  641.     backToMine(h - 1)
  642. end
  643.  
  644. function continueMiningEven(h)
  645.     backToMine(h - 1)
  646. end
  647.  
  648. function backToMine(h)
  649.     if h > 0 then
  650.        
  651.         turtle.down()
  652.         h = h - 1
  653.         backToMine(h)
  654.     else
  655.         digDown()
  656.     end
  657.  
  658.  
  659. end
  660.  
  661. function toSurface(h)
  662.     if h > 1 then
  663.         turtle.up()
  664.         h = h - 1
  665.         toSurface(h)
  666.     else
  667.         returnToBegining()
  668.         storeOre()
  669.         storeCobbel()
  670.         refuelTurtle()
  671.         if (turtleDepth < startHeight) then
  672.             if evenOrOdd(turtleDepth) then
  673.                 continueMiningEven(turtleDepth)
  674.             else
  675.                 continueMiningOdd(turtleDepth)
  676.             end
  677.         end
  678.     end
  679. end
  680.  
  681.  
  682. function goingBank(h)
  683.    
  684.     if h > 1 then
  685.         turtle.digUp()
  686.         turtle.up()
  687.         h = h - 1
  688.         goingBank(h)
  689.     else
  690.         storeOre()
  691.         storeCobbel()
  692.         refuelTurtle()
  693.         if (turtleDepth < startHeight) then
  694.             if evenOrOdd(turtleDepth) then
  695.                 continueMiningEven(turtleDepth)
  696.             else
  697.                 continueMiningOdd(turtleDepth)
  698.             end
  699.         end
  700.     end
  701.  
  702. end
  703.  
  704. function goUp(h)
  705.     if h > 1 then
  706.         turtle.digUp()
  707.         turtle.up()
  708.         h = h - 1
  709.         goUp(h)
  710.     end
  711. end
  712.  
  713.  
  714. function returnToBase()
  715.     turtle.turnLeft()
  716.     turtle.turnLeft()
  717.     if evenOrOdd(turtleDepth) then
  718.         goingBank(turtleDepth + 1)
  719.    
  720.     else if evenOrOdd(turtleDepth) == false then
  721.         toSurface(turtleDepth + 1)
  722.    
  723.    
  724.     end
  725.     end
  726.  
  727. end
  728.  
  729. function run()
  730.    
  731.    
  732.     local state = turtleRun()
  733.    
  734.    
  735.     if state == 1 and turtleDepth ~= (startHeight) then
  736.        
  737.         digDown()
  738.         turtleDepth = turtleDepth + 1
  739.         print(turtleDepth .. "down" .. startHeight)
  740.         -- flipTurtle = not flipTurtle
  741.         turtle.turnLeft()
  742.         turtle.turnLeft()
  743.     end
  744.  
  745. end
  746.  
  747.  
  748.  
  749. function runFill()
  750.    
  751.    
  752.    
  753.     local state2 = turtleFill()
  754.    
  755.     turtle.select(2)
  756.     if state2 == 1 then
  757.        
  758.         turtle.digUp()
  759.         turtle.up()
  760.         turtleDepth = turtleDepth - 1
  761.         print(turtleDepth .. "DF depth")
  762.         turtle.turnLeft()
  763.         turtle.turnLeft()
  764.    
  765.    
  766.     end
  767. end
  768.  
  769.  
  770. function fill()
  771.     turtle.turnLeft()
  772.     turtle.turnLeft()
  773.     fillCobbel()
  774.     refuelTurtle()
  775.     turtle.select(2)
  776.     backToMine(turtleDepth - 2)
  777. --turtleDepth = turtleDepth - 1
  778. end
  779.  
  780.  
  781.  
  782. while true do
  783.    
  784.     if start == true then
  785.         if noFaithInUser(width) == true then
  786.             break
  787.         end
  788.         startHeight = startHeight - 2
  789.         refuel(1)
  790.         storeCobbel()
  791.         refuelTurtle()
  792.     end
  793.    
  794.    
  795.     while turtleDepth <= startHeight do
  796.         if startHeight > turtleDepth then
  797.             run()
  798.         else
  799.             break
  800.         end
  801.    
  802.     end
  803.     if done == false then
  804.         returnToBase()
  805.         done = true
  806.     end
  807.    
  808.     done = true
  809.     local fillStart = true
  810.    
  811.     if fillStart == true then
  812.         fill()
  813.         turtle.select(2)
  814.         fillStart = false
  815.         turtleDepth = turtleDepth - 1
  816.     end
  817.     while turtleDepth >= -1 and done == true do
  818.        
  819.         if turtleDepth > -1 then
  820.            
  821.             runFill()
  822.            
  823.             if (turtleDepth == -1) then
  824.                 break
  825.             end
  826.        
  827.         else
  828.            
  829.             break
  830.         end
  831.    
  832.     end
  833.    
  834.     if banked == false and flip == true then
  835.         turtle.turnRight()
  836.         block()
  837.         move(width - 2)
  838.         turtle.turnLeft()
  839.         block()
  840.         move(length - 1)
  841.     end
  842.    
  843.     print("Hole dug! Hole frackein done.")
  844.     sleep(10)
  845.     break
  846. end
Add Comment
Please, Sign In to add comment