Advertisement
Nemirel345

farm.lua

Mar 24th, 2023 (edited)
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. farmLength = 19
  2. farmWidth = 8
  3.  
  4. function fuel()
  5.     if turtle.getFuelLevel() < 1 then
  6.         turtle.select(1)
  7.         turtle.refuel(1)
  8.     end
  9. end
  10.  
  11. function move(d, n)
  12.     n = n or 1
  13.     for i=1,n do
  14.         fuel()
  15.         d()
  16.     end
  17. end
  18.  
  19. function harvest()
  20.     local success, data = turtle.inspectDown()
  21.     if success then
  22.         if data.state.age == 7 then
  23.             turtle.digDown()
  24.             turtle.select(2)
  25.             if turtle.getItemCount() > 1 then
  26.                 turtle.placeDown()
  27.             end
  28.         end
  29.     end
  30. end
  31.  
  32. function harvestRow()
  33.     harvest()
  34.     for i=2, farmLength do
  35.         move(turtle.forward)
  36.         harvest()
  37.     end
  38. end
  39.  
  40. turnAround = {
  41.     [0] = turtle.turnLeft,
  42.     [1] = turtle.turnRight
  43. }
  44.  
  45. function run()
  46.     move(turtle.forward)
  47.     for row=1, farmWidth do
  48.         harvestRow()
  49.         if row < farmWidth then
  50.             turnAround[row % 2]()
  51.             move(turtle.forward)
  52.             turnAround[row % 2]()
  53.         elseif row % 2 == 1 then
  54.             turtle.turnRight()
  55.             turtle.turnRight()
  56.             move(turtle.forward,farmLength-1)
  57.         end
  58.     end
  59.     turtle.turnRight()
  60.     move(turtle.forward,farmWidth-1)
  61.     turtle.turnRight()
  62.     move(turtle.back)
  63.    
  64.     for i=3,16 do
  65.         turtle.select(i)
  66.         turtle.dropDown()
  67.     end
  68. end
  69.  
  70. local alarm = os.setAlarm(5)
  71. while true do
  72.     local evt, arg = os.pullEvent("alarm")
  73.     if arg == alarm then
  74.         run()
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement