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.

179 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "tf_gamerules.h"
  8. #include "entity_bird.h"
  9. #include "KeyValues.h"
  10. #include "filesystem.h"
  11. #include "tf_fx.h"
  12. LINK_ENTITY_TO_CLASS( entity_bird, CEntityBird );
  13. #define ENTITYBIRD_MODEL "models/props_forest/bird.mdl"
  14. //-----------------------------------------------------------------------------
  15. // Purpose:
  16. //-----------------------------------------------------------------------------
  17. void CEntityBird::Spawn( void )
  18. {
  19. Precache();
  20. BaseClass::Spawn();
  21. SetMoveType( MOVETYPE_NONE );
  22. SetSolid( SOLID_BBOX );
  23. SetModel( ENTITYBIRD_MODEL );
  24. SetSize( -Vector(8,8,0), Vector(8,8,16) );
  25. SetSequence(0);
  26. m_takedamage = DAMAGE_YES;
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Purpose:
  30. //-----------------------------------------------------------------------------
  31. void CEntityBird::Precache( void )
  32. {
  33. BaseClass::Precache();
  34. // We deliberately allow late precaches here. It'll cause a hitch, but it'll ensure
  35. // we don't load the model on any map that doesn't have birds, and we'll make sure
  36. // we don't accidentally keep loading it after the birds are supposed to go away.
  37. bool bAllowPrecache = CBaseEntity::IsPrecacheAllowed();
  38. CBaseEntity::SetAllowPrecache( true );
  39. PrecacheModel( ENTITYBIRD_MODEL );
  40. CBaseEntity::SetAllowPrecache( bAllowPrecache );
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. void CEntityBird::Touch( CBaseEntity *pOther )
  46. {
  47. BaseClass::Touch( pOther );
  48. // If we're touched by anything, we pop!
  49. Explode();
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. //-----------------------------------------------------------------------------
  54. int CEntityBird::OnTakeDamage( const CTakeDamageInfo &info )
  55. {
  56. Explode();
  57. return 1;
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose:
  61. //-----------------------------------------------------------------------------
  62. void CEntityBird::Explode( void )
  63. {
  64. Vector vecOrigin = WorldSpaceCenter();
  65. CPVSFilter filter( vecOrigin );
  66. TE_TFExplosion( filter, 0.0f, vecOrigin, Vector(0,0,1), TF_WEAPON_NONE, 0 );
  67. UTIL_Remove( this );
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose: Spawn random birds at locations on certain maps.
  71. //-----------------------------------------------------------------------------
  72. void CEntityBird::SpawnRandomBirds( void )
  73. {
  74. const char *pszMapName = STRING( gpGlobals->mapname );
  75. if ( !pszMapName || !pszMapName[0] )
  76. return;
  77. KeyValues *pFileKV = new KeyValues( "birds" );
  78. if ( !pFileKV->LoadFromFile( g_pFullFileSystem, "scripts/birds.txt", "MOD" ) )
  79. return;
  80. // Build a list of birds in the map already, and make sure we don't spawn any on those spots again
  81. CUtlVector<Vector> vecExistingBirds;
  82. CBaseEntity *pBird = gEntList.FindEntityByClassname( NULL, "entity_bird" );
  83. while( pBird )
  84. {
  85. vecExistingBirds.AddToTail( pBird->GetAbsOrigin() );
  86. pBird = gEntList.FindEntityByClassname( pBird, "entity_bird" );
  87. }
  88. // See if we have an entry for this map
  89. KeyValues *pMapKV = pFileKV->FindKey( pszMapName );
  90. if ( pMapKV )
  91. {
  92. CUtlVector<Vector> vecLocations;
  93. KeyValues *pkvLoc = pMapKV->GetFirstSubKey();
  94. while ( pkvLoc )
  95. {
  96. const char *pszLocation = pkvLoc->GetString();
  97. int iIdx = vecLocations.AddToTail();
  98. UTIL_StringToVector( vecLocations[iIdx].Base(), pszLocation );
  99. pkvLoc = pkvLoc->GetNextKey();
  100. }
  101. if ( vecLocations.Count() )
  102. {
  103. //int iLocation = RandomInt(0,vecLocations.Count()-1);
  104. for ( int i = 0; i < vecLocations.Count(); i++ )
  105. {
  106. bool bExists = false;
  107. // Make sure there isn't a bird here already
  108. FOR_EACH_VEC( vecExistingBirds, iBird )
  109. {
  110. if ( vecLocations[i].DistToSqr(vecExistingBirds[iBird]) < (32*32) )
  111. {
  112. bExists = true;
  113. break;
  114. }
  115. }
  116. if ( !bExists )
  117. {
  118. CBaseEntity::Create( "entity_bird", vecLocations[i], QAngle(0,RandomFloat(0,360),0) );
  119. }
  120. }
  121. }
  122. }
  123. pFileKV->deleteThis();
  124. }
  125. //-----------------------------------------------------------------------------
  126. //-----------------------------------------------------------------------------
  127. #define ENTITY_FLYING_BIRD_SPEED_MIN 200
  128. #define ENTITY_FLYING_BIRD_SPEED_MAX 500
  129. //-----------------------------------------------------------------------------
  130. // Purpose:
  131. //-----------------------------------------------------------------------------
  132. void SpawnClientsideFlyingBird( Vector &vecSpawn )
  133. {
  134. float flyAngle = RandomFloat( -M_PI, M_PI );
  135. float flyAngleRate = RandomFloat( -1.5f, 1.5f );
  136. float accelZ = RandomFloat( 0.5f, 2.0f );
  137. float speed = RandomFloat( ENTITY_FLYING_BIRD_SPEED_MIN, ENTITY_FLYING_BIRD_SPEED_MAX );
  138. float flGlideTime = RandomFloat( 0.25f, 1.0f );
  139. bool bAllowPrecache = CBaseEntity::IsPrecacheAllowed();
  140. CBaseEntity::SetAllowPrecache( true );
  141. CBaseEntity::PrecacheModel( "models/props_forest/dove.mdl" );
  142. CBaseEntity::SetAllowPrecache( bAllowPrecache );
  143. CPVSFilter filter( vecSpawn );
  144. UserMessageBegin( filter, "SpawnFlyingBird" );
  145. WRITE_VEC3COORD( vecSpawn );
  146. WRITE_FLOAT( flyAngle );
  147. WRITE_FLOAT( flyAngleRate );
  148. WRITE_FLOAT( accelZ );
  149. WRITE_FLOAT( speed );
  150. WRITE_FLOAT( flGlideTime );
  151. MessageEnd();
  152. }