GraySloth

ComputerCraft: 'Fell' excavates up for trees

Feb 6th, 2013
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.75 KB | None | 0 0
  1. --v1.1 by GraySloth 2013-Mar-14, new cc1.5 version. will probably not change much,
  2. --here is a video demonstration of my scripts, it is almost good. http://youtu.be/9vhl7dSENs8
  3. --based on the excavate program but goes up instead of down, great for jungle and ExtrabiomesXL trees
  4. local tArgs = { ... }
  5. if #tArgs ~= 1 then
  6.     print( "Usage: fell <diameter>" )
  7.     return
  8. end
  9.  
  10. -- the "timber" variable will prevent it from going up forever, by making it return once it has gone up two levels without digging up or forward
  11. local size = tonumber( tArgs[1] )
  12. if size < 1 then
  13.     print( "fell diameter must be positive" )
  14.     return
  15. end
  16.    
  17. local depth = 0
  18. local unloaded = 0
  19. local collected = 0
  20. local timber = 0
  21.  
  22. local xPos,zPos = 0,0
  23. local xDir,zDir = 0,1
  24.  
  25. local goTo -- Filled in further down
  26. local refuel -- Filled in further down
  27.  
  28. local function unload( _bKeepOneFuelStack )
  29.     print( "Unloading items..." )
  30.     for n=1,16 do
  31.         local nCount = turtle.getItemCount(n)
  32.         if nCount > 0 then
  33.             turtle.select(n)           
  34.             local bDrop = true
  35.             if _bKeepOneFuelStack and turtle.refuel(0) then
  36.                 bDrop = false
  37.                 _bKeepOneFuelStack = false
  38.             end        
  39.             if bDrop then
  40.                 turtle.drop()
  41.                 unloaded = unloaded + nCount
  42.             end
  43.         end
  44.     end
  45.     collected = 0
  46.     turtle.select(1)
  47. end
  48.  
  49. local function returnSupplies()
  50.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  51.     print( "Returning to ground level..." )
  52.     goTo( 0,0,0,0,-1 )
  53.    
  54.     local fuelNeeded = 2*(x+y+z) + 1
  55.     if not refuel( fuelNeeded ) then
  56.         unload( true )
  57.         print( "Waiting for fuel" )
  58.         while not refuel( fuelNeeded ) do
  59.             sleep(1)
  60.         end
  61.     else
  62.         unload( true ) 
  63.     end
  64.    
  65.     print( "Resuming mining..." )
  66.     goTo( x,y,z,xd,zd )
  67. end
  68.  
  69. local function collect()   
  70.     local bFull = true
  71.     local nTotalItems = 0
  72.     for n=1,16 do
  73.         local nCount = turtle.getItemCount(n)
  74.         if nCount == 0 then
  75.             bFull = false
  76.         end
  77.         nTotalItems = nTotalItems + nCount
  78.     end
  79.    
  80.     if nTotalItems > collected then
  81.         collected = nTotalItems
  82.         if math.fmod(collected + unloaded, 50) == 0 then
  83.             print( "Chopped "..(collected + unloaded).." items." )
  84.         end
  85.     end
  86.    
  87.     if bFull then
  88.         print( "No empty slots left." )
  89.         return false
  90.     end
  91.     return true
  92. end
  93.  
  94. function refuel( ammount )
  95.     local fuelLevel = turtle.getFuelLevel()
  96.     print( fuelLevel )
  97.     if fuelLevel == "unlimited" then
  98.         return true
  99.     end
  100.    
  101.     local needed = ammount or (xPos + zPos + depth + 1)
  102.     if turtle.getFuelLevel() < needed then
  103.         print( needed )
  104.         local fueled = false
  105.         for n=1,16 do
  106.             if turtle.getItemCount(n) > 0 then
  107.                 turtle.select(n)
  108.                 if turtle.refuel(1) then
  109.                     print( fuelLevel )
  110.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  111.                         turtle.refuel(1)
  112.                         print( fuelLevel )
  113.                     end
  114.                     if turtle.getFuelLevel() >= needed then
  115.                         turtle.select(1)
  116.                         return true
  117.                     end
  118.                 end
  119.             end
  120.         end
  121.         turtle.select(1)
  122.         return false
  123.     end
  124.    
  125.     return true
  126. end
  127.  
  128. local function tryForwards()
  129.     if not refuel() then
  130.         print( "Not enough Fuel" )
  131.         returnSupplies()
  132.     end
  133.    
  134.     while not turtle.forward() do
  135.         if turtle.detect() then
  136.             if turtle.dig() then
  137.                 timber = 0
  138.                 if not collect() then
  139.                     returnSupplies()
  140.                 end
  141.             else
  142.                 return false
  143.             end
  144.         elseif turtle.attack() then
  145.             if not collect() then
  146.                 returnSupplies()
  147.             end
  148.         else
  149.             sleep( 0.5 )
  150.         end
  151.     end
  152.    
  153.     xPos = xPos + xDir
  154.     zPos = zPos + zDir
  155.     return true
  156. end
  157.  
  158. local function tryUp()
  159.     if not refuel() then
  160.         print( "Not enough Fuel" )
  161.         returnSupplies()
  162.     end
  163.    
  164.     while not turtle.up() do
  165.         if turtle.detectUp() then
  166.             if turtle.digUp() then
  167.                 timber = 0
  168.                 if not collect() then
  169.                     returnSupplies()
  170.                 end
  171.             else
  172.                 return false
  173.             end
  174.         elseif turtle.attackUp() then
  175.             if not collect() then
  176.                 returnSupplies()
  177.             end
  178.         else
  179.             sleep( 0.5 )
  180.         end
  181.     end
  182.    
  183.  
  184.     depth = depth + 1
  185.     if math.fmod( depth, 10 ) == 0 then
  186.         print( "Ascended "..depth.." metres." )
  187.     end
  188.    
  189.     timber = timber + 1
  190.     if timber > 2 then
  191.         return false
  192.     end
  193.  
  194.     return true
  195. end
  196.  
  197. local function turnLeft()
  198.     turtle.turnLeft()
  199.     xDir, zDir = -zDir, xDir
  200. end
  201.  
  202. local function turnRight()
  203.     turtle.turnRight()
  204.     xDir, zDir = zDir, -xDir
  205. end
  206.  
  207. function goTo( x, y, z, xd, zd )
  208.     while depth > y do
  209.         if turtle.down() then
  210.             depth = depth - 1
  211.         elseif turtle.digDown() or turtle.attackDown() then
  212.             collect()
  213.         else
  214.             sleep( 0.5 )
  215.         end
  216.     end
  217.  
  218.     if xPos > x then
  219.         while xDir ~= -1 do
  220.             turnLeft()
  221.         end
  222.         while xPos > x do
  223.             if turtle.forward() then
  224.                 xPos = xPos - 1
  225.             elseif turtle.dig() or turtle.attack() then
  226.                 collect()
  227.             else
  228.                 sleep( 0.5 )
  229.             end
  230.         end
  231.     elseif xPos < x then
  232.         while xDir ~= 1 do
  233.             turnLeft()
  234.         end
  235.         while xPos < x do
  236.             if turtle.forward() then
  237.                 xPos = xPos + 1
  238.             elseif turtle.dig() or turtle.attack() then
  239.                 collect()
  240.             else
  241.                 sleep( 0.5 )
  242.             end
  243.         end
  244.     end
  245.    
  246.     if zPos > z then
  247.         while zDir ~= -1 do
  248.             turnLeft()
  249.         end
  250.         while zPos > z do
  251.             if turtle.forward() then
  252.                 zPos = zPos - 1
  253.             elseif turtle.dig() or turtle.attack() then
  254.                 collect()
  255.             else
  256.                 sleep( 0.5 )
  257.             end
  258.         end
  259.     elseif zPos < z then
  260.         while zDir ~= 1 do
  261.             turnLeft()
  262.         end
  263.         while zPos < z do
  264.             if turtle.forward() then
  265.                 zPos = zPos + 1
  266.             elseif turtle.dig() or turtle.attack() then
  267.                 collect()
  268.             else
  269.                 sleep( 0.5 )
  270.             end
  271.         end
  272.     end
  273.    
  274.     while depth < y do
  275.         if turtle.up() then
  276.             depth = depth + 1
  277.         elseif turtle.digUp() or turtle.attackUp() then
  278.             collect()
  279.         else
  280.             sleep( 0.5 )
  281.         end
  282.     end
  283.    
  284.     while zDir ~= zd or xDir ~= xd do
  285.         turnLeft()
  286.     end
  287. end
  288.  
  289. if not refuel() then
  290.     print( "Out of Fuel" )
  291.     return
  292. end
  293.  
  294. print( "Chopping..." )
  295.  
  296. local reseal = false
  297. turtle.select(1)
  298. if turtle.digUp() then
  299.     reseal = true
  300. end
  301.  
  302. local alternate = 0
  303. local done = false
  304. while not done do
  305.     for n=1,size do
  306.         for m=1,size-1 do
  307.             if not tryForwards() then
  308.                 done = true
  309.                 break
  310.             end
  311.         end
  312.         if done then
  313.             break
  314.         end
  315.         if n<size then
  316.             if math.fmod(n + alternate,2) == 0 then
  317.                 turnLeft()
  318.                 if not tryForwards() then
  319.                     done = true
  320.                     break
  321.                 end
  322.                 turnLeft()
  323.             else
  324.                 turnRight()
  325.                 if not tryForwards() then
  326.                     done = true
  327.                     break
  328.                 end
  329.                 turnRight()
  330.             end
  331.         end
  332.     end
  333.     if done then
  334.         break
  335.     end
  336.    
  337.     if size > 1 then
  338.         if math.fmod(size,2) == 0 then
  339.             turnRight()
  340.         else
  341.             if alternate == 0 then
  342.                 turnLeft()
  343.             else
  344.                 turnRight()
  345.             end
  346.             alternate = 1 - alternate
  347.         end
  348.     end
  349.    
  350.     if not tryUp() then
  351.         done = true
  352.         break
  353.     end
  354. end
  355.  
  356. print( "Returning to ground level..." )
  357.  
  358. -- Return to where we started
  359. goTo( 0,0,0,0,-1 )
  360. unload( false )
  361. goTo( 0,0,0,0,1 )
  362.  
  363. -- Seal the hole
  364. if reseal then
  365.     turtle.placeUp()
  366. end
  367.  
  368. print( "Chopped "..(collected + unloaded).." items total." )
Advertisement
Add Comment
Please, Sign In to add comment