#include <amxmodx>
#include <fakemeta>

#define VERSION "0.1"
#define LEET 1337
//#define DEBUG 	1

const g_iProg 		= (0xFF);
const g_iWeaps		= (0x1E);
const g_iMaxSpeed	= (0x2EE);
const g_iMaxCzas	= (0xA);

new g_iMaxPlayers;

new Float:g_fTempMaxSpeed[0x21];
new Float:g_fSprintLeft[0x21];

new const Float:g_fWeaponsSpeed[g_iWeaps + 1]={ //THX 4 DARKGL ZA TABLICE ;)
	000.0,
	250.0, 000.0, 260.0, 250.0, 240.0, 
	250.0, 250.0, 240.0, 250.0, 250.0,                
	250.0, 250.0, 210.0, 240.0, 240.0,    
	250.0, 250.0, 210.0, 250.0, 220.0,              
	230.0, 230.0, 250.0, 210.0, 250.0,            
	250.0, 235.0, 221.0, 250.0, 245.0 
};

public plugin_init() {
	register_plugin("Szybki bieg", VERSION, "diablix")
	
	g_iMaxPlayers = get_maxplayers();
	
	register_forward(FM_CmdStart, "fwCmdStart", 0);
	register_event("CurWeapon", "eventCurWeapon", "be", "1=1");
	
	set_task(0.4, "taskPrintAmmount", _, _, _, "b");
}

public plugin_cfg() set_cvar_num("sv_maxspeed", g_iMaxSpeed);

public eventCurWeapon(id){
	if(g_fTempMaxSpeed[id] >= float(g_iProg)){
		set_pev(id, pev_maxspeed, g_fTempMaxSpeed[id]);
		client_cmd(id, "cl_forwardspeed ^"%d^"", floatround(g_fTempMaxSpeed[id]));
		
		#if defined DEBUG
		client_print(id, 3, "%.1f", g_fTempMaxSpeed[id]);
		#endif
	}
	else{
		set_pev(id, pev_maxspeed, g_fWeaponsSpeed[get_user_weapon(id)]);
		
		#if defined DEBUG
		client_print(id, 3, "%.1f", g_fWeaponsSpeed[get_user_weapon(id)]);
		#endif
	}
}

public fwCmdStart(id, iHandle, iSeed){
	if(!is_user_alive(id)) return FMRES_IGNORED;
	
	new Float:fmove, Float:smove;
	get_uc(iHandle, UC_ForwardMove, fmove);
	get_uc(iHandle, UC_SideMove, smove);
	
	new Float:maxspeed;
	pev(id, pev_maxspeed, maxspeed);
	new Float:walkspeed = (maxspeed * 0.52); 
	fmove = floatabs(fmove);
	smove = floatabs(smove);
	
	if(fmove <= walkspeed && smove <= walkspeed && !(fmove == 0.0 && smove == 0.0)){	
		if(g_fSprintLeft[id] >= 0.5){
			if(task_exists(id + LEET)) remove_task(id + LEET);
			g_fTempMaxSpeed[id] = g_fTempMaxSpeed[id] < 400.0 ? 400.0 : g_fTempMaxSpeed[id];
			g_fTempMaxSpeed[id] = g_fTempMaxSpeed[id] < float(g_iMaxSpeed) ? g_fTempMaxSpeed[id] + 10.0 : g_fTempMaxSpeed[id];
			set_pev(id, pev_maxspeed, g_fTempMaxSpeed[id]);
			client_cmd(id, "cl_forwardspeed ^"%d^"", floatround(g_fTempMaxSpeed[id]));
			engclient_cmd(id, "weapon_knife");
			
			g_fSprintLeft[id] -= 0.05;
			
			for(new i = 0 ; i < 0x10 ; i++){
				new bitButtons 		= get_uc(iHandle, UC_Buttons);
				new bitOldbuttons 	= pev(id, pev_oldbuttons);
				if(!(((1<<3)|(1<<4)|(1<<12)) & (1<<i))){
					if((bitButtons & (1<<i))){
						if((1<<2) & (1<<i))
							set_pev(id, pev_oldbuttons, bitOldbuttons | (1<<i));
						
						else
							set_uc(iHandle, UC_Buttons, bitButtons & (~(1<<i)));
						
						return FMRES_SUPERCEDE;
					}
				}
			}
		}
		else{
			g_fTempMaxSpeed[id] = 0.0;
			eventCurWeapon(id);
			client_print(id, print_center, "Odpocznij chwile!");
		}
		return FMRES_IGNORED;
	}
	else{
		if(!task_exists(id + LEET)) set_task(1.0, "taskRecoverSprint", id + LEET);
		g_fTempMaxSpeed[id] = 0.0;
		eventCurWeapon(id);
	}
	return FMRES_IGNORED;
}

public taskRecoverSprint(task_id){
	new id = task_id - LEET;
	
	g_fSprintLeft[id]++;
	
	if(floatround(g_fSprintLeft[id]) < g_iMaxCzas || floatround(g_fSprintLeft[id]) > g_iMaxCzas){
		if(floatround(g_fSprintLeft[id]) < g_iMaxCzas) set_task(1.0, "taskRecoverSprint", id + LEET);
		if(floatround(g_fSprintLeft[id]) > g_iMaxCzas) g_fSprintLeft[id] = float(g_iMaxCzas);
	}
}

public taskPrintAmmount(){
	static id; //statyczna
	for(id = 1 ; id <= g_iMaxPlayers ; id++){
		if(is_user_alive(id)){
			new iSprint = floatround(g_fSprintLeft[id]);
			new sSprint[g_iMaxCzas];
			
			while(iSprint >= 1){
				iSprint--;
				add(sSprint, sizeof sSprint - 1, "*");
			}
			iSprint = floatround(g_fSprintLeft[id]);
			set_hudmessage(iSprint ? 0x64 : 0xFF, iSprint ? 0x48 : 0x0, iSprint ? 0xC : 0x0, 0.01, 0.77, 0, 0.000001, 0.405, 0.000001, 0.000001, -1);
			show_hudmessage(id, "SPRINT : [%s]", sSprint);	
		}
	}
}
