Advertisement
brensen11

bexcavate

Sep 29th, 2022 (edited)
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. local m = require("blib")
  2. local chestDumpLoc = {x = 1, y = 1, z = 1, xf = 0, yf = -1}
  3.  
  4. function NavigateToLocation(location)
  5.     -- Navigate Z axis
  6.     while m.coords.z ~= location.z do
  7.         if(m.coords.z < location.z) then
  8.             m.MapUp()
  9.         else
  10.             m.MapDown()
  11.         end
  12.     end
  13.  
  14.     -- Orient Turtle Y Direction
  15.     if(m.coords.y < location.y) then
  16.         while m.coords.yf ~= 1 do
  17.             m.MapTurnRight()
  18.         end
  19.     else
  20.         while m.coords.yf ~= -1 do
  21.             m.MapTurnRight()
  22.         end
  23.     end
  24.  
  25.     -- Navigate Y axis
  26.     while m.coords.y ~= location.y do
  27.         m.MapForward()
  28.     end
  29.  
  30.     -- Orient Turtle X Direction
  31.     if(m.coords.x < location.x) then
  32.         while m.coords.xf ~= 1 do
  33.             m.MapTurnRight()
  34.         end
  35.     else
  36.         while m.coords.xf ~= -1 do
  37.             m.MapTurnRight()
  38.         end
  39.     end
  40.  
  41.     -- Navigate X axis
  42.     while m.coords.x ~= location.x do
  43.         m.MapForward()
  44.     end
  45.  
  46.     -- Orient according to location specs
  47.     while m.coords.xf ~= location.xf do
  48.         m.MapTurnRight()
  49.     end
  50.  
  51.     while m.coords.yf ~= location.yf do
  52.         m.MapTurnRight()
  53.     end
  54.  
  55. end
  56.  
  57. function DistanceFromLocation(location)
  58.     xd = math.abs(location.x - m.coords.x)
  59.     yd = math.abs(location.y - m.coords.y)
  60.     zd = math.abs(location.z - m.coords.z)
  61.  
  62.     return xd + yd + zd
  63. end
  64.  
  65. --- Digging Process ---
  66. function Start()
  67.  
  68.     m.MapForward()
  69.     if(m.SelectItem("minecraft:chest")) then
  70.        m.MapTurnRight()
  71.        m.MapTurnRight()
  72.        turtle.place()
  73.        m.MapTurnRight()
  74.        m.MapTurnRight()
  75.     end
  76.  
  77.  
  78.     for z = 1, Height do
  79.        
  80.         for x = 1, Width do                         -- starting length cuts one turn off, but not one column off. see below
  81.  
  82.             for y = 1, Length - 1 do                -- width cuts one length down off the next
  83.                
  84.                 -- check for fuel
  85.                 local d = DistanceFromLocation(chestDumpLoc) + 5
  86.                 if(not m.CheckFuel(d)) then
  87.                     local savedLoc = m.table_deepcopy(m.coords)
  88.                     NavigateToLocation(chestDumpLoc)
  89.                     while not m.SelectItem("minecraft:coal") do
  90.                         print("Out of fuel ...")
  91.                         print("Place coal in the inventory to continue.")
  92.                         os.sleep(3)
  93.                     end
  94.                     m.CheckFuel(d)
  95.                     NavigateToLocation(savedLoc)
  96.                 end        
  97.  
  98.                 if(m.InventoryFull()) then
  99.                     -- navigate to chestLoc
  100.                     local savedLoc = m.table_deepcopy(m.coords)
  101.                     NavigateToLocation(chestDumpLoc)
  102.                     m.DumpItemsExistingChest()
  103.                     NavigateToLocation(savedLoc)
  104.                 end
  105.                
  106.                 m.DetectAndDig()
  107.                 m.MapForward()
  108.                
  109.             end
  110.  
  111.             if(Width - x ~= 0) then -- Technically on the last column, we want to continue forward so we can't do 'Width - 1', but don't turn into the next column
  112.                 if(lastTurn == "Right") then -- if we're on an even column, turn left
  113.                     m.MapTurnRightDig()
  114.                     lastTurn = "Left"
  115.                 else -- odd columns (start is 1 btw) turn right!
  116.                     m.MapTurnLeftDig()
  117.                     lastTurn = "Right"
  118.                 end
  119.             end
  120.  
  121.         end
  122.  
  123.         if(Height - z ~= 0) then
  124.             turtle.digDown()
  125.             m.MapDown()
  126.             m.MapTurnLeft()
  127.             m.MapTurnLeft()
  128.         end
  129.        
  130.     end
  131.  
  132.     return 1
  133. end
  134.  
  135. function Main()
  136.     local response
  137.     print("Welcome to the Minecraft file building program")
  138.     print("What length do you want to excavate?")
  139.  
  140.     -- Gets Y Input from user
  141.     print("Length?")
  142.     Length = tonumber(io.read())
  143.     while Length == nil do
  144.         print("oops! enter a number!")
  145.         Length = tonumber(io.read())
  146.     end
  147.  
  148.  
  149.     -- Gets X Input from user
  150.     print("")
  151.     print("Width?")
  152.     Width = tonumber(io.read())
  153.     while Width == nil do
  154.         print("oops! enter a number!")
  155.         Width = tonumber(io.read())
  156.     end
  157.  
  158.     -- Gets Z Input from user
  159.     print("")
  160.     print("Depth?")
  161.     Height = tonumber(io.read())
  162.     while Height == nil do
  163.         print("oops! enter a number!")
  164.         Height = tonumber(io.read())
  165.     end
  166.  
  167.     print("Mining Started ... ")
  168.     Start()
  169.  
  170.     chestDumpLoc.y = 0
  171.     chestDumpLoc.z = 1
  172.     NavigateToLocation(chestDumpLoc)
  173.     print("Finished!")
  174. end
  175. Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement