@@ -42,7 +42,11 @@ public static class VoiceGameMgrGlobals
4242
4343 // Set game rules to allow all clients to talk to each other.
4444 // Muted players still can't talk to each other.
45+ #if GMOD_DLL
46+ public static readonly ConVar sv_alltalk = new ( "sv_alltalk" , "0" , FCvar . Notify , "Players can hear all other players, no team restrictions" ) ;
47+ #else
4548 public static readonly ConVar sv_alltalk = new ( "sv_alltalk" , "0" , FCvar . Notify | FCvar . Replicated , "Players can hear all other players, no team restrictions" ) ;
49+ #endif
4650
4751 public static readonly VoiceGameMgr g_VoiceGameMgr = new ( ) ;
4852
@@ -67,9 +71,15 @@ public static void VoiceServerDebug(ReadOnlySpan<char> msg) {
6771// VoiceGameMgr manages which clients can hear which other clients.
6872public class VoiceGameMgr
6973{
74+ #if GMOD_DLL
75+ double LastVoicePacketTime ;
76+ #endif
7077 IVoiceGameMgrHelper ? Helper ;
7178 int MaxPlayers ;
7279 double UpdateInterval ; // How long since the last update.
80+ #if GMOD_DLL
81+ double FullUpdateInterval ;
82+ #endif
7383 int ProximityDistance ;
7484
7585 public VoiceGameMgr ( ) {
@@ -94,17 +104,67 @@ public void SetHelper(IVoiceGameMgrHelper? helper) {
94104 // If gameplay mode is teamplay, then only players on the same team can hear each other.
95105 // Player masks are always applied.
96106 public void Update ( double frametime ) {
107+ #if GMOD_DLL
108+ UpdateInterval += frametime ;
109+ FullUpdateInterval += frametime ;
110+ if ( UpdateInterval < UPDATE_INTERVAL )
111+ return ;
112+
113+ if ( gpGlobals . CurTime - LastVoicePacketTime > UPDATE_INTERVAL && UpdateInterval < 5.0 )
114+ return ;
115+
116+ if ( UpdateInterval > 3.0 || FullUpdateInterval > 5.0 ) {
117+ FullUpdateInterval = 0 ;
118+ UpdatePlayers ( true , null ) ;
119+ }
120+ else
121+ UpdatePlayers ( false , null ) ;
122+ #else
97123 // Only update periodically.
98124 UpdateInterval += frametime ;
99125 if ( UpdateInterval < UPDATE_INTERVAL )
100126 return ;
101127
102128 UpdateMasks ( ) ;
129+ #endif
130+ }
131+
132+ #if GMOD_DLL
133+ public void OnReceivedVoicePacket ( BaseEntity ? entity ) {
134+ if ( entity == null || ! entity . IsPlayer ( ) )
135+ return ;
136+
137+ BasePlayer player = ( BasePlayer ) entity ;
138+ bool startedTalking = gpGlobals . CurTime - player . LastVoicePacketTime > UPDATE_INTERVAL ;
139+ player . OnVoicePacketReceived ( ) ;
140+ LastVoicePacketTime = gpGlobals . CurTime ;
141+
142+ if ( startedTalking )
143+ UpdatePlayers ( false , player ) ;
144+ }
145+
146+ void UpdatePlayers ( bool full , BasePlayer ? talker ) {
147+ UpdateInterval = 0 ;
148+
149+ for ( int iClient = 0 ; iClient < MaxPlayers ; iClient ++ ) {
150+ BaseEntity ? ent = Util . PlayerByIndex ( iClient + 1 ) ;
151+ if ( ent != null && ent . IsPlayer ( ) )
152+ UpdatePlayer ( ( BasePlayer ) ent , full , talker ) ;
153+ }
103154 }
155+ #endif
104156
105157 // Called when a new client connects (unsquelches its entity for everyone).
106- public void ClientConnected ( Edict edict ) {
107- int index = edict . EdictIndex - 1 ;
158+ public void ClientConnected ( Edict ? edict ) {
159+ #if GMOD_DLL
160+ if ( edict == null )
161+ return ;
162+ #endif
163+ int index = edict ! . EdictIndex - 1 ;
164+ #if GMOD_DLL
165+ if ( ( uint ) index >= VOICE_MAX_PLAYERS )
166+ return ;
167+ #endif
108168
109169 // Clear out everything we use for deltas on this guy.
110170 g_bWantModEnable . Set ( index , true ) ;
@@ -164,6 +224,74 @@ public bool IsPlayerIgnoringPlayer(int talker, int listener) {
164224 return g_BanMasks [ listener - 1 ] . IsBitSet ( talker - 1 ) ;
165225 }
166226
227+ #if GMOD_DLL
228+ void UpdatePlayer ( BasePlayer player , bool full , BasePlayer ? talker ) {
229+ int playerIndex = player . EntIndex ( ) ;
230+ int iClient = playerIndex - 1 ;
231+
232+ SingleUserRecipientFilter user = new ( player ) ;
233+
234+ // Request the state of their "VModEnable" cvar
235+ if ( g_bWantModEnable . IsBitSet ( iClient ) ) {
236+ UserMessageBegin ( user , "RequestState" ) ;
237+ MessageEnd ( ) ;
238+ // Only send it once
239+ g_bWantModEnable . Set ( iClient , false ) ;
240+ }
241+
242+ PlayerBitSet gameRulesMask = default ;
243+ PlayerBitSet proximityMask = default ;
244+ if ( g_PlayerModEnable . IsBitSet ( iClient ) ) {
245+ bool proximity = false ;
246+
247+ // Build a mask of who they can hear based on the game rules
248+ for ( int iOtherClient = 0 ; iOtherClient < MaxPlayers ; iOtherClient ++ ) {
249+ BaseEntity ? otherEnt = Util . PlayerByIndex ( iOtherClient + 1 ) ;
250+ if ( otherEnt == null || ! otherEnt . IsPlayer ( ) )
251+ continue ;
252+
253+ BasePlayer other = ( BasePlayer ) otherEnt ;
254+ bool canHear ;
255+ if ( full || other == talker || gpGlobals . CurTime - other . LastVoicePacketTime < 1.0 )
256+ canHear = Helper ! . CanPlayerHearPlayer ( player , other , ref proximity ) ;
257+ else {
258+ canHear = g_pVoiceServer . GetClientListening ( playerIndex , iOtherClient + 1 ) ;
259+ proximity = g_pVoiceServer . GetClientProximity ( playerIndex , iOtherClient + 1 ) ;
260+ }
261+
262+ if ( canHear ) {
263+ gameRulesMask . Set ( iOtherClient , true ) ;
264+ proximityMask . Set ( iOtherClient , proximity ) ;
265+ }
266+ }
267+ }
268+
269+ // If this is different from what the client has, send an update
270+ if ( gameRulesMask != g_SentGameRulesMasks [ iClient ] ||
271+ g_BanMasks [ iClient ] != g_SentBanMasks [ iClient ] ) {
272+ g_SentGameRulesMasks [ iClient ] = gameRulesMask ;
273+ g_SentBanMasks [ iClient ] = g_BanMasks [ iClient ] ;
274+
275+ UserMessageBegin ( user , "VoiceMask" ) ;
276+ int dw ;
277+ for ( dw = 0 ; dw < VOICE_MAX_PLAYERS_DW ; dw ++ ) {
278+ MessageWriteLong ( ( int ) gameRulesMask . GetWord ( dw ) ) ;
279+ MessageWriteLong ( ( int ) g_BanMasks [ iClient ] . GetWord ( dw ) ) ;
280+ }
281+ MessageWriteByte ( g_PlayerModEnable . IsBitSet ( iClient ) ? 1 : 0 ) ;
282+ MessageEnd ( ) ;
283+ }
284+
285+ // Tell the engine.
286+ for ( int iOtherClient = 0 ; iOtherClient < MaxPlayers ; iOtherClient ++ ) {
287+ bool canHear = gameRulesMask . IsBitSet ( iOtherClient ) && ! g_BanMasks [ iClient ] . IsBitSet ( iOtherClient ) ;
288+ g_pVoiceServer . SetClientListening ( playerIndex , iOtherClient + 1 , canHear ) ;
289+
290+ if ( canHear )
291+ g_pVoiceServer . SetClientProximity ( playerIndex , iOtherClient + 1 , proximityMask . IsBitSet ( iOtherClient ) ) ;
292+ }
293+ }
294+ #else
167295 // Force it to update the client masks.
168296 void UpdateMasks ( ) {
169297 UpdateInterval = 0 ;
@@ -228,4 +356,5 @@ void UpdateMasks() {
228356 }
229357 }
230358 }
359+ #endif
231360}
0 commit comments