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.

278 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Implements visual effects entities: sprites, beams, bubbles, etc.
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "EnvMessage.h"
  9. #include "engine/IEngineSound.h"
  10. #include "KeyValues.h"
  11. #include "filesystem.h"
  12. #include "Color.h"
  13. #include "gamestats.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. LINK_ENTITY_TO_CLASS( env_message, CMessage );
  17. BEGIN_DATADESC( CMessage )
  18. DEFINE_KEYFIELD( m_iszMessage, FIELD_STRING, "message" ),
  19. DEFINE_KEYFIELD( m_sNoise, FIELD_SOUNDNAME, "messagesound" ),
  20. DEFINE_KEYFIELD( m_MessageAttenuation, FIELD_INTEGER, "messageattenuation" ),
  21. DEFINE_KEYFIELD( m_MessageVolume, FIELD_FLOAT, "messagevolume" ),
  22. DEFINE_FIELD( m_Radius, FIELD_FLOAT ),
  23. DEFINE_INPUTFUNC( FIELD_VOID, "ShowMessage", InputShowMessage ),
  24. DEFINE_OUTPUT(m_OnShowMessage, "OnShowMessage"),
  25. END_DATADESC()
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. void CMessage::Spawn( void )
  30. {
  31. Precache();
  32. SetSolid( SOLID_NONE );
  33. SetMoveType( MOVETYPE_NONE );
  34. switch( m_MessageAttenuation )
  35. {
  36. case 1: // Medium radius
  37. m_Radius = ATTN_STATIC;
  38. break;
  39. case 2: // Large radius
  40. m_Radius = ATTN_NORM;
  41. break;
  42. case 3: //EVERYWHERE
  43. m_Radius = ATTN_NONE;
  44. break;
  45. default:
  46. case 0: // Small radius
  47. m_Radius = SNDLVL_IDLE;
  48. break;
  49. }
  50. m_MessageAttenuation = 0;
  51. // Remap volume from [0,10] to [0,1].
  52. m_MessageVolume *= 0.1;
  53. // No volume, use normal
  54. if ( m_MessageVolume <= 0 )
  55. {
  56. m_MessageVolume = 1.0;
  57. }
  58. }
  59. void CMessage::Precache( void )
  60. {
  61. if ( m_sNoise != NULL_STRING )
  62. {
  63. PrecacheScriptSound( STRING(m_sNoise) );
  64. }
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Input handler for showing the message and/or playing the sound.
  68. //-----------------------------------------------------------------------------
  69. void CMessage::InputShowMessage( inputdata_t &inputdata )
  70. {
  71. CBaseEntity *pPlayer = NULL;
  72. if ( m_spawnflags & SF_MESSAGE_ALL )
  73. {
  74. UTIL_ShowMessageAll( STRING( m_iszMessage ) );
  75. }
  76. else
  77. {
  78. if ( inputdata.pActivator && inputdata.pActivator->IsPlayer() )
  79. {
  80. pPlayer = inputdata.pActivator;
  81. }
  82. else
  83. {
  84. pPlayer = (gpGlobals->maxClients > 1) ? NULL : UTIL_GetLocalPlayer();
  85. }
  86. if ( pPlayer && pPlayer->IsPlayer() )
  87. {
  88. UTIL_ShowMessage( STRING( m_iszMessage ), ToBasePlayer( pPlayer ) );
  89. }
  90. }
  91. if ( m_sNoise != NULL_STRING )
  92. {
  93. CPASAttenuationFilter filter( this );
  94. EmitSound_t ep;
  95. ep.m_nChannel = CHAN_BODY;
  96. ep.m_pSoundName = (char*)STRING(m_sNoise);
  97. ep.m_flVolume = m_MessageVolume;
  98. ep.m_SoundLevel = ATTN_TO_SNDLVL( m_Radius );
  99. EmitSound( filter, entindex(), ep );
  100. }
  101. if ( m_spawnflags & SF_MESSAGE_ONCE )
  102. {
  103. UTIL_Remove( this );
  104. }
  105. m_OnShowMessage.FireOutput( inputdata.pActivator, this );
  106. }
  107. void CMessage::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  108. {
  109. inputdata_t inputdata;
  110. inputdata.pActivator = NULL;
  111. inputdata.pCaller = NULL;
  112. InputShowMessage( inputdata );
  113. }
  114. class CCredits : public CPointEntity
  115. {
  116. public:
  117. DECLARE_CLASS( CMessage, CPointEntity );
  118. DECLARE_DATADESC();
  119. void Spawn( void );
  120. void InputRollCredits( inputdata_t &inputdata );
  121. void InputRollOutroCredits( inputdata_t &inputdata );
  122. void InputShowLogo( inputdata_t &inputdata );
  123. void InputSetLogoLength( inputdata_t &inputdata );
  124. COutputEvent m_OnCreditsDone;
  125. virtual void OnRestore();
  126. private:
  127. void RollOutroCredits();
  128. bool m_bRolledOutroCredits;
  129. float m_flLogoLength;
  130. };
  131. LINK_ENTITY_TO_CLASS( env_credits, CCredits );
  132. BEGIN_DATADESC( CCredits )
  133. DEFINE_INPUTFUNC( FIELD_VOID, "RollCredits", InputRollCredits ),
  134. DEFINE_INPUTFUNC( FIELD_VOID, "RollOutroCredits", InputRollOutroCredits ),
  135. DEFINE_INPUTFUNC( FIELD_VOID, "ShowLogo", InputShowLogo ),
  136. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetLogoLength", InputSetLogoLength ),
  137. DEFINE_OUTPUT( m_OnCreditsDone, "OnCreditsDone"),
  138. DEFINE_FIELD( m_bRolledOutroCredits, FIELD_BOOLEAN ),
  139. DEFINE_FIELD( m_flLogoLength, FIELD_FLOAT )
  140. END_DATADESC()
  141. void CCredits::Spawn( void )
  142. {
  143. SetSolid( SOLID_NONE );
  144. SetMoveType( MOVETYPE_NONE );
  145. }
  146. static void CreditsDone_f( void )
  147. {
  148. CCredits *pCredits = (CCredits*)gEntList.FindEntityByClassname( NULL, "env_credits" );
  149. if ( pCredits )
  150. {
  151. pCredits->m_OnCreditsDone.FireOutput( pCredits, pCredits );
  152. }
  153. }
  154. static ConCommand creditsdone("creditsdone", CreditsDone_f );
  155. extern ConVar sv_unlockedchapters;
  156. void CCredits::OnRestore()
  157. {
  158. BaseClass::OnRestore();
  159. if ( m_bRolledOutroCredits )
  160. {
  161. // Roll them again so that the client .dll will send the "creditsdone" message and we'll
  162. // actually get back to the main menu
  163. RollOutroCredits();
  164. }
  165. }
  166. void CCredits::RollOutroCredits()
  167. {
  168. sv_unlockedchapters.SetValue( "15" );
  169. CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
  170. CSingleUserRecipientFilter user( pPlayer );
  171. user.MakeReliable();
  172. UserMessageBegin( user, "CreditsMsg" );
  173. WRITE_BYTE( 3 );
  174. MessageEnd();
  175. }
  176. void CCredits::InputRollOutroCredits( inputdata_t &inputdata )
  177. {
  178. RollOutroCredits();
  179. // In case we save restore
  180. m_bRolledOutroCredits = true;
  181. gamestats->Event_Credits();
  182. }
  183. void CCredits::InputShowLogo( inputdata_t &inputdata )
  184. {
  185. CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
  186. CSingleUserRecipientFilter user( pPlayer );
  187. user.MakeReliable();
  188. if ( m_flLogoLength )
  189. {
  190. UserMessageBegin( user, "LogoTimeMsg" );
  191. WRITE_FLOAT( m_flLogoLength );
  192. MessageEnd();
  193. }
  194. else
  195. {
  196. UserMessageBegin( user, "CreditsMsg" );
  197. WRITE_BYTE( 1 );
  198. MessageEnd();
  199. }
  200. }
  201. void CCredits::InputSetLogoLength( inputdata_t &inputdata )
  202. {
  203. m_flLogoLength = inputdata.value.Float();
  204. }
  205. void CCredits::InputRollCredits( inputdata_t &inputdata )
  206. {
  207. CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
  208. CSingleUserRecipientFilter user( pPlayer );
  209. user.MakeReliable();
  210. UserMessageBegin( user, "CreditsMsg" );
  211. WRITE_BYTE( 2 );
  212. MessageEnd();
  213. }