
   #include <amxmod>
   #include <amxmisc>
   #define ADMIN_TEAMPLAY ADMIN_MAP
   new logfilename[256]
   new bool:ismember[33] = {false,...}
   new bool:startwar = false
   new bool:war = false
   new max_opponents
   new clanteam[3]
   new clantag[32]
   new mp_autoteambalance
   new mp_limitteams
   new sv_password[32]
   new amx_warpass[32]
   //////////////////////
   // Helper Functions //
   //////////////////////
   public say_command(id,cmd[],args[]){
       new name[32]
       new authid[16]
       get_user_name(id,name,31)
       get_user_authid(id,authid,15)
       log_to_file(logfilename,"Cmd:          ^"%s<%d><%s><>^"         %s
   %s",name,get_user_userid(id),authid,cmd,args)
       switch(get_cvar_num("amx_show_activity")){
           case     2:    client_print(0,print_chat,"ADMIN     %s:     %s
   %s",name,cmd,args)
           case 1:    client_print(0,print_chat,"ADMIN: %s %s",cmd,args)
       }
   }
   public print_usage(id,level,cid){
       new hcmd[32], hinfo[128], hflag
       get_concmd(cid,hcmd,31,hflag,hinfo,127,level)
       console_print(id,"Usage:  %s %s",hcmd,hinfo)
   }
   public isnum(str[]){
       if (str[0] == 0)
           return false
       for (new i=0; str[i] != 0; i++)
           if ((str[i] < '0') || (str[i] > '9'))
               return false
       return true
   }
   public get_membersnum(){
       new membersnum = 0
       for (new i = 1; i <= get_maxplayers(); i++)
           if (ismember[i])
               membersnum++
       return membersnum
   }
   public set_pass(){
       new playersnum = get_playersnum(1)
       new membersnum = get_membersnum()
       new pwd[32]
       get_cvar_string("sv_password",pwd,31)
       if     (membersnum     +    max_opponents    >    playersnum    &&
   equal(pwd,amx_warpass))
           set_cvar_string(sv_password,"")
       else   if   (membersnum   +   max_opponents   <=   playersnum   &&
   !equal(pwd,amx_warpass))
           set_cvar_string("sv_password",amx_warpass)
   }
   public changeteam(id,team[],bool:admins){
       if (!admins && get_user_flags(id) & ADMIN_TEAMPLAY)
           return
       user_kill(id,1)
       engclient_cmd(id, "chooseteam")
       engclient_cmd(id, "menuselect", team)
       engclient_cmd(id, "menuselect", "5")
       set_hudmessage(0,255,255,-1.0,0.32,0,2.0,6.0,0.8,0.8,6)
       show_hudmessage(id,"Public    war:    %s   (%s)   vs   Rest   (max
   %d)",clantag,clanteam,max_opponents)
   }
   public force_teamselect(bool:admins){
       if (get_membersnum() < 1 ){
           stop_war()
           return
       }
       new team
       for (new i = 1; i <= get_maxplayers(); i++){
           team = get_user_team(i)
           if (ismember[i]){
               if (clanteam[0] == 'T'){
                   if (team != 1)
                       changeteam(i,"1",admins)
               } else {
                   if (team == 1)
                       changeteam(i,"2",admins)
               }
           } else {
               if (clanteam[0] == 'T'){
                   if (team == 1)
                       changeteam(i,"2",admins)
               } else {
                   if (team != 1)
                       changeteam(i,"1",admins)
               }
           }
       }
   }
   public start_war(){
       startwar = false
       set_cvar_num("mp_autoteambalance",0)
       set_cvar_num("mp_limitteams",0)
       set_cvar_num("sv_restartround",4)
       war = true
       force_teamselect(true)
       set_pass()
   }
   public stop_war() {
       set_cvar_string("sv_password",sv_password)
       set_cvar_num("mp_autoteambalance",mp_autoteambalance)
       set_cvar_num("mp_limitteams",mp_limitteams)
       set_cvar_num("sv_restartround",4)
       war = false
       startwar = false
       set_hudmessage(0,255,255,-1.0,0.32,0,2.0,6.0,0.8,0.8,6)
       show_hudmessage(0,"Public war is over")
       return PLUGIN_HANDLED
   }
   ////////////////////
   // Admin Commands //
   ////////////////////
   public admin_war(id,level,cid){
       if (!cmd_access(id,level,cid,4))
           return PLUGIN_HANDLED
       new arg[3], arg2[3], arg3[32]
       read_argv(1,arg,2)
       read_argv(2,arg2,2)
       read_argv(3,arg3,31)
       remove_quotes(arg)
       remove_quotes(arg2)
       remove_quotes(arg3)
       if (isnum(arg)){
           max_opponents = strtonum(arg) < 16 ? strtonum(arg) : 16
       } else {
           print_usage(id,level,cid)
           return PLUGIN_HANDLED
       }
       if (!equali(arg2,"t") && !equali(arg2,"ct")){
           print_usage(id,level,cid)
           return PLUGIN_HANDLED
       }
       strtoupper(arg2)
       copy(clanteam,2,arg2)
       copy(clantag,31,arg3)
       new name[32]
       for (new i = 1; i <= get_maxplayers(); i++){
           get_user_name(i,name,31)
           ismember[i] = contain(name,clantag) != -1 ? true : false
       }
       set_hudmessage(0,255,255,-1.0,0.32,0,2.0,6.0,0.8,0.8,6)
       show_hudmessage(0,"Prepare   to   fight:  %s  (%s)  vs  Rest  (max
   %d)",clantag,clanteam,max_opponents)
       startwar = true
       new cmd[32], args[192]
       read_argv(0,cmd,31)
       read_args(args,191)
       say_command(id,cmd,args)
       console_print(id,"War  will start next round: %s (%s) vs Rest (max
   %d)",clantag,clanteam,max_opponents)
       return PLUGIN_HANDLED
   }
   public admin_war_stop(id,level,cid){
       if (!cmd_access(id,level,cid,1))
           return PLUGIN_HANDLED
       stop_war()
       new cmd[32], args[192]
       read_argv(0,cmd,31)
       read_args(args,191)
       say_command(id,cmd,args)
       console_print(id,"War is over... Peace man!")
       return PLUGIN_HANDLED
   }
   public admin_war_team(id,level,cid){
       if (!cmd_access(id, level, cid, 2))
           return PLUGIN_HANDLED
       new cmd[32], arg[32]
       read_argv(0,cmd,31)
       read_argv(1,arg,31)
       new player = cmd_target(id,arg,8)
       if (!player)
           return PLUGIN_HANDLED
       if (cmd[8] == 't'){
           ismember[player] = clanteam[0] == 'T' ? true : false
           changeteam(player,"1",true)
       } else {
           ismember[player] = clanteam[0] == 'T' ? false : true
           changeteam(player,"2",true)
       }
       new args[192], name[32]
       read_args(args,191)
       get_user_name(player,name,31)
       say_command(id,cmd,args)
       console_print(id,"Command has been sent to %s",name)
       return PLUGIN_HANDLED
   }
   public admin_war_max(id,level,cid){
       if (!cmd_access(id, level, cid, 2))
           return PLUGIN_HANDLED
       new arg[3]
       read_argv(1,arg,2)
       remove_quotes(arg)
       if (isnum(arg)){
           max_opponents = strtonum(arg) < 16 ? strtonum(arg) : 16
       } else {
           print_usage(id,level,cid)
           return PLUGIN_HANDLED
       }
       new cmd[32], args[192]
       read_argv(0,cmd,31)
       read_args(args,191)
       say_command(id,cmd,args)
       console_print(id,"Number   of   maximum   opponents   changed   to
   %d",max_opponents)
       return PLUGIN_HANDLED
   }
   public admin_war_status(id,level,cid){
       if (war || startwar) {
           new name[32]
           client_print(id,print_console,"%s    (%s)    vs    Rest   (max
   %d)",clantag,clanteam,max_opponents)
           for (new i = 1; i <= get_maxplayers(); i++){
               if (ismember[i]){
                   get_user_name(i,name,31)
                   client_print(id,print_console," %s",name)
               }
           }
       } else {
           client_print(id,print_console,"No public war going on.")
       }
       return PLUGIN_HANDLED
   }
   ////////////////////////////////
   // Handle and Event Functions //
   ////////////////////////////////
   public teamscore_event(){
       if (startwar){
           set_hudmessage(0,255,255,-1.0,0.32,0,2.0,6.0,0.8,0.8,6)
           show_hudmessage(0,"Prepare  to  fight:  %s  (%s)  vs Rest (max
   %d)",clantag,clanteam,max_opponents)
       } else if (war){
           force_teamselect(false)
       }
   }
   public roundtime_event(){
       if (startwar){
           if (get_cvar_num("mp_roundtime") * 60 != read_data(1)){
               startwar = false
               start_war()
           }
       } else if (war){
           force_teamselect(false)
       }
   }
   public handle_chooseteam(id){
       if (!war)
           return PLUGIN_CONTINUE
       if (get_user_flags(id) & ADMIN_TEAMPLAY)
           return PLUGIN_CONTINUE
       new team = get_user_team(id)
       if (ismember[id]){
           if (clanteam[0] == 'T'){
               if (team == 1){
                   client_print(id,print_center,"Team      change     not
   allowed^n^n%s vs Rest",clantag)
                   return PLUGIN_HANDLED
               }
           } else {
               if (team == 2){
                   client_print(id,print_center,"Team      change     not
   allowed^n^n%s vs Rest",clantag)
                   return PLUGIN_HANDLED
               }
           }
       } else {
           if (clanteam[0] == 'T'){
               if (team == 2){
                   client_print(id,print_center,"Team      change     not
   allowed^n^n%s vs Rest",clantag)
                   return PLUGIN_HANDLED
               }
           } else {
               if (team == 1){
                   client_print(id,print_center,"Team      change     not
   allowed^n^n%s vs Rest",clantag)
                   return PLUGIN_HANDLED
               }
           }
       }
       return PLUGIN_CONTINUE
   }
   public client_connect(id){
       ismember[id] = false
       return PLUGIN_CONTINUE
   }
   public client_disconnect(id){
       if (!war)
           return PLUGIN_CONTINUE
       ismember[id] = false
       if (get_membersnum() < 1 ){
           stop_war()
           return PLUGIN_CONTINUE
       }
       set_pass()
       return PLUGIN_CONTINUE
   }
   public client_infochanged(id){
       if(!war)
           return PLUGIN_CONTINUE
       new team = get_user_team(id)
       if ( (get_user_flags(id) & ADMIN_TEAMPLAY) ){
           if (clanteam[0] == 'T')
               ismember[id] = team == 1 ? true : false
           else
               ismember[id] = team != 1 ? true : false
       }
       set_pass()
       return PLUGIN_CONTINUE
   }
   public plugin_init(){
       register_plugin("Public War","0.1b","-X.Soul")
       register_concmd("amx_war","admin_war",ADMIN_TEAMPLAY,"<max
   opponents> <t or ct> <clan tag>")
       register_concmd("amx_war_t","admin_war_team",ADMIN_TEAMPLAY,"<auth
   id, nick or #userid>")
       register_concmd("amx_war_ct","admin_war_team",ADMIN_TEAMPLAY,"<aut
   hid, nick or #userid>")
       register_concmd("amx_war_max","admin_war_max",ADMIN_TEAMPLAY,"<max
   opponents>")
       register_concmd("amx_war_stop","admin_war_stop",ADMIN_TEAMPLAY)
       register_concmd("amx_war_status","admin_war_status",ADMIN_TEAMPLAY
   )
       register_cvar("amx_warpass","war",FCVAR_PROTECTED)
       register_event("TeamScore","teamscore_event","ac")
       register_event("RoundTime","roundtime_event","bc")
       register_clcmd("chooseteam","handle_chooseteam")
       get_cvar_string("sv_password",sv_password,31)
       get_cvar_string("amx_warpass",amx_warpass,31)
       mp_autoteambalance = get_cvar_num("mp_autoteambalance")
       mp_limitteams = get_cvar_num("mp_limitteams")
       get_time("addons/amx/logs/admin%m%d.log",logfilename,255)
       return PLUGIN_CONTINUE
   }
