Advertisement
C0BRA

Chat thing

Dec 31st, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.59 KB | None | 0 0
  1. radio = self.CreateLink("radio")
  2. keyboard = self.CreateLink("keyboard")
  3. console = self.CreateLink("console")
  4.  
  5. local y = 0
  6. local x = 0
  7.  
  8. local Caps = false
  9. local NumberCaps = '!"£$%^&*()'
  10. local SpecialKeys = {
  11.     [61] = "\\",
  12.     [62] = "-",
  13.     [63] = "=",
  14.     [65] = " ",
  15.     [58] = ",",
  16.     [59] = ".",
  17.     [60] = "/"
  18. }
  19. local SpecialKeys_Caps = {
  20.     [61] = "|",
  21.     [62] = "_",
  22.     [63] = "+",
  23.     [58] = "<",
  24.     [59] = ">",
  25.     [60] = "?"
  26. }
  27.  
  28. function GetCharFromKey(key)
  29.     if key >= 1 and key <= 10 then
  30.         if Caps then return NumberCaps[key - 1] end
  31.         return string.char(key + (48 - 1))
  32.     end
  33.    
  34.     if key >= 37 and key <= 46 then
  35.         return string.char(key + (48 - 37))
  36.     end
  37.    
  38.     if key >= 11 and key <= 36 then
  39.         if Caps then
  40.             return string.upper(string.char(key + (97 - 11)))
  41.         end
  42.         return string.char(key + (97 - 11))
  43.     end
  44.    
  45.     if Caps then
  46.         return SpecialKeys_Caps[key] or SpecialKeys[key]
  47.     end
  48.     return SpecialKeys[key]
  49. end
  50.  
  51. local first = true
  52. local current = ""
  53.  
  54. function ReadKeyboard()
  55.     while true do
  56.         local event = keyboard.NextEvent()
  57.         if not event then break end
  58.        
  59.         if event.Key == 79 then -- Shift
  60.             Caps = event.Down
  61.             continue
  62.         elseif event.Key == 51 or event.Key == 64 then -- ENTER
  63.             if radio.HasData() then return end
  64.             if not event.Down then continue end
  65.            
  66.             SendMessage(current)
  67.             console.Draw(string.rep(" ", 61), 0, 61, nil, Color(0,0,0,255))
  68.             current = ""
  69.             continue
  70.         elseif event.Key == 66 then -- BKSP
  71.             if not event.Down then continue end
  72.             current = string.sub(current, 1, string.len(current) - 1)
  73.             console.Draw(string.rep(" ", 61), 0, 61, nil, Color(0,0,0,255))
  74.             console.Draw(current, 0, 61)
  75.             continue
  76.         end
  77.        
  78.         if not event.Down then continue end
  79.        
  80.         local char = GetCharFromKey(event.Key)
  81.         if not char then continue end
  82.        
  83.         current = current .. char
  84.         console.Draw(char, string.len(current) - 1, 61)
  85.     end
  86. end
  87.  
  88. local CurrentStatus = ""
  89. function SetStatus(status)
  90.     if CurrentStatus == status then return end
  91.     CurrentStatus = status
  92.    
  93.     local len = string.len(status)
  94.     local spacesl = (63 - math.ceil(len)) / 2
  95.     local spacesr = spacesl
  96.     if len + spacesr + spacesl > 61 then
  97.         spacesr = spacesr - 1
  98.     end
  99.    
  100.     local str = string.rep(" ", spacesl) .. status .. string.rep(" ", spacesr)
  101.     console.Draw(str, 0, 0, Color(0,0,0,255), Color(255,255,255,255))
  102. end
  103.  
  104. function SendMessage(str)
  105.     if string.StartWith(str, "!") then
  106.        
  107.         if string.StartWith(str, "!freq ") then
  108.             local freq = tonumber(string.sub(str, string.len("!freq "))) or 200
  109.            
  110.             radio.SetFrequency(freq)
  111.            
  112.             AddChatLine("set frequency to " .. tostring(freq) .. "MHz", Color(255, 255, 0))
  113.            
  114.             return
  115.         elseif string.StartWith(str, "!pwr ") then
  116.             local pwr = tonumber(string.sub(str, string.len("!pwr "))) or 5
  117.            
  118.             radio.SetOutputPower(pwr)
  119.            
  120.             AddChatLine("set output power to " .. tostring(pwr) .. "W", Color(255, 255, 0))
  121.            
  122.             return
  123.         elseif string.StartWith(str, "!scan") then
  124.             local pwr = tonumber(string.sub(str, string.len("!pwr "))) or 5
  125.            
  126.             radio.SetOutputPower(pwr)
  127.            
  128.             AddChatLine("scannning..." .. tostring(pwr) .. "W", Color(255, 255, 0))
  129.            
  130.             return
  131.         end
  132.        
  133.         AddChatLine("error: unknown command!", Color(255, 255, 0))
  134.         return
  135.     end
  136.    
  137.     for i = 1, string.len(str) do
  138.         radio.WriteByte(string.byte(str[i]))
  139.     end
  140.    
  141.     radio.WriteByte(0)
  142.    
  143.     AddChatLine(str, Color(0, 255, 0))
  144. end
  145.  
  146. function AddChatLine(str, col)
  147.     y = (y + 1) % 58
  148.     local blanky = (y + 1) % 58
  149.    
  150.     console.Draw(string.rep(" ", 61), 0, blanky + 1, nil, Color(0,0,0,255))
  151.     console.Draw(str, 0, y + 1, col)
  152. end
  153.  
  154. local Recieved = ""
  155. local lowest_amp = 10000
  156. function Think()
  157.     if not radio.Connected then return 1 end
  158.     if not keyboard.Connected then return 1 end
  159.     if not console.Connected then return 1 end
  160.    
  161.     if first then
  162.         first = false
  163.         radio.SetSquelch(1)
  164.         radio.SetOutputPower(5)
  165.        
  166.         console.Clear(Color(0, 0, 0, 255))
  167.         SetStatus("waiting")
  168.     end
  169.    
  170.     if keyboard.HasTyper() then
  171.         ReadKeyboard()
  172.        
  173.         if radio.SendQueueSize() != 0 then
  174.             SetStatus("sending")
  175.         elseif CurrentStatus == "sending" then
  176.             SetStatus("waiting")
  177.         end
  178.     end
  179.    
  180.     if radio.HasData() then
  181.        
  182.         SetStatus("recieving data")
  183.        
  184.         while radio.HasData() do
  185.             local b, amp = radio.ReadByte()
  186.            
  187.             if amp < lowest_amp then
  188.                 lowest_amp = amp
  189.             end
  190.            
  191.             if b == 0 then
  192.                 SetStatus("waiting")
  193.                 local col = nil
  194.                
  195.                 if lowest_amp <= 2.5 then
  196.                     col = Color(255, 127, 0)
  197.                 end
  198.                
  199.                 AddChatLine(Recieved, col)
  200.                 Recieved = ""
  201.                 lowest_amp = 10000
  202.             else
  203.                 Recieved = Recieved .. string.char(b)
  204.             end
  205.         end
  206.     end
  207.    
  208.     return 0
  209. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement