Advertisement
buffsovernexus

Untitled

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