// EventScripts corelib v0.90b
//  This library is used and shared by other scripts.
//  It includes tons of contributions from the EventScripts Community. Thanks!

block load
{
  es_xsetinfo _tempcore 0
  es_xsetinfo _exists 0

  // register the "refcount" command
  es_xdoblock corelib/refcount_register
  // register the "stack" command
  es_xdoblock corelib/stack_register
  // register the "while" command
  es_xdoblock corelib/while_register
  // register the "cheatexec" command
  es_xdoblock corelib/cheatexec_register
  // register the "getplayerinfo" command
  es_xdoblock corelib/getplayerinfo_register
  // register the "downloadable" command
  es_xdoblock corelib/downloadable_register
  // register the "getrandplayer" command
  es_xdoblock corelib/randplayer_register
  // register the "profilecmd" command
  es_xdoblock corelib/profilecmd_register
  // register the "isnull" command
  es_xdoblock corelib/isnull_register
  // register the "keygroupremove" command
  es_xdoblock corelib/keygroupremove_register
  // register the "forcecase" command
  es_xdoblock corelib/forcecase_register
  // registers the "keymath" command
  es_xdoblock corelib/keymath_register
  // registers the "nearcoord" command
  es_xdoblock corelib/nearcoord_register
  // register the "vote" command
  es_xdoblock corelib/vote_register


  // load subscripts
  es_xload corelib/linkedlist
  es_xload corelib/foreach
  es_xload corelib/repeat
  es_xload corelib/average
  es_xload corelib/score
  es_xload corelib/xalias
  es_xload corelib/textlib
  es_xload corelib/playergetset
  es_xload corelib/keyfilter
  es_xload corelib/uniqueid
  es_xload corelib/getbotname
  es_xload corelib/damage
  es_xload corelib/playervar
  es_xload corelib/timelib
  es_xload corelib/statlog
  es_xload corelib/keygroupsort
}

block unload
{
  // unload subscripts
  es_xunload corelib/keygroupsort
  es_xunload corelib/statlog
  es_xunload corelib/timelib
  es_xunload corelib/playervar
  es_xunload corelib/damage
  es_xunload corelib/getbotname
  es_xunload corelib/uniqueid
  es_xunload corelib/keyfilter
  es_xunload corelib/playergetset
  es_xunload corelib/textlib
  es_xunload corelib/xalias
  es_xunload corelib/score
  es_xunload corelib/average
  es_xunload corelib/repeat
  es_xunload corelib/foreach
  es_xload corelib/linkedlist
}

// eventscripts_noisy reference count. Scripts that require noisy can be unloaded safely, so that is doesn't break other scripts which require noisy

block noisy_on
{
   refcount increment eventscripts_noisy
}
block noisy_off
{
   refcount decrement eventscripts_noisy
}

block while_register
{
  // variables used by while
  es_xsetinfo _whilestatement 0
  es_xsetinfo _whilecmd 0
  es_xsetinfo _whilecond 0
  // check to see if while exists
  es_xexists _exists command while
  if (server_var(_exists) = 0) do
  {
    es_xregcmd while corelib/while "Allows you to loop with a condition and a block."
  }
}

// stacks are supported entirely through corelib
// the following block handles everything for the 'stack' console command
block while
{
  // get condition
  // get command
  // create alias self "if cond then command;self"
  es es_xif (server_var(_whilestatement) = 0) do
  {
    stack create _whilestack
  }
  else do
  {
    es stack push _whilestack server_var(_whilestatement)
    es stack push _whilestack server_var(_whilecmd)
  }
  es_xgetargv _whilecond 1
  es es_xif (server_var(_whilecond) != 0) do
  {
    es_xgetargv _whilecmd 2
    es es_xif (server_var(_whilecmd) != 0) do
    {
      es es_xformat _whilecmd "%1;_whileloop" server_var(_whilecmd)
      es alias _whilecmd server_var(_whilecmd)
      es es_xformat _whilestatement "es es_xif (%1) then _whilecmd" server_var(_whilecond)
      es alias _whileloop server_var(_whilestatement)
      _whileloop
    }
  }
  stack pop _whilecmd _whilestack
  es alias _whilecmd server_var(_whilecmd)
  stack pop _whilestatement _whilestack
  es alias _whileloop server_var(_whilestatement)
}

// exists to test the while loop command, ignore
// es_xdoblock corelib/while_test to test the while command

block while_test
{
  log on
  profile begin while_test
  es_setinfo _while_test2 9
  while "server_var(_while_test2) > 0" "es_xdoblock corelib/while_test2"
  es es_xif (server_var(_while_test2) > 0) then es_log FAILURE: while test failed. RED RED RED RED
  es_setinfo _while_test3 5
  while "server_var(_while_test3) > 0" "es_xdoblock corelib/while_test3"
  es es_xif (server_var(_while_test2) > 0) then es_log FAILURE: while test2 failed. RED RED RED RED
  es es_xif (server_var(_while_test3) > 0) then es_log FAILURE: while test3 failed. RED RED RED RED
  es_setinfo _while_test4 0
  while "server_var(_while_test4) < 6" "es_xmath _while_test4 + 1"
  es es_xif (server_var(_while_test3) > 0) then es_log FAILURE: while test4 failed. RED RED RED RED
  profile end while_test
}

// exists to test the while loop command, ignore
block while_test2
{
  es_log _while_test2: server_var(_while_test2)
  es_xmath _while_test2 - 1
}

// exists to test the while loop command, ignore
block while_test3
{
  es_log _while_test3: server_var(_while_test3)
  // call an inner while loop
  es_setinfo _while_test2 4
  while "server_var(_while_test2) > 0" "es_xdoblock corelib/while_test2"

  es_setinfo _while_test2 5
  while "server_var(_while_test2) > 0" "es_xdoblock corelib/while_test2"

  es_xmath _while_test3 - 1
}

block stack_register
{
  es_xsetinfo _stackname 0
  es_xsetinfo _stackexists 0
  es_xsetinfo _stackvalue 0
  es_xsetinfo _stackvar 0

  // if the stack command isn't already registered
  es_xexists _exists command stack
  if (server_var(_exists) = 0) do
  {
    es_xregcmd stack corelib/stack "Allows you to do various stack operations with a named stack. Supports create, delete, pop, push, getlength."
  }
}

block stack
{
  // get the operation they want to do
  es_xgetargv _tempcore 1
  es_format _tempcore "corelib/stack_%1" server_var(_tempcore)
  es_doblock server_var(_tempcore)
}

// stack create stackname
block stack_create
{
  // get the stack name
  es_xgetargv _tempcore 2
  es es_xif (server_var(_tempcore) != 0) do
  {
    es es_xformat _stackname "stack_%1" server_var(_tempcore)
    es es_xexists _stackexists keygroup server_var(_stackname)
    es es_xif (server_var(_stackexists) = 0) do
    {
      es es_xkeygroupcreate server_var(_stackname)
      es es_xkeycreate server_var(_stackname) private
      es es_xkeysetvalue server_var(_stackname) private type stack
      es es_xkeysetvalue server_var(_stackname) private head 0
      es es_xkeysetvalue server_var(_stackname) private tail -1
      es es_xkeysetvalue server_var(_stackname) private length 0
    }
    else do
    {
      es es_xdbgmsg 0 stack: The stack already exists: server_var(_stackname)
    }
  }
  else do
  {
    es_xdbgmsg 0 stack: Invalid arguments to stack create. Syntax: stack create <stackname>
  }
}

