Advertisement
Itz_AJ_Playz

Untitled

Aug 5th, 2024
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. -- Initialize farm size variables
  2. local width, height
  3.  
  4. -- Function to read farm size from user input
  5. local function readFarmSize()
  6. print("Enter farm width: ")
  7. width = tonumber(read())
  8. print("Enter farm height: ")
  9. height = tonumber(read())
  10. end
  11.  
  12. -- Function to check and refuel if needed
  13. local function checkFuel()
  14. local fuelLevel = turtle.getFuelLevel()
  15. print("Current fuel level: " .. fuelLevel)
  16. if fuelLevel < 10 then
  17. print("Low fuel. Refueling...")
  18. turtle.select(1) -- Assume fuel is in slot 1
  19. if not turtle.refuel() then
  20. print("Failed to refuel. Check fuel source.")
  21. return false
  22. end
  23. end
  24. return true
  25. end
  26.  
  27. -- Function to check if a crop below the turtle is fully grown
  28. local function isCropGrown()
  29. local success, data = turtle.inspectDown()
  30. if success then
  31. return data.state and data.state.age == 7
  32. end
  33. return false
  34. end
  35.  
  36. -- Function to get the seed type from a slot
  37. local function getSeedType()
  38. for i = 1, 16 do
  39. local item = turtle.getItemDetail(i)
  40. if item and item.name:find("seed") then
  41. return i
  42. end
  43. end
  44. return nil
  45. end
  46.  
  47. -- Function to replant a crop using the detected seed type
  48. local function replantCrop()
  49. local seedSlot = getSeedType()
  50. if seedSlot then
  51. turtle.select(seedSlot)
  52. turtle.placeDown()
  53. else
  54. print("No seeds found in inventory.")
  55. end
  56. end
  57.  
  58. -- Function to move the turtle forward a certain number of steps
  59. local function moveForward(steps)
  60. for i = 1, steps do
  61. if not checkFuel() then
  62. return
  63. end
  64. if not turtle.forward() then
  65. print("Unable to move forward, possibly blocked")
  66. return
  67. end
  68. end
  69. end
  70.  
  71. -- Function to move the turtle to the next row
  72. local function moveToNextRow(isEvenRow)
  73. if isEvenRow then
  74. turtle.turnLeft()
  75. moveForward(1)
  76. turtle.turnLeft()
  77. else
  78. turtle.turnRight()
  79. moveForward(1)
  80. turtle.turnRight()
  81. end
  82. end
  83.  
  84. -- Function to move the turtle back to its starting position (bottom-right corner)
  85. local function returnToStart()
  86. -- Turn around to face the starting corner
  87. turtle.turnRight()
  88. turtle.turnRight()
  89. moveForward(height - 1) -- Move up the farm
  90. turtle.turnLeft()
  91. moveForward(width - 1) -- Move to the bottom-right corner
  92. turtle.turnLeft()
  93. end
  94.  
  95. -- Function to deposit items into a chest behind the turtle's starting position
  96. local function depositItems()
  97. -- Turn to face the chest (which is behind the turtle's starting position)
  98. turtle.turnLeft()
  99. turtle.turnLeft()
  100. for i = 1, 16 do
  101. turtle.select(i)
  102. turtle.drop()
  103. end
  104. -- Return to the original orientation
  105. turtle.turnLeft()
  106. turtle.turnLeft()
  107. end
  108.  
  109. -- Function to harvest and replant crops in a zigzag pattern
  110. local function harvestAndReplant()
  111. for y = 1, height do
  112. for x = 1, width do
  113. if isCropGrown() then
  114. turtle.digDown()
  115. replantCrop() -- Replant the same type of seed detected in inventory
  116. end
  117. if x < width then
  118. moveForward(1) -- Move forward one block
  119. end
  120. end
  121. if y < height then
  122. moveToNextRow(y % 2 == 1)
  123. end
  124. end
  125. end
  126.  
  127. -- Main program loop
  128. print("Welcome to the Turtle Farm Program!")
  129. print("Please place fuel in slot 1 of the turtle's inventory.")
  130. readFarmSize()
  131.  
  132. while true do
  133. harvestAndReplant()
  134. returnToStart()
  135. depositItems()
  136. os.sleep(1800) -- Wait for 30 minutes before the next cycle
  137. end
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement