---- Voicechat popup, radio commands, text chat stuff -- special processing for certain special chat types function GM:ChatText(idx, name, text, type) if type == "joinleave" then if string.find(text, "Changed name during a round") then chat.AddText("A player was automatically kicked for changing their name during a round.") return true end end if idx == 0 and type == "none" then if string.sub(text, 1, 9) == "(TRAITOR)" then -- (TRAITOR) nicknametext local nick, text_only = string.match(text, "%(TRAITOR%) (.-)\t(.*)") local tab = { Color( 255, 30, 40 ), "(TRAITOR) ", Color( 255, 200, 20), nick, Color( 255, 255, 200), ": " .. text_only }; chat.AddText( unpack(tab) ) return true elseif string.sub(text, 1, 11) == "(DETECTIVE)" then -- (DETECTIVE) nicknametext local nick, text_only = string.match(text, "%(DETECTIVE%) (.-)\t(.*)") local tab = { Color( 20, 100, 255 ), "(DETECTIVE) ", Color( 25, 250, 255), nick, Color( 200, 255, 255), ": " .. text_only }; chat.AddText( unpack(tab) ) return true elseif string.sub(text, 1, 12) == "(LAST WORDS)" then local tab = {} -- (LAST WORDS)(role)nicknametext local role, nick, text_only = string.match(text, "%(LAST WORDS%)%(([DI])%)(.-)\t(.*)") table.insert( tab, Color( 150, 150, 150 )) table.insert( tab, "(LAST WORDS) ") if role == "D" then table.insert( tab, Color(50, 200, 255)) else table.insert( tab, Color(0, 200, 0)) end table.insert( tab, nick) table.insert( tab, Color( 255, 255, 255 ) ) table.insert( tab, ": ".. text_only ) chat.AddText( unpack(tab) ) return true elseif string.sub(text, 1, 6) == "(VOTE)" then chat.AddText(Color(255, 180, 0), string.sub(text, 8)) return true end end return self.BaseClass:ChatText(idx, name, text, type) end function GM:OnPlayerChat( ply, text, teamchat, dead ) -- Detectives have blue name if IsValid(ply) and ply:IsActiveDetective() then chat.AddText(Color(50, 200, 255), ply:Nick(), Color(255,255,255), ": " .. text) return true end return self.BaseClass:OnPlayerChat(ply, text, teamchat, dead) end local last_chat = "" function GM:ChatTextChanged(text) last_chat = text end function ChatInterrupt(um) local client = LocalPlayer() local id = um:ReadLong() local last_seen = IsValid(client.last_id) and client.last_id:EntIndex() or 0 local last_words = "." if last_chat == "" then if RADIO.LastRadio.t > CurTime() - 2 then last_words = RADIO.LastRadio.msg end else last_words = last_chat end RunConsoleCommand("_deathrec", tostring(id), tostring(last_seen), last_words) end usermessage.Hook("interrupt_chat", ChatInterrupt) --- Radio RADIO = {} RADIO.Show = false RADIO.StoredTarget = {nick="", t=0} RADIO.LastRadio = {msg="", t=0} -- [key] -> command RADIO.Commands = { {cmd="yes", text="Yes.", format=false}, {cmd="no", text="No.", format=false}, {cmd="help", text="Help!", format=false}, {cmd="imwith", text="I'm with %s.", format=true}, {cmd="see", text="I see %s.", format=true}, {cmd="suspect", text="%s acts suspicious.", format=true}, {cmd="traitor", text="%s is the traitor!", format=true}, {cmd="check", text="Anyone still alive?", format=false} }; local radioframe = nil function RADIO:ShowRadioCommands(state) if not state then if radioframe and radioframe:IsValid() then radioframe:Remove() radioframe = nil -- don't capture keys self.Show = false end else local client = LocalPlayer() if not ValidEntity(client) then return end if not radioframe then local w, h = 200, 300 radioframe = vgui.Create("DForm") radioframe:SetName("Quickchat keys") radioframe:SetSize(w, h) radioframe:SetMouseInputEnabled(false) radioframe:SetKeyboardInputEnabled(false) radioframe:CenterVertical() -- ASS radioframe.ForceResize = function(s) local w = 0 for k,v in pairs(s.Items) do if v.Left:GetWide() > w then w = v.Left:GetWide() end end s:SetWide(w + 20) end for key, command in pairs(self.Commands) do local dlabel = vgui.Create("DLabel", radioframe) local id = key .. ": " local txt = id .. command.text if command.format then dlabel:SetText(string.format(txt, "nobody")) else dlabel:SetText(txt) end dlabel:SetFont("TabLarge") dlabel:SizeToContents() if command.format then dlabel.nick = "" dlabel.id = id dlabel.txt = command.text dlabel.Think = function(s) local tgt, v = RADIO:GetTarget() if s.nick != tgt then s.nick = tgt tgt = string.format(s.txt, tgt) if v then tgt = util.Capitalize(tgt) end s:SetText(s.id .. tgt) s:SizeToContents() radioframe:ForceResize() end end end radioframe:AddItem(dlabel) end radioframe:ForceResize() end radioframe:MakePopup() -- grabs input on init(), which happens in makepopup radioframe:SetMouseInputEnabled(false) radioframe:SetKeyboardInputEnabled(false) -- capture slot keys while we're open self.Show = true timer.Create("radiocmdshow", 3, 1, RADIO.ShowRadioCommands, self, false) end end function RADIO:SendCommand(slotidx) local c = self.Commands[slotidx] if c then RunConsoleCommand("ttt_radio", c.cmd) self:ShowRadioCommands(false) end end -- Returns targeted nick, with second return value indicating whether it is -- generic (ie. "nobody") and can therefore be capitalized function RADIO:GetTargetNick() if not ValidEntity(LocalPlayer()) then return end local trace = LocalPlayer():GetEyeTrace(MASK_SHOT) if not trace or (not trace.Hit) or (not IsValid(trace.Entity)) then return end local ent = trace.Entity if ent:IsPlayer() then if ent:GetNWBool("disguised", false) then return "someone in disguise", true else return ent:Nick(), false end elseif ent:GetClass() == "prop_ragdoll" and CORPSE.GetPlayerNick(ent, "") != "" then if DetectiveMode() and not CORPSE.GetFound(ent, false) then return "an unidentified corpse", true else return CORPSE.GetPlayerNick(ent, "A Terrorist") .. "'s corpse", false end end end function RADIO:GetTarget() local client = LocalPlayer() if ValidEntity(client) then local current, vague = self:GetTargetNick() if current then return current, vague end local stored = self.StoredTarget if stored.nick != "" and stored.t > (CurTime() - 3) then return stored.nick, stored.vague end end return "nobody", true end function RADIO:StoreTarget() local current, vague = self:GetTargetNick() if current then self.StoredTarget.nick = current self.StoredTarget.vague = vague self.StoredTarget.t = CurTime() end end -- Radio commands are a console cmd instead of directly sent from RADIO, because -- this way players can bind keys to them local function RadioCommand(ply, cmd, arg) if not ValidEntity(ply) or #arg != 1 then print("ttt_radio failed, too many arguments?") return end if RADIO.LastRadio.t > (CurTime() - 0.5) then return end local type = arg[1] local text = "" local target, vague = RADIO:GetTarget() -- TODO: rework to avoid specifying text in two places in different ways if type == "yes" then text = "Yes." elseif type == "no" then text = "No." elseif type == "help" then text = "Help!" elseif type == "imwith" then text = "I'm with " .. target .. "." elseif type == "see" then text = "I see " .. target .. "." elseif type == "traitor" then text = target .. " is a traitor!" elseif type == "suspect" then text = target .. " is acting suspicious." elseif type == "check" then text = "Anyone still alive?" else print("ttt_radio failed, argument not valid radiocommand") return end if vague then text = util.Capitalize(text) end RADIO.LastRadio.t = CurTime() RADIO.LastRadio.msg = text RunConsoleCommand("say", text) end local function RadioComplete(cmd, arg) local c = {} for k, cmd in pairs(RADIO.Commands) do table.insert(c, cmd.cmd) end return c end concommand.Add("ttt_radio", RadioCommand, RadioComplete) --- voicechat stuff VOICE = {} local MutedState = nil -- voice popups, copied from base gamemode and modified g_VoicePanelList = nil local voicepos = CreateConVar("ttt_voicechat_topleft", "1", FCVAR_ARCHIVE) -- Because users may change the voice position setting at any time, we -- reposition this whenever someone new speaks, so that it remains up to date. local function PositionVoicePanelList() if voicepos:GetBool() then g_VoicePanelList:SetPos(25, 25) g_VoicePanelList:SetBottomUp(false) else g_VoicePanelList:SetPos(ScrW() - 250, 100) g_VoicePanelList:SetBottomUp(true) end end -- 255 at 100 -- 5 at 5000 local function VoiceNotifyThink(pnl) if not (ValidPanel(pnl) and LocalPlayer() and IsValid(pnl.Player)) then return end local d = LocalPlayer():GetPos():Distance(pnl.Player:GetPos()) pnl:SetAlpha(math.max(-0.1 * d + 255, 15)) end local PlayerVoicePanels = {} function GM:PlayerStartVoice( ply ) local client = LocalPlayer() if not IsValid(g_VoicePanelList) or not IsValid(client) then return end -- There'd be an extra one if voice_loopback is on, so remove it. GAMEMODE:PlayerEndVoice(ply, true) if not IsValid(ply) then return end -- Tell server this is global if client == ply then if client:IsActiveTraitor() then if (not client:KeyDown(IN_SPEED)) and (not client:KeyDownLast(IN_SPEED)) then client.traitor_gvoice = true RunConsoleCommand("tvog", "1") else client.traitor_gvoice = false RunConsoleCommand("tvog", "0") end end VOICE.SetSpeaking(true) end local pnl = vgui.Create("VoiceNotify") pnl:Setup(ply) if GetGlobalBool("ttt_locational_voice", false) and (not ply:IsSpec()) and (ply != client) then pnl.Player = ply pnl.Think = VoiceNotifyThink end if client:IsActiveTraitor() then if ply == client then if not client.traitor_gvoice then pnl.Color = Color(210, 0, 0, 255) end elseif ply:IsActiveTraitor() then if not ply.traitor_gvoice then pnl.Color = Color(200, 0, 0, 255) -- unhook locational fade think pnl.Think = nil end end end if ply:IsActiveDetective() then pnl.Color = Color(0, 0, 200, 255) end PositionVoicePanelList() g_VoicePanelList:AddItem( pnl ) PlayerVoicePanels[ply] = pnl end local function ReceiveVoiceState(um) local idx = um:ReadShort() local state = um:ReadBool() -- prevent glitching due to chat starting/ending across round boundary if GAMEMODE.round_state != ROUND_ACTIVE then return end if (not ValidEntity(LocalPlayer())) or (not LocalPlayer():IsActiveTraitor()) then return end local ply = player.GetByID(idx) if ValidEntity(ply) then ply.traitor_gvoice = state if IsValid(PlayerVoicePanels[ply]) then PlayerVoicePanels[ply].Color = state and Color(0,200,0) or Color(200, 0, 0) end end end usermessage.Hook("tvo", ReceiveVoiceState) local function VoiceClean() for ply, pnl in pairs( PlayerVoicePanels ) do if (not IsValid(pnl)) or (not IsValid(ply)) then GAMEMODE:PlayerEndVoice(ply) end end end timer.Create( "VoiceClean", 10, 0, VoiceClean ) function GM:PlayerEndVoice(ply, no_reset) if IsValid( PlayerVoicePanels[ply] ) then g_VoicePanelList:RemoveItem( PlayerVoicePanels[ply] ) PlayerVoicePanels[ply]:Remove() PlayerVoicePanels[ply] = nil end if IsValid(ply) and not no_reset then ply.traitor_gvoice = false end if ply == LocalPlayer() then VOICE.SetSpeaking(false) end -- this was inactive due to a bug for a long time, and started causing -- problems when enabled, so now commented out -- local client = LocalPlayer() -- if client == ply and client:IsActiveTraitor() then -- if (not client:KeyDown(IN_SPEED)) and (not client:KeyDownLast(IN_SPEED)) then -- client.traitor_gvoice = false -- RunConsoleCommand("tvog", "0") -- end -- end end local function CreateVoiceVGUI() g_VoicePanelList = vgui.Create( "DPanelList" ) g_VoicePanelList:ParentToHUD() PositionVoicePanelList() g_VoicePanelList:SetSize( 200, ScrH() - 200 ) g_VoicePanelList:SetDrawBackground( false ) g_VoicePanelList:SetSpacing( 8 ) MutedState = vgui.Create("DLabel") MutedState:SetPos(ScrW() - 200, ScrH() - 50) MutedState:SetSize(200, 50) MutedState:SetFont("Trebuchet19") MutedState:SetText("") MutedState:SetTextColor(Color(240, 240, 240, 250)) MutedState:SetVisible(false) end hook.Add( "InitPostEntity", "CreateVoiceVGUI", CreateVoiceVGUI ) MUTE_NONE = 0 MUTE_TERROR = TEAM_TERROR MUTE_SPEC = TEAM_SPEC local MuteStates = {MUTE_NONE, MUTE_TERROR, MUTE_SPEC} local MuteText = { [MUTE_NONE] = "", [MUTE_TERROR] = "LIVING PLAYERS MUTED", [MUTE_SPEC] = "SPECTATORS MUTED" }; local function SetMuteState(state) if MutedState then MutedState:SetText(MuteText[state]) MutedState:SetVisible(state != MUTE_NONE) end end local mute_state = MUTE_NONE function VOICE.CycleMuteState(force_state) mute_state = force_state or next(MuteText, mute_state) if not mute_state then mute_state = MUTE_NONE end SetMuteState(mute_state) return mute_state end local battery_max = 100 local battery_min = 10 function VOICE.InitBattery() LocalPlayer().voice_battery = battery_max end local function GetRechargeRate() local r = GetGlobalFloat("ttt_voice_drain_recharge", 0.05) if LocalPlayer().voice_battery < battery_min then r = r / 2 end return r end local function GetDrainRate() if not GetGlobalBool("ttt_voice_drain", false) then return 0 end if GetRoundState() != ROUND_ACTIVE then return 0 end local ply = LocalPlayer() if (not IsValid(ply)) or ply:IsSpec() then return 0 end if ply:IsAdmin() then return GetGlobalFloat("ttt_voice_drain_admin", 0) else return GetGlobalFloat("ttt_voice_drain_normal", 0) end end local function IsTraitorChatting(client) return client:IsActiveTraitor() and (not client.traitor_gvoice) end function VOICE.Tick() if not GetGlobalBool("ttt_voice_drain", false) then return end local client = LocalPlayer() if VOICE.IsSpeaking() and (not IsTraitorChatting(client)) then client.voice_battery = client.voice_battery - GetDrainRate() --print("speaking", client.voice_battery) if not VOICE.CanSpeak() then client.voice_battery = 0 RunConsoleCommand("-voicerecord") end elseif client.voice_battery < battery_max then client.voice_battery = client.voice_battery + GetRechargeRate() end end -- Player:IsSpeaking() does not work for localplayer function VOICE.IsSpeaking() return LocalPlayer().speaking end function VOICE.SetSpeaking(state) LocalPlayer().speaking = state end function VOICE.CanSpeak() if not GetGlobalBool("ttt_voice_drain", false) then return true end return LocalPlayer().voice_battery > battery_min or IsTraitorChatting(LocalPlayer()) end local speaker = surface.GetTextureID("voice/icntlk_sv") function VOICE.Draw(client) local b = client.voice_battery if b >= battery_max then return end local x = 25 local y = 10 local w = 200 local h = 6 if b < battery_min and CurTime() % 0.2 < 0.1 then surface.SetDrawColor(200, 0, 0, 155) else surface.SetDrawColor(0, 200, 0, 255) end surface.DrawOutlinedRect(x, y, w, h) surface.SetTexture(speaker) surface.DrawTexturedRect(5, 5, 16, 16) x = x + 1 y = y + 1 w = w - 2 h = h - 2 surface.SetDrawColor(0, 200, 0, 150) surface.DrawRect(x, y, w * math.Clamp((client.voice_battery - 10) / 90, 0, 1), h) end