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