Advertisement
buffsovernexus

Untitled

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