]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - world.c
updated to version 1.50, build 75.
[xonotic/darkplaces.git] / world.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // world.c -- world query functions
21
22 #include "quakedef.h"
23
24 /*
25
26 entities never clip against themselves, or their owner
27
28 line of sight checks trace->crosscontent, but bullets don't
29
30 */
31
32
33 typedef struct
34 {
35         vec3_t          boxmins, boxmaxs;// enclose the test object along entire move
36         float           *mins, *maxs;   // size of the moving object
37         vec3_t          mins2, maxs2;   // size when clipping against mosnters
38         float           *start, *end;
39         trace_t         trace;
40         int                     type;
41         edict_t         *passedict;
42 } moveclip_t;
43
44
45 /*
46 ===============================================================================
47
48 HULL BOXES
49
50 ===============================================================================
51 */
52
53
54 static  hull_t          box_hull;
55 static  dclipnode_t     box_clipnodes[6];
56 static  mplane_t        box_planes[6];
57
58 /*
59 ===================
60 SV_InitBoxHull
61
62 Set up the planes and clipnodes so that the six floats of a bounding box
63 can just be stored out and get a proper hull_t structure.
64 ===================
65 */
66 void SV_InitBoxHull (void)
67 {
68         int             i;
69         int             side;
70
71         box_hull.clipnodes = box_clipnodes;
72         box_hull.planes = box_planes;
73         box_hull.firstclipnode = 0;
74         box_hull.lastclipnode = 5;
75
76         for (i=0 ; i<6 ; i++)
77         {
78                 box_clipnodes[i].planenum = i;
79                 
80                 side = i&1;
81                 
82                 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
83                 if (i != 5)
84                         box_clipnodes[i].children[side^1] = i + 1;
85                 else
86                         box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
87                 
88                 box_planes[i].type = i>>1;
89                 box_planes[i].normal[i>>1] = 1;
90         }
91         
92 }
93
94
95 /*
96 ===================
97 SV_HullForBox
98
99 To keep everything totally uniform, bounding boxes are turned into small
100 BSP trees instead of being compared directly.
101 ===================
102 */
103 hull_t  *SV_HullForBox (vec3_t mins, vec3_t maxs)
104 {
105         box_planes[0].dist = maxs[0];
106         box_planes[1].dist = mins[0];
107         box_planes[2].dist = maxs[1];
108         box_planes[3].dist = mins[1];
109         box_planes[4].dist = maxs[2];
110         box_planes[5].dist = mins[2];
111
112         return &box_hull;
113 }
114
115
116
117 /*
118 ================
119 SV_HullForEntity
120
121 Returns a hull that can be used for testing or clipping an object of mins/maxs
122 size.
123 Offset is filled in to contain the adjustment that must be added to the
124 testing object's origin to get a point to use with the returned hull.
125 ================
126 */
127 extern qboolean hlbsp;
128 hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
129 {
130         model_t         *model;
131         vec3_t          size;
132         vec3_t          hullmins, hullmaxs;
133         hull_t          *hull;
134
135 // decide which clipping hull to use, based on the size
136         if (ent->v.solid == SOLID_BSP)
137         {       // explicit hulls in the BSP model
138                 if (ent->v.movetype != MOVETYPE_PUSH)
139                         Host_Error ("SOLID_BSP without MOVETYPE_PUSH");
140
141                 model = sv.models[ (int)ent->v.modelindex ];
142
143                 // LordHavoc: fixed SOLID_BSP error message
144                 if (!model || model->type != mod_brush)
145                 {
146                         Con_Printf ("SOLID_BSP with a non bsp model, entity dump:\n");
147                         ED_Print (ent);
148                         Host_Error ("SOLID_BSP with a non bsp model\n");
149                 }
150
151                 VectorSubtract (maxs, mins, size);
152                 // LordHavoc: FIXME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
153                 if (hlbsp)
154                 {
155                         if (size[0] < 3)
156                                 hull = &model->hulls[0]; // 0x0x0
157                         else if (size[0] <= 32)
158                         {
159                                 if (size[2] < 54) // pick the nearest of 36 or 72
160                                         hull = &model->hulls[3]; // 32x32x36
161                                 else
162                                         hull = &model->hulls[1]; // 32x32x72
163                         }
164                         else
165                                 hull = &model->hulls[2]; // 64x64x64
166                 }
167                 else
168                 {
169                         if (size[0] < 3)
170                                 hull = &model->hulls[0]; // 0x0x0
171                         else if (size[0] <= 32)
172                                 hull = &model->hulls[1]; // 32x32x56
173                         else
174                                 hull = &model->hulls[2]; // 64x64x88
175                 }
176
177 // calculate an offset value to center the origin
178                 VectorSubtract (hull->clip_mins, mins, offset);
179                 VectorAdd (offset, ent->v.origin, offset);
180         }
181         else
182         {       // create a temp hull from bounding box sizes
183
184                 VectorSubtract (ent->v.mins, maxs, hullmins);
185                 VectorSubtract (ent->v.maxs, mins, hullmaxs);
186                 hull = SV_HullForBox (hullmins, hullmaxs);
187                 
188                 VectorCopy (ent->v.origin, offset);
189         }
190
191
192         return hull;
193 }
194
195 /*
196 ===============================================================================
197
198 ENTITY AREA CHECKING
199
200 ===============================================================================
201 */
202
203 typedef struct areanode_s
204 {
205         int             axis;           // -1 = leaf node
206         float   dist;
207         struct areanode_s       *children[2];
208         link_t  trigger_edicts;
209         link_t  solid_edicts;
210 } areanode_t;
211
212 #define AREA_DEPTH      4
213 #define AREA_NODES      32
214
215 static  areanode_t      sv_areanodes[AREA_NODES];
216 static  int                     sv_numareanodes;
217
218 /*
219 ===============
220 SV_CreateAreaNode
221
222 ===============
223 */
224 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
225 {
226         areanode_t      *anode;
227         vec3_t          size;
228         vec3_t          mins1, maxs1, mins2, maxs2;
229
230         anode = &sv_areanodes[sv_numareanodes];
231         sv_numareanodes++;
232
233         ClearLink (&anode->trigger_edicts);
234         ClearLink (&anode->solid_edicts);
235         
236         if (depth == AREA_DEPTH)
237         {
238                 anode->axis = -1;
239                 anode->children[0] = anode->children[1] = NULL;
240                 return anode;
241         }
242         
243         VectorSubtract (maxs, mins, size);
244         if (size[0] > size[1])
245                 anode->axis = 0;
246         else
247                 anode->axis = 1;
248         
249         anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
250         VectorCopy (mins, mins1);       
251         VectorCopy (mins, mins2);       
252         VectorCopy (maxs, maxs1);       
253         VectorCopy (maxs, maxs2);       
254         
255         maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
256         
257         anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
258         anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
259
260         return anode;
261 }
262
263 /*
264 ===============
265 SV_ClearWorld
266
267 ===============
268 */
269 void SV_ClearWorld (void)
270 {
271         SV_InitBoxHull ();
272         
273         memset (sv_areanodes, 0, sizeof(sv_areanodes));
274         sv_numareanodes = 0;
275         SV_CreateAreaNode (0, sv.worldmodel->mins, sv.worldmodel->maxs);
276 }
277
278
279 /*
280 ===============
281 SV_UnlinkEdict
282
283 ===============
284 */
285 void SV_UnlinkEdict (edict_t *ent)
286 {
287         if (!ent->area.prev)
288                 return;         // not linked in anywhere
289         RemoveLink (&ent->area);
290         ent->area.prev = ent->area.next = NULL;
291 }
292
293
294 /*
295 ====================
296 SV_TouchLinks
297 ====================
298 */
299 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
300 {
301         link_t          *l, *next;
302         edict_t         *touch;
303         int                     old_self, old_other;
304
305 loc0:
306 // touch linked edicts
307         for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
308         {
309                 next = l->next;
310                 touch = EDICT_FROM_AREA(l);
311                 if (touch == ent)
312                         continue;
313                 if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
314                         continue;
315                 if (ent->v.absmin[0] > touch->v.absmax[0]
316                 || ent->v.absmin[1] > touch->v.absmax[1]
317                 || ent->v.absmin[2] > touch->v.absmax[2]
318                 || ent->v.absmax[0] < touch->v.absmin[0]
319                 || ent->v.absmax[1] < touch->v.absmin[1]
320                 || ent->v.absmax[2] < touch->v.absmin[2] )
321                         continue;
322                 old_self = pr_global_struct->self;
323                 old_other = pr_global_struct->other;
324
325                 pr_global_struct->self = EDICT_TO_PROG(touch);
326                 pr_global_struct->other = EDICT_TO_PROG(ent);
327                 pr_global_struct->time = sv.time;
328                 PR_ExecuteProgram (touch->v.touch);
329
330                 pr_global_struct->self = old_self;
331                 pr_global_struct->other = old_other;
332         }
333         
334 // recurse down both sides
335         if (node->axis == -1)
336                 return;
337         
338         // LordHavoc: optimized recursion
339 //      if (ent->v.absmax[node->axis] > node->dist) SV_TouchLinks (ent, node->children[0]);
340 //      if (ent->v.absmin[node->axis] < node->dist) SV_TouchLinks (ent, node->children[1]);
341         if (ent->v.absmax[node->axis] > node->dist)
342         {
343                 if (ent->v.absmin[node->axis] < node->dist)
344                         SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
345                 node = node->children[0];
346                 goto loc0;
347         }
348         else
349         {
350                 if (ent->v.absmin[node->axis] < node->dist)
351                 {
352                         node = node->children[1];
353                         goto loc0;
354                 }
355         }
356 }
357
358
359 /*
360 ===============
361 SV_FindTouchedLeafs
362
363 ===============
364 */
365 void SV_FindTouchedLeafs (edict_t *ent, mnode_t *node)
366 {
367         mplane_t        *splitplane;
368         mleaf_t         *leaf;
369         int                     sides;
370         int                     leafnum;
371
372 loc0:
373         if (node->contents == CONTENTS_SOLID)
374                 return;
375         
376 // add an efrag if the node is a leaf
377
378         if ( node->contents < 0)
379         {
380                 if (ent->num_leafs == MAX_ENT_LEAFS)
381                         return;
382
383                 leaf = (mleaf_t *)node;
384                 leafnum = leaf - sv.worldmodel->leafs - 1;
385
386                 ent->leafnums[ent->num_leafs] = leafnum;
387                 ent->num_leafs++;                       
388                 return;
389         }
390         
391 // NODE_MIXED
392
393         splitplane = node->plane;
394         sides = BOX_ON_PLANE_SIDE(ent->v.absmin, ent->v.absmax, splitplane);
395         
396 // recurse down the contacted sides
397         // LordHavoc: optimized recursion
398 //      if (sides & 1) SV_FindTouchedLeafs (ent, node->children[0]);
399 //      if (sides & 2) SV_FindTouchedLeafs (ent, node->children[1]);
400         switch (sides)
401         {
402         case 1:
403                 node = node->children[0];
404                 goto loc0;
405         case 2:
406                 node = node->children[1];
407                 goto loc0;
408         default: // 3
409                 if (node->children[0]->contents != CONTENTS_SOLID)
410                         SV_FindTouchedLeafs (ent, node->children[0]);
411                 node = node->children[1];
412                 goto loc0;
413         }
414 }
415
416 /*
417 ===============
418 SV_LinkEdict
419
420 ===============
421 */
422 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
423 {
424         areanode_t      *node;
425
426         if (ent->area.prev)
427                 SV_UnlinkEdict (ent);   // unlink from old position
428                 
429         if (ent == sv.edicts)
430                 return;         // don't add the world
431
432         if (ent->free)
433                 return;
434
435 // set the abs box
436
437 // LordHavoc: enabling rotating bmodels
438         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
439         {       // expand for rotation
440                 float           max, v;
441                 int                     i;
442
443                 max = 0;
444                 for (i=0 ; i<3 ; i++)
445                 {
446                         v =fabs( ent->v.mins[i]);
447                         if (v > max)
448                                 max = v;
449                         v =fabs( ent->v.maxs[i]);
450                         if (v > max)
451                                 max = v;
452                 }
453                 for (i=0 ; i<3 ; i++)
454                 {
455                         ent->v.absmin[i] = ent->v.origin[i] - max;
456                         ent->v.absmax[i] = ent->v.origin[i] + max;
457                 }
458         }
459         else
460         {
461                 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);  
462                 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
463         }
464
465 //
466 // to make items easier to pick up and allow them to be grabbed off
467 // of shelves, the abs sizes are expanded
468 //
469         if ((int)ent->v.flags & FL_ITEM)
470         {
471                 ent->v.absmin[0] -= 15;
472                 ent->v.absmin[1] -= 15;
473                 ent->v.absmax[0] += 15;
474                 ent->v.absmax[1] += 15;
475         }
476         else
477         {       // because movement is clipped an epsilon away from an actual edge,
478                 // we must fully check even when bounding boxes don't quite touch
479                 ent->v.absmin[0] -= 1;
480                 ent->v.absmin[1] -= 1;
481                 ent->v.absmin[2] -= 1;
482                 ent->v.absmax[0] += 1;
483                 ent->v.absmax[1] += 1;
484                 ent->v.absmax[2] += 1;
485         }
486         
487 // link to PVS leafs
488         ent->num_leafs = 0;
489         if (ent->v.modelindex)
490                 SV_FindTouchedLeafs (ent, sv.worldmodel->nodes);
491
492         if (ent->v.solid == SOLID_NOT)
493                 return;
494
495 // find the first node that the ent's box crosses
496         node = sv_areanodes;
497         while (1)
498         {
499                 if (node->axis == -1)
500                         break;
501                 if (ent->v.absmin[node->axis] > node->dist)
502                         node = node->children[0];
503                 else if (ent->v.absmax[node->axis] < node->dist)
504                         node = node->children[1];
505                 else
506                         break;          // crosses the node
507         }
508         
509 // link it in   
510
511         if (ent->v.solid == SOLID_TRIGGER)
512                 InsertLinkBefore (&ent->area, &node->trigger_edicts);
513         else
514                 InsertLinkBefore (&ent->area, &node->solid_edicts);
515         
516 // if touch_triggers, touch all entities at this node and descend for more
517         if (touch_triggers)
518                 SV_TouchLinks ( ent, sv_areanodes );
519 }
520
521
522
523 /*
524 ===============================================================================
525
526 POINT TESTING IN HULLS
527
528 ===============================================================================
529 */
530
531 // SV_HullPointContents moved to cpu_noasm.c
532
533 /*
534 ============
535 SV_TestEntityPosition
536
537 This could be a lot more efficient...
538 ============
539 */
540 edict_t *SV_TestEntityPosition (edict_t *ent)
541 {
542         trace_t trace;
543
544         trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, 0, ent);
545         
546         if (trace.startsolid)
547                 return sv.edicts;
548                 
549         return NULL;
550 }
551
552
553 /*
554 ===============================================================================
555
556 LINE TESTING IN HULLS
557
558 ===============================================================================
559 */
560
561 // 1/32 epsilon to keep floating point happy
562 #define DIST_EPSILON    (0.03125)
563
564 /*
565 ==================
566 SV_RecursiveHullCheck
567
568 ==================
569 */
570 qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
571 {
572         dclipnode_t     *node;
573         mplane_t        *plane;
574         float           t1, t2;
575         float           frac;
576         int                     i;
577         vec3_t          mid;
578         int                     side;
579         float           midf;
580
581 loc0:
582 // check for empty
583         if (num < 0)
584         {
585                 if (num != CONTENTS_SOLID)
586                 {
587                         trace->allsolid = false;
588                         if (num == CONTENTS_EMPTY)
589                                 trace->inopen = true;
590                         else
591                                 trace->inwater = true;
592                 }
593                 else
594                         trace->startsolid = true;
595                 return true;            // empty
596         }
597
598         if (num < hull->firstclipnode || num > hull->lastclipnode)
599                 Sys_Error ("SV_RecursiveHullCheck: bad node number");
600
601 //
602 // find the point distances
603 //
604         node = hull->clipnodes + num;
605         plane = hull->planes + node->planenum;
606
607         t1 = PlaneDiff(p1, plane);
608         t2 = PlaneDiff(p2, plane);
609         
610 #if 1
611         if (t1 >= 0 && t2 >= 0)
612         // LordHavoc: optimized recursion
613 //              return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
614         {
615                 num = node->children[0];
616                 goto loc0;
617         }
618         if (t1 < 0 && t2 < 0)
619 //              return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
620         {
621                 num = node->children[1];
622                 goto loc0;
623         }
624 #else
625         if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
626                 return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
627         if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
628                 return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
629 #endif
630
631 // put the crosspoint DIST_EPSILON pixels on the near side
632         if (t1 < 0)
633                 frac = bound(0, (t1 + DIST_EPSILON)/(t1-t2), 1);
634         else
635                 frac = bound(0, (t1 - DIST_EPSILON)/(t1-t2), 1);
636                 
637         midf = p1f + (p2f - p1f)*frac;
638         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
639         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
640         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
641
642         side = (t1 < 0);
643
644 // move up to the node
645         if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
646                 return false;
647
648 #ifdef PARANOID
649         if (SV_HullPointContents (hull, node->children[side], mid) == CONTENTS_SOLID)
650         {
651                 Con_Printf ("mid PointInHullSolid\n");
652                 return false;
653         }
654 #endif
655         
656         if (SV_HullPointContents (hull, node->children[side^1], mid) != CONTENTS_SOLID)
657 // go past the node
658                 return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
659         // mid would need to be duplicated during recursion...
660         /*
661         {
662                 p1f = midf;
663                 p1 = mid;
664                 num = node->children[side^1];
665                 goto loc0;
666         }
667         */
668
669         if (trace->allsolid)
670                 return false;           // never got out of the solid area
671         
672 //==================
673 // the other side of the node is solid, this is the impact point
674 //==================
675         if (!side)
676         {
677                 VectorCopy (plane->normal, trace->plane.normal);
678                 trace->plane.dist = plane->dist;
679         }
680         else
681         {
682                 // LordHavoc: unrolled vector operation because the compiler can't be sure vec3_origin is 0
683 //              VectorSubtract (vec3_origin, plane->normal, trace->plane.normal);
684                 trace->plane.normal[0] = -plane->normal[0];
685                 trace->plane.normal[1] = -plane->normal[1];
686                 trace->plane.normal[2] = -plane->normal[2];
687                 trace->plane.dist = -plane->dist;
688         }
689
690         while (SV_HullPointContents (hull, hull->firstclipnode, mid) == CONTENTS_SOLID)
691         { // shouldn't really happen, but does occasionally
692                 frac -= 0.1;
693                 if (frac < 0)
694                 {
695                         trace->fraction = midf;
696                         VectorCopy (mid, trace->endpos);
697                         Con_DPrintf ("backup past 0\n");
698                         return false;
699                 }
700                 midf = p1f + (p2f - p1f)*frac;
701                 for (i=0 ; i<3 ; i++)
702                         mid[i] = p1[i] + frac*(p2[i] - p1[i]);
703         }
704
705         trace->fraction = midf;
706         VectorCopy (mid, trace->endpos);
707
708         return false;
709 }
710
711 qboolean SV_TestLine (hull_t *hull, int num, vec3_t p1, vec3_t p2)
712 {
713         dclipnode_t     *node;
714         mplane_t        *plane;
715         float           t1, t2, frac;
716         vec3_t          mid;
717         int                     side;
718
719 loc0:
720 // check for empty
721         if (num < 0)
722                 return num != CONTENTS_SOLID;
723
724         if (num < hull->firstclipnode || num > hull->lastclipnode)
725                 Sys_Error ("SV_RecursiveHullCheck: bad node number");
726
727 //
728 // find the point distances
729 //
730         node = hull->clipnodes + num;
731         plane = hull->planes + node->planenum;
732
733         t1 = PlaneDiff(p1, plane);
734         t2 = PlaneDiff(p2, plane);
735         
736         if (t1 >= 0 && t2 >= 0)
737         {
738                 num = node->children[0];
739                 goto loc0;
740         }
741         if (t1 < 0 && t2 < 0)
742         {
743                 num = node->children[1];
744                 goto loc0;
745         }
746
747 // put the crosspoint DIST_EPSILON pixels on the near side
748         side = (t1 < 0);
749
750         if (side)
751                 frac = bound(0, (t1 + DIST_EPSILON)/(t1-t2), 1);
752         else
753                 frac = bound(0, (t1 - DIST_EPSILON)/(t1-t2), 1);
754                 
755         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
756         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
757         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
758
759         if (node->children[side] < 0)
760         {
761                 if (node->children[side] == CONTENTS_SOLID)
762                         return false;
763                 return SV_TestLine(hull, node->children[!side], mid, p2);
764 //              num = node->children[!side];
765 //              VectorCopy(mid, p1);
766 //              goto loc0;
767         }
768         else if (SV_TestLine(hull, node->children[side], p1, mid))
769         {
770                 return SV_TestLine(hull, node->children[!side], mid, p2);
771 //              num = node->children[!side];
772 //              VectorCopy(mid, p1);
773 //              goto loc0;
774         }
775         else
776                 return false;
777 }
778
779
780 /*
781 ==================
782 SV_ClipMoveToEntity
783
784 Handles selection or creation of a clipping hull, and offseting (and
785 eventually rotation) of the end points
786 ==================
787 */
788 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
789 {
790         trace_t         trace;
791         vec3_t          offset;
792         vec3_t          start_l, end_l;
793         hull_t          *hull;
794
795 // fill in a default trace
796         memset (&trace, 0, sizeof(trace_t));
797         trace.fraction = 1;
798         trace.allsolid = true;
799         VectorCopy (end, trace.endpos);
800
801 // get the clipping hull
802         hull = SV_HullForEntity (ent, mins, maxs, offset);
803
804         VectorSubtract (start, offset, start_l);
805         VectorSubtract (end, offset, end_l);
806
807 // LordHavoc: enabling rotating bmodels
808         // rotate start and end into the models frame of reference
809         if (ent->v.solid == SOLID_BSP && 
810         (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
811         {
812                 vec3_t  forward, right, up;
813                 vec3_t  temp;
814
815                 AngleVectors (ent->v.angles, forward, right, up);
816
817                 VectorCopy (start_l, temp);
818                 start_l[0] = DotProduct (temp, forward);
819                 start_l[1] = -DotProduct (temp, right);
820                 start_l[2] = DotProduct (temp, up);
821
822                 VectorCopy (end_l, temp);
823                 end_l[0] = DotProduct (temp, forward);
824                 end_l[1] = -DotProduct (temp, right);
825                 end_l[2] = DotProduct (temp, up);
826         }
827
828 // trace a line through the apropriate clipping hull
829         SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);
830
831 // LordHavoc: enabling rotating bmodels
832         // rotate endpos back to world frame of reference
833         if (ent->v.solid == SOLID_BSP && 
834         (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
835         {
836                 vec3_t  a;
837                 vec3_t  forward, right, up;
838                 vec3_t  temp;
839
840                 if (trace.fraction != 1)
841                 {
842                         VectorSubtract (vec3_origin, ent->v.angles, a);
843                         AngleVectors (a, forward, right, up);
844
845                         VectorCopy (trace.endpos, temp);
846                         trace.endpos[0] = DotProduct (temp, forward);
847                         trace.endpos[1] = -DotProduct (temp, right);
848                         trace.endpos[2] = DotProduct (temp, up);
849
850                         VectorCopy (trace.plane.normal, temp);
851                         trace.plane.normal[0] = DotProduct (temp, forward);
852                         trace.plane.normal[1] = -DotProduct (temp, right);
853                         trace.plane.normal[2] = DotProduct (temp, up);
854                 }
855         }
856
857 // fix trace up by the offset
858         if (trace.fraction != 1)
859                 VectorAdd (trace.endpos, offset, trace.endpos);
860
861 // did we clip the move?
862         if (trace.fraction < 1 || trace.startsolid  )
863                 trace.ent = ent;
864
865         return trace;
866 }
867
868 //===========================================================================
869
870 /*
871 ====================
872 SV_ClipToLinks
873
874 Mins and maxs enclose the entire area swept by the move
875 ====================
876 */
877 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
878 {
879         link_t          *l, *next;
880         edict_t         *touch;
881         trace_t         trace;
882
883 loc0:
884 // touch linked edicts
885         for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
886         {
887                 next = l->next;
888                 touch = EDICT_FROM_AREA(l);
889                 if (touch->v.solid == SOLID_NOT)
890                         continue;
891                 if (touch == clip->passedict)
892                         continue;
893                 if (touch->v.solid == SOLID_TRIGGER)
894                         Sys_Error ("Trigger in clipping list");
895
896                 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
897                         continue;
898
899                 if (clip->boxmins[0] > touch->v.absmax[0]
900                 || clip->boxmins[1] > touch->v.absmax[1]
901                 || clip->boxmins[2] > touch->v.absmax[2]
902                 || clip->boxmaxs[0] < touch->v.absmin[0]
903                 || clip->boxmaxs[1] < touch->v.absmin[1]
904                 || clip->boxmaxs[2] < touch->v.absmin[2] )
905                         continue;
906
907                 if (clip->passedict!=0 && clip->passedict->v.size[0] && !touch->v.size[0])
908                         continue;       // points never interact
909
910         // might intersect, so do an exact clip
911                 if (clip->trace.allsolid)
912                         return;
913                 if (clip->passedict)
914                 {
915                         if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
916                                 continue;       // don't clip against own missiles
917                         if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
918                                 continue;       // don't clip against owner
919                         // LordHavoc: corpse code
920                         if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
921                                 continue;
922                         if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
923                                 continue;
924                 }
925
926                 if ((int)touch->v.flags & FL_MONSTER)
927                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
928                 else
929                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
930                 if (trace.allsolid || trace.startsolid ||
931                 trace.fraction < clip->trace.fraction)
932                 {
933                         trace.ent = touch;
934                         if (clip->trace.startsolid)
935                         {
936                                 clip->trace = trace;
937                                 clip->trace.startsolid = true;
938                         }
939                         else
940                                 clip->trace = trace;
941                 }
942                 else if (trace.startsolid)
943                         clip->trace.startsolid = true;
944         }
945         
946 // recurse down both sides
947         if (node->axis == -1)
948                 return;
949
950         // LordHavoc: optimized recursion
951 //      if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
952 //      if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
953         if (clip->boxmaxs[node->axis] > node->dist)
954         {
955                 if (clip->boxmins[node->axis] < node->dist)
956                         SV_ClipToLinks(node->children[1], clip);
957                 node = node->children[0];
958                 goto loc0;
959         }
960         else if (clip->boxmins[node->axis] < node->dist)
961         {
962                 node = node->children[1];
963                 goto loc0;
964         }
965 }
966
967
968 /*
969 ==================
970 SV_MoveBounds
971 ==================
972 */
973 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
974 {
975 #if 0
976 // debug to test against everything
977 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
978 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
979 #else
980         int             i;
981         
982         for (i=0 ; i<3 ; i++)
983         {
984                 if (end[i] > start[i])
985                 {
986                         boxmins[i] = start[i] + mins[i] - 1;
987                         boxmaxs[i] = end[i] + maxs[i] + 1;
988                 }
989                 else
990                 {
991                         boxmins[i] = end[i] + mins[i] - 1;
992                         boxmaxs[i] = start[i] + maxs[i] + 1;
993                 }
994         }
995 #endif
996 }
997
998 /*
999 ==================
1000 SV_Move
1001 ==================
1002 */
1003 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
1004 {
1005         moveclip_t      clip;
1006         int                     i;
1007
1008         memset ( &clip, 0, sizeof ( moveclip_t ) );
1009
1010 // clip to world
1011         clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
1012
1013         clip.start = start;
1014         clip.end = end;
1015         clip.mins = mins;
1016         clip.maxs = maxs;
1017         clip.type = type;
1018         clip.passedict = passedict;
1019
1020         if (type == MOVE_MISSILE)
1021         {
1022                 for (i=0 ; i<3 ; i++)
1023                 {
1024                         clip.mins2[i] = -15;
1025                         clip.maxs2[i] = 15;
1026                 }
1027         }
1028         else
1029         {
1030                 VectorCopy (mins, clip.mins2);
1031                 VectorCopy (maxs, clip.maxs2);
1032         }
1033         
1034 // create the bounding box of the entire move
1035         SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
1036
1037 // clip to entities
1038         SV_ClipToLinks ( sv_areanodes, &clip );
1039
1040         return clip.trace;
1041 }