Advertisement
buffsovernexus

Untitled

May 14th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.65 KB | None | 0 0
  1. --[[
  2.  
  3. FarmerBot v1.0.0
  4.  
  5. -- Banned Mods --
  6. Natura > This mod was not properly built in the sense that the bot cannot understand when crops are done growing.
  7. HarvestCraft > This mod does not work well with the learning system. I will keep it in, but it will struggle with any harvestcraft crops.
  8.  
  9. -- Instructions --
  10. 1) Place your bot on top of a chest, which is one space below the bottom-right of the farm.
  11. 2) Run this script.
  12. 3) Enter in how many rows there are (how many rows are to the left?)
  13. 4) Enter in how long each row is (how far does the bot have to go down the row)
  14. 5) Enter in what seed the bot will place on empty farmland.
  15.  
  16. -- Side Notes --
  17. ~ The bot will stop working properly if you do not give it more fuel.
  18. ~ The bot will dynamically learn new props from the mod list.
  19. ~ The bot will refuel automatically if it runs into a fuel source (like feeding it a lava bucket in a hopper) before it goes again.
  20. ~ This script will give detailed information on each newly learned crop.
  21.  
  22.  
  23. -- Future Plans --
  24. ~ The bot will the ability to ignore a specific seed (enter in 0 instead of a seed ID) and place whatever it has in its inventory.
  25.  
  26. -- Known Issues --
  27. ~ For some reason, after the first go, the bot will randomly go off the grid. This may be a rogue .turnLeft() call.
  28. ]]--
  29.  
  30. -- Globals --
  31. local crops = {
  32. { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot", label = "Carrots" },
  33. { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds", label = "Wheat" },
  34. { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato", label = "Potatoes" },
  35. { id = 4, mature = 7, name = "magicalcrops:MinicioCrop", seed = "magicalcrops:MinicioSeeds", label = "Minicio" },
  36. { id = 5, mature = 7, name = "magicalcrops:FireCrop", seed = "magicalcrops:FireSeeds", label = "Fire" },
  37. { id = 6, mature = 7, name = "magicalcrops:SheepCrop", seed = "magicalcrops:SheepSeeds", label = "Sheep" },
  38. { id = 7, mature = 7, name = "magicalcrops:NatureCrop", seed = "magicalcrops:NatureSeeds", label = "Nature" },
  39. { id = 8, mature = 7, name = "magicalcrops:FluixCrop", seed = "magicalcrops:FluixSeeds", label = "Fluix" },
  40. { id = 9, mature = 7, name = "magicalcrops:QuartzCrop", seed = "magicalcrops:QuartzSeeds", label = "Quartz" },
  41. { id = 10, mature = 7, name = "magicalcrops:WaterCrop", seed = "magicalcrops:WaterSeeds", label = "Water" },
  42. { id = 11, mature = 7, name = "magicalcrops:NetherCrop", seed = "magicalcrops:NetherSeeds", label = "Nether" },
  43. { id = 12, mature = 7, name = "magicalcrops:EarthCrop", seed = "magicalcrops:EarthSeeds", label = "Earth" },
  44. { id = 13, mature = 7, name = "magicalcrops:DiamondCrop", seed = "magicalcrops:DiamondSeeds", label = "Diamond" }
  45. }
  46. local mods = { "harvestcraft", "botania", "growthcraft", "extrautilities", "magicalcrops", "witchery" }
  47.  
  48.  
  49. -- [[ START PLAYER INPUT ]]--
  50. local input = { rows = 0, length = 0, crop = 0 }
  51.  
  52. term.write("Enter number of rows (left): ")
  53. input.rows = tonumber(read())
  54. term.write("Enter in length of row (forward): ")
  55. input.length = tonumber(read())
  56. for k,v in pairs (crops) do
  57. print("#", v.id," - ", v.label)
  58. end
  59. term.write("Enter crop ID (or 0) for seeding: ")
  60. input.crop = tonumber(read())
  61. -- [[ END PLAYER INPUT ]]--
  62.  
  63. --[[ refuel()
  64.  
  65. Task: To check for any fuel in the slots and consume it.
  66.  
  67. ]]--
  68. function refuel()
  69. for i = 16,1,-1 do
  70. turtle.select(i)
  71. turtle.refuel( math.ceil(turtle.getItemCount()) )
  72. end
  73. end
  74.  
  75. --[[ dropItems()
  76.  
  77. Task: Select each item and put the item into the chest below.
  78.  
  79. ]]--
  80. function dropItems()
  81. for i = 16,1,-1 do
  82. turtle.select(i)
  83. turtle.dropDown()
  84. end
  85. end
  86.  
  87. --[[ handleModem()
  88.  
  89. Task: Allow the bot to remotely speak to a specific computer.
  90.  
  91. ]]--
  92. function handleModem()
  93. if rednet.isOpen("right") then
  94. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected to modem!", "bttp")
  95. else
  96. rednet.open("right")
  97. rednet.send(12, "[" .. os.getComputerLabel() .. "] Turning on modem...", "bttp")
  98. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected successfully!", "bttp")
  99. end
  100. end
  101.  
  102. --[[ fixBlock()
  103.  
  104. Task: Place a seed crop based on <input.crop>, or any seed if no specific crop.
  105.  
  106. ]]--
  107. function fixBlock()
  108. turtle.digDown()
  109. -- If the crop is 0, then the user wants the bot to place any seeds in the inventory.
  110. if input.crop == 0 then
  111. for k,v in pairs(crops) do
  112. if hasSeeds(v.id) then
  113. turtle.placeDown()
  114. break
  115. end
  116. end
  117. else
  118. -- The user has selected a specific crop.
  119. if hasSeeds(input.crop) then
  120. turtle.placeDown()
  121. end
  122. end
  123. end
  124.  
  125. -- 3.0 Function: Have the bot learn crops instead of manually entering them.
  126. function learnCrop(cropBlock)
  127. -- Determine if the block is in any of the mods we listed.
  128. for i = 1, getSize(mods) do
  129. if string.match( string.lower(cropBlock.name) , string.lower(mods[i]) ) then
  130. -- Step 1: Get the current list of crops and make the ID.
  131. local id = getSize(crops) + 1
  132. local cropName = cropBlock.name
  133.  
  134. -- Step 1b: Learn the maturity...
  135. local maturity = 7
  136. if cropBlock ~= nil then maturity = cropBlock.metadata end
  137.  
  138. -- Step 2: Break the block to find the seed.
  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. local isKnown = false
  145. for k,v in pairs(crops) do
  146. if data ~= nil then
  147. -- The crop is known, therefore skip it.
  148. if data.name == v.seed then
  149. isKnown = true
  150. end
  151. end
  152. end
  153.  
  154. if isKnown == false then
  155. local checkDuplicates = false
  156. if data ~= nil then
  157. for j=1, getSize(unknown) do
  158. if unknown[j].name == data.name then
  159. checkDuplicates = true
  160. end
  161. end
  162. end
  163. if checkDuplicates == false then
  164. unknown[getSize(unknown) + 1] = data
  165. end
  166. end
  167. end
  168.  
  169. -- Step 3: Find the seed using the unknown blocks in the turtle.
  170. -- Quick Logic: If only one new item is found, then that is the seed.
  171. -- Other quick logic: If more than one (probably 2 at most) then check for 'seed' in text.
  172. local seedName = ""
  173. getSize(unknown)
  174. if getSize(unknown) == 1 then
  175. seedName = unknown[1].name
  176. else
  177. for i=1,getSize(unknown) do
  178. if string.match( string.lower(unknown[i].name), string.lower("seed") ) then
  179. seedName = unknown[i].name
  180. break
  181. end
  182. end
  183. end
  184.  
  185. -- Step 4: Enter in new crop to list of crops.
  186. local crop = { id = id, mature = maturity, name = cropName, seed = seedName, label = cropName }
  187. crops[getSize(crops) + 1] = crop
  188.  
  189. rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
  190. print(">> NEW CROP <<")
  191. print("Crop: ", crop.name)
  192. print("Seed:" , crop.seed)
  193. print("Maturity: ", crop.mature)
  194.  
  195. -- Step 5: Replace seed.
  196. if hasSeeds(id) then
  197. turtle.placeDown()
  198. else
  199. rednet.send(12, "[" .. os.getComputerLabel() .. "] Cannot replace seed: " .. cropName, "bttp")
  200. end
  201. end
  202. end
  203.  
  204. end
  205.  
  206. --[[ Helper Functions ]]--
  207. function getSize(tbl)
  208. local count = 0
  209. if tbl == nil then return count end
  210. for _ in pairs(tbl) do count = count + 1 end
  211. return count
  212. end
  213. function isGrown(row, length, current, expected)
  214. if current == expected then
  215. return true
  216. end
  217. return false
  218. end
  219. function hasSeeds(id)
  220. for k,v in pairs(crops) do
  221. if v.id == id then
  222. for i = 1, 16 do
  223. local data = turtle.getItemDetail(i)
  224. if data ~= nil then
  225. if data.name == v.seed then
  226. turtle.select(i)
  227. return true
  228. end
  229. end
  230. end
  231. end
  232. end
  233. return false
  234. end
  235. --[[ End Helper Functions ]]--
  236.  
  237. --[[ START FARMING TASK ]]--
  238. -- Adjust bot placement.
  239. if turtle.detectDown() then
  240. turtle.up()
  241. turtle.forward()
  242. end
  243.  
  244. -- Make sure the bot is connected to the network.
  245. handleModem()
  246.  
  247. -- Declare settings entered so user can confirm.
  248. rednet.send(12, "----- " .. os.getComputerLabel() .. " Task -----", "bttp")
  249. rednet.send(12, "" .. input.rows .. "x" .. input.length, "bttp")
  250.  
  251. while true do
  252. for i = 1, input.rows do
  253. for j = 1, input.length do
  254. -- Determine if there is a crop below the bot.
  255. if turtle.detectDown() then
  256. local success, data = turtle.inspectDown()
  257. if success then
  258. -- Bot AI: Determine if the crop is known, or if its even a crop.
  259. local isKnown = false
  260. if data.name ~= "minecraft:cobblestone" or data.name ~= "minecraft:water" or data.name ~= "minecraft:cobblestone_slab" then
  261. for k,v in pairs(crops) do
  262. if data.name == v.name then
  263. isKnown = true
  264. if isGrown(i,j,data.metadata,v.mature) then
  265. -- The crop is fully matured. Need to break crop and replace with seed.
  266. turtle.digDown()
  267. turtle.suckDown()
  268. local curpos = turtle.getSelectedSlot();
  269. if hasSeeds(v.id) then
  270. turtle.placeDown()
  271. else
  272. rednet.send(12, "[" .. os.getComputerLabel() .. "] No seeds for " .. v.label .. " @ (" .. i .. "," .. j .. ")", "bttp")
  273. end
  274. -- Legend: "Reset the positioning of the selected slot so bot can stack crops properly."
  275. turtle.select(curpos)
  276. end
  277. end
  278. end
  279. -- If the crop is unknown, have the bot attempt to learn the crop.
  280. if isKnown == false then
  281. learnCrop(data)
  282. end
  283. end
  284. else
  285. rednet.send(12, "[" .. os.getComputerLabel() .. "] ERROR: No Block @ (" .. i .. "," .. j .. ")", "bttp")
  286. end
  287. else
  288. -- The block below does not have a crop. Attempt to remedy this.
  289. fixBlock()
  290. end
  291.  
  292. -- Go forward one to continue.
  293. if j < input.length then
  294. turtle.forward()
  295. end
  296. end
  297.  
  298. -- Done reviewing the row. Now move to new row.
  299. local isEven = i % 2 == 0
  300. if isEven == false then
  301. turtle.turnLeft()
  302. turtle.forward()
  303. turtle.turnLeft()
  304. else
  305. turtle.turnRight()
  306. turtle.forward()
  307. turtle.turnRight()
  308. end
  309. end
  310.  
  311. -- Goal: Attempt to go back to start.
  312.  
  313. -- Logic: If the number of rows to the left are even, then the bot will end up on a different side.
  314. if input.rows % 2 == 1 then
  315. for k = 1, input.length do
  316. turtle.forward()
  317. end
  318. else
  319. turtle.turnLeft()
  320. turtle.turnLeft()
  321. turtle.forward()
  322. end
  323. turtle.turnLeft()
  324. for l = 1, input.rows do
  325. turtle.forward()
  326. end
  327. turtle.turnLeft()
  328.  
  329. -- Drop items in hopper below.
  330. turtle.down()
  331.  
  332. -- Attempt to refuel if refueling is setup.
  333. os.sleep(2)
  334. refuel()
  335. -- Drop all items in the inventory into chest below.
  336. dropItems()
  337. -- Wait for crops to grow a bit.
  338. os.sleep(300)
  339.  
  340. -- Reset bot position for next harvest.
  341. turtle.up()
  342. turtle.forward()
  343. end
  344. --[[ END FARMING TASK ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement