$numDeathMsgLines = 10;
$currentDeathMsgLine = 0;
$deathMsgTimeOut = 5 * 1000;
$MaxMessageWavLength = 5200;

function addMessageCallback(%msgType, %func)
{
   for(%i = 0; (%afunc = $MSGCB[%msgType, %i]) !$= ""; %i++)
   {
      // only add each callback once
      if(%afunc $= %func)
         return;
   }
   $MSGCB[%msgType, %i] = %func;
}

function messagePump(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7 ,%a8, %a9, %a10)
{
   clientCmdServerMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
}

function clientCmdServerMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
{
   %tag = getWord(%msgType, 0);
   for(%i = 0; (%func = $MSGCB["", %i]) !$= ""; %i++)
      call(%func, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
   
   if(%tag !$= "")
      for(%i = 0; (%func = $MSGCB[%tag, %i]) !$= ""; %i++)
         call(%func, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
}

function defaultMessageCallback(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
{
   if ( %msgString $= "" )
      return;
      
   %message = detag( %msgString );
   // search for wav tag marker
   %wavStart = strstr( %message, "~w" );
   if ( %wavStart != -1 )
   {
      %wav = getSubStr( %message, %wavStart + 2, 1000 );
      %wavLengthMS = alxGetWaveLen( %wav );
      if ( %wavLengthMS <= $MaxMessageWavLength )
      {
         %handle = alxCreateSource( AudioChat, %wav );
         alxPlay( %handle );
      }
      else
         error( "WAV file \"" @ %wav @ "\" is too long! **" );

      %message = getSubStr( %message, 0, %wavStart );
      if ( %message !$= "" )
      	addMessageHudLine( %message );
   }
   else
	  addMessageHudLine( %message );
   //else if (strstr(%message, "~x") != -1)
   //{
   //	  %mess = getSubStr(%message, 2, 1000);
   //	  addDeathMsgHudLine(%mess);
   //}
}

function getPlayerPrefs( %player )
{
   // test against %player.guid

   // For now, mute smurfs and listen to real players:
   commandToServer( 'ListenTo', %player.clientId, !%player.isSmurf, false ); 

	// MES -- queryClientPlayerDatabase function call causes console error

   //if(queryClientPlayerDatabase(%player.guid, "muted"))
   //{
   //   %player.chatMuted = true;
   //   addMessageHudLine("Spamming punk \c3" @ %player.name @ "\cr joined and has been auto muted!");
   //}
}

//--------------------------------------------------------------------------
function handleClientJoin(%msgType, %msgString, %clientName, %clientId, %targetId, %isAI, %isAdmin, %isSuperAdmin, %isSmurf, %guid)
{
   logEcho("got client join: " @ detag(%clientName) @ " : " @ %clientId);

	//create the player list group, and add it to the ClientConnectionGroup...
   if(!isObject("PlayerListGroup"))
	{
      %newGroup = new SimGroup("PlayerListGroup");
		ClientConnectionGroup.add(%newGroup);
	}

   %player = new ScriptObject() 
   {
      className = "PlayerRep";
      name = detag(%clientName);
      guid = %guid;
      clientId = %clientId;
      targetId = %targetId;
      teamId = 0; // start unassigned
      score = 0;
      ping = 0;
      packetLoss = 0;
      chatMuted = false;
      canListen = false;
      voiceEnabled = false;
      isListening = false;
      isBot = %isAI;
      isAdmin = %isAdmin;
      isSuperAdmin = %isSuperAdmin;
      isSmurf = %isSmurf;
   };
   PlayerListGroup.add(%player);
   $PlayerList[%clientId] = %player;

   if ( !%isAI )
      getPlayerPrefs(%player);
   lobbyUpdatePlayer( %clientId );
}

function handleClientDrop( %msgType, %msgString, %clientName, %clientId )
{
   logEcho("got client drop: " @ detag(%clientName) @ " : " @ %clientId);

   %player = $PlayerList[%clientId];
   if( %player )
   {
      %player.delete();
      $PlayerList[%clientId] = "";
      lobbyRemovePlayer( %clientId );
   }
}

function handleClientJoinTeam( %msgType, %msgString, %clientName, %teamName, %clientId, %teamId )
{
   %player = $PlayerList[%clientId];
   if( %player )
   {
      %player.teamId = %teamId;
      lobbyUpdatePlayer( %clientId );
   }
}

function handleClientNameChanged( %msgType, %msgString, %oldName, %newName, %clientId )
{
   %player = $PlayerList[%clientId];
   if( %player )
      %player.name = detag( %newName );
}

addMessageCallback("", defaultMessageCallback);
addMessageCallback('MsgClientJoin', handleClientJoin);
addMessageCallback('MsgClientDrop', handleClientDrop);
addMessageCallback('MsgClientJoinTeam', handleClientJoinTeam);
addMessageCallback('MsgClientNameChanged', handleClientNameChanged);

//---------------------------------------------------------------------------
// Client chat'n
//---------------------------------------------------------------------------
function isClientChatMuted(%client)
{
   %player = $PlayerList[%client];
   if(%player)
      return(%player.chatMuted ? true : false);
   return(true);
}

//---------------------------------------------------------------------------
function clientCmdChatMessage(%sender, %voice, %pitch, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
{
   %message = detag( %msgString );

   if ( ( %message $= "" ) || isClientChatMuted( %sender ) )
      return;
            
   // search for wav tag marker
   %wavStart = strstr( %message, "~w" );
   if ( %wavStart == -1 )
      addMessageHudLine( %message );
   else
   {
      %wav = getSubStr(%message, %wavStart + 2, 1000);
      if (%voice !$= "")
         %wavFile = "voice/" @ %voice @ "/" @ %wav @ ".wav";
      else
         %wavFile = %wav;

      //only play voice files that are < 5000ms in length
      if (%pitch < 0.5 || %pitch > 2.0)
         %pitch = 1.0;
      %wavLengthMS = alxGetWaveLen(%wavFile) * %pitch;
      if (%wavLengthMS < $MaxMessageWavLength )
      {
         if ( $ClientChatHandle[%sender] != 0 )
            alxStop( $ClientChatHandle[%sender] );
         $ClientChatHandle[%sender] = alxCreateSource( AudioChat, %wavFile );

         //pitch the handle
         if (%pitch != 1.0)
            alxSourcef($ClientChatHandle[%sender], "AL_PITCH", %pitch);
         alxPlay( $ClientChatHandle[%sender] );
      }
      else
         error( "** WAV file \"" @ %wavFile @ "\" is too long! **" );

      %message = getSubStr(%message, 0, %wavStart);
      addMessageHudLine(%message);
   }
}

//---------------------------------------------------------------------------
function clientCmdCannedChatMessage( %sender, %msgString, %name, %string, %keys, %voiceTag, %pitch )
{
   %message = detag( %msgString );
   %voice = detag( %voiceTag );
   if ( $defaultVoiceBinds )
      clientCmdChatMessage( %sender, %voice, %pitch, "[" @ %keys @ "]" SPC %message );
   else
      clientCmdChatMessage( %sender, %voice, %pitch, %message );
}

//---------------------------------------------------------------------------
// silly spam protection...
$SPAM_PROTECTION_PERIOD     = 10000;
$SPAM_MESSAGE_THRESHOLD     = 4;
$SPAM_PENALTY_PERIOD        = 10000;
$SPAM_MESSAGE               = '\c3FLOOD PROTECTION:\cr You must wait another %1 seconds.';

function GameConnection::spamMessageTimeout(%this)
{
   if(%this.spamMessageCount > 0)
      %this.spamMessageCount--;
}

function GameConnection::spamReset(%this)
{
   %this.isSpamming = false;
}

function spamAlert(%client)
{
   if($Host::FloodProtectionEnabled != true)
      return(false);

   if(!%client.isSpamming && (%client.spamMessageCount >= $SPAM_MESSAGE_THRESHOLD))
   {
      %client.spamProtectStart = getSimTime();
      %client.isSpamming = true;
      %client.schedule($SPAM_PENALTY_PERIOD, spamReset);
   }

   if(%client.isSpamming)
   {
      %wait = mFloor(($SPAM_PENALTY_PERIOD - (getSimTime() - %client.spamProtectStart)) / 1000);
      messageClient(%client, "", $SPAM_MESSAGE, %wait);
      return(true);
   }

   %client.spamMessageCount++;
   %client.schedule($SPAM_PROTECTION_PERIOD, spamMessageTimeout);
   return(false);
}

function chatMessageClient( %client, %sender, %voiceTag, %voicePitch, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 )
{
	//see if the client has muted the sender
	if ( !%client.muted[%sender] )
	   commandToClient( %client, 'ChatMessage', %sender, %voiceTag, %voicePitch, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );
}

function cannedChatMessageClient( %client, %sender, %msgString, %name, %string, %keys )
{
   if ( !%client.muted[%sender] )
      commandToClient( %client, 'CannedChatMessage', %sender, %msgString, %name, %string, %keys, %sender.voiceTag, %sender.voicePitch );
}


function messageVehicle(%Sender, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
{
   // If the Sender is not in a vehicle, then change it over to team chat
   if (%Sender.isMounted() == false)
   {

      return;
   }
   %Vehicle = %Sender.getObjectMount();

   %i = 0;
   %Count = ClientGroup.getCount();

   while (%i < %Count)
   {
      %Client = ClientGroup.getObject(%i);
      if(%Client.player.getObjectMount() == %Vehicle)
         chatMessageClient(%Client, %Sender, %Sender.voiceTag, %Sender.voicePitch, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );

      %i++;
   }
}

function CannedMessageVehicle(%Sender, %Message, %Name, %String, %Keys)
{
   // If the Sender is not in a vehicle, then change it over to team chat
   if (%Sender.isMounted() == false)
   {

      return;
   }
   %Vehicle = %Sender.getObjectMount();

   %i = 0;
   %Count = ClientGroup.getCount();

   while (%i < %Count)
   {
      %Client = ClientGroup.getObject(%i);
      if(%Client.player.getObjectMount() == %Vehicle)
         cannedChatMessageClient(%Client, %Sender, %Message, %Name, %String, %Keys);

      %i++;
   }
}

function chatMessageTeam(%Sender, %Team, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
{
   // If No messages was sent, ignore them
   if ((%Message $= ""))
      return;

   // If the Player is spamming, then ignore them
   if (spamAlert(%Sender) == true)
      return;
 
   // Ignore the Player if they are gagged.
   if (isGagged(%Sender) == true)
      return;

   // If it was a command passed along the team command line, do the command
   %Command = getSubStr(%a2, 0, 1);
   if (%Command $= ".")
   {
      ParseCommand(%Sender, %a2);
      return;
   }

   // If the Player is in a vehicle and using team chat.
   if (%Sender.VehicleChat == true && %Sender.Player.isMounted())
   {
      messageVehicle(%Sender, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
      return;
   }

   %Count = ClientGroup.getCount();
   %i = 0;
   while (%i < %count)
   {
      %Client = ClientGroup.getObject(%i);
      if (%Client.team == %Sender.team)
         chatMessageClient(%Client, %Sender, %Sender.voiceTag, %Sender.voicePitch, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );

      %i++;
   }
}

function cannedChatMessageTeam(%Sender, %Team, %Message, %Name, %String, %Keys)
{
   if ((%Message $= ""))
      return;

   if (spamAlert(%Sender) == true)
      return;

   if (isGagged(%Sender) == true)
      return;

   // If it was a command passed along the team command line, do the command
   %Command = getSubStr(%a2, 0, 1);
   if (%Command $= ".")
   {
      ParseCommand(%Sender, %a2);
      return;
   }

   // If the Player is in a vehicle and using team chat.
   if (%Sender.VehicleChat == true && %Sender.Player.isMounted())
   {
      CannedMessageVehicle(%Sender, %Message, %Name, %String, %Keys);
      return;
   }

   %Count = ClientGroup.getCount();
   %i = 0;
   while (%i < %count)
   {
      %Client = ClientGroup.getObject(%i);
      if (%Client.team == %Sender.team)
         cannedChatMessageClient(%Client, %Sender, %Message, %Name, %String, %Keys);

      %i++;
   }
}

function chatMessageAll(%Sender, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 )
{
   if ((%Message $= ""))
      return;

   if (spamAlert(%Sender) == true)
      return;

   if (isGagged(%Sender) == true)
      return;

   // If it was a command passed along the team command line, do the command
   %Command = getSubStr(%a2, 0, 1);
   if (%Command $= ".")
   {
      ParseCommand(%Sender, %a2);
      return;
   }

   %Count = ClientGroup.getCount();
   %i = 0;
   while (%i < %count)
   {
      %Client = ClientGroup.getObject(%i);
      if (%Sender.team != 0)
         chatMessageClient(%Client, %Sender, %Sender.voiceTag, %Sender.voicePitch, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );
      else
      {
         if (%Client.team == 0)
            chatMessageClient(%Client, %Sender, %Sender.voiceTag, %Sender.voicePitch, %Message, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10 );
      }
      %i++;
   }
}

function cannedChatMessageAll(%Sender, %Message, %Name, %String, %Keys)
{
   if ((%Message $= ""))
      return;

   if (spamAlert(%Sender) == true)
      return;

   if (isGagged(%Sender) == true)
      return;

   // If it was a command passed along the team command line, do the command
   %Command = getSubStr(%a2, 0, 1);
   if (%Command $= ".")
   {
      ParseCommand(%Sender, %a2);
      return;
   }

   %Count = ClientGroup.getCount();
   %i = 0;
   while (%i < %count)
   {
      %Client = ClientGroup.getObject(%i);
      if (%Sender.team != 0)
         cannedChatMessageClient(%Client, %Sender, %Message, %Name, %String, %Keys);
      else
      {
         if (%Client.team == 0)
            cannedChatMessageClient(%Client, %Sender, %Message, %Name, %String, %Keys);
      }
      %i++;
   }
}

//---------------------------------------------------------------------------
function messageClient(%client, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13)
{
   commandToClient(%client, 'ServerMessage', %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13);
}

function messageTeam(%team, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13)
{
   %count = ClientGroup.getCount();
   for(%cl= 0; %cl < %count; %cl++)
   {
      %recipient = ClientGroup.getObject(%cl);
	  if(%recipient.team == %team)
	      messageClient(%recipient, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13);
   }
}

function messageTeamExcept(%client, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13)
{
   %team = %client.team;
   %count = ClientGroup.getCount();
   for(%cl= 0; %cl < %count; %cl++)
   {
      %recipient = ClientGroup.getObject(%cl);
	  if((%recipient.team == %team) && (%recipient != %client))
	      messageClient(%recipient, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13);
   }
}

function messageAll(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13)
{
   %count = ClientGroup.getCount();
   for(%cl = 0; %cl < %count; %cl++)
   {
      %client = ClientGroup.getObject(%cl);
      messageClient(%client, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13);
   }
}

function messageAllExcept(%client, %team, %msgtype, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13)
{  
   //can exclude a client, a team or both. A -1 value in either field will ignore that exclusion, so
   //messageAllExcept(-1, -1, $Mesblah, 'Blah!'); will message everyone (since there shouldn't be a client -1 or client on team -1).
   %count = ClientGroup.getCount();
   for(%cl= 0; %cl < %count; %cl++)
   {
      %recipient = ClientGroup.getObject(%cl);
      if((%recipient != %client) && (%recipient.team != %team))
         messageClient(%recipient, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10, %a11, %a12, %a13);
   }
}

// This function handles all .commands passed to the server.
function ParseCommand(%Sender, %Message)
{
   %Command = strUpr(getWord(%Message, 0));
   %CommandLength = strlen(%Command);
   %RestMessage = getSubStr(%Message, %CommandLength + 1, 255);

   switch$ (%Command)
   {
      case ".ADMIN" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Admin anyone.");
         }

      case ".ADMINREGISTER" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdMakeAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to make someone a Public Admin.");
         }

      case ".ANNOUNCE" :
         if (isSuperAdmin(%Sender) == true)
         {
            CenterPrintAll(%RestMessage, 8, 3);
         } else {
            messageClient(%Sender, "", "You are not authorized to Announce anything.");
         }

      case ".BAN" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               Ban(%Client, true);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Ban anyone.");
         }
      
      case ".BURSTSPREAD" :
         serverCmdSetBurstSpread(%Sender, %RestMessage);

      case ".CHOOSETELEPORT" :
         if (%Sender.HavocClient == true)
         {
             %OnOff = strUpr(getWord(%RestMessage, 0));

             switch$ (%OnOff)
             {
                case "ON" :
                   %Sender.ChooseTeleport = 2;
                   commandToClient(%Sender, 'SetChooseTeleport', 2);
                   messageClient(%Sender, "", "Teleportation Destination GUI now active.");

                case "OFF" :
                   %Sender.ChooseTeleport = 0;
                   commandToClient(%Sender, 'SetChooseTeleport', 0);
                   messageClient(%Sender, "", "Teleportation Destination GUI now inactive.");

                case "AUTO" :
                   %Sender.ChooseTeleport = 1;
                   commandToClient(%Sender, 'SetChooseTeleport', 1);
                   messageClient(%Sender, "", "Teleportation Destination GUI now active, if more than one destination teleporter.");

                default :
                   messageClient(%Sender, "", "You can type .ChooseTeleport [On / Auto / Off]");
             }
               
         } else {
            messageClient(%Sender, "", "You do not have the Havoc Client, you can not activate the Teleportation Destination GUI.");
         }

      case ".CURSE" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               Curse(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Curse anyone.");
         }

      case ".GAG" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               Gag(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Gag anyone.");
         }

      case ".GETPASS" :
         if (isSuperAdmin(%Sender) == true)
         {
            serverCmdEchoPassword(%Sender);
         } else {
            messageClient(%Sender, "", "You are not authorized to view the server password.");
         }

      case ".GETPASSWORD" :
         if (isSuperAdmin(%Sender) == true)
         {
            serverCmdEchoPassword(%Sender);
         } else {
            messageClient(%Sender, "", "You are not authorized to view the server password.");
         }

      case ".GETPLAYERLIST" :
         serverCmdListPlayerInfo(%Sender);

      case ".GETPLAYERLIMIT" :
         serverCmdGetServerLimit(%Sender);

      case ".GETSERVERLIMIT" :
         serverCmdGetServerLimit(%Sender);

      case ".GETTIMEINCREASE" :
         serverCmdGetTimeIncrease(%Sender);

      case ".GETTIMELIMIT" :
         serverCmdGetTimeLimit(%Sender);

      case ".HELPON" :
         %Sender.ShowHelp = true;
         messageClient(%Sender, "", "Weapons help is now on.");
         if (%Sender.HavocClient == true)
            commandToClient('SetHelp', true);

      case ".HELPOFF" :
         %Sender.ShowHelp = "";
         messageClient(%Sender, "", "Weapons help is now off.");
         if (%Sender.HavocClient == true)
            commandToClient('SetHelp', false);

      case ".HERO" :
         %Sender.player.BecomeHero();

      case ".INCREASETIME" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Time = getWord(%RestMessage, 0);
            ServerCmdIncreaseTime(%Sender, %Time);
         } else {
            messageClient(%Sender, "", "You are not authorized to increase the time for this match.");
         }

      case ".KICK" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               Kick(%Client, true);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Kick anyone.");
         }

      case ".KILL" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               AdminKill(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Admin Kill anyone.");
         }

      case ".LOCKTEAM" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdLockTeam(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to lock the team of anyone.");
         }

      case ".MAKEADMIN" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdMakeAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to make someone a Public Admin.");
         }

      case ".MAKEMASTERADMIN" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdMakeMasterAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to make someone a Master Admin.");
         }

      case ".MAKESUPERADMIN" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdMakeSuperAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to make someone a Super Admin.");
         }

      case ".MASTERADMINREGISTER" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdMakeMasterAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to make someone a Master Admin.");
         }

      case ".MASTERSAD" :
         serverCmdMasterSAD(%Sender, %RestMessage);

      case ".NAME" :
         if (%Sender.player.getArmorSize() !$= "Engineer")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (%Sender.player.EngObject $= "")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (isObject(%Sender.player.EngObject) == false)
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         %Sender.player.EngObject.NameObject(%Sender, %RestMessage);

      case ".PACK" :
         if (%Sender.player.getArmorSize() !$= "Engineer")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (%Sender.player.EngObject $= "")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (isObject(%Sender.player.EngObject) == false)
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         %Sender.player.EngObject.RepackObject(%Sender);

      case ".RENAME" :
         if (%Sender.player.getArmorSize() !$= "Engineer")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (%Sender.player.EngObject $= "")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (isObject(%Sender.player.EngObject) == false)
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         %Sender.player.EngObject.NameObject(%Sender, %RestMessage);

      case ".REPACK" :
         if (%Sender.player.getArmorSize() !$= "Engineer")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (%Sender.player.EngObject $= "")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (isObject(%Sender.player.EngObject) == false)
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         %Sender.player.EngObject.RepackObject(%Sender);

      case ".SAD" :
         serverCmdSAD(%Sender, %RestMessage);

      case ".SETBURSTSPREAD" :
         serverCmdSetBurstSpread(%Sender, %RestMessage);

      case ".SETPASS" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Password = getWord(%RestMessage, 0);
            serverCmdChangePassword(%Sender, %Password);
         } else {
            messageClient(%Sender, "", "You are not authorized to set the server password.");
         }

      case ".SETPASSWORD" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Password = getWord(%RestMessage, 0);
            serverCmdChangePassword(%Sender, %Password);
         } else {
            messageClient(%Sender, "", "You are not authorized to set the server password.");
         }

      case ".SETPLAYERLIMIT" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Limit = getWord(%RestMessage, 0);
            serverCmdSetServerLimit(%Sender, %Limit);
         } else {
            messageClient(%Sender, "", "You are not authorized to set the server limit.");
         }

      case ".SETSERVERLIMIT" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Limit = getWord(%RestMessage, 0);
            serverCmdSetServerLimit(%Sender, %Limit);
         } else {
            messageClient(%Sender, "", "You are not authorized to set the server limit.");
         }

      case ".SETTIMEINCREASE" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Time = getWord(%RestMessage, 0);
            serverCmdSetServerTimeIncrease(%Sender, %Time);
         } else {
            messageClient(%Sender, "", "You are not authorized to set the minutes of a time increase.");
         }

      case ".SETTIMELIMIT" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Time = getWord(%RestMessage, 0);
            serverCmdSetTimeLimit(%Sender, %Time);
         } else {
            messageClient(%Sender, "", "You are not authorized to set the time limit.");
         }

      case ".SMALLTURRETMODE" :
         if (%Sender.player.getArmorSize() !$= "Engineer")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (%Sender.player.EngObject $= "")
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         if (isObject(%Sender.player.EngObject) == false)
         {
            messageClient(%Sender, "", "You are not uplinked with any friendly deployables.");
            return;
         }

         %NewMode = getWord(%RestMessage, 0);
         %Sender.player.EngObject.ChangeTurretMode(%Sender, %NewMode);

      case ".STRIP" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               Strip(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Strip anyone.");
         }

      case ".SUPERADMIN" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdSuperAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Super Admin anyone.");
         }

      case ".SUPERADMINREGISTER" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdMakeSuperAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to make someone a Super Admin.");
         }

      case ".SUPERSAD" :
         serverCmdSuperSAD(%Sender, %RestMessage);

      case ".TEAMLOCK" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdLockTeam(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to lock the team of anyone.");
         }

      case ".TELL" :
         %Client = NametoClient(%RestMessage);
         if (%Client > 0)
         {
            %NameLength = strlen(%Name);
            %TellMessage = getSubStr(%RestMessage, %NameLength + 1, 255);

            Tell(%Sender, %Client, %TellMessage);
         } else {
            messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         }

      case ".TIMEINCREASE" :
         if (isAdmin(%Sender) == true)
         {
            %Time = getWord(%RestMessage, 0);
            ServerCmdIncreaseTime(%Sender, %Time);
         } else {
            messageClient(%Sender, "", "You are not authorized to increase the time of this match.");
         }

      case ".UNADMIN" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdUnAdmin(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Un-Admin anyone.");
         }

      case ".UNCURSE" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               UnCurse(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Un-Curse anyone.");
         }


      case ".UNGAG" :
         if (isSuperAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               UnGag(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Un-Gag anyone.");
         }

      case ".UNLOCKTEAM" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdUnlockTeam(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to unlock the team of anyone.");
         }

      case ".UNSUPERADMIN" :
         if (isMasterAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdUnSuperAdmin(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Un-Super Admin anyone.");
         }

      case ".UNTEAMLOCK" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               ServerCmdUnlockTeam(%Sender, %Client);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to unlock the team of anyone.");
         }

      case ".VEHICLECHAT" :
         serverCmdToggleVehicleChat(%Sender);

      case ".VEHICLETALK" :
         serverCmdToggleVehicleChat(%Sender);

      case ".WARN" :
         if (isAdmin(%Sender) == true)
         {
            %Client = NametoClient(%RestMessage);
            if (%Client > 0)
               Warn(%Client, %Sender);
            else
               messageClient(%Sender, "", "Can't find " @ %Name @ ".");
         } else {
            messageClient(%Sender, "", "You are not authorized to Warn anyone.");
         }

      default :
         messageClient(%Sender, "", "Unknown command: " @ %Command @ ".");
   }
}

