cozzimoto

TRCVC - Server/Controller B:7

Nov 11th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.75 KB | None | 0 0
  1. -- Turtle RC Voice Command [OP Edition] - Server/Controller
  2. -- Author: theonlycozzy
  3. -- Nov 25 2013
  4.  
  5. local BUILD = 7
  6. local GUI, meta, turtles, Args = {nY=24},{},{},{...}
  7. local TGB, MOD = nil
  8. local running = true
  9. local sColors = {offline=0x575757,-- Not connected to Server
  10.                  inactive=0x06799F,  -- Inactive on Controller
  11.                  active=0x00BB3F,    -- Active on Controller
  12.                  busy=0xFF8300,   -- Currently Working
  13.                  eFatal=0xA61A00, -- Turtle Program Crashed
  14.                  eLost=0xC9C9C9,  -- No GPS Location
  15.                  eFuel=0xDBD800,  -- Low Fuel
  16.                  eInv=0xFF0070,}  -- Full Inventory
  17. ------------------------------------------------------------
  18. -- Functions
  19. local logBug = function(TEXT,ERROR)
  20.   local timeStamp = "["..os.day().."] "..os.time().."| "
  21.   if meta.deBug then
  22.     local f = fs.open('debug.txt','a')
  23.     f.writeLine(timeStamp..TEXT)
  24.     f.close()
  25.   end
  26.   if ERROR then error(TEXT,0) end
  27. end
  28. local openProperties = function()
  29.   local f = fs.open("server.properties","r")
  30.   meta = textutils.unserialize(f.readLine())
  31.   turtles = textutils.unserialize(f.readLine())
  32.   f.close()
  33. end
  34. local saveProperties = function()
  35.   local f = fs.open("server.properties","w")
  36.   f.writeLine(textutils.serialize(meta))
  37.   f.writeLine(textutils.serialize(turtles))
  38.   f.close()
  39. end
  40. local splitString = function(STRING)
  41.   local t = {}
  42.   for token in string.gmatch(STRING,"[^%s]+") do
  43.     t[#t+1] = token
  44.   end
  45.   return t
  46. end
  47. local sendProtocol = function(PROTOCOL,TYPE,ID,DATA)
  48.   if type(DATA) ~= "table" then return false end
  49.   local packet = {ptl=PROTOCOL,cType=TYPE,data=DATA}
  50.   MOD.transmit(ID,meta.hostChannel,textutils.serialize(packet))
  51.   logBug("Server Sent: "..textutils.serialize(packet))
  52.   logBug("on Channel: "..ID)
  53. end
  54. local drawTurtletoHUD = function(ID,tDATA)
  55.   if type(tDATA) == "table" then
  56.     local Position, STATUS
  57.     if tDATA.xPos and tDATA.zPos and tDATA.ypos then
  58.       Position = tDATA.xPos.." "..tDATA.zPos.." ["..tDATA.yPos.."]"
  59.     else
  60.       Position = "Unknown"
  61.     end
  62.     if tDATA.status then
  63.       STATUS = sColors[tDATA.status]
  64.     else
  65.       STATUS = 0x343434
  66.     end
  67.     -- DRAW A NEW BOX
  68.     GUI[ID] = {}
  69.     GUI[ID]['BG'] = TGB.addBox(10,GUI.nY,100,10,STATUS,0.75)
  70.     GUI[ID]['ID'] = TGB.addText(12,GUI.nY+3,tostring(ID),0x000000)
  71.     GUI[ID]['FUEL'] = TGB.addText(35,GUI.nY+3,tostring(tDATA.fuelLevel),0x000000)
  72.     GUI[ID]['GPSL'] = TGB.addText(60,GUI.nY+3,Position,0x000000)
  73.     -- SET SCALE AND Z POSITIONING
  74.     GUI[ID]['BG'].setZIndex(2)
  75.     GUI[ID]['ID'].setZIndex(3) GUI[ID]['ID'].setScale(0.5)
  76.     GUI[ID]['FUEL'].setZIndex(3) GUI[ID]['FUEL'].setScale(0.5)
  77.     GUI[ID]['GPSL'].setZIndex(4) GUI[ID]['GPSL'].setScale(0.5)
  78.     GUI[ID]['height'] = GUI.nY
  79.     GUI[ID]['GP'] = {}
  80.     -- Check for Group
  81.     if turtles[ID] then
  82.       if turtles[ID]["group"] then
  83.         GUI[ID]["GP"] = TGB.addText(2,GUI[ID]["height"]+3,"["..turtles[ID].group.."]",0x000000)
  84.         GUI[ID]["GP"].setZIndex(4) GUI[ID]["GP"].setScale(0.5)
  85.       end
  86.     end
  87.     -- Bring Background down with added Box
  88.     GUI["BG"].setHeight(GUI["BG"].getHeight()+11)
  89.     GUI.nY = GUI.nY + 11
  90.    
  91.     turtles[ID] = tDATA
  92.     turtles[ID].status = "inactive"
  93.     saveProperties()
  94.   else return false end
  95. end
  96. local remTurtlefromHUD = function(ID)
  97.   if GUI[ID] then
  98.     GUI[ID]['BG'].delete()
  99.     GUI[ID]['ID'].delete()
  100.     GUI[ID]['FUEL'].delete()
  101.     GUI[ID]['GPSL'].delete()
  102.     GUI[ID] = nil
  103.     turtles[ID] = nil
  104.     saveProperties()
  105.   end
  106. end
  107. local updateTurtleHUD = function(ID,STATUS)
  108.   if turtles[ID] then
  109.     local sUpdate = sColors[STATUS]
  110.     if GUI[ID] then
  111.       GUI[ID]['BG'].setColor(sUpdate)
  112.       return true
  113.     end
  114.   end
  115.   return false
  116. end
  117. ------------------------------------------------------------
  118. -- Init check
  119. term.clear() term.setCursorPos(1,1)
  120. for key,side in pairs(rs.getSides()) do -- Detect Peripherals
  121.   if peripheral.getType(side) == "terminal_glasses_bridge" then
  122.     TGB = peripheral.wrap(side)
  123.   elseif peripheral.getType(side) == "modem" then
  124.     MOD = peripheral.wrap(side)
  125.   end
  126. end
  127. if not TGB and not MOD then  -- Error check Peripherals
  128.   logBug("Requires Open Peripheral & Wireless Modem to run",true)
  129. elseif not TGB then
  130.   logBug("Requires Open Peripheral 0.1.9+ to run",true)
  131. elseif not MOD then
  132.   logBug("Requires a Wireless Modem to run",true)
  133. end
  134. if Args[1] == "-u" or Args[1] == "-uninstall" then -- Delete all files
  135.   if fs.exists('debug.txt') then
  136.     fs.delete('debug.txt')
  137.   end
  138.   if fs.exists('server.properties') then
  139.     fs.delete('server.properties')
  140.   end
  141.   return false
  142. end
  143. if fs.exists("server.properties") then
  144.   openProperties()
  145.   logBug("Server Properties file Loaded")
  146. else
  147.   meta = {hostChannel=nil,machineID=os.getComputerID(),deBug=false,warningFuel=100,warningInv=15,cUpdate=7,startups=1}
  148.   meta.hostChannel = math.ceil(math.random(128,65535))
  149.   saveProperties()
  150.   logBug("Server Properties file Created/Saved")
  151. end
  152. MOD.open(meta.hostChannel)
  153. print("Hosting on: "..meta.hostChannel)
  154. ------------------------------------------------------------
  155. -- Init GUI drawings
  156. TGB.clear()
  157. GUI["BG"] = TGB.addBox(0,0,120,30,0x808080,0.45)
  158. GUI["HR"] = TGB.addText(5,2,"RC Turtle Voice Command",0xFF2800)
  159. GUI["CH"] = TGB.addText(12,11,"Host Channel: "..meta.hostChannel,0xFF8300)
  160. GUI["TL"] = TGB.addText(12,19,"-ID-      -Fuel-     -Location-",0x3AAACF)
  161. GUI["BG"].setZIndex(0)
  162. GUI["HR"].setZIndex(1) GUI["HR"].setScale(0.9)
  163. GUI["CH"].setZIndex(1) GUI["CH"].setScale(0.65)
  164. GUI["TL"].setZIndex(1) GUI["TL"].setScale(0.5)
  165. -- END Init HUD drawings
  166. if turtles then
  167.   logBug("Re-establishing connections via Server-side")
  168.   for ID,DATA in pairs(turtles) do
  169.     turtles[ID]["status"] = "offline"
  170.     sendProtocol("connect","renew",ID,{"ping"})
  171.     os.startTimer(1.5)
  172.     local responded = false
  173.     while not responded do
  174.       local EV = { os.pullEvent() }
  175.       if EV[1] == "timer" then
  176.         responded = true
  177.         logBug("ERROR: Turtle timed out "..ID.." set to Offline")
  178.       elseif EV[1] == "modem_message" then
  179.         responded = true
  180.         logBug("Server Received [REDNET]: "..textutils.serialize(EV))
  181.         if EV[4] == tonumber(ID) then
  182.           turtles[ID]["status"] = "inactive"
  183.           logBug(ID.." Confirmed and set to In-Active")
  184.         end
  185.       end
  186.     end
  187.     drawTurtletoHUD(ID,DATA)
  188.   end
  189.   logBug("Re-establishing connections Server-side Completed")
  190. end
  191. if Args[1] == "-d" then
  192.   meta.deBug=true
  193. end
  194. ------------------------------------------------------------
  195. -- Start Program
  196. logBug(string.rep("=",30).." SERVER BUILD ["..BUILD.."] INITIATED "..string.rep("=",30))
  197. while running do
  198.   local EV = {os.pullEvent()}
  199.   if EV[1] == "chat_command" then
  200.     logBug("Server Received [Commands]: "..EV[2])
  201.     local cmds = splitString(string.lower(EV[2]))
  202.    
  203.     if cmds[1] == "walk" or cmds[1] == "goto" or cmds[1] == "turn" then
  204.       local TYPE = cmds[1]
  205.       table.remove(cmds,1)
  206.       for ID, data in pairs(turtles) do
  207.         if data["status"] == "active" then
  208.           sendProtocol("movement",TYPE,ID,cmds)
  209.         end
  210.       end
  211.    
  212.     elseif cmds[1] == "group" then
  213.       local TYPE = cmds[2]
  214.      
  215.       if TYPE == "add" then
  216.         local ID = tonumber(cmds[3])
  217.         local groupNum = tonumber(cmds[4])
  218.        
  219.         turtles[ID]["group"] = groupNum
  220.         GUI[ID]["GP"] = TGB.addText(2,GUI[ID]["height"]+3,"["..groupNum.."]",0x000000)
  221.         GUI[ID]["GP"].setZIndex(4) GUI[ID]["GP"].setScale(0.5)
  222.         logBug("Successfully Added: "..ID.." to Group "..groupNum)
  223.        
  224.       elseif TYPE == "delt" then -- Delete specific turtle from group
  225.         local ID = tonumber(cmds[3])
  226.         local gN
  227.        
  228.         if turtles[ID].group then
  229.           gN = turtles[ID].group
  230.           turtles[ID].group = nil
  231.           if GUI[ID]["GP"] then GUI[ID]["GP"].delete() end
  232.           logBug("Successfully removed "..ID.." from group "..gN)
  233.         else
  234.           logBug("ERROR: Turtle not in Group")
  235.         end
  236.      
  237.       elseif TYPE == "delg" then -- Delete entire Group
  238.      
  239.      
  240.       elseif TYPE == "sel" then
  241.         for ID, data in pairs(turtles) do
  242.           if data.group == tonumber(cmds[3]) then
  243.             turtles[ID].status = "active"
  244.           else
  245.             turtles[ID].status = "inactive"
  246.           end
  247.         end
  248.      
  249.       elseif TYPE == "off" then
  250.         for ID,DATA in pairs(turtles) do
  251.           if DATA.status == "active" then
  252.             DATA.status = "inactive"
  253.           end
  254.         end
  255.       end
  256.      
  257.     elseif cmds[1] == "toggle" then
  258.       local ID = tonumber(cmds[2])
  259.       if turtles[ID].status == "inactive" then
  260.         turtles[ID].status = "active"
  261.         logBug("Activated "..tostring(ID))
  262.         print("Activated "..tostring(ID))
  263.       else
  264.         turtles[ID].status = "inactive"
  265.         logBug("De-activated "..tostring(ID))
  266.         print("De-activated "..tostring(ID))
  267.       end
  268.     elseif cmds[1] == "kill" or cmds[1] == "die" then
  269.       logBug("Kill Command Initiated..")
  270.       running = false
  271.     end
  272.   -- HANDLE SERVER RESPONSES
  273.   elseif EV[1] == "modem_message" then
  274.     local ID = tonumber(EV[4])
  275.     local pakg = textutils.unserialize(EV[5])
  276.     logBug("Server Received [REDNET]: "..EV[5])
  277.     if type(pakg) == "table" then      
  278.       if pakg.ptl == "connect" then
  279.         if pakg.cType == "new" then
  280.           drawTurtletoHUD(ID,pakg.data)
  281.         end
  282.       end
  283.     end
  284.   end
  285.  
  286.   saveProperties()
  287.   for ID,DATA in pairs(turtles) do
  288.     updateTurtleHUD(ID,DATA.status)
  289.   end
  290. end
  291. -------- TERMINATION CODE --------
  292. for ID, data in pairs(turtles) do
  293.   data.status = "offline"
  294.   sendProtocol("terminate","kill",ID,data)
  295. end
  296. MOD.closeAll()
  297. TGB.clear()
  298. saveProperties()
Advertisement
Add Comment
Please, Sign In to add comment