Advertisement
buffsovernexus

FarmBotDB v1.0.0

May 22nd, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. --[[
  2.  
  3. FarmBotDB
  4. -----------------------------------
  5. This script is designated to convert an Advanced Computer into a database.
  6.  
  7. ]]--
  8.  
  9. -- List of DEFAULT crops.
  10. local default_crops = {
  11. { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot", label = "Carrots" },
  12. { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds", label = "Wheat" },
  13. { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato", label = "Potatoes" },
  14. { id = 4, mature = 7, name = "magicalcrops:MinicioCrop", seed = "magicalcrops:MinicioSeeds", label = "Minicio" },
  15. { id = 5, mature = 7, name = "magicalcrops:FireCrop", seed = "magicalcrops:FireSeeds", label = "Fire" },
  16. { id = 6, mature = 7, name = "magicalcrops:SheepCrop", seed = "magicalcrops:SheepSeeds", label = "Sheep" },
  17. { id = 7, mature = 7, name = "magicalcrops:NatureCrop", seed = "magicalcrops:NatureSeeds", label = "Nature" },
  18. { id = 8, mature = 7, name = "magicalcrops:FluixCrop", seed = "magicalcrops:FluixSeeds", label = "Fluix" },
  19. { id = 9, mature = 7, name = "magicalcrops:QuartzCrop", seed = "magicalcrops:QuartzSeeds", label = "Quartz" },
  20. { id = 10, mature = 7, name = "magicalcrops:WaterCrop", seed = "magicalcrops:WaterSeeds", label = "Water" },
  21. { id = 11, mature = 7, name = "magicalcrops:NetherCrop", seed = "magicalcrops:NetherSeeds", label = "Nether" },
  22. { id = 12, mature = 7, name = "magicalcrops:EarthCrop", seed = "magicalcrops:EarthSeeds", label = "Earth" },
  23. { id = 13, mature = 7, name = "magicalcrops:DiamondCrop", seed = "magicalcrops:DiamondSeeds", label = "Diamond" }
  24. }
  25.  
  26. -- RESPONSE TO ALL FARMBOTS --
  27. local response = {
  28. -- The hardcoded list of mods for the bot to handle.
  29. mods = { "harvestcraft", "botania", "growthcraft", "extrautilities", "magicalcrops", "witchery" },
  30. -- The default crop list.
  31. crops = {},
  32. -- Potential commands for the bot to run (like update the parent script it has)
  33. cmds = {},
  34. -- The user's input into that specific bot. This allows the bot to handle being restarted.
  35. input = {},
  36. -- This is the information sent to the DB (like ID of bot)
  37. verify = {},
  38. -- This is the starting place of the bot.
  39. gps = {}
  40. }
  41.  
  42. function findMonitor()
  43. local periList = peripheral.getNames()
  44.  
  45. for i = 1, #periList do
  46. if peripheral.getType(periList[i]) == "monitor" then
  47. return periList[i]
  48. end
  49. end
  50. return nil
  51. end
  52. function findModem()
  53. local periList = peripheral.getNames()
  54.  
  55. for i = 1, #periList do
  56. if peripheral.getType(periList[i]) == "modem" then
  57. return periList[i]
  58. end
  59. end
  60. return nil
  61. end
  62.  
  63. local modem = findModem()
  64. local monitor = findMonitor()
  65. local hasMonitor = monitor ~= nil
  66.  
  67. --[[ HELPER FUNCTIONS ]]--
  68. function getSize(tbl)
  69. local count = 0
  70. if tbl == nil then return count end
  71. for _ in pairs(tbl) do count = count + 1 end
  72. return count
  73. end
  74. -- Since I seem to be using this a lot, just make it a function.
  75. function getCropList()
  76. local cropsFile = fs.open("crops", "r")
  77. local cropsString = cropsFile.readAll()
  78. local cropsList = textutils.unserialize(cropsString)
  79. cropsFile.close()
  80. return cropsList
  81. end
  82. -- Lets make this a function since its a lot of code.
  83. function addToCropList(crop)
  84. -- First, check if the crop has a seed
  85. if crop.seed ~= nil then
  86. -- Second, check if the cropdata is > 7.
  87. if crop.metadata > 7 then crop.maturity = crop.metadata end
  88.  
  89. -- Add the crop to the cropList.
  90. local cropList = getCropList()
  91. cropList[getSize(cropList) + 1] = crop
  92.  
  93. -- Rewrite file.
  94. fs.delete("crops")
  95. local cropFile = fs.open("crops", "w")
  96. cropFile.write(textutils.serialize(cropList))
  97. cropFile.close()
  98. return true
  99. else
  100. return false
  101. end
  102. return false
  103. end
  104.  
  105. function getGPS(senderId)
  106. local file = fs.open("/GPS/"..senderId, "w")
  107. local gps = textutils.unserialize(file.readAll())
  108. file.close()
  109. return gps
  110. end
  111.  
  112. function setGPS(senderId, gps)
  113. if fs.exists("/GPS/"..senderId) then fs.delete("/GPS/"..senderId) end
  114. local file = fs.open("/GPS/"..senderId, "w")
  115. file.write(textutils.serialize(gps))
  116. file.close()
  117. end
  118.  
  119. --[[
  120. Listen for any new devices sending messages VIA rednet and convert to table.
  121. ]]--
  122. function handleFunctionality()
  123. -- On message received.
  124. local senderId, message, protocol = rednet.receive()
  125. -- Determine if it was from an Admin Control Terminal or FarmBot
  126. if protocol == "fbtpa" then
  127. local sent = textutils.unserialize(message)
  128. -- [[ ADMIN CONTROL TERMINAL ]] --
  129. if sent.request == "latest" then
  130. -- Update response with proper list.
  131. response.crops = getCropList()
  132.  
  133. -- Send the response to the ID.
  134. rednet.send(senderId, textutils.serialize(response), "fbtpa")
  135. elseif sent.request == "gps_start" then
  136. print("> FarmBot ID: ", sent.data.id)
  137. -- Get the latest GPS location.
  138. local gps = getGPS(sent.data.id)
  139. response.gps = gps
  140. mon.clear()
  141. mon.write(textutils.serialize(gps))
  142. rednet.send(senderId, textutils.serialize(response), "fbtpa")
  143. end
  144. elseif protocol == "fbtp" then
  145. -- [[ FARM BOT ]] --
  146. local data = textutils.unserialize(message)
  147.  
  148. if data.request == "latest" then
  149. -- Update response with proper list.
  150. response.crops = getCropList()
  151.  
  152. -- Send the response to the ID.
  153. rednet.send(senderId, textutils.serialize(response), "fbtp")
  154. elseif data.request == "input_ask" then
  155. -- Send any relevant information about previous user input.
  156. local input = {}
  157. local isSuccess = false
  158.  
  159. if fs.exists("/TASK/" .. senderId) then
  160. --Get the latest input from the file.
  161. local inputFile = fs.open("/TASK/" .. senderId, "r")
  162. response.input = textutils.unserialize(inputFile.readAll())
  163. inputFile.close()
  164. isSuccess = true
  165. end
  166.  
  167. response.verify = { isSuccess = isSuccess }
  168. -- Send data back.
  169. rednet.send(senderId, textutils.serialize(response), "fbtp")
  170.  
  171. elseif data.request == "input_send" then
  172. local input = data.input
  173. local file = fs.open("/TASK/" .. senderId, "w")
  174.  
  175. file.write(textutils.serialize(input))
  176.  
  177. file.close()
  178.  
  179. response.verify = { isSuccess = true }
  180. rednet.send(senderId, textutils.serialize(response), "fbtp")
  181.  
  182. elseif data.request == "learn" then
  183. -- Receiving new crop from BOT.
  184. local crop = data.learned
  185. -- Attempt to add crop to list.
  186. local isSuccess = addToCropList(crop)
  187. response.verify = { isSuccess = isSuccess }
  188. response.crops = getCropList()
  189.  
  190. rednet.send(senderId, textutils.serialize(response), "fbtp")
  191. elseif data.request == "gps_send" then
  192. -- Receive the GPS coordinates and save.
  193. setGPS(senderId, data.gps)
  194.  
  195. response.verify = { isSuccess = true }
  196. -- Send data back.
  197. rednet.send(senderId, textutils.serialize(response), "fbtp")
  198. elseif data.request == "gps_ask" then
  199. -- Grab the GPS coordinates and save to a file.
  200. local gps = getGPS(senderId)
  201. -- Send it in a response.
  202. response.gps = gps
  203. response.verify = { isSuccess = true }
  204.  
  205. rednet.send(senderId, textutils.serialize(response), "fbtp")
  206. end
  207. end
  208.  
  209.  
  210. -- Now reset response.
  211. response.crops = {}
  212. response.cmds = {}
  213. response.input = {}
  214. response.verify = {}
  215. response.gps = {}
  216. end
  217.  
  218. --[[
  219. This is where the script starts... Do not edit this.
  220. ]]--
  221. print(" FarmBotDB")
  222. print("---------------------------------------------------")
  223.  
  224. if fs.exists("crops") == false then
  225. local createFile = fs.open("crops", "w")
  226. local defaultList = textutils.serialize(default_crops)
  227. createFile.write(defaultList)
  228. createFile.close()
  229. print("WARN: Created new 'crops' file with default crops.")
  230. else
  231. print("SUCCESS: Found 'crops' list.")
  232. end
  233.  
  234. -- Check if "TASK" folder exists.
  235. if fs.isDir("/TASK") == false then
  236. print("WARN: Created new 'TASK' folder for bot info.")
  237. fs.makeDir("/TASK")
  238. else
  239. print("SUCCESS: Found 'TASK' folder.")
  240. end
  241.  
  242. -- Check if "GPS" folder exists.
  243. if fs.isDir("/GPS") == false then
  244. print("WARN: Created new 'GPS' folder for bot starting location.")
  245. fs.makeDir("/GPS")
  246. else
  247. print("SUCCESS: Found 'GPS' folder.")
  248. end
  249.  
  250. if modem ~= nil then
  251. print("SUCCESS: Found modem @ ", modem)
  252. if rednet.isOpen(modem) == false then
  253. rednet.open(modem)
  254. end
  255. print("ID - ", os.getComputerID())
  256. print("Host - ", os.getComputerLabel())
  257. print("FarmBot Protocol - fbtp")
  258. print("FarmBotAdmin Protocol - fbtpa")
  259. print("---------------------------------------------------")
  260. print("This terminal will only read new crops, verify added crops of validity, and send FarmBots the updated crop list. " ..
  261. "It is recommended to add the FarmBotAdmin terminal system to this to maximize functionality.")
  262. rednet.host("fbtp", os.getComputerLabel())
  263. rednet.host("fbtpa", os.getComputerLabel())
  264.  
  265. while true do
  266. handleFunctionality()
  267. end
  268. -- The user has decided to shut down.
  269. term.clear()
  270. print("FBDB TERMINATED! Please run CTRL + S to run script again.")
  271. else
  272. print("FATAL ERROR: You need to have a modem connected to your terminal...")
  273. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement