Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local version = "Hivemind v0.0"
- local path = "//hivemind/"
- -- Declare data variable
- local data = {}
- local dataFile = path.."system.txt"
- -- Populate data variable if file exists
- if fs.exists(dataFile) then
- file = fs.open(dataFile,"r")
- data = textutils.unserialise(file.readAll())
- file.close()
- else data = {["system"] = {}, ["network"] = {}} end
- -- Function to save data to file
- local save = function()
- file = fs.open(dataFile,"w")
- file.write(textutils.serialise(data))
- file.close()
- end
- -- Functions for system management
- local system = {}
- system.peripherals = function()
- -- Check peripherals
- data.system.peripherals = {}
- side = peripheral.getNames()
- for i = 1,#side do
- data.system.peripherals[side[i]] = peripheral.getType(side[i])
- end
- -- Check neural interface modules
- if peripheral.getType("back") == "neuralInterface" then
- if peripheral.wrap("back").listModules() then
- data.system.peripherals.modules = peripheral.wrap("back").listModules()
- end
- end
- save()
- return data.system.peripherals end
- system.position = function()
- -- Check position with GPS
- data.system.pos = {}
- data.system.pos["x"],data.system.pos["y"],data.system.pos["z"] = gps.locate()
- if not data.system.pos["x"] then data.system.pos = "Lost" end
- save()
- end
- system.bearing = function()
- system.position()
- -- Check where turtle is facing
- if turtle and data.system.pos then
- if turtle.getFuelLevel() > 1 and data.system.pos["x"] then
- turns = 0
- for attempt = 1,4 do
- if not data.system.pos["bearing"] and turtle.forward() then
- local tmpX,tmpY,tmpZ = gps.locate()
- if tmpX then
- tmpX = tmpX - data.system.pos["x"]
- tmpZ = tmpZ - data.system.pos["z"]
- if tmpX ~= 0 then
- if tmpX > 0 then
- data.system.pos["bearing"] = "east"
- else
- data.system.pos["bearing"] = "west"
- end
- elseif tmpZ ~= 0 then
- if tmpZ > 0 then
- data.system.pos["bearing"] = "south"
- else
- data.system.pos["bearing"] = "north"
- end
- end
- end
- if not turtle.back() then
- system.position()
- end
- end
- if not data.system.pos["bearing"] then
- turtle.turnLeft()
- turns = turns + 1
- else
- if turns == 3 then
- turtle.turnLeft()
- else
- for t = 1,turns do
- turtle.turnRight()
- end
- end
- break
- end
- end
- end
- -- Check where neural interface is facing
- elseif peripheral.getType("back") == "neuralInterface" then
- if peripheral.wrap("back").hasModule("plethora:sensor") and peripheral.wrap("back").hasModule("plethora:introspection") then
- data.system.pos["bearing"] = peripheral.wrap("back").getMetaOwner().yaw
- end
- end
- save()
- return data.system.pos["bearing"] end
- system.inventory = function()
- -- Check turtle inventory
- if turtle then
- data.system.inventory = {}
- for i = 1,16 do
- if turtle.getItemDetail(i) then
- data.system.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
- data.system.inventory = {}
- for itemSlot,itemData in pairs(peripheral.wrap("back").getEquipment().list()) do
- data.system.inventory[itemSlot] = itemData.name
- end
- end
- end
- save()
- return data.system.inventory end
- system.fuel = function()
- -- Check turtle fuel level
- if turtle then
- data.system.fuel = turtle.getFuelLevel()
- end
- save()
- return data.system.fuel end
- local network = {}
- network.members = function(id)
- if data.network.type == "server" then
- if fs.exists(path.."network/") then
- data.network.members = fs.list(path.."network/")
- for i = 1,#data.network.members do
- data.network.members[i] = tonumber(data.network.members[i])
- end
- else data.network.members = {} end
- if id then
- id = tonumber(id)
- exists = false
- for i = 1,#data.network.members do
- if data.network.members[i] == id then
- exists = true
- break
- end
- end
- return exists
- else return data.network.members end
- end
- save()
- end
- network.create = function()
- data.network.type = "server"
- save()
- end
- network.delete = function()
- data.network = {}
- save()
- end
- network.join = function(id)
- data.network.type = "client"
- data.network.server = id
- save()
- modem.transmit(17007,7,os.getComputerID())
- end
- network.leave = function()
- data.network = {}
- save()
- end
- local checkEvent = function()
- evt = nil
- evt = {os.pullEvent()}
- if evt[1] == "modem_message" then
- if evt[4] == 7 then
- if not network.members(tonumber(evt[5])) then
- table.insert(data.network.members,tonumber(evt[5]))
- file = fs.open(path.."network/"..evt[5],"w")
- file.write("test")
- file.close()
- end
- else
- print(evt[1]..": "..evt[4].." --> "..evt[3].." ["..math.floor(evt[6]).."]\n"..evt[5])
- end
- sleep(5)
- elseif evt[1] == "peripheral" or evt[1] == "peripheral_detach" then
- system.peripheral()
- elseif evt[1] == "key" and evt[2] == keys.delete then
- data.network = {}
- fs.delete(path)
- running = nil
- end
- end
- -- Run program
- while true do
- running = true
- if not peripheral.find("modem") then
- shell.run("clear")
- print("Error: No modem attached")
- while not peripheral.find("modem") do
- os.pullEvent("peripheral")
- end
- else
- modem = peripheral.find("modem")
- modem.closeAll()
- for i = 17001,17071 do
- modem.open(i)
- end
- while running and modem.isOpen(17007) do
- shell.run("clear")
- if not data.network.type then
- write("[Create] or [Join] Hive? ")
- input = string.lower(read())
- if input == "create" then
- network.create()
- elseif input == "join" then
- write("Server ID: ")
- input = tonumber(read())
- if input then
- network.join(input)
- end
- end
- elseif data.network.type == "server" then
- network.members()
- print(textutils.serialise(data.network.members))
- checkEvent()
- elseif data.network.type == "client" then
- print("Server: "..textutils.serialise(data.network.server))
- checkEvent()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment