
   /* AMX Mod script.
   *
   * (c) Copyright 2003, McEarp
   * wyattmc@mcearp.com
   *
   * This file is provided as is (no warranties).
   *
   * Switch Teams v0.9.2.3
   *
   * switch_teams.sma
   *
   * ADMIN_SLAY Required
   * Access level e
   *
   * Usage:
   *  amx_t  <authid, nick or #userid>: changes a user to Terrorist force
   the moment they die
   *   amx_ct   <authid,   nick   or   #userid>:   changes   a   user  to
   Counter-Terrorist force the moment they die
   * say /switchme - to switch forces the moment you die
   * say_team /switchme - to switch forces the moment you die
   *  say_team /switchall - players from both forces swap sides when they
   die
   * say /switchall - to switch forces of all players the moment they die
   *
   *
   * add to menu.cfg: (optional)
   * amx_pmenu "Select player to send to CTs" "amx_ct \'%name%\'" "b" "e"
   * amx_pmenu "Select player to send to Ts" "amx_t \'%name%\'" "b" "e"
   *
   *
   * add to server.cfg: (optional)
   * switch_teams < value >
   * switch_teams 1 = Enable Plugin (default)
   * switch_teams 0 = Disable Plugin
   *
   *
   * Revision History
   * ~~~~~~~~~~~~~~~~
   * - v0.9.2.1:
   * - Public Release 2/13/2003
   *
   * - v0.9.2.2:
   * - Changed color from green to Red for terrorist and blue for CT
   *
   * - v0.9.2.3:
   * - Added say_team /switchme - to switch forces the moment you die
   *  -  Added  say_team /switchall - players from both forces swap sides
   when they die
   *  - Added say /switchall - to switch forces of all players the moment
   they die
   * - Reformated the logging
   * - Changed variables for easier understanding mostly for myself
   *
   * - Future Release:
   * - Working on trying to switch at end of round without killing player
   and loosing weapons like teambalance does
   *
   */
   #include <amxmod>
   new switchteams[33]
   new g_logFileName[256]
   public switch_now(chIndex[]){
      new victimIndex = str_to_num(chIndex)
      new victimName[33]
      get_user_name(victimIndex,victimName,32)
      if(switchteams[victimIndex]){
         if(switchteams[victimIndex] == 3){
            client_print(victimIndex,print_chat,"*  [SWITCH TEAMS] Only 1
   team change is allowed...")
            return PLUGIN_HANDLED
         }
         if(switchteams[victimIndex] == 1){
            switchteams[victimIndex] = 3
            engclient_cmd(victimIndex, "chooseteam")
            engclient_cmd(victimIndex, "menuselect", "1")
            if(is_user_bot(victimIndex))       engclient_cmd(victimIndex,
   "menuselect", "5")
            else client_cmd(victimIndex,"slot5")
            set_hudmessage(255, 50, 50, -1.0, 0.4, 0, 6.0, 8.0, 1.0, 1.0,
   4)
            show_hudmessage(victimIndex,"You are now a ^n Terrorist.")
            client_print(0,print_chat,"*  [SWITCH TEAMS] %s is now on the
   Terrorist force",victimName)
            return PLUGIN_HANDLED
         }
         if(switchteams[victimIndex] == 2){
            switchteams[victimIndex] = 3
            engclient_cmd(victimIndex, "chooseteam")
            engclient_cmd(victimIndex, "menuselect", "2")
            if(is_user_bot(victimIndex))       engclient_cmd(victimIndex,
   "menuselect", "5")
            else client_cmd(victimIndex,"slot5")
            set_hudmessage(0,  50, 255, -1.0, 0.4, 0, 6.0, 8.0, 1.0, 1.0,
   4)
            show_hudmessage(victimIndex,"You     are     now     a     ^n
   Counter-Terrorist.")
            client_print(0,print_chat,"*  [SWITCH TEAMS] %s is now on the
   Counter-Terrorist force",victimName)
            return PLUGIN_HANDLED
         }
      }
      return PLUGIN_CONTINUE
   }
   public deathmsg_event(){
      new victim = read_data(2)
      new chIndex[33]
      num_to_str(victim,chIndex,32)
      set_task(1.0, "switch_now" , 1 , chIndex, 32)
      return PLUGIN_HANDLED
   }
   public switch_forces(id){
      if(!(get_cvar_float("switch_teams"))){
         if(get_user_flags(id)&ADMIN_SLAY &&
   get_user_flags(id)&ADMIN_RCON){
            client_print(id,print_chat,"* [SWITCH TEAMS] <amx_t | amx_ct>
   is disabled. To enabled type the folowing:")
            client_print(id,print_chat,"amx_rcon switch_teams 1")
         }
         else   client_print(id,print_chat,"*  [SWITCH  TEAMS]  <amx_t  |
   amx_ct> is disabled")
         return PLUGIN_HANDLED
      }
      if(!(get_user_flags(id)&ADMIN_SLAY)){
         client_print(id,print_chat,"*  [SWITCH TEAMS] You have no access
   to that command")
         return PLUGIN_HANDLED
      }
      if(read_argc()<2){
         client_print(id,print_chat,"*   [SWITCH   TEAMS]   Usage:  amx_t
   <authid  | part of nick>: Changes a user to Terrorist force the moment
   die.")
         client_print(id,print_chat,"*   [SWITCH   TEAMS]  Usage:  amx_ct
   <authid | part of nick>: Changes a user to Counter-Terrorist force the
   moment die.")
         return PLUGIN_HANDLED
      }
      new playerName[33]
      read_argv(1,playerName,32)
      new target
      target = find_player("a",playerName)
      if(!target) target = find_player("k",playerName)
      if(!target) target = find_player("c",playerName)
      if(!target){
         client_print(id,print_chat,"* [SWITCH TEAMS] Player not found")
         return PLUGIN_HANDLED
      }
      new playerIndex = get_user_index(playerName)
      new command[33]
      read_argv(0,command,32)
      new MyTeam[33]
      get_user_team(playerIndex,MyTeam,32)
      new NewTeam[33]
      if(equal(MyTeam,"CT")) NewTeam = "Terrorist"
      else NewTeam = "Counter-Terrorist"
      if(switchteams[playerIndex] == 3){
         client_print(id,print_chat,"*  [SWITCH TEAMS] Only 1 team change
   is allowed...")
         return PLUGIN_HANDLED
      }
      if(switchteams[playerIndex] == 1 || switchteams[playerIndex] == 2){
         client_print(id,print_chat,"*  [SWITCH TEAMS] (reminder) %s will
   be joining the %s force shortly",playerName,NewTeam)
         return PLUGIN_HANDLED
      }
      if(equal(MyTeam,"TERRORIST") && equal(command,"amx_t")){
         client_print(id,print_chat,"*  [SWITCH  TEAMS]  %s is already on
   the Terrorist force",playerName)
         return PLUGIN_HANDLED
      }
      if(equal(MyTeam,"CT") && equal(command,"amx_ct")){
         client_print(id,print_chat,"*  [SWITCH  TEAMS]  %s is already on
   the Counter-Terrorist force",playerName)
         return PLUGIN_HANDLED
      }
      new name[33]
      get_user_name(id,name,32)
      new authidA[33]
      get_user_authid(id,authidA,32)
      new authidB[33]
      get_user_authid(playerIndex,authidB,32)
      if(equal(command,"amx_t")){
         switchteams[playerIndex] = 1
         if(!is_user_alive(playerIndex)  ||  equal(MyTeam,"SPECTATOR") ||
   equal(MyTeam,"UNASSIGNED")){
            num_to_str(playerIndex,playerName,32)
            set_task(0.5, "switch_now" , 1 , playerName, 32)
         }
         if(is_user_alive(playerIndex)){
            set_hudmessage(255,  50,  50,  -1.0,  0.38, 0, 6.0, 8.0, 1.0,
   1.0, 4)
            show_hudmessage(playerIndex,"You   will   be  switched  to  a
   TERRORIST when you die.^n Compliments of your friendly ADMIN")
            client_print(0,print_chat,"*   [SWITCH   TEAMS]  %s  will  be
   joining the %s force shortly",playerName,NewTeam)
         }
         log_to_file(g_logFileName,"Cmd:  ^"%s<%d><%s><>^"  used  [SWITCH
   TEAMS] AMX_T on ^"%s<%d><%s><>^"",
   name,get_user_userid(id),authidA,playerName,get_user_userid(playerInde
   x),authidB)
      }
      if(equal(command,"amx_ct")){
         switchteams[playerIndex] = 2
         if(!is_user_alive(playerIndex)  ||  equal(MyTeam,"SPECTATOR") ||
   equal(MyTeam,"UNASSIGNED")){
            num_to_str(playerIndex,playerName,32)
            set_task(0.5, "switch_now" , 1 , playerName, 32)
         }
         if(is_user_alive(playerIndex)){
            set_hudmessage(0, 50, 255, -1.0, 0.38, 0, 6.0, 8.0, 1.0, 1.0,
   4)
            show_hudmessage(playerIndex,"You  will  be  switched  to a CT
   when you die.^n Compliments of your friendly ADMIN")
            client_print(0,print_chat,"*   [SWITCH   TEAMS]  %s  will  be
   joining the %s force shortly",playerName,NewTeam)
         }
         log_to_file(g_logFileName,"Cmd:  ^"%s<%d><%s><>^"  used  [SWITCH
   TEAMS] AMX_CT on ^"%s<%d><%s><>^"",
   name,get_user_userid(id),authidA,playerName,get_user_userid(playerInde
   x),authidB)
      }
      return PLUGIN_HANDLED
   }
   public switch_me(id){
      if(!(get_cvar_float("switch_teams"))){
         if(get_user_flags(id)&ADMIN_SLAY &&
   get_user_flags(id)&ADMIN_RCON){
            client_print(id, print_chat,"* [SWITCH TEAMS] <say /switchme>
   is disabled. To enabled type the folowing:")
            client_print(id, print_chat,"amx_rcon switch_teams 1")
         }
         else   client_print(id,   print_chat,"*   [SWITCH   TEAMS]  <say
   /switchme> is disabled")
         return PLUGIN_HANDLED
      }
      new playersName[33]
      get_user_name(id,playersName,32)
      new target
      target = find_player("a",playersName)
      if(!target) return PLUGIN_HANDLED
      new playerIndex = get_user_index(playersName)
      new MyTeam[33],NewTeam[33]
      get_user_team(playerIndex,MyTeam,32)
      if(equal(MyTeam,"UNASSIGNED") || equal(MyTeam,"SPECTATOR")){
         client_print(target,print_chat,"*  [SWITCH TEAMS] You must be on
   a team to use this")
         return PLUGIN_HANDLED
      }
      if(equal(MyTeam,"CT")) NewTeam = "Terrorist"
      else NewTeam = "Counter-Terrorist"
      if(switchteams[target] == 3){
         client_print(id,print_chat,"*  [SWITCH TEAMS] Only 1 team change
   is allowed...")
         return PLUGIN_HANDLED
      }
      if(switchteams[target] == 1 || switchteams[target] == 2){
         client_print(id,print_chat,"*  [SWITCH TEAMS] (reminder) %s will
   be joining the %s force shortly",playersName,NewTeam)
         return PLUGIN_HANDLED
      }
      if(!is_user_alive(target)){
         if(equal(NewTeam,"Terrorist")) switchteams[target] = 1
         if(equal(NewTeam,"Counter-Terrorist")) switchteams[target] = 2
         num_to_str(target,playersName,32)
         set_task(0.5, "switch_now" , 1 , playersName, 32)
         return PLUGIN_HANDLED
      }
      if(is_user_alive(target)){
         if(equal(NewTeam,"Terrorist")){
            switchteams[target] = 1
            set_hudmessage(255,  50,  50,  -1.0,  0.38, 0, 6.0, 8.0, 1.0,
   1.0, 4)
            show_hudmessage(target,"You  will  be switched to a TERRORIST
   when you die.^n Compliments of your own free will")
            client_print(0,print_chat,"*   [SWITCH   TEAMS]  %s  will  be
   joining the %s force shortly",playersName,NewTeam)
            return PLUGIN_HANDLED
         }
         if(equal(NewTeam,"Counter-Terrorist")){
            switchteams[target] = 2
            set_hudmessage(0, 50, 255, -1.0, 0.38, 0, 6.0, 8.0, 1.0, 1.0,
   4)
            show_hudmessage(target,"You    will    be   switched   to   a
   COUNTER-TERRORIST when you die.^n Compliments of your own free will")
            client_print(0,print_chat,"*   [SWITCH   TEAMS]  %s  will  be
   joining the %s force shortly",playersName,NewTeam)
            return PLUGIN_HANDLED
         }
      }
      return PLUGIN_HANDLED
   }
   public switch_all_players(id){
      if(!(get_cvar_float("switch_teams"))){
         if(get_user_flags(id)&ADMIN_SLAY &&
   get_user_flags(id)&ADMIN_RCON){
            client_print(id,print_chat,"* [SWITCH TEAMS] <say /switchall>
   is disabled. To enabled type the folowing:")
            client_print(id,print_chat,"amx_rcon switch_teams 1")
         }
         else    client_print(id,print_chat,"*    [SWITCH   TEAMS]   <say
   /switchall> is disabled")
         return PLUGIN_HANDLED
      }
      if(!(get_user_flags(id)&ADMIN_SLAY)){
         client_print(id,print_chat,"*  [SWITCH TEAMS] You have no access
   to that command")
         return PLUGIN_HANDLED
      }
      new
   playerIndex,numberofplayers,xplayers[32],NewTeam[33],MyTeam[33],MyName
   [33],playerID[32],Float:count = 1.0
      get_players(xplayers, numberofplayers)
      for(new i = 0; i < numberofplayers; ++i){
         playerIndex=xplayers[i]
         get_user_name(playerIndex,MyName,32)
         get_user_team(playerIndex,MyTeam,32)
         if(equal(MyTeam,"TERRORIST")){
            switchteams[playerIndex] = 2
            NewTeam = "Counter-Terrorist"
         }
         if(equal(MyTeam,"CT")){
            switchteams[playerIndex] = 1
            NewTeam = "Terrorist"
         }
         if(!is_user_alive(playerIndex)){
            num_to_str(playerIndex,playerID,32)
            set_task(count, "switch_now" , 1 , playerID, 32)
            ++count
         }
         else{
            set_hudmessage(255,  50,  50,  -1.0,  0.38, 0, 6.0, 8.0, 1.0,
   1.0, 4)
            show_hudmessage(playerIndex,"You  will  be  switched  to a %s
   when you die.^n Compliments of your friendly ADMIN",NewTeam)
         }
      }
      client_print(0,print_chat,"*  [SWITCH  TEAMS]  All  players will be
   switching force shortly")
      new name[33]
      get_user_name(id,name,32)
      new authidA[33]
      get_user_authid(id,authidA,32)
      log_to_file(g_logFileName,"Cmd:   ^"%s<%d><%s><>^"   used   [SWITCH
   TEAMS] /switchall ", name,get_user_userid(id),authidA)
      return PLUGIN_HANDLED
   }
   public reset_hud(){
      new playerIndex
      new xplayers[32]
      new numberofplayers
      get_players(xplayers, numberofplayers)
      for(new i = 0; i < numberofplayers; ++i){
         playerIndex=xplayers[i]
         if(switchteams[playerIndex] == 3) switchteams[playerIndex] = 0
      }
      return PLUGIN_CONTINUE
   }
   public plugin_init(){
      register_plugin("Switch Teams","0.9.2.3","McEarp")
      register_cvar("switch_teams","1")
      register_event("DeathMsg","deathmsg_event","a")
      register_event("ResetHUD","reset_hud","b")
      register_clcmd("say  /switchme","switch_me",0,"-  helps  you switch
   forces the moment you die")
      register_clcmd("say_team   /switchme","switch_me",0,"-   helps  you
   switch forces the moment you die")
      register_clcmd("say  /switchall","switch_all_players",ADMIN_SLAY,"-
   players from both forces swap sides when they die")
      register_clcmd("say_team
   /switchall","switch_all_players",ADMIN_SLAY,"-   players   from   both
   forces swap sides when they die")
      register_clcmd("amx_t","switch_forces",ADMIN_SLAY,"<authid, nick or
   #userid>: Changes a user to Terrorist force the moment they die")
      register_clcmd("amx_ct","switch_forces",ADMIN_SLAY,"<authid,   nick
   or #userid>: Changes a user to Counter-Terrorist force the moment they
   die")
      get_time("addons/amx/logs/admin%m%d.log",g_logFileName,255)
      return PLUGIN_CONTINUE
   }
