Advertisement
buffsovernexus

FarmBot v1.0.0

May 11th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. --[[
  2.  
  3. FarmerBot v1.0.0
  4.  
  5. Guide
  6. -----------------
  7. Crops: Wheat
  8. -----------------
  9.  
  10. Goals:
  11. Farm down a line of crops and constantly check the crops.
  12.  
  13. ]]--
  14.  
  15. -- Globals --
  16. local CARROT = { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot" }
  17. local WHEAT = { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds" }
  18. local POTATO = { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato" }
  19.  
  20. -- Helper Function: Determine if the crop is fully developed. If not, alert the user.
  21. function isGrown(row, length, current, expected)
  22. rednet.send(11, "[FarmBot] Crop (" .. row .. ", " .. length .. ") status: " .. (current/expected) * 100 .. "%", "bttp")
  23. if current == expected then
  24. return true
  25. end
  26. return false
  27. end
  28.  
  29. -- Helper Function: Determines if the crop ID has any seeds for said crop. Selects seed if true.
  30. function hasSeeds(id)
  31. if id == CARROT.id then
  32. for i = 1, 16 do
  33. local data = turtle.getItemDetail(i)
  34. if data.name == CARROT.seed then
  35. turtle.select(i)
  36. return true
  37. end
  38. end
  39. elseif id == WHEAT.id then
  40. for i = 1, 16 do
  41. local data = turtle.getItemDetail(i)
  42. if data.name == WHEAT.seed then
  43. turtle.select(i)
  44. return true
  45. end
  46. end
  47. elseif id == POTATO.id then
  48. for i = 1, 16 do
  49. local data = turtle.getItemDetail(i)
  50. if data.name == POTATO.seed then
  51. turtle.select(i)
  52. return true
  53. end
  54. end
  55. else
  56. return false
  57. end
  58.  
  59. end
  60.  
  61. function dropItems()
  62. for i = 16,1,-1 do
  63. turtle.select(i)
  64. turtle.dropDown()
  65. end
  66. end
  67.  
  68. -- Network Handling (modem on right)
  69. if rednet.isOpen("right") then
  70. rednet.send(11, "[FarmBot] Connected to modem!", "bttp")
  71. else
  72. rednet.open("right")
  73. rednet.send(11, "[FarmBot] Turning on modem...", "bttp")
  74. rednet.send(11, "[FarmBot] Connected successfully!", "bttp")
  75. end
  76.  
  77.  
  78. -- Player Input
  79. local input = { rows = 0, length = 0 }
  80.  
  81. -- Ask player for input
  82. term.write("Please enter number of rows: ")
  83. input.rows = tonumber(read())
  84. term.write("Please enter in length of row: ")
  85. input.length = tonumber(read())
  86.  
  87. rednet.send(11, "[FarmBot] Farming " .. input.rows .. "x" .. input.length .. " sized farm.", "bttp")
  88.  
  89. -- Preface: Determine if the bot was placed on the ground. If so, go up one block.
  90. if turtle.detectDown() then
  91. turtle.up()
  92. os.sleep(2)
  93. turtle.forward()
  94. end
  95.  
  96. while true do
  97.  
  98. -- Start farming the area.
  99. for i = 1, input.rows do
  100.  
  101. for j = 1, input.length do
  102. -- Check status of current crop.
  103. if turtle.detectDown() then
  104. --There is something below. Determine if it is something we can handle.
  105. local success, data = turtle.inspectDown()
  106. if success then
  107. --Determine if names match to any crop
  108. if data.name == CARROT.name then
  109. --Determine if the crop is grown. If it is, remove it and replace it.
  110. if isGrown(i,j,data.metadata,CARROT.mature) then
  111. --Break the block... Then replace...
  112. turtle.digDown()
  113. turtle.suckDown() -- Pick up the local crop.
  114. if hasSeeds(CARROT.id) then
  115. turtle.placeDown()
  116. else
  117. rednet.send(11, "[FarmBot] Unable to find seed for CARROT (" .. i .. "," .. j .. ")", "bttp")
  118. end
  119. end
  120. elseif data.name == WHEAT.name then
  121. --Determine if the crop is grown. If it is, remove it and replace it.
  122. if isGrown(i,j,data.metadata,WHEAT.mature) then
  123. --Break the block... Then replace...
  124. turtle.digDown()
  125. turtle.suckDown() -- Pick up the local crop.
  126. if hasSeeds(WHEAT.id) then
  127. turtle.placeDown()
  128. else
  129. rednet.send(11, "[FarmBot] Unable to find seed for WHEAT (" .. i .. "," .. j .. ")", "bttp")
  130. end
  131. end
  132. elseif data.name == POTATO.name then
  133. --Determine if the crop is grown. If it is, remove it and replace it.
  134. if isGrown(i,j,data.metadata,POTATO.mature) then
  135. --Break the block... Then replace...
  136. turtle.digDown()
  137. turtle.suckDown() -- Pick up the local crop.
  138. if hasSeeds(POTATO.id) then
  139. turtle.placeDown()
  140. else
  141. rednet.send(11, "[FarmBot] Unable to find seed for POTATO (" .. i .. "," .. j .. ")", "bttp")
  142. end
  143. end
  144. else
  145. rednet.send(11, "[FarmBot] Unable to recognize crop (" .. data.name .. ")", "bttp")
  146. end
  147. else
  148. rednet.send(11, "[FarmBot] Cannot read block (" .. i .. "," .. j .. ")", "bttp")
  149. end
  150. else
  151. rednet.send(11, "[FarmBot] No block found (" .. i .. "," .. j .. ")", "bttp")
  152. end
  153.  
  154. -- Go forward one to continue.
  155. turtle.forward()
  156. end
  157. os.sleep(3)
  158. -- Done reviewing the row. Now move to new row.
  159. local isEven = i % 2 == 0
  160. if isEven == false then
  161. turtle.turnLeft()
  162. turtle.forward()
  163. turtle.turnLeft()
  164. else
  165. turtle.turnRight()
  166. turtle.forward()
  167. turtle.turnRight()
  168. end
  169. end
  170. rednet.send(11, "[FarmBot] Resetting back to start.", "bttp")
  171. os.sleep(5)
  172.  
  173. -- Go back to start where chest is.
  174. for k = 1, input.length do
  175. turtle.forward()
  176. end
  177. turtle.turnLeft()
  178. for l = 1, input.rows do
  179. turtle.forward()
  180. end
  181. turtle.turnLeft()
  182.  
  183. -- Drop items in hopper below.
  184. turtle.down()
  185. os.sleep(2)
  186. dropItems()
  187. os.sleep(2)
  188. turtle.up()
  189.  
  190. turtle.forward()
  191. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement