]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/g_subs.qc
0aaf5d337c2c1be0b2e689e54952ddbed3bbb9e9
[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) // returns the number of traces done, for benchmarking
502 {
503         vector pos, dir, t;
504         float nudge;
505
506         //nudge = 2 * cvar("collision_impactnudge"); // why not?
507         nudge = 0.5;
508
509         dir = normalize(v2 - v1);
510
511         pos = v1 + dir * nudge;
512
513         float c;
514         c = 0;
515
516         for(;;)
517         {
518                 if((pos - v1) * dir >= (v2 - v1) * dir)
519                 {
520                         // went too far
521                         trace_fraction = 1;
522                         trace_endpos = v2;
523                         return c;
524                 }
525
526                 tracebox(pos, mi, ma, v2, nomonsters, forent);
527                 ++c;
528
529                 if(c == 50)
530                 {
531                         dprint("HOLY SHIT! When tracing from ", vtos(v1), " to ", vtos(v2), "\n");
532                         dprint("  Nudging gets us nowhere at ", vtos(pos), "\n");
533                         dprint("  trace_endpos is ", vtos(trace_endpos), "\n");
534                         dprint("  trace distance is ", ftos(vlen(pos - trace_endpos)), "\n");
535                 }
536
537                 if(trace_startsolid)
538                 {
539                         // we started inside solid.
540                         // then trace from endpos to pos
541                         t = trace_endpos;
542                         tracebox(t, mi, ma, pos, nomonsters, forent);
543                         ++c;
544                         if(trace_startsolid)
545                         {
546                                 // t is still inside solid? bad
547                                 // force advance, then, and retry
548                                 pos = t + dir * nudge;
549                         }
550                         else
551                         {
552                                 // we actually LEFT solid!
553                                 trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
554                                 return c;
555                         }
556                 }
557                 else
558                 {
559                         // pos is outside solid?!? but why?!? never mind, just return it.
560                         trace_endpos = pos;
561                         trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
562                         return c;
563                 }
564         }
565 }
566
567 void traceline_inverted (vector v1, vector v2, float nomonsters, entity forent)
568 {
569 #if 0
570         vector pos, dir, t;
571         float nudge;
572
573         //nudge = 2 * cvar("collision_impactnudge"); // why not?
574         nudge = 0.5;
575
576         dir = normalize(v2 - v1);
577
578         pos = v1 + dir * nudge;
579
580         for(;;)
581         {
582                 if((pos - v1) * dir >= (v2 - v1) * dir)
583                 {
584                         // went too far
585                         trace_fraction = 1;
586                         return;
587                 }
588
589                 traceline(pos, v2, nomonsters, forent);
590
591                 if(trace_startsolid)
592                 {
593                         // we started inside solid.
594                         // then trace from endpos to pos
595                         t = trace_endpos;
596                         traceline(t, pos, nomonsters, forent);
597                         if(trace_startsolid)
598                         {
599                                 // t is inside solid? bad
600                                 // force advance, then
601                                 pos = pos + dir * nudge;
602                         }
603                         else
604                         {
605                                 // we actually LEFT solid!
606                                 trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
607                                 return;
608                         }
609                 }
610                 else
611                 {
612                         // pos is outside solid?!? but why?!? never mind, just return it.
613                         trace_endpos = pos;
614                         trace_fraction = ((trace_endpos - v1) * dir) / ((v2 - v1) * dir);
615                         return;
616                 }
617         }
618 #else
619         tracebox_inverted(v1, '0 0 0', '0 0 0', v2, nomonsters, forent);
620 }
621
622 /*
623 ==================
624 findbetterlocation
625
626 Returns a point at least 12 units away from walls
627 (useful for explosion animations, although the blast is performed where it really happened)
628 Ripped from DPMod
629 ==================
630 */
631 vector findbetterlocation (vector org, float mindist)
632 {
633         vector  loc;
634         vector vec;
635         float c, h;
636
637         vec = mindist * '1 0 0';
638         c = 0;
639         while (c < 6)
640         {
641                 traceline (org, org + vec, TRUE, world);
642                 vec = vec * -1;
643                 if (trace_fraction < 1)
644                 {
645                         loc = trace_endpos;
646                         traceline (loc, loc + vec, TRUE, world);
647                         if (trace_fraction >= 1)
648                                 org = loc + vec;
649                 }
650                 if (c & 1)
651                 {
652                         h = vec_y;
653                         vec_y = vec_x;
654                         vec_x = vec_z;
655                         vec_z = h;
656                 }
657                 c = c + 1;
658         }
659
660         return org;
661 }
662
663 /*
664 ==================
665 crandom
666
667 Returns a random number between -1.0 and 1.0
668 ==================
669 */
670 float crandom (void)
671 {
672         return 2 * (random () - 0.5);
673 }
674
675 /*
676 ==================
677 Angc used for animations
678 ==================
679 */
680
681
682 float angc (float a1, float a2)
683 {
684         float   a;
685
686         while (a1 > 180)
687                 a1 = a1 - 360;
688         while (a1 < -179)
689                 a1 = a1 + 360;
690
691         while (a2 > 180)
692                 a2 = a2 - 360;
693         while (a2 < -179)
694                 a2 = a2 + 360;
695
696         a = a1 - a2;
697         while (a > 180)
698                 a = a - 360;
699         while (a < -179)
700                 a = a + 360;
701
702         return a;
703 }
704
705 .string lodtarget1;
706 .string lodtarget2;
707 .string lodmodel1;
708 .string lodmodel2;
709 .float lodmodelindex0;
710 .float lodmodelindex1;
711 .float lodmodelindex2;
712 .float loddistance1;
713 .float loddistance2;
714
715 float LOD_customize()
716 {
717         float d;
718
719         if(autocvar_loddebug)
720         {
721                 d = autocvar_loddebug;
722                 if(d == 1)
723                         self.modelindex = self.lodmodelindex0;
724                 else if(d == 2 || !self.lodmodelindex2)
725                         self.modelindex = self.lodmodelindex1;
726                 else // if(d == 3)
727                         self.modelindex = self.lodmodelindex2;
728                 return TRUE;
729         }
730
731         // TODO csqc network this so it only gets sent once
732         d = vlen(NearestPointOnBox(self, other.origin) - other.origin);
733         if(d < self.loddistance1)
734                 self.modelindex = self.lodmodelindex0;
735         else if(!self.lodmodelindex2 || d < self.loddistance2)
736                 self.modelindex = self.lodmodelindex1;
737         else
738                 self.modelindex = self.lodmodelindex2;
739
740         return TRUE;
741 }
742
743 void LOD_uncustomize()
744 {
745         self.modelindex = self.lodmodelindex0;
746 }
747
748 void LODmodel_attach()
749 {
750         entity e;
751
752         if(!self.loddistance1)
753                 self.loddistance1 = 1000;
754         if(!self.loddistance2)
755                 self.loddistance2 = 2000;
756         self.lodmodelindex0 = self.modelindex;
757
758         if(self.lodtarget1 != "")
759         {
760                 e = find(world, targetname, self.lodtarget1);
761                 if(e)
762                 {
763                         self.lodmodel1 = e.model;
764                         remove(e);
765                 }
766         }
767         if(self.lodtarget2 != "")
768         {
769                 e = find(world, targetname, self.lodtarget2);
770                 if(e)
771                 {
772                         self.lodmodel2 = e.model;
773                         remove(e);
774                 }
775         }
776
777         if(autocvar_loddebug < 0)
778         {
779                 self.lodmodel1 = self.lodmodel2 = ""; // don't even initialize
780         }
781
782         if(self.lodmodel1 != "")
783         {
784                 vector mi, ma;
785                 mi = self.mins;
786                 ma = self.maxs;
787
788                 precache_model(self.lodmodel1);
789                 setmodel(self, self.lodmodel1);
790                 self.lodmodelindex1 = self.modelindex;
791
792                 if(self.lodmodel2 != "")
793                 {
794                         precache_model(self.lodmodel2);
795                         setmodel(self, self.lodmodel2);
796                         self.lodmodelindex2 = self.modelindex;
797                 }
798
799                 self.modelindex = self.lodmodelindex0;
800                 setsize(self, mi, ma);
801         }
802
803         if(self.lodmodelindex1)
804                 if not(self.SendEntity)
805                         SetCustomizer(self, LOD_customize, LOD_uncustomize);
806 }
807
808 void ApplyMinMaxScaleAngles(entity e)
809 {
810         if(e.angles_x != 0 || e.angles_z != 0 || self.avelocity_x != 0 || self.avelocity_z != 0) // "weird" rotation
811         {
812                 e.maxs = '1 1 1' * vlen(
813                         '1 0 0' * max(-e.mins_x, e.maxs_x) +
814                         '0 1 0' * max(-e.mins_y, e.maxs_y) +
815                         '0 0 1' * max(-e.mins_z, e.maxs_z)
816                 );
817                 e.mins = -e.maxs;
818         }
819         else if(e.angles_y != 0 || self.avelocity_y != 0) // yaw only is a bit better
820         {
821                 e.maxs_x = vlen(
822                         '1 0 0' * max(-e.mins_x, e.maxs_x) +
823                         '0 1 0' * max(-e.mins_y, e.maxs_y)
824                 );
825                 e.maxs_y = e.maxs_x;
826                 e.mins_x = -e.maxs_x;
827                 e.mins_y = -e.maxs_x;
828         }
829         if(e.scale)
830                 setsize(e, e.mins * e.scale, e.maxs * e.scale);
831         else
832                 setsize(e, e.mins, e.maxs);
833 }
834
835 void SetBrushEntityModel()
836 {
837         if(self.model != "")
838         {
839                 precache_model(self.model);
840                 setmodel(self, self.model); // no precision needed
841                 InitializeEntity(self, LODmodel_attach, INITPRIO_FINDTARGET);
842         }
843         setorigin(self, self.origin);
844         ApplyMinMaxScaleAngles(self);
845 }
846
847 void SetBrushEntityModelNoLOD()
848 {
849         if(self.model != "")
850         {
851                 precache_model(self.model);
852                 setmodel(self, self.model); // no precision needed
853         }
854         setorigin(self, self.origin);
855         ApplyMinMaxScaleAngles(self);
856 }
857
858 /*
859 ================
860 InitTrigger
861 ================
862 */
863
864 void SetMovedir()
865 {
866         if (self.movedir != '0 0 0')
867                 self.movedir = normalize(self.movedir);
868         else
869         {
870                 makevectors (self.angles);
871                 self.movedir = v_forward;
872         }
873
874         self.angles = '0 0 0';
875 }
876
877 void InitTrigger()
878 {
879 // trigger angles are used for one-way touches.  An angle of 0 is assumed
880 // to mean no restrictions, so use a yaw of 360 instead.
881         SetMovedir ();
882         self.solid = SOLID_TRIGGER;
883         SetBrushEntityModel();
884         self.movetype = MOVETYPE_NONE;
885         self.modelindex = 0;
886         self.model = "";
887 }
888
889 void InitSolidBSPTrigger()
890 {
891 // trigger angles are used for one-way touches.  An angle of 0 is assumed
892 // to mean no restrictions, so use a yaw of 360 instead.
893         SetMovedir ();
894         self.solid = SOLID_BSP;
895         SetBrushEntityModel();
896         self.movetype = MOVETYPE_NONE; // why was this PUSH? -div0
897 //      self.modelindex = 0;
898         self.model = "";
899 }
900
901 float InitMovingBrushTrigger()
902 {
903 // trigger angles are used for one-way touches.  An angle of 0 is assumed
904 // to mean no restrictions, so use a yaw of 360 instead.
905         self.solid = SOLID_BSP;
906         SetBrushEntityModel();
907         self.movetype = MOVETYPE_PUSH;
908         if(self.modelindex == 0)
909         {
910                 objerror("InitMovingBrushTrigger: no brushes found!");
911                 return 0;
912         }
913         return 1;
914 }