Guest User

Shaft miner for ComputerCraft v1.5

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