// stack push stackname value
block stack_push
{
  // get the stack name
  es_xgetargv _tempcore 2
  es es_xif (server_var(_tempcore) != 0) do
  {
    es es_xformat _stackname "stack_%1" server_var(_tempcore)
    es es_xexists _stackexists keygroup server_var(_stackname)
    es es_xif (server_var(_stackexists) = 1) do
    {
      es_xgetargv _stackvalue 3
      // new value is tail + 1
      es es_xkeygetvalue _tempcore server_var(_stackname) private tail
      es_xmath _tempcore + 1
      es es_xkeycreate server_var(_stackname) server_var(_tempcore)
      // add the stack value to the stack
      es es_xkeysetvalue server_var(_stackname) server_var(_tempcore) value server_var(_stackvalue)
      // update the new tail value
      es es_xkeysetvalue server_var(_stackname) private tail server_var(_tempcore)
      // cheat: length is tail + 1
      es_xmath _tempcore + 1
      es es_xkeysetvalue server_var(_stackname) private length server_var(_tempcore)
    }
    else do
    {
      es es_xdbgmsg 0 stack: The stack doesn't exist: server_var(_stackname)
    }
  }
  else do
  {
    es_xdbgmsg 0 stack: Invalid arguments to stack push. Syntax: stack push <stackname> <value>
  }
}

// stack pop var stackname
block stack_pop
{
  // get the stack name
  es_xgetargv _tempcore 3
  es es_xif (server_var(_tempcore) != 0) do
  {
    es es_xformat _stackname "stack_%1" server_var(_tempcore)
    es es_xexists _stackexists keygroup server_var(_stackname)
    es es_xif (server_var(_stackexists) = 1) do
    {
      es_xgetargv _stackvar 2
      // value is @ tail
      es es_xkeygetvalue _tempcore server_var(_stackname) private tail
      es es_xif (server_var(_tempcore) > -1) do
      {
        es es_xkeygetvalue server_var(_stackvar) server_var(_stackname) server_var(_tempcore) value
        es es_xkeydelete server_var(_stackname) server_var(_tempcore)
        // cheat: length is tail + 1
        es es_xkeysetvalue server_var(_stackname) private length server_var(_tempcore)
        es_xmath _tempcore - 1
        // update the new tail value
        es es_xkeysetvalue server_var(_stackname) private tail server_var(_tempcore)
      }
      else do
      {
        es es_xdbgmsg 0 stack: ERROR: Pop attempt when stack empty.
      }
    }
    else do
    {
      es es_xdbgmsg 0 stack: The stack doesn't exist: server_var(_stackname)
    }
  }
  else do
  {
    es_xdbgmsg 0 stack: Invalid arguments to stack pop. Syntax: stack pop <var> <stackname>
  }
}

// stack delete stackname
block stack_delete
{
  // get the stack name
  es_xgetargv _tempcore 2
  es es_xif (server_var(_tempcore) != 0) do
  {
    es es_xformat _stackname "stack_%1" server_var(_tempcore)
    es es_xexists _stackexists keygroup server_var(_stackname)
    es es_xif (server_var(_stackexists) = 1) do
    {
      es es_xkeygroupdelete server_var(_stackname)
    }
    else do
    {
      es es_xdbgmsg 0 stack: The stack doesn't exist: server_var(_stackname)
    }
  }
  else do
  {
    es_xdbgmsg 0 stack: Invalid arguments to stack delete. Syntax: stack delete <stackname>
  }
}

// stack getlength var stackname
block stack_getlength
{
  // get the stack name
  es_xgetargv _tempcore 3
  es es_xif (server_var(_tempcore) != 0) do
  {
    es es_xformat _stackname "stack_%1" server_var(_tempcore)
    es es_xexists _stackexists keygroup server_var(_stackname)
    es es_xif (server_var(_stackexists) = 1) do
    {
      es_xgetargv _stackvar 2
      es es_xkeygetvalue server_var(_stackvar) server_var(_stackname) private length
    }
    else do
    {
      es es_xdbgmsg 0 stack: The stack doesn't exist: server_var(_stackname)
    }
  }
  else do
  {
    es_xdbgmsg 0 stack: Invalid arguments to stack getlength. Syntax: stack getlength <var> <stackname>
  }
}

// this function exists to test the stack commands, you can ignore
block stack_test
{
  log on
  profile begin stack_test
  es_xsetinfo _testcore 0
  stack create teststack
  stack push teststack 1
  stack pop _testcore teststack
  es es_xif (server_var(_testcore) != 1) then es_log create/push/pop failed. RED RED RED
  stack push teststack 1
  stack push teststack 2
  stack push teststack "hello"
  stack push teststack 3
  stack push teststack 4
  stack pop _testcore teststack
  es es_xif (server_var(_testcore) != 4) then es_log multi-push/pop failed. RED RED RED
  stack pop _testcore teststack
  stack pop _testcore teststack
  es es_xif (server_var(_testcore) != "hello") then es_log multi-push/pop failed2. RED RED RED
  stack push teststack 5
  stack pop _testcore teststack
  es es_xif (server_var(_testcore) != "5") then es_log multi-push/pop failed3. RED RED RED
  stack pop _testcore teststack
  es es_xif (server_var(_testcore) != "2") then es_log multi-push/pop failed4. RED RED RED
  stack getlength _testcore teststack
  es es_xif (server_var(_testcore) != "1") then es_log getlength failed: server_var(_testcore) RED RED RED
  stack delete teststack
  profile end stack_test
}

// called to register the cheatexec command
block cheatexec_register
{
  // if the stack command isn't already registered
  es_xexists _tempcore command cheatexec
  if (server_var(_tempcore) = 0) do
  {
    // while variables
    es_xsetinfo _cheatbak 0
    es_xsetinfo _cheatuserid 0
    es_xsetinfo _cheatcmd 0
    es_xregcmd cheatexec corelib/cheatexec "Have player execute a cheat command"
  }
}

block refcount_register
{
  // variables used by refcount
  es_xsetinfo _refc_ac 0
  es_xsetinfo _refc_a1 0
  es_xsetinfo _refc_a2 0
  es_xsetinfo _refc_curcount 0
  es_xsetinfo _refc_a3 0
  es_xsetinfo _refc_old 0
  // check to see if refcount exists
  es_xexists _exists command refcount
  es es_xif (server_var(_exists) = 0) do 
  {
    es_xregcmd refcount corelib/refcount "Increase or decrease the reference count for a variable"
    es_xkeygroupcreate _refc_refs
  }
}

