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.

619 lines
17 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Defines a combine ball and a combine ball launcher which have certain properties
  4. // overwritten to make use of them in portal game play.
  5. //
  6. //=====================================================================================//
  7. #include "cbase.h" // for pch
  8. #include "prop_combine_ball.h" // for base class
  9. #include "te_effect_dispatch.h" // for the explosion/impact effects
  10. #include "prop_portal.h" // Special case code for passing through portals. We need the class definition.
  11. #include "soundenvelope.h"
  12. #include "physicsshadowclone.h"
  13. // resource file names
  14. #define IMPACT_DECAL_NAME "decals/smscorch1model"
  15. // context think
  16. #define UPDATE_THINK_CONTEXT "UpdateThinkContext"
  17. class CPropEnergyBall : public CPropCombineBall
  18. {
  19. public:
  20. DECLARE_CLASS( CPropEnergyBall, CPropCombineBall );
  21. DECLARE_DATADESC();
  22. DECLARE_SERVERCLASS();
  23. virtual void Precache();
  24. virtual void CreateSounds( void );
  25. virtual void StopLoopingSounds( void );
  26. virtual void Spawn();
  27. virtual void Activate( void );
  28. // Overload for unlimited bounces and predictable movement
  29. virtual void VPhysicsCollision( int index, gamevcollisionevent_t *pEvent );
  30. // Overload for less sound, no shake.
  31. virtual void ExplodeThink( void );
  32. // Update in a time till death update
  33. virtual void Think ( void );
  34. virtual void EndTouch( CBaseEntity *pOther );
  35. virtual void StartTouch( CBaseEntity *pOther );
  36. virtual void NotifySystemEvent( CBaseEntity *pNotify, notify_system_event_t eventType, const notify_system_event_params_t &params );
  37. CHandle<CProp_Portal> m_hTouchedPortal; // Pointer to the portal we are touched most recently
  38. bool m_bTouchingPortal1; // Are we touching portal 1
  39. bool m_bTouchingPortal2; // Are we touching portal 2
  40. // Remember the last known direction of travel, incase our velocity is cleared.
  41. Vector m_vLastKnownDirection;
  42. // After portal teleports, we force the life to be at least this number.
  43. float m_fMinLifeAfterPortal;
  44. CNetworkVar( bool, m_bIsInfiniteLife );
  45. CNetworkVar( float, m_fTimeTillDeath );
  46. CSoundPatch *m_pAmbientSound;
  47. };
  48. LINK_ENTITY_TO_CLASS( prop_energy_ball, CPropEnergyBall );
  49. BEGIN_DATADESC( CPropEnergyBall )
  50. DEFINE_FIELD( m_hTouchedPortal, FIELD_EHANDLE ),
  51. DEFINE_FIELD( m_bTouchingPortal1, FIELD_BOOLEAN ),
  52. DEFINE_FIELD( m_bTouchingPortal2, FIELD_BOOLEAN ),
  53. DEFINE_FIELD( m_vLastKnownDirection, FIELD_VECTOR ),
  54. DEFINE_FIELD( m_fMinLifeAfterPortal, FIELD_FLOAT ),
  55. DEFINE_FIELD( m_bIsInfiniteLife, FIELD_BOOLEAN ),
  56. DEFINE_FIELD( m_fTimeTillDeath, FIELD_FLOAT ),
  57. DEFINE_SOUNDPATCH( m_pAmbientSound ),
  58. DEFINE_THINKFUNC( Think ),
  59. END_DATADESC()
  60. IMPLEMENT_SERVERCLASS_ST( CPropEnergyBall, DT_PropEnergyBall )
  61. SendPropBool( SENDINFO( m_bIsInfiniteLife ) ),
  62. SendPropFloat ( SENDINFO( m_fTimeTillDeath ) ),
  63. END_SEND_TABLE()
  64. //-----------------------------------------------------------------------------
  65. // Purpose: Precache
  66. // Input : -
  67. //-----------------------------------------------------------------------------
  68. void CPropEnergyBall::Precache()
  69. {
  70. BaseClass::Precache();
  71. PrecacheScriptSound( "EnergyBall.Explosion" );
  72. PrecacheScriptSound( "EnergyBall.Launch" );
  73. PrecacheScriptSound( "EnergyBall.Impact" );
  74. PrecacheScriptSound( "EnergyBall.AmbientLoop" );
  75. UTIL_PrecacheDecal( IMPACT_DECAL_NAME, false );
  76. }
  77. void CPropEnergyBall::CreateSounds()
  78. {
  79. if (!m_pAmbientSound)
  80. {
  81. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  82. CPASAttenuationFilter filter( this );
  83. m_pAmbientSound = controller.SoundCreate( filter, entindex(), "EnergyBall.AmbientLoop" );
  84. controller.Play( m_pAmbientSound, 1.0, 100 );
  85. }
  86. }
  87. void CPropEnergyBall::StopLoopingSounds()
  88. {
  89. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  90. controller.SoundDestroy( m_pAmbientSound );
  91. m_pAmbientSound = NULL;
  92. BaseClass::StopLoopingSounds();
  93. }
  94. //-----------------------------------------------------------------------------
  95. // Purpose:
  96. // Input : -
  97. //-----------------------------------------------------------------------------
  98. void CPropEnergyBall::Spawn()
  99. {
  100. Precache();
  101. BaseClass::Spawn();
  102. m_bTouchingPortal1 = false;
  103. m_bTouchingPortal2 = false;
  104. m_bIsInfiniteLife = false;
  105. m_fTimeTillDeath = -1;
  106. m_fMinLifeAfterPortal = 5;
  107. // Init last known direction to our initial direction
  108. GetVelocity( &m_vLastKnownDirection, NULL );
  109. }
  110. void CPropEnergyBall::Activate( void )
  111. {
  112. BaseClass::Activate();
  113. CreateSounds();
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose: Keep a constant velocity despite collisions, make impact sounds and effects
  117. //-----------------------------------------------------------------------------
  118. void CPropEnergyBall::VPhysicsCollision( int index, gamevcollisionevent_t *pEvent )
  119. {
  120. // Skip combine ball's collision, but do everything below it.
  121. BaseClass::BaseClass::VPhysicsCollision( index, pEvent );
  122. Vector preVelocity = pEvent->preVelocity[index];
  123. // float flSpeed = VectorNormalize( preVelocity );
  124. // It's ok to change direction, but maintain speed = m_flSpeed.
  125. Vector vecFinalVelocity = pEvent->postVelocity[index];
  126. VectorNormalize( vecFinalVelocity );
  127. if ( m_bTouchingPortal2 || m_bTouchingPortal1 )
  128. {
  129. AssertMsg ( m_hTouchedPortal.Get(), "Touching a portal, but recorded an invalid handle." );
  130. }
  131. // Used for deciding if we play our impact effects/sounds
  132. bool bIsEnteringPortalAndLockingAxisForward = false;
  133. // Fixed bounce axis when in a portal environment
  134. if ( (m_bTouchingPortal2 || m_bTouchingPortal1) && m_hTouchedPortal.Get() )
  135. {
  136. // Force our velocity to be either towards or away from the portal, no bouncing at odd angles allowed
  137. CProp_Portal* pPortal = m_hTouchedPortal.Get();
  138. // Only lock to the portal's forward axis if we're in it's world bounds
  139. // We use a tolerance of four, because the render bounds thickness for a portal is 4, and this function
  140. // intersects with a plane.
  141. bool bHitPortal = UTIL_IsBoxIntersectingPortal( GetAbsOrigin(), WorldAlignSize(), pPortal, 4.0f );
  142. // We definitely hit a portal
  143. if ( bHitPortal && pPortal && pPortal->IsActivedAndLinked() )
  144. {
  145. Vector vecTouchedPortalFace;
  146. pPortal->GetVectors( &vecTouchedPortalFace, NULL, NULL );
  147. vecTouchedPortalFace.NormalizeInPlace();
  148. float fDot = vecTouchedPortalFace.Dot( vecFinalVelocity );
  149. // closer to 'towards' the portal, force it to go that direction
  150. if ( fDot < 0 )
  151. {
  152. vecFinalVelocity = -vecTouchedPortalFace;
  153. // Since we're going 'through', don't do surfaceprop based collision effects
  154. // because the it will look like we didn't hit anything.
  155. pEvent->surfaceProps[0] = pEvent->surfaceProps[1] = physprops->GetSurfaceIndex( "default" );
  156. bIsEnteringPortalAndLockingAxisForward = true;
  157. }
  158. else // Closer to 'away from' the portal. Force the energy ball to go that direction
  159. {
  160. vecFinalVelocity = vecTouchedPortalFace;
  161. }
  162. }
  163. }
  164. // Plant a decal on any solid brushes we hit
  165. if ( !bIsEnteringPortalAndLockingAxisForward )
  166. {
  167. trace_t tr;
  168. UTIL_TraceLine ( GetAbsOrigin(), GetAbsOrigin() + 60*preVelocity, MASK_SHOT,
  169. this, COLLISION_GROUP_NONE, &tr);
  170. // Only place decals and draw effects if we hit something valid
  171. if ( tr.m_pEnt )
  172. {
  173. // Cball impact effect (using same trace as the decal placement above)
  174. CEffectData data;
  175. data.m_flRadius = 16;
  176. data.m_vNormal = tr.plane.normal;
  177. data.m_vOrigin = tr.endpos + tr.plane.normal * 1.0f;
  178. DispatchEffect( "cball_bounce", data );
  179. if ( tr.m_pEnt )
  180. {
  181. UTIL_DecalTrace( &tr, "EnergyBall.Impact" );
  182. }
  183. }
  184. EmitSound( "EnergyBall.Impact" );
  185. }
  186. // Record our direction so our fixed direction hacks know we have changed direction immediately
  187. m_vLastKnownDirection = vecFinalVelocity;
  188. // Scale new velocity to our fixed speed
  189. vecFinalVelocity *= GetSpeed();
  190. // Try to update the velocity now, however I'm told this rarely works.
  191. // We will spam updates in our think function to help get us in the direction we want to go.
  192. PhysCallbackSetVelocity( pEvent->pObjects[index], vecFinalVelocity );
  193. }
  194. void CPropEnergyBall::NotifySystemEvent(CBaseEntity *pNotify, notify_system_event_t eventType, const notify_system_event_params_t &params )
  195. {
  196. // On teleport, we record a pointer to the portal we are arriving at
  197. if ( eventType == NOTIFY_EVENT_TELEPORT )
  198. {
  199. CProp_Portal *pEnteredPortal = dynamic_cast<CProp_Portal*>( pNotify );
  200. if( pEnteredPortal )
  201. {
  202. m_vLastKnownDirection = pEnteredPortal->m_matrixThisToLinked.ApplyRotation( m_vLastKnownDirection );
  203. m_vLastKnownDirection.NormalizeInPlace();
  204. IPhysicsObject *pPhysObject = VPhysicsGetObject();
  205. if( pPhysObject )
  206. {
  207. Vector vNewVelocity = m_vLastKnownDirection * GetSpeed();
  208. pPhysObject->SetVelocityInstantaneous( &vNewVelocity, NULL );
  209. }
  210. // Record the new portal for the purposes of locking our movement
  211. m_hTouchedPortal = pEnteredPortal->m_hLinkedPortal;
  212. }
  213. else
  214. {
  215. m_hTouchedPortal = NULL;
  216. }
  217. // If an energy ball passes a portal (teleports), add a make sure its life is >= sk_energy_ball_min_life_after_portal
  218. float fCurTimeTillDeath = GetNextThink( "ExplodeTimerContext" );
  219. // If we are set to die, then refresh that time if it is below a set threshold
  220. if ( fCurTimeTillDeath > 0 )
  221. {
  222. float fTimeLeft = fCurTimeTillDeath - gpGlobals->curtime;
  223. float fMinLife = m_fMinLifeAfterPortal;
  224. float fTimeToDie = (fTimeLeft > fMinLife) ? (fTimeLeft) : (fMinLife);
  225. SetContextThink( &CPropCombineBall::ExplodeThink, gpGlobals->curtime + fTimeToDie, "ExplodeTimerContext" );
  226. }
  227. }
  228. //BaseClass::NotifySystemEvent( pNotify, eventType, params );
  229. }
  230. //-----------------------------------------------------------------------------
  231. // Purpose: Send down the time till death to the client code to help indicate when the ball will detonate
  232. // Input : -
  233. //-----------------------------------------------------------------------------
  234. void CPropEnergyBall::Think()
  235. {
  236. // Finite life energy balls send the time till death down to the client for display purposes
  237. if ( !m_bIsInfiniteLife )
  238. {
  239. m_fTimeTillDeath = GetNextThink( "ExplodeTimerContext" ) - gpGlobals->curtime;
  240. SetNextThink ( gpGlobals->curtime + 0.5f );
  241. }
  242. // Force our movement to be at desired speed
  243. IPhysicsObject* pMyObject = VPhysicsGetObject();
  244. if ( pMyObject )
  245. {
  246. // get our current speed
  247. Vector vCurVelocity, vNewVelocity;
  248. pMyObject->GetVelocity( &vCurVelocity, NULL );
  249. float fCurSpeed = vCurVelocity.Length();
  250. if ( fCurSpeed < GetSpeed() )
  251. {
  252. m_vLastKnownDirection.NormalizeInPlace();
  253. vNewVelocity = m_vLastKnownDirection * GetSpeed();
  254. pMyObject->SetVelocityInstantaneous( &vNewVelocity, NULL );
  255. }
  256. }
  257. SetNextThink( gpGlobals->curtime + 0.1 );
  258. }
  259. //-----------------------------------------------------------------------------
  260. // Purpose: Make a sound/effect for the removal of the energy ball, and switch to the cleanup think
  261. // Input : -
  262. //-----------------------------------------------------------------------------
  263. void CPropEnergyBall::ExplodeThink( )
  264. {
  265. // Tell the respawner to make a new one
  266. if ( GetSpawner() )
  267. {
  268. GetSpawner()->RespawnBallPostExplosion();
  269. }
  270. //Destruction effect
  271. CBroadcastRecipientFilter filter2;
  272. CEffectData data;
  273. data.m_vOrigin = GetAbsOrigin();
  274. DispatchEffect( "ManhackSparks", data );
  275. EmitSound( "EnergyBall.Explosion" );
  276. // Turn us off and wait because we need our trails to finish up properly
  277. SetAbsVelocity( vec3_origin );
  278. SetMoveType( MOVETYPE_NONE );
  279. AddSolidFlags( FSOLID_NOT_SOLID );
  280. SetEmitState( false );
  281. SetContextThink( &CPropCombineBall::SUB_Remove, gpGlobals->curtime + 0.5f, "RemoveContext" );
  282. StopLoopingSounds();
  283. }
  284. void CPropEnergyBall::StartTouch( CBaseEntity *pOther )
  285. {
  286. Assert( pOther );
  287. if( CPhysicsShadowClone::IsShadowClone( pOther ) )
  288. {
  289. CBaseEntity *pCloned = ((CPhysicsShadowClone *)pOther)->GetClonedEntity();
  290. if( pCloned )
  291. pOther = pCloned;
  292. }
  293. // Kill the player on hit.
  294. if ( pOther->IsPlayer() )
  295. {
  296. CTakeDamageInfo info( this, GetOwnerEntity(), GetAbsVelocity(), GetAbsOrigin(), 1500.0f, DMG_DISSOLVE );
  297. pOther->OnTakeDamage( info );
  298. // Destruct when we hit the player
  299. SetContextThink( &CPropCombineBall::ExplodeThink, gpGlobals->curtime, "ExplodeTimerContext" );
  300. }
  301. CProp_Portal* pPortal = dynamic_cast<CProp_Portal*>(pOther);
  302. // If toucher is a prop portal
  303. if ( pPortal )
  304. {
  305. // Record the touched portal for locking collision movements.
  306. // The forward direction we want to follow is the forward vector of the portal we've touched most recently
  307. m_hTouchedPortal = pPortal;
  308. // record that we touched this portal
  309. if ( pPortal->m_bIsPortal2 == false )
  310. {
  311. m_bTouchingPortal1 = true;
  312. }
  313. else //if ( pPortal->m_bIsPortal2 == true )
  314. {
  315. m_bTouchingPortal2 = true;
  316. }
  317. }
  318. BaseClass::StartTouch( pOther );
  319. }
  320. void CPropEnergyBall::EndTouch( CBaseEntity *pOther )
  321. {
  322. CProp_Portal* pPortal = dynamic_cast<CProp_Portal*>(pOther);
  323. if ( pPortal )
  324. {
  325. // We are no longer touching this portal
  326. if ( pPortal->m_bIsPortal2 == false )
  327. {
  328. m_bTouchingPortal1 = false;
  329. }
  330. else //if ( pPortal->m_bIsPortal2 == true )
  331. {
  332. m_bTouchingPortal2 = false;
  333. }
  334. }
  335. BaseClass::EndTouch( pOther );
  336. }
  337. class CEnergyBallLauncher : public CPointCombineBallLauncher
  338. {
  339. public:
  340. DECLARE_CLASS( CEnergyBallLauncher, CPointCombineBallLauncher );
  341. DECLARE_DATADESC();
  342. virtual void SpawnBall();
  343. virtual void Precache();
  344. virtual void Spawn();
  345. private:
  346. float m_fBallLifetime;
  347. float m_fMinBallLifeAfterPortal;
  348. COutputEvent m_OnPostSpawnBall;
  349. };
  350. LINK_ENTITY_TO_CLASS( point_energy_ball_launcher, CEnergyBallLauncher );
  351. BEGIN_DATADESC( CEnergyBallLauncher )
  352. DEFINE_KEYFIELD( m_fBallLifetime, FIELD_FLOAT, "BallLifetime" ),
  353. DEFINE_KEYFIELD( m_fMinBallLifeAfterPortal, FIELD_FLOAT, "MinLifeAfterPortal" ),
  354. DEFINE_OUTPUT ( m_OnPostSpawnBall, "OnPostSpawnBall" ),
  355. END_DATADESC()
  356. void CEnergyBallLauncher::Precache()
  357. {
  358. BaseClass::Precache();
  359. UTIL_PrecacheDecal( IMPACT_DECAL_NAME, false );
  360. }
  361. void CEnergyBallLauncher::Spawn()
  362. {
  363. Precache();
  364. BaseClass::Spawn();
  365. }
  366. void CEnergyBallLauncher::SpawnBall()
  367. {
  368. CPropEnergyBall *pBall = static_cast<CPropEnergyBall*>( CreateEntityByName( "prop_energy_ball" ) );
  369. if ( pBall == NULL )
  370. return;
  371. pBall->SetRadius( m_flBallRadius );
  372. Vector vecAbsOrigin = GetAbsOrigin();
  373. Vector zaxis;
  374. pBall->SetAbsOrigin( vecAbsOrigin );
  375. pBall->SetSpawner( this );
  376. pBall->SetSpeed( m_flMaxSpeed );
  377. float flSpeed = m_flMaxSpeed;
  378. Vector vDirection;
  379. QAngle qAngle = GetAbsAngles();
  380. AngleVectors( qAngle, &vDirection, NULL, NULL );
  381. vDirection *= flSpeed;
  382. pBall->SetAbsVelocity( vDirection );
  383. DispatchSpawn(pBall);
  384. pBall->Activate();
  385. pBall->SetState( CPropCombineBall::STATE_LAUNCHED );
  386. pBall->SetCollisionGroup( COLLISION_GROUP_PROJECTILE );
  387. pBall->m_fMinLifeAfterPortal = m_fMinBallLifeAfterPortal;
  388. // Additional setup of the physics object for energy ball uses
  389. IPhysicsObject *pBallObj = pBall->VPhysicsGetObject();
  390. if ( pBallObj )
  391. {
  392. // Make sure we dont use air drag
  393. pBallObj->EnableDrag( false );
  394. // Remove damping
  395. float speed, rot;
  396. speed = rot = 0.0f;
  397. pBallObj->SetDamping( &speed, &rot );
  398. // HUGE rotational inertia, don't allow the ball to have any spin
  399. Vector vInertia( 1e30, 1e30, 1e30 );
  400. pBallObj->SetInertia( vInertia );
  401. // Low mass to let it bounce off of obstructions for certain puzzles.
  402. pBallObj->SetMass( 1.0f );
  403. }
  404. // Only expire if the lifetme field is positive
  405. if ( m_fBallLifetime >=0 )
  406. {
  407. pBall->StartLifetime( m_fBallLifetime );
  408. pBall->m_bIsInfiniteLife = false;
  409. }
  410. else
  411. {
  412. pBall->m_bIsInfiniteLife = true;
  413. }
  414. // Think function, used to update time till death and avoid sleeping
  415. pBall->SetNextThink ( gpGlobals->curtime + 0.1f );
  416. EmitSound( "EnergyBall.Launch" );
  417. m_OnPostSpawnBall.FireOutput( this, this );
  418. }
  419. static void fire_energy_ball_f( void )
  420. {
  421. if( sv_cheats->GetBool() == false ) //heavy handed version since setting the concommand with FCVAR_CHEATS isn't working like I thought
  422. return;
  423. CBasePlayer *pPlayer = (CBasePlayer *)UTIL_GetCommandClient();
  424. Vector ptEyes, vForward;
  425. ptEyes = pPlayer->EyePosition();
  426. pPlayer->EyeVectors( &vForward );
  427. {
  428. CPropEnergyBall *pBall = static_cast<CPropEnergyBall*>( CreateEntityByName( "prop_energy_ball" ) );
  429. if ( pBall == NULL )
  430. return;
  431. pBall->SetRadius( 12.0f );
  432. pBall->SetAbsOrigin( ptEyes + (vForward * 50.0f) );
  433. pBall->SetSpawner( NULL );
  434. pBall->SetSpeed( 400.0f );
  435. pBall->SetAbsVelocity( vForward * 400.0f );
  436. DispatchSpawn(pBall);
  437. pBall->Activate();
  438. pBall->SetState( CPropCombineBall::STATE_LAUNCHED );
  439. pBall->SetCollisionGroup( COLLISION_GROUP_PROJECTILE );
  440. pBall->m_fMinLifeAfterPortal = 5.0f;
  441. // Additional setup of the physics object for energy ball uses
  442. IPhysicsObject *pBallObj = pBall->VPhysicsGetObject();
  443. if ( pBallObj )
  444. {
  445. // Make sure we dont use air drag
  446. pBallObj->EnableDrag( false );
  447. // Remove damping
  448. float speed, rot;
  449. speed = rot = 0.0f;
  450. pBallObj->SetDamping( &speed, &rot );
  451. // HUGE rotational inertia, don't allow the ball to have any spin
  452. Vector vInertia( 1e30, 1e30, 1e30 );
  453. pBallObj->SetInertia( vInertia );
  454. // Low mass to let it bounce off of obstructions for certain puzzles.
  455. pBallObj->SetMass( 1.0f );
  456. }
  457. pBall->StartLifetime( 10.0f );
  458. pBall->m_bIsInfiniteLife = false;
  459. // Think function, used to update time till death and avoid sleeping
  460. pBall->SetNextThink ( gpGlobals->curtime + 0.1f );
  461. }
  462. }
  463. ConCommand fire_energy_ball( "fire_energy_ball", fire_energy_ball_f, "Fires a test energy ball out of your face", FCVAR_CHEAT );