Advertisement
brensen11

Strip Mine

Jun 2nd, 2022 (edited)
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.23 KB | None | 0 0
  1. local m = require( "blib" )
  2. local torch = "minecraft:torch"
  3. local chest = "minecraft:chest"
  4.  
  5. local Length
  6. local Width
  7.  
  8. local fuelMin = 25
  9.  
  10. local reqTorches
  11.  
  12. local function ends_with(str, ending)
  13.     if(str == nil) then
  14.         return false
  15.     end
  16.  
  17.     return ending == "" or str:sub(-#ending) == ending -- colon operator and string doc - https://docs.coronalabs.com/api/library/string/sub.html
  18.  end
  19.  
  20.  function Shutdown(message)
  21.     print(message)
  22.     print("Powering down...")
  23.     return 1 -- By C convention 0 is success, 1 is failure
  24. end
  25.  
  26. function IsOre(OreName)
  27.     local returnVal = ends_with(OreName, "ore")
  28.     if(OreName == "minecraft:diamond_ore") then
  29.         returnVal = false
  30.     end
  31.     return(returnVal)
  32. end
  33.  
  34. function MineIfOre(direction)
  35.     local success, inspection
  36.     if(direction == "up") then
  37.         success, inspection = turtle.inspectUp()
  38.         if(IsOre(inspection.name)) then
  39.             turtle.digUp()
  40.         end
  41.         return
  42.     elseif(direction == "down") then
  43.         success, inspection = turtle.inspectDown()
  44.         if(IsOre(inspection.name) or inspection.name == "minecraft:lava") then
  45.             turtle.digDown()
  46.             if(m.SelectItem("minecraft:cobblestone") == true) then
  47.                 turtle.placeDown()
  48.             end
  49.         end
  50.         return
  51.     elseif(direction == "forward") then
  52.         success, inspection = turtle.inspect()
  53.     else
  54.         success, inspection = turtle.inspect()
  55.     end
  56.  
  57.     if(IsOre(inspection.name)) then
  58.         turtle.dig()
  59.     end
  60. end
  61.  
  62.  
  63. function InspectLR()
  64.     m.CheckFuel(fuelMin)
  65.     turtle.turnLeft()
  66.     MineIfOre()
  67.     turtle.turnRight()
  68.     turtle.turnRight()
  69.     MineIfOre()
  70.     turtle.turnLeft()
  71. end
  72.  
  73. function DigSequence()
  74.     m.CheckFuel(fuelMin)
  75.     m.DetectAndDig()
  76.     turtle.forward()
  77.     MineIfOre("down")
  78.     InspectLR()
  79.     turtle.digUp()
  80.     turtle.up()
  81.     MineIfOre("up")
  82.     InspectLR()
  83.     turtle.down()
  84. end
  85.  
  86. function FirstLap()
  87.     local trueWidth = (Width - 1)*2 + Width
  88.  
  89.     for y = 1, Length do
  90.         m.CheckFuel(fuelMin)
  91.         m.DetectAndDig()
  92.         turtle.forward()
  93.         turtle.digUp()
  94.     end
  95.     turtle.turnRight()
  96.     for x = 1, trueWidth - 1 do
  97.         m.CheckFuel(fuelMin)
  98.         m.DetectAndDig()
  99.         turtle.forward()
  100.         turtle.digUp()
  101.     end
  102.     turtle.turnRight()
  103.     for y = 1, Length - 1 do
  104.         m.CheckFuel(fuelMin)
  105.         m.DetectAndDig()
  106.         turtle.forward()
  107.         turtle.digUp()
  108.     end
  109.     turtle.turnRight()
  110.     for x = 1, trueWidth - 1 do
  111.         m.CheckFuel(fuelMin)
  112.         m.DetectAndDig()
  113.         turtle.forward()
  114.         turtle.digUp()
  115.     end
  116.     turtle.turnRight()
  117. end
  118.  
  119. function Start()
  120.  
  121.     FirstLap()
  122.    
  123.     m.CheckFuel(fuelMin)
  124.     InspectLR()
  125.     turtle.digUp()
  126.     turtle.up()
  127.     InspectLR()
  128.     turtle.down()
  129.  
  130.     local torchL = 6
  131.  
  132.     for x = 1, Width do                         -- starting length cuts one turn off, but not one column off. see below
  133.  
  134.  
  135.         for y = 1, Length - 1 do                -- width cuts one length down off the next
  136.              
  137.             -- check for fuel
  138.              if(not m.CheckFuel(fuelMin)) then
  139.                 return Shutdown("Out of Fuel!")
  140.             end
  141.            
  142.             -- check if inventory is full
  143.             if(m.InventoryFull() == true) then
  144.                 if(m.DumpItemsChest() == false) then
  145.                     return Shutdown("Out of chests!")
  146.                 end
  147.             end
  148.  
  149.             -- check for torch placement
  150.             torchL = torchL + 1                         -- increment torchPlacementLength
  151.  
  152.             if(reqTorches == true) then
  153.                 if(torchL >= 6) then    -- if its been 6 blocks wide and traveled 8 blocks down, place a torch
  154.                     if(not m.PlaceTorch("left")) then
  155.                         return Shutdown("Out of torches!")
  156.                     end
  157.                     torchL = 0
  158.                 end
  159.             end
  160.  
  161.             -- Actual Mining Sequence
  162.             DigSequence()
  163.  
  164.         end
  165.  
  166.         if( Width - x ~= 0) then -- Technically on the last column, we continue forward, but don't turn into the next column
  167.             m.CheckFuel(fuelMin)
  168.             if(math.fmod(x, 2) == 0) then -- if we're on an even column, turn left (LUA STARTS WITH 1 weird i know)
  169.                 turtle.turnLeft()
  170.                 DigSequence()
  171.                 DigSequence()
  172.                 DigSequence()
  173.                 turtle.turnLeft()
  174.                
  175.             else -- odd columns (start is 1 btw) turn right!
  176.                 turtle.turnRight()
  177.                 DigSequence()
  178.                 DigSequence()
  179.                 DigSequence()
  180.                 turtle.turnRight()
  181.             end
  182.         end
  183.  
  184.     end
  185.  
  186. end
  187.  
  188. function Main()
  189.     local response
  190.     print("Welcome to the Strip Mining Program")
  191.     print("Strip Mining will mine hallways 2 blocks apart, and try its best to mine out all surrounding ore")
  192.     print("")
  193.     print("Do you want to require torches?")
  194.     print("(y for yes, n for no)")
  195.     response = read()
  196.     if(response == "y") then
  197.         reqTorches = true
  198.     else
  199.         reqTorches = false
  200.     end
  201.  
  202.      --------------- check if has items --------------------------
  203.      if(m.GetItemIndex(torch) == nil and reqTorches == true)then
  204.         print("You don't have any torches!")
  205.         return
  206.     elseif( m.GetItemIndex(chest) == nil) then
  207.         print("You don't have any chests!")
  208.         return
  209.     end
  210.  
  211.     ----------------- check if has fuel ---------------------------
  212.     if(not m.CheckFuel(10))then
  213.         print("You don't have fuel!")
  214.         return
  215.     end
  216.  
  217.     --------------- Gets Y Input from user ------------------------
  218.     print("Length?")
  219.     Length = tonumber(io.read())
  220.     while Length == nil do
  221.         print("oops! enter a number!")
  222.         Length = tonumber(io.read())
  223.     end
  224.  
  225.     ---------------- Gets X Input from user ---------------------
  226.     print("")
  227.     print("Width?")
  228.     Width = tonumber(io.read())
  229.     while Width == nil do
  230.         print("oops! enter a number!")
  231.         Width = tonumber(io.read())
  232.     end
  233.  
  234.     print("Strip Mining Started ... ")
  235.     Start()
  236.     print("finished!")
  237.    
  238. end
  239. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement