]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/triggers/subs.qc
Merge branch 'terencehill/quickmenu_file_example' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / triggers / subs.qc
1 void SUB_NullThink() { }
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 ()
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 ()
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 ()
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 ()
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 = new(SUB_CalcMove_controller);
226         controller.owner = self;
227         controller.platmovetype = self.platmovetype;
228         controller.platmovetype_start = self.platmovetype_start;
229         controller.platmovetype_end = self.platmovetype_end;
230         SUB_CalcMove_controller_setbezier(controller, self.SUB_ORIGIN, tcontrol, tdest);
231         controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
232         controller.animstate_starttime = time;
233         controller.animstate_endtime = time + traveltime;
234         controller.think = SUB_CalcMove_controller_think;
235         controller.think1 = self.SUB_THINK;
236
237         // the thinking is now done by the controller
238         self.SUB_THINK = SUB_NullThink; // for PushMove
239         self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
240
241         // invoke controller
242         setself(controller);
243         self.think();
244         setself(self.owner);
245 }
246
247 void SUB_CalcMove (vector tdest, float tspeedtype, float tspeed, void() func)
248 {SELFPARAM();
249         vector  delta;
250         float   traveltime;
251
252         if (!tspeed)
253                 objerror ("No speed is defined!");
254
255         self.think1 = func;
256         self.finaldest = tdest;
257         self.SUB_THINK = SUB_CalcMoveDone;
258
259         if (tdest == self.SUB_ORIGIN)
260         {
261                 self.SUB_VELOCITY = '0 0 0';
262                 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
263                 return;
264         }
265
266         delta = tdest - self.SUB_ORIGIN;
267
268         switch(tspeedtype)
269         {
270                 default:
271                 case TSPEED_START:
272                 case TSPEED_END:
273                 case TSPEED_LINEAR:
274                         traveltime = vlen (delta) / tspeed;
275                         break;
276                 case TSPEED_TIME:
277                         traveltime = tspeed;
278                         break;
279         }
280
281         // Very short animations don't really show off the effect
282         // of controlled animation, so let's just use linear movement.
283         // Alternatively entities can choose to specify non-controlled movement.
284         // The only currently implemented alternative movement is linear (value 1)
285         if (traveltime < 0.15 || (self.platmovetype_start == 1 && self.platmovetype_end == 1)) // is this correct?
286         {
287                 self.SUB_VELOCITY = delta * (1/traveltime);     // QuakeC doesn't allow vector/float division
288                 self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
289                 return;
290         }
291
292         // now just run like a bezier curve...
293         SUB_CalcMove_Bezier((self.SUB_ORIGIN + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
294 }
295
296 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void() func)
297 {SELFPARAM();
298         WITH(entity, self, ent, SUB_CalcMove(tdest, tspeedtype, tspeed, func));
299 }
300
301 /*
302 =============
303 SUB_CalcAngleMove
304
305 calculate self.SUB_AVELOCITY and self.SUB_NEXTTHINK to reach destangle from
306 self.angles rotating
307
308 The calling function should make sure self.SUB_THINK is valid
309 ===============
310 */
311 void SUB_CalcAngleMoveDone ()
312 {SELFPARAM();
313         // After rotating, set angle to exact final angle
314         self.angles = self.finalangle;
315         self.SUB_AVELOCITY = '0 0 0';
316         self.SUB_NEXTTHINK = -1;
317         if (self.think1)
318                 self.think1 ();
319 }
320
321 // FIXME: I fixed this function only for rotation around the main axes
322 void SUB_CalcAngleMove (vector destangle, float tspeedtype, float tspeed, void() func)
323 {SELFPARAM();
324         vector  delta;
325         float   traveltime;
326
327         if (!tspeed)
328                 objerror ("No speed is defined!");
329
330         // take the shortest distance for the angles
331         self.angles_x -= 360 * floor((self.angles_x - destangle_x) / 360 + 0.5);
332         self.angles_y -= 360 * floor((self.angles_y - destangle_y) / 360 + 0.5);
333         self.angles_z -= 360 * floor((self.angles_z - destangle_z) / 360 + 0.5);
334         delta = destangle - self.angles;
335
336         switch(tspeedtype)
337         {
338                 default:
339                 case TSPEED_START:
340                 case TSPEED_END:
341                 case TSPEED_LINEAR:
342                         traveltime = vlen (delta) / tspeed;
343                         break;
344                 case TSPEED_TIME:
345                         traveltime = tspeed;
346                         break;
347         }
348
349         self.think1 = func;
350         self.finalangle = destangle;
351         self.SUB_THINK = SUB_CalcAngleMoveDone;
352
353         if (traveltime < 0.1)
354         {
355                 self.SUB_AVELOCITY = '0 0 0';
356                 self.SUB_NEXTTHINK = self.SUB_LTIME + 0.1;
357                 return;
358         }
359
360         self.SUB_AVELOCITY = delta * (1 / traveltime);
361         self.SUB_NEXTTHINK = self.SUB_LTIME + traveltime;
362 }
363
364 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void() func)
365 {SELFPARAM();
366         WITH(entity, self, ent, SUB_CalcAngleMove (destangle, tspeedtype, tspeed, func));
367 }