triggers.qc 16 KB

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