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.

1463 lines
37 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. //---------------------------------------------------------
  9. //---------------------------------------------------------
  10. #include "cbase.h"
  11. #include "decals.h"
  12. #include "fire.h"
  13. #include "entitylist.h"
  14. #include "basecombatcharacter.h"
  15. #include "ndebugoverlay.h"
  16. #include "engine/IEngineSound.h"
  17. #include "ispatialpartition.h"
  18. #include "collisionutils.h"
  19. #include "tier0/vprof.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. /********************************************************************
  23. NOTE: if you are looking at this file becase you would like flares
  24. to be considered as fires (and thereby trigger gas traps), be aware
  25. that the env_flare class is actually found in weapon_flaregun.cpp
  26. and is really a repurposed piece of ammunition. (env_flare isn't the
  27. rod-like safety flare prop, but rather the bit of flame on the end.)
  28. You will have some difficulty making it work here, because CFlare
  29. does not inherit from CFire and will thus not be enumerated by
  30. CFireSphere::EnumElement(). In order to have flares be detected and
  31. used by this system, you will need to promote certain member functions
  32. of CFire into an interface class from which both CFire and CFlare
  33. inherit. You will also need to modify CFireSphere::EnumElement so that
  34. it properly disambiguates between fires and flares.
  35. For some partial work towards this end, see changelist 192474.
  36. ********************************************************************/
  37. #define FIRE_HEIGHT 256.0f
  38. #define FIRE_SCALE_FROM_SIZE(firesize) (firesize * (1/FIRE_HEIGHT))
  39. #define FIRE_MAX_GROUND_OFFSET 24.0f //(2 feet)
  40. #define DEFAULT_ATTACK_TIME 4.0f
  41. #define DEFAULT_DECAY_TIME 8.0f
  42. // UNDONE: This shouldn't be constant but depend on specific fire
  43. #define FIRE_WIDTH 128
  44. #define FIRE_MINS Vector(-20,-20,0 ) // Sould be FIRE_WIDTH in size
  45. #define FIRE_MAXS Vector( 20, 20,20) // Sould be FIRE_WIDTH in size
  46. #define FIRE_SPREAD_DAMAGE_MULTIPLIER 2.0
  47. #define FIRE_MAX_HEAT_LEVEL 64.0f
  48. #define FIRE_NORMAL_ATTACK_TIME 20.0f
  49. #define FIRE_THINK_INTERVAL 0.1
  50. ConVar fire_maxabsorb( "fire_maxabsorb", "50" );
  51. ConVar fire_absorbrate( "fire_absorbrate", "3" );
  52. ConVar fire_extscale("fire_extscale", "12");
  53. ConVar fire_extabsorb("fire_extabsorb", "5");
  54. ConVar fire_heatscale( "fire_heatscale", "1.0" );
  55. ConVar fire_incomingheatscale( "fire_incomingheatscale", "0.1" );
  56. ConVar fire_dmgscale( "fire_dmgscale", "0.1" );
  57. ConVar fire_dmgbase( "fire_dmgbase", "1" );
  58. ConVar fire_growthrate( "fire_growthrate", "1.0" );
  59. ConVar fire_dmginterval( "fire_dmginterval", "1.0" );
  60. #define VPROF_FIRE(s) VPROF( s )
  61. class CFire : public CBaseEntity
  62. {
  63. public:
  64. DECLARE_CLASS( CFire, CBaseEntity );
  65. int DrawDebugTextOverlays(void);
  66. CFire( void );
  67. virtual void UpdateOnRemove( void );
  68. void Precache( void );
  69. void Init( const Vector &position, float scale, float attackTime, float fuel, int flags, int fireType );
  70. bool GoOut();
  71. void BurnThink();
  72. void GoOutThink();
  73. void GoOutInSeconds( float seconds );
  74. void SetOwner( CBaseEntity *hOwner ) { m_hOwner = hOwner; }
  75. void Scale( float end, float time );
  76. void AddHeat( float heat, bool selfHeat = false );
  77. int OnTakeDamage( const CTakeDamageInfo &info );
  78. bool IsBurning( void ) const;
  79. bool GetFireDimensions( Vector *pFireMins, Vector *pFireMaxs );
  80. void Extinguish( float heat );
  81. void DestroyEffect();
  82. virtual void Update( float simTime );
  83. void Spawn( void );
  84. void Activate( void );
  85. void StartFire( void );
  86. void Start();
  87. void SetToOutSize()
  88. {
  89. UTIL_SetSize( this, Vector(-8,-8,0), Vector(8,8,8) );
  90. }
  91. float GetHeatLevel() { return m_flHeatLevel; }
  92. virtual int UpdateTransmitState();
  93. void DrawDebugGeometryOverlays(void)
  94. {
  95. if (m_debugOverlays & OVERLAY_BBOX_BIT)
  96. {
  97. if ( m_lastDamage > gpGlobals->curtime && m_flHeatAbsorb > 0 )
  98. {
  99. NDebugOverlay::EntityBounds(this, 88, 255, 128, 0 ,0);
  100. char tempstr[512];
  101. Q_snprintf( tempstr, sizeof(tempstr), "Heat: %.1f", m_flHeatAbsorb );
  102. EntityText(1,tempstr, 0);
  103. }
  104. else if ( !IsBurning() )
  105. {
  106. NDebugOverlay::EntityBounds(this, 88, 88, 128, 0 ,0);
  107. }
  108. if ( IsBurning() )
  109. {
  110. Vector mins, maxs;
  111. if ( GetFireDimensions( &mins, &maxs ) )
  112. {
  113. NDebugOverlay::Box(GetAbsOrigin(), mins, maxs, 128, 0, 0, 10, 0);
  114. }
  115. }
  116. }
  117. BaseClass::DrawDebugGeometryOverlays();
  118. }
  119. void Disable();
  120. //Inputs
  121. void InputStartFire( inputdata_t &inputdata );
  122. void InputExtinguish( inputdata_t &inputdata );
  123. void InputExtinguishTemporary( inputdata_t &inputdata );
  124. void InputEnable( inputdata_t &inputdata );
  125. void InputDisable( inputdata_t &inputdata );
  126. protected:
  127. void Spread( void );
  128. void SpawnEffect( fireType_e type, float scale );
  129. CHandle<CBaseFire> m_hEffect;
  130. EHANDLE m_hOwner;
  131. int m_nFireType;
  132. float m_flFuel;
  133. float m_flDamageTime;
  134. float m_lastDamage;
  135. float m_flFireSize; // size of the fire in world units
  136. float m_flHeatLevel; // Used as a "health" for the fire. > 0 means the fire is burning
  137. float m_flHeatAbsorb; // This much heat must be "absorbed" before it gets transferred to the flame size
  138. float m_flDamageScale;
  139. float m_flMaxHeat;
  140. float m_flLastHeatLevel;
  141. //NOTENOTE: Lifetime is an expression of the sum total of these amounts plus the global time when started
  142. float m_flAttackTime; //Amount of time to scale up
  143. bool m_bEnabled;
  144. bool m_bStartDisabled;
  145. bool m_bDidActivate;
  146. COutputEvent m_OnIgnited;
  147. COutputEvent m_OnExtinguished;
  148. DECLARE_DATADESC();
  149. };
  150. class CFireSphere : public IPartitionEnumerator
  151. {
  152. public:
  153. CFireSphere( CFire **pList, int listMax, bool onlyActiveFires, const Vector &origin, float radius );
  154. // This gets called by the enumeration methods with each element
  155. // that passes the test.
  156. virtual IterationRetval_t EnumElement( IHandleEntity *pHandleEntity );
  157. int GetCount() { return m_count; }
  158. bool AddToList( CFire *pEntity );
  159. private:
  160. Vector m_origin;
  161. float m_radiusSqr;
  162. CFire **m_pList;
  163. int m_listMax;
  164. int m_count;
  165. bool m_onlyActiveFires;
  166. };
  167. CFireSphere::CFireSphere( CFire **pList, int listMax, bool onlyActiveFires, const Vector &origin, float radius )
  168. {
  169. m_pList = pList;
  170. m_listMax = listMax;
  171. m_count = 0;
  172. m_onlyActiveFires = onlyActiveFires;
  173. m_origin = origin;
  174. m_radiusSqr = radius * radius;
  175. }
  176. bool CFireSphere::AddToList( CFire *pFire )
  177. {
  178. if ( m_count >= m_listMax )
  179. return false;
  180. m_pList[m_count] = pFire;
  181. m_count++;
  182. return true;
  183. }
  184. IterationRetval_t CFireSphere::EnumElement( IHandleEntity *pHandleEntity )
  185. {
  186. CBaseEntity *pEntity = gEntList.GetBaseEntity( pHandleEntity->GetRefEHandle() );
  187. if ( pEntity )
  188. {
  189. // UNDONE: Measure which of these is faster
  190. // CFire *pFire = dynamic_cast<CFire *>(pEntity);
  191. if ( !FClassnameIs( pEntity, "env_fire" ) )
  192. return ITERATION_CONTINUE;
  193. CFire *pFire = static_cast<CFire *>(pEntity);
  194. if ( pFire )
  195. {
  196. if ( !m_onlyActiveFires || pFire->IsBurning() )
  197. {
  198. if ( (m_origin - pFire->GetAbsOrigin()).LengthSqr() < m_radiusSqr )
  199. {
  200. if ( !AddToList( pFire ) )
  201. return ITERATION_STOP;
  202. }
  203. }
  204. }
  205. }
  206. return ITERATION_CONTINUE;
  207. }
  208. int FireSystem_GetFiresInSphere( CFire **pList, int listMax, bool onlyActiveFires, const Vector &origin, float radius )
  209. {
  210. CFireSphere sphereEnum( pList, listMax, onlyActiveFires, origin, radius );
  211. ::partition->EnumerateElementsInSphere( PARTITION_ENGINE_NON_STATIC_EDICTS, origin, radius, false, &sphereEnum );
  212. return sphereEnum.GetCount();
  213. }
  214. bool FireSystem_IsValidFirePosition( const Vector &position, float testRadius )
  215. {
  216. CFire *pList[1];
  217. int count = FireSystem_GetFiresInSphere( pList, ARRAYSIZE(pList), true, position, testRadius );
  218. if ( count > 0 )
  219. return false;
  220. return true;
  221. }
  222. //------------------------------------------------------------------------------
  223. // Purpose :
  224. // Input :
  225. // Output :
  226. //------------------------------------------------------------------------------
  227. bool FireSystem_IsFireInWall( Vector &position, fireType_e type )
  228. {
  229. // Don't check natural fire against walls
  230. if (type == FIRE_NATURAL)
  231. return false;
  232. trace_t tr;
  233. UTIL_TraceHull( position, position+Vector(0,0,0.1), FIRE_MINS,FIRE_MAXS,MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );
  234. if (tr.fraction != 1.0 || tr.startsolid)
  235. {
  236. //NDebugOverlay::Box(position,FIRE_MINS,FIRE_MAXS,255,0,0,50,10);
  237. return true;
  238. }
  239. //NDebugOverlay::Box(position,FIRE_MINS,FIRE_MAXS,0,255,0,50,10);
  240. return false;
  241. }
  242. //-----------------------------------------------------------------------------
  243. // Purpose: Determines whether or not a new fire may be placed at a given location
  244. // Input : &position - where we are trying to put the new fire
  245. // separationRadius - the maximum distance fires must be apart from one another
  246. // Output : Returns true on success, false on failure.
  247. //-----------------------------------------------------------------------------
  248. bool FireSystem_CanAddFire( Vector *position, float separationRadius, fireType_e type, int flags )
  249. {
  250. //See if we found a fire inside the sphere
  251. if ( !FireSystem_IsValidFirePosition( *position, separationRadius ) )
  252. return false;
  253. // Unless our fire is floating, make sure were not too high
  254. if (!(flags & SF_FIRE_DONT_DROP))
  255. {
  256. trace_t tr;
  257. Vector startpos = *position;
  258. Vector endpos = *position;
  259. startpos[2] += 1;
  260. endpos[2] -= FIRE_MAX_GROUND_OFFSET;
  261. UTIL_TraceLine( startpos, endpos, MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );
  262. //See if we're floating too high
  263. if ( ( tr.allsolid ) || ( tr.startsolid) || ( tr.fraction == 1.0f ) )
  264. {
  265. return false;
  266. }
  267. //TODO: If we've hit an entity here, start it on fire
  268. CBaseEntity *pEntity = tr.m_pEnt;
  269. if ( ENTINDEX( pEntity->edict() ) != 0 )
  270. {
  271. return false;
  272. }
  273. }
  274. // Check if fire is in a wall, if so try shifting around a bit
  275. if (FireSystem_IsFireInWall( *position, type ))
  276. {
  277. Vector vTestPos = *position;
  278. vTestPos.x += 10;
  279. if (FireSystem_IsValidFirePosition( vTestPos, separationRadius ) && !FireSystem_IsFireInWall( vTestPos, type ))
  280. {
  281. *position = vTestPos;
  282. return true;
  283. }
  284. vTestPos.y += 10;
  285. if (FireSystem_IsValidFirePosition( vTestPos, separationRadius ) && !FireSystem_IsFireInWall( vTestPos, type ))
  286. {
  287. *position = vTestPos;
  288. return true;
  289. }
  290. vTestPos.y -= 20;
  291. if (FireSystem_IsValidFirePosition( vTestPos, separationRadius ) && !FireSystem_IsFireInWall( vTestPos, type ))
  292. {
  293. *position = vTestPos;
  294. return true;
  295. }
  296. vTestPos.x -= 20;
  297. if (FireSystem_IsValidFirePosition( vTestPos, separationRadius ) && !FireSystem_IsFireInWall( vTestPos, type ))
  298. {
  299. *position = vTestPos;
  300. return true;
  301. }
  302. return false;
  303. }
  304. //Able to add here
  305. return true;
  306. }
  307. //-----------------------------------------------------------------------------
  308. // Purpose: Starts a fire at a specified location
  309. // Input : &position - position to start the fire at
  310. // flags - any special modifiers
  311. //-----------------------------------------------------------------------------
  312. bool FireSystem_StartFire( const Vector &position, float fireHeight, float attack, float fuel, int flags, CBaseEntity *owner, fireType_e type )
  313. {
  314. VPROF_FIRE( "FireSystem_StartFire1" );
  315. Vector testPos = position;
  316. //Must be okay to add fire here
  317. if ( FireSystem_CanAddFire( &testPos, 16.0f, type, flags ) == false )
  318. {
  319. CFire *pFires[16];
  320. int fireCount = FireSystem_GetFiresInSphere( pFires, ARRAYSIZE(pFires), true, position, 16.0f );
  321. for ( int i = 0; i < fireCount; i++ )
  322. {
  323. // add to this fire
  324. pFires[i]->AddHeat( fireHeight, false );
  325. }
  326. return false;
  327. }
  328. //Create a new fire entity
  329. CFire *fire = (CFire *) CreateEntityByName( "env_fire" );
  330. if ( fire == NULL )
  331. return false;
  332. //Spawn the fire
  333. // Fires not placed by a designer should be cleaned up automatically (not catch fire again)
  334. fire->AddSpawnFlags( SF_FIRE_DIE_PERMANENT );
  335. fire->Spawn();
  336. fire->Init( testPos, fireHeight, attack, fuel, flags, type );
  337. fire->Start();
  338. fire->SetOwner( owner );
  339. return true;
  340. }
  341. //-----------------------------------------------------------------------------
  342. // Purpose: Starts a fire on a specified model.
  343. // Input : pEntity - The model entity to catch on fire.
  344. // fireHeight -
  345. // attack -
  346. // fuel -
  347. // flags -
  348. // owner -
  349. // type -
  350. // Output : Returns true on success, false on failure.
  351. //-----------------------------------------------------------------------------
  352. bool FireSystem_StartFire( CBaseAnimating *pEntity, float fireHeight, float attack, float fuel, int flags, CBaseEntity *owner, fireType_e type )
  353. {
  354. VPROF_FIRE( "FireSystem_StartFire2" );
  355. Vector position = pEntity->GetAbsOrigin();
  356. Vector testPos = position;
  357. // Make sure its a valid position for fire (not in a wall, etc)
  358. if ( FireSystem_CanAddFire( &testPos, 16.0f, type, flags ) == false )
  359. {
  360. // Contribute heat to all fires within 16 units of this fire.
  361. CFire *pFires[16];
  362. int fireCount = FireSystem_GetFiresInSphere( pFires, ARRAYSIZE(pFires), true, position, 16.0f );
  363. for ( int i = 0; i < fireCount; i++ )
  364. {
  365. pFires[i]->AddHeat( fireHeight, false );
  366. }
  367. return false;
  368. }
  369. // Create a new fire entity
  370. CFire *fire = (CFire *) CreateEntityByName( "env_fire" );
  371. if ( fire == NULL )
  372. {
  373. return false;
  374. }
  375. // Spawn the fire.
  376. // Fires not placed by a designer should be cleaned up automatically (not catch fire again).
  377. fire->AddSpawnFlags( SF_FIRE_DIE_PERMANENT );
  378. fire->Spawn();
  379. fire->Init( testPos, fireHeight, attack, fuel, flags, type );
  380. fire->Start();
  381. fire->SetOwner( owner );
  382. return true;
  383. }
  384. void FireSystem_ExtinguishInRadius( const Vector &origin, float radius, float rate )
  385. {
  386. // UNDONE: pass this instead of percent
  387. float heat = (1-rate) * fire_extscale.GetFloat();
  388. CFire *pFires[32];
  389. int fireCount = FireSystem_GetFiresInSphere( pFires, ARRAYSIZE(pFires), false, origin, radius );
  390. for ( int i = 0; i < fireCount; i++ )
  391. {
  392. pFires[i]->Extinguish( heat );
  393. }
  394. }
  395. //-----------------------------------------------------------------------------
  396. // Purpose:
  397. // Input : &origin -
  398. // radius -
  399. // heat -
  400. //-----------------------------------------------------------------------------
  401. void FireSystem_AddHeatInRadius( const Vector &origin, float radius, float heat )
  402. {
  403. VPROF_FIRE( "FireSystem_AddHeatInRadius" );
  404. CFire *pFires[32];
  405. int fireCount = FireSystem_GetFiresInSphere( pFires, ARRAYSIZE(pFires), false, origin, radius );
  406. for ( int i = 0; i < fireCount; i++ )
  407. {
  408. pFires[i]->AddHeat( heat );
  409. }
  410. }
  411. //-----------------------------------------------------------------------------
  412. bool FireSystem_GetFireDamageDimensions( CBaseEntity *pEntity, Vector *pFireMins, Vector *pFireMaxs )
  413. {
  414. CFire *pFire = dynamic_cast<CFire *>(pEntity);
  415. if ( pFire && pFire->GetFireDimensions( pFireMins, pFireMaxs ) )
  416. {
  417. *pFireMins /= FIRE_SPREAD_DAMAGE_MULTIPLIER;
  418. *pFireMaxs /= FIRE_SPREAD_DAMAGE_MULTIPLIER;
  419. return true;
  420. }
  421. pFireMins->Init();
  422. pFireMaxs->Init();
  423. return false;
  424. }
  425. //==================================================
  426. // CFire
  427. //==================================================
  428. BEGIN_DATADESC( CFire )
  429. DEFINE_FIELD( m_hEffect, FIELD_EHANDLE ),
  430. DEFINE_FIELD( m_hOwner, FIELD_EHANDLE ),
  431. DEFINE_KEYFIELD( m_nFireType, FIELD_INTEGER, "firetype" ),
  432. DEFINE_FIELD( m_flFuel, FIELD_FLOAT ),
  433. DEFINE_FIELD( m_flDamageTime, FIELD_TIME ),
  434. DEFINE_FIELD( m_lastDamage, FIELD_TIME ),
  435. DEFINE_KEYFIELD( m_flFireSize, FIELD_FLOAT, "firesize" ),
  436. DEFINE_KEYFIELD( m_flHeatLevel, FIELD_FLOAT, "ignitionpoint" ),
  437. DEFINE_FIELD( m_flHeatAbsorb, FIELD_FLOAT ),
  438. DEFINE_KEYFIELD( m_flDamageScale,FIELD_FLOAT, "damagescale" ),
  439. DEFINE_FIELD( m_flMaxHeat, FIELD_FLOAT ),
  440. //DEFINE_FIELD( m_flLastHeatLevel, FIELD_FLOAT ),
  441. DEFINE_KEYFIELD( m_flAttackTime, FIELD_FLOAT, "fireattack" ),
  442. DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
  443. DEFINE_KEYFIELD( m_bStartDisabled, FIELD_BOOLEAN, "StartDisabled" ),
  444. DEFINE_FIELD( m_bDidActivate, FIELD_BOOLEAN ),
  445. DEFINE_FUNCTION( BurnThink ),
  446. DEFINE_FUNCTION( GoOutThink ),
  447. DEFINE_INPUTFUNC( FIELD_VOID, "StartFire", InputStartFire ),
  448. DEFINE_INPUTFUNC( FIELD_FLOAT, "Extinguish", InputExtinguish ),
  449. DEFINE_INPUTFUNC( FIELD_FLOAT, "ExtinguishTemporary", InputExtinguishTemporary ),
  450. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  451. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  452. DEFINE_OUTPUT( m_OnIgnited, "OnIgnited" ),
  453. DEFINE_OUTPUT( m_OnExtinguished, "OnExtinguished" ),
  454. END_DATADESC()
  455. LINK_ENTITY_TO_CLASS( env_fire, CFire );
  456. //==================================================
  457. // CFire
  458. //==================================================
  459. CFire::CFire( void )
  460. {
  461. m_flFuel = 0.0f;
  462. m_flAttackTime = 0.0f;
  463. m_flDamageTime = 0.0f;
  464. m_lastDamage = 0;
  465. m_nFireType = FIRE_NATURAL;
  466. //Spreading
  467. m_flHeatAbsorb = 8.0f;
  468. m_flHeatLevel = 0;
  469. // Must be in the constructor!
  470. AddEFlags( EFL_USE_PARTITION_WHEN_NOT_SOLID );
  471. }
  472. //-----------------------------------------------------------------------------
  473. // UpdateOnRemove
  474. //-----------------------------------------------------------------------------
  475. void CFire::UpdateOnRemove( void )
  476. {
  477. //Stop any looping sounds that might be playing
  478. StopSound( "Fire.Plasma" );
  479. DestroyEffect();
  480. // Chain at end to mimic destructor unwind order
  481. BaseClass::UpdateOnRemove();
  482. }
  483. //-----------------------------------------------------------------------------
  484. // Purpose:
  485. //-----------------------------------------------------------------------------
  486. void CFire::Precache( void )
  487. {
  488. if ( m_nFireType == FIRE_NATURAL )
  489. {
  490. UTIL_PrecacheOther("_firesmoke");
  491. if ( m_spawnflags & SF_FIRE_SMOKELESS )
  492. {
  493. PrecacheParticleSystem( "env_fire_tiny" );
  494. PrecacheParticleSystem( "env_fire_small" );
  495. PrecacheParticleSystem( "env_fire_medium" );
  496. PrecacheParticleSystem( "env_fire_large" );
  497. }
  498. else
  499. {
  500. PrecacheParticleSystem( "env_fire_tiny_smoke" );
  501. PrecacheParticleSystem( "env_fire_small_smoke" );
  502. PrecacheParticleSystem( "env_fire_medium_smoke" );
  503. PrecacheParticleSystem( "env_fire_large_smoke" );
  504. }
  505. }
  506. if ( m_nFireType == FIRE_PLASMA )
  507. {
  508. UTIL_PrecacheOther("_plasma");
  509. }
  510. PrecacheScriptSound( "Fire.Plasma" );
  511. }
  512. //------------------------------------------------------------------------------
  513. // Purpose : Input handler for starting the fire.
  514. //------------------------------------------------------------------------------
  515. void CFire::InputStartFire( inputdata_t &inputdata )
  516. {
  517. if ( !m_bEnabled )
  518. return;
  519. StartFire();
  520. }
  521. void CFire::InputEnable( inputdata_t &inputdata )
  522. {
  523. m_bEnabled = true;
  524. }
  525. void CFire::InputDisable( inputdata_t &inputdata )
  526. {
  527. Disable();
  528. }
  529. void CFire::Disable()
  530. {
  531. m_bEnabled = false;
  532. if ( IsBurning() )
  533. {
  534. GoOut();
  535. }
  536. }
  537. //-----------------------------------------------------------------------------
  538. // Purpose:
  539. // Input : &inputdata -
  540. //-----------------------------------------------------------------------------
  541. void CFire::InputExtinguish( inputdata_t &inputdata )
  542. {
  543. m_spawnflags &= ~SF_FIRE_INFINITE;
  544. GoOutInSeconds( inputdata.value.Float() );
  545. }
  546. //-----------------------------------------------------------------------------
  547. // Purpose:
  548. // Input : &inputdata -
  549. //-----------------------------------------------------------------------------
  550. void CFire::InputExtinguishTemporary( inputdata_t &inputdata )
  551. {
  552. GoOutInSeconds( inputdata.value.Float() );
  553. }
  554. //-----------------------------------------------------------------------------
  555. // Purpose: Starts burning.
  556. //-----------------------------------------------------------------------------
  557. void CFire::StartFire( void )
  558. {
  559. if ( m_hEffect != NULL )
  560. return;
  561. // Trace down and start a fire there. Nothing fancy yet.
  562. Vector vFirePos;
  563. trace_t tr;
  564. if ( m_spawnflags & SF_FIRE_DONT_DROP )
  565. {
  566. vFirePos = GetAbsOrigin();
  567. }
  568. else
  569. {
  570. UTIL_TraceLine( GetAbsOrigin(), GetAbsOrigin() - Vector( 0, 0, 1024 ), MASK_FIRE_SOLID, this, COLLISION_GROUP_NONE, &tr );
  571. vFirePos = tr.endpos;
  572. }
  573. int spawnflags = m_spawnflags;
  574. m_spawnflags |= SF_FIRE_START_ON;
  575. Init( vFirePos, m_flFireSize, m_flAttackTime, GetHealth(), m_spawnflags, (fireType_e) m_nFireType );
  576. Start();
  577. m_spawnflags = spawnflags;
  578. }
  579. //-----------------------------------------------------------------------------
  580. // Purpose:
  581. //-----------------------------------------------------------------------------
  582. void CFire::Spawn( void )
  583. {
  584. BaseClass::Spawn();
  585. Precache();
  586. m_takedamage = DAMAGE_NO;
  587. SetSolid( SOLID_NONE );
  588. AddEffects( EF_NODRAW );
  589. SetToOutSize();
  590. // set up the ignition point
  591. m_flHeatAbsorb = m_flHeatLevel * 0.05;
  592. m_flHeatLevel = 0;
  593. Init( GetAbsOrigin(), m_flFireSize, m_flAttackTime, m_flFuel, m_spawnflags, m_nFireType );
  594. if( m_bStartDisabled )
  595. {
  596. Disable();
  597. }
  598. else
  599. {
  600. m_bEnabled = true;
  601. }
  602. }
  603. int CFire::UpdateTransmitState()
  604. {
  605. // Don't want to be FL_EDICT_DONTSEND because our fire entity may make us transmit.
  606. return SetTransmitState( FL_EDICT_ALWAYS );
  607. }
  608. //-----------------------------------------------------------------------------
  609. // Purpose:
  610. //-----------------------------------------------------------------------------
  611. void CFire::Activate( void )
  612. {
  613. BaseClass::Activate();
  614. //See if we should start active
  615. if ( !m_bDidActivate && ( m_spawnflags & SF_FIRE_START_ON ) )
  616. {
  617. m_flHeatLevel = m_flMaxHeat;
  618. StartFire();
  619. }
  620. m_bDidActivate = true;
  621. }
  622. //-----------------------------------------------------------------------------
  623. // Purpose:
  624. //-----------------------------------------------------------------------------
  625. void CFire::SpawnEffect( fireType_e type, float scale )
  626. {
  627. CBaseFire *pEffect = NULL;
  628. switch ( type )
  629. {
  630. default:
  631. case FIRE_NATURAL:
  632. {
  633. CFireSmoke *fireSmoke = (CFireSmoke *) CreateEntityByName( "_firesmoke" );
  634. fireSmoke->EnableSmoke( ( m_spawnflags & SF_FIRE_SMOKELESS )==false );
  635. fireSmoke->EnableGlow( ( m_spawnflags & SF_FIRE_NO_GLOW )==false );
  636. fireSmoke->EnableVisibleFromAbove( ( m_spawnflags & SF_FIRE_VISIBLE_FROM_ABOVE )!=false );
  637. pEffect = fireSmoke;
  638. m_nFireType = FIRE_NATURAL;
  639. m_takedamage = DAMAGE_YES;
  640. }
  641. break;
  642. case FIRE_PLASMA:
  643. {
  644. CPlasma *plasma = (CPlasma *) CreateEntityByName( "_plasma" );
  645. plasma->EnableSmoke( true );
  646. pEffect = plasma;
  647. m_nFireType = FIRE_PLASMA;
  648. m_takedamage = DAMAGE_YES;
  649. // Start burn sound
  650. EmitSound( "Fire.Plasma" );
  651. }
  652. break;
  653. }
  654. UTIL_SetOrigin( pEffect, GetAbsOrigin() );
  655. pEffect->Spawn();
  656. pEffect->SetParent( this );
  657. pEffect->Scale( m_flFireSize, m_flFireSize, 0 );
  658. //Start it going
  659. pEffect->Enable( ( m_spawnflags & SF_FIRE_START_ON ) );
  660. m_hEffect = pEffect;
  661. }
  662. //-----------------------------------------------------------------------------
  663. // Purpose: Spawn and initialize the fire
  664. // Input : &position - where the fire resides
  665. // lifetime -
  666. //-----------------------------------------------------------------------------
  667. void CFire::Init( const Vector &position, float scale, float attackTime, float fuel, int flags, int fireType )
  668. {
  669. m_flAttackTime = attackTime;
  670. m_spawnflags = flags;
  671. m_nFireType = fireType;
  672. if ( flags & SF_FIRE_INFINITE )
  673. {
  674. fuel = 0;
  675. }
  676. m_flFuel = fuel;
  677. if ( m_flFuel )
  678. {
  679. m_spawnflags |= SF_FIRE_DIE_PERMANENT;
  680. }
  681. Vector localOrigin = position;
  682. if ( GetMoveParent() )
  683. {
  684. EntityMatrix parentMatrix;
  685. parentMatrix.InitFromEntity( GetMoveParent() );
  686. localOrigin = parentMatrix.WorldToLocal( position );
  687. }
  688. UTIL_SetOrigin( this, localOrigin );
  689. SetSolid( SOLID_NONE );
  690. m_flFireSize = scale;
  691. m_flMaxHeat = FIRE_MAX_HEAT_LEVEL * FIRE_SCALE_FROM_SIZE(scale);
  692. //See if we should start on
  693. if ( m_spawnflags & SF_FIRE_START_FULL )
  694. {
  695. m_flHeatLevel = m_flMaxHeat;
  696. }
  697. m_flLastHeatLevel = 0;
  698. }
  699. void CFire::Start()
  700. {
  701. float boxWidth = (m_flFireSize * (FIRE_WIDTH/FIRE_HEIGHT))*0.5f;
  702. UTIL_SetSize(this, Vector(-boxWidth,-boxWidth,0),Vector(boxWidth,boxWidth,m_flFireSize));
  703. //Spawn the client-side effect
  704. SpawnEffect( (fireType_e)m_nFireType, FIRE_SCALE_FROM_SIZE(m_flFireSize) );
  705. m_OnIgnited.FireOutput( this, this );
  706. SetThink( &CFire::BurnThink );
  707. m_flDamageTime = 0;
  708. // think right now
  709. BurnThink();
  710. }
  711. //-----------------------------------------------------------------------------
  712. // Purpose: Determines whether or not the fire is still active
  713. // Output : Returns true on success, false on failure.
  714. //-----------------------------------------------------------------------------
  715. bool CFire::IsBurning( void ) const
  716. {
  717. if ( m_flHeatLevel > 0 )
  718. return true;
  719. return false;
  720. }
  721. //-----------------------------------------------------------------------------
  722. // Purpose: Get the damage box of the fire
  723. //-----------------------------------------------------------------------------
  724. bool CFire::GetFireDimensions( Vector *pFireMins, Vector *pFireMaxs )
  725. {
  726. if ( m_flHeatLevel <= 0 )
  727. {
  728. pFireMins->Init();
  729. pFireMaxs->Init();
  730. return false;
  731. }
  732. float scale = m_flHeatLevel / m_flMaxHeat;
  733. float damageRadius = scale * m_flFireSize * FIRE_WIDTH / FIRE_HEIGHT * 0.5;
  734. damageRadius *= FIRE_SPREAD_DAMAGE_MULTIPLIER; //FIXME: Trying slightly larger radius for burning
  735. if ( damageRadius < 16 )
  736. {
  737. damageRadius = 16;
  738. }
  739. pFireMins->Init(-damageRadius,-damageRadius,0);
  740. pFireMaxs->Init(damageRadius,damageRadius,m_flFireSize*scale);
  741. return true;
  742. }
  743. //-----------------------------------------------------------------------------
  744. // Purpose: Update the fire and its children
  745. //-----------------------------------------------------------------------------
  746. void CFire::Update( float simTime )
  747. {
  748. VPROF_FIRE( "CFire::Update" );
  749. if ( m_flFuel != 0 )
  750. {
  751. m_flFuel -= simTime;
  752. if ( m_flFuel <= 0 )
  753. {
  754. GoOutInSeconds( 1 );
  755. return;
  756. }
  757. }
  758. float strength = m_flHeatLevel / FIRE_MAX_HEAT_LEVEL;
  759. if ( m_flHeatLevel != m_flLastHeatLevel )
  760. {
  761. m_flLastHeatLevel = m_flHeatLevel;
  762. // Make the effect the appropriate size given the heat level
  763. m_hEffect->Scale( strength, 0.5f );
  764. }
  765. // add heat to myself (grow)
  766. float addedHeat = (m_flAttackTime > 0) ? m_flMaxHeat / m_flAttackTime : m_flMaxHeat;
  767. addedHeat *= simTime * fire_growthrate.GetFloat();
  768. AddHeat( addedHeat, true );
  769. // add heat to nearby fires
  770. float outputHeat = strength * m_flHeatLevel;
  771. Vector fireMins;
  772. Vector fireMaxs;
  773. Vector fireEntityDamageMins;
  774. Vector fireEntityDamageMaxs;
  775. GetFireDimensions( &fireMins, &fireMaxs );
  776. if ( FIRE_SPREAD_DAMAGE_MULTIPLIER != 1.0 ) // if set to 1.0, optimizer will remove this code
  777. {
  778. fireEntityDamageMins = fireMins / FIRE_SPREAD_DAMAGE_MULTIPLIER;
  779. fireEntityDamageMaxs = fireMaxs / FIRE_SPREAD_DAMAGE_MULTIPLIER;
  780. }
  781. //NDebugOverlay::Box( GetAbsOrigin(), fireMins, fireMaxs, 255, 255, 255, 0, fire_dmginterval.GetFloat() );
  782. fireMins += GetAbsOrigin();
  783. fireMaxs += GetAbsOrigin();
  784. if ( FIRE_SPREAD_DAMAGE_MULTIPLIER != 1.0 )
  785. {
  786. fireEntityDamageMins += GetAbsOrigin();
  787. fireEntityDamageMaxs += GetAbsOrigin();
  788. }
  789. CBaseEntity *pNearby[256];
  790. CFire *pFires[16];
  791. int nearbyCount = UTIL_EntitiesInBox( pNearby, ARRAYSIZE(pNearby), fireMins, fireMaxs, 0 );
  792. int fireCount = 0;
  793. int i;
  794. // is it time to do damage?
  795. bool damage = false;
  796. int outputDamage = 0;
  797. if ( m_flDamageTime <= gpGlobals->curtime )
  798. {
  799. m_flDamageTime = gpGlobals->curtime + fire_dmginterval.GetFloat();
  800. outputDamage = (fire_dmgbase.GetFloat() + outputHeat * fire_dmgscale.GetFloat() * m_flDamageScale) * fire_dmginterval.GetFloat();
  801. if ( outputDamage )
  802. {
  803. damage = true;
  804. }
  805. }
  806. int damageFlags = (m_nFireType == FIRE_NATURAL) ? DMG_BURN : DMG_PLASMA;
  807. for ( i = 0; i < nearbyCount; i++ )
  808. {
  809. CBaseEntity *pOther = pNearby[i];
  810. if ( pOther == this )
  811. {
  812. continue;
  813. }
  814. else if ( FClassnameIs( pOther, "env_fire" ) )
  815. {
  816. if ( fireCount < ARRAYSIZE(pFires) )
  817. {
  818. pFires[fireCount] = (CFire *)pOther;
  819. fireCount++;
  820. }
  821. continue;
  822. }
  823. else if ( pOther->m_takedamage == DAMAGE_NO )
  824. {
  825. pNearby[i] = NULL;
  826. }
  827. else if ( damage )
  828. {
  829. bool bDoDamage;
  830. if ( FIRE_SPREAD_DAMAGE_MULTIPLIER != 1.0 && !pOther->IsPlayer() ) // if set to 1.0, optimizer will remove this code
  831. {
  832. Vector otherMins, otherMaxs;
  833. pOther->CollisionProp()->WorldSpaceAABB( &otherMins, &otherMaxs );
  834. bDoDamage = IsBoxIntersectingBox( otherMins, otherMaxs,
  835. fireEntityDamageMins, fireEntityDamageMaxs );
  836. }
  837. else
  838. bDoDamage = true;
  839. if ( bDoDamage )
  840. {
  841. // Make sure can actually see entity (don't damage through walls)
  842. trace_t tr;
  843. UTIL_TraceLine( this->WorldSpaceCenter(), pOther->WorldSpaceCenter(), MASK_FIRE_SOLID, pOther, COLLISION_GROUP_NONE, &tr );
  844. if (tr.fraction == 1.0 && !tr.startsolid)
  845. {
  846. pOther->TakeDamage( CTakeDamageInfo( this, this, outputDamage, damageFlags ) );
  847. }
  848. }
  849. }
  850. }
  851. outputHeat *= fire_heatscale.GetFloat() * simTime;
  852. if ( fireCount > 0 )
  853. {
  854. outputHeat /= fireCount;
  855. for ( i = 0; i < fireCount; i++ )
  856. {
  857. pFires[i]->AddHeat( outputHeat, false );
  858. }
  859. }
  860. }
  861. // Destroy any effect I have
  862. void CFire::DestroyEffect()
  863. {
  864. CBaseFire *pEffect = m_hEffect;
  865. if ( pEffect != NULL )
  866. {
  867. //disable the graphics and remove the entity
  868. pEffect->Enable( false );
  869. UTIL_Remove( pEffect );
  870. }
  871. }
  872. //-----------------------------------------------------------------------------
  873. // Purpose: Think
  874. //-----------------------------------------------------------------------------
  875. void CFire::BurnThink( void )
  876. {
  877. SetNextThink( gpGlobals->curtime + FIRE_THINK_INTERVAL );
  878. Update( FIRE_THINK_INTERVAL );
  879. }
  880. void CFire::GoOutThink()
  881. {
  882. GoOut();
  883. }
  884. void CFire::GoOutInSeconds( float seconds )
  885. {
  886. Scale( 0.0f, seconds );
  887. SetThink( &CFire::GoOutThink );
  888. SetNextThink( gpGlobals->curtime + seconds );
  889. }
  890. //------------------------------------------------------------------------------
  891. // Purpose : Blasts of significant size blow out fires that take damage
  892. // Input :
  893. // Output :
  894. //------------------------------------------------------------------------------
  895. int CFire::OnTakeDamage( const CTakeDamageInfo &info )
  896. {
  897. return 0;
  898. }
  899. void CFire::AddHeat( float heat, bool selfHeat )
  900. {
  901. if ( m_bEnabled )
  902. {
  903. if ( !selfHeat )
  904. {
  905. if ( IsBurning() )
  906. {
  907. // scale back the incoming heat from surrounding fires
  908. // if I've already ignited
  909. heat *= fire_incomingheatscale.GetFloat();
  910. }
  911. }
  912. m_lastDamage = gpGlobals->curtime + 0.5;
  913. bool start = m_flHeatLevel <= 0 ? true : false;
  914. if ( m_flHeatAbsorb > 0 )
  915. {
  916. float absorbDamage = heat * fire_absorbrate.GetFloat();
  917. if ( absorbDamage > m_flHeatAbsorb )
  918. {
  919. heat -= m_flHeatAbsorb / fire_absorbrate.GetFloat();
  920. m_flHeatAbsorb = 0;
  921. }
  922. else
  923. {
  924. m_flHeatAbsorb -= absorbDamage;
  925. heat = 0;
  926. }
  927. }
  928. m_flHeatLevel += heat;
  929. if ( start && m_flHeatLevel > 0 && m_hEffect == NULL )
  930. {
  931. StartFire();
  932. }
  933. if ( m_flHeatLevel > m_flMaxHeat )
  934. m_flHeatLevel = m_flMaxHeat;
  935. }
  936. }
  937. //-----------------------------------------------------------------------------
  938. // Purpose:
  939. // Input : end -
  940. // time -
  941. //-----------------------------------------------------------------------------
  942. void CFire::Scale( float end, float time )
  943. {
  944. CBaseFire *pEffect = m_hEffect;
  945. if ( pEffect )
  946. {
  947. pEffect->Scale( end, time );
  948. }
  949. }
  950. //-----------------------------------------------------------------------------
  951. // Purpose:
  952. // Input : time -
  953. //-----------------------------------------------------------------------------
  954. void CFire::Extinguish( float heat )
  955. {
  956. if ( !m_bEnabled )
  957. return;
  958. m_lastDamage = gpGlobals->curtime + 0.5;
  959. bool out = m_flHeatLevel > 0 ? true : false;
  960. m_flHeatLevel -= heat;
  961. m_flHeatAbsorb += fire_extabsorb.GetFloat() * heat;
  962. if ( m_flHeatAbsorb > fire_maxabsorb.GetFloat() )
  963. {
  964. m_flHeatAbsorb = fire_maxabsorb.GetFloat();
  965. }
  966. // drift toward the average attack time after being sprayed
  967. // some fires are heavily scripted so their attack looks weird
  968. // once interacted with. Basically, this blends out the scripting
  969. // as the fire is sprayed with the extinguisher.
  970. float averageAttackTime = m_flMaxHeat * (FIRE_NORMAL_ATTACK_TIME/FIRE_MAX_HEAT_LEVEL);
  971. m_flAttackTime = Approach( averageAttackTime, m_flAttackTime, 2 * gpGlobals->frametime );
  972. if ( m_flHeatLevel <= 0 )
  973. {
  974. m_flHeatLevel = 0;
  975. if ( out )
  976. {
  977. GoOut();
  978. }
  979. }
  980. }
  981. bool CFire::GoOut()
  982. {
  983. //Signal death
  984. m_OnExtinguished.FireOutput( this, this );
  985. DestroyEffect();
  986. m_flHeatLevel -= 20;
  987. if ( m_flHeatLevel > 0 )
  988. m_flHeatLevel = 0;
  989. m_flLastHeatLevel = m_flHeatLevel;
  990. SetThink(NULL);
  991. SetNextThink( TICK_NEVER_THINK );
  992. if ( m_spawnflags & SF_FIRE_DIE_PERMANENT )
  993. {
  994. UTIL_Remove( this );
  995. return true;
  996. }
  997. SetToOutSize();
  998. return false;
  999. }
  1000. //==================================================
  1001. // CEnvFireSource is a source of heat that the player
  1002. // cannot put out
  1003. //==================================================
  1004. #define FIRESOURCE_THINK_TIME 0.25 // seconds to
  1005. #define SF_FIRESOURCE_START_ON 0x0001
  1006. class CEnvFireSource : public CBaseEntity
  1007. {
  1008. DECLARE_CLASS( CEnvFireSource, CBaseEntity );
  1009. public:
  1010. void Spawn();
  1011. void Think();
  1012. void TurnOn();
  1013. void TurnOff();
  1014. void InputEnable( inputdata_t &inputdata );
  1015. void InputDisable( inputdata_t &inputdata );
  1016. DECLARE_DATADESC();
  1017. private:
  1018. bool m_bEnabled;
  1019. float m_radius;
  1020. float m_damage;
  1021. };
  1022. BEGIN_DATADESC( CEnvFireSource )
  1023. DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
  1024. DEFINE_KEYFIELD( m_radius, FIELD_FLOAT, "fireradius" ),
  1025. DEFINE_KEYFIELD( m_damage,FIELD_FLOAT, "firedamage" ),
  1026. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  1027. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  1028. END_DATADESC()
  1029. LINK_ENTITY_TO_CLASS( env_firesource, CEnvFireSource );
  1030. void CEnvFireSource::Spawn()
  1031. {
  1032. if ( m_spawnflags & SF_FIRESOURCE_START_ON )
  1033. {
  1034. TurnOn();
  1035. }
  1036. else
  1037. {
  1038. TurnOff();
  1039. }
  1040. }
  1041. void CEnvFireSource::Think()
  1042. {
  1043. if ( !m_bEnabled )
  1044. return;
  1045. SetNextThink( gpGlobals->curtime + FIRESOURCE_THINK_TIME );
  1046. CFire *pFires[128];
  1047. int fireCount = FireSystem_GetFiresInSphere( pFires, ARRAYSIZE(pFires), false, GetAbsOrigin(), m_radius );
  1048. for ( int i = 0; i < fireCount; i++ )
  1049. {
  1050. pFires[i]->AddHeat( m_damage * FIRESOURCE_THINK_TIME );
  1051. }
  1052. }
  1053. void CEnvFireSource::TurnOn()
  1054. {
  1055. if ( m_bEnabled )
  1056. return;
  1057. m_bEnabled = true;
  1058. SetNextThink( gpGlobals->curtime );
  1059. }
  1060. void CEnvFireSource::TurnOff()
  1061. {
  1062. if ( !m_bEnabled )
  1063. return;
  1064. m_bEnabled = false;
  1065. SetNextThink( TICK_NEVER_THINK );
  1066. }
  1067. void CEnvFireSource::InputEnable( inputdata_t &inputdata )
  1068. {
  1069. TurnOn();
  1070. }
  1071. void CEnvFireSource::InputDisable( inputdata_t &inputdata )
  1072. {
  1073. TurnOff();
  1074. }
  1075. //==================================================
  1076. // CEnvFireSensor detects changes in heat
  1077. //==================================================
  1078. #define SF_FIRESENSOR_START_ON 1
  1079. class CEnvFireSensor : public CBaseEntity
  1080. {
  1081. DECLARE_CLASS( CEnvFireSensor, CBaseEntity );
  1082. public:
  1083. void Spawn();
  1084. void Think();
  1085. void TurnOn();
  1086. void TurnOff();
  1087. void InputEnable( inputdata_t &inputdata );
  1088. void InputDisable( inputdata_t &inputdata );
  1089. DECLARE_DATADESC();
  1090. private:
  1091. bool m_bEnabled;
  1092. bool m_bHeatAtLevel;
  1093. float m_radius;
  1094. float m_targetLevel;
  1095. float m_targetTime;
  1096. float m_levelTime;
  1097. COutputEvent m_OnHeatLevelStart;
  1098. COutputEvent m_OnHeatLevelEnd;
  1099. };
  1100. BEGIN_DATADESC( CEnvFireSensor )
  1101. DEFINE_KEYFIELD( m_radius, FIELD_FLOAT, "fireradius" ),
  1102. DEFINE_KEYFIELD( m_targetLevel, FIELD_FLOAT, "heatlevel" ),
  1103. DEFINE_KEYFIELD( m_targetTime, FIELD_FLOAT, "heattime" ),
  1104. DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
  1105. DEFINE_FIELD( m_bHeatAtLevel, FIELD_BOOLEAN ),
  1106. DEFINE_FIELD( m_levelTime, FIELD_FLOAT ),
  1107. DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
  1108. DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
  1109. DEFINE_OUTPUT( m_OnHeatLevelStart, "OnHeatLevelStart"),
  1110. DEFINE_OUTPUT( m_OnHeatLevelEnd, "OnHeatLevelEnd"),
  1111. END_DATADESC()
  1112. LINK_ENTITY_TO_CLASS( env_firesensor, CEnvFireSensor );
  1113. void CEnvFireSensor::Spawn()
  1114. {
  1115. if ( m_spawnflags & SF_FIRESENSOR_START_ON )
  1116. {
  1117. TurnOn();
  1118. }
  1119. else
  1120. {
  1121. TurnOff();
  1122. }
  1123. }
  1124. void CEnvFireSensor::Think()
  1125. {
  1126. if ( !m_bEnabled )
  1127. return;
  1128. float time = m_targetTime * 0.25;
  1129. if ( time < 0.1 )
  1130. {
  1131. time = 0.1;
  1132. }
  1133. SetNextThink( gpGlobals->curtime + time );
  1134. float heat = 0;
  1135. CFire *pFires[128];
  1136. int fireCount = FireSystem_GetFiresInSphere( pFires, ARRAYSIZE(pFires), true, GetAbsOrigin(), m_radius );
  1137. for ( int i = 0; i < fireCount; i++ )
  1138. {
  1139. heat += pFires[i]->GetHeatLevel();
  1140. }
  1141. if ( heat >= m_targetLevel )
  1142. {
  1143. m_levelTime += time;
  1144. if ( m_levelTime >= m_targetTime )
  1145. {
  1146. if ( !m_bHeatAtLevel )
  1147. {
  1148. m_bHeatAtLevel = true;
  1149. m_OnHeatLevelStart.FireOutput( this, this );
  1150. }
  1151. }
  1152. }
  1153. else
  1154. {
  1155. m_levelTime = 0;
  1156. if ( m_bHeatAtLevel )
  1157. {
  1158. m_bHeatAtLevel = false;
  1159. m_OnHeatLevelEnd.FireOutput( this, this );
  1160. }
  1161. }
  1162. }
  1163. void CEnvFireSensor::TurnOn()
  1164. {
  1165. if ( m_bEnabled )
  1166. return;
  1167. m_bEnabled = true;
  1168. SetNextThink( gpGlobals->curtime );
  1169. m_bHeatAtLevel = false;
  1170. m_levelTime = 0;
  1171. }
  1172. void CEnvFireSensor::TurnOff()
  1173. {
  1174. if ( !m_bEnabled )
  1175. return;
  1176. m_bEnabled = false;
  1177. SetNextThink( TICK_NEVER_THINK );
  1178. if ( m_bHeatAtLevel )
  1179. {
  1180. m_bHeatAtLevel = false;
  1181. m_OnHeatLevelEnd.FireOutput( this, this );
  1182. }
  1183. }
  1184. void CEnvFireSensor::InputEnable( inputdata_t &inputdata )
  1185. {
  1186. TurnOn();
  1187. }
  1188. void CEnvFireSensor::InputDisable( inputdata_t &inputdata )
  1189. {
  1190. TurnOff();
  1191. }
  1192. //-----------------------------------------------------------------------------
  1193. // Purpose: Draw any debug text overlays
  1194. // Output : Current text offset from the top
  1195. //-----------------------------------------------------------------------------
  1196. int CFire::DrawDebugTextOverlays( void )
  1197. {
  1198. int text_offset = BaseClass::DrawDebugTextOverlays();
  1199. if (m_debugOverlays & OVERLAY_TEXT_BIT)
  1200. {
  1201. char tempstr[512];
  1202. // print flame size
  1203. Q_snprintf(tempstr,sizeof(tempstr)," size: %f", m_flFireSize);
  1204. EntityText(text_offset,tempstr,0);
  1205. text_offset++;
  1206. }
  1207. return text_offset;
  1208. }