forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl_entityreport.cpp
More file actions
607 lines (512 loc) · 15.5 KB
/
cl_entityreport.cpp
File metadata and controls
607 lines (512 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "client_pch.h"
#include "ivideomode.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
ConVar cl_entityreport( "cl_entityreport", "0", FCVAR_CHEAT, "For debugging, draw entity states to console" );
ConVar cl_entityreport_sorted( "cl_entityreport_sorted", "0", FCVAR_CHEAT, "For debugging, draw entity states to console in sorted order. [0 = disabled, 1 = average, 2 = current, 3 = peak" );
enum
{
ENTITYSORT_NONE = 0,
ENTITYSORT_AVG = 1,
ENTITYSORT_CURRENT = 2,
ENTITYSORT_PEAK = 3,
};
// How quickly to move rolling average for entityreport
#define BITCOUNT_AVERAGE 0.95f
// How long to flush item when something important happens
#define EFFECT_TIME 1.5f
// How long to latch peak bit count for item
#define PEAK_LATCH_TIME 2.0f;
//-----------------------------------------------------------------------------
// Purpose: Entity report event types
//-----------------------------------------------------------------------------
enum
{
FENTITYBITS_ZERO = 0,
FENTITYBITS_ADD = 0x01,
FENTITYBITS_LEAVEPVS = 0x02,
FENTITYBITS_DELETE = 0x04,
};
//-----------------------------------------------------------------------------
// Purpose: Data about an entity
//-----------------------------------------------------------------------------
typedef struct
{
// Bits used for last message
int bits;
// Rolling average of bits used
float average;
// Last bit peak
int peak;
// Time at which peak was last reset
float peaktime;
// Event info
int flags;
// If doing effect, when it will finish
float effectfinishtime;
// If event was deletion, remember client class for a little bit
ClientClass *deletedclientclass;
} ENTITYBITS;
// List of entities we are keeping data bout
static ENTITYBITS s_EntityBits[ MAX_EDICTS ];
// Used to sort by average
int CompareEntityBits(const void* pIndexA, const void* pIndexB )
{
int indexA = *(int*)pIndexA;
int indexB = *(int*)pIndexB;
ENTITYBITS *pEntryA = &s_EntityBits[indexA];
ENTITYBITS *pEntryB = &s_EntityBits[indexB];
/*
if ( pEntryA->flags == FENTITYBITS_ZERO )
{
if ( pEntryB->flags == FENTITYBITS_ZERO )
{
return 0;
}
return 1;
}
else if ( pEntryB->flags == FENTITYBITS_ZERO )
{
return -1;
}
*/
// sort dormant, out-of-pvs to the end
IClientNetworkable *pNetA = entitylist->GetClientNetworkable( indexA );
IClientNetworkable *pNetB = entitylist->GetClientNetworkable( indexB );
bool bDormantA = pNetA == NULL || pNetA->IsDormant();
bool bDormantB = pNetB == NULL || pNetB->IsDormant();
if ( bDormantA != bDormantB )
{
return bDormantA ? 1 : -1;
}
switch ( cl_entityreport_sorted.GetInt() )
{
case ENTITYSORT_AVG:
if ( pEntryA->average > pEntryB->average )
{
return -1;
}
if ( pEntryA->average < pEntryB->average )
{
return 1;
}
break;
case ENTITYSORT_CURRENT:
if ( pEntryA->bits > pEntryB->bits )
{
return -1;
}
if ( pEntryA->bits < pEntryB->bits )
{
return 1;
}
break;
case ENTITYSORT_PEAK:
default:
if ( pEntryA->peak > pEntryB->peak )
{
return -1;
}
if ( pEntryA->peak < pEntryB->peak )
{
return 1;
}
}
return 0;
}
//-----------------------------------------------------------------------------
// Purpose: Zero out structure ( level transition/startup )
//-----------------------------------------------------------------------------
void CL_ResetEntityBits( void )
{
memset( s_EntityBits, 0, sizeof( s_EntityBits ) );
}
//-----------------------------------------------------------------------------
// Purpose: Record activity
// Input : entnum -
// bitcount -
//-----------------------------------------------------------------------------
void CL_RecordEntityBits( int entnum, int bitcount )
{
if ( entnum < 0 || entnum >= MAX_EDICTS )
{
return;
}
ENTITYBITS *slot = &s_EntityBits[ entnum ];
slot->bits = bitcount;
// Update average
slot->average = ( BITCOUNT_AVERAGE ) * slot->average + ( 1.f - BITCOUNT_AVERAGE ) * bitcount;
// Recompute peak
if ( realtime >= slot->peaktime )
{
slot->peak = 0.0f;
slot->peaktime = realtime + PEAK_LATCH_TIME;
}
// Store off peak
if ( bitcount > slot->peak )
{
slot->peak = bitcount;
}
}
//-----------------------------------------------------------------------------
// Purpose: Record entity add event
// Input : entnum -
//-----------------------------------------------------------------------------
void CL_RecordAddEntity( int entnum )
{
if ( !cl_entityreport.GetBool() || entnum < 0 || entnum >= MAX_EDICTS )
{
return;
}
ENTITYBITS *slot = &s_EntityBits[ entnum ];
slot->flags = FENTITYBITS_ADD;
slot->effectfinishtime = realtime + EFFECT_TIME;
}
//-----------------------------------------------------------------------------
// Purpose: record entity leave event
// Input : entnum -
//-----------------------------------------------------------------------------
void CL_RecordLeavePVS( int entnum )
{
if ( !cl_entityreport.GetBool() || entnum < 0 || entnum >= MAX_EDICTS )
{
return;
}
ENTITYBITS *slot = &s_EntityBits[ entnum ];
slot->flags = FENTITYBITS_LEAVEPVS;
slot->effectfinishtime = realtime + EFFECT_TIME;
}
//-----------------------------------------------------------------------------
// Purpose: record entity deletion event
// Input : entnum -
// *pclass -
//-----------------------------------------------------------------------------
void CL_RecordDeleteEntity( int entnum, ClientClass *pclass )
{
if ( !cl_entityreport.GetBool() || entnum < 0 || entnum >= MAX_EDICTS )
{
return;
}
ENTITYBITS *slot = &s_EntityBits[ entnum ];
slot->flags = FENTITYBITS_DELETE;
slot->effectfinishtime = realtime + EFFECT_TIME;
slot->deletedclientclass = pclass;
}
//-----------------------------------------------------------------------------
// Purpose: Shows entity status report if cl_entityreport cvar is set
//-----------------------------------------------------------------------------
class CEntityReportPanel : public CBasePanel
{
typedef CBasePanel BaseClass;
public:
// Construction
CEntityReportPanel( vgui::Panel *parent );
virtual ~CEntityReportPanel( void );
// Refresh
virtual void Paint();
virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
virtual bool ShouldDraw( void );
// Helpers
void ApplyEffect( ENTITYBITS *entry, int& r, int& g, int& b );
bool DrawEntry( int row, int col, int rowheight, int colwidth, int entityIdx );
private:
// Font to use for drawing
vgui::HFont m_hFont;
};
static CEntityReportPanel *g_pEntityReportPanel = NULL;
//-----------------------------------------------------------------------------
// Purpose: Creates the CEntityReportPanel VGUI panel
// Input : *parent -
//-----------------------------------------------------------------------------
void CL_CreateEntityReportPanel( vgui::Panel *parent )
{
g_pEntityReportPanel = new CEntityReportPanel( parent );
}
//-----------------------------------------------------------------------------
// Purpose: Instances the entity report panel
// Input : *parent -
//-----------------------------------------------------------------------------
CEntityReportPanel::CEntityReportPanel( vgui::Panel *parent ) :
CBasePanel( parent, "CEntityReportPanel" )
{
// Need parent here, before loading up textures, so getSurfaceBase
// will work on this panel ( it's null otherwise )
SetSize( videomode->GetModeStereoWidth(), videomode->GetModeStereoHeight() );
SetPos( 0, 0 );
SetVisible( true );
SetCursor( null );
m_hFont = vgui::INVALID_FONT;
SetFgColor( Color( 0, 0, 0, 255 ) );
SetPaintBackgroundEnabled( false );
SetPaintBorderEnabled(false);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CEntityReportPanel::~CEntityReportPanel( void )
{
}
void CEntityReportPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
// If you change this font, be sure to mark it with
// $use_in_fillrate_mode in its .vmt file
m_hFont = pScheme->GetFont( "DefaultVerySmall", false );
Assert( m_hFont );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CEntityReportPanel::ShouldDraw( void )
{
if ( !cl_entityreport.GetInt() )
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Helper to flash colors
// Input : cycle -
// value -
// Output : static int
//-----------------------------------------------------------------------------
static int MungeColorValue( float cycle, int& value )
{
int midpoint;
int remaining;
bool invert = false;
if ( value < 128 )
{
invert = true;
value = 255 - value;
}
midpoint = value / 2;
remaining = value - midpoint;
midpoint = midpoint + remaining / 2;
value = midpoint + ( remaining / 2 ) * cycle;
if ( invert )
{
value = 255 - value;
}
value = max( 0, value );
value = min( 255, value );
return value;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : frac -
// r -
// g -
// b -
//-----------------------------------------------------------------------------
void CEntityReportPanel::ApplyEffect( ENTITYBITS *entry, int& r, int& g, int& b )
{
bool effectactive = ( realtime <= entry->effectfinishtime ) ? true : false;
if ( !effectactive )
return;
float frequency = 3.0f;
float frac = ( EFFECT_TIME - ( entry->effectfinishtime - realtime ) ) / EFFECT_TIME;
frac = min( 1.f, frac );
frac = max( 0.f, frac );
frac *= 2.0 * M_PI;
frac = sin( frequency * frac );
if ( entry->flags & FENTITYBITS_LEAVEPVS )
{
r = MungeColorValue( frac, r );
}
else if ( entry->flags & FENTITYBITS_ADD )
{
g = MungeColorValue( frac, g );
}
else if ( entry->flags & FENTITYBITS_DELETE )
{
r = MungeColorValue( frac, r );
g = MungeColorValue( frac, g );
b = MungeColorValue( frac, b );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CEntityReportPanel::DrawEntry( int row, int col, int rowheight, int colwidth, int entityIdx )
{
IClientNetworkable *pNet;
ClientClass *pClientClass;
bool inpvs;
int r, g, b, a;
bool effectactive;
ENTITYBITS *entry;
int top = 5;
int left = 5;
pNet = entitylist->GetClientNetworkable( entityIdx );
entry = &s_EntityBits[ entityIdx ];
effectactive = ( realtime <= entry->effectfinishtime ) ? true : false;
if ( pNet && ((pClientClass = pNet->GetClientClass())) != NULL )
{
inpvs = !pNet->IsDormant();
if ( inpvs )
{
if ( entry->average >= 5 )
{
r = 200; g = 200; b = 250;
a = 255;
}
else
{
r = 200; g = 255; b = 100;
a = 255;
}
}
else
{
r = 255; g = 150; b = 100;
a = 255;
}
ApplyEffect( entry, r, g, b );
char text[256];
wchar_t unicode[ 256 ];
Q_snprintf( text, sizeof(text), "(%i) %s", entityIdx, pClientClass->m_pNetworkName );
g_pVGuiLocalize->ConvertANSIToUnicode( text, unicode, sizeof( unicode ) );
DrawColoredText( m_hFont, left + col * colwidth, top + row * rowheight, r, g, b, a, unicode );
if ( inpvs )
{
float fracs[ 3 ];
fracs[ 0 ] = (float)( entry->bits >> 3 ) / 100.0f;
fracs[ 1 ] = (float)( entry->peak >> 3 ) / 100.0f;
fracs[ 2 ] = (float)( (int)entry->average >> 3 ) / 100.0f;
for ( int j = 0; j < 3; j++ )
{
fracs[ j ] = max( 0.0f, fracs[ j ] );
fracs[ j ] = min( 1.0f, fracs[ j ] );
}
int rcright = left + col * colwidth + colwidth-2;
int wide = colwidth / 3;
int rcleft = rcright - wide;
int rctop = top + row * rowheight;
int rcbottom = rctop + rowheight - 1;
vgui::surface()->DrawSetColor( 63, 63, 63, 127 );
vgui::surface()->DrawFilledRect( rcleft, rctop, rcright, rcbottom );
// draw a box around it
vgui::surface()->DrawSetColor( 200, 200, 200, 127 );
vgui::surface()->DrawOutlinedRect( rcleft, rctop, rcright, rcbottom );
// Draw current as a filled rect
vgui::surface()->DrawSetColor( 200, 255, 100, 192 );
vgui::surface()->DrawFilledRect( rcleft, rctop + rowheight / 2, rcleft + wide * fracs[ 0 ], rcbottom - 1 );
// Draw average a vertical bar
vgui::surface()->DrawSetColor( 192, 192, 192, 255 );
vgui::surface()->DrawFilledRect( rcleft + wide * fracs[ 2 ], rctop + rowheight / 2, rcleft + wide * fracs[ 2 ] + 1, rcbottom - 1 );
// Draw peak as a vertical red tick
vgui::surface()->DrawSetColor( 192, 0, 0, 255 );
vgui::surface()->DrawFilledRect( rcleft + wide * fracs[ 1 ], rctop + 1, rcleft + wide * fracs[ 1 ] + 1, rctop + rowheight / 2 );
}
// drew something...
return true;
}
/*else
{
r = 63; g = 63; b = 63;
a = 220;
ApplyEffect( entry, r, g, b );
wchar_t unicode[ 256 ];
g_pVGuiLocalize->ConvertANSIToUnicode( ( effectactive && entry->deletedclientclass ) ?
entry->deletedclientclass->m_pNetworkName : "unused", unicode, sizeof( unicode ) );
DrawColoredText( m_hFont, left + col * colwidth, top + row * rowheight, r, g, b, a,
L"(%i) %s", i, unicode );
}*/
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CEntityReportPanel::Paint()
{
VPROF( "CEntityReportPanel::Paint" );
if ( !m_hFont )
return;
if ( !cl.IsActive() )
return;
if ( !entitylist )
return;
int top = 5;
int left = 5;
int row = 0;
int col = 0;
int colwidth = 160;
int rowheight = vgui::surface()->GetFontTall( m_hFont );
IClientNetworkable *pNet;
bool effectactive;
ENTITYBITS *entry;
int lastused = entitylist->GetMaxEntities()-1;
while ( lastused > 0 )
{
pNet = entitylist->GetClientNetworkable( lastused );
entry = &s_EntityBits[ lastused ];
effectactive = ( realtime <= entry->effectfinishtime ) ? true : false;
if ( pNet && pNet->GetClientClass() )
{
break;
}
if ( effectactive )
break;
lastused--;
}
int start = 0;
if ( cl_entityreport.GetInt() > 1 )
{
start = cl_entityreport.GetInt();
}
// draw sorted
if ( cl_entityreport_sorted.GetInt() != ENTITYSORT_NONE )
{
// copy and sort
int entityIndices[MAX_EDICTS];
int count = lastused - start + 1;
for ( int i = 0, entityIdx = start; entityIdx <= lastused; ++i, ++entityIdx )
{
entityIndices[i] = entityIdx;
}
qsort( entityIndices, count, sizeof(int), CompareEntityBits );
// now draw
for ( int i = 0; i < count; ++i )
{
int entityIdx = entityIndices[i];
if ( DrawEntry( row, col, rowheight, colwidth, entityIdx ) )
{
row++;
if ( top + row * rowheight > videomode->GetModeStereoHeight() - rowheight )
{
row = 0;
col++;
// No more space anyway, give up
if ( left + ( col + 1 ) * 200 > videomode->GetModeStereoWidth() )
return;
}
}
}
}
// not sorted, old method with items scattered across the screen
else
{
for ( int i = start; i <= lastused; i++ )
{
DrawEntry( row, col, rowheight, colwidth, i );
row++;
if ( top + row * rowheight > videomode->GetModeStereoHeight() - rowheight )
{
row = 0;
col++;
// No more space anyway, give up
if ( left + ( col + 1 ) * 200 > videomode->GetModeStereoWidth() )
return;
}
}
}
}