RJ-Bradach

Branch Mining Turtle

Jun 18th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 31.81 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------------------------
  2. -----------------------------------------API'S--------------------------------------------------------------------------
  3. ------------------------------------------------------------------------------------------------------------------------
  4. os.loadAPI("betterButton")
  5. ------------------------------------------------------------------------------------------------------------------------
  6. ---------------------------------------Variables------------------------------------------------------------------------
  7. ------------------------------------------------------------------------------------------------------------------------
  8. local w, h = term.getSize() -- Width, and Height of terminal
  9. local atStart = true -- The turtle is in its starting position
  10. local y = 0
  11. local t = turtle -- Abbreviation for turtle ;)
  12. local side = "Left" -- Which side to strip towards
  13. local strips = 1 -- # of strips to mine
  14. local isUp
  15. local length = 1 -- Length of strips
  16. local full = false -- Whether the turtle's inventory is full or not
  17. local offset = 0 -- How many strips to start offset by
  18. local between = false -- Whether the turtle is between strips or not
  19. local DistanceFromStripStart = 0 -- How far (t is) from the beginning of a strip
  20. local DistanceFromShaftStart = 0 -- How far from the start of the mineshaft
  21. local DistanceFromCurrentStrip = 0 -- How far from the strip (left off)
  22. local DistanceFromCurrentStripSpot = 0 -- How far from the spot inside strip (left off)
  23. local count = 0 -- Non-specific variable for counting
  24. local incNum = 1 -- Increment value for setting values in GUI
  25. local fromVeinOrder = {...} -- Table for the order of moves the turtle takes from the vein back into the strip of ore
  26. local toVeinOrder = {...} -- Table for the order of moves the turtle takes from the strip into the vein of ore
  27. local BI = betterButton.new() -- BI = Branch Info, instance for buttons
  28. local menu = betterButton.new() -- Start menu instance for buttons
  29. local orientation = 0 -- = starting orientation
  30.  
  31. local pages = {
  32.     -- Table for menu pages
  33.     "page1", "page2", "page3", "page4", "page5", "page6", "page7", "page8"
  34. }
  35. local filteredOres = {}
  36.  
  37. local lengths = {strips, length, offset}
  38.  
  39. local distances = {
  40.     -- Table for 4 common distances to use
  41.     DistanceFromStripStart, DistanceFromShaftStart,
  42.     DistanceFromCurrentStripSpot, DistanceFromCurrentStrip
  43. }
  44.  
  45. local ores = {
  46.     -- Table for ore names to filter by
  47.     "minecraft:gold_ore", "minecraft:iron_ore", "minecraft:redstone_ore",
  48.     "minecraft:emerald_ore", "minecraft:coal_ore", "minecraft:diamond_ore",
  49.     "minecraft:lapis_ore", "thaumcraft:ore_amber", "thaumcraft:ore_cinnabar",
  50.     "thaumcraft:ore_quartz", "appiedenergistics2:quartz_ore",
  51.     "appiedenergistics2:charged_quartz_ore",
  52.     {"biomesoplenty:gem_ore", 0, 1, 2, 3, 4, 5, 6, 7},
  53.     {"forestry:resources", 0, 1, 2},
  54.     {"immersiveengineering:ore", 0, 1, 2, 3, 4, 5},
  55.     {"ic2:resource", 1, 2, 3, 4}, {"mekanism:oreblock", 0, 1, 2},
  56.     {"railcraft:ore_metal", 0, 1, 2, 3, 4, 5},
  57.     {"thermalfoundation:ore", 0, 1, 2, 3, 4, 5, 6, 7, 8}
  58. }
  59.  
  60. local oresPlain = {
  61.     -- Table with readable ore names for menu buttons
  62.     "Gold", "Iron", "Redstone", "Emerald", "Coal", "Diamond", "Lapis",
  63.     "TCAmber", "Cinnabar", "Quartz", -- 10
  64.     "Certus Quartz", "Charged Quartz", {
  65.         "Biomesoplenty Ores", "Amethyst", "Ruby", "Peridot", "Topaz",
  66.         "Tanzanite", "Malachite", "Sapphire", "Amber"
  67.     }, {"Forestry Ores", "Apatite", "F-Copper", "F-Tin"}, {
  68.         "Immersive Ores", "Imm-Copper", "Bauxite", "Imm-Lead", "Imm-Silver",
  69.         "Imm-Nickel", "Imm-Uranium"
  70.     }, {"IC2 Ores", "IC2-Copper", "IC2-Lead", "IC2-Tin", "IC2-Uranium"},
  71.     {"Mekanism Ores", "Osmium", "Mek-Copper", "Mek-Tin"}, {
  72.         "Railcraft Ores", "Rail-Copper", "Rail-Tin", "Rail-Lead", "Rail-Silver",
  73.         "Rail-Nickel", "Zinc"
  74.     }, -- 18
  75.     {
  76.         "Thermal Foundation Ores", "Therm-Copper", "Therm-Tin", "Therm-Silver",
  77.         "Therm-Lead", "Aluminum", "Therm-Nickel", "Platinum", "Irididum",
  78.         "Mana Infused"
  79.     }
  80. }
  81. ------------------------------------------------------------------------------------------------------------------------
  82. ---------------------------------------------Functions------------------------------------------------------------------
  83. ------------------------------------------------------------------------------------------------------------------------
  84. ------------------------------------------------------------------------------------------------------------------------
  85. ---------------------------------------------Main Menu------------------------------------------------------------------
  86. ------------------------------------------------------------------------------------------------------------------------
  87. function mainMenu() -- Function to create starting menu
  88.     menu:add("Start", function() mainMenuFunction("Start") end,
  89.              math.ceil(w / 2) - 10, math.ceil(h / 2) - 2, math.ceil(w / 2) + 10,
  90.              math.ceil(h / 2) - 2, colors.yellow, colors.green, colors.black,
  91.              colors.black)
  92.     menu:add("How to Use", function() mainMenuFunction("How to Use") end,
  93.              math.ceil(w / 2) - 10, math.ceil(h / 2), math.ceil(w / 2) + 10,
  94.              math.ceil(h / 2), colors.yellow, colors.green, colors.black,
  95.              colors.black)
  96.     menu:add("Quit", function() mainMenuFunction("Quit") end,
  97.              math.ceil(w / 2) - 10, math.ceil(h / 2) + 2, math.ceil(w / 2) + 10,
  98.              math.ceil(h / 2) + 2, colors.yellow, colors.green, colors.black,
  99.              colors.black)
  100.     menu:draw()
  101.     setColors(colors.black, colors.orange)
  102.     term.setCursorPos(math.ceil(w / 2) - 8, 1)
  103.     print("RJ's Branch Miner")
  104.     menu:run()
  105. end
  106. function mainMenuFunction(name)
  107.     if (name == "Start") then
  108.         menu:flash(name)
  109.         sleep(0.15)
  110.         main()
  111.     elseif (name == "How to Use") then
  112.         menu:flash(name)
  113.         sleep(0.15)
  114.         howTo()
  115.     elseif (name == "Quit") then
  116.         quit(menu)
  117.     end
  118. end
  119. function howTo()
  120.     local tutorialStrings = {
  121.         "1. Place a chest behind and underneath your turtle. Place fuel in the chest underneath the turtle.",
  122.         "2. Press 'Start' on the main menu to begin.",
  123.         "3. Select any ores you want the turtle to mine with left-click or right-click. Use right click to filter a mod's ores specifically.",
  124.         "4. After you have filtered your ores, press next; Select strip length, #strips, strip offset, and direction.",
  125.         "5. Press 'Mine!'"
  126.     }
  127.     while true do
  128.         for i = 1, 5 do
  129.             setColors(colors.red, colors.orange)
  130.             clear()
  131.             term.setCursorPos(1, h)
  132.             print("*HOLD CTRL + T to quit out.*")
  133.             term.setCursorPos(1, 1)
  134.             setColors(colors.black, colors.orange)
  135.             print(tutorialStrings[i])
  136.             sleep(5)
  137.         end
  138.     end
  139. end
  140. ------------------------------------------------------------------------------------------------------------------------
  141. ------------------------------------------------------Pages-------------------------------------------------------------
  142. ------------------------------------------------------------------------------------------------------------------------
  143. function initializePages()
  144.     for i = 1, #pages do
  145.         pages[i] = betterButton.new()
  146.         createPage(pages[i], i)
  147.     end
  148. end
  149. function createPage(page, pageIndex)
  150.     pageIndex = pageIndex or nil
  151.     local x = 1
  152.     local y = 1
  153.     local text
  154.     page:add("Next", nil, w - 5, h, w, h, colors.yellow, colors.green,
  155.              colors.black, colors.white)
  156.     page:add("Back", nil, 1, h, 6, h, colors.yellow, colors.green, colors.black,
  157.              colors.white)
  158.     page:add("Quit", nil, math.ceil(w / 2) - 3, h, math.ceil(w / 2) + 2, h,
  159.              colors.yellow, colors.green, colors.black, colors.white)
  160.     if (page == pages[1]) then
  161.         page:add("All", nil, math.ceil(w / 2) - 6, h - 2, math.ceil(w / 2) - 2,
  162.                  h - 2, colors.yellow, colors.green, colors.black, colors.white)
  163.         page:add("None", nil, math.ceil(w / 2), h - 2, math.ceil(w / 2) + 5,
  164.                  h - 2, colors.yellow, colors.green, colors.black, colors.white)
  165.         toggle(page, "None")
  166.         for i = 1, #oresPlain do
  167.             if (type(oresPlain[i]) ~= "table") then
  168.                 text = oresPlain[i]
  169.                 if (x + string.len(text) + 2 > w) then
  170.                     y = y + 1
  171.                     x = 1
  172.                 end
  173.                 page:add(text, nil, x, y, string.len(text) + x + 1, y,
  174.                          colors.yellow, colors.green, colors.black, colors.white)
  175.                        
  176.             else
  177.                 text = oresPlain[i][1]
  178.                 if (x + string.len(text) + 2 > w) then
  179.                     y = y + 1
  180.                     x = 1
  181.                 end
  182.                 page:add(text, nil, x, y, string.len(text) + x + 1, y,
  183.                          colors.yellow, colors.green, colors.black, colors.white)
  184.                        
  185.             end
  186.             toggle(page, text)
  187.             x = x + string.len(text) + 3
  188.         end
  189.     else
  190.         for j = 2, #oresPlain[pageIndex + 11] do
  191.             text = oresPlain[pageIndex + 11][j]
  192.             if (x + string.len(text) + 4 > w) then
  193.                 y = y + 2
  194.                 x = 1
  195.             end
  196.             page:add(text, nil, x, y, string.len(text) + x + 1, y,
  197.                      colors.yellow, colors.green, colors.black, colors.white)
  198.             x = x + string.len(text) + 3
  199.         end
  200.     end
  201. end
  202. function branchInfoCheck()
  203.     BI = betterButton.new()
  204.     local leftArrow = "<"
  205.     local rightArrow = ">"
  206.     local ar1 = {leftArrow, label = "ar1"}
  207.     local ar2 = {leftArrow, label = "ar2"}
  208.     local ar3 = {leftArrow, label = "ar3"}
  209.     local ar4 = {rightArrow, label = "ar4"}
  210.     local ar5 = {rightArrow, label = "ar5"}
  211.     local ar6 = {rightArrow, label = "ar6"}
  212.     BI:add(ar1, function() arrowFunc("dec", 1, 1) end, w - 8, 1, w - 8, 1,
  213.            colors.orange, colors.green, colors.black, colors.white)
  214.     BI:add(ar2, function() arrowFunc("dec", 2, 2) end, w - 8, 2, w - 8, 2,
  215.            colors.orange, colors.green, colors.black, colors.white)
  216.     BI:add(ar3, function() arrowFunc("dec", 3, 3) end, w - 8, 3, w - 8, 3,
  217.            colors.orange, colors.green, colors.black, colors.white)
  218.     BI:add(ar4, function() arrowFunc("inc", 1, 1) end, w, 1, w, 1,
  219.            colors.orange, colors.green, colors.black, colors.white)
  220.     BI:add(ar5, function() arrowFunc("inc", 2, 2) end, w, 2, w, 2,
  221.            colors.orange, colors.green, colors.black, colors.white)
  222.     BI:add(ar6, function() arrowFunc("inc", 3, 3) end, w, 3, w, 3,
  223.            colors.orange, colors.green, colors.black, colors.white)
  224.     BI:add("Mine", function() mineButton() end, w - 5, h, w, h, colors.yellow,
  225.            colors.green, colors.black, colors.yellow)
  226.     BI:add("Quit", function() quit(BI) end, math.ceil(w - (w / 2) - 3), h,
  227.            math.ceil(w - (w / 2) + 2), h, colors.yellow, colors.green,
  228.            colors.black, colors.yellow)
  229.     BI:add("Back", function() backButton(BI) end, 1, h, 6, h, colors.yellow,
  230.            colors.green, colors.black, colors.white)
  231.     BI:add("1", function() setIncrement(1) end, w - 8, 4, w - 5, 4,
  232.            colors.yellow, colors.green, colors.black, colors.white)
  233.     BI:add("Left", function() pickSide("Left") end, 6, 7, 11, 7, colors.yellow,
  234.            colors.green, colors.black, colors.white)
  235.     BI:add("Right", function() pickSide("Right") end, 13, 7, 19, 7,
  236.            colors.yellow, colors.green, colors.black, colors.white)
  237.     BI:toggleButton("1")
  238.     BI:toggleButton("Left")
  239.     BI:add("10", function() setIncrement(10) end, w - 3, 4, w, 4, colors.yellow,
  240.            colors.green, colors.black, colors.white)
  241.     BI:draw()
  242.     drawBranchStaticInfo()
  243.     BI:run()
  244. end
  245. function drawBranchStaticInfo()
  246.     term.setCursorPos(1, 1)
  247.     setColors(colors.black, colors.orange, false)
  248.     print("Strip Length:")
  249.     term.setCursorPos(1, 2)
  250.     print("Number of Strips:")
  251.     term.setCursorPos(1, 3)
  252.     print("Offset (unit: strips):")
  253.     term.setCursorPos(1, 5)
  254.     print("Which way to strip towards?")
  255.     for i = 1, 3 do
  256.         term.setCursorPos(w - 6, i)
  257.         formatCheck(i)
  258.     end
  259. end
  260. ------------------------------------------------------------------------------------------------------------------------
  261. -----------------------------------------Buttons------------------------------------------------------------------------
  262. ------------------------------------------------------------------------------------------------------------------------
  263. function pullClick(page)
  264.     while true do
  265.         local event, name, button = page:handleEvents(
  266.                                         os.pullEvent("mouse_click"))
  267.         buttonDecision(page, event, name, button)
  268.     end
  269. end
  270. function drawPage(page)
  271.     page:draw()
  272.     pullClick(page)
  273. end
  274. function toggle(page, name, color)
  275.     if (color ~= nil) then page.buttonList[name].activeColor = color end
  276.     return page:toggleButton(name)
  277. end
  278. function buttonDecision(page, event, name, button)
  279.     if (event == "button_click") then
  280.         if (name == "Back") then
  281.             backButton(page)
  282.         elseif (name == "Next") then
  283.             branchInfoCheck()
  284.         elseif (name == "Quit") then
  285.             quit(page)
  286.         elseif (name == "None" or name == "All") then
  287.             selectAllorNone(page, name)
  288.         else
  289.             if (button == 1) then
  290.                 toggle(page, name, colors.green)
  291.                 editFilteredOres(page, name)
  292.             elseif (button == 2) then
  293.                 for i = 1, #oresPlain do
  294.                     if (type(oresPlain[i]) == "table" and name ==
  295.                         oresPlain[i][1]) then
  296.                         if (page.buttonList[name].active == false) then
  297.                             toggle(page, name, colors.brown)
  298.                             drawPage(pages[i - 11])
  299.                         elseif (page.buttonList[name].activeColor == 8192 and
  300.                             page.buttonList[name].active == true) then
  301.                             toggle(page, name)
  302.                             editFilteredOres(page, name)
  303.                             toggle(page, name, colors.brown)
  304.                             drawPage(pages[i - 11])
  305.                         end
  306.                     else
  307.                         toggle(page, name, colors.green)
  308.                         editFilteredOres(page, name)
  309.                     end
  310.                 end
  311.             end
  312.         end
  313.     end
  314. end
  315. function backButton(page)
  316.     page:flash("Back")
  317.     sleep(0.15)
  318.     for i = 1, #filteredOres do
  319.         if (type(filteredOres[i]) ~= "table") then
  320.             print(filteredOres[i])
  321.         else
  322.             for j = 1, #filteredOres[i] do print(filteredOres[i][j]) end
  323.         end
  324.     end
  325.     if (page == pages[1]) then
  326.         menu:draw()
  327.         setColors(colors.black, colors.orange)
  328.         term.setCursorPos(math.ceil(w / 2) - 8, 1)
  329.         print("RJ's Branch Miner")
  330.         menu:run()
  331.     else
  332.         drawPage(pages[1])
  333.     end
  334.     pullClick(pages[1])
  335. end
  336. function quit(page)
  337.     page:flash("Quit")
  338.     sleep(0.15)
  339.     clear()
  340.     error("Quitting!!!")
  341. end
  342. function arrowFunc(increment, var, height)
  343.     if (increment == "inc" and lengths[var] + incNum <= 999) then
  344.         lengths[var] = lengths[var] + incNum
  345.     elseif (increment == "dec" and lengths[var] - incNum >= 0) then
  346.         lengths[var] = lengths[var] - incNum
  347.     end
  348.     term.setCursorPos(w - 6, height)
  349.     formatCheck(var)
  350. end
  351. function selectAllorNone(page, name)
  352.     local state
  353.     if (name == "All") then
  354.         state = false
  355.     else
  356.         state = true
  357.     end
  358.     if (page.buttonList[name].active ~= true) then
  359.         toggle(page, "None", colors.green)
  360.         toggle(page, "All", colors.green)
  361.     end
  362.     for i = 1, #oresPlain do
  363.         if (type(oresPlain[i]) ~= "table") then
  364.             if (page.buttonList[oresPlain[i]].active == state) then
  365.                 toggle(page, oresPlain[i], colors.green)
  366.                 editFilteredOres(page, oresPlain[i])
  367.             end
  368.         elseif (type(oresPlain[i]) == "table") then
  369.             if (page.buttonList[oresPlain[i][1]].active == state) then
  370.                 toggle(page, oresPlain[i][1], colors.green)
  371.                 editFilteredOres(page, oresPlain[i][1])
  372.             end
  373.         end
  374.     end
  375. end
  376. function pickSide(lefRigh) -- function to ask which side to strip towards -----MESSED UPPPPPPP
  377.     if (BI.buttonList[lefRigh].active == false) then
  378.         side = lefRigh
  379.         BI:toggleButton("Right")
  380.         BI:toggleButton("Left")
  381.         drawBranchStaticInfo()
  382.     end
  383. end
  384. function setIncrement(value)
  385.     if (BI.buttonList[tostring(value)].active == false) then
  386.         incNum = value
  387.         BI:toggleButton("1")
  388.         BI:toggleButton("10")
  389.         drawBranchStaticInfo()
  390.     end
  391.     drawBranchStaticInfo()
  392. end
  393. function mineButton() branchMine() end
  394. ------------------------------------------------------------------------------------------------------------------------
  395. -----------------------------------------------ORE CHECKING-------------------------------------------------------------
  396. ------------------------------------------------------------------------------------------------------------------------
  397. function inspect()
  398.     local success, block = t.inspectUp()
  399.     for i = 1, #filteredOres do
  400.         if (type(filteredOres[i] ~= "table") and block.name == filteredOres[i]) then
  401.             digUp()
  402.             up()
  403.             table.insert(toVeinOrder, "up")
  404.             table.insert(fromVeinOrder, 1, "down")
  405.             inspect()
  406.         elseif (type(filteredOres[i]) == "table" and block.name ==
  407.             filteredOres[i][1]) then
  408.             for j = 1, #filteredOres[i] do
  409.                 if (block.metadata == filteredOres[i][j]) then
  410.                     digUp()
  411.                     up()
  412.                     table.insert(toVeinOrder, "up")
  413.                     table.insert(fromVeinOrder, 1, "down")
  414.                     inspect()
  415.                 end
  416.             end
  417.         end
  418.     end
  419.     local success, block = t.inspectDown()
  420.     for i = 1, #filteredOres do
  421.         if (type(filteredOres[i] ~= "table") and block.name == filteredOres[i]) then
  422.             digDown()
  423.             down()
  424.             table.insert(fromVeinOrder, 1, "up")
  425.             table.insert(toVeinOrder, "down")
  426.             inspect()
  427.         elseif (type(filteredOres[i]) == "table" and block.name ==
  428.             filteredOres[i][1]) then
  429.             for j = 1, #filteredOres[i] do
  430.                 if (block.metadata == filteredOres[i][j]) then
  431.                     digDown()
  432.                     down()
  433.                     table.insert(toVeinOrder, "down")
  434.                     table.insert(fromVeinOrder, 1, "up")
  435.                     inspect()
  436.                 end
  437.             end
  438.         end
  439.     end
  440.     for j = 1, 4 do
  441.         local success, block = t.inspect()
  442.         for i = 1, #filteredOres do
  443.             if (type(filteredOres[i] ~= "table") and block.name ==
  444.                 filteredOres[i]) then
  445.                 dig()
  446.                 forward(nil, nil, false)
  447.                 table.insert(toVeinOrder, "forward")
  448.                 table.insert(fromVeinOrder, 1, "back")
  449.                 inspect()
  450.             elseif (type(filteredOres[i]) == "table" and block.name ==
  451.                 filteredOres[i][1]) then
  452.                 for j = 1, #filteredOres[i] do
  453.                     if (block.metadata == filteredOres[i][j]) then
  454.                         dig()
  455.                         forward(nil, nil, false)
  456.                         table.insert(toVeinOrder, "forward")
  457.                         table.insert(fromVeinOrder, 1, "back")
  458.                         inspect()
  459.                     end
  460.                 end
  461.             end
  462.         end
  463.         t.turnRight()
  464.         table.insert(fromVeinOrder, 1, "left")
  465.         table.insert(toVeinOrder, "right")
  466.     end
  467.     fromVeinToStrip(true)
  468. end
  469. function findIndex(tableName, name)
  470.     for index = 1, #tableName do
  471.         if (type(tableName[index]) ~= "table") then
  472.             if (tableName[index] == name) then return index, nil end
  473.         elseif (type(tableName[index]) == "table") then
  474.             for index2 = 1, #tableName[index] do
  475.                 if (tableName[index][index2] == name) then
  476.                     return index, index2
  477.                 end
  478.             end
  479.         end
  480.     end
  481. end
  482. function branchMine()
  483.     clear()
  484.     fuelCheck()
  485.     shaft()
  486.     for i = 1, lengths[2] do strip() end
  487.     backToStart()
  488.     dropInventory()
  489.     done(true)
  490. end
  491. ------------------------------------------------------------------------------------------------------------------------
  492. --------------------------------------------------Strips----------------------------------------------------------------
  493. ------------------------------------------------------------------------------------------------------------------------
  494. function offsetShaft()
  495.     for i = 1, ((lengths[2] - 1) * 3 - offset) do back(2, false) end
  496.     turn(3)
  497. end
  498. function shaft()
  499.     up()
  500.     if (lengths[2] > 1) then
  501.         turn(1)
  502.         for i = 1, ((lengths[2] - 1) + lengths[3]) * 3 do
  503.             dig()
  504.             forward(0, 2, true)
  505.             digDown()
  506.         end
  507.         turn(3)
  508.     end
  509. end
  510. function strip()
  511.     for i = 1, lengths[1] do
  512.         dig()
  513.         forward(0, 1, true)
  514.         digDown()
  515.     end
  516.     down()
  517.     inspect()
  518.     returnToStripStart()
  519.     if (distances[2] ~= ((lengths[1] + lengths[3]) * 3) - 3) then
  520.         turn(1)
  521.         for i = 1, 3 do back(2, true) end
  522.         turn(3)
  523.         up()
  524.     end
  525. end
  526. ------------------------------------------------------------------------------------------------------------------------
  527. --------------------------------------------------Returning Places------------------------------------------------------
  528. ------------------------------------------------------------------------------------------------------------------------
  529. function fromVeinToStrip(check)
  530.     for i = 1, #fromVeinOrder do
  531.         if (check == true) then
  532.             repeat
  533.                 local move = fromVeinOrder[i]
  534.                 if (move == "back") then
  535.                    back(nil, false)
  536.                 elseif (move == "up") then
  537.                     up()
  538.                 elseif (move == "down") then
  539.                     down()
  540.                 end
  541.                 table.remove(fromVeinOrder, 1)
  542.             until (move ~= "left")
  543.             if (fromVeinOrder[1] == nil) then
  544.                 deleteTableContents(toVeinOrder)
  545.                 deleteTableContents(fromVeinOrder)
  546.             end
  547.             return
  548.         elseif (check == false) then
  549.             local move = fromVeinOrder[i]
  550.             if (move == "back") then
  551.                 back(nil, false)
  552.             elseif (move == "up") then
  553.                 up()
  554.             elseif (move == "down") then
  555.                 down()
  556.             elseif (move == "left") then
  557.                 t.turnLeft()
  558.             end
  559.         end
  560.     end
  561.     return
  562. end
  563. function toVeinfromStrip()
  564.     for i = 1, #toVeinOrder do
  565.         local move = toVeinOrder[i]
  566.         if (move == "forward") then
  567.             forward(nil, nil, false)
  568.         elseif (move == "up") then
  569.             up()
  570.         elseif (move == "down") then
  571.             down()
  572.         elseif (move == "right") then
  573.             t.turnRight()
  574.         end
  575.     end
  576.     return
  577. end
  578. function returnToStripStart()
  579.     while (distances[1] ~= 0) do back(1, true) end
  580. end
  581. function backToStripSpot()
  582.     if (isUp == true) then
  583.         up()
  584.     end ---changed t.up() to up()
  585.     turn(1)
  586.     while (distances[4] > 0) do
  587.         forward(4, 2, false)
  588.     end
  589.     if (distances[3] == 0) then
  590.     else
  591.         turn(3)
  592.         while (distances[3] > 0) do
  593.             forward(3, 1, false)
  594.         end
  595.     end
  596.     distances[3] = 0
  597.     distances[4] = 0
  598.     toVeinfromStrip()
  599. end
  600. function backToStart()
  601.     if (atStart == false) then
  602.         fromVeinToStrip(false)
  603.         if (orientation%4 ~= 0) then turn(4 - orientation%4) end
  604.         distances[3] = distances[1]
  605.         distances[4] = distances[2]
  606.         while (distances[1] ~= 0) do back(1, false) end
  607.         if (distances[1] == 0) then
  608.             if (between == false) then turn(1) end
  609.         end
  610.         while (distances[2] ~= 0) do back(2, false) end
  611.         turn(3)
  612.         isUp = false
  613.         local success, block = t.inspectDown()
  614.         while (block.name ~= "minecraft:chest") do
  615.             isUp = true
  616.             down()
  617.             success, block = t.inspectDown()
  618.         end
  619.         atStart = true
  620.         distances[2] = 0
  621.         distances[1] = 0
  622.     end
  623.     return
  624. end
  625. ------------------------------------------------------------------------------------------------------------------------
  626. -------------------------------------------------Dig--------------------------------------------------------------------
  627. ------------------------------------------------------------------------------------------------------------------------
  628. function dig()
  629.     while (t.detect() == true) do
  630.         t.dig()
  631.             inventoryCheck()
  632.         sleep(0.8)
  633.     end
  634. end
  635.  
  636. function digDown()
  637.     while (t.detectDown() == true) do
  638.         t.digDown()
  639.         inventoryCheck()
  640.         sleep(0.8)
  641.     end
  642. end
  643.  
  644. function digUp()
  645.     while (t.detectUp() == true) do
  646.         t.digUp()
  647.         inventoryCheck()
  648.         sleep(0.8)
  649.     end
  650. end
  651. ------------------------------------------------------------------------------------------------------------------------
  652. ----------------------------------------------------Movement------------------------------------------------------------
  653. ------------------------------------------------------------------------------------------------------------------------
  654. function up()
  655.     while not (t.up()) do
  656.         t.attackUp()
  657.         digUp()
  658.         sleep(0.8)
  659.     end
  660.     y = y + 1
  661.     print(y)
  662.     atStart = false
  663. end
  664. function down()
  665.     while not (t.down()) do
  666.         t.attackDown()
  667.         digDown()
  668.         sleep(0.8)
  669.     end
  670.     y = y - 1
  671.     print(y)
  672. end
  673. function back(m, inspec)
  674.     if (distances[m] == nil or distances[m] > 0) then
  675.         while not (t.back()) do
  676.                 turn(2)
  677.             if (t.detect()) then
  678.                 dig()
  679.             else
  680.                 for i = 1, 8 do t.attack() end
  681.             end
  682.             turn(2)
  683.         end
  684.         if (m ~= nil) then distances[m] = distances[m] - 1 end
  685.         if (atStart == true) then atStart = false end
  686.         if (inspec == true) then inspect() end
  687.     end
  688.     print("Back")
  689.     print(distances[m])
  690.     print("--------")
  691. end
  692. function forward(m, p, inspec)
  693.     if (distances[m] == nil or distances[m] > 0) then
  694.         while not (t.forward()) do
  695.             if (t.detect()) then
  696.                 dig()
  697.             else
  698.                 t.attack()
  699.             end
  700.         end
  701.         if (distances[m] ~= nil and distances[m] > 0) then distances[m] = distances[m] - 1 end
  702.         if (p ~= nil) then distances[p] = distances[p] + 1 end
  703.         if (atStart == true) then atStart = false end
  704.         if (inspec == true) then inspect() end
  705.         fuelCheck()
  706.     end
  707.     print("Forward")
  708.     print(distances[m])
  709.     print(distances[p])
  710.     print("--------")
  711. end
  712. function turn(x)
  713.     if (side == "Left") then
  714.         for i = 1, x do t.turnLeft() end
  715.     else
  716.         for i = 1, x do t.turnRight() end
  717.     end
  718.     orientation = orientation + x
  719. end
  720. ------------------------------------------------------------------------------------------------------------------------
  721. ------------------------------------------------------MISC--------------------------------------------------------------
  722. ------------------------------------------------------------------------------------------------------------------------
  723. function editFilteredOres(page, name)
  724.     local index, index2 = findIndex(oresPlain, name)
  725.     if (page.buttonList[name].active == true) then
  726.         if (index2 == nil) then
  727.             table.insert(filteredOres, 1, ores[index])
  728.         else
  729.             if (index2 ~= 1) then
  730.                 table.insert(filteredOres, 1,
  731.                              {ores[index][1], ores[index][index2]})
  732.             else
  733.                 table.insert(filteredOres, 1, ores[index][1])
  734.             end
  735.         end
  736.     elseif (page.buttonList[name].active == false) then
  737.         if (index2 == nil) then
  738.             table.remove(filteredOres, findIndex(filteredOres, ores[index]))
  739.         else
  740.             table.remove(filteredOres,
  741.                          findIndex(filteredOres, ores[index][index2])) -- not working with metadata values
  742.         end
  743.     end
  744. end
  745. function formatCheck(var)
  746.     if (lengths[var] < 10) then
  747.         print(" 00" .. lengths[var] .. " ")
  748.     elseif (lengths[var] < 100) then
  749.         print(" 0" .. lengths[var] .. " ")
  750.     else
  751.         print(" " .. lengths[var] .. " ")
  752.     end
  753. end
  754. function dropInventory()
  755.     if (atStart == false) then backToStart() end
  756.     turn(2)
  757.     for i = 1, 16 do
  758.         t.select(i)
  759.         if (t.getItemCount(i) > 0) then
  760.             local data = t.getItemDetail()
  761.             if (data.name == "minecraft:coal") then
  762.                 if (t.dropDown() == false) then
  763.                     done(false)
  764.                 end
  765.             else
  766.                 if (t.drop() == false) then
  767.                     t.drop()
  768.                 end
  769.             end
  770.         end
  771.     end
  772.     turn(2)
  773. end
  774. function fuelCheck() -- Checks if there's fuel
  775.     if ((distances[2] + distances[1]) >= (t.getFuelLevel() - 10)) then
  776.         refuel()
  777.     end
  778.     return
  779. end
  780. function refuel() -- function to refuel
  781.     if (atStart == true) then -- If the turtle is in starting position
  782.         for i = 1, 16 do -- for all slots
  783.             t.select(i)
  784.             t.suckDown() -- suck coal from chest below
  785.             t.refuel() -- refuel with that coal
  786.         end
  787.         return
  788.     else -- if turtle isn't at starting position
  789.         for i = 1, 16 do -- for all slots
  790.             if (t.getItemCount(i) > 0) then
  791.                     t.select(i)
  792.                     t.refuel()
  793.                     break -- has fuel so it can move forward
  794.             end
  795.         end
  796.         backToStart() -- if not, go back to start to get some fuel
  797.         refuel()
  798.     end
  799.     if (distances[3] + distances[4] < t.getFuelLevel() - 10) then
  800.         backToStripSpot()
  801.     else
  802.         print("No fuel")
  803.         done(false)
  804.     end
  805.     return
  806. end
  807. function inventoryCheck()
  808.     for i = 1, 16 do
  809.         if (t.getItemCount(i) == 0) then
  810.             full = false
  811.             return
  812.         else
  813.             full = true
  814.         end
  815.     end
  816.     if (full == true) then
  817.         dropInventory()
  818.         backToStripSpot()
  819.     end
  820. end
  821. function done(isDone)
  822.     t.select(1)
  823.     if (isDone == false) then
  824.         print("This is how much fuel I have left: " .. t.getFuelLevel())
  825.         error("I could not finish everything.")
  826.     else
  827.         print("I mined " .. lengths[2] .. ", " .. lengths[1] .. " block strips.")
  828.         print("This is how much fuel I have left: " .. t.getFuelLevel())
  829.         error("Done Mining")
  830.     end
  831. end
  832. function clear()
  833.     term.clear()
  834.     term.setCursorPos(1, 1)
  835. end
  836. function deleteTableContents(table)
  837.     count = #table
  838.     for i = 0, count do table[i] = nil end
  839.     count = 0
  840.     return
  841. end
  842. function setColors(text, background, clear)
  843.     clear = clear or false
  844.     if (term.isColor() == true) then
  845.         term.setTextColor(text)
  846.         term.setBackgroundColor(background)
  847.         if (clear == true) then term.clear() end
  848.     end
  849. end
  850. ------------------------------------------------------------------------------------------------------------------------
  851. ------------------------------------------------------MAIN--------------------------------------------------------------
  852. ------------------------------------------------------------------------------------------------------------------------
  853. function main()
  854.     initializePages()
  855.     drawPage(pages[1])
  856. end
  857. mainMenu()
Add Comment
Please, Sign In to add comment