Guest User

Shaft miner 1.1 for ComputerCraft Mining Turtles

a guest
Sep 7th, 2012
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.22 KB | None | 0 0
  1. --variables
  2. --len = shaft length
  3. local len = 1
  4. --notDig = number of blocks that are not mined in the shaft
  5. local notDig = 3
  6. --torch = location of torches
  7. local torch = 16 - notDig
  8. --curDist = distance from start
  9. local curDist = 0
  10. --top = bool for checking if the turtle is mining the top ot bot level of the shaft
  11. local top = false
  12. --run init instead of the main program
  13. runProgram = "main"
  14. --isAtHome = remember if you're at home
  15. isAtHome = true
  16.  
  17.  
  18. --clear the screen
  19. function clrScr()
  20.     term.clear()
  21.     term.setCursorPos(1,1)
  22. end
  23.  
  24. --argument parsing
  25. local tArgs = {...}
  26. if #tArgs > 2 or #tArgs == 0 then
  27.     term.clear()
  28.     print( "Type 'shaft help' for instructions" )
  29. elseif #tArgs == 1 then
  30.     if tonumber(tArgs[1]) ~= nil then
  31.         runProgram = "main"
  32.         len = tonumber( tArgs[1] )
  33.     else
  34.         runProgram = tArgs[1]
  35.     end
  36. elseif #tArgs == 2 then
  37.     len = tonumber( tArgs[1] )
  38.     notDig = tonumber ( tArgs[2] )
  39.     torch = 16 - notDig
  40. end
  41.  
  42. --
  43. function runHelp()
  44.     clrScr()
  45.     print("Usage: shaft [length] [blocks]")
  46.     print("Length is the length of the mining shaft.")
  47.     print("Blocks is the number of blocks that will not be mined [range: 0-10]. By default it's 3 for stone, gravel and dirt and they need to be put in slots 16, 15 and 14")
  48.     print("Enter for more...")
  49.     io.read()
  50.     term.clear()
  51.     print("Torches are placed every 8 blocks and go 1 slot before the ores that won't be mined, by default 13.")
  52.     print("Place chest underneath the starting position.")
  53.     print("use 'shaft init' to make a starting room")
  54.     print("Enter to continue.")
  55.     io.read()
  56. end
  57.  
  58. --refueling
  59. function refuel(need, message)
  60.     while turtle.getFuelLevel() < (need) do
  61.         for n=1,(16-notDig) do
  62.             local nCount = turtle.getItemCount(n)
  63.             if nCount > 0 then
  64.                 turtle.select(n)
  65.                 refueled = turtle.refuel(1)
  66.                 while turtle.getFuelLevel() < need and refueled == true do
  67.                     refueled = turtle.refuel(1)
  68.                     if turtle.getFuelLevel() > need and refueled == true then
  69.                         break
  70.                     end
  71.                 end
  72.             end
  73.         end
  74.         if turtle.getFuelLevel() < need and isAtHome == true then
  75.             print(message)
  76.             print("Fuel level: ",turtle.getFuelLevel())
  77.             print("Mining distance: ",curDist)
  78.             io.read()
  79.             refuel(need,message)
  80.         elseif turtle.getFuelLevel() < need and isAtHome == false then
  81.             goHome()
  82.             emptyInventory()
  83.             refuel(need,message)
  84.             goBack()
  85.         end
  86.         clrScr()
  87.         print("Turtle refuled. Returning to work...")
  88.     end
  89. end
  90.  
  91. --build the starting room
  92. function initRoom()
  93.     refuel(30, "Please input fuel to continue and press enter.")
  94.     turtle.select(1)
  95.     mineUp()
  96.     digAndMove()
  97.     turtle.turnLeft()
  98.     digAndMove()
  99.     turtle.turnLeft()
  100.     digAndMove()
  101.     digAndMove()
  102.     turtle.turnLeft()
  103.     digAndMove()
  104.     turtle.turnLeft()
  105.     digAndMove()
  106.     digAndMove()
  107.     turtle.turnRight()
  108.     digAndMove()
  109.     turtle.turnRight()
  110.     digAndMove()
  111.     digAndMove()
  112.     turtle.turnRight()
  113.     turtle.digDown()
  114.     turtle.down()
  115.     digAndMove()
  116.     digAndMove()
  117.     turtle.turnRight()
  118.     digAndMove()
  119.     turtle.turnRight()
  120.     digAndMove()
  121.     digAndMove()
  122.     turtle.turnLeft()
  123.     digAndMove()
  124.     turtle.turnLeft()
  125.     digAndMove()
  126.     digAndMove()
  127.     turtle.back()
  128.     turtle.turnRight()
  129.     turtle.back()
  130.    
  131.     turtle.turnLeft()
  132.     turtle.place()
  133.     turtle.turnRight()
  134. end
  135.  
  136. --check if blocks that should not be mined and torches are inserted
  137. function validateInv()
  138.     clrScr()
  139.     while turtle.getItemCount(torch) < 2 do
  140.         print("")
  141.         print("Please put torches into slot ",torch," and press enter to continue.")
  142.         io.read()
  143.     end
  144.     while not blocksPresent do
  145.         blocksPresent = true
  146.         for i = 17-notDig, 16 do
  147.             if turtle.getItemCount(i) == 0 then blocksPresent = false end
  148.         end
  149.         if not blocksPresent then
  150.             print("Please put at least 1 block into slots ",17-notDig,"-16 for the items you don't want to mine and press enter.")
  151.             io.read()
  152.         end
  153.     end
  154.     clrScr()
  155. end
  156.  
  157. --torch placement
  158. function placeTorch()
  159.     turtle.select(torch)
  160.     i = curDist
  161.     while i > 0 do
  162.         i = i-8
  163.     end
  164.     if turtle.getItemCount(torch) < 2 then
  165.         clrScr()
  166.         print("Almost out of torches! Returning home...")
  167.         goHome()
  168.         emptyInventory()
  169.         while turtle.getItemCount(torch) < 2 do
  170.             print("")
  171.             print("Please insert torches into slot ",torch," and press enter.")
  172.             io.read()
  173.         end
  174.         refuel(curDist*2 + 150, "Not enough fuel to return to job. Insert fuel and press enter.")
  175.         clrScr()
  176.         print("Turtle ready! Resuming work...")
  177.         goBack()
  178.     end
  179.     if i == 0 then
  180.         turtle.placeUp()
  181.     end
  182. end
  183.  
  184. --return home
  185. function goHome()
  186.     if top then
  187.         turtle.turnLeft()
  188.         turtle.turnLeft()
  189.     else
  190.         turtle.up()
  191.     end
  192.     for i = 1, curDist do
  193.         turtle.forward()
  194.     end
  195.     turtle.down()
  196.     isAtHome = true
  197. end
  198.  
  199. --go back to work you lazy sucker!
  200. function goBack()
  201.     turtle.up()
  202.     for i = 1, curDist do
  203.         turtle.back()
  204.     end
  205.     if top then
  206.         turtle.turnLeft()
  207.         turtle.turnLeft()
  208.     else
  209.         turtle.down()
  210.     end
  211.     isAtHome = false
  212. end
  213.  
  214. --check if there's a need to empty the inventory
  215. function checkInventory()
  216.     drop = 15-notDig
  217.     tempCounter = 0
  218.     for i = 1, drop do
  219.         if turtle.getItemCount(i) > 0 then tempCounter = tempCounter + 1 end
  220.     end
  221.     if tempCounter == drop then
  222.         clrScr()
  223.         print("Inventory full, returning home.")
  224.         goHome()
  225.         emptyInventory()
  226.         refuel(curDist*2 + 150, "Not enough fuel to return to work. Insert fuel and press enter.")
  227.         clrScr()
  228.         print("Turtle ready! Resuming work...")
  229.         goBack()
  230.     end
  231. end
  232.  
  233. --function that moves forward and digs if there's an obstacle
  234. function digAndMove()
  235.   while not turtle.forward() do
  236.     sleep(0.5)
  237.     turtle.dig()
  238.   end
  239. end
  240.  
  241. --function to clean inventory
  242. function emptyInventory()
  243.     turtle.turnRight()
  244.     for i = 1, 15-notDig do
  245.         turtle.select(i)
  246.         if turtle.getItemCount(i) > 0 then
  247.             while not turtle.drop() do
  248.                 print("")
  249.                 print("Chest full. Please empty chest and press enter to continue.")
  250.                 io.read()
  251.             end
  252.         end
  253.     end
  254.     turtle.turnLeft()
  255. end
  256.  
  257. --special case for mining up
  258. function mineUp()
  259.     while not turtle.up() do
  260.       turtle.digUp()
  261.       sleep(0.5)
  262.     end
  263. end
  264.  
  265. --check for ore below miner
  266. function checkDown()
  267.   ore = true
  268.   if turtle.detectDown() then
  269.     for i = 17-notDig, 16 do
  270.         turtle.select(i)
  271.         if turtle.compareDown() then
  272.             ore = false
  273.         end
  274.     end
  275.   else
  276.     ore = false
  277.   end
  278.   return ore
  279. end
  280.  
  281. --check for ore above miner
  282. function checkUp()
  283.   ore = true
  284.   if turtle.detectUp() then
  285.     for i = 17-notDig, 16 do
  286.         turtle.select(i)
  287.         if turtle.compareUp() then
  288.             ore = false
  289.         end
  290.     end
  291.   else
  292.     ore = false
  293.   end
  294.   return ore
  295. end
  296.  
  297. --check for ore in front miner
  298. function checkFront()
  299.   ore = true
  300.   if turtle.detect() then
  301.     for i = 17-notDig, 16 do
  302.         turtle.select(i)
  303.         if turtle.compare() then
  304.             ore = false
  305.         end
  306.     end
  307.   else
  308.     ore = false
  309.   end
  310.   return ore
  311. end
  312.  
  313. --check the 6 sides around the miner and mine if you find ore, then move in and repeat
  314. function mineOre()
  315.   if checkDown() then
  316.     turtle.digDown()
  317.     turtle.down()
  318.     mineOre()
  319.     turtle.up()
  320.   end
  321.   if checkUp() then
  322.     mineUp()
  323.     mineOre()
  324.     turtle.down()
  325.   end
  326.   if checkFront() then
  327.     digAndMove()
  328.     mineOre()
  329.     turtle.back()
  330.   end  
  331.   turtle.turnRight()
  332.   if checkFront() then
  333.     digAndMove()
  334.     mineOre()
  335.     turtle.back()
  336.   end  
  337.   turtle.turnRight()
  338.   if checkFront() then
  339.     digAndMove()
  340.     mineOre()
  341.     turtle.back()
  342.   end  
  343.   turtle.turnRight()
  344.   if checkFront() then
  345.     digAndMove()
  346.     mineOre()
  347.     turtle.back()
  348.   end
  349.   turtle.turnRight()
  350. end
  351.  
  352. --main diging program
  353. function mainDig()
  354.     clrScr()
  355.     print("Orders received.")
  356.     print("Starting to mine a shaft ",len," long with ",notDig," block exceptions.")
  357.    
  358.     --validate the inventory before starting
  359.     refuel(95, "Input fuel and press enter!")
  360.     validateInv()
  361.    
  362.     --go up 1 as we will start mining at the top row
  363.     isAtHome = false
  364.     mineUp()
  365.     mineOre()
  366.  
  367.     --start diging the top and checking it for ore and refueling if needed
  368.     top = true
  369.     for i = 1, len do
  370.         checkInventory()
  371.         refuel(curDist + 95, "Out of fuel, please refil and press enter.")
  372.         digAndMove()
  373.         mineOre()
  374.         curDist = curDist + 1
  375.     end
  376.    
  377.     --move down to bottom row
  378.     turtle.digDown()
  379.     turtle.down()
  380.     turtle.turnLeft()
  381.     turtle.turnLeft()
  382.     top = false
  383.     mineOre()
  384.    
  385.     --now dig the bottom while, again, checking for ore and fuel but also place down torches
  386.     for i = 1, (len-1) do
  387.         checkInventory()
  388.         refuel(curDist + 95, "Out of fuel, please refil and press enter.")
  389.         digAndMove()
  390.         mineOre()
  391.         curDist = curDist - 1
  392.         placeTorch()
  393.        
  394.     end
  395.    
  396.     turtle.forward()
  397.     emptyInventory()
  398.     turtle.turnLeft()
  399.     turtle.turnLeft()
  400.     isAtHome = true
  401.    
  402.     clrScr()
  403.     print("Work complete!")
  404. end
  405.  
  406. --run init room
  407. function runInit()
  408.     turtle.select(1)
  409.     while turtle.getItemCount(1) < 1 do
  410.         print("Please insert chest into slot 1 and press enter")
  411.         io.read()
  412.     end
  413.     initRoom()
  414.     clrScr()
  415.     print("Startup location ready!")
  416. end
  417.  
  418. --setup for another shaft on the left
  419. function shaftLeft()
  420.     refuel(30, "Please input fuel to continue and press enter.")
  421.     turtle.turnLeft()
  422.     clrScr()
  423.     while turtle.getItemCount(1) < 1 do
  424.         print("Please insert chest into slot 1 and press enter")
  425.         io.read()
  426.     end
  427.     clrScr()
  428.     if turtle.detect() then
  429.         print("WARNING! Clear out the chest before moving on! This is your only warning, once you hit enter the turtle will move on!")
  430.         io.read()
  431.     end
  432.     clrScr()
  433.     print("Moving on to the left!")
  434.     mineUp()
  435.     for i = 1, 4 do
  436.         digAndMove()
  437.         turtle.turnLeft()
  438.         digAndMove()
  439.         turtle.back()
  440.         turtle.turnLeft()
  441.         turtle.turnLeft()
  442.         digAndMove()
  443.         turtle.back()
  444.         turtle.turnLeft()
  445.     end
  446.     turtle.digDown()
  447.     turtle.down()
  448.     turtle.turnRight()
  449.     runInit()
  450.     turtle.turnRight()
  451.     for i = 1, 4 do
  452.         digAndMove()
  453.         turtle.turnLeft()
  454.         digAndMove()
  455.         turtle.back()
  456.         turtle.turnLeft()
  457.         turtle.turnLeft()
  458.         digAndMove()
  459.         turtle.back()
  460.         turtle.turnLeft()
  461.     end
  462.     for i = 1, 4 do
  463.         turtle.back()
  464.     end
  465.     turtle.turnLeft()
  466.     clrScr()
  467.     print("Left expansion ready!")
  468. end
  469.  
  470. --setup for another shaft on the right
  471. function shaftRight()
  472.     refuel(30, "Please input fuel to continue and press enter.")
  473.     turtle.turnRight()
  474.     clrScr()
  475.     while turtle.getItemCount(1) < 1 do
  476.         print("Please insert chest into slot 1 and press enter")
  477.         io.read()
  478.     end
  479.     clrScr()
  480.     print("Moving on to the right!")
  481.     mineUp()
  482.     for i = 1, 4 do
  483.         digAndMove()
  484.         turtle.turnLeft()
  485.         digAndMove()
  486.         turtle.back()
  487.         turtle.turnLeft()
  488.         turtle.turnLeft()
  489.         digAndMove()
  490.         turtle.back()
  491.         turtle.turnLeft()
  492.     end
  493.     turtle.digDown()
  494.     turtle.down()
  495.     turtle.turnLeft()
  496.     runInit()
  497.     turtle.turnLeft()
  498.     for i = 1, 4 do
  499.         digAndMove()
  500.         turtle.turnLeft()
  501.         digAndMove()
  502.         turtle.back()
  503.         turtle.turnLeft()
  504.         turtle.turnLeft()
  505.         digAndMove()
  506.         turtle.back()
  507.         turtle.turnLeft()
  508.     end
  509.     for i = 1, 4 do
  510.         turtle.back()
  511.     end
  512.     turtle.place()
  513.     turtle.turnRight()
  514.     clrScr()
  515.     print("Right expansion ready!")
  516. end
  517.  
  518. --choose what to run
  519. function main()
  520.     clrScr()
  521.     if runProgram == "init" then
  522.         runInit()
  523.     elseif runProgram == "help" then
  524.         runHelp()
  525.     elseif runProgram == "left" then
  526.         shaftLeft()
  527.     elseif runProgram == "right" then
  528.         shaftRight()
  529.     else
  530.         if len > 0 and notDig < 11 and #tArgs > 0 and #tArgs < 2 then
  531.             mainDig()
  532.         elseif #tArgs > 0 and #tArgs < 2 then
  533.             print("Shaft needs to be at least 1 long and blocks need to be less then 11 or empty.")
  534.         end
  535.     end
  536. end
  537.  
  538.  
  539. --and in the end, just run the main! :)
  540. main()
Advertisement
Add Comment
Please, Sign In to add comment