Counter Strike : Global Offensive Source Code
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.

641 lines
24 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Misc utility code.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef UTIL_H
  8. #define UTIL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "ai_activity.h"
  13. #include "steam/steam_gameserver.h"
  14. #include "enginecallback.h"
  15. #include "basetypes.h"
  16. #include "tempentity.h"
  17. #include "string_t.h"
  18. #include "gamestringpool.h"
  19. #include "engine/IEngineTrace.h"
  20. #include "worldsize.h"
  21. #include "dt_send.h"
  22. #include "server_class.h"
  23. #include "shake.h"
  24. #include "vstdlib/random.h"
  25. #include <string.h>
  26. #include "utlvector.h"
  27. #include "util_shared.h"
  28. #include "shareddefs.h"
  29. #include "networkvar.h"
  30. struct levellist_t;
  31. class IServerNetworkable;
  32. class IEntityFactory;
  33. #ifdef _WIN32
  34. #define SETUP_EXTERNC(mapClassName)\
  35. extern "C" _declspec( dllexport ) IServerNetworkable* mapClassName( void );
  36. #else
  37. #define SETUP_EXTERNC(mapClassName)
  38. #endif
  39. #include "tier0/memdbgon.h"
  40. // entity creation
  41. // creates an entity that has not been linked to a classname
  42. template< class T >
  43. T *_CreateEntityTemplate( T *newEnt, const char *className )
  44. {
  45. MEM_ALLOC_CREDIT_(MEM_ALLOC_CLASSNAME(T));
  46. newEnt = new T; // this is the only place 'new' should be used!
  47. newEnt->PostConstructor( className );
  48. return newEnt;
  49. }
  50. #include "tier0/memdbgoff.h"
  51. enum EUtilSayTextMessageType_t
  52. {
  53. kEUtilSayTextMessageType_Default, // default text
  54. kEUtilSayTextMessageType_TeamonlyChat, // teamonly chat
  55. kEUtilSayTextMessageType_AllChat, // all chat
  56. };
  57. // This is the glue that hooks .MAP entity class names to our CPP classes
  58. abstract_class IEntityFactoryDictionary
  59. {
  60. public:
  61. virtual void InstallFactory( IEntityFactory *pFactory, const char *pClassName ) = 0;
  62. virtual IServerNetworkable *Create( const char *pClassName ) = 0;
  63. virtual void Destroy( const char *pClassName, IServerNetworkable *pNetworkable ) = 0;
  64. virtual IEntityFactory *FindFactory( const char *pClassName ) = 0;
  65. virtual const char *GetCannonicalName( const char *pClassName ) = 0;
  66. };
  67. IEntityFactoryDictionary *EntityFactoryDictionary();
  68. inline bool CanCreateEntityClass( const char *pszClassname )
  69. {
  70. return ( EntityFactoryDictionary() != NULL && EntityFactoryDictionary()->FindFactory( pszClassname ) != NULL );
  71. }
  72. abstract_class IEntityFactory
  73. {
  74. public:
  75. virtual IServerNetworkable *Create( const char *pClassName ) = 0;
  76. virtual void Destroy( IServerNetworkable *pNetworkable ) = 0;
  77. virtual size_t GetEntitySize() = 0;
  78. };
  79. template <class T>
  80. class CEntityFactory : public IEntityFactory
  81. {
  82. public:
  83. CEntityFactory( const char *pClassName )
  84. {
  85. EntityFactoryDictionary()->InstallFactory( this, pClassName );
  86. }
  87. IServerNetworkable *Create( const char *pClassName )
  88. {
  89. T* pEnt = _CreateEntityTemplate((T*)NULL, pClassName);
  90. return pEnt->NetworkProp();
  91. }
  92. void Destroy( IServerNetworkable *pNetworkable )
  93. {
  94. if ( pNetworkable )
  95. {
  96. pNetworkable->Release();
  97. }
  98. }
  99. virtual size_t GetEntitySize()
  100. {
  101. return sizeof(T);
  102. }
  103. };
  104. #define LINK_ENTITY_TO_CLASS(mapClassName,DLLClassName) \
  105. static CEntityFactory<DLLClassName> mapClassName( #mapClassName );
  106. //
  107. // Conversion among the three types of "entity", including identity-conversions.
  108. //
  109. extern CGlobalVars *gpGlobals;
  110. extern bool g_bIsLogging;
  111. inline int ENTINDEX( edict_t *pEdict )
  112. {
  113. if ( !pEdict )
  114. return 0;
  115. int edictIndex = pEdict - gpGlobals->pEdicts;
  116. Assert( edictIndex < MAX_EDICTS && edictIndex >= 0 );
  117. return edictIndex;
  118. }
  119. int ENTINDEX( CBaseEntity *pEnt );
  120. inline edict_t* INDEXENT( int iEdictNum )
  121. {
  122. Assert(iEdictNum>=0 && iEdictNum < MAX_EDICTS);
  123. if ( gpGlobals->pEdicts )
  124. {
  125. edict_t *pEdict = gpGlobals->pEdicts + iEdictNum;
  126. if ( pEdict->IsFree() )
  127. return NULL;
  128. return pEdict;
  129. }
  130. return NULL;
  131. }
  132. // Testing the three types of "entity" for nullity
  133. inline bool FNullEnt(const edict_t* pent)
  134. {
  135. return pent == NULL || ENTINDEX((edict_t*)pent) == 0;
  136. }
  137. // Dot products for view cone checking
  138. #define VIEW_FIELD_FULL (float)-1.0 // +-180 degrees
  139. #define VIEW_FIELD_WIDE (float)-0.7 // +-135 degrees 0.1 // +-85 degrees, used for full FOV checks
  140. #define VIEW_FIELD_NARROW (float)0.7 // +-45 degrees, more narrow check used to set up ranged attacks
  141. #define VIEW_FIELD_ULTRA_NARROW (float)0.9 // +-25 degrees, more narrow check used to set up ranged attacks
  142. class CBaseEntity;
  143. class CBasePlayer;
  144. // Misc useful
  145. inline bool FStrEq(const char *sz1, const char *sz2)
  146. {
  147. return ( sz1 == sz2 || stricmp(sz1, sz2) == 0 );
  148. }
  149. #if 0
  150. // UNDONE: Remove/alter MAKE_STRING so we can do this?
  151. inline bool FStrEq( string_t str1, string_t str2 )
  152. {
  153. // now that these are pooled, we can compare them with
  154. // integer equality
  155. return str1 == str2;
  156. }
  157. #endif
  158. const char *nexttoken(char *token, const char *str, char sep);
  159. // Misc. Prototypes
  160. void UTIL_SetSize (CBaseEntity *pEnt, const Vector &vecMin, const Vector &vecMax);
  161. void UTIL_ClearTrace ( trace_t &trace );
  162. void UTIL_SetTrace (trace_t& tr, const Ray_t &ray, edict_t* edict, float fraction, int hitgroup, unsigned int contents, const Vector& normal, float intercept );
  163. int UTIL_PrecacheDecal ( const char *name, bool preload = false );
  164. //-----------------------------------------------------------------------------
  165. float UTIL_GetSimulationInterval();
  166. //-----------------------------------------------------------------------------
  167. // Purpose: Gets a player pointer by 1-based index
  168. // If player is not yet spawned or connected, returns NULL
  169. // Input : playerIndex - index of the player - first player is index 1
  170. //-----------------------------------------------------------------------------
  171. // NOTENOTE: Use UTIL_GetLocalPlayer instead of UTIL_PlayerByIndex IF you're in single player
  172. // and you want the player.
  173. CBasePlayer *UTIL_PlayerByIndex( int playerIndex );
  174. // NOTENOTE: Use this instead of UTIL_PlayerByIndex IF you're in single player
  175. // and you want the player.
  176. // not useable in multiplayer - see UTIL_GetListenServerHost()
  177. CBasePlayer* UTIL_GetLocalPlayer( void );
  178. // get the local player on a listen server
  179. CBasePlayer *UTIL_GetListenServerHost( void );
  180. // Convenience function so we don't have to make this check all over
  181. inline CBasePlayer * UTIL_GetLocalPlayerOrListenServerHost( void )
  182. {
  183. if ( gpGlobals->maxClients > 1 )
  184. {
  185. if ( engine->IsDedicatedServer() )
  186. {
  187. return NULL;
  188. }
  189. return UTIL_GetListenServerHost();
  190. }
  191. return UTIL_GetLocalPlayer();
  192. }
  193. CBasePlayer* UTIL_PlayerByUserId( int userID );
  194. CBasePlayer* UTIL_PlayerByName( const char *name ); // not case sensitive
  195. CBasePlayer* UTIL_PlayerByAccountID( AccountID_t accountID );
  196. // Returns true if the command was issued by the listenserver host, or by the dedicated server, via rcon or the server console.
  197. // This is valid during ConCommand execution.
  198. bool UTIL_IsCommandIssuedByServerAdmin( void );
  199. CBaseEntity* UTIL_EntityByIndex( int entityIndex );
  200. void UTIL_GetPlayerConnectionInfo( int playerIndex, int& ping, int &packetloss );
  201. void UTIL_SetClientVisibilityPVS( edict_t *pClient, const unsigned char *pvs, int pvssize );
  202. bool UTIL_ClientPVSIsExpanded();
  203. #if defined ( PORTAL )
  204. void UTIL_SetClientCheckPVS( edict_t *pClient, const unsigned char *pvs, int pvssize );
  205. #endif
  206. edict_t *UTIL_FindClientInPVS( edict_t *pEdict );
  207. edict_t *UTIL_FindClientInVisibilityPVS( edict_t *pEdict );
  208. edict_t *UTIL_FindClientInPVSGuts(edict_t *pEdict, unsigned char *pvs, unsigned pvssize );
  209. // This is a version which finds any clients whose PVS intersects the box
  210. CBaseEntity *UTIL_FindClientInPVS( const Vector &vecBoxMins, const Vector &vecBoxMaxs );
  211. CBaseEntity *UTIL_EntitiesInPVS( CBaseEntity *pPVSEntity, CBaseEntity *pStartingEntity );
  212. // Pass in an array of pointers and an array size, it fills the array and returns the number inserted
  213. int UTIL_EntitiesInBox( const Vector &mins, const Vector &maxs, CFlaggedEntitiesEnum *pEnum );
  214. int UTIL_EntitiesInSphere( const Vector &center, float radius, CFlaggedEntitiesEnum *pEnum );
  215. inline int UTIL_EntitiesInBox( CBaseEntity **pList, int listMax, const Vector &mins, const Vector &maxs, int flagMask )
  216. {
  217. CFlaggedEntitiesEnum boxEnum( pList, listMax, flagMask );
  218. return UTIL_EntitiesInBox( mins, maxs, &boxEnum );
  219. }
  220. inline int UTIL_EntitiesInSphere( CBaseEntity **pList, int listMax, const Vector &center, float radius, int flagMask )
  221. {
  222. CFlaggedEntitiesEnum sphereEnum( pList, listMax, flagMask );
  223. return UTIL_EntitiesInSphere( center, radius, &sphereEnum );
  224. }
  225. // marks the entity for deletion so it will get removed next frame
  226. void UTIL_Remove( IServerNetworkable *oldObj );
  227. void UTIL_Remove( CBaseEntity *oldObj );
  228. // deletes an entity, without any delay. Only use this when sure no pointers rely on this entity.
  229. void UTIL_DisableRemoveImmediate();
  230. void UTIL_EnableRemoveImmediate();
  231. void UTIL_RemoveImmediate( CBaseEntity *oldObj );
  232. // make this a fixed size so it just sits on the stack
  233. #define MAX_SPHERE_QUERY 512
  234. class CEntitySphereQuery
  235. {
  236. public:
  237. // currently this builds the list in the constructor
  238. // UNDONE: make an iterative query of ISpatialPartition so we could
  239. // make queries like this optimal
  240. CEntitySphereQuery( const Vector &center, float radius, int flagMask=0 );
  241. CBaseEntity *GetCurrentEntity();
  242. inline void NextEntity() { m_listIndex++; }
  243. private:
  244. int m_listIndex;
  245. int m_listCount;
  246. CBaseEntity *m_pList[MAX_SPHERE_QUERY];
  247. };
  248. enum soundlevel_t;
  249. // Drops an entity onto the floor
  250. int UTIL_DropToFloor( CBaseEntity *pEntity, unsigned int mask, CBaseEntity *pIgnore = NULL );
  251. // Returns false if any part of the bottom of the entity is off an edge that is not a staircase.
  252. bool UTIL_CheckBottom( CBaseEntity *pEntity, ITraceFilter *pTraceFilter, float flStepSize );
  253. void UTIL_SetOrigin ( CBaseEntity *entity, const Vector &vecOrigin, bool bFireTriggers = false );
  254. void UTIL_EmitAmbientSound ( int entindex, const Vector &vecOrigin, const char *samp, float vol, soundlevel_t soundlevel, int fFlags, int pitch, float soundtime = 0.0f, float *duration = NULL );
  255. void UTIL_ParticleEffect ( const Vector &vecOrigin, const Vector &vecDirection, uint32 ulColor, int ulCount );
  256. void UTIL_ScreenShake ( const Vector &center, float amplitude, float frequency, float duration, float radius, ShakeCommand_t eCommand, bool bAirShake = false, CUtlVector<CBasePlayer *> *ignore = NULL );
  257. void UTIL_ScreenShakeObject ( CBaseEntity *pEnt, const Vector &center, float amplitude, float frequency, float duration, float radius, ShakeCommand_t eCommand, bool bAirShake=false );
  258. void UTIL_ScreenTilt ( const Vector &center, const QAngle &tiltAngle, float duration, float radius, float tiltTime, ShakeCommand_t eCommand, bool bEaseInOut );
  259. void UTIL_ViewPunch ( const Vector &center, QAngle angPunch, float radius, bool bInAir );
  260. void UTIL_ShowMessage ( const char *pString, CBasePlayer *pPlayer );
  261. void UTIL_ShowMessageAll ( const char *pString );
  262. void UTIL_ScreenFadeAll ( const color32 &color, float fadeTime, float holdTime, int flags );
  263. void UTIL_ScreenFade ( CBaseEntity *pEntity, const color32 &color, float fadeTime, float fadeHold, int flags );
  264. void UTIL_MuzzleFlash ( const Vector &origin, const QAngle &angles, int scale, int type );
  265. Vector UTIL_PointOnLineNearestPoint(const Vector& vStartPos, const Vector& vEndPos, const Vector& vPoint, bool clampEnds = false );
  266. int UTIL_EntityInSolid( CBaseEntity *ent );
  267. bool UTIL_IsMasterTriggered (string_t sMaster, CBaseEntity *pActivator);
  268. void UTIL_BloodStream( const Vector &origin, const Vector &direction, int color, int amount );
  269. void UTIL_BloodSpray( const Vector &pos, const Vector &dir, int color, int amount, int flags );
  270. void UTIL_BloodSprayPrecache();
  271. Vector UTIL_RandomBloodVector( void );
  272. void UTIL_ImpactTrace( trace_t *pTrace, int iDamageType, char *pCustomImpactName = NULL );
  273. void UTIL_PlayerDecalTrace( trace_t *pTrace, Vector const &right, int playernum );
  274. void UTIL_Smoke( const Vector &origin, const float scale, const float framerate );
  275. void UTIL_AxisStringToPointDir( Vector &start, Vector &dir, const char *pString );
  276. void UTIL_AxisStringToPointPoint( Vector &start, Vector &end, const char *pString );
  277. void UTIL_AxisStringToUnitDir( Vector &dir, const char *pString );
  278. void UTIL_ClipPunchAngleOffset( QAngle &in, const QAngle &punch, const QAngle &clip );
  279. void UTIL_PredictedPosition( CBaseEntity *pTarget, float flTimeDelta, Vector *vecPredictedPosition );
  280. void UTIL_Beam( Vector &Start, Vector &End, int nModelIndex, int nHaloIndex, unsigned char FrameStart, unsigned char FrameRate,
  281. float Life, unsigned char Width, unsigned char EndWidth, unsigned char FadeLength, unsigned char Noise, unsigned char Red, unsigned char Green,
  282. unsigned char Blue, unsigned char Brightness, unsigned char Speed);
  283. const char *UTIL_VarArgs( PRINTF_FORMAT_STRING const char *format, ... ) FMTFUNCTION( 1, 2 );
  284. bool UTIL_IsValidEntity( CBaseEntity *pEnt );
  285. bool UTIL_TeamsMatch( const char *pTeamName1, const char *pTeamName2 );
  286. // snaps a vector to the nearest axis vector (if within epsilon)
  287. void UTIL_SnapDirectionToAxis( Vector &direction, float epsilon = 0.002f );
  288. //Set the entity to point at the target specified
  289. bool UTIL_PointAtEntity( CBaseEntity *pEnt, CBaseEntity *pTarget );
  290. void UTIL_PointAtNamedEntity( CBaseEntity *pEnt, string_t strTarget );
  291. // Copy the pose parameter values from one entity to the other
  292. bool UTIL_TransferPoseParameters( CBaseEntity *pSourceEntity, CBaseEntity *pDestEntity );
  293. void UTIL_Bubbles( const Vector& mins, const Vector& maxs, int count );
  294. void UTIL_BubbleTrail( const Vector& from, const Vector& to, int count );
  295. // allows precacheing of other entities
  296. void UTIL_PrecacheOther( const char *szClassname, const char *modelName = NULL );
  297. // Creates the netoworking baseline for this entity's serverclass if it doesn't yet exist in the engine's list.
  298. void UTIL_EnsureInstanceBaseline( const char *szClassname );
  299. // prints a message to each client
  300. void UTIL_ClientPrintAll( int msg_dest, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL );
  301. inline void UTIL_CenterPrintAll( const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL )
  302. {
  303. UTIL_ClientPrintAll( HUD_PRINTCENTER, msg_name, param1, param2, param3, param4 );
  304. }
  305. void UTIL_ValidateSoundName( string_t &name, const char *defaultStr );
  306. void UTIL_ClientPrintFilter( IRecipientFilter& filter, int msg_dest, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL );
  307. // prints messages through the HUD
  308. void ClientPrint( CBasePlayer *player, int msg_dest, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL );
  309. // prints a message to the HUD say (chat)
  310. void UTIL_SayText( const char *pText, CBasePlayer *pEntity );
  311. void UTIL_SayTextAll( const char *pText, CBasePlayer *pEntity = NULL, EUtilSayTextMessageType_t eMessageType = kEUtilSayTextMessageType_Default );
  312. void UTIL_SayTextFilter( IRecipientFilter& filter, const char *pText, CBasePlayer *pEntity, EUtilSayTextMessageType_t eMessageType );
  313. void UTIL_SayText2Filter( IRecipientFilter& filter, CBasePlayer *pEntity, EUtilSayTextMessageType_t eMessageType, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL );
  314. byte *UTIL_LoadFileForMe( const char *filename, int *pLength );
  315. void UTIL_FreeFile( byte *buffer );
  316. class CGameTrace;
  317. typedef CGameTrace trace_t;
  318. //-----------------------------------------------------------------------------
  319. // These are inlined for backwards compatibility
  320. //-----------------------------------------------------------------------------
  321. inline float UTIL_Approach( float target, float value, float speed )
  322. {
  323. return Approach( target, value, speed );
  324. }
  325. inline float UTIL_ApproachAngle( float target, float value, float speed )
  326. {
  327. return ApproachAngle( target, value, speed );
  328. }
  329. inline float UTIL_AngleDistance( float next, float cur )
  330. {
  331. return AngleDistance( next, cur );
  332. }
  333. inline float UTIL_AngleMod(float a)
  334. {
  335. return anglemod(a);
  336. }
  337. inline float UTIL_AngleDiff( float destAngle, float srcAngle )
  338. {
  339. return AngleDiff( destAngle, srcAngle );
  340. }
  341. typedef struct hudtextparms_s
  342. {
  343. float x;
  344. float y;
  345. int effect;
  346. byte r1, g1, b1, a1;
  347. byte r2, g2, b2, a2;
  348. float fadeinTime;
  349. float fadeoutTime;
  350. float holdTime;
  351. float fxTime;
  352. int channel;
  353. } hudtextparms_t;
  354. //-----------------------------------------------------------------------------
  355. // Sets the model to be associated with an entity
  356. //-----------------------------------------------------------------------------
  357. void UTIL_SetModel( CBaseEntity *pEntity, const char *pModelName );
  358. // prints as transparent 'title' to the HUD
  359. void UTIL_HudMessageAll( const hudtextparms_t &textparms, const char *pMessage );
  360. void UTIL_HudMessage( CBasePlayer *pToPlayer, const hudtextparms_t &textparms, const char *pMessage );
  361. // brings up hud keyboard hints display
  362. void UTIL_HudHintText( CBaseEntity *pEntity, const char *pMessage );
  363. // Writes message to console with timestamp and FragLog header.
  364. void UTIL_LogPrintf( PRINTF_FORMAT_STRING const char *fmt, ... ) FMTFUNCTION( 1, 2 );
  365. // Sorta like FInViewCone, but for nonNPCs.
  366. float UTIL_DotPoints ( const Vector &vecSrc, const Vector &vecCheck, const Vector &vecDir );
  367. void UTIL_StripToken( const char *pKey, char *pDest );// for redundant keynames
  368. // Misc functions
  369. int BuildChangeList( levellist_t *pLevelList, int maxList );
  370. // computes gravity scale for an absolute gravity. Pass the result into CBaseEntity::SetGravity()
  371. float UTIL_ScaleForGravity( float desiredGravity );
  372. //
  373. // How did I ever live without ASSERT?
  374. //
  375. #ifdef DEBUG
  376. void DBG_AssertFunction(bool fExpr, const char* szExpr, const char* szFile, int szLine, const char* szMessage);
  377. #define ASSERT(f) DBG_AssertFunction((bool)((f)!=0), #f, __FILE__, __LINE__, NULL)
  378. #define ASSERTSZ(f, sz) DBG_AssertFunction((bool)((f)!=0), #f, __FILE__, __LINE__, sz)
  379. #else // !DEBUG
  380. #define ASSERT(f)
  381. #define ASSERTSZ(f, sz)
  382. #endif // !DEBUG
  383. //
  384. // Constants that were used only by QC (maybe not used at all now)
  385. //
  386. // Un-comment only as needed
  387. //
  388. #include "globals.h"
  389. #define LFO_SQUARE 1
  390. #define LFO_TRIANGLE 2
  391. #define LFO_RANDOM 3
  392. // func_rotating
  393. #define SF_BRUSH_ROTATE_Y_AXIS 0
  394. #define SF_BRUSH_ROTATE_START_ON 1
  395. #define SF_BRUSH_ROTATE_BACKWARDS 2
  396. #define SF_BRUSH_ROTATE_Z_AXIS 4
  397. #define SF_BRUSH_ROTATE_X_AXIS 8
  398. #define SF_BRUSH_ROTATE_SMALLRADIUS 128
  399. #define SF_BRUSH_ROTATE_MEDIUMRADIUS 256
  400. #define SF_BRUSH_ROTATE_LARGERADIUS 512
  401. #define SF_BRUSH_ROTATE_CLIENTSIDE 1024
  402. #define PUSH_BLOCK_ONLY_X 1
  403. #define PUSH_BLOCK_ONLY_Y 2
  404. #define SF_LIGHT_START_OFF 1
  405. #define SPAWNFLAG_NOMESSAGE 1
  406. #define SPAWNFLAG_NOTOUCH 1
  407. #define SPAWNFLAG_DROIDONLY 4
  408. #define SPAWNFLAG_USEONLY 1 // can't be touched, must be used (buttons)
  409. #define TELE_PLAYER_ONLY 1
  410. #define TELE_SILENT 2
  411. // Sound Utilities
  412. enum soundlevel_t;
  413. void SENTENCEG_Init();
  414. void SENTENCEG_Stop(edict_t *entity, int isentenceg, int ipick);
  415. int SENTENCEG_PlayRndI(edict_t *entity, int isentenceg, float volume, soundlevel_t soundlevel, int flags, int pitch);
  416. int SENTENCEG_PlayRndSz(edict_t *entity, const char *szrootname, float volume, soundlevel_t soundlevel, int flags, int pitch);
  417. int SENTENCEG_PlaySequentialSz(edict_t *entity, const char *szrootname, float volume, soundlevel_t soundlevel, int flags, int pitch, int ipick, int freset);
  418. void SENTENCEG_PlaySentenceIndex( edict_t *entity, int iSentenceIndex, float volume, soundlevel_t soundlevel, int flags, int pitch );
  419. int SENTENCEG_PickRndSz(const char *szrootname);
  420. int SENTENCEG_GetIndex(const char *szrootname);
  421. int SENTENCEG_Lookup(const char *sample);
  422. char TEXTURETYPE_Find( trace_t *ptr );
  423. void UTIL_EmitSoundSuit(edict_t *entity, const char *sample);
  424. int UTIL_EmitGroupIDSuit(edict_t *entity, int isentenceg);
  425. int UTIL_EmitGroupnameSuit(edict_t *entity, const char *groupname);
  426. void UTIL_RestartAmbientSounds( void );
  427. class EntityMatrix : public VMatrix
  428. {
  429. public:
  430. void InitFromEntity( CBaseEntity *pEntity, int iAttachment=0 );
  431. void InitFromEntityLocal( CBaseEntity *entity );
  432. inline Vector LocalToWorld( const Vector &vVec ) const
  433. {
  434. return VMul4x3( vVec );
  435. }
  436. inline Vector WorldToLocal( const Vector &vVec ) const
  437. {
  438. return VMul4x3Transpose( vVec );
  439. }
  440. inline Vector LocalToWorldRotation( const Vector &vVec ) const
  441. {
  442. return VMul3x3( vVec );
  443. }
  444. inline Vector WorldToLocalRotation( const Vector &vVec ) const
  445. {
  446. return VMul3x3Transpose( vVec );
  447. }
  448. };
  449. inline float UTIL_DistApprox( const Vector &vec1, const Vector &vec2 );
  450. inline float UTIL_DistApprox2D( const Vector &vec1, const Vector &vec2 );
  451. //---------------------------------------------------------
  452. //---------------------------------------------------------
  453. inline float UTIL_DistApprox( const Vector &vec1, const Vector &vec2 )
  454. {
  455. float dx;
  456. float dy;
  457. float dz;
  458. dx = vec1.x - vec2.x;
  459. dy = vec1.y - vec2.y;
  460. dz = vec1.z - vec2.z;
  461. return fabs(dx) + fabs(dy) + fabs(dz);
  462. }
  463. //---------------------------------------------------------
  464. //---------------------------------------------------------
  465. inline float UTIL_DistApprox2D( const Vector &vec1, const Vector &vec2 )
  466. {
  467. float dx;
  468. float dy;
  469. dx = vec1.x - vec2.x;
  470. dy = vec1.y - vec2.y;
  471. return fabs(dx) + fabs(dy);
  472. }
  473. // Find out if an entity is facing another entity or position within a given tolerance range
  474. bool UTIL_IsFacingWithinTolerance( CBaseEntity *pViewer, const Vector &vecPosition, float flDotTolerance, float *pflDot = NULL );
  475. bool UTIL_IsFacingWithinTolerance( CBaseEntity *pViewer, CBaseEntity *pTarget, float flDotTolerance, float *pflDot = NULL );
  476. void UTIL_GetDebugColorForRelationship( int nRelationship, int &r, int &g, int &b );
  477. struct datamap_t;
  478. extern const char *UTIL_FunctionToName( datamap_t *pMap, inputfunc_t pFunction );
  479. extern void UTIL_FunctionFromName( datamap_t *pMap, const char *pName, inputfunc_t *ppFunction );
  480. int UTIL_GetCommandClientIndex( void );
  481. CBasePlayer *UTIL_GetCommandClient( void );
  482. AngularImpulse WorldToLocalRotation( const VMatrix &localToWorld, const Vector &worldAxis, float rotation );
  483. void UTIL_WorldToParentSpace( CBaseEntity *pEntity, Vector &vecPosition, QAngle &vecAngles );
  484. void UTIL_WorldToParentSpace( CBaseEntity *pEntity, Vector &vecPosition, Quaternion &quat );
  485. void UTIL_ParentToWorldSpace( CBaseEntity *pEntity, Vector &vecPosition, QAngle &vecAngles );
  486. void UTIL_ParentToWorldSpace( CBaseEntity *pEntity, Vector &vecPosition, Quaternion &quat );
  487. bool UTIL_LoadAndSpawnEntitiesFromScript( CUtlVector <CBaseEntity*> &entities, const char *pScriptFile, const char *pBlock, bool bActivate = true );
  488. // HudMessagePanel helpers
  489. void UTIL_MessageTextAll( const char *text, Color color = Color( 0, 0, 0, 0 ) ); // Send a HudMessagePanel string to clients
  490. void UTIL_MessageText( CBasePlayer *player, const char *text, Color color = Color( 0, 0, 0, 0 ) ); // Send a HudMessagePanel string to a client
  491. void UTIL_ResetMessageTextAll( void ); // Reset clients' HudMessagePanel
  492. void UTIL_ResetMessageText( CBasePlayer *player ); // Reset a client's HudMessagePanel
  493. void UTIL_SendClientCommandKVToPlayer( KeyValues *pKV, CBasePlayer *pPlayer = NULL );
  494. void UTIL_RecordAchievementEvent( const char *pszAchievementname, CBasePlayer *pPlayer = NULL );
  495. //--------------------------------------------------------------------------------------------------------
  496. /**
  497. * Return true if ground is fairly level within the given radius around an entity
  498. * Trace 4 vertical hull-quadrants and test their collisions and ground heights and normals
  499. */
  500. bool UTIL_IsGroundLevel( float radius, const Vector &position, float hullHeight, int mask, const CBaseEntity *ignore, bool debugTraces = false );
  501. // Given a vector, clamps the scalar axes to MAX_COORD_FLOAT ranges from worldsize.h
  502. void UTIL_BoundToWorldSize( Vector *pVecPos );
  503. #endif // UTIL_H