#include <amxmodx>
#include <cstrike>
#include <fakemeta_util>
#include <hamsandwich>
#include <engine>

#define TASK_FIRE 8612
#define TASK_RELOAD 9712

#define DAMAGE 86
#define CLIP 45
#define BPAMMO 250
#define SPEED 1.5
#define RECOIL 0.75

#define PLASMA_SPEED 1000.0
#define PLASMA_RADIUS 100.0

#define WEAPON_SECRETCODE 1946

#define WEAPONANIM_SHOOT random_num(3, 5)

#define IDLE 0
#define WEAPONANIM_RELOAD 1
#define DRAW1 2

#define WEAPONTIME_DRAW 0.75
#define WEAPONTIME_RELOAD 3.5

#define PLASMABALL_CLASSNAME "plasmaball"

#define WEAPON_CSW CSW_UMP45
#define WEAPON_NAME "weapon_ump45"

forward OnRemoveData();

new bool:has_weapon[33], bool:reloading[33], Float:idle[33];
new g_MuzzleFlash_SprId, g_PlasmaExp_SprId;


new plasma_models[][] = {
	"models/plasma_day/v_plasma.mdl",
	"models/plasma_day/p_plasma.mdl"
}

new plasma_sounds[][] = {
	"plasma_day/plasma/plasma_fire.wav",
	"plasma_day/plasma/plasma_hit.wav",
	"plasma_day/plasma/plasma_reload.wav"
}
new plasma_sprites[][] = {
	"sprites/plasma/plasma_beam.spr"
}

public plugin_init() 
{
	register_plugin("Weapon Laser", "1.0.6b", "cypis");

	RegisterHam(Ham_Item_Deploy, WEAPON_NAME, "Weapon_Deploy", 1);
	RegisterHam(Ham_Weapon_WeaponIdle, WEAPON_NAME, "Weapon_WeaponIdle");

	register_forward(FM_CmdStart, "CmdStart");
	//register_forward(FM_Touch, "Touch");
	register_forward(FM_UpdateClientData, "UpdateClientData_Post", 1);

	register_think(PLASMABALL_CLASSNAME, "fw_Think_Plasma")
	register_touch(PLASMABALL_CLASSNAME, "*", "fw_Touch_Plasma")
	
	register_srvcmd("jail_add_plazma", "UstawBazooke");
}

public plugin_precache()
{
	for(new i=0; i<sizeof(plasma_models); i++)
		precache_model(plasma_models[i]);
	for(new i=0; i<sizeof(plasma_sounds); i++)
		precache_sound(plasma_sounds[i]);
	for(new i=0; i<sizeof(plasma_sprites); i++)
		precache_model(plasma_sprites[i]);
	g_PlasmaExp_SprId = precache_model("sprites/plasma/plasma_exp.spr");
	g_MuzzleFlash_SprId = precache_model("sprites/muzzleflash27.spr");
}

public UstawBazooke(id)
{
	new int[3];
	read_argv(1, int, 2);
	id = str_to_num(int);

	has_weapon[id] = true;
	reloading[id] = false;
	fm_give_item(id, WEAPON_NAME);
}

public OnRemoveData()
{
	for(new i=1; i<=32; i++)
		has_weapon[i] = false;
}
	
public CmdStart(id, uc)
{
	if(!is_user_alive(id) || !has_weapon[id])
		return FMRES_IGNORED;

	new ammo, weapon = get_user_weapon(id, ammo);
	if(weapon == WEAPON_CSW)
	{ 
		new button = get_uc(uc, UC_Buttons);
		if(button & IN_ATTACK)
		{
			set_uc(uc, UC_Buttons, button & ~IN_ATTACK);
			
			if(reloading[id] || !idle[id]) 
				return FMRES_IGNORED;
			if(idle[id] && (get_gametime()-idle[id]<=WEAPONTIME_DRAW)) 
				return FMRES_IGNORED;
		
			fm_set_animation(id, WEAPONANIM_SHOOT);
			emit_sound(id, CHAN_WEAPON, plasma_sounds[0], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
			
			Make_Muzzleflash(id);
			Create_PlasmaBall(id);

			set_user_clip(id, ammo - 1);
			
			UsunTask(id);
			reloading[id] = true;
			
			set_task(0.5, "task_wait_fire", id+TASK_FIRE);
		}
		else if(button & IN_RELOAD)
		{
			set_uc(uc, UC_Buttons, button & ~IN_RELOAD);
		}
	}
	else
	{
		UsunTask(id);
		idle[id] = 0.0;
	}	
	return FMRES_IGNORED;
}	

public Weapon_Deploy(ent)
{
	new id = get_pdata_cbase(ent, 41, 4);
	if(!is_user_alive(id) || !has_weapon[id])
		return;
		
	set_pev(id, pev_viewmodel2, plasma_models[0]);
	set_pev(id, pev_weaponmodel2, plasma_models[1]);
	fm_set_animation(id, DRAW1);
}

public Weapon_WeaponIdle(ent)
{
	new id = get_pdata_cbase(ent, 41, 4);
	if(!is_user_alive(id))
		return;
		
	if(!has_weapon[id] || idle[id])
		return;
		
	if(reloading[id])
	{
		UsunTask(id);
		fm_set_animation(id, WEAPONANIM_RELOAD);
		set_task(WEAPONTIME_RELOAD, "task_launcher_reload", id+TASK_RELOAD);
	}
	else
		fm_set_animation(id, IDLE);
	
	idle[id] = get_gametime();
}

public UpdateClientData_Post(id, sendweapons, cd_handle) 
{ 
	if(!is_user_alive(id))
		return FMRES_IGNORED;
	
	if(get_user_weapon(id) == WEAPON_CSW && has_weapon[id])
	{
		set_cd(cd_handle, CD_ID, 0);
		return FMRES_HANDLED;
	}
	return FMRES_IGNORED;
} 

public task_wait_fire(id)
{
	id -= TASK_FIRE;
	fm_set_animation(id, WEAPONANIM_RELOAD);
	set_task(WEAPONTIME_RELOAD, "task_launcher_reload", id+TASK_RELOAD);
}

public task_launcher_reload(id)
{
	id -= TASK_RELOAD;
	reloading[id] = false;
}

UsunTask(id)
{
	if(task_exists(id+TASK_FIRE))
		remove_task(id+TASK_FIRE);
				
	if(task_exists(id+TASK_RELOAD))
		remove_task(id+TASK_RELOAD);
}

stock fm_set_animation(id, animation)
{
	set_pev(id, pev_weaponanim, animation);
    
	message_begin(MSG_ONE, SVC_WEAPONANIM, .player = id);
	write_byte(animation);
	write_byte(pev(id, pev_body));
	message_end();
}

public Create_PlasmaBall(id)
{
	static Float:StartOrigin[3], Float:TargetOrigin[3], Float:MyVelocity[3], Float:VecLength
	
	get_position(id, 48.0, 10.0, -5.0, StartOrigin)
	get_position(id, 1024.0, 0.0, 0.0, TargetOrigin)
	
	pev(id, pev_velocity, MyVelocity)
	VecLength = vector_length(MyVelocity)
	
	if(VecLength) 
	{
		TargetOrigin[0] += random_float(-16.0, 16.0); TargetOrigin[1] += random_float(-16.0, 16.0); TargetOrigin[2] += random_float(-16.0, 16.0)
	} else {
		TargetOrigin[0] += random_float(-8.0, 8.0); TargetOrigin[1] += random_float(-8.0, 8.0); TargetOrigin[2] += random_float(-8.0, 8.0)
	}
	
	static Ent; Ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "env_sprite"))
	if(!pev_valid(Ent)) return
	
	// Set info for ent
	set_pev(Ent, pev_movetype, MOVETYPE_FLY)
	set_pev(Ent, pev_rendermode, kRenderTransAdd)
	set_pev(Ent, pev_renderamt, 10.0)
	set_pev(Ent, pev_iuser1, id) // Better than pev_owner
	set_pev(Ent, pev_iuser2, get_user_team(id))
	set_pev(Ent, pev_fuser1, get_gametime() + 3.0)
	set_pev(Ent, pev_scale, random_float(0.1, 0.25))
	set_pev(Ent, pev_nextthink, halflife_time() + 0.1)
	
	entity_set_string(Ent, EV_SZ_classname, PLASMABALL_CLASSNAME)
	engfunc(EngFunc_SetModel, Ent, plasma_sprites[0])
	set_pev(Ent, pev_mins, Float:{-1.0, -1.0, -1.0})
	set_pev(Ent, pev_maxs, Float:{1.0, 1.0, 1.0})
	set_pev(Ent, pev_origin, StartOrigin)
	set_pev(Ent, pev_gravity, 0.01)
	set_pev(Ent, pev_solid, SOLID_TRIGGER)
	set_pev(Ent, pev_frame, 0.0)
	
	static Float:Velocity[3]
	get_speed_vector(StartOrigin, TargetOrigin, PLASMA_SPEED, Velocity)
	set_pev(Ent, pev_velocity, Velocity)
}

public Make_Muzzleflash(id)
{
	static Float:Origin[3], TE_FLAG
	get_position(id, 32.0, 6.0, -15.0, Origin)
	
	TE_FLAG |= TE_EXPLFLAG_NODLIGHTS
	TE_FLAG |= TE_EXPLFLAG_NOSOUND
	TE_FLAG |= TE_EXPLFLAG_NOPARTICLES
	
	engfunc(EngFunc_MessageBegin, MSG_ONE_UNRELIABLE, SVC_TEMPENTITY, Origin, id)
	write_byte(TE_EXPLOSION)
	engfunc(EngFunc_WriteCoord, Origin[0])
	engfunc(EngFunc_WriteCoord, Origin[1])
	engfunc(EngFunc_WriteCoord, Origin[2])
	write_short(g_MuzzleFlash_SprId)
	write_byte(2)
	write_byte(30)
	write_byte(TE_FLAG)
	message_end()
}


public fw_Think_Plasma(Ent)
{
	if(!pev_valid(Ent))
		return
		
	static Float:RenderAmt; pev(Ent, pev_renderamt, RenderAmt)
	
	RenderAmt += 50.0
	RenderAmt = float(clamp(floatround(RenderAmt), 0, 255))
	
	set_pev(Ent, pev_renderamt, RenderAmt)
	set_pev(Ent, pev_nextthink, halflife_time() + 0.1)
}

public fw_Touch_Plasma(Ent, Id)
{
	if(!pev_valid(Ent))
		return
	if(pev(Ent, pev_movetype) == MOVETYPE_NONE)
		return
		
	// Exp Sprite
	static Float:Origin[3], TE_FLAG
	pev(Ent, pev_origin, Origin)
	
	TE_FLAG |= TE_EXPLFLAG_NODLIGHTS
	TE_FLAG |= TE_EXPLFLAG_NOSOUND
	TE_FLAG |= TE_EXPLFLAG_NOPARTICLES
	
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
	write_byte(TE_EXPLOSION)
	engfunc(EngFunc_WriteCoord, Origin[0])
	engfunc(EngFunc_WriteCoord, Origin[1])
	engfunc(EngFunc_WriteCoord, Origin[2])
	write_short(g_PlasmaExp_SprId)
	write_byte(7)
	write_byte(30)
	write_byte(TE_FLAG)
	message_end()	
	
	// Exp Sound
	emit_sound(Ent, CHAN_BODY, plasma_sounds[1], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
	
	// Damage
	Damage_Plasma(Ent, Id)
	
	// Remove Ent
	set_pev(Ent, pev_movetype, MOVETYPE_NONE)
	set_task(0.1, "Remove_PlasmaBall", Ent)
}

public Damage_Plasma(Ent, Id)
{
	static Owner; Owner = pev(Ent, pev_iuser1)
	static Attacker; 
	if(!is_user_alive(Owner)) 
	{
		Attacker = 0
		return
	} else Attacker = Owner
	
	if(is_user_alive(Id))
		ExecuteHamB(Ham_TakeDamage, Id, Attacker, Attacker, float(DAMAGE), DMG_ACID)
	
	for(new i = 1; i < 32; i++)
	{
		if(!is_user_alive(i))
			continue
		if(entity_range(i, Ent) > PLASMA_RADIUS)
			continue
			
		ExecuteHamB(Ham_TakeDamage, i, Attacker, Attacker, float(DAMAGE) / random_float(1.25, 1.5), DMG_ACID)
	}
}

public Remove_PlasmaBall(Ent)
{
	if(!pev_valid(Ent)) return
	engfunc(EngFunc_RemoveEntity, Ent)
}

stock get_position(id,Float:forw, Float:right, Float:up, Float:vStart[])
{
	static Float:vOrigin[3], Float:vAngle[3], Float:vForward[3], Float:vRight[3], Float:vUp[3]
	
	pev(id, pev_origin, vOrigin)
	pev(id, pev_view_ofs, vUp) //for player
	xs_vec_add(vOrigin, vUp, vOrigin)
	pev(id, pev_v_angle, vAngle) // if normal entity ,use pev_angles
	
	angle_vector(vAngle, ANGLEVECTOR_FORWARD, vForward) //or use EngFunc_AngleVectors
	angle_vector(vAngle, ANGLEVECTOR_RIGHT, vRight)
	angle_vector(vAngle, ANGLEVECTOR_UP, vUp)
	
	vStart[0] = vOrigin[0] + vForward[0] * forw + vRight[0] * right + vUp[0] * up
	vStart[1] = vOrigin[1] + vForward[1] * forw + vRight[1] * right + vUp[1] * up
	vStart[2] = vOrigin[2] + vForward[2] * forw + vRight[2] * right + vUp[2] * up
}

stock get_speed_vector(const Float:origin1[3],const Float:origin2[3],Float:speed, Float:new_velocity[3])
{
	new_velocity[0] = origin2[0] - origin1[0]
	new_velocity[1] = origin2[1] - origin1[1]
	new_velocity[2] = origin2[2] - origin1[2]
	static Float:num; num = floatsqroot(speed*speed / (new_velocity[0]*new_velocity[0] + new_velocity[1]*new_velocity[1] + new_velocity[2]*new_velocity[2]))
	new_velocity[0] *= num
	new_velocity[1] *= num
	new_velocity[2] *= num
	
	return 1;
}

stock set_user_clip(id, ammo)
{
	new weaponid = get_pdata_cbase(id, 373, 5) //const m_pActiveItem = 373;
	if(pev_valid(weaponid))
		set_pdata_int(weaponid, 51, ammo, 4); 
}
