]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/g_subs.qc
add WritePicture
[divverent/nexuiz.git] / data / qcsrc / server / g_subs.qc
1 void SUB_Null() {};
2
3 void(vector destangle, float tspeed, void() func) SUB_CalcAngleMove;
4 void()  SUB_CalcMoveDone;
5 void() SUB_CalcAngleMoveDone;
6 //void() SUB_UseTargets;
7 void() SUB_Remove;
8
9 void spawnfunc_info_null (void)
10 {
11         remove(self);
12         // if anything breaks, tell the mapper to fix his map! info_null is meant to remove itself immediately.
13 }
14
15 /*
16 ==================
17 SUB_Remove
18
19 Remove self
20 ==================
21 */
22 void SUB_Remove (void)
23 {
24         remove (self);
25 }
26
27 /*
28 ==================
29 SUB_VanishOrRemove
30
31 Makes client invisible or removes non-client
32 ==================
33 */
34 void SUB_VanishOrRemove (entity ent)
35 {
36         if (ent.flags & FL_CLIENT)
37         {
38                 // vanish
39                 ent.model = "";
40                 ent.effects = 0;
41                 ent.glow_size = 0;
42                 ent.pflags = 0;
43         }
44         else
45         {
46                 // remove
47                 remove (ent);
48         }
49 }
50
51 void SUB_SetFade_Think (void)
52 {
53         self.think = SUB_SetFade_Think;
54         self.nextthink = self.fade_time;
55         self.alpha = 1 - (time - self.fade_time) * self.fade_rate;
56         if (self.alpha < 0.01)
57                 SUB_VanishOrRemove(self);
58         self.alpha = bound(0.01, self.alpha, 1);
59 }
60
61 /*
62 ==================
63 SUB_SetFade
64
65 Fade 'ent' out when time >= 'when'
66 ==================
67 */
68 void SUB_SetFade (entity ent, float when, float fadetime)
69 {
70         //if (ent.flags & FL_CLIENT) // && ent.deadflag != DEAD_NO)
71         //      return;
72         //ent.alpha = 1;
73         ent.fade_rate = 1/fadetime;
74         ent.fade_time = when;
75         ent.think = SUB_SetFade_Think;
76         ent.nextthink = when;
77 }
78
79 /*
80 =============
81 SUB_CalcMove
82
83 calculate self.velocity and self.nextthink to reach dest from
84 self.origin traveling at speed
85 ===============
86 */
87 void SUB_CalcMoveDone (void)
88 {
89         // After moving, set origin to exact final destination
90
91         setorigin (self, self.finaldest);
92         self.velocity = '0 0 0';
93         self.nextthink = -1;
94         if (self.think1)
95                 self.think1 ();
96 }
97
98 void SUB_CalcMove (vector tdest, float tspeed, void() func)
99 {
100         vector  delta;
101         float   traveltime;
102
103         if (!tspeed)
104                 objerror ("No speed is defined!");
105
106         self.think1 = func;
107         self.finaldest = tdest;
108         self.think = SUB_CalcMoveDone;
109
110         if (tdest == self.origin)
111         {
112                 self.velocity = '0 0 0';
113                 self.nextthink = self.ltime + 0.1;
114                 return;
115         }
116
117         delta = tdest - self.origin;
118         traveltime = vlen (delta) / tspeed;
119
120         if (traveltime < 0.1)
121         {
122                 self.velocity = '0 0 0';
123                 self.nextthink = self.ltime + 0.1;
124                 return;
125         }
126
127         self.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
128
129         self.nextthink = self.ltime + traveltime;
130 }
131
132 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeed, void() func)
133 {
134         entity  oldself;
135
136         oldself = self;
137         self = ent;
138
139         SUB_CalcMove (tdest, tspeed, func);
140
141         self = oldself;
142 }
143
144 /*
145 =============
146 SUB_CalcAngleMove
147
148 calculate self.avelocity and self.nextthink to reach destangle from
149 self.angles rotating
150
151 The calling function should make sure self.think is valid
152 ===============
153 */
154 void SUB_CalcAngleMoveDone (void)
155 {
156         // After rotating, set angle to exact final angle
157         self.angles = self.finalangle;
158         self.avelocity = '0 0 0';
159         self.nextthink = -1;
160         if (self.think1)
161                 self.think1 ();
162 }
163
164 void SUB_CalcAngleMove (vector destangle, float tspeed, void() func)
165 {
166         vector  delta;
167         float   traveltime;
168
169         if (!tspeed)
170                 objerror ("No speed is defined!");
171
172         delta = destangle - self.angles;
173         traveltime = vlen (delta) / tspeed;
174
175         self.avelocity = delta * (1 / traveltime);
176
177         self.think1 = func;
178         self.finalangle = destangle;
179
180         self.think = SUB_CalcAngleMoveDone;
181         self.nextthink = self.ltime + traveltime;
182 }
183
184 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeed, void() func)
185 {
186         entity  oldself;
187
188         oldself = self;
189         self = ent;
190
191         SUB_CalcAngleMove (destangle, tspeed, func);
192
193         self = oldself;
194 }
195
196 /*
197 ==================
198 main
199
200 unused but required by the engine
201 ==================
202 */
203 void main (void)
204 {
205
206 }
207
208 // Sound functions
209
210 /*
211 ==================
212 PointSound
213
214 Play a sound at the given location
215 ==================
216 */
217 void PointSound (vector org, string snd, float vol, float attn)
218 {
219         entity  speaker;
220
221         speaker = spawn ();
222         setorigin (speaker, org);
223         sound (speaker, CHAN_BODY, snd, vol, attn);
224         remove (speaker);
225 }
226
227 // Misc
228
229 /*
230 ==================
231 traceline_antilag
232
233 A version of traceline that must be used by SOLID_SLIDEBOX things that want to hit SOLID_CORPSE things with a trace attack
234 Additionally it moves players back into the past before the trace and restores them afterward.
235 ==================
236 */
237 void traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
238 {
239         local entity player;
240         local float oldsolid;
241
242         // check whether antilagged traces are enabled
243         if (lag < 0.001)
244                 lag = 0;
245         if (clienttype(forent) != CLIENTTYPE_REAL)
246                 lag = 0; // only antilag for clients
247         if (lag)
248         if (cvar("g_antilag") != 2)
249                 lag = 0;
250
251         // change shooter to SOLID_BBOX so the shot can hit corpses
252         oldsolid = source.solid;
253         source.solid = SOLID_BBOX;
254
255         if (lag)
256         {
257                 // take players back into the past
258                 player = player_list;
259                 while (player)
260                 {
261                         antilag_takeback(player, time - lag);
262                         player = player.nextplayer;
263                 }
264         }
265
266         // do the trace
267         traceline (v1, v2, nomonst, forent);
268
269         // restore players to current positions
270         if (lag)
271         {
272                 player = player_list;
273                 while (player)
274                 {
275                         antilag_restore(player);
276                         player = player.nextplayer;
277                 }
278         }
279
280         // restore shooter solid type
281         source.solid = oldsolid;
282 }
283
284 /*
285 ==================
286 findbetterlocation
287
288 Returns a point at least 12 units away from walls
289 (useful for explosion animations, although the blast is performed where it really happened)
290 Ripped from DPMod
291 ==================
292 */
293 vector findbetterlocation (vector org, float mindist)
294 {
295         vector  loc;
296         vector vec;
297         float c;
298
299         vec = mindist * '1 0 0';
300         c = 0;
301         while (c < 6)
302         {
303                 traceline (org, org + vec, TRUE, world);
304                 vec = vec * -1;
305                 if (trace_fraction < 1)
306                 {
307                         loc = trace_endpos;
308                         traceline (loc, loc + vec, TRUE, world);
309                         if (trace_fraction >= 1)
310                                 org = loc + vec;
311                 }
312                 if (c & 1)
313                 {
314                         vec_z = vec_y;
315                         vec_y = vec_x;
316                         vec_x = vec_z;
317                 }
318                 c = c + 1;
319         }
320
321         return org;
322 }
323
324 /*
325 ==================
326 crandom
327
328 Returns a random number between -1.0 and 1.0
329 ==================
330 */
331 float crandom (void)
332 {
333         return 2 * (random () - 0.5);
334 }
335
336 /*
337 ==================
338 Angc used for animations
339 ==================
340 */
341
342
343 float angc (float a1, float a2)
344 {
345         float   a;
346
347         while (a1 > 180)
348                 a1 = a1 - 360;
349         while (a1 < -179)
350                 a1 = a1 + 360;
351
352         while (a2 > 180)
353                 a2 = a2 - 360;
354         while (a2 < -179)
355                 a2 = a2 + 360;
356
357         a = a1 - a2;
358         while (a > 180)
359                 a = a - 360;
360         while (a < -179)
361                 a = a + 360;
362
363         return a;
364 }
365
366
367 /*
368 ================
369 InitTrigger
370 ================
371 */
372
373 void SetMovedir()
374 {
375         if (self.movedir != '0 0 0')
376                 self.movedir = normalize(self.movedir);
377         else
378         {
379                 if (self.angles == '0 -1 0')
380                         self.movedir = '0 0 1';
381                 else if (self.angles == '0 -2 0')
382                         self.movedir = '0 0 -1';
383                 else
384                 {
385                         makevectors (self.angles);
386                         self.movedir = v_forward;
387                 }
388         }
389
390         self.angles = '0 0 0';
391 };
392
393 void InitTrigger()
394 {
395 // trigger angles are used for one-way touches.  An angle of 0 is assumed
396 // to mean no restrictions, so use a yaw of 360 instead.
397         if (self.movedir == '0 0 0')
398         if (self.angles != '0 0 0')
399                 SetMovedir ();
400         self.solid = SOLID_TRIGGER;
401         setmodel (self, self.model);    // set size and link into world, no precision needed
402         self.movetype = MOVETYPE_NONE;
403         self.modelindex = 0;
404         self.model = "";
405 };
406
407 void InitSolidBSPTrigger()
408 {
409 // trigger angles are used for one-way touches.  An angle of 0 is assumed
410 // to mean no restrictions, so use a yaw of 360 instead.
411         if (self.movedir == '0 0 0')
412         if (self.angles != '0 0 0')
413                 SetMovedir ();
414         self.solid = SOLID_BSP;
415         setmodel (self, self.model);    // set size and link into world, no precision needed
416         self.movetype = MOVETYPE_PUSH;
417 //      self.modelindex = 0;
418         self.model = "";
419 };