Advertisement
buffsovernexus

Untitled

May 14th, 2019
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 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.metadata > 7 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. 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[i].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 = maturity, name = cropName, seed = seedName, label = cropName }
  170. crops[getSize(crops) + 1] = crop
  171.  
  172. rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
  173. print(">> NEW CROP <<")
  174. print("Crop: ", crop.name)
  175. print("Seed:" , crop.seed)
  176. print("Maturity: ", crop.mature)
  177.  
  178. -- Step 5: Replace seed.
  179. if hasSeeds(id) then
  180. turtle.placeDown()
  181. else
  182. rednet.send(12, "[" .. os.getComputerLabel() .. "] Cannot replace seed: " .. cropName, "bttp")
  183. end
  184. end
  185. end
  186.  
  187. end
  188.  
  189.  
  190.  
  191.  
  192. -- Preface: Determine if the bot was placed on the ground. If so, go up one block.
  193. if turtle.detectDown() then
  194. turtle.up()
  195. turtle.forward()
  196. end
  197.  
  198. --[[ START FARMING TASK ]]--
  199. while true do
  200. -- Start farming the area.
  201. for i = 1, input.rows do
  202.  
  203. for j = 1, input.length do
  204. -- Check status of current crop.
  205. if turtle.detectDown() then
  206. --There is something below. Determine if it is something we can handle.
  207. local success, data = turtle.inspectDown()
  208. if success then
  209. local isKnown = false
  210. if data.name ~= "minecraft:cobblestone" or data.name ~= "minecraft:water" or data.name ~= "minecraft:cobblestone_slab" then
  211. for k,v in pairs(crops) do
  212. if data.name == v.name then
  213. isKnown = true
  214. if isGrown(i,j,data.metadata,v.mature) then
  215. --Break the block... Then replace...
  216. turtle.digDown()
  217. turtle.suckDown() -- Pick up the local crop.
  218. local curpos = turtle.getSelectedSlot();
  219. if hasSeeds(v.id) then
  220. turtle.placeDown()
  221. else
  222. rednet.send(12, "[" .. os.getComputerLabel() .. "] No seeds for " .. v.label .. " @ (" .. i .. "," .. j .. ")", "bttp")
  223. end
  224. turtle.select(curpos)
  225. end
  226. end
  227. end
  228. -- 3.0 Change: Have the robot learn the new crop.
  229. if isKnown == false then
  230. learnCrop(data)
  231. end
  232. end
  233. else
  234. rednet.send(12, "[" .. os.getComputerLabel() .. "] ERROR: No Block @ (" .. i .. "," .. j .. ")", "bttp")
  235. end
  236. else
  237. fixBlock()
  238. end
  239.  
  240. -- Go forward one to continue.
  241. if j < input.length then
  242. turtle.forward()
  243. end
  244. end
  245. -- Done reviewing the row. Now move to new row.
  246. local isEven = i % 2 == 0
  247. if isEven == false then
  248. turtle.turnLeft()
  249. turtle.forward()
  250. turtle.turnLeft()
  251. else
  252. turtle.turnRight()
  253. turtle.forward()
  254. turtle.turnRight()
  255. end
  256. end
  257.  
  258. -- Go back to start where chest is.
  259. if input.rows % 2 == 1 then
  260. for k = 1, input.length do
  261. turtle.forward()
  262. end
  263. else
  264. turtle.turnLeft()
  265. turtle.turnLeft()
  266. turtle.forward()
  267. end
  268. turtle.turnLeft()
  269. for l = 1, input.rows do
  270. turtle.forward()
  271. end
  272. turtle.turnLeft()
  273.  
  274. -- Drop items in hopper below.
  275. turtle.down()
  276.  
  277. os.sleep(2)
  278. refuel()
  279. dropItems()
  280. os.sleep(300)
  281.  
  282. turtle.up()
  283.  
  284. turtle.forward()
  285. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement