Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Farming Turtle program
- @author: Ascobol
- a program for farming different plants in Minecraft FTB with help of ComputerCraft
- !!I tried to keep the functions as variable as possible, but the program is still made for my setup!!
- --]]
- -- Default values
- local defaultWaitTime
- local defaultRedstoneInput
- -- Global Values
- local farmMode = 1
- local waitTime = 0
- local rowLength = 0
- local distanceBetweenRows = 0
- local plantRows = 0
- -- Header for each menu
- function baseMenu()
- -- Clearing screen
- term.clear()
- -- Program start
- print ("Farming Turtle")
- print ("==============")
- end
- -- "Shows" a menu to select the farming mode
- function menuFarmMode()
- baseMenu()
- --Inputs
- print ("What do you want to farm?")
- print (" 1 - Wheat, Barley etc")
- print (" 2 - Melon, Pumpkin etc")
- print (" 3 - Sugar Cane")
- write ("Farm Mode: ")
- farmMode = tonumber(io.read())
- print()
- if(farmMode < 1 or farmMode > 3) then
- error("Invalid farmMode Input")
- end
- end
- -- "Shows" a menu to input the waiting time, careful can throw errors
- function menuWaitingTime()
- baseMenu()
- --Inputs
- print ("How long shall the idle time be?")
- print (" <0 = No idling, manual handling")
- print (" >0 = Idle time in seconds")
- print (" 0 = Default time (" .. defaultWaitTime .. " sec)")
- write ("Idle Time: ")
- waitTime = tonumber(io.read())
- print()
- if ( waitTime == 0 ) then
- waitTime = defaultWaitTime
- elseif ( waitTime < 0 ) then
- waitTime = tonumber(waitTime)
- elseif ( waitTime > 0 ) then
- waitTime = tonumber(waitTime)
- else
- error("Invalid IdleTime Input")
- end
- end
- -- "Shows" a menu to input the row length, careful can throw errors
- function menuRowLength()
- baseMenu()
- --Inputs
- print ("How long is one row?")
- write ("Row Length: ")
- rowLength = tonumber(io.read())
- print()
- if ( rowLength == nil ) then
- error("Invalid RowLength Input")
- end
- end
- -- "Shows" a menu to input the distance between rows, careful can throw errors
- function menuRowDistance()
- baseMenu()
- --Inputs
- print ("How far is it to the next row (incl. the next row)?")
- write("Row Distance: ")
- distanceBetweenRows = tonumber(io.read())
- print()
- if ( distanceBetweenRows == nil ) then
- error("Invalid RowDistance Input")
- end
- end
- -- "Shows" a menu to input the number of rows to plant, careful can throw errors
- function menuPlantRows()
- baseMenu()
- --Inputs
- print ("How many rows shall be planted/harvested?")
- write("Number of Rows: ")
- plantRows = tonumber(io.read())
- print()
- if ( plantRows == nil ) then
- error("Invalid NumberOfPlantRows Input")
- end
- end
- function menuShowInputs()
- baseMenu()
- print("FarmMode: " .. farmMode)
- print("IdleTime: " .. waitTime)
- print("RowLength: " .. rowLength)
- print("RowDistance: " .. distanceBetweenRows)
- print("NumberOfRows: " .. plantRows)
- print("")
- write("Press any key to continue, F1 to cancel")
- ascoTurtleApi.waitForInput()
- print()
- end
- function initProgram()
- -- Default values
- defaultWaitTime = 7200
- defaultRedstoneInput = "top"
- defaultPlantRows = 4
- defaultRowLength = 53
- defaultDistanceBetweenRows = 2
- local validInput = false
- while(not validInput) do
- validInput = pcall(menuFarmMode)
- end
- validInput = false
- while(not validInput) do
- validInput = pcall(menuWaitingTime)
- end
- validInput = false
- while(not validInput) do
- validInput = pcall(menuRowLength)
- end
- validInput = false
- while(not validInput) do
- validInput = pcall(menuRowDistance)
- end
- validInput = false
- while(not validInput) do
- validInput = pcall(menuPlantRows)
- end
- menuShowInputs()
- end
- -- Waits for an redstone input or a timer
- function idle()
- print("Idle now for " .. waitTime .. " seconds.")
- local waitInSeconds = waitTime
- local redstoneInput = defaultRedstoneInput
- local interuptted = false
- local waitedSeconds = 0
- if (waitTime < 0) then
- print("Manual Control active. Stop idling by redstone input at " .. defaultRedstoneInput .. " side.")
- waitInSeconds = waitedSeconds + 1
- end
- while (not(interuptted) and (waitedSeconds < waitInSeconds)) do
- if (checkRedstone(redstoneInput)) then
- interuptted = true
- print "Interrupted by player"
- else
- os.sleep(1)
- waitedSeconds = waitedSeconds + 1
- local x,y = term.getCursorPos()
- term.setCursorPos(x, y-1)
- term.clearLine()
- print("Waited " .. ascoUtilityApi.formatSeconds(waitedSeconds))
- if (waitTime < 0) then
- waitInSeconds = waitedSeconds + 1
- end
- end
- end
- print "Finished idling, back to work..."
- end
- --Moving around the field
- function harvestAndPlant(pRowLength, pPlantRows, pDistanceBetweenRows)
- if(farmMode == 1) then
- harvestingFarmMode1(pRowLength, pPlantRows, pDistanceBetweenRows)
- elseif(farmMode == 2) then
- harvestingFarmMode2(pRowLength, pPlantRows, pDistanceBetweenRows)
- elseif(farmMode == 3) then
- harvestingFarmMode3(pRowLength, pPlantRows, pDistanceBetweenRows)
- end
- print("Harvesting, planting and emptying done.")
- end
- --Farming in Mode 1 (Wheat, Barley, Carrots, Potatoes ...)
- function harvestingFarmMode1(pRowLength, pPlantRows, pDistanceBetweenRows)
- print("Start harvesting and planting...")
- ascoFarmingApi.checkSeeds()
- --Start in lower right corner with harvest/plant
- turtle.turnLeft()
- for row = 1, pPlantRows do
- --Harvest
- ascoFarmingApi.harvestDown()
- for position = 1, pRowLength -1 do
- ascoTurtleApi.moveForward()
- ascoFarmingApi.harvestDown()
- end
- ascoTurtleApi.turnAround()
- --Get stack of seeds
- ascoTurtleApi.makeMaxStack(1)
- --Plant
- ascoFarmingApi.plant()
- for position = 1, pRowLength -1 do
- ascoTurtleApi.moveForward()
- ascoFarmingApi.plant()
- end
- --Next row?
- if(row ~= pPlantRows) then
- turtle.turnLeft()
- for move = 1, pDistanceBetweenRows do
- ascoTurtleApi.moveForward()
- end
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- --Go home
- for moves = 1, ((pPlantRows - 1) * pDistanceBetweenRows) do
- ascoTurtleApi.moveForward()
- end
- --Parking Position
- ascoTurtleApi.turnAround()
- --Emptying inventory
- ascoTurtleApi.emptyInventory("right")
- end
- --Farming in Mode 2 (Melon, Pumpkin ...)
- --No planting, only harvesting
- function harvestingFarmMode2(pRowLength, pPlantRows, pDistanceBetweenRows)
- print("Start harvesting ...")
- --Start in lower right corner with harvest/plant
- turtle.turnLeft()
- for row = 1, pPlantRows do
- --Harvest
- ascoFarmingApi.harvestDown()
- for position = 1, pRowLength -1 do
- ascoTurtleApi.moveForward()
- ascoFarmingApi.harvestDown()
- end
- --Next row?
- if(row ~= pPlantRows) then
- --Turn Turtle and move to next row
- if(ascoUtilityApi.isEvenNumber(row)) then
- turtle.turnLeft()
- ascoTurtleApi.moveForward()
- ascoTurtleApi.moveForward()
- turtle.turnLeft()
- else
- turtle.turnRight()
- for move = 1, pDistanceBetweenRows do
- ascoTurtleApi.moveForward()
- end
- turtle.turnRight()
- end
- else
- if(ascoUtilityApi.isEvenNumber(row)) then
- turtle.turnRight()
- else
- ascoTurtleApi.turnAround()
- for position = 1, pRowLength -1 do
- ascoTurtleApi.moveForward()
- end
- turtle.turnRight()
- end
- end
- end
- --Go home
- local homedist = math.floor(pPlantRows/2)*pDistanceBetweenRows + 2 * math.ceil(pPlantRows/2) - 1
- if(ascoUtilityApi.isEvenNumber(pPlantRows)) then
- homedist = homedist - 1
- else
- homedist = homedist + 1
- end
- for moves = 1, homedist do
- ascoTurtleApi.moveForward()
- end
- --Parking Position
- ascoTurtleApi.turnAround()
- --Emptying inventory
- ascoTurtleApi.emptyInventory("right")
- end
- --Farming in Mode 3 (Sugar Cane ...)
- --No planting, only harvesting
- function harvestingFarmMode3(pRowLength, pPlantRows, pDistanceBetweenRows)
- print("Start harvesting ...")
- --Start in lower right corner with harvest/plant
- turtle.turnLeft()
- turtle.up()
- for row = 1, pPlantRows do
- --Harvest
- --3rd level
- ascoFarmingApi.harvestFront()
- for position = 1, pRowLength + 1 do
- ascoTurtleApi.moveForward()
- ascoFarmingApi.harvestFront()
- ascoFarmingApi.harvestDown()
- end
- --Next row?
- if(row ~= pPlantRows) then
- --Turn Turtle and move to next row
- if(ascoUtilityApi.isEvenNumber(row)) then
- turtle.turnLeft()
- for step = 1, pDistanceBetweenRows do
- ascoTurtleApi.moveForward()
- end
- turtle.turnLeft()
- else
- turtle.turnRight()
- for step = 1, pDistanceBetweenRows do
- ascoTurtleApi.moveForward()
- end
- turtle.turnRight()
- end
- else
- if(ascoUtilityApi.isEvenNumber(row)) then
- turtle.turnRight()
- else
- turtle.turnRight()
- ascoTurtleApi.moveForward()
- turtle.turnRight()
- for step = 1, pDistanceBetweenRows do
- ascoTurtleApi.moveForward()
- end
- turtle.turnRight()
- ascoTurtleApi.moveForward()
- end
- end
- end
- --Go home
- local homedist = (pPlantRows * pDistanceBetweenRows) - pDistanceBetweenRows
- for moves = 1, homedist do
- ascoTurtleApi.moveForward()
- end
- --Parking Position
- ascoTurtleApi.turnAround()
- turtle.down()
- --Emptying inventory
- ascoTurtleApi.emptyInventory("bottom")
- end
- --Helper
- function checkRedstone(side)
- return (redstone.getInput(side))
- end
- -- Main function
- function Main()
- --APIs are loaded in the downloader
- initProgram()
- while (true) do
- harvestAndPlant(rowLength, plantRows, distanceBetweenRows)
- ascoTurtleApi.printFuelStatus()
- idle()
- end
- end
- -- Call the main method
- Main()
Advertisement
Add Comment
Please, Sign In to add comment