Advertisement
buffsovernexus

Untitled

May 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.40 KB | None | 0 0
  1. --[[
  2.  
  3. FarmerBot v1.0.0
  4.  
  5. Guide
  6. -----------------
  7. Crops: Wheat
  8. -----------------
  9.  
  10. Goals:
  11. Farm down a line of crops and constantly check the crops.
  12.  
  13. 2.0 Goals:
  14. - Allow multiple bots at the same time (designating their name in logs)
  15. - Attempt to till the ground below it (excluding water)
  16. - Add more crops
  17. ]]--
  18.  
  19. -- Globals --
  20. local crops = {
  21. { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot", label = "Carrots" },
  22. { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds", label = "Wheat" },
  23. { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato", label = "Potatoes" },
  24. { id = 4, mature = 7, name = "magicalcrops:MinicioCrop", seed = "magicalcrops:MinicioSeeds", label = "Minicio" },
  25. { id = 5, mature = 7, name = "magicalcrops:FireCrop", seed = "magicalcrops:FireSeeds", label = "Fire" },
  26. { id = 6, mature = 7, name = "magicalcrops:SheepCrop", seed = "magicalcrops:SheepSeeds", label = "Sheep" },
  27. { id = 7, mature = 7, name = "magicalcrops:NatureCrop", seed = "magicalcrops:NatureSeeds", label = "Nature" },
  28. { id = 8, mature = 7, name = "magicalcrops:FluixCrop", seed = "magicalcrops:FluixSeeds", label = "Fluix" },
  29. { id = 9, mature = 7, name = "magicalcrops:QuartzCrop", seed = "magicalcrops:QuartzSeeds", label = "Quartz" },
  30. { id = 10, mature = 7, name = "magicalcrops:WaterCrop", seed = "magicalcrops:WaterSeeds", label = "Water" },
  31. { id = 11, mature = 7, name = "magicalcrops:NetherCrop", seed = "magicalcrops:NetherSeeds", label = "Nether" },
  32. { id = 12, mature = 7, name = "magicalcrops:EarthCrop", seed = "magicalcrops:EarthSeeds", label = "Earth" },
  33. { id = 13, mature = 7, name = "magicalcrops:DiamondCrop", seed = "magicalcrops:DiamondSeeds", label = "Diamond" }
  34. }
  35.  
  36. local mods = { "harvestcraft", "botania", "natura", "growthcraft", "extrautilities", "magicalcrops", "witchery" }
  37.  
  38. -- [[ START PLAYER INPUT ]]--
  39. local input = { rows = 0, length = 0, crop = 0 }
  40.  
  41. term.write("Enter number of rows (left): ")
  42. input.rows = tonumber(read())
  43. term.clear()
  44. term.write("Enter in length of row (forward): ")
  45. input.length = tonumber(read())
  46. term.clear()
  47. for k,v in pairs (crops) do
  48. print("#", v.id," - ", v.label)
  49. end
  50. term.write("Enter crop ID for seeding: ")
  51. input.crop = tonumber(read())
  52. term.clear()
  53.  
  54. -- [[ END PLAYER INPUT ]]--
  55. rednet.send(12, "----- " .. os.getComputerLabel() .. " Task -----", "bttp")
  56. rednet.send(12, " " .. input.rows .. "x" .. input.length, "bttp")
  57.  
  58. -- Helper Function: Determine if the crop is fully developed. If not, alert the user.
  59. function isGrown(row, length, current, expected)
  60. if current == expected then
  61. return true
  62. end
  63. return false
  64. end
  65.  
  66. -- Helper Function: Determines if the crop ID has any seeds for said crop. Selects seed if true.
  67. function hasSeeds(id)
  68. for k,v in pairs(crops) do
  69. if v.id == id then
  70. for i = 1, 16 do
  71. local data = turtle.getItemDetail(i)
  72. if data ~= nil then
  73. if data.name == v.seed then
  74. turtle.select(i)
  75. return true
  76. end
  77. end
  78. end
  79. end
  80. end
  81. return false
  82. end
  83.  
  84. --refueling
  85. function refuel(level)
  86. for i = 1, 16 do
  87. if turtle.getFuelLevel() > level then
  88. break
  89. end
  90. turtle.select(i)
  91. if refuel(0) then
  92. turtle.refuel( math.ceil(turtle.getItemCount()) )
  93. end
  94. end
  95. end
  96.  
  97. function dropItems()
  98. for i = 16,1,-1 do
  99. turtle.select(i)
  100. turtle.dropDown()
  101. end
  102. end
  103.  
  104. -- Network Handling (modem on right)
  105. if rednet.isOpen("right") then
  106. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected to modem!", "bttp")
  107. else
  108. rednet.open("right")
  109. rednet.send(12, "[" .. os.getComputerLabel() .. "] Turning on modem...", "bttp")
  110. rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected successfully!", "bttp")
  111. end
  112.  
  113. -- 2.0 Function: Attempt to fix an un-tilled grass block
  114. function fixBlock()
  115. turtle.digDown()
  116. if hasSeeds(input.crop) then
  117. turtle.placeDown()
  118. end
  119. end
  120.  
  121. -- Helper Function: Finds the table size.
  122. function getSize(tbl)
  123. local count = 0
  124. for _ in pairs(tbl) do count = count + 1 end
  125. return count
  126. end
  127.  
  128. -- 3.0 Function: Have the bot learn crops instead of manually entering them.
  129. function learnCrop(cropBlock)
  130. -- Determine if the block is in any of the mods we listed.
  131. for i = 1, getSize(mods) do
  132. if string.match( string.lower(cropBlock.name) , string.lower(mods[i]) ) then
  133. -- Step 1: Get the current list of crops and make the ID.
  134. local id = getSize(crops) + 1
  135. local cropName = cropBlock.name
  136.  
  137. -- Step 2: Break the block to find the seed.
  138. turtle.digDown()
  139. local unknown = {}
  140. for i = 1, 16 do
  141. local data = turtle.getItemDetail(i)
  142. --Look through each inventory space to find the unknown crop
  143. local isKnown = false
  144. for k,v in pairs(crops) do
  145. if data ~= nil then
  146. if data.name == v.seed then
  147. --Found something not recognized.
  148. isKnown = true
  149. end
  150. end
  151. end
  152. if isKnown == false then
  153. unknown[getSize(unknown) + 1] = data
  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 = 7, 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