Advertisement
Schleimpilz

Treefarm by Area

May 19th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | None | 0 0
  1. print("Saplings^2?")
  2. x=read()
  3.  
  4. print("max grow height of your trees?")
  5. y=read()
  6.  
  7. sidelength=3*(tonumber(x))
  8. row=tonumber(x)
  9. rowLength=row
  10. rowCount=0
  11.  
  12. local depth = 0
  13. local unloaded = 0
  14. local collected = 0
  15.  
  16. local xPos,zPos = 0,0
  17. local xDir,zDir = 0,1
  18.  
  19. local goTo -- Filled in further down
  20. local refuel -- Filled in further down
  21.  
  22. local function unload( _bKeepOneFuelStack )
  23.     print( "Unloading items..." )
  24.     for n=1,16 do
  25.         local nCount = turtle.getItemCount(n)
  26.         if nCount > 0 then
  27.             turtle.select(n)           
  28.             local bDrop = true
  29.             if _bKeepOneFuelStack and turtle.refuel(0) then
  30.                 bDrop = false
  31.                 _bKeepOneFuelStack = false
  32.             end        
  33.             if bDrop then
  34.                 turtle.drop()
  35.                 unloaded = unloaded + nCount
  36.             end
  37.         end
  38.     end
  39.     collected = 0
  40.     turtle.select(1)
  41. end
  42.  
  43. local function returnSupplies()
  44.     local x,y,z,xd,zd = xPos,depth,zPos,xDir,zDir
  45.     print( "Returning to surface..." )
  46.     goTo( 0,0,0,0,-1 )
  47.    
  48.     local fuelNeeded = 2*(x+y+z) + 1
  49.     if not refuel( fuelNeeded ) then
  50.         unload( true )
  51.         print( "Waiting for fuel" )
  52.         while not refuel( fuelNeeded ) do
  53.             os.pullEvent( "turtle_inventory" )
  54.         end
  55.     else
  56.         unload( true ) 
  57.     end
  58.    
  59.     print( "Resuming mining..." )
  60.     goTo( x,y,z,xd,zd )
  61. end
  62.  
  63. local function collect()   
  64.     local bFull = true
  65.     local nTotalItems = 0
  66.     for n=1,16 do
  67.         local nCount = turtle.getItemCount(n)
  68.         if nCount == 0 then
  69.             bFull = false
  70.         end
  71.         nTotalItems = nTotalItems + nCount
  72.     end
  73.    
  74.     if nTotalItems > collected then
  75.         collected = nTotalItems
  76.         if math.fmod(collected + unloaded, 50) == 0 then
  77.             print( "Mined "..(collected + unloaded).." items." )
  78.         end
  79.     end
  80.    
  81.     if bFull then
  82.         print( "No empty slots left." )
  83.         return false
  84.     end
  85.     return true
  86. end
  87.  
  88. function refuel( ammount )
  89.     local fuelLevel = turtle.getFuelLevel()
  90.     if fuelLevel == "unlimited" then
  91.         return true
  92.     end
  93.    
  94.     local needed = ammount or (xPos + zPos + depth + 2)
  95.     if turtle.getFuelLevel() < needed then
  96.         local fueled = false
  97.         for n=1,16 do
  98.             if turtle.getItemCount(n) > 0 then
  99.                 turtle.select(n)
  100.                 if turtle.refuel(1) then
  101.                     while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < needed do
  102.                         turtle.refuel(1)
  103.                     end
  104.                     if turtle.getFuelLevel() >= needed then
  105.                         turtle.select(1)
  106.                         return true
  107.                     end
  108.                 end
  109.             end
  110.         end
  111.         turtle.select(1)
  112.         return false
  113.     end
  114.    
  115.     return true
  116. end
  117.  
  118. local function tryForwards()
  119.     if not refuel() then
  120.         print( "Not enough Fuel" )
  121.         returnSupplies()
  122.     end
  123.    
  124.     while not turtle.forward() do
  125.         if turtle.detect() then
  126.             if turtle.dig() then
  127.                 if not collect() then
  128.                     returnSupplies()
  129.                 end
  130.             else
  131.                 return false
  132.             end
  133.         elseif turtle.attack() then
  134.             if not collect() then
  135.                 returnSupplies()
  136.             end
  137.         else
  138.             sleep( 0.5 )
  139.         end
  140.     end
  141.    
  142.     xPos = xPos + xDir
  143.     zPos = zPos + zDir
  144.     return true
  145. end
  146.  
  147. local function tryDown()
  148.     if not refuel() then
  149.         print( "Not enough Fuel" )
  150.         returnSupplies()
  151.     end
  152.    
  153.     while not turtle.down() do
  154.         if turtle.detectDown() then
  155.             if turtle.digDown() then
  156.                 if not collect() then
  157.                     returnSupplies()
  158.                 end
  159.             else
  160.                 return false
  161.             end
  162.         elseif turtle.attackDown() then
  163.             if not collect() then
  164.                 returnSupplies()
  165.             end
  166.         else
  167.             sleep( 0.5 )
  168.         end
  169.     end
  170.  
  171.     depth = depth + 1
  172.     if math.fmod( depth, 10 ) == 0 then
  173.         print( "Descended "..depth.." metres." )
  174.     end
  175.  
  176.     return true
  177. end
  178.  
  179. local function turnLeft()
  180.     turtle.turnLeft()
  181.     xDir, zDir = -zDir, xDir
  182. end
  183.  
  184. local function turnRight()
  185.     turtle.turnRight()
  186.     xDir, zDir = zDir, -xDir
  187. end
  188.  
  189. function goTo( x, y, z, xd, zd )
  190.     while depth > y do
  191.         if turtle.up() then
  192.             depth = depth - 1
  193.         elseif turtle.digUp() or turtle.attackUp() then
  194.             collect()
  195.         else
  196.             sleep( 0.5 )
  197.         end
  198.     end
  199.  
  200.     if xPos > x then
  201.         while xDir ~= -1 do
  202.             turnLeft()
  203.         end
  204.         while xPos > x do
  205.             if turtle.forward() then
  206.                 xPos = xPos - 1
  207.             elseif turtle.dig() or turtle.attack() then
  208.                 collect()
  209.             else
  210.                 sleep( 0.5 )
  211.             end
  212.         end
  213.     elseif xPos < x then
  214.         while xDir ~= 1 do
  215.             turnLeft()
  216.         end
  217.         while xPos < x do
  218.             if turtle.forward() then
  219.                 xPos = xPos + 1
  220.             elseif turtle.dig() or turtle.attack() then
  221.                 collect()
  222.             else
  223.                 sleep( 0.5 )
  224.             end
  225.         end
  226.     end
  227.    
  228.     if zPos > z then
  229.         while zDir ~= -1 do
  230.             turnLeft()
  231.         end
  232.         while zPos > z do
  233.             if turtle.forward() then
  234.                 zPos = zPos - 1
  235.             elseif turtle.dig() or turtle.attack() then
  236.                 collect()
  237.             else
  238.                 sleep( 0.5 )
  239.             end
  240.         end
  241.     elseif zPos < z then
  242.         while zDir ~= 1 do
  243.             turnLeft()
  244.         end
  245.         while zPos < z do
  246.             if turtle.forward() then
  247.                 zPos = zPos + 1
  248.             elseif turtle.dig() or turtle.attack() then
  249.                 collect()
  250.             else
  251.                 sleep( 0.5 )
  252.             end
  253.         end
  254.     end
  255.    
  256.     while depth < y do
  257.         if turtle.down() then
  258.             depth = depth + 1
  259.         elseif turtle.digDown() or turtle.attackDown() then
  260.             collect()
  261.         else
  262.             sleep( 0.5 )
  263.         end
  264.     end
  265.    
  266.     while zDir ~= zd or xDir ~= xd do
  267.         turnLeft()
  268.     end
  269. end
  270.  
  271. if not refuel() then
  272.     print( "Out of Fuel" )
  273.     return
  274. end
  275.  
  276. print( "Excavating..." )
  277.  
  278. local reseal = false
  279. turtle.select(1)
  280. if turtle.digDown() then
  281.     reseal = true
  282. end
  283.  
  284. local alternate = 0
  285. local done = false
  286. while not done do
  287.     for n=1,size do
  288.         for m=1,size-1 do
  289.             if not tryForwards() then
  290.                 done = true
  291.                 break
  292.             end
  293.         end
  294.         if done then
  295.             break
  296.         end
  297.         if n<size then
  298.             if math.fmod(n + alternate,2) == 0 then
  299.                 turnLeft()
  300.                 if not tryForwards() then
  301.                     done = true
  302.                     break
  303.                 end
  304.                 turnLeft()
  305.             else
  306.                 turnRight()
  307.                 if not tryForwards() then
  308.                     done = true
  309.                     break
  310.                 end
  311.                 turnRight()
  312.             end
  313.         end
  314.     end
  315.     if done then
  316.         break
  317.     end
  318.    
  319.     if size > 1 then
  320.         if math.fmod(size,2) == 0 then
  321.             turnRight()
  322.         else
  323.             if alternate == 0 then
  324.                 turnLeft()
  325.             else
  326.                 turnRight()
  327.             end
  328.             alternate = 1 - alternate
  329.         end
  330.     end
  331.    
  332.     if not tryDown() then
  333.         done = true
  334.         break
  335.     end
  336. end
  337.  
  338. print( "Returning to surface..." )
  339.  
  340. -- Return to where we started
  341. goTo( 0,0,0,0,-1 )
  342. unload( false )
  343. goTo( 0,0,0,0,1 )
  344.  
  345. -- Seal the hole
  346. if reseal then
  347.     turtle.placeDown()
  348. end
  349.  
  350. print( "Mined "..(collected + unloaded).." items total." )
  351.  
  352.  
  353. --planting the trees; turtle must initially be placed on the floor of the farm, with one block of air between it and the first sapling.
  354. --It will plant a square of saplings with as many saplings per side as specified earlier.
  355.  
  356. turtle.up()
  357. while rowCount<row do
  358.     while rowLength>0 do
  359.         turtle.select(1)
  360.         turtle.forward()
  361.         turtle.digDown()
  362.         turtle.placeDown()
  363.         turtle.forward()
  364.         turtle.forward()
  365.         rowLength=rowLength-1
  366.     end
  367.     rowLength=row
  368.     if (rowCount % 2 == 0) then
  369.         turtle.turnRight()
  370.         turtle.forward()
  371.         turtle.forward()
  372.         turtle.forward()
  373.         turtle.turnRight()
  374.         turtle.forward()
  375.     else
  376.         turtle.turnLeft()
  377.         turtle.forward()
  378.         turtle.forward()
  379.         turtle.forward()
  380.         turtle.turnLeft()
  381.         turtle.forward()
  382.     end
  383.     rowCount=rowCount+1
  384.     print(rowCount)
  385. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement