/*********************************************************
 * Allow chatting during pause - Version 0.8             *
 *********************************************************
 *                                                       *
 * Name: plugin_rindy_pausechat                          *
 * Author: Rinde (rinde@fiatnox.de)                      *
 * Released: 11/11/03                                    *
 *                                                       *
 *                                                       *
 * Commands:                                             *
 *                                                       *
 * admin_pause: Pauses and unpauses game. When the game  *
 *              is paused by this command, you can chat  *
 *              and team-chat during pause.              *
 *              The "pausable" cvar is set to 1 for      *
 *              command execution and back afterwards.   *
 *                                                       *
 * Changelog:                                            *
 *                                                       *
 * Version 0.8                                           *
 *                                                       *
 *  -  Initial release                                   *
 *                                                       *
 *********************************************************/
 
/* Includes */
#include <adminlib>
#include <plugin>
 
new g_Version[] = "0.8";
new g_Pause = 0;
new g_maxplayers;
 
/* Function Declarations */
forward HandleSay(HLCommand,HLData,HLUserName,UserIndex);
forward HandleTeamSay(HLCommand,HLData,HLUserName,UserIndex);
forward PauseLoopback(HLCommand,HLData,HLUserName,UserIndex);
forward Pause(HLCommand,HLData,HLUserName,UserIndex);
 
/* Event Handlers */
public plugin_init() {
    plugin_registerinfo("Rinde's Pause Chat Plugin","Allows to chat when game is paused",g_Version);
    if(getvar("allow_client_exec") == 0) {
//        plugin_setdesc("ERROR, check logfile for details");
        return PLUGIN_CONTINUE;
    }
    plugin_registercmd("say","HandleSay",ACCESS_ALL);
    plugin_registercmd("say_team","HandleTeamSay",ACCESS_ALL);
    plugin_registercmd("pause_loopback","PauseLoopback",ACCESS_ALL,"");
    plugin_registercmd("admin_pause","Pause",ACCESS_BAN,"");
    g_maxplayers = min(maxplayercount(),32);
    return PLUGIN_CONTINUE;
}
 
/* Command Handlers */
public HandleSay(HLCommand,HLData,HLUserName,UserIndex) {
    if(g_Pause) {
        static Data[MAX_DATA_LENGTH];
        static User[MAX_NAME_LENGTH];
        new Dead,ListenDead;
        new i;
        convert_string(HLData,Data,MAX_DATA_LENGTH);
        strstripquotes(Data);
        if(Data[0] == 0) {
            return PLUGIN_HANDLED;
        }
        playerinfo(UserIndex,User,MAX_NAME_LENGTH,_,_,_,Dead);
        if(Dead) {
            snprintf(Data,MAX_DATA_LENGTH,"*DEAD*%s :    %s",User,Data);
        } else {
            snprintf(Data,MAX_DATA_LENGTH,"%s :    %s",User,Data);
        }
        for(i=1;i<=g_maxplayers;i++) {
            if(playerinfo(i,User,MAX_NAME_LENGTH,_,_,_,ListenDead) && Dead == ListenDead) {
                messageex(User,Data,print_chat);
            }
        }
        return PLUGIN_HANDLED;
    }
    return PLUGIN_CONTINUE;
}
 
public HandleTeamSay(HLCommand,HLData,HLUserName,UserIndex) {
    if(g_Pause) {
        new Data[MAX_DATA_LENGTH];
        new User[MAX_NAME_LENGTH];
        new Team,ListenTeam;
        new Dead,ListenDead;
        new i;
        convert_string(HLData,Data,MAX_DATA_LENGTH);
        strstripquotes(Data);
        if(Data[0] == 0) {
            return PLUGIN_HANDLED;
        }
        playerinfo(UserIndex,User,MAX_NAME_LENGTH,_,_,Team,Dead);
        switch((Team<<1) & Dead) {
            case 0: snprintf(Data,MAX_DATA_LENGTH,"(Terrorist) %s :   %s",User,Data);
            case 1: snprintf(Data,MAX_DATA_LENGTH,"*DEAD*(Terrorist) %s :   %s",User,Data);
            case 2: snprintf(Data,MAX_DATA_LENGTH,"(Counter-Terrorist) %s :   %s",User,Data);
            case 3: snprintf(Data,MAX_DATA_LENGTH,"*DEAD*(CT) %s :   %s",User,Data);
        }
        for(i=1;i<=g_maxplayers;i++) {
            if(playerinfo(i,User,MAX_NAME_LENGTH,_,_,ListenTeam,ListenDead) && Team == ListenTeam && Dead == ListenDead) {
                messageex(User,Data,print_chat);
            }
        }
        return PLUGIN_HANDLED;
    }
    return PLUGIN_CONTINUE;
}
 
public PauseLoopback(HLCommand,HLData,HLUserName,UserIndex) {
    if(getvar("pausable")) {
        g_Pause = ~g_Pause;
        setstrvar("pausable","0");
    }
    return PLUGIN_HANDLED;
}
 
public Pause(HLCommand,HLData,HLUserName,UserIndex) {
    new UserName[MAX_NAME_LENGTH];
    convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
    execclient(UserName,"pause;pause_loopback");
    setstrvar("pausable","1");
    if(g_Pause) {
        selfmessage("Unpaused game.");
    } else {
        selfmessage("Paused game.");
    }
    return PLUGIN_HANDLED;
}