g_trigger.c 14 KB

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