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
- return data.state and data.state.age == 7
- end
- return false
- end
- -- Function to get the seed type from a slot
- local function getSeedType()
- for i = 1, 16 do
- local item = turtle.getItemDetail(i)
- if item and item.name:find("seed") then
- return i
- end
- end
- return nil
- end
- -- Function to replant a crop using the detected seed type
- local function replantCrop()
- local seedSlot = getSeedType()
- if seedSlot then
- turtle.select(seedSlot)
- turtle.placeDown()
- else
- print("No seeds found in inventory.")
- end
- end
- -- Function to move the turtle forward a certain number of steps
- local function moveForward(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)
- 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()
- -- 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()
- -- 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()
- for y = 1, height do
- for x = 1, width do
- if isCropGrown() then
- turtle.digDown()
- replantCrop() -- Replant the same type of seed detected in inventory
- 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