Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This program is meant to harvest crops like netherwart, carrots,
- -- potatos, etc. that serve as their own seeds, and that have growth stages
- -- that can be monitored by a BUD (block update detector).
- local growStates = 3
- local dropFn = turtle.dropDown
- local digFn = turtle.dig
- local placeFn = turtle.place
- local maxWait = 60*45
- local budSide = "back"
- local dropWait = 30
- local trackMax = 50
- function usage()
- print("usage: " .. shell.getRunningProgram() .. " <growStates>")
- print(" [ <cropDir> <dropChestDir> [(<maxWaitMin> m|<maxWaitSec> s)")
- print(" [<budSide> [(<dropWaitMin> m|<dropWaitSec> s) [<trackMax>]]]]] ")
- -- e.g: budFarmer 3 front down 45 m back 30 s 10
- -- Plant crop in front [<cropDir>] of the turtle.
- -- Wait for 3 block updates [<growStates>] (indicated by a redstone
- -- pulse on the back side of the turtle [<budSide>]) or up to
- -- 45 minutes [<maxWaitMin>]. If the drop chest underneath
- -- [<dropChestDir>] is full, wait 30 seconds [<dropWaitSec>]
- -- and try again. Track statistics for the last 10 cycles [<trackMax>].
- error()
- end
- function numArg(str)
- local ret = tonumber(str)
- if ret == nil then
- usage()
- end
- return ret
- end
- function timeArg(arg, unit)
- if unit == "m" then
- return numArg(arg) * 60
- elseif unit == "s" then
- return numArg(arg)
- else
- usage()
- end
- end
- local argc = 0
- local args = {...}
- argc = argc + 1
- if args[argc] == nil then
- usage()
- else
- growStates = numArg(args[argc])
- end
- argc = argc + 1
- if args[argc] ~= nil then
- if args[argc] == "front" then
- digFn = turtle.dig
- placeFn = turtle.place
- elseif args[argc] == "down" then
- digFn = turtle.digDown
- paceFn = turtle.placeDown
- else
- usage()
- end
- end
- argc = argc + 1
- if args[argc] ~= nil then
- if args[argc] == "front" then
- dropFn = turtle.drop
- elseif args[argc] == "down" then
- dropFn = turtle.dropDown
- elseif args[argc] == "up" then
- dropFn = turtle.dropUp
- else
- usage()
- end
- end
- argc = argc + 1
- if args[argc] ~= nil then
- maxWait = timeArg(args[argc], args[argc+1])
- argc = argc+ 1
- end
- argc = argc + 1
- if args[argc] ~= nill then
- budSide = args[argc]
- end
- argc = argc + 1
- if args[argc] ~= nil then
- dropWait = timeArg(args[argc], args[argc+1])
- argc = argc+ 1
- end
- argc = argc + 1
- if args[argc] ~= nill then
- trackMax = args[argc]
- end
- local next = 1
- local total = 0
- local timeouts = { }
- local growTimes = { }
- local growYields = { }
- local lastTime = os.clock()
- for i = 1, trackMax do
- timeouts[i] = 0
- growTimes[i] = 0
- growYields[i] = 0
- end
- function println(str)
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y)
- term.clearLine()
- print(str)
- end
- function printover(str)
- local x, y = term.getCursorPos()
- term.setCursorPos(1, y)
- term.clearLine()
- term.write(str)
- term.setCursorPos(1, y)
- end
- function trackYields(growYield, growTime, dropTime, timedOut)
- local cur = next
- next = (next % trackMax) + 1
- total = total + 1
- if timedOut then
- timeouts[cur] = 1
- else
- timeouts[cur] = 0
- end
- growTimes[cur] = growTime
- growYields[cur] = growYield
- local n = math.min(total, trackMax)
- local avgTime = 0
- local avgYield = 0
- local numTimeouts = 0
- for i = 1, n do
- avgTime = avgTime + growTimes[i]
- avgYield = avgYield + growYields[i]
- numTimeouts = numTimeouts + timeouts[i]
- end
- if avgTime ~= 0 then
- avgTime = avgTime / n
- end
- if avgYield ~= 0 then
- avgYield = avgYield / n
- end
- local timeoutStr
- if timedOut then
- timeoutStr = "t"
- else
- timeoutStr = "h"
- end
- -- %2.2f seems to be bugged??
- println(string.format("%3i %s %1i %2i:%02i ~%2i %1i.%1i %2i:%02i, %i t",
- total,
- timeoutStr,
- growYield,
- growTime / 60,
- growTime % 60,
- n,
- avgYield,
- (avgYield * 10) % 10,
- avgTime / 60,
- avgTime % 60,
- numTimeouts))
- end
- local lastPlant = os.clock()
- function sincePlantStr()
- local time = os.clock() - lastPlant
- return string.format("%2i:%02i", time / 60, time % 60)
- end
- local timerID = nil
- function harvest()
- local growYield = 0
- local growTime = 0
- local dropTime = 0
- growTime = os.clock() - lastPlant
- local time = os.clock()
- -- Keep 1 spare for planting
- while not dropFn(turtle.getItemCount(1) - 1) do
- dropFail = true
- dropTime = os.clock() - time
- printover(string.format("Failed dropping crop %2i:%02i", dropTime / 60, dropTime % 60))
- sleep(dropWait)
- dropTime = os.clock() - time
- end
- term.clearLine()
- if not dropTime == 0 then
- println(string.format("Waited %2i:%02i for space to drop", dropTime / 60, dropTime % 60))
- end
- digFn()
- placeFn()
- lastPlant = os.clock()
- timerID = os.startTimer(maxWait)
- growYield = turtle.getItemCount(1)
- return growYield, growTime, dropTime
- end
- function main()
- local firstYield = harvest()
- println("Initial Yield " .. tostring(firstYield))
- local budCount = 0
- while true do
- local e, p1 = os.pullEvent()
- if e == "redstone" then
- if redstone.getInput(budSide) then
- budCount = budCount + 1
- printover(sincePlantStr() .. " budCount " .. tostring(budCount))
- if budCount >= growStates then
- budCount = 0
- local growYield, growTime, dropTime = harvest()
- trackYields(growYield, growTime, dropTime, false)
- end
- end
- end
- if e == "timer" and timerID == p1 then
- local growYield, growTime, dropTime = harvest()
- trackYields(growYield, growTime, dropTime, true)
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment