triggers.qc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /* Copyright (C) 1996-2022 id Software LLC
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. See file, 'COPYING', for details.
  14. */
  15. entity stemp, otemp, s, old;
  16. void() trigger_reactivate =
  17. {
  18. self.solid = SOLID_TRIGGER;
  19. };
  20. //=============================================================================
  21. float SPAWNFLAG_NOMESSAGE = 1;
  22. float SPAWNFLAG_NOTOUCH = 1;
  23. // the wait time has passed, so set back up for another activation
  24. void() multi_wait =
  25. {
  26. if (self.max_health)
  27. {
  28. self.health = self.max_health;
  29. self.takedamage = DAMAGE_YES;
  30. self.solid = SOLID_BBOX;
  31. }
  32. };
  33. // the trigger was just touched/killed/used
  34. // self.enemy should be set to the activator so it can be held through a delay
  35. // so wait for the delay time before firing
  36. void() multi_trigger =
  37. {
  38. if (self.nextthink > time)
  39. {
  40. return; // allready been triggered
  41. }
  42. if (self.classname == "trigger_secret")
  43. {
  44. if (self.enemy.classname != "player")
  45. return;
  46. found_secrets = found_secrets + 1;
  47. WriteByte (MSG_ALL, SVC_FOUNDSECRET);
  48. msg_entity = self.enemy;
  49. WriteByte (MSG_ONE, SVC_ACHIEVEMENT);
  50. WriteString(MSG_ONE, "ACH_FIND_SECRET");
  51. }
  52. if (self.enemy.classname == "player" && world.model == "maps/hip3m1.bsp" && self.message == "$map_not_supposed_be_here")
  53. {
  54. msg_entity = self.enemy;
  55. WriteByte (MSG_ONE, SVC_ACHIEVEMENT);
  56. WriteString(MSG_ONE, "ACH_FIND_SUPPOSED_BE_HERE");
  57. }
  58. if (self.noise)
  59. sound (self, CHAN_VOICE, self.noise, 1, ATTN_NORM);
  60. // don't trigger again until reset
  61. self.takedamage = DAMAGE_NO;
  62. activator = self.enemy;
  63. SUB_UseTargets();
  64. if (self.wait > 0)
  65. {
  66. self.think = multi_wait;
  67. self.nextthink = time + self.wait;
  68. }
  69. else
  70. { // we can't just remove (self) here, because this is a touch function
  71. // called wheil C code is looping through area links...
  72. self.touch = SUB_Null;
  73. self.nextthink = time + 0.1;
  74. self.think = SUB_Remove;
  75. }
  76. //MED 12/01/96 added cnt stuff
  77. if (self.cnt > 0)
  78. {
  79. self.cnt = self.cnt - 1;
  80. if (self.cnt == 0)
  81. {
  82. self.touch = SUB_Null;
  83. self.nextthink = time + 0.1;
  84. self.think = SUB_Remove;
  85. }
  86. }
  87. };
  88. void() multi_killed =
  89. {
  90. self.enemy = damage_attacker;
  91. multi_trigger();
  92. };
  93. void() multi_use =
  94. {
  95. self.enemy = activator;
  96. multi_trigger();
  97. };
  98. void() multi_touch =
  99. {
  100. if (other.classname != "player")
  101. return;
  102. // if the trigger has an angles field, check player's facing direction
  103. if (self.movedir != '0 0 0')
  104. {
  105. makevectors (other.angles);
  106. if (v_forward * self.movedir < 0)
  107. return; // not facing the right way
  108. }
  109. self.enemy = other;
  110. multi_trigger ();
  111. };
  112. //MED 12/01/96 added count field
  113. /*QUAKED trigger_multiple (.5 .5 .5) ? notouch
  114. Variable sized repeatable trigger. Must be targeted at one or more entities. If "health" is set, the trigger must be killed to activate each time.
  115. If "delay" is set, the trigger waits some time after activating before firing.
  116. "wait" : Seconds between triggerings. (.2 default)
  117. "cnt" how many times it can be triggered (infinite default)
  118. If notouch is set, the trigger is only fired by other entities, not by touching.
  119. NOTOUCH has been obsoleted by trigger_relay!
  120. sounds
  121. 1) secret
  122. 2) beep beep
  123. 3) large switch
  124. 4)
  125. set "message" to text string
  126. */
  127. void() trigger_multiple =
  128. {
  129. if (self.sounds == 1)
  130. {
  131. precache_sound ("misc/secret.wav");
  132. self.noise = "misc/secret.wav";
  133. }
  134. else if (self.sounds == 2)
  135. {
  136. precache_sound ("misc/talk.wav");
  137. self.noise = "misc/talk.wav";
  138. }
  139. else if (self.sounds == 3)
  140. {
  141. precache_sound ("misc/trigger1.wav");
  142. self.noise = "misc/trigger1.wav";
  143. }
  144. if (!self.wait)
  145. self.wait = 0.2;
  146. self.use = multi_use;
  147. InitTrigger ();
  148. if (self.health)
  149. {
  150. if (self.spawnflags & SPAWNFLAG_NOTOUCH)
  151. objerror ("health and notouch don't make sense\n");
  152. self.max_health = self.health;
  153. self.th_die = multi_killed;
  154. self.takedamage = DAMAGE_YES;
  155. self.solid = SOLID_BBOX;
  156. setorigin (self, self.origin); // make sure it links into the world
  157. }
  158. else
  159. {
  160. if ( !(self.spawnflags & SPAWNFLAG_NOTOUCH) )
  161. {
  162. self.touch = multi_touch;
  163. }
  164. }
  165. //MED 12/01/96 added cnt stuff
  166. if (self.cnt == 0)
  167. self.cnt = -1;
  168. };
  169. /*QUAKED trigger_once (.5 .5 .5) ? notouch
  170. Variable sized trigger. Triggers once, then removes itself. You must set the key "target" to the name of another object in the level that has a matching
  171. "targetname". If "health" is set, the trigger must be killed to activate.
  172. If notouch is set, the trigger is only fired by other entities, not by touching.
  173. if "killtarget" is set, any objects that have a matching "target" will be removed when the trigger is fired.
  174. if "angle" is set, the trigger will only fire when someone is facing the direction of the angle. Use "360" for an angle of 0.
  175. sounds
  176. 1) secret
  177. 2) beep beep
  178. 3) large switch
  179. 4)
  180. set "message" to text string
  181. */
  182. void() trigger_once =
  183. {
  184. self.wait = -1;
  185. trigger_multiple();
  186. };
  187. //=============================================================================
  188. /*QUAKED trigger_relay (.5 .5 .5) (-8 -8 -8) (8 8 8)
  189. This fixed size trigger cannot be touched, it can only be fired by other events. It can contain killtargets, targets, delays, and messages.
  190. */
  191. void() trigger_relay =
  192. {
  193. self.use = SUB_UseTargets;
  194. };
  195. //=============================================================================
  196. /*QUAKED trigger_secret (.5 .5 .5) ?
  197. secret counter trigger
  198. sounds
  199. 1) secret
  200. 2) beep beep
  201. 3)
  202. 4)
  203. set "message" to text string
  204. */
  205. void() trigger_secret =
  206. {
  207. total_secrets = total_secrets + 1;
  208. self.wait = -1;
  209. if (!self.message)
  210. self.message = "$qc_found_secret";
  211. if (!self.sounds)
  212. self.sounds = 1;
  213. if (self.sounds == 1)
  214. {
  215. precache_sound ("misc/secret.wav");
  216. self.noise = "misc/secret.wav";
  217. }
  218. else if (self.sounds == 2)
  219. {
  220. precache_sound ("misc/talk.wav");
  221. self.noise = "misc/talk.wav";
  222. }
  223. trigger_multiple ();
  224. };
  225. //=============================================================================
  226. void() counter_use =
  227. {
  228. local string junk;
  229. self.count = self.count - 1;
  230. if (self.count < 0)
  231. return;
  232. if (self.count != 0)
  233. {
  234. if (activator.classname == "player"
  235. && (self.spawnflags & SPAWNFLAG_NOMESSAGE) == 0)
  236. {
  237. if (self.count >= 4)
  238. centerprint (activator, "$qc_more_go");
  239. else if (self.count == 3)
  240. centerprint (activator, "$qc_three_more");
  241. else if (self.count == 2)
  242. centerprint (activator, "$qc_two_more");
  243. else
  244. centerprint (activator, "$qc_one_more");
  245. }
  246. return;
  247. }
  248. if (activator.classname == "player"
  249. && (self.spawnflags & SPAWNFLAG_NOMESSAGE) == 0)
  250. centerprint(activator, "$qc_sequence_completed");
  251. self.enemy = activator;
  252. multi_trigger ();
  253. };
  254. /*QUAKED trigger_counter (.5 .5 .5) ? nomessage
  255. Acts as an intermediary for an action that takes multiple inputs.
  256. If nomessage is not set, t will print "1 more.. " etc when triggered and "sequence complete" when finished.
  257. After the counter has been triggered "count" times (default 2), it will fire all of it's targets and remove itself.
  258. */
  259. void() trigger_counter =
  260. {
  261. self.wait = -1;
  262. if (!self.count)
  263. self.count = 2;
  264. self.use = counter_use;
  265. };
  266. /*
  267. ==============================================================================
  268. TELEPORT TRIGGERS
  269. ==============================================================================
  270. */
  271. float PLAYER_ONLY = 1;
  272. float SILENT = 2;
  273. void() play_teleport =
  274. {
  275. local float v;
  276. local string tmpstr;
  277. v = random() * 5;
  278. if (v < 1)
  279. tmpstr = "misc/r_tele1.wav";
  280. else if (v < 2)
  281. tmpstr = "misc/r_tele2.wav";
  282. else if (v < 3)
  283. tmpstr = "misc/r_tele3.wav";
  284. else if (v < 4)
  285. tmpstr = "misc/r_tele4.wav";
  286. else
  287. tmpstr = "misc/r_tele5.wav";
  288. sound (self, CHAN_VOICE, tmpstr, 1, ATTN_NORM);
  289. remove (self);
  290. };
  291. void(vector org) spawn_tfog =
  292. {
  293. s = spawn ();
  294. s.origin = org;
  295. s.nextthink = time + 0.2;
  296. s.think = play_teleport;
  297. WriteByte (MSG_BROADCAST, SVC_TEMPENTITY);
  298. WriteByte (MSG_BROADCAST, TE_TELEPORT);
  299. WriteCoord (MSG_BROADCAST, org_x);
  300. WriteCoord (MSG_BROADCAST, org_y);
  301. WriteCoord (MSG_BROADCAST, org_z);
  302. };
  303. void() tdeath_touch =
  304. {
  305. if (other == self.owner)
  306. return;
  307. // frag anyone who teleports in on top of an invincible player
  308. if (other.classname == "player")
  309. {
  310. if (other.invincible_finished > time)
  311. self.classname = "teledeath2";
  312. if (self.owner.classname != "player")
  313. { // other monsters explode themselves
  314. T_Damage (self.owner, self, self, 50000);
  315. return;
  316. }
  317. }
  318. if (other.health)
  319. {
  320. T_Damage (other, self, self, 50000);
  321. }
  322. };
  323. void(vector org, entity death_owner) spawn_tdeath =
  324. {
  325. local entity death;
  326. death = spawn();
  327. death.classname = "teledeath";
  328. death.movetype = MOVETYPE_NONE;
  329. death.solid = SOLID_TRIGGER;
  330. death.angles = '0 0 0';
  331. setsize (death, death_owner.mins - '1 1 1', death_owner.maxs + '1 1 1');
  332. setorigin (death, org);
  333. death.touch = tdeath_touch;
  334. death.nextthink = time + 0.2;
  335. death.think = SUB_Remove;
  336. death.owner = death_owner;
  337. force_retouch = 2; // make sure even still objects get hit
  338. };
  339. void() teleport_touch =
  340. {
  341. local entity t;
  342. local vector org;
  343. if (self.targetname)
  344. {
  345. if (self.nextthink < time)
  346. {
  347. return; // not fired yet
  348. }
  349. }
  350. if (self.spawnflags & PLAYER_ONLY)
  351. {
  352. if (other.classname != "player")
  353. return;
  354. }
  355. // only teleport living creatures
  356. if (other.health <= 0 || other.solid != SOLID_SLIDEBOX)
  357. return;
  358. SUB_UseTargets ();
  359. // put a tfog where the player was
  360. spawn_tfog (other.origin);
  361. t = find (world, targetname, self.target);
  362. if (!t)
  363. objerror ("couldn't find target");
  364. // spawn a tfog flash in front of the destination
  365. makevectors (t.mangle);
  366. org = t.origin + 32 * v_forward;
  367. spawn_tfog (org);
  368. spawn_tdeath(t.origin, other);
  369. // move the player and lock him down for a little while
  370. if (!other.health)
  371. {
  372. other.origin = t.origin;
  373. other.velocity = (v_forward * other.velocity_x) + (v_forward * other.velocity_y);
  374. return;
  375. }
  376. setorigin (other, t.origin);
  377. other.angles = t.mangle;
  378. if (other.classname == "player")
  379. {
  380. other.fixangle = 1; // turn this way immediately
  381. other.teleport_time = time + 0.7;
  382. if (other.flags & FL_ONGROUND)
  383. other.flags = other.flags - FL_ONGROUND;
  384. other.velocity = v_forward * 300;
  385. }
  386. other.flags = other.flags - other.flags & FL_ONGROUND;
  387. };
  388. /*QUAKED info_teleport_destination (.5 .5 .5) (-8 -8 -8) (8 8 32)
  389. This is the destination marker for a teleporter. It should have a "targetname" field with the same value as a teleporter's "target" field.
  390. */
  391. void() info_teleport_destination =
  392. {
  393. // this does nothing, just serves as a target spot
  394. self.mangle = self.angles;
  395. self.angles = '0 0 0';
  396. self.model = "";
  397. self.origin = self.origin + '0 0 27';
  398. if (!self.targetname)
  399. objerror ("no targetname");
  400. };
  401. void() teleport_use =
  402. {
  403. self.nextthink = time + 0.2;
  404. force_retouch = 2; // make sure even still objects get hit
  405. self.think = SUB_Null;
  406. };
  407. /*QUAKED trigger_teleport (.5 .5 .5) ? PLAYER_ONLY SILENT
  408. Any object touching this will be transported to the corresponding info_teleport_destination entity. You must set the "target" field, and create an object with a "targetname" field that matches.
  409. If the trigger_teleport has a targetname, it will only teleport entities when it has been fired.
  410. */
  411. void() trigger_teleport =
  412. {
  413. local vector o;
  414. InitTrigger ();
  415. self.touch = teleport_touch;
  416. // find the destination
  417. if (!self.target)
  418. objerror ("no target");
  419. self.use = teleport_use;
  420. if (!(self.spawnflags & SILENT))
  421. {
  422. precache_sound ("ambience/hum1.wav");
  423. o = (self.mins + self.maxs)*0.5;
  424. ambientsound (o, "ambience/hum1.wav",0.5 , ATTN_STATIC);
  425. }
  426. };
  427. /*
  428. ==============================================================================
  429. trigger_setskill
  430. ==============================================================================
  431. */
  432. void() trigger_skill_touch =
  433. {
  434. if (other.classname != "player")
  435. return;
  436. cvar_set ("skill", self.message);
  437. };
  438. /*QUAKED trigger_setskill (.5 .5 .5) ?
  439. sets skill level to the value of "message".
  440. Only used on start map.
  441. */
  442. void() trigger_setskill =
  443. {
  444. InitTrigger ();
  445. self.touch = trigger_skill_touch;
  446. };
  447. /*
  448. ==============================================================================
  449. ONLY REGISTERED TRIGGERS
  450. ==============================================================================
  451. */
  452. void() trigger_onlyregistered_touch =
  453. {
  454. if (other.classname != "player")
  455. return;
  456. if (self.attack_finished > time)
  457. return;
  458. self.attack_finished = time + 2;
  459. if (cvar("registered"))
  460. {
  461. self.message = "";
  462. SUB_UseTargets ();
  463. remove (self);
  464. }
  465. else
  466. {
  467. if (self.message != "")
  468. {
  469. centerprint (other, self.message);
  470. sound (other, CHAN_BODY, "misc/talk.wav", 1, ATTN_NORM);
  471. }
  472. }
  473. };
  474. /*QUAKED trigger_onlyregistered (.5 .5 .5) ?
  475. Only fires if playing the registered version, otherwise prints the message
  476. */
  477. void() trigger_onlyregistered =
  478. {
  479. precache_sound ("misc/talk.wav");
  480. InitTrigger ();
  481. self.touch = trigger_onlyregistered_touch;
  482. };
  483. //============================================================================
  484. //JIM
  485. void( entity ent, float amount ) hurt_setdamage =
  486. {
  487. ent.dmg = amount;
  488. if ( !amount )
  489. {
  490. ent.solid = SOLID_NOT;
  491. }
  492. else
  493. {
  494. ent.solid = SOLID_TRIGGER;
  495. }
  496. ent.nextthink = -1;
  497. };
  498. void() hurt_on =
  499. {
  500. self.solid = SOLID_TRIGGER;
  501. self.nextthink = -1;
  502. };
  503. void() hurt_touch =
  504. {
  505. if (other.takedamage)
  506. {
  507. self.solid = SOLID_NOT;
  508. T_Damage (other, self, self, self.dmg);
  509. self.think = hurt_on;
  510. self.nextthink = time + 1;
  511. //MED 12/01/96 added count stuff
  512. if (self.cnt > 0)
  513. {
  514. self.cnt = self.cnt - 1;
  515. if (self.cnt == 0)
  516. {
  517. self.touch = SUB_Null;
  518. self.nextthink = time + 0.1;
  519. self.think = SUB_Remove;
  520. }
  521. }
  522. }
  523. return;
  524. };
  525. //MED 12/01/96 added count field
  526. /*QUAKED trigger_hurt (.5 .5 .5) ?
  527. Any object touching this will be hurt
  528. set dmg to damage amount
  529. defalt dmg = 5
  530. "cnt" default infinite, how many times to trigger
  531. */
  532. void() trigger_hurt =
  533. {
  534. InitTrigger ();
  535. self.touch = hurt_touch;
  536. if (!self.dmg)
  537. self.dmg = 5;
  538. //MED 12/01/96 added count stuff
  539. if (self.cnt == 0)
  540. self.cnt = -1;
  541. };
  542. //============================================================================
  543. float PUSH_ONCE = 1;
  544. void() trigger_push_touch =
  545. {
  546. if (other.classname == "grenade")
  547. other.velocity = self.speed * self.movedir * 10;
  548. else if (other.health > 0)
  549. {
  550. other.velocity = self.speed * self.movedir * 10;
  551. if (other.classname == "player")
  552. {
  553. if (other.fly_sound < time)
  554. {
  555. other.fly_sound = time + 1.5;
  556. sound (other, CHAN_AUTO, "ambience/windfly.wav", 1, ATTN_NORM);
  557. }
  558. }
  559. }
  560. if (self.spawnflags & PUSH_ONCE)
  561. remove(self);
  562. };
  563. /*QUAKED trigger_push (.5 .5 .5) ? PUSH_ONCE
  564. Pushes the player
  565. */
  566. void() trigger_push =
  567. {
  568. InitTrigger ();
  569. precache_sound ("ambience/windfly.wav");
  570. self.touch = trigger_push_touch;
  571. if (!self.speed)
  572. self.speed = 1000;
  573. };
  574. //============================================================================
  575. void() trigger_monsterjump_touch =
  576. {
  577. if ( other.flags & (FL_MONSTER | FL_FLY | FL_SWIM) != FL_MONSTER )
  578. return;
  579. // set XY even if not on ground, so the jump will clear lips
  580. other.velocity_x = self.movedir_x * self.speed;
  581. other.velocity_y = self.movedir_y * self.speed;
  582. if ( !(other.flags & FL_ONGROUND) )
  583. return;
  584. other.flags = other.flags - FL_ONGROUND;
  585. other.velocity_z = self.height;
  586. //MED 12/01/96 added count stuff
  587. if (self.cnt>0)
  588. {
  589. self.cnt = self.cnt - 1;
  590. if (self.cnt == 0)
  591. {
  592. self.touch = SUB_Null;
  593. self.nextthink = time + 0.1;
  594. self.think = SUB_Remove;
  595. }
  596. }
  597. };
  598. //MED 12/01/96 added count field
  599. /*QUAKED trigger_monsterjump (.5 .5 .5) ?
  600. Walking monsters that touch this will jump in the direction of the trigger's angle
  601. "speed" default to 200, the speed thrown forward
  602. "height" default to 200, the speed thrown upwards
  603. "cnt" default infinite, how many times to trigger
  604. */
  605. void() trigger_monsterjump =
  606. {
  607. if (!self.speed)
  608. self.speed = 200;
  609. if (!self.height)
  610. self.height = 200;
  611. if (self.angles == '0 0 0')
  612. self.angles = '0 360 0';
  613. //MED 12/01/96 added count stuff
  614. if (self.cnt == 0)
  615. self.cnt = -1;
  616. InitTrigger ();
  617. self.touch = trigger_monsterjump_touch;
  618. };