PageRenderTime 71ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/neo/d3xp/Misc.cpp

https://bitbucket.org/egetun/id-tech-4
C++ | 3753 lines | 1981 code | 541 blank | 1231 comment | 321 complexity | 83cfe221af36628eda5dd7bb0df35ee0 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, CC0-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. /*
  21. Various utility objects and functions.
  22. */
  23. #include "../idlib/precompiled.h"
  24. #pragma hdrstop
  25. #include "Game_local.h"
  26. /*
  27. ===============================================================================
  28. idSpawnableEntity
  29. A simple, spawnable entity with a model and no functionable ability of it's own.
  30. For example, it can be used as a placeholder during development, for marking
  31. locations on maps for script, or for simple placed models without any behavior
  32. that can be bound to other entities. Should not be subclassed.
  33. ===============================================================================
  34. */
  35. CLASS_DECLARATION( idEntity, idSpawnableEntity )
  36. END_CLASS
  37. /*
  38. ======================
  39. idSpawnableEntity::Spawn
  40. ======================
  41. */
  42. void idSpawnableEntity::Spawn() {
  43. // this just holds dict information
  44. }
  45. /*
  46. ===============================================================================
  47. idPlayerStart
  48. ===============================================================================
  49. */
  50. const idEventDef EV_TeleportStage( "<TeleportStage>", "e" );
  51. CLASS_DECLARATION( idEntity, idPlayerStart )
  52. EVENT( EV_Activate, idPlayerStart::Event_TeleportPlayer )
  53. EVENT( EV_TeleportStage, idPlayerStart::Event_TeleportStage )
  54. END_CLASS
  55. /*
  56. ===============
  57. idPlayerStart::idPlayerStart
  58. ================
  59. */
  60. idPlayerStart::idPlayerStart( void ) {
  61. teleportStage = 0;
  62. }
  63. /*
  64. ===============
  65. idPlayerStart::Spawn
  66. ================
  67. */
  68. void idPlayerStart::Spawn( void ) {
  69. teleportStage = 0;
  70. }
  71. /*
  72. ================
  73. idPlayerStart::Save
  74. ================
  75. */
  76. void idPlayerStart::Save( idSaveGame *savefile ) const {
  77. savefile->WriteInt( teleportStage );
  78. }
  79. /*
  80. ================
  81. idPlayerStart::Restore
  82. ================
  83. */
  84. void idPlayerStart::Restore( idRestoreGame *savefile ) {
  85. savefile->ReadInt( teleportStage );
  86. }
  87. /*
  88. ================
  89. idPlayerStart::ClientReceiveEvent
  90. ================
  91. */
  92. bool idPlayerStart::ClientReceiveEvent( int event, int time, const idBitMsg &msg ) {
  93. int entityNumber;
  94. switch( event ) {
  95. case EVENT_TELEPORTPLAYER: {
  96. entityNumber = msg.ReadBits( GENTITYNUM_BITS );
  97. idPlayer *player = static_cast<idPlayer *>( gameLocal.entities[entityNumber] );
  98. if ( player != NULL && player->IsType( idPlayer::Type ) ) {
  99. Event_TeleportPlayer( player );
  100. }
  101. return true;
  102. }
  103. default: {
  104. return idEntity::ClientReceiveEvent( event, time, msg );
  105. }
  106. }
  107. return false;
  108. }
  109. /*
  110. ===============
  111. idPlayerStart::Event_TeleportStage
  112. FIXME: add functionality to fx system ( could be done with player scripting too )
  113. ================
  114. */
  115. void idPlayerStart::Event_TeleportStage( idEntity *_player ) {
  116. idPlayer *player;
  117. if ( !_player->IsType( idPlayer::Type ) ) {
  118. common->Warning( "idPlayerStart::Event_TeleportStage: entity is not an idPlayer\n" );
  119. return;
  120. }
  121. player = static_cast<idPlayer*>(_player);
  122. float teleportDelay = spawnArgs.GetFloat( "teleportDelay" );
  123. switch ( teleportStage ) {
  124. case 0:
  125. player->playerView.Flash( colorWhite, 125 );
  126. player->SetInfluenceLevel( INFLUENCE_LEVEL3 );
  127. player->SetInfluenceView( spawnArgs.GetString( "mtr_teleportFx" ), NULL, 0.0f, NULL );
  128. gameSoundWorld->FadeSoundClasses( 0, -20.0f, teleportDelay );
  129. player->StartSound( "snd_teleport_start", SND_CHANNEL_BODY2, 0, false, NULL );
  130. teleportStage++;
  131. PostEventSec( &EV_TeleportStage, teleportDelay, player );
  132. break;
  133. case 1:
  134. gameSoundWorld->FadeSoundClasses( 0, 0.0f, 0.25f );
  135. teleportStage++;
  136. PostEventSec( &EV_TeleportStage, 0.25f, player );
  137. break;
  138. case 2:
  139. player->SetInfluenceView( NULL, NULL, 0.0f, NULL );
  140. TeleportPlayer( player );
  141. player->StopSound( SND_CHANNEL_BODY2, false );
  142. player->SetInfluenceLevel( INFLUENCE_NONE );
  143. teleportStage = 0;
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. /*
  150. ===============
  151. idPlayerStart::TeleportPlayer
  152. ================
  153. */
  154. void idPlayerStart::TeleportPlayer( idPlayer *player ) {
  155. float pushVel = spawnArgs.GetFloat( "push", "300" );
  156. float f = spawnArgs.GetFloat( "visualEffect", "0" );
  157. const char *viewName = spawnArgs.GetString( "visualView", "" );
  158. idEntity *ent = viewName ? gameLocal.FindEntity( viewName ) : NULL;
  159. #ifdef _D3XP
  160. SetTimeState ts( player->timeGroup );
  161. #endif
  162. if ( f && ent ) {
  163. // place in private camera view for some time
  164. // the entity needs to teleport to where the camera view is to have the PVS right
  165. player->Teleport( ent->GetPhysics()->GetOrigin(), ang_zero, this );
  166. player->StartSound( "snd_teleport_enter", SND_CHANNEL_ANY, 0, false, NULL );
  167. player->SetPrivateCameraView( static_cast<idCamera*>(ent) );
  168. // the player entity knows where to spawn from the previous Teleport call
  169. if ( !gameLocal.isClient ) {
  170. player->PostEventSec( &EV_Player_ExitTeleporter, f );
  171. }
  172. } else {
  173. // direct to exit, Teleport will take care of the killbox
  174. player->Teleport( GetPhysics()->GetOrigin(), GetPhysics()->GetAxis().ToAngles(), NULL );
  175. // multiplayer hijacked this entity, so only push the player in multiplayer
  176. if ( gameLocal.isMultiplayer ) {
  177. player->GetPhysics()->SetLinearVelocity( GetPhysics()->GetAxis()[0] * pushVel );
  178. }
  179. }
  180. }
  181. /*
  182. ===============
  183. idPlayerStart::Event_TeleportPlayer
  184. ================
  185. */
  186. void idPlayerStart::Event_TeleportPlayer( idEntity *activator ) {
  187. idPlayer *player;
  188. if ( activator->IsType( idPlayer::Type ) ) {
  189. player = static_cast<idPlayer*>( activator );
  190. } else {
  191. player = gameLocal.GetLocalPlayer();
  192. }
  193. if ( player ) {
  194. if ( spawnArgs.GetBool( "visualFx" ) ) {
  195. teleportStage = 0;
  196. Event_TeleportStage( player );
  197. } else {
  198. if ( gameLocal.isServer ) {
  199. idBitMsg msg;
  200. byte msgBuf[MAX_EVENT_PARAM_SIZE];
  201. msg.Init( msgBuf, sizeof( msgBuf ) );
  202. msg.BeginWriting();
  203. msg.WriteBits( player->entityNumber, GENTITYNUM_BITS );
  204. ServerSendEvent( EVENT_TELEPORTPLAYER, &msg, false, -1 );
  205. }
  206. TeleportPlayer( player );
  207. }
  208. }
  209. }
  210. /*
  211. ===============================================================================
  212. idActivator
  213. ===============================================================================
  214. */
  215. CLASS_DECLARATION( idEntity, idActivator )
  216. EVENT( EV_Activate, idActivator::Event_Activate )
  217. END_CLASS
  218. /*
  219. ===============
  220. idActivator::Save
  221. ================
  222. */
  223. void idActivator::Save( idSaveGame *savefile ) const {
  224. savefile->WriteBool( stay_on );
  225. }
  226. /*
  227. ===============
  228. idActivator::Restore
  229. ================
  230. */
  231. void idActivator::Restore( idRestoreGame *savefile ) {
  232. savefile->ReadBool( stay_on );
  233. if ( stay_on ) {
  234. BecomeActive( TH_THINK );
  235. }
  236. }
  237. /*
  238. ===============
  239. idActivator::Spawn
  240. ================
  241. */
  242. void idActivator::Spawn( void ) {
  243. bool start_off;
  244. spawnArgs.GetBool( "stay_on", "0", stay_on );
  245. spawnArgs.GetBool( "start_off", "0", start_off );
  246. GetPhysics()->SetClipBox( idBounds( vec3_origin ).Expand( 4 ), 1.0f );
  247. GetPhysics()->SetContents( 0 );
  248. if ( !start_off ) {
  249. BecomeActive( TH_THINK );
  250. }
  251. }
  252. /*
  253. ===============
  254. idActivator::Think
  255. ================
  256. */
  257. void idActivator::Think( void ) {
  258. RunPhysics();
  259. if ( thinkFlags & TH_THINK ) {
  260. if ( TouchTriggers() ) {
  261. if ( !stay_on ) {
  262. BecomeInactive( TH_THINK );
  263. }
  264. }
  265. }
  266. Present();
  267. }
  268. /*
  269. ===============
  270. idActivator::Activate
  271. ================
  272. */
  273. void idActivator::Event_Activate( idEntity *activator ) {
  274. if ( thinkFlags & TH_THINK ) {
  275. BecomeInactive( TH_THINK );
  276. } else {
  277. BecomeActive( TH_THINK );
  278. }
  279. }
  280. /*
  281. ===============================================================================
  282. idPathCorner
  283. ===============================================================================
  284. */
  285. CLASS_DECLARATION( idEntity, idPathCorner )
  286. EVENT( AI_RandomPath, idPathCorner::Event_RandomPath )
  287. END_CLASS
  288. /*
  289. =====================
  290. idPathCorner::Spawn
  291. =====================
  292. */
  293. void idPathCorner::Spawn( void ) {
  294. }
  295. /*
  296. =====================
  297. idPathCorner::DrawDebugInfo
  298. =====================
  299. */
  300. void idPathCorner::DrawDebugInfo( void ) {
  301. idEntity *ent;
  302. idBounds bnds( idVec3( -4.0, -4.0f, -8.0f ), idVec3( 4.0, 4.0f, 64.0f ) );
  303. for( ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() ) {
  304. if ( !ent->IsType( idPathCorner::Type ) ) {
  305. continue;
  306. }
  307. idVec3 org = ent->GetPhysics()->GetOrigin();
  308. gameRenderWorld->DebugBounds( colorRed, bnds, org, 0 );
  309. }
  310. }
  311. /*
  312. ============
  313. idPathCorner::RandomPath
  314. ============
  315. */
  316. idPathCorner *idPathCorner::RandomPath( const idEntity *source, const idEntity *ignore ) {
  317. int i;
  318. int num;
  319. int which;
  320. idEntity *ent;
  321. idPathCorner *path[ MAX_GENTITIES ];
  322. num = 0;
  323. for( i = 0; i < source->targets.Num(); i++ ) {
  324. ent = source->targets[ i ].GetEntity();
  325. if ( ent && ( ent != ignore ) && ent->IsType( idPathCorner::Type ) ) {
  326. path[ num++ ] = static_cast<idPathCorner *>( ent );
  327. if ( num >= MAX_GENTITIES ) {
  328. break;
  329. }
  330. }
  331. }
  332. if ( !num ) {
  333. return NULL;
  334. }
  335. which = gameLocal.random.RandomInt( num );
  336. return path[ which ];
  337. }
  338. /*
  339. =====================
  340. idPathCorner::Event_RandomPath
  341. =====================
  342. */
  343. void idPathCorner::Event_RandomPath( void ) {
  344. idPathCorner *path;
  345. path = RandomPath( this, NULL );
  346. idThread::ReturnEntity( path );
  347. }
  348. /*
  349. ===============================================================================
  350. idDamagable
  351. ===============================================================================
  352. */
  353. const idEventDef EV_RestoreDamagable( "<RestoreDamagable>" );
  354. CLASS_DECLARATION( idEntity, idDamagable )
  355. EVENT( EV_Activate, idDamagable::Event_BecomeBroken )
  356. EVENT( EV_RestoreDamagable, idDamagable::Event_RestoreDamagable )
  357. END_CLASS
  358. /*
  359. ================
  360. idDamagable::idDamagable
  361. ================
  362. */
  363. idDamagable::idDamagable( void ) {
  364. count = 0;
  365. nextTriggerTime = 0;
  366. }
  367. /*
  368. ================
  369. idDamagable::Save
  370. ================
  371. */
  372. void idDamagable::Save( idSaveGame *savefile ) const {
  373. savefile->WriteInt( count );
  374. savefile->WriteInt( nextTriggerTime );
  375. }
  376. /*
  377. ================
  378. idDamagable::Restore
  379. ================
  380. */
  381. void idDamagable::Restore( idRestoreGame *savefile ) {
  382. savefile->ReadInt( count );
  383. savefile->ReadInt( nextTriggerTime );
  384. }
  385. /*
  386. ================
  387. idDamagable::Spawn
  388. ================
  389. */
  390. void idDamagable::Spawn( void ) {
  391. idStr broken;
  392. health = spawnArgs.GetInt( "health", "5" );
  393. spawnArgs.GetInt( "count", "1", count );
  394. nextTriggerTime = 0;
  395. // make sure the model gets cached
  396. spawnArgs.GetString( "broken", "", broken );
  397. if ( broken.Length() && !renderModelManager->CheckModel( broken ) ) {
  398. gameLocal.Error( "idDamagable '%s' at (%s): cannot load broken model '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), broken.c_str() );
  399. }
  400. fl.takedamage = true;
  401. GetPhysics()->SetContents( CONTENTS_SOLID );
  402. }
  403. /*
  404. ================
  405. idDamagable::BecomeBroken
  406. ================
  407. */
  408. void idDamagable::BecomeBroken( idEntity *activator ) {
  409. float forceState;
  410. int numStates;
  411. int cycle;
  412. float wait;
  413. if ( gameLocal.time < nextTriggerTime ) {
  414. return;
  415. }
  416. spawnArgs.GetFloat( "wait", "0.1", wait );
  417. nextTriggerTime = gameLocal.time + SEC2MS( wait );
  418. if ( count > 0 ) {
  419. count--;
  420. if ( !count ) {
  421. fl.takedamage = false;
  422. } else {
  423. health = spawnArgs.GetInt( "health", "5" );
  424. }
  425. }
  426. idStr broken;
  427. spawnArgs.GetString( "broken", "", broken );
  428. if ( broken.Length() ) {
  429. SetModel( broken );
  430. }
  431. // offset the start time of the shader to sync it to the gameLocal time
  432. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  433. spawnArgs.GetInt( "numstates", "1", numStates );
  434. spawnArgs.GetInt( "cycle", "0", cycle );
  435. spawnArgs.GetFloat( "forcestate", "0", forceState );
  436. // set the state parm
  437. if ( cycle ) {
  438. renderEntity.shaderParms[ SHADERPARM_MODE ]++;
  439. if ( renderEntity.shaderParms[ SHADERPARM_MODE ] > numStates ) {
  440. renderEntity.shaderParms[ SHADERPARM_MODE ] = 0;
  441. }
  442. } else if ( forceState ) {
  443. renderEntity.shaderParms[ SHADERPARM_MODE ] = forceState;
  444. } else {
  445. renderEntity.shaderParms[ SHADERPARM_MODE ] = gameLocal.random.RandomInt( numStates ) + 1;
  446. }
  447. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  448. ActivateTargets( activator );
  449. if ( spawnArgs.GetBool( "hideWhenBroken" ) ) {
  450. Hide();
  451. PostEventMS( &EV_RestoreDamagable, nextTriggerTime - gameLocal.time );
  452. BecomeActive( TH_THINK );
  453. }
  454. }
  455. /*
  456. ================
  457. idDamagable::Killed
  458. ================
  459. */
  460. void idDamagable::Killed( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location ) {
  461. if ( gameLocal.time < nextTriggerTime ) {
  462. health += damage;
  463. return;
  464. }
  465. BecomeBroken( attacker );
  466. }
  467. #ifdef _D3XP
  468. /*
  469. ================
  470. idDamagable::Hide
  471. ================
  472. */
  473. void idDamagable::Hide( void ) {
  474. idEntity::Hide();
  475. GetPhysics()->SetContents( 0 );
  476. }
  477. /*
  478. ================
  479. idDamagable::Show
  480. ================
  481. */
  482. void idDamagable::Show( void ) {
  483. idEntity::Show();
  484. GetPhysics()->SetContents( CONTENTS_SOLID );
  485. }
  486. #endif
  487. /*
  488. ================
  489. idDamagable::Event_BecomeBroken
  490. ================
  491. */
  492. void idDamagable::Event_BecomeBroken( idEntity *activator ) {
  493. BecomeBroken( activator );
  494. }
  495. /*
  496. ================
  497. idDamagable::Event_RestoreDamagable
  498. ================
  499. */
  500. void idDamagable::Event_RestoreDamagable( void ) {
  501. health = spawnArgs.GetInt( "health", "5" );
  502. Show();
  503. }
  504. /*
  505. ===============================================================================
  506. idExplodable
  507. ===============================================================================
  508. */
  509. CLASS_DECLARATION( idEntity, idExplodable )
  510. EVENT( EV_Activate, idExplodable::Event_Explode )
  511. END_CLASS
  512. /*
  513. ================
  514. idExplodable::Spawn
  515. ================
  516. */
  517. void idExplodable::Spawn( void ) {
  518. Hide();
  519. }
  520. /*
  521. ================
  522. idExplodable::Event_Explode
  523. ================
  524. */
  525. void idExplodable::Event_Explode( idEntity *activator ) {
  526. const char *temp;
  527. if ( spawnArgs.GetString( "def_damage", "damage_explosion", &temp ) ) {
  528. gameLocal.RadiusDamage( GetPhysics()->GetOrigin(), activator, activator, this, this, temp );
  529. }
  530. StartSound( "snd_explode", SND_CHANNEL_ANY, 0, false, NULL );
  531. // Show() calls UpdateVisuals, so we don't need to call it ourselves after setting the shaderParms
  532. renderEntity.shaderParms[SHADERPARM_RED] = 1.0f;
  533. renderEntity.shaderParms[SHADERPARM_GREEN] = 1.0f;
  534. renderEntity.shaderParms[SHADERPARM_BLUE] = 1.0f;
  535. renderEntity.shaderParms[SHADERPARM_ALPHA] = 1.0f;
  536. renderEntity.shaderParms[SHADERPARM_TIMEOFFSET] = -MS2SEC( gameLocal.time );
  537. renderEntity.shaderParms[SHADERPARM_DIVERSITY] = 0.0f;
  538. Show();
  539. PostEventMS( &EV_Remove, 2000 );
  540. ActivateTargets( activator );
  541. }
  542. /*
  543. ===============================================================================
  544. idSpring
  545. ===============================================================================
  546. */
  547. CLASS_DECLARATION( idEntity, idSpring )
  548. EVENT( EV_PostSpawn, idSpring::Event_LinkSpring )
  549. END_CLASS
  550. /*
  551. ================
  552. idSpring::Think
  553. ================
  554. */
  555. void idSpring::Think( void ) {
  556. idVec3 start, end, origin;
  557. idMat3 axis;
  558. // run physics
  559. RunPhysics();
  560. if ( thinkFlags & TH_THINK ) {
  561. // evaluate force
  562. spring.Evaluate( gameLocal.time );
  563. start = p1;
  564. if ( ent1->GetPhysics() ) {
  565. axis = ent1->GetPhysics()->GetAxis();
  566. origin = ent1->GetPhysics()->GetOrigin();
  567. start = origin + start * axis;
  568. }
  569. end = p2;
  570. if ( ent2->GetPhysics() ) {
  571. axis = ent2->GetPhysics()->GetAxis();
  572. origin = ent2->GetPhysics()->GetOrigin();
  573. end = origin + p2 * axis;
  574. }
  575. gameRenderWorld->DebugLine( idVec4(1, 1, 0, 1), start, end, 0, true );
  576. }
  577. Present();
  578. }
  579. /*
  580. ================
  581. idSpring::Event_LinkSpring
  582. ================
  583. */
  584. void idSpring::Event_LinkSpring( void ) {
  585. idStr name1, name2;
  586. spawnArgs.GetString( "ent1", "", name1 );
  587. spawnArgs.GetString( "ent2", "", name2 );
  588. if ( name1.Length() ) {
  589. ent1 = gameLocal.FindEntity( name1 );
  590. if ( !ent1 ) {
  591. gameLocal.Error( "idSpring '%s' at (%s): cannot find first entity '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), name1.c_str() );
  592. }
  593. }
  594. else {
  595. ent1 = gameLocal.entities[ENTITYNUM_WORLD];
  596. }
  597. if ( name2.Length() ) {
  598. ent2 = gameLocal.FindEntity( name2 );
  599. if ( !ent2 ) {
  600. gameLocal.Error( "idSpring '%s' at (%s): cannot find second entity '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), name2.c_str() );
  601. }
  602. }
  603. else {
  604. ent2 = gameLocal.entities[ENTITYNUM_WORLD];
  605. }
  606. spring.SetPosition( ent1->GetPhysics(), id1, p1, ent2->GetPhysics(), id2, p2 );
  607. BecomeActive( TH_THINK );
  608. }
  609. /*
  610. ================
  611. idSpring::Spawn
  612. ================
  613. */
  614. void idSpring::Spawn( void ) {
  615. float Kstretch, damping, restLength;
  616. spawnArgs.GetInt( "id1", "0", id1 );
  617. spawnArgs.GetInt( "id2", "0", id2 );
  618. spawnArgs.GetVector( "point1", "0 0 0", p1 );
  619. spawnArgs.GetVector( "point2", "0 0 0", p2 );
  620. spawnArgs.GetFloat( "constant", "100.0f", Kstretch );
  621. spawnArgs.GetFloat( "damping", "10.0f", damping );
  622. spawnArgs.GetFloat( "restlength", "0.0f", restLength );
  623. spring.InitSpring( Kstretch, 0.0f, damping, restLength );
  624. ent1 = ent2 = NULL;
  625. PostEventMS( &EV_PostSpawn, 0 );
  626. }
  627. /*
  628. ===============================================================================
  629. idForceField
  630. ===============================================================================
  631. */
  632. const idEventDef EV_Toggle( "Toggle", NULL );
  633. CLASS_DECLARATION( idEntity, idForceField )
  634. EVENT( EV_Activate, idForceField::Event_Activate )
  635. EVENT( EV_Toggle, idForceField::Event_Toggle )
  636. EVENT( EV_FindTargets, idForceField::Event_FindTargets )
  637. END_CLASS
  638. /*
  639. ===============
  640. idForceField::Toggle
  641. ================
  642. */
  643. void idForceField::Toggle( void ) {
  644. if ( thinkFlags & TH_THINK ) {
  645. BecomeInactive( TH_THINK );
  646. } else {
  647. BecomeActive( TH_THINK );
  648. }
  649. }
  650. /*
  651. ================
  652. idForceField::Think
  653. ================
  654. */
  655. void idForceField::Think( void ) {
  656. if ( thinkFlags & TH_THINK ) {
  657. // evaluate force
  658. forceField.Evaluate( gameLocal.time );
  659. }
  660. Present();
  661. }
  662. /*
  663. ================
  664. idForceField::Save
  665. ================
  666. */
  667. void idForceField::Save( idSaveGame *savefile ) const {
  668. savefile->WriteStaticObject( forceField );
  669. }
  670. /*
  671. ================
  672. idForceField::Restore
  673. ================
  674. */
  675. void idForceField::Restore( idRestoreGame *savefile ) {
  676. savefile->ReadStaticObject( forceField );
  677. }
  678. /*
  679. ================
  680. idForceField::Spawn
  681. ================
  682. */
  683. void idForceField::Spawn( void ) {
  684. idVec3 uniform;
  685. float explosion, implosion, randomTorque;
  686. if ( spawnArgs.GetVector( "uniform", "0 0 0", uniform ) ) {
  687. forceField.Uniform( uniform );
  688. } else if ( spawnArgs.GetFloat( "explosion", "0", explosion ) ) {
  689. forceField.Explosion( explosion );
  690. } else if ( spawnArgs.GetFloat( "implosion", "0", implosion ) ) {
  691. forceField.Implosion( implosion );
  692. }
  693. if ( spawnArgs.GetFloat( "randomTorque", "0", randomTorque ) ) {
  694. forceField.RandomTorque( randomTorque );
  695. }
  696. if ( spawnArgs.GetBool( "applyForce", "0" ) ) {
  697. forceField.SetApplyType( FORCEFIELD_APPLY_FORCE );
  698. } else if ( spawnArgs.GetBool( "applyImpulse", "0" ) ) {
  699. forceField.SetApplyType( FORCEFIELD_APPLY_IMPULSE );
  700. } else {
  701. forceField.SetApplyType( FORCEFIELD_APPLY_VELOCITY );
  702. }
  703. forceField.SetPlayerOnly( spawnArgs.GetBool( "playerOnly", "0" ) );
  704. forceField.SetMonsterOnly( spawnArgs.GetBool( "monsterOnly", "0" ) );
  705. // set the collision model on the force field
  706. forceField.SetClipModel( new idClipModel( GetPhysics()->GetClipModel() ) );
  707. // remove the collision model from the physics object
  708. GetPhysics()->SetClipModel( NULL, 1.0f );
  709. if ( spawnArgs.GetBool( "start_on" ) ) {
  710. BecomeActive( TH_THINK );
  711. }
  712. }
  713. /*
  714. ===============
  715. idForceField::Event_Toggle
  716. ================
  717. */
  718. void idForceField::Event_Toggle( void ) {
  719. Toggle();
  720. }
  721. /*
  722. ================
  723. idForceField::Event_Activate
  724. ================
  725. */
  726. void idForceField::Event_Activate( idEntity *activator ) {
  727. float wait;
  728. Toggle();
  729. if ( spawnArgs.GetFloat( "wait", "0.01", wait ) ) {
  730. PostEventSec( &EV_Toggle, wait );
  731. }
  732. }
  733. /*
  734. ================
  735. idForceField::Event_FindTargets
  736. ================
  737. */
  738. void idForceField::Event_FindTargets( void ) {
  739. FindTargets();
  740. RemoveNullTargets();
  741. if ( targets.Num() ) {
  742. forceField.Uniform( targets[0].GetEntity()->GetPhysics()->GetOrigin() - GetPhysics()->GetOrigin() );
  743. }
  744. }
  745. /*
  746. ===============================================================================
  747. idAnimated
  748. ===============================================================================
  749. */
  750. const idEventDef EV_Animated_Start( "<start>" );
  751. const idEventDef EV_LaunchMissiles( "launchMissiles", "ssssdf" );
  752. const idEventDef EV_LaunchMissilesUpdate( "<launchMissiles>", "dddd" );
  753. const idEventDef EV_AnimDone( "<AnimDone>", "d" );
  754. const idEventDef EV_StartRagdoll( "startRagdoll" );
  755. #ifdef _D3XP
  756. const idEventDef EV_SetAnimation( "setAnimation", "s" );
  757. const idEventDef EV_GetAnimationLength( "getAnimationLength", NULL, 'f' );
  758. #endif
  759. CLASS_DECLARATION( idAFEntity_Gibbable, idAnimated )
  760. EVENT( EV_Activate, idAnimated::Event_Activate )
  761. EVENT( EV_Animated_Start, idAnimated::Event_Start )
  762. EVENT( EV_StartRagdoll, idAnimated::Event_StartRagdoll )
  763. EVENT( EV_AnimDone, idAnimated::Event_AnimDone )
  764. EVENT( EV_Footstep, idAnimated::Event_Footstep )
  765. EVENT( EV_FootstepLeft, idAnimated::Event_Footstep )
  766. EVENT( EV_FootstepRight, idAnimated::Event_Footstep )
  767. EVENT( EV_LaunchMissiles, idAnimated::Event_LaunchMissiles )
  768. EVENT( EV_LaunchMissilesUpdate, idAnimated::Event_LaunchMissilesUpdate )
  769. #ifdef _D3XP
  770. EVENT( EV_SetAnimation, idAnimated::Event_SetAnimation )
  771. EVENT( EV_GetAnimationLength, idAnimated::Event_GetAnimationLength )
  772. #endif
  773. END_CLASS
  774. /*
  775. ===============
  776. idAnimated::idAnimated
  777. ================
  778. */
  779. idAnimated::idAnimated() {
  780. anim = 0;
  781. blendFrames = 0;
  782. soundJoint = INVALID_JOINT;
  783. activated = false;
  784. combatModel = NULL;
  785. activator = NULL;
  786. current_anim_index = 0;
  787. num_anims = 0;
  788. }
  789. /*
  790. ===============
  791. idAnimated::idAnimated
  792. ================
  793. */
  794. idAnimated::~idAnimated() {
  795. delete combatModel;
  796. combatModel = NULL;
  797. }
  798. /*
  799. ===============
  800. idAnimated::Save
  801. ================
  802. */
  803. void idAnimated::Save( idSaveGame *savefile ) const {
  804. savefile->WriteInt( current_anim_index );
  805. savefile->WriteInt( num_anims );
  806. savefile->WriteInt( anim );
  807. savefile->WriteInt( blendFrames );
  808. savefile->WriteJoint( soundJoint );
  809. activator.Save( savefile );
  810. savefile->WriteBool( activated );
  811. }
  812. /*
  813. ===============
  814. idAnimated::Restore
  815. ================
  816. */
  817. void idAnimated::Restore( idRestoreGame *savefile ) {
  818. savefile->ReadInt( current_anim_index );
  819. savefile->ReadInt( num_anims );
  820. savefile->ReadInt( anim );
  821. savefile->ReadInt( blendFrames );
  822. savefile->ReadJoint( soundJoint );
  823. activator.Restore( savefile );
  824. savefile->ReadBool( activated );
  825. }
  826. /*
  827. ===============
  828. idAnimated::Spawn
  829. ================
  830. */
  831. void idAnimated::Spawn( void ) {
  832. idStr animname;
  833. int anim2;
  834. float wait;
  835. const char *joint;
  836. joint = spawnArgs.GetString( "sound_bone", "origin" );
  837. soundJoint = animator.GetJointHandle( joint );
  838. if ( soundJoint == INVALID_JOINT ) {
  839. gameLocal.Warning( "idAnimated '%s' at (%s): cannot find joint '%s' for sound playback", name.c_str(), GetPhysics()->GetOrigin().ToString(0), joint );
  840. }
  841. LoadAF();
  842. // allow bullets to collide with a combat model
  843. if ( spawnArgs.GetBool( "combatModel", "0" ) ) {
  844. combatModel = new idClipModel( modelDefHandle );
  845. }
  846. // allow the entity to take damage
  847. if ( spawnArgs.GetBool( "takeDamage", "0" ) ) {
  848. fl.takedamage = true;
  849. }
  850. blendFrames = 0;
  851. current_anim_index = 0;
  852. spawnArgs.GetInt( "num_anims", "0", num_anims );
  853. blendFrames = spawnArgs.GetInt( "blend_in" );
  854. animname = spawnArgs.GetString( num_anims ? "anim1" : "anim" );
  855. if ( !animname.Length() ) {
  856. anim = 0;
  857. } else {
  858. anim = animator.GetAnim( animname );
  859. if ( !anim ) {
  860. gameLocal.Error( "idAnimated '%s' at (%s): cannot find anim '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), animname.c_str() );
  861. }
  862. }
  863. if ( spawnArgs.GetBool( "hide" ) ) {
  864. Hide();
  865. if ( !num_anims ) {
  866. blendFrames = 0;
  867. }
  868. } else if ( spawnArgs.GetString( "start_anim", "", animname ) ) {
  869. anim2 = animator.GetAnim( animname );
  870. if ( !anim2 ) {
  871. gameLocal.Error( "idAnimated '%s' at (%s): cannot find anim '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), animname.c_str() );
  872. }
  873. animator.CycleAnim( ANIMCHANNEL_ALL, anim2, gameLocal.time, 0 );
  874. } else if ( anim ) {
  875. // init joints to the first frame of the animation
  876. animator.SetFrame( ANIMCHANNEL_ALL, anim, 1, gameLocal.time, 0 );
  877. if ( !num_anims ) {
  878. blendFrames = 0;
  879. }
  880. }
  881. spawnArgs.GetFloat( "wait", "-1", wait );
  882. if ( wait >= 0 ) {
  883. PostEventSec( &EV_Activate, wait, this );
  884. }
  885. }
  886. /*
  887. ===============
  888. idAnimated::LoadAF
  889. ===============
  890. */
  891. bool idAnimated::LoadAF( void ) {
  892. idStr fileName;
  893. if ( !spawnArgs.GetString( "ragdoll", "*unknown*", fileName ) ) {
  894. return false;
  895. }
  896. af.SetAnimator( GetAnimator() );
  897. return af.Load( this, fileName );
  898. }
  899. /*
  900. ===============
  901. idAnimated::GetPhysicsToSoundTransform
  902. ===============
  903. */
  904. bool idAnimated::GetPhysicsToSoundTransform( idVec3 &origin, idMat3 &axis ) {
  905. animator.GetJointTransform( soundJoint, gameLocal.time, origin, axis );
  906. axis = renderEntity.axis;
  907. return true;
  908. }
  909. /*
  910. ================
  911. idAnimated::StartRagdoll
  912. ================
  913. */
  914. bool idAnimated::StartRagdoll( void ) {
  915. // if no AF loaded
  916. if ( !af.IsLoaded() ) {
  917. return false;
  918. }
  919. // if the AF is already active
  920. if ( af.IsActive() ) {
  921. return true;
  922. }
  923. // disable any collision model used
  924. GetPhysics()->DisableClip();
  925. // start using the AF
  926. af.StartFromCurrentPose( spawnArgs.GetInt( "velocityTime", "0" ) );
  927. return true;
  928. }
  929. /*
  930. =====================
  931. idAnimated::PlayNextAnim
  932. =====================
  933. */
  934. void idAnimated::PlayNextAnim( void ) {
  935. const char *animname;
  936. int len;
  937. int cycle;
  938. if ( current_anim_index >= num_anims ) {
  939. Hide();
  940. if ( spawnArgs.GetBool( "remove" ) ) {
  941. PostEventMS( &EV_Remove, 0 );
  942. } else {
  943. current_anim_index = 0;
  944. }
  945. return;
  946. }
  947. Show();
  948. current_anim_index++;
  949. spawnArgs.GetString( va( "anim%d", current_anim_index ), NULL, &animname );
  950. if ( !animname ) {
  951. anim = 0;
  952. animator.Clear( ANIMCHANNEL_ALL, gameLocal.time, FRAME2MS( blendFrames ) );
  953. return;
  954. }
  955. anim = animator.GetAnim( animname );
  956. if ( !anim ) {
  957. gameLocal.Warning( "missing anim '%s' on %s", animname, name.c_str() );
  958. return;
  959. }
  960. if ( g_debugCinematic.GetBool() ) {
  961. gameLocal.Printf( "%d: '%s' start anim '%s'\n", gameLocal.framenum, GetName(), animname );
  962. }
  963. spawnArgs.GetInt( "cycle", "1", cycle );
  964. if ( ( current_anim_index == num_anims ) && spawnArgs.GetBool( "loop_last_anim" ) ) {
  965. cycle = -1;
  966. }
  967. animator.CycleAnim( ANIMCHANNEL_ALL, anim, gameLocal.time, FRAME2MS( blendFrames ) );
  968. animator.CurrentAnim( ANIMCHANNEL_ALL )->SetCycleCount( cycle );
  969. len = animator.CurrentAnim( ANIMCHANNEL_ALL )->PlayLength();
  970. if ( len >= 0 ) {
  971. PostEventMS( &EV_AnimDone, len, current_anim_index );
  972. }
  973. // offset the start time of the shader to sync it to the game time
  974. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  975. animator.ForceUpdate();
  976. UpdateAnimation();
  977. UpdateVisuals();
  978. Present();
  979. }
  980. /*
  981. ===============
  982. idAnimated::Event_StartRagdoll
  983. ================
  984. */
  985. void idAnimated::Event_StartRagdoll( void ) {
  986. StartRagdoll();
  987. }
  988. /*
  989. ===============
  990. idAnimated::Event_AnimDone
  991. ================
  992. */
  993. void idAnimated::Event_AnimDone( int animindex ) {
  994. if ( g_debugCinematic.GetBool() ) {
  995. const idAnim *animPtr = animator.GetAnim( anim );
  996. gameLocal.Printf( "%d: '%s' end anim '%s'\n", gameLocal.framenum, GetName(), animPtr ? animPtr->Name() : "" );
  997. }
  998. if ( ( animindex >= num_anims ) && spawnArgs.GetBool( "remove" ) ) {
  999. Hide();
  1000. PostEventMS( &EV_Remove, 0 );
  1001. } else if ( spawnArgs.GetBool( "auto_advance" ) ) {
  1002. PlayNextAnim();
  1003. } else {
  1004. activated = false;
  1005. }
  1006. ActivateTargets( activator.GetEntity() );
  1007. }
  1008. /*
  1009. ===============
  1010. idAnimated::Event_Activate
  1011. ================
  1012. */
  1013. void idAnimated::Event_Activate( idEntity *_activator ) {
  1014. if ( num_anims ) {
  1015. PlayNextAnim();
  1016. activator = _activator;
  1017. return;
  1018. }
  1019. if ( activated ) {
  1020. // already activated
  1021. return;
  1022. }
  1023. activated = true;
  1024. activator = _activator;
  1025. ProcessEvent( &EV_Animated_Start );
  1026. }
  1027. /*
  1028. ===============
  1029. idAnimated::Event_Start
  1030. ================
  1031. */
  1032. void idAnimated::Event_Start( void ) {
  1033. int cycle;
  1034. int len;
  1035. Show();
  1036. if ( num_anims ) {
  1037. PlayNextAnim();
  1038. return;
  1039. }
  1040. if ( anim ) {
  1041. if ( g_debugCinematic.GetBool() ) {
  1042. const idAnim *animPtr = animator.GetAnim( anim );
  1043. gameLocal.Printf( "%d: '%s' start anim '%s'\n", gameLocal.framenum, GetName(), animPtr ? animPtr->Name() : "" );
  1044. }
  1045. spawnArgs.GetInt( "cycle", "1", cycle );
  1046. animator.CycleAnim( ANIMCHANNEL_ALL, anim, gameLocal.time, FRAME2MS( blendFrames ) );
  1047. animator.CurrentAnim( ANIMCHANNEL_ALL )->SetCycleCount( cycle );
  1048. len = animator.CurrentAnim( ANIMCHANNEL_ALL )->PlayLength();
  1049. if ( len >= 0 ) {
  1050. PostEventMS( &EV_AnimDone, len, 1 );
  1051. }
  1052. }
  1053. // offset the start time of the shader to sync it to the game time
  1054. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  1055. animator.ForceUpdate();
  1056. UpdateAnimation();
  1057. UpdateVisuals();
  1058. Present();
  1059. }
  1060. /*
  1061. ===============
  1062. idAnimated::Event_Footstep
  1063. ===============
  1064. */
  1065. void idAnimated::Event_Footstep( void ) {
  1066. StartSound( "snd_footstep", SND_CHANNEL_BODY, 0, false, NULL );
  1067. }
  1068. /*
  1069. =====================
  1070. idAnimated::Event_LaunchMissilesUpdate
  1071. =====================
  1072. */
  1073. void idAnimated::Event_LaunchMissilesUpdate( int launchjoint, int targetjoint, int numshots, int framedelay ) {
  1074. idVec3 launchPos;
  1075. idVec3 targetPos;
  1076. idMat3 axis;
  1077. idVec3 dir;
  1078. idEntity * ent;
  1079. idProjectile * projectile;
  1080. const idDict * projectileDef;
  1081. const char * projectilename;
  1082. projectilename = spawnArgs.GetString( "projectilename" );
  1083. projectileDef = gameLocal.FindEntityDefDict( projectilename, false );
  1084. if ( !projectileDef ) {
  1085. gameLocal.Warning( "idAnimated '%s' at (%s): 'launchMissiles' called with unknown projectile '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), projectilename );
  1086. return;
  1087. }
  1088. StartSound( "snd_missile", SND_CHANNEL_WEAPON, 0, false, NULL );
  1089. animator.GetJointTransform( ( jointHandle_t )launchjoint, gameLocal.time, launchPos, axis );
  1090. launchPos = renderEntity.origin + launchPos * renderEntity.axis;
  1091. animator.GetJointTransform( ( jointHandle_t )targetjoint, gameLocal.time, targetPos, axis );
  1092. targetPos = renderEntity.origin + targetPos * renderEntity.axis;
  1093. dir = targetPos - launchPos;
  1094. dir.Normalize();
  1095. gameLocal.SpawnEntityDef( *projectileDef, &ent, false );
  1096. if ( !ent || !ent->IsType( idProjectile::Type ) ) {
  1097. gameLocal.Error( "idAnimated '%s' at (%s): in 'launchMissiles' call '%s' is not an idProjectile", name.c_str(), GetPhysics()->GetOrigin().ToString(0), projectilename );
  1098. }
  1099. projectile = ( idProjectile * )ent;
  1100. projectile->Create( this, launchPos, dir );
  1101. projectile->Launch( launchPos, dir, vec3_origin );
  1102. if ( numshots > 0 ) {
  1103. PostEventMS( &EV_LaunchMissilesUpdate, FRAME2MS( framedelay ), launchjoint, targetjoint, numshots - 1, framedelay );
  1104. }
  1105. }
  1106. /*
  1107. =====================
  1108. idAnimated::Event_LaunchMissiles
  1109. =====================
  1110. */
  1111. void idAnimated::Event_LaunchMissiles( const char *projectilename, const char *sound, const char *launchjoint, const char *targetjoint, int numshots, int framedelay ) {
  1112. const idDict * projectileDef;
  1113. jointHandle_t launch;
  1114. jointHandle_t target;
  1115. projectileDef = gameLocal.FindEntityDefDict( projectilename, false );
  1116. if ( !projectileDef ) {
  1117. gameLocal.Warning( "idAnimated '%s' at (%s): unknown projectile '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), projectilename );
  1118. return;
  1119. }
  1120. launch = animator.GetJointHandle( launchjoint );
  1121. if ( launch == INVALID_JOINT ) {
  1122. gameLocal.Warning( "idAnimated '%s' at (%s): unknown launch joint '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), launchjoint );
  1123. gameLocal.Error( "Unknown joint '%s'", launchjoint );
  1124. }
  1125. target = animator.GetJointHandle( targetjoint );
  1126. if ( target == INVALID_JOINT ) {
  1127. gameLocal.Warning( "idAnimated '%s' at (%s): unknown target joint '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), targetjoint );
  1128. }
  1129. spawnArgs.Set( "projectilename", projectilename );
  1130. spawnArgs.Set( "missilesound", sound );
  1131. CancelEvents( &EV_LaunchMissilesUpdate );
  1132. ProcessEvent( &EV_LaunchMissilesUpdate, launch, target, numshots - 1, framedelay );
  1133. }
  1134. #ifdef _D3XP
  1135. /*
  1136. =====================
  1137. idAnimated::Event_SetAnimation
  1138. =====================
  1139. */
  1140. void idAnimated::Event_SetAnimation( const char *animName ) {
  1141. //BSM Nerve: Need to add some error checking so we don't change the animation
  1142. //in the middle of the existing animation
  1143. anim = animator.GetAnim( animName );
  1144. if ( !anim ) {
  1145. gameLocal.Error( "idAnimated '%s' at (%s): cannot find anim '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), animName );
  1146. }
  1147. }
  1148. /*
  1149. =====================
  1150. idAnimated::Event_GetAnimationLength
  1151. =====================
  1152. */
  1153. void idAnimated::Event_GetAnimationLength() {
  1154. float length = 0;
  1155. if(anim) {
  1156. length = (float)(animator.AnimLength( anim )) / 1000.f;
  1157. }
  1158. idThread::ReturnFloat(length);
  1159. }
  1160. #endif
  1161. /*
  1162. ===============================================================================
  1163. idStaticEntity
  1164. Some static entities may be optimized into inline geometry by dmap
  1165. ===============================================================================
  1166. */
  1167. CLASS_DECLARATION( idEntity, idStaticEntity )
  1168. EVENT( EV_Activate, idStaticEntity::Event_Activate )
  1169. END_CLASS
  1170. /*
  1171. ===============
  1172. idStaticEntity::idStaticEntity
  1173. ===============
  1174. */
  1175. idStaticEntity::idStaticEntity( void ) {
  1176. spawnTime = 0;
  1177. active = false;
  1178. fadeFrom.Set( 1, 1, 1, 1 );
  1179. fadeTo.Set( 1, 1, 1, 1 );
  1180. fadeStart = 0;
  1181. fadeEnd = 0;
  1182. runGui = false;
  1183. }
  1184. /*
  1185. ===============
  1186. idStaticEntity::Save
  1187. ===============
  1188. */
  1189. void idStaticEntity::Save( idSaveGame *savefile ) const {
  1190. savefile->WriteInt( spawnTime );
  1191. savefile->WriteBool( active );
  1192. savefile->WriteVec4( fadeFrom );
  1193. savefile->WriteVec4( fadeTo );
  1194. savefile->WriteInt( fadeStart );
  1195. savefile->WriteInt( fadeEnd );
  1196. savefile->WriteBool( runGui );
  1197. }
  1198. /*
  1199. ===============
  1200. idStaticEntity::Restore
  1201. ===============
  1202. */
  1203. void idStaticEntity::Restore( idRestoreGame *savefile ) {
  1204. savefile->ReadInt( spawnTime );
  1205. savefile->ReadBool( active );
  1206. savefile->ReadVec4( fadeFrom );
  1207. savefile->ReadVec4( fadeTo );
  1208. savefile->ReadInt( fadeStart );
  1209. savefile->ReadInt( fadeEnd );
  1210. savefile->ReadBool( runGui );
  1211. }
  1212. /*
  1213. ===============
  1214. idStaticEntity::Spawn
  1215. ===============
  1216. */
  1217. void idStaticEntity::Spawn( void ) {
  1218. bool solid;
  1219. bool hidden;
  1220. // an inline static model will not do anything at all
  1221. if ( spawnArgs.GetBool( "inline" ) || gameLocal.world->spawnArgs.GetBool( "inlineAllStatics" ) ) {
  1222. Hide();
  1223. return;
  1224. }
  1225. solid = spawnArgs.GetBool( "solid" );
  1226. hidden = spawnArgs.GetBool( "hide" );
  1227. if ( solid && !hidden ) {
  1228. GetPhysics()->SetContents( CONTENTS_SOLID );
  1229. } else {
  1230. GetPhysics()->SetContents( 0 );
  1231. }
  1232. spawnTime = gameLocal.time;
  1233. active = false;
  1234. idStr model = spawnArgs.GetString( "model" );
  1235. if ( model.Find( ".prt" ) >= 0 ) {
  1236. // we want the parametric particles out of sync with each other
  1237. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = gameLocal.random.RandomInt( 32767 );
  1238. }
  1239. fadeFrom.Set( 1, 1, 1, 1 );
  1240. fadeTo.Set( 1, 1, 1, 1 );
  1241. fadeStart = 0;
  1242. fadeEnd = 0;
  1243. // NOTE: this should be used very rarely because it is expensive
  1244. runGui = spawnArgs.GetBool( "runGui" );
  1245. if ( runGui ) {
  1246. BecomeActive( TH_THINK );
  1247. }
  1248. }
  1249. /*
  1250. ================
  1251. idStaticEntity::ShowEditingDialog
  1252. ================
  1253. */
  1254. void idStaticEntity::ShowEditingDialog( void ) {
  1255. common->InitTool( EDITOR_PARTICLE, &spawnArgs );
  1256. }
  1257. /*
  1258. ================
  1259. idStaticEntity::Think
  1260. ================
  1261. */
  1262. void idStaticEntity::Think( void ) {
  1263. idEntity::Think();
  1264. if ( thinkFlags & TH_THINK ) {
  1265. if ( runGui && renderEntity.gui[0] ) {
  1266. idPlayer *player = gameLocal.GetLocalPlayer();
  1267. if ( player ) {
  1268. if ( !player->objectiveSystemOpen ) {
  1269. renderEntity.gui[0]->StateChanged( gameLocal.time, true );
  1270. if ( renderEntity.gui[1] ) {
  1271. renderEntity.gui[1]->StateChanged( gameLocal.time, true );
  1272. }
  1273. if ( renderEntity.gui[2] ) {
  1274. renderEntity.gui[2]->StateChanged( gameLocal.time, true );
  1275. }
  1276. }
  1277. }
  1278. }
  1279. if ( fadeEnd > 0 ) {
  1280. idVec4 color;
  1281. if ( gameLocal.time < fadeEnd ) {
  1282. color.Lerp( fadeFrom, fadeTo, ( float )( gameLocal.time - fadeStart ) / ( float )( fadeEnd - fadeStart ) );
  1283. } else {
  1284. color = fadeTo;
  1285. fadeEnd = 0;
  1286. BecomeInactive( TH_THINK );
  1287. }
  1288. SetColor( color );
  1289. }
  1290. }
  1291. }
  1292. /*
  1293. ================
  1294. idStaticEntity::Fade
  1295. ================
  1296. */
  1297. void idStaticEntity::Fade( const idVec4 &to, float fadeTime ) {
  1298. GetColor( fadeFrom );
  1299. fadeTo = to;
  1300. fadeStart = gameLocal.time;
  1301. fadeEnd = gameLocal.time + SEC2MS( fadeTime );
  1302. BecomeActive( TH_THINK );
  1303. }
  1304. /*
  1305. ================
  1306. idStaticEntity::Hide
  1307. ================
  1308. */
  1309. void idStaticEntity::Hide( void ) {
  1310. idEntity::Hide();
  1311. GetPhysics()->SetContents( 0 );
  1312. }
  1313. /*
  1314. ================
  1315. idStaticEntity::Show
  1316. ================
  1317. */
  1318. void idStaticEntity::Show( void ) {
  1319. idEntity::Show();
  1320. if ( spawnArgs.GetBool( "solid" ) ) {
  1321. GetPhysics()->SetContents( CONTENTS_SOLID );
  1322. }
  1323. }
  1324. /*
  1325. ================
  1326. idStaticEntity::Event_Activate
  1327. ================
  1328. */
  1329. void idStaticEntity::Event_Activate( idEntity *activator ) {
  1330. idStr activateGui;
  1331. spawnTime = gameLocal.time;
  1332. active = !active;
  1333. const idKeyValue *kv = spawnArgs.FindKey( "hide" );
  1334. if ( kv ) {
  1335. if ( IsHidden() ) {
  1336. Show();
  1337. } else {
  1338. Hide();
  1339. }
  1340. }
  1341. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( spawnTime );
  1342. renderEntity.shaderParms[5] = active;
  1343. // this change should be a good thing, it will automatically turn on
  1344. // lights etc.. when triggered so that does not have to be specifically done
  1345. // with trigger parms.. it MIGHT break things so need to keep an eye on it
  1346. renderEntity.shaderParms[ SHADERPARM_MODE ] = ( renderEntity.shaderParms[ SHADERPARM_MODE ] ) ? 0.0f : 1.0f;
  1347. BecomeActive( TH_UPDATEVISUALS );
  1348. }
  1349. /*
  1350. ================
  1351. idStaticEntity::WriteToSnapshot
  1352. ================
  1353. */
  1354. void idStaticEntity::WriteToSnapshot( idBitMsgDelta &msg ) const {
  1355. GetPhysics()->WriteToSnapshot( msg );
  1356. WriteBindToSnapshot( msg );
  1357. WriteColorToSnapshot( msg );
  1358. WriteGUIToSnapshot( msg );
  1359. msg.WriteBits( IsHidden()?1:0, 1 );
  1360. }
  1361. /*
  1362. ================
  1363. idStaticEntity::ReadFromSnapshot
  1364. ================
  1365. */
  1366. void idStaticEntity::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  1367. bool hidden;
  1368. GetPhysics()->ReadFromSnapshot( msg );
  1369. ReadBindFromSnapshot( msg );
  1370. ReadColorFromSnapshot( msg );
  1371. ReadGUIFromSnapshot( msg );
  1372. hidden = msg.ReadBits( 1 ) == 1;
  1373. if ( hidden != IsHidden() ) {
  1374. if ( hidden ) {
  1375. Hide();
  1376. } else {
  1377. Show();
  1378. }
  1379. }
  1380. if ( msg.HasChanged() ) {
  1381. UpdateVisuals();
  1382. }
  1383. }
  1384. /*
  1385. ===============================================================================
  1386. idFuncEmitter
  1387. ===============================================================================
  1388. */
  1389. CLASS_DECLARATION( idStaticEntity, idFuncEmitter )
  1390. EVENT( EV_Activate, idFuncEmitter::Event_Activate )
  1391. END_CLASS
  1392. /*
  1393. ===============
  1394. idFuncEmitter::idFuncEmitter
  1395. ===============
  1396. */
  1397. idFuncEmitter::idFuncEmitter( void ) {
  1398. hidden = false;
  1399. }
  1400. /*
  1401. ===============
  1402. idFuncEmitter::Spawn
  1403. ===============
  1404. */
  1405. void idFuncEmitter::Spawn( void ) {
  1406. if ( spawnArgs.GetBool( "start_off" ) ) {
  1407. hidden = true;
  1408. renderEntity.shaderParms[SHADERPARM_PARTICLE_STOPTIME] = MS2SEC( 1 );
  1409. UpdateVisuals();
  1410. } else {
  1411. hidden = false;
  1412. }
  1413. }
  1414. /*
  1415. ===============
  1416. idFuncEmitter::Save
  1417. ===============
  1418. */
  1419. void idFuncEmitter::Save( idSaveGame *savefile ) const {
  1420. savefile->WriteBool( hidden );
  1421. }
  1422. /*
  1423. ===============
  1424. idFuncEmitter::Restore
  1425. ===============
  1426. */
  1427. void idFuncEmitter::Restore( idRestoreGame *savefile ) {
  1428. savefile->ReadBool( hidden );
  1429. }
  1430. /*
  1431. ================
  1432. idFuncEmitter::Event_Activate
  1433. ================
  1434. */
  1435. void idFuncEmitter::Event_Activate( idEntity *activator ) {
  1436. if ( hidden || spawnArgs.GetBool( "cycleTrigger" ) ) {
  1437. renderEntity.shaderParms[SHADERPARM_PARTICLE_STOPTIME] = 0;
  1438. renderEntity.shaderParms[SHADERPARM_TIMEOFFSET] = -MS2SEC( gameLocal.time );
  1439. hidden = false;
  1440. } else {
  1441. renderEntity.shaderParms[SHADERPARM_PARTICLE_STOPTIME] = MS2SEC( gameLocal.time );
  1442. hidden = true;
  1443. }
  1444. UpdateVisuals();
  1445. }
  1446. /*
  1447. ================
  1448. idFuncEmitter::WriteToSnapshot
  1449. ================
  1450. */
  1451. void idFuncEmitter::WriteToSnapshot( idBitMsgDelta &msg ) const {
  1452. msg.WriteBits( hidden ? 1 : 0, 1 );
  1453. msg.WriteFloat( renderEntity.shaderParms[ SHADERPARM_PARTICLE_STOPTIME ] );
  1454. msg.WriteFloat( renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] );
  1455. }
  1456. /*
  1457. ================
  1458. idFuncEmitter::ReadFromSnapshot
  1459. ================
  1460. */
  1461. void idFuncEmitter::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  1462. hidden = msg.ReadBits( 1 ) != 0;
  1463. renderEntity.shaderParms[ SHADERPARM_PARTICLE_STOPTIME ] = msg.ReadFloat();
  1464. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = msg.ReadFloat();
  1465. if ( msg.HasChanged() ) {
  1466. UpdateVisuals();
  1467. }
  1468. }
  1469. /*
  1470. ===============================================================================
  1471. idFuncSplat
  1472. ===============================================================================
  1473. */
  1474. const idEventDef EV_Splat( "<Splat>" );
  1475. CLASS_DECLARATION( idFuncEmitter, idFuncSplat )
  1476. EVENT( EV_Activate, idFuncSplat::Event_Activate )
  1477. EVENT( EV_Splat, idFuncSplat::Event_Splat )
  1478. END_CLASS
  1479. /*
  1480. ===============
  1481. idFuncSplat::idFuncSplat
  1482. ===============
  1483. */
  1484. idFuncSplat::idFuncSplat( void ) {
  1485. }
  1486. /*
  1487. ===============
  1488. idFuncSplat::Spawn
  1489. ===============
  1490. */
  1491. void idFuncSplat::Spawn( void ) {
  1492. }
  1493. /*
  1494. ================
  1495. idFuncSplat::Event_Splat
  1496. ================
  1497. */
  1498. void idFuncSplat::Event_Splat( void ) {
  1499. const char *splat = NULL;
  1500. int count = spawnArgs.GetInt( "splatCount", "1" );
  1501. for ( int i = 0; i < count; i++ ) {
  1502. splat = spawnArgs.RandomPrefix( "mtr_splat", gameLocal.random );
  1503. if ( splat && *splat ) {
  1504. float size = spawnArgs.GetFloat( "splatSize", "128" );
  1505. float dist = spawnArgs.GetFloat( "splatDistance", "128" );
  1506. float angle = spawnArgs.GetFloat( "splatAngle", "0" );
  1507. gameLocal.ProjectDecal( GetPhysics()->GetOrigin(), GetPhysics()->GetAxis()[2], dist, true, size, splat, angle );
  1508. }
  1509. }
  1510. StartSound( "snd_splat", SND_CHANNEL_ANY, 0, false, NULL );
  1511. }
  1512. /*
  1513. ================
  1514. idFuncSplat::Event_Activate
  1515. ================
  1516. */
  1517. void idFuncSplat::Event_Activate( idEntity *activator ) {
  1518. idFuncEmitter::Event_Activate( activator );
  1519. PostEventSec( &EV_Splat, spawnArgs.GetFloat( "splatDelay", "0.25" ) );
  1520. StartSound( "snd_spurt", SND_CHANNEL_ANY, 0, false, NULL );
  1521. }
  1522. /*
  1523. ===============================================================================
  1524. idFuncSmoke
  1525. ===============================================================================
  1526. */
  1527. CLASS_DECLARATION( idEntity, idFuncSmoke )
  1528. EVENT( EV_Activate, idFuncSmoke::Event_Activate )
  1529. END_CLASS
  1530. /*
  1531. ===============
  1532. idFuncSmoke::idFuncSmoke
  1533. ===============
  1534. */
  1535. idFuncSmoke::idFuncSmoke() {
  1536. smokeTime = 0;
  1537. smoke = NULL;
  1538. restart = false;
  1539. }
  1540. /*
  1541. ===============
  1542. idFuncSmoke::Save
  1543. ===============
  1544. */
  1545. void idFuncSmoke::Save( idSaveGame *savefile ) const {
  1546. savefile->WriteInt( smokeTime );
  1547. savefile->WriteParticle( smoke );
  1548. savefile->WriteBool( restart );
  1549. }
  1550. /*
  1551. ===============
  1552. idFuncSmoke::Restore
  1553. ===============
  1554. */
  1555. void idFuncSmoke::Restore( idRestoreGame *savefile ) {
  1556. savefile->ReadInt( smokeTime );
  1557. savefile->ReadParticle( smoke );
  1558. savefile->ReadBool( restart );
  1559. }
  1560. /*
  1561. ===============
  1562. idFuncSmoke::Spawn
  1563. ===============
  1564. */
  1565. void idFuncSmoke::Spawn( void ) {
  1566. const char *smokeName = spawnArgs.GetString( "smoke" );
  1567. if ( *smokeName != '\0' ) {
  1568. smoke = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, smokeName ) );
  1569. } else {
  1570. smoke = NULL;
  1571. }
  1572. if ( spawnArgs.GetBool( "start_off" ) ) {
  1573. smokeTime = 0;
  1574. restart = false;
  1575. } else if ( smoke ) {
  1576. smokeTime = gameLocal.time;
  1577. BecomeActive( TH_UPDATEPARTICLES );
  1578. restart = true;
  1579. }
  1580. GetPhysics()->SetContents( 0 );
  1581. }
  1582. /*
  1583. ================
  1584. idFuncSmoke::Event_Activate
  1585. ================
  1586. */
  1587. void idFuncSmoke::Event_Activate( idEntity *activator ) {
  1588. if ( thinkFlags & TH_UPDATEPARTICLES ) {
  1589. restart = false;
  1590. return;
  1591. } else {
  1592. BecomeActive( TH_UPDATEPARTICLES );
  1593. restart = true;
  1594. smokeTime = gameLocal.time;
  1595. }
  1596. }
  1597. /*
  1598. ===============
  1599. idFuncSmoke::Think
  1600. ================
  1601. */
  1602. void idFuncSmoke::Think( void ) {
  1603. // if we are completely closed off from the player, don't do anything at all
  1604. if ( CheckDormant() || smoke == NULL || smokeTime == -1 ) {
  1605. return;
  1606. }
  1607. if ( ( thinkFlags & TH_UPDATEPARTICLES) && !IsHidden() ) {
  1608. if ( !gameLocal.smokeParticles->EmitSmoke( smoke, smokeTime, gameLocal.random.CRandomFloat(), GetPhysics()->GetOrigin(), GetPhysics()->GetAxis(), timeGroup /*_D3XP*/ ) ) {
  1609. if ( restart ) {
  1610. smokeTime = gameLocal.time;
  1611. } else {
  1612. smokeTime = 0;
  1613. BecomeInactive( TH_UPDATEPARTICLES );
  1614. }
  1615. }
  1616. }
  1617. }
  1618. /*
  1619. ===============================================================================
  1620. idTextEntity
  1621. ===============================================================================
  1622. */
  1623. CLASS_DECLARATION( idEntity, idTextEntity )
  1624. END_CLASS
  1625. /*
  1626. ================
  1627. idTextEntity::Spawn
  1628. ================
  1629. */
  1630. void idTextEntity::Spawn( void ) {
  1631. // these are cached as the are used each frame
  1632. text = spawnArgs.GetString( "text" );
  1633. playerOriented = spawnArgs.GetBool( "playerOriented" );
  1634. bool force = spawnArgs.GetBool( "force" );
  1635. if ( developer.GetBool() || force ) {
  1636. BecomeActive(TH_THINK);
  1637. }
  1638. }
  1639. /*
  1640. ================
  1641. idTextEntity::Save
  1642. ================
  1643. */
  1644. void idTextEntity::Save( idSaveGame *savefile ) const {
  1645. savefile->WriteString( text );
  1646. savefile->WriteBool( playerOriented );
  1647. }
  1648. /*
  1649. ================
  1650. idTextEntity::Restore
  1651. ================
  1652. */
  1653. void idTextEntity::Restore( idRestoreGame *savefile ) {
  1654. savefile->ReadString( text );
  1655. savefile->ReadBool( playerOriented );
  1656. }
  1657. /*
  1658. ================
  1659. idTextEntity::Think
  1660. ================
  1661. */
  1662. void idTextEntity::Think( void ) {
  1663. if ( thinkFlags & TH_THINK ) {
  1664. gameRenderWorld->DrawText( text, GetPhysics()->GetOrigin(), 0.25, colorWhite, playerOriented ? gameLocal.GetLocalPlayer()->viewAngles.ToMat3() : GetPhysics()->GetAxis().Transpose(), 1 );
  1665. for ( int i = 0; i < targets.Num(); i++ ) {
  1666. if ( targets[i].GetEntity() ) {
  1667. gameRenderWorld->DebugArrow( colorBlue, GetPhysics()->GetOrigin(), targets[i].GetEntity()->GetPhysics()->GetOrigin(), 1 );
  1668. }
  1669. }
  1670. } else {
  1671. BecomeInactive( TH_ALL );
  1672. }
  1673. }
  1674. /*
  1675. ===============================================================================
  1676. idVacuumSeperatorEntity
  1677. Can be triggered to let vacuum through a portal (blown out window)
  1678. ===============================================================================
  1679. */
  1680. CLASS_DECLARATION( idEntity, idVacuumSeparatorEntity )
  1681. EVENT( EV_Activate, idVacuumSeparatorEntity::Event_Activate )
  1682. END_CLASS
  1683. /*
  1684. ================
  1685. idVacuumSeparatorEntity::idVacuumSeparatorEntity
  1686. ================
  1687. */
  1688. idVacuumSeparatorEntity::idVacuumSeparatorEntity( void ) {
  1689. portal = 0;
  1690. }
  1691. /*
  1692. ================
  1693. idVacuumSeparatorEntity::Save
  1694. ================
  1695. */
  1696. void idVacuumSeparatorEntity::Save( idSaveGame *savefile ) const {
  1697. savefile->WriteInt( (int)portal );
  1698. savefile->WriteInt( gameRenderWorld->GetPortalState( portal ) );
  1699. }
  1700. /*
  1701. ================
  1702. idVacuumSeparatorEntity::Restore
  1703. ================
  1704. */
  1705. void idVacuumSeparatorEntity::Restore( idRestoreGame *savefile ) {
  1706. int state;
  1707. savefile->ReadInt( (int &)portal );
  1708. savefile->ReadInt( state );
  1709. gameLocal.SetPortalState( portal, state );
  1710. }
  1711. /*
  1712. ================
  1713. idVacuumSeparatorEntity::Spawn
  1714. ================
  1715. */
  1716. void idVacuumSeparatorEntity::Spawn() {
  1717. idBounds b;
  1718. b = idBounds( spawnArgs.GetVector( "origin" ) ).Expand( 16 );
  1719. portal = gameRenderWorld->FindPortal( b );
  1720. if ( !portal ) {
  1721. gameLocal.Warning( "VacuumSeparator '%s' didn't contact a portal", spawnArgs.GetString( "name" ) );
  1722. return;
  1723. }
  1724. gameLocal.SetPortalState( portal, PS_BLOCK_AIR | PS_BLOCK_LOCATION );
  1725. }
  1726. /*
  1727. ================
  1728. idVacuumSeparatorEntity::Event_Activate
  1729. ================
  1730. */
  1731. void idVacuumSeparatorEntity::Event_Activate( idEntity *activator ) {
  1732. if ( !portal ) {
  1733. return;
  1734. }
  1735. gameLocal.SetPortalState( portal, PS_BLOCK_NONE );
  1736. }
  1737. /*
  1738. ===============================================================================
  1739. idLocationSeparatorEntity
  1740. ===============================================================================
  1741. */
  1742. CLASS_DECLARATION( idEntity, idLocationSeparatorEntity )
  1743. END_CLASS
  1744. /*
  1745. ================
  1746. idLocationSeparatorEntity::Spawn
  1747. ================
  1748. */
  1749. void idLocationSeparatorEntity::Spawn() {
  1750. idBounds b;
  1751. b = idBounds( spawnArgs.GetVector( "origin" ) ).Expand( 16 );
  1752. qhandle_t portal = gameRenderWorld->FindPortal( b );
  1753. if ( !portal ) {
  1754. gameLocal.Warning( "LocationSeparator '%s' didn't contac…

Large files files are truncated, but you can click here to view the full file