
/* AMX Mod X
*   Snowballs
*
* (c) Copyright 2008 by FakeNick
*
* This file is provided as is (no warranties)
*
*     DESCRIPTION
*       This plugin is changing grenade view model and grenade world model
*	for snowball models. Also it is adding (optional) trail and glow
*
*     MODULES
*	fakemeta
*
*     CVARS
*	snowballs_on - turns snowballs on/off (default on)
*	snowballs_trail - turns on/off trail behind snowball (default on)
*	snowballs_rendering - turns on/off a glow for snowballs (default on)
*
*     VERSIONS
*	1.0 - first release
*
*/

#include <amxmodx>
#include <fakemeta>

new VERSION[] = "1.0"

new MODEL_NADE_WORLD[] = "models/snowballs/w_snowball.mdl"
new MODEL_NADE_VIEW[] = "models/snowballs/v_snowball.mdl"
new MODEL_TRAIL[] = "sprites/laserbeam.spr"

//Cvars
new on
new rendering
new trail

//For snowball trail
new g_trail

public plugin_init()
{
	register_plugin("Snowballs!", VERSION, "FakeNick")
	on = register_cvar("snowballs_on","1")
	if(get_pcvar_num(on))
	{
		rendering = register_cvar("snowballs_rendering","1")
		trail = register_cvar("snowballs_trail","1")
		
		register_forward(FM_SetModel,"forward_model")
		
		register_event("CurWeapon","func_modelchange_hook","be","1=1")
	}
	
}
public plugin_precache()
{
	engfunc(EngFunc_PrecacheModel,MODEL_NADE_WORLD)
	engfunc(EngFunc_PrecacheModel,MODEL_NADE_VIEW)
	g_trail = engfunc(EngFunc_PrecacheModel,MODEL_TRAIL)
}
public func_modelchange_hook(id)
{
	new weapon_id = read_data(2)
	if(weapon_id == CSW_HEGRENADE || weapon_id == CSW_FLASHBANG || weapon_id == CSW_SMOKEGRENADE)
	{
		set_pev(id, pev_viewmodel, engfunc(EngFunc_AllocString, MODEL_NADE_VIEW))
	}
}
public forward_model(entity,const model[])
{
	if(!pev_valid(entity))
	{
		return FMRES_IGNORED
	}
	new rend = get_pcvar_num(rendering)
	new tr = get_pcvar_num(trail)
	if(equali(model,"models/w_hegrenade.mdl"))
	{
		engfunc(EngFunc_SetModel,entity,MODEL_NADE_WORLD)
		if(rend)
		{
			set_rendering(entity,kRenderFxGlowShell,255,0,0,kRenderNormal,255)
		}
		if(tr)
		{
			set_trail(entity,255,0,0,255)
		}
		return FMRES_SUPERCEDE
		
	}else if(equali(model,"models/w_flashbang.mdl"))
	{
		engfunc(EngFunc_SetModel,entity,MODEL_NADE_WORLD)
		if(rend)
		{
			set_rendering(entity,kRenderFxGlowShell,255,255,255,kRenderNormal,255)
		}
		if(tr)
		{
			set_trail(entity,255,255,255,255)
		}
		return FMRES_SUPERCEDE
		
	}else if(equali(model,"models/w_smokegrenade.mdl"))
	{
		engfunc(EngFunc_SetModel,entity,MODEL_NADE_WORLD)
		if(rend)
		{
			set_rendering(entity,kRenderFxGlowShell,0,255,0,kRenderNormal,255)
		}
		if(tr)
		{
			set_trail(entity,0,255,0,255)
		}
		return FMRES_SUPERCEDE
	}
	return FMRES_IGNORED
}
stock set_trail(id,r,g,b,bright)
{
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
	write_byte(22)              
	write_short(id)         
	write_short(g_trail)        
	write_byte(25)              
	write_byte(5)               
	write_byte(r)             
	write_byte(g)               
	write_byte(b)                
	write_byte(bright)                
	message_end()
}
// teame06's function
stock set_rendering(index, fx=kRenderFxNone, r=0, g=0, b=0, render=kRenderNormal, amount=16)
{
	set_pev(index, pev_renderfx, fx)
	new Float:RenderColor[3]
	RenderColor[0] = float(r)
	RenderColor[1] = float(g)
	RenderColor[2] = float(b)
	set_pev(index, pev_rendercolor, RenderColor)
	set_pev(index, pev_rendermode, render)
	set_pev(index, pev_renderamt, float(amount))
}
