raymanLPs

brancher turtle script

Jan 26th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.22 KB | None | 0 0
  1. -- Branch Mining Turtle (with Recursion!)
  2. -- Mines and follows ores (and returns!)
  3. -- Variables
  4. turtle.select(1)
  5.  
  6. term.clear()
  7. term.setCursorPos(1,1)
  8. print(os.getComputerLabel() .. " at your service!")
  9. print()
  10. print("Please place blocks as follows:")
  11. print("Slot 1 - Enderchest")
  12. print("Slot 2 - Stone (Smooth)")
  13. print("Slot 3 - Dirt")
  14. print("Slot 4 - Gravel")
  15. print("Slot 5 - Fuel (coal or lava)")
  16. print()
  17. print("Press [ENTER] when ready.")
  18. read(input)
  19.  
  20. -- Starting Out
  21. for s = 1,5 do
  22.     if turtle.getItemCount(s) < 1 then
  23.         print("Fill your slots!")
  24.         quit()
  25.     end
  26. end
  27.  
  28. continue = false
  29. continueStepCount = 0
  30. continueYCount = 0
  31. facing = 0
  32. currentY = 0
  33. currentX = 0
  34. currentZ = 0
  35. maxChase = 100
  36. currentChase = 0
  37.  
  38. -- Functions
  39. function trackUp()
  40.     if turtle.up() then
  41.         currentZ = currentZ + 1
  42.         return true;
  43.     else
  44.         return false;
  45.     end
  46. end
  47.  
  48. function trackDown()
  49.     if turtle.down() then
  50.         currentZ = currentZ - 1
  51.         return true;
  52.     else
  53.         return false;
  54.     end
  55. end
  56.  
  57. function trackForward()
  58.     if turtle.forward() then
  59.         if facing == 0 then
  60.             currentY = currentY + 1
  61.         elseif facing == 1 then
  62.             currentX = currentX + 1
  63.         elseif facing == 2 then
  64.             currentY = currentY - 1
  65.         elseif facing == 3 then
  66.             currentX = currentX - 1
  67.         end
  68.         return true;
  69.     else
  70.         return false;
  71.     end
  72. end
  73.  
  74. function trackBackward()
  75.     if turtle.back() then
  76.         if facing == 0 then
  77.             currentY = currentY - 1
  78.         elseif facing == 1 then
  79.             currentX = currentX - 1
  80.         elseif facing == 2 then
  81.             currentY = currentY + 1
  82.         elseif facing == 3 then
  83.             currentX = currentX + 1
  84.         end
  85.         return true;
  86.     else
  87.         return false;
  88.     end
  89. end
  90.  
  91. function trackRight()
  92.     turtle.turnRight()
  93.     facing = facing + 1;
  94.     if facing == 4 then
  95.         facing = 0
  96.     end
  97. end
  98.  
  99. function trackLeft()
  100.     turtle.turnLeft()
  101.     facing = facing - 1;
  102.     if facing == -1 then
  103.         facing = 3
  104.     end
  105. end
  106.  
  107. function tfuel()
  108.     if turtle.getItemCount(16) > 0 then emptyOres() end
  109.     if turtle.getFuelLevel() < 16 then
  110.         turtle.select(5)
  111.         turtle.refuel(1)
  112.     end
  113. end
  114.  
  115. local function turnAround()
  116.     trackRight()
  117.     trackRight()
  118. end
  119.  
  120. local function attemptForward()
  121.     while trackForward() == false do
  122.         turtle.dig()
  123.         os.sleep(.5)
  124.     end
  125. end
  126.  
  127. function emptyOres()
  128.     while turtle.detect() == true do
  129.         turtle.dig()
  130.         os.sleep(.5)
  131.     end
  132.     turtle.select(1)
  133.     turtle.place()
  134.     local i
  135.     for i = 6, 16 do
  136.         turtle.select(i)
  137.         turtle.drop()
  138.     end
  139.     turtle.select(1)
  140.     turtle.dig()
  141.     os.sleep(.5)
  142. end
  143.  
  144. local function checkUp()
  145.     if turtle.detectUp() == false then
  146.         return false;
  147.     end
  148.     local i
  149.     for i = 2, 5 do
  150.         turtle.select(i)
  151.         if turtle.compareUp() == true then
  152.             return false;
  153.         end
  154.     end
  155.     return true;
  156. end
  157.  
  158. local function checkDown()
  159.     if turtle.detectDown() == false then
  160.         return false;
  161.     end
  162.     local i
  163.     for i = 2, 5 do
  164.         turtle.select(i)
  165.         if turtle.compareDown() == true then
  166.             return false;
  167.         end
  168.     end
  169.     return true;
  170. end
  171.  
  172. local function checkForward()
  173.     if turtle.detect() == false then
  174.         return false;
  175.     end
  176.     local i
  177.     for i = 2, 5 do
  178.         turtle.select(i)
  179.         if turtle.compare() == true then
  180.             return false;
  181.         end
  182.     end
  183.     return true;
  184. end
  185.  
  186. -- Recursive ore detection and "chase".
  187. -- 0 is none, 1 is below, 2 is above, 3 is behind
  188. local function detectAndChaseOre(arrivalFrom)
  189.     local retries = 10
  190.     tfuel()
  191.     if arrivalFrom == 0 then
  192.         currentChase = 0
  193.     end
  194.     if checkUp() == true then
  195.         retries = 10
  196.         while currentChase < maxChase and trackUp() == false and retries > 0 do
  197.             turtle.digUp()
  198.             os.sleep(.5)
  199.             retries = retries - 1
  200.         end
  201.         if retries ~= 0 and currentChase < maxChase then
  202.             currentChase = currentChase + 1
  203.             detectAndChaseOre(1)
  204.         end
  205.     end
  206.     if checkDown() == true then
  207.         retries = 10
  208.         while currentChase < maxChase and trackDown() == false and retries > 0 do
  209.             turtle.digDown()
  210.             os.sleep(.5)
  211.             retries = retries - 1
  212.         end
  213.         if retries ~= 0 and currentChase < maxChase then
  214.             currentChase = currentChase + 1
  215.             detectAndChaseOre(2)
  216.         end
  217.     end
  218.     -- check all 4 directions
  219.     local rotation
  220.     for rotation = 0, 3 do
  221.         if checkForward() == true then
  222.             retries = 10
  223.             while currentChase < maxChase and trackForward() == false and retries > 0 do
  224.                 turtle.dig()
  225.                 os.sleep(.5)
  226.                 retries = retries - 1
  227.             end
  228.             if retries ~= 0 and currentChase < maxChase then
  229.                 currentChase = currentChase + 1
  230.                 detectAndChaseOre(3)
  231.             end
  232.         end
  233.         trackRight()
  234.     end
  235.    
  236.     if arrivalFrom == 1 then
  237.         trackDown()
  238.     elseif arrivalFrom == 2 then
  239.         trackUp()
  240.     elseif arrivalFrom == 3 then
  241.         trackBackward()
  242.     end
  243. end
  244.  
  245. local function digBranch()
  246.     local branchLength
  247.     for branchLength = 0, 20 do
  248.         while attemptForward() == false do
  249.             turtle.dig()
  250.         end
  251.         writeStatusDisplay()
  252.         detectAndChaseOre(0)
  253.     end
  254.     turnAround()
  255.     for branchLength = 0, 20 do
  256.         while attemptForward() == false do
  257.             turtle.dig()
  258.         end
  259.         tfuel()
  260.     end
  261. end
  262.  
  263. function writeContinueFile(theStepCount, theYCount)
  264.     h = fs.open("r86brancont", "w")
  265.     h.writeLine(currentY)
  266.     h.writeLine(currentZ)
  267.     h.writeLine(theStepCount)
  268.     h.writeLine(theYCount)
  269.     h.close()
  270. end
  271.  
  272. function writeStatusDisplay()
  273.     term.clear()
  274.     term.setCursorPos(1,1)
  275.     print("Current Fuel: " .. turtle.getFuelLevel())
  276.     print("Current Length: " .. (currentY + 1) .. "/52")
  277.     print("Current Height: " .. (math.abs(currentZ) + 1) .. "/9")
  278. end
  279.  
  280. function digMainChamber()
  281.     local stepCount = 0
  282.     local YCount = 0
  283.     if math.abs(currentZ) % 2 == 1 then
  284.         stepCount = -2
  285.     end
  286.     if continue == true then
  287.         stepCount = continueStepCount
  288.         YCount = continueYCount
  289.         continue = false
  290.     end
  291.     local chamberLength
  292.     for chamberLength = YCount, 51 do
  293.         if stepCount % 4 == 0 then
  294.             writeContinueFile(stepCount, chamberLength)
  295.             trackLeft()
  296.             digBranch()
  297.             digBranch()
  298.             trackRight()
  299.         end
  300.         while attemptForward() == false do
  301.             turtle.dig()
  302.         end
  303.         stepCount = stepCount + 1
  304.         writeStatusDisplay()
  305.         detectAndChaseOre(0)
  306.     end
  307. end
  308.  
  309. if fs.exists("r86brancont") then
  310.     continue = nil
  311.     while continue ~= "y" and continue ~= "Y" and continue ~= "n" and continue ~= "N" do
  312.         print("Detected previous mining operation")
  313.         print("Continue previous operation? (Y/N)")
  314.     continue = read(input)
  315.     end
  316.     if continue == "Y" or continue == "y" then
  317.         h = fs.open("r86brancont", "r")
  318.         contY = tonumber(h.readLine())
  319.         contZ = tonumber(h.readLine())
  320.         continueStepCount = tonumber(h.readLine())
  321.         continueYCount = tonumber(h.readLine())
  322.         h.close()
  323.         if contY ~= nil and contZ ~= nil and continueStepCount ~= nil and continueYCount ~= nil then
  324.             while currentY ~= contY do
  325.                 while trackForward() == false do
  326.                     turtle.dig()
  327.                     os.sleep(.5)
  328.                 end
  329.             end
  330.             while currentZ ~= contZ do
  331.                 while trackDown() == false do
  332.                     turtle.digDown()
  333.                     os.sleep(.5)
  334.                 end
  335.             end
  336.             if contZ % 2 == 1 then
  337.                 turnAround()
  338.             end
  339.         continue = true
  340.         else
  341.             print("Unable to continue, file malformation detected")
  342.         fs.delete("r86brancont")
  343.         end
  344.     elseif continue == "N" or continue == "n" then
  345.         print("Starting new mining operation")
  346.     fs.delete("r86brancont")
  347.     end
  348. end
  349.  
  350. local totalHeight
  351. for totalHeight = math.abs(currentZ),8 do
  352.     digMainChamber()
  353.     turnAround()
  354.     turtle.digDown()
  355.     trackDown()
  356. end
  357. for totalHeight = 0,8 do
  358.     trackUp()
  359. end
  360. turnAround()
  361.  
  362. fs.delete("r86brancont")
  363. emptyOres()
Advertisement
Add Comment
Please, Sign In to add comment