/*********************************************************
 *           Bugblatter Glow Extension - V3.4            *
 *********************************************************
 *                                                       *
 * This plug-in is linux & win32 friendly.               *
 *                                                       *
 * Version 3.4:                                          *
 *                                                       *
 *  - Updated to compile on Admin Mod V2.50.56           *
 *                                                       *
 * Version 3.3:                                          *
 *                                                       *
 *  - Updated for language_plugin version 3.3            *
 *                                                       *
 * Version 3.2:                                          *
 *                                                       *
 *  - French & German translations added                 *
 *                                                       *
 * Version 3.1:                                          *
 *                                                       *
 *  - Added spawn mode                                   *
 *                                                       *
 * Version 2.6:                                          *
 *                                                       *
 *  -  Initial Version.                                  *
 *********************************************************
 */
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
#include <plugin>
 
#include "clientio"
#include "settings"
#include "language"
 
new g_Version[] = "3.4";
 
new g_Red[5]   = { 0, 255, 0,   255, 0 };
new g_Green[5] = { 0, 0,   0,   255, 255 };
new g_Blue[5]  = { 0, 0,   255, 0,   0 };
 
#define DEFAULT_MODE 1
#define MIN_MODE 0
#define MAX_MODE 3
 
#define DEFAULT_REPORT 1
#define MIN_REPORT 0
#define MAX_REPORT 1
 
/*
 *********************************************************
 *          CONSTANTS FOR MULTI-LINGUAL MESSAGES         *
 *********************************************************
*/
 
new MSG_REPORTTITLE[]="Bugblatter glow configuration:|Configuration du glow Bugblatter:|Bugblatter Glow Konfiguration:";
new MSG_RPT_DISABLED[]="Glow is currently disabled (bbglow_mode)|Glow est actuellement desactive (bbglow_mode)|Glow ist abgeschaltet (bbglow_mode)";
new MSG_RPT_ENABLED[]="Glow is currently enabled and changes immediately (bbglow_mode)|Glow est actuellement actif et change immediatement (bbglow_mode)|Glow ist aktiv und wechselt sofort (bbglow_mode)";
new MSG_RPT_ROUND[]="Glow is currently enabled and changes at the end of the round (bbglow_mode)|Glow est actuellement actif et change a la fin du round (bbglow_mode)|Glow ist aktiv und wechselt erst beim Rundenende (bbglow_mode)";
new MSG_RPT_SPAWN[]="Glow is currently enabled at spawn point only (bbglow_mode)|Glow est courrament seulement actif au spawn (bbglow_mode)|Glow ist ausschliesslich beim Spawnpoint aktiv (bbglow_mode)";
new MSG_MODE_ERROR[]="Unknown mode '%1s'. Valid modes are: on off round spawn|Mode inconnu '%1s'. Les modes valides sont: on off round|Unbekannter Modus '%1s'. Gueltige Modi: on off round spawn";
 
 
/*
 *********************************************************
 *                     GLOBAL VARIABLES                  *
 *********************************************************
*/
 
/* Configuration variables */
new g_Report=DEFAULT_REPORT;                                /* Automatically show report after command? */
new g_Mode=DEFAULT_MODE;                                    /* Plugin mode */
new g_Deaths[MAX_PLAYERS];
 
 
/* Function declarations */
forward BBGlowReport(HLCommand,HLData,HLUserName,UserIndex);
forward BBGlowEnabled(HLCommand,HLData,HLUserName,UserIndex);
forward BBLogdTeamAction(HLCommand,HLData,HLUserName,UserIndex);
forward BBLogdPlayerAction(HLCommand,HLData,HLUserName,UserIndex);
 
