triggers.qc 15 KB

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