2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-22 09:57:21 +01:00
hlsdk-xash3d/dlls/stats.cpp

152 lines
3.9 KiB
C++
Raw Normal View History

2018-01-19 18:00:32 +01:00
//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
2016-06-04 15:24:23 +02:00
//
// Purpose: New version of the slider bar
//
// $NoKeywords: $
//=============================================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "player.h"
#include "trains.h"
#include "nodes.h"
#include "weapons.h"
#include "soundent.h"
#include "monsters.h"
2017-02-01 15:55:40 +01:00
#include "../engine/shake.h"
2016-06-04 15:24:23 +02:00
#include "decals.h"
#include "gamerules.h"
float AmmoDamage( const char *pName )
{
2016-07-31 15:48:50 +02:00
if( !pName )
2016-06-04 15:24:23 +02:00
return 0;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "9mm" ) )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmg9MM;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "357" ) )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmg357;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "ARgrenades" ) )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgM203Grenade;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "buckshot" ) )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgBuckshot;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "bolts") )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgCrossbowMonster;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "rockets") )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgRPG;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "uranium") )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgGauss;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "Hand Grenade") )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgHandGrenade;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "Satchel Charge") )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgSatchel;
2016-07-31 15:48:50 +02:00
if( !strcmp( pName, "Trip Mine") )
2016-06-04 15:24:23 +02:00
return gSkillData.plrDmgTripmine;
return 0;
}
2017-07-23 23:24:55 +02:00
void UpdateStatsFile( float dataTime, const char *pMapname, float health, float ammo, int skillLevel )
2016-06-04 15:24:23 +02:00
{
FILE *fp;
fp = fopen( "stats.txt", "a" );
2016-07-31 15:48:50 +02:00
if( !fp )
2016-06-04 15:24:23 +02:00
return;
fprintf( fp, "%6.2f, %6.2f, %6.2f, %s, %2d\n", dataTime, health, ammo, pMapname, skillLevel );
fclose( fp );
}
#define AMMO_THRESHOLD 10 // This much ammo goes by before it is "interesting"
#define HEALTH_THRESHOLD 10 // Same for health
#define OUTPUT_LATENCY 3 // This many seconds for ammo/health to settle
typedef struct
{
2016-07-31 15:48:50 +02:00
int lastAmmo;
float lastHealth;
float lastOutputTime; // NOTE: These times are in "game" time -- a running total of elapsed time since the game started
float nextOutputTime;
float dataTime;
float gameTime;
float lastGameTime;
2016-06-04 15:24:23 +02:00
} TESTSTATS;
2016-07-31 15:48:50 +02:00
TESTSTATS gStats = { 0, 0, 0, 0, 0, 0, 0 };
2016-06-04 15:24:23 +02:00
void UpdateStats( CBasePlayer *pPlayer )
{
int i;
2016-07-31 15:48:50 +02:00
int ammoCount[MAX_AMMO_SLOTS];
2016-06-04 15:24:23 +02:00
memcpy( ammoCount, pPlayer->m_rgAmmo, MAX_AMMO_SLOTS * sizeof(int) );
// Keep a running time, so the graph doesn't overlap
2016-07-31 15:48:50 +02:00
if( gpGlobals->time < gStats.lastGameTime ) // Changed level or died, don't b0rk
2016-06-04 15:24:23 +02:00
{
gStats.lastGameTime = gpGlobals->time;
gStats.dataTime = gStats.gameTime;
}
gStats.gameTime += gpGlobals->time - gStats.lastGameTime;
gStats.lastGameTime = gpGlobals->time;
2016-07-31 15:48:50 +02:00
for( i = 0; i < MAX_ITEM_TYPES; i++ )
2016-06-04 15:24:23 +02:00
{
CBasePlayerItem *p = pPlayer->m_rgpPlayerItems[i];
2016-07-31 15:48:50 +02:00
while( p )
2016-06-04 15:24:23 +02:00
{
2016-08-02 13:59:22 +02:00
ItemInfo II = {0};
2016-07-31 15:48:50 +02:00
p->GetItemInfo( &II );
int index = pPlayer->GetAmmoIndex( II.pszAmmo1 );
if( index >= 0 )
ammoCount[index] += ( (CBasePlayerWeapon *)p )->m_iClip;
2016-06-04 15:24:23 +02:00
p = p->m_pNext;
}
}
float ammo = 0;
2016-07-31 15:48:50 +02:00
for( i = 1; i < MAX_AMMO_SLOTS; i++ )
2016-06-04 15:24:23 +02:00
{
ammo += ammoCount[i] * AmmoDamage( CBasePlayerItem::AmmoInfoArray[i].pszName );
}
float health = pPlayer->pev->health + pPlayer->pev->armorvalue * 2; // Armor is 2X health
float ammoDelta = fabs( ammo - gStats.lastAmmo );
float healthDelta = fabs( health - gStats.lastHealth );
int forceWrite = 0;
2016-07-31 15:48:50 +02:00
if( health <= 0 && gStats.lastHealth > 0 )
2016-06-04 15:24:23 +02:00
forceWrite = 1;
2016-07-31 15:48:50 +02:00
if( ( ammoDelta > AMMO_THRESHOLD || healthDelta > HEALTH_THRESHOLD ) && !forceWrite )
2016-06-04 15:24:23 +02:00
{
2016-07-31 15:48:50 +02:00
if( gStats.nextOutputTime == 0 )
2016-06-04 15:24:23 +02:00
gStats.dataTime = gStats.gameTime;
gStats.lastAmmo = ammo;
gStats.lastHealth = health;
gStats.nextOutputTime = gStats.gameTime + OUTPUT_LATENCY;
}
2016-07-31 15:48:50 +02:00
else if( ( gStats.nextOutputTime != 0 && gStats.nextOutputTime < gStats.gameTime ) || forceWrite )
2016-06-04 15:24:23 +02:00
{
2017-07-23 23:24:55 +02:00
UpdateStatsFile( gStats.dataTime, STRING( gpGlobals->mapname ), health, ammo, (int)CVAR_GET_FLOAT( "skill" ) );
2016-06-04 15:24:23 +02:00
gStats.lastAmmo = ammo;
gStats.lastHealth = health;
gStats.lastOutputTime = gStats.gameTime;
gStats.nextOutputTime = 0;
}
}
void InitStats( CBasePlayer *pPlayer )
{
gStats.lastGameTime = gpGlobals->time; // Fixup stats time
}