Advertisement
buffsovernexus

Untitled

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