Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

474 lines
9.9 KiB

9 years ago
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// Health.cpp
//
// implementation of CHudHealth class
//
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
9 years ago
#include "mobility_int.h"
9 years ago
DECLARE_MESSAGE( m_Health, Health )
DECLARE_MESSAGE( m_Health, Damage )
9 years ago
#define PAIN_NAME "sprites/%d_pain.spr"
#define DAMAGE_NAME "sprites/%d_dmg.spr"
int giDmgHeight, giDmgWidth;
9 years ago
int giDmgFlags[NUM_DMG_TYPES] =
9 years ago
{
DMG_POISON,
DMG_ACID,
DMG_FREEZE|DMG_SLOWFREEZE,
DMG_DROWN,
DMG_BURN|DMG_SLOWBURN,
DMG_NERVEGAS,
DMG_RADIATION,
DMG_SHOCK,
DMG_CALTROP,
DMG_TRANQ,
DMG_CONCUSS,
DMG_HALLUC
};
9 years ago
int CHudHealth::Init( void )
9 years ago
{
9 years ago
HOOK_MESSAGE( Health );
HOOK_MESSAGE( Damage );
m_iHealth = 100;
9 years ago
m_fFade = 0;
m_iFlags = 0;
m_bitsDamage = 0;
m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 0;
giDmgHeight = 0;
giDmgWidth = 0;
9 years ago
memset( m_dmg, 0, sizeof(DAMAGE_IMAGE) * NUM_DMG_TYPES );
9 years ago
9 years ago
gHUD.AddHudElem( this );
9 years ago
return 1;
}
void CHudHealth::Reset( void )
{
// make sure the pain compass is cleared when the player respawns
m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 0;
// force all the flashing damage icons to expire
m_bitsDamage = 0;
9 years ago
for( int i = 0; i < NUM_DMG_TYPES; i++ )
9 years ago
{
m_dmg[i].fExpire = 0;
}
}
9 years ago
int CHudHealth::VidInit( void )
9 years ago
{
m_hSprite = 0;
m_HUD_dmg_bio = gHUD.GetSpriteIndex( "dmg_bio" ) + 1;
m_HUD_cross = gHUD.GetSpriteIndex( "cross" );
9 years ago
giDmgHeight = gHUD.GetSpriteRect( m_HUD_dmg_bio ).right - gHUD.GetSpriteRect( m_HUD_dmg_bio ).left;
giDmgWidth = gHUD.GetSpriteRect( m_HUD_dmg_bio ).bottom - gHUD.GetSpriteRect( m_HUD_dmg_bio ).top;
9 years ago
return 1;
}
9 years ago
int CHudHealth::MsgFunc_Health( const char *pszName, int iSize, void *pbuf )
9 years ago
{
// TODO: update local health data
BEGIN_READ( pbuf, iSize );
int x = READ_BYTE();
m_iFlags |= HUD_ACTIVE;
// Only update the fade if we've changed health
9 years ago
if( x != m_iHealth )
9 years ago
{
m_fFade = FADE_TIME;
m_iHealth = x;
}
return 1;
}
9 years ago
int CHudHealth::MsgFunc_Damage( const char *pszName, int iSize, void *pbuf )
9 years ago
{
BEGIN_READ( pbuf, iSize );
int armor = READ_BYTE(); // armor
int damageTaken = READ_BYTE(); // health
long bitsDamage = READ_LONG(); // damage bits
vec3_t vecFrom;
for( int i = 0; i < 3; i++ )
9 years ago
vecFrom[i] = READ_COORD();
9 years ago
UpdateTiles( gHUD.m_flTime, bitsDamage );
9 years ago
// Actually took damage?
9 years ago
if( damageTaken > 0 || armor > 0 )
9 years ago
{
9 years ago
CalcDamageDirection( vecFrom );
9 years ago
if( gMobileEngfuncs && damageTaken > 0 )
{
9 years ago
float time = damageTaken * 4.0f;
9 years ago
9 years ago
if( time > 200.0f )
time = 200.0f;
gMobileEngfuncs->pfnVibrate( time, 0 );
9 years ago
}
}
return 1;
}
// Returns back a color from the
// Green <-> Yellow <-> Red ramp
void CHudHealth::GetPainColor( int &r, int &g, int &b )
{
#if 0
9 years ago
int iHealth = m_iHealth;
9 years ago
if( iHealth > 25 )
9 years ago
iHealth -= 25;
9 years ago
else if( iHealth < 0 )
9 years ago
iHealth = 0;
9 years ago
g = iHealth * 255 / 100;
r = 255 - g;
b = 0;
#else
if( m_iHealth <= 0 )
{
6 years ago
r = 250;
g = b = 0;
}
else if( m_iHealth > 15 )
9 years ago
{
9 years ago
UnpackRGB( r, g, b, RGB_YELLOWISH );
9 years ago
}
else
{
UnpackRGB( r, g, b, RGB_YELLOWISH );
float f = ( sin( ( 16 - gHUD.m_flTime ) * m_iHealth ) + 1.0 ) * 0.5;
r = (int)( r * f * 58.0f );
g = (int)( g * f );
b = (int)( b * f );
9 years ago
}
9 years ago
#endif
9 years ago
}
9 years ago
int CHudHealth::Draw( float flTime )
9 years ago
{
int r, g, b;
int a = 0, x, y;
int HealthWidth;
9 years ago
if( ( gHUD.m_iHideHUDDisplay & HIDEHUD_HEALTH ) || gEngfuncs.IsSpectateOnly() )
9 years ago
return 1;
9 years ago
if( !m_hSprite )
m_hSprite = LoadSprite( PAIN_NAME );
9 years ago
// Has health changed? Flash the health #
9 years ago
if( m_fFade )
9 years ago
{
9 years ago
m_fFade -= ( gHUD.m_flTimeDelta * 20 );
if( m_fFade <= 0 )
9 years ago
{
m_fFade = 0;
}
// Fade the health number back to dim
a = MIN_ALPHA + ( m_fFade / FADE_TIME ) * 128;
9 years ago
}
else
a = MIN_ALPHA;
9 years ago
// If health is getting low, make it bright red
9 years ago
if( m_iHealth <= 15 )
a = 192;
9 years ago
9 years ago
GetPainColor( r, g, b );
9 years ago
ScaleColors( r, g, b, a );
9 years ago
wrect_t cross = gHUD.GetSpriteRect( m_HUD_cross );
9 years ago
int CrossWidth = cross.right - cross.left;
int CrossHeight = cross.bottom - cross.top;
9 years ago
x = 10;
y = ScreenHeight - CrossHeight - 10;
9 years ago
SPR_Set( gHUD.GetSprite( m_HUD_cross ), r, g, b );
SPR_DrawAdditive( 0, 10, y, &cross );
9 years ago
x = CrossWidth + 20;
y = ScreenHeight - gHUD.m_iFontHeight - 10;
9 years ago
gHUD.DrawHudNumber( x, y, DHN_3DIGITS | DHN_DRAWZERO, m_iHealth, r, g, b );
9 years ago
9 years ago
DrawDamage( flTime );
return DrawPain( flTime );
9 years ago
}
9 years ago
void CHudHealth::CalcDamageDirection( vec3_t vecFrom )
9 years ago
{
vec3_t forward, right, up;
float side, front;
9 years ago
vec3_t vecOrigin, vecAngles;
9 years ago
if( !vecFrom[0] && !vecFrom[1] && !vecFrom[2] )
9 years ago
{
m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 0;
return;
}
9 years ago
memcpy( vecOrigin, gHUD.m_vecOrigin, sizeof(vec3_t) );
memcpy( vecAngles, gHUD.m_vecAngles, sizeof(vec3_t) );
9 years ago
9 years ago
VectorSubtract( vecFrom, vecOrigin, vecFrom );
9 years ago
float flDistToTarget = vecFrom.Length();
vecFrom = vecFrom.Normalize();
9 years ago
AngleVectors( vecAngles, forward, right, up );
9 years ago
9 years ago
front = DotProduct( vecFrom, right );
side = DotProduct( vecFrom, forward );
9 years ago
9 years ago
if( flDistToTarget <= 50 )
9 years ago
{
m_fAttackFront = m_fAttackRear = m_fAttackRight = m_fAttackLeft = 1;
}
else
{
9 years ago
if( side > 0 )
9 years ago
{
9 years ago
if( side > 0.3 )
m_fAttackFront = max( m_fAttackFront, side );
9 years ago
}
else
{
9 years ago
float f = fabs( side );
if( f > 0.3 )
m_fAttackRear = max( m_fAttackRear, f );
9 years ago
}
9 years ago
if( front > 0 )
9 years ago
{
9 years ago
if( front > 0.3 )
m_fAttackRight = max( m_fAttackRight, front );
9 years ago
}
else
{
9 years ago
float f = fabs( front );
if( f > 0.3 )
m_fAttackLeft = max( m_fAttackLeft, f );
9 years ago
}
}
}
9 years ago
int CHudHealth::DrawPain( float flTime )
9 years ago
{
9 years ago
if( !( m_fAttackFront || m_fAttackRear || m_fAttackLeft || m_fAttackRight) )
9 years ago
return 1;
int r, g, b;
int x, y, a, shade;
// TODO: get the shift value of the health
a = 255; // max brightness until then
float fFade = gHUD.m_flTimeDelta * 2;
9 years ago
9 years ago
// SPR_Draw top
9 years ago
if( m_fAttackFront > 0.4 )
9 years ago
{
9 years ago
GetPainColor( r, g, b );
9 years ago
shade = a * max( m_fAttackFront, 0.5 );
9 years ago
ScaleColors( r, g, b, shade );
SPR_Set( m_hSprite, r, g, b );
9 years ago
9 years ago
x = ScreenWidth / 2 - SPR_Width( m_hSprite, 0 ) / 2;
y = ScreenHeight / 2 - SPR_Height( m_hSprite, 0 ) * 3;
SPR_DrawAdditive( 0, x, y, NULL );
9 years ago
m_fAttackFront = max( 0, m_fAttackFront - fFade );
} else
m_fAttackFront = 0;
9 years ago
if( m_fAttackRight > 0.4 )
9 years ago
{
9 years ago
GetPainColor( r, g, b );
9 years ago
shade = a * max( m_fAttackRight, 0.5 );
9 years ago
ScaleColors( r, g, b, shade );
SPR_Set( m_hSprite, r, g, b );
9 years ago
9 years ago
x = ScreenWidth / 2 + SPR_Width( m_hSprite, 1 ) * 2;
y = ScreenHeight / 2 - SPR_Height( m_hSprite,1 ) / 2;
SPR_DrawAdditive( 1, x, y, NULL );
9 years ago
m_fAttackRight = max( 0, m_fAttackRight - fFade );
9 years ago
}
else
9 years ago
m_fAttackRight = 0;
9 years ago
if( m_fAttackRear > 0.4 )
9 years ago
{
9 years ago
GetPainColor( r, g, b );
9 years ago
shade = a * max( m_fAttackRear, 0.5 );
9 years ago
ScaleColors( r, g, b, shade );
SPR_Set( m_hSprite, r, g, b );
9 years ago
9 years ago
x = ScreenWidth / 2 - SPR_Width( m_hSprite, 2 ) / 2;
y = ScreenHeight / 2 + SPR_Height( m_hSprite, 2 ) * 2;
SPR_DrawAdditive( 2, x, y, NULL );
9 years ago
m_fAttackRear = max( 0, m_fAttackRear - fFade );
9 years ago
}
else
9 years ago
m_fAttackRear = 0;
9 years ago
if( m_fAttackLeft > 0.4 )
9 years ago
{
9 years ago
GetPainColor( r, g, b );
9 years ago
shade = a * max( m_fAttackLeft, 0.5 );
9 years ago
ScaleColors( r, g, b, shade );
SPR_Set( m_hSprite, r, g, b );
9 years ago
9 years ago
x = ScreenWidth / 2 - SPR_Width( m_hSprite, 3 ) * 3;
y = ScreenHeight / 2 - SPR_Height( m_hSprite,3 ) / 2;
SPR_DrawAdditive( 3, x, y, NULL );
9 years ago
m_fAttackLeft = max( 0, m_fAttackLeft - fFade );
} else
m_fAttackLeft = 0;
return 1;
}
9 years ago
int CHudHealth::DrawDamage( float flTime )
9 years ago
{
int i, r, g, b, a;
DAMAGE_IMAGE *pdmg;
if( !m_bitsDamage )
9 years ago
return 1;
9 years ago
UnpackRGB( r, g, b, RGB_YELLOWISH );
a = (int)( fabs( sin( flTime * 2 ) ) * 256.0 );
9 years ago
9 years ago
ScaleColors( r, g, b, a );
9 years ago
// check for bits that should be expired
9 years ago
for( i = 0; i < NUM_DMG_TYPES; i++ )
9 years ago
{
9 years ago
if( m_bitsDamage & giDmgFlags[i] )
9 years ago
{
9 years ago
pdmg = &m_dmg[i];
// Draw all the items
SPR_Set( gHUD.GetSprite( m_HUD_dmg_bio + i ), r, g, b );
SPR_DrawAdditive( 0, pdmg->x, pdmg->y, &gHUD.GetSpriteRect( m_HUD_dmg_bio + i ) );
9 years ago
pdmg->fExpire = min( flTime + DMG_IMAGE_LIFE, pdmg->fExpire );
9 years ago
if( pdmg->fExpire <= flTime // when the time has expired
9 years ago
&& a < 40 ) // and the flash is at the low point of the cycle
{
pdmg->fExpire = 0;
int y = pdmg->y;
pdmg->x = pdmg->y = 0;
// move everyone above down
9 years ago
for( int j = 0; j < NUM_DMG_TYPES; j++ )
9 years ago
{
pdmg = &m_dmg[j];
9 years ago
if( ( pdmg->y ) && ( pdmg->y < y ) )
9 years ago
pdmg->y += giDmgHeight;
}
m_bitsDamage &= ~giDmgFlags[i]; // clear the bits
}
}
}
return 1;
}
9 years ago
void CHudHealth::UpdateTiles( float flTime, long bitsDamage )
9 years ago
{
DAMAGE_IMAGE *pdmg;
// Which types are new?
long bitsOn = ~m_bitsDamage & bitsDamage;
9 years ago
for( int i = 0; i < NUM_DMG_TYPES; i++ )
9 years ago
{
pdmg = &m_dmg[i];
// Is this one already on?
9 years ago
if( m_bitsDamage & giDmgFlags[i] )
9 years ago
{
pdmg->fExpire = flTime + DMG_IMAGE_LIFE; // extend the duration
9 years ago
if( !pdmg->fBaseline )
9 years ago
pdmg->fBaseline = flTime;
}
// Are we just turning it on?
9 years ago
if( bitsOn & giDmgFlags[i] )
9 years ago
{
// put this one at the bottom
9 years ago
pdmg->x = giDmgWidth / 8;
9 years ago
pdmg->y = ScreenHeight - giDmgHeight * 2;
pdmg->fExpire=flTime + DMG_IMAGE_LIFE;
9 years ago
9 years ago
// move everyone else up
9 years ago
for( int j = 0; j < NUM_DMG_TYPES; j++ )
9 years ago
{
9 years ago
if( j == i )
9 years ago
continue;
pdmg = &m_dmg[j];
9 years ago
if( pdmg->y )
9 years ago
pdmg->y -= giDmgHeight;
}
pdmg = &m_dmg[i];
9 years ago
}
}
9 years ago
// damage bits are only turned on here; they are turned off when the draw time has expired (in DrawDamage())
m_bitsDamage |= bitsDamage;
}