Advertisement
zkb1325

Minecraft Computer Craft Advanced Tunnel

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