/*
 *********************************************************
 *                  PLUGIN INITIALISATION                *
 *********************************************************
*/
 
 
public plugin_init() {
 
  plugin_registerinfo("Bugblatter's Glow Plugin","Makes the lead player on each team glow",g_Version);
 
  /* Check server is configured properly */
  new fOK = checkVaultOK();
  if (fOK == 0) {
    return PLUGIN_CONTINUE;
  }
  /* No need to abort if this one fails */
  checkLanguage();
 
  /* Read saved configuration from vault */
  readvaultnum("bbglow_report",g_Report,DEFAULT_REPORT,MIN_REPORT,MAX_REPORT);
  readvaultnum("bbglow_mode", g_Mode,DEFAULT_MODE,MIN_MODE,MAX_MODE);
 
  RegisterPlugin();
  language_init();
 
  plugin_registercmd("bbglow_logdwa","BBLogdWorldAction",131072,"");
  exec("logd_reg 62 admin_command bbglow_logdwa",0);
  plugin_registercmd("bbglow_logdsuicide","BBLogdSuicide",131072,"");
  exec("logd_reg 53 admin_command bbglow_logdsuicide",0);
  plugin_registercmd("bbglow_logdkill","BBLogdKill",131072,"");
  exec("logd_reg 57 admin_command bbglow_logdkill",0);
 
 
  new i;
  for(i=0;i<MAX_PLAYERS;i++) {
    g_Deaths[i]=0;
  }
 
  plugin_message("Plugin initialisation complete.");
 
 
  return PLUGIN_CONTINUE;
}
 
RegisterPlugin() {
  /* Register callbacks with admin mod */
 
  plugin_registercmd("bbglow_mode","BBGlowMode",ACCESS_CONFIG,
                     "bbglow_mode <^"on^" | ^"round^" | | ^"spawn^" | ^"off^">: Enable / disable glowing");
  plugin_registercmd("bbglow_report","BBGlowReport",ACCESS_CONFIG,
                     "bbglow_report [ ^"on^" | ^"off^" ]: Shows the glow config, and enables/disable report after every command");
 
}
 
 
public BBGlowMode(HLCommand,HLData,HLUserName,UserIndex) {
  new Data[MAX_DATA_LENGTH];
  safe_convert(HLData,Data,MAX_DATA_LENGTH);
  if (strcasecmp(Data,"on")==0) {
    g_Mode = 1;
    SetGlow(0);
    writevaultnum("bbglow_mode",g_Mode);
    return ShowConfig(UserIndex,0);
  }
 
  if (strcasecmp(Data,"off")==0) {
    g_Mode = 0;
    SetGlow(0);
    writevaultnum("bbglow_mode",g_Mode);
    return ShowConfig(UserIndex,0);
  }
 
  if (strcasecmp(Data,"round")==0) {
    g_Mode = 2;
    writevaultnum("bbglow_mode",g_Mode);
    return ShowConfig(UserIndex,0);
  }
 
  if (strcasecmp(Data,"spawn")==0) {
    g_Mode = 3;
    SetGlow(0);
    writevaultnum("bbglow_mode",g_Mode);
    return ShowConfig(UserIndex,0);
  }
 
  language_sayf(UserIndex,MSG_MODE_ERROR,print_type:print_console,0,0,0,0,Data);
  return PLUGIN_HANDLED;
 
 
}
 
public BBGlowReset(HLCommand,HLData,HLUserName,UserIndex) {
  g_Mode=DEFAULT_MODE;
  writevaultnum("bbglow_mode",g_Mode);
  SetGlow(0);
  return ShowConfig(UserIndex,0);
}
 
public BBGlowReport(HLCommand,HLData,HLUserName,UserIndex) {
  if (readHLonoff(HLData,g_Report,DEFAULT_REPORT,MIN_REPORT,MAX_REPORT)) {
    writevaultnum("bbglow_report",g_Report);
    ShowConfig(UserIndex,0);
  }
  else {
    ShowConfig(UserIndex,1);
  }
  return PLUGIN_HANDLED;
}
 
 
ShowConfig(UserIndex,Force) {
  if ((Force==0) && (g_Report==0)) {
    return PLUGIN_HANDLED;
  }
 
  language_say(UserIndex,MSG_REPORTTITLE,print_type:print_console);
  if (g_Mode ==0) {
    language_say(UserIndex,MSG_RPT_DISABLED,print_type:print_console);
  }
  else if (g_Mode==1) {
    language_say(UserIndex,MSG_RPT_ENABLED,print_type:print_console);
  }
  else if (g_Mode==2) {
    language_say(UserIndex,MSG_RPT_ROUND,print_type:print_console);
  }
  else {
    language_say(UserIndex,MSG_RPT_SPAWN,print_type:print_console);
  }
  return PLUGIN_HANDLED;
}
 
/*
 *********************************************************
 *                 LOGD CALLBACK HANDLERS                *
 *********************************************************
*/
 
public BBLogdWorldAction(HLCommand,HLData,HLUserName,UserIndex) {
  new Data[MAX_DATA_LENGTH];
  safe_convert(HLData,Data,MAX_DATA_LENGTH);
 
  if (strmatch(Data,"Round_Start",11)==1) {
    SetGlow(0);
  }
  else if (strmatch(Data,"Round_End",9)==1) {
    if (g_Mode==3) {
      SetGlow(1);
    }
    else {
      SetGlow(0);
    }
  }
 
  return PLUGIN_HANDLED;
}
 
public BBLogdSuicide(HLCommand,HLData,HLUserName,UserIndex) {
  new Data[MAX_DATA_LENGTH];
  new strIndex[MAX_DATA_LENGTH];
  new nIndex;
  safe_convert(HLData,Data,MAX_DATA_LENGTH);
  strbreak(Data,strIndex,Data,MAX_DATA_LENGTH);
  nIndex=strtonum(strIndex);
  if (nIndex>0 && nIndex<MAX_PLAYERS) {
    g_Deaths[nIndex]=g_Deaths[nIndex]+1;
    if (g_Mode == 1) {
      SetGlow(0);
    }
  }
}
 
public BBLogdKill(HLCommand,HLData,HLUserName,UserIndex) {
  new Data[MAX_DATA_LENGTH];
  new strIndex[MAX_DATA_LENGTH];
  new nIndex;
  safe_convert(HLData,Data,MAX_DATA_LENGTH);
  strbreak(Data,strIndex,Data,MAX_DATA_LENGTH);
  strbreak(Data,strIndex,Data,MAX_DATA_LENGTH);
  nIndex=strtonum(strIndex);
 
  if (nIndex>0 && nIndex<MAX_PLAYERS) {
    g_Deaths[nIndex]=g_Deaths[nIndex]+1;
    if (g_Mode == 1) {
      SetGlow(0);
    }
  }
}
 
 
public plugin_disconnect(HLUserName, UserIndex) {
  g_Deaths[UserIndex]=0;
}
 
SetGlow(fSpawn) {
  new nTopScore[5];
  new nTopDeaths[5];
  new nTopPlayer[5];
  new fMultiple[5];
  new i;
 
  for (i=0;i <5 ;i++) {
    nTopScore[i]=-10000;
    nTopPlayer[i]=-1;
    nTopDeaths[i]=10000;
    fMultiple[i]=0;
  }
 
  new Name[MAX_NAME_LENGTH];
  new frags=0;
  new session;
  new wonid;
  new team;
 
  new c = maxplayercount();
  for(i=1; i<=c; i++) {
    if (playerinfo(i,Name,MAX_NAME_LENGTH,session,wonid,team)) {
      if (team>0 && team <5) {
        if (get_userFrags(Name,frags) == 1) {
          if (nTopScore[team]>-10000) {
            fMultiple[team]=1;
          }
          if (frags > nTopScore[team]) {
            nTopScore[team]=frags;
            nTopDeaths[team]=g_Deaths[i];
            nTopPlayer[team]=i;
          }
          else {
            if (frags == nTopScore[team]) {
              if (g_Deaths[i] < nTopDeaths[team]) {
                nTopScore[team]=frags;
                nTopPlayer[team]=i;
                nTopDeaths[team]=g_Deaths[i];
              }
              else {
                nTopPlayer[team]=-1;
              }
            }
          }
        }
      }
    }
  }
 
  for(i=1; i<=c; i++) {
    if (playerinfo(i,Name,MAX_NAME_LENGTH,session,wonid,team)) {
      if ((g_Mode>0) && ((g_Mode<3) || (fSpawn==1))) {
        for (team=0;team <5 ;team++) {
          if ((nTopPlayer[team] == i) && (nTopScore[team] > 0) && (fMultiple[team]>0)) {
            glow(Name,g_Red[team],g_Green[team],g_Blue[team]);
            break;
          }
        }
      }
      else {
        team=5;
      }
 
      if (team==5) {
        glow(Name,0,0,0);
      }
    }
  }
 
}