KRIN3X

Krin3x's variable dimension tunneler

Mar 17th, 2022 (edited)
2,890
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.44 KB | None | 1 0
  1. local args = { ... }
  2.  
  3. if #args ~= 3 then
  4.     print("Usage: <scriptname> <width> <height> <length>")
  5.     return
  6. end
  7.  
  8. local maxWidth = args[1]
  9. local maxHeight = args[2]
  10. local maxLength = args[3]
  11.  
  12. local height = 0
  13. local unloaded = 0
  14. local collected = 0
  15.  
  16. local xPos,zPos = 0,0
  17. local xDir,zDir = 0,1
  18.  
  19. local goTo -- Filled in further down
  20. local refuel -- Filled in further down
  21.  
  22. shell.run("pastebin get BgrVUJyh kapi")
  23. os.loadAPI("kapi")
  24.  
  25. local function unload( _bKeepOneFuelStack )
  26.     print( "Unloading items..." )
  27.     for n=1,16 do
  28.         local nCount = turtle.getItemCount(n)
  29.         if nCount > 0 then
  30.             turtle.select(n)            
  31.             local bDrop = true
  32.             if _bKeepOneFuelStack and turtle.refuel(0) then
  33.                 bDrop = false
  34.                 _bKeepOneFuelStack = false
  35.             end        
  36.             if bDrop then
  37.                 turtle.drop()
  38.                 unloaded = unloaded + nCount
  39.             end
  40.         end
  41.     end
  42.     collected = 0
  43.     turtle.select(1)
  44. end
  45.  
  46. local function returnSupplies()
  47.     local x,y,z,xd,zd = xPos,height,zPos,xDir,zDir
  48.     print( "Returning to surface..." )
  49.     goTo( 0,0,0,0,-1 )
  50.    
  51.     local fuelNeeded = 2*(x+y+z) + 1
  52.     if not refuel( fuelNeeded ) then
  53.         unload( true )
  54.         print( "Waiting for fuel" )
  55.         while not refuel( fuelNeeded ) do
  56.             os.pullEvent( "turtle_inventory" )
  57.         end
  58.     else
  59.         unload( true )  
  60.     end
  61.    
  62.     print( "Resuming mining..." )
  63.     goTo( x,y,z,xd,zd )
  64. end
  65.  
  66. local function collect()    
  67.     local bFull = true
  68.     local nTotalItems = 0
  69.     for n=1,16 do
  70.         local nCount = turtle.getItemCount(n)
  71.         if nCount == 0 then
  72.             bFull = false
  73.         end
  74.         nTotalItems = nTotalItems + nCount
  75.     end
  76.    
  77.     if nTotalItems > collected then
  78.         collected = nTotalItems
  79.         if math.fmod(collected + unloaded, 50) == 0 then
  80.             kapi.notify( "Mined "..(collected + unloaded).." items." )
  81.         end
  82.     end
  83.    
  84.     if bFull then
  85.         kapi.notify("No empty slots left.")
  86.         returnSupplies()
  87.         kapi.notify("returning supplies.")
  88.         return false
  89.     end
  90.     return true
  91. end
  92.  
  93. function refuel( ammount )
  94.     local fuelLevel = turtle.getFuelLevel()
  95.     if fuelLevel == "unlimited" then
  96.         return true
  97.     end
  98.    
  99.     local needed = ammount or (maxWidth - xPos + maxLength - zPos + maxHeight - height + 2)
  100.     kapi.notify("fuel: ".. turtle.getFuelLevel())
  101.     if turtle.getFuelLevel() < needed then
  102.         kapi.notify("refueling: "..needed)
  103.         local fueled = false
  104.         for n=1,16 do
  105.             if turtle.getItemCount(n) > 0 then
  106.                 turtle.select(n)
  107.                 if turtle.refuel(1) then
  108.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  109.                         turtle.refuel(1)
  110.                     end
  111.                     if turtle.getFuelLevel() >= needed then
  112.                         turtle.select(1)
  113.                         return true
  114.                     end
  115.                 end
  116.             end
  117.         end
  118.         turtle.select(1)
  119.         return false
  120.     end
  121.    
  122.     return true
  123. end
  124.  
  125. local function tryForwards()
  126.     if not refuel() then
  127.         print( "Not enough Fuel" )
  128.         returnSupplies()
  129.     end
  130.    
  131.     while not turtle.forward() do
  132.         if turtle.detect() then
  133.             if turtle.dig() then
  134.                 if not collect() then
  135.                     returnSupplies()
  136.                 end
  137.             else
  138.                 return false
  139.             end
  140.         elseif turtle.attack() then
  141.             if not collect() then
  142.                 returnSupplies()
  143.             end
  144.         else
  145.             sleep( 0.5 )
  146.         end
  147.     end
  148.    
  149.     xPos = xPos + xDir
  150.     zPos = zPos + zDir
  151.     return true
  152. end
  153.  
  154. local function tryDown()
  155.     if not refuel() then
  156.         kapi.notify( "Not enough Fuel" )
  157.         returnSupplies()
  158.     end
  159.    
  160.     while not turtle.down() do
  161.         if turtle.detectDown() then
  162.             if turtle.digDown() then
  163.                 if not collect() then
  164.                     returnSupplies()
  165.                 end
  166.             else
  167.                 return false
  168.             end
  169.         elseif turtle.attackDown() then
  170.             if not collect() then
  171.                 returnSupplies()
  172.             end
  173.         else
  174.             sleep( 0.5 )
  175.         end
  176.     end
  177.  
  178.     height = height - 1
  179.     if math.fmod( height, 10 ) == 0 then
  180.         print( "Descended "..height.." metres." )
  181.     end
  182.  
  183.     return true
  184. end
  185.  
  186. local function tryUp()
  187.     if not refuel() then
  188.         kapi.notify( "Not enough Fuel" )
  189.         returnSupplies()
  190.     end
  191.    
  192.     while not turtle.up() do
  193.         if turtle.detectUp() then
  194.             if turtle.digUp() then
  195.                 if not collect() then
  196.                     returnSupplies()
  197.                 end
  198.             else
  199.                 return false
  200.             end
  201.         elseif turtle.attackUp() then
  202.             if not collect() then
  203.                 returnSupplies()
  204.             end
  205.         else
  206.             sleep( 0.5 )
  207.         end
  208.     end
  209.  
  210.     height = height + 1
  211.     if math.fmod( height, 10 ) == 0 then
  212.         print( "Ascended "..height.." metres." )
  213.     end
  214.  
  215.     return true
  216. end
  217.  
  218. local function turnLeft()
  219.     turtle.turnLeft()
  220.     xDir, zDir = -zDir, xDir
  221. end
  222.  
  223. local function turnRight()
  224.     turtle.turnRight()
  225.     xDir, zDir = zDir, -xDir
  226. end
  227.  
  228. function goTo( x, y, z, xd, zd )  
  229.     while height - y ~= 0 do
  230.         if height < y then
  231.             if turtle.up() then
  232.                 height = height + 1
  233.             elseif turtle.digUp() or turtle.attackUp() then
  234.                 collect()
  235.             else
  236.                 sleep( 0.5 )
  237.             end
  238.          elseif height > y then
  239.             if turtle.down() then
  240.                 height = height - 1
  241.             elseif turtle.digDown() or turtle.attackDown() then
  242.                 collect()
  243.             else
  244.                 sleep( 0.5 )
  245.             end
  246.         end
  247.     end
  248.  
  249.     if xPos > x then
  250.         while xDir ~= -1 do
  251.             turnLeft()
  252.         end
  253.         while xPos > x do
  254.             if turtle.forward() then
  255.                 xPos = xPos - 1
  256.             elseif turtle.dig() or turtle.attack() then
  257.                 collect()
  258.             else
  259.                 sleep( 0.5 )
  260.             end
  261.         end
  262.     elseif xPos < x then
  263.         while xDir ~= 1 do
  264.             turnLeft()
  265.         end
  266.         while xPos < x do
  267.             if turtle.forward() then
  268.                 xPos = xPos + 1
  269.             elseif turtle.dig() or turtle.attack() then
  270.                 collect()
  271.             else
  272.                 sleep( 0.5 )
  273.             end
  274.         end
  275.     end
  276.    
  277.     if zPos > z then
  278.         while zDir ~= -1 do
  279.             turnLeft()
  280.         end
  281.         while zPos > z do
  282.             if turtle.forward() then
  283.                 zPos = zPos - 1
  284.             elseif turtle.dig() or turtle.attack() then
  285.                 collect()
  286.             else
  287.                 sleep( 0.5 )
  288.             end
  289.         end
  290.     elseif zPos < z then
  291.         while zDir ~= 1 do
  292.             turnLeft()
  293.         end
  294.         while zPos < z do
  295.             if turtle.forward() then
  296.                 zPos = zPos + 1
  297.             elseif turtle.dig() or turtle.attack() then
  298.                 collect()
  299.             else
  300.                 sleep( 0.5 )
  301.             end
  302.         end
  303.     end
  304.    
  305.     while zDir ~= zd or xDir ~= xd do
  306.         turnLeft()
  307.     end
  308. end
  309.  
  310. if not refuel() then
  311.     kapi.notify( "Out of Fuel" )
  312.     return
  313. end
  314.  
  315. print( "Tunneling..." )
  316.  
  317. local Yalternate = false
  318. local Xalternate = false
  319. local W,H = 0,0
  320.  
  321. for l=1, maxLength do
  322.     kapi.notify("distance: "..l .."/"..maxLength)
  323.     kapi.notify("Around ".. (maxWidth*maxHeight*maxLength) - (maxWidth*zPos*maxHeight).. " blocks left to dig")
  324.     for w=0, maxWidth-1 do        
  325.         for h=0, maxHeight-1 do
  326.             if Yalternate then
  327.                 H = maxHeight-1-h
  328.             else
  329.                 H = h
  330.             end        
  331.             if Xalternate then
  332.                 W = maxWidth-1-w
  333.             else
  334.                 W = w
  335.             end
  336.             goTo(W,H,l,xDir,zDir)
  337.         end
  338.         Yalternate = not Yalternate
  339.         refuel()
  340.     end
  341.     Xalternate = not Xalternate  
  342. end
  343. print( "Mined "..(collected + unloaded).." items total." )
Add Comment
Please, Sign In to add comment