Advertisement
buffsovernexus

Untitled

May 14th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.50 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(level)
  71. for i = 1, 16 do
  72. if turtle.getFuelLevel() > level then
  73. break
  74. end
  75. turtle.select(i)
  76. if refuel(0) then
  77. turtle.refuel( math.ceil(turtle.getItemCount()) )
  78. end
  79. end
  80. end
  81.  
  82. function dropItems()
  83. for i = 16,1,-1 do
  84. turtle.select(i)
  85. turtle.dropDown()
  86. end
  87. end
  88.  
  89. -- Network Handling (modem on right)
  90. if rednet.isOpen("right") then
  91. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected to modem!", "bttp")
  92. else
  93. rednet.open("right")
  94. rednet.send(12, "[" .. os.getComputerLabel() .. "] Turning on modem...", "bttp")
  95. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected successfully!", "bttp")
  96. end
  97.  
  98. -- 2.0 Function: Attempt to fix an un-tilled grass block
  99. function fixBlock()
  100. turtle.digDown()
  101. if hasSeeds(input.crop) then
  102. turtle.placeDown()
  103. end
  104. end
  105.  
  106. -- Helper Function: Finds the table size.
  107. function getSize(tbl)
  108. local count = 0
  109. if tbl == nil then return count end
  110. for _ in pairs(tbl) do count = count + 1 end
  111. return count
  112. end
  113.  
  114. -- 3.0 Function: Have the bot learn crops instead of manually entering them.
  115. function learnCrop(cropBlock)
  116. -- Determine if the block is in any of the mods we listed.
  117. for i = 1, getSize(mods) do
  118. if string.match( string.lower(cropBlock.name) , string.lower(mods[i]) ) then
  119. -- Step 1: Get the current list of crops and make the ID.
  120. local id = getSize(crops) + 1
  121. local cropName = cropBlock.name
  122.  
  123. -- Step 1b: Learn the maturity...
  124. local maturity = 7
  125. if cropBlock.metadata > 7 then maturity = cropBlock.metadata end
  126.  
  127. -- Step 2: Break the block to find the seed.
  128. turtle.digDown()
  129. local unknown = {}
  130. for i = 1, 16 do
  131. local data = turtle.getItemDetail(i)
  132. --Look through each inventory space to find the unknown crop
  133. local isKnown = false
  134. for k,v in pairs(crops) do
  135. if data ~= nil then
  136. if data.name == v.seed then
  137. --Found something not recognized.
  138. isKnown = true
  139. end
  140. end
  141. end
  142. if isKnown == false then
  143. local checkDuplicates = false
  144. if data ~= nil then
  145. for j=1, getSize(unknown) do
  146. if unknown[j].name == data.name then
  147. checkDuplicates = true
  148. end
  149. end
  150. end
  151. if checkDuplicates == false then
  152. unknown[getSize(unknown) + 1] = data
  153. end
  154. end
  155. end
  156.  
  157. -- Step 3: Find the seed using the unknown blocks in the turtle.
  158. -- Quick Logic: If only one new item is found, then that is the seed.
  159. -- Other quick logic: If more than one (probably 2 at most) then check for 'seed' in text.
  160. local seedName = ""
  161. if getSize(unknown) == 1 then
  162. seedName = unknown[1].name
  163. else
  164. for i=1,getSize(unknown) do
  165. if string.match( string.lower(unknown[i].name), string.lower("seed") ) then
  166. seedName = unknown[i].name
  167. break
  168. end
  169. end
  170. end
  171.  
  172. -- Step 4: Enter in new crop to list of crops.
  173. local crop = { id = id, mature = maturity, name = cropName, seed = seedName, label = cropName }
  174. crops[getSize(crops) + 1] = crop
  175.  
  176. rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
  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. local needs = input.rows * input.length + input.rows + input.length + 2
  279. refuel(needs)
  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