Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Initialize farm size variables
- local width, height
- -- Function to read farm size from user input
- local function readFarmSize()
- print("Enter farm width: ")
- width = tonumber(read())
- print("Enter farm height: ")
- height = tonumber(read())
- end
- -- Function to check and refuel if needed
- local function checkFuel()
- local fuelLevel = turtle.getFuelLevel()
- print("Current fuel level: " .. fuelLevel)
- if fuelLevel < 10 then
- print("Low fuel. Refueling...")
- turtle.select(1) -- Assume fuel is in slot 1
- if not turtle.refuel() then
- print("Failed to refuel. Check fuel source.")
- return false
- end
- end
- return true
- end
- -- Function to check if a crop below the turtle is fully grown
- local function isCropGrown()
- local success, data = turtle.inspectDown()
- if success then
- local age = data.state and data.state.age
- print("Crop age: " .. (age or "not available"))
- return age and age == 7
- end
- print("No crop detected below.")
- return false
- end
- -- Function to generate seed name from crop name
- local function getSeedName(cropName)
- -- Check if the crop name ends with "_crop"
- if cropName:match("_crop$") then
- -- Replace "_crop" with "_seed"
- return cropName:gsub("_crop$", "_seed")
- else
- -- Simply add "_seeds" to the end
- return cropName:lower():gsub(" ", "_") .. "_seeds"
- end
- end
- -- Function to find and select the seed item in inventory
- local function selectSeed(seedName)
- print("Searching for seed: " .. seedName)
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name == seedName then
- print("Selecting seed: " .. seedName)
- turtle.select(i)
- return true
- end
- end
- print("Seed not found in inventory: " .. seedName)
- return false
- end
- -- Function to replant a crop using the detected seed type
- local function replantCrop(cropName)
- local seedName = getSeedName(cropName)
- print("Generated seed name: " .. seedName)
- if selectSeed(seedName) then
- print("Replanting seed: " .. seedName)
- if not turtle.placeDown() then
- print("Failed to place seed.")
- end
- else
- print("Unable to replant. Seed not found.")
- end
- end
- -- Function to move the turtle forward a certain number of steps
- local function moveForward(steps)
- print("Moving forward " .. steps .. " steps")
- for i = 1, steps do
- if not checkFuel() then
- return
- end
- if not turtle.forward() then
- print("Unable to move forward, possibly blocked")
- return
- end
- end
- end
- -- Function to move the turtle to the next row
- local function moveToNextRow(isEvenRow)
- print("Moving to next row. Is even row? " .. tostring(isEvenRow))
- if isEvenRow then
- turtle.turnLeft()
- moveForward(1)
- turtle.turnLeft()
- else
- turtle.turnRight()
- moveForward(1)
- turtle.turnRight()
- end
- end
- -- Function to move the turtle back to its starting position (bottom-right corner)
- local function returnToStart()
- print("Returning to start position")
- -- Turn around to face the starting corner
- turtle.turnRight()
- turtle.turnRight()
- moveForward(height - 1) -- Move up the farm
- turtle.turnLeft()
- moveForward(width - 1) -- Move to the bottom-right corner
- turtle.turnLeft()
- end
- -- Function to deposit items into a chest behind the turtle's starting position
- local function depositItems()
- print("Depositing items")
- -- Turn to face the chest (which is behind the turtle's starting position)
- turtle.turnLeft()
- turtle.turnLeft()
- for i = 1, 16 do
- turtle.select(i)
- turtle.drop()
- end
- -- Return to the original orientation
- turtle.turnLeft()
- turtle.turnLeft()
- end
- -- Function to harvest and replant crops in a zigzag pattern
- local function harvestAndReplant()
- print("Starting harvest and replant process")
- for y = 1, height do
- for x = 1, width do
- if isCropGrown() then
- -- Inspect the crop before harvesting
- local success, data = turtle.inspectDown()
- if success then
- local cropName = data.name
- print("Detected crop: " .. cropName)
- -- Harvest the crop
- print("Harvesting crop at position (" .. x .. ", " .. y .. ")")
- turtle.digDown()
- -- Wait for a second to ensure the crop is fully processed
- os.sleep(1)
- -- Replant the crop using the detected seed type
- replantCrop(cropName)
- else
- print("Failed to inspect crop.")
- end
- end
- if x < width then
- moveForward(1) -- Move forward one block
- end
- end
- if y < height then
- moveToNextRow(y % 2 == 1)
- end
- end
- end
- -- Main program loop
- print("Welcome to the Turtle Farm Program!")
- print("Please place fuel in slot 1 of the turtle's inventory.")
- readFarmSize()
- while true do
- harvestAndReplant()
- returnToStart()
- depositItems()
- os.sleep(1800) -- Wait for 30 minutes before the next cycle
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement