Advertisement
bcash8

Turtle strip miner

Jan 28th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. --list of all ores or blocks we want to mine--
  2. local ores = {"minecraft:iron_ore",
  3. "minecraft:diamond_ore",
  4. "minecraft:coal_ore",
  5. "minecraft:gold_ore",
  6. "minecraft:emerald_ore",
  7. "minecraft:restone_ore"}
  8.  
  9. --stuff to throw away--
  10. local garbage = {"minecraft:dirt",
  11. "minecraft:cobblestone",
  12. "minecraft:gravel",
  13. "minecraft:sand",
  14. "minecraft:granite",
  15. "minecraft:andesite",
  16. "minecraft:diorite"}
  17.  
  18. --if below this number it will use coal to refuel--
  19. local refill = 1000
  20.  
  21. local tArgs = {...}
  22. local tunnelLength = 0
  23.  
  24. --set tunnels length--
  25. if tonumber(tArgs[1]) > 0 then
  26.      tunnelLength = tonumber(tArgs[1])
  27. else
  28.      tunnelLength = 50
  29. end
  30.  
  31. local fuelLevel = turtle.getFuelLevel()
  32. local fuelFormula = math.ceil((tunnelLength*4)+(tunnelLength*0.45)+100)
  33.  
  34. --test fuel level. If good ask if ready--
  35. if fuelLevel < fuelFormula then
  36.      error("[Fuel Required]: " .. fuelFormula - fuelLevel)
  37. end
  38.  
  39. --this function checks the block and gives info--
  40. function inspect(side)
  41.      local worked, data = false, {}
  42.      if not side then
  43.           worked, data = turtle.inspect()
  44.      elseif side == 1 then
  45.           worked, data = turtle.inspectUp()
  46.      elseif side == 2 then
  47.           worked, data = turtle.inspectDown()
  48.      end
  49.      
  50.      if worked then
  51.           for i=1,#ores do
  52.                if data.name == ores[i] then
  53.                     return data.name
  54.                 end
  55.           end
  56.           return false
  57.      end
  58. end
  59.  
  60. --all movement functions. Makes it look better--
  61.  
  62. function forward()
  63.      repeat turtle.dig() until turtle.forward()
  64.      return true
  65. end
  66.  
  67. function back()
  68.     if not turtle.back() then
  69.         turtle.turnRight()
  70.         turtle.turnRight()
  71.         repeat turtle.dig() until turtle.forward()
  72.         turtle.turnLeft()
  73.         turtle.turnLeft()
  74.     end
  75.     return true
  76. end
  77.  
  78. function up()
  79.      repeat turtle.digUp() until turtle.up()
  80.      return true
  81. end
  82.  
  83. function down()
  84.      repeat turtle.digDown() until turtle.down()
  85.      return true
  86. end
  87.      
  88. --this is what does all the looking for ores around the turtle. Recursive function--
  89.  
  90. function scan( nTry )
  91.  
  92.      if not nTry then
  93.           nTry = 1
  94.      elseif nTry > 180 then
  95.           return
  96.      end
  97.  
  98. --check to the right--
  99.      turtle.turnRight()
  100.      if inspect() then
  101.           forward()
  102.           scan(nTry+1)
  103.           back()
  104.      end
  105.      turtle.turnLeft()
  106.  
  107. --check to the left--
  108.      turtle.turnLeft()
  109.      if inspect() then
  110.          forward()
  111.          scan(nTry+1)
  112.          back()
  113.      end
  114.      turtle.turnRight()
  115.  
  116. --check in front--
  117.      if inspect() then
  118.           forward()
  119.           scan(nTry+1)
  120.           back()
  121.      end
  122.  
  123. --check up--
  124.      if inspect(1) then
  125.           up()
  126.           scan(nTry+1)
  127.           down()
  128.      end
  129.  
  130. --check down--
  131.      if inspect(2) then
  132.           down()
  133.           scan(nTry+1)
  134.           up()
  135.      end
  136. end
  137.  
  138. --cleans out and reorganizes the inventory--
  139. function purgeInv()
  140.      for i=1,16 do
  141.           if turtle.getItemCount(i) > 0 then
  142.               local data = turtle.getItemDetail(i)
  143.              
  144.                if turtle.getItemCount(i) < 64 then
  145.                     for j=i+1,16 do
  146.                       local  d = turtle.getItemDetail(j)
  147.                          if d and d.name == data.name then
  148.                               turtle.select(j)
  149.                               turtle.transferTo(i)
  150.                          end
  151.                     end
  152.                end
  153.                
  154.                for b=1,#garbage do
  155.                     if data.name == garbage[b] then
  156.                          turtle.select(i)
  157.                          turtle.drop()
  158.                          break
  159.                     elseif data.name == "minecraft:coal" then
  160.                          if turtle.getFuelLevel() < refill then
  161.                               turtle.refuel()
  162.                               break
  163.                          end
  164.                     end
  165.                end
  166.           end
  167.      end
  168. end
  169.  
  170. --this is the main function where all the other functions are called in order and makes it work--
  171.  
  172. function main()
  173.      print("TUNNEL LENGTH: " .. tunnelLength)
  174.      print("START [Y/N]")
  175.      while true do
  176.      
  177.          local _,c = os.pullEvent("char")
  178.          
  179.           if c:lower() == "y" then
  180.          
  181.                --start mining--
  182.                for t=1,tunnelLength do
  183.                     forward()
  184.                     scan()
  185.                     up()
  186.                     scan()
  187.                     down()
  188.                     purgeInv()
  189.                 end
  190.                
  191.                turtle.turnRight()
  192.                turtle.turnRight()
  193.                
  194.                --return trip--
  195.                for t=1, tunnelLength do
  196.                     forward()
  197.                end
  198.                return
  199.           else
  200.                error("You didn't press y")
  201.           end
  202.      end
  203. end
  204.  
  205.  
  206.  
  207. --call the main function to start it all--
  208. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement