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.

233 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "tf_extra_map_entity.h"
  8. #include "KeyValues.h"
  9. #include "filesystem.h"
  10. struct EntityWhiteList_t
  11. {
  12. const char *pszKeyName;
  13. const char *pszEntName;
  14. };
  15. // limit the entities that can be created using this method
  16. EntityWhiteList_t g_szEntityWhiteList[] =
  17. {
  18. // { "rocket", "entity_rocket" },
  19. // { "carrier", "entity_carrier" },
  20. { "sign", "entity_sign" },
  21. // { "saucer", "entity_saucer" },
  22. };
  23. LINK_ENTITY_TO_CLASS( entity_rocket, CExtraMapEntity_Rocket );
  24. LINK_ENTITY_TO_CLASS( entity_carrier, CExtraMapEntity_Carrier );
  25. LINK_ENTITY_TO_CLASS( entity_sign, CExtraMapEntity_Sign );
  26. LINK_ENTITY_TO_CLASS( entity_saucer, CExtraMapEntity_Saucer );
  27. BEGIN_DATADESC( CExtraMapEntity )
  28. DEFINE_THINKFUNC( AnimThink ),
  29. END_DATADESC();
  30. IMPLEMENT_AUTO_LIST( IExtraMapEntityAutoList );
  31. //-----------------------------------------------------------------------------
  32. // Purpose:
  33. //-----------------------------------------------------------------------------
  34. void CExtraMapEntity::Spawn( void )
  35. {
  36. Precache();
  37. BaseClass::Spawn();
  38. SetModel( STRING( GetModelName() ) );
  39. SetMoveType( MOVETYPE_NONE );
  40. SetSequence( 0 );
  41. if ( ShouldAnimate() )
  42. {
  43. ResetSequenceInfo();
  44. SetThink( &CExtraMapEntity::AnimThink );
  45. SetNextThink( gpGlobals->curtime + 0.1f );
  46. }
  47. }
  48. void CExtraMapEntity::AnimThink( void )
  49. {
  50. StudioFrameAdvance();
  51. DispatchAnimEvents( this );
  52. SetNextThink( gpGlobals->curtime + 0.1f );
  53. }
  54. void CExtraMapEntity::Precache( void )
  55. {
  56. BaseClass::Precache();
  57. bool bAllowPrecache = CBaseEntity::IsPrecacheAllowed();
  58. CBaseEntity::SetAllowPrecache( true );
  59. Precache_Internal();
  60. CBaseEntity::SetAllowPrecache( bAllowPrecache );
  61. }
  62. void CExtraMapEntity::Precache_Internal( void )
  63. {
  64. PrecacheModel( STRING( GetModelName() ) );
  65. }
  66. const char *CExtraMapEntity::ValidateKeyName( const char *pszKey )
  67. {
  68. if ( pszKey && pszKey[0] )
  69. {
  70. for ( int i = 0; i < ARRAYSIZE( g_szEntityWhiteList ); i++ )
  71. {
  72. if ( FStrEq( pszKey, g_szEntityWhiteList[i].pszKeyName ) )
  73. {
  74. return g_szEntityWhiteList[i].pszEntName;
  75. }
  76. }
  77. }
  78. return NULL;
  79. }
  80. void CExtraMapEntity::PrepareModelName( const char *szModelName )
  81. {
  82. const char *pszTemp = GetDefaultModel();
  83. if ( szModelName && szModelName[0] )
  84. {
  85. pszTemp = szModelName;
  86. }
  87. SetModelName( AllocPooledString( pszTemp ) );
  88. }
  89. void CExtraMapEntity::SpawnExtraModel( void )
  90. {
  91. /*
  92. const char *pszMapName = STRING( gpGlobals->mapname );
  93. if ( !pszMapName || !pszMapName[0] )
  94. return;
  95. KeyValues *pFileKV = new KeyValues( "models" );
  96. if ( !pFileKV->LoadFromFile( g_pFullFileSystem, "scripts/extra_models.txt", "MOD" ) )
  97. return;
  98. // See if we have an entry for this map.
  99. KeyValues *pMapKV = pFileKV->FindKey( pszMapName );
  100. if ( pMapKV )
  101. {
  102. FOR_EACH_SUBKEY( pMapKV, pSubKeyEnt )
  103. {
  104. const char *pszEntName = ValidateKeyName( pSubKeyEnt->GetName() );
  105. if ( !pszEntName )
  106. continue;
  107. FOR_EACH_SUBKEY( pSubKeyEnt, pSubKeyCount )
  108. {
  109. Vector loc = vec3_origin;
  110. QAngle rot( 0, 0, 0 );
  111. char szModelName[MAX_PATH];
  112. szModelName[0] = '\0';
  113. float flChance = 1.0f; // assume we want to show everything unless specified in the .txt file
  114. FOR_EACH_SUBKEY( pSubKeyCount, pSubKey )
  115. {
  116. if ( FStrEq( pSubKey->GetName(), "location" ) )
  117. {
  118. const char *pszLoc = pSubKey->GetString();
  119. UTIL_StringToVector( loc.Base(), pszLoc );
  120. }
  121. else if ( FStrEq( pSubKey->GetName(), "rotation" ) )
  122. {
  123. const char *pszRot = pSubKey->GetString();
  124. UTIL_StringToVector( rot.Base(), pszRot );
  125. }
  126. else if ( FStrEq( pSubKey->GetName(), "model" ) )
  127. {
  128. V_strcpy_safe( szModelName, pSubKey->GetString() );
  129. }
  130. else if ( FStrEq( pSubKey->GetName(), "chance" ) )
  131. {
  132. flChance = pSubKey->GetFloat();
  133. if ( flChance > 1.0f )
  134. {
  135. flChance = 1.0f;
  136. }
  137. }
  138. }
  139. if ( ( flChance > 0.0f ) && ( RandomFloat( 0, 1 ) < flChance ) )
  140. {
  141. CExtraMapEntity *pExtraMapEntity = static_cast< CExtraMapEntity* >( CBaseEntity::CreateNoSpawn( pszEntName, loc, rot ) );
  142. if ( pExtraMapEntity )
  143. {
  144. pExtraMapEntity->PrepareModelName( szModelName );
  145. DispatchSpawn( pExtraMapEntity );
  146. }
  147. }
  148. }
  149. }
  150. }
  151. pFileKV->deleteThis();
  152. */
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Grordbort rocket model in the skybox
  156. //-----------------------------------------------------------------------------
  157. void CExtraMapEntity_Rocket::Spawn( void )
  158. {
  159. BaseClass::Spawn();
  160. SetSolid( SOLID_BBOX );
  161. SetSize( -Vector( 8, 8, 0 ), Vector( 8, 8, 16 ) );
  162. }
  163. void CExtraMapEntity_Rocket::Precache_Internal( void )
  164. {
  165. BaseClass::Precache_Internal();
  166. PrecacheParticleSystem( "smoke_blackbillow_skybox" );
  167. }
  168. //-----------------------------------------------------------------------------
  169. // Purpose: Mann vs. Machine carrier model in the skybox
  170. //-----------------------------------------------------------------------------
  171. void CExtraMapEntity_Carrier::Spawn( void )
  172. {
  173. BaseClass::Spawn();
  174. SetSolid( SOLID_BBOX );
  175. SetSize( -Vector( 8, 8, 0 ), Vector( 8, 8, 16 ) );
  176. }
  177. //-----------------------------------------------------------------------------
  178. // Purpose: Sign models to tease future updates
  179. //-----------------------------------------------------------------------------
  180. void CExtraMapEntity_Sign::Spawn( void )
  181. {
  182. BaseClass::Spawn();
  183. SetSolid( SOLID_NONE );
  184. AddEffects( EF_NOSHADOW );
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Purpose: Saucer models to tease future updates
  188. //-----------------------------------------------------------------------------
  189. void CExtraMapEntity_Saucer::Spawn( void )
  190. {
  191. BaseClass::Spawn();
  192. SetSolid( SOLID_NONE );
  193. AddEffects( EF_NOSHADOW );
  194. }