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.

227 lines
5.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //
  9. // Geiger.cpp
  10. //
  11. // implementation of CHudAmmo class
  12. //
  13. #include "cbase.h"
  14. #include "hudelement.h"
  15. #include "hud_macros.h"
  16. #include "engine/IEngineSound.h"
  17. #include "SoundEmitterSystem/isoundemittersystembase.h"
  18. #include "iclientmode.h"
  19. #include <vgui_controls/Controls.h>
  20. #include <vgui_controls/Panel.h>
  21. #include <vgui/ISurface.h>
  22. using namespace vgui;
  23. // memdbgon must be the last include file in a .cpp file!!!
  24. #include "tier0/memdbgon.h"
  25. static const float TARGET_FRAMETIME = 1.0 / 60.0;
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. class CHudGeiger: public CHudElement, public vgui::Panel
  30. {
  31. DECLARE_CLASS_SIMPLE( CHudGeiger, vgui::Panel );
  32. public:
  33. CHudGeiger( const char *pElementName );
  34. void Init( void );
  35. void VidInit( void );
  36. bool ShouldDraw( void );
  37. virtual void ApplySchemeSettings( vgui::IScheme *scheme );
  38. virtual void Paint( void );
  39. void MsgFunc_Geiger(bf_read &msg);
  40. private:
  41. int m_iGeigerRange;
  42. };
  43. DECLARE_HUDELEMENT( CHudGeiger );
  44. DECLARE_HUD_MESSAGE( CHudGeiger, Geiger );
  45. //-----------------------------------------------------------------------------
  46. // Purpose:
  47. //-----------------------------------------------------------------------------
  48. CHudGeiger::CHudGeiger( const char *pElementName ) :
  49. CHudElement( pElementName ), BaseClass( NULL, "HudGeiger" )
  50. {
  51. vgui::Panel *pParent = g_pClientMode->GetViewport();
  52. SetParent( pParent );
  53. SetHiddenBits( HIDEHUD_HEALTH );
  54. }
  55. void CHudGeiger::ApplySchemeSettings( IScheme *scheme )
  56. {
  57. BaseClass::ApplySchemeSettings( scheme );
  58. SetPaintBackgroundEnabled( false );
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose:
  62. //-----------------------------------------------------------------------------
  63. void CHudGeiger::Init(void)
  64. {
  65. HOOK_HUD_MESSAGE( CHudGeiger, Geiger );
  66. m_iGeigerRange = 0;
  67. };
  68. //-----------------------------------------------------------------------------
  69. // Purpose:
  70. //-----------------------------------------------------------------------------
  71. void CHudGeiger::VidInit(void)
  72. {
  73. m_iGeigerRange = 0;
  74. };
  75. //-----------------------------------------------------------------------------
  76. // Purpose:
  77. //-----------------------------------------------------------------------------
  78. void CHudGeiger::MsgFunc_Geiger(bf_read &msg)
  79. {
  80. // update geiger data
  81. m_iGeigerRange = msg.ReadByte();
  82. m_iGeigerRange = m_iGeigerRange << 2;
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose:
  86. //-----------------------------------------------------------------------------
  87. bool CHudGeiger::ShouldDraw( void )
  88. {
  89. return ( CHudElement::ShouldDraw() && ( m_iGeigerRange < 1000 && m_iGeigerRange > 0 ) );
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Purpose:
  93. //-----------------------------------------------------------------------------
  94. void CHudGeiger::Paint()
  95. {
  96. int pct;
  97. float flvol=0;
  98. bool highsound = false;
  99. // piecewise linear is better than continuous formula for this
  100. if (m_iGeigerRange > 800)
  101. {
  102. pct = 0; //Msg ( "range > 800\n");
  103. }
  104. else if (m_iGeigerRange > 600)
  105. {
  106. pct = 2;
  107. flvol = 0.4; //Msg ( "range > 600\n");
  108. }
  109. else if (m_iGeigerRange > 500)
  110. {
  111. pct = 4;
  112. flvol = 0.5; //Msg ( "range > 500\n");
  113. }
  114. else if (m_iGeigerRange > 400)
  115. {
  116. pct = 8;
  117. flvol = 0.6; //Msg ( "range > 400\n");
  118. highsound = true;
  119. }
  120. else if (m_iGeigerRange > 300)
  121. {
  122. pct = 8;
  123. flvol = 0.7; //Msg ( "range > 300\n");
  124. highsound = true;
  125. }
  126. else if (m_iGeigerRange > 200)
  127. {
  128. pct = 28;
  129. flvol = 0.78; //Msg ( "range > 200\n");
  130. highsound = true;
  131. }
  132. else if (m_iGeigerRange > 150)
  133. {
  134. pct = 40;
  135. flvol = 0.80; //Msg ( "range > 150\n");
  136. highsound = true;
  137. }
  138. else if (m_iGeigerRange > 100)
  139. {
  140. pct = 60;
  141. flvol = 0.85; //Msg ( "range > 100\n");
  142. highsound = true;
  143. }
  144. else if (m_iGeigerRange > 75)
  145. {
  146. pct = 80;
  147. flvol = 0.9; //Msg ( "range > 75\n");
  148. //gflGeigerDelay = cl.time + GEIGERDELAY * 0.75;
  149. highsound = true;
  150. }
  151. else if (m_iGeigerRange > 50)
  152. {
  153. pct = 90;
  154. flvol = 0.95; //Msg ( "range > 50\n");
  155. }
  156. else
  157. {
  158. pct = 95;
  159. flvol = 1.0; //Msg ( "range < 50\n");
  160. }
  161. int n = random->RandomInt( 0, 64 );
  162. // Make geiger noise chance framerate independent
  163. float flMultiplier = 1 / ( gpGlobals->frametime / TARGET_FRAMETIME );
  164. // Constrain the multiplier
  165. if ( flMultiplier > 10 )
  166. {
  167. flMultiplier = 10;
  168. }
  169. else if ( flMultiplier < 0.1 )
  170. {
  171. flMultiplier = 0.1;
  172. }
  173. n *= flMultiplier;
  174. if ( n < pct )
  175. {
  176. char sz[256];
  177. if ( highsound )
  178. {
  179. strcpy( sz, "Geiger.BeepHigh" );
  180. }
  181. else
  182. {
  183. strcpy( sz, "Geiger.BeepLow" );
  184. }
  185. CSoundParameters params;
  186. if ( C_BaseEntity::GetParametersForSound( sz, params, NULL ) )
  187. {
  188. flvol = ( flvol * ( random->RandomInt( 0,127 ) ) / 255 ) + 0.25;
  189. CLocalPlayerFilter filter;
  190. EmitSound_t ep;
  191. ep.m_nChannel = params.channel;
  192. ep.m_pSoundName = params.soundname;
  193. ep.m_flVolume = flvol;
  194. ep.m_SoundLevel = params.soundlevel;
  195. ep.m_nPitch = params.pitch;
  196. C_BaseEntity::EmitSound( filter, SOUND_FROM_LOCAL_PLAYER, ep );
  197. }
  198. }
  199. }