Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configuration
- local CROP_AGE = 7 -- Maximum crop growth stage
- local FARM_WIDTH = 9 -- Width of farm (number of blocks per row)
- local FARM_ROWS = 5 -- Total number of farmland rows
- local WATER_GAP = true -- True if water is in between rows
- -- Function to inspect the block below the turtle
- local function getBlockBelow()
- local success, block = turtle.inspectDown()
- if success then
- return block.name, block.state
- end
- return nil, nil
- end
- -- Function to find seeds in inventory
- local function selectSeeds()
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and item.name:find("seeds") then
- turtle.select(slot)
- return true
- end
- end
- return false
- end
- -- Function to handle harvesting and replanting
- local function processFarmland()
- local blockName, blockState = getBlockBelow()
- if blockName then
- if blockName:find("crop") then
- -- If it's a crop, check maturity
- if blockState and blockState.age == CROP_AGE then
- print("Harvesting mature crop...")
- turtle.digDown() -- Harvest crop
- if selectSeeds() then
- print("Replanting...")
- turtle.placeDown() -- Replant
- end
- end
- elseif blockName:find("farmland") then
- -- If it's farmland but no crop, plant seeds if available
- if selectSeeds() then
- print("Planting new crop...")
- turtle.placeDown()
- end
- elseif blockName:find("water") then
- print("Water detected under turtle.")
- end
- end
- end
- -- Function to move forward safely, handling failures
- local function moveForward()
- local attempts = 0
- while attempts < 3 do
- if turtle.forward() then
- print("Moved forward.")
- return true
- else
- print("Move failed. Retrying...")
- attempts = attempts + 1
- sleep(0.5) -- Short delay before retrying
- end
- end
- print("Move forward failed after retries.")
- return false
- end
- -- Function to farm a full row
- local function farmRow()
- for i = 1, FARM_WIDTH - 1 do
- processFarmland()
- moveForward()
- end
- processFarmland()
- end
- -- Function to cross water safely
- local function crossWater()
- print("Crossing water...")
- turtle.up()
- moveForward()
- turtle.down()
- end
- -- Function to handle the full farm cycle
- local function farmFullField()
- print("Starting farm cycle...")
- for row = 1, FARM_ROWS do
- farmRow()
- -- Move to the next row if not the last
- if row < FARM_ROWS then
- if row % 2 == 1 then
- turtle.turnRight()
- if WATER_GAP then crossWater() else moveForward() end
- turtle.turnRight()
- else
- turtle.turnLeft()
- if WATER_GAP then crossWater() else moveForward() end
- turtle.turnLeft()
- end
- end
- end
- print("Farm cycle complete. Returning to start...")
- -- Return to start position (move back row-by-row)
- turtle.turnRight()
- for i = 1, FARM_ROWS - 1 do
- if WATER_GAP then crossWater() else moveForward() end
- end
- turtle.turnRight()
- print("Back at start position.")
- end
- -- Function to deposit harvested crops into the storage controller
- local function depositCrops()
- print("Depositing crops into storage controller...")
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item and not item.name:find("seeds") then
- turtle.select(slot)
- turtle.dropBack() -- Drop items into storage controller
- end
- end
- end
- -- Main loop
- while true do
- farmFullField()
- depositCrops()
- print("Sleeping for 5 minutes...")
- sleep(300) -- Wait 5 minutes before the next cycle
- end
Advertisement
Add Comment
Please, Sign In to add comment