]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/subs.qc
Draw: purge SELFPARAM
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / subs.qc
1 void SUB_NullThink(void) { }
2
3 void()  SUB_CalcMoveDone;
4 void() SUB_CalcAngleMoveDone;
5 //void() SUB_UseTargets;
6
7 /*
8 ==================
9 SUB_Friction
10
11 Applies some friction to self
12 ==================
13 */
14 .float friction;
15 void SUB_Friction (void)
16 {SELFPARAM();
17         self.SUB_NEXTTHINK = time;
18         if(self.SUB_FLAGS & FL_ONGROUND)
19                 self.SUB_VELOCITY = self.SUB_VELOCITY * (1 - frametime * self.friction);
20 }
21
22 /*
23 ==================
24 SUB_VanishOrRemove
25
26 Makes client invisible or removes non-client
27 ==================
28 */
29 void SUB_VanishOrRemove (entity ent)
30 {
31         if (IS_CLIENT(ent))
32         {
33                 // vanish
34                 ent.alpha = -1;
35                 ent.effects = 0;
36 #ifdef SVQC
37                 ent.glow_size = 0;
38                 ent.pflags = 0;
39 #endif
40         }
41         else
42         {
43                 // remove
44                 remove (ent);
45         }
46 }
47
48 void SUB_SetFade_Think (void)
49 {SELFPARAM();
50         if(self.alpha == 0)
51                 self.alpha = 1;
52         self.SUB_THINK = SUB_SetFade_Think;
53         self.SUB_NEXTTHINK = time;
54         self.alpha -= frametime * self.fade_rate;
55         if (self.alpha < 0.01)
56                 SUB_VanishOrRemove(self);
57         else
58                 self.SUB_NEXTTHINK = time;
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 fading_time)
69 {
70         ent.fade_rate = 1/fading_time;
71         ent.SUB_THINK = SUB_SetFade_Think;
72         ent.SUB_NEXTTHINK = when;
73 }
74
75 /*
76 =============
77 SUB_CalcMove
78
79 calculate self.SUB_VELOCITY and self.SUB_NEXTTHINK to reach dest from
80 self.SUB_ORIGIN traveling at speed
81 ===============
82 */
83 void SUB_CalcMoveDone (void)
84 {SELFPARAM();
85         // After moving, set origin to exact final destination
86
87         SUB_SETORIGIN (self, self.finaldest);
88         self.SUB_VELOCITY = '0 0 0';
89         self.SUB_NEXTTHINK = -1;
90         if (self.think1)
91                 self.think1 ();
92 }
93
94 .float platmovetype_turn;
95 void SUB_CalcMove_controller_think (void)
96 {SELFPARAM();
97         entity oldself;
98         float traveltime;
99         float phasepos;
100         float nexttick;
101         vector delta;
102         vector delta2;
103         vector veloc;
104         vector angloc;
105         vector nextpos;
106         delta = self.destvec;
107         delta2 = self.destvec2;
108         if(time < self.animstate_endtime)
109         {
110                 nexttick = time + PHYS_INPUT_FRAMETIME;
111
112                 traveltime = self.animstate_endtime - self.animstate_starttime;
113                 phasepos = (nexttick - self.animstate_starttime) / traveltime; // range: [0, 1]
114                 phasepos = cubic_speedfunc(self.platmovetype_start, self.platmovetype_end, phasepos);
115                 nextpos = self.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
116                 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
117
118                 if(self.owner.platmovetype_turn)
119                 {
120                         vector destangle;
121                         destangle = delta + 2 * delta2 * phasepos;
122                         destangle = vectoangles(destangle);
123                         destangle_x = -destangle_x; // flip up / down orientation
124
125                         // take the shortest distance for the angles
126                         vector v = SUB_ANGLES(self.owner);
127                         v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
128                         v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
129                         v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
130                         SUB_ANGLES(self.owner) = v;
131                         angloc = destangle - SUB_ANGLES(self.owner);
132                         angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
133                         self.owner.SUB_AVELOCITY = angloc;
134                 }
135                 if(nexttick < self.animstate_endtime)
136                         veloc = nextpos - self.owner.SUB_ORIGIN;
137                 else
138                         veloc = self.finaldest - self.owner.SUB_ORIGIN;
139                 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
140
141                 self.owner.SUB_VELOCITY = veloc;
142                 self.nextthink = nexttick;
143         }
144         else
145         {
146                 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
147                 oldself = self;
148                 self.owner.SUB_THINK = self.think1;
149                 setself(self.owner);
150                 remove(oldself);
151                 self.SUB_THINK();
152         }
153 }
154
155 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
156 {
157         // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
158         // 2 * control * t - 2 * control * t * t + destin * t * t
159         // 2 * control * t + (destin - 2 * control) * t * t
160
161         setorigin(controller, org);
162         control -= org;
163         destin -= org;
164
165         controller.destvec = 2 * control; // control point
166         controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
167         // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
168 }
169
170 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
171 {
172         // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
173         // 2 * control * t - 2 * control * t * t + destin * t * t
174         // 2 * control * t + (destin - 2 * control) * t * t
175
176         setorigin(controller, org);
177         destin -= org;
178
179         controller.destvec = destin; // end point
180         controller.destvec2 = '0 0 0';
181 }
182
183 float TSPEED_TIME = -1;
184 float TSPEED_LINEAR = 0;
185 float TSPEED_START = 1;
186 float TSPEED_END = 2;
187 // TODO average too?
188
189 void SUB_CalcMove_Bezier (vector tcontrol, vector tdest, float tspeedtype, float tspeed, void() func)
190 {SELFPARAM();
191         float   traveltime;
192         entity controller;
193
194         if (!tspeed)
195                 objerror ("No speed is defined!");
196
197         self.think1 = func;
198         self.finaldest = tdest;
199         self.SUB_THINK = SUB_CalcMoveDone;
200
201         switch(tspeedtype)
202         {
203                 default:
204                 case TSPEED_START:
205                         traveltime = 2 * vlen(tcontrol - self.SUB_ORIGIN) / tspeed;
206                         break;
207                 case TSPEED_END:
208                         traveltime = 2 * vlen(tcontrol - tdest)       / tspeed;
209                         break;
210                 case TSPEED_LINEAR:
211                         traveltime = vlen(tdest - self.SUB_ORIGIN)        / tspeed;
212                         break;
213                 case TSPEED_TIME:
214                         traveltime = tspeed;
215                         break;
216         }
217
218         if (traveltime < 0.1) // useless anim
219         {
220                 self.SUB_VELOCITY = '0 0 0';
221                 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
222                 return;
223         }
224
225         controller = spawn();
226         controller.classname = "SUB_CalcMove_controller";
227         controller.owner = self;
228         controller.platmovetype = self.platmovetype;
229         controller.platmovetype_start = self.platmovetype_start;
230         controller.platmovetype_end = self.platmovetype_end;
231         SUB_CalcMove_controller_setbezier(controller, self.SUB_ORIGIN, tcontrol, tdest);
232         controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
233         controller.animstate_starttime = time;
234         controller.animstate_endtime = time + traveltime;
235         controller.think = SUB_CalcMove_controller_think;
236         controller.think1 = self.SUB_THINK;
237
238         // the thinking is now done by the controller
239         self.SUB_THINK = SUB_NullThink; // for PushMove
240         self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
241
242         // invoke controller
243         setself(controller);
244         self.think();
245         setself(self.owner);
246 }
247
248 void SUB_CalcMove (vector tdest, float tspeedtype, float tspeed, void() func)
249 {SELFPARAM();
250         vector  delta;
251         float   traveltime;
252
253         if (!tspeed)
254                 objerror ("No speed is defined!");
255
256         self.think1 = func;
257         self.finaldest = tdest;
258         self.SUB_THINK = SUB_CalcMoveDone;
259
260         if (tdest == self.SUB_ORIGIN)
261         {
262                 self.SUB_VELOCITY = '0 0 0';
263                 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
264                 return;
265         }
266
267         delta = tdest - self.SUB_ORIGIN;
268
269         switch(tspeedtype)
270         {
271                 default:
272                 case TSPEED_START:
273                 case TSPEED_END:
274                 case TSPEED_LINEAR:
275                         traveltime = vlen (delta) / tspeed;
276                         break;
277                 case TSPEED_TIME:
278                         traveltime = tspeed;
279                         break;
280         }
281
282         // Very short animations don't really show off the effect
283         // of controlled animation, so let's just use linear movement.
284         // Alternatively entities can choose to specify non-controlled movement.
285         // The only currently implemented alternative movement is linear (value 1)
286         if (traveltime < 0.15 || (self.platmovetype_start == 1 && self.platmovetype_end == 1)) // is this correct?
287         {
288                 self.SUB_VELOCITY = delta * (1/traveltime);     // QuakeC doesn't allow vector/float division
289                 self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
290                 return;
291         }
292
293         // now just run like a bezier curve...
294         SUB_CalcMove_Bezier((self.SUB_ORIGIN + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
295 }
296
297 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void() func)
298 {SELFPARAM();
299         WITH(entity, self, ent, SUB_CalcMove(tdest, tspeedtype, tspeed, func));
300 }
301
302 /*
303 =============
304 SUB_CalcAngleMove
305
306 calculate self.SUB_AVELOCITY and self.SUB_NEXTTHINK to reach destangle from
307 self.angles rotating
308
309 The calling function should make sure self.SUB_THINK is valid
310 ===============
311 */
312 void SUB_CalcAngleMoveDone (void)
313 {SELFPARAM();
314         // After rotating, set angle to exact final angle
315         self.angles = self.finalangle;
316         self.SUB_AVELOCITY = '0 0 0';
317         self.SUB_NEXTTHINK = -1;
318         if (self.think1)
319                 self.think1 ();
320 }
321
322 // FIXME: I fixed this function only for rotation around the main axes
323 void SUB_CalcAngleMove (vector destangle, float tspeedtype, float tspeed, void() func)
324 {SELFPARAM();
325         vector  delta;
326         float   traveltime;
327
328         if (!tspeed)
329                 objerror ("No speed is defined!");
330
331         // take the shortest distance for the angles
332         self.angles_x -= 360 * floor((self.angles_x - destangle_x) / 360 + 0.5);
333         self.angles_y -= 360 * floor((self.angles_y - destangle_y) / 360 + 0.5);
334         self.angles_z -= 360 * floor((self.angles_z - destangle_z) / 360 + 0.5);
335         delta = destangle - self.angles;
336
337         switch(tspeedtype)
338         {
339                 default:
340                 case TSPEED_START:
341                 case TSPEED_END:
342                 case TSPEED_LINEAR:
343                         traveltime = vlen (delta) / tspeed;
344                         break;
345                 case TSPEED_TIME:
346                         traveltime = tspeed;
347                         break;
348         }
349
350         self.think1 = func;
351         self.finalangle = destangle;
352         self.SUB_THINK = SUB_CalcAngleMoveDone;
353
354         if (traveltime < 0.1)
355         {
356                 self.SUB_AVELOCITY = '0 0 0';
357                 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
358                 return;
359         }
360
361         self.SUB_AVELOCITY = delta * (1 / traveltime);
362         self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
363 }
364
365 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void() func)
366 {SELFPARAM();
367         WITH(entity, self, ent, SUB_CalcAngleMove (destangle, tspeedtype, tspeed, func));
368 }