g_trigger.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // Copyright (c) ZeniMax Media Inc.
  2. // Licensed under the GNU General Public License 2.0.
  3. #include "g_local.h"
  4. void InitTrigger (edict_t *self)
  5. {
  6. if (!VectorCompare (self->s.angles, vec3_origin))
  7. G_SetMovedir (self->s.angles, self->movedir);
  8. self->solid = SOLID_TRIGGER;
  9. self->movetype = MOVETYPE_NONE;
  10. gi.setmodel (self, self->model);
  11. self->svflags = SVF_NOCLIENT;
  12. }
  13. // the wait time has passed, so set back up for another activation
  14. void multi_wait (edict_t *ent)
  15. {
  16. ent->nextthink = 0;
  17. }
  18. // the trigger was just activated
  19. // ent->activator should be set to the activator so it can be held through a delay
  20. // so wait for the delay time before firing
  21. void multi_trigger (edict_t *ent)
  22. {
  23. if (ent->nextthink)
  24. return; // already been triggered
  25. G_UseTargets (ent, ent->activator);
  26. if (ent->wait > 0)
  27. {
  28. ent->think = multi_wait;
  29. ent->nextthink = level.time + ent->wait;
  30. }
  31. else
  32. { // we can't just remove (self) here, because this is a touch function
  33. // called while looping through area links...
  34. ent->touch = NULL;
  35. ent->nextthink = level.time + FRAMETIME;
  36. ent->think = G_FreeEdict;
  37. }
  38. }
  39. void Use_Multi (edict_t *ent, edict_t *other, edict_t *activator)
  40. {
  41. ent->activator = activator;
  42. multi_trigger (ent);
  43. }
  44. void Touch_Multi (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  45. {
  46. if(other->client)
  47. {
  48. if (self->spawnflags & 2)
  49. return;
  50. }
  51. else if (other->svflags & SVF_MONSTER)
  52. {
  53. if (!(self->spawnflags & 1))
  54. return;
  55. }
  56. else
  57. return;
  58. if (!VectorCompare(self->movedir, vec3_origin))
  59. {
  60. vec3_t forward;
  61. AngleVectors(other->s.angles, forward, NULL, NULL);
  62. if (_DotProduct(forward, self->movedir) < 0)
  63. return;
  64. }
  65. self->activator = other;
  66. multi_trigger (self);
  67. }
  68. /*QUAKED trigger_multiple (.5 .5 .5) ? MONSTER NOT_PLAYER TRIGGERED
  69. Variable sized repeatable trigger. Must be targeted at one or more entities.
  70. If "delay" is set, the trigger waits some time after activating before firing.
  71. "wait" : Seconds between triggerings. (.2 default)
  72. sounds
  73. 1) secret
  74. 2) beep beep
  75. 3) large switch
  76. 4)
  77. set "message" to text string
  78. */
  79. void trigger_enable (edict_t *self, edict_t *other, edict_t *activator)
  80. {
  81. self->solid = SOLID_TRIGGER;
  82. self->use = Use_Multi;
  83. gi.linkentity (self);
  84. }
  85. void SP_trigger_multiple (edict_t *ent)
  86. {
  87. if (ent->sounds == 1)
  88. ent->noise_index = gi.soundindex ("misc/secret.wav");
  89. else if (ent->sounds == 2)
  90. ent->noise_index = gi.soundindex ("misc/talk.wav");
  91. else if (ent->sounds == 3)
  92. ent->noise_index = gi.soundindex ("misc/trigger1.wav");
  93. if (!ent->wait)
  94. ent->wait = 0.2;
  95. ent->touch = Touch_Multi;
  96. ent->movetype = MOVETYPE_NONE;
  97. ent->svflags |= SVF_NOCLIENT;
  98. if (ent->spawnflags & 4)
  99. {
  100. ent->solid = SOLID_NOT;
  101. ent->use = trigger_enable;
  102. }
  103. else
  104. {
  105. ent->solid = SOLID_TRIGGER;
  106. ent->use = Use_Multi;
  107. }
  108. if (!VectorCompare(ent->s.angles, vec3_origin))
  109. G_SetMovedir (ent->s.angles, ent->movedir);
  110. gi.setmodel (ent, ent->model);
  111. gi.linkentity (ent);
  112. }
  113. /*QUAKED trigger_once (.5 .5 .5) ? x x TRIGGERED
  114. Triggers once, then removes itself.
  115. You must set the key "target" to the name of another object in the level that has a matching "targetname".
  116. If TRIGGERED, this trigger must be triggered before it is live.
  117. sounds
  118. 1) secret
  119. 2) beep beep
  120. 3) large switch
  121. 4)
  122. "message" string to be displayed when triggered
  123. */
  124. void SP_trigger_once(edict_t *ent)
  125. {
  126. // make old maps work because I messed up on flag assignments here
  127. // triggered was on bit 1 when it should have been on bit 4
  128. if (ent->spawnflags & 1)
  129. {
  130. vec3_t v;
  131. VectorMA (ent->mins, 0.5, ent->size, v);
  132. ent->spawnflags &= ~1;
  133. ent->spawnflags |= 4;
  134. gi.dprintf("fixed TRIGGERED flag on %s at %s\n", ent->classname, vtos(v));
  135. }
  136. ent->wait = -1;
  137. SP_trigger_multiple (ent);
  138. }
  139. /*QUAKED trigger_relay (.5 .5 .5) (-8 -8 -8) (8 8 8)
  140. This fixed size trigger cannot be touched, it can only be fired by other events.
  141. */
  142. void trigger_relay_use (edict_t *self, edict_t *other, edict_t *activator)
  143. {
  144. G_UseTargets (self, activator);
  145. }
  146. void SP_trigger_relay (edict_t *self)
  147. {
  148. self->use = trigger_relay_use;
  149. }
  150. /*
  151. ==============================================================================
  152. trigger_key
  153. ==============================================================================
  154. */
  155. /*QUAKED trigger_key (.5 .5 .5) (-8 -8 -8) (8 8 8)
  156. A relay trigger that only fires it's targets if player has the proper key.
  157. Use "item" to specify the required key, for example "key_data_cd"
  158. */
  159. void trigger_key_use (edict_t *self, edict_t *other, edict_t *activator)
  160. {
  161. int index;
  162. if (!self->item)
  163. return;
  164. if (!activator->client)
  165. return;
  166. index = ITEM_INDEX(self->item);
  167. if (!activator->client->pers.inventory[index])
  168. {
  169. if (level.time < self->touch_debounce_time)
  170. return;
  171. self->touch_debounce_time = level.time + 5.0;
  172. gi.centerprintf (activator, "You need the %s", self->item->pickup_name);
  173. gi.sound (activator, CHAN_AUTO, gi.soundindex ("misc/keytry.wav"), 1, ATTN_NORM, 0);
  174. return;
  175. }
  176. gi.sound (activator, CHAN_AUTO, gi.soundindex ("misc/keyuse.wav"), 1, ATTN_NORM, 0);
  177. if (coop->value)
  178. {
  179. int player;
  180. edict_t *ent;
  181. if (strcmp(self->item->classname, "key_power_cube") == 0)
  182. {
  183. int cube;
  184. for (cube = 0; cube < 8; cube++)
  185. if (activator->client->pers.power_cubes & (1 << cube))
  186. break;
  187. for (player = 1; player <= game.maxclients; player++)
  188. {
  189. ent = &g_edicts[player];
  190. if (!ent->inuse)
  191. continue;
  192. if (!ent->client)
  193. continue;
  194. if (ent->client->pers.power_cubes & (1 << cube))
  195. {
  196. ent->client->pers.inventory[index]--;
  197. ent->client->pers.power_cubes &= ~(1 << cube);
  198. }
  199. }
  200. }
  201. else
  202. {
  203. for (player = 1; player <= game.maxclients; player++)
  204. {
  205. ent = &g_edicts[player];
  206. if (!ent->inuse)
  207. continue;
  208. if (!ent->client)
  209. continue;
  210. ent->client->pers.inventory[index] = 0;
  211. }
  212. }
  213. }
  214. else
  215. {
  216. activator->client->pers.inventory[index]--;
  217. }
  218. G_UseTargets (self, activator);
  219. self->use = NULL;
  220. }
  221. void SP_trigger_key (edict_t *self)
  222. {
  223. if (!st.item)
  224. {
  225. gi.dprintf("no key item for trigger_key at %s\n", vtos(self->s.origin));
  226. return;
  227. }
  228. self->item = FindItemByClassname (st.item);
  229. if (!self->item)
  230. {
  231. gi.dprintf("item %s not found for trigger_key at %s\n", st.item, vtos(self->s.origin));
  232. return;
  233. }
  234. if (!self->target)
  235. {
  236. gi.dprintf("%s at %s has no target\n", self->classname, vtos(self->s.origin));
  237. return;
  238. }
  239. gi.soundindex ("misc/keytry.wav");
  240. gi.soundindex ("misc/keyuse.wav");
  241. self->use = trigger_key_use;
  242. }
  243. /*
  244. ==============================================================================
  245. trigger_counter
  246. ==============================================================================
  247. */
  248. /*QUAKED trigger_counter (.5 .5 .5) ? nomessage
  249. Acts as an intermediary for an action that takes multiple inputs.
  250. If nomessage is not set, t will print "1 more.. " etc when triggered and "sequence complete" when finished.
  251. After the counter has been triggered "count" times (default 2), it will fire all of it's targets and remove itself.
  252. */
  253. void trigger_counter_use(edict_t *self, edict_t *other, edict_t *activator)
  254. {
  255. if (self->count == 0)
  256. return;
  257. self->count--;
  258. if (self->count)
  259. {
  260. if (! (self->spawnflags & 1))
  261. {
  262. gi.centerprintf(activator, "%i more to go...", self->count);
  263. gi.sound (activator, CHAN_AUTO, gi.soundindex ("misc/talk1.wav"), 1, ATTN_NORM, 0);
  264. }
  265. return;
  266. }
  267. if (! (self->spawnflags & 1))
  268. {
  269. gi.centerprintf(activator, "Sequence completed!");
  270. gi.sound (activator, CHAN_AUTO, gi.soundindex ("misc/talk1.wav"), 1, ATTN_NORM, 0);
  271. }
  272. self->activator = activator;
  273. multi_trigger (self);
  274. }
  275. void SP_trigger_counter (edict_t *self)
  276. {
  277. self->wait = -1;
  278. if (!self->count)
  279. self->count = 2;
  280. self->use = trigger_counter_use;
  281. }
  282. /*
  283. ==============================================================================
  284. trigger_always
  285. ==============================================================================
  286. */
  287. /*QUAKED trigger_always (.5 .5 .5) (-8 -8 -8) (8 8 8)
  288. This trigger will always fire. It is activated by the world.
  289. */
  290. void SP_trigger_always (edict_t *ent)
  291. {
  292. // we must have some delay to make sure our use targets are present
  293. if (ent->delay < 0.2)
  294. ent->delay = 0.2;
  295. G_UseTargets(ent, ent);
  296. }
  297. /*
  298. ==============================================================================
  299. trigger_push
  300. ==============================================================================
  301. */
  302. #if 0
  303. #define PUSH_ONCE 1
  304. static int windsound;
  305. void trigger_push_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  306. {
  307. if (strcmp(other->classname, "grenade") == 0)
  308. {
  309. VectorScale (self->movedir, self->speed * 10, other->velocity);
  310. }
  311. else if (other->health > 0)
  312. {
  313. VectorScale (self->movedir, self->speed * 10, other->velocity);
  314. if (other->client)
  315. {
  316. // don't take falling damage immediately from this
  317. VectorCopy (other->velocity, other->client->oldvelocity);
  318. if (other->fly_sound_debounce_time < level.time)
  319. {
  320. other->fly_sound_debounce_time = level.time + 1.5;
  321. gi.sound (other, CHAN_AUTO, windsound, 1, ATTN_NORM, 0);
  322. }
  323. }
  324. }
  325. if (self->spawnflags & PUSH_ONCE)
  326. G_FreeEdict (self);
  327. }
  328. void SP_trigger_push (edict_t *self)
  329. {
  330. InitTrigger (self);
  331. windsound = gi.soundindex ("misc/windfly.wav");
  332. self->touch = trigger_push_touch;
  333. if (!self->speed)
  334. self->speed = 1000;
  335. gi.linkentity (self);
  336. }
  337. #endif
  338. // RAFAEL
  339. #define PUSH_ONCE 1
  340. static int windsound;
  341. void trigger_push_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  342. {
  343. if (strcmp(other->classname, "grenade") == 0)
  344. {
  345. VectorScale (self->movedir, self->speed * 10, other->velocity);
  346. }
  347. else if (other->health > 0)
  348. {
  349. VectorScale (self->movedir, self->speed * 10, other->velocity);
  350. if (other->client)
  351. {
  352. // don't take falling damage immediately from this
  353. VectorCopy (other->velocity, other->client->oldvelocity);
  354. if (other->fly_sound_debounce_time < level.time)
  355. {
  356. other->fly_sound_debounce_time = level.time + 1.5;
  357. gi.sound (other, CHAN_AUTO, windsound, 1, ATTN_NORM, 0);
  358. }
  359. }
  360. }
  361. if (self->spawnflags & PUSH_ONCE)
  362. G_FreeEdict (self);
  363. }
  364. /*QUAKED trigger_push (.5 .5 .5) ? PUSH_ONCE PUSH_PLUS PUSH_RAMP
  365. Pushes the player
  366. "speed" defaults to 1000
  367. "wait" defaults to 10 must use PUSH_PLUS used for on
  368. */
  369. void trigger_push_active (edict_t *self);
  370. void trigger_effect (edict_t *self)
  371. {
  372. vec3_t origin;
  373. vec3_t size;
  374. int i;
  375. VectorScale (self->size, 0.5, size);
  376. VectorAdd (self->absmin, size, origin);
  377. for (i=0; i<10; i++)
  378. {
  379. origin[2] += (self->speed * 0.01) * (i + random());
  380. gi.WriteByte (svc_temp_entity);
  381. gi.WriteByte (TE_TUNNEL_SPARKS);
  382. gi.WriteByte (1);
  383. gi.WritePosition (origin);
  384. gi.WriteDir (vec3_origin);
  385. gi.WriteByte (0x74 + (rand()&7));
  386. gi.multicast (self->s.origin, MULTICAST_PVS);
  387. }
  388. }
  389. void trigger_push_inactive (edict_t *self)
  390. {
  391. if (self->delay > level.time)
  392. {
  393. self->nextthink = level.time + 0.1;
  394. }
  395. else
  396. {
  397. self->touch = trigger_push_touch;
  398. self->think = trigger_push_active;
  399. self->nextthink = level.time + 0.1;
  400. self->delay = self->nextthink + self->wait;
  401. }
  402. }
  403. void trigger_push_active (edict_t *self)
  404. {
  405. if (self->delay > level.time)
  406. {
  407. self->nextthink = level.time + 0.1;
  408. trigger_effect (self);
  409. }
  410. else
  411. {
  412. self->touch = NULL;
  413. self->think = trigger_push_inactive;
  414. self->nextthink = level.time + 0.1;
  415. self->delay = self->nextthink + self->wait;
  416. }
  417. }
  418. void SP_trigger_push (edict_t *self)
  419. {
  420. InitTrigger (self);
  421. windsound = gi.soundindex ("misc/windfly.wav");
  422. self->touch = trigger_push_touch;
  423. if (self->spawnflags & 2)
  424. {
  425. if (!self->wait)
  426. self->wait = 10;
  427. self->think = trigger_push_active;
  428. self->nextthink = level.time + 0.1;
  429. self->delay = self->nextthink + self->wait;
  430. }
  431. if (!self->speed)
  432. self->speed = 1000;
  433. gi.linkentity (self);
  434. }
  435. /*
  436. ==============================================================================
  437. trigger_hurt
  438. ==============================================================================
  439. */
  440. /*QUAKED trigger_hurt (.5 .5 .5) ? START_OFF TOGGLE SILENT NO_PROTECTION SLOW
  441. Any entity that touches this will be hurt.
  442. It does dmg points of damage each server frame
  443. SILENT supresses playing the sound
  444. SLOW changes the damage rate to once per second
  445. NO_PROTECTION *nothing* stops the damage
  446. "dmg" default 5 (whole numbers only)
  447. */
  448. void hurt_use (edict_t *self, edict_t *other, edict_t *activator)
  449. {
  450. if (self->solid == SOLID_NOT)
  451. self->solid = SOLID_TRIGGER;
  452. else
  453. self->solid = SOLID_NOT;
  454. gi.linkentity (self);
  455. if (!(self->spawnflags & 2))
  456. self->use = NULL;
  457. }
  458. void hurt_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  459. {
  460. int dflags;
  461. if (!other->takedamage)
  462. return;
  463. if (self->timestamp > level.time)
  464. return;
  465. if (self->spawnflags & 16)
  466. self->timestamp = level.time + 1;
  467. else
  468. self->timestamp = level.time + FRAMETIME;
  469. if (!(self->spawnflags & 4))
  470. {
  471. if ((level.framenum % 10) == 0)
  472. gi.sound (other, CHAN_AUTO, self->noise_index, 1, ATTN_NORM, 0);
  473. }
  474. if (self->spawnflags & 8)
  475. dflags = DAMAGE_NO_PROTECTION;
  476. else
  477. dflags = 0;
  478. T_Damage (other, self, self, vec3_origin, other->s.origin, vec3_origin, self->dmg, self->dmg, dflags, MOD_TRIGGER_HURT);
  479. }
  480. void SP_trigger_hurt (edict_t *self)
  481. {
  482. InitTrigger (self);
  483. self->noise_index = gi.soundindex ("world/electro.wav");
  484. self->touch = hurt_touch;
  485. if (!self->dmg)
  486. self->dmg = 5;
  487. if (self->spawnflags & 1)
  488. self->solid = SOLID_NOT;
  489. else
  490. self->solid = SOLID_TRIGGER;
  491. if (self->spawnflags & 2)
  492. self->use = hurt_use;
  493. gi.linkentity (self);
  494. }
  495. /*
  496. ==============================================================================
  497. trigger_gravity
  498. ==============================================================================
  499. */
  500. /*QUAKED trigger_gravity (.5 .5 .5) ?
  501. Changes the touching entites gravity to
  502. the value of "gravity". 1.0 is standard
  503. gravity for the level.
  504. */
  505. void trigger_gravity_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  506. {
  507. other->gravity = self->gravity;
  508. }
  509. void SP_trigger_gravity (edict_t *self)
  510. {
  511. if (st.gravity == 0)
  512. {
  513. gi.dprintf("trigger_gravity without gravity set at %s\n", vtos(self->s.origin));
  514. G_FreeEdict (self);
  515. return;
  516. }
  517. InitTrigger (self);
  518. self->gravity = atoi(st.gravity);
  519. self->touch = trigger_gravity_touch;
  520. }
  521. /*
  522. ==============================================================================
  523. trigger_monsterjump
  524. ==============================================================================
  525. */
  526. /*QUAKED trigger_monsterjump (.5 .5 .5) ?
  527. Walking monsters that touch this will jump in the direction of the trigger's angle
  528. "speed" default to 200, the speed thrown forward
  529. "height" default to 200, the speed thrown upwards
  530. */
  531. void trigger_monsterjump_touch (edict_t *self, edict_t *other, cplane_t *plane, csurface_t *surf)
  532. {
  533. if (other->flags & (FL_FLY | FL_SWIM) )
  534. return;
  535. if (other->svflags & SVF_DEADMONSTER)
  536. return;
  537. if ( !(other->svflags & SVF_MONSTER))
  538. return;
  539. // set XY even if not on ground, so the jump will clear lips
  540. other->velocity[0] = self->movedir[0] * self->speed;
  541. other->velocity[1] = self->movedir[1] * self->speed;
  542. if (!other->groundentity)
  543. return;
  544. other->groundentity = NULL;
  545. other->velocity[2] = self->movedir[2];
  546. }
  547. void SP_trigger_monsterjump (edict_t *self)
  548. {
  549. if (!self->speed)
  550. self->speed = 200;
  551. if (!st.height)
  552. st.height = 200;
  553. if (self->s.angles[YAW] == 0)
  554. self->s.angles[YAW] = 360;
  555. InitTrigger (self);
  556. self->touch = trigger_monsterjump_touch;
  557. self->movedir[2] = st.height;
  558. }