Guest User

advancedtunnel

a guest
Oct 22nd, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.09 KB | None | 0 0
  1. -- Advanced Tunnel by Henness
  2. -- Version 1.3.4 1/4/2013
  3.  
  4. -- Config
  5. Version = "v1.3.4"
  6. HeightQuestion = false
  7. height = 2
  8. WidthQuestion = false
  9. width = 2
  10. lenghth = 15
  11. LengthQuestion = false
  12. TorchesQuestion = false
  13.  
  14. -- Functions
  15. function version() -- Version function
  16.     return Version
  17. end
  18.  
  19. function clearscreen() -- Clearscreen function
  20.     term.clear()
  21.     regwrite( "Advanced Tunnel", 12, 1 )
  22.     regwrite( version(), 2, 12 )
  23.     regwrite( "Designed by Henness", 17, 12 )
  24.     term.setCursorPos( 1, 3 )
  25. end
  26.  
  27. function regwrite(string, columnVar, rowVar) -- Write a string to the cords
  28.     term.setCursorPos( columnVar, rowVar )
  29.     write (string)
  30. end
  31.  
  32. function arcwrite(number, columnVar, rowVar) -- Write a number "right to left" to the cords
  33.     digits = math.ceil(math.log10(number))
  34.     if 10^digits == number then
  35.         digits = digits + 1
  36.     end
  37.     term.setCursorPos( columnVar, rowVar )
  38.     while digits > 1 do
  39.         digits = digits - 1
  40.         columnVar = columnVar - 1
  41.         term.setCursorPos( columnVar, rowVar )
  42.     end
  43.     write (number)
  44. end
  45.  
  46. function createtable() -- Make a table function
  47. clearscreen()
  48. print ( [[
  49.  +--------------------------------+
  50.  |Blocks Mined:                 0 |
  51.  |Torches Placed:               0 |
  52.  +--------------------------------+
  53.  |             Status             |
  54.  |[                              ]|
  55.  |                0%              |
  56.  +--------------------------------+
  57. ]] )
  58. placedValue = 0
  59. minedValue = 0
  60. end
  61.  
  62. function updatetable() -- Update the table
  63.     -- Update Blocks Mined
  64.     minedValue = minedValue + 1
  65.     arcwrite(minedValue, 33, 4)
  66.    
  67.     -- Update Percentage
  68.     if length > 0 then
  69.         area = (height * width * length)
  70.     else
  71.         area = minedValue
  72.     end
  73.     percentage = math.ceil((minedValue / area) * 100)
  74.     arcwrite(percentage, 19, 9)
  75.    
  76.     -- Update Percentage Bar
  77.     percentageBar = math.floor(percentage * 0.3)
  78.     columnVar = 4
  79.     while percentageBar > 0 do
  80.         regwrite( "=", columnVar, 8 )
  81.         percentageBar = percentageBar - 1
  82.         columnVar = columnVar + 1
  83.     end
  84. end
  85.  
  86. function placetorch() -- Place a torch
  87.     if torches == true then
  88.         if lengthSpacingVar == 0 and ( spaceFromWallVar == 0 or widthSpacingVar == 0 ) then
  89.             turtle.placeDown()
  90.             if turtle.getItemCount(1) == 0 then
  91.                 noTorches = true
  92.             end
  93.             spaceFromWallVar = -1
  94.             widthSpacingVar = widthSpacing
  95.             -- Update Torches Placed
  96.             placedValue = placedValue + 1
  97.             arcwrite(placedValue, 33, 5)
  98.         else
  99.             spaceFromWallVar = spaceFromWallVar - 1
  100.             widthSpacingVar = widthSpacingVar - 1
  101.         end
  102.     end
  103. end
  104.  
  105. function widthtest() -- Test width for numbers that = 4+10n
  106.     widthTestVar = width
  107.     while widthTestVar > 4 do
  108.         widthTestVar = widthTestVar - 10
  109.     end
  110.     if widthTestVar == 4 then
  111.         return true
  112.     end
  113. end
  114.  
  115. function setspacing() -- Calculate spacing of torches
  116.     if width < 6 then
  117.         if math.fmod( width, 2 ) == 0 then -- If even
  118.             if width == 2 then
  119.                 lengthSpacing = 9
  120.                 spaceFromWall = ( 1 + ( -1 )^placedValue ) / 2
  121.             else
  122.                 lengthSpacing = 7
  123.                 spaceFromWall = (( 1 + ( -1 )^placedValue ) / 2 ) + 1
  124.             end
  125.         else -- If odd
  126.             spaceFromWall = math.floor( width / 2 )
  127.             if width == 5 then
  128.                 lengthSpacing = 6
  129.             elseif width == 3 then
  130.                 lengthSpacing = 8
  131.             else
  132.                 lengthSpacing = 10
  133.             end
  134.         end
  135.         widthSpacing = -1
  136.     else
  137.         if math.fmod( width, 2 ) == 0 then -- If even
  138.             widthSpacing = 4
  139.             lengthSpacing = 4
  140.             if widthtest() == true or width == 6 then
  141.                 spaceFromWall = 0          
  142.             else
  143.                 spaceFromWallVar = width
  144.                 while spaceFromWallVar > 7 do
  145.                     spaceFromWallVar = spaceFromWallVar - ( widthSpacing + 1 )
  146.                 end
  147.                 spaceFromWall = ( spaceFromWallVar - 1 ) / 2
  148.             end
  149.         else -- If odd
  150.             widthSpacing = 5
  151.             lengthSpacing = 4
  152.             if width == 7 then
  153.                 spaceFromWall = 0
  154.             else
  155.                 spaceFromWallVar = width
  156.                 while spaceFromWallVar > 7 do
  157.                     spaceFromWallVar = spaceFromWallVar - ( widthSpacing + 1 )
  158.                 end
  159.                 spaceFromWall = ( spaceFromWallVar - 1 ) / 2
  160.             end
  161.         end
  162.     end
  163.     spaceFromWallVar = spaceFromWall
  164.     widthSpacingVar = widthSpacing
  165.     lengthSpacingVar = lengthSpacing
  166. end
  167.  
  168. function digdown() -- Dig down and update the table
  169.     if turtle.detectDown() then
  170.         turtle.digDown()
  171.     end
  172.     updatetable()
  173. end
  174.  
  175. function digup() -- Dig up and update the table
  176.     while turtle.detectUp() do
  177.         turtle.digUp()
  178.         sleep(0.5)
  179.     end
  180.     updatetable()
  181. end
  182.  
  183. function digforward() -- Dig forward and update the table
  184.     local moved = moveforward()
  185.     if not moved and turtle.detect() then
  186.         repeat turtle.dig() until moveforward()
  187.         local moved = true
  188.     elseif not moved and not turtle.detect() then
  189.         repeat turtle.attack() until moveforward()
  190.     end
  191.     updatetable()
  192. end
  193.  
  194. function moveforward() -- Move forward
  195.     return turtle.forward()
  196. end
  197.  
  198. function largetunnel() -- Mine a tunnel larger then one wide
  199.     widthvar = width - 1
  200.     while widthvar > 0 do
  201.         if tunnelheight == 1 then
  202.             digdown()
  203.             placetorch()
  204.             moveforward()
  205.         end
  206.         if tunnelheight == 2 then
  207.             digdown()
  208.             placetorch()
  209.             digforward()
  210.         end
  211.         if tunnelheight == 3 then
  212.             digup()
  213.             digdown()
  214.             placetorch()
  215.             digforward()
  216.         end
  217.         if tunnelheight > 3 then
  218.             digup()
  219.             digdown()
  220.             digforward()
  221.         end
  222.         widthvar = widthvar - 1
  223.     end
  224.     if tunnelheight == 1 then
  225.         digdown()
  226.         tunnelheight = tunnelheight - 1
  227.     elseif tunnelheight == 2 then
  228.         digdown()
  229.         tunnelheight = tunnelheight - 2
  230.     elseif tunnelheight == 3 then
  231.         digup()
  232.         digdown()
  233.         tunnelheight = tunnelheight - 3
  234.     else
  235.         digup()
  236.         digdown()
  237.         tunnelheight = tunnelheight - 3
  238.         if tunnelheight == 1 then
  239.             turtle.down()
  240.         end
  241.         if tunnelheight == 2 then
  242.             turtle.down()
  243.             digdown()
  244.             turtle.down()
  245.         end
  246.         if tunnelheight >= 3 then
  247.             turtle.down()
  248.             digdown()
  249.             turtle.down()
  250.             digdown()
  251.             turtle.down()
  252.         end
  253.     end
  254. end
  255.  
  256. function smalltunnel() -- Mine a one wide tunnel
  257.     if tunnelheight == 2 then
  258.         digdown()
  259.     end
  260.     if tunnelheight == 3 then
  261.         digup()
  262.         digdown()
  263.     end
  264.     if tunnelheight > 3 then
  265.         digup()
  266.         digdown()
  267.         turtle.down()
  268.         tunnelheight = tunnelheight - 3
  269.         while tunnelheight > 1 do
  270.             digdown()
  271.             turtle.down()
  272.             tunnelheight = tunnelheight - 1
  273.         end
  274.         digdown()
  275.     end
  276.     placetorch()
  277. end
  278.  
  279. -- Questions
  280. clearscreen()
  281. while HeightQuestion == true do -- Height Question
  282.     print("Height of tunnel?")
  283.     height = tonumber(read())
  284.     clearscreen()
  285.     if height == nil then
  286.         print( "Please answer with a number." )
  287.     elseif height >= 2 then
  288.         HeightQuestion = false
  289.         startheight = height - 3
  290.     elseif height == 1 then
  291.         print( "The tunnel height must be larger than one." )
  292.     elseif height == 0 then
  293.         print( "The tunnel height can't be infinite." )
  294.     else
  295.         print( "The tunnel height must be positive." )
  296.     end
  297. end
  298.  
  299. clearscreen()
  300. while WidthQuestion == true do -- Width Question
  301.     print("Width of tunnel?")
  302.     width = tonumber(read())
  303.     clearscreen()
  304.     if width == nil then
  305.         print( "Please answer with a number." )
  306.     elseif width > 0 then
  307.         WidthQuestion = false
  308.     elseif width == 0 then
  309.         print( "The tunnel width can't be infinite." )
  310.     else
  311.         print( "The tunnel width must be positive." )
  312.     end
  313. end
  314.  
  315. clearscreen()
  316. while LengthQuestion == true do -- Length Question
  317.     print("Length of tunnel?")
  318.     length = tonumber(read())
  319.     clearscreen()
  320.     if length == nil then
  321.         print( "Please answer with a number." )
  322.     elseif length > 0 then
  323.         LengthQuestion = false
  324.         lengthVar = 0  
  325.     elseif length == 0 then
  326.         LengthQuestion = false
  327.         Infinitelength = true
  328.         TorchesQuestion = false
  329.         TorchSpacingQuestion = false
  330.         lengthVar = (-1)
  331.     else
  332.         print( "The tunnel length must be positive." )
  333.     end
  334. end
  335.  
  336. clearscreen()
  337. while TorchesQuestion == true do -- Torch Question
  338.     print("Place torches?")
  339.     light = string.lower(read())
  340.     clearscreen()
  341.     if light == ( 'yes' ) then
  342.         if turtle.getItemCount(1) == 0 then
  343.             print("Please place torches in the first inventory slot.")
  344.         else
  345.         torches = true
  346.         TorchesQuestion = false
  347.         noTorches = false
  348.         setspacing()
  349.         lengthSpacingVar = 0
  350.         end
  351.     elseif light == ( 'no' ) then
  352.         torches = false
  353.         TorchesQuestion = false
  354.     else
  355.         print("Please answer yes or no.")
  356.     end
  357. end
  358.  
  359. -- Create the gui
  360. createtable()
  361.  
  362. -- Mining Loop
  363. turtle.up()
  364. while startheight > 0 do
  365.     local moved = turtle.up()
  366.     if not moved and turtle.detectUp() then
  367.         repeat turtle.digUp() until turtle.up()
  368.         local moved = true
  369.     elseif not moved and not turtle.detectUp() then
  370.         repeat turtle.attackUp() until turtle.up()
  371.     end
  372.     startheight = startheight - 1
  373. end
  374. while lengthVar < length do
  375.     lengthVar = lengthVar + 1
  376.     digforward()
  377.     if width > 1 then
  378.         turtle.turnRight()
  379.     end
  380.  
  381.     -- Mine a one wide tunnel or a larger tunnel.
  382.     tunnelheight = height
  383.     if width > 1 then
  384.         while tunnelheight > 0 do
  385.             largetunnel()
  386.             turtle.turnRight()
  387.             turtle.turnRight()
  388.             if tunnelheight > 0 then
  389.                 largetunnel()
  390.                 if tunnelheight > 0 then
  391.                     turtle.turnRight()
  392.                     turtle.turnRight()
  393.                 end
  394.             else
  395.                 widthvar = width - 1
  396.                 while widthvar > 0 do
  397.                     moveforward()
  398.                     widthvar = widthvar - 1
  399.                 end
  400.             end
  401.         end
  402.     else
  403.         smalltunnel()
  404.     end
  405.     if torches == true then
  406.         if lengthSpacingVar == 0 then
  407.             spaceFromWallVar = spaceFromWall
  408.             widthSpacingVar = widthSpacing
  409.             lengthSpacingVar = lengthSpacing
  410.         else
  411.             spaceFromWallVar = spaceFromWall
  412.             widthSpacingVar = widthSpacing
  413.             lengthSpacingVar = lengthSpacingVar - 1
  414.         end
  415.     end
  416.     startheight = height - 3
  417.     while startheight > 0 do
  418.         turtle.up()
  419.         startheight = startheight - 1
  420.     end
  421.  
  422.     -- Infinite Length
  423.     if Infinitelength == true then
  424.         lengthVar = lengthVar - 1
  425.     end
  426.  
  427.     -- Stop or Continue
  428.     if lengthVar == length or noTorches == true then
  429.         if width > 1 then
  430.             turtle.turnLeft()
  431.         else
  432.             turtle.turnRight()
  433.             turtle.turnRight()
  434.         end
  435.         while lengthVar > 0 do
  436.             moveforward()
  437.             lengthVar = lengthVar - 1
  438.         end
  439.         while startheight < ( height - 3 ) do
  440.             turtle.down()
  441.             startheight = startheight + 1
  442.         end
  443.         turtle.turnLeft()
  444.         turtle.turnLeft()
  445.         lengthVar = length
  446.         term.setCursorPos( 1, 11 )
  447.         if noTorches == true then
  448.             print ("Out of Torches!")
  449.         else
  450.             print ("Tunnel Complete!")
  451.         end
  452.         term.setCursorPos( 1, 2 )
  453.     else
  454.         if width > 1 then
  455.             turtle.turnRight()
  456.         end
  457.     end
  458. end
Advertisement
Add Comment
Please, Sign In to add comment