Advertisement
buffsovernexus

Untitled

May 12th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 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. 2.0 Goals:
  14. - Allow multiple bots at the same time (designating their name in logs)
  15. - Attempt to till the ground below it (excluding water)
  16. - Add more crops
  17. ]]--
  18.  
  19. -- Globals --
  20. local crops = {
  21. { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot", label = "Carrots" },
  22. { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds", label = "Wheat" },
  23. { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato", label = "Potatoes" },
  24. { id = 4, mature = 7, name = "magicalcrops:MinicioCrop", seed = "magicalcrops:MinicioSeeds", label = "Minicio" },
  25. { id = 5, mature = 7, name = "magicalcrops:FireCrop", seed = "magicalcrops:FireSeeds", label = "Fire" },
  26. { id = 6, mature = 7, name = "magicalcrops:SheepCrop", seed = "magicalcrops:SheepSeeds", label = "Sheep" },
  27. { id = 7, mature = 7, name = "magicalcrops:NatureCrop", seed = "magicalcrops:NatureSeeds", label = "Nature" },
  28. { id = 8, mature = 7, name = "magicalcrops:FluixCrop", seed = "magicalcrops:FluixSeeds", label = "Fluix" },
  29. { id = 9, mature = 7, name = "magicalcrops:QuartzCrop", seed = "magicalcrops:QuartzSeeds", label = "Quartz" },
  30. { id = 10, mature = 7, name = "magicalcrops:WaterCrop", seed = "magicalcrops:WaterSeeds", label = "Water" },
  31. { id = 11, mature = 7, name = "magicalcrops:NetherCrop", seed = "magicalcrops:NetherSeeds", label = "Nether" },
  32. { id = 12, mature = 7, name = "magicalcrops:EarthCrop", seed = "magicalcrops:EarthSeeds", label = "Earth" },
  33. { id = 13, mature = 7, name = "magicalcrops:DiamondCrop", seed = "magicalcrops:DiamondSeeds", label = "Diamond" }
  34. }
  35.  
  36. local mods = { "harvestcraft", "botania", "natura", "growthcraft", "extrautilities", "magicalcrops", "witchery" }
  37.  
  38. -- [[ START PLAYER INPUT ]]--
  39. local input = { rows = 0, length = 0, crop = 0 }
  40.  
  41. term.write("Enter number of rows (left): ")
  42. input.rows = tonumber(read())
  43. term.clear()
  44. term.write("Enter in length of row (forward): ")
  45. input.length = tonumber(read())
  46. term.clear()
  47. for k,v in pairs (crops) do
  48. print("#", v.id," - ", v.label)
  49. end
  50. term.write("Enter crop ID for seeding: ")
  51. input.crop = tonumber(read())
  52. term.clear()
  53.  
  54. -- [[ END PLAYER INPUT ]]--
  55. rednet.send(12, "----- " .. os.getComputerLabel() .. " Task -----", "bttp")
  56. rednet.send(12, " " .. input.rows .. "x" .. input.length, "bttp")
  57.  
  58. -- Helper Function: Determine if the crop is fully developed. If not, alert the user.
  59. function isGrown(row, length, current, expected)
  60. if current == expected then
  61. return true
  62. end
  63. return false
  64. end
  65.  
  66. -- Helper Function: Determines if the crop ID has any seeds for said crop. Selects seed if true.
  67. function hasSeeds(id)
  68. for k,v in pairs(crops) do
  69. if v.id == id then
  70. for i = 1, 16 do
  71. local data = turtle.getItemDetail(i)
  72. if data ~= nil then
  73. if data.name == v.seed then
  74. turtle.select(i)
  75. return true
  76. end
  77. end
  78. end
  79. end
  80. end
  81. return false
  82. end
  83.  
  84. --refueling
  85. function refuel(level)
  86. for i = 1, 16 do
  87. if turtle.getFuelLevel() > level then
  88. break
  89. end
  90. turtle.select(i)
  91. if refuel(0) then
  92. turtle.refuel( math.ceil(turtle.getItemCount()) )
  93. end
  94. end
  95. end
  96.  
  97. function dropItems()
  98. for i = 16,1,-1 do
  99. turtle.select(i)
  100. turtle.dropDown()
  101. end
  102. end
  103.  
  104. -- Network Handling (modem on right)
  105. if rednet.isOpen("right") then
  106. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected to modem!", "bttp")
  107. else
  108. rednet.open("right")
  109. rednet.send(12, "[" .. os.getComputerLabel() .. "] Turning on modem...", "bttp")
  110. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected successfully!", "bttp")
  111. end
  112.  
  113. -- 2.0 Function: Attempt to fix an un-tilled grass block
  114. function fixBlock()
  115. turtle.digDown()
  116. if hasSeeds(input.crop) then
  117. turtle.placeDown()
  118. end
  119. end
  120.  
  121. -- Helper Function: Finds the table size.
  122. function getSize(tbl)
  123. local count = 0
  124. for _ in pairs(tbl) do count = count + 1 end
  125. return count
  126. end
  127.  
  128. -- 3.0 Function: Have the bot learn crops instead of manually entering them.
  129. function learnCrop(cropBlock)
  130. -- Determine if the block is in any of the mods we listed.
  131. for i = 1, getSize(mods) do
  132. if string.match( string.lower(cropBlock.name) , string.lower(mods[i]) ) then
  133. -- Step 1: Get the current list of crops and make the ID.
  134. local id = getSize(crops) + 1
  135. local cropName = cropBlock.name
  136.  
  137. -- Step 2: Break the block to find the seed.
  138. local seedName = ""
  139. turtle.digDown()
  140. local unknown = {}
  141. for i = 1, 16 do
  142. local data = turtle.getItemDetail(i)
  143. --Look through each inventory space to find the unknown crop
  144. for k,v in pairs(crops) do
  145. if data ~= nil then
  146. if data.name ~= v.name then
  147. --Found something not recognized.
  148. unknown[getSize(unknown) + 1] = data
  149. end
  150. end
  151. end
  152. end
  153.  
  154. -- Step 3: Find the seed using the unknown blocks in the turtle.
  155. -- Quick Logic: If only one new item is found, then that is the seed.
  156. -- Other quick logic: If more than one (probably 2 at most) then check for 'seed' in text.
  157. if getSize(unknown) == 1 then
  158. seedName = unknown[1].name
  159. else
  160. for i=1,getSize(unknown) do
  161. if string.match( string.lower(unknown[i].name), string.lower("seed") ) then
  162. seedName = unknown.name
  163. break
  164. end
  165. end
  166. end
  167.  
  168. -- Step 4: Enter in new crop to list of crops.
  169. local crop = { id = id, mature = 7, name = cropName, seed = seedName, label = cropName }
  170. crops[getSize(crops) + 1] = crop
  171.  
  172. rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
  173.  
  174. -- Step 5: Replace seed.
  175. hasSeeds(id)
  176. turtle.placeDown()
  177. end
  178. end
  179.  
  180. end
  181.  
  182.  
  183.  
  184.  
  185. -- Preface: Determine if the bot was placed on the ground. If so, go up one block.
  186. if turtle.detectDown() then
  187. turtle.up()
  188. turtle.forward()
  189. end
  190.  
  191. --[[ START FARMING TASK ]]--
  192. while true do
  193. -- Start farming the area.
  194. for i = 1, input.rows do
  195.  
  196. for j = 1, input.length do
  197. -- Check status of current crop.
  198. if turtle.detectDown() then
  199. --There is something below. Determine if it is something we can handle.
  200. local success, data = turtle.inspectDown()
  201. if success then
  202. local isKnown = false
  203. if data.name ~= "minecraft:cobblestone" or data.name ~= "minecraft:water" or data.name ~= "minecraft:cobblestone_slab" then
  204. for k,v in pairs(crops) do
  205. if data.name == v.name then
  206. isKnown = true
  207. if isGrown(i,j,data.metadata,v.mature) then
  208. --Break the block... Then replace...
  209. turtle.digDown()
  210. turtle.suckDown() -- Pick up the local crop.
  211. local curpos = turtle.getSelectedSlot();
  212. if hasSeeds(v.id) then
  213. turtle.placeDown()
  214. else
  215. rednet.send(12, "[" .. os.getComputerLabel() .. "] No seeds for " .. v.label .. " @ (" .. i .. "," .. j .. ")", "bttp")
  216. end
  217. turtle.select(curpos)
  218. end
  219. end
  220. end
  221. -- 3.0 Change: Have the robot learn the new crop.
  222. if isKnown == false then
  223. learnCrop(data)
  224. end
  225. end
  226. else
  227. rednet.send(12, "[" .. os.getComputerLabel() .. "] ERROR: No Block @ (" .. i .. "," .. j .. ")", "bttp")
  228. end
  229. else
  230. fixBlock()
  231. end
  232.  
  233. -- Go forward one to continue.
  234. if j < input.length then
  235. turtle.forward()
  236. end
  237. end
  238. -- Done reviewing the row. Now move to new row.
  239. local isEven = i % 2 == 0
  240. if isEven == false then
  241. turtle.turnLeft()
  242. turtle.forward()
  243. turtle.turnLeft()
  244. else
  245. turtle.turnRight()
  246. turtle.forward()
  247. turtle.turnRight()
  248. end
  249. end
  250.  
  251. -- Go back to start where chest is.
  252. if input.rows % 2 == 1 then
  253. for k = 1, input.length do
  254. turtle.forward()
  255. end
  256. else
  257. turtle.turnLeft()
  258. turtle.turnLeft()
  259. turtle.forward()
  260. end
  261. turtle.turnLeft()
  262. for l = 1, input.rows do
  263. turtle.forward()
  264. end
  265. turtle.turnLeft()
  266.  
  267. -- Drop items in hopper below.
  268. turtle.down()
  269.  
  270. os.sleep(2)
  271. local needs = input.rows * input.length + input.rows + input.length + 2
  272. refuel(needs)
  273. dropItems()
  274. os.sleep(300)
  275.  
  276. turtle.up()
  277.  
  278. turtle.forward()
  279. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement