/*********************************************************
 *           Bugblatter Radio Plugin - V3.1              *
 *********************************************************
 *                                                       *
 * Version 3.1:                                          *
 *                                                       *
 *  -  No change                                         *
 *                                                       *
 * Version 2.6:                                          *
 *                                                       *
 *  -  No change                                         *
 *                                                       *
 * Version 2.5:                                          *
 *  -  Does not return PLUGIN_FAILRE during init         *
 *  -  Move prominent error messages in log              *
 *                                                       *
 * Version 2.4:                                          *
 *  -  Updated for adminmod 2.50.50                      *
 *  -  Works around bug in calling admin_command bb,,,   *
 *     from in a .cfg file saved in DOS format           *
 *  -  Removed unnecessary check for language plugin     *
 *  -  Removed "../cstrile" from exec'd command. This    *
 *     means this plugin will now only work in cs, but   *
 *     is needed due to new adminmod security on execs   *
 *                                                       *
 * Version 2.3:                                          *
 *  -  Checks values of server.cfg vars during init      *
 *                                                       *
 * Version 2.2:                                          *
 *  - No changes                                         *
 *                                                       *
 * Version 2.1:                                          *
 *  - Changed keys to F2-F9 because CS1.5 uses F1        *
 *  - Prevents dead players from sending/receiving       *
 *    messages                                           *
 *  - Updated for adminmod 2.50.37                       *
 *                                                       *
 * Version 2.0:                                          *
 *  - First version                                      *
 *                                                       *
 *********************************************************/
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
#include <plugin>
 
#include "clientio"
#include "settings"
 
new g_Version[] = "2.6";
 
#define MAX_MENU_LENGTH 512;
 
new g_Wavs[8][] = { "circleback","com_followcom","ct_imhit","hitassist","ct_point","getout","matedown","meetme" };
new g_UserInMenu[MAX_PLAYERS];
new g_Registered = 0;
 
 
 
forward SayCommand(HLCommand,HLData,HLUserName,UserIndex);
forward display_bindmenu(Index,Name[]);
forward BindKey(UserName[],MenuChoice);
forward BBRadioMenu(HLCommand,HLData,HLUserName,UserIndex);
forward display_radiomenu(Index,Name[]);
forward menuselect(HLCommand,HLData,HLUserName,UserIndex);
forward RadioPlay(UserIndex,Message);
 
 
 
 
public plugin_init() {
  plugin_registerinfo("Bugblatter's Radio Commands Plugin","Adds extra radio commands to the game",g_Version);
 
  /* Check server is configured properly */
  new fOK = checkFileAccessRead();
  fOK = checkAllowClientExec() && fOK;
  fOK = checkAdminVoteAutostart() && fOK;
  fOK = checkVaultOK() && fOK;
  if (fOK == 0) {
    return PLUGIN_CONTINUE;
  }
 
  plugin_registercmd("bbradio_menu","BBRadioMenu",ACCESS_ALL);
  plugin_registercmd("menuselect","menuselect",ACCESS_ALL);
  plugin_registercmd("say","SayCommand",ACCESS_ALL);
 
  g_Registered=1;
  return PLUGIN_CONTINUE;
}
 
public plugin_connect(HLUserName, HLIP, UserIndex) {
  if (g_Registered ==0) {
    return PLUGIN_FAILURE;
  }
  selfmessage("====================================================");
  selfmessage("This server supports some additional radio commands.");
  selfmessage("To bind them to a key say ^"extra radio^"");
  selfmessage("====================================================");
  return PLUGIN_CONTINUE;
}
 
public SayCommand(HLCommand,HLData,HLUserName,UserIndex) {
  new Data[MAX_DATA_LENGTH];
  new UserName[MAX_NAME_LENGTH];
  safe_convert(HLData,Data,MAX_DATA_LENGTH);
  safe_convert(HLUserName,UserName,MAX_NAME_LENGTH);
 
  strstripquotes(Data);
 
  if (strmatch(Data,"extra radio",11)==1) {
     display_bindmenu(UserIndex,UserName);
  }
}
 
display_bindmenu(Index,Name[]) {
  new act_keys=63;
  new MenuText[MAX_MENU_LENGTH];
  snprintf(MenuText, MAX_MENU_LENGTH, "Bind radio to which key:^n^n1. b^n2. v^n3. n^n4. r^n5. g^n6. Do not bind^n");
  menu(Name,MenuText,act_keys);
  g_UserInMenu[Index]=2;
  return 0;
}
 
 
BindKey(UserName[],MenuChoice) {
  switch(MenuChoice) {
  case 1:
    execclient(UserName,"bind b bbradio_menu");
  case 2:
    execclient(UserName,"bind v bbradio_menu");
  case 3:
    execclient(UserName,"bind n bbradio_menu");
  case 4:
    execclient(UserName,"bind r bbradio_menu");
  case 5:
    execclient(UserName,"bind g bbradio_menu");
  }
 
  return PLUGIN_CONTINUE;
}
 
public BBRadioMenu(HLCommand,HLData,HLUserName,UserIndex){
  new UserName[MAX_NAME_LENGTH];
  safe_convert(HLUserName,UserName,MAX_NAME_LENGTH);
  display_radiomenu(UserIndex,UserName);
  return PLUGIN_HANDLED;
}
 
display_radiomenu(Index,Name[]) {
  new act_keys=255;
  new MenuText[MAX_MENU_LENGTH];
  snprintf(MenuText, MAX_MENU_LENGTH, "Extra radio messages:^n^n1. Circle back^n2. Follow my command^n3. Im hit^n4. I'm hit, need assistance^n5. I'll take the point^n6. Get out of here^n^n7. Teammate down^n8. Rendevous^n");
  menu(Name,MenuText,act_keys);
  g_UserInMenu[Index]=1;
  return 0;
}
 
public menuselect(HLCommand,HLData,HLUserName,UserIndex){
  if (g_Registered ==0) {
    return PLUGIN_FAILURE;
  }
 
  new Data[MAX_DATA_LENGTH];
  safe_convert(HLData,Data,MAX_DATA_LENGTH);
  new Name[MAX_DATA_LENGTH];
  safe_convert(HLUserName,Name,MAX_DATA_LENGTH);
 
  if (g_UserInMenu[UserIndex]==0) {
    return PLUGIN_CONTINUE;
  }
 
  if  (g_UserInMenu[UserIndex]==2) {
    BindKey(Name,strtonum(Data));
  }
  else {
    RadioPlay(UserIndex,strtonum(Data));
  }
 
  g_UserInMenu[UserIndex]=0;
  return PLUGIN_CONTINUE;
}
 
public RadioPlay(UserIndex,Message) {
 
  new cmd[MAX_DATA_LENGTH];
  snprintf(cmd,MAX_DATA_LENGTH,"speak sound/radio/%s.wav",g_Wavs[Message-1]);
 
  new sessionid = 0;
  new AuthID[MAX_AUTHID_LENGTH];
  new teamid = 0;
  new username[MAX_DATA_LENGTH];
  new dead =0;
  new wonid=0;
 
 
  if(playerinfo(UserIndex, username, MAX_DATA_LENGTH, sessionid, wonid, teamid,dead,AuthID)==1) {
    if (dead==0) {
      safe_execteam(teamid,cmd,0);
    }
  }
 
  return PLUGIN_HANDLED;
}