#include <amxmodx>
#include <colorchat>
#include <cstrike>
#include <engine>
#include <fakemeta>
#include <fun>
#include <hamsandwich>

#define VIP ADMIN_LEVEL_G
#define SVIP ADMIN_LEVEL_H

new CsArmorType:armortype, skokii[33], skoki[33];

public plugin_init()
{
	register_plugin("[JB] VIP & SVIP", "v1.0", "Skull");
	RegisterHam(Ham_TakeDamage, "player", "TakeDamage");
	RegisterHam(Ham_Spawn, "player", "SpawnedEventPre", 1);
	register_forward(FM_CmdStart, "CmdStartPre");
	register_forward(FM_PlayerPreThink, "PreThink");
	register_forward(FM_UpdateClientData, "UpdateClientData", 1)
	register_event("CurWeapon", "Event_CurWeapon", "be","1=1");
	register_event("DeathMsg", "DeathMsg", "a");
	register_message(get_user_msgid("SayText"),"handleSayText");
	register_message(get_user_msgid("ScoreAttrib"), "VipStatus");
	register_clcmd("say_team", "VipChat");
	register_clcmd("say /vip", "ShowMotd_vip");
	register_clcmd("say /svip", "ShowMotd_svip");
}

public plugin_precache()
{
	precache_model("models/jb_skull/v_baseball22.mdl");
	precache_model("models/jb_skull/v_lom22.mdl");
	precache_model("models/jb_skull/v_deagle_v2.mdl");
	precache_model("models/jb_skull/v_ak47_v2.mdl");
	precache_model("models/jb_skull/v_m4a1_v2.mdl");
	precache_model("models/jb_skull/v_m249_v2.mdl");

	precache_model("models/jb_skull/p_baseball22.mdl");
	precache_model("models/jb_skull/p_lom22.mdl");
	precache_model("models/jb_skull/p_deagle_v2.mdl");
	precache_model("models/jb_skull/p_ak47_v2.mdl");
	precache_model("models/jb_skull/p_m4a1_v2.mdl");
	precache_model("models/jb_skull/p_m249_v2.mdl");
}

public client_PreThink(id)
{
	if(get_user_flags(id) & SVIP && is_user_alive(id))
	{
		entity_set_float(id, EV_FL_fuser2, 0.0);
	
		if(entity_get_int(id, EV_INT_button) & 2)
		{
			new flags = entity_get_int(id, EV_INT_flags);
		
			if(flags & (1<<11) || entity_get_int(id, EV_INT_waterlevel) >= 2 || !(flags & FL_ONGROUND))
				return PLUGIN_CONTINUE;

			new Float:velocity[3];
			entity_get_vector(id, EV_VEC_velocity, velocity);
			velocity[2] += 250.0;
			entity_set_vector(id, EV_VEC_velocity, velocity);
			entity_set_int(id, EV_INT_gaitsequence, 6);
		}
	}
	return PLUGIN_CONTINUE;
}

public TakeDamage(victim, entity, attacker, Float:damage, damagebits)
{
	if(!is_user_connected(attacker) || !is_user_connected(victim))
		return HAM_IGNORED;

	if(get_user_flags(attacker) & VIP && get_user_weapon(attacker) == CSW_KNIFE)
	{
		SetHamParamFloat(4, damage * 1.3);
		return HAM_HANDLED;
	}
	if(get_user_flags(attacker) & SVIP && get_user_weapon(attacker) == CSW_KNIFE)
	{
		SetHamParamFloat(4, damage * 1.5);
		return HAM_HANDLED;
	}
	return HAM_IGNORED;
}

public SpawnedEventPre(id)
{
	if(get_user_flags(id) & VIP && is_user_alive(id))
	{
		set_user_footsteps(id, 1);
		skokii[id] = 1;
		set_user_health(id, get_user_health(id) +50);
		cs_set_user_armor(id, min(cs_get_user_armor(id, armortype) +100, 300), armortype);
		cs_set_user_nvg(id);
	}
	if(get_user_flags(id) & SVIP && is_user_alive(id))
	{
		set_user_footsteps(id, 1);
		skoki[id] = 2;
		set_user_health(id, get_user_health(id) +50);
		cs_set_user_armor(id, min(cs_get_user_armor(id, armortype) +100, 300), armortype);
		cs_set_user_nvg(id);
	}
}