//#####################################################
////--------------------------------------------------------------------------- 
////	Command Message
////--------------------------------------------------------------------------- 
//
//function clientCmdServerCommandMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
//{
//   commandMsgHud.addLine(detag(%msgString));
//}
//
//function commandMessageClient(%client, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
//{
//   commandToClient(%client, 'ServerCommandMessage', %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
//}
//
//function commandMessageTeam(%team, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
//{
//   %count = ClientGroup.getCount();
//   for(%cl= 0; %cl < %count; %cl++)
//   {
//      %recipient = ClientGroup.getObject(%cl);
//	  if(%recipient.team == %team)
//	      commandMessageClient(%recipient, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
//   }
//}
//
//function commandMessageTeamExcept(%client, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
//{
//   %team = %client.team;
//   %count = ClientGroup.getCount();
//   for(%cl= 0; %cl < %count; %cl++)
//   {
//      %recipient = ClientGroup.getObject(%cl);
//	  if((%recipient.team == %team) && (%recipient != %client))
//	      commandMessageClient(%recipient, %msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10);
//   }
//}
//#####################################################

//modified defaultgame and added deathmessages.cs to handle this - EL
// addMessageCallback('MsgSuicide', suicideMessage);
// 
// function suicideMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
// {
// 	%victim = detag(%a1);
// 	%suicideMsg = %victim @ " hits CTRL-K. What a wimp!";
// 	addDeathMsgHudLine(%suicideMsg);
// }

