Advertisement
buffsovernexus

Untitled

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