public CmdStartPre(id, uc_handle)
{
	if(get_user_flags(id) & VIP && is_user_alive(id))
	{
		new flags = pev(id, pev_flags);

		if((get_uc(uc_handle, UC_Buttons) & IN_JUMP) && !(flags & (1<<9)) && !(pev(id, pev_oldbuttons) & IN_JUMP) && skokii[id] > 0)
		{
			--skokii[id];
			new Float:velocity[3];
			pev(id, pev_velocity, velocity);
			velocity[2] = random_float(265.0, 285.0);
			set_pev(id, pev_velocity, velocity);
		}
		else if(flags & FL_ONGROUND && skokii[id] != -1)
			skokii[id] = 1;
	}
	if(get_user_flags(id) & SVIP && is_user_alive(id))
	{
		new flags = pev(id, pev_flags);

		if((get_uc(uc_handle, UC_Buttons) & IN_JUMP) && !(flags & (1<<9)) && !(pev(id, pev_oldbuttons) & IN_JUMP) && skoki[id] > 0)
		{
			--skoki[id];
			new Float:velocity[3];
			pev(id, pev_velocity, velocity);
			velocity[2] = random_float(265.0, 285.0);
			set_pev(id, pev_velocity, velocity);
		}
		else if(flags & FL_ONGROUND && skoki[id] != -1)
			skoki[id] = 2;
	}
}

public PreThink(id)
{
	if(cs_get_user_team(id) == CS_TEAM_CT && get_user_flags(id) & VIP || get_user_flags(id) & SVIP)
		set_pev(id, pev_punchangle, {0.0,0.0,0.0})
	return PLUGIN_HANDLED;
}

public UpdateClientData(id, sw, cd_handle)
{
	if(cs_get_user_team(id) == CS_TEAM_CT && get_user_flags(id) & VIP || get_user_flags(id) & SVIP)
		set_cd(cd_handle, CD_PunchAngle, {0.0,0.0,0.0})
	return PLUGIN_HANDLED;
}

public Event_CurWeapon(id)
{
	if(get_user_flags(id) & VIP && cs_get_user_team(id) == CS_TEAM_T && get_user_weapon(id) == CSW_KNIFE)
	{
		entity_set_string(id, EV_SZ_viewmodel, "models/jb_skull/v_baseball22.mdl")
		entity_set_string(id, EV_SZ_weaponmodel, "models/jb_skull/p_baseball22.mdl")
	}
	if(get_user_flags(id) & SVIP && cs_get_user_team(id) == CS_TEAM_T && get_user_weapon(id) == CSW_KNIFE)
	{
		entity_set_string(id, EV_SZ_viewmodel, "models/jb_skull/v_lom22.mdl")
		entity_set_string(id, EV_SZ_weaponmodel, "models/jb_skull/p_lom22.mdl")
	}
	if(get_user_flags(id) & VIP || get_user_flags(id) & SVIP)
	{
		if(get_user_weapon(id) == CSW_DEAGLE)
		{
			entity_set_string(id, EV_SZ_viewmodel, "models/jb_skull/v_deagle_v2.mdl")
			entity_set_string(id, EV_SZ_weaponmodel, "models/jb_skull/p_deagle_v2.mdl")
		}
		if(get_user_weapon(id) == CSW_AK47)
		{
			entity_set_string(id, EV_SZ_viewmodel, "models/jb_skull/v_ak47_v2.mdl")
			entity_set_string(id, EV_SZ_weaponmodel, "models/jb_skull/p_ak47_v2.mdl")
		}
		if(get_user_weapon(id) == CSW_M4A1)
		{
			entity_set_string(id, EV_SZ_viewmodel, "models/jb_skull/v_m4a1_v2.mdl")
			entity_set_string(id, EV_SZ_weaponmodel, "models/jb_skull/p_m4a1_v2.mdl")
		}
		if(get_user_weapon(id) == CSW_M249)
		{
			entity_set_string(id, EV_SZ_viewmodel, "models/jb_skull/v_m249_v2.mdl")
			entity_set_string(id, EV_SZ_weaponmodel, "models/jb_skull/p_m249_v2.mdl")
		}
	}
	return PLUGIN_CONTINUE;
}

public DeathMsg()
{
	new kid = read_data(1);
	new vid = read_data(2);
	new hs = read_data(3);
	
	if(is_user_alive(kid) && get_user_flags(kid) & SVIP && get_user_team(kid) != get_user_team(vid))
		cs_set_user_money(kid, cs_get_user_money(kid)+(hs?500:300));
}

public handleSayText(msgId, msgDest, msgEnt)
{
	new id = get_msg_arg_int(1);
	
	if(is_user_connected(id) && get_user_flags(id) & VIP)
	{
		new szTmp[256], szTmp2[256];
		get_msg_arg_string(2, szTmp, charsmax(szTmp))
		new szPrefix[64] = "^x04[VIP]";
		
		if(!equal(szTmp, "#Cstrike_Chat_All"))
		{
			add(szTmp2, charsmax(szTmp2), szPrefix);
			add(szTmp2, charsmax(szTmp2), " ");
			add(szTmp2, charsmax(szTmp2), szTmp);
		}
		else
		{
			add(szTmp2, charsmax(szTmp2), szPrefix);
			add(szTmp2, charsmax(szTmp2), "^x03 %s1^x01 :  %s2");
		}
		set_msg_arg_string(2, szTmp2);
	}
	if(is_user_connected(id) && get_user_flags(id) & SVIP)
	{
		new szTmp[256], szTmp2[256];
		get_msg_arg_string(2, szTmp, charsmax(szTmp))
		new szPrefix[64] = "^x04[SuperVIP]";
		
		if(!equal(szTmp, "#Cstrike_Chat_All"))
		{
			add(szTmp2, charsmax(szTmp2), szPrefix);
			add(szTmp2, charsmax(szTmp2), " ");
			add(szTmp2, charsmax(szTmp2), szTmp);
		}
		else
		{
			add(szTmp2, charsmax(szTmp2), szPrefix);
			add(szTmp2, charsmax(szTmp2), "^x03 %s1^x01 :  %s2");
		}
		set_msg_arg_string(2, szTmp2);
	}
	return PLUGIN_CONTINUE;
}

public VipStatus()
{
	new id = get_msg_arg_int(1);

	if(is_user_alive(id) && get_user_flags(id) & VIP || get_user_flags(id) & SVIP)
		set_msg_arg_int(2, ARG_BYTE, get_msg_arg_int(2)|4);
}

public VipChat(id)
{
	if(get_user_flags(id) & VIP || get_user_flags(id) & SVIP)
	{
		new g_Msg[256],
		g_Text[256];
		read_args(g_Msg, charsmax(g_Msg));
		remove_quotes(g_Msg);
		
		if(g_Msg[0] == '*' && g_Msg[1])
		{
			new g_Name[64];
			get_user_name(id, g_Name, charsmax(g_Name));
			formatex(g_Text, charsmax(g_Text), "^x04[**VIP Chat**) ^x03%s : ^x04%s", g_Name, g_Msg[1]);
			
			for(new i=1;i<33;i++)
			{
				if(is_user_connected(i) && get_user_flags(i) & VIP || get_user_flags(i) & SVIP && cs_get_user_team(i) == CS_TEAM_T)
					ColorChat(i, GREEN, "%s", g_Text);
			}
			return PLUGIN_HANDLED_MAIN;
		}
	}
	return PLUGIN_CONTINUE;
}

public ShowMotd_vip(id)
	show_motd(id, "vip.txt", "Informacje o vipie");

public ShowMotd_svip(id)
	show_motd(id, "svip.txt", "Informacje o vipie");