jgonzales394

ComputerCraft mine program

Dec 15th, 2012
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. local tArgs={...}
  2. if #tArgs~=1 then
  3.     print("Usage: mine <diameter>")
  4.     print("ex: mine 10 will mine a 10x10x2 area")
  5.     return
  6. end
  7.  
  8. local size=tonumber(tArgs[1])
  9. if size<1 then
  10.     print("mine diameter must be positve")
  11.     return
  12. end
  13.  
  14. local torches
  15.  
  16. local function torch()
  17.     torches=turtle.getItemCount(1)
  18.     if torches==0 then
  19.         return false
  20.     end
  21.     return true
  22. end
  23.  
  24. while not torch() do
  25.     print("Please put torches in slot 1")
  26.     sleep(2)
  27. end
  28.  
  29. local collected=0
  30.  
  31. local function collect()
  32.     collected=collected+1
  33.     if math.fmod(collected,25)==0 then
  34.         print("Mined "..collected.." items")
  35.     end
  36. end
  37.  
  38. local function tryDig()
  39.     while turtle.detect() do
  40.         if turtle.dig() then
  41.             collect()
  42.             sleep(0.5)
  43.         else
  44.             return false
  45.         end
  46.     end
  47.     return true
  48. end
  49.  
  50. local function tryDigUp()
  51.     while turtle.detectUp() do
  52.         if turtle.digUp() then
  53.             collect()
  54.             sleep(0.5)
  55.         else
  56.             return false
  57.         end
  58.     end
  59.     return true
  60. end
  61.  
  62. local function refuel()
  63.     local fuelLevel=turtle.getFuelLevel()
  64.     if fuelLevel == "unlimited" or fuelLevel>0 then
  65.         return
  66.     end
  67.    
  68.     local function tryRefuel()
  69.         for n=1,16 do
  70.             if turtle.getItemCount(n)>0 then
  71.                 turtle.select(n)
  72.                 if turtle.refuel(1) then
  73.                     turtle.select(1)
  74.                     return true
  75.                 end
  76.             end
  77.         end
  78.         turtle.select(1)
  79.         return false
  80.     end
  81.    
  82.     if not tryRefuel() then
  83.         print("Add more fuel to continue.")
  84.         while not tryRefuel() do
  85.             sleep(1)
  86.         end
  87.         print("Resumming mine")
  88.     end
  89. end
  90.  
  91. local function tryForward()
  92.     refuel()
  93.     while not turtle.forward() do
  94.         if turtle.detect() then
  95.             if not tryDig() then
  96.                 return false
  97.             end
  98.         elseif turtle.attack() then
  99.             collect()
  100.         else
  101.             sleep( 0.5 )
  102.         end
  103.     end
  104.     return true
  105. end
  106.  
  107. local function placeTorch()
  108.     if torch() then
  109.         turtle.turnLeft()
  110.         turtle.turnLeft()
  111.         turtle.select(1)
  112.         turtle.place()
  113.         torches=torches-1
  114.         turtle.turnLeft()
  115.         turtle.turnLeft()
  116.     else
  117.         while not torch() do
  118.             sleep(2)
  119.         end
  120.         turtle.turnLeft()
  121.         turtle.turnLeft()
  122.         turtle.select(1)
  123.         turtle.place()
  124.         torches=torches-1
  125.         turtle.turnLeft()
  126.         turtle.turnLeft()
  127.     end
  128.     if torches==0 then
  129.         while not torch() do
  130.             print("Out of torches")
  131.             print("put torches in slot 1")
  132.             sleep(2)
  133.         end
  134.     end
  135. end
  136.  
  137. local function checkinv()
  138.     local inv=0
  139.    
  140.     for i=1,16 do
  141.         if turtle.getItemCount(i)>0 then
  142.             inv=inv+1
  143.         end
  144.     end
  145.    
  146.     if inv==16 then
  147.         return false
  148.     else
  149.         inv=0
  150.         return true
  151.     end
  152. end
  153.  
  154. local modN,modB
  155.  
  156. local function dig()
  157.     for b=1,size-1 do
  158.         modB=b%5
  159.         while not checkinv() do
  160.             print("Inventory full")
  161.             sleep(3)
  162.         end
  163.         if modB==0 and modN==0 then
  164.             placeTorch()
  165.             tryDigUp()
  166.             tryForward()
  167.             tryDigUp()
  168.         else
  169.             tryDigUp()
  170.             tryForward()
  171.             tryDigUp()
  172.         end
  173.         if b==size-1 and modN==0 then
  174.             placeTorch()
  175.         end
  176.     end
  177. end
  178.  
  179. local function left()
  180.     turtle.turnLeft()
  181.     tryDigUp()
  182.     tryForward()
  183.     tryDigUp()
  184.     turtle.turnLeft()
  185. end
  186.  
  187. local function right()
  188.     turtle.turnRight()
  189.     tryDigUp()
  190.     tryForward()
  191.     tryDigUp()
  192.     turtle.turnRight()
  193. end
  194.  
  195. print("Minning...")
  196.  
  197. local alternate=0
  198. local start=true
  199.  
  200. for n=1,size do
  201.     modN=n%7
  202.     if n==size or start then
  203.         modN=0
  204.         start=false
  205.     end
  206.     dig()
  207.     if n<size then
  208.         if math.fmod(n + alternate,2) == 0 then
  209.             right()
  210.         else
  211.             left()
  212.         end
  213.     end
  214. end
  215.  
  216. print("Minning Done")
  217. print("Mined a total of "..collected.." items")
Advertisement
Add Comment
Please, Sign In to add comment