fatboychummy

RecursiveDigBoi.lua

Mar 4th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.54 KB | None | 0 0
  1. ----------------------------------
  2. -- A simple miner
  3. -- Note that it does not use fuel
  4. -- Copyright 2019 Fatboychummy
  5. ----------------------------------
  6.  
  7. local tArgs = {...}
  8.  
  9.  
  10. --------------------
  11. --Arguments:
  12. --1: distance of overall stripmine
  13. --2: distance between each branch
  14. --3: how far each branch will dig sideways
  15. --4: nil
  16. -------------------
  17.  
  18. local ores = {
  19.   basic_block_core = {metas = {[7] = true,[8] = true}},
  20.   resource = {metas = {[1] = true,[2] = true,[3] = true,[4] = true}},
  21.   coal_ore = {metas = {[0] = true}},
  22.   redstone_ore = {metas = {[0] = true}},
  23.   iron_ore = {metas = {[0] = true}},
  24.   gold_ore = {metas = {[0] = true}},
  25.   lapis_ore = {metas = {[0] = true}},
  26.   diamond_ore = {metas = {[0] = true}},
  27.   emerald_ore = {metas = {[0] = true}},
  28.   ore = {metas = {[0] = true, [1] = true, [2] = true, [3] = true, [4] = true, [5] = true}},
  29.   ore_fluid = {metas = {[0] = true}},
  30.   quartz_ore = {metas = {[0] = true}},
  31.   ore_stonequartz = {metas = {[0] = true}},
  32.   blockmetal = {metas = {[0] = true}}
  33. }
  34.  
  35.  
  36. --[[
  37.   The purpose of the tort functions, is to ensure that the turtle
  38.   go forward/down/up/back, instead of just failing to move.
  39. ]]
  40. local tort = {
  41.   forward = function()
  42.     local i = 0
  43.     while not turtle.forward() do
  44.       turtle.attack()
  45.       turtle.dig()
  46.       i = i + 1
  47.       if i > 100 then
  48.         return false
  49.       end
  50.       os.sleep(0.1)
  51.     end
  52.     return true
  53.   end,
  54.   back = function()
  55.     local i = 0
  56.     while not turtle.back() do
  57.       turtle.turnRight()
  58.       turtle.turnRight()
  59.       turtle.attack()
  60.       turtle.dig()
  61.       turtle.turnRight()
  62.       turtle.turnRight()
  63.       i = i + 1
  64.       if i > 100 then
  65.         return false
  66.       end
  67.       os.sleep(0.1)
  68.     end
  69.     return true
  70.   end,
  71.   down = function()
  72.     local i = 0
  73.     while not turtle.down() do
  74.       turtle.attackDown()
  75.       turtle.digDown()
  76.       i = i + 1
  77.       if i > 100 then
  78.         return false
  79.       end
  80.       os.sleep(0.1)
  81.     end
  82.     return true
  83.   end,
  84.   up = function()
  85.     local i = 0
  86.     while not turtle.up() do
  87.       turtle.attackUp()
  88.       turtle.digUp()
  89.       i = i + 1
  90.       if i > 100 then
  91.         return false
  92.       end
  93.       os.sleep(0.1)
  94.     end
  95.     return true
  96.   end
  97. }
  98.  
  99.  
  100.  
  101. --Remove extra "minecraft:" from strings
  102. local function rExtra(str)
  103.   return str:match(":(.+)")
  104. end
  105.  
  106. --Check if a block is an ore.
  107. local function isOre(block)
  108.   if type(block) ~= "table" then return false end
  109.   local nm = rExtra(block.name) --Remove extra stuff, check if the block is in "ores"
  110.   if ores[nm] then
  111.     if ores[nm]["metas"] then --if it's indexed by metadata (fastest), then go here
  112.       if ores[nm].metas[block.metadata] then --if the metadata is there, then it's an ore.
  113.         return true
  114.       end
  115.     end
  116.   end
  117.   return false
  118. end
  119.  
  120.  
  121.  
  122.  
  123. --Recursively mine out veins of ores
  124. local function mineStuff(side)
  125.   print(side and "ore " .. tostring(side) or "ore forward")
  126.   --Check if a block should be mined, and if it should, do it.
  127.   local function check(side)
  128.     local fn = turtle.inspect
  129.     if side == "up" then
  130.       fn = turtle.inspectUp
  131.     elseif side == "down" then
  132.       fn = turtle.inspectDown
  133.     end
  134.     --Above:
  135.     --Inspect whichever side is entered.
  136.     if isOre(({fn()})[2]) then
  137.       mineStuff(side)
  138.     end
  139.     --Mine whatever side is detected, if it is an ore.
  140.   end
  141.  
  142.   local fn = turtle.dig
  143.   local fn2 = tort.forward
  144.   local fn3 = tort.back
  145.   if side == "up" then
  146.     fn = turtle.digUp
  147.     fn2 = tort.up
  148.     fn3 = tort.down
  149.   elseif side == "down" then
  150.     fn = turtle.digDown
  151.     fn2 = tort.down
  152.     fn3 = tort.up
  153.   end
  154.   --  Above: check what side the ore was detected on, and swap which functions to
  155.   --run depending on the situation.
  156.  
  157.   fn()
  158.   fn2()
  159.   for i = 1,4 do check() turtle.turnRight() end
  160.   check("up")
  161.   check("down")
  162.   fn3()
  163.   --[[Above:
  164.     at it's most basic:
  165.       destroy ore
  166.       move into space ore was at
  167.       search if there's more ore around it
  168.       go back to where we were before.
  169.   ]]
  170.   os.sleep()
  171. end
  172.  
  173. --Dig stripmine
  174. local function dig(dist)
  175.   assert(type(dist) == "number" and dist >= 1 and dist % 1 == 0,"Expected number, greater than 0, divisible by 1")
  176.   for i = 1,dist do
  177.     local ok,dat = turtle.inspect()
  178.     local ok2,dat2 = turtle.inspectUp()
  179.     local ok3,dat3 = turtle.inspectDown()
  180.     --Detect stuff
  181.     if ok then
  182.       if isOre(dat) then
  183.         mineStuff()
  184.       end
  185.     end
  186.     if ok2 then
  187.       if isOre(dat2) then
  188.         mineStuff("up")
  189.       end
  190.     end
  191.     if ok3 then
  192.       if isOre(dat3) then
  193.         mineStuff("down")
  194.       end
  195.     end
  196.     --Above: if there is ore in front, above, or below, dig it
  197.     turtle.dig()
  198.     turtle.digUp()
  199.     --Dig then dig up so as to make a 2x1 space to walk down
  200.  
  201.     --[[check2:
  202.       basic:
  203.         look at block to left, if ore, dig
  204.         look at block to right, if ore, dig
  205.         look at blocks above/below, if ore, dig
  206.         go up one space, repeat the above once
  207.         go back down, go forwards.
  208.     ]]
  209.     local function check2()
  210.       turtle.turnLeft()
  211.       if isOre(({turtle.inspect()})[2]) then
  212.         mineStuff()
  213.       end
  214.       turtle.turnLeft()
  215.       turtle.turnLeft()
  216.       if isOre(({turtle.inspect()})[2]) then
  217.         mineStuff()
  218.       end
  219.       turtle.turnLeft()
  220.       if isOre(({turtle.inspectUp()})[2]) then
  221.         mineStuff("up")
  222.       end
  223.       if isOre(({turtle.inspectDown()})[2]) then
  224.         mineStuff("down")
  225.       end
  226.     end
  227.     check2()
  228.     tort.up()
  229.     check2()
  230.     tort.down()
  231.     tort.forward()
  232.   end
  233. end
  234.  
  235. local mxDist = tonumber(tArgs[1])
  236. local branchDist = tonumber(tArgs[2])
  237. local branchMxDist = tonumber(tArgs[3])
  238. local Nil = tArgs[4]
  239.  
  240. assert(type(mxDist) == "number","Bad argument #1: Expected number, could not convert.")
  241. assert(type(branchDist) == "number","Bad argument #2: Expected number, could not convert.")
  242. assert(type(branchMxDist) == "number","Bad argument #3: Expected number, could not convert.")
  243. assert(type(Nil) == "nil","Bad argument #4: Expected nil, but got something.  Why do you do this to me?")
  244.  
  245. local branches = mxDist / branchDist
  246.  
  247. dig(mxDist)
  248. turtle.turnLeft()
  249. turtle.turnLeft()
  250. for i = 1,branches do
  251.   turtle.turnRight()
  252.   dig(branchMxDist)
  253.   turtle.turnRight()
  254.   turtle.turnRight()
  255.   for o = 1,branchMxDist do
  256.     tort.forward()
  257.   end
  258.   dig(branchMxDist)
  259.   turtle.turnRight()
  260.   turtle.turnRight()
  261.   for o = 1,branchMxDist do
  262.     tort.forward()
  263.   end
  264.   turtle.turnLeft()
  265.   for o = 1,branchDist do
  266.     tort.forward()
  267.   end
  268. end
Add Comment
Please, Sign In to add comment