new Float: pHealth[MAX_PLAYERS];
SetTimer("CheckH", 1000, 1);
stock SetPlayerHealthEx(playerid, Float: health)
{
pHealth[playerid] = health;
SetPlayerHeatlh(playerid, health);
return 1;
}
stock GetPlayerHealthEx(playerid, &Float: health)
{
GetPlayerHealth(playerid, health);
return 1;
}
forward CheckH();
public CheckH()
{
static Float:old_health;
foreach(Player, i)
{
GetPlayerHealth(i, old_health);
if(old_health != pHealth[i])
{
SetPlayerHealthEx(i, pHealth[i]);
}
}
return 1;
}
new
Float:g_afPreviousHealth[ MAX_PLAYERS ], // This variable holds the last known health of each player
bool:g_abIsSpawned[ MAX_PLAYERS char ] // This variable defines wether a player is spawned or not
;
public OnPlayerConnect( playerid )
{
g_abIsSpawned[ playerid ] = false; // The player just connected - not spawned
}
public OnPlayerStateChange( playerid, newstate, oldstate )
{
if ( newstate == PLAYER_STATE_ONFOOT || newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER || newstate == PLAYER_STATE_SPAWNED )
{ // ^ If the new state is one of the states spawned players has ..
if ( oldstate == PLAYER_STATE_NONE || oldstate == PLAYER_STATE_SPECTATING || oldstate == PLAYER_STATE_WASTED )
{ // ^ If the old state isn't one of the states spawned players has ..
g_abIsSpawned [ playerid ] = true; // The player is now considered spawned
g_afPreviousHealth[ playerid ] = 100.0; // Health is set to 100 when you spawn
}
}
else
{ // The new state is one that spawned players don't have
g_abIsSpawned[ playerid ] = false;
}
}
public OnPlayerUpdate( playerid )
{
static
Float:s_fHealth
;
GetPlayerHealth( playerid, s_fHealth ); // Get the current health
if ( s_fHealth != g_afPreviousHealth[ playerid ] ) // Compare it to the last known health
{
if ( s_fHealth > g_afPreviousHealth[ playerid ] )
{ // The new health is more than the previous!
static
s_szMessage[ 64 ]
;
format( s_szMessage, sizeof( s_szMessage ), "Health increased for player %d: %0.f -> %0.f", playerid, g_afPreviousHealth[ playerid ], s_fHealth );
SendClientMessageToAll( -1, s_szMessage );
}
g_afPreviousHealth[ playerid ] = s_fHealth; // Set the previous health to the new one
}
return 1; // Never forget this in OnPlayerUpdate
}
checking every players health 40-60times a second isn't the best idea
|
else if(old_health < pHealth[i])
{
pHealth[i] = old_health;
}