Advertisement
Itz_AJ_Playz

Untitled

Aug 5th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 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. local age = data.state and data.state.age
  32. print("Crop age: " .. (age or "not available"))
  33. return age and age == 7
  34. end
  35. print("No crop detected below.")
  36. return false
  37. end
  38.  
  39. -- Function to generate seed name from crop name
  40. local function getSeedName(cropName)
  41. -- Check if the crop name ends with "_crop"
  42. if cropName:match("_crop$") then
  43. -- Replace "_crop" with "_seed"
  44. return cropName:gsub("_crop$", "_seed")
  45. else
  46. -- Simply add "_seeds" to the end
  47. return cropName:lower():gsub(" ", "_") .. "_seeds"
  48. end
  49. end
  50.  
  51. -- Function to find and select the seed item in inventory
  52. local function selectSeed(seedName)
  53. print("Searching for seed: " .. seedName)
  54. for i = 1, 16 do
  55. local item = turtle.getItemDetail(i)
  56. if item and item.name == seedName then
  57. print("Selecting seed: " .. seedName)
  58. turtle.select(i)
  59. return true
  60. end
  61. end
  62. print("Seed not found in inventory: " .. seedName)
  63. return false
  64. end
  65.  
  66. -- Function to replant a crop using the detected seed type
  67. local function replantCrop(cropName)
  68. local seedName = getSeedName(cropName)
  69. print("Generated seed name: " .. seedName)
  70. if selectSeed(seedName) then
  71. print("Replanting seed: " .. seedName)
  72. if not turtle.placeDown() then
  73. print("Failed to place seed.")
  74. end
  75. else
  76. print("Unable to replant. Seed not found.")
  77. end
  78. end
  79.  
  80. -- Function to move the turtle forward a certain number of steps
  81. local function moveForward(steps)
  82. print("Moving forward " .. steps .. " steps")
  83. for i = 1, steps do
  84. if not checkFuel() then
  85. return
  86. end
  87. if not turtle.forward() then
  88. print("Unable to move forward, possibly blocked")
  89. return
  90. end
  91. end
  92. end
  93.  
  94. -- Function to move the turtle to the next row
  95. local function moveToNextRow(isEvenRow)
  96. print("Moving to next row. Is even row? " .. tostring(isEvenRow))
  97. if isEvenRow then
  98. turtle.turnLeft()
  99. moveForward(1)
  100. turtle.turnLeft()
  101. else
  102. turtle.turnRight()
  103. moveForward(1)
  104. turtle.turnRight()
  105. end
  106. end
  107.  
  108. -- Function to move the turtle back to its starting position (bottom-right corner)
  109. local function returnToStart()
  110. print("Returning to start position")
  111. -- Turn around to face the starting corner
  112. turtle.turnRight()
  113. turtle.turnRight()
  114. moveForward(height - 1) -- Move up the farm
  115. turtle.turnLeft()
  116. moveForward(width - 1) -- Move to the bottom-right corner
  117. turtle.turnLeft()
  118. end
  119.  
  120. -- Function to deposit items into a chest behind the turtle's starting position
  121. local function depositItems()
  122. print("Depositing items")
  123. -- Turn to face the chest (which is behind the turtle's starting position)
  124. turtle.turnLeft()
  125. turtle.turnLeft()
  126. for i = 1, 16 do
  127. turtle.select(i)
  128. turtle.drop()
  129. end
  130. -- Return to the original orientation
  131. turtle.turnLeft()
  132. turtle.turnLeft()
  133. end
  134.  
  135. -- Function to harvest and replant crops in a zigzag pattern
  136. local function harvestAndReplant()
  137. print("Starting harvest and replant process")
  138. for y = 1, height do
  139. for x = 1, width do
  140. if isCropGrown() then
  141. -- Inspect the crop before harvesting
  142. local success, data = turtle.inspectDown()
  143. if success then
  144. local cropName = data.name
  145. print("Detected crop: " .. cropName)
  146.  
  147. -- Harvest the crop
  148. print("Harvesting crop at position (" .. x .. ", " .. y .. ")")
  149. turtle.digDown()
  150.  
  151. -- Wait for a second to ensure the crop is fully processed
  152. os.sleep(1)
  153.  
  154. -- Replant the crop using the detected seed type
  155. replantCrop(cropName)
  156. else
  157. print("Failed to inspect crop.")
  158. end
  159. end
  160. if x < width then
  161. moveForward(1) -- Move forward one block
  162. end
  163. end
  164. if y < height then
  165. moveToNextRow(y % 2 == 1)
  166. end
  167. end
  168. end
  169.  
  170. -- Main program loop
  171. print("Welcome to the Turtle Farm Program!")
  172. print("Please place fuel in slot 1 of the turtle's inventory.")
  173. readFarmSize()
  174.  
  175. while true do
  176. harvestAndReplant()
  177. returnToStart()
  178. depositItems()
  179. os.sleep(1800) -- Wait for 30 minutes before the next cycle
  180. end
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement