Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local scriptName = "farmer"
- dofile("turtle_bot")
- dofile("timer")
- if #args < 3 then
- print("Usage: " .. scriptName .. " {<crops>} <rows> <columns>")
- print("Note: <crops> should be a list of crop names. Available crops: {" .. table.concat(map(CROPS, function(k, _) return k end), ", ") .. "}")
- return false
- end
- local t = TurtleBot.new(scriptName, "North")
- t:connect()
- local crops = {}
- local function validateCrops()
- local cropsArg = splitString(args[1], ',')
- for _, crop in ipairs(cropsArg) do
- if not CROPS[crop] then
- print("Error: Invalid crop name '" .. crop .. "'")
- return false
- end
- table.insert(crops, CROPS[crop])
- print("Valid crop: " .. CROPS[crop].name)
- end
- return true
- end
- local function harvest()
- local success, data = turtle.inspectDown()
- if not success then return false end
- local validCropNames = map(crops, function(k,v) return v.name end)
- if not inList(data.name, validCropNames) then return false end
- local crop = find(CROPS, function(k, v)
- if v.name == data.name and v.mature == data.metadata then
- return v
- end
- end)
- if not crop then return false end
- t:dig("down")
- return true
- end
- local function plant(idx)
- if turtle.detectDown() then return false end
- local crop = crops[(idx % #crops) + 1]
- if not t:selectItem(crop.seed) then return false end
- t:dig("down")
- turtle.placeDown()
- return true
- end
- local function printInfo(interval)
- shell.run("clear")
- print("Current fuel level: " .. turtle.getFuelLevel() .. ".")
- print("Sleeping for " .. interval .. " seconds...")
- end
- local function handleTurn(column, func)
- local turn = nil
- if column % 2 == 1 then
- turn = function() t:right() end
- else
- turn = function() t:left() end
- end
- turn()
- if func ~= nil then func() end
- t:forward()
- turn()
- end
- local function reset(rows, columns)
- if columns % 2 == 1 then
- t:left(2)
- t:forward(rows - 1)
- end
- t:right()
- t:forward(columns - 1)
- t:right()
- end
- local function getSleepInterval(elapsedTime, minGrowthSpeed)
- local interval = minGrowthSpeed - elapsedTime
- if interval < 0 then return 0 else return interval end
- end
- local function getSeeds(seedMaxCount)
- for _, crop in pairs(crops) do
- t:enderCollect(crop.channel, seedMaxCount)
- end
- end
- local function main()
- if not validateCrops() then return end
- local rows = tonumber(args[2])
- local columns = tonumber(args[3])
- local timer = Timer.new()
- local minGrowthSpeed = math.min(unpack(map(crops, function(k, v) return v.speed end)))
- local seedMaxCount = rows * columns / #crops
- t:awaitInventory(ITEMS.enderChest)
- while true do
- getSeeds(seedMaxCount)
- t:dig("up")
- timer:reset()
- timer:start()
- for c = 1, columns do
- for r = 2, rows do -- Turtle is already in the first cell
- harvest()
- plant(c+r)
- t:forward()
- if r == rows and c ~= columns then
- handleTurn(c, function() harvest(); plant(c+r+1); end)
- end
- end
- end
- harvest()
- plant(columns+rows+1)
- reset(rows, columns)
- t:combine()
- t:enderDeposit(COLOR_CHANNELS.farmingDepot, {})
- t:enderRefuel(1000, 2000, ITEMS.charcoal)
- t:dig("up")
- timer:stop()
- local interval = getSleepInterval(timer.elapsedTime, minGrowthSpeed)
- printInfo(interval)
- sleep(interval)
- end
- end
- t:execute(main)
Advertisement
Add Comment
Please, Sign In to add comment