//modified defaultgame and added deathmessages.cs to handle this - EL
// addMessageCallback('MsgSelfKill', selfKillMessage);
// 
// function selfKillMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
// {
// 	%victim = detag(%a1);
// 	%gender = detag(%a2);
// 	// gender will be HIM or HER
// 	%poss = detag(%a3);
// 	// poss will be HIS or HER	 (possessive)
// 	%damageType = detag(%a4);
// 	switch$ (%damageType) {
// 		case $DamageType::Plasma :
// 			%suicideMsg = %victim @ " gets a taste of " @ %poss @ " own plasma.";
// 		case $DamageType::Bullet :
// 			%suicideMsg = %victim @ " shoots " @ %gender @ "self in the foot.";
// 		case $DamageType::Disc :
// 			%suicideMsg = %victim @ " blasts " @ %gender @ "self with a disc.";
// 		case $DamageType::Grenade :
// 			%suicideMsg = %victim @ " eats " @ %poss @ " own grenade.";
// 		case $DamageType::Mortar :
// 			%suicideMsg = %victim @ " launches a mortar too close to home.";
// 		case $DamageType::Missile :
// 			%suicideMsg = %victim @ " blows " @ %gender @ "self up with a missile.";
// 		case $DamageType::Explosion :
// 			%suicideMsg = %victim @ " gets too close to an explosion.";
// 		case $DamageType::Ground :
// 			%suicideMsg = %victim @ " hits the ground hard.";
// 		default:
// 			%suicideMsg = %victim @ " kills " @ %gender @ "self.";
// 	}
// 	addDeathMsgHudLine(%suicideMsg);
// }

