#include amxmodx
#include amxmisc
#define MAX_PLAYERS 32
new g_iHeadcount, g_timing, g_iTic_quota, g_iTic_sleep, g_iTic;

/*
 * Experimental for any OS but Linux gets the higher numbers with pingboost.
 * DO NOT put sys_ticrate anywhere as a parameter on the server launch code.
*/

public plugin_init()

{
    register_plugin("Variable sys_ticrate", "A", ".sρiηX҉.");
    
    g_timing     = register_cvar("sys_timing",  "1"); //0|1 disables|enables plugin.
    g_iTic_sleep = register_cvar("sys_sleep",  "32"); //Tic hibernation rate.
    g_iTic_quota = register_cvar("sys_quota", "312"); //Tic rate quota.
    g_iTic       = get_cvar_pointer("sys_ticrate"); //Base tic rate. Only used to launch server with.

}


public client_putinserver(id)

{
    if (get_pcvar_num(g_timing) == 1)
    {
    new iAlloted_Tic;

    if( is_user_connected(id) && (!is_user_bot(id)) && (iPlayers() >= 1) ) {

        iAlloted_Tic = (iPlayers() * get_pcvar_num(g_iTic_quota) )
        
        set_pcvar_num(g_iTic,iAlloted_Tic)
        
        }
    }
}

/*
 * 
 * name: client_disconnect(id) or client_disconnected(id)
 * @param depends on what flavor of amxx you use.
 * @return triggers Sys_ticrate adjustments when players leave.
 * 
 */

    #if AMXX_VERSION_NUM < 183;

public client_disconnect(id)

    #else

public client_disconnected(id)

    #endif
    {
    if (get_pcvar_num(g_timing) == 1){
    if (iPlayers() < 2){
        set_pcvar_num(g_iTic,get_pcvar_num(g_iTic_sleep));
        }
            else client_putinserver(id);
    }
}

/*
 * 
 * name: stock iPlayers()
 * @param depends on what flavor of amxx you use.
 * @return total number of humans on multi-player.
 * 
 */

stock iPlayers()

{
    #if AMXX_VERSION_NUM == 182;
        new players[ MAX_PLAYERS ],pNum
        get_players(players,pNum,"ch")
        g_iHeadcount = pNum;

    #else

        g_iHeadcount = get_playersnum_ex(GetPlayersFlags:GetPlayers_ExcludeBots)

    #endif

    return g_iHeadcount
}