block refcount
{
  es_xgetargc _refc_ac
  es es_xif (server_var(_refc_ac) > 1) do
  { 
    es_xgetargv _refc_a1 1
    es es_xif (server_var(_refc_a1) = increment) do
    { 
      es_xgetargv _refc_a2 2
      es es_xif (server_var(_refc_ac) > 2) do
      {
        es_xsetinfo _tempcore 0
        es es_xexists _tempcore variable server_var(_refc_a2)
        es es_xif (server_var(_tempcore) = 1) do
        {
          es_xsetinfo _tempcore 0
          es es_xexists _tempcore key _refc_refs server_var(_refc_a2)
          es es_xif (server_var(_tempcore) = 1) do
          {
            es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a2) refcount
            es_xmath _refc_curcount + 1
            es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount server_var(_refc_curcount)
          }
          else do
          {
            es es_xkeycreate _refc_refs server_var(_refc_a2)
            es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount 1
            es es_xkeysetvalue _refc_refs server_var(_refc_a2) oldvalue server_var(server_var(_refc_a2))
            es_xsetinfo _refc_curcount 1
          }
          es es_xif (server_var(_refc_ac) > 3) do
          {
            es_xgetargv _refc_a3 3
            es es_xif (server_var(_refc_a3) = 1) do
            {
              es es_xforcevalue server_var(_refc_a2) 1
            }
            else do
            {
              es server_var(_refc_a2) 1
            }
          }
          else do
          {
            es server_var(_refc_a2) 1
          }
          es es_xdbgmsg 1 [refcount] The ref. count for server_var(_refc_a2) is now server_var(_refc_curcount)!
        }
        else do
        {
          es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
        }
      }
      else do
      {
        es_xdbgmsg 0 [refcount] You must provide a return CVAR!
      }
    }
    else do
    {
      es es_xif (server_var(_refc_a1) = decrement) do
      {
        es_xgetargv _refc_a2 2
        es es_xif (server_var(_refc_ac) > 2) do
        {
          es_xsetinfo _tempcore 0
          es es_xexists _tempcore variable server_var(_refc_a2)
          es es_xif (server_var(_tempcore) = 1) do
          {
            es_xsetinfo _tempcore 0
            es es_xexists _tempcore key _refc_refs server_var(_refc_a2)
            es es_xif (server_var(_tempcore) = 1) do
            {
              es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a2) refcount
              es_xmath _refc_curcount - 1
              es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount server_var(_refc_curcount)
              es es_xif (server_var(_refc_curcount) <= 0) do
              {
                es_xsetinfo _refc_curcount 0
                es es_xkeysetvalue _refc_refs server_var(_refc_a2) refcount server_var(_refc_curcount)
                es es_xkeygetvalue _refc_old _refc_refs server_var(_refc_a2) oldvalue
                es es_xif (server_var(_refc_ac) > 3) do
                {
                  es_xgetargv _refc_a3 3
                  es es_xif (server_var(_refc_a3) = 1) do
                  {
                    es es_xforcevalue server_var(_refc_a2) server_var(_refc_old)
                  }
                  else do
                  {
                    es server_var(_refc_a2) server_var(_refc_old)
                  }
                }
                else do
                {
                  es server_var(_refc_a2) server_var(_refc_old)
                }
              }
              else do
              {
                es es_xif (server_var(_refc_ac) > 3) do
                {
                  es_xgetargv _refc_a3 3
                  es es_xif (server_var(_refc_a3) = 1) do
                  {
                    es es_xforcevalue server_var(_refc_a2) 1
                  }
                  else do
                  {
                    es server_var(_refc_a2) 1
                  }
                }
                else do
                {
                  es server_var(_refc_a2) 1
                }
              }
              es es_xdbgmsg 1 [refcount] The ref. count for server_var(_refc_a2) is now server_var(_refc_curcount)!
            }
            else do
            {
              es es_xdbgmsg 0 [refcount] There is no ref. count for server_var(_refc_a2)!
            }
          }
          else do
          {
            es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
          }
        }
        else do
        {
          es_xdbgmsg 0 [refcount] You must provide a return CVAR!
        }
      }
      else do
      {
        es es_xif (server_var(_refc_a1) = count) do
        {
          es es_xif (server_var(_refc_ac) > 2) do
          {
            es es_xif (server_var(_refc_ac) > 3) do
            {
              es_xgetargv _refc_a2 2
              es_xsetinfo _tempcore 0
              es es_xexists _tempcore variable server_var(_refc_a2)
              es es_xif (server_var(_tempcore) = 1) do
              {
                es_xgetargv _refc_a3 3
                es_xsetinfo _tempcore 0
                es es_xexists _tempcore variable server_var(_refc_a3)
                es es_xif (server_var(_tempcore) = 1) do
                {
                  es_xsetinfo _tempcore 0
                  es es_xexists _tempcore key _refc_refs server_var(_refc_a3)
                  es es_xif (server_var(_tempcore) = 1) do
                  {
                    es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a3) refcount
                    es server_var(_refc_a2) server_var(_refc_curcount)
                    es es_xdbgmsg 0 [refcount] The ref. count for server_var(_refc_a3) is server_var(_refc_curcount)!
                  }
                  else do
                  {
                    es server_var(_refc_a2) server_var(_refc_curcount)
                    es es_xdbgmsg 0 [refcount] There is no ref. count for server_var(_refc_a2)!
                  }
                }
                else do
                {
                  es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
                }
              }
              else do
              {
                es es_xdbgmsg 0 [refcount] The return CVAR server_var(_refc_a2) does not exist!
              }
            }
            else do
            {
              es_xgetargv _refc_a2 2
              es_xsetinfo _tempcore 0
              es es_xexists _tempcore variable server_var(_refc_a2)
              es es_xif (server_var(_tempcore) = 1) do
              {
                es_xsetinfo _tempcore 0
                es es_xexists _tempcore key _refc_refs server_var(_refc_a2)
                es es_xif (server_var(_tempcore) = 1) do
                {
                  es es_xkeygetvalue _refc_curcount _refc_refs server_var(_refc_a2) refcount
                  es es_xdbgmsg 0 [refcount] The ref. count for server_var(_refc_a2) is server_var(_refc_curcount)!
                }
                else do
                {
                  es es_xdbgmsg 0 [refcount] There is no ref. count for server_var(_refc_a2)!
                }
              }
              else do
              {
                es es_xdbgmsg 0 [refcount] The CVAR server_var(_refc_a2) does not exist!
              }
            }
          }
          else do
          {
            es_xdbgmsg 0 [refcount] You must provide a CVAR!
          }
        }
        else do
        {
          es es_xdbgmsg 0 [refcount] server_var(_refc_a1) is an invalid command!
        }
      }
    }
  }
  else do
  {
    es_xdbgmsg 0 [refcount] You must provide a command!
  }
}

block cheatexec
{
  // first argument is userid
  es_xgetargv _cheatuserid 1
  // second argument is command
  es_xgetargv _cheatcmd 2
  es_xcopy _cheatbak sv_cheats
  es_xforcevalue sv_cheats 1
  es es_xsexec server_var(_cheatuserid) server_var(_cheatcmd)
  es es_xforcevalue sv_cheats server_var(_cheatbak)
}

// called to register the getplayerinfo command
block getplayerinfo_register
{
  // if the stack command isn't already registered
  es_xexists _tempcore command getplayerinfo
  if (server_var(_tempcore) = 0) do
  {
    // while variables
    es_xsetinfo _playerinfo_var 0
    es_xsetinfo _playerinfo_userid 0
    es_xsetinfo _playerinfo_info 0
    es_xregcmd getplayerinfo corelib/getplayerinfo "Return specific information about a player"
  }
}

block getplayerinfo
{
  es_xgetargv _playerinfo_var 1
  es_xgetargv _playerinfo_userid 2
  es_xgetargv _playerinfo_info 3
  es es_xexists _tempcore variable server_var(_playerinfo_var)
  es es_xif (server_var(_tempcore) = 1) do
  {
    es es_xexists _tempcore userid server_var(_playerinfo_userid)
    es es_xif (server_var(_tempcore) = 1) do
    {
      es es_xcreateplayerlist _getplayerinfo server_var(_playerinfo_userid)
      es es_xnq es_xkeygetvalue server_var(_playerinfo_var) _getplayerinfo server_var(_playerinfo_userid) server_var(_playerinfo_info)
      es_xkeygroupdelete _getplayerinfo
    }
    else do
    {
      es es_xdbgmsg 0 getplayerinfo: server_var(_playerinfo_userid) player does not exist!
      es es_xcopy server_var(_playerinfo_var) _tempcore
    }
  }
  else do
  {
    es es_xdbgmsg 0 getplayerinfo: server_var(_playerinfo_var) variable does not exist!
  }
}

// called to register the downloadable command
block downloadable_register
{
  // if the stack command isn't already registered
  es_xexists _tempcore command downloadable
  if (server_var(_tempcore) = 0) do
  {
    es_xregcmd downloadable corelib/downloadable "Specifies a file that should be marked as required for client download"
  }
}

block downloadable
{
  es_xgetargv _tempcore 1
  es es_xif (server_var(_tempcore) != 0) do
  {
    es es_xstringtable downloadables server_var(_tempcore)
  }
  else do
  {
    es_xdbgmsg 0 downloadable: Please specifify a file path for the downloadable
  }
}


block randplayer_register
{
  // if the getrandplayer command isn't already registered
  es_xexists _tempcore command getrandplayer
  if (server_var(_tempcore) = 0) do
  {
    es_xsetinfo _rndp_num 0
    es_xsetinfo _rndp_arg1 0
    es_xsetinfo _rndp_arg2 0
    es_xsetinfo _rndp_argc 0
    es_xsetinfo _rndp_id 0
    es_xregcmd getrandplayer corelib/randomplayer "Returns a random player using a filter you specify"
  }
}

// Random Player Command by Venjax
// Usage: randplayer <var> <filter>
// <var> returns random userid based on filter
// uses foreach filters
block randomplayer
{
  es_xsetinfo _rndp_cnt 0
  es_xgetargv _rndp_arg1 1
  es_xgetargv _rndp_arg2 2
  es_xgetargc _rndp_argc
  es es_xif (server_var(_rndp_argc) < 3) do
  {
    es_xdbgmsg 0 Syntax: getrandplayer <var> <indentifier>
  }
  else do
  {
    es_xkeygroupcreate _rndp_kg
    es foreach player _rndp_id server_var(_rndp_arg2) "es_doblock corelib/randgather"
    es es_xrand _rndp_num 1 server_var(_rndp_cnt)
    es es_xexists _rndp_argc key _rndp_kg server_var(_rndp_num) rnd_userid
    es es_xif (server_var(_rndp_argc) = 1) do
    {
      es es_xkeygetvalue server_var(_rndp_arg1) _rndp_kg server_var(_rndp_num) rnd_userid
      es_xkeygroupdelete _rndp_kg
    }
    else do
    {
      es_xdbgmsg 0 Warning: getrandplayer failed: No user on specified filter
    }
  }
}

block randgather
{
  es_xmath _rndp_cnt + 1
  es es_xkeycreate _rndp_kg server_var(_rndp_cnt)
  es es_xkeysetvalue _rndp_kg server_var(_rndp_cnt) rnd_userid server_var(_rndp_id)
}

block randtest
{
  es_xsetinfo _rndp_tst 0
  es_xsetinfo _rndp_tst1 0
  es_xsetinfo _rndp_tval 0
  es_xsetinfo _rndp_prof 0
  es_xcreateplayerlist _rndp_test
  getrandplayer _rndp_tst #all
  es es_xexists _rndp_tst1 key _rndp_test server_var(_rndp_tst)
  es es_xif (server_var(_rndp_tst1) = 1) do
  {
    es es_xmsg PASS: Randplayer <var> #all results: server_var(_rndp_tst) and this is a valid userid
  }
  else do
  {
    es es_xmsg FAIL: Randplayer <var> #all results: server_var(_rndp_tst) and is not a valid userid
  }
  getrandplayer _rndp_tst #t
  es es_xexists _rndp_tst1 key _rndp_test server_var(_rndp_tst)
  es es_xif (server_var(_rndp_tst1) = 1) do
  {
    es es_xkeygetvalue _rndp_tval _rndp_test server_var(_rndp_tst) teamid
    es es_xif (server_var(_rndp_tval) = 2) do
    {
       es es_xmsg PASS: Randplayer <var> #t results: server_var(_rndp_tst) and this is a valid userid on Terrorists team
    }
    else do
    {
      es es_xmsg FAIL: Randplayer <var> #t results: server_var(_rndp_tst) and is not a valid userid on Terrorists team
    }
  }
  else do
  {
    es es_xmsg FAIL: Randplayer <var> #t results: server_var(_rndp_tst) and is not a valid userid
  }
  es_xkeygroupdelete _rndp_test
}


block profilecmd_register
{
  es_xexists _tempcore command profilecmd
  if (server_var(_tempcore) = 0) do
  {
    es_xsetinfo _prfc_arg1 0
    es_xsetinfo _prfc_arg2 0
    es_regcmd profilecmd corelib/profilecmd "Runs a command and gives you a relative performance value. (Relative because some overhead is introduced)"
  }
}

// Usage: profilecmd <var> "cmd"
// <var> returns profile time
block profilecmd
{
  es_xgetargv _prfc_arg1 1
  es_xgetargv _prfc_arg2 2
  es profile begin server_var(_prfc_arg1)
  esnq server_var(_prfc_arg2)
  es profile end server_var(_prfc_arg1)
}

block profilecmd_test
{
  es_setinfo myvarx 0
  alias mathp es_mathparse myvarx myvarx+1
  alias mathx es_math myvarx + 1
  profilecmd myvar mathp
  profilecmd myvar mathx
  //profilecmd myvar echo Hello
  //profilecmd myvar2 es_msg Hello
}


block isnull_register
{
  es_xexists _tempcore command isnull
  if (server_var(_tempcore) = 0) do
  {
    //Register command
    es_xregcmd isnull "corelib/isnull" "Syntax: isnull <return var> <variable to check>"
  }
}

// isnull <returnvar> <var-to-check>
// submitted by ichthys
block isnull
{
  //es isnull <returnvar> <var to check>
  es_xsetinfo _isnullvar 0
  es_xgetargv _isnullvar 2
  es_xsetinfo _coretemp 0
  //Check if test variable exists
  es es_xexists _coretemp variable server_var(_isnullvar)
  es es_xif (server_var(_coretemp) != 0) do
  {
    //Get return variable
    es_xsetinfo _isnullreturnvar 0
    es_xgetargv _isnullreturnvar 1
    es_xsetinfo _isnulltemp 0
    //Copy value to check
    es es_xcopy _isnulltemp server_var(_isnullvar)
    es es_xif (server_var(_isnulltemp) = 0) do
    {
      //it is either 0 or null
      //Add to value, if it doesn't change it must be null
      es_xcopy _tempcore eventscripts_debug 
      es_xsetinfo eventscripts_debug -1 
      es_xmath _isnulltemp + 1 
      es_xcopy eventscripts_debug _tempcore
      es es_xif (server_var(_isnulltemp) = 1) then es_xsetinfo server_var(_isnullreturnvar) 0
      es es_xif (server_var(_isnulltemp) = 0) then es_xsetinfo server_var(_isnullreturnvar) 1
    }
    else do
    {
      //It's not null
      es es_xsetinfo server_var(_isnullreturnvar) 0
    }
  }
  else do
  {
    es_xdbgmsg 0 [isnull] error: test variable not found
  }
}

// called to register the keygroupremove command
block keygroupremove_register
{
  // if the keygroupremove command isn't already registered
  es_xexists _tempcore command keygroupremove
  if (server_var(_tempcore) = 0) do
  {
    es_xregcmd keygroupremove corelib/keygroupremove "Removes an existing keygroup from memory without error. (Usefull for keygroup deletes on mapchange)"
  }
}

// By [NATO]Hunter
// removes a keygroup without spamming console
block keygroupremove
{
  es_xgetargv _tempcore 1
  es es_xif (server_var(_tempcore) != 0) do
  {
    es_xsetinfo _tempcheck 0
    es es_xexists _tempcheck keygroup server_var(_tempcore)
    es es_xif (server_var(_tempcheck) = 1) do
    {
      es es_xkeygroupdelete server_var(_tempcore)
    }
  }
  else do
  {
    es_xdbgmsg 0 keygroupremove: Please specifify a keygroup for keygroupremove
  }
}

block forcecase_register
{
  // if the getrandplayer command isn't already registered
  es_xexists _tempcore command forcecase
  if (server_var(_tempcore) = 0) do
  {
    es_xsetinfo _fcase_arg1 0
    es_xsetinfo _fcase_arg2 0
    es_xregcmd forcecase corelib/forcecase "Returns a string in upper or lower case"
  }
}

// By [NATO]Hunter
// Force a string to upper or lower case
// Usage: forcecase <var> <upper/lower>
// <var> returns the string in new case that was stored in <var>
block forcecase
{
  es_xgetargv _fcase_arg1 1
  es es_xif (server_var(_fcase_arg1) != 0) do
  {
    es_xgetargv _fcase_arg2 2
    es es_xif (server_var(_fcase_arg2) = "lower") do
    {
      es es_xstring server_var(_fcase_arg1) replace "A" "a"
      es es_xstring server_var(_fcase_arg1) replace "B" "b"
      es es_xstring server_var(_fcase_arg1) replace "C" "c"
      es es_xstring server_var(_fcase_arg1) replace "D" "d"
      es es_xstring server_var(_fcase_arg1) replace "E" "e"
      es es_xstring server_var(_fcase_arg1) replace "F" "f"
      es es_xstring server_var(_fcase_arg1) replace "G" "g"
      es es_xstring server_var(_fcase_arg1) replace "H" "h"
      es es_xstring server_var(_fcase_arg1) replace "I" "i"
      es es_xstring server_var(_fcase_arg1) replace "J" "j"
      es es_xstring server_var(_fcase_arg1) replace "K" "k"
      es es_xstring server_var(_fcase_arg1) replace "L" "l"
      es es_xstring server_var(_fcase_arg1) replace "M" "m"
      es es_xstring server_var(_fcase_arg1) replace "N" "n"
      es es_xstring server_var(_fcase_arg1) replace "O" "o"
      es es_xstring server_var(_fcase_arg1) replace "P" "p"
      es es_xstring server_var(_fcase_arg1) replace "Q" "q"
      es es_xstring server_var(_fcase_arg1) replace "R" "r"
      es es_xstring server_var(_fcase_arg1) replace "S" "s"
      es es_xstring server_var(_fcase_arg1) replace "T" "t"
      es es_xstring server_var(_fcase_arg1) replace "U" "u"
      es es_xstring server_var(_fcase_arg1) replace "V" "v"
      es es_xstring server_var(_fcase_arg1) replace "W" "w"
      es es_xstring server_var(_fcase_arg1) replace "X" "x"
      es es_xstring server_var(_fcase_arg1) replace "Y" "y"
      es es_xstring server_var(_fcase_arg1) replace "Z" "z"
    }
    else do
    {
      es es_xif (server_var(_fcase_arg2) = "upper") do
      {
        es es_xstring server_var(_fcase_arg1) replace "a" "A"
        es es_xstring server_var(_fcase_arg1) replace "b" "B"
        es es_xstring server_var(_fcase_arg1) replace "c" "C"
        es es_xstring server_var(_fcase_arg1) replace "d" "D"
        es es_xstring server_var(_fcase_arg1) replace "e" "E"
        es es_xstring server_var(_fcase_arg1) replace "f" "F"
        es es_xstring server_var(_fcase_arg1) replace "g" "G"
        es es_xstring server_var(_fcase_arg1) replace "h" "H"
        es es_xstring server_var(_fcase_arg1) replace "i" "I"
        es es_xstring server_var(_fcase_arg1) replace "j" "J"
        es es_xstring server_var(_fcase_arg1) replace "k" "K"
        es es_xstring server_var(_fcase_arg1) replace "l" "L"
        es es_xstring server_var(_fcase_arg1) replace "m" "M"
        es es_xstring server_var(_fcase_arg1) replace "n" "N"
        es es_xstring server_var(_fcase_arg1) replace "o" "O"
        es es_xstring server_var(_fcase_arg1) replace "p" "P"
        es es_xstring server_var(_fcase_arg1) replace "q" "Q"
        es es_xstring server_var(_fcase_arg1) replace "r" "R"
        es es_xstring server_var(_fcase_arg1) replace "s" "S"
        es es_xstring server_var(_fcase_arg1) replace "t" "T"
        es es_xstring server_var(_fcase_arg1) replace "u" "U"
        es es_xstring server_var(_fcase_arg1) replace "v" "V"
        es es_xstring server_var(_fcase_arg1) replace "w" "W"
        es es_xstring server_var(_fcase_arg1) replace "x" "X"
        es es_xstring server_var(_fcase_arg1) replace "y" "Y"
        es es_xstring server_var(_fcase_arg1) replace "z" "Z"
      }
      else do
      {
        es_xdbgmsg 0 forcecase: Invalid arguments for forcecase. Syntax: forcecase <var> <upper/lower>
      }
    }
  }
  else do
  {
    es_xdbgmsg 0 forcecase: Invalid arguments for forcecase. Syntax: forcecase <var> <upper/lower>
  }
}

block forcecase_test1
{
  log on
  es_xsetinfo _fcase_test "Hello my Name is Hunter, I am from Germany. I am living in a city called Oberhausen"
  profile begin _fcase_profile
  forcecase _fcase_test upper
  profile end _fcase_profile
  es es_xif (server_var(_fcase_test) != "HELLO MY NAME IS HUNTER, I AM FROM GERMANY. I AM LIVING IN A CITY CALLED OBERHAUSEN") then es_xlog FAIL : forcecase : server_var(_fcase_test) != "HELLO MY NAME IS HUNTER, I AM FROM GERMANY. I AM LIVING IN A CITY CALLED OBERHAUSEN."
}

block forcecase_test2
{
  log on
  es_xsetinfo _fcase_test "Hello my Name is Hunter, I am from Germany. I am living in a city called Oberhausen"
  profile begin _fcase_profile
  forcecase _fcase_test lower
  profile end _fcase_profile
  es es_xif (server_var(_fcase_test) != "hello my name is hunter, i am from germany. i am living in a city called oberhausen") then es_xlog FAIL : forcecase : server_var(_fcase_test) != "hello my name is hunter, i am from germany. i am living in a city called oberhausen."
}


block keymath_register
{
   es_xsetinfo _tempcore 0
   es_xexists _tempcore command "keymath"
   if (server_var(_tempcore) = 0) do
   {
      //Register command
      es_xregcmd keymath "corelib/keymath" "Syntax: keymath [keygroup] <key> <keyvalue> <operator> [value] (see es_math for operators)"
      es_xsetinfo _keymathkeygroupvar 0
      es_xsetinfo _keymathkeyvar 0
      es_xsetinfo _keymathkeyvaluevar 0
      es_xsetinfo _keymathoperatorvar 0
      es_xsetinfo _keymathvaluevar 0
      es_xsetinfo _keymathargs 0
      es_xsetinfo _keymathmathvar 0
      es_xsetinfo keymathtimer 0
   }
}

block keymath
{
   es_xgetargc _keymathargs
   es es_xif (server_var(_keymathargs) < 4) do
   {
      es_xgetargs _tempcore
      echo keymath: Format is:
      echo "  keymath [keygroup] <key> <keyvalue> <operator> [value]"
      if (server_var(_tempcore) = 0) do
      {
         es es_xformat _tempcore "Your line was: %1" server_var(_tempcore)
         es_xstring _tempcore replace 0 "(null)"
         es echo server_var(_tempcore)
      }
      else do
      {
         es echo Your line was: server_var(_tempcore)
      }
   }
   else do
   {
      es_xgetargv _keymathkeygroupvar 1
      es_xgetargv _keymathkeyvar 1
      es es_xexists _tempcore keygroup server_var(_keymathkeygroupvar)
      if (server_var(_tempcore) = 0) do
      {
         es es_xexists _tempcore key server_var(_keymathkeyvar)
         if (server_var(_tempcore) = 0) do
         {
            echo [keymath Error] Unable to find the keygroup/key!
            es_xgetargs _tempcore
            es echo Your line was: server_var(_tempcore)
         }
         else do
         {
            es_xdoblock corelib/keymath_key
         }
      }
      else do
      {
         es_xdoblock corelib/keymath_keygroup
      }
   }
}

