triggers.qc 15 KB

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