Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- FarmBotDB
- -----------------------------------
- This script is designated to convert an Advanced Computer into a database.
- ]]--
- -- List of DEFAULT crops.
- local default_crops = {
- { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot", label = "Carrots" },
- { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds", label = "Wheat" },
- { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato", label = "Potatoes" },
- { id = 4, mature = 7, name = "magicalcrops:MinicioCrop", seed = "magicalcrops:MinicioSeeds", label = "Minicio" },
- { id = 5, mature = 7, name = "magicalcrops:FireCrop", seed = "magicalcrops:FireSeeds", label = "Fire" },
- { id = 6, mature = 7, name = "magicalcrops:SheepCrop", seed = "magicalcrops:SheepSeeds", label = "Sheep" },
- { id = 7, mature = 7, name = "magicalcrops:NatureCrop", seed = "magicalcrops:NatureSeeds", label = "Nature" },
- { id = 8, mature = 7, name = "magicalcrops:FluixCrop", seed = "magicalcrops:FluixSeeds", label = "Fluix" },
- { id = 9, mature = 7, name = "magicalcrops:QuartzCrop", seed = "magicalcrops:QuartzSeeds", label = "Quartz" },
- { id = 10, mature = 7, name = "magicalcrops:WaterCrop", seed = "magicalcrops:WaterSeeds", label = "Water" },
- { id = 11, mature = 7, name = "magicalcrops:NetherCrop", seed = "magicalcrops:NetherSeeds", label = "Nether" },
- { id = 12, mature = 7, name = "magicalcrops:EarthCrop", seed = "magicalcrops:EarthSeeds", label = "Earth" },
- { id = 13, mature = 7, name = "magicalcrops:DiamondCrop", seed = "magicalcrops:DiamondSeeds", label = "Diamond" }
- }
- -- RESPONSE TO ALL FARMBOTS --
- local response = {
- -- The hardcoded list of mods for the bot to handle.
- mods = { "harvestcraft", "botania", "growthcraft", "extrautilities", "magicalcrops", "witchery" },
- -- The default crop list.
- crops = {},
- -- Potential commands for the bot to run (like update the parent script it has)
- cmds = {},
- -- The user's input into that specific bot. This allows the bot to handle being restarted.
- input = {},
- -- This is the information sent to the DB (like ID of bot)
- verify = {},
- -- This is the starting place of the bot.
- gps = {}
- }
- function findMonitor()
- local periList = peripheral.getNames()
- for i = 1, #periList do
- if peripheral.getType(periList[i]) == "monitor" then
- return periList[i]
- end
- end
- return nil
- end
- function findModem()
- local periList = peripheral.getNames()
- for i = 1, #periList do
- if peripheral.getType(periList[i]) == "modem" then
- return periList[i]
- end
- end
- return nil
- end
- local modem = findModem()
- local monitor = findMonitor()
- local hasMonitor = monitor ~= nil
- --[[ HELPER FUNCTIONS ]]--
- function getSize(tbl)
- local count = 0
- if tbl == nil then return count end
- for _ in pairs(tbl) do count = count + 1 end
- return count
- end
- -- Since I seem to be using this a lot, just make it a function.
- function getCropList()
- local cropsFile = fs.open("crops", "r")
- local cropsString = cropsFile.readAll()
- local cropsList = textutils.unserialize(cropsString)
- cropsFile.close()
- return cropsList
- end
- -- Lets make this a function since its a lot of code.
- function addToCropList(crop)
- -- First, check if the crop has a seed
- if crop.seed ~= nil then
- -- Second, check if the cropdata is > 7.
- if crop.metadata > 7 then crop.maturity = crop.metadata end
- -- Add the crop to the cropList.
- local cropList = getCropList()
- cropList[getSize(cropList) + 1] = crop
- -- Rewrite file.
- fs.delete("crops")
- local cropFile = fs.open("crops", "w")
- cropFile.write(textutils.serialize(cropList))
- cropFile.close()
- return true
- else
- return false
- end
- return false
- end
- function getGPS(senderId)
- local file = fs.open("/GPS/"..senderId, "w")
- local gps = textutils.unserialize(file.readAll())
- file.close()
- return gps
- end
- function setGPS(senderId, gps)
- if fs.exists("/GPS/"..senderId) then fs.delete("/GPS/"..senderId) end
- local file = fs.open("/GPS/"..senderId, "w")
- file.write(textutils.serialize(gps))
- file.close()
- end
- --[[
- Listen for any new devices sending messages VIA rednet and convert to table.
- ]]--
- function handleFunctionality()
- -- On message received.
- local senderId, message, protocol = rednet.receive()
- -- Determine if it was from an Admin Control Terminal or FarmBot
- if protocol == "fbtpa" then
- local sent = textutils.unserialize(message)
- -- [[ ADMIN CONTROL TERMINAL ]] --
- if sent.request == "latest" then
- -- Update response with proper list.
- response.crops = getCropList()
- -- Send the response to the ID.
- rednet.send(senderId, textutils.serialize(response), "fbtpa")
- elseif sent.request == "gps_start" then
- print("> FarmBot ID: ", sent.data.id)
- -- Get the latest GPS location.
- local gps = getGPS(sent.data.id)
- response.gps = gps
- mon.clear()
- mon.write(textutils.serialize(gps))
- rednet.send(senderId, textutils.serialize(response), "fbtpa")
- end
- elseif protocol == "fbtp" then
- -- [[ FARM BOT ]] --
- local data = textutils.unserialize(message)
- if data.request == "latest" then
- -- Update response with proper list.
- response.crops = getCropList()
- -- Send the response to the ID.
- rednet.send(senderId, textutils.serialize(response), "fbtp")
- elseif data.request == "input_ask" then
- -- Send any relevant information about previous user input.
- local input = {}
- local isSuccess = false
- if fs.exists("/TASK/" .. senderId) then
- --Get the latest input from the file.
- local inputFile = fs.open("/TASK/" .. senderId, "r")
- response.input = textutils.unserialize(inputFile.readAll())
- inputFile.close()
- isSuccess = true
- end
- response.verify = { isSuccess = isSuccess }
- -- Send data back.
- rednet.send(senderId, textutils.serialize(response), "fbtp")
- elseif data.request == "input_send" then
- local input = data.input
- local file = fs.open("/TASK/" .. senderId, "w")
- file.write(textutils.serialize(input))
- file.close()
- response.verify = { isSuccess = true }
- rednet.send(senderId, textutils.serialize(response), "fbtp")
- elseif data.request == "learn" then
- -- Receiving new crop from BOT.
- local crop = data.learned
- -- Attempt to add crop to list.
- local isSuccess = addToCropList(crop)
- response.verify = { isSuccess = isSuccess }
- response.crops = getCropList()
- rednet.send(senderId, textutils.serialize(response), "fbtp")
- elseif data.request == "gps_send" then
- -- Receive the GPS coordinates and save.
- setGPS(senderId, data.gps)
- response.verify = { isSuccess = true }
- -- Send data back.
- rednet.send(senderId, textutils.serialize(response), "fbtp")
- elseif data.request == "gps_ask" then
- -- Grab the GPS coordinates and save to a file.
- local gps = getGPS(senderId)
- -- Send it in a response.
- response.gps = gps
- response.verify = { isSuccess = true }
- rednet.send(senderId, textutils.serialize(response), "fbtp")
- end
- end
- -- Now reset response.
- response.crops = {}
- response.cmds = {}
- response.input = {}
- response.verify = {}
- response.gps = {}
- end
- --[[
- This is where the script starts... Do not edit this.
- ]]--
- print(" FarmBotDB")
- print("---------------------------------------------------")
- if fs.exists("crops") == false then
- local createFile = fs.open("crops", "w")
- local defaultList = textutils.serialize(default_crops)
- createFile.write(defaultList)
- createFile.close()
- print("WARN: Created new 'crops' file with default crops.")
- else
- print("SUCCESS: Found 'crops' list.")
- end
- -- Check if "TASK" folder exists.
- if fs.isDir("/TASK") == false then
- print("WARN: Created new 'TASK' folder for bot info.")
- fs.makeDir("/TASK")
- else
- print("SUCCESS: Found 'TASK' folder.")
- end
- -- Check if "GPS" folder exists.
- if fs.isDir("/GPS") == false then
- print("WARN: Created new 'GPS' folder for bot starting location.")
- fs.makeDir("/GPS")
- else
- print("SUCCESS: Found 'GPS' folder.")
- end
- if modem ~= nil then
- print("SUCCESS: Found modem @ ", modem)
- if rednet.isOpen(modem) == false then
- rednet.open(modem)
- end
- print("ID - ", os.getComputerID())
- print("Host - ", os.getComputerLabel())
- print("FarmBot Protocol - fbtp")
- print("FarmBotAdmin Protocol - fbtpa")
- print("---------------------------------------------------")
- print("This terminal will only read new crops, verify added crops of validity, and send FarmBots the updated crop list. " ..
- "It is recommended to add the FarmBotAdmin terminal system to this to maximize functionality.")
- rednet.host("fbtp", os.getComputerLabel())
- rednet.host("fbtpa", os.getComputerLabel())
- while true do
- handleFunctionality()
- end
- -- The user has decided to shut down.
- term.clear()
- print("FBDB TERMINATED! Please run CTRL + S to run script again.")
- else
- print("FATAL ERROR: You need to have a modem connected to your terminal...")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement