Guest User

Untitled

a guest
Oct 18th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.51 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. runInit = false
  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 tArgs[1] == "help" then
  31.         term.clear()
  32.         print("Usage: shaft [length] [blocks]")
  33.         print("Length is the length of the mining shaft.")
  34.         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")
  35.         print("Enter for more...")
  36.         io.read()
  37.         term.clear()
  38.         print("Torches are placed every 8 blocks and go 1 slot before the ores that won't be mined, by default 13.")
  39.         print("Place chest underneath the starting position.")
  40.         print("use 'shaft init' to make a starting room")
  41.         print("Enter to continue.")
  42.         io.read()
  43.     elseif tArgs[1] == "init" then
  44.         runInit = true
  45.     else
  46.         len = tonumber( tArgs[1] )
  47.     end
  48. elseif #tArgs == 2 then
  49.     len = tonumber( tArgs[1] )
  50.     notDig = tonumber ( tArgs[2] )
  51. end
  52.  
  53. --refueling
  54. function refuel(need, message)
  55.     while turtle.getFuelLevel() < (need) do
  56.         for n=1,(16-notDig) do
  57.             local nCount = turtle.getItemCount(n)
  58.             if nCount > 0 then
  59.                 turtle.select(n)
  60.                 refueled = turtle.refuel(1)
  61.                 while turtle.getFuelLevel() < need and refueled == true do
  62.                     refueled = turtle.refuel(1)
  63.                     if turtle.getFuelLevel() > need and refueled == true then
  64.                         break
  65.                     end
  66.                 end
  67.             end
  68.         end
  69.         if turtle.getFuelLevel() < need and isAtHome == true then
  70.             print(message)
  71.             io.read()
  72.             refuel(need,message)
  73.         elseif turtle.getFuelLevel() < need and isAtHome == false then
  74.             goHome()
  75.             refuel(need,message)
  76.             goBack()
  77.         end
  78.         clrScr()
  79.         print("Turtle refuled. Returning to work...")
  80.     end
  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.turnLeft()
  90.     digAndMove()
  91.     turtle.turnLeft()
  92.     digAndMove()
  93.     digAndMove()
  94.     turtle.digDown()
  95.     turtle.down()
  96.     turtle.turnLeft()
  97.     turtle.turnLeft()
  98.     digAndMove()
  99.     digAndMove()
  100.     turtle.turnRight()
  101.     digAndMove()
  102.     turtle.turnLeft()
  103.     turtle.back()
  104.     turtle.turnLeft()
  105.     turtle.place()
  106.     turtle.turnRight()
  107. end
  108.  
  109. --check if blocks that should not be mined and torches are inserted
  110. function validateInv()
  111.     clrScr()
  112.     while turtle.getItemCount(torch) < 2 do
  113.         print("")
  114.         print("Please put torches into slot ",torch," and press enter to continue.")
  115.         io.read()
  116.     end
  117.     while not blocksPresent do
  118.         blocksPresent = true
  119.         for i = 17-notDig, 16 do
  120.             if turtle.getItemCount(i) == 0 then blocksPresent = false end
  121.         end
  122.         if not blocksPresent then
  123.             print("Please put at least 1 block into slots ",17-notDig,"-16 for the items you don't want to mine and press enter.")
  124.             io.read()
  125.         end
  126.     end
  127.     clrScr()
  128. end
  129.  
  130. --torch placement
  131. function placeTorch()
  132.     turtle.select(torch)
  133.     i = curDist
  134.     while i > 0 do
  135.         i = i-8
  136.     end
  137.     if turtle.getItemCount(torch) < 2 then
  138.         clrScr()
  139.         print("Almost out of torches! Returning home...")
  140.         goHome()
  141.         emptyInventory()
  142.         while turtle.getItemCount(torch) < 2 do
  143.             print("")
  144.             print("Please insert torches into slot ",torch," and press enter.")
  145.             io.read()
  146.         end
  147.         refuel(curDist*2 + 95, "Not enough fuel to return to job. Insert fuel and press enter.")
  148.         clrScr()
  149.         print("Turtle ready! Resuming work...")
  150.         goBack()
  151.     end
  152.     if i == 0 then
  153.         turtle.placeUp()
  154.     end
  155. end
  156.  
  157. --return home
  158. function goHome()
  159.     if top then
  160.         turtle.turnLeft()
  161.         turtle.turnLeft()
  162.     else
  163.         turtle.up()
  164.     end
  165.     for i = 1, curDist do
  166.         turtle.forward()
  167.     end
  168.     turtle.down()
  169.     isAtHome = true
  170. end
  171.  
  172. --go back to work you lazy sucker!
  173. function goBack()
  174.     turtle.up()
  175.     for i = 1, curDist do
  176.         turtle.back()
  177.     end
  178.     if top then
  179.         turtle.turnLeft()
  180.         turtle.turnLeft()
  181.     else
  182.         turtle.down()
  183.     end
  184.     isAtHome = false
  185. end
  186.  
  187. --check if there's a need to empty the inventory
  188. function checkInventory()
  189.     drop = 15-notDig
  190.     tempCounter = 0
  191.     for i = 1, drop do
  192.         if turtle.getItemCount(i) > 0 then tempCounter = tempCounter + 1 end
  193.     end
  194.     if tempCounter == drop then
  195.         clrScr()
  196.         print("Inventory full, returning home.")
  197.         goHome()
  198.         emptyInventory()
  199.         refuel(curDist*2+95, "Not enough fuel to return to work. Insert fuel and press enter.")
  200.         clrScr()
  201.         print("Turtle ready! Resuming work...")
  202.         goBack()
  203.     end
  204. end
  205.  
  206. --function that moves forward and digs if there's an obstacle
  207. function digAndMove()
  208.   while not turtle.forward() do
  209.     sleep(0.5)
  210.     turtle.dig()
  211.   end
  212. end
  213.  
  214. --function to clean inventory
  215. function emptyInventory()
  216.     turtle.turnRight()
  217.     for i = 1, 15-notDig do
  218.         turtle.select(i)
  219.         if turtle.getItemCount(i) > 0 then
  220.             while not turtle.drop() do
  221.                 print("")
  222.                 print("Chest full. Please empty chest and press enter to continue.")
  223.                 io.read()
  224.             end
  225.         end
  226.     end
  227.     turtle.turnLeft()
  228. end
  229.  
  230. --special case for mining up
  231. function mineUp()
  232.     while not turtle.up() do
  233.       turtle.digUp()
  234.       sleep(0.5)
  235.     end
  236. end
  237.  
  238. --check for ore below miner
  239. function checkDown()
  240.   ore = true
  241.   if turtle.detectDown() then
  242.     for i = 17-notDig, 16 do
  243.         turtle.select(i)
  244.         if turtle.compareDown() then
  245.             ore = false
  246.         end
  247.     end
  248.   else
  249.     ore = false
  250.   end
  251.   return ore
  252. end
  253.  
  254. --check for ore above miner
  255. function checkUp()
  256.   ore = true
  257.   if turtle.detectUp() then
  258.     for i = 17-notDig, 16 do
  259.         turtle.select(i)
  260.         if turtle.compareUp() then
  261.             ore = false
  262.         end
  263.     end
  264.   else
  265.     ore = false
  266.   end
  267.   return ore
  268. end
  269.  
  270. --check for ore in front miner
  271. function checkFront()
  272.   ore = true
  273.   if turtle.detect() then
  274.     for i = 17-notDig, 16 do
  275.         turtle.select(i)
  276.         if turtle.compare() then
  277.             ore = false
  278.         end
  279.     end
  280.   else
  281.     ore = false
  282.   end
  283.   return ore
  284. end
  285.  
  286. --check the 6 sides around the miner and mine if you find ore, then move in and repeat
  287. function mineOre()
  288.   if checkDown() then
  289.     turtle.digDown()
  290.     turtle.down()
  291.     mineOre()
  292.     turtle.up()
  293.   end
  294.   if checkUp() then
  295.     mineUp()
  296.     mineOre()
  297.     turtle.down()
  298.   end
  299.   if checkFront() then
  300.     digAndMove()
  301.     mineOre()
  302.     turtle.back()
  303.   end  
  304.   turtle.turnRight()
  305.   if checkFront() then
  306.     digAndMove()
  307.     mineOre()
  308.     turtle.back()
  309.   end  
  310.   turtle.turnRight()
  311.   if checkFront() then
  312.     digAndMove()
  313.     mineOre()
  314.     turtle.back()
  315.   end  
  316.   turtle.turnRight()
  317.   if checkFront() then
  318.     digAndMove()
  319.     mineOre()
  320.     turtle.back()
  321.   end
  322.   turtle.turnRight()
  323. end
  324.  
  325. --main program
  326. function main()
  327.     clrScr()
  328.     print("Orders received.")
  329.     print("Starting to mine a shaft ",len," long with ",notDig," block exceptions.")
  330.    
  331.     --validate the inventory before starting
  332.     refuel(95, "Input fuel and press enter!")
  333.     validateInv()
  334.    
  335.     --go up 1 as we will start mining at the top row
  336.     isAtHome = false
  337.     mineUp()
  338.     mineOre()
  339.  
  340.     --start diging the top and checking it for ore and refueling if needed
  341.     top = true
  342.     for i = 1, len do
  343.         checkInventory()
  344.         refuel(curDist+95, "Out of fuel, please refil and press enter.")
  345.         digAndMove()
  346.         mineOre()
  347.         curDist = curDist + 1
  348.     end
  349.    
  350.     --move down to bottom row
  351.     turtle.digDown()
  352.     turtle.down()
  353.     turtle.turnLeft()
  354.     turtle.turnLeft()
  355.     top = false
  356.     mineOre()
  357.    
  358.     --now dig the bottom while, again, checking for ore and fuel but also place down torches
  359.     for i = 1, (len-1) do
  360.         checkInventory()
  361.         refuel(curDist+95, "Out of fuel, please refil and press enter.")
  362.         digAndMove()
  363.         mineOre()
  364.         curDist = curDist - 1
  365.         placeTorch()
  366.        
  367.     end
  368.    
  369.     turtle.forward()
  370.     emptyInventory()
  371.     turtle.turnLeft()
  372.     turtle.turnLeft()
  373.     isAtHome = true
  374.    
  375.     clrScr()
  376.     print("Work complete!")
  377. end
  378.  
  379. --choose what to run
  380. clrScr()
  381. if runInit then
  382.     turtle.select(1)
  383.     while turtle.getItemCount(1) < 1 do
  384.         print("Please insert chest into slot 1 and press enter")
  385.         io.read()
  386.     end
  387.     initRoom()
  388.     clrScr()
  389.     print("Startup location ready!")
  390. elseif tArgs[1] == "help" then
  391.     print("")
  392. else
  393.     if len > 0 and notDig < 11 and #tArgs > 0 and #tArgs < 2 then
  394.         main()
  395.     elseif #tArgs > 0 and #tArgs < 2 then
  396.         print("Shaft needs to be at least 1 long and blocks need to be less then 11 or empty.")
  397.     end
  398. end
Add Comment
Please, Sign In to add comment