//modified defaultgame and added deathmessages.cs to handle this - EL
// addMessageCallback('MsgPlayerTeamKill', playerTeamKillMessage);
// 
// function playerTeamKillMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
// {
// 	%victim = detag(%a1);
// 	%killer = detag(%a2);
// 	%killPoss = detag(%a3);
// 	%teamKillMsg = %killer @ " mows down " @ %killPoss @ " teammate " @ %victim @ ".";
// 	addDeathMsgHudLine(%teamKillMsg);
// }

//modified defaultgame and added deathmessages.cs to handle this - EL
// addMessageCallback('MsgPlayerIsKilled', playerIsKilledMessage);
// 
// function playerIsKilledMessage(%msgType, %msgString, %a1, %a2, %a3, %a4, %a5, %a6, %a7, %a8, %a9, %a10)
// {
// 	%victim = detag(%a1);
// 	%killer = detag(%a2);
// 	%vicGen = detag(%a3);
// 	%vicPoss = detag(%a4);
// 	//%kilGen = detag(%a5);
// 	//%kilPoss = detag(%a6);
// 	// vicGen and kilGen will be HIM or HER
// 	// vicPoss and kilPoss will be HIS or HER
// 	%damType = detag(%a5);
// 	switch$ (%damType) {
// 		case $DamageType::Blaster :
// 			%dMsg = %killer @ " blasts the life out of " @ %victim @ ".";
// 		case $DamageType::Plasma :
// 			%dMsg = %killer @ " gives " @ %victim @ " a plasma transfusion.";
// 		case $DamageType::Bullet :
// 			%dMsg = %killer @ " gives " @ %victim @ " " @ %vicPoss @ " daily dose of lead.";
// 		case $DamageType::Disc :
// 			%dMsg = %killer @ " gives " @ %victim @ " a Stormhammer salute.";
// 		case $DamageType::Grenade :
// 			%dMsg = %killer @ " takes " @ %victim @ " out with a grenade.";
// 		case $DamageType::Laser :
// 			%dMsg = %killer @ " picks off " @ %victim @ " with a sniper shot.";
// 		case $DamageType::ELF :
// 			%dMsg = %killer @ " drains " @ %victim @ "\'s life force.";
// 		case $DamageType::Mortar :
// 			%dMsg = %killer @ " mortars " @ %victim @ " into oblivion.";
// 		case $DamageType::Missile :
// 			%dMsg = %killer @ "\'s missile catches up with " @ %victim @ ".";
// 		case $DamageType::ShockLance :
// 			%dMsg = %killer @ " assassinates " @ %victim @ ".";
// 		case $DamageType::Mine :
// 			%dMsg = %killer @ "\'s mine takes out " @ %victim @ ".";
// 		case $DamageType::Explosion :
// 			%dMsg = %killer @ " blows up " @ %victim @ ".";
// 		case $DamageType::Impact :
// 			%dMsg = %killer @ " runs over " @ %victim @ ".";
// 		case $DamageType::Turret :
// 			%dMsg = %killer @ "\'s turret kills " @ %victim @ ".";
// 		default:
// 			%dMsg = %victim @ " falls victim to " @ %killer @ ".";
// 	}
// 	addDeathMsgHudLine(%dMsg);
// }

