mchenault13

branchMine

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