home *** CD-ROM | disk | FTP | other *** search
/ Gambler 19 / GAMBLERCD19.BIN / UTILS / 3D / BRONIE / DUAL_LAU.ZIP / src / g_trigger.c < prev    next >
C/C++ Source or Header  |  1997-11-22  |  12KB  |  538 lines

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