Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Data API v0.0
- -- A handy list of variables which can be returned as table/string or saved to file
- -- .get() returns all data toggled to true or takes a string parameter for a single type of data
- -- data types are listed below under data.settings.save, using "settings" as a type returns the current settings
- -- .toggle() lets you change which settings are saved to file and which are gathered by empty .info()
- -- Declare variable for data storage
- local data = {["cache"] = {}, ["settings"] = {}}
- -- Default pathing for data storage
- data.settings.path = {
- ["root"] = "//data/",
- }
- data.settings.path.settings = data.settings.path.root .. "settings.txt"
- data.settings.path.data = data.settings.path.root .. "system.txt"
- -- Default settings to determine which data is saved to file
- data.settings.save = {
- ["basic"] = true,
- ["peripheral"] = true,
- ["inventory"] = true,
- ["fuel"] = true,
- ["position"] = true,
- ["bearing"] = true,
- }
- -- Default settings to determine external feature usage
- data.settings.use = {
- ["gps"] = true,
- ["settingsFile"] = false,
- ["dataFile"] = false,
- }
- -- Function to load data from file if exists
- local load = function()
- if fs.exists(data.settings.path.settings) then
- local file = fs.open(data.settings.path.settings,"r")
- data.settings = textutils.unserialise(file.readAll())
- file.close()
- end
- end
- -- Function to save data to file
- local save = function()
- if data.settings.use.settingsFile then
- local file = fs.open(data.settings.path.settings,"w")
- file.write(textutils.serialise(data.settings))
- file.close()
- else
- if fs.exists(data.settings.path.settings) then
- fs.delete(data.settings.path.settings)
- end
- end
- if data.settings.use.dataFile then
- local hasData = false
- for k,v in pairs(data.cache) do hasData = true end
- if hasData then
- local file = fs.open(data.settings.path.data,"w")
- file.write(textutils.serialise(data.cache))
- file.close()
- elseif fs.exists(data.settings.path.data) then
- fs.delete(data.settings.path.data)
- end
- else
- if fs.exists(data.settings.path.data) then
- fs.delete(data.settings.path.data)
- end
- end
- if fs.exists(data.settings.path.root) and #fs.list(data.settings.path.root) == 0 then
- fs.delete(data.settings.path.root)
- end
- end
- -- Function to get basic system data
- data.basic = function()
- data.cache.basic = {}
- -- Check device ID
- data.cache.basic.id = os.getComputerID()
- -- Check device label if device has one
- if os.getComputerLabel() then data.cache.basic.name = os.getComputerLabel() end
- -- Check if advanced/basic
- if term.isColour() then data.cache.basic.grade = "advanced"
- else data.cache.basic.grade = "basic" end
- -- Check device type
- if peripheral.getType("back") == "neuralInterface" then data.cache.basic.type = "neural"
- elseif turtle then data.cache.basic.type = "turtle"
- elseif pocket then data.cache.basic.type = "pocket"
- else data.cache.basic.type = "computer" end
- -- Return data, save if set to
- local output = data.cache.basic
- if data.settings.save.basic then
- save()
- else
- data.cache.basic = nil
- end
- return output
- end
- -- Function to get system peripherals
- data.peripheral = function()
- data.cache.peripheral = {}
- -- Check peripheral
- side = peripheral.getNames()
- for i = 1,#side do
- data.cache.peripheral[side[i]] = peripheral.getType(side[i])
- end
- -- Check neural interface modules
- if peripheral.getType("back") == "neuralInterface" then
- if peripheral.wrap("back").listModules() then
- data.cache.peripheral.modules = peripheral.wrap("back").listModules()
- end
- end
- -- Return data, save if set to
- local output = data.cache.peripheral
- if data.settings.save.peripheral then
- save()
- else
- data.cache.peripheral = nil
- end
- return output
- end
- -- Function to get turtle inventory or neural interface equipment
- data.inventory = function()
- data.cache.inventory = {}
- -- Check turtle inventory
- if turtle then
- for i = 1,16 do
- if turtle.getItemDetail(i) then
- data.cache.inventory[i] = {turtle.getItemDetail(i).count, turtle.getItemDetail(i).name}
- end
- end
- -- Check neural interface equipment
- elseif peripheral.getType("back") == "neuralInterface" then
- if peripheral.wrap("back").hasModule("plethora:introspection") then
- for itemSlot,itemData in pairs(peripheral.wrap("back").getEquipment().list()) do
- data.cache.inventory[itemSlot] = itemData.name
- end
- end
- else
- data.cache.inventory = nil
- end
- -- Return data, save if set to
- local output = data.cache.inventory
- if data.settings.save.inventory then
- save()
- else
- data.cache.inventory = nil
- end
- return output
- end
- -- Function to get turtle fuel count
- data.fuel = function()
- if turtle then
- data.cache.fuel = turtle.getFuelLevel()
- else
- data.cache.fuel = nil
- end
- -- Return data, save if set to
- local output = data.cache.fuel
- if data.settings.save.fuel then
- save()
- else
- data.cache.fuel = nil
- end
- return output
- end
- -- Function to get system co-ordinates
- data.position = function()
- if data.settings.use.gps and gps.locate() then
- -- Check position with GPS
- data.cache.position = {}
- data.cache.position["x"],data.cache.position["y"],data.cache.position["z"] = gps.locate()
- if not data.cache.position["x"] then data.cache.position = "lost" end
- else
- data.cache.position = nil
- end
- -- Return data, save if set to
- local output = data.cache.position
- if data.settings.save.position then
- save()
- else
- data.cache.position = nil
- end
- return output
- end
- -- Function to find which way turtle is facing or get neural interface yaw
- data.bearing = function()
- if data.settings.use.gps and gps.locate() then
- data.cache.bearing = nil
- -- Check where turtle is facing
- if turtle and turtle.getFuelLevel() > 1 then
- local oldX,oldY,oldZ = gps.locate()
- local turns = 0
- for attempt = 1,4 do
- if not data.cache.bearing and turtle.forward() then
- local newX,newY,newZ = gps.locate()
- if oldX and newX then
- newX = newX - oldX
- newZ = newZ - oldZ
- if newX ~= 0 then
- if newX > 0 then
- data.cache.bearing = "east"
- else
- data.cache.bearing = "west"
- end
- elseif newZ ~= 0 then
- if newZ > 0 then
- data.cache.bearing = "south"
- else
- data.cache.bearing = "north"
- end
- end
- end
- if not turtle.back() then
- data.position()
- end
- end
- if not data.cache.bearing then
- turtle.turnLeft()
- turns = turns + 1
- else
- if turns == 3 then
- turtle.turnLeft()
- if data.cache.bearing == "north" then
- data.cache.bearing = "west"
- elseif data.cache.bearing == "east" then
- data.cache.bearing = "north"
- elseif data.cache.bearing == "south" then
- data.cache.bearing = "east"
- elseif data.cache.bearing == "west" then
- data.cache.bearing = "south"
- end
- else
- for t = 1,turns do
- turtle.turnRight()
- if data.cache.bearing == "north" then
- data.cache.bearing = "east"
- elseif data.cache.bearing == "east" then
- data.cache.bearing = "south"
- elseif data.cache.bearing == "south" then
- data.cache.bearing = "west"
- elseif data.cache.bearing == "west" then
- data.cache.bearing = "north"
- end
- end
- end
- break
- end
- end
- -- Check neural interface yaw
- elseif peripheral.getType("back") == "neuralInterface" then
- if peripheral.wrap("back").hasModule("plethora:sensor") then
- if peripheral.wrap("back").hasModule("plethora:introspection") then
- data.cache.bearing = peripheral.wrap("back").getMetaOwner().yaw
- end
- end
- end
- end
- -- Return data, save if set to
- local output = data.cache.bearing
- if data.settings.save.bearing then
- save()
- else
- data.cache.position = nil
- end
- return output
- end
- -- Function to get all system data in one
- local getData = function(type)
- if not type then
- if data.settings.save.basic then
- data.basic()
- end
- if data.settings.save.peripheral then
- data.peripheral()
- end
- if data.settings.save.inventory then
- data.inventory()
- end
- if data.settings.save.fuel then
- data.fuel()
- end
- if data.settings.save.position then
- data.position()
- end
- if data.settings.save.bearing then
- data.bearing()
- end
- return data.cache
- elseif type == "settings" then
- return data.settings
- elseif type == "basic" or type == "basics" then
- return data.basic()
- elseif type == "peripheral" or type == "peripherals" or type == "periph" then
- return data.peripheral()
- elseif type == "inventory" or type == "inv" then
- return data.inventory()
- elseif type == "fuel" then
- return data.fuel()
- elseif type == "position" or type == "pos" then
- return data.position()
- elseif type == "bearing" or type == "facing" then
- return data.bearing()
- else
- return "Usage: info([type])"
- end
- end
- -- Function to switch settings between true and false
- local toggleSettings = function(setting,state)
- local output = nil
- if state ~= true and state ~= false then
- local state = nil
- end
- if setting == "all" then
- if state ~= nil then
- for k,v in pairs(data.settings.save) do
- data.settings.save[k] = state
- end
- output = "All "..tostring(state)
- else
- output = "New state required"
- end
- elseif data.settings.use[setting] ~= nil then
- if state ~= nil then
- data.settings.use[setting] = state
- else
- data.settings.use[setting] = not data.settings.use[setting]
- end
- output = data.settings.use[setting]
- elseif data.settings.save[setting] ~= nil then
- if state ~= nil then
- data.settings.save[setting] = state
- else
- data.settings.save[setting] = not data.settings.save[setting]
- end
- output = data.settings.save[setting]
- else
- output = "Usage: toggle(<setting/all>[, true/false])"
- end
- getData()
- return output
- end
- -- Load system if exists or create if not
- if not fs.exists(data.settings.path.settings) then
- getData()
- else
- load()
- getData()
- end
- -- Allow function access
- return {
- get = getData,
- toggle = toggleSettings,
- }
Advertisement
Add Comment
Please, Sign In to add comment