block keymath_keygroup
{
   es_xgetargv _keymathkeyvar 2
   es_xgetargv _keymathkeyvaluevar 3
   es es_xkeygetvalue _keymathmathvar server_var(_keymathkeygroupvar) server_var(_keymathkeyvar) server_var(_keymathkeyvaluevar)
   es_xgetargv _keymathoperatorvar 4
   es es_xif (server_var(_keymathargs) = 6) do
   {
      es_xgetargv _keymathvaluevar 5
      es es_xmath _keymathmathvar server_var(_keymathoperatorvar) server_var(_keymathvaluevar)
      es es_xkeysetvalue server_var(_keymathkeygroupvar) server_var(_keymathkeyvar) server_var(_keymathkeyvaluevar) server_var(_keymathmathvar)
   }
   else do
   {
      es es_xmath _keymathmathvar server_var(_keymathoperatorvar)
      es es_xkeysetvalue server_var(_keymathkeygroupvar) server_var(_keymathkeyvar) server_var(_keymathkeyvaluevar) server_var(_keymathmathvar)
   }
}

block keymath_key
{
   es_xgetargv _keymathkeyvaluevar 2
   es es_xkeygetvalue _keymathmathvar server_var(_keymathkeyvar) server_var(_keymathkeyvaluevar)
   es_xgetargv _keymathoperatorvar 3
   es es_xif (server_var(_keymathargs) = 5) do
   {
      es_xgetargv _keymathvaluevar 4
      es es_xmath _keymathmathvar server_var(_keymathoperatorvar) server_var(_keymathvaluevar)
      es es_xkeysetvalue server_var(_keymathkeyvar) server_var(_keymathkeyvaluevar) server_var(_keymathmathvar)
   }
   else do
   {
      es es_xmath _keymathmathvar server_var(_keymathoperatorvar) server_var(_keymathvaluevar)
      es es_xkeysetvalue server_var(_keymathkeyvar) server_var(_keymathkeyvaluevar) server_var(_keymathmathvar)
   }
}


// nearcoord command v0.2
// Submitted by Wonder
block nearcoord_register
{
  es_xsetinfo _tempcore 0
  es_xexists _tempcore command nearcoord
  es es_xif (server_var(_tempcore) < 1) do
  {
    es_xregcmd nearcoord corelib/nearcoord "Loops through matching players in range of a set of coordinates."
  }
  es_xsetinfo _nc_ac 0
  es_xsetinfo _nc_a1 0
  es_xsetinfo _nc_a3 0
  es_xsetinfo _nc_a6 0
  es_xsetinfo _nc_lx 0
  es_xsetinfo _nc_rx 0
  es_xsetinfo _nc_a4 0
  es_xsetinfo _nc_a7 0
  es_xsetinfo _nc_ly 0
  es_xsetinfo _nc_ry 0
  es_xsetinfo _nc_a5 0
  es_xsetinfo _nc_a8 0
  es_xsetinfo _nc_lz 0
  es_xsetinfo _nc_rz 0
  es_xsetinfo _nc_a2 0
  es_xsetinfo _nc_a9 0
  es_xsetinfo __nc_id 0
  es_xsetinfo _nc_ux 0
  es_xsetinfo _nc_uy 0
  es_xsetinfo _nc_uz 0
}
block nearcoord
{
  es_xgetargc _nc_ac
  es es_xif (server_var(_nc_ac) > 9) do
  {
    es_xgetargv _nc_a1 1
    es_xsetinfo _tempcore 0
    es es_xexists _tempcore variable server_var(_nc_a1)
    es es_xif (server_var(_tempcore) > 0) do
    {
      es_xgetargv _nc_a3 3
      es_xgetargv _nc_a6 6
      es_xcopy _nc_lx _nc_a3
      es es_xmath _nc_lx - server_var(_nc_a6)
      es_xcopy _nc_rx _nc_a3
      es es_xmath _nc_rx + server_var(_nc_a6)
      es_xgetargv _nc_a4 4
      es_xgetargv _nc_a7 7
      es_xcopy _nc_ly _nc_a4
      es es_xmath _nc_ly - server_var(_nc_a7)
      es_xcopy _nc_ry _nc_a4
      es es_xmath _nc_ry + server_var(_nc_a7)
      es_xgetargv _nc_a5 5
      es_xgetargv _nc_a8 8
      es_xcopy _nc_lz _nc_a5
      es es_xmath _nc_lz - server_var(_nc_a8)
      es_xcopy _nc_rz _nc_a5
      es es_xmath _nc_rz + server_var(_nc_a8)
      es_xgetargv _nc_a2 2
      es_xgetargv _nc_a9 9
      es foreach player __nc_id server_var(_nc_a2) "es_xdoblock corelib/nearcoord_checkcoords"
      alias __nearcoord_cmd
    }
    es_xelse do
    {
      es_xdbgmsg 0 [nearcoord] CVAR does not exist.
    }
  }
  es_xelse do
  {
    es_xdbgmsg 0 [nearcoord] Syntax error. nearcoord <cvar for userid> <identifier> <x coord> <y coord> <z coord> <x range> <y range> <z range> <"command to execute">
  }
}

block nearcoord_checkcoords
{
  es es_xgetplayerlocation _nc_ux _nc_uy _nc_uz server_var(__nc_id)
  es es_xif (server_var(_nc_ux) >= server_var(_nc_lx)) do
  {
    es es_xif (server_var(_nc_ux) <= server_var(_nc_rx)) do
    {
      es es_xif (server_var(_nc_uy) >= server_var(_nc_ly)) do
      {
        es es_xif (server_var(_nc_uy) <= server_var(_nc_ry)) do
        {
          es es_xif (server_var(_nc_uz) >= server_var(_nc_lz)) do
          {
            es es_xif (server_var(_nc_uz) <= server_var(_nc_rz)) do
            {
              es server_var(_nc_a1) server_var(__nc_id)
              es alias __nearcoord_cmd server_var(_nc_a9)
              __nearcoord_cmd
            }
          }
        }
      }
    }
  }
}

block nearcoord_test
{
// nearcoord <cvar for userid> <identifier> <x coord> <y coord> <z coord> <x range> <y range> <z range> <"command to execute">
alias xxx "es_msg server_var(playerv) Hello!"
profilecmd nc_test "nearcoord playerv #all 0 0 0 3000 3000 3000 xxx"
}


// vote allows you to start a user vote
// syntax:
// vote start <votename> <time> <callback-blockname> "<question>" "<answer1>" "<answer2> "[answer3]" ... "[answer9]"
// vote stop <votename>
// vote submit <votename> <userid> <answer#>
// vote menu <votename> <userid>
// vote getstatus <variable> <userid>
// vote listener
// Originally submitted by [NATO]Hunter
block vote
{
  // get the operation they want to do
  es_xgetargv _tempcore 1
  if (server_var(_tempcore) equalto "listener") do
  {
    es_xdoblock corelib/vote_listener
  }
  else do
  {
    if (server_var(_tempcore) equalto "stop") do
    {
      es_xdoblock corelib/vote_stop
    }
    else do
    {
      if (server_var(_tempcore) equalto "submit") do
      {
        es_xdoblock corelib/vote_add
      }
      else do
      {
        if (server_var(_tempcore) equalto "menu") do
        {
          es_xdoblock corelib/vote_menu
        }
        else do
        {
          if (server_var(_tempcore) equalto "getstatus") do
          {
            es_xdoblock corelib/vote_get
          }
          else do
          {
            if (server_var(_tempcore) equalto "start") do
            {
              es_xdoblock corelib/vote_start
            }
          }
        }
      }
    }
  }
}

// inspect es_client_command
block vote_listener
{
  if (event_var(command) equalto "menuselect") do
  {
    es_setinfo _tempcore 0
    es vote getstatus _tempcore event_var(userid)
    if (server_var(_tempcore) notequalto 0) do
    {
      es vote submit server_var(_tempcore) event_var(userid) event_var(commandstring)
    }
  }
}

// start the vote
block vote_start
{
  es_xsetinfo _vote_name 0
  es_xgetargv _vote_name 2
  if (server_var(_vote_name) != 0) do
  {
    es_xsetinfo _vote_time 0
    es_xgetargv _vote_time 3
    if (server_var(_vote_time) != 0) do
    {
      es_xsetinfo _vote_block 0
      es_xgetargv _vote_block 4
      if (server_var(_vote_block) != 0) do
      {
        es_xsetinfo _vote_question 0
        es_xgetargv _vote_question 5
        if (server_var(_vote_question) != 0) do
        {
          es_xsetinfo _vote_answer1 0
          es_xgetargv _vote_answer1 6
          if (server_var(_vote_answer1) != 0) do
          {
            es_xsetinfo _vote_answer2 0
            es_xgetargv _vote_answer2 7
            if (server_var(_vote_answer2) != 0) do
            {
              es_xsetinfo _vote_answer3 0
              es_xgetargv _vote_answer3 8
              es_xsetinfo _vote_answer4 0
              es_xgetargv _vote_answer4 9
              es_xsetinfo _vote_answer5 0
              es_xgetargv _vote_answer5 10
              es_xsetinfo _vote_answer6 0
              es_xgetargv _vote_answer6 11
              es_xsetinfo _vote_answer7 0
              es_xgetargv _vote_answer7 12
              es_xsetinfo _vote_answer8 0
              es_xgetargv _vote_answer8 13
              es_xsetinfo _vote_answer9 0
              es_xgetargv _vote_answer9 14
              es_xsetinfo _vote_userid 0
              es_xsetinfo _vote_menu 0
              es_format _vote_menu "%1\n->1 %2\n->2 %3" server_var(_vote_question) server_var(_vote_answer1) server_var(_vote_answer2)
              if (server_var(_vote_answer3) != 0) then es_xformat _vote_menu "%1\n->3 %2" server_var(_vote_menu) server_var(_vote_answer3)
              if (server_var(_vote_answer4) != 0) then es_xformat _vote_menu "%1\n->4 %2" server_var(_vote_menu) server_var(_vote_answer4)
              if (server_var(_vote_answer5) != 0) then es_xformat _vote_menu "%1\n->5 %2" server_var(_vote_menu) server_var(_vote_answer5)
              if (server_var(_vote_answer6) != 0) then es_xformat _vote_menu "%1\n->6 %2" server_var(_vote_menu) server_var(_vote_answer6)
              if (server_var(_vote_answer7) != 0) then es_xformat _vote_menu "%1\n->7 %2" server_var(_vote_menu) server_var(_vote_answer7)
              if (server_var(_vote_answer8) != 0) then es_xformat _vote_menu "%1\n->8 %2" server_var(_vote_menu) server_var(_vote_answer8)
              if (server_var(_vote_answer9) != 0) then es_xformat _vote_menu "%1\n->9 %2" server_var(_vote_menu) server_var(_vote_answer9)
              es_format _vote_menu "%1\n->0 Exit" server_var(_vote_menu)
              es_keycreate _vote_corelib server_var(_vote_name)
              es_keysetvalue _vote_corelib server_var(_vote_name) block server_var(_vote_block)
              es_keysetvalue _vote_corelib server_var(_vote_name) menu server_var(_vote_menu)
              es_keysetvalue _vote_corelib server_var(_vote_name) question server_var(_vote_question)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_1 server_var(_vote_answer1)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_2 server_var(_vote_answer2)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_3 server_var(_vote_answer3)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_4 server_var(_vote_answer4)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_5 server_var(_vote_answer5)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_6 server_var(_vote_answer6)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_7 server_var(_vote_answer7)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_8 server_var(_vote_answer8)
              es_keysetvalue _vote_corelib server_var(_vote_name) answertext_9 server_var(_vote_answer9)
              foreach player _vote_userid #all "es_menu server_var(_vote_time) server_var(_vote_userid) server_var(_vote_menu)"
              foreach player _vote_userid #all "es_keysetvalue _vote_corelib _vote_users server_var(_vote_userid) server_var(_vote_name)"
              es_delayed server_var(_vote_time) vote stop server_var(_vote_name)
            }
            else do
            {
              if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
            }
          }
          else do
          {
            if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
          }
        }
        else do
        {
          if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
        }
      }
      else do
      {
        if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
      }
    }
    else do
    {
      if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
    }
  }
  else do
  {
    if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote start. Syntax: vote start <name> <time> <blockname> <question> <answer1> <answer2> [answer3] ... [answer9]
  }
}
// stop a running vote if the time is over
block vote_stop
{
  es_xsetinfo _vote_name 0
  es_xgetargv _vote_name 2
  if (server_var(_vote_name) != 0) do
  {
    es_exists _tempcore key _vote_corelib server_var(_vote_name)
    if (server_var(_tempcore) == 1) do
    {
      es_keygetvalue _vote_block _vote_corelib server_var(_vote_name) block
      es_xsetinfo vote_question 0
      es_xsetinfo vote_answertext1 0
      es_xsetinfo vote_answertext2 0
      es_xsetinfo vote_answertext3 0
      es_xsetinfo vote_answertext4 0
      es_xsetinfo vote_answertext5 0
      es_xsetinfo vote_answertext6 0
      es_xsetinfo vote_answertext7 0
      es_xsetinfo vote_answertext8 0
      es_xsetinfo vote_answertext9 0
      es_xsetinfo vote_answertext 0
      es_xsetinfo vote_answer1 0
      es_xsetinfo vote_answer2 0
      es_xsetinfo vote_answer3 0
      es_xsetinfo vote_answer4 0
      es_xsetinfo vote_answer5 0
      es_xsetinfo vote_answer6 0
      es_xsetinfo vote_answer7 0
      es_xsetinfo vote_answer8 0
      es_xsetinfo vote_answer9 0
      es_xsetinfo vote_answer 0
      es_xsetinfo _vote_answertext 0
      es_xsetinfo _vote_temp 0
      es_keygetvalue vote_question _vote_corelib server_var(_vote_name) question
      es_keygetvalue vote_answertext1 _vote_corelib server_var(_vote_name) answertext_1
      es_keygetvalue vote_answertext2 _vote_corelib server_var(_vote_name) answertext_2
      es_keygetvalue vote_answertext3 _vote_corelib server_var(_vote_name) answertext_3
      es_keygetvalue vote_answertext4 _vote_corelib server_var(_vote_name) answertext_4
      es_keygetvalue vote_answertext5 _vote_corelib server_var(_vote_name) answertext_5
      es_keygetvalue vote_answertext6 _vote_corelib server_var(_vote_name) answertext_6
      es_keygetvalue vote_answertext7 _vote_corelib server_var(_vote_name) answertext_7
      es_keygetvalue vote_answertext8 _vote_corelib server_var(_vote_name) answertext_8
      es_keygetvalue vote_answertext9 _vote_corelib server_var(_vote_name) answertext_9
      es_keygetvalue vote_answer1 _vote_corelib server_var(_vote_name) answer_1
      es_keygetvalue vote_answer2 _vote_corelib server_var(_vote_name) answer_2
      es_keygetvalue vote_answer3 _vote_corelib server_var(_vote_name) answer_3
      es_keygetvalue vote_answer4 _vote_corelib server_var(_vote_name) answer_4
      es_keygetvalue vote_answer5 _vote_corelib server_var(_vote_name) answer_5
      es_keygetvalue vote_answer6 _vote_corelib server_var(_vote_name) answer_6
      es_keygetvalue vote_answer7 _vote_corelib server_var(_vote_name) answer_7
      es_keygetvalue vote_answer8 _vote_corelib server_var(_vote_name) answer_8
      es_keygetvalue vote_answer9 _vote_corelib server_var(_vote_name) answer_9
      if (server_var(vote_answer1) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer1)
        es_xsetinfo vote_answer 1
      }
      if (server_var(vote_answer2) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer2)
        es_xsetinfo vote_answer 2
      }
      if (server_var(vote_answer3) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer3)
        es_xsetinfo vote_answer 3
      }
      if (server_var(vote_answer4) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer4)
        es_xsetinfo vote_answer 4
      }
      if (server_var(vote_answer5) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer5)
        es_xsetinfo vote_answer 5
      }
      if (server_var(vote_answer6) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer6)
        es_xsetinfo vote_answer 6
      }
      if (server_var(vote_answer7) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer7)
        es_xsetinfo vote_answer 7
      }
      if (server_var(vote_answer8) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer8)
        es_xsetinfo vote_answer 8
      }
      if (server_var(vote_answer9) > server_var(_vote_temp)) do
      {
        es_setinfo _vote_temp server_var(vote_answer9)
        es_xsetinfo vote_answer 9
      }
      es_format _vote_answertext "vote_answertext%1" server_var(vote_answer)
      es_copy vote_answertext server_var(_vote_answertext)
      es_doblock server_var(_vote_block)
      es_keydelete _vote_corelib server_var(_vote_name)
    }
  }
  else do
  {
    if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote stop. Syntax: vote stop <name>
  }
}
// vote
block vote_add
{
  es_xsetinfo _vote_name 0
  es_xgetargv _vote_name 2
  if (server_var(_vote_name) != 0) do
  {
    es_xsetinfo _vote_exists 0
    es_exists _vote_exists key _vote_corelib server_var(_vote_name)
    if (server_var(_vote_exists) == 1) do
    {
      es_xsetinfo _vote_userid 0
      es_xgetargv _vote_userid 3
      if (server_var(_vote_userid) > 0) do
      {
        es_xsetinfo _vote_answer 0
        es_xgetargv _vote_answer 4
        if (server_var(_vote_answer) > 0) do
        {
          if (server_var(_vote_answer) < 10) do
          {
            es_xsetinfo _vote_count 0
            es_xsetinfo _vote_userok 0
            es_xsetinfo _vote_answertext 0
            es_format _vote_userid "userid_%1" server_var(_vote_userid)
            es_format _vote_answertext "answertext_%1" server_var(_vote_answer)
            es_format _vote_answer "answer_%1" server_var(_vote_answer)
            es_keygetvalue _vote_userok _vote_corelib server_var(_vote_name) server_var(_vote_userid)
            if (server_var(_vote_userok) != 1) do
            {
              es_keygetvalue _vote_answertext _vote_corelib server_var(_vote_name) server_var(_vote_answertext)
              if (server_var(_vote_answertext) != 0) do
              {
                es_keysetvalue _vote_corelib server_var(_vote_name) server_var(_vote_userid) 1
                es_keygetvalue _vote_count _vote_corelib server_var(_vote_name) server_var(_vote_answer)
                if (server_var(_vote_count) <= 0) then es_xsetinfo _vote_count 0
                es_xmath _vote_count + 1
                es_keysetvalue _vote_corelib server_var(_vote_name) server_var(_vote_answer) server_var(_vote_count)
              }
              else do
              {
                if (server_var(eventscripts_debug) > -1) then echo corelib: This answer doesn't exist
              }
            }
            else do
            {
              es_xgetargv _vote_userid 3
              if (server_var(eventscripts_debug) > -1) then echo corelib: The user server_var(_vote_userid) voted already
            }
          }
        }
      }
      else do
      {
        if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote add. Syntax: vote add <name> <userid> <answer#>
      }
    }
    else do
    {
      if (server_var(eventscripts_debug) > -1) then echo corelib: The vote server_var(_vote_name) isn't existing
    }
  }
  else do
  {
    if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote add. Syntax: vote add <name> <userid> <answer#>
  }
}
// re-enter the menu
block vote_menu
{
  es_xsetinfo _vote_name 0
  es_xgetargv _vote_name 2
  if (server_var(_vote_name) != 0) do
  {
    es_exists _tempcore key _vote_corelib server_var(_vote_name)
    if (server_var(_tempcore) == 1) do
    {
      es_xsetinfo _vote_userid 0
      es_xgetargv _vote_userid 3
      es_getuserid _vote_userid server_var(_vote_userid)
      if (server_var(_vote_userid) > 0) do
      {
        es_keygetvalue _vote_menu _vote_corelib server_var(_vote_name) menu
        es_keysetvalue _vote_corelib _vote_users server_var(_vote_userid) server_var(_vote_name)
        es_menu 20 server_var(_vote_userid) server_var(_vote_menu)
      }
      else do
      {
        if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote menu. Syntax: vote menu <name> <userid>
      }
    }
    else do
    {
      if (server_var(eventscripts_debug) > -1) then echo corelib: The vote server_var(_vote_name) isn't existing
    }
  }
  else do
  {
    if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote menu. Syntax: vote menu <name> <userid>
  }
}
// handle menuselect
block vote_get
{
  es_xsetinfo _vote_name 0
  es_xgetargv _vote_name 2
  if (server_var(_vote_name) != 0) do
  {
    es_xsetinfo _vote_userid 0
    es_xgetargv _vote_userid 3
    if (server_var(_vote_userid) > 0) do
    {
      es_setinfo server_var(_vote_name) 0
      es_keygetvalue server_var(_vote_name) _vote_corelib _vote_users server_var(_vote_userid)
    }
    else do
    {
      if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote getstatus. Syntax: vote getstatus <variable> <userid>
    }
  }
  else do
  {
    if (server_var(eventscripts_debug) > -1) then echo corelib: Invalid arguments to vote getstatus. Syntax: vote getstatus <variable> <userid>
  }
}
// called to register the vote command
block vote_register
{
  // if the vote command isn't already registered
  es_xsetinfo _tempcore 0
  es_xexists _tempcore command "vote"
  if (server_var(_tempcore) == 0) do
  {
    // vote keygroup
    es_xkeygroupcreate _vote_corelib
    es_xkeycreate _vote_corelib _vote_users

    es_xregcmd vote corelib/vote "Allows you to create a user vote."
  }
}
block vote_test
{
  log on
  es_xsetinfo testvotename 0
  es_xsetinfo _tempcore 0
  vote start testvote 10 "corelib/vote_testb" "Is this vote working?" "Yes" "No" "Don't know" "Not important"
  vote add testvote 25 1
  vote add testvote 26 1
  vote add testvote 27 1
  vote add testvote 28 2
  vote add testvote 29 4
  vote add testvote 30 4
  es_xgetuserid _tempcore
  es vote getstatus testvotename server_var(_tempcore)
  if (server_var(testvotename) != "testvote") then es_xmsg FAILURE: vote test1 failed.
}
block vote_testb
{
  es_msg vote_question = server_var(vote_question)
  es_msg vote_answertext1 = server_var(vote_answertext1)
  es_msg vote_answertext2 = server_var(vote_answertext2)
  es_msg vote_answertext3 = server_var(vote_answertext3)
  es_msg vote_answertext4 = server_var(vote_answertext4)
  es_msg vote_answertext5 = server_var(vote_answertext5)
  es_msg vote_answertext6 = server_var(vote_answertext6)
  es_msg vote_answertext7 = server_var(vote_answertext7)
  es_msg vote_answertext8 = server_var(vote_answertext8)
  es_msg vote_answertext9 = server_var(vote_answertext9)
  es_msg vote_answer1 = server_var(vote_answer1)
  es_msg vote_answer2 = server_var(vote_answer2)
  es_msg vote_answer3 = server_var(vote_answer3)
  es_msg vote_answer4 = server_var(vote_answer4)
  es_msg vote_answer5 = server_var(vote_answer5)
  es_msg vote_answer6 = server_var(vote_answer6)
  es_msg vote_answer7 = server_var(vote_answer7)
  es_msg vote_answer8 = server_var(vote_answer8)
  es_msg vote_answer9 = server_var(vote_answer9)
  es_msg vote_answertext = server_var(vote_answertext)
  es_msg vote_answer = server_var(vote_answer)
  if (server_var(vote_answer) != 1) then es_xmsg FAILURE: vote test1 failed.
}