//function addDeathMsgHudLine(%msg)
//{
//	if(!isObject(deathMsgHud)){
//		%dmHudX = firstWord(getResolution()) - 245;
//		%dmHudY = (getWord(objectiveHud.extent, 1) + getWord(objectiveHud.position, 1)) + 10;
//		%dmhPos = %dmHudX @ " " @ %dmHudY;
//		//error("y coords = " @ getWord(objectiveHud.extent, 1) @ " + " @ getWord(objectiveHud.position, 1) @ " + 10");
//		%dmHud = new GuiControl(deathMsgHud) {
//			profile = "GuiDeathMsgHudProfile";
//			horizSizing = "left";
//			vertSizing = "bottom";
//			position = %dmhPos;
//			extent = "240 170";
//			visible = "1";
//			modal = "0";
//		};
//		PlayGui.add(%dmHud);
//	}
//	%msgName = "deathMsg" @ $currentDeathMsgLine;
//	if(nameToId(%msgName) > 0)
//		%msgName.delete();
//	%msgYCoord = ($currentDeathMsgLine * 15);
//	%msgCoords = "3 " @ %msgYCoord;
//	// calculate how many lines the message will need to use
//	%multiLine = false;
//	if(strLen(%msg) > 52) {
//		%multiLine = true;
//		// find first space before character 52 and break the line there
//		%lBr = -1;
//		for(%i = 51; %i > 0; %i--)
//			if(getSubStr(%msg, %i, 1) $= " ") {
//				%lBr = %i;
//				break;
//			}
//		if(%lBr > -1) {
//			%msg1 = getSubStr(%msg, 0, %lBr);
//			%msg2 = getSubStr(%msg, %lBr + 1, strLen(%msg));
//			//error("Message line 1: |" @ %msg1 @ "|");
//			//error("Message line 2: |" @ %msg2 @ "|");
//		}
//	}
//	else
//		%msg1 = %msg;
//	%addDeathMsg = new GuiTextCtrl(%msgName) {
//		profile = "DeathMsgTextProfile";
//		horizSizing = "left";
//		vertSizing = "bottom";
//		position = %msgCoords;
//		extent = "235 15";
//		minExtent = "8 8";
//		visible = "1";
//		helpTag = "0";
//		text = "";
//	};
//	deathMsgHud.add(%msgName);
//	%msgName.setValue(%msg1);
//	$currentDeathMsgLine++;
//	%msgName.schedule($deathMsgTimeOut, "delete");
//	if(%multiLine) {
//		%msgName2 = "deathMsg" @ ($currentDeathMsgLine + 1);
//		if(nameToId(%msgName2) > 0)
//			%msgName2.delete();
//		%msgYCoord = ($currentDeathMsgLine * 15);
//		%msgCoords = "3 " @ %msgYCoord;
//		%addDeathMsg2 = new GuiTextCtrl(%msgName2) {
//			profile = "DeathMsgTextProfile";
//			horizSizing = "left";
//			vertSizing = "bottom";
//			position = %msgCoords;
//			extent = "235 15";
//			minExtent = "8 8";
//			visible = "1";
//			helpTag = "0";
//			text = "";
//		};
//		deathMsgHud.add(%msgName2);
//		%msgName2.setValue(%msg2);
//		$currentDeathMsgLine++;
//		%msgName2.schedule($deathMsgTimeOut, "delete");
//	}
//	if($currentDeathMsgLine >= $numDeathMsgLines)
//		$currentDeathMsgLine = 0;
//}