
   /* AMXMOD script.
   *
   * Deathmatch AutoPilot by GeekGod
   * Derivative work (C)2003 GeekGod Industries, this file is provided as
   is (no warranties).
   *
   * Further references of DM   = Deathmatch
   *                       DMAP = Deathmatch Autopilot
   *
   * Usage:
   *       amx_dm_vote                     Initiate      a     DeathMatch
   AutoPilot vote session.
   *                                       If  DM  is enabled, it will be
   disabled if voted off.
   *       amx_dmauto_enable               Enable              DeathMatch
   AutoPilot.  Enforces round times.
   *       amx_dmauto_disable              Disable Deathmatch AutoPilot
   *       amx_dm_enable                   Enable    DeathMatch   without
   AutoPilot.  No round times.
   *       amx_dm_disable                  Disable   DeathMatch   without
   AutoPilot
   *       amx_dm_roundtime                Set  Round  Time  of Autopilot
   Matches
   *
   * Cvars:
   *       dm_ap_always_enable  1or0        1  =  DM  Autopilot is ALWAYS
   enabled.  Stick in server.cfg
   *       amx_respawn_roundtime           Amount   of   time  that  each
   Autopilot round lasts
   *       amx_spawn_protect               Protect  spawning  user  for a
   few seconds.  Player glows during
   *                                       protection period.
   *       amx_spawn_protect_time          Amount   of  time  to  protect
   newly spawned user
   *
   * DESCRIPTION:
   *   Deathmatch   Autopilot   takes   the   respawn  mod  to  the  next
   level.  Respawn sessions last for as long
   *   as  the  admin  defines  the amx_respawn_roundtime.   Players keep
   respawning during the time alloted
   *   and  a  warning  is  anounced  upon  round  end.   If you can stay
   standing at the end of the match
   *   your considered a bad ass.
   *
   *   During  the respawn period, a protection time is set on the person
   to protect from spawn campers
   *   if the amx_spawn_protect cvar is set to 1.   Default is on.
   *   In  addition,  the  person  glows  during  protection time so that
   others can avoid them.  Before
   *   this  change,  the  oppisite  team would simply camp in your spawn
   start area.  This adds a little
   *   more excitement.
   *
   *   DeathMatch  Autopilot  is based on Respawn code by f117bomb.  Vote
   code is based on
   *   Map Extend code written by OLO
   *
   *   Thanks  to the AMX community for their positive feedback and their
   continued
   *   desire for this mod to live on.
   *
   * CHANGES:
   *  12/23/03:  Fixed  line  variable  bug in dmautopilot.txt loader and
   corrected a respawn.sma missing line
   *  12/21/03: Changed to 80 second timer before reading dmautopilot.txt
   *  12/21/03: Forgot return PLUGIN_CONTINUE in many locations
   *  12/21/03:  Added  cstrike/addons/amx/dmautopilot.txt text file that
   controls automatic enabling
   *            of Deathmatch Autopilot mode.  List each map you want (on
   their own line) to
   *            automatically enable DMAP on.   Example:  fy_iceworld
   *  12/20/03: Included spacedudes respawn code after team select
   *  12/20/03: Merged respawn.sma changes from 08/2003
   *  12/20/03:  Added  notification  that  user will spawn in 20 seconds
   after team select
   *  12/20/03:  Added  amx_always_spawn cvar to control if should always
   spawn
   *  12/20/03: Fixed a few misc. bugs
   *   4/09/03: Fixed vote tracking bug
   *   4/09/03: Added cvar amx_respawn_vote_allowed to turn off voting
   *   4/07/03: Fixed join after selecting skin bug
   *   4/07/03: Fixed silly respawn protection bug
   *   4/03/03: Fixed amx_vm_vote spam bug
   *   4/03/03:  Added checks to see if amx_dm_vote has been triggered in
   last 20 mins when
   *            new vote initiated.
   *   4/01/03: Added stopvote command
   *   3/22/03:  Added  checks to make sure user is not spectating before
   spawning
   *   3/22/03: Added checks to make sure user is not disconnected before
   spawning
   *   3/22/03:  Added  dm_ap_always_enable  cvar  to  always  enable  DM
   Autopilot
   *   3/22/03:  Changed  script  to  use cvars for tracking round times,
   respawn protection time.
   *   3/22/03: Changed to use 60 minute tracker all the time.
   *   3/19/03: Now changes mp_buytime to 99999 if enabled.
   *   3/18/03: Initial release
   *   3/13/03: Attempted 10 second respawns, messes everything up. Needs
   more thinking
   *   3/13/03: Added glowing during spawn protection period
   *   3/12/03: Merged in vote code from extend map mod, rewritten to not
   use set_timer
   *            when vote not in progress
   *   3/12/03: Merged in spawn protection code by JustinHoMi and EJ
   *   3/12/03: Merged in few of the Quake sounds by DanRaZor
   *   3/11/03: Rewrote, added DMAP framework
   *    3/9/03: Initial version, rewritten from Respawn code by f117bomb
   *
   */
   #include <amxmod>
   new minuteslapsed
   new minutessincelastvote
   new dVotes
   new bool:dm_running
   new waiting_for_new_round
   new voteinprogress
   new votecancelled
   public TeamSelect(id)
   {
     if(get_cvar_num("amx_respawn_enable")         ==         1        ||
   get_cvar_num("amx_always_spawn") == 1)
     {
       new sId[32]
       sId[0] = id
       set_task(5.0,"respawn",0,sId,31)
     }
     return PLUGIN_CONTINUE
   }
   public death_msg()
   {
      /* User died, do they need to respawn? */
      if(get_cvar_num("amx_respawn_enable") == 0) return PLUGIN_CONTINUE
      /* Yep, needs to respawn.  Postpone with set_task for 0.5 */
      new vIndex = read_data(2)
      new svIndex[2]
      svIndex[0] = vIndex
      set_task(0.5,"respawn",0,svIndex,2)
      return PLUGIN_CONTINUE
   }
   public client_putinserver(id)
   {
      new svIndex[2]
      svIndex[0] = id
      set_task(10.0,"client_notify",0,svIndex,2)   /*  notify  client  of
   match */
      return PLUGIN_CONTINUE
   }
   public client_notify(svIndex[])
   {
      new vIndex = str_to_num(svIndex)
      if(get_cvar_num("amx_respawn_enable") == 1)
      {
           client_cmd(vIndex,  "echo  ^"  * Deathmatch Autopilot Match in
   progress!  10 seconds until spawn!  ^"")
      }
      return PLUGIN_CONTINUE
   }
   public plugin_end()
   {
           amx_dmauto_disable()
           return PLUGIN_CONTINUE
   }
   public respawn(svIndex[])
   {
      /* Respawn user */
      new vIndex = svIndex[0]
      if(get_user_team(vIndex)  ==  3  ||  is_user_alive(vIndex))  return
   PLUGIN_CONTINUE
      client_print(vIndex,print_chat,"[AMX]     DeathMatch     Autopilot:
   Respawning!  Get ready to fight!")
      user_spawn(vIndex)
      /* respawn health protection */
      if(get_cvar_num("amx_respawn_enable") == 1)
      {
              if(get_cvar_num("amx_spawn_protect")==1)
              {
                   if(get_cvar_num("amx_dm_spam") == 1)
                   {
                           client_print(vIndex,print_chat,"*    [AMX]   *
   Deathmatch Autopilot respawn health protection enabled ")
                   }
                   set_user_rendering(vIndex,kRenderFxGlowShell,255,255,0
   ,kRenderTransAlpha,255)
                   set_user_health(vIndex,9999)
                   set_task(5.0,"protect_off",0,svIndex,31)
              }
      }
      return PLUGIN_CONTINUE
   }
   public plugin_precache()
   {
           /* Cache sounds on connect */
           precache_sound("misc/prepare.wav")
           precache_sound("misc/hd_horn.wav")
           return PLUGIN_CONTINUE
   }
   public dmvote(id){
           /* Show vote */
           dVotes = 0
           if(minutessincelastvote<10)
           {
                   client_print(id,print_chat,"*  You  just  triggered  a
   vote.  Chill.")
                   return PLUGIN_CONTINUE
           }
           voteinprogress=1
           minutessincelastvote=0
           new mapname[64]
           get_mapname(mapname,63)
           new menu[256]
           if (dm_running)
                   format(menu,255,"\yAMX  Enable  deathmatch  autopilot?
   \w^n^n1.  Yes^n2.  No")
           else
                   format(menu,255,"AMX         Enable         deathmatch
   autopilot?  \w^n^n1. Yes^n2.  No")
           show_menu(0,(1<<0)|(1<<1),menu,10)
           set_task(10.0,"dm_votes")
           client_cmd(0,"spk doorbell")
           client_print(0,print_chat,"*   Voting   for   the   Deathmatch
   Autopilot Started.")
           return PLUGIN_CONTINUE
   }
   public dm_votes(){
           /* Called during final phase of voting */
           if(votecancelled==1)
           {
                   client_print(0,print_chat,"*     Voting     cancelled.
   Deathmatch Autopilot will not be enabled.")
                   votecancelled=0
                   return PLUGIN_HANDLED
           }
           new players[32], inum
           get_players(players,inum,"c")
           new Float:step_time = get_cvar_float("amx_dm_step")
           new Float:mp_time = get_cvar_float("mp_timelimit")
           new Float:vote_ratio = get_cvar_float("amx_dm_ratio")
           new  Float:result_v  =  inum ? (float(dVotes) / float(inum)) :
   0.0
           if (result_v >= vote_ratio)
           {
                   client_print(0,print_chat,"*     Voting    successful.
   Deathmatch Autopilot will be enabled.")
                   new Float:new_time = mp_time + step_time
                   set_cvar_float("mp_timelimit",new_time)
                   if(get_cvar_num("amx_dm_enable") == 0)
                   {
                           voteinprogress=0
                           amx_dmauto_enable()
                   }
           }
              else
           {
                   client_print(0,print_chat,"* Voting failed. Deathmatch
   Autopilot will not be enabled.")
                   voteinprogress=0
                   amx_dmauto_disable()
           }
           return PLUGIN_HANDLED
   }
   public dm_count(id,key){
           /* Counts votes as they come in */
           if(votecancelled==1) return PLUGIN_HANDLED
           new name[32]
           get_user_name(id,name,31)
           client_print(0,print_chat,"*     %s    voted    %s    enabling
   deathmatch",name,key ? "against" : "for")
           if (!key) ++dVotes
           return PLUGIN_HANDLED
   }
   public amx_dm_vote(id)
   {
           /* Initiate vote */
           new name[32]
           get_user_name(id,name,31)
           if(get_cvar_num("amx_respawn_vote_allowed") == 0)
           {
                   client_print(id,print_chat,"*   Deathmatch   vote  not
   allowed.",name)
                   return PLUGIN_HANDLED
           }
           if(voteinprogress == 1)
           {
                   client_print(id,print_chat,"[AMX]    DeathMatch   vote
   already in progress.  Chill.")
                   return PLUGIN_HANDLED
           }
           client_print(0,print_chat,"*    %s    voted    for    enabling
   deathmatch",name)
           dmvote(id)
           return PLUGIN_HANDLED
   }
   public mslvc(ids[])
   {
           minutessincelastvote++
           new param[1]
           param[0]=0
           set_task(5.0,"mslvc",853,param,2)
           return PLUGIN_CONTINUE
   }
   public skin_selected(id)
   {
      new parm[2]
      parm[0]=id
      set_task(5.0,"wait_alive",853,parm,2)
      return PLUGIN_CONTINUE
   }
   public wait_alive(parm[]){
      new id = parm[0]
      if (!is_user_alive(id))
      {
           respawn(parm)
      }
      return PLUGIN_CONTINUE
   }
   public plugin_init()
   {
      /* ...from jailbreak... */
      register_menucmd(register_menuid("Terrorist_Select"),1023,"skin_sel
   ected")
      register_menucmd(register_menuid("CT_Select"),1023,"skin_selected")
      register_menucmd(-26,1023,"skin_selected")      // VGUI
      register_menucmd(-27,1023,"skin_selected")      // VGUI
      /* Setup plugin environment */
      register_plugin("AutoDeathmatch","0.2.1","[bsd] GeekGod")
      register_menucmd(register_menuid("AMX                        Enable
   "),(1<<0)|(1<<1),"dm_count")
      register_cvar("amx_dm_ratio","0.4")
      register_cvar("amx_always_spawn","1")
      register_cvar("amx_respawn_vote_allowed","1")
      register_cvar("amx_dm_max","90")
      register_cvar("amx_dm_step","15")
      register_cvar("amx_dm_answers","0")
      register_clcmd("amx_cancelvote","stopvote")
      /*  To  enable  plugin all the time, stick dm_ap_always_enable 1 in
   server.cfg */
      register_cvar("dm_ap_always_enable","0")
      register_event("SendAudio","roundender","a","2=%!MRAD_terwin","2=%!
   MRAD_ctwin","2=%!MRAD_rounddraw")
      register_cvar("amx_respawn_enable","0")
      register_cvar("amx_respawn_auto","0")
      register_cvar("amx_respawn_roundtime","2.0")
      register_cvar("amx_respawm_time","0.5")
      register_cvar("amx_spawn_protect_time","2.0")
      register_cvar("amx_spawn_protect","1")
      register_cvar("amx_dm_spam","1")
      register_clcmd("amx_dm_enable","amx_dm_enable",ADMIN_LEVEL_H,"Turns
   on deathmatch")
      register_clcmd("amx_dm_disable","amx_dm_disable",ADMIN_LEVEL_H,"Tur
   ns off deathmatch")
      register_clcmd("amx_dmauto_enable","amx_dmauto_enable",ADMIN_LEVEL_
   H,"Turns on Deathmatch Autopilot")
      register_clcmd("amx_dmauto_disable","amx_dmauto_disable",ADMIN_LEVE
   L_H,"Turns off Deathmatch Autopilot")
      register_clcmd("amx_dm_vote","amx_dm_vote")
      register_clcmd("amx_dm_roundtime","amx_dm_roundtime")
      register_event("DeathMsg","death_msg","a")
      register_event("ShowMenu","TeamSelect","b","4&Team_Select")
      register_event("VGUIMenu","TeamSelect","b","1=2")
      new mod_name[32]
      get_modname(mod_name,31)
      dm_running = equal(mod_name,"cstrike") ? true : false
      new ids[1]
      ids[0]=0
      minutessincelastvote=5000
      if(get_cvar_num("dm_ap_always_enable") == 1)
      {
           set_cvar_num("amx_respawn_auto",1)
      }
      set_task(60.0, "keeptrackoftime", 0, ids, 1)
      new param[1]
      param[0]=0
      mslvc(param)
      set_task(80.0, "startround_event", 0, ids, 1)
      /* return PLUGIN_CONTINUE */
   }
   public startround_event(id)
   {
           /*
                   check to see if user defined dmautopilot.txt text file
   which
                   defines maps to automatically enable dmautopilot on
           */
           new pathtomaps[32] = "addons/amx/dmautopilot.txt"
           new currentmap[32],linestr[256],maptext[32]
           new line = 0, found_this_map = 0, stextsize = 0
           get_mapname(currentmap,31)
           if (file_exists(pathtomaps) == 1)
           {
                   while(read_file(pathtomaps,line,linestr,255,stextsize)
   &&line<200)
                   {
                           format(maptext,31,"%s",linestr)
                           if(equali(maptext,currentmap))
                           {
                                   /*   map   is   defined,  lets  enable
   dmautopilot */
                                   if(get_cvar_num("amx_respawn_auto") ==
   0 && waiting_for_new_round == 0)
                                   {
                                           amx_dmauto_enable()
                                           found_this_map = 1
                                           client_print(0,print_chat,"*
   Found this map in Deathmatch Autopilot auto enable file.")
                                           line = 99999
                                   } else
                                   {
                                           client_print(0,print_chat,"*
   either amx_respawn_auto !== 0 or waiting_for_new_round !== 0")
                                   }
                           }
                           line++
                   }
           } else
           {
                   client_print(0,print_chat,"*    Could    not    locate
   dmautopilot.txt")
           }
           if(found_this_map  ==  0) client_print(0,print_chat,"* Map was
   not found in Deathmatch Autopilot auto enable file.")
           return PLUGIN_CONTINUE
   }
   public amx_dm_roundtime()
   {
           new text[32]
           read_args(text,31)
           set_cvar_float("amx_respawn_roundtime",floatstr(text))
           return PLUGIN_HANDLED
   }
   public amx_dm_enable()
   {
           /* Manually turn on DeathMatch */
           if(get_cvar_num("amx_dm_spam") == 1)
           {
                   show_hudmessage(0,   "Deathmatch  Autopilot  by  [bsd]
   GeekGod: Get ready to Fight!");
           }
           set_cvar_num("amx_respawn_enable",1)
           new ids[1]
           ids[0]=0
           server_cmd("mp_freezetime 0")
           return PLUGIN_HANDLED
   }
   public amx_dm_disable()
   {
           /* Manually turn off DeathMatch */
           if(get_cvar_num("amx_dm_spam") == 1)
           {
                   show_hudmessage(0,  "Deathmatch by [bsd] GeekGod: Good
   Fight!");
           }
           server_cmd("mp_freezetime 0")
           set_cvar_num("amx_respawn_enable",0)
           set_cvar_float("mp_buytime",0.3)
           return PLUGIN_HANDLED
   }
   public amx_dmauto_enable()
   {
           /* Manually turn on DeathMatch Autopilot */
           if(get_cvar_num("amx_dm_spam") == 1)
           {
                   show_hudmessage(0,  "Deathmatch  by GeekGod: Get ready
   to Fight next Round!");
           }
           waiting_for_new_round = 1
           return PLUGIN_HANDLED
   }
   public amx_dmauto_disable()
   {
           /* Manually disable Deathmatch Autopilot */
           if(get_cvar_num("amx_dm_spam") == 1)
           {
                   show_hudmessage(0,  "Deathmatch  Autopilot by GeekGod:
   Good Fight!");
           }
           set_cvar_num("amx_respawn_enable",0)
           set_cvar_num("amx_respawn_auto",0)
           set_cvar_float("mp_buytime",0.3)
           server_cmd("mp_freezetime 5")
           return PLUGIN_HANDLED
   }
   public game_restart()
   {
           /* Round start after Deathmatch auto ending */
           if(get_cvar_num("amx_respawn_auto") == 1)
           {
                   if(get_cvar_num("amx_respawn_enable") == 0)
                   {
                           if(get_cvar_num("amx_dm_spam") == 1)
                           {
                                   show_hudmessage(0,  "DMAP  by GeekGod:
   Deathmatch Enabled!");
                           }
                           set_cvar_num("amx_respawn_enable",1)
                   }
           }
           return PLUGIN_CONTINUE
   }
   public roundender()
   {
           new dm_ap_always_enable
           dm_ap_always_enable=get_cvar_num("dm_ap_always_enable")
           new numofplayers,players[32]
           get_players(players, numofplayers, "")
           if(dm_ap_always_enable) waiting_for_new_round=1
           if(waiting_for_new_round == 1 && numofplayers < 6)
           {
                   if(numofplayers < 6)
                   {
                           client_print(0,print_chat,"[AMX]  * DeathMatch
   will begin when there are more than 5 players.")
                   }
                     else
                   {
                           client_print(0,print_chat,"[AMX]  * DeathMatch
   Autopilot will begin next round.")
                   }
           }
              else
           {
                   if(waiting_for_new_round == 1)
                   {       /* Waiting until Next round to start */
                           waiting_for_new_round = 0
                           minuteslapsed=0
                           server_cmd("mp_freezetime 0")
                           set_cvar_num("amx_respawn_auto",1)
                           set_cvar_num("mp_buytime",99999)
                   }
                   if(dm_ap_always_enable == 1)
                   {       /* Always start */
                           minuteslapsed=0
                           server_cmd("mp_freezetime 0")
                           set_cvar_num("amx_respawn_auto",1)
                           set_cvar_num("mp_buytime",99999)
                   }
                   /* Before round start, tell them to get ready  */
                   if(get_cvar_num("amx_respawn_auto") == 1)
                   {
                           if(get_cvar_num("amx_respawn_enable") == 0)
                           {
                                   if(get_cvar_num("amx_dm_spam") == 1)
                                   {
                                           show_hudmessage(0,   "DMAP  by
   GeekGod: Get ready to Fight!");
                                   }
                                   new ids[1]
                                   ids[0]=0
                                   set_task(5.0, "renablers", 0, ids, 1)
                           }
                   }
           }
           return PLUGIN_CONTINUE
   }
   public renablers()
   {
           if(get_cvar_num("amx_respawn_auto") == 1)
           {
                   /*  Renable  Deathmatch  mode after round begins if in
   autopilot mode */
                   set_cvar_num("amx_respawn_enable",1)
                   client_cmd(0,"spk misc/prepare")
                   minuteslapsed=0
           }
           return PLUGIN_CONTINUE
   }
   public keeptrackoftime()
   {
           /*  First  check to see if there is less than 2 mintes, if so,
   disable mods. */
           if(get_cvar_num("dm_ap_always_enable") == 1)
           {
                   if(get_cvar_num("amx_respawn_auto") == 0)
                   {
                           client_print(0,print_chat,"*      [AMX]      *
   Deathmatch Autopilot will start next round.")
                   }
           }
           new timeleft = get_timeleft()
           if(get_cvar_num("amx_respawn_enable") == 1)
           {
                   if(get_cvar_num("mp_timelimit") > 0)
                   {
                           if(timeleft < 30)
                           {
                                   /*   Less  than  2  minutes,  time  to
   disable groovy mod */
                                   if(get_cvar_num("amx_dm_spam") == 1)
                                   {
                                           show_hudmessage(0,   "DMAP  by
   GeekGod: 2 Minute Marker -- Good Fight!");
                                   }
                                   set_cvar_num("amx_respawn_enable",0)
                                   set_cvar_num("amx_respawn_auto",0)
                                   server_cmd("mp_freezetime 5")
                           }
                   }
           }
           /* Called once a minute to keep track of autopilot time */
           if(get_cvar_num("amx_respawn_enable") == 1)
           {
                   ++minuteslapsed
                   if(minuteslapsed >
   get_cvar_float("amx_respawn_roundtime"))
                   {
                           if(get_cvar_num("amx_respawn_auto") == 1)
                           {
                                   if(get_cvar_num("amx_dm_spam") == 1)
                                   {
                                           show_hudmessage(0,   "DMAP  by
   GeekGod: Last Round!");
                                   }
                                   client_cmd(0,"spk misc/hd_horn")
                                   set_cvar_num("amx_respawn_enable",0)
                           }
                   }
                     else
                   {
                           if(get_cvar_float("amx_respawn_roundtime")-min
   uteslapsed == 1)
                           {
                                   if(get_cvar_num("amx_dm_spam") == 1)
                                   {
                                           show_hudmessage(0, "DeathMatch
   mod By GeekGod: 1 minute has passed!");
                                   }
                           }
                   }
           }
           new ids[1]
           ids[0]=0
           set_task(60.0, "keeptrackoftime", 0, ids, 1)
           return PLUGIN_CONTINUE
   }
   public protect_off(svIndex[])
   {
           /* Turn off spawn Protection */
           new vIndex = str_to_num(svIndex)
           if(get_cvar_num("amx_spawn_protect")==1)
           {
                   set_user_health(vIndex,100)
                   set_user_rendering(vIndex,kRenderFxGlowShell,0,0,0,kRe
   nderTransAlpha,255)
                   if(get_cvar_num("amx_dm_spam") == 1)
                   {
                           client_print(vIndex,print_chat,"*    [AMX]   *
   Deathmatch Autopilot respawn health protection disabled ")
                   }
           }
           return PLUGIN_CONTINUE
   }
   public stopvote(id){
      if (!(get_user_flags(id)&ADMIN_MAP)){
         client_print(id,print_console,"[AMX]  You have no access to that
   command")
         return PLUGIN_HANDLED
      }
      if(voteinprogress==0) return PLUGIN_CONTINUE
      new name[32]
      get_user_name(id,name,31)
      votecancelled=1
      client_print(0,print_chat,"[AMX]  %s   has  disabled  the  vote  in
   progress.",name)
      return PLUGIN_CONTINUE
   }
