Sam54123

Computercraft Cow Farm Turtle

Apr 17th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.21 KB | None | 0 0
  1. -- Constants
  2. requiredWheat = 48
  3. requiredFuel = 500
  4. waitTime = 6*60 -- Seconds between breed cycles
  5.  
  6. fuelDirection = "right"
  7. wheatDirection = "up"
  8.  
  9. --  Breeds the cows with Wheat
  10. function breed()
  11.     -- Power the water in the farm
  12.     redstonePulse()
  13.    
  14.     -- Wait for cows
  15.     os.sleep(1)
  16.    
  17.     -- Place wheat
  18.     for i=1,200,1 do
  19.         turtle.place()
  20.     end
  21.    
  22.     -- Turn off water
  23.     redstonePulse()
  24.    
  25. end
  26.  
  27. -- Fires a 2 tick pulse of redstone on the left and right of the turtle
  28. function redstonePulse()
  29.     redstone.setOutput("left", true)
  30.     redstone.setOutput("right", true)
  31.     os.sleep(0.2)
  32.     redstone.setOutput("left", false)
  33.     redstone.setOutput("right", false)
  34. end
  35.  
  36. -- Select the next slot
  37. function selectNext()
  38.     if turtle.getSelectedSlot() == 16 then
  39.         turtle.select(1)
  40.     else
  41.         turtle.select(turtle.getSelectedSlot()+1)
  42.     end
  43. end
  44.  
  45. -- Go through all the stations and perform a breed cycle
  46. function breedCycle()
  47.     turtle.select(1)
  48.     print("Starting breed cycle")
  49.    
  50.     -- Check fuel
  51.     if turtle.getFuelLevel() < requiredFuel then
  52.         getFuel(fuelDirection)
  53.     end
  54.     if turtle.getItemCount() < requiredWheat then
  55.         getWheat(wheatDirection)
  56.     end
  57.  
  58.     turtle.forward()
  59.     turtle.forward()
  60.     turtle.forward()
  61.    
  62.     turtle.turnRight()
  63.     turtle.forward()
  64.     breed()
  65.     turtle.turnLeft()
  66.     turtle.turnLeft()
  67.    
  68.     turtle.forward()
  69.     turtle.forward()
  70.     breed()
  71.     turtle.back()
  72.     turtle.turnRight()
  73.    
  74.     turtle.forward()
  75.     turtle.forward()
  76.     turtle.forward()
  77.     turtle.forward()
  78.    
  79.     turtle.turnRight()
  80.     turtle.forward()
  81.     breed()
  82.     turtle.turnLeft()
  83.     turtle.turnLeft()
  84.    
  85.     turtle.forward()
  86.     turtle.forward()
  87.     breed()
  88.     turtle.back()
  89.     turtle.turnRight()
  90.    
  91.     for i=1,7,1 do
  92.         turtle.back()
  93.     end
  94. end
  95.  
  96. function getFuel(direction)
  97.     print("Refueling")
  98.     turtle.select(16)
  99.     local success = false
  100.     local alreadyComplained = false;
  101.    
  102.     -- Face chest
  103.     if direction=="left" then
  104.         turtle.turnLeft()
  105.     end
  106.     if direction=="right" then
  107.         turtle.turnRight()
  108.     end
  109.    
  110.     while not success do
  111.         os.queueEvent("randomEvent")
  112.         os.pullEvent()
  113.         -- Get the fuel
  114.         if direction=="down" then
  115.                 turtle.suckDown()
  116.             end
  117.         if direction=="up" then
  118.             turtle.suckUp()
  119.         end
  120.         if direction=="left" then
  121.             turtle.suck()
  122.         end
  123.         if direction=="right" then
  124.             turtle.suck()
  125.         end
  126.        
  127.         -- Attempt refuel
  128.         turtle.refuel()
  129.         success = turtle.getFuelLevel() >= requiredFuel
  130.        
  131.         -- Place item back in chest if bucket
  132.         if direction=="down" then
  133.                 turtle.dropDown()
  134.             end
  135.         if direction=="up" then
  136.             turtle.dropUp()
  137.         end
  138.         if direction=="left" or direction=="right" then
  139.             turtle.drop()
  140.         end
  141.        
  142.         -- Complain if unsuccessfull
  143.         if not success and not alreadyComplained then
  144.             print("Not enough fuel. Please place fuel in chest.")
  145.             alreadyComplained = true
  146.         end
  147.     end
  148.    
  149.     turtle.select(1)
  150.     print("Fuel at "..string.format(turtle.getFuelLevel()).."/"..string.format(requiredFuel))
  151.    
  152.     -- Face forward
  153.     if direction=="left" then
  154.         turtle.turnRight()
  155.     end
  156.     if direction=="right" then
  157.         turtle.turnLeft()
  158.     end
  159.    
  160. end
  161.  
  162. function getWheat(direction)
  163.     print("Obtaining wheat")
  164.     local success = false
  165.     local alreadyComplained = false
  166.     turtle.select(1)
  167.     local numWheat = turtle.getItemSpace()
  168.    
  169.     -- Face chest
  170.     if direction=="left" then
  171.         turtle.turnLeft()
  172.     end
  173.     if direction=="right" then
  174.         turtle.turnRight()
  175.     end
  176.    
  177.     while not success do
  178.         os.queueEvent("randomEvent")
  179.         os.pullEvent()
  180.         -- Get the wheat
  181.         if direction=="down" then
  182.             turtle.suckDown(numWheat)
  183.         end
  184.         if direction=="up" then
  185.             turtle.suckUp(numWheat)
  186.         end
  187.         if direction=="left" then
  188.             turtle.suck(numWheat)
  189.         end
  190.         if direction=="right" then
  191.             turtle.suck(numWheat)
  192.         end
  193.        
  194.         -- See if I have enough
  195.         success = turtle.getItemCount() >= requiredWheat
  196.        
  197.         -- Complain if unsuccessfull
  198.         if not success and not alreadyComplained then
  199.             print("Not enough wheat. Please place "..requiredWheat-turtle.getItemCount().." wheat in the chest.")
  200.             alreadyComplained = true
  201.         end
  202.     end
  203.    
  204.     -- Face forward
  205.     if direction=="left" then
  206.         turtle.turnRight()
  207.     end
  208.     if direction=="right" then
  209.         turtle.turnLeft()
  210.     end
  211.    
  212.     print("Wheat obtained")
  213. end
  214.  
  215. -- Main loop
  216. function main()
  217.     while true do
  218.         breedCycle()
  219.         os.sleep(waitTime)
  220.     end
  221. end
  222.        
  223.  
  224. main()
Add Comment
Please, Sign In to add comment