subs.qc 6.8 KB

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