cobra_tomtrein

Untitled

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