Advertisement
ccraftersanonmoose

Farm turtle

Feb 22nd, 2025
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. -- Configuration
  2. local FIELD_SIZE = 9
  3. local STORAGE_CONTROLLER = "storage" -- Adjust if necessary
  4.  
  5. -- Wrap the storage controller
  6. local storage = peripheral.wrap(STORAGE_CONTROLLER)
  7.  
  8. -- Function to check if a block is fully grown
  9. local function isMature()
  10.     local success, block = turtle.inspectDown()
  11.     if success and block.state and block.state.age == 7 then
  12.         return true
  13.     end
  14.     return false
  15. end
  16.  
  17. -- Function to find seeds in the turtle's inventory
  18. local function selectSeeds()
  19.     for slot = 1, 16 do
  20.         local item = turtle.getItemDetail(slot)
  21.         if item and (item.name:find("seeds") or item.name:find("sapling")) then
  22.             turtle.select(slot)
  23.             return true
  24.         end
  25.     end
  26.     return false
  27. end
  28.  
  29. -- Function to harvest and replant
  30. local function harvestAndReplant()
  31.     if isMature() then
  32.         turtle.digDown()
  33.         if selectSeeds() then
  34.             turtle.placeDown()
  35.         end
  36.     end
  37. end
  38.  
  39. -- Function to move through the farm in a serpentine pattern
  40. local function navigateFarm()
  41.     for row = 1, FIELD_SIZE do
  42.         for col = 1, FIELD_SIZE - 1 do
  43.             harvestAndReplant()
  44.             turtle.forward()
  45.         end
  46.         harvestAndReplant()
  47.  
  48.         if row < FIELD_SIZE then
  49.             if row % 2 == 1 then
  50.                 turtle.turnRight()
  51.                 turtle.forward()
  52.                 turtle.turnRight()
  53.             else
  54.                 turtle.turnLeft()
  55.                 turtle.forward()
  56.                 turtle.turnLeft()
  57.             end
  58.         end
  59.     end
  60.  
  61.     -- Return to starting position
  62.     if FIELD_SIZE % 2 == 0 then
  63.         turtle.turnRight()
  64.         for i = 1, FIELD_SIZE - 1 do
  65.             turtle.forward()
  66.         end
  67.         turtle.turnRight()
  68.     else
  69.         turtle.turnLeft()
  70.         for i = 1, FIELD_SIZE - 1 do
  71.             turtle.forward()
  72.         end
  73.         turtle.turnLeft()
  74.         for i = 1, FIELD_SIZE - 1 do
  75.             turtle.forward()
  76.         end
  77.         turtle.turnLeft()
  78.         turtle.turnLeft()
  79.     end
  80. end
  81.  
  82. -- Function to deposit harvested crops into storage
  83. local function depositCrops()
  84.     print("Depositing crops...")
  85.     for slot = 1, 16 do
  86.         local item = turtle.getItemDetail(slot)
  87.         if item and not (item.name:find("seeds") or item.name:find("sapling")) then
  88.             turtle.select(slot)
  89.             turtle.dropDown() -- Drop into the storage controller
  90.         end
  91.     end
  92. end
  93.  
  94. -- Main loop
  95. while true do
  96.     navigateFarm()
  97.     depositCrops()
  98.     print("Farm cycle complete. Sleeping for 5 minutes...")
  99.     sleep(300) -- Wait 5 minutes before next harvest
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement