mathiaas

farmer

Apr 10th, 2024 (edited)
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.74 KB | None | 0 0
  1. local args = {...}
  2. local scriptName = "farmer"
  3.  
  4. dofile("turtle_bot")
  5. dofile("timer")
  6.  
  7.  
  8. if #args < 3 then
  9.     print("Usage: " .. scriptName .. " {<crops>} <rows> <columns>")
  10.     print("Note: <crops> should be a list of crop names. Available crops: {" .. table.concat(map(CROPS, function(k, _) return k end), ", ") .. "}")
  11.     return false
  12. end
  13.  
  14.  
  15. local t = TurtleBot.new(scriptName, "North")
  16. t:connect()
  17. local crops = {}
  18.  
  19. local function validateCrops()
  20.     local cropsArg = splitString(args[1], ',')
  21.     for _, crop in ipairs(cropsArg) do
  22.         if not CROPS[crop] then
  23.             print("Error: Invalid crop name '" .. crop .. "'")
  24.             return false
  25.         end
  26.         table.insert(crops, CROPS[crop])
  27.         print("Valid crop: " .. CROPS[crop].name)
  28.     end
  29.     return true
  30. end
  31.  
  32.  
  33. local function harvest()
  34.     local success, data = turtle.inspectDown()
  35.     if not success then return false end
  36.     local validCropNames = map(crops, function(k,v) return v.name end)
  37.     if not inList(data.name, validCropNames) then return false end
  38.  
  39.  
  40.     local crop = find(CROPS, function(k, v)
  41.         if v.name == data.name and v.mature == data.metadata then
  42.             return v
  43.         end
  44.     end)
  45.  
  46.     if not crop then return false end
  47.  
  48.     t:dig("down")
  49.     return true
  50. end
  51.  
  52.  
  53. local function plant(idx)
  54.     if turtle.detectDown() then return false end
  55.     local crop = crops[(idx % #crops) + 1]
  56.  
  57.  
  58.     if not t:selectItem(crop.seed) then return false end
  59.     t:dig("down")
  60.     turtle.placeDown()
  61.  
  62.     return true
  63. end
  64.  
  65.  
  66. local function printInfo(interval)
  67.     shell.run("clear")
  68.     print("Current fuel level: " .. turtle.getFuelLevel() .. ".")
  69.     print("Sleeping for " .. interval .. " seconds...")
  70. end
  71.  
  72.  
  73. local function handleTurn(column, func)
  74.     local turn = nil
  75.     if column % 2 == 1 then
  76.         turn = function() t:right() end
  77.     else
  78.         turn = function() t:left() end
  79.     end
  80.  
  81.     turn()
  82.     if func ~= nil then func() end
  83.     t:forward()
  84.     turn()
  85. end
  86.  
  87.  
  88. local function reset(rows, columns)
  89.     if columns % 2 == 1 then
  90.         t:left(2)
  91.         t:forward(rows - 1)
  92.     end
  93.     t:right()
  94.     t:forward(columns - 1)
  95.     t:right()
  96. end
  97.  
  98.  
  99. local function getSleepInterval(elapsedTime, minGrowthSpeed)
  100.     local interval = minGrowthSpeed - elapsedTime
  101.     if interval < 0 then return 0 else return interval end
  102. end
  103.  
  104.  
  105. local function getSeeds(seedMaxCount)
  106.     for _, crop in pairs(crops) do
  107.         t:enderCollect(crop.channel, seedMaxCount)
  108.     end
  109. end
  110.  
  111.  
  112. local function main()
  113.     if not validateCrops() then return end
  114.     local rows = tonumber(args[2])
  115.     local columns = tonumber(args[3])
  116.     local timer = Timer.new()
  117.     local minGrowthSpeed = math.min(unpack(map(crops, function(k, v) return v.speed end)))
  118.     local seedMaxCount = rows * columns / #crops
  119.     t:awaitInventory(ITEMS.enderChest)
  120.  
  121.     while true do
  122.         getSeeds(seedMaxCount)
  123.         t:dig("up")
  124.  
  125.         timer:reset()
  126.         timer:start()
  127.         for c = 1, columns do
  128.             for r = 2, rows do -- Turtle is already in the first cell
  129.                 harvest()
  130.                 plant(c+r)
  131.                 t:forward()
  132.                 if r == rows and c ~= columns then
  133.                     handleTurn(c, function() harvest(); plant(c+r+1); end)
  134.                 end
  135.             end
  136.         end
  137.  
  138.         harvest()
  139.         plant(columns+rows+1)
  140.         reset(rows, columns)
  141.         t:combine()
  142.         t:enderDeposit(COLOR_CHANNELS.farmingDepot, {})
  143.         t:enderRefuel(1000, 2000, ITEMS.charcoal)
  144.         t:dig("up")
  145.  
  146.         timer:stop()
  147.         local interval = getSleepInterval(timer.elapsedTime, minGrowthSpeed)
  148.         printInfo(interval)
  149.         sleep(interval)
  150.     end
  151. end
  152.  
  153.  
  154. t:execute(main)
Advertisement
Add Comment
Please, Sign In to add comment