Advertisement
jille_Jr

CC: Mining program (turtle) using inspect

Oct 21st, 2017 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. --(( Settings ))--
  2.  
  3. local fuelcap = 120
  4. local shaftoffset = 5
  5.  
  6. local junk = {
  7.     "minecraft:stone",
  8.     "minecraft:cobblestone",
  9.     "minecraft:dirt",
  10.     "minecraft:grass",
  11.     "minecraft:gravel",
  12.     --"minecraft:sand",
  13.     "chisel:marble1",
  14.     "chisel:marble2",
  15.     "chisel:limestone1",
  16.     "chisel:limestone2",
  17.     "chisel:basalt1",
  18.     "chisel:basalt2",
  19. }
  20.  
  21. local fuel = {
  22.     "minecraft:coal",
  23.     "minecraft:charcoal",
  24.     "minecraft:planks",
  25.     "minecraft:wood",
  26. }
  27.  
  28. --(( Variables ))--
  29.  
  30. local depth = 0
  31.  
  32. --(( Functions ))--
  33.  
  34.  
  35.  
  36. local function selectfromlist(list)
  37.     for slot=1,16 do
  38.         local data = turtle.getItemDetail(slot)
  39.  
  40.         if data then
  41.             for _,block in ipairs(list) do
  42.                 if block == data.name then
  43.                     turtle.select(slot)
  44.                     return true
  45.                 end
  46.             end
  47.         end
  48.     end
  49.     return false
  50. end
  51.  
  52. local function selectfuel()
  53.     return selectfromlist(fuel)
  54. end
  55.  
  56. local function selectjunk()
  57.     return selectfromlist(junk)
  58. end
  59.  
  60. -- side = "", "up", or "down"
  61. local function isvaluable()
  62.     local succ,data = turtle.inspect()
  63.  
  64.     if not succ then
  65.         return false
  66.     end
  67.  
  68.     for _,block in ipairs(junk) do
  69.         if data.name == block then
  70.             return false
  71.         end
  72.     end
  73.  
  74.     return true
  75. end
  76.  
  77. local function refuel()
  78.     local fuel = turtle.getFuelLevel()
  79.     while fuel < fuelcap do
  80.         print("Searching for fuel...")
  81.         if selectfuel() and turtle.refuel(1) then
  82.             return true
  83.         end
  84.         print("No fuel found. Press SPACE to search again!")
  85.         local key
  86.         repeat _,key=os.pullEvent("key")
  87.         until key==keys.space
  88.     end
  89. end
  90.  
  91. local function dig(dir)
  92.     if not dir or dir == "forward" then
  93.         dir = ""
  94.     end
  95.     dir = dir:sub(1,1):upper() .. dir:sub(2):lower()
  96.     turtle.select(1)
  97.     if not turtle["detect"..dir]() then return true end
  98.     while not turtle["dig"..dir]() do
  99.         if not turtle["attack"..dir]() then
  100.             return false
  101.         end
  102.     end
  103.     return true
  104. end
  105.  
  106. local function move(dir)
  107.     refuel()
  108.     while not turtle[dir]() do
  109.         if dig(dir) == false then
  110.             return false
  111.         end
  112.     end
  113.     return true
  114. end
  115.  
  116. local function down()
  117.     if move("down") then
  118.         depth = depth + 1
  119.         return true
  120.     end
  121.     return false
  122. end
  123.  
  124. local function up()
  125.     if move("up") then
  126.         depth = depth - 1
  127.         return true
  128.     end
  129.     return false
  130. end
  131.  
  132. local function forward()
  133.     return move("forward")
  134. end
  135.  
  136. local function checksides()
  137.     local turns = {turtle.turnRight,turtle.turnLeft}
  138.     local turn = turns[math.random(1,#turns)]
  139.     for count = 1,4 do
  140.         if isvaluable() then
  141.             dig()
  142.         end
  143.         turn()
  144.     end
  145. end
  146.  
  147. local function mine()
  148.     -- go down little first
  149.     repeat until down()
  150.     repeat until down()
  151.     if selectjunk() then
  152.         turtle.placeUp()
  153.     end
  154.  
  155.     -- down to bottom
  156.     while true do
  157.         checksides()
  158.         if not down() then
  159.             print("Bedrock after " .. depth .. " blocks!")
  160.             break
  161.         end
  162.     end
  163.  
  164.     -- up to miss bedrock noise
  165.     for i=1,5 do
  166.         repeat until up()
  167.     end
  168.  
  169.     -- move to next shaft
  170.     for i=1,shaftoffset do
  171.         repeat until forward()
  172.     end
  173.  
  174.     -- up to surface
  175.     while depth > 0 do
  176.         checksides()
  177.         repeat until up()
  178.     end
  179.  
  180.     -- fill hole
  181.     if selectjunk() then
  182.         turtle.placeDown()
  183.     end
  184. end
  185.  
  186. local function formatTime(sec)
  187.     if sec > 60 then
  188.         return math.floor(sec/60).." min "..math.floor(sec%60).." sec"
  189.     end
  190.     return math.floor(sec) .. " sec"
  191. end
  192.  
  193. print("Commencing mining operation!")
  194. local start = os.clock()
  195.  
  196. mine()
  197.  
  198. local took = os.clock() - start
  199. print("Operation complete! Took " .. formatTime(took))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement