home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / quake / programs / qplus11 / subs.qc < prev    next >
Encoding:
Text File  |  1996-08-22  |  5.9 KB  |  306 lines

  1. void() SUB_Null = {};
  2.  
  3. void() SUB_Remove = {remove(self);};
  4.  
  5. /*
  6. QuakeEd only writes a single float for angles (bad idea), so up and down are
  7. just constant angles.
  8. */
  9. vector() SetMovedir =
  10. {
  11.     if (self.angles == '0 -1 0')
  12.         self.movedir = '0 0 1';
  13.     else if (self.angles == '0 -2 0')
  14.         self.movedir = '0 0 -1';
  15.     else
  16.     {
  17.         makevectors (self.angles);
  18.         self.movedir = v_forward;
  19.     }
  20.     
  21.     self.angles = '0 0 0';
  22. };
  23.  
  24. /*
  25. ================
  26. InitTrigger
  27. ================
  28. */
  29. void() InitTrigger =
  30. {
  31. // trigger angles are used for one-way touches.  An angle of 0 is assumed
  32. // to mean no restrictions, so use a yaw of 360 instead.
  33.     if (self.angles != '0 0 0')
  34.         SetMovedir ();
  35.     self.solid = SOLID_TRIGGER;
  36.     setmodel (self, self.model);    // set size and link into world
  37.     self.movetype = MOVETYPE_NONE;
  38.     self.modelindex = 0;
  39.     self.model = "";
  40. };
  41.  
  42. /*
  43. =============
  44. SUB_CalcMove
  45.  
  46. calculate self.velocity and self.nextthink to reach dest from
  47. self.origin traveling at speed
  48. ===============
  49. */
  50. void(entity ent, vector tdest, float tspeed, void() func) SUB_CalcMoveEnt =
  51. {
  52. local entity    stemp;
  53.     stemp = self;
  54.     self = ent;
  55.  
  56.     SUB_CalcMove (tdest, tspeed, func);
  57.     self = stemp;
  58. };
  59.  
  60. void(vector tdest, float tspeed, void() func) SUB_CalcMove =
  61. {
  62. local vector    vdestdelta;
  63. local float        len, traveltime;
  64.  
  65.     if (!tspeed)
  66.         objerror("No speed is defined!");
  67.  
  68.     self.think1 = func;
  69.     self.finaldest = tdest;
  70.     self.think = SUB_CalcMoveDone;
  71.  
  72.     if (tdest == self.origin)
  73.     {
  74.         self.velocity = '0 0 0';
  75.         self.nextthink = self.ltime + 0.1;
  76.         return;
  77.     }
  78.         
  79. // set destdelta to the vector needed to move
  80.     vdestdelta = tdest - self.origin;
  81.     
  82. // calculate length of vector
  83.     len = vlen (vdestdelta);
  84.     
  85. // divide by speed to get time to reach dest
  86.     traveltime = len / tspeed;
  87.  
  88.     if (traveltime < 0.1)
  89.     {
  90.         self.velocity = '0 0 0';
  91.         self.nextthink = self.ltime + 0.1;
  92.         return;
  93.     }
  94.     
  95. // set nextthink to trigger a think when dest is reached
  96.     self.nextthink = self.ltime + traveltime;
  97.  
  98. // scale the destdelta vector by the time spent traveling to get velocity
  99.     self.velocity = vdestdelta * (1/traveltime);    // qcc won't take vec/float    
  100. };
  101.  
  102. /*
  103. ============
  104. After moving, set origin to exact final destination
  105. ============
  106. */
  107. void()  SUB_CalcMoveDone =
  108. {
  109.     setorigin(self, self.finaldest);
  110.     self.velocity = '0 0 0';
  111.     self.nextthink = -1;
  112.     if (self.think1)
  113.         self.think1();
  114. };
  115.  
  116. /*
  117. =============
  118. SUB_CalcAngleMove
  119.  
  120. calculate self.avelocity and self.nextthink to reach destangle from
  121. self.angles rotating 
  122.  
  123. The calling function should make sure self.think is valid
  124. ===============
  125. */
  126. void(entity ent, vector destangle, float tspeed, void() func) SUB_CalcAngleMoveEnt =
  127. {
  128. local entity        stemp;
  129.     stemp = self;
  130.     self = ent;
  131.     SUB_CalcAngleMove (destangle, tspeed, func);
  132.     self = stemp;
  133. };
  134.  
  135. void(vector destangle, float tspeed, void() func) SUB_CalcAngleMove =
  136. {
  137. local vector    destdelta;
  138. local float        len, traveltime;
  139.  
  140.     if (!tspeed)
  141.         objerror("No speed is defined!");
  142.         
  143. // set destdelta to the vector needed to move
  144.     destdelta = destangle - self.angles;
  145.     
  146. // calculate length of vector
  147.     len = vlen (destdelta);
  148.     
  149. // divide by speed to get time to reach dest
  150.     traveltime = len / tspeed;
  151.  
  152. // set nextthink to trigger a think when dest is reached
  153.     self.nextthink = self.ltime + traveltime;
  154.  
  155. // scale the destdelta vector by the time spent traveling to get velocity
  156.     self.avelocity = destdelta * (1 / traveltime);
  157.     
  158.     self.think1 = func;
  159.     self.finalangle = destangle;
  160.     self.think = SUB_CalcAngleMoveDone;
  161. };
  162.  
  163. /*
  164. ============
  165. After rotating, set angle to exact final angle
  166. ============
  167. */
  168. void() SUB_CalcAngleMoveDone =
  169. {
  170.     self.angles = self.finalangle;
  171.     self.avelocity = '0 0 0';
  172.     self.nextthink = -1;
  173.     if (self.think1)
  174.         self.think1();
  175. };
  176.  
  177. //=============================================================================
  178.  
  179. void() DelayThink =
  180. {
  181.     activator = self.enemy;
  182.     SUB_UseTargets ();
  183.     remove(self);
  184. };
  185.  
  186. /*
  187. ==============================
  188. SUB_UseTargets
  189.  
  190. the global "activator" should be set to the entity that initiated the firing.
  191.  
  192. If self.delay is set, a DelayedUse entity will be created that will actually
  193. do the SUB_UseTargets after that many seconds have passed.
  194.  
  195. Centerprints any self.message to the activator.
  196.  
  197. Removes all entities with a targetname that match self.killtarget,
  198. and removes them, so some events can remove other triggers.
  199.  
  200. Search for (string)targetname in all entities that
  201. match (string)self.target and call their .use function
  202. ==============================
  203. */
  204.  
  205. void() SUB_UseTargets =
  206. {
  207.     local entity t, stemp, otemp, act;
  208.  
  209. //
  210. // check for a delay
  211. //
  212.     if (self.delay)
  213.     {
  214.     // create a temp object to fire at a later time
  215.         t = spawn();
  216.         t.classname = "DelayedUse";
  217.         t.nextthink = time + self.delay;
  218.         t.think = DelayThink;
  219.         t.enemy = activator;
  220.         t.message = self.message;
  221.         t.killtarget = self.killtarget;
  222.         t.target = self.target;
  223.         return;
  224.     }
  225.     
  226. //
  227. // print the message
  228. //
  229.     if (activator.classname == "player" && self.message != "")
  230.     {
  231.         centerprint (activator, self.message);
  232.         if (!self.noise)
  233.             sound (activator, CHAN_VOICE, "misc/talk.wav", 1, ATTN_NORM);
  234.     }
  235.  
  236. //
  237. // kill the killtagets
  238. //
  239.     if (self.killtarget)
  240.     {
  241.         t = world;
  242.         do
  243.         {
  244.             t = find (t, targetname, self.killtarget);
  245.             if (!t)
  246.                 return;
  247.             remove (t);
  248.         } while ( 1 );
  249.     }
  250.     
  251. //
  252. // fire targets
  253. //
  254.     if (self.target)
  255.     {
  256.         act = activator;
  257.         t = world;
  258.         do
  259.         {
  260.             t = find (t, targetname, self.target);
  261.             if (!t)
  262.             {
  263.                 return;
  264.             }
  265.             stemp = self;
  266.             otemp = other;
  267.             self = t;
  268.             other = stemp;
  269.             if (self.use != SUB_Null)
  270.             {
  271.                 if (self.use)
  272.                     self.use ();
  273.             }
  274.             self = stemp;
  275.             other = otemp;
  276.             activator = act;
  277.         } while ( 1 );
  278.     }
  279. };
  280.  
  281. /*
  282. in nightmare mode, all attack_finished times become 0
  283. some monsters refire twice automatically
  284. */
  285.  
  286. void(float normal) SUB_AttackFinished =
  287. {
  288.     self.cnt = 0;        // refire count for nightmare
  289.     if (skill != 3)
  290.         self.attack_finished = time + normal;
  291. };
  292.  
  293. float (entity targ) visible;
  294.  
  295. void (void() thinkst) SUB_CheckRefire =
  296. {
  297.     if (skill != 3)
  298.         return;
  299.     if (self.cnt == 1)
  300.         return;
  301.     if (!visible (self.enemy))
  302.         return;
  303.     self.cnt = 1;
  304.     self.think = thinkst;
  305. };
  306.