SecurityCamera.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. SecurityCamera.cpp
  22. Security camera that triggers targets when player is in view
  23. */
  24. #include "../idlib/precompiled.h"
  25. #pragma hdrstop
  26. #include "Game_local.h"
  27. /***********************************************************************
  28. idSecurityCamera
  29. ***********************************************************************/
  30. const idEventDef EV_SecurityCam_ReverseSweep( "<reverseSweep>" );
  31. const idEventDef EV_SecurityCam_ContinueSweep( "<continueSweep>" );
  32. const idEventDef EV_SecurityCam_Pause( "<pause>" );
  33. const idEventDef EV_SecurityCam_Alert( "<alert>" );
  34. const idEventDef EV_SecurityCam_AddLight( "<addLight>" );
  35. CLASS_DECLARATION( idEntity, idSecurityCamera )
  36. EVENT( EV_SecurityCam_ReverseSweep, idSecurityCamera::Event_ReverseSweep )
  37. EVENT( EV_SecurityCam_ContinueSweep, idSecurityCamera::Event_ContinueSweep )
  38. EVENT( EV_SecurityCam_Pause, idSecurityCamera::Event_Pause )
  39. EVENT( EV_SecurityCam_Alert, idSecurityCamera::Event_Alert )
  40. EVENT( EV_SecurityCam_AddLight, idSecurityCamera::Event_AddLight )
  41. END_CLASS
  42. /*
  43. ================
  44. idSecurityCamera::Save
  45. ================
  46. */
  47. void idSecurityCamera::Save( idSaveGame *savefile ) const {
  48. savefile->WriteFloat( angle );
  49. savefile->WriteFloat( sweepAngle );
  50. savefile->WriteInt( modelAxis );
  51. savefile->WriteBool( flipAxis );
  52. savefile->WriteFloat( scanDist );
  53. savefile->WriteFloat( scanFov );
  54. savefile->WriteFloat( sweepStart );
  55. savefile->WriteFloat( sweepEnd );
  56. savefile->WriteBool( negativeSweep );
  57. savefile->WriteBool( sweeping );
  58. savefile->WriteInt( alertMode );
  59. savefile->WriteFloat( stopSweeping );
  60. savefile->WriteFloat( scanFovCos );
  61. savefile->WriteVec3( viewOffset );
  62. savefile->WriteInt( pvsArea );
  63. savefile->WriteStaticObject( physicsObj );
  64. savefile->WriteTraceModel( trm );
  65. }
  66. /*
  67. ================
  68. idSecurityCamera::Restore
  69. ================
  70. */
  71. void idSecurityCamera::Restore( idRestoreGame *savefile ) {
  72. savefile->ReadFloat( angle );
  73. savefile->ReadFloat( sweepAngle );
  74. savefile->ReadInt( modelAxis );
  75. savefile->ReadBool( flipAxis );
  76. savefile->ReadFloat( scanDist );
  77. savefile->ReadFloat( scanFov );
  78. savefile->ReadFloat( sweepStart );
  79. savefile->ReadFloat( sweepEnd );
  80. savefile->ReadBool( negativeSweep );
  81. savefile->ReadBool( sweeping );
  82. savefile->ReadInt( alertMode );
  83. savefile->ReadFloat( stopSweeping );
  84. savefile->ReadFloat( scanFovCos );
  85. savefile->ReadVec3( viewOffset );
  86. savefile->ReadInt( pvsArea );
  87. savefile->ReadStaticObject( physicsObj );
  88. savefile->ReadTraceModel( trm );
  89. }
  90. /*
  91. ================
  92. idSecurityCamera::Spawn
  93. ================
  94. */
  95. void idSecurityCamera::Spawn( void ) {
  96. idStr str;
  97. sweepAngle = spawnArgs.GetFloat( "sweepAngle", "90" );
  98. health = spawnArgs.GetInt( "health", "100" );
  99. scanFov = spawnArgs.GetFloat( "scanFov", "90" );
  100. scanDist = spawnArgs.GetFloat( "scanDist", "200" );
  101. flipAxis = spawnArgs.GetBool( "flipAxis" );
  102. modelAxis = spawnArgs.GetInt( "modelAxis" );
  103. if ( modelAxis < 0 || modelAxis > 2 ) {
  104. modelAxis = 0;
  105. }
  106. spawnArgs.GetVector( "viewOffset", "0 0 0", viewOffset );
  107. if ( spawnArgs.GetBool( "spotLight" ) ) {
  108. PostEventMS( &EV_SecurityCam_AddLight, 0 );
  109. }
  110. negativeSweep = ( sweepAngle < 0 ) ? true : false;
  111. sweepAngle = abs( sweepAngle );
  112. scanFovCos = cos( scanFov * idMath::PI / 360.0f );
  113. angle = GetPhysics()->GetAxis().ToAngles().yaw;
  114. StartSweep();
  115. SetAlertMode( SCANNING );
  116. BecomeActive( TH_THINK );
  117. if ( health ) {
  118. fl.takedamage = true;
  119. }
  120. pvsArea = gameLocal.pvs.GetPVSArea( GetPhysics()->GetOrigin() );
  121. // if no target specified use ourself
  122. str = spawnArgs.GetString( "cameraTarget" );
  123. if ( str.Length() == 0 ) {
  124. spawnArgs.Set( "cameraTarget", spawnArgs.GetString( "name" ) );
  125. }
  126. // check if a clip model is set
  127. spawnArgs.GetString( "clipmodel", "", str );
  128. if ( !str[0] ) {
  129. str = spawnArgs.GetString( "model" ); // use the visual model
  130. }
  131. if ( !collisionModelManager->TrmFromModel( str, trm ) ) {
  132. gameLocal.Error( "idSecurityCamera '%s': cannot load collision model %s", name.c_str(), str.c_str() );
  133. return;
  134. }
  135. GetPhysics()->SetContents( CONTENTS_SOLID );
  136. GetPhysics()->SetClipMask( MASK_SOLID | CONTENTS_BODY | CONTENTS_CORPSE | CONTENTS_MOVEABLECLIP );
  137. // setup the physics
  138. UpdateChangeableSpawnArgs( NULL );
  139. }
  140. /*
  141. ================
  142. idSecurityCamera::Event_AddLight
  143. ================
  144. */
  145. void idSecurityCamera::Event_AddLight( void ) {
  146. idDict args;
  147. idVec3 right, up, target, temp;
  148. idVec3 dir;
  149. float radius;
  150. idVec3 lightOffset;
  151. idLight *spotLight;
  152. dir = GetAxis();
  153. dir.NormalVectors( right, up );
  154. target = GetPhysics()->GetOrigin() + dir * scanDist;
  155. radius = tan( scanFov * idMath::PI / 360.0f );
  156. up = dir + up * radius;
  157. up.Normalize();
  158. up = GetPhysics()->GetOrigin() + up * scanDist;
  159. up -= target;
  160. right = dir + right * radius;
  161. right.Normalize();
  162. right = GetPhysics()->GetOrigin() + right * scanDist;
  163. right -= target;
  164. spawnArgs.GetVector( "lightOffset", "0 0 0", lightOffset );
  165. args.Set( "origin", ( GetPhysics()->GetOrigin() + lightOffset ).ToString() );
  166. args.Set( "light_target", target.ToString() );
  167. args.Set( "light_right", right.ToString() );
  168. args.Set( "light_up", up.ToString() );
  169. args.SetFloat( "angle", GetPhysics()->GetAxis()[0].ToYaw() );
  170. spotLight = static_cast<idLight *>( gameLocal.SpawnEntityType( idLight::Type, &args ) );
  171. spotLight->Bind( this, true );
  172. spotLight->UpdateVisuals();
  173. }
  174. /*
  175. ================
  176. idSecurityCamera::DrawFov
  177. ================
  178. */
  179. void idSecurityCamera::DrawFov( void ) {
  180. int i;
  181. float radius, a, s, c, halfRadius;
  182. idVec3 right, up;
  183. idVec4 color(1, 0, 0, 1), color2(0, 0, 1, 1);
  184. idVec3 lastPoint, point, lastHalfPoint, halfPoint, center;
  185. idVec3 dir = GetAxis();
  186. dir.NormalVectors( right, up );
  187. radius = tan( scanFov * idMath::PI / 360.0f );
  188. halfRadius = radius * 0.5f;
  189. lastPoint = dir + up * radius;
  190. lastPoint.Normalize();
  191. lastPoint = GetPhysics()->GetOrigin() + lastPoint * scanDist;
  192. lastHalfPoint = dir + up * halfRadius;
  193. lastHalfPoint.Normalize();
  194. lastHalfPoint = GetPhysics()->GetOrigin() + lastHalfPoint * scanDist;
  195. center = GetPhysics()->GetOrigin() + dir * scanDist;
  196. for ( i = 1; i < 12; i++ ) {
  197. a = idMath::TWO_PI * i / 12.0f;
  198. idMath::SinCos( a, s, c );
  199. point = dir + right * s * radius + up * c * radius;
  200. point.Normalize();
  201. point = GetPhysics()->GetOrigin() + point * scanDist;
  202. gameRenderWorld->DebugLine( color, lastPoint, point );
  203. gameRenderWorld->DebugLine( color, GetPhysics()->GetOrigin(), point );
  204. lastPoint = point;
  205. halfPoint = dir + right * s * halfRadius + up * c * halfRadius;
  206. halfPoint.Normalize();
  207. halfPoint = GetPhysics()->GetOrigin() + halfPoint * scanDist;
  208. gameRenderWorld->DebugLine( color2, point, halfPoint );
  209. gameRenderWorld->DebugLine( color2, lastHalfPoint, halfPoint );
  210. lastHalfPoint = halfPoint;
  211. gameRenderWorld->DebugLine( color2, halfPoint, center );
  212. }
  213. }
  214. /*
  215. ================
  216. idSecurityCamera::GetRenderView
  217. ================
  218. */
  219. renderView_t *idSecurityCamera::GetRenderView() {
  220. renderView_t *rv = idEntity::GetRenderView();
  221. rv->fov_x = scanFov;
  222. rv->fov_y = scanFov;
  223. rv->viewaxis = GetAxis().ToAngles().ToMat3();
  224. rv->vieworg = GetPhysics()->GetOrigin() + viewOffset;
  225. return rv;
  226. }
  227. /*
  228. ================
  229. idSecurityCamera::CanSeePlayer
  230. ================
  231. */
  232. bool idSecurityCamera::CanSeePlayer( void ) {
  233. int i;
  234. float dist;
  235. idPlayer *ent;
  236. trace_t tr;
  237. idVec3 dir;
  238. pvsHandle_t handle;
  239. handle = gameLocal.pvs.SetupCurrentPVS( pvsArea );
  240. for ( i = 0; i < gameLocal.numClients; i++ ) {
  241. ent = static_cast<idPlayer*>(gameLocal.entities[ i ]);
  242. if ( !ent || ( ent->fl.notarget ) ) {
  243. continue;
  244. }
  245. // if there is no way we can see this player
  246. if ( !gameLocal.pvs.InCurrentPVS( handle, ent->GetPVSAreas(), ent->GetNumPVSAreas() ) ) {
  247. continue;
  248. }
  249. dir = ent->GetPhysics()->GetOrigin() - GetPhysics()->GetOrigin();
  250. dist = dir.Normalize();
  251. if ( dist > scanDist ) {
  252. continue;
  253. }
  254. if ( dir * GetAxis() < scanFovCos ) {
  255. continue;
  256. }
  257. idVec3 eye;
  258. eye = ent->EyeOffset();
  259. gameLocal.clip.TracePoint( tr, GetPhysics()->GetOrigin(), ent->GetPhysics()->GetOrigin() + eye, MASK_OPAQUE, this );
  260. if ( tr.fraction == 1.0 || ( gameLocal.GetTraceEntity( tr ) == ent ) ) {
  261. gameLocal.pvs.FreeCurrentPVS( handle );
  262. return true;
  263. }
  264. }
  265. gameLocal.pvs.FreeCurrentPVS( handle );
  266. return false;
  267. }
  268. /*
  269. ================
  270. idSecurityCamera::SetAlertMode
  271. ================
  272. */
  273. void idSecurityCamera::SetAlertMode( int alert ) {
  274. if (alert >= SCANNING && alert <= ACTIVATED) {
  275. alertMode = alert;
  276. }
  277. renderEntity.shaderParms[ SHADERPARM_MODE ] = alertMode;
  278. UpdateVisuals();
  279. }
  280. /*
  281. ================
  282. idSecurityCamera::Think
  283. ================
  284. */
  285. void idSecurityCamera::Think( void ) {
  286. float pct;
  287. float travel;
  288. if ( thinkFlags & TH_THINK ) {
  289. if ( g_showEntityInfo.GetBool() ) {
  290. DrawFov();
  291. }
  292. if (health <= 0) {
  293. BecomeInactive( TH_THINK );
  294. return;
  295. }
  296. }
  297. // run physics
  298. RunPhysics();
  299. if ( thinkFlags & TH_THINK ) {
  300. if (CanSeePlayer()) {
  301. if (alertMode == SCANNING) {
  302. float sightTime;
  303. SetAlertMode(ALERT);
  304. stopSweeping = gameLocal.time;
  305. if (sweeping) {
  306. CancelEvents( &EV_SecurityCam_Pause );
  307. } else {
  308. CancelEvents( &EV_SecurityCam_ReverseSweep );
  309. }
  310. sweeping = false;
  311. StopSound( SND_CHANNEL_ANY, false );
  312. StartSound( "snd_sight", SND_CHANNEL_BODY, 0, false, NULL );
  313. sightTime = spawnArgs.GetFloat( "sightTime", "5" );
  314. PostEventSec(&EV_SecurityCam_Alert, sightTime);
  315. }
  316. } else {
  317. if (alertMode == ALERT) {
  318. float sightResume;
  319. SetAlertMode(LOSINGINTEREST);
  320. CancelEvents( &EV_SecurityCam_Alert );
  321. sightResume = spawnArgs.GetFloat( "sightResume", "1.5" );
  322. PostEventSec( &EV_SecurityCam_ContinueSweep, sightResume );
  323. }
  324. if ( sweeping ) {
  325. idAngles a = GetPhysics()->GetAxis().ToAngles();
  326. pct = ( gameLocal.time - sweepStart ) / ( sweepEnd - sweepStart );
  327. travel = pct * sweepAngle;
  328. if ( negativeSweep ) {
  329. a.yaw = angle + travel;
  330. } else {
  331. a.yaw = angle - travel;
  332. }
  333. SetAngles( a );
  334. }
  335. }
  336. }
  337. Present();
  338. }
  339. /*
  340. ================
  341. idSecurityCamera::GetAxis
  342. ================
  343. */
  344. const idVec3 idSecurityCamera::GetAxis( void ) const {
  345. return (flipAxis) ? -GetPhysics()->GetAxis()[modelAxis] : GetPhysics()->GetAxis()[modelAxis];
  346. };
  347. /*
  348. ================
  349. idSecurityCamera::SweepSpeed
  350. ================
  351. */
  352. float idSecurityCamera::SweepSpeed( void ) const {
  353. return spawnArgs.GetFloat( "sweepSpeed", "5" );
  354. }
  355. /*
  356. ================
  357. idSecurityCamera::StartSweep
  358. ================
  359. */
  360. void idSecurityCamera::StartSweep( void ) {
  361. int speed;
  362. sweeping = true;
  363. sweepStart = gameLocal.time;
  364. speed = SEC2MS( SweepSpeed() );
  365. sweepEnd = sweepStart + speed;
  366. PostEventMS( &EV_SecurityCam_Pause, speed );
  367. StartSound( "snd_moving", SND_CHANNEL_BODY, 0, false, NULL );
  368. }
  369. /*
  370. ================
  371. idSecurityCamera::Event_ContinueSweep
  372. ================
  373. */
  374. void idSecurityCamera::Event_ContinueSweep( void ) {
  375. float pct = (stopSweeping - sweepStart) / (sweepEnd - sweepStart);
  376. float f = gameLocal.time - (sweepEnd - sweepStart) * pct;
  377. int speed;
  378. sweepStart = f;
  379. speed = MS2SEC( SweepSpeed() );
  380. sweepEnd = sweepStart + speed;
  381. PostEventMS( &EV_SecurityCam_Pause, speed * (1.0 - pct));
  382. StartSound( "snd_moving", SND_CHANNEL_BODY, 0, false, NULL );
  383. SetAlertMode(SCANNING);
  384. sweeping = true;
  385. }
  386. /*
  387. ================
  388. idSecurityCamera::Event_Alert
  389. ================
  390. */
  391. void idSecurityCamera::Event_Alert( void ) {
  392. float wait;
  393. SetAlertMode(ACTIVATED);
  394. StopSound( SND_CHANNEL_ANY, false );
  395. StartSound( "snd_activate", SND_CHANNEL_BODY, 0, false, NULL );
  396. ActivateTargets(this);
  397. CancelEvents( &EV_SecurityCam_ContinueSweep );
  398. wait = spawnArgs.GetFloat( "wait", "20" );
  399. PostEventSec( &EV_SecurityCam_ContinueSweep, wait );
  400. }
  401. /*
  402. ================
  403. idSecurityCamera::Event_ReverseSweep
  404. ================
  405. */
  406. void idSecurityCamera::Event_ReverseSweep( void ) {
  407. angle = GetPhysics()->GetAxis().ToAngles().yaw;
  408. negativeSweep = !negativeSweep;
  409. StartSweep();
  410. }
  411. /*
  412. ================
  413. idSecurityCamera::Event_Pause
  414. ================
  415. */
  416. void idSecurityCamera::Event_Pause( void ) {
  417. float sweepWait;
  418. sweepWait = spawnArgs.GetFloat( "sweepWait", "0.5" );
  419. sweeping = false;
  420. StopSound( SND_CHANNEL_ANY, false );
  421. StartSound( "snd_stop", SND_CHANNEL_BODY, 0, false, NULL );
  422. PostEventSec( &EV_SecurityCam_ReverseSweep, sweepWait );
  423. }
  424. /*
  425. ============
  426. idSecurityCamera::Killed
  427. ============
  428. */
  429. void idSecurityCamera::Killed( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location ) {
  430. sweeping = false;
  431. StopSound( SND_CHANNEL_ANY, false );
  432. const char *fx = spawnArgs.GetString( "fx_destroyed" );
  433. if ( fx[0] != '\0' ) {
  434. idEntityFx::StartFx( fx, NULL, NULL, this, true );
  435. }
  436. physicsObj.SetSelf( this );
  437. physicsObj.SetClipModel( new idClipModel( trm ), 0.02f );
  438. physicsObj.SetOrigin( GetPhysics()->GetOrigin() );
  439. physicsObj.SetAxis( GetPhysics()->GetAxis() );
  440. physicsObj.SetBouncyness( 0.2f );
  441. physicsObj.SetFriction( 0.6f, 0.6f, 0.2f );
  442. physicsObj.SetGravity( gameLocal.GetGravity() );
  443. physicsObj.SetContents( CONTENTS_SOLID );
  444. physicsObj.SetClipMask( MASK_SOLID | CONTENTS_BODY | CONTENTS_CORPSE | CONTENTS_MOVEABLECLIP );
  445. SetPhysics( &physicsObj );
  446. physicsObj.DropToFloor();
  447. }
  448. /*
  449. ============
  450. idSecurityCamera::Pain
  451. ============
  452. */
  453. bool idSecurityCamera::Pain( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location ) {
  454. const char *fx = spawnArgs.GetString( "fx_damage" );
  455. if ( fx[0] != '\0' ) {
  456. idEntityFx::StartFx( fx, NULL, NULL, this, true );
  457. }
  458. return true;
  459. }
  460. /*
  461. ================
  462. idSecurityCamera::Present
  463. Present is called to allow entities to generate refEntities, lights, etc for the renderer.
  464. ================
  465. */
  466. void idSecurityCamera::Present( void ) {
  467. // don't present to the renderer if the entity hasn't changed
  468. if ( !( thinkFlags & TH_UPDATEVISUALS ) ) {
  469. return;
  470. }
  471. BecomeInactive( TH_UPDATEVISUALS );
  472. // camera target for remote render views
  473. if ( cameraTarget ) {
  474. renderEntity.remoteRenderView = cameraTarget->GetRenderView();
  475. }
  476. // if set to invisible, skip
  477. if ( !renderEntity.hModel || IsHidden() ) {
  478. return;
  479. }
  480. // add to refresh list
  481. if ( modelDefHandle == -1 ) {
  482. modelDefHandle = gameRenderWorld->AddEntityDef( &renderEntity );
  483. } else {
  484. gameRenderWorld->UpdateEntityDef( modelDefHandle, &renderEntity );
  485. }
  486. }