ccraftersanonmoose

Farm turtle

Feb 22nd, 2025 (edited)
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.99 KB | None | 0 0
  1. -- Configuration
  2. local CROP_AGE = 7 -- Maximum crop growth stage
  3. local FARM_WIDTH = 9  -- Width of farm (number of blocks per row)
  4. local FARM_ROWS = 5   -- Total number of farmland rows
  5. local WATER_GAP = true -- True if water is in between rows
  6.  
  7. -- Function to inspect the block below the turtle
  8. local function getBlockBelow()
  9.     local success, block = turtle.inspectDown()
  10.     if success then
  11.         return block.name, block.state
  12.     end
  13.     return nil, nil
  14. end
  15.  
  16. -- Function to find seeds in inventory
  17. local function selectSeeds()
  18.     for slot = 1, 16 do
  19.         local item = turtle.getItemDetail(slot)
  20.         if item and item.name:find("seeds") then
  21.             turtle.select(slot)
  22.             return true
  23.         end
  24.     end
  25.     return false
  26. end
  27.  
  28. -- Function to handle harvesting and replanting
  29. local function processFarmland()
  30.     local blockName, blockState = getBlockBelow()
  31.  
  32.     if blockName then
  33.         if blockName:find("crop") then
  34.             -- If it's a crop, check maturity
  35.             if blockState and blockState.age == CROP_AGE then
  36.                 print("Harvesting mature crop...")
  37.                 turtle.digDown() -- Harvest crop
  38.                 if selectSeeds() then
  39.                     print("Replanting...")
  40.                     turtle.placeDown() -- Replant
  41.                 end
  42.             end
  43.         elseif blockName:find("farmland") then
  44.             -- If it's farmland but no crop, plant seeds if available
  45.             if selectSeeds() then
  46.                 print("Planting new crop...")
  47.                 turtle.placeDown()
  48.             end
  49.         elseif blockName:find("water") then
  50.             print("Water detected under turtle.")
  51.         end
  52.     end
  53. end
  54.  
  55. -- Function to move forward safely, handling failures
  56. local function moveForward()
  57.     local attempts = 0
  58.     while attempts < 3 do
  59.         if turtle.forward() then
  60.             print("Moved forward.")
  61.             return true
  62.         else
  63.             print("Move failed. Retrying...")
  64.             attempts = attempts + 1
  65.             sleep(0.5) -- Short delay before retrying
  66.         end
  67.     end
  68.     print("Move forward failed after retries.")
  69.     return false
  70. end
  71.  
  72. -- Function to farm a full row
  73. local function farmRow()
  74.     for i = 1, FARM_WIDTH - 1 do
  75.         processFarmland()
  76.         moveForward()
  77.     end
  78.     processFarmland()
  79. end
  80.  
  81. -- Function to cross water safely
  82. local function crossWater()
  83.     print("Crossing water...")
  84.     turtle.up()
  85.     moveForward()
  86.     turtle.down()
  87. end
  88.  
  89. -- Function to handle the full farm cycle
  90. local function farmFullField()
  91.     print("Starting farm cycle...")
  92.     for row = 1, FARM_ROWS do
  93.         farmRow()
  94.  
  95.         -- Move to the next row if not the last
  96.         if row < FARM_ROWS then
  97.             if row % 2 == 1 then
  98.                 turtle.turnRight()
  99.                 if WATER_GAP then crossWater() else moveForward() end
  100.                 turtle.turnRight()
  101.             else
  102.                 turtle.turnLeft()
  103.                 if WATER_GAP then crossWater() else moveForward() end
  104.                 turtle.turnLeft()
  105.             end
  106.         end
  107.     end
  108.  
  109.     print("Farm cycle complete. Returning to start...")
  110.     -- Return to start position (move back row-by-row)
  111.     turtle.turnRight()
  112.     for i = 1, FARM_ROWS - 1 do
  113.         if WATER_GAP then crossWater() else moveForward() end
  114.     end
  115.     turtle.turnRight()
  116.     print("Back at start position.")
  117. end
  118.  
  119. -- Function to deposit harvested crops into the storage controller
  120. local function depositCrops()
  121.     print("Depositing crops into storage controller...")
  122.     for slot = 1, 16 do
  123.         local item = turtle.getItemDetail(slot)
  124.         if item and not item.name:find("seeds") then
  125.             turtle.select(slot)
  126.             turtle.dropBack() -- Drop items into storage controller
  127.         end
  128.     end
  129. end
  130.  
  131. -- Main loop
  132. while true do
  133.     farmFullField()
  134.     depositCrops()
  135.     print("Sleeping for 5 minutes...")
  136.     sleep(300) -- Wait 5 minutes before the next cycle
  137. end
Advertisement
Add Comment
Please, Sign In to add comment