Guest User

mine

a guest
Oct 21st, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.85 KB | None | 0 0
  1. -- Mining script
  2.  
  3. local mode = ""
  4. local size = 0
  5. local cycles = 0
  6. local currentCycle = 0
  7. local continue
  8.  
  9. function main()
  10.     continue = false
  11.    
  12.     writeMenu()
  13.    
  14.     if mode == "e" then
  15.    
  16.         if continue == false then
  17.             checkEngineeringInventory()
  18.         end
  19.        
  20.         print("")
  21.         print("Engineering turtle starting")
  22.        
  23.         for currentCycle = 1, cycles, 1 do
  24.             if continue == true then
  25.                 continue = false
  26.                 engineering2()
  27.             else
  28.                 engineering1()
  29.             end
  30.  
  31.             saveSettings()
  32.             print("Finished cycle "..currentCycle.."/"..cycles)
  33.         end
  34.        
  35.     elseif mode == "m" then
  36.    
  37.         if continue == false then
  38.             checkMiningInventory()
  39.         end
  40.        
  41.         print("")
  42.         print("Mining turtle starting")
  43.  
  44.         for currentCycle = 1, cycles, 1 do
  45.             if continue == true then
  46.                 continue = false
  47.                 mining2()
  48.             else
  49.                 mining1()
  50.             end
  51.  
  52.             saveSettings()
  53.             print("Finished cycle "..currentCycle.."/"..cycles)
  54.         end
  55.        
  56.     end
  57. end
  58.  
  59. function writeMenu()
  60.     local f = 0;
  61.     local input = "";
  62.    
  63.     term.clear()
  64.     term.setCursorPos(1, 1)
  65.    
  66.     print("Turtle miner v0.1")
  67.     print("-----------------")
  68.    
  69.     f = checkFuel()
  70.    
  71.     if f == false then
  72.         print("Please feed me a bit of fuel!")
  73.         return
  74.     else
  75.         print(f.." fuel remaining!")
  76.     end
  77.    
  78.     if peripheral.getType("right") ~= "chunkLoader" then
  79.         print("No chunkloading module found!")
  80.     end
  81.    
  82.     print("")
  83.    
  84.     if peripheral.getType("bottom") == "turtle" or peripheral.getType("front") == "tesseract" then
  85.         print("Continue?")
  86.         print("1. Yes")
  87.         print("2. No")
  88.         input = io.read()
  89.         continue = true
  90.     end
  91.    
  92.     if input == "1" then
  93.         loadSettings()
  94.        
  95.         print("Mode: "..mode)
  96.         print("Size: "..size)
  97.         print("Cycles: "..cycles)
  98.         print("Current cycle: "..currentCycle)
  99.        
  100.         return
  101.     end
  102.    
  103.     print("Please select turtle function:")
  104.     print("1. Engineering turtle")
  105.     print("2. Mining turtle")
  106.     input = io.read()
  107.    
  108.     if input == "1" then
  109.         mode = "e"
  110.     elseif input == "2" then
  111.         mode = "m"
  112.     else
  113.         print("Invalid input!")
  114.         sleep(2)
  115.         writeMenu()
  116.     end
  117.    
  118.     term.clear()
  119.     term.setCursorPos(1, 1)
  120.    
  121.     -- Ask for the size of the mining line
  122.     print("How many miners do you want to deploy?")
  123.     input = io.read()
  124.    
  125.     if tonumber(input) < 1 then
  126.         print("Have to use more than 0 miners!")
  127.         sleep(2)
  128.         writeMenu()
  129.     elseif tonumber(input) > 63 then
  130.         print("Cannot use more than 63 miners!")
  131.         sleep(2)
  132.         writeMenu()
  133.     else
  134.         size = tonumber(input)
  135.     end
  136.    
  137.     -- Ask for the number of cycles to make
  138.     print("How many cycles do yo want to make?")
  139.     input = io.read()
  140.    
  141.     if tonumber(input) < 1 then
  142.         print("You have to make at least 1 cycle!")
  143.         sleep(2)
  144.         writeMenu()
  145.     else
  146.         cycles = tonumber(input)
  147.     end
  148.    
  149.     saveSettings()
  150. end
  151.  
  152. function engineering1()
  153.     -- Wait for the ender chest to fuel up the turtle
  154.    
  155.     turtle.turnRight()
  156.     waitFor("front", "ender_chest")
  157.    
  158.     if turtle.getFuelLevel() < 1000 then
  159.         print("Refueling turtle")
  160.         turtle.select(16)
  161.         suck()
  162.         turtle.refuel()
  163.     end
  164.    
  165.     turtle.turnLeft()
  166.    
  167.     -- Move to the end of the line without placing tesseracts
  168.     forward(size, false)
  169.    
  170.     -- Wait for the 1st pipe to be laid
  171.     turtle.turnRight()
  172.  
  173.     waitFor("front", "conveyor_belt")
  174.     sleep(2)
  175.     turtle.attack()
  176.     sleep(1)
  177.     turtle.attack()
  178.     sleep(1)
  179.     turtle.turnRight();
  180.    
  181.     -- Place all the redstone energy conduits
  182.  
  183.     turtle.select(2)
  184.     for i = 1, size, 1 do
  185.         placeDown()
  186.         forward(1, false)
  187.     end
  188.    
  189.     -- Place a last redstone energy conduit
  190.     placeDown()
  191.    
  192.     -- Place down the energy tesseract
  193.     forward(1, false)
  194.     turtle.select(3)
  195.     placeDown()
  196.    
  197.     -- Go back and wrench the energy conduit
  198.     turnAround()
  199.     forward(1, false)
  200.     turtle.attackDown()
  201.    
  202.     -- Move to the first minig well to monitor the status
  203.     turtle.turnRight()
  204.     forward(1, false)
  205.     turtle.turnRight()
  206.    
  207.     -- Place the item tesseract in front of the turtle
  208.     turtle.select(1)
  209.     place(false)
  210.    
  211.     -- Wait for the mining wells to start up
  212.    
  213.     while turtle.getItemCount(1) == 0 do
  214.         sleep(1)
  215.     end
  216.    
  217.     engineering2()
  218. end
  219.  
  220. function engineering2()
  221.     print("Waiting for wells to finish")
  222.     waitForMiningWellsToFinish()
  223.    
  224.     -- Mining well finished, get the item tesseract
  225.     turtle.select(1)
  226.     turtle.dig()
  227.    
  228.     forward(1,false)
  229.    
  230.     turtle.turnRight()
  231.     -- pick up the energy tesseract
  232.     turtle.select(3)
  233.     turtle.forward(1, false)
  234.     turtle.turnRight()
  235.     turtle.digDown()
  236.    
  237.     -- Pickup the energy conduits
  238.     turtle.select(2)
  239.  
  240.     forward(1, false)
  241.     turtle.digDown()
  242.     forward(1, false)
  243.    
  244.     for i = 1, size, 1 do
  245.         turtle.digDown()
  246.         forward(1, false)
  247.     end
  248.    
  249.     turnAround()
  250.     for i = 1, size, 1 do
  251.         forward(1, false)
  252.     end
  253.    
  254.     turtle.forward()
  255.    
  256.     turtle.turnLeft()
  257.     turtle.forward()
  258.     turtle.turnLeft()
  259. end
  260.  
  261. function mining1()
  262.     print("Waiting for engineering turtle on the left")
  263.     waitFor("left", "turtle")
  264.  
  265.     -- Fuel up the turtle first
  266.     up(1, true)
  267.     turtle.select(4)
  268.     placeDown()
  269.    
  270.     if turtle.getFuelLevel() < 10000 then
  271.         print("refuelling!")
  272.         turtle.select(4)
  273.         suckDown()
  274.         turtle.refuel()
  275.     end
  276.    
  277.     sleep(1)
  278.     turtle.select(3)
  279.     turtle.digDown()
  280.    
  281.     -- Move out the way for the item tesseract
  282.     forward(1, true)
  283.     down(1, false)
  284.    
  285.     -- Lay down the mining wells first
  286.     turtle.select(1)
  287.    
  288.     for i = 1, size, 1 do
  289.         placeDown()
  290.         forward(1, true)   
  291.     end
  292.    
  293.     turnAround()
  294.     up(1, true)
  295.     forward(1, true)
  296.    
  297.     -- Go back laying the transport pipes
  298.     for i = 1, size, 1 do
  299.         if i == 1 then
  300.             turtle.select(3)
  301.         else
  302.             turtle.select(2)
  303.         end
  304.        
  305.         placeDown()
  306.         forward(1, true)
  307.     end
  308.    
  309.     turnAround()
  310.    
  311.     mining2()
  312. end
  313.  
  314. function mining2()
  315.     waitFor("bottom", "turtle")
  316.     waitForToLeave("bottom", "turtle")
  317.     down(1, false)
  318.    
  319.     for i = 1, size, 1 do
  320.         forward(1, true)
  321.        
  322.         if i == size then
  323.             turtle.select(3)
  324.         else
  325.             turtle.select(2)
  326.         end
  327.        
  328.         turtle.dig()
  329.         turtle.select(1)
  330.         turtle.digDown()
  331.     end
  332.    
  333.     turtle.turnRight()
  334.     turtle.forward()
  335.     turtle.turnRight()
  336.    
  337.     for i = 1, size, 1 do
  338.         forward(1, false)
  339.     end
  340.    
  341.     turnAround()
  342. end
  343.  
  344. function checkEngineeringInventory()
  345.    
  346.     term.clear()
  347.     term.setCursorPos(1, 1)
  348.    
  349.     if turtle.getItemCount(1) == 1 then
  350.         print("Slot 1 contains an ITEM tesseract")
  351.     else
  352.         print("Place an ITEM tesseract in slot 1")
  353.         io.read()
  354.         checkEngineeringInventory()
  355.     end
  356.    
  357.     if turtle.getItemCount(2) == size + 1 then
  358.         print("Slot 2 contains enough energy conduits")
  359.     else
  360.         print("Place "..(size + 1).." energy conduits in slot 2")
  361.         io.read()
  362.         checkEngineeringInventory()
  363.     end
  364.    
  365.     if turtle.getItemCount(3) == 1 then
  366.         print("Slot 1 contains an ENERGY tesseract")
  367.     else
  368.         print("Place an ENERGY tesseract in slot 1")
  369.         io.read()
  370.         checkEngineeringInventory()
  371.     end
  372.    
  373.     for i = 4,16,1 do
  374.         if turtle.getItemCount(i) > 0 then
  375.             print("Slot "..i.." must be empty!")
  376.             io.read()
  377.             checkEngineeringInventory()
  378.         end
  379.     end
  380. end
  381.  
  382. function checkMiningInventory()
  383.    
  384.     term.clear()
  385.     term.setCursorPos(1, 1)
  386.    
  387.     if turtle.getItemCount(1) == size then
  388.         print("Slot 1 contains enough mining wells")
  389.     else
  390.         print("Place "..(size).." mining wells in slot 1")
  391.         io.read()
  392.         checkMiningInventory()
  393.     end
  394.    
  395.     if turtle.getItemCount(2) == size - 1 then
  396.         print("Slot 2 contains enough golden pipes")
  397.     else
  398.         print("Place "..(size - 1).." golden pipes slot 2")
  399.         io.read()
  400.         checkMiningInventory()
  401.     end
  402.    
  403.     if turtle.getItemCount(3) == 1 then
  404.         print("Slot 3 contains a iron pipe")
  405.     else
  406.         print("Place 1 iron pipe slot 3")
  407.         io.read()
  408.         checkMiningInventory()
  409.     end
  410.    
  411.     if turtle.getItemCount(4) == 1 then
  412.         print("Slot 4 contains ender chest with fuel")
  413.     else
  414.         print("Place 1 ender chest with fuel in slot 4")
  415.         io.read()
  416.         checkMiningInventory()
  417.     end
  418.    
  419.     for i = 5,16,1 do
  420.         if turtle.getItemCount(i) > 0 then
  421.             print("Slot "..i.." must be empty!")
  422.             io.read()
  423.             checkMiningInventory()
  424.         end
  425.     end
  426. end
  427.  
  428. function forward(places, destruct)
  429.     for i = 1, places, 1 do
  430.         if turtle.forward() == false then
  431.             while turtle.detect() do
  432.                 if destruct == true then
  433.                     turtle.dig()
  434.                 end
  435.                 sleep(1)
  436.             end
  437.             turtle.forward()
  438.         end
  439.     end
  440. end
  441.  
  442. function down(places, destruct)
  443.     for i = 1, places, 1 do
  444.         if turtle.down() == false then
  445.             while turtle.detectDown() do
  446.                 if destruct == true then
  447.                     turtle.digDown()
  448.                 end
  449.                 sleep(1)
  450.             end
  451.             turtle.down()
  452.         end
  453.     end
  454. end
  455.  
  456. function up(places, destruct)
  457.     for i = 1, places, 1 do
  458.         if turtle.up() == false then
  459.             while turtle.detectUp() do
  460.                 if destruct == true then
  461.                     turtle.digUp()
  462.                 end
  463.                 sleep(1)
  464.             end
  465.             turtle.up()
  466.         end
  467.     end
  468. end
  469.  
  470. function suck()
  471.     while turtle.suck() == false do
  472.         sleep(1)
  473.     end
  474. end
  475.  
  476. function suckUp()
  477.     while turtle.suckUp() == false do
  478.         sleep(1)
  479.     end
  480. end
  481.  
  482. function suckDown()
  483.     while turtle.suckDown() == false do
  484.         sleep(1)
  485.     end
  486. end
  487.  
  488. function place(destruct)
  489.     if turtle.place() == false then
  490.         while turtle.detect() do
  491.             if destruct == true then
  492.                 turtle.dig()
  493.             end
  494.             sleep(1)
  495.         end
  496.         turtle.place()
  497.     end
  498. end
  499.  
  500. function placeDown(destruct)
  501.     if turtle.placeDown() == false then
  502.         while turtle.detectDown() do
  503.             if destruct == true then
  504.                 turtle.digDown()
  505.             end
  506.             sleep(1)
  507.         end
  508.         turtle.placeDown()
  509.     end
  510. end
  511.  
  512. function placeUp(destruct)
  513.     if turtle.placeUp() == false then
  514.         while turtle.detectUp() do
  515.             if destruct == true then
  516.                 turtle.digUp()
  517.             end
  518.             sleep(1)
  519.         end
  520.         turtle.placeUp()
  521.     end
  522. end
  523.  
  524. function back(places, destruct)
  525.     turnAround()
  526.     forward(places, destruct)
  527.     turnAround()
  528. end
  529.  
  530. function turnAround()
  531.     turtle.turnLeft()
  532.     turtle.turnLeft()
  533. end
  534.  
  535. -- Wait for an item to the side to appear
  536. function waitFor(side, name)
  537.     while true do
  538.         if peripheral.getType(side) == name then
  539.             return;
  540.         else
  541.             sleep(1);
  542.         end
  543.     end
  544. end
  545.  
  546. -- Wait for an item to the side to be gone
  547. function waitForToLeave(side, name)
  548.     while true do
  549.         if peripheral.getType(side) ~= name then
  550.             return;
  551.         else
  552.             sleep(1);
  553.         end
  554.     end
  555. end
  556.  
  557. -- Wait for the mining wells to finish
  558. function waitForMiningWellsToFinish()
  559.     local timer = 0;
  560.     while true do
  561.         local c = 0;
  562.         for i = 1, 16, 1 do
  563.             if turtle.getItemCount(i) > 0 then
  564.                 c = c + turtle.getItemCount(i);
  565.                 turtle.select(i)
  566.                 turtle.drop()
  567.             end
  568.         end
  569.        
  570.         if c == 0 then
  571.             timer = timer + 1
  572.         else
  573.             timer = 0
  574.         end
  575.        
  576.         if timer > 4 then
  577.             return
  578.         end
  579.        
  580.         print(timer.." seconds no items found")
  581.         sleep(1)
  582.     end
  583. end
  584.  
  585. function checkFuel()
  586.     local fuelLevel = turtle.getFuelLevel()
  587.    
  588.     if fuelLevel < 100 then
  589.         return false
  590.     else
  591.         return fuelLevel
  592.     end
  593. end
  594.  
  595. function saveSettings()
  596.     fs.makeDir("saves")
  597.    
  598.     local file = fs.open("saves/mine.sav","w")
  599.     file.writeLine(mode)
  600.     file.writeLine(size)
  601.     file.writeLine(cycles)
  602.     file.writeLine(currentCycle)
  603.     file.close()
  604. end
  605.  
  606. function loadSettings()
  607.     fs.makeDir("saves")
  608.    
  609.     local file = fs.open("saves/mine.sav","r")
  610.     mode = file.readLine()
  611.     size = tonumber(file.readLine())
  612.     cycles = tonumber(file.readLine())
  613.     currentCycle = tonumber(file.readLine())
  614.     file.close()
  615. end
  616.  
  617. main()
Advertisement
Add Comment
Please, Sign In to add comment