Advertisement
Chronno

Computercraft New Excavate

Apr 20th, 2013
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.95 KB | None | 0 0
  1. --This program is a modification of the original excavate program in Computercraft.
  2. --It will run just like the original except will put items in an enderchest in slot 16
  3. --and pull fuel from another enderchest in slot 15.
  4.  
  5.  
  6. local tArgs = { ... }
  7. if #tArgs ~= 1 then
  8.     print( "Usage: excavate <diameter>" )
  9.     return
  10. end
  11.  
  12. -- Mine in a quarry pattern until we hit something we can't dig
  13. local size = tonumber( tArgs[1] )
  14. if size < 1 then
  15.     print( "Excavate diameter must be positive" )
  16.     return
  17. end
  18.    
  19. local depth = 0
  20. local unloaded = 0
  21. local collected = 0
  22.  
  23. local xPos,zPos = 0,0
  24. local xDir,zDir = 0,1
  25.  
  26. local goTo -- Filled in further down
  27. local refuel -- Filled in further down
  28.  
  29. function chronnoRefuel()
  30.     turtle.select(15)
  31.     turtle.placeUp()
  32.     turtle.suckUp()
  33.     turtle.refuel()
  34.     turtle.digUp()
  35.     turtle.select(1)
  36. end
  37.  
  38. local function unload( _bKeepOneFuelStack )
  39.     print( "Unloading items..." )
  40.     for n=1,14 do
  41.         local nCount = turtle.getItemCount(n)
  42.         if nCount > 0 then
  43.             turtle.select(n)
  44.             turtle.dropUp()
  45.         end
  46.     end
  47.     collected = 0
  48.     turtle.select(1)
  49. end
  50.  
  51. local function returnSupplies()
  52.     print( "Dumping inventory..." )
  53.     turtle.select(16)
  54.     turtle.placeUp()
  55.     unload(false)
  56.     print( "Resuming mining..." )
  57.     turtle.select(16)
  58.     turtle.digUp()
  59.     turtle.select(1)
  60. end
  61.  
  62. local function collect()   
  63.     local bFull = true
  64.     local nTotalItems = 0
  65.     for n=1,16 do
  66.         local nCount = turtle.getItemCount(n)
  67.         if nCount == 0 then
  68.             bFull = false
  69.         end
  70.         nTotalItems = nTotalItems + nCount
  71.     end
  72.    
  73.     if nTotalItems > collected then
  74.         collected = nTotalItems
  75.         if math.fmod(collected + unloaded, 50) == 0 then
  76.             print( "Mined "..(collected + unloaded).." items." )
  77.         end
  78.     end
  79.    
  80.     if bFull then
  81.         print( "No empty slots left." )
  82.         return false
  83.     end
  84.     return true
  85. end
  86.  
  87. function refuel( ammount )
  88.     local fuelLevel = turtle.getFuelLevel()
  89.     if fuelLevel == "unlimited" then
  90.         return true
  91.     end
  92.    
  93.     local needed = ammount or (xPos + zPos + depth + 1)
  94.     if turtle.getFuelLevel() < needed then
  95.         local fueled = false
  96.         chronnoRefuel()
  97.         turtle.select(1)
  98.         return false
  99.     end
  100.     return true
  101. end
  102.  
  103. local function tryForwards()
  104.     if not refuel() then
  105.         print( "Not enough Fuel" )
  106.         returnSupplies()
  107.     end
  108.    
  109.     while not turtle.forward() do
  110.         if turtle.detect() then
  111.             if turtle.dig() then
  112.                 if not collect() then
  113.                     returnSupplies()
  114.                 end
  115.             else
  116.                 return false
  117.             end
  118.         elseif turtle.attack() then
  119.             if not collect() then
  120.                 returnSupplies()
  121.             end
  122.         else
  123.             sleep( 0.5 )
  124.         end
  125.     end
  126.    
  127.     xPos = xPos + xDir
  128.     zPos = zPos + zDir
  129.     return true
  130. end
  131.  
  132. local function tryDown()
  133.     if not refuel() then
  134.         print( "Not enough Fuel" )
  135.         returnSupplies()
  136.     end
  137.    
  138.     while not turtle.down() do
  139.         if turtle.detectDown() then
  140.             if turtle.digDown() then
  141.                 if not collect() then
  142.                     returnSupplies()
  143.                 end
  144.             else
  145.                 return false
  146.             end
  147.         elseif turtle.attackDown() then
  148.             if not collect() then
  149.                 returnSupplies()
  150.             end
  151.         else
  152.             sleep( 0.5 )
  153.         end
  154.     end
  155.  
  156.     depth = depth + 1
  157.     if math.fmod( depth, 10 ) == 0 then
  158.         print( "Descended "..depth.." metres." )
  159.     end
  160.  
  161.     return true
  162. end
  163.  
  164. local function turnLeft()
  165.     turtle.turnLeft()
  166.     xDir, zDir = -zDir, xDir
  167. end
  168.  
  169. local function turnRight()
  170.     turtle.turnRight()
  171.     xDir, zDir = zDir, -xDir
  172. end
  173.  
  174. function goTo( x, y, z, xd, zd )
  175.     while depth > y do
  176.         if turtle.up() then
  177.             depth = depth - 1
  178.         elseif turtle.digUp() or turtle.attackUp() then
  179.             collect()
  180.         else
  181.             sleep( 0.5 )
  182.         end
  183.     end
  184.  
  185.     if xPos > x then
  186.         while xDir ~= -1 do
  187.             turnLeft()
  188.         end
  189.         while xPos > x do
  190.             if turtle.forward() then
  191.                 xPos = xPos - 1
  192.             elseif turtle.dig() or turtle.attack() then
  193.                 collect()
  194.             else
  195.                 sleep( 0.5 )
  196.             end
  197.         end
  198.     elseif xPos < x then
  199.         while xDir ~= 1 do
  200.             turnLeft()
  201.         end
  202.         while xPos < x do
  203.             if turtle.forward() then
  204.                 xPos = xPos + 1
  205.             elseif turtle.dig() or turtle.attack() then
  206.                 collect()
  207.             else
  208.                 sleep( 0.5 )
  209.             end
  210.         end
  211.     end
  212.    
  213.     if zPos > z then
  214.         while zDir ~= -1 do
  215.             turnLeft()
  216.         end
  217.         while zPos > z do
  218.             if turtle.forward() then
  219.                 zPos = zPos - 1
  220.             elseif turtle.dig() or turtle.attack() then
  221.                 collect()
  222.             else
  223.                 sleep( 0.5 )
  224.             end
  225.         end
  226.     elseif zPos < z then
  227.         while zDir ~= 1 do
  228.             turnLeft()
  229.         end
  230.         while zPos < z do
  231.             if turtle.forward() then
  232.                 zPos = zPos + 1
  233.             elseif turtle.dig() or turtle.attack() then
  234.                 collect()
  235.             else
  236.                 sleep( 0.5 )
  237.             end
  238.         end
  239.     end
  240.    
  241.     while depth < y do
  242.         if turtle.down() then
  243.             depth = depth + 1
  244.         elseif turtle.digDown() or turtle.attackDown() then
  245.             collect()
  246.         else
  247.             sleep( 0.5 )
  248.         end
  249.     end
  250.    
  251.     while zDir ~= zd or xDir ~= xd do
  252.         turnLeft()
  253.     end
  254. end
  255.  
  256. if not refuel() then
  257.     print( "Out of Fuel" )
  258.     return
  259. end
  260.  
  261. print( "Excavating..." )
  262.  
  263. local reseal = false
  264. turtle.select(1)
  265. if turtle.digDown() then
  266.     reseal = true
  267. end
  268.  
  269. local alternate = 0
  270. local done = false
  271. while not done do
  272.     for n=1,size do
  273.         for m=1,size-1 do
  274.             if not tryForwards() then
  275.                 done = true
  276.                 break
  277.             end
  278.         end
  279.         if done then
  280.             break
  281.         end
  282.         if n<size then
  283.             if math.fmod(n + alternate,2) == 0 then
  284.                 turnLeft()
  285.                 if not tryForwards() then
  286.                     done = true
  287.                     break
  288.                 end
  289.                 turnLeft()
  290.             else
  291.                 turnRight()
  292.                 if not tryForwards() then
  293.                     done = true
  294.                     break
  295.                 end
  296.                 turnRight()
  297.             end
  298.         end
  299.     end
  300.     if done then
  301.         break
  302.     end
  303.    
  304.     if size > 1 then
  305.         if math.fmod(size,2) == 0 then
  306.             turnRight()
  307.         else
  308.             if alternate == 0 then
  309.                 turnLeft()
  310.             else
  311.                 turnRight()
  312.             end
  313.             alternate = 1 - alternate
  314.         end
  315.     end
  316.    
  317.     if not tryDown() then
  318.         done = true
  319.         break
  320.     end
  321. end
  322.  
  323. print( "Returning to surface..." )
  324.  
  325. -- Return to where we started
  326. goTo( 0,0,0,0,-1 )
  327. turtle.select(16)
  328. turtle.placeUp()
  329. unload( false )
  330. turtle.select(16)
  331. turtle.digUp()
  332. turtle.select(1)
  333. goTo( 0,0,0,0,1 )
  334.  
  335. -- Seal the hole
  336. if reseal then
  337.     turtle.placeDown()
  338. end
  339.  
  340. print( "Mined "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement