zkb1325

Minecraft Computer Craft Advanced Tunnel

Feb 7th, 2024 (edited)
1,420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.58 KB | None | 0 0
  1. shell.run("clear")
  2.  
  3. while tunnelLength == nil do
  4.     term.setCursorPos(1, 1)
  5.     term.clearLine()
  6.     term.write("Tunnel Length: ")
  7.     tunnelLength = tonumber(read())
  8. end
  9.  
  10. while tunnelWidth == nil do
  11.     term.setCursorPos(1, 2)
  12.     term.clearLine()
  13.     term.write("Tunnel Width: ")
  14.     tunnelWidth = tonumber(read())
  15. end
  16.  
  17. while tunnelHeight == nil do
  18.     term.setCursorPos(1, 3)
  19.     term.clearLine()
  20.     term.write("Tunnel Height: ")
  21.     tunnelHeight = tonumber(read())
  22. end
  23.  
  24. while skippedRows == nil do
  25.     term.setCursorPos(1, 4)
  26.     term.clearLine()
  27.     term.write("Skip Rows: ")
  28.     skippedRows = tonumber(read())
  29. end
  30.  
  31. if tunnelHeight > 1 then
  32.     while lightSpacing == nil do
  33.         term.setCursorPos(1, 5)
  34.         term.clearLine()
  35.         term.write("Light spacing (0 for no lights): ")
  36.         lightSpacing = tonumber(read())
  37.         if lightSpacing == 1 then
  38.             print(" - Must be greater then 1")
  39.             lightSpacing = nil
  40.         end
  41.     end
  42. else
  43.     lightSpacing = 0
  44. end
  45.  
  46. local totalArea = tunnelWidth*tunnelLength*tunnelHeight
  47. local numberOfInventories = math.ceil(totalArea/64/11)
  48. local estimatedFuelUsage = totalArea + (tunnelLength*numberOfInventories) + (skippedRows*numberOfInventories)
  49. if tunnelHeight%2 == 0 then
  50.     estimatedFuelUsage = estimatedFuelUsage + (tunnelWidth*tunnelLength)
  51. end
  52. estimatedFuelUsage = math.ceil(estimatedFuelUsage)
  53.  
  54. local startingFuel = turtle.getFuelLevel()
  55.  
  56. term.clear()
  57. term.setCursorPos(1,1)
  58. print(""..tunnelLength.." blocks long, "..tunnelWidth.." blocks wide, and "..tunnelHeight.." blocks tall")
  59. if lightSpacing == 0 then
  60.     print("No lights will be placed")
  61. else
  62.     print("A light will be placed from slot 16 every "..lightSpacing.." blocks, place a chest to the right to refill")
  63. end
  64. print("")
  65. print("Current fuel: "..startingFuel)
  66. print("Estimated fuel usage: "..estimatedFuelUsage)
  67. if startingFuel < estimatedFuelUsage then
  68.     print("!!! Turtle may run out of fuel before finishing !!!")
  69. end
  70. print("")
  71. print("Press ENTER to confirm or type <restart> to start over or type <exit> to exit program")
  72. term.write("Confirm: ")
  73. confirmStart = read()
  74. if confirmStart == "restart" then
  75.     shell.exit()
  76.     shell.run(shell.getRunningProgram())
  77.     return
  78. end
  79. if confirmStart == "exit" then
  80.     term.clear()
  81.     term.setCursorPos(1,1)
  82.     return
  83. end
  84. term.clear()
  85. term.setCursorPos(1, 1)
  86.  
  87. local blocksBroken = 0
  88. local moves = 0
  89. local movesToChest = 0
  90. local reverseMoves = 0
  91. local extraMoves = ((tunnelHeight-1)*tunnelLength)+tunnelLength-1
  92. local currentTunnelLength = 1
  93. local turtlePosX = 1
  94. local turtlePosY = 1
  95. local turtlePosZ = 0
  96. local notification = ""
  97. local startTime = os.epoch("utc")/1000
  98.  
  99. function digWidth(w)
  100.     for u = 1, w-1 do
  101.         if shouldPlaceLight() then
  102.             placeLight()
  103.         end
  104.         while turtle.dig() == true do
  105.             blocksBroken = blocksBroken + 1
  106.             sleep(0.6)
  107.         end
  108.         if turtle.forward() == false then
  109.             turtle.dig()
  110.             blocksBroken = blocksBroken + 1
  111.         else
  112.             moves = moves + 1
  113.         end
  114.  
  115.         if turtlePosY%2 == 1 then
  116.             turtlePosX = turtlePosX + 1
  117.         else
  118.             turtlePosX = turtlePosX - 1
  119.         end
  120.  
  121.         printTurtleStats()
  122.     end
  123. end
  124.  
  125. function reverse(w)
  126.     for u = 1, w-1 do
  127.         turtle.back()
  128.         moves = moves + 1
  129.         reverseMoves = reverseMoves + 1
  130.         turtlePosX = turtlePosX - 1
  131.         printTurtleStats()
  132.     end
  133. end
  134.  
  135. function prepForNextRow()
  136.     while turtle.digUp() == true do
  137.         blocksBroken = blocksBroken + 1
  138.         sleep(0.6)
  139.     end
  140.     if turtle.up() == false then
  141.         turtle.digUp()
  142.         blocksBroken = blocksBroken + 1
  143.     else
  144.         moves = moves + 1
  145.     end
  146.  
  147.     turtlePosY = turtlePosY + 1
  148.     turtle.turnLeft()
  149.     turtle.turnLeft()
  150. end
  151.  
  152. function digOutNextSection()
  153.     while turtle.dig() == true do
  154.         blocksBroken = blocksBroken + 1
  155.         sleep(0.6)
  156.     end
  157.     if turtle.forward() == false then
  158.         turtle.dig()
  159.         blocksBroken = blocksBroken + 1
  160.     else
  161.         moves = moves + 1
  162.     end
  163.  
  164.     turtlePosZ = turtlePosZ + 1
  165.     printTurtleStats()
  166.  
  167.     turtle.turnRight()
  168.     for h = 1, tunnelHeight do
  169.         notification = "Digging width"
  170.         digWidth(tunnelWidth)
  171.         if h < tunnelHeight then
  172.             prepForNextRow()
  173.         end
  174.     end
  175.     if turtlePosY%2 == 1 then
  176.         notification = "Reversing"
  177.         reverse(tunnelWidth)
  178.         turtle.turnLeft()
  179.     else
  180.         turtle.turnRight()
  181.     end
  182.     for h = 1, tunnelHeight-1 do
  183.         while turtle.digDown() == true do
  184.             blocksBroken = blocksBroken + 1
  185.             sleep(0.6)
  186.         end
  187.         if turtle.down() == false then
  188.             turtle.digDown()
  189.             blocksBroken = blocksBroken + 1
  190.         else
  191.             reverseMoves = reverseMoves + 1
  192.             moves = moves + 1
  193.         end
  194.         turtlePosY = turtlePosY - 1
  195.         printTurtleStats()
  196.     end
  197. end
  198.  
  199. function moveTurtleToChest()
  200.     turtle.turnRight()
  201.     turtle.turnRight()
  202.     for i = 1, currentTunnelLength do
  203.         turtle.forward()
  204.         moves = moves + 1
  205.         movesToChest = movesToChest + 1
  206.         turtlePosZ = turtlePosZ - 1
  207.         printTurtleStats()
  208.     end
  209. end
  210.  
  211. function moveTurtleBackToFront()
  212.     turtle.turnLeft()
  213.     turtle.turnLeft()
  214.     for i = 1, currentTunnelLength do
  215.         turtle.forward()
  216.         moves = moves + 1
  217.         movesToChest = movesToChest + 1
  218.         turtlePosZ = turtlePosZ + 1
  219.         printTurtleStats()
  220.     end
  221. end
  222.  
  223. function emptyTurtleToChest()
  224.     for i = 1, 15 do
  225.         turtle.select(i)
  226.         turtle.drop()
  227.     end
  228.     turtle.select(1)
  229. end
  230.  
  231. function turtleShouldEmpty()
  232.     local occupiedSlots = 0
  233.     for i = 1, 16 do
  234.         if turtle.getItemCount(i) > 0 then
  235.             occupiedSlots = occupiedSlots + 1
  236.         end
  237.         i = i + 1
  238.     end
  239.     return 16 - occupiedSlots <= 4
  240. end
  241.  
  242. function shouldPlaceLight()
  243.     if turtlePosX%lightSpacing == 0 and turtlePosZ%lightSpacing == 0 and turtlePosY == 2 then
  244.         return true
  245.     else
  246.         return false
  247.     end
  248. end
  249.  
  250. function hasLights()
  251.     return turtle.getItemCount(16) > 0
  252. end
  253.  
  254. function placeLight()
  255.     if not hasLights() then return end
  256.     turtle.select(16)
  257.     turtle.placeDown()
  258.     turtle.select(1)
  259. end
  260.  
  261. function lightChestAtStart()
  262.     i,cd = turtle.inspect()
  263.     return cd["tags"]["forge:chests"] == true
  264. end
  265.  
  266. function takeLights()
  267.     turtle.select(16)
  268.     turtle.suck(turtle.getItemSpace())
  269.     turtle.select(1)
  270. end
  271.  
  272. function disp_time(time)
  273.     local days = math.floor(time/86400)
  274.     local hours = math.floor(math.mod(time, 86400)/3600)
  275.     local minutes = math.floor(math.mod(time,3600)/60)
  276.     local seconds = math.floor(math.mod(time,60))
  277.     return string.format("%d:%02d:%02d:%02d",days,hours,minutes,seconds)
  278. end
  279.  
  280. function getFuelUsed()
  281.     return startingFuel - turtle.getFuelLevel()
  282. end
  283.  
  284. function printTurtleStats()
  285.     term.clear()
  286.     term.setCursorPos(1, 1)
  287.     local percent = math.ceil((moves-movesToChest-reverseMoves)/totalArea*100)
  288.     local checkTime = os.epoch("utc")/1000
  289.     print("Time Elapsed: "..disp_time(checkTime-startTime))
  290.     print("Progress "..percent.."% complete ("..(moves-movesToChest-reverseMoves).."/"..totalArea..")")
  291.     print("Fuel used: "..getFuelUsed().."/"..estimatedFuelUsage)
  292.     print("Remaining estimated fuel usage: "..estimatedFuelUsage-getFuelUsed())
  293.     print("Fuel remaining: "..turtle.getFuelLevel())
  294.     print("X: "..turtlePosX)
  295.     print("Y: "..turtlePosY)
  296.     print("Z: "..turtlePosZ)
  297.     print(notification)
  298. end
  299.  
  300. for tl = 1, tunnelLength+skippedRows do
  301.     if currentTunnelLength > skippedRows then
  302.         digOutNextSection()
  303.         printTurtleStats()
  304.  
  305.         if turtleShouldEmpty() and currentTunnelLength ~= tunnelLength then
  306.             notification = "Going to empty inventory"
  307.             moveTurtleToChest()
  308.             emptyTurtleToChest()
  309.             if lightSpacing > 0 then
  310.                 turtle.turnLeft()
  311.                 if lightChestAtStart() then
  312.                     takeLights()
  313.                 end
  314.                 turtle.turnRight()
  315.             end
  316.             notification = "Moving back from chest"
  317.             moveTurtleBackToFront()
  318.         end
  319.     else
  320.         turtle.forward()
  321.         turtlePosZ = turtlePosZ + 1
  322.     end
  323.     currentTunnelLength = currentTunnelLength + 1
  324. end
  325. currentTunnelLength = currentTunnelLength - 1
  326. moveTurtleToChest()
  327. printTurtleStats()
  328. emptyTurtleToChest()
  329. turtle.turnLeft()
  330. turtle.turnLeft()
  331.  
Advertisement
Add Comment
Please, Sign In to add comment