Advertisement
brensen11

BetterMining

May 4th, 2022 (edited)
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.78 KB | None | 0 0
  1. local m = require( "blib" )
  2. local torch = "minecraft:torch"
  3. local chest = "minecraft:chest"
  4. local enderChest = "minecraft:ender_chest"
  5. local chestDumpLoc = {x = 1, y = 0, z = 1, xf = 0, yf = -1}
  6.  
  7. local Length
  8. local Width
  9.  
  10. local reqTorches
  11. local reqChests
  12.  
  13. function Shutdown(message)
  14.     print(message)
  15.     print("Powering down...")
  16.     return 1
  17. end
  18.  
  19. function NavigateToLocation(location, order)
  20.  
  21.     for i = 1, 2 do
  22.         print(order[i] == "x")
  23.         print(order[i])
  24.         print("x")
  25.         if(order[i] == "x") then
  26.             -- Orient Turtle X Direction
  27.             if(m.coords.x < location.x) then
  28.                 while m.coords.xf ~= 1 do
  29.                     m.MapTurnRight()
  30.                 end
  31.             else
  32.                 while m.coords.xf ~= -1 do
  33.                     m.MapTurnRight()
  34.                 end
  35.             end
  36.        
  37.             -- Navigate X axis
  38.             while m.coords.x ~= location.x do
  39.                 m.MapForward()
  40.             end
  41.         elseif (order[i] == "y") then
  42.                 -- Orient Turtle Y Direction
  43.             if(m.coords.y < location.y) then
  44.                 while m.coords.yf ~= 1 do
  45.                     m.MapTurnRight()
  46.                 end
  47.             else
  48.                 while m.coords.yf ~= -1 do
  49.                     m.MapTurnRight()
  50.                 end
  51.             end
  52.        
  53.             -- Navigate Y axis
  54.             while m.coords.y ~= location.y do
  55.                 m.MapForward()
  56.             end
  57.         end
  58.    
  59.     end
  60.    
  61.     -- Orient according to location specs
  62.     while m.coords.xf ~= location.xf do
  63.         m.MapTurnRight()
  64.     end
  65.  
  66.     while m.coords.yf ~= location.yf do
  67.         m.MapTurnRight()
  68.     end
  69.  
  70. end
  71.  
  72. function DistanceFromLocation(location)
  73.     xd = math.abs(location.x - m.coords.x)
  74.     yd = math.abs(location.y - m.coords.y)
  75.     zd = math.abs(location.z - m.coords.z)
  76.     return xd + yd + zd
  77. end
  78.  
  79. --- Actual Mining ---
  80. function Start()
  81.  
  82.     if(m.SelectItem("minecraft:chest")) then
  83.         turtle.digDown()
  84.         turtle.placeDown()
  85.     end
  86.     m.Forward3() -- The turtle starts outside the square, as such, it enters at the start, and the Y index
  87.                  -- is actually decremented by one, since mining on the x axis will manage one block off each
  88.                  -- level change
  89.  
  90.     local torchL = 0
  91.     local torchW = 8
  92.  
  93.  
  94.     -- fix not finish on width
  95.     -- fix chest storage thing (puts in coal)
  96.     for x = 1, Width do                         -- starting length cuts one turn off, but not one column off. see below
  97.         torchW = torchW + 1                     -- increment torchPlacingWidth
  98.  
  99.         for y = 1, Length - 1 do                -- width cuts one length down off the next
  100.            
  101.             -- check for fuel
  102.             local fuelAmount = DistanceFromLocation(chestDumpLoc) + 5
  103.             if(not m.CheckFuel(fuelAmount)) then
  104.                 local savedLoc = m.table_deepcopy(m.coords)
  105.                 NavigateToLocation(chestDumpLoc, {"x", "y"})
  106.                 while not m.SelectItem("minecraft:coal") do
  107.                     print("Out of fuel ...")
  108.                     print("Place coal in the inventory to continue.")
  109.                     os.sleep(3)
  110.                 end
  111.                 m.CheckFuel(fuelAmount)
  112.                 NavigateToLocation(savedLoc, {"y", "x"})
  113.             end
  114.            
  115.             -- check for torch placement
  116.             torchL = torchL + 1                 -- increment torchPlacementLength
  117.             if(reqTorches == true) then
  118.                 if(torchL >= 8 and torchW >= 6) then -- if its been 6 blocks wide and traveled 8 blocks down, place a torch
  119.                     if(not m.PlaceTorch()) then
  120.                         local savedLoc = m.table_deepcopy(m.coords)
  121.                         NavigateToLocation(chestDumpLoc, {"x", "y"})
  122.                         while not m.SelectItem("minecraft:torch") do
  123.                             print("Out of torches ...")
  124.                             print("Place coal in the inventory to continue.")
  125.                             os.sleep(3)
  126.                         end
  127.                         NavigateToLocation(savedLoc, {"y", "x"})
  128.                     end
  129.                     torchL = 1
  130.                 end
  131.             end
  132.  
  133.            
  134.             -- check if inventory is full
  135.             if(m.InventoryFull() == true) then
  136.                 local savedLoc = m.table_deepcopy(m.coords)
  137.                 NavigateToLocation(chestDumpLoc, {"x", "y"})
  138.                 if(not m.DumpItemsExistingChest("down")) then
  139.                     local s, data = turtle.inspectDown()
  140.                     while data.name ~= "minecraft:chest" do
  141.                         s, data = turtle.inspectDown()
  142.                         print("Waiting for chest ... ")
  143.                         os.sleep(3)
  144.                     end
  145.                     m.DumpItemsExistingChest("down")
  146.                 end
  147.                 NavigateToLocation(savedLoc, {"y", "x"})
  148.             end
  149.            
  150.  
  151.             m.Forward3()
  152.         end
  153.  
  154.         if( Width - x ~= 0) then -- Technically on the last column, we continue forward, but don't turn into the next column
  155.             if(math.fmod(x, 2) == 0) then -- if we're on an even column, turn left
  156.                 m.TurnLeftDig()
  157.             else -- odd columns (start is 1 btw) turn right!
  158.                 m.TurnRightDig()
  159.             end
  160.         end
  161.  
  162.         if(torchW >= 6)then -- if its been 6 blocks wide, then reset!
  163.             torchW = 0
  164.         end
  165.  
  166.     end
  167.  
  168.     return 1
  169. end
  170.  
  171.  
  172.  
  173. --- main ---
  174. function Main()
  175.     local response
  176.     print("Welcome to the BetterMining Program")
  177.     print("BetterMining will mine a 2D plane defined by your X and Y inputs")
  178.     print("")
  179.     print("Do you want to require torches?")
  180.     print("(y for yes, n for no)")
  181.     response = read()
  182.     if(response == "y") then
  183.         reqTorches = true
  184.     else
  185.         reqTorches = false
  186.     end
  187.  
  188.     -- check if has items
  189.     if(m.GetItemIndex(torch) == nil and reqTorches == true)then
  190.         print("You don't have any torches!")
  191.         return
  192.     end
  193.  
  194.     --check if has fuel
  195.     if(not m.CheckFuel(10))then
  196.         print("You don't have fuel!")
  197.         return
  198.     end
  199.  
  200.     m.Clear()
  201.    
  202.  
  203.     -- Gets Y Input from user
  204.     print("Length?")
  205.     Length = tonumber(io.read())
  206.     while Length == nil do
  207.         print("oops! enter a number!")
  208.         Length = tonumber(io.read())
  209.     end
  210.  
  211.  
  212.     -- Gets X Input from user
  213.     print("")
  214.     print("Width?")
  215.     Width = tonumber(io.read())
  216.     while Width == nil do
  217.         print("oops! enter a number!")
  218.         Width = tonumber(io.read())
  219.     end
  220.  
  221.    
  222.     print("Mining Started ... ")
  223.     Start()
  224.     NavigateToLocation(chestDumpLoc, {"x", "y"})
  225.     print("Finished!")
  226. end
  227. Main()
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement