Guest User

stripmine

a guest
May 4th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 18.41 KB | None | 0 0
  1. local function refuel()
  2.     local fuelLevel = turtle.getFuelLevel()
  3.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  4.         return
  5.     end
  6.  
  7.     local function tryRefuel()
  8.         for n=1,16 do
  9.             if turtle.getItemCount(n) > 0 then
  10.                 turtle.select(n)
  11.                 if turtle.refuel(1) then
  12.                     turtle.select(1)
  13.                     return true
  14.                 end
  15.             end
  16.         end
  17.         turtle.select(1)
  18.         return false
  19.     end
  20.  
  21.     if not tryRefuel() then
  22.         print( "Add more fuel to continue." )
  23.         while not tryRefuel() do
  24.             sleep(1)
  25.         end
  26.         print( "Resuming branch mine..." )
  27.     end
  28. end
  29.  
  30. local function tryDig()
  31.     while turtle.detect() do
  32.         if turtle.dig() then
  33.             sleep(0.5)
  34.         else
  35.             return false
  36.         end
  37.     end
  38.     return true
  39. end
  40.  
  41. local function tryDigUp()
  42.     while turtle.detectUp() do
  43.         if turtle.digUp() then
  44.             sleep(0.5)
  45.         else
  46.             return false
  47.         end
  48.     end
  49.     return true
  50. end
  51.  
  52. local function tryDigDown()
  53.     while turtle.detectDown() do
  54.         if turtle.digDown() then
  55.             sleep(0.5)
  56.         else
  57.             return false
  58.         end
  59.     end
  60.     return true
  61. end
  62.  
  63. local function tryUp()
  64.     refuel()
  65.     while not turtle.up() do
  66.         if turtle.detectUp() then
  67.             if not tryDigUp() then
  68.                 return false
  69.             end
  70.         elseif turtle.attackUp() then
  71.         else
  72.             sleep( 0.5 )
  73.         end
  74.     end
  75.     return true
  76. end
  77.  
  78. local function tryDown()
  79.     refuel()
  80.     while not turtle.down() do
  81.         if turtle.detectDown() then
  82.             if not tryDigDown() then
  83.                 return false
  84.             end
  85.         elseif turtle.attackDown() then
  86.         else
  87.             sleep( 0.5 )
  88.         end
  89.     end
  90.     return true
  91. end
  92.  
  93. local function tryForward()
  94.     refuel()
  95.     while not turtle.forward() do
  96.         if turtle.detect() then
  97.             if not tryDig() then
  98.                 return false
  99.             end
  100.         elseif turtle.attack() then
  101.         else
  102.             sleep( 0.5 )
  103.         end
  104.     end
  105.     return true
  106. end
  107.  
  108. local function tryBuildPath(left,right)
  109.     refuel()
  110.     turtle.select(2)
  111.     while not turtle.detectDown() do
  112.         if turtle.placeDown() then
  113.         elseif turtle.attackDown() then
  114.         else
  115.             sleep( 0.5 )
  116.         end
  117.     end
  118.     if left then
  119.         turtle.turnLeft()
  120.         while not turtle.detect() do
  121.             if turtle.place() then
  122.             elseif turtle.attack() then
  123.             else
  124.                 sleep( 0.5 )
  125.             end
  126.         end
  127.         turtle.turnRight()
  128.     end
  129.     if right then
  130.         turtle.turnRight()
  131.         while not turtle.detect() do
  132.             if turtle.place() then
  133.             elseif turtle.attack() then
  134.             else
  135.                 sleep( 0.5 )
  136.             end
  137.         end
  138.         turtle.turnLeft()
  139.     end
  140.     return true
  141. end
  142.  
  143.  
  144. local function excavate_height(settings, h, l, return_to_origin, connect_branch_ends, branch_side, wall_left, wall_right)
  145.     -- clear h*l area
  146.     -- tryBuildPath(wall_left, wall_right)
  147.     -- tryDigUp()
  148.     for n=1,l do
  149.         tryForward()
  150.         tryBuildPath(wall_left, wall_right)
  151.         tryDigUp()
  152.     end
  153.     if h == 3 then
  154.         turtle.turnLeft()
  155.         turtle.turnLeft()
  156.         tryUp()
  157.         tryDigUp()
  158.         tryUp()
  159.         for n=1,l do
  160.             tryForward()
  161.         end
  162.         tryDown()
  163.         tryDown()
  164.         turtle.turnRight()
  165.         turtle.turnRight()
  166.     elseif h == 4 then
  167.         turtle.turnLeft()
  168.         turtle.turnLeft()
  169.         tryUp()
  170.         tryDigUp()
  171.         tryUp()
  172.         tryDigUp()
  173.         for n=1,l do
  174.             tryForward()
  175.             tryDigUp()
  176.         end
  177.         tryDown()
  178.         tryDown()
  179.         turtle.turnRight()
  180.         turtle.turnRight()
  181.     else
  182.         if connect_branch_ends then
  183.             if branch_side == "right" then
  184.                 turtle.turnRight()
  185.                 tryDig()
  186.                 turtle.turnLeft()
  187.                 turtle.turnLeft()
  188.                 tryBuildPath(false, true)
  189.                 for i=1,settings["branch_spacing"] do
  190.                     tryForward()
  191.                     tryBuildPath(true, true)
  192.                     tryDigUp()
  193.                 end
  194.                 turtle.turnRight()
  195.                 turtle.turnRight()
  196.                 for i=1,settings["branch_spacing"] do
  197.                     tryForward()
  198.                 end
  199.                 turtle.turnLeft()
  200.             elseif branch_side == "left" then
  201.                 turtle.turnLeft()
  202.                 tryDig()
  203.                 turtle.turnRight()
  204.                 turtle.turnRight()
  205.                 tryBuildPath(true, false)
  206.                 for i=1,settings["branch_spacing"] do
  207.                     tryForward()
  208.                     tryBuildPath(true, true)
  209.                     tryDigUp()
  210.                 end
  211.                 turtle.turnRight()
  212.                 turtle.turnRight()
  213.                 for i=1,settings["branch_spacing"] do
  214.                     tryForward()
  215.                 end
  216.                 turtle.turnRight()
  217.             end
  218.         end
  219.         if return_to_origin then
  220.             turtle.turnRight()
  221.             turtle.turnRight()
  222.             for n=1,l do
  223.                 tryForward()
  224.             end
  225.             turtle.turnLeft()
  226.             turtle.turnLeft()
  227.         end
  228.     end
  229. end
  230.  
  231.  
  232. local function excavate(settings, w, h, l, return_to_origin, connect_branch_ends, branch_side)
  233.     if w == 1 then
  234.         excavate_height(settings, h, l, return_to_origin, connect_branch_ends, branch_side, true, true)
  235.     elseif w == 3 then
  236.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, false, false)
  237.         turtle.turnLeft()
  238.         tryForward()
  239.         turtle.turnRight()
  240.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, true, false)
  241.         turtle.turnRight()
  242.         tryForward()
  243.         tryForward()
  244.         turtle.turnLeft()
  245.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, false, true)
  246.         turtle.turnLeft()
  247.         tryForward()
  248.         turtle.turnRight()
  249.         if not return_to_origin then
  250.             for n=1,l do
  251.                 tryForward()
  252.             end
  253.         end
  254.     elseif w == 5 then
  255.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, false, false)
  256.         turtle.turnLeft()
  257.         tryForward()
  258.         turtle.turnRight()
  259.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, false, false)
  260.         turtle.turnLeft()
  261.         tryForward()
  262.         turtle.turnRight()
  263.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, true, false)
  264.         turtle.turnRight()
  265.         tryForward()
  266.         tryForward()
  267.         tryForward()
  268.         turtle.turnLeft()
  269.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, false, false)
  270.         turtle.turnRight()
  271.         tryForward()
  272.         turtle.turnLeft()
  273.         excavate_height(settings, h, l, true, connect_branch_ends, branch_side, false, true)
  274.         turtle.turnLeft()
  275.         tryForward()
  276.         tryForward()
  277.         turtle.turnRight()
  278.         if not return_to_origin then
  279.             for n=1,l do
  280.                 tryForward()
  281.             end
  282.         end
  283.     end
  284. end
  285.  
  286.  
  287. local function dig_branches(settings)
  288.     print("Beginning to excavate branches")
  289.  
  290.     local num_segments = math.floor(settings["trunk_length"] / (settings["branch_spacing"]+1))
  291.     local return_to_origin = true
  292.  
  293.     for i=1,num_segments do
  294.         print("Currently working on branch pairs " .. i .. "/" .. num_segments)
  295.         tryForward()
  296.         print("Beginning to excavate right branch")
  297.         turtle.turnRight()
  298.         -- skip gap between trunk center and side wall
  299.         if settings["trunk_size"] == "medium" then
  300.             tryForward();
  301.         elseif settings["trunk_size"] == "large" then
  302.             tryForward();
  303.             tryForward();
  304.         end
  305.         excavate(
  306.             settings,
  307.             1,
  308.             2,
  309.             settings["branch_length"],
  310.             return_to_origin,
  311.             settings["connect_branch_ends"],
  312.             "right"
  313.         )
  314.         turtle.turnLeft()
  315.         print("Beginning to excavate left branch")
  316.         turtle.turnLeft()
  317.         -- Skip gap between trunk center and side wall
  318.         -- Include extras to compensate for gap skipping
  319.         -- from the other branch side
  320.         if settings["trunk_size"] == "medium" then
  321.             tryForward();
  322.             tryForward();
  323.         elseif settings["trunk_size"] == "large" then
  324.             tryForward();
  325.             tryForward();
  326.             tryForward();
  327.             tryForward();
  328.         end
  329.         excavate(
  330.             settings,
  331.             1,
  332.             2,
  333.             settings["branch_length"],
  334.             return_to_origin,
  335.             settings["connect_branch_ends"],
  336.             "left"
  337.         )
  338.         turtle.turnRight()
  339.         turtle.turnRight()
  340.         -- Include extras to compensate for gap skipping
  341.         -- from the other branch side
  342.         if settings["trunk_size"] == "medium" then
  343.             tryForward();
  344.         elseif settings["trunk_size"] == "large" then
  345.             tryForward();
  346.             tryForward();
  347.         end
  348.         turtle.turnLeft()
  349.     -- move forward a segment
  350.         for s=1,settings["branch_spacing"] do
  351.             tryForward()
  352.         end
  353.     end
  354.     print("Done excavating branches")
  355. end
  356.  
  357.  
  358. local function dig_trunk_segment(settings)
  359.     print("Beginning trunk segment")
  360.     local trunk_width
  361.     local trunk_height
  362.     if settings["trunk_size"] == "small" then
  363.         trunk_width = 1
  364.         trunk_height = 2
  365.     elseif settings["trunk_size"] == "medium" then
  366.         trunk_width = 3
  367.         trunk_height = 3
  368.     elseif settings["trunk_size"] == "large" then
  369.         trunk_width = 5
  370.         trunk_height = 4
  371.     end
  372.     local return_to_origin, connect_branch_ends, branch_side = false, false, false
  373.     excavate(
  374.         settings,
  375.         trunk_width,
  376.         trunk_height,
  377.         settings["branch_spacing"]+1,
  378.         return_to_origin,
  379.         connect_branch_ends,
  380.         branch_side
  381.     )
  382.     print("End of trunk segment")
  383. end
  384.  
  385.  
  386. local function dig_trunk(settings)
  387.     print("Beginning to excavate main trunk")
  388.  
  389.     local num_segments = math.floor(settings["trunk_length"] / (settings["branch_spacing"]+1))
  390.  
  391. -- Dig trunk segments
  392.     for i=1,num_segments do
  393.         print("Currently working on segment " .. i .. "/" .. num_segments)
  394.         dig_trunk_segment(settings)
  395.     end
  396.     print("Done excavating main trunk.")
  397.     -- return to origin
  398.     turtle.turnLeft()
  399.     turtle.turnLeft()
  400.     for a = 1,num_segments*(settings["branch_spacing"]+1) do
  401.         tryForward()
  402.     end
  403.     turtle.turnLeft()
  404.     turtle.turnLeft()
  405. end
  406.  
  407.  
  408. local function start_digging(settings)
  409.     if settings["dig_main_trunk"] then
  410.         dig_trunk(settings)
  411.     end
  412.     dig_branches(settings)
  413. end
  414.  
  415. --[[ INTERFACE
  416. ================================================================== ]]
  417.  
  418. local
  419.     about_program,
  420.     view_help,
  421.     config_trunk_length,
  422.     config_trunk_size,
  423.     config_trunk,
  424.     config_branch_spacing,
  425.     config_branch_length,
  426.     config_branches,
  427.     view_settings,
  428.     main_menu
  429.  
  430. about_program = function(settings)
  431.     shell.run('clear')
  432.     print("      BRANCH MINE | ABOUT PROGRAM      ")
  433.     print("           v1.0 (2012-12-29)           ")
  434.     print("                                       ")
  435.     print(" Coded by Sandwich (@sandwich_hlp on   ")
  436.     print(" Twitter; sandwichHLP on YouTube)      ")
  437.     print(" Some functions taken from the 'tunnel'")
  438.     print(" program included with ComputerCraft / ")
  439.     print(" Feed The Beast.                       ")
  440.     print("                                       ")
  441.     print("                                       ")
  442.     print("         Press ENTER to continue       ")
  443.     io.read()
  444.     main_menu(settings)
  445. end
  446.  
  447. view_help = function(settings)
  448.     shell.run('clear')
  449.     print("       BRANCH MINE | HELP & TIPS       ")
  450.     print("                                       ")
  451.     print("Place COAL into Slot 1 (charcoal won't ")
  452.     print("get auto-restocked during mining).     ")
  453.     print("Place COBBLESTONE into Slot 2 (used for")
  454.     print("placing down blocks)                   ")
  455.     print("                                       ")
  456.     print("Fill remaining slots with single blocks")
  457.     print("that you want the Turtle to keep, such ")
  458.     print("as iron, gold, and diamonds.           ")
  459.     print("                                       ")
  460.     print("         Press ENTER to continue       ")
  461.     io.read()
  462.     main_menu(settings)
  463. end
  464.  
  465.  
  466. config_trunk_length = function(settings)
  467.     shell.run('clear')
  468.     print(" BRANCH MINE | MAIN TRUNK CONFIGURATION")
  469.     print("                                       ")
  470.     if settings["dig_main_trunk"] == false then
  471.         print("Even though the Main Trunk is currently")
  472.         print("set to NOT be dug out, this value still")
  473.         print("is used to calculate how many branches ")
  474.         print("should be dug.                         ")
  475.         print("                                       ")
  476.     end
  477.     print(" Enter new length and press ENTER:     ")
  478.     local input = io.read()
  479.     input = input + 0 -- typecast to number
  480.     if type(input) == "number" then
  481.         settings["trunk_length"] = input
  482.         config_trunk(settings)
  483.     else
  484.         print("Invalid input - numbers only please")
  485.         textutils.slowPrint(".......................................")
  486.         config_trunk_length(settings)
  487.     end
  488. end
  489.  
  490.  
  491. config_trunk_size = function(settings)
  492.     shell.run('clear')
  493.     print(" BRANCH MINE | MAIN TRUNK CONFIGURATION")
  494.     print("                                       ")
  495.     print(" 1. Small - 2 high x 1 wide            ")
  496.     print(" 2. Medium - 3 high x 3 wide           ")
  497.     print(" 3. Large - 4 high x 5 wide            ")
  498.     print("                                       ")
  499.     print(" Select size [1-3] and press ENTER:  ")
  500.     local input = io.read()
  501.     if (input=="1") then
  502.         settings["trunk_size"] = "small"
  503.         config_trunk(settings)
  504.     elseif (input=="2") then
  505.         settings["trunk_size"] = "medium"
  506.         config_trunk(settings)
  507.     elseif (input=="3") then
  508.         settings["trunk_size"] = "large"
  509.         config_trunk(settings)
  510.     else
  511.         print("Invalid input - enter a number 1-3.")
  512.         textutils.slowPrint(".......................................")
  513.         config_trunk_size(settings)
  514.     end
  515. end
  516.  
  517.  
  518. config_trunk = function(settings)
  519.  
  520.     shell.run('clear')
  521.  
  522.     print(" BRANCH MINE | MAIN TRUNK CONFIGURATION")
  523.     print("                                       ")
  524.     print(" (Current values are in parentheses)   ")
  525.     print("                                       ")
  526.     write("1. Toggle Main Trunk Excavation ")
  527.     if settings["dig_main_trunk"] then write("(Dig)\n") else write("(Skip)\n") end
  528.     print("2. Edit Length (" .. settings["trunk_length"] .. ")")
  529.     write("3. Edit Size ")
  530.     if settings["trunk_size"] == "small" then
  531.         write("(Small - 2h x 1w)\n")
  532.     elseif settings["trunk_size"] == "medium" then
  533.         write("(Medium - 3h x 3w)\n")
  534.     elseif settings["trunk_size"] == "large" then
  535.         write("(Large - 4h x 5w)\n")
  536.     end
  537.     if settings["dig_main_trunk"] == false then
  538.         print(" (affects start points for branches)")
  539.     end
  540.     print("4. Main Menu                           ")
  541.     print("                                       ")
  542.     print(" Type a number [1-4] and press ENTER:  ")
  543.     local input = io.read()
  544.     if input == "1" then
  545.         settings["dig_main_trunk"] = not settings["dig_main_trunk"]
  546.         config_trunk(settings)
  547.     elseif input == "2" then
  548.         config_trunk_length(settings)
  549.     elseif input == "3" then
  550.         config_trunk_size(settings)
  551.     elseif input == "4" then
  552.         main_menu(settings)
  553.     else
  554.         print("Invalid input - enter a number 1-4")
  555.         textutils.slowPrint(".......................................")
  556.         config_trunk(settings)
  557.     end
  558. end
  559.  
  560.  
  561. config_branch_spacing = function(settings)
  562.     shell.run('clear')
  563.     print("  BRANCH MINE | BRANCH CONFIGURATION   ")
  564.     print("                                       ")
  565.     print("This is how many blocks will be between")
  566.     print("each branch. 3 is recommended.         ")
  567.     print("                                       ")
  568.     print("    Enter distance between branches    ")
  569.     print("           and press ENTER:            ")
  570.     local input = io.read()
  571.     input = input + 0 -- typecast to number
  572.     if type(input) == "number" then
  573.         settings["branch_spacing"] = input
  574.         config_branches(settings)
  575.     else
  576.         print("Invalid input - numbers only please")
  577.         textutils.slowPrint(".......................................")
  578.         config_branch_spacing(settings)
  579.     end
  580. end
  581.  
  582.  
  583. config_branch_length = function(settings)
  584.     shell.run('clear')
  585.     print("  BRANCH MINE | BRANCH CONFIGURATION   ")
  586.     print("                                       ")
  587.     print(" How long should each branch be? Please")
  588.     print(" note that this is counted from the    ")
  589.     print(" walls of the main trunk, not from the ")
  590.     print(" center.                               ")
  591.     print("                                       ")
  592.     print("     Enter length and press ENTER:     ")
  593.     local input = io.read()
  594.     input = input + 0 -- typecast to number
  595.     if type(input) == "number" then
  596.         settings["branch_length"] = input
  597.         config_branches(settings)
  598.     else
  599.         print("Invalid input - numbers only please")
  600.         textutils.slowPrint(".......................................")
  601.         config_branch_length(settings)
  602.     end
  603. end
  604.  
  605.  
  606. config_branches = function(settings)
  607.  
  608.     shell.run('clear')
  609.  
  610.     print("  BRANCH MINE | BRANCH CONFIGURATION   ")
  611.     print("                                       ")
  612.     print(" (Current values are in parentheses)   ")
  613.     print("                                       ")
  614.     print("1. Edit Spacing (" .. settings["branch_spacing"] .. ")")
  615.     print("2. Edit Length: (" .. settings["branch_length"] .. ")")
  616.     write("3. Toggle Connect Branch Ends ")
  617.     if settings["connect_branch_ends"] then write("(Yes)\n") else write("(No)\n") end
  618.     print("4. Main Menu                           ")
  619.     print("                                       ")
  620.     print(" Type a number [1-4] and press ENTER:  ")
  621.     local input = io.read()
  622.     if input == "1" then
  623.         config_branch_spacing(settings)
  624.     elseif input == "2" then
  625.         config_branch_length(settings)
  626.     elseif input == "3" then
  627.         settings["connect_branch_ends"] = not settings["connect_branch_ends"]
  628.         config_branches(settings)
  629.     elseif input == "4" then
  630.         main_menu(settings)
  631.     else
  632.         print("Invalid input - enter a number 1-3")
  633.         textutils.slowPrint(".......................................")
  634.         config_branches(settings)
  635.     end
  636. end
  637.  
  638.  
  639. view_settings = function(settings)
  640.  
  641.     shell.run('clear')
  642.  
  643.     print("      BRANCH MINE | VIEW SETTINGS      ")
  644.     print("                                       ")
  645.     print("Main Trunk Length: " .. settings["trunk_length"])
  646.     write("Main Trunk Size: ")
  647.     if settings["trunk_size"] == "small" then
  648.         write("Small - 2h x 1w\n")
  649.     elseif settings["trunk_size"] == "medium" then
  650.         write("Medium - 3h x 3w\n")
  651.     elseif settings["trunk_size"] == "large" then
  652.         write("Large - 4h x 5w\n")
  653.     end
  654.     write("Excavate Main Trunk: ")
  655.     if settings["dig_main_trunk"] then write("Yes\n") else write ("No\n") end
  656.     print("Spacing Between Branches: " .. settings["branch_spacing"])
  657.     print("Branch Length: " .. settings["branch_length"])
  658.     write("Connect Branch Ends: ")
  659.     if settings["connect_branch_ends"] then write("Yes\n") else write ("No\n") end
  660.     print("                                       ")
  661.     print("         Press ENTER to continue       ")
  662.     io.read()
  663.     main_menu(settings)
  664. end
  665.  
  666. main_menu = function(settings)
  667.  
  668.     shell.run('clear')
  669.  
  670.     print("        BRANCH MINE | MAIN MENU        ")
  671.     print("                                       ")
  672.     print(" 1. View settings                      ")
  673.     print(" 2. Configure main trunk               ")
  674.     print(" 3. Configure branches                 ")
  675.     print(" 4. Help                               ")
  676.     print(" 5. About this program                 ")
  677.     print(" 6. BEGIN                              ")
  678.     print("                                       ")
  679.     print(" Type a number 1-6 and press ENTER     ")
  680.  
  681.     local input = io.read()
  682.     if input == "1" then
  683.         view_settings(settings)
  684.     elseif input == "2" then
  685.         config_trunk(settings)
  686.     elseif input == "3" then
  687.         config_branches(settings)
  688.     elseif input == "4" then
  689.         view_help(settings)
  690.     elseif input == "5" then
  691.         about_program(settings)
  692.     elseif input == "6" then
  693.         start_digging(settings)
  694.     else
  695.         print("Invalid input - enter a number 1-6")
  696.         textutils.slowPrint(".......................................")
  697.         main_menu(settings)
  698.     end
  699. end
  700.  
  701.  
  702. -- DEFAULTS
  703. local settings={}
  704. settings["trunk_length"] = 20
  705. settings["trunk_size"] = "medium"
  706. settings["dig_main_trunk"] = true
  707. settings["branch_spacing"] = 3
  708. settings["branch_length"] = 50
  709. settings["connect_branch_ends"] = true
  710.  
  711. main_menu(settings)
Add Comment
Please, Sign In to add comment