home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / game / mp / VoiceComms.cpp < prev   
C/C++ Source or Header  |  2005-11-14  |  4KB  |  176 lines

  1. #include "../../idlib/precompiled.h"
  2. #pragma hdrstop
  3. #include "../Game_local.h"
  4.  
  5. #ifdef _USE_VOICECHAT
  6.  
  7. #define MAX_VOICE_PACKET_SIZE    1022
  8.  
  9. idCVar si_voiceChat( "si_voiceChat", "1", CVAR_GAME | CVAR_BOOL | PC_CVAR_ARCHIVE | CVAR_SERVERINFO, "enables or disables voice chat through the server" );
  10. idCVar g_voiceChatDebug( "g_voiceChatDebug", "0", CVAR_GAME | CVAR_BOOL, "display info on voicechat net traffic" );
  11.  
  12. void idMultiplayerGame::XmitVoiceData( void )
  13. {
  14.     idBitMsg    outMsg;
  15.     int            count, total;
  16.     byte        buffer[MAX_VOICE_PACKET_SIZE];
  17.     byte        msgBuf[MAX_VOICE_PACKET_SIZE + 1];
  18.  
  19.     if( !gameLocal.serverInfo.GetBool( "si_voiceChat" ) )
  20.     {
  21.         return;
  22.     }
  23.  
  24.     outMsg.Init( msgBuf, sizeof( msgBuf ) );
  25.  
  26.     // Grab any new input and send up to the server
  27.     count = soundSystem->GetVoiceData( buffer, MAX_VOICE_PACKET_SIZE );
  28.     total = count;
  29.     while( count )
  30.     {
  31.         outMsg.BeginWriting();
  32.         outMsg.BeginReading();
  33.         // Client side cvar
  34.         if( cvarSystem->GetCVarBool( "s_voiceChatEcho" ) )
  35.         {
  36.             outMsg.WriteByte( GAME_RELIABLE_MESSAGE_VOICEDATA_CLIENT_ECHO );
  37.         }
  38.         else
  39.         {
  40.             outMsg.WriteByte( GAME_RELIABLE_MESSAGE_VOICEDATA_CLIENT );
  41.         }
  42.         outMsg.WriteData( buffer, count );
  43.     
  44.         // FIXME!!! FIXME!!! This should be unreliable - this is very bad
  45.         networkSystem->ClientSendReliableMessage( outMsg );
  46.  
  47.         count = soundSystem->GetVoiceData( buffer, MAX_VOICE_PACKET_SIZE );
  48.         total += count;
  49.     }
  50.  
  51.     if( total && g_voiceChatDebug.GetBool() )
  52.     {
  53.         common->Printf( "VC: sent %d bytes\n", total );
  54.     }
  55. }
  56.  
  57. bool idMultiplayerGame::CanTalk( idPlayer *from, idPlayer *to, bool echo )
  58. {
  59.     if( !to )
  60.     {
  61.         return( false );
  62.     }
  63.  
  64.     if( !from )
  65.     {
  66.         return( false );
  67.     }
  68.  
  69.     if( from->spectating != to->spectating )
  70.     {
  71.         return( false );
  72.     }
  73.  
  74.     if( to->IsPlayerMuted( from ) )
  75.     {
  76.         return( false );
  77.     }
  78.  
  79.     if( gameLocal.IsTeamGame() && from->team != to->team )
  80.     {
  81.         return( false );
  82.     }
  83.  
  84.     if( from == to )
  85.     {
  86.         return( echo );
  87.     }
  88.  
  89.     return( true );
  90. }
  91.  
  92. void idMultiplayerGame::ReceiveAndForwardVoiceData( int clientNum, const idBitMsg &inMsg, bool echo )
  93. {
  94.     assert( clientNum >= 0 && clientNum < MAX_CLIENTS );
  95.  
  96.     idBitMsg    outMsg;
  97.     int            i;
  98.     byte        msgBuf[MAX_VOICE_PACKET_SIZE + 2];
  99.     idPlayer *    from;
  100.     
  101.     from = ( idPlayer * )gameLocal.entities[clientNum];
  102.     if( !gameLocal.serverInfo.GetBool( "si_voiceChat" ) || !from )
  103.     {
  104.         return;
  105.     }
  106.  
  107.     // Create a new packet with forwarded data
  108.     outMsg.Init( msgBuf, sizeof( msgBuf ) );
  109.     outMsg.WriteByte( GAME_UNRELIABLE_MESSAGE_VOICEDATA_SERVER );
  110.     outMsg.WriteByte( clientNum );
  111.     outMsg.WriteData( inMsg.GetReadData(), inMsg.GetRemainingData() );
  112.  
  113.     if( g_voiceChatDebug.GetBool() )
  114.     {
  115.         common->Printf( "VC: Received %d bytes, forwarding...\n", inMsg.GetRemainingData() );
  116.     }
  117.  
  118.     // Forward to appropriate parties
  119.     for( i = 0; i < gameLocal.numClients; i++ ) 
  120.     {
  121.         idPlayer* to = ( idPlayer * )gameLocal.entities[i];
  122.         if( to && to->GetUserInfo() && to->GetUserInfo()->GetBool( "s_voiceChatReceive" ) )
  123.         {
  124.             if( i != gameLocal.localClientNum && CanTalk( from, to, echo ) )
  125.             {
  126.                 gameLocal.SendUnreliableMessage( outMsg, to->entityNumber );
  127.                 if( g_voiceChatDebug.GetBool() )
  128.                 {
  129.                     common->Printf( " ... to client %d\n", i );
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.     // This is revolting
  136.     // Listen servers need to manually call the receive function
  137.     if( gameLocal.isListenServer )
  138.     {
  139.         // Skip over control byte
  140.         outMsg.ReadByte();
  141.  
  142.         idPlayer* to = gameLocal.GetLocalPlayer();
  143.         if( to->GetUserInfo()->GetBool( "s_voiceChatReceive" ) )
  144.         {
  145.             if( CanTalk( from, to, echo ) )
  146.             {
  147.                 if( g_voiceChatDebug.GetBool() )
  148.                 {
  149.                     common->Printf( " ... to local client %d\n", gameLocal.localClientNum );
  150.                 }
  151.                 ReceiveAndPlayVoiceData( outMsg );
  152.             }
  153.         }
  154.     }
  155. }
  156.  
  157. void idMultiplayerGame::ReceiveAndPlayVoiceData( const idBitMsg &inMsg )
  158. {
  159.     int        clientNum;
  160.  
  161.     if( !gameLocal.serverInfo.GetBool( "si_voiceChat" ) )
  162.     {
  163.         return;
  164.     }
  165.     
  166.     clientNum = inMsg.ReadByte();
  167.     soundSystem->PlayVoiceData( clientNum, inMsg.GetReadData(), inMsg.GetRemainingData() );
  168.     if( g_voiceChatDebug.GetBool() )
  169.     {
  170.         common->Printf( "VC: Playing %d bytes\n", inMsg.GetRemainingData() );
  171.     }
  172. }
  173. #endif
  174.  
  175. // end
  176.