
   /* AMX Mod script.
   *
   * (c) Copyright 2003, BouBou (c) DevProd.com
   * This file is provided as is (no warranties).
   *
   * SwissGamesCave Server Admin
   *
   * Original by OLO, adapted for DoD by BouBou
   */
   #include <amxmod>
   #define MAX_MAPS 64
   #define MAX_CMDS 64
   #define MAX_PPL 24
   #define MAX_CVARS 32
   #define MAX_TEAM 24
   new bool:cstrike_running
   /* plain maps: name and num */
   new map_dat[MAX_MAPS][32]
   new map_num = 0
   /* commands: name, cmd to execute, flags and num */
   new cmd_name_dat[MAX_CMDS][32]
   new cmd_cmd_dat[MAX_CMDS][64]
   new cmd_misc_dat[MAX_CMDS][2]
   new cmd_num = 0
   /* player menu: name, cmd to exec., flags and num */
   new ppl_name_dat[MAX_PPL][32]
   new ppl_cmd_dat[MAX_PPL][64]
   new ppl_misc_dat[MAX_PPL][2]
   new ppl_num = 0
   /* team menu: name, cmd to exec., flags and num */
   new team_name_dat[MAX_PPL][32]
   new team_cmd_dat[MAX_PPL][64]
   new team_misc_dat[MAX_PPL][2]
   new team_num = 0
   /* cvars: name, flags, values for cvars */
   new cvar_name_dat[MAX_CVARS][32]
   new cvar_misc_dat[MAX_CVARS][32]
   new cvar_cmd_dat[MAX_CVARS*5][64]
   new cvar_cmd_num = 0
   new cvar_num = 0
   /* stuff for player */
   new user_options[33][64]
   new user_position[33]
   new user_players[33][32]
   new user_select[33]
   new user_select_max[33]
   /* action of selected option in main menu */
   public action_main_menu(id,key){
       switch(key){
           case 0: { /* key 1 pressed - menu for players */
               /* get commands for players basing on flags to commands
                    store indexes of this commands in user_options
                    and return number of these commands
               */
               user_select_max[id] =
   ppl_options(get_user_flags(id),user_options[id])
               /* if current position of selected command is greater then
   max then set it to 0    */
               user_select[id] %= user_select_max[id]
               /* display players menu */
               show_ppl_menu(id,0)
           }
           case 1: { /* commands menu selected */
             /* gather info about available commands */
               user_select_max[id] =
   cmd_options(get_user_flags(id),user_options[id])
               show_cmd_menu(id,0)
           }
           case 2: { /* cvars menu selected */
               user_select_max[id] =
   cvar_options(get_user_flags(id),user_options[id])
               show_cvar_menu(id,0)
           }
           case 3: { /* maps */
               user_select[id] %= 4 /* max. 3 options */
               show_map_menu(id,0)
           }
           case 4: { /* key 1 pressed - menu for players */
               /* get commands for players basing on flags to commands
                    store indexes of this commands in user_options
                    and return number of these commands
               */
               user_select_max[id] =
   team_options(get_user_flags(id),user_options[id])
               /* if current position of selected command is greater then
   max then set it to 0    */
               //user_select[id] %= user_select_max[id]
               /* display players menu */
               show_team_menu(id,0)
           }
       }
       return PLUGIN_HANDLED
   }
   public action_cmd_menu(id,key){
       if (key==8)
           show_cmd_menu(id,++user_position[id])
       else if (key==9){
           if (--user_position[id] < 0)
               show_main_menu(id)
           else
               show_cmd_menu(id,user_position[id])
       }
       else {
           new option = user_position[id] * 8 + key
           new ctype = cmd_misc_dat[user_options[id][option]][1]
           new command[64]
           copy(command,63,cmd_cmd_dat[user_options[id][option]])
           while(replace(command,63,"\'","^"")){
               // do nothing
           }
           if (ctype & 1)
               server_cmd(command)
           else if (ctype & 2)
               client_cmd(id,command)
           else if (ctype & 4)
               client_cmd(0,command)
           if (ctype & 8)
               show_main_menu(id)
           else if (!(ctype & 16))
               show_cmd_menu(id,user_position[id])
       }
       return PLUGIN_HANDLED
   }
   public action_map_menu(id,key){
       if (key==7) {
           ++user_select[id]
           user_select[id] %= 4
           show_map_menu(id,user_position[id])
       }
       else if (key==8){
           show_map_menu(id,++user_position[id])
       }
       else if (key==9){
           if (--user_position[id] < 0)
               show_main_menu(id)
           else
               show_map_menu(id,user_position[id])
       }
       else {
           new option = user_position[id] * 7 + key
           switch(user_select[id]){
           case 0:    client_cmd(id,"amx_map ^"%s^"",map_dat[option])
           case 1:    client_cmd(id,"amx_map ^"%s^" 3",map_dat[option])
           case 2:    client_cmd(id,"amx_votemap ^"%s^"",map_dat[option])
           case 3:    {
                   set_cvar_string("amx_nextmap",map_dat[option])
                   show_map_menu(id,user_position[id])
               }
           }
       }
       return PLUGIN_HANDLED
   }
   public action_cvar_menu(id,key){
       if (key==8){
           show_cvar_menu(id,++user_position[id])
       }
       else if (key==9){
           if (--user_position[id] < 0)
               show_main_menu(id)
           else
               show_cvar_menu(id,user_position[id])
       }
       else {
           new option = user_position[id] * 8 + key
           new omax = cvar_misc_dat[user_options[id][option]][1]
           new cvalue[64], a
           get_cvar_string(cvar_name_dat[user_options[id][option]],cvalue
   ,63)
           for(a = 2; a < omax; ++a){
               if
   (equal(cvalue,cvar_cmd_dat[cvar_misc_dat[user_options[id][option]][a]]
   ))    {
                   if (a >= omax - 1)
                       a = 2
                   else
                       a++
                   set_cvar_string(cvar_name_dat[user_options[id][option]
   ],cvar_cmd_dat[cvar_misc_dat[user_options[id][option]][a]])
                   break;
               }
           }
           if (a >= omax)
   set_cvar_string(cvar_name_dat[user_options[id][option]],cvar_cmd_dat[c
   var_misc_dat[user_options[id][option]][2]])
           show_cvar_menu(id,user_position[id])
       }
       return PLUGIN_HANDLED
   }
   public action_ppl_menu(id,key){
       if (key==7) {
           ++user_select[id]
           user_select[id] %= user_select_max[id]
           show_ppl_menu(id,user_position[id])
       }
       else if (key==8){
           show_ppl_menu(id,++user_position[id])
       }
       else if (key==9){
           if (--user_position[id] < 0)
               show_main_menu(id)
           else
               show_ppl_menu(id,user_position[id])
       }
       else {
           new option = user_position[id] * 7 + key
           new index = user_players[id][option]
           new ctype = ppl_misc_dat[user_options[id][user_select[id]]][1]
           if (is_user_connected(index)) {
               new command[64], authid[16], name[32], userid[32]
               copy(command,63,ppl_cmd_dat[user_options[id][user_select[i
   d]]])
               get_user_authid(index,authid,15)
               get_user_name(index,name,31)
               numtostr(get_user_userid(index),userid,31)
               replace(command,63,"%userid%",userid)
               replace(command,63,"%authid%",authid)
               replace(command,63,"%name%",name)
               while ( replace(command,63,"'","^"") ) {
                   // do nothing
               }
               if (ctype & 1){
                   server_cmd(command)
                   server_exec()
               }
               else if (ctype & 2)
                   client_cmd(id,command)
               else if (ctype & 4)
                   client_cmd(index,command)
           }
           if (ctype & 8)
               show_main_menu(id)
           else if (!(ctype & 16))
               show_ppl_menu(id,user_position[id])
       }
       return PLUGIN_HANDLED
   }
   public action_team_menu(id,key){
       if (key==7) {
           ++user_select[id]
           user_select[id] %= user_select_max[id]
           show_team_menu(id,user_position[id])
       }
       else if (key==8){
           show_team_menu(id,++user_position[id])
       }
       else if (key==9){
           if (--user_position[id] < 0)
               show_main_menu(id)
           else
               show_team_menu(id,user_position[id])
       }
       else {
           new option = user_position[id] * 7 + key
           new index = user_players[id][option]
           new ctype =
   team_misc_dat[user_options[id][user_select[id]]][1]
           if (is_user_connected(index)) {
               new authid[16], name[32], userid[32]

               get_user_authid(index,authid,15)
               get_user_name(index,name,31)

               numtostr(get_user_userid(index),userid,31)

               if (user_select[id] == 0) {
                   client_cmd(index, "jointeam 1")
                   client_print(0,print_console,"[AMX]    Client   ^"%s^"
   switched to allies.",name)
               }
               if (user_select[id] == 1) {
                   client_cmd(index, "jointeam 2")
                   client_print(0,print_console,"[AMX]    Client   ^"%s^"
   switched to axis.",name)
               }
           }

           if (ctype & 8)
               show_main_menu(id)
           else if (!(ctype & 16))
               show_team_menu(id,user_position[id])
       }
       return PLUGIN_HANDLED
   }
   /* gather informations */
   cmd_options(flags,options[64]) {
       new onum = 0
       for(new a = 0; a < cmd_num; ++a)
           if (cmd_misc_dat[a][0] & flags)
               options[onum++] = a
       return onum
   }
   ppl_options(flags,options[64]) {
       new onum = 0
       for(new a = 0; a < ppl_num; ++a)
           if (ppl_misc_dat[a][0] & flags)
               options[onum++] = a
       return onum
   }
   team_options(flags,options[64]) {
       new onum = 0
       for(new a = 0; a < team_num; ++a)
           if (team_misc_dat[a][0] & flags)
               options[onum++] = a
       return onum
   }
   cvar_options(flags,options[64]) {
       new onum = 0
       for(new a = 0; a < cvar_num; ++a)
           if (cvar_misc_dat[a][0] & flags)
               options[onum++] = a
       return onum
   }
   /* displaying */
   show_main_menu(id){
       new menu_body[192]

       if (cstrike_running)
           copy(menu_body,191,"\yAMX  DoD  Admin  Menu^n\w^n\%c1.  Player
   Menu\w^n\%c2.  Cmd  Menu\w^n\%c3. Cvar Menu\w^n\%c4. Map Menu\w^n\%c5.
   Team Menu\w^n^n0. Exit")
       else
           copy(menu_body,191,"AMX DoD Admin
   Menu^n-------------------------^n^n%c.  Player Menu^n%c. Cmd Menu^n%c.
   Cvar Menu^n%c. Map Menu^n%c. Team Menu^n^n0. Exit")

       new c_ppl_m, c_cmd_m, c_cvar_m, c_map_m, c_team_m, keys = (1<<9)
       c_ppl_m   =   c_cmd_m   =   c_cvar_m   =   c_map_m  =  c_team_m  =
   (cstrike_running ? 'd' : '#' )
       if (ppl_options(get_user_flags(id),user_options[id])){
           keys |= (1<<0)
           c_ppl_m = cstrike_running ? 'w' : '1'
       }
       if (cmd_options(get_user_flags(id),user_options[id])){
           keys |= (1<<1)
           c_cmd_m = cstrike_running ? 'w' : '2'
       }
       if (cvar_options(get_user_flags(id),user_options[id])){
           keys |= (1<<2)
           c_cvar_m = cstrike_running ? 'w' : '3'
       }
       if (map_num){
           keys |= (1<<3)
           c_map_m = cstrike_running ? 'w' : '4'
       }
       if (ppl_options(get_user_flags(id),user_options[id])){
           keys |= (1<<4)
           c_team_m = cstrike_running ? 'w' : '5'
       }


       user_position[id] = 0
       format(menu_body,191,menu_body,c_ppl_m,c_cmd_m,c_cvar_m,c_map_m,c_
   team_m)
       show_menu(id,keys,menu_body)
       return PLUGIN_HANDLED
   }
   show_cvar_menu(id,position){
       new menu_body[512]
       if (cstrike_running)
        copy(menu_body,511,"\yAMX Cvar Menu\R%d/%d^n\w^n")
       else
        format(menu_body,511,"%-20s%%d/%%d^n^n","AMX Cvar Menu")
       new loc_cvar_num = user_select_max[id]
       new mbpos = 0
       mbpos       =       format(menu_body,511,menu_body,position      +
   1,((loc_cvar_num/8)+((loc_cvar_num%8)?1:0)))
       new temp[32], cval[32], start = position * 8, end = start + 8
       if (end > loc_cvar_num) end = loc_cvar_num
       new pos = 0, keys = (1<<9)
       for(new a = start; a < end; ++a){
           get_cvar_string(cvar_name_dat[user_options[id][a]],cval,31)

           if (cstrike_running)
               format(temp,31,"%d.            %s\R%s^n\w",pos           +
   1,cvar_name_dat[user_options[id][a]],cval)
           else
               format(temp,31,"%d.        %-20s        %s^n",pos        +
   1,cvar_name_dat[user_options[id][a]],cval)

           mbpos+=copy(menu_body[mbpos],511 - mbpos,temp)
           keys |= (1<<pos)
           pos++
       }
       if (end != loc_cvar_num){
           mbpos+=copy(menu_body[mbpos],511   -  mbpos,"^n9.  More...^n0.
   Back")
           keys |= (1<<8)
       }
       else {
           mbpos+=copy(menu_body[mbpos],511 - mbpos,"^n0. Back")
       }
       show_menu(id,keys,menu_body)
       return PLUGIN_HANDLED
   }
   show_map_menu(id,position){
       new menu_body[512]
       if (cstrike_running)
        copy(menu_body,511,"\yAMX Map Menu\R%d/%d^n\w^n")
       else
        format(menu_body,511,"%-20s%%d/%%d^n^n","AMX Map Menu")
       new mbpos = 0
       mbpos=format(menu_body,511,menu_body,position                    +
   1,((map_num/7)+((map_num%7)?1:0)))
       new temp[32], start = position * 7, end = start + 7
       if (end > map_num) end = map_num
       new pos = 0, keys = (1<<9)|(1<<7)
       for(new a = start; a < end; ++a){
           format(temp,31,"%d. %s^n",pos + 1,map_dat[a])
           mbpos+=copy(menu_body[mbpos],511-mbpos,temp)
           keys |= (1<<pos)
           pos++
       }
       switch(user_select[id]){
       case    0:   mbpos+=copy(menu_body[mbpos],511-mbpos,"^n8.   Change
   level^n")
       case 1: mbpos+=copy(menu_body[mbpos],511-mbpos,"^n8. End level^n")
       case 2: mbpos+=copy(menu_body[mbpos],511-mbpos,"^n8. Vote map^n")
       case     3:    mbpos+=copy(menu_body[mbpos],511-mbpos,"^n8.    Set
   nextmap^n")
       }
       if (end != map_num){
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n9.       More...^n0.
   Back")
           keys |= (1<<8)
       }
       else {
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n0. Back")
       }
       show_menu(id,keys,menu_body)
       return PLUGIN_HANDLED
   }
   show_cmd_menu(id,position){
       new menu_body[512]
       if (cstrike_running)
        copy(menu_body,511,"\yAMX Cmd Menu\R%d/%d^n\w^n")
       else
        format(menu_body,511,"%-20s%%d/%%d^n^n","AMX Cmd Menu")
       new loc_cmd_num = user_select_max[id]
       new mbpos = 0
       mbpos       =       format(menu_body,511,menu_body,position      +
   1,((loc_cmd_num/8)+((loc_cmd_num%8)?1:0)))
       new temp[64], start = position * 8, end = start + 8
       if (end > loc_cmd_num) end = loc_cmd_num
       new pos = 0, keys = (1<<9)
       for(new a = start; a < end; ++a){
           format(temp,63,"%d. %s^n",pos +
   1,cmd_name_dat[user_options[id][a]])
           mbpos+=copy(menu_body[mbpos],511-mbpos,temp)
           keys |= (1<<pos)
           pos++
       }
       if (end != loc_cmd_num){
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n9.       More...^n0.
   Back")
           keys |= (1<<8)
       }
       else {
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n0. Back")
       }
       show_menu(id,keys,menu_body)
       return PLUGIN_HANDLED
   }
   show_ppl_menu(id,position){
       new menu_body[512]
       if (cstrike_running)
        copy(menu_body,511,"\yAMX Player Menu\R%d/%d^n\w^n")
       else
        format(menu_body,511,"%-20s%%d/%%d^n^n","AMX Player Menu")
       new inum, start = position * 7
       get_players(user_players[id],inum)
       if (start >= inum) start = position = user_position[id] = 0
       new mbpos = 0
       mbpos=format(menu_body,511,menu_body,position                    +
   1,((inum/7)+((inum%7)?1:0)))
       new temp[64], name[32], end = start + 7
       if (end > inum) end = inum
       new pos = 0, keys = (1<<9)|(1<<7), status[6]
       for(new a = start; a < end; ++a){
           get_user_name(user_players[id][a],name,31)
           if (get_user_flags(user_players[id][a])&ADMIN_IMMUNITY)    {
               if (cstrike_running)
                   format(status,5,"\d%d",pos + 1)
               else
                   copy(status,3,"#")
           }
           else {
               num_to_str(pos + 1,status,3)
               keys |= (1<<pos)
           }
           if (cstrike_running)
               format(temp,63,"%s. %s^n\w",status,name)
           else
               format(temp,63,"%s. %s^n",status,name)
           mbpos+=copy(menu_body[mbpos],511-mbpos,temp)
           pos++
       }
       format(temp,63,"^n8.
   %s^n",ppl_name_dat[user_options[id][user_select[id]]])
       mbpos+=copy(menu_body[mbpos],511-mbpos,temp)
       if (end != inum){
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n9.       More...^n0.
   Back")
           keys |= (1<<8)
       }
       else {
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n0. Back")
       }
       show_menu(id,keys,menu_body)
       return PLUGIN_HANDLED
   }
   show_team_menu(id,position){
       new menu_body[512]
       if (cstrike_running)
        copy(menu_body,511,"\yAMX Team Menu\R%d/%d^n\w^n")
       else
        format(menu_body,511,"%-20s%%d/%%d^n^n","AMX Team Menu")
       new inum, start = position * 7
       get_players(user_players[id],inum)
       if (start >= inum) start = position = user_position[id] = 0
       new mbpos = 0
       mbpos=format(menu_body,511,menu_body,position                    +
   1,((inum/7)+((inum%7)?1:0)))
       new temp[64], name[32], end = start + 7
       if (end > inum) end = inum
       new pos = 0, keys = (1<<9)|(1<<7), status[6]
       for(new a = start; a < end; ++a){
           get_user_name(user_players[id][a],name,31)
           if (get_user_flags(user_players[id][a])&ADMIN_IMMUNITY)    {
               if (cstrike_running)
                   format(status,5,"\d%d",pos + 1)
               else
                   copy(status,3,"#")
           }
           else {
               num_to_str(pos + 1,status,3)
               keys |= (1<<pos)
           }
           if (cstrike_running)
               format(temp,63,"%s. %s^n\w",status,name)
           else
               format(temp,63,"%s. %s^n",status,name)
           mbpos+=copy(menu_body[mbpos],511-mbpos,temp)
           pos++
       }
       format(temp,63,"^n8.
   %s^n",team_name_dat[user_options[id][user_select[id]]])
       mbpos+=copy(menu_body[mbpos],511-mbpos,temp)
       if (end != inum){
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n9.       More...^n0.
   Back")
           keys |= (1<<8)
       }
       else {
           mbpos+=copy(menu_body[mbpos],511-mbpos,"^n0. Back")
       }
       show_menu(id,keys,menu_body)
       return PLUGIN_HANDLED
   }
   /* command amx_menu */
   public admin_menu(id){
       if (!(get_user_flags(id)&ADMIN_MENU)){
           client_print(id,print_console,"You  have  no  access  to  that
   command")
           return PLUGIN_HANDLED
       }
       show_main_menu(id)
       return PLUGIN_HANDLED
   }
   /* configuration */
   public set_cvar_menu(){
       if (read_argc()>3&&cvar_num<MAX_CVARS){
           new data[32], a, cmax = read_argc() - 1
           read_argv(1,cvar_name_dat[cvar_num],31)
           for(a = 2; a < cmax;a++){
               read_argv(a,cvar_cmd_dat[cvar_cmd_num],63)
               cvar_misc_dat[cvar_num][a] = cvar_cmd_num++
               cvar_misc_dat[cvar_num][1] = a + 1
           }
           read_argv(a,data,31)
           cvar_misc_dat[cvar_num++][0] = read_flags(data)
       }
       return PLUGIN_HANDLED
   }
   public set_player_menu(){
       if (read_argc()>3&&ppl_num<MAX_PPL){
           new data[32]
           read_argv(1,ppl_name_dat[ppl_num],31)
           read_argv(2,ppl_cmd_dat[ppl_num],63)
           read_argv(3,data,31)
           ppl_misc_dat[ppl_num][1] = read_flags(data)
           read_argv(4,data,31)
           ppl_misc_dat[ppl_num++][0] = read_flags(data)
       }
       return PLUGIN_HANDLED
   }
   public set_team_menu(){
       if (read_argc()>3&&team_num<MAX_TEAM){
           new data[32]
           read_argv(1,team_name_dat[team_num],63)
           read_argv(2,team_cmd_dat[team_num],63)
           read_argv(3,data,31)
           team_misc_dat[team_num][1] = read_flags(data)
           read_argv(4,data,31)
           team_misc_dat[team_num++][0] = read_flags(data)
       }
       return PLUGIN_HANDLED
   }
   public set_cmd_menu(){
       if (read_argc()>4&&cmd_num<MAX_CMDS){
           new data[32]
           read_argv(1,cmd_name_dat[cmd_num],63)
           read_argv(2,cmd_cmd_dat[cmd_num],63)
           read_argv(3,data,31)
           cmd_misc_dat[cmd_num][1] = read_flags(data)
           read_argv(4,data,31)
           cmd_misc_dat[cmd_num++][0] = read_flags(data)
       }
       return PLUGIN_HANDLED
   }
   public set_map_menu(){
       if (read_argc()>1&&map_num<MAX_MAPS){
           new data[32]
           read_argv(1,data,31)
           if (is_map_valid(data))
               copy(map_dat[map_num++],31,data)
       }
       return PLUGIN_HANDLED
   }
   /* initialization */
   public plugin_init() {
       register_plugin("DoD Admin Menu","0.9.3","BouBou")
       register_menucmd(register_menuid("AMX           DoD          Admin
   Menu"),1023,"action_main_menu")
       register_menucmd(register_menuid("AMX                          Cmd
   Menu"),1023,"action_cmd_menu")
       register_menucmd(register_menuid("AMX                          Map
   Menu"),1023,"action_map_menu")
       register_menucmd(register_menuid("AMX                       Player
   Menu"),1023,"action_ppl_menu")
       register_menucmd(register_menuid("AMX                         Cvar
   Menu"),1023,"action_cvar_menu")
       register_menucmd(register_menuid("AMX                         Team
   Menu"),1023,"action_team_menu")
       register_srvcmd("amx_pmenu","set_player_menu")
       register_srvcmd("amx_vmenu","set_cvar_menu")
       register_srvcmd("amx_cmenu","set_cmd_menu")
       register_srvcmd("amx_amenu","set_map_menu")
       register_srvcmd("amx_tmenu","set_team_menu")
       register_clcmd("amx_menu","admin_menu",ADMIN_MENU,"-      displays
   admin. menu")
       server_cmd("exec addons/amx/menu.cfg")
       new mod_name[32]
       get_modname(mod_name,31)
       cstrike_running = equal(mod_name,"cstrike") ? true : false
       return PLUGIN_CONTINUE
   }
