_Jayson

CC Miner

Mar 21st, 2022 (edited)
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | None | 0 0
  1. local options = {
  2.     startBlock = "minecraft:spruce_planks",
  3.     wingFreq = 4,
  4.     wingLength = 10,
  5.     wantedOre = {
  6.         "minecraft:coal_ore",
  7.         "minecraft:deepslate_coal_ore",
  8.         "minecraft:iron_ore",
  9.         "minecraft:deepslate_iron_ore",
  10.         "minecraft:copper_ore",
  11.         "minecraft:deepslate_copper_ore",
  12.         "minecraft:gold_ore",
  13.         "minecraft:deepslate_gold_ore",
  14.         "minecraft:redstone_ore",
  15.         "minecraft:deepslate_redstone_ore",
  16.         "minecraft:emeral_ore",
  17.         "minecraft:deepslate_emerald_ore",
  18.         "minecraft:lapis_ore",
  19.         "minecraft:deepslate_lapis_ore",
  20.         "minecraft:diamond_ore",
  21.         "minecraft:deepslate_diamond_ore",
  22.     },
  23.     fuelMin = 100,
  24. }
  25. local side
  26. local current = -1
  27. local t = turtle
  28.  
  29.  
  30.  
  31.  
  32. local function compare(wanted, found)
  33.     for i = 1, #wanted do
  34.         if found == wanted[i] then
  35.             return true
  36.         end
  37.     end
  38.     return false
  39. end
  40.  
  41. local function detect(dir)
  42.     if dir == "forward" then
  43.         return t.detect()
  44.     elseif dir == "down" then
  45.         return t.detectDown()
  46.     elseif dir == "up" then
  47.         return t.detectUp()
  48.     end
  49. end
  50.  
  51. local function inspect(dir)
  52.     local success, data
  53.     if dir == "forward" then
  54.         success, data = t.inspect()
  55.     elseif dir == "down" then
  56.         success, data = t.inspectDown()
  57.     elseif dir == "up" then
  58.         success, data = t.inspectUp()
  59.     end
  60.     return success, data
  61. end
  62.  
  63. local function dig(dir)
  64.     if dir == "forward" then
  65.         t.dig()
  66.     elseif dir == "down" then
  67.         t.digDown()
  68.     elseif dir == "up" then
  69.         t.digUp()
  70.     end
  71. end
  72.  
  73. local function move(dir)
  74.     if dir == "forward" then
  75.         t.forward()
  76.     elseif dir == "down" then
  77.         t.down()
  78.     elseif dir == "up" then
  79.         t.up()
  80.     elseif dir == "back" then  
  81.         t.back()
  82.     end
  83. end
  84.  
  85. local function detectTurtle(dir)
  86.     local success, data = inspect(dir)
  87.     if success and data.name == "computercraft:turtle_advanced" then
  88.         return true
  89.     end
  90.     return false
  91. end
  92.  
  93. local function mineMove(dir)
  94.     local dug = false
  95.     while detect(dir) do
  96.         if not detectTurtle(dir) then
  97.             dig(dir)
  98.             dug = true
  99.         else
  100.             sleep(2)
  101.         end
  102.     end
  103.     move(dir)
  104.     return dug
  105. end
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. local function refuel()
  113.     print("refueling...")
  114.     for i=1,16,1 do
  115.         t.select(i)
  116.         t.refuel(t.getItemCount(i))
  117.     end
  118.     t.select(1)
  119.     print("fuel level: "..t.getFuelLevel())
  120. end
  121.  
  122. local function detectOre()
  123.     local opposites = {
  124.         ["forward"] = "back",
  125.         ["back"] = "forward",
  126.         ["up"] = "down",
  127.         ["down"] = "up",
  128.     }
  129.     local function func(dir)
  130.         local success, data = inspect(dir)
  131.         if success and compare(options.wantedOre, data.name) then
  132.             print("found " .. data.name)
  133.             mineMove(dir)
  134.             detectOre()
  135.             mineMove(opposites[dir])
  136.         end
  137.     end
  138.     func("up")
  139.     func("down")
  140.     for _=1,4,1 do
  141.         func("forward")
  142.         t.turnRight()
  143.     end
  144. end
  145.  
  146. local function sideShaft()
  147.     for _=1,options.wingLength,1 do
  148.         mineMove("forward")
  149.         detectOre()
  150.     end
  151.     mineMove("up")
  152.     mineMove("up")
  153.     mineMove("up")
  154.     t.turnLeft()
  155.     t.turnLeft()
  156.     for _=1,options.wingLength,1 do
  157.         detectOre()
  158.         mineMove("forward")
  159.     end
  160.     mineMove("down")
  161.     mineMove("down")
  162.     mineMove("down")
  163.     t.turnLeft()
  164.     t.turnLeft()
  165. end
  166.  
  167. local function mainShaft()
  168.     if t.getFuelLevel() < options.fuelMin then
  169.         refuel()
  170.     end
  171.  
  172.     while t.detectUp() do
  173.         t.digUp()
  174.     end
  175.  
  176.     if current == -1 then
  177.         while not t.detect() do
  178.             local success, data = inspect("down")
  179.             if success and data.name == options.startBlock then
  180.                 break
  181.             end
  182.             t.forward()
  183.         end
  184.         current = 1
  185.         t.turnLeft()
  186.         t.turnLeft()
  187.     elseif current <= options.wingFreq then
  188.         current = current + 1
  189.         mineMove("forward")
  190.     else
  191.         current = 1
  192.         if side == "left" then t.turnLeft()
  193.         elseif side == "right" then t.turnRight()
  194.         end
  195.  
  196.         if t.detect() then
  197.             sideShaft()
  198.         end
  199.  
  200.         if side == "left" then t.turnRight()
  201.         elseif side == "right" then t.turnLeft()
  202.         end
  203.     end
  204. end
  205.  
  206. local function main()
  207.     -- TODO: check fuel levels
  208.     -- TODO: check inventory space
  209.     mainShaft()
  210. end
  211.  
  212. local function init()
  213.     print("BRANCH MINING MK2")
  214.     print("shaft frequency: "..options.wingFreq)
  215.     print("shaft length: "..options.wingLength)
  216.     refuel()
  217.  
  218.     t.turnLeft()
  219.     if t.detect() then side = "left" end -- TODO: filter out torches and turtles
  220.     t.turnLeft()
  221.     t.turnLeft()
  222.     if t.detect() then side = "right" end
  223.     t.turnRight()
  224.     print("side: "..side)
  225.  
  226.     if side ~= nil then
  227.         while true do
  228.             main()
  229.         end
  230.     end
  231. end
  232. init()
  233.  
  234. -- i hate lua
Add Comment
Please, Sign In to add comment