]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/common/mapobjects/subs.qc
Merge branch 'bones_was_here/sandbox_object_fix' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / common / mapobjects / subs.qc
1 #include "subs.qh"
2
3 void SUB_NullThink(entity this) { }
4
5 void SUB_CalcMoveDone(entity this);
6 void SUB_CalcAngleMoveDone(entity this);
7
8 #ifdef SVQC
9 spawnfunc(info_null)
10 {
11         delete(this);
12         // if anything breaks, tell the mapper to fix his map! info_null is meant to remove itself immediately.
13 }
14 #endif
15
16 /*
17 ==================
18 SUB_Friction
19
20 Applies some friction to this
21 ==================
22 */
23 .float friction;
24 void SUB_Friction (entity this)
25 {
26         this.nextthink = time;
27         if(IS_ONGROUND(this))
28                 this.velocity = this.velocity * (1 - frametime * this.friction);
29 }
30
31 /*
32 ==================
33 SUB_VanishOrRemove
34
35 Makes client invisible or removes non-client
36 ==================
37 */
38 void SUB_VanishOrRemove (entity ent)
39 {
40         if (IS_CLIENT(ent))
41         {
42                 // vanish
43                 ent.alpha = -1;
44                 ent.effects = 0;
45 #ifdef SVQC
46                 ent.glow_size = 0;
47                 ent.pflags = 0;
48 #endif
49         }
50         else
51         {
52                 // remove
53                 delete(ent);
54         }
55 }
56
57 void SUB_SetFade_Think (entity this)
58 {
59         if(this.alpha == 0)
60                 this.alpha = 1;
61         setthink(this, SUB_SetFade_Think);
62         this.nextthink = time;
63         this.alpha -= frametime * this.fade_rate;
64         if (this.alpha < 0.01)
65                 SUB_VanishOrRemove(this);
66         else
67                 this.nextthink = time;
68 }
69
70 /*
71 ==================
72 SUB_SetFade
73
74 Fade ent out when time >= vanish_time
75 ==================
76 */
77 void SUB_SetFade(entity ent, float vanish_time, float fading_time)
78 {
79         if (fading_time <= 0)
80                 fading_time = 0.01;
81         ent.fade_rate = 1/fading_time;
82         setthink(ent, SUB_SetFade_Think);
83         ent.nextthink = vanish_time;
84 }
85
86 /*
87 =============
88 SUB_CalcMove
89
90 calculate this.velocity and this.nextthink to reach dest from
91 this.origin traveling at speed
92 ===============
93 */
94 void SUB_CalcMoveDone(entity this)
95 {
96         // After moving, set origin to exact final destination
97
98         setorigin (this, this.finaldest);
99         this.velocity = '0 0 0';
100         this.nextthink = -1;
101         if (this.think1 && this.think1 != SUB_CalcMoveDone)
102                 this.think1 (this);
103 }
104
105 void SUB_CalcMovePause(entity this)
106 {
107         this.move_controller.animstate_starttime += frametime;
108         this.move_controller.animstate_endtime += frametime;
109 }
110
111 .float platmovetype_turn;
112 void SUB_CalcMove_controller_think (entity this)
113 {
114         float traveltime;
115         float phasepos;
116         float nexttick;
117         vector delta;
118         vector delta2;
119         vector veloc;
120         vector angloc;
121         vector nextpos;
122         delta = this.destvec;
123         delta2 = this.destvec2;
124         if(time < this.animstate_endtime)
125         {
126                 nexttick = time + PHYS_INPUT_FRAMETIME;
127
128                 traveltime = this.animstate_endtime - this.animstate_starttime;
129                 phasepos = (nexttick - this.animstate_starttime) / traveltime; // range: [0, 1]
130                 phasepos = cubic_speedfunc(this.platmovetype_start, this.platmovetype_end, phasepos);
131                 nextpos = this.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
132                 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
133
134                 if(this.owner.platmovetype_turn)
135                 {
136                         vector destangle;
137                         destangle = delta + 2 * delta2 * phasepos;
138                         destangle = vectoangles(destangle);
139                         destangle_x = -destangle_x; // flip up / down orientation
140
141                         // take the shortest distance for the angles
142                         vector v = this.owner.angles;
143                         v.x -= 360 * floor((v.x - destangle_x) / 360 + 0.5);
144                         v.y -= 360 * floor((v.y - destangle_y) / 360 + 0.5);
145                         v.z -= 360 * floor((v.z - destangle_z) / 360 + 0.5);
146                         this.owner.angles = v;
147                         angloc = destangle - this.owner.angles;
148                         angloc = angloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
149                         this.owner.avelocity = angloc;
150                 }
151                 if(nexttick < this.animstate_endtime)
152                         veloc = nextpos - this.owner.origin;
153                 else
154                         veloc = this.finaldest - this.owner.origin;
155                 veloc = veloc * (1 / PHYS_INPUT_FRAMETIME); // so it arrives for the next frame
156
157                 this.owner.velocity = veloc;
158                 this.nextthink = nexttick;
159         }
160         else
161         {
162                 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
163                 entity own = this.owner;
164                 setthink(own, this.think1);
165                 // set the owner's reference to this entity to NULL
166                 own.move_controller = NULL;
167                 delete(this);
168                 getthink(own)(own);
169         }
170 }
171
172 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector destin)
173 {
174         // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
175         // 2 * control * t - 2 * control * t * t + destin * t * t
176         // 2 * control * t + (destin - 2 * control) * t * t
177
178         //setorigin(controller, org); // don't link to the world
179         controller.origin = org;
180         control -= org;
181         destin -= org;
182
183         controller.destvec = 2 * control; // control point
184         controller.destvec2 = destin - 2 * control; // quadratic part required to reach end point
185         // also: initial d/dphasepos origin = 2 * control, final speed = 2 * (destin - control)
186 }
187
188 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector destin)
189 {
190         // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + destin * t * t
191         // 2 * control * t - 2 * control * t * t + destin * t * t
192         // 2 * control * t + (destin - 2 * control) * t * t
193
194         //setorigin(controller, org); // don't link to the world
195         controller.origin = org;
196         destin -= org;
197
198         controller.destvec = destin; // end point
199         controller.destvec2 = '0 0 0';
200 }
201
202 void SUB_CalcMove_Bezier (entity this, vector tcontrol, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
203 {
204         float   traveltime;
205         entity controller;
206
207         if (!tspeed)
208                 objerror (this, "No speed is defined!");
209
210         this.think1 = func;
211         this.finaldest = tdest;
212         setthink(this, SUB_CalcMoveDone);
213
214         switch(tspeedtype)
215         {
216                 default:
217                 case TSPEED_START:
218                         traveltime = 2 * vlen(tcontrol - this.origin) / tspeed;
219                         break;
220                 case TSPEED_END:
221                         traveltime = 2 * vlen(tcontrol - tdest)       / tspeed;
222                         break;
223                 case TSPEED_LINEAR:
224                         traveltime = vlen(tdest - this.origin)        / tspeed;
225                         break;
226                 case TSPEED_TIME:
227                         traveltime = tspeed;
228                         break;
229         }
230
231         if (traveltime < 0.1) // useless anim
232         {
233                 this.velocity = '0 0 0';
234                 this.nextthink = this.ltime + 0.1;
235                 return;
236         }
237
238         // delete the previous controller, otherwise changing movement midway is glitchy
239         if (this.move_controller != NULL)
240         {
241                 delete(this.move_controller);
242         }
243         controller = new_pure(SUB_CalcMove_controller);
244         controller.owner = this;
245         this.move_controller = controller;
246         controller.platmovetype = this.platmovetype;
247         controller.platmovetype_start = this.platmovetype_start;
248         controller.platmovetype_end = this.platmovetype_end;
249         SUB_CalcMove_controller_setbezier(controller, this.origin, tcontrol, tdest);
250         controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
251         controller.animstate_starttime = time;
252         controller.animstate_endtime = time + traveltime;
253         setthink(controller, SUB_CalcMove_controller_think);
254         controller.think1 = getthink(this);
255
256         // the thinking is now done by the controller
257         setthink(this, SUB_NullThink); // for PushMove
258         this.nextthink = this.ltime + traveltime;
259
260         // invoke controller
261         getthink(controller)(controller);
262 }
263
264 void SUB_CalcMove (entity this, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
265 {
266         vector  delta;
267         float   traveltime;
268
269         if (!tspeed)
270                 objerror (this, "No speed is defined!");
271
272         this.think1 = func;
273         this.finaldest = tdest;
274         setthink(this, SUB_CalcMoveDone);
275
276         if (tdest == this.origin)
277         {
278                 this.velocity = '0 0 0';
279                 this.nextthink = this.ltime + 0.1;
280                 return;
281         }
282
283         delta = tdest - this.origin;
284
285         switch(tspeedtype)
286         {
287                 default:
288                 case TSPEED_START:
289                 case TSPEED_END:
290                 case TSPEED_LINEAR:
291                         traveltime = vlen (delta) / tspeed;
292                         break;
293                 case TSPEED_TIME:
294                         traveltime = tspeed;
295                         break;
296         }
297
298         // Very short animations don't really show off the effect
299         // of controlled animation, so let's just use linear movement.
300         // Alternatively entities can choose to specify non-controlled movement.
301         // The only currently implemented alternative movement is linear (value 1)
302         if (traveltime < 0.15 || (this.platmovetype_start == 1 && this.platmovetype_end == 1)) // is this correct?
303         {
304                 this.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
305                 this.nextthink = this.ltime + traveltime;
306                 return;
307         }
308
309         // now just run like a bezier curve...
310         SUB_CalcMove_Bezier(this, (this.origin + tdest) * 0.5, tdest, tspeedtype, tspeed, func);
311 }
312
313 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeedtype, float tspeed, void(entity this) func)
314 {
315         SUB_CalcMove(ent, tdest, tspeedtype, tspeed, func);
316 }
317
318 /*
319 =============
320 SUB_CalcAngleMove
321
322 calculate this.avelocity and this.nextthink to reach destangle from
323 this.angles rotating
324
325 The calling function should make sure this.setthink is valid
326 ===============
327 */
328 void SUB_CalcAngleMoveDone(entity this)
329 {
330         // After rotating, set angle to exact final angle
331         this.angles = this.finalangle;
332         this.avelocity = '0 0 0';
333         this.nextthink = -1;
334         if (this.think1 && this.think1 != SUB_CalcAngleMoveDone) // avoid endless loops
335                 this.think1 (this);
336 }
337
338 // FIXME: I fixed this function only for rotation around the main axes
339 void SUB_CalcAngleMove (entity this, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
340 {
341         if (!tspeed)
342                 objerror (this, "No speed is defined!");
343
344         // take the shortest distance for the angles
345         this.angles_x -= 360 * floor((this.angles_x - destangle_x) / 360 + 0.5);
346         this.angles_y -= 360 * floor((this.angles_y - destangle_y) / 360 + 0.5);
347         this.angles_z -= 360 * floor((this.angles_z - destangle_z) / 360 + 0.5);
348         vector delta = destangle - this.angles;
349         float traveltime;
350
351         switch(tspeedtype)
352         {
353                 default:
354                 case TSPEED_START:
355                 case TSPEED_END:
356                 case TSPEED_LINEAR:
357                         traveltime = vlen (delta) / tspeed;
358                         break;
359                 case TSPEED_TIME:
360                         traveltime = tspeed;
361                         break;
362         }
363
364         this.think1 = func;
365         this.finalangle = destangle;
366         setthink(this, SUB_CalcAngleMoveDone);
367
368         if (traveltime < 0.1)
369         {
370                 this.avelocity = '0 0 0';
371                 this.nextthink = this.ltime + 0.1;
372                 return;
373         }
374
375         this.avelocity = delta * (1 / traveltime);
376         this.nextthink = this.ltime + traveltime;
377 }
378
379 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeedtype, float tspeed, void(entity this) func)
380 {
381         SUB_CalcAngleMove (ent, destangle, tspeedtype, tspeed, func);
382 }
383
384 #ifdef SVQC
385 void ApplyMinMaxScaleAngles(entity e)
386 {
387         if(e.angles.x != 0 || e.angles.z != 0 || e.avelocity.x != 0 || e.avelocity.z != 0) // "weird" rotation
388         {
389                 e.maxs = '1 1 1' * vlen(
390                         '1 0 0' * max(-e.mins.x, e.maxs.x) +
391                         '0 1 0' * max(-e.mins.y, e.maxs.y) +
392                         '0 0 1' * max(-e.mins.z, e.maxs.z)
393                 );
394                 e.mins = -e.maxs;
395         }
396         else if(e.angles.y != 0 || e.avelocity.y != 0) // yaw only is a bit better
397         {
398                 e.maxs_x = vlen(
399                         '1 0 0' * max(-e.mins.x, e.maxs.x) +
400                         '0 1 0' * max(-e.mins.y, e.maxs.y)
401                 );
402                 e.maxs_y = e.maxs.x;
403                 e.mins_x = -e.maxs.x;
404                 e.mins_y = -e.maxs.x;
405         }
406         if(e.scale)
407                 setsize(e, RoundPerfectVector(e.mins * e.scale), RoundPerfectVector(e.maxs * e.scale));
408         else
409                 setsize(e, e.mins, e.maxs);
410 }
411
412 void SetBrushEntityModel(entity this, bool with_lod)
413 {
414         if(this.model != "")
415         {
416                 precache_model(this.model);
417                 if(this.mins != '0 0 0' || this.maxs != '0 0 0')
418                 {
419                         vector mi = this.mins;
420                         vector ma = this.maxs;
421                         _setmodel(this, this.model); // no precision needed
422                         setsize(this, mi, ma);
423                 }
424                 else
425                         _setmodel(this, this.model); // no precision needed
426                 if(with_lod)
427                         InitializeEntity(this, LODmodel_attach, INITPRIO_FINDTARGET);
428
429                 if(endsWith(this.model, ".obj")) // WORKAROUND: darkplaces currently rotates .obj models on entities incorrectly, we need to add 180 degrees to the Y axis
430                         this.angles_y = anglemods(this.angles_y - 180);
431         }
432         setorigin(this, this.origin);
433         ApplyMinMaxScaleAngles(this);
434 }
435
436 bool LOD_customize(entity this, entity client)
437 {
438         if(autocvar_loddebug)
439         {
440                 int d = autocvar_loddebug;
441                 if(d == 1)
442                         this.modelindex = this.lodmodelindex0;
443                 else if(d == 2 || !this.lodmodelindex2)
444                         this.modelindex = this.lodmodelindex1;
445                 else // if(d == 3)
446                         this.modelindex = this.lodmodelindex2;
447                 return true;
448         }
449
450         // TODO csqc network this so it only gets sent once
451         vector near_point = NearestPointOnBox(this, client.origin);
452         if(vdist(near_point - client.origin, <, this.loddistance1))
453                 this.modelindex = this.lodmodelindex0;
454         else if(!this.lodmodelindex2 || vdist(near_point - client.origin, <, this.loddistance2))
455                 this.modelindex = this.lodmodelindex1;
456         else
457                 this.modelindex = this.lodmodelindex2;
458
459         return true;
460 }
461
462 void LOD_uncustomize(entity this)
463 {
464         this.modelindex = this.lodmodelindex0;
465 }
466
467 void LODmodel_attach(entity this)
468 {
469         entity e;
470
471         if(!this.loddistance1)
472                 this.loddistance1 = 1000;
473         if(!this.loddistance2)
474                 this.loddistance2 = 2000;
475         this.lodmodelindex0 = this.modelindex;
476
477         if(this.lodtarget1 != "")
478         {
479                 e = find(NULL, targetname, this.lodtarget1);
480                 if(e)
481                 {
482                         this.lodmodel1 = e.model;
483                         delete(e);
484                 }
485         }
486         if(this.lodtarget2 != "")
487         {
488                 e = find(NULL, targetname, this.lodtarget2);
489                 if(e)
490                 {
491                         this.lodmodel2 = e.model;
492                         delete(e);
493                 }
494         }
495
496         if(autocvar_loddebug < 0)
497         {
498                 this.lodmodel1 = this.lodmodel2 = ""; // don't even initialize
499         }
500
501         if(this.lodmodel1 != "")
502         {
503                 vector mi, ma;
504                 mi = this.mins;
505                 ma = this.maxs;
506
507                 precache_model(this.lodmodel1);
508                 _setmodel(this, this.lodmodel1);
509                 this.lodmodelindex1 = this.modelindex;
510
511                 if(this.lodmodel2 != "")
512                 {
513                         precache_model(this.lodmodel2);
514                         _setmodel(this, this.lodmodel2);
515                         this.lodmodelindex2 = this.modelindex;
516                 }
517
518                 this.modelindex = this.lodmodelindex0;
519                 setsize(this, mi, ma);
520         }
521
522         if(this.lodmodelindex1)
523                 if (!getSendEntity(this))
524                         SetCustomizer(this, LOD_customize, LOD_uncustomize);
525 }
526
527 /*
528 ================
529 InitTrigger
530 ================
531 */
532
533 void SetMovedir(entity this)
534 {
535         if(this.movedir != '0 0 0')
536                 this.movedir = normalize(this.movedir);
537         else
538         {
539                 makevectors(this.angles);
540                 this.movedir = v_forward;
541         }
542
543         this.angles = '0 0 0';
544 }
545
546 void InitTrigger(entity this)
547 {
548 // trigger angles are used for one-way touches.  An angle of 0 is assumed
549 // to mean no restrictions, so use a yaw of 360 instead.
550         SetMovedir(this);
551         this.solid = SOLID_TRIGGER;
552         SetBrushEntityModel(this, false);
553         set_movetype(this, MOVETYPE_NONE);
554         this.modelindex = 0;
555         this.model = "";
556 }
557
558 void InitSolidBSPTrigger(entity this)
559 {
560 // trigger angles are used for one-way touches.  An angle of 0 is assumed
561 // to mean no restrictions, so use a yaw of 360 instead.
562         SetMovedir(this);
563         this.solid = SOLID_BSP;
564         SetBrushEntityModel(this, false);
565         set_movetype(this, MOVETYPE_NONE); // why was this PUSH? -div0
566 //      this.modelindex = 0;
567         this.model = "";
568 }
569
570 bool InitMovingBrushTrigger(entity this)
571 {
572 // trigger angles are used for one-way touches.  An angle of 0 is assumed
573 // to mean no restrictions, so use a yaw of 360 instead.
574         this.solid = SOLID_BSP;
575         SetBrushEntityModel(this, true);
576         set_movetype(this, MOVETYPE_PUSH);
577         if(this.modelindex == 0)
578         {
579                 objerror(this, "InitMovingBrushTrigger: no brushes found!");
580                 return false;
581         }
582         return true;
583 }
584 #endif