hlsdk-xash3d/cl_dll/death.cpp

310 lines
8.0 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* 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.
*
****/
//
// death notice
//
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>
#include <stdio.h>
#if USE_VGUI
#include "vgui_TeamFortressViewport.h"
#endif
DECLARE_MESSAGE( m_DeathNotice, DeathMsg )
2016-06-04 15:24:23 +02:00
struct DeathNoticeItem {
2016-07-03 15:39:55 +02:00
char szKiller[MAX_PLAYER_NAME_LENGTH * 2];
char szVictim[MAX_PLAYER_NAME_LENGTH * 2];
2016-06-04 15:24:23 +02:00
int iId; // the index number of the associated sprite
int iSuicide;
int iTeamKill;
int iNonPlayerKill;
float flDisplayTime;
float *KillerColor;
float *VictimColor;
};
#define MAX_DEATHNOTICES 4
static int DEATHNOTICE_DISPLAY_TIME = 6;
#define DEATHNOTICE_TOP 32
2016-07-03 15:39:55 +02:00
DeathNoticeItem rgDeathNoticeList[MAX_DEATHNOTICES + 1];
2016-06-04 15:24:23 +02:00
float g_ColorBlue[3] = { 0.6, 0.8, 1.0 };
2016-07-03 15:39:55 +02:00
float g_ColorRed[3] = { 1.0, 0.25, 0.25 };
2016-06-04 15:24:23 +02:00
float g_ColorGreen[3] = { 0.6, 1.0, 0.6 };
float g_ColorYellow[3] = { 1.0, 0.7, 0.0 };
float g_ColorGrey[3] = { 0.8, 0.8, 0.8 };
float *GetClientColor( int clientIndex )
{
2016-07-03 15:39:55 +02:00
switch( g_PlayerExtraInfo[clientIndex].teamnumber )
2016-06-04 15:24:23 +02:00
{
case 1: return g_ColorBlue;
case 2: return g_ColorRed;
case 3: return g_ColorYellow;
case 4: return g_ColorGreen;
case 0: return g_ColorYellow;
2016-07-03 15:39:55 +02:00
default: return g_ColorGrey;
2016-06-04 15:24:23 +02:00
}
return NULL;
}
2016-07-03 15:39:55 +02:00
int CHudDeathNotice::Init( void )
2016-06-04 15:24:23 +02:00
{
gHUD.AddHudElem( this );
HOOK_MESSAGE( DeathMsg );
2019-11-03 20:05:51 +01:00
CVAR_CREATE( "hud_deathnotice_time", "6", FCVAR_ARCHIVE );
2016-06-04 15:24:23 +02:00
return 1;
}
2016-07-03 15:39:55 +02:00
void CHudDeathNotice::InitHUDData( void )
2016-06-04 15:24:23 +02:00
{
memset( rgDeathNoticeList, 0, sizeof(rgDeathNoticeList) );
}
2016-07-03 15:39:55 +02:00
int CHudDeathNotice::VidInit( void )
2016-06-04 15:24:23 +02:00
{
m_HUD_d_skull = gHUD.GetSpriteIndex( "d_skull" );
return 1;
}
2016-07-03 15:39:55 +02:00
int CHudDeathNotice::Draw( float flTime )
2016-06-04 15:24:23 +02:00
{
int x, y, r, g, b;
2016-07-03 15:39:55 +02:00
for( int i = 0; i < MAX_DEATHNOTICES; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].iId == 0 )
2016-06-04 15:24:23 +02:00
break; // we've gone through them all
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].flDisplayTime < flTime )
{
// display time has expired
2016-06-04 15:24:23 +02:00
// remove the current item from the list
2016-07-03 15:39:55 +02:00
memmove( &rgDeathNoticeList[i], &rgDeathNoticeList[i + 1], sizeof(DeathNoticeItem) * ( MAX_DEATHNOTICES - i ) );
2016-06-04 15:24:23 +02:00
i--; // continue on the next item; stop the counter getting incremented
continue;
}
2019-08-11 23:25:50 +02:00
rgDeathNoticeList[i].flDisplayTime = Q_min( rgDeathNoticeList[i].flDisplayTime, gHUD.m_flTime + DEATHNOTICE_DISPLAY_TIME );
2016-06-04 15:24:23 +02:00
// Only draw if the viewport will let me
// vgui dropped out
#if USE_VGUI
if( gViewPort && gViewPort->AllowedToPrintText() )
#endif
2016-06-04 15:24:23 +02:00
{
// Draw the death notice
2016-07-03 15:39:55 +02:00
y = YRES( DEATHNOTICE_TOP ) + 2 + ( 20 * i ); //!!!
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
int id = ( rgDeathNoticeList[i].iId == -1 ) ? m_HUD_d_skull : rgDeathNoticeList[i].iId;
x = ScreenWidth - ConsoleStringLen( rgDeathNoticeList[i].szVictim ) - ( gHUD.GetSpriteRect(id).right - gHUD.GetSpriteRect(id).left );
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
if( !rgDeathNoticeList[i].iSuicide )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
x -= ( 5 + ConsoleStringLen( rgDeathNoticeList[i].szKiller ) );
2016-06-04 15:24:23 +02:00
// Draw killers name
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].KillerColor )
2016-06-04 15:24:23 +02:00
DrawSetTextColor( rgDeathNoticeList[i].KillerColor[0], rgDeathNoticeList[i].KillerColor[1], rgDeathNoticeList[i].KillerColor[2] );
x = 5 + DrawConsoleString( x, y, rgDeathNoticeList[i].szKiller );
}
2016-07-03 15:39:55 +02:00
r = 255; g = 80; b = 0;
if( rgDeathNoticeList[i].iTeamKill )
2016-06-04 15:24:23 +02:00
{
r = 10; g = 240; b = 10; // display it in sickly green
}
// Draw death weapon
SPR_Set( gHUD.GetSprite(id), r, g, b );
SPR_DrawAdditive( 0, x, y, &gHUD.GetSpriteRect(id) );
2016-07-03 15:39:55 +02:00
x += ( gHUD.GetSpriteRect(id).right - gHUD.GetSpriteRect(id).left );
2016-06-04 15:24:23 +02:00
// Draw victims name (if it was a player that was killed)
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].iNonPlayerKill == FALSE )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].VictimColor )
2016-06-04 15:24:23 +02:00
DrawSetTextColor( rgDeathNoticeList[i].VictimColor[0], rgDeathNoticeList[i].VictimColor[1], rgDeathNoticeList[i].VictimColor[2] );
x = DrawConsoleString( x, y, rgDeathNoticeList[i].szVictim );
}
}
}
return 1;
}
// This message handler may be better off elsewhere
2016-07-03 15:39:55 +02:00
int CHudDeathNotice::MsgFunc_DeathMsg( const char *pszName, int iSize, void *pbuf )
2016-06-04 15:24:23 +02:00
{
2016-04-17 22:30:05 +02:00
int i;
2016-06-04 15:24:23 +02:00
m_iFlags |= HUD_ACTIVE;
BEGIN_READ( pbuf, iSize );
int killer = READ_BYTE();
int victim = READ_BYTE();
char killedwith[32];
strcpy( killedwith, "d_" );
2016-06-13 15:50:38 +02:00
strncat( killedwith, READ_STRING(), sizeof(killedwith) - strlen(killedwith) - 1 );
2022-11-14 04:14:24 +01:00
killedwith[sizeof(killedwith) - 1] = '\0';
2016-06-04 15:24:23 +02:00
#if USE_VGUI
if (gViewPort)
gViewPort->DeathMsg( killer, victim );
#endif
2016-07-03 15:39:55 +02:00
gHUD.m_Spectator.DeathMessage( victim );
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
for( i = 0; i < MAX_DEATHNOTICES; i++ )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].iId == 0 )
2016-06-04 15:24:23 +02:00
break;
}
2016-07-03 15:39:55 +02:00
if( i == MAX_DEATHNOTICES )
{
// move the rest of the list forward to make room for this item
memmove( rgDeathNoticeList, rgDeathNoticeList + 1, sizeof(DeathNoticeItem) * MAX_DEATHNOTICES );
2016-06-04 15:24:23 +02:00
i = MAX_DEATHNOTICES - 1;
}
gHUD.GetAllPlayersInfo();
2016-06-04 15:24:23 +02:00
// Get the Killer's name
2017-06-29 15:56:03 +02:00
const char *killer_name = "";
killer_name = g_PlayerInfoList[killer].name;
2016-07-03 15:39:55 +02:00
if( !killer_name )
2016-06-04 15:24:23 +02:00
{
killer_name = "";
rgDeathNoticeList[i].szKiller[0] = 0;
}
else
{
rgDeathNoticeList[i].KillerColor = GetClientColor( killer );
2022-11-14 04:47:47 +01:00
strncpy( rgDeathNoticeList[i].szKiller, killer_name, MAX_PLAYER_NAME_LENGTH - 1 );
2016-07-03 15:39:55 +02:00
rgDeathNoticeList[i].szKiller[MAX_PLAYER_NAME_LENGTH - 1] = 0;
2016-06-04 15:24:23 +02:00
}
// Get the Victim's name
2017-06-29 15:56:03 +02:00
const char *victim_name = "";
2016-06-04 15:24:23 +02:00
// If victim is -1, the killer killed a specific, non-player object (like a sentrygun)
2019-10-11 08:08:05 +02:00
if( ( (signed char)victim ) != -1 )
2016-07-03 15:39:55 +02:00
victim_name = g_PlayerInfoList[victim].name;
2017-06-29 15:56:03 +02:00
if( !victim_name )
2016-06-04 15:24:23 +02:00
{
victim_name = "";
rgDeathNoticeList[i].szVictim[0] = 0;
}
else
{
rgDeathNoticeList[i].VictimColor = GetClientColor( victim );
2022-11-14 04:47:47 +01:00
strncpy( rgDeathNoticeList[i].szVictim, victim_name, MAX_PLAYER_NAME_LENGTH - 1 );
2016-07-03 15:39:55 +02:00
rgDeathNoticeList[i].szVictim[MAX_PLAYER_NAME_LENGTH - 1] = 0;
2016-06-04 15:24:23 +02:00
}
// Is it a non-player object kill?
2019-10-11 08:08:05 +02:00
if( ( (signed char)victim ) == -1 )
2016-06-04 15:24:23 +02:00
{
rgDeathNoticeList[i].iNonPlayerKill = TRUE;
// Store the object's name in the Victim slot (skip the d_ bit)
2016-07-03 15:39:55 +02:00
strcpy( rgDeathNoticeList[i].szVictim, killedwith + 2 );
2016-06-04 15:24:23 +02:00
}
else
{
2017-06-29 15:56:03 +02:00
if( killer == victim || killer == 0 )
2016-06-04 15:24:23 +02:00
rgDeathNoticeList[i].iSuicide = TRUE;
2017-06-29 15:56:03 +02:00
if( !strcmp( killedwith, "d_teammate" ) )
2016-06-04 15:24:23 +02:00
rgDeathNoticeList[i].iTeamKill = TRUE;
}
// Find the sprite in the list
int spr = gHUD.GetSpriteIndex( killedwith );
rgDeathNoticeList[i].iId = spr;
DEATHNOTICE_DISPLAY_TIME = CVAR_GET_FLOAT( "hud_deathnotice_time" );
rgDeathNoticeList[i].flDisplayTime = gHUD.m_flTime + DEATHNOTICE_DISPLAY_TIME;
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].iNonPlayerKill )
2016-06-04 15:24:23 +02:00
{
ConsolePrint( rgDeathNoticeList[i].szKiller );
ConsolePrint( " killed a " );
ConsolePrint( rgDeathNoticeList[i].szVictim );
ConsolePrint( "\n" );
}
else
{
// record the death notice in the console
2016-07-03 15:39:55 +02:00
if( rgDeathNoticeList[i].iSuicide )
2016-06-04 15:24:23 +02:00
{
ConsolePrint( rgDeathNoticeList[i].szVictim );
2016-07-03 15:39:55 +02:00
if( !strcmp( killedwith, "d_world" ) )
2016-06-04 15:24:23 +02:00
{
ConsolePrint( " died" );
}
else
{
ConsolePrint( " killed self" );
}
}
2016-07-03 15:39:55 +02:00
else if( rgDeathNoticeList[i].iTeamKill )
2016-06-04 15:24:23 +02:00
{
ConsolePrint( rgDeathNoticeList[i].szKiller );
ConsolePrint( " killed his teammate " );
ConsolePrint( rgDeathNoticeList[i].szVictim );
}
else
{
ConsolePrint( rgDeathNoticeList[i].szKiller );
ConsolePrint( " killed " );
ConsolePrint( rgDeathNoticeList[i].szVictim );
}
2016-07-03 15:39:55 +02:00
if( *killedwith && (*killedwith > 13 ) && strcmp( killedwith, "d_world" ) && !rgDeathNoticeList[i].iTeamKill )
2016-06-04 15:24:23 +02:00
{
ConsolePrint( " with " );
// replace the code names with the 'real' names
2016-07-03 15:39:55 +02:00
if( !strcmp( killedwith + 2, "egon" ) )
2016-06-04 15:24:23 +02:00
strcpy( killedwith, "d_gluon gun" );
2017-06-29 15:56:03 +02:00
if( !strcmp( killedwith + 2, "gauss" ) )
2016-06-04 15:24:23 +02:00
strcpy( killedwith, "d_tau cannon" );
2016-07-03 15:39:55 +02:00
ConsolePrint( killedwith + 2 ); // skip over the "d_" part
2016-06-04 15:24:23 +02:00
}
ConsolePrint( "\n" );
}
return 1;
}