Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs={...}
- local turtleId
- local isWirelessTurtle
- local messageOutputFileName
- local function printUsage()
- write([[
- Usage:
- farm <length> <width> [timer]
- <length> is the length of each row
- of the field
- <width> is the number of rows
- [timer] is an optional number of
- seconds to wait before repeating.
- -- Press any key to continue --]])
- os.pullEvent("key")
- print([[
- Placement
- The turtle should begin above a chest facing towards the first row, and will move right for each subsequent row. The chest should contain the item to be planted. If the plant has seeds different from the harvested items, like wheat, place a 2nd chest above the turtle for the output.
- Note: If area is not already plowed or
- planted it must be a farming turtle.]])
- end
- if #tArgs<2 then
- printUsage()
- return
- end
- local length=tonumber(tArgs[1])
- local width=tonumber(tArgs[2])
- local repeatTime=tonumber(tArgs[3]) or 0
- isWirelessTurtle = peripheral.isPresent("right")
- if (isWirelessTurtle == true) then
- turtleId = os.getComputerLabel()
- rednet.open("right")
- end
- function writeMessage(message)
- print(message)
- -- If this turtle has a modem, then write the message to red net
- if (isWirelessTurtle == true) then
- if (turtleId == nil) then
- rednet.broadcast(message)
- else
- -- Broadcast the message (prefixed with the turtle's id)
- rednet.broadcast("[".. turtleId.."] "..message)
- end
- end
- if (messageOutputFileName ~= nil) then
- -- Open file, write message and close file (flush doesn't seem to work!)
- local outputFile = io.open(messageOutputFileName, "a")
- outputFile:write(message)
- outputFile:write("\n")
- outputFile:close()
- end
- end
- local function repeatUntil(func)
- while not func() do sleep(.5) end
- end
- local function step()
- repeatUntil(turtle.forward)
- --dry soil occassionally untils between dig and plant,
- --so repeat until successful on planting (or out of things to plant)
- local tryCount=0
- repeat
- turtle.digDown()
- tryCount=tryCount+1
- until turtle.placeDown() or turtle.getItemCount(1)==0 or tryCount==3
- end
- local function waitForKey(prompt)
- writeMessage(prompt)
- os.pullEvent("key")
- end
- local function harvest()
- --do I have enough fuel?
- local cost=(length+1)*width
- --if odd rows, add return trip
- if width%2==1 then
- cost=cost+length
- end
- while type(turtle.getFuelLevel())=="number" and cost>turtle.getFuelLevel() do
- waitForKey("Insufficient fuel. Place fuel in any slot and press a key to continue...")
- for i=2,16 do
- turtle.select(i)
- if turtle.refuel() then
- break
- end
- end
- turtle.select(1)
- term.clear()
- end
- writeMessage("Harvesting...")
- step()
- local turnNext=turtle.turnRight
- local turnOther=turtle.turnLeft
- for row=1,width do
- for pos=1,length-1 do
- step()
- end
- if row<width then
- turnNext()
- step()
- turnNext()
- end
- local temp=turnNext
- turnNext=turnOther
- turnOther=temp
- end
- --return to start position
- --if odd number of rows, we're at far end of rows, return
- if width%2==1 then
- for i=1,length do
- --if we hit something, just wait a bit and retry;
- repeatUntil(turtle.back)
- end
- turtle.turnLeft()
- else
- --was even rows, so at right end of rows facing opposite way
- repeatUntil(turtle.forward)
- turtle.turnRight()
- end
- --head back to first row
- for i=1,width-1 do
- repeatUntil(turtle.forward)
- end
- --turn to face starting direction
- turtle.turnRight()
- --unload!
- writeMessage("Unloading...")
- --anythign matching what's in slot 1 goes down, rest goes up
- for i=2,16 do
- turtle.select(i)
- if turtle.getItemCount(i)>0 then
- if turtle.compareTo(1) then
- while not turtle.dropDown() do
- waitForKey("input chest full, empty it and press any key to continue...")
- writeMessage("input chest full, empty it and press any key to continue...")
- end
- else
- while not turtle.dropUp() do
- waitForKey("output chest full, empty it and press any key to continue...")
- writeMessage("output chest full, empty it and press any key to continue...")
- end
- end
- end
- end
- turtle.select(1)
- end
- --load up with stuffs
- turtle.select(1)
- turtle.suckDown()
- if repeatTime>0 then
- while true do
- harvest()
- term.clear()
- local i=repeatTime
- while i>0 do
- local tick,interval=1,"second"
- if i>3600 then
- tick,interval=3600,"hour"
- elseif i>60 then
- tick,interval=60,"minute"
- end
- local count=math.floor(i/tick)
- if count==1 and tick>1 then
- tick=i-tick
- else
- interval=interval.."s"
- end
- term.setCursorPos(1,1)
- term.clearLine()
- write("Harvesting again in "..count.." "..interval)
- sleep(tick)
- i=i-tick
- end
- end
- else
- harvest()
- --drop seeds back
- turtle.select(1)
- turtle.dropDown()
- end
Advertisement
Add Comment
Please, Sign In to add comment