Advertisement
buffsovernexus

Untitled

May 12th, 2019
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 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 1b: Learn the maturity...
  138. local maturity = 7
  139. if cropBlock.metadata > 7 then maturity = cropBlock.metadata end
  140.  
  141. -- Step 2: Break the block to find the seed.
  142. turtle.digDown()
  143. local unknown = {}
  144. for i = 1, 16 do
  145. local data = turtle.getItemDetail(i)
  146. --Look through each inventory space to find the unknown crop
  147. local isKnown = false
  148. for k,v in pairs(crops) do
  149. if data ~= nil then
  150. if data.name == v.seed then
  151. --Found something not recognized.
  152. isKnown = true
  153. end
  154. end
  155. end
  156. if isKnown == false then
  157. for i=1, getSize(unknown) do
  158. if unknown[i].name ~= data.name then
  159. unknown[getSize(unknown) + 1] = data
  160. end
  161. end
  162. end
  163. end
  164.  
  165. -- Step 3: Find the seed using the unknown blocks in the turtle.
  166. -- Quick Logic: If only one new item is found, then that is the seed.
  167. -- Other quick logic: If more than one (probably 2 at most) then check for 'seed' in text.
  168. local seedName = ""
  169. if getSize(unknown) == 1 then
  170. seedName = unknown[1].name
  171. else
  172. for i=1,getSize(unknown) do
  173. if string.match( string.lower(unknown[i].name), string.lower("seed") ) then
  174. seedName = unknown[i].name
  175. break
  176. end
  177. end
  178. end
  179.  
  180. -- Step 4: Enter in new crop to list of crops.
  181. local crop = { id = id, mature = maturity, name = cropName, seed = seedName, label = cropName }
  182. crops[getSize(crops) + 1] = crop
  183.  
  184. rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
  185.  
  186. -- Step 5: Replace seed.
  187. if hasSeeds(id) then
  188. turtle.placeDown()
  189. else
  190. rednet.send(12, "[" .. os.getComputerLabel() .. "] Cannot replace seed: " .. cropName, "bttp")
  191. end
  192. end
  193. end
  194.  
  195. end
  196.  
  197.  
  198.  
  199.  
  200. -- Preface: Determine if the bot was placed on the ground. If so, go up one block.
  201. if turtle.detectDown() then
  202. turtle.up()
  203. turtle.forward()
  204. end
  205.  
  206. --[[ START FARMING TASK ]]--
  207. while true do
  208. -- Start farming the area.
  209. for i = 1, input.rows do
  210.  
  211. for j = 1, input.length do
  212. -- Check status of current crop.
  213. if turtle.detectDown() then
  214. --There is something below. Determine if it is something we can handle.
  215. local success, data = turtle.inspectDown()
  216. if success then
  217. local isKnown = false
  218. if data.name ~= "minecraft:cobblestone" or data.name ~= "minecraft:water" or data.name ~= "minecraft:cobblestone_slab" then
  219. for k,v in pairs(crops) do
  220. if data.name == v.name then
  221. isKnown = true
  222. if isGrown(i,j,data.metadata,v.mature) then
  223. --Break the block... Then replace...
  224. turtle.digDown()
  225. turtle.suckDown() -- Pick up the local crop.
  226. local curpos = turtle.getSelectedSlot();
  227. if hasSeeds(v.id) then
  228. turtle.placeDown()
  229. else
  230. rednet.send(12, "[" .. os.getComputerLabel() .. "] No seeds for " .. v.label .. " @ (" .. i .. "," .. j .. ")", "bttp")
  231. end
  232. turtle.select(curpos)
  233. end
  234. end
  235. end
  236. -- 3.0 Change: Have the robot learn the new crop.
  237. if isKnown == false then
  238. learnCrop(data)
  239. end
  240. end
  241. else
  242. rednet.send(12, "[" .. os.getComputerLabel() .. "] ERROR: No Block @ (" .. i .. "," .. j .. ")", "bttp")
  243. end
  244. else
  245. fixBlock()
  246. end
  247.  
  248. -- Go forward one to continue.
  249. if j < input.length then
  250. turtle.forward()
  251. end
  252. end
  253. -- Done reviewing the row. Now move to new row.
  254. local isEven = i % 2 == 0
  255. if isEven == false then
  256. turtle.turnLeft()
  257. turtle.forward()
  258. turtle.turnLeft()
  259. else
  260. turtle.turnRight()
  261. turtle.forward()
  262. turtle.turnRight()
  263. end
  264. end
  265.  
  266. -- Go back to start where chest is.
  267. if input.rows % 2 == 1 then
  268. for k = 1, input.length do
  269. turtle.forward()
  270. end
  271. else
  272. turtle.turnLeft()
  273. turtle.turnLeft()
  274. turtle.forward()
  275. end
  276. turtle.turnLeft()
  277. for l = 1, input.rows do
  278. turtle.forward()
  279. end
  280. turtle.turnLeft()
  281.  
  282. -- Drop items in hopper below.
  283. turtle.down()
  284.  
  285. os.sleep(2)
  286. local needs = input.rows * input.length + input.rows + input.length + 2
  287. refuel(needs)
  288. dropItems()
  289. os.sleep(300)
  290.  
  291. turtle.up()
  292.  
  293. turtle.forward()
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement