Advertisement
bcash8

Turtle Strip Miner 2.0

Mar 3rd, 2021 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.47 KB | None | 0 0
  1. --list of all ores or blocks we want to mine--
  2. local ores = {
  3. "minecraft:iron_ore",
  4. "minecraft:diamond_ore",
  5. "minecraft:coal_ore",
  6. "minecraft:gold_ore",
  7. "minecraft:emerald_ore",
  8. "minecraft:redstone_ore",
  9. "minecraft:lapis_lazuli_ore",
  10. "create:zinc_ore",
  11. "mekanism:tin_ore",
  12. "mekanism:copper_ore",
  13. "mekansim:lead_ore",
  14. "mekanism:osmium_ore"
  15. }
  16.  
  17. --stuff to throw away--
  18. local garbage = {
  19. "minecraft:dirt",
  20. "minecraft:cobblestone",
  21. "minecraft:gravel",
  22. "minecraft:sand",
  23. "minecraft:granite",
  24. "minecraft:diorite",
  25. "minecraft:cobbled_deepslate"
  26. }
  27. --if below this number it will use coal to refuel--
  28. local refill = 2000
  29. local tArgs = {...}
  30. local tunnelLength = 0
  31. local numTunnels = 0
  32. local pos = {0,0,0,1}
  33. local tunnelFormula = 0
  34.  
  35. --set tunnels length--
  36. if tArgs[2] then
  37.     tunnelLength = tonumber(tArgs[2])
  38.     numTunnels = tonumber(tArgs[1])
  39. else
  40.     error("[USAGE]: <number of tunnels> <length of tunnels>")
  41. end
  42.  
  43. local fuelLevel = turtle.getFuelLevel()
  44.  
  45. local fuelFormula = math.ceil((tunnelLength*4)+(tunnelLength*0.45)*numTunnels)+150
  46. --test fuel level. If good ask if ready--
  47. if fuelLevel < fuelFormula then
  48.     error("[Fuel Required]: " .. fuelFormula - fuelLevel)
  49. end
  50.  
  51. --this function checks the block and gives info--
  52. function inspect(side)
  53.     local worked, data = false,{}
  54.     if not side then
  55.         worked, data = turtle.inspect()
  56.     elseif side == 1 then
  57.         worked, data = turtle.inspectUp()
  58.     elseif side == 2 then
  59.         worked, data = turtle.inspectDown()
  60.     end
  61.  
  62.     if worked then
  63.         for i=1,#ores do
  64.             if data.name == ores[i] then
  65.                 return data.name
  66.             end
  67.         end
  68.  
  69.         if oreName(data.name) then
  70.             return data.name
  71.         end
  72.  
  73.         return false
  74.     end
  75. end
  76.  
  77. --all movement functions. Makes it look better--
  78. function right()
  79.     turtle.turnRight()
  80.     pos[4] = pos[4] + 1
  81.     if pos[4] >= 5 then
  82.        pos[4] = 1
  83.     end
  84. end
  85.  
  86. function left()
  87.     turtle.turnLeft()
  88.     pos[4] = pos[4] - 1
  89.     if pos[4] <= 0 then
  90.        pos[4] = 4
  91.     end
  92. end
  93.  
  94. function forward()
  95.     repeat turtle.dig() until turtle.forward()
  96.     if pos[4] == 1 then
  97.         pos[1] = pos[1] + 1
  98.     elseif pos[4] == 2 then
  99.         pos[3] = pos[3] + 1
  100.     elseif pos[4] == 3 then
  101.         pos[1] = pos[1] - 1
  102.     else
  103.         pos[3] = pos[3] - 1
  104.     end
  105.     return true
  106. end
  107.  
  108. function back()
  109.     if not turtle.back() then
  110.        right()
  111.        right()
  112.        forward()
  113.        left()
  114.        left()
  115.     else
  116.        if pos[4] == 1 then
  117.            pos[1] = pos[1] - 1
  118.        elseif pos[4] == 2 then
  119.            pos[3] = pos[3] - 1
  120.        elseif pos[4] == 3 then
  121.            pos[1] = pos[1] + 1
  122.        else
  123.            pos[3] = pos[3] + 1
  124.        end
  125.     end
  126.     return true
  127. end
  128.  
  129. function up()
  130.     repeat turtle.digUp() until turtle.up()
  131.     pos[2] = pos[2] + 1
  132.     return true
  133. end
  134.  
  135. function down()
  136.     repeat turtle.digDown() until turtle.down()
  137.     pos[2] = pos[2] - 1
  138.     return true
  139. end
  140.  
  141. function setDir(d)
  142.     while pos[4] > d do
  143.        left()
  144.     end
  145.     while pos[4] < d do
  146.         right()
  147.     end
  148. end
  149.  
  150. function goto(tbl)
  151.     while pos[2] > tbl[2] do
  152.         down()
  153.     end
  154.     while pos[2] < tbl[2] do
  155.         up()
  156.     end
  157.     if pos[3] > tbl[3] then
  158.         setDir(4)
  159.         while pos[3] > tbl[3] do
  160.            forward()
  161.         end
  162.     end
  163.     if pos[3] < tbl[3] then
  164.         setDir(2)
  165.         while pos[3] < tbl[3] do
  166.             forward()
  167.         end
  168.     end
  169.     if pos[1] > tbl[1] then
  170.         setDir(3)
  171.         while pos[1] > tbl[1] do
  172.             forward()
  173.         end
  174.     end
  175.     if pos[1] < tbl[1] then
  176.          setDir(1)
  177.          while pos[1] < tbl[1] do
  178.              forward()
  179.          end
  180.     end
  181.     setDir(tbl[4])
  182. end
  183.  
  184. --this is what does all the looking for ores around the turtle. Recursive function--
  185. function scan( nTry )
  186.     if not nTry then
  187.         nTry = 1
  188.     elseif nTry > 180 then
  189.         return
  190.     end
  191.  
  192.     --check to the right--
  193.     right()
  194.     if inspect() then
  195.         forward()
  196.         scan(nTry+1)
  197.         back()
  198.     end
  199.     left()
  200.  
  201.     --check to the left--
  202.     left()
  203.     if inspect() then
  204.         forward()
  205.         scan(nTry+1)
  206.         back()
  207.     end
  208.     right()
  209.  
  210.     --check in front--
  211.     if inspect() then
  212.         forward()
  213.         scan(nTry+1)
  214.         back()
  215.     end
  216.  
  217.     --check up--
  218.     if inspect(1) then
  219.         up()
  220.         scan(nTry+1)
  221.         down()
  222.     end
  223.  
  224.     --check down--
  225.     if inspect(2) then
  226.         down()
  227.         scan(nTry+1)
  228.         up()
  229.     end
  230. end
  231.  
  232. function oreName(name)
  233.     if string.find(name, "ore") then
  234.         return true
  235.     end
  236.     return false
  237. end
  238.  
  239. function dumpItems()
  240.     purgeInv()
  241.     for i=1,16 do
  242.         turtle.select(i)
  243.         turtle.drop()
  244.     end
  245. end
  246.  
  247. --cleans out and reorganizes the inventory--
  248. function purgeInv()
  249.     for i=1,16 do
  250.         if turtle.getItemCount(i) > 0 then
  251.             local data = turtle.getItemDetail(i)
  252.             if turtle.getItemCount(i) < 64 then
  253.                 for j=i+1,16 do
  254.                     local d = turtle.getItemDetail(j)
  255.                     if d and d.name == data.name then
  256.                         turtle.select(j)
  257.                         turtle.transferTo(i)
  258.                     end
  259.                 end
  260.             end
  261.             for b=1,#garbage do
  262.                 if data.name == garbage[b] then
  263.                     turtle.select(i)
  264.                     turtle.drop()
  265.                     break
  266.                 elseif data.name == "minecraft:coal" then
  267.                     if turtle.getFuelLevel() < refill then
  268.                         turtle.select(i)
  269.                         turtle.refuel()
  270.                         break
  271.                     end
  272.                 end
  273.             end
  274.         end
  275.     end
  276.     turtle.select(1)
  277. end
  278.  
  279. function moveForward()
  280.     forward()
  281.     scan()
  282.     up()
  283.     scan()
  284.     down()
  285. end
  286.  
  287. --this is the main function where all the other functions are called in order and makes it work--
  288. function main()
  289.     print("[MINING] ".. numTunnels .. " x ".. tunnelLength)
  290.     print("START [Y/N]")
  291.     while true do
  292.         local _,c = os.pullEvent("char")
  293.         if c:lower() == "y" then
  294.             if numTunnels > 2 then
  295.                 tunnelFormula = math.ceil((numTunnels-2)/2)
  296.             else
  297.                 tunnelFormula = 0
  298.             end
  299.            
  300.             --start mining--
  301.             for b=1, tunnelFormula*4 do
  302.                 moveForward()
  303.                 if b % 3 == 0 then
  304.                     purgeInv()
  305.                 end
  306.             end
  307.             goto({0,0,0,3})
  308.             dumpItems()
  309.          
  310.             local moves = 0
  311.             local tmpDir = nil
  312.             for i=1,numTunnels do
  313.                 if i > 2 then
  314.                     moves = math.ceil((i-2)/2)*4
  315.                 else
  316.                     moves = 0
  317.                 end
  318.  
  319.                 if i%2 == 0 then
  320.                     tmpDir = 2
  321.                 else
  322.                     tmpDir = 4
  323.                 end
  324.                 goto({moves,0,0,tmpDir})
  325.  
  326.                 for t=1,tunnelLength do
  327.                     moveForward()
  328.                     if t % 5 == 0 then
  329.                         purgeInv()
  330.                     end
  331.                 end
  332.                 goto({0,0,0,3})
  333.                 dumpItems()
  334.             end
  335.             return
  336.         else
  337.             error("You didn't press y")
  338.         end
  339.     end
  340. end
  341.  
  342. --call the main function to start it all--
  343. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement