Advertisement
PinSeventy

Strip Mining Turtle

Jul 1st, 2014
30,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.01 KB | None | 0 0
  1. --Variable Declarations
  2. local mainShaftDim = "2x2"
  3. local sideShaftDim = "2x1"
  4. local shaftFreq = "3"
  5. local totalCost = 0
  6. local input = ""
  7. local length = 0
  8. local currentPos = 1
  9. local chestDist = 0
  10. local doFuel = false
  11.  
  12.  
  13.  
  14. --Function Declarations
  15. function oreDetect()
  16.     if checkWanted() then
  17.         print("Found something I want")
  18.  
  19.         -- Getting first block
  20.         turtle.dig()
  21.         turtle.forward()
  22.         print("Got it!")
  23.  
  24.         -- Begin checking for more ore
  25.         print("Checking for more on the right")
  26.         turtle.turnRight()
  27.         oreDetect()
  28.         print("No more there")
  29.         turtle.turnLeft()
  30.  
  31.         print("Checking for more on the left")
  32.         turtle.turnLeft()
  33.         oreDetect()
  34.         print("No more there")
  35.         turtle.turnRight()
  36.     end
  37.  
  38.     if checkWantedUp() then
  39.         print("There's some above me!")
  40.         turtle.digUp()
  41.         turtle.up()
  42.         print("Got it!")
  43.         print("Checking for more")
  44.  
  45.         oreDetect()
  46.         print("No more there")
  47.         turtle.down()
  48.     end
  49.  
  50.     if checkWantedDown() then
  51.         print("There's something belowe me!")
  52.         turtle.digDown()
  53.         turtle.down()
  54.  
  55.         print("Got it!")
  56.         print("Checking for more")
  57.         oreDetect()
  58.         print("No more there")
  59.         turtle.up()
  60.     end
  61.  
  62.     -- Return to original spot when no more wanted ore
  63.     print("Back to where I started")
  64.     turtle.back()
  65. end
  66.  
  67. -- Checks if block is a wanted material
  68. function checkWanted()
  69.     -- Requires stone, dirt, gravel, marble in slots 12 - 15
  70.     wanted = false
  71.     if turtle.detect() then
  72.  
  73.  
  74.  
  75.         turtle.select(12)
  76.         if turtle.compare() == false then
  77.             wanted = true
  78.         end
  79.  
  80.         turtle.select(13)
  81.         if turtle.compare() == false then
  82.             wanted = true
  83.         end
  84.  
  85.         turtle.select(14)
  86.         if turtle.compare() == false then
  87.             wanted = true
  88.         end
  89.  
  90.         turtle.select(15)
  91.         if turtle.compare() == false then
  92.             wanted = true
  93.         end
  94.     end
  95.     return wanted
  96. end
  97.  
  98. function checkWantedUp()
  99.     wanted = false
  100.  
  101.     if turtle.detectUp() then
  102.         turtle.select(12)
  103.         if turtle.compareUp() == false then
  104.             wanted = true
  105.         end
  106.  
  107.         turtle.select(13)
  108.         if turtle.compareUp() == false then
  109.             wanted = true
  110.         end
  111.  
  112.         turtle.select(14)
  113.         if turtle.compareUp() == false then
  114.             wanted = true
  115.         end
  116.  
  117.         turtle.select(15)
  118.         if turtle.compareUp() == false then
  119.             wanted = true
  120.         end
  121.     end
  122.     return wanted
  123. end
  124.  
  125. function checkWantedDown()
  126.     wanted = false
  127.  
  128.     if turtle.detectDown() then
  129.         turtle.select(12)
  130.         if turtle.compareDown() == false then
  131.             wanted = true
  132.         end
  133.  
  134.         turtle.select(13)
  135.         if turtle.compareDown() == false then
  136.             wanted = true
  137.         end
  138.  
  139.         turtle.select(14)
  140.         if turtle.compareDown() == false then
  141.             wanted = true
  142.         end
  143.  
  144.         turtle.select(15)
  145.         if turtle.compareDown() == false then
  146.             wanted = true
  147.         end
  148.     end
  149.     return wanted
  150. end
  151.  
  152. -- Refuels the turtle with slots 1-15
  153. function refuel()
  154.     for i=1,15,1 do
  155.         turtle.select(i)
  156.         turtle.refuel(turtle.getItemCount(i))
  157.     end
  158.     turtle.select(1)
  159. end
  160.  
  161. function dig()
  162.     oreDetect()
  163.     turtle.dig()
  164. end
  165.  
  166. -- Mines the main 2x2 shaft of the system
  167. function mainShaft()
  168.     turtle.select(1)
  169.     for i=1,4,1 do
  170.         while turtle.detect() == true do
  171.             dig()
  172.         end
  173.         turtle.forward()
  174.         if not turtle.detectDown() then
  175.             turtle.placeDown()
  176.         end
  177.         while turtle.detectUp() do
  178.             turtle.digUp()
  179.         end
  180.         turtle.turnRight()
  181.         while turtle.detect() == true do
  182.             dig()
  183.         end
  184.         turtle.forward()
  185.         if not turtle.detectDown() then
  186.             turtle.placeDown()
  187.         end
  188.         while turtle.detectUp() do
  189.             turtle.digUp()
  190.         end
  191.         turtle.turnRight()
  192.         turtle.turnRight()
  193.         while turtle.detect() do
  194.             dig()
  195.         end
  196.         turtle.forward()
  197.         if not turtle.detectDown() then
  198.             turtle.placeDown()
  199.         end
  200.         turtle.turnRight()
  201.     end
  202.     turtle.turnRight()
  203.     turtle.turnRight()
  204.     turtle.select(16)
  205.     turtle.forward()
  206.     turtle.placeUp()
  207.     turtle.turnLeft()
  208.     turtle.forward()
  209.     turtle.placeUp()
  210.     turtle.turnLeft()
  211.     turtle.forward()
  212.     turtle.turnLeft()
  213.     turtle.forward()
  214.     turtle.turnRight()
  215. end
  216.  
  217. -- Mines a 2x1 side shaft off of the main shaft of the system
  218. function sideShaft()
  219.     turtle.select(1)
  220.     for i=0,10,1 do
  221.         while turtle.detect() == true do
  222.             dig()
  223.         end
  224.         turtle.forward()
  225.         if turtle.detectDown() == false then
  226.             turtle.placeDown()
  227.         end
  228.         while turtle.detectUp() == true do
  229.             turtle.digUp()
  230.         end
  231.     end
  232.     turtle.select(16)
  233.     turtle.turnRight()
  234.     turtle.turnRight()
  235.     turtle.place()
  236.     turtle.turnRight()
  237.     turtle.turnRight()
  238.     turtle.select(1)
  239.     for i=0,10,1 do
  240.         while turtle.detect() == true do
  241.             dig()
  242.         end
  243.         turtle.forward()
  244.         if turtle.detectDown() == false then
  245.             turtle.placeDown()
  246.         end
  247.         while turtle.detectUp() == true do
  248.             turtle.digUp()
  249.         end
  250.     end
  251.     turtle.turnRight()
  252.     turtle.turnRight()
  253.     turtle.select(16)
  254.     turtle.place()
  255.     turtle.up()
  256.     for i=1,23,1 do
  257.         while turtle.detect() do
  258.             dig()
  259.         end
  260.         turtle.forward()
  261.     end
  262.     turtle.down()
  263. end
  264.  
  265.  
  266. -- Places all the goodies in the designated chest
  267. function deposit()
  268.     for i=1, (chestDist), 1 do
  269.         turtle.forward()
  270.     end
  271.     for i=1, 11, 1 do
  272.         turtle.select(i)
  273.         turtle.drop()
  274.     end
  275.     refuel()
  276.     if turtle.getItemCount(16) < 6 then
  277.         stopMining()
  278.     end
  279.     if turtle.getFuelLevel() < (108 + 2 * chestDist) then
  280.         stopMining()
  281.     end
  282.     turtle.turnRight()
  283.     turtle.turnRight()
  284.     for i=1, (chestDist), 1 do
  285.         turtle.forward()
  286.     end
  287.  
  288. end
  289.  
  290. -- Stops mining for some reason or another
  291. function stopMining()
  292.     if turtle.getFuelLevel() < (108 + 2 * chestDist) then
  293.         print("I am low on fuel.")
  294.         local remainFuel = 0
  295.         for i=currentPos, length, 1 do
  296.             remainFuel = (108 + 2 * (4 * i + 1))
  297.         end
  298.         print("I need "..remainFuel.." to finish.")
  299.         print("That's "..(remainFuel/80).." coal or "..(remainFuel/1000).." lava.")
  300.         print("Type (cont) to refuel & continue mining.")
  301.         doFuel = true
  302.     end
  303.     if turtle.getItemCount(16) < 6 then
  304.         print("I am low on torches.")
  305.         print("Place more in slot 9...")
  306.         while turtle.getItemCount(16) < 6 do
  307.             sleep(1)
  308.         end
  309.         print("Type (cont) to continue mining.")
  310.         doFuel = false
  311.     end
  312.     local cont = false
  313.     while cont == false do
  314.         --[[
  315.         if rs.getInput("right") == true then --Flickers RS torch on right of chest
  316.             rs.setOutput("right", false)
  317.         else
  318.             rs.setOutput("right", true)
  319.         end
  320.         ]]
  321.         local input = io.read()
  322.         if input == "cont" then
  323.  
  324.             if doFuel then
  325.                 refuel()
  326.             end
  327.  
  328.             turtle.turnRight()
  329.             turtle.turnRight()
  330.             for i=1, chestDist, 1 do
  331.                 turtle.forward()
  332.             end
  333.             currentPos = currentPos + 1
  334.             run()
  335.         else
  336.             print("Incorrect input.")
  337.         end
  338.     end
  339. end
  340.  
  341. -- Run the mining loop
  342. function run()
  343.     while currentPos < length do
  344.         chestDist = (currentPos * 4 + 4)
  345.         mainShaft()
  346.         turtle.turnLeft()
  347.         sideShaft()
  348.         sideShaft()
  349.         turtle.turnLeft()
  350.         deposit()
  351.         currentPos = currentPos + 1
  352.     end
  353. end
  354.  
  355. -- Alert system for user, using the redstone torch
  356. function idle()
  357.     print("Mining Complete")
  358.     print("Type (end) to terminate program")
  359.     local input = "null"
  360.     rs.setOutput("right", true)
  361.     while input ~= "end" do
  362.         input = io.read()
  363.     end
  364.     rs.setOutput("right", false)
  365. end
  366.  
  367. --Execution
  368. print("----------------------------------")
  369. print("Branch Mine Turtle Activated")
  370. print("----------------------------------")
  371. sleep(2)
  372. print("Beginning Mine w. Following Specs:")
  373. print("Main Shaft: "..mainShaftDim)
  374. print("Side Shafts: "..sideShaftDim)
  375. print("Branch Frequency: "..shaftFreq)
  376. print("----------------------------------")
  377.  
  378. print("Number of Shafts:")
  379. length = tonumber(read())
  380. currentPos = 0
  381. print("----------------------------------")
  382.  
  383. for x = length, 0, -1 do
  384.     totalCost = totalCost + (108 + 2 * (4 * x + 1))
  385. end
  386.  
  387.  
  388. print("Current Fuel: "..turtle.getFuelLevel())
  389. print("Fuel cost: "..totalCost)
  390. print("Coal cost: "..(totalCost/80))
  391. print("Lava cost: "..(totalCost/1000))
  392. sleep(2)
  393. print("----------------------------------")
  394. print("Would you like to refuel now (y/n)?")
  395. input = io.read()
  396. if input == "y" then
  397.     refuel()
  398.     print("Refueled.")
  399. else
  400.     print("Did not refuel.")
  401. end
  402. print("Turtle now has "..turtle.getFuelLevel().." fuel.")
  403. print("----------------------------------")
  404.  
  405. print("Now beginning mining!")
  406.  
  407. run()
  408. turtle.turnRight()
  409. turtle.turnRight()
  410. for i=1, chestDist, 1 do
  411.     turtle.forward()
  412. end
  413. idle()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement