#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <xs>
#define VERSION "0.0.1"
#define PLUGIN "Animated Spray"
//#define m_nCustomSprayFrames 485
#define m_flNextDecalTime 486
#define GetCustomDecalFrames(%0) 6 // get_pdata_int(%0,m_nCustomSprayFrames)
// #define SetCustomDecalFrames(%0,%1) set_pdata_int(%0,m_nCustomSprayFrames,%1)
#define MAX_PLAYERS 32
#define UTIL_MakeVectors(%0) engfunc(EngFunc_MakeVectors,%0)
#define TRACE_LINE(%0,%1,%2,%3,%4) engfunc(EngFunc_TraceLine,%0,%1,%2,%3,%4)
#define ALLOC_STRING(%0) engfunc(EngFunc_AllocString, %0)
#define CREATE_NAMED_ENTITY(%0) engfunc(EngFunc_CreateNamedEntity, %0)
#define REMOVE_ENTITY(%0) engfunc(EngFunc_RemoveEntity, %0)
#define WRITE_COORD(%0) engfunc(EngFunc_WriteCoord,%0)
new const SPRAY_SOUND[] = "player/sprayer.wav"
new const INFO_TARGET[] = "info_target"
new g_iFirstDecalIndex
new decalfrequency
new g_iMaxPlayers
new g_iPlayerSprayCan[MAX_PLAYERS+1]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, "ConnorMcLeod")
decalfrequency = get_cvar_pointer("decalfrequency")
RegisterHam(Ham_Player_ImpulseCommands, "player", "CBasePlayer_ImpulseCommands")
RegisterHam(Ham_Think, INFO_TARGET, "CSprayCan_Think")
g_iFirstDecalIndex = engfunc(EngFunc_DecalIndex, "{lambda01")
g_iMaxPlayers = get_maxplayers()
}
public plugin_precache()
{
precache_sound(SPRAY_SOUND)
}
public CBasePlayer_ImpulseCommands( id )
{
if( pev(id, pev_impulse) == 201 )
{
static Float:flGameTime ; flGameTime = get_gametime()
if( flGameTime >= get_pdata_float(id, m_flNextDecalTime) )
{
new Float:start[3], Float:end[3], Float:origin[3], Float:v_angle[3]
pev(id, pev_v_angle, v_angle)
UTIL_MakeVectors(v_angle)
pev(id, pev_origin, origin)
pev(id, pev_view_ofs, end)
xs_vec_add(origin, end, start)
global_get(glb_v_forward, end)
xs_vec_mul_scalar(end, 128.0, end)
xs_vec_add(start, end, end)
UTIL_TraceLine ( start, end, IGNORE_MONSTERS, id)
new Float:flFraction
get_tr2(0, TR_flFraction, flFraction)
if ( flFraction != 1.0 )
{
set_pdata_float(id, m_flNextDecalTime, flGameTime + get_pcvar_float(decalfrequency))
static info_target
if( !info_target )
{
info_target = ALLOC_STRING( INFO_TARGET )
}
new ent = CREATE_NAMED_ENTITY( info_target )
origin[2] += 32.0
set_pev(ent, pev_origin, origin)
set_pev(ent, pev_angles, v_angle)
g_iPlayerSprayCan[id] = ent
set_pev(ent, pev_frame, 0.0)
set_pev(ent, pev_nextthink, flGameTime + 0.1)
emit_sound(ent, CHAN_VOICE, SPRAY_SOUND, 1.0, ATTN_NORM, 0, PITCH_NORM)
}
}
set_pev(id, pev_impulse, 0)
}
}
public CSprayCan_Think( ent )
{
new id
for(id = g_iMaxPlayers; id>=0; id--)
{
if( g_iPlayerSprayCan[id] == ent )
{
break
}
}
if( id <= 0 )
{
return
}
new nFrames = GetCustomDecalFrames( id )
new Float:start[3], Float:end[3]
pev(ent, pev_angles, start)
UTIL_MakeVectors( start )
pev(ent, pev_origin, start)
global_get(glb_v_forward, end)
xs_vec_mul_scalar(end, 128.0, end)
xs_vec_add(end, start, end)
UTIL_TraceLine (start, end, IGNORE_MONSTERS, id)
new Float:frame
pev(ent, pev_frame, frame)
UTIL_DecalTrace(0, g_iFirstDecalIndex - floatround(frame))
if ( ++frame >= nFrames)
{
REMOVE_ENTITY(ent)
g_iPlayerSprayCan[id] = 0
return
}
set_pev(ent, pev_frame, frame)
set_pev(ent, pev_nextthink, get_gametime() + 0.1)
}
UTIL_DecalTrace( pTrace, index )
{
static entityIndex, pHit,
message, Float:vecEndPos[3]
pHit = get_tr2(pTrace, TR_pHit)
if ( pev_valid(pHit) )
{
if ( !ExecuteHam(Ham_IsBSPModel, pHit) )
return;
entityIndex = pHit
}
else
entityIndex = 0
message = TE_DECAL;
if ( entityIndex != 0 )
{
if ( index > 255 )
{
message = TE_DECALHIGH;
index -= 256
}
}
else
{
message = TE_WORLDDECAL;
if ( index > 255 )
{
message = TE_WORLDDECALHIGH;
index -= 256
}
}
get_tr2(pTrace, TR_vecEndPos, vecEndPos)
message_begin( MSG_BROADCAST, SVC_TEMPENTITY )
{
write_byte( message )
WRITE_COORD( vecEndPos[0] )
WRITE_COORD( vecEndPos[1] )
WRITE_COORD( vecEndPos[2] )
write_byte( index )
if ( entityIndex )
{
write_short( entityIndex )
}
}
message_end()
}
UTIL_TraceLine( Float:vecStart[3], Float:vecEnd[3], igmon, pentIgnore, ptr=0 )
{
TRACE_LINE( vecStart, vecEnd, igmon == IGNORE_MONSTERS, pentIgnore, ptr )
}