Team Fortress 2 Source Code as on 22/4/2020
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.

181 lines
7.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #if !defined( UTIL_H )
  10. #define UTIL_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include <soundflags.h>
  15. #include "mathlib/vector.h"
  16. #include <shareddefs.h>
  17. #include "shake.h"
  18. #include "bitmap/imageformat.h"
  19. #include "ispatialpartition.h"
  20. #include "materialsystem/MaterialSystemUtil.h"
  21. #include "inputsystem/InputEnums.h"
  22. class Vector;
  23. class QAngle;
  24. class IMaterial;
  25. class ITexture;
  26. class IClientEntity;
  27. class CHudTexture;
  28. class CGameTrace;
  29. class C_BaseEntity;
  30. struct Ray_t;
  31. struct client_textmessage_t;
  32. typedef CGameTrace trace_t;
  33. namespace vgui
  34. {
  35. typedef unsigned long HFont;
  36. };
  37. extern bool g_MakingDevShots;
  38. // ScreenHeight returns the height of the screen, in pixels
  39. int ScreenHeight( void );
  40. // ScreenWidth returns the width of the screen, in pixels
  41. int ScreenWidth( void );
  42. #define XRES(x) ( x * ( ( float )ScreenWidth() / 640.0 ) )
  43. #define YRES(y) ( y * ( ( float )ScreenHeight() / 480.0 ) )
  44. int UTIL_ComputeStringWidth( vgui::HFont& font, const char *str );
  45. int UTIL_ComputeStringWidth( vgui::HFont& font, const wchar_t *str );
  46. float UTIL_AngleDiff( float destAngle, float srcAngle );
  47. void UTIL_Bubbles( const Vector& mins, const Vector& maxs, int count );
  48. void UTIL_Smoke( const Vector &origin, const float scale, const float framerate );
  49. void UTIL_ImpactTrace( trace_t *pTrace, int iDamageType, const char *pCustomImpactName = NULL );
  50. int UTIL_PrecacheDecal( const char *name, bool preload = false );
  51. void UTIL_EmitAmbientSound( C_BaseEntity *entity, const Vector &vecOrigin, const char *samp, float vol, soundlevel_t soundlevel, int fFlags, int pitch );
  52. void UTIL_SetOrigin( C_BaseEntity *entity, const Vector &vecOrigin );
  53. void UTIL_ScreenShake( const Vector &center, float amplitude, float frequency, float duration, float radius, ShakeCommand_t eCommand, bool bAirShake=false );
  54. byte *UTIL_LoadFileForMe( const char *filename, int *pLength );
  55. void UTIL_FreeFile( byte *buffer );
  56. void UTIL_MakeSafeName( const char *oldName, OUT_Z_CAP(newNameBufSize) char *newName, int newNameBufSize ); ///< Cleans up player names for putting in vgui controls (cleaned names can be up to original*2+1 in length)
  57. const char *UTIL_SafeName( const char *oldName ); ///< Wraps UTIL_MakeSafeName, and returns a static buffer
  58. void UTIL_ReplaceKeyBindings( const wchar_t *inbuf, int inbufsizebytes, OUT_Z_BYTECAP(outbufsizebytes) wchar_t *outbuf, int outbufsizebytes, GameActionSet_t action_set = GAME_ACTION_SET_NONE );
  59. // Fade out an entity based on distance fades
  60. unsigned char UTIL_ComputeEntityFade( C_BaseEntity *pEntity, float flMinDist, float flMaxDist, float flFadeScale );
  61. client_textmessage_t *TextMessageGet( const char *pName );
  62. char *VarArgs( PRINTF_FORMAT_STRING const char *format, ... );
  63. // Get the entity the local player is spectating (can be a player or a ragdoll entity).
  64. int GetSpectatorTarget();
  65. int GetSpectatorMode( void );
  66. bool IsPlayerIndex( int index );
  67. void UpdateLocalPlayerVisionFlags();
  68. int GetLocalPlayerIndex( void );
  69. int GetLocalPlayerVisionFilterFlags( bool bWeaponsCheck = false );
  70. bool IsLocalPlayerUsingVisionFilterFlags( int nFlags, bool bWeaponsCheck = false );
  71. int GetLocalPlayerTeam( void );
  72. bool IsLocalPlayerSpectator( void );
  73. void NormalizeAngles( QAngle& angles );
  74. void InterpolateAngles( const QAngle& start, const QAngle& end, QAngle& output, float frac );
  75. void InterpolateVector( float frac, const Vector& src, const Vector& dest, Vector& output );
  76. //-----------------------------------------------------------------------------
  77. // Base light indices to avoid index collision
  78. //-----------------------------------------------------------------------------
  79. enum
  80. {
  81. LIGHT_INDEX_TE_DYNAMIC = 0x10000000,
  82. LIGHT_INDEX_PLAYER_BRIGHT = 0x20000000,
  83. LIGHT_INDEX_MUZZLEFLASH = 0x40000000,
  84. };
  85. void UTIL_PrecacheOther( const char *szClassname );
  86. void UTIL_SetTrace(trace_t& tr, const Ray_t& ray, C_BaseEntity *edict, float fraction, int hitgroup, unsigned int contents, const Vector& normal, float intercept );
  87. bool GetVectorInScreenSpace( Vector pos, int& iX, int& iY, Vector *vecOffset = NULL );
  88. bool GetVectorInHudSpace( Vector pos, int& iX, int& iY, Vector *vecOffset = NULL );
  89. bool GetTargetInScreenSpace( C_BaseEntity *pTargetEntity, int& iX, int& iY, Vector *vecOffset = NULL );
  90. bool GetTargetInHudSpace( C_BaseEntity *pTargetEntity, int& iX, int& iY, Vector *vecOffset = NULL );
  91. // prints messages through the HUD (stub in client .dll right now )
  92. class C_BasePlayer;
  93. void ClientPrint( C_BasePlayer *player, int msg_dest, const char *msg_name, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL );
  94. // Pass in an array of pointers and an array size, it fills the array and returns the number inserted
  95. int UTIL_EntitiesInBox( C_BaseEntity **pList, int listMax, const Vector &mins, const Vector &maxs, int flagMask, int partitionMask = PARTITION_CLIENT_NON_STATIC_EDICTS );
  96. int UTIL_EntitiesInSphere( C_BaseEntity **pList, int listMax, const Vector &center, float radius, int flagMask, int partitionMask = PARTITION_CLIENT_NON_STATIC_EDICTS );
  97. int UTIL_EntitiesAlongRay( C_BaseEntity **pList, int listMax, const Ray_t &ray, int flagMask, int partitionMask = PARTITION_CLIENT_NON_STATIC_EDICTS );
  98. // make this a fixed size so it just sits on the stack
  99. #define MAX_SPHERE_QUERY 256
  100. class CEntitySphereQuery
  101. {
  102. public:
  103. // currently this builds the list in the constructor
  104. // UNDONE: make an iterative query of ISpatialPartition so we could
  105. // make queries like this optimal
  106. CEntitySphereQuery( const Vector &center, float radius, int flagMask=0, int partitionMask = PARTITION_CLIENT_NON_STATIC_EDICTS );
  107. C_BaseEntity *GetCurrentEntity();
  108. inline void NextEntity() { m_listIndex++; }
  109. private:
  110. int m_listIndex;
  111. int m_listCount;
  112. C_BaseEntity *m_pList[MAX_SPHERE_QUERY];
  113. };
  114. C_BaseEntity *CreateEntityByName( const char *className );
  115. // creates an entity by name, and ensure it's correctness
  116. // does not spawn the entity
  117. // use the CREATE_ENTITY() macro which wraps this, instead of using it directly
  118. template< class T >
  119. T *_CreateEntity( T *newClass, const char *className )
  120. {
  121. T *newEnt = dynamic_cast<T*>( CreateEntityByName(className) );
  122. if ( !newEnt )
  123. {
  124. Warning( "classname %s used to create wrong class type\n", className );
  125. Assert(0);
  126. }
  127. return newEnt;
  128. }
  129. #define CREATE_ENTITY( newClass, className ) _CreateEntity( (newClass*)NULL, className )
  130. #define CREATE_UNSAVED_ENTITY( newClass, className ) _CreateEntityTemplate( (newClass*)NULL, className )
  131. // Misc useful
  132. inline bool FStrEq(const char *sz1, const char *sz2)
  133. {
  134. return (sz1 == sz2 || V_stricmp(sz1, sz2) == 0);
  135. }
  136. // Given a vector, clamps the scalar axes to MAX_COORD_FLOAT ranges from worldsize.h
  137. void UTIL_BoundToWorldSize( Vector *pVecPos );
  138. // Increments the passed key for the current map, eg "viewed" if TF holds the number of times the player has
  139. // viewed the intro movie for this map
  140. void UTIL_IncrementMapKey( const char *pszCustomKey );
  141. // Gets the value of the passed key for the current map, eg "viewed" for number of times the player has viewed
  142. // the intro movie for this map
  143. int UTIL_GetMapKeyCount( const char *pszCustomKey );
  144. // Returns true if the user has loaded any maps, false otherwise.
  145. bool UTIL_HasLoadedAnyMap();
  146. #endif // !UTIL_H