Trigger.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "Game_local.h"
  23. /*
  24. ===============================================================================
  25. idTrigger
  26. ===============================================================================
  27. */
  28. const idEventDef EV_Enable( "enable", NULL );
  29. const idEventDef EV_Disable( "disable", NULL );
  30. CLASS_DECLARATION( idEntity, idTrigger )
  31. EVENT( EV_Enable, idTrigger::Event_Enable )
  32. EVENT( EV_Disable, idTrigger::Event_Disable )
  33. END_CLASS
  34. /*
  35. ================
  36. idTrigger::DrawDebugInfo
  37. ================
  38. */
  39. void idTrigger::DrawDebugInfo( void ) {
  40. idMat3 axis = gameLocal.GetLocalPlayer()->viewAngles.ToMat3();
  41. idVec3 up = axis[ 2 ] * 5.0f;
  42. idBounds viewTextBounds( gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin() );
  43. idBounds viewBounds( gameLocal.GetLocalPlayer()->GetPhysics()->GetOrigin() );
  44. idBounds box( idVec3( -4.0f, -4.0f, -4.0f ), idVec3( 4.0f, 4.0f, 4.0f ) );
  45. idEntity *ent;
  46. idEntity *target;
  47. int i;
  48. bool show;
  49. const function_t *func;
  50. viewTextBounds.ExpandSelf( 128.0f );
  51. viewBounds.ExpandSelf( 512.0f );
  52. for( ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() ) {
  53. if ( ent->GetPhysics()->GetContents() & ( CONTENTS_TRIGGER | CONTENTS_FLASHLIGHT_TRIGGER ) ) {
  54. show = viewBounds.IntersectsBounds( ent->GetPhysics()->GetAbsBounds() );
  55. if ( !show ) {
  56. for( i = 0; i < ent->targets.Num(); i++ ) {
  57. target = ent->targets[ i ].GetEntity();
  58. if ( target && viewBounds.IntersectsBounds( target->GetPhysics()->GetAbsBounds() ) ) {
  59. show = true;
  60. break;
  61. }
  62. }
  63. }
  64. if ( !show ) {
  65. continue;
  66. }
  67. gameRenderWorld->DebugBounds( colorOrange, ent->GetPhysics()->GetAbsBounds() );
  68. if ( viewTextBounds.IntersectsBounds( ent->GetPhysics()->GetAbsBounds() ) ) {
  69. gameRenderWorld->DrawText( ent->name.c_str(), ent->GetPhysics()->GetAbsBounds().GetCenter(), 0.1f, colorWhite, axis, 1 );
  70. gameRenderWorld->DrawText( ent->GetEntityDefName(), ent->GetPhysics()->GetAbsBounds().GetCenter() + up, 0.1f, colorWhite, axis, 1 );
  71. if ( ent->IsType( idTrigger::Type ) ) {
  72. func = static_cast<idTrigger *>( ent )->GetScriptFunction();
  73. } else {
  74. func = NULL;
  75. }
  76. if ( func ) {
  77. gameRenderWorld->DrawText( va( "call script '%s'", func->Name() ), ent->GetPhysics()->GetAbsBounds().GetCenter() - up, 0.1f, colorWhite, axis, 1 );
  78. }
  79. }
  80. for( i = 0; i < ent->targets.Num(); i++ ) {
  81. target = ent->targets[ i ].GetEntity();
  82. if ( target ) {
  83. gameRenderWorld->DebugArrow( colorYellow, ent->GetPhysics()->GetAbsBounds().GetCenter(), target->GetPhysics()->GetOrigin(), 10, 0 );
  84. gameRenderWorld->DebugBounds( colorGreen, box, target->GetPhysics()->GetOrigin() );
  85. if ( viewTextBounds.IntersectsBounds( target->GetPhysics()->GetAbsBounds() ) ) {
  86. gameRenderWorld->DrawText( target->name.c_str(), target->GetPhysics()->GetAbsBounds().GetCenter(), 0.1f, colorWhite, axis, 1 );
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. /*
  94. ================
  95. idTrigger::Enable
  96. ================
  97. */
  98. void idTrigger::Enable( void ) {
  99. GetPhysics()->SetContents( CONTENTS_TRIGGER );
  100. GetPhysics()->EnableClip();
  101. }
  102. /*
  103. ================
  104. idTrigger::Disable
  105. ================
  106. */
  107. void idTrigger::Disable( void ) {
  108. // we may be relinked if we're bound to another object, so clear the contents as well
  109. GetPhysics()->SetContents( 0 );
  110. GetPhysics()->DisableClip();
  111. }
  112. /*
  113. ================
  114. idTrigger::CallScript
  115. ================
  116. */
  117. void idTrigger::CallScript( void ) const {
  118. idThread *thread;
  119. if ( scriptFunction ) {
  120. thread = new idThread( scriptFunction );
  121. thread->DelayedStart( 0 );
  122. }
  123. }
  124. /*
  125. ================
  126. idTrigger::GetScriptFunction
  127. ================
  128. */
  129. const function_t *idTrigger::GetScriptFunction( void ) const {
  130. return scriptFunction;
  131. }
  132. /*
  133. ================
  134. idTrigger::Save
  135. ================
  136. */
  137. void idTrigger::Save( idSaveGame *savefile ) const {
  138. if ( scriptFunction ) {
  139. savefile->WriteString( scriptFunction->Name() );
  140. } else {
  141. savefile->WriteString( "" );
  142. }
  143. }
  144. /*
  145. ================
  146. idTrigger::Restore
  147. ================
  148. */
  149. void idTrigger::Restore( idRestoreGame *savefile ) {
  150. idStr funcname;
  151. savefile->ReadString( funcname );
  152. if ( funcname.Length() ) {
  153. scriptFunction = gameLocal.program.FindFunction( funcname );
  154. if ( scriptFunction == NULL ) {
  155. gameLocal.Warning( "idTrigger_Multi '%s' at (%s) calls unknown function '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), funcname.c_str() );
  156. }
  157. } else {
  158. scriptFunction = NULL;
  159. }
  160. }
  161. /*
  162. ================
  163. idTrigger::Event_Enable
  164. ================
  165. */
  166. void idTrigger::Event_Enable( void ) {
  167. Enable();
  168. }
  169. /*
  170. ================
  171. idTrigger::Event_Disable
  172. ================
  173. */
  174. void idTrigger::Event_Disable( void ) {
  175. Disable();
  176. }
  177. /*
  178. ================
  179. idTrigger::idTrigger
  180. ================
  181. */
  182. idTrigger::idTrigger() {
  183. scriptFunction = NULL;
  184. }
  185. /*
  186. ================
  187. idTrigger::Spawn
  188. ================
  189. */
  190. void idTrigger::Spawn( void ) {
  191. GetPhysics()->SetContents( CONTENTS_TRIGGER );
  192. idStr funcname = spawnArgs.GetString( "call", "" );
  193. if ( funcname.Length() ) {
  194. scriptFunction = gameLocal.program.FindFunction( funcname );
  195. if ( scriptFunction == NULL ) {
  196. gameLocal.Warning( "trigger '%s' at (%s) calls unknown function '%s'", name.c_str(), GetPhysics()->GetOrigin().ToString(0), funcname.c_str() );
  197. }
  198. } else {
  199. scriptFunction = NULL;
  200. }
  201. }
  202. /*
  203. ===============================================================================
  204. idTrigger_Multi
  205. ===============================================================================
  206. */
  207. const idEventDef EV_TriggerAction( "<triggerAction>", "e" );
  208. CLASS_DECLARATION( idTrigger, idTrigger_Multi )
  209. EVENT( EV_Touch, idTrigger_Multi::Event_Touch )
  210. EVENT( EV_Activate, idTrigger_Multi::Event_Trigger )
  211. EVENT( EV_TriggerAction, idTrigger_Multi::Event_TriggerAction )
  212. END_CLASS
  213. /*
  214. ================
  215. idTrigger_Multi::idTrigger_Multi
  216. ================
  217. */
  218. idTrigger_Multi::idTrigger_Multi( void ) {
  219. wait = 0.0f;
  220. random = 0.0f;
  221. delay = 0.0f;
  222. random_delay = 0.0f;
  223. nextTriggerTime = 0;
  224. removeItem = 0;
  225. touchClient = false;
  226. touchOther = false;
  227. triggerFirst = false;
  228. triggerWithSelf = false;
  229. }
  230. /*
  231. ================
  232. idTrigger_Multi::Save
  233. ================
  234. */
  235. void idTrigger_Multi::Save( idSaveGame *savefile ) const {
  236. savefile->WriteFloat( wait );
  237. savefile->WriteFloat( random );
  238. savefile->WriteFloat( delay );
  239. savefile->WriteFloat( random_delay );
  240. savefile->WriteInt( nextTriggerTime );
  241. savefile->WriteString( requires );
  242. savefile->WriteInt( removeItem );
  243. savefile->WriteBool( touchClient );
  244. savefile->WriteBool( touchOther );
  245. savefile->WriteBool( triggerFirst );
  246. savefile->WriteBool( triggerWithSelf );
  247. }
  248. /*
  249. ================
  250. idTrigger_Multi::Restore
  251. ================
  252. */
  253. void idTrigger_Multi::Restore( idRestoreGame *savefile ) {
  254. savefile->ReadFloat( wait );
  255. savefile->ReadFloat( random );
  256. savefile->ReadFloat( delay );
  257. savefile->ReadFloat( random_delay );
  258. savefile->ReadInt( nextTriggerTime );
  259. savefile->ReadString( requires );
  260. savefile->ReadInt( removeItem );
  261. savefile->ReadBool( touchClient );
  262. savefile->ReadBool( touchOther );
  263. savefile->ReadBool( triggerFirst );
  264. savefile->ReadBool( triggerWithSelf );
  265. }
  266. /*
  267. ================
  268. idTrigger_Multi::Spawn
  269. "wait" : Seconds between triggerings, 0.5 default, -1 = one time only.
  270. "call" : Script function to call when triggered
  271. "random" wait variance, default is 0
  272. Variable sized repeatable trigger. Must be targeted at one or more entities.
  273. so, the basic time between firing is a random time between
  274. (wait - random) and (wait + random)
  275. ================
  276. */
  277. void idTrigger_Multi::Spawn( void ) {
  278. spawnArgs.GetFloat( "wait", "0.5", wait );
  279. spawnArgs.GetFloat( "random", "0", random );
  280. spawnArgs.GetFloat( "delay", "0", delay );
  281. spawnArgs.GetFloat( "random_delay", "0", random_delay );
  282. if ( random && ( random >= wait ) && ( wait >= 0 ) ) {
  283. random = wait - 1;
  284. gameLocal.Warning( "idTrigger_Multi '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  285. }
  286. if ( random_delay && ( random_delay >= delay ) && ( delay >= 0 ) ) {
  287. random_delay = delay - 1;
  288. gameLocal.Warning( "idTrigger_Multi '%s' at (%s) has random_delay >= delay", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  289. }
  290. spawnArgs.GetString( "requires", "", requires );
  291. spawnArgs.GetInt( "removeItem", "0", removeItem );
  292. spawnArgs.GetBool( "triggerFirst", "0", triggerFirst );
  293. spawnArgs.GetBool( "triggerWithSelf", "0", triggerWithSelf );
  294. if ( spawnArgs.GetBool( "anyTouch" ) ) {
  295. touchClient = true;
  296. touchOther = true;
  297. } else if ( spawnArgs.GetBool( "noTouch" ) ) {
  298. touchClient = false;
  299. touchOther = false;
  300. } else if ( spawnArgs.GetBool( "noClient" ) ) {
  301. touchClient = false;
  302. touchOther = true;
  303. } else {
  304. touchClient = true;
  305. touchOther = false;
  306. }
  307. nextTriggerTime = 0;
  308. if ( spawnArgs.GetBool( "flashlight_trigger" ) ) {
  309. GetPhysics()->SetContents( CONTENTS_FLASHLIGHT_TRIGGER );
  310. } else {
  311. GetPhysics()->SetContents( CONTENTS_TRIGGER );
  312. }
  313. }
  314. /*
  315. ================
  316. idTrigger_Multi::CheckFacing
  317. ================
  318. */
  319. bool idTrigger_Multi::CheckFacing( idEntity *activator ) {
  320. if ( spawnArgs.GetBool( "facing" ) ) {
  321. if ( !activator->IsType( idPlayer::Type ) ) {
  322. return true;
  323. }
  324. idPlayer *player = static_cast< idPlayer* >( activator );
  325. float dot = player->viewAngles.ToForward() * GetPhysics()->GetAxis()[0];
  326. float angle = RAD2DEG( idMath::ACos( dot ) );
  327. if ( angle > spawnArgs.GetFloat( "angleLimit", "30" ) ) {
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. /*
  334. ================
  335. idTrigger_Multi::TriggerAction
  336. ================
  337. */
  338. void idTrigger_Multi::TriggerAction( idEntity *activator ) {
  339. ActivateTargets( triggerWithSelf ? this : activator );
  340. CallScript();
  341. if ( wait >= 0 ) {
  342. nextTriggerTime = gameLocal.time + SEC2MS( wait + random * gameLocal.random.CRandomFloat() );
  343. } else {
  344. // we can't just remove (this) here, because this is a touch function
  345. // called while looping through area links...
  346. nextTriggerTime = gameLocal.time + 1;
  347. PostEventMS( &EV_Remove, 0 );
  348. }
  349. }
  350. /*
  351. ================
  352. idTrigger_Multi::Event_TriggerAction
  353. ================
  354. */
  355. void idTrigger_Multi::Event_TriggerAction( idEntity *activator ) {
  356. TriggerAction( activator );
  357. }
  358. /*
  359. ================
  360. idTrigger_Multi::Event_Trigger
  361. the trigger was just activated
  362. activated should be the entity that originated the activation sequence (ie. the original target)
  363. activator should be set to the activator so it can be held through a delay
  364. so wait for the delay time before firing
  365. ================
  366. */
  367. void idTrigger_Multi::Event_Trigger( idEntity *activator ) {
  368. if ( nextTriggerTime > gameLocal.time ) {
  369. // can't retrigger until the wait is over
  370. return;
  371. }
  372. // see if this trigger requires an item
  373. if ( !gameLocal.RequirementMet( activator, requires, removeItem ) ) {
  374. return;
  375. }
  376. if ( !CheckFacing( activator ) ) {
  377. return;
  378. }
  379. if ( triggerFirst ) {
  380. triggerFirst = false;
  381. return;
  382. }
  383. // don't allow it to trigger twice in a single frame
  384. nextTriggerTime = gameLocal.time + 1;
  385. if ( delay > 0 ) {
  386. // don't allow it to trigger again until our delay has passed
  387. nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
  388. PostEventSec( &EV_TriggerAction, delay, activator );
  389. } else {
  390. TriggerAction( activator );
  391. }
  392. }
  393. /*
  394. ================
  395. idTrigger_Multi::Event_Touch
  396. ================
  397. */
  398. void idTrigger_Multi::Event_Touch( idEntity *other, trace_t *trace ) {
  399. if( triggerFirst ) {
  400. return;
  401. }
  402. bool player = other->IsType( idPlayer::Type );
  403. if ( player ) {
  404. if ( !touchClient ) {
  405. return;
  406. }
  407. if ( static_cast< idPlayer * >( other )->spectating ) {
  408. return;
  409. }
  410. } else if ( !touchOther ) {
  411. return;
  412. }
  413. if ( nextTriggerTime > gameLocal.time ) {
  414. // can't retrigger until the wait is over
  415. return;
  416. }
  417. // see if this trigger requires an item
  418. if ( !gameLocal.RequirementMet( other, requires, removeItem ) ) {
  419. return;
  420. }
  421. if ( !CheckFacing( other ) ) {
  422. return;
  423. }
  424. if ( spawnArgs.GetBool( "toggleTriggerFirst" ) ) {
  425. triggerFirst = true;
  426. }
  427. nextTriggerTime = gameLocal.time + 1;
  428. if ( delay > 0 ) {
  429. // don't allow it to trigger again until our delay has passed
  430. nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
  431. PostEventSec( &EV_TriggerAction, delay, other );
  432. } else {
  433. TriggerAction( other );
  434. }
  435. }
  436. /*
  437. ===============================================================================
  438. idTrigger_EntityName
  439. ===============================================================================
  440. */
  441. CLASS_DECLARATION( idTrigger, idTrigger_EntityName )
  442. EVENT( EV_Touch, idTrigger_EntityName::Event_Touch )
  443. EVENT( EV_Activate, idTrigger_EntityName::Event_Trigger )
  444. EVENT( EV_TriggerAction, idTrigger_EntityName::Event_TriggerAction )
  445. END_CLASS
  446. /*
  447. ================
  448. idTrigger_EntityName::idTrigger_EntityName
  449. ================
  450. */
  451. idTrigger_EntityName::idTrigger_EntityName( void ) {
  452. wait = 0.0f;
  453. random = 0.0f;
  454. delay = 0.0f;
  455. random_delay = 0.0f;
  456. nextTriggerTime = 0;
  457. triggerFirst = false;
  458. }
  459. /*
  460. ================
  461. idTrigger_EntityName::Save
  462. ================
  463. */
  464. void idTrigger_EntityName::Save( idSaveGame *savefile ) const {
  465. savefile->WriteFloat( wait );
  466. savefile->WriteFloat( random );
  467. savefile->WriteFloat( delay );
  468. savefile->WriteFloat( random_delay );
  469. savefile->WriteInt( nextTriggerTime );
  470. savefile->WriteBool( triggerFirst );
  471. savefile->WriteString( entityName );
  472. }
  473. /*
  474. ================
  475. idTrigger_EntityName::Restore
  476. ================
  477. */
  478. void idTrigger_EntityName::Restore( idRestoreGame *savefile ) {
  479. savefile->ReadFloat( wait );
  480. savefile->ReadFloat( random );
  481. savefile->ReadFloat( delay );
  482. savefile->ReadFloat( random_delay );
  483. savefile->ReadInt( nextTriggerTime );
  484. savefile->ReadBool( triggerFirst );
  485. savefile->ReadString( entityName );
  486. }
  487. /*
  488. ================
  489. idTrigger_EntityName::Spawn
  490. ================
  491. */
  492. void idTrigger_EntityName::Spawn( void ) {
  493. spawnArgs.GetFloat( "wait", "0.5", wait );
  494. spawnArgs.GetFloat( "random", "0", random );
  495. spawnArgs.GetFloat( "delay", "0", delay );
  496. spawnArgs.GetFloat( "random_delay", "0", random_delay );
  497. if ( random && ( random >= wait ) && ( wait >= 0 ) ) {
  498. random = wait - 1;
  499. gameLocal.Warning( "idTrigger_EntityName '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  500. }
  501. if ( random_delay && ( random_delay >= delay ) && ( delay >= 0 ) ) {
  502. random_delay = delay - 1;
  503. gameLocal.Warning( "idTrigger_EntityName '%s' at (%s) has random_delay >= delay", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  504. }
  505. spawnArgs.GetBool( "triggerFirst", "0", triggerFirst );
  506. entityName = spawnArgs.GetString( "entityname" );
  507. if ( !entityName.Length() ) {
  508. gameLocal.Error( "idTrigger_EntityName '%s' at (%s) doesn't have 'entityname' key specified", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  509. }
  510. nextTriggerTime = 0;
  511. if ( !spawnArgs.GetBool( "noTouch" ) ) {
  512. GetPhysics()->SetContents( CONTENTS_TRIGGER );
  513. }
  514. }
  515. /*
  516. ================
  517. idTrigger_EntityName::TriggerAction
  518. ================
  519. */
  520. void idTrigger_EntityName::TriggerAction( idEntity *activator ) {
  521. ActivateTargets( activator );
  522. CallScript();
  523. if ( wait >= 0 ) {
  524. nextTriggerTime = gameLocal.time + SEC2MS( wait + random * gameLocal.random.CRandomFloat() );
  525. } else {
  526. // we can't just remove (this) here, because this is a touch function
  527. // called while looping through area links...
  528. nextTriggerTime = gameLocal.time + 1;
  529. PostEventMS( &EV_Remove, 0 );
  530. }
  531. }
  532. /*
  533. ================
  534. idTrigger_EntityName::Event_TriggerAction
  535. ================
  536. */
  537. void idTrigger_EntityName::Event_TriggerAction( idEntity *activator ) {
  538. TriggerAction( activator );
  539. }
  540. /*
  541. ================
  542. idTrigger_EntityName::Event_Trigger
  543. the trigger was just activated
  544. activated should be the entity that originated the activation sequence (ie. the original target)
  545. activator should be set to the activator so it can be held through a delay
  546. so wait for the delay time before firing
  547. ================
  548. */
  549. void idTrigger_EntityName::Event_Trigger( idEntity *activator ) {
  550. if ( nextTriggerTime > gameLocal.time ) {
  551. // can't retrigger until the wait is over
  552. return;
  553. }
  554. if ( !activator || ( activator->name != entityName ) ) {
  555. return;
  556. }
  557. if ( triggerFirst ) {
  558. triggerFirst = false;
  559. return;
  560. }
  561. // don't allow it to trigger twice in a single frame
  562. nextTriggerTime = gameLocal.time + 1;
  563. if ( delay > 0 ) {
  564. // don't allow it to trigger again until our delay has passed
  565. nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
  566. PostEventSec( &EV_TriggerAction, delay, activator );
  567. } else {
  568. TriggerAction( activator );
  569. }
  570. }
  571. /*
  572. ================
  573. idTrigger_EntityName::Event_Touch
  574. ================
  575. */
  576. void idTrigger_EntityName::Event_Touch( idEntity *other, trace_t *trace ) {
  577. if( triggerFirst ) {
  578. return;
  579. }
  580. if ( nextTriggerTime > gameLocal.time ) {
  581. // can't retrigger until the wait is over
  582. return;
  583. }
  584. if ( !other || ( other->name != entityName ) ) {
  585. return;
  586. }
  587. nextTriggerTime = gameLocal.time + 1;
  588. if ( delay > 0 ) {
  589. // don't allow it to trigger again until our delay has passed
  590. nextTriggerTime += SEC2MS( delay + random_delay * gameLocal.random.CRandomFloat() );
  591. PostEventSec( &EV_TriggerAction, delay, other );
  592. } else {
  593. TriggerAction( other );
  594. }
  595. }
  596. /*
  597. ===============================================================================
  598. idTrigger_Timer
  599. ===============================================================================
  600. */
  601. const idEventDef EV_Timer( "<timer>", NULL );
  602. CLASS_DECLARATION( idTrigger, idTrigger_Timer )
  603. EVENT( EV_Timer, idTrigger_Timer::Event_Timer )
  604. EVENT( EV_Activate, idTrigger_Timer::Event_Use )
  605. END_CLASS
  606. /*
  607. ================
  608. idTrigger_Timer::idTrigger_Timer
  609. ================
  610. */
  611. idTrigger_Timer::idTrigger_Timer( void ) {
  612. random = 0.0f;
  613. wait = 0.0f;
  614. on = false;
  615. delay = 0.0f;
  616. }
  617. /*
  618. ================
  619. idTrigger_Timer::Save
  620. ================
  621. */
  622. void idTrigger_Timer::Save( idSaveGame *savefile ) const {
  623. savefile->WriteFloat( random );
  624. savefile->WriteFloat( wait );
  625. savefile->WriteBool( on );
  626. savefile->WriteFloat( delay );
  627. savefile->WriteString( onName );
  628. savefile->WriteString( offName );
  629. }
  630. /*
  631. ================
  632. idTrigger_Timer::Restore
  633. ================
  634. */
  635. void idTrigger_Timer::Restore( idRestoreGame *savefile ) {
  636. savefile->ReadFloat( random );
  637. savefile->ReadFloat( wait );
  638. savefile->ReadBool( on );
  639. savefile->ReadFloat( delay );
  640. savefile->ReadString( onName );
  641. savefile->ReadString( offName );
  642. }
  643. /*
  644. ================
  645. idTrigger_Timer::Spawn
  646. Repeatedly fires its targets.
  647. Can be turned on or off by using.
  648. ================
  649. */
  650. void idTrigger_Timer::Spawn( void ) {
  651. spawnArgs.GetFloat( "random", "1", random );
  652. spawnArgs.GetFloat( "wait", "1", wait );
  653. spawnArgs.GetBool( "start_on", "0", on );
  654. spawnArgs.GetFloat( "delay", "0", delay );
  655. onName = spawnArgs.GetString( "onName" );
  656. offName = spawnArgs.GetString( "offName" );
  657. if ( random >= wait && wait >= 0 ) {
  658. random = wait - 0.001;
  659. gameLocal.Warning( "idTrigger_Timer '%s' at (%s) has random >= wait", name.c_str(), GetPhysics()->GetOrigin().ToString(0) );
  660. }
  661. if ( on ) {
  662. PostEventSec( &EV_Timer, delay );
  663. }
  664. }
  665. /*
  666. ================
  667. idTrigger_Timer::Enable
  668. ================
  669. */
  670. void idTrigger_Timer::Enable( void ) {
  671. // if off, turn it on
  672. if ( !on ) {
  673. on = true;
  674. PostEventSec( &EV_Timer, delay );
  675. }
  676. }
  677. /*
  678. ================
  679. idTrigger_Timer::Disable
  680. ================
  681. */
  682. void idTrigger_Timer::Disable( void ) {
  683. // if on, turn it off
  684. if ( on ) {
  685. on = false;
  686. CancelEvents( &EV_Timer );
  687. }
  688. }
  689. /*
  690. ================
  691. idTrigger_Timer::Event_Timer
  692. ================
  693. */
  694. void idTrigger_Timer::Event_Timer( void ) {
  695. ActivateTargets( this );
  696. // set time before next firing
  697. if ( wait >= 0.0f ) {
  698. PostEventSec( &EV_Timer, wait + gameLocal.random.CRandomFloat() * random );
  699. }
  700. }
  701. /*
  702. ================
  703. idTrigger_Timer::Event_Use
  704. ================
  705. */
  706. void idTrigger_Timer::Event_Use( idEntity *activator ) {
  707. // if on, turn it off
  708. if ( on ) {
  709. if ( offName.Length() && offName.Icmp( activator->GetName() ) ) {
  710. return;
  711. }
  712. on = false;
  713. CancelEvents( &EV_Timer );
  714. } else {
  715. // turn it on
  716. if ( onName.Length() && onName.Icmp( activator->GetName() ) ) {
  717. return;
  718. }
  719. on = true;
  720. PostEventSec( &EV_Timer, delay );
  721. }
  722. }
  723. /*
  724. ===============================================================================
  725. idTrigger_Count
  726. ===============================================================================
  727. */
  728. CLASS_DECLARATION( idTrigger, idTrigger_Count )
  729. EVENT( EV_Activate, idTrigger_Count::Event_Trigger )
  730. EVENT( EV_TriggerAction, idTrigger_Count::Event_TriggerAction )
  731. END_CLASS
  732. /*
  733. ================
  734. idTrigger_Count::idTrigger_Count
  735. ================
  736. */
  737. idTrigger_Count::idTrigger_Count( void ) {
  738. goal = 0;
  739. count = 0;
  740. delay = 0.0f;
  741. }
  742. /*
  743. ================
  744. idTrigger_Count::Save
  745. ================
  746. */
  747. void idTrigger_Count::Save( idSaveGame *savefile ) const {
  748. savefile->WriteInt( goal );
  749. savefile->WriteInt( count );
  750. savefile->WriteFloat( delay );
  751. }
  752. /*
  753. ================
  754. idTrigger_Count::Restore
  755. ================
  756. */
  757. void idTrigger_Count::Restore( idRestoreGame *savefile ) {
  758. savefile->ReadInt( goal );
  759. savefile->ReadInt( count );
  760. savefile->ReadFloat( delay );
  761. }
  762. /*
  763. ================
  764. idTrigger_Count::Spawn
  765. ================
  766. */
  767. void idTrigger_Count::Spawn( void ) {
  768. spawnArgs.GetInt( "count", "1", goal );
  769. spawnArgs.GetFloat( "delay", "0", delay );
  770. count = 0;
  771. }
  772. /*
  773. ================
  774. idTrigger_Count::Event_Trigger
  775. ================
  776. */
  777. void idTrigger_Count::Event_Trigger( idEntity *activator ) {
  778. // goal of -1 means trigger has been exhausted
  779. if (goal >= 0) {
  780. count++;
  781. if ( count >= goal ) {
  782. if (spawnArgs.GetBool("repeat")) {
  783. count = 0;
  784. } else {
  785. goal = -1;
  786. }
  787. PostEventSec( &EV_TriggerAction, delay, activator );
  788. }
  789. }
  790. }
  791. /*
  792. ================
  793. idTrigger_Count::Event_TriggerAction
  794. ================
  795. */
  796. void idTrigger_Count::Event_TriggerAction( idEntity *activator ) {
  797. ActivateTargets( activator );
  798. CallScript();
  799. if ( goal == -1 ) {
  800. PostEventMS( &EV_Remove, 0 );
  801. }
  802. }
  803. /*
  804. ===============================================================================
  805. idTrigger_Hurt
  806. ===============================================================================
  807. */
  808. CLASS_DECLARATION( idTrigger, idTrigger_Hurt )
  809. EVENT( EV_Touch, idTrigger_Hurt::Event_Touch )
  810. EVENT( EV_Activate, idTrigger_Hurt::Event_Toggle )
  811. END_CLASS
  812. /*
  813. ================
  814. idTrigger_Hurt::idTrigger_Hurt
  815. ================
  816. */
  817. idTrigger_Hurt::idTrigger_Hurt( void ) {
  818. on = false;
  819. delay = 0.0f;
  820. nextTime = 0;
  821. }
  822. /*
  823. ================
  824. idTrigger_Hurt::Save
  825. ================
  826. */
  827. void idTrigger_Hurt::Save( idSaveGame *savefile ) const {
  828. savefile->WriteBool( on );
  829. savefile->WriteFloat( delay );
  830. savefile->WriteInt( nextTime );
  831. }
  832. /*
  833. ================
  834. idTrigger_Hurt::Restore
  835. ================
  836. */
  837. void idTrigger_Hurt::Restore( idRestoreGame *savefile ) {
  838. savefile->ReadBool( on );
  839. savefile->ReadFloat( delay );
  840. savefile->ReadInt( nextTime );
  841. }
  842. /*
  843. ================
  844. idTrigger_Hurt::Spawn
  845. Damages activator
  846. Can be turned on or off by using.
  847. ================
  848. */
  849. void idTrigger_Hurt::Spawn( void ) {
  850. spawnArgs.GetBool( "on", "1", on );
  851. spawnArgs.GetFloat( "delay", "1.0", delay );
  852. nextTime = gameLocal.time;
  853. Enable();
  854. }
  855. /*
  856. ================
  857. idTrigger_Hurt::Event_Touch
  858. ================
  859. */
  860. void idTrigger_Hurt::Event_Touch( idEntity *other, trace_t *trace ) {
  861. const char *damage;
  862. if ( on && other && gameLocal.time >= nextTime ) {
  863. damage = spawnArgs.GetString( "def_damage", "damage_painTrigger" );
  864. other->Damage( NULL, NULL, vec3_origin, damage, 1.0f, INVALID_JOINT );
  865. ActivateTargets( other );
  866. CallScript();
  867. nextTime = gameLocal.time + SEC2MS( delay );
  868. }
  869. }
  870. /*
  871. ================
  872. idTrigger_Hurt::Event_Toggle
  873. ================
  874. */
  875. void idTrigger_Hurt::Event_Toggle( idEntity *activator ) {
  876. on = !on;
  877. }
  878. /*
  879. ===============================================================================
  880. idTrigger_Fade
  881. ===============================================================================
  882. */
  883. CLASS_DECLARATION( idTrigger, idTrigger_Fade )
  884. EVENT( EV_Activate, idTrigger_Fade::Event_Trigger )
  885. END_CLASS
  886. /*
  887. ================
  888. idTrigger_Fade::Event_Trigger
  889. ================
  890. */
  891. void idTrigger_Fade::Event_Trigger( idEntity *activator ) {
  892. idVec4 fadeColor;
  893. int fadeTime;
  894. idPlayer *player;
  895. player = gameLocal.GetLocalPlayer();
  896. if ( player ) {
  897. fadeColor = spawnArgs.GetVec4( "fadeColor", "0, 0, 0, 1" );
  898. fadeTime = SEC2MS( spawnArgs.GetFloat( "fadeTime", "0.5" ) );
  899. player->playerView.Fade( fadeColor, fadeTime );
  900. PostEventMS( &EV_ActivateTargets, fadeTime, activator );
  901. }
  902. }
  903. /*
  904. ===============================================================================
  905. idTrigger_Touch
  906. ===============================================================================
  907. */
  908. CLASS_DECLARATION( idTrigger, idTrigger_Touch )
  909. EVENT( EV_Activate, idTrigger_Touch::Event_Trigger )
  910. END_CLASS
  911. /*
  912. ================
  913. idTrigger_Touch::idTrigger_Touch
  914. ================
  915. */
  916. idTrigger_Touch::idTrigger_Touch( void ) {
  917. clipModel = NULL;
  918. }
  919. /*
  920. ================
  921. idTrigger_Touch::Spawn
  922. ================
  923. */
  924. void idTrigger_Touch::Spawn( void ) {
  925. // get the clip model
  926. clipModel = new idClipModel( GetPhysics()->GetClipModel() );
  927. // remove the collision model from the physics object
  928. GetPhysics()->SetClipModel( NULL, 1.0f );
  929. if ( spawnArgs.GetBool( "start_on" ) ) {
  930. BecomeActive( TH_THINK );
  931. }
  932. }
  933. /*
  934. ================
  935. idTrigger_Touch::Save
  936. ================
  937. */
  938. void idTrigger_Touch::Save( idSaveGame *savefile ) {
  939. savefile->WriteClipModel( clipModel );
  940. }
  941. /*
  942. ================
  943. idTrigger_Touch::Restore
  944. ================
  945. */
  946. void idTrigger_Touch::Restore( idRestoreGame *savefile ) {
  947. savefile->ReadClipModel( clipModel );
  948. }
  949. /*
  950. ================
  951. idTrigger_Touch::TouchEntities
  952. ================
  953. */
  954. void idTrigger_Touch::TouchEntities( void ) {
  955. int numClipModels, i;
  956. idBounds bounds;
  957. idClipModel *cm, *clipModelList[ MAX_GENTITIES ];
  958. if ( clipModel == NULL || scriptFunction == NULL ) {
  959. return;
  960. }
  961. bounds.FromTransformedBounds( clipModel->GetBounds(), clipModel->GetOrigin(), clipModel->GetAxis() );
  962. numClipModels = gameLocal.clip.ClipModelsTouchingBounds( bounds, -1, clipModelList, MAX_GENTITIES );
  963. for ( i = 0; i < numClipModels; i++ ) {
  964. cm = clipModelList[ i ];
  965. if ( !cm->IsTraceModel() ) {
  966. continue;
  967. }
  968. idEntity *entity = cm->GetEntity();
  969. if ( !entity ) {
  970. continue;
  971. }
  972. if ( !gameLocal.clip.ContentsModel( cm->GetOrigin(), cm, cm->GetAxis(), -1,
  973. clipModel->Handle(), clipModel->GetOrigin(), clipModel->GetAxis() ) ) {
  974. continue;
  975. }
  976. ActivateTargets( entity );
  977. idThread *thread = new idThread();
  978. thread->CallFunction( entity, scriptFunction, false );
  979. thread->DelayedStart( 0 );
  980. }
  981. }
  982. /*
  983. ================
  984. idTrigger_Touch::Think
  985. ================
  986. */
  987. void idTrigger_Touch::Think( void ) {
  988. if ( thinkFlags & TH_THINK ) {
  989. TouchEntities();
  990. }
  991. idEntity::Think();
  992. }
  993. /*
  994. ================
  995. idTrigger_Touch::Event_Trigger
  996. ================
  997. */
  998. void idTrigger_Touch::Event_Trigger( idEntity *activator ) {
  999. if ( thinkFlags & TH_THINK ) {
  1000. BecomeInactive( TH_THINK );
  1001. } else {
  1002. BecomeActive( TH_THINK );
  1003. }
  1004. }
  1005. /*
  1006. ================
  1007. idTrigger_Touch::Enable
  1008. ================
  1009. */
  1010. void idTrigger_Touch::Enable( void ) {
  1011. BecomeActive( TH_THINK );
  1012. }
  1013. /*
  1014. ================
  1015. idTrigger_Touch::Disable
  1016. ================
  1017. */
  1018. void idTrigger_Touch::Disable( void ) {
  1019. BecomeInactive( TH_THINK );
  1020. }