shiryavsky

Turtle Farmer

Dec 12th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. -- farmer by shaolin
  2. -- store it to startup file
  3.  
  4. fuelLevelToRefuelAt = 10
  5.  
  6. function main()
  7.   print("-------------------------")
  8.   print("--- farmer by shaolin ---")
  9.   print("-------------------------")
  10.   print("---  redstone on back ---")
  11.   print("---    fuel on top    ---")
  12.   print("---  result on down   ---")
  13.   print("-------------------------")
  14.   while turtle.getItemCount(1) < 1 do
  15.     print("Place seed in 1st slot, and press enter...")
  16.     io.read()
  17.   end  
  18.   searchStartPosition()
  19.   while (true) do
  20.     waitSignal()
  21.     if chargeInventory() then  
  22.       processFarming()
  23.       searchStartPosition()
  24.       chargeInventory()
  25.     end
  26.   end
  27. end
  28.  
  29.  
  30. function chargeInventory()
  31.   turtle.select(16)
  32.   turtle.suckUp()
  33.   if turtle.getItemCount(16) < 2 then
  34.     print("Fill the fuel chest and try again!")
  35.     return false
  36.   end
  37.   for i=2,15 do
  38.     turtle.select(i)
  39.     if turtle.compareTo(16) then
  40.       turtle.dropUp()
  41.     else
  42.       if turtle.compareTo(1) then
  43.          turtle.transferTo(1)
  44.          if (i > 2) then
  45.            turtle.transferTo(2) -- store seeds in 1,2 slots
  46.          end
  47.          turtle.dropDown()
  48.       else
  49.          turtle.dropDown()
  50.       end
  51.     end  
  52.     while (i > 2) and turtle.getItemCount(i) > 0 do
  53.       print("Chest is full, empty it and press enter..")
  54.       io.read()
  55.       turtle.dropDown()
  56.     end
  57.   end
  58.   return true
  59. end
  60.  
  61. function selectSeedSlot()
  62.   turtle.select(1)
  63.   for i=2,15 do
  64.     if turtle.compareTo(i) then
  65.       turtle.select(i)
  66.       return true
  67.     end
  68.   end
  69.   return turtle.getItemCount(1) > 2      
  70. end
  71.  
  72. function dig()
  73.        if not turtle.detectUp() then
  74.           turtle.select(2)
  75.           turtle.digDown()
  76.           turtle.suckDown()
  77.           if selectSeedSlot() then
  78.             if not turtle.placeDown() then
  79.                turtle.digDown()  -- hoe
  80.                turtle.placeDown()
  81.             end
  82.           end
  83.         end
  84. end
  85.  
  86. function processFarming()
  87.   turtle.turnLeft()
  88.   while not turtle.detect() do
  89.     ensureFuel()
  90.     forwardToKill()
  91.   end
  92.   turtle.turnRight() --left bottom pos of field
  93.   print("Start farming...")  
  94.   fin = false
  95.   while not fin do    
  96.     for i = 1,2 do
  97.       while not turtle.detect() do
  98.         ensureFuel()
  99.         dig()
  100.         turtle.forward()
  101.       end
  102.       if i==1 then
  103.         turtle.turnRight()
  104.         if turtle.detect() then
  105.           fin = true
  106.         else
  107.           dig()
  108.           turtle.forward()
  109.         end
  110.         turtle.turnRight()
  111.       else
  112.         turtle.turnLeft()
  113.         if turtle.detect() then
  114.           fin = true
  115.         else
  116.           dig()
  117.           turtle.forward()
  118.         end
  119.         turtle.turnLeft()
  120.       end
  121.     end
  122.   end
  123.   dig()
  124.   print("... complete!")
  125. end
  126.  
  127. function waitSignal()
  128.   print("Waiting for signal...")
  129.   while not redstone.getInput("back") do
  130.     os.sleep(1)
  131.   end
  132.   print("... got it!")
  133. end
  134.  
  135. function searchStartPosition()
  136.   print("Finding start position...")
  137.   if turtle.detectUp() and turtle.detect() then
  138.     turtle.turnRight()
  139.     turtle.turnRight()
  140.   end
  141.   if turtle.detectUp() then
  142.     print("... already!")
  143.     return true
  144.   end
  145.   while not turtle.detectUp() do
  146.     ensureFuel()
  147.     i = 0
  148.     while turtle.detect() do
  149.       turtle.turnRight()
  150.       i = i + 1
  151.       if i > 4 then
  152.         print("I'm lost! where I am? fix and [enter]")
  153.         io.read()
  154.       end
  155.     end
  156.     forwardToKill()
  157.   end
  158.   turtle.turnRight()
  159.   print("... found!")
  160. end
  161.  
  162. function forwardToKill()
  163.     i = 0    
  164.     while (not turtle.forward()) and (i < 20) do
  165.       print("Somebody on my farm!")
  166.       turtle.attack()
  167.       i = i + 1
  168.     end
  169. end
  170.  
  171. --Check fuel, refueling from inventory special slot or not
  172. function ensureFuel()
  173.   -- Determine whether a refuel is required
  174.   local fuelLevel = turtle.getFuelLevel()
  175.   if (fuelLevel ~= "unlimited") then
  176.     if (fuelLevel < fuelLevelToRefuelAt) then
  177.       -- Need to refuel
  178.       turtle.select(16)
  179.       local fuelItems = turtle.getItemCount(16)
  180.       -- Do we need to impact the emergency fuel to continue? (always  
  181.       -- keep one fuel item in slot 16)
  182.       if (fuelItems == 0) then
  183.         print("I want more fuel!")
  184.         io.read()
  185.       elseif (fuelItems == 1) then
  186.         --load fuel from another slot
  187.     local fueled = false
  188.     local fromSlot = 2
  189.         for i=fromSlot,15,1 do
  190.           turtle.select(i)
  191.       if (turtle.refuel(0)) then  -- I can eat that
  192.         print("Refueling from not fuel slot: "+i)
  193.         turtle.refuel(1)
  194.         turtle.transferTo(16)          
  195.         fueled = true
  196.           end
  197.         end
  198.         if not fueled then
  199.           print("completely out of fuel!")
  200.         end
  201.       else --fuelItems > 1
  202.         print("Time to refueling!")
  203.         turtle.refuel(1)
  204.       end
  205.     end
  206.   end
  207. end
  208.  
  209. main()
Advertisement
Add Comment
Please, Sign In to add comment