Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local farmY
- local initialX
- local initialZ
- local finalX
- local finalZ
- local farmDelay
- local state = "notDefined"
- local chestPosition
- local checkInterval = 60
- local tempVar = {}
- function loadFarmInfo()
- if fs.exists("FarmInfo") then
- local file = fs.open("FarmInfo","r")
- for i=0,6 do
- tempVar[i] = tonumber(file.readLine())
- end
- file.close()
- farmY = tempVar[0]
- initialX = tempVar[1]
- initialZ = tempVar[2]
- finalX = tempVar[3]
- finalZ = tempVar[4]
- farmDelay = tempVar[5]
- chestPosition = tempVar[6]
- else
- requestFarmInfo()
- end
- end
- --Defines the farm information
- function requestFarmInfo()
- write("Farm Y level: ")
- farmY = read()
- write("Initial X: ")
- initialX = read()
- write("Initial Z: ")
- initialZ = read()
- write("Final X: ")
- finalX = read()
- write("Final Z: ")
- finalZ = read()
- write("How many seconds to wait between each cycle: ")
- farmDelay = read()
- write("Chest position (number) relative to turtle home: ")
- chestPosition = read()
- local file = fs.open("FarmInfo","w")
- file.writeLine(farmY)
- file.writeLine(initialX)
- file.writeLine(initialZ)
- file.writeLine(finalX)
- file.writeLine(finalZ)
- file.writeLine(farmDelay)
- file.writeLine(chestPosition)
- file.close()
- end
- --Performs an action in a line
- function doLine(towardsFinal,action)
- if towardsFinal then
- while x ~= finalX do
- performAction(action)
- if initialX > finalX then
- shell.run("goto",x-1,y,z)
- else
- shell.run("goto",x+1,y,z)
- end
- end
- else
- while x ~= initialX do
- performAction(action)
- if initialX > finalX then
- shell.run("goto",x+1,y,z)
- else
- shell.run("goto",x-1,y,z)
- end
- end
- end
- performAction(action)
- end
- --Performs an action
- function performAction(action)
- if action == "collect" then
- suckAround()
- elseif action == "plant" then
- selectCactus()
- turtle.placeDown()
- end
- end
- --Suck every direction to collect everything
- function suckAround()
- while turtle.suckUp() do end
- while turtle.suckDown() do end
- for i = 0,3 do
- while turtle.suck() do end
- shell.run("turnRight")
- end
- end
- --Performs an action on the whole farm
- function doFarm(action)
- --Boolean variable that stores the direction the turtle has to go
- local towardsFinal
- --Decide which direction the turtle has to go
- if (finalZ - initialZ)%2 == 0 then
- if (finalZ - z)%2 == 0 then
- towardsFinal = true
- else
- towardsFinal = false
- end
- else
- if(finalZ - z)%2 == 0 then
- towardsFinal = false
- else
- towardsFinal = true
- end
- end
- --Call doLine for each line, until it reaches the final position
- while z ~= finalZ do
- doLine(towardsFinal,action)
- if initialZ > finalZ then
- shell.run("goto",x,y,z-1)
- else
- shell.run("goto",x,y,z+1)
- end
- --switch the direction the turtle has to go
- towardsFinal = not towardsFinal
- end
- doLine(towardsFinal,action)
- end
- --Select the cactus to be planted
- function selectCactus()
- for i=1,15 do
- turtle.select(i)
- if turtle.compareTo(16) then
- break
- end
- end
- end
- --Stores what the turtle just collected
- function dropOff()
- for i=1,15 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(16)
- local toDrop = turtle.getItemCount(16) - 1
- turtle.drop(toDrop)
- end
- --Saves the state of the turtle in a file, in case of reboot
- function registerState(newState)
- state = newState
- file = io.open("StateFile","w")
- file:write(state)
- file:close()
- displayInfo()
- end
- --In case of reboot, this function will be called
- function loadState()
- if fs.exists("StateFile") then
- print("Loading State")
- file = io.open("StateFile","r")
- state = file:read()
- file:close()
- displayInfo()
- else
- print("No state found, defining goHome")
- state = "goHome"
- end
- end
- function displayInfo()
- term.clear()
- term.setCursorPos(1,1)
- write("State: "..state)
- term.setCursorPos(1,2)
- write("Fuel Level: "..turtle.getFuelLevel())
- term.setCursorPos(1,3)
- write("Order: ")
- end
- -- Main Code --
- function main()
- while(true) do
- --Check for fuel
- if turtle.getFuelLevel() < 100 then
- write("Out of Fuel \n")
- break
- end
- if state == "notDefined" then
- loadState()
- end
- if state == "collect" then
- doFarm("collect")
- registerState("goOriginUp")
- end
- if state == "goOriginUp" then
- shell.run("goto",initialX,farmY+1,initialZ)
- registerState("plant")
- end
- if state == "plant" then
- doFarm("plant")
- registerState("goHome")
- end
- if state == "goHome" then
- shell.run("gohome")
- shell.run("setFacing",chestPosition)
- dropOff()
- registerState("wait"..farmDelay)
- end
- if string.match(state,"wait") == "wait" then
- time = string.match(state,"%p*%d+")
- time = tonumber(time)
- if time <= 0 then
- registerState("goOrigin")
- else
- sleep(checkInterval)
- time = time - checkInterval
- registerState("wait"..time)
- end
- end
- if state == "goOrigin" then
- turtle.select(1)
- shell.run("goto",initialX,farmY,initialZ)
- registerState("collect")
- end
- end
- end
- function readInput()
- while (true) do
- term.setCursorPos(8,3)
- local input = read()
- if input == "quit" then
- term.clear()
- return
- end
- if input == "resetFarm" then
- term.clear()
- fs.delete("FarmInfo")
- return
- end
- end
- end
- loadFarmInfo()
- --Run the farm program parallel to the readInput function, so you can give orders while the turtle works
- parallel.waitForAny(readInput, main)
Advertisement
Add Comment
Please, Sign In to add comment