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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Shared util code between client and server.
//
//=============================================================================//
#ifndef UTIL_SHARED_H
#define UTIL_SHARED_H
#ifdef _WIN32
#pragma once
#endif
#include "mathlib/vector.h"
#include "cmodel.h"
#include "utlvector.h"
#include "networkvar.h"
#include "engine/IEngineTrace.h"
#include "engine/IStaticPropMgr.h"
#include "shared_classnames.h"
#ifdef CLIENT_DLL
#include "cdll_client_int.h"
#endif
#ifdef PORTAL
#include "portal_util_shared.h"
#endif
//-----------------------------------------------------------------------------
// Forward declarations
//-----------------------------------------------------------------------------
class CGameTrace;
class CBasePlayer;
typedef CGameTrace trace_t;
extern ConVar developer; // developer mode
//-----------------------------------------------------------------------------
// Language IDs.
//-----------------------------------------------------------------------------
#define LANGUAGE_ENGLISH 0
#define LANGUAGE_GERMAN 1
#define LANGUAGE_FRENCH 2
#define LANGUAGE_BRITISH 3
//-----------------------------------------------------------------------------
// Pitch + yaw
//-----------------------------------------------------------------------------
float UTIL_VecToYaw (const Vector &vec);
float UTIL_VecToPitch (const Vector &vec);
float UTIL_VecToYaw (const matrix3x4_t& matrix, const Vector &vec);
float UTIL_VecToPitch (const matrix3x4_t& matrix, const Vector &vec);
Vector UTIL_YawToVector ( float yaw );
//-----------------------------------------------------------------------------
// Shared random number generators for shared/predicted code:
// whenever generating random numbers in shared/predicted code, these functions
// have to be used. Each call should specify a unique "sharedname" string that
// seeds the random number generator. In loops make sure the "additionalSeed"
// is increased with the loop counter, otherwise it will always return the
// same random number
//-----------------------------------------------------------------------------
float SharedRandomFloat( const char *sharedname, float flMinVal, float flMaxVal, int additionalSeed = 0 );
int SharedRandomInt( const char *sharedname, int iMinVal, int iMaxVal, int additionalSeed = 0 );
Vector SharedRandomVector( const char *sharedname, float minVal, float maxVal, int additionalSeed = 0 );
QAngle SharedRandomAngle( const char *sharedname, float minVal, float maxVal, int additionalSeed = 0 );
//-----------------------------------------------------------------------------
// Standard collision filters...
//-----------------------------------------------------------------------------
bool PassServerEntityFilter( const IHandleEntity *pTouch, const IHandleEntity *pPass );
bool StandardFilterRules( IHandleEntity *pHandleEntity, int fContentsMask );
//-----------------------------------------------------------------------------
// Converts an IHandleEntity to an CBaseEntity
//-----------------------------------------------------------------------------
inline const CBaseEntity *EntityFromEntityHandle( const IHandleEntity *pConstHandleEntity )
{
IHandleEntity *pHandleEntity = const_cast<IHandleEntity*>(pConstHandleEntity);
#ifdef CLIENT_DLL
IClientUnknown *pUnk = (IClientUnknown*)pHandleEntity;
return pUnk->GetBaseEntity();
#else
if ( staticpropmgr->IsStaticProp( pHandleEntity ) )
return NULL;
IServerUnknown *pUnk = (IServerUnknown*)pHandleEntity;
return pUnk->GetBaseEntity();
#endif
}
inline CBaseEntity *EntityFromEntityHandle( IHandleEntity *pHandleEntity )
{
#ifdef CLIENT_DLL
IClientUnknown *pUnk = (IClientUnknown*)pHandleEntity;
return pUnk->GetBaseEntity();
#else
if ( staticpropmgr->IsStaticProp( pHandleEntity ) )
return NULL;
IServerUnknown *pUnk = (IServerUnknown*)pHandleEntity;
return pUnk->GetBaseEntity();
#endif
}
typedef bool (*ShouldHitFunc_t)( IHandleEntity *pHandleEntity, int contentsMask );
//-----------------------------------------------------------------------------
// traceline methods
//-----------------------------------------------------------------------------
class CTraceFilterSimple : public CTraceFilter
{
public:
// It does have a base, but we'll never network anything below here..
DECLARE_CLASS_NOBASE( CTraceFilterSimple );
CTraceFilterSimple( const IHandleEntity *passentity, int collisionGroup, ShouldHitFunc_t pExtraShouldHitCheckFn = NULL );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
virtual void SetPassEntity( const IHandleEntity *pPassEntity ) { m_pPassEnt = pPassEntity; }
virtual void SetCollisionGroup( int iCollisionGroup ) { m_collisionGroup = iCollisionGroup; }
const IHandleEntity *GetPassEntity( void ){ return m_pPassEnt;}
private:
const IHandleEntity *m_pPassEnt;
int m_collisionGroup;
ShouldHitFunc_t m_pExtraShouldHitCheckFunction;
};
class CTraceFilterSkipTwoEntities : public CTraceFilterSimple
{
public:
// It does have a base, but we'll never network anything below here..
DECLARE_CLASS( CTraceFilterSkipTwoEntities, CTraceFilterSimple );
CTraceFilterSkipTwoEntities( const IHandleEntity *passentity, const IHandleEntity *passentity2, int collisionGroup );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
virtual void SetPassEntity2( const IHandleEntity *pPassEntity2 ) { m_pPassEnt2 = pPassEntity2; }
private:
const IHandleEntity *m_pPassEnt2;
};
class CTraceFilterSimpleList : public CTraceFilterSimple
{
public:
CTraceFilterSimpleList( int collisionGroup );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
void AddEntityToIgnore( IHandleEntity *pEntity );
protected:
CUtlVector<IHandleEntity*> m_PassEntities;
};
class CTraceFilterOnlyNPCsAndPlayer : public CTraceFilterSimple
{
public:
CTraceFilterOnlyNPCsAndPlayer( const IHandleEntity *passentity, int collisionGroup )
: CTraceFilterSimple( passentity, collisionGroup )
{
}
virtual TraceType_t GetTraceType() const
{
return TRACE_ENTITIES_ONLY;
}
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
};
class CTraceFilterNoNPCsOrPlayer : public CTraceFilterSimple
{
public:
CTraceFilterNoNPCsOrPlayer( const IHandleEntity *passentity, int collisionGroup )
: CTraceFilterSimple( passentity, collisionGroup )
{
}
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
};
//-----------------------------------------------------------------------------
// Purpose: Custom trace filter used for NPC LOS traces
//-----------------------------------------------------------------------------
class CTraceFilterLOS : public CTraceFilterSkipTwoEntities
{
public:
CTraceFilterLOS( IHandleEntity *pHandleEntity, int collisionGroup, IHandleEntity *pHandleEntity2 = NULL );
bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
};
class CTraceFilterSkipClassname : public CTraceFilterSimple
{
public:
CTraceFilterSkipClassname( const IHandleEntity *passentity, const char *pchClassname, int collisionGroup );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
private:
const char *m_pchClassname;
};
class CTraceFilterSkipTwoClassnames : public CTraceFilterSkipClassname
{
public:
// It does have a base, but we'll never network anything below here..
DECLARE_CLASS( CTraceFilterSkipTwoClassnames, CTraceFilterSkipClassname );
CTraceFilterSkipTwoClassnames( const IHandleEntity *passentity, const char *pchClassname, const char *pchClassname2, int collisionGroup );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
private:
const char *m_pchClassname2;
};
class CTraceFilterSimpleClassnameList : public CTraceFilterSimple
{
public:
CTraceFilterSimpleClassnameList( const IHandleEntity *passentity, int collisionGroup );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
void AddClassnameToIgnore( const char *pchClassname );
private:
CUtlVector<const char*> m_PassClassnames;
};
class CTraceFilterChain : public CTraceFilter
{
public:
CTraceFilterChain( ITraceFilter *pTraceFilter1, ITraceFilter *pTraceFilter2 );
virtual bool ShouldHitEntity( IHandleEntity *pHandleEntity, int contentsMask );
private:
ITraceFilter *m_pTraceFilter1;
ITraceFilter *m_pTraceFilter2;
};
// helper
void DebugDrawLine( const Vector& vecAbsStart, const Vector& vecAbsEnd, int r, int g, int b, bool test, float duration );
extern ConVar r_visualizetraces;
inline void UTIL_TraceLine( const Vector& vecAbsStart, const Vector& vecAbsEnd, unsigned int mask,
const IHandleEntity *ignore, int collisionGroup, trace_t *ptr )
{
Ray_t ray;
ray.Init( vecAbsStart, vecAbsEnd );
CTraceFilterSimple traceFilter( ignore, collisionGroup );
enginetrace->TraceRay( ray, mask, &traceFilter, ptr );
if( r_visualizetraces.GetBool() )
{
DebugDrawLine( ptr->startpos, ptr->endpos, 255, 0, 0, true, -1.0f );
}
}
inline void UTIL_TraceLine( const Vector& vecAbsStart, const Vector& vecAbsEnd, unsigned int mask,
ITraceFilter *pFilter, trace_t *ptr )
{
Ray_t ray;
ray.Init( vecAbsStart, vecAbsEnd );
enginetrace->TraceRay( ray, mask, pFilter, ptr );
if( r_visualizetraces.GetBool() )
{
DebugDrawLine( ptr->startpos, ptr->endpos, 255, 0, 0, true, -1.0f );
}
}
inline void UTIL_TraceHull( const Vector &vecAbsStart, const Vector &vecAbsEnd, const Vector &hullMin,
const Vector &hullMax, unsigned int mask, const IHandleEntity *ignore,
int collisionGroup, trace_t *ptr )
{
Ray_t ray;
ray.Init( vecAbsStart, vecAbsEnd, hullMin, hullMax );
CTraceFilterSimple traceFilter( ignore, collisionGroup );
enginetrace->TraceRay( ray, mask, &traceFilter, ptr );
if( r_visualizetraces.GetBool() )
{
DebugDrawLine( ptr->startpos, ptr->endpos, 255, 255, 0, true, -1.0f );
}
}
inline void UTIL_TraceHull( const Vector &vecAbsStart, const Vector &vecAbsEnd, const Vector &hullMin,
const Vector &hullMax, unsigned int mask, ITraceFilter *pFilter, trace_t *ptr )
{
Ray_t ray;
ray.Init( vecAbsStart, vecAbsEnd, hullMin, hullMax );
enginetrace->TraceRay( ray, mask, pFilter, ptr );
if( r_visualizetraces.GetBool() )
{
DebugDrawLine( ptr->startpos, ptr->endpos, 255, 255, 0, true, -1.0f );
}
}
inline void UTIL_TraceRay( const Ray_t &ray, unsigned int mask,
const IHandleEntity *ignore, int collisionGroup, trace_t *ptr, ShouldHitFunc_t pExtraShouldHitCheckFn = NULL )
{
CTraceFilterSimple traceFilter( ignore, collisionGroup, pExtraShouldHitCheckFn );
enginetrace->TraceRay( ray, mask, &traceFilter, ptr );
if( r_visualizetraces.GetBool() )
{
DebugDrawLine( ptr->startpos, ptr->endpos, 255, 0, 0, true, -1.0f );
}
}
// Sweeps a particular entity through the world
void UTIL_TraceEntity( CBaseEntity *pEntity, const Vector &vecAbsStart, const Vector &vecAbsEnd, unsigned int mask, trace_t *ptr );
void UTIL_TraceEntity( CBaseEntity *pEntity, const Vector &vecAbsStart, const Vector &vecAbsEnd,
unsigned int mask, ITraceFilter *pFilter, trace_t *ptr );
void UTIL_TraceEntity( CBaseEntity *pEntity, const Vector &vecAbsStart, const Vector &vecAbsEnd,
unsigned int mask, const IHandleEntity *ignore, int collisionGroup, trace_t *ptr );
bool UTIL_EntityHasMatchingRootParent( CBaseEntity *pRootParent, CBaseEntity *pEntity );
inline int UTIL_PointContents( const Vector &vec )
{
return enginetrace->GetPointContents( vec );
}
// Sweeps against a particular model, using collision rules
void UTIL_TraceModel( const Vector &vecStart, const Vector &vecEnd, const Vector &hullMin,
const Vector &hullMax, CBaseEntity *pentModel, int collisionGroup, trace_t *ptr );
void UTIL_ClipTraceToPlayers( const Vector& vecAbsStart, const Vector& vecAbsEnd, unsigned int mask, ITraceFilter *filter, trace_t *tr );
// Particle effect tracer
void UTIL_ParticleTracer( const char *pszTracerEffectName, const Vector &vecStart, const Vector &vecEnd, int iEntIndex = 0, int iAttachment = 0, bool bWhiz = false );
// Old style, non-particle system, tracers
void UTIL_Tracer( const Vector &vecStart, const Vector &vecEnd, int iEntIndex = 0, int iAttachment = TRACER_DONT_USE_ATTACHMENT, float flVelocity = 0, bool bWhiz = false, const char *pCustomTracerName = NULL, int iParticleID = 0 );
bool UTIL_IsLowViolence( void );
bool UTIL_ShouldShowBlood( int bloodColor );
void UTIL_BloodDrips( const Vector &origin, const Vector &direction, int color, int amount );
void UTIL_BloodImpact( const Vector &pos, const Vector &dir, int color, int amount );
void UTIL_BloodDecalTrace( trace_t *pTrace, int bloodColor );
void UTIL_DecalTrace( trace_t *pTrace, char const *decalName );
bool UTIL_IsSpaceEmpty( CBaseEntity *pMainEnt, const Vector &vMin, const Vector &vMax );
void UTIL_StringToVector( float *pVector, const char *pString );
void UTIL_StringToIntArray( int *pVector, int count, const char *pString );
void UTIL_StringToFloatArray( float *pVector, int count, const char *pString );
void UTIL_StringToColor32( color32 *color, const char *pString );
CBasePlayer *UTIL_PlayerByIndex( int entindex );
//=============================================================================
// HPE_BEGIN:
// [menglish] Added UTIL function for events in client win_panel which transmit the player as a user ID
//=============================================================================
CBasePlayer *UTIL_PlayerByUserId( int userID );
//=============================================================================
// HPE_END
//=============================================================================
// decodes a buffer using a 64bit ICE key (inplace)
void UTIL_DecodeICE( unsigned char * buffer, int size, const unsigned char *key);
//--------------------------------------------------------------------------------------------------------------
/**
* Given a position and a ray, return the shortest distance between the two.
* If 'pos' is beyond either end of the ray, the returned distance is negated.
*/
inline float DistanceToRay( const Vector &pos, const Vector &rayStart, const Vector &rayEnd, float *along = NULL, Vector *pointOnRay = NULL )
{
Vector to = pos - rayStart;
Vector dir = rayEnd - rayStart;
float length = dir.NormalizeInPlace();
float rangeAlong = DotProduct( dir, to );
if (along)
{
*along = rangeAlong;
}
float range;
if (rangeAlong < 0.0f)
{
// off start point
range = -(pos - rayStart).Length();
if (pointOnRay)
{
*pointOnRay = rayStart;
}
}
else if (rangeAlong > length)
{
// off end point
range = -(pos - rayEnd).Length();
if (pointOnRay)
{
*pointOnRay = rayEnd;
}
}
else // within ray bounds
{
Vector onRay = rayStart + rangeAlong * dir;
range = (pos - onRay).Length();
if (pointOnRay)
{
*pointOnRay = onRay;
}
}
return range;
}
//--------------------------------------------------------------------------------------------------------------
/**
* Macro for creating an interface that when inherited from automatically maintains a list of instances
* that inherit from that interface.
*/
// interface for entities that want to a auto maintained global list
#define DECLARE_AUTO_LIST( interfaceName ) \
class interfaceName; \
abstract_class interfaceName \
{ \
public: \
interfaceName( bool bAutoAdd = true ); \
virtual ~interfaceName(); \
static void Add( interfaceName *pElement ) { m_##interfaceName##AutoList.AddToTail( pElement ); } \
static void Remove( interfaceName *pElement ) { m_##interfaceName##AutoList.FindAndFastRemove( pElement ); } \
static const CUtlVector< interfaceName* >& AutoList( void ) { return m_##interfaceName##AutoList; } \
private: \
static CUtlVector< interfaceName* > m_##interfaceName##AutoList; \
};
// Creates the auto add/remove constructor/destructor...
// Pass false to the constructor to not auto add
#define IMPLEMENT_AUTO_LIST( interfaceName ) \
CUtlVector< class interfaceName* > interfaceName::m_##interfaceName##AutoList; \
interfaceName::interfaceName( bool bAutoAdd ) \
{ \
if ( bAutoAdd ) \
{ \
Add( this ); \
} \
} \
interfaceName::~interfaceName() \
{ \
Remove( this ); \
}
//--------------------------------------------------------------------------------------------------------------
/**
* Simple class for tracking intervals of game time.
* Upon creation, the timer is invalidated. To measure time intervals, start the timer via Start().
*/
class IntervalTimer
{
public:
IntervalTimer( void )
{
m_timestamp = -1.0f;
}
void Reset( void )
{
m_timestamp = Now();
}
void Start( void )
{
m_timestamp = Now();
}
void Invalidate( void )
{
m_timestamp = -1.0f;
}
bool HasStarted( void ) const
{
return (m_timestamp > 0.0f);
}
/// if not started, elapsed time is very large
float GetElapsedTime( void ) const
{
return (HasStarted()) ? (Now() - m_timestamp) : 99999.9f;
}
bool IsLessThen( float duration ) const
{
return (Now() - m_timestamp < duration) ? true : false;
}
bool IsGreaterThen( float duration ) const
{
return (Now() - m_timestamp > duration) ? true : false;
}
private:
float m_timestamp;
float Now( void ) const; // work-around since client header doesn't like inlined gpGlobals->curtime
};
//--------------------------------------------------------------------------------------------------------------
/**
* Simple class for counting down a short interval of time.
* Upon creation, the timer is invalidated. Invalidated countdown timers are considered to have elapsed.
*/
class CountdownTimer
{
public:
CountdownTimer( void )
{
m_timestamp = -1.0f;
m_duration = 0.0f;
}
void Reset( void )
{
m_timestamp = Now() + m_duration;
}
void Start( float duration )
{
m_timestamp = Now() + duration;
m_duration = duration;
}
void Invalidate( void )
{
m_timestamp = -1.0f;
}
bool HasStarted( void ) const
{
return (m_timestamp > 0.0f);
}
bool IsElapsed( void ) const
{
return (Now() > m_timestamp);
}
float GetElapsedTime( void ) const
{
return Now() - m_timestamp + m_duration;
}
float GetRemainingTime( void ) const
{
return (m_timestamp - Now());
}
/// return original countdown time
float GetCountdownDuration( void ) const
{
return (m_timestamp > 0.0f) ? m_duration : 0.0f;
}
private:
float m_duration;
float m_timestamp;
float Now( void ) const; // work-around since client header doesn't like inlined gpGlobals->curtime
};
char* ReadAndAllocStringValue( KeyValues *pSub, const char *pName, const char *pFilename = NULL );
int UTIL_StringFieldToInt( const char *szValue, const char **pValueStrings, int iNumStrings );
//-----------------------------------------------------------------------------
// Holidays
//-----------------------------------------------------------------------------
// Used at level change and round start to re-calculate which holiday is active
void UTIL_CalculateHolidays();
bool UTIL_IsHolidayActive( /*EHoliday*/ int eHoliday );
/*EHoliday*/ int UTIL_GetHolidayForString( const char* pszHolidayName );
// This will return the first active holiday string it can find. In the case of multiple
// holidays overlapping, the list order will act as priority.
const char *UTIL_GetActiveHolidayString();
#endif // UTIL_SHARED_H
|