steveg22

TreeFarmer

Sep 24th, 2013
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.01 KB | None | 0 0
  1. state = "charging"
  2. position = 0
  3.  
  4. while true do
  5.     -- print("state = " .. state .. ", position = " .. position .. ", fuel = " .. turtle.getFuelLevel())
  6.        
  7.     if state == "nextposition" then
  8.         position = position + 1
  9.         if position > 35 then
  10.            position = 0
  11.         end
  12.        
  13.         if position >= 0 and position <= 8 then
  14.             turtle.turnLeft()
  15.             if position > 0 then
  16.                 turtle.forward()
  17.                 turtle.turnRight()
  18.             end
  19.         end
  20.        
  21.         if position >= 9 and position <= 17 then
  22.             turtle.turnLeft()
  23.             if position > 9 then
  24.                 turtle.forward()
  25.                 turtle.turnRight()
  26.             end
  27.         end
  28.        
  29.         if position >= 18 and position <= 26 then
  30.             turtle.turnLeft()
  31.             if position > 18 then
  32.                 turtle.forward()
  33.                 turtle.turnRight()
  34.             end
  35.         end
  36.        
  37.         if position >= 27 and position <= 35 then
  38.             turtle.turnLeft()
  39.             if position > 27 then
  40.                 turtle.forward()
  41.                 turtle.turnRight()
  42.             end
  43.         end
  44.        
  45.         -- there might be items on the ground, pick them up
  46.        
  47.         turtle.select(1)
  48.         turtle.suck()
  49.        
  50.         if turtle.getItemCount(2) > 0 then  -- we have excess items, drop them
  51.             turtle.turnRight()
  52.             turtle.turnRight()
  53.             for n=2,4 do
  54.                 turtle.select(n)
  55.                 turtle.drop()
  56.             end
  57.             turtle.turnRight()
  58.             turtle.turnRight()
  59.         end
  60.        
  61.         state = "logging"
  62.     end
  63.        
  64.     if state == "logging" then
  65.         turtle.select(1)
  66.        
  67.         if turtle.getItemCount(1) > 0 and turtle.compareTo(16) == true then
  68.            
  69.             turtle.place()
  70.             turtle.select(15)
  71.            
  72.             if turtle.compare() == true then
  73.                 turtle.select(2)
  74.                 turtle.dig()
  75.                 turtle.forward()
  76.                 while turtle.compareUp() == true do
  77.                     turtle.digUp()
  78.                     turtle.up()
  79.                 end
  80.                
  81.                 while turtle.detectDown() == false do
  82.                     turtle.down()
  83.                 end
  84.                
  85.                 turtle.back()
  86.                 turtle.turnRight()
  87.                 turtle.turnRight()
  88.                 turtle.drop()
  89.                 turtle.turnRight()
  90.                 turtle.turnRight()
  91.                 turtle.select(1)
  92.                 turtle.place()
  93.             end
  94.            
  95.             state = "nextposition"
  96.         else
  97.             state = "reload"
  98.         end
  99.     end
  100.    
  101.     if state == "charging" then
  102.         if turtle.getFuelLevel() > 1000 then
  103.             turtle.forward()
  104.             turtle.forward()
  105.             turtle.forward()
  106.             turtle.forward()
  107.             turtle.turnLeft()
  108.             turtle.forward()
  109.             turtle.forward()
  110.             turtle.forward()
  111.             turtle.forward()
  112.             state = "logging"
  113.             position = 0
  114.             print("switching to logging state")
  115.         end
  116.     end
  117.        
  118.     if state == "reload" then
  119.        
  120.         -- prepare turtle to return to center of farm
  121.        
  122.         turtle.down()
  123.         turtle.down()
  124.         turtle.back()
  125.         turtle.back()
  126.         turtle.back()
  127.         turtle.back()
  128.         turtle.turnRight()
  129.        
  130.         -- now get it to the center block
  131.        
  132.         if position >= 0 and position <= 8 then
  133.             while position < 4 do
  134.                 turtle.back()
  135.                 position = position + 1
  136.             end
  137.            
  138.             while position > 4 do
  139.                 turtle.forward()
  140.                 position = position - 1
  141.             end
  142.         end
  143.        
  144.         if position >= 9 and position <= 17 then
  145.             while position < 13 do
  146.                 turtle.back()
  147.                 position = position + 1
  148.             end
  149.            
  150.             while position > 13 do
  151.                 turtle.forward()
  152.                 position = position - 1
  153.             end
  154.             turtle.turnRight()
  155.         end
  156.        
  157.         if position >= 18 and position <= 26 then
  158.             while position < 22 do
  159.                 turtle.back()
  160.                 position = position + 1
  161.             end
  162.            
  163.             while position > 22 do
  164.                 turtle.forward()
  165.                 position = position - 1
  166.             end
  167.             turtle.turnRight()
  168.             turtle.turnRight()
  169.         end
  170.        
  171.         if position >= 27 and position <= 35 then
  172.             while position < 31 do
  173.                 turtle.back()
  174.                 position = position + 1
  175.             end
  176.            
  177.             while position > 31 do
  178.                 turtle.forward()
  179.                 position = position - 1
  180.             end
  181.             turtle.turnLeft()
  182.         end
  183.        
  184.        
  185.         -- now get it to the chest with the saplings
  186.        
  187.         turtle.turnRight()
  188.         turtle.turnRight()
  189.        
  190.         for n=1,8 do
  191.             turtle.forward()
  192.         end
  193.         turtle.up()
  194.         turtle.up()
  195.         turtle.up()
  196.         turtle.forward()
  197.         turtle.forward()
  198.            
  199.         -- we should be on top of the chest, get some saplings
  200.        
  201.         turtle.select(1)
  202.         turtle.suckDown()
  203.        
  204.         -- and get turtle back to center block
  205.        
  206.         turtle.turnLeft()
  207.         turtle.turnLeft()
  208.         turtle.forward()
  209.         turtle.forward()
  210.         turtle.down()
  211.         turtle.down()
  212.         turtle.down()
  213.        
  214.         for n=1,8 do
  215.             turtle.forward()
  216.         end
  217.        
  218.         turtle.up()
  219.         turtle.up()
  220.        
  221.         state = "charging"
  222.     end
  223.        
  224.     if turtle.getFuelLevel() < 100 then
  225.         state = "reload"
  226.     end
  227.    
  228.     os.queueEvent("loop")
  229.     os.pullEvent()
  230. end
Advertisement
Add Comment
Please, Sign In to add comment