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