Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle RC Voice Command [OP Edition] - Server/Controller
- -- Author: theonlycozzy
- -- Nov 25 2013
- local BUILD = 7
- local GUI, meta, turtles, Args = {nY=24},{},{},{...}
- local TGB, MOD = nil
- local running = true
- local sColors = {offline=0x575757,-- Not connected to Server
- inactive=0x06799F, -- Inactive on Controller
- active=0x00BB3F, -- Active on Controller
- busy=0xFF8300, -- Currently Working
- eFatal=0xA61A00, -- Turtle Program Crashed
- eLost=0xC9C9C9, -- No GPS Location
- eFuel=0xDBD800, -- Low Fuel
- eInv=0xFF0070,} -- Full Inventory
- ------------------------------------------------------------
- -- Functions
- local logBug = function(TEXT,ERROR)
- local timeStamp = "["..os.day().."] "..os.time().."| "
- if meta.deBug then
- local f = fs.open('debug.txt','a')
- f.writeLine(timeStamp..TEXT)
- f.close()
- end
- if ERROR then error(TEXT,0) end
- end
- local openProperties = function()
- local f = fs.open("server.properties","r")
- meta = textutils.unserialize(f.readLine())
- turtles = textutils.unserialize(f.readLine())
- f.close()
- end
- local saveProperties = function()
- local f = fs.open("server.properties","w")
- f.writeLine(textutils.serialize(meta))
- f.writeLine(textutils.serialize(turtles))
- f.close()
- end
- local splitString = function(STRING)
- local t = {}
- for token in string.gmatch(STRING,"[^%s]+") do
- t[#t+1] = token
- end
- return t
- end
- local sendProtocol = function(PROTOCOL,TYPE,ID,DATA)
- if type(DATA) ~= "table" then return false end
- local packet = {ptl=PROTOCOL,cType=TYPE,data=DATA}
- MOD.transmit(ID,meta.hostChannel,textutils.serialize(packet))
- logBug("Server Sent: "..textutils.serialize(packet))
- logBug("on Channel: "..ID)
- end
- local drawTurtletoHUD = function(ID,tDATA)
- if type(tDATA) == "table" then
- local Position, STATUS
- if tDATA.xPos and tDATA.zPos and tDATA.ypos then
- Position = tDATA.xPos.." "..tDATA.zPos.." ["..tDATA.yPos.."]"
- else
- Position = "Unknown"
- end
- if tDATA.status then
- STATUS = sColors[tDATA.status]
- else
- STATUS = 0x343434
- end
- -- DRAW A NEW BOX
- GUI[ID] = {}
- GUI[ID]['BG'] = TGB.addBox(10,GUI.nY,100,10,STATUS,0.75)
- GUI[ID]['ID'] = TGB.addText(12,GUI.nY+3,tostring(ID),0x000000)
- GUI[ID]['FUEL'] = TGB.addText(35,GUI.nY+3,tostring(tDATA.fuelLevel),0x000000)
- GUI[ID]['GPSL'] = TGB.addText(60,GUI.nY+3,Position,0x000000)
- -- SET SCALE AND Z POSITIONING
- GUI[ID]['BG'].setZIndex(2)
- GUI[ID]['ID'].setZIndex(3) GUI[ID]['ID'].setScale(0.5)
- GUI[ID]['FUEL'].setZIndex(3) GUI[ID]['FUEL'].setScale(0.5)
- GUI[ID]['GPSL'].setZIndex(4) GUI[ID]['GPSL'].setScale(0.5)
- GUI[ID]['height'] = GUI.nY
- GUI[ID]['GP'] = {}
- -- Check for Group
- if turtles[ID] then
- if turtles[ID]["group"] then
- GUI[ID]["GP"] = TGB.addText(2,GUI[ID]["height"]+3,"["..turtles[ID].group.."]",0x000000)
- GUI[ID]["GP"].setZIndex(4) GUI[ID]["GP"].setScale(0.5)
- end
- end
- -- Bring Background down with added Box
- GUI["BG"].setHeight(GUI["BG"].getHeight()+11)
- GUI.nY = GUI.nY + 11
- turtles[ID] = tDATA
- turtles[ID].status = "inactive"
- saveProperties()
- else return false end
- end
- local remTurtlefromHUD = function(ID)
- if GUI[ID] then
- GUI[ID]['BG'].delete()
- GUI[ID]['ID'].delete()
- GUI[ID]['FUEL'].delete()
- GUI[ID]['GPSL'].delete()
- GUI[ID] = nil
- turtles[ID] = nil
- saveProperties()
- end
- end
- local updateTurtleHUD = function(ID,STATUS)
- if turtles[ID] then
- local sUpdate = sColors[STATUS]
- if GUI[ID] then
- GUI[ID]['BG'].setColor(sUpdate)
- return true
- end
- end
- return false
- end
- ------------------------------------------------------------
- -- Init check
- term.clear() term.setCursorPos(1,1)
- for key,side in pairs(rs.getSides()) do -- Detect Peripherals
- if peripheral.getType(side) == "terminal_glasses_bridge" then
- TGB = peripheral.wrap(side)
- elseif peripheral.getType(side) == "modem" then
- MOD = peripheral.wrap(side)
- end
- end
- if not TGB and not MOD then -- Error check Peripherals
- logBug("Requires Open Peripheral & Wireless Modem to run",true)
- elseif not TGB then
- logBug("Requires Open Peripheral 0.1.9+ to run",true)
- elseif not MOD then
- logBug("Requires a Wireless Modem to run",true)
- end
- if Args[1] == "-u" or Args[1] == "-uninstall" then -- Delete all files
- if fs.exists('debug.txt') then
- fs.delete('debug.txt')
- end
- if fs.exists('server.properties') then
- fs.delete('server.properties')
- end
- return false
- end
- if fs.exists("server.properties") then
- openProperties()
- logBug("Server Properties file Loaded")
- else
- meta = {hostChannel=nil,machineID=os.getComputerID(),deBug=false,warningFuel=100,warningInv=15,cUpdate=7,startups=1}
- meta.hostChannel = math.ceil(math.random(128,65535))
- saveProperties()
- logBug("Server Properties file Created/Saved")
- end
- MOD.open(meta.hostChannel)
- print("Hosting on: "..meta.hostChannel)
- ------------------------------------------------------------
- -- Init GUI drawings
- TGB.clear()
- GUI["BG"] = TGB.addBox(0,0,120,30,0x808080,0.45)
- GUI["HR"] = TGB.addText(5,2,"RC Turtle Voice Command",0xFF2800)
- GUI["CH"] = TGB.addText(12,11,"Host Channel: "..meta.hostChannel,0xFF8300)
- GUI["TL"] = TGB.addText(12,19,"-ID- -Fuel- -Location-",0x3AAACF)
- GUI["BG"].setZIndex(0)
- GUI["HR"].setZIndex(1) GUI["HR"].setScale(0.9)
- GUI["CH"].setZIndex(1) GUI["CH"].setScale(0.65)
- GUI["TL"].setZIndex(1) GUI["TL"].setScale(0.5)
- -- END Init HUD drawings
- if turtles then
- logBug("Re-establishing connections via Server-side")
- for ID,DATA in pairs(turtles) do
- turtles[ID]["status"] = "offline"
- sendProtocol("connect","renew",ID,{"ping"})
- os.startTimer(1.5)
- local responded = false
- while not responded do
- local EV = { os.pullEvent() }
- if EV[1] == "timer" then
- responded = true
- logBug("ERROR: Turtle timed out "..ID.." set to Offline")
- elseif EV[1] == "modem_message" then
- responded = true
- logBug("Server Received [REDNET]: "..textutils.serialize(EV))
- if EV[4] == tonumber(ID) then
- turtles[ID]["status"] = "inactive"
- logBug(ID.." Confirmed and set to In-Active")
- end
- end
- end
- drawTurtletoHUD(ID,DATA)
- end
- logBug("Re-establishing connections Server-side Completed")
- end
- if Args[1] == "-d" then
- meta.deBug=true
- end
- ------------------------------------------------------------
- -- Start Program
- logBug(string.rep("=",30).." SERVER BUILD ["..BUILD.."] INITIATED "..string.rep("=",30))
- while running do
- local EV = {os.pullEvent()}
- if EV[1] == "chat_command" then
- logBug("Server Received [Commands]: "..EV[2])
- local cmds = splitString(string.lower(EV[2]))
- if cmds[1] == "walk" or cmds[1] == "goto" or cmds[1] == "turn" then
- local TYPE = cmds[1]
- table.remove(cmds,1)
- for ID, data in pairs(turtles) do
- if data["status"] == "active" then
- sendProtocol("movement",TYPE,ID,cmds)
- end
- end
- elseif cmds[1] == "group" then
- local TYPE = cmds[2]
- if TYPE == "add" then
- local ID = tonumber(cmds[3])
- local groupNum = tonumber(cmds[4])
- turtles[ID]["group"] = groupNum
- GUI[ID]["GP"] = TGB.addText(2,GUI[ID]["height"]+3,"["..groupNum.."]",0x000000)
- GUI[ID]["GP"].setZIndex(4) GUI[ID]["GP"].setScale(0.5)
- logBug("Successfully Added: "..ID.." to Group "..groupNum)
- elseif TYPE == "delt" then -- Delete specific turtle from group
- local ID = tonumber(cmds[3])
- local gN
- if turtles[ID].group then
- gN = turtles[ID].group
- turtles[ID].group = nil
- if GUI[ID]["GP"] then GUI[ID]["GP"].delete() end
- logBug("Successfully removed "..ID.." from group "..gN)
- else
- logBug("ERROR: Turtle not in Group")
- end
- elseif TYPE == "delg" then -- Delete entire Group
- elseif TYPE == "sel" then
- for ID, data in pairs(turtles) do
- if data.group == tonumber(cmds[3]) then
- turtles[ID].status = "active"
- else
- turtles[ID].status = "inactive"
- end
- end
- elseif TYPE == "off" then
- for ID,DATA in pairs(turtles) do
- if DATA.status == "active" then
- DATA.status = "inactive"
- end
- end
- end
- elseif cmds[1] == "toggle" then
- local ID = tonumber(cmds[2])
- if turtles[ID].status == "inactive" then
- turtles[ID].status = "active"
- logBug("Activated "..tostring(ID))
- print("Activated "..tostring(ID))
- else
- turtles[ID].status = "inactive"
- logBug("De-activated "..tostring(ID))
- print("De-activated "..tostring(ID))
- end
- elseif cmds[1] == "kill" or cmds[1] == "die" then
- logBug("Kill Command Initiated..")
- running = false
- end
- -- HANDLE SERVER RESPONSES
- elseif EV[1] == "modem_message" then
- local ID = tonumber(EV[4])
- local pakg = textutils.unserialize(EV[5])
- logBug("Server Received [REDNET]: "..EV[5])
- if type(pakg) == "table" then
- if pakg.ptl == "connect" then
- if pakg.cType == "new" then
- drawTurtletoHUD(ID,pakg.data)
- end
- end
- end
- end
- saveProperties()
- for ID,DATA in pairs(turtles) do
- updateTurtleHUD(ID,DATA.status)
- end
- end
- -------- TERMINATION CODE --------
- for ID, data in pairs(turtles) do
- data.status = "offline"
- sendProtocol("terminate","kill",ID,data)
- end
- MOD.closeAll()
- TGB.clear()
- saveProperties()
Advertisement
Add Comment
Please, Sign In to add comment