RandomShovel

[CC][Beta] Branch Miner

Jul 14th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.05 KB | None | 0 0
  1. --[[  Config  ]]--
  2.  
  3. local mTorch = true
  4. -->  Places torches
  5.  
  6. local torchDistance = 5
  7. -->  The distance the torches will be placed
  8.  
  9. local rFill = true
  10. -->  Fills ring to prevent liquid leaks
  11. --->  Not Implemented
  12.  
  13. local enderchest = false
  14. -->  Uses an enderchest to empty inventory
  15.  
  16. local textFlash = true
  17. -->  Flashes various text!
  18. --->  For important reasons of course!
  19. --->  You can disable this if you can't see the message clearly... :(
  20.  
  21. local rTStart = true
  22. -->  The turtle will return to start when finished
  23.  
  24. local sRFuel = false
  25. -->  The turtle will refuel 10% of all fuel
  26.  
  27. local lastSlot = 13
  28. -->  The last slot the turtle will check for items
  29. -->  Just in case you wanted to shrink.. your.. turtle's inventory..?
  30.  
  31.  
  32. --[[  Slot Settings  ]]--
  33.  
  34. local slots = {
  35.  {14, "Enderchest"},
  36.  {15, "Fuel"},
  37.  {16, "Torch"}
  38. }
  39. -->  The first number is the slot number, the second is the display name
  40.  
  41.  
  42. --[[  DO NOT EDIT WHAT'S BELOW UNLESS YOU KNOW WHAT YOU'RE DOING  ]]--
  43.  
  44.  
  45. --[[  Variables  ]]--
  46.  
  47. -->  pVersion uses a specific naming convention.
  48. --->  The numbers on the left side of the dash (-) is the last version the program was edited in
  49. --->  The number(s) on the right side of the dash (-) is the release version
  50.  
  51. local pVersion = "1.7.10-2"
  52. local pHeader = "Branch Miner v"..pVersion.." BETA"
  53. local pFooter = "Bug reports ARE helpful!"
  54. local tDistance = 0
  55.  
  56.  
  57. --[[  Functions  ]]--
  58.  
  59. local function cs()
  60.  term.clear()
  61.  term.setCursorPos(1, 1)
  62. end
  63.  
  64. local function centerT(text, textType)
  65.  local mx, my = term.getSize()
  66.  local cx, cy = term.getCursorPos()
  67.  term.setCursorPos(math.floor((mx-string.len(text))/2), cy)
  68.  if string.lower(textType) == "print" then
  69.   print(text)
  70.  elseif string.lower(textType) == "write" then
  71.   write(text)
  72.  elseif string.lower(textType) == "error" then
  73.   printError(text)
  74.  elseif string.lower(textType) == "swrite" then
  75.   textutils.slowWrite(text)
  76.  elseif string.lower(textType) == "sprint" then
  77.   textutils.slowPrint(text)
  78.  end
  79. end
  80.  
  81. local function dScreen()
  82.  cs()
  83.  centerT(pHeader, "print")
  84.  centerT("-------------", "print")
  85.  local mx, my = term.getSize()
  86.  term.setCursorPos(1, my-1)
  87.  centerT("-------------", "print")
  88.  centerT(pFooter, "write")
  89.  term.setCursorPos(1, 5)
  90. end
  91.  
  92. local function cFuel()
  93.  if sRFuel then
  94.   local fuelLevel = turtle.getFuelLevel()
  95.   local sFuel = 0
  96.    for a=1, lastSlot do
  97.     if turtle.getItemCount(a) > 0 then
  98.      turtle.select(a)
  99.      for b=1, #slots do
  100.       if string.lower(slots[b][2]) == "fuel" then
  101.        if turtle.compareTo(slots[b][1]) then
  102.         sFuel = sFuel + turtle.getItemCount(a)
  103.        end
  104.       end
  105.      end
  106.     end
  107.    end
  108.    local sFuel = math.floor(sFuel/10)
  109.   if fuelLevel <= 20 then
  110.    for a=1, lastSlot do
  111.     turtle.select(a)
  112.     for b=1, #slots do
  113.      if string.lower(slots[b][2]) == "fuel" then
  114.       if turtle.compareTo(slots[b][1]) then
  115.        local iFuel = turtle.getItemCount(a)
  116.        local uFuel = iFuel - sFuel
  117.        if sFuel <= iFuel then
  118.         turtle.refuel(sFuel)
  119.         break
  120.        else
  121.         turtle.refuel()
  122.         sFuel = sFuel - iFuel
  123.        end
  124.       end
  125.      end
  126.     end
  127.    end
  128.   elseif fuelLevel == "unlimited" then
  129.    centerT("You have unlimited fuel!", "print")
  130.    centerT("Why did you enable smart refuel..?", "print")
  131.   end
  132.  else
  133.   for a=1, lastSlot do
  134.    turtle.select(a)
  135.    for b=1, #slots do
  136.     if string.lower(slots[b][2]) == "fuel" then
  137.      if turtle.compareTo(15) then
  138.       turtle.refuel()
  139.       break
  140.      end
  141.     end
  142.    end
  143.   end
  144.  end
  145. end
  146.  
  147. local function sInventory()
  148.  if turtle.getItemCount(lastSlot) > 0 then
  149.   for a=1, lastSlot do
  150.    if turtle.getItemCount(a) < 64 and turtle.getItemCount(a) > 0 then
  151.     for b=a+1, lastSlot do
  152.      if turtle.getItemCount(b) > 0 then
  153.       turtle.select(b)
  154.       if string.lower(slots[1][2]) == "enderchest" then
  155.        if enderchest and b ~= slots[1][1] then
  156.         if turtle.compareTo(a) and turtle.getItemCount(a) < 64 then
  157.          turtle.select(b)
  158.          turtle.transferTo(a)
  159.         end
  160.        else
  161.         if turtle.compareTo(a) and turtle.getItemCount(a) < 64 then
  162.          turtle.select(b)
  163.          turtle.transferTo(a)
  164.         end
  165.        end
  166.       end
  167.      end
  168.     end
  169.    end
  170.   end
  171.  
  172.   for a=1, lastSlot do
  173.    for b=a+1, lastSlot do
  174.     if string.lower(slots[1][2]) == "enderchest" then
  175.      if enderchest and b ~= slots[1][1] then
  176.       if turtle.getItemCount(a) == 0 and turtle.getItemCount(b) > 0 then
  177.        turtle.select(b)
  178.        turtle.transferTo(a)
  179.       end
  180.     else
  181.       if turtle.getItemCount(a) == 0 and turtle.getItemCount(b) > 0 then
  182.        turtle.select(b)
  183.        turtle.transferTo(a)
  184.       end
  185.      end
  186.     end
  187.    end
  188.   end
  189.  end
  190. end
  191.  
  192. local function eInventory()
  193.  sInventory()
  194.  if turtle.getItemCount(lastSlot) > 0 then
  195.   if enderchest then
  196.    if turtle.getItemCount(lastSlot) > 0 then
  197.     turtle.select(16)
  198.     turtle.turnLeft()
  199.     turtle.turnLeft()
  200.     turtle.place()
  201.     for a=1, lastSlot do
  202.      turtle.select(a)
  203.      turtle.drop()
  204.     end
  205.     turtle.select(16)
  206.     turtle.dig()
  207.     turtle.select(1)
  208.     turtle.turnRight()
  209.     turtle.turnRight()
  210.    end
  211.   else
  212.    turtle.turnLeft()
  213.    turtle.turnLeft()
  214.    for a=1, tDistance do
  215.     turtle.forward()
  216.    end
  217.    for a=1, lastSlot do
  218.     turtle.select(a)
  219.     turtle.drop()
  220.    end
  221.    turtle.turnLeft()
  222.    turtle.turnLeft()
  223.    for a=1, tDistance do
  224.     turtle.forward()
  225.    end
  226.   end
  227.  end
  228. end
  229.  
  230. local function stats()
  231.  cs()
  232.  local fuelLevel = turtle.getFuelLevel()
  233.  local availFuel = 0
  234.  local uInventory = 0
  235.  local fInventory = 64 * lastSlot
  236.  for a=1, lastSlot do
  237.   if turtle.getItemCount(a) > 0 then
  238.    uInventory = uInventory + turtle.getItemCount(a)
  239.    turtle.select(a)
  240.   end
  241.   for b=1, #slots do
  242.    if string.lower(slots[b][2]) == "fuel" then
  243.     if turtle.compareTo(slots[b][1]) then
  244.      availFuel = availFuel + turtle.getItemCount(a)
  245.     end
  246.    end
  247.   end
  248.  end
  249.  if smart_refuel then
  250.   local sFuel = math.floor(availFuel/10)
  251.  end
  252.  dScreen()
  253.  centerT("Fuel: "..fuelLevel, "print")
  254.  if smart_refuel then
  255.   centerT("Fuel Supplies: "..sFuel.."/"..availFuel, "print")
  256.  else
  257.   centerT("Fuel Supplies: "..availFuel, "print")
  258.  end
  259.  local pInventory = math.floor((uInventory/fInventory)*100)
  260.  centerT("Last Storage Slot: "..lastSlot, "print")
  261.  centerT("Storage: "..pInventory.."% full ("..uInventory.." items)", "print")
  262.  centerT("Distance: "..tDistance.." out of "..length.." ("..length-tDistance.." left) ", "print")
  263. end
  264.  
  265. local function bMine()
  266.  for a=1, length do
  267.   sInventory()
  268.   stats()
  269.   cFuel()
  270.   turtle.select(1)
  271.   for b=1, 3 do
  272.    if a ~= length then
  273.     if turtle.detect() then
  274.      repeat
  275.       turtle.dig()
  276.       sleep(0.5)
  277.      until not turtle.detect()
  278.     end
  279.    end
  280.    turtle.turnLeft()
  281.    if turtle.detect() then
  282.     repeat
  283.      turtle.dig()
  284.      sleep(0.5)
  285.     until not turtle.detect()
  286.    end
  287.    if b == 2 and (tDistance % torchDistance) == 0 and mTorch then
  288.     turtle.forward()
  289.     if turtle.detect() then
  290.      turtle.dig()
  291.     end
  292.     for d=1, #slots do
  293.      if string.lower(slots[d][2]) == "torch" then
  294.       turtle.select(slots[d][1])
  295.       turtle.place()
  296.      end
  297.     end
  298.     turtle.back()
  299.    end
  300.    turtle.turnRight()
  301.    turtle.turnRight()
  302.    if turtle.detect() then
  303.     repeat
  304.      turtle.dig()
  305.      sleep(0.5)
  306.     until not turtle.detect()
  307.    end
  308.    turtle.turnLeft()
  309.    if b < 3 then
  310.     if turtle.detectUp() then
  311.      repeat
  312.       turtle.digUp()
  313.       sleep(0.5)
  314.      until not turtle.detectUp()
  315.     end
  316.    end
  317.    if b < 3 then
  318.     turtle.up()
  319.    end
  320.   end
  321.   for b=1, 2 do
  322.    turtle.down()
  323.   end
  324.   if not turtle.forward() then
  325.    repeat
  326.     turtle.dig()
  327.     sleep(0.5)
  328.    until not turtle.detect()
  329.    turtle.forward()
  330.   end
  331.   tDistance = tDistance + 1
  332.   eInventory()
  333.  end
  334.  if rTStart then
  335.   turtle.turnRight()
  336.   turtle.turnRight()
  337.   for a=1, length do
  338.    turtle.forward()
  339.   end
  340.  end
  341. end
  342.  
  343.  
  344. --[[  Main  ]]--
  345.  
  346.  --[[  Get Inputs  ]]--
  347.  
  348.  repeat
  349.   dScreen()
  350.   centerT("How far should I tunnel?", "print")
  351.   centerT("TIP: This shouldn't be less than 10", "print")
  352.   local cx, cy = term.getCursorPos()
  353.   write("> ")
  354.   local ulength = read()
  355.   term.setCursorPos(1, cy-1)
  356.   term.clearLine(cy-1)
  357.   term.setCursorPos(1, cy)
  358.   term.clearLine(cy)
  359.   local mx, my = term.getSize()
  360.   term.setCursorPos(1, my-3)
  361.   centerT("Distance: "..ulength, "print")
  362.   if type(tonumber(ulength)) == "nil" then
  363.    length = string.len(ulength)
  364.   else
  365.    length = tonumber(ulength)
  366.   end
  367.   sleep(1)
  368.  until length > 0
  369.  
  370.  
  371.  --[[  Slot Usage  ]]--
  372.  
  373.  dScreen()
  374.  centerT("Loading...", "swrite")
  375.  sleep(0.5)
  376.  term.clearLine(1)
  377.  term.setCursorPos(1, 4)
  378.  for a=slots[1][1], slots[#slots][1] do
  379.   if a == 14 then
  380.    if enderchest then
  381.     centerT('Please put an "'..slots[a-13][2]..'" in slot '..slots[a-13][1], "print")
  382.    end
  383.   elseif a == 15 then
  384.    centerT('Please put "'..slots[a-13][2]..'" in slot '..slots[a-13][1], "print")
  385.   else
  386.    centerT('Please put a "'..slots[a-13][2]..'" in slot '..slots[a-13][1], "print")
  387.   end
  388.  end
  389.  local mx, my = term.getSize()
  390.  term.setCursorPos(1, my-3)
  391.  centerT("Press any key to continue", "print")
  392.  local event = os.pullEvent("key")
  393.  if event then
  394.   for a=slots[1][1], slots[#slots][1] do
  395.    cs()
  396.    dScreen()
  397.    if a == 14 then
  398.     if string.lower(slots[a-13][2]) == "enderchest" and enderchest then
  399.      repeat
  400.       cs()
  401.       dScreen()
  402.       if textFlash then
  403.        sleep(1)
  404.       end
  405.       if turtle.getItemCount(a) > 0 then
  406.        print('Assuming an "'..slots[a-13][2]..'" is in slot '..a)
  407.       else
  408.        centerT('Missing an "'..slots[a-13][2]..'" in slot '..a, "error")
  409.        centerT("I will check again in 1 second...", "print")
  410.       end
  411.       if textFlash then
  412.        sleep(1)
  413.       end
  414.      until turtle.getItemCount(a) > 0
  415.     else
  416.      centerT("Enderchest use is disabled!", "error")
  417.      if lastSlot == 13 then
  418.       print("Assuming last slot is the default value")
  419.       lastSlot = 14
  420.       sleep(0.5)
  421.      end
  422.     end
  423.    elseif a == 15 then
  424.     repeat
  425.      cs()
  426.      dScreen()
  427.      if textFlash then
  428.       sleep(1)
  429.      end
  430.      if turtle.getItemCount(a) > 0 then
  431.       centerT('Assuming "'..slots[a-13][2]..'" is in slot '..a, "print")
  432.      else
  433.       centerT('Missing "'..slots[a-13][2]..'" in slot '..a, "error")
  434.       centerT("I will check again in 1 second...", "print")
  435.      end
  436.      if textFlash then
  437.       sleep(1)
  438.      end
  439.     until turtle.getItemCount(a) > 0
  440.    else
  441.     repeat
  442.      cs()
  443.      dScreen()
  444.      if textFlash then
  445.       sleep(1)
  446.      end
  447.      if turtle.getItemCount(a) > 0 then
  448.       centerT('Assuming a "'..slots[a-13][2]..'" is in slot '..a, "print")
  449.      else
  450.       centerT('Missing a "'..slots[a-13][2]..'" in slot '..a, "error")
  451.       centerT("I will check again in 1 second...", "print")
  452.      end
  453.      if textFlash then
  454.       sleep(1)
  455.      end
  456.     until turtle.getItemCount(a) > 0
  457.    end
  458.    sleep(0.5)
  459.   end
  460.  end
  461.  
  462.  --[[  Branch Mine  ]]--
  463.  
  464.  bMine()
  465.  stats()
  466.  local mx, my = term.getSize()
  467.  centerT("Finished!", "print")
  468.  sleep(2)
  469.  cs()
Advertisement
Add Comment
Please, Sign In to add comment