Advertisement
Nehulol

farm.lua

Dec 15th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.80 KB | None | 0 0
  1. -- Automated Crop Farm Script
  2. -- Configure the field dimensions
  3. print('Input field width:')
  4. local fieldHeight = tonumber(read())
  5. print('Input field height:')
  6. local fieldWidth = tonumber(read())
  7. print('Input wait time:')
  8. local waitTime = tonumber(read()) or 240
  9.  
  10. local crop = turtle.getItemDetail(1)
  11.  
  12. -- Function to plant crops
  13. local function plant()
  14.     for i = 2, 16 do
  15.         local item = turtle.getItemDetail(i)
  16.         if item and item.name == crop.name then
  17.             turtle.select(i)
  18.             if not turtle.detectDown() and turtle.getItemCount() > 0 then
  19.                 turtle.placeDown()
  20.             end
  21.             break
  22.         end
  23.     end
  24. end
  25.  
  26. -- Function to harvest crops
  27. local function harvest()
  28.     if turtle.detectDown() then
  29.         local success, data = turtle.inspectDown()
  30.         if success and data.state and data.state.age == 7 then
  31.             turtle.digDown()
  32.         end
  33.     end
  34.     plant()
  35. end
  36.  
  37. -- Refuel if needed
  38. local function refuel()
  39.     for i = 2, 16 do
  40.         local item = turtle.getItemDetail(i)
  41.         if item then
  42.             turtle.select(i)
  43.             for _ = 1, item.count do
  44.                 turtle.refuel()
  45.             end
  46.         end
  47.     end
  48. end
  49.  
  50. -- Unload inventory into chest
  51. local function unload()
  52.     for i = 1, 16 do
  53.         turtle.select(i)
  54.         if i == 1 then
  55.             turtle.dropUp(turtle.getItemCount()-1)
  56.         else
  57.             turtle.dropUp()
  58.         end
  59.     end
  60. end
  61.  
  62. -- Main loop for farming
  63. local function farm()
  64.  
  65.     local fuelLevel = turtle.getFuelLevel()
  66.     if fuelLevel < fieldWidth*fieldHeight*1.25 then
  67.         print("Not enough fuel. Current fuel: " .. fuelLevel)
  68.         return
  69.     end
  70.  
  71.     for y = 1, fieldHeight do
  72.         for x = 1, fieldWidth - 1 do
  73.             harvest()
  74.             turtle.forward()
  75.         end
  76.         harvest()
  77.  
  78.         -- Turn at the end of a row
  79.         if y < fieldHeight then
  80.             if y % 2 == 1 then
  81.                 turtle.turnRight()
  82.                 turtle.forward()
  83.                 turtle.turnRight()
  84.             else
  85.                 turtle.turnLeft()
  86.                 turtle.forward()
  87.                 turtle.turnLeft()
  88.             end
  89.         end
  90.     end
  91.  
  92.     -- Return to start
  93.     if fieldHeight % 2 == 1 then
  94.         turtle.turnLeft()
  95.         for i = 1, fieldHeight - 1 do
  96.             turtle.forward()
  97.         end
  98.         turtle.turnRight()
  99.         for i = 1, fieldWidth - 1 do
  100.             turtle.back()
  101.         end
  102.     else
  103.         turtle.turnRight()
  104.         for i = 1, fieldHeight - 1 do
  105.             turtle.forward()
  106.         end
  107.         turtle.turnRight()
  108.     end
  109.  
  110.     -- Unload inventory
  111.     unload()
  112. end
  113.  
  114. -- Start farming loop
  115. while true do
  116.     refuel()
  117.     farm()
  118.     os.sleep(waitTime) -- Wait for crops to grow
  119. end
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement