]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_subs.qc
Merge remote-tracking branch 'origin/master' into terencehill/cursormode
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / g_subs.qc
1 void SUB_Null() {}
2 float SUB_True() { return 1; }
3 float SUB_False() { return 0; }
4
5 void(vector destangle, float tspeed, void() func) SUB_CalcAngleMove;
6 void()  SUB_CalcMoveDone;
7 void() SUB_CalcAngleMoveDone;
8 //void() SUB_UseTargets;
9 void() SUB_Remove;
10
11 void spawnfunc_info_null (void)
12 {
13         remove(self);
14         // if anything breaks, tell the mapper to fix his map! info_null is meant to remove itself immediately.
15 }
16
17 void setanim(entity e, vector anim, float looping, float override, float restart)
18 {
19         if (!anim)
20                 return; // no animation was given to us! We can't use this. 
21                 
22         if (anim_x == e.animstate_startframe)
23         if (anim_y == e.animstate_numframes)
24         if (anim_z == e.animstate_framerate)
25         {
26                 if(restart)
27                 {
28                         if(restart > 0)
29                         if(anim_y == 1) // ZYM animation
30                                 BITXOR_ASSIGN(e.effects, EF_RESTARTANIM_BIT);
31                 }
32                 else
33                         return;
34         }
35         e.animstate_startframe = anim_x;
36         e.animstate_numframes = anim_y;
37         e.animstate_framerate = anim_z;
38         e.animstate_starttime = servertime - 0.1 * serverframetime; // shift it a little bit into the past to prevent float inaccuracy hiccups
39         e.animstate_endtime = e.animstate_starttime + e.animstate_numframes / e.animstate_framerate;
40         e.animstate_looping = looping;
41         e.animstate_override = override;
42         e.frame = e.animstate_startframe;
43         e.frame1time = servertime;
44 }
45
46 void updateanim(entity e)
47 {
48         if (time >= e.animstate_endtime)
49         {
50                 if (e.animstate_looping)
51                 {
52                         e.animstate_starttime = e.animstate_endtime;
53                         e.animstate_endtime = e.animstate_starttime + e.animstate_numframes / e.animstate_framerate;
54                 }
55                 e.animstate_override = FALSE;
56         }
57         e.frame = e.animstate_startframe + bound(0, (time - e.animstate_starttime) * e.animstate_framerate, e.animstate_numframes - 1);
58         //print(ftos(time), " -> ", ftos(e.frame), "\n");
59 }
60
61 vector animfixfps(entity e, vector a)
62 {
63         // multi-frame anim: keep as-is
64         if(a_y == 1)
65         {
66                 float dur;
67                 dur = frameduration(e.modelindex, a_x);
68                 if(dur > 0)
69                         a_z = 1.0 / dur;
70         }
71         return a;
72 }
73
74 /*
75 ==================
76 SUB_Remove
77
78 Remove self
79 ==================
80 */
81 void SUB_Remove (void)
82 {
83         remove (self);
84 }
85
86 /*
87 ==================
88 SUB_Friction
89
90 Applies some friction to self
91 ==================
92 */
93 .float friction;
94 void SUB_Friction (void)
95 {
96         self.nextthink = time;
97         if(self.flags & FL_ONGROUND)
98                 self.velocity = self.velocity * (1 - frametime * self.friction);
99 }
100
101 /*
102 ==================
103 SUB_VanishOrRemove
104
105 Makes client invisible or removes non-client
106 ==================
107 */
108 void SUB_VanishOrRemove (entity ent)
109 {
110         if (ent.flags & FL_CLIENT)
111         {
112                 // vanish
113                 ent.alpha = -1;
114                 ent.effects = 0;
115                 ent.glow_size = 0;
116                 ent.pflags = 0;
117         }
118         else
119         {
120                 // remove
121                 remove (ent);
122         }
123 }
124
125 void SUB_SetFade_Think (void)
126 {
127         if(self.alpha == 0)
128                 self.alpha = 1;
129         self.think = SUB_SetFade_Think;
130         self.nextthink = time;
131         self.alpha -= frametime * self.fade_rate;
132         if (self.alpha < 0.01)
133                 SUB_VanishOrRemove(self);
134         else
135                 self.nextthink = time;
136 }
137
138 /*
139 ==================
140 SUB_SetFade
141
142 Fade 'ent' out when time >= 'when'
143 ==================
144 */
145 void SUB_SetFade (entity ent, float when, float fadetime)
146 {
147         //if (ent.flags & FL_CLIENT) // && ent.deadflag != DEAD_NO)
148         //      return;
149         //ent.alpha = 1;
150         ent.fade_rate = 1/fadetime;
151         ent.think = SUB_SetFade_Think;
152         ent.nextthink = when;
153 }
154
155 /*
156 =============
157 SUB_CalcMove
158
159 calculate self.velocity and self.nextthink to reach dest from
160 self.origin traveling at speed
161 ===============
162 */
163 void SUB_CalcMoveDone (void)
164 {
165         // After moving, set origin to exact final destination
166
167         setorigin (self, self.finaldest);
168         self.velocity = '0 0 0';
169         self.nextthink = -1;
170         if (self.think1)
171                 self.think1 ();
172 }
173
174 void SUB_CalcMove_controller_think (void)
175 {
176         entity oldself;
177         float traveltime;
178         float phasepos;
179         float nexttick;
180         vector delta;
181         vector delta2;
182         vector veloc;
183         vector nextpos;
184         delta = self.destvec;
185         delta2 = self.destvec2;
186         if(time < self.animstate_endtime) {
187                 nexttick = time + sys_frametime;
188
189                 traveltime = self.animstate_endtime - self.animstate_starttime;
190                 phasepos = (nexttick - self.animstate_starttime) / traveltime; // range: [0, 1]
191                 if(self.platmovetype != 1)
192                 {
193                         phasepos = 3.14159265 + (phasepos * 3.14159265); // range: [pi, 2pi]
194                         phasepos = cos(phasepos); // cos [pi, 2pi] is in [-1, 1]
195                         phasepos = phasepos + 1; // correct range to [0, 2]
196                         phasepos = phasepos / 2; // correct range to [0, 1]
197                 }
198                 nextpos = self.origin + (delta * phasepos) + (delta2 * phasepos * phasepos);
199                 // derivative: delta + 2 * delta2 * phasepos (e.g. for angle positioning)
200
201                 if(nexttick < self.animstate_endtime) {
202                         veloc = nextpos - self.owner.origin;
203                         veloc = veloc * (1 / sys_frametime); // so it arrives for the next frame
204                 } else {
205                         veloc = self.finaldest - self.owner.origin;
206                         veloc = veloc * (1 / sys_frametime); // so it arrives for the next frame
207                 }
208                 self.owner.velocity = veloc;
209                 self.nextthink = nexttick;
210         } else {
211                 // derivative: delta + 2 * delta2 (e.g. for angle positioning)
212                 oldself = self;
213                 self.owner.think = self.think1;
214                 self = self.owner;
215                 remove(oldself);
216                 self.think();
217         }
218 }
219
220 void SUB_CalcMove_controller_setbezier (entity controller, vector org, vector control, vector dest)
221 {
222         // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + dest * t * t
223         // 2 * control * t - 2 * control * t * t + dest * t * t
224         // 2 * control * t + (dest - 2 * control) * t * t
225
226         controller.origin = org; // starting point
227         control -= org;
228         dest -= org;
229
230         controller.destvec = 2 * control; // control point
231         controller.destvec2 = dest - 2 * control; // quadratic part required to reach end point
232 }
233
234 void SUB_CalcMove_controller_setlinear (entity controller, vector org, vector dest)
235 {
236         // 0 * (1-t) * (1-t) + 2 * control * t * (1-t) + dest * t * t
237         // 2 * control * t - 2 * control * t * t + dest * t * t
238         // 2 * control * t + (dest - 2 * control) * t * t
239
240         controller.origin = org; // starting point
241         dest -= org;
242
243         controller.destvec = dest; // end point
244         controller.destvec2 = '0 0 0';
245 }
246
247 void SUB_CalcMove_Bezier (vector tcontrol, vector tdest, float tspeed, void() func)
248 {
249         float   traveltime;
250         entity controller;
251
252         if (!tspeed)
253                 objerror ("No speed is defined!");
254
255         self.think1 = func;
256         self.finaldest = tdest;
257         self.think = SUB_CalcMoveDone;
258
259         if(tspeed > 0) // positive: start speed
260                 traveltime = 2 * vlen(tcontrol - self.origin) /  tspeed;
261         else // negative: end speed
262                 traveltime = 2 * vlen(tcontrol - tdest)       / -tspeed;
263
264         if (traveltime < 0.1) // useless anim
265         {
266                 self.velocity = '0 0 0';
267                 self.nextthink = self.ltime + 0.1;
268                 return;
269         }
270
271         controller = spawn();
272         controller.classname = "SUB_CalcMove_controller";
273         controller.owner = self;
274         controller.platmovetype = self.platmovetype;
275         SUB_CalcMove_controller_setbezier(controller, self.origin, tcontrol, tdest);
276         controller.finaldest = (tdest + '0 0 0.125'); // where do we want to end? Offset to overshoot a bit.
277         controller.animstate_starttime = time;
278         controller.animstate_endtime = time + traveltime;
279         controller.think = SUB_CalcMove_controller_think;
280         controller.think1 = self.think;
281
282         // the thinking is now done by the controller
283         self.think = SUB_Null;
284         self.nextthink = self.ltime + traveltime;
285         
286         // invoke controller
287         self = controller;
288         self.think();
289         self = self.owner;
290 }
291
292 void SUB_CalcMove (vector tdest, float tspeed, void() func)
293 {
294         vector  delta;
295         float   traveltime;
296
297         if (!tspeed)
298                 objerror ("No speed is defined!");
299
300         self.think1 = func;
301         self.finaldest = tdest;
302         self.think = SUB_CalcMoveDone;
303
304         if (tdest == self.origin)
305         {
306                 self.velocity = '0 0 0';
307                 self.nextthink = self.ltime + 0.1;
308                 return;
309         }
310
311         delta = tdest - self.origin;
312         traveltime = vlen (delta) / tspeed;
313
314         // Very short animations don't really show off the effect
315         // of controlled animation, so let's just use linear movement.
316         // Alternatively entities can choose to specify non-controlled movement.
317         // The only currently implemented alternative movement is linear (value 1)
318         if (traveltime < 0.15 || self.platmovetype == 1)
319         {
320                 self.velocity = delta * (1/traveltime); // QuakeC doesn't allow vector/float division
321                 self.nextthink = self.ltime + traveltime;
322                 return;
323         }
324
325         // now just run like a bezier curve...
326         SUB_CalcMove_Bezier((self.origin + tdest) * 0.5, tdest, tspeed, func);
327 }
328
329 void SUB_CalcMoveEnt (entity ent, vector tdest, float tspeed, void() func)
330 {
331         entity  oldself;
332
333         oldself = self;
334         self = ent;
335
336         SUB_CalcMove (tdest, tspeed, func);
337
338         self = oldself;
339 }
340
341 /*
342 =============
343 SUB_CalcAngleMove
344
345 calculate self.avelocity and self.nextthink to reach destangle from
346 self.angles rotating
347
348 The calling function should make sure self.think is valid
349 ===============
350 */
351 void SUB_CalcAngleMoveDone (void)
352 {
353         // After rotating, set angle to exact final angle
354         self.angles = self.finalangle;
355         self.avelocity = '0 0 0';
356         self.nextthink = -1;
357         if (self.think1)
358                 self.think1 ();
359 }
360
361 // FIXME: I fixed this function only for rotation around the main axes
362 void SUB_CalcAngleMove (vector destangle, float tspeed, void() func)
363 {
364         vector  delta;
365         float   traveltime;
366
367         if (!tspeed)
368                 objerror ("No speed is defined!");
369
370         // take the shortest distance for the angles
371         self.angles_x -= 360 * floor((self.angles_x - destangle_x) / 360 + 0.5);
372         self.angles_y -= 360 * floor((self.angles_y - destangle_y) / 360 + 0.5);
373         self.angles_z -= 360 * floor((self.angles_z - destangle_z) / 360 + 0.5);
374         delta = destangle - self.angles;
375         traveltime = vlen (delta) / tspeed;
376
377         self.think1 = func;
378         self.finalangle = destangle;
379         self.think = SUB_CalcAngleMoveDone;
380
381         if (traveltime < 0.1)
382         {
383                 self.avelocity = '0 0 0';
384                 self.nextthink = self.ltime + 0.1;
385                 return;
386         }
387
388         self.avelocity = delta * (1 / traveltime);
389         self.nextthink = self.ltime + traveltime;
390 }
391
392 void SUB_CalcAngleMoveEnt (entity ent, vector destangle, float tspeed, void() func)
393 {
394         entity  oldself;
395
396         oldself = self;
397         self = ent;
398
399         SUB_CalcAngleMove (destangle, tspeed, func);
400
401         self = oldself;
402 }
403
404 /*
405 ==================
406 main
407
408 unused but required by the engine
409 ==================
410 */
411 void main (void)
412 {
413
414 }
415
416 // Misc
417
418 /*
419 ==================
420 traceline_antilag
421
422 A version of traceline that must be used by SOLID_SLIDEBOX things that want to hit SOLID_CORPSE things with a trace attack
423 Additionally it moves players back into the past before the trace and restores them afterward.
424 ==================
425 */
426 void tracebox_antilag_force_wz (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag, float wz)
427 {
428         entity player;
429         float oldsolid;
430
431         // check whether antilagged traces are enabled
432         if (lag < 0.001)
433                 lag = 0;
434         if (clienttype(forent) != CLIENTTYPE_REAL)
435                 lag = 0; // only antilag for clients
436
437         // change shooter to SOLID_BBOX so the shot can hit corpses
438         oldsolid = source.dphitcontentsmask;
439         if(source)
440                 source.dphitcontentsmask = DPCONTENTS_SOLID | DPCONTENTS_BODY | DPCONTENTS_CORPSE;
441
442         if (lag)
443         {
444                 // take players back into the past
445                 FOR_EACH_PLAYER(player)
446                         if(player != forent)
447                                 antilag_takeback(player, time - lag);
448         }
449
450         // do the trace
451         if(wz)
452                 WarpZone_TraceBox (v1, mi, ma, v2, nomonst, forent);
453         else
454                 tracebox (v1, mi, ma, v2, nomonst, forent);
455
456         // restore players to current positions
457         if (lag)
458         {
459                 FOR_EACH_PLAYER(player)
460                         if(player != forent)
461                                 antilag_restore(player);
462         }
463
464         // restore shooter solid type
465         if(source)
466                 source.dphitcontentsmask = oldsolid;
467 }
468 void traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
469 {
470         tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, FALSE);
471 }
472 void traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
473 {
474         if (autocvar_g_antilag != 2 || source.cvar_cl_noantilag)
475                 lag = 0;
476         traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
477 }
478 void tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
479 {
480         if (autocvar_g_antilag != 2 || source.cvar_cl_noantilag)
481                 lag = 0;
482         tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, FALSE);
483 }
484 void WarpZone_traceline_antilag_force (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
485 {
486         tracebox_antilag_force_wz(source, v1, '0 0 0', '0 0 0', v2, nomonst, forent, lag, TRUE);
487 }
488 void WarpZone_traceline_antilag (entity source, vector v1, vector v2, float nomonst, entity forent, float lag)
489 {
490         if (autocvar_g_antilag != 2 || source.cvar_cl_noantilag)
491                 lag = 0;
492         WarpZone_traceline_antilag_force(source, v1, v2, nomonst, forent, lag);
493 }
494 void WarpZone_tracebox_antilag (entity source, vector v1, vector mi, vector ma, vector v2, float nomonst, entity forent, float lag)
495 {
496         if (autocvar_g_antilag != 2 || source.cvar_cl_noantilag)
497                 lag = 0;
498         tracebox_antilag_force_wz(source, v1, mi, ma, v2, nomonst, forent, lag, TRUE);
499 }
500
501 float tracebox_inverted (vector v1, vector mi, vector ma, vector v2, float nomonsters, entity forent, float stopatentity) // returns the number of traces done, for benchmarking
502 {
503         vector pos, dir, t;
504         float nudge;
505         entity stopentity;
506
507         //nudge = 2 * cvar("collision_impactnudge"); // why not?
508         nudge = 0.5;
509
510         dir = normalize(v2 - v1);
511
512         pos = v1 + dir * nudge;
513
514         float c;
515         c = 0;
516
517         for(;;)
518         {
519                 if((pos - v1) * dir >= (v2 - v1) * dir)
520                 {
521                         // went too far
522                         trace_fraction = 1;
523                         trace_endpos = v2;
524                         return c;
525                 }
526
527                 tracebox(pos, mi, ma, v2, nomonsters, forent);
528                 ++c;
529
530                 if(c == 50)
531                 {
532                         dprint("HOLY SHIT! When tracing from ", vtos(v1), " to ", vtos(v2), "\n");
533                         dprint("  Nudging gets us nowhere at ", vtos(pos), "\n");
534                         dprint("  trace_endpos is ", vtos(trace_endpos), "\n");
535                         dprint("  trace distance is ", ftos(vlen(pos - trace_endpos)), "\n");
536                 }
537
538                 stopentity = trace_ent;
539
540                 if(trace_startsolid)
541                 {
542                         // we started inside solid.
543                         // then trace from endpos to pos
544                         t = trace_endpos;
545                         tracebox(t, mi, ma, pos, nomonsters, forent);
546                         ++c;
547                         if(trace_startsolid)
548                         {
549                                 // t is still inside solid? bad
550                                 // force advance, then, and retry
551                                 pos = t + dir * nudge;
552
553                                 // but if we hit an entity, stop RIGHT before it
554                                 if(stopatentity && stopentity)
555                                 {
556                                         trace_ent = stopentity;
557                                         trace_endpos = t;
558                                         trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
559                                         return c;
560                                 }
561                         }
562                         else
563                         {
564                                 // we actually LEFT solid!
565                                 trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
566                                 return c;
567                         }
568                 }
569                 else
570                 {
571                         // pos is outside solid?!? but why?!? never mind, just return it.
572                         trace_endpos = pos;
573                         trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
574                         return c;
575                 }
576         }
577 }
578
579 void traceline_inverted (vector v1, vector v2, float nomonsters, entity forent, float stopatentity)
580 {
581         tracebox_inverted(v1, '0 0 0', '0 0 0', v2, nomonsters, forent, stopatentity);
582 }
583
584 /*
585 ==================
586 findbetterlocation
587
588 Returns a point at least 12 units away from walls
589 (useful for explosion animations, although the blast is performed where it really happened)
590 Ripped from DPMod
591 ==================
592 */
593 vector findbetterlocation (vector org, float mindist)
594 {
595         vector  loc;
596         vector vec;
597         float c, h;
598
599         vec = mindist * '1 0 0';
600         c = 0;
601         while (c < 6)
602         {
603                 traceline (org, org + vec, TRUE, world);
604                 vec = vec * -1;
605                 if (trace_fraction < 1)
606                 {
607                         loc = trace_endpos;
608                         traceline (loc, loc + vec, TRUE, world);
609                         if (trace_fraction >= 1)
610                                 org = loc + vec;
611                 }
612                 if (c & 1)
613                 {
614                         h = vec_y;
615                         vec_y = vec_x;
616                         vec_x = vec_z;
617                         vec_z = h;
618                 }
619                 c = c + 1;
620         }
621
622         return org;
623 }
624
625 /*
626 ==================
627 crandom
628
629 Returns a random number between -1.0 and 1.0
630 ==================
631 */
632 float crandom (void)
633 {
634         return 2 * (random () - 0.5);
635 }
636
637 /*
638 ==================
639 Angc used for animations
640 ==================
641 */
642
643
644 float angc (float a1, float a2)
645 {
646         float   a;
647
648         while (a1 > 180)
649                 a1 = a1 - 360;
650         while (a1 < -179)
651                 a1 = a1 + 360;
652
653         while (a2 > 180)
654                 a2 = a2 - 360;
655         while (a2 < -179)
656                 a2 = a2 + 360;
657
658         a = a1 - a2;
659         while (a > 180)
660                 a = a - 360;
661         while (a < -179)
662                 a = a + 360;
663
664         return a;
665 }
666
667 .string lodtarget1;
668 .string lodtarget2;
669 .string lodmodel1;
670 .string lodmodel2;
671 .float lodmodelindex0;
672 .float lodmodelindex1;
673 .float lodmodelindex2;
674 .float loddistance1;
675 .float loddistance2;
676
677 float LOD_customize()
678 {
679         float d;
680
681         if(autocvar_loddebug)
682         {
683                 d = autocvar_loddebug;
684                 if(d == 1)
685                         self.modelindex = self.lodmodelindex0;
686                 else if(d == 2 || !self.lodmodelindex2)
687                         self.modelindex = self.lodmodelindex1;
688                 else // if(d == 3)
689                         self.modelindex = self.lodmodelindex2;
690                 return TRUE;
691         }
692
693         // TODO csqc network this so it only gets sent once
694         d = vlen(NearestPointOnBox(self, other.origin) - other.origin);
695         if(d < self.loddistance1)
696                 self.modelindex = self.lodmodelindex0;
697         else if(!self.lodmodelindex2 || d < self.loddistance2)
698                 self.modelindex = self.lodmodelindex1;
699         else
700                 self.modelindex = self.lodmodelindex2;
701
702         return TRUE;
703 }
704
705 void LOD_uncustomize()
706 {
707         self.modelindex = self.lodmodelindex0;
708 }
709
710 void LODmodel_attach()
711 {
712         entity e;
713
714         if(!self.loddistance1)
715                 self.loddistance1 = 1000;
716         if(!self.loddistance2)
717                 self.loddistance2 = 2000;
718         self.lodmodelindex0 = self.modelindex;
719
720         if(self.lodtarget1 != "")
721         {
722                 e = find(world, targetname, self.lodtarget1);
723                 if(e)
724                 {
725                         self.lodmodel1 = e.model;
726                         remove(e);
727                 }
728         }
729         if(self.lodtarget2 != "")
730         {
731                 e = find(world, targetname, self.lodtarget2);
732                 if(e)
733                 {
734                         self.lodmodel2 = e.model;
735                         remove(e);
736                 }
737         }
738
739         if(autocvar_loddebug < 0)
740         {
741                 self.lodmodel1 = self.lodmodel2 = ""; // don't even initialize
742         }
743
744         if(self.lodmodel1 != "")
745         {
746                 vector mi, ma;
747                 mi = self.mins;
748                 ma = self.maxs;
749
750                 precache_model(self.lodmodel1);
751                 setmodel(self, self.lodmodel1);
752                 self.lodmodelindex1 = self.modelindex;
753
754                 if(self.lodmodel2 != "")
755                 {
756                         precache_model(self.lodmodel2);
757                         setmodel(self, self.lodmodel2);
758                         self.lodmodelindex2 = self.modelindex;
759                 }
760
761                 self.modelindex = self.lodmodelindex0;
762                 setsize(self, mi, ma);
763         }
764
765         if(self.lodmodelindex1)
766                 if not(self.SendEntity)
767                         SetCustomizer(self, LOD_customize, LOD_uncustomize);
768 }
769
770 void ApplyMinMaxScaleAngles(entity e)
771 {
772         if(e.angles_x != 0 || e.angles_z != 0 || self.avelocity_x != 0 || self.avelocity_z != 0) // "weird" rotation
773         {
774                 e.maxs = '1 1 1' * vlen(
775                         '1 0 0' * max(-e.mins_x, e.maxs_x) +
776                         '0 1 0' * max(-e.mins_y, e.maxs_y) +
777                         '0 0 1' * max(-e.mins_z, e.maxs_z)
778                 );
779                 e.mins = -e.maxs;
780         }
781         else if(e.angles_y != 0 || self.avelocity_y != 0) // yaw only is a bit better
782         {
783                 e.maxs_x = vlen(
784                         '1 0 0' * max(-e.mins_x, e.maxs_x) +
785                         '0 1 0' * max(-e.mins_y, e.maxs_y)
786                 );
787                 e.maxs_y = e.maxs_x;
788                 e.mins_x = -e.maxs_x;
789                 e.mins_y = -e.maxs_x;
790         }
791         if(e.scale)
792                 setsize(e, e.mins * e.scale, e.maxs * e.scale);
793         else
794                 setsize(e, e.mins, e.maxs);
795 }
796
797 void SetBrushEntityModel()
798 {
799         if(self.model != "")
800         {
801                 precache_model(self.model);
802                 if(self.mins != '0 0 0' || self.maxs != '0 0 0')
803                 {
804                         vector mi = self.mins;
805                         vector ma = self.maxs;
806                         setmodel(self, self.model); // no precision needed
807                         setsize(self, mi, ma);
808                 }
809                 else
810                         setmodel(self, self.model); // no precision needed
811                 InitializeEntity(self, LODmodel_attach, INITPRIO_FINDTARGET);
812         }
813         setorigin(self, self.origin);
814         ApplyMinMaxScaleAngles(self);
815 }
816
817 void SetBrushEntityModelNoLOD()
818 {
819         if(self.model != "")
820         {
821                 precache_model(self.model);
822                 if(self.mins != '0 0 0' || self.maxs != '0 0 0')
823                 {
824                         vector mi = self.mins;
825                         vector ma = self.maxs;
826                         setmodel(self, self.model); // no precision needed
827                         setsize(self, mi, ma);
828                 }
829                 else
830                         setmodel(self, self.model); // no precision needed
831         }
832         setorigin(self, self.origin);
833         ApplyMinMaxScaleAngles(self);
834 }
835
836 /*
837 ================
838 InitTrigger
839 ================
840 */
841
842 void SetMovedir()
843 {
844         if (self.movedir != '0 0 0')
845                 self.movedir = normalize(self.movedir);
846         else
847         {
848                 makevectors (self.angles);
849                 self.movedir = v_forward;
850         }
851
852         self.angles = '0 0 0';
853 }
854
855 void InitTrigger()
856 {
857 // trigger angles are used for one-way touches.  An angle of 0 is assumed
858 // to mean no restrictions, so use a yaw of 360 instead.
859         SetMovedir ();
860         self.solid = SOLID_TRIGGER;
861         SetBrushEntityModel();
862         self.movetype = MOVETYPE_NONE;
863         self.modelindex = 0;
864         self.model = "";
865 }
866
867 void InitSolidBSPTrigger()
868 {
869 // trigger angles are used for one-way touches.  An angle of 0 is assumed
870 // to mean no restrictions, so use a yaw of 360 instead.
871         SetMovedir ();
872         self.solid = SOLID_BSP;
873         SetBrushEntityModel();
874         self.movetype = MOVETYPE_NONE; // why was this PUSH? -div0
875 //      self.modelindex = 0;
876         self.model = "";
877 }
878
879 float InitMovingBrushTrigger()
880 {
881 // trigger angles are used for one-way touches.  An angle of 0 is assumed
882 // to mean no restrictions, so use a yaw of 360 instead.
883         self.solid = SOLID_BSP;
884         SetBrushEntityModel();
885         self.movetype = MOVETYPE_PUSH;
886         if(self.modelindex == 0)
887         {
888                 objerror("InitMovingBrushTrigger: no brushes found!");
889                 return 0;
890         }
891         return 1;
892 }