PHP Code:
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#include <cstrike> // cs_get_user_team ...
// For testing...
// #include <fun>
#define cfg_file "addons/amxmodx/configs/hp_based_glow.ini"
new Array:color_maxpercent
new Array:color_minpercent
new Array:glow_color[3]
new player_maxhealth[33]
new show_glow[33]
new Float:show_glowtime[33]
new show_glowcolor[33][3]
public plugin_init()
{
register_plugin("Glow color based on HP", "1.0", "Gyiove(Daredevil) & ezio_auditore")
RegisterHam(Ham_Spawn, "player", "player_spawn", 1)
RegisterHam(Ham_TakeDamage, "player", "player_takedamage")
register_forward( FM_AddToFullPack, "player_render", 1 )
cfg_loadcolors()
}
// For bots...
public client_putinserver(id) if (is_user_bot(id)) set_task( 0.5, "ham_forbots", id )
public ham_forbots(id)
{
RegisterHamFromEntity(Ham_TakeDamage, id, "player_takedamage")
RegisterHamFromEntity(Ham_Spawn, id, "player_spawn", 1)
}
// id2 is attacker
public player_takedamage(id, inflictor, id2, Float:damage, damage_type)
{
static Float:hp, Float:maxhp, percent, a, percent_min, percent_max
// Attacter is not player or attacter is doing dmg for herself
// That last check i added is blocking team kill glow show
if( id == id2 || !is_user_connected(id2) || cs_get_user_team(id) == cs_get_user_team(id2) ) return
// Check if player have more hp somehow and update max health
update_maxhealth(id)
show_glow[id2] = id
show_glowtime[id2] = 2.0
hp = float(update_maxhealth(id))
maxhp = float(player_maxhealth[id])
percent = floatround( (hp / maxhp) * 100.0 )
show_glowcolor[id2][0] = 255
show_glowcolor[id2][1] = 255
show_glowcolor[id2][2] = 255
for( a = 0; a < ArraySize(color_maxpercent); a++ )
{
percent_min = ArrayGetCell( color_minpercent, a )
percent_max = ArrayGetCell( color_maxpercent, a )
if( percent >= percent_min && percent_max >= percent )
{
show_glowcolor[id2][0] = ArrayGetCell( glow_color[0], a )
show_glowcolor[id2][1] = ArrayGetCell( glow_color[1], a )
show_glowcolor[id2][2] = ArrayGetCell( glow_color[2], a )
break
}
}
}
public player_render( es_handle, e, ent, host, hostflags, player, pSet )
{
if( !player || show_glow[host] != ent ) return
if( show_glowtime[host] < 0.0 )
{
set_es(es_handle, ES_RenderFx, kRenderNormal)
set_es(es_handle, ES_RenderColor, { 0, 0, 0 } )
show_glow[host] = 0
return
}
static rgb[3], Float:d
d = show_glowtime[host]
if( d > 1.0 ) d = 1.0
rgb[0] = floatround(float(show_glowcolor[host][0]) * d)
rgb[1] = floatround(float(show_glowcolor[host][1]) * d)
rgb[2] = floatround(float(show_glowcolor[host][2]) * d)
set_es(es_handle, ES_RenderFx, kRenderFxGlowShell)
set_es(es_handle, ES_RenderColor, rgb )
set_es(es_handle, ES_RenderAmt, 80 )
show_glowtime[host] -= 0.01
}
public player_spawn(id)
{
// for testing...
// if( id == 1) set_user_health(id, 99000)
player_maxhealth[id] = get_user_health(id)
set_task( 0.1, "check_hpagain", id ) // Maybe some mod will increase hp, lets check that
}
public check_hpagain(id) update_maxhealth(id)
cfg_loadcolors()
{
new a, f, line[64], maxpre[4], minpre[4], c, d, rgb
f = fopen(cfg_file, "rt")
// Create arrays where we push ur loaded data
color_maxpercent = ArrayCreate(1,1)
color_minpercent = ArrayCreate(1,1)
for( a = 0; a < 3; a++ ) glow_color[a] = ArrayCreate(1,1)
// Starding loop, loop end when file ends.
// Also if ur file dont exists or for some ohter reason file was unable to open when we dont start the loop and skip everything
while (f && !feof(f))
{
fgets(f, line, charsmax(line)) // Reading lines from file 'cfg_file'
replace(line, charsmax(line), "^n", "") // Cutting next line so we read only one line at the time
// If line first char is / or whitespace or nothing then skip that line
if( line[0] == '/' || line[0] == ' ' || !line[0] ) continue
/*
If line first char is G then we assume that line look something like this:
Glow color Red:51
*/
if( rgb == 3 ) rgb = 0
if( line[0] == 'G' )
{
c = 0
d = 0
for( a = 0; a < 64; a++ )
{
if( !line[a] ) break // no chars in line anymore? Lets stop the loop then ...
if(d && isdigit(line[a]))
{
if( c == 3 ) break // there is no room in variable where we put or numbers
minpre[c] = line[a] // lets use random variable to hold that color
minpre[c + 1] = 0
c++
}
if(line[a] == ':') d = 1 // we found a place where comes color value
}
// push ur readed data to array
ArrayPushCell( glow_color[rgb], str_to_num(minpre) )
rgb++
continue
}
/*
If line first char is H then we assume that line look something like this:
HP:100% - 70%
*/
if( line[0] == 'H' )
{
// Lets get first numbers into maxpre variable
c = 0
d = 0
for( a = 0; a < 64; a++ )
{
if( !line[a] ) break // no chars in line anymore? Lets stop the loop then ...
if( c == 3 && d ) break // there is no room in variable where we put or numbers
if( line[a] == '-' ) // If char in a pos is - then it means we have to copy numbers into ohter variable
{
d++
c = 0
continue
}
if( c == 3 ) continue // we cant stop loop, so we wait...
if(isdigit(line[a])) // Char in a pos is number, lets copy that into ur variable
{
if(d)
{
minpre[c] = line[a]
minpre[c + 1] = 0
}
else
{
maxpre[c] = line[a]
maxpre[c + 1] = 0
}
c++
}
}
// Lets push ur loaded data into arrays
ArrayPushCell( color_maxpercent, str_to_num(maxpre) )
ArrayPushCell( color_minpercent, str_to_num(minpre) )
}
}
}
stock update_maxhealth(id)
{
static h
h = get_user_health(id)
if( h > player_maxhealth[id] ) player_maxhealth[id] = h
return h
}