]> icculus.org git repositories - divverent/darkplaces.git/blob - world.c
Major speedup to model loading, using lightnormalindex table now.
[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 #if     !id386
514
515 /*
516 ==================
517 SV_HullPointContents
518
519 ==================
520 */
521 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
522 {
523         dclipnode_t     *node;
524         mplane_t        *plane;
525
526         while (num >= 0)
527         {
528                 if (num < hull->firstclipnode || num > hull->lastclipnode)
529                         Sys_Error ("SV_HullPointContents: bad node number");
530         
531                 node = hull->clipnodes + num;
532                 plane = hull->planes + node->planenum;
533                 
534 // LordHavoc: optimized this slightly (probably no actual impact due to compiler optimization)
535                 if (plane->type < 3)
536                         num = node->children[p[plane->type] < plane->dist];
537                 else
538                         num = node->children[DotProduct (plane->normal, p) < plane->dist];
539         }
540         
541         return num;
542 }
543
544 #endif  // !id386
545
546
547 /*
548 ============
549 SV_TestEntityPosition
550
551 This could be a lot more efficient...
552 ============
553 */
554 edict_t *SV_TestEntityPosition (edict_t *ent)
555 {
556         trace_t trace;
557
558         trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, 0, ent);
559         
560         if (trace.startsolid)
561                 return sv.edicts;
562                 
563         return NULL;
564 }
565
566
567 /*
568 ===============================================================================
569
570 LINE TESTING IN HULLS
571
572 ===============================================================================
573 */
574
575 // 1/32 epsilon to keep floating point happy
576 #define DIST_EPSILON    (0.03125)
577
578 /*
579 ==================
580 SV_RecursiveHullCheck
581
582 ==================
583 */
584 qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
585 {
586         dclipnode_t     *node;
587         mplane_t        *plane;
588         float           t1, t2;
589         float           frac;
590         int                     i;
591         vec3_t          mid;
592         int                     side;
593         float           midf;
594
595 loc0:
596 // check for empty
597         if (num < 0)
598         {
599                 if (num != CONTENTS_SOLID)
600                 {
601                         trace->allsolid = false;
602                         if (num == CONTENTS_EMPTY)
603                                 trace->inopen = true;
604                         else
605                                 trace->inwater = true;
606                 }
607                 else
608                         trace->startsolid = true;
609                 return true;            // empty
610         }
611
612         if (num < hull->firstclipnode || num > hull->lastclipnode)
613                 Sys_Error ("SV_RecursiveHullCheck: bad node number");
614
615 //
616 // find the point distances
617 //
618         node = hull->clipnodes + num;
619         plane = hull->planes + node->planenum;
620
621         if (plane->type < 3)
622         {
623                 t1 = p1[plane->type] - plane->dist;
624                 t2 = p2[plane->type] - plane->dist;
625         }
626         else
627         {
628                 t1 = DotProduct (plane->normal, p1) - plane->dist;
629                 t2 = DotProduct (plane->normal, p2) - plane->dist;
630         }
631         
632 #if 1
633         if (t1 >= 0 && t2 >= 0)
634         // LordHavoc: optimized recursion
635 //              return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
636         {
637                 num = node->children[0];
638                 goto loc0;
639         }
640         if (t1 < 0 && t2 < 0)
641 //              return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
642         {
643                 num = node->children[1];
644                 goto loc0;
645         }
646 #else
647         if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
648                 return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
649         if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
650                 return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
651 #endif
652
653 // put the crosspoint DIST_EPSILON pixels on the near side
654         if (t1 < 0)
655                 frac = (t1 + DIST_EPSILON)/(t1-t2);
656         else
657                 frac = (t1 - DIST_EPSILON)/(t1-t2);
658         if (frac < 0)
659                 frac = 0;
660         if (frac > 1)
661                 frac = 1;
662                 
663         midf = p1f + (p2f - p1f)*frac;
664         for (i=0 ; i<3 ; i++)
665                 mid[i] = p1[i] + frac*(p2[i] - p1[i]);
666
667         side = (t1 < 0);
668
669 // move up to the node
670         if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
671                 return false;
672
673 #ifdef PARANOID
674         if (SV_HullPointContents (hull, node->children[side], mid) == CONTENTS_SOLID)
675         {
676                 Con_Printf ("mid PointInHullSolid\n");
677                 return false;
678         }
679 #endif
680         
681         if (SV_HullPointContents (hull, node->children[side^1], mid) != CONTENTS_SOLID)
682 // go past the node
683                 return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
684         // mid would need to be duplicated during recursion...
685         /*
686         {
687                 p1f = midf;
688                 p1 = mid;
689                 num = node->children[side^1];
690                 goto loc0;
691         }
692         */
693
694         if (trace->allsolid)
695                 return false;           // never got out of the solid area
696         
697 //==================
698 // the other side of the node is solid, this is the impact point
699 //==================
700         if (!side)
701         {
702                 VectorCopy (plane->normal, trace->plane.normal);
703                 trace->plane.dist = plane->dist;
704         }
705         else
706         {
707                 // LordHavoc: unrolled vector operation because the compiler can't be sure vec3_origin is 0
708 //              VectorSubtract (vec3_origin, plane->normal, trace->plane.normal);
709                 trace->plane.normal[0] = -plane->normal[0];
710                 trace->plane.normal[1] = -plane->normal[1];
711                 trace->plane.normal[2] = -plane->normal[2];
712                 trace->plane.dist = -plane->dist;
713         }
714
715         while (SV_HullPointContents (hull, hull->firstclipnode, mid) == CONTENTS_SOLID)
716         { // shouldn't really happen, but does occasionally
717                 frac -= 0.1;
718                 if (frac < 0)
719                 {
720                         trace->fraction = midf;
721                         VectorCopy (mid, trace->endpos);
722                         Con_DPrintf ("backup past 0\n");
723                         return false;
724                 }
725                 midf = p1f + (p2f - p1f)*frac;
726                 for (i=0 ; i<3 ; i++)
727                         mid[i] = p1[i] + frac*(p2[i] - p1[i]);
728         }
729
730         trace->fraction = midf;
731         VectorCopy (mid, trace->endpos);
732
733         return false;
734 }
735
736
737 /*
738 ==================
739 SV_ClipMoveToEntity
740
741 Handles selection or creation of a clipping hull, and offseting (and
742 eventually rotation) of the end points
743 ==================
744 */
745 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
746 {
747         trace_t         trace;
748         vec3_t          offset;
749         vec3_t          start_l, end_l;
750         hull_t          *hull;
751
752 // fill in a default trace
753         memset (&trace, 0, sizeof(trace_t));
754         trace.fraction = 1;
755         trace.allsolid = true;
756         VectorCopy (end, trace.endpos);
757
758 // get the clipping hull
759         hull = SV_HullForEntity (ent, mins, maxs, offset);
760
761         VectorSubtract (start, offset, start_l);
762         VectorSubtract (end, offset, end_l);
763
764 // LordHavoc: enabling rotating bmodels
765         // rotate start and end into the models frame of reference
766         if (ent->v.solid == SOLID_BSP && 
767         (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
768         {
769                 vec3_t  forward, right, up;
770                 vec3_t  temp;
771
772                 AngleVectors (ent->v.angles, forward, right, up);
773
774                 VectorCopy (start_l, temp);
775                 start_l[0] = DotProduct (temp, forward);
776                 start_l[1] = -DotProduct (temp, right);
777                 start_l[2] = DotProduct (temp, up);
778
779                 VectorCopy (end_l, temp);
780                 end_l[0] = DotProduct (temp, forward);
781                 end_l[1] = -DotProduct (temp, right);
782                 end_l[2] = DotProduct (temp, up);
783         }
784
785 // trace a line through the apropriate clipping hull
786         SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);
787
788 // LordHavoc: enabling rotating bmodels
789         // rotate endpos back to world frame of reference
790         if (ent->v.solid == SOLID_BSP && 
791         (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
792         {
793                 vec3_t  a;
794                 vec3_t  forward, right, up;
795                 vec3_t  temp;
796
797                 if (trace.fraction != 1)
798                 {
799                         VectorSubtract (vec3_origin, ent->v.angles, a);
800                         AngleVectors (a, forward, right, up);
801
802                         VectorCopy (trace.endpos, temp);
803                         trace.endpos[0] = DotProduct (temp, forward);
804                         trace.endpos[1] = -DotProduct (temp, right);
805                         trace.endpos[2] = DotProduct (temp, up);
806
807                         VectorCopy (trace.plane.normal, temp);
808                         trace.plane.normal[0] = DotProduct (temp, forward);
809                         trace.plane.normal[1] = -DotProduct (temp, right);
810                         trace.plane.normal[2] = DotProduct (temp, up);
811                 }
812         }
813
814 // fix trace up by the offset
815         if (trace.fraction != 1)
816                 VectorAdd (trace.endpos, offset, trace.endpos);
817
818 // did we clip the move?
819         if (trace.fraction < 1 || trace.startsolid  )
820                 trace.ent = ent;
821
822         return trace;
823 }
824
825 //===========================================================================
826
827 /*
828 ====================
829 SV_ClipToLinks
830
831 Mins and maxs enclose the entire area swept by the move
832 ====================
833 */
834 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
835 {
836         link_t          *l, *next;
837         edict_t         *touch;
838         trace_t         trace;
839
840 loc0:
841 // touch linked edicts
842         for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
843         {
844                 next = l->next;
845                 touch = EDICT_FROM_AREA(l);
846                 if (touch->v.solid == SOLID_NOT)
847                         continue;
848                 if (touch == clip->passedict)
849                         continue;
850                 if (touch->v.solid == SOLID_TRIGGER)
851                         Sys_Error ("Trigger in clipping list");
852
853                 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
854                         continue;
855
856                 if (clip->boxmins[0] > touch->v.absmax[0]
857                 || clip->boxmins[1] > touch->v.absmax[1]
858                 || clip->boxmins[2] > touch->v.absmax[2]
859                 || clip->boxmaxs[0] < touch->v.absmin[0]
860                 || clip->boxmaxs[1] < touch->v.absmin[1]
861                 || clip->boxmaxs[2] < touch->v.absmin[2] )
862                         continue;
863
864                 if (clip->passedict!=0 && clip->passedict->v.size[0] && !touch->v.size[0])
865                         continue;       // points never interact
866
867         // might intersect, so do an exact clip
868                 if (clip->trace.allsolid)
869                         return;
870                 if (clip->passedict)
871                 {
872                         if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
873                                 continue;       // don't clip against own missiles
874                         if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
875                                 continue;       // don't clip against owner
876                         // LordHavoc: corpse code
877                         if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
878                                 continue;
879                         if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
880                                 continue;
881                 }
882
883                 if ((int)touch->v.flags & FL_MONSTER)
884                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
885                 else
886                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
887                 if (trace.allsolid || trace.startsolid ||
888                 trace.fraction < clip->trace.fraction)
889                 {
890                         trace.ent = touch;
891                         if (clip->trace.startsolid)
892                         {
893                                 clip->trace = trace;
894                                 clip->trace.startsolid = true;
895                         }
896                         else
897                                 clip->trace = trace;
898                 }
899                 else if (trace.startsolid)
900                         clip->trace.startsolid = true;
901         }
902         
903 // recurse down both sides
904         if (node->axis == -1)
905                 return;
906
907         // LordHavoc: optimized recursion
908 //      if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
909 //      if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
910         if (clip->boxmaxs[node->axis] > node->dist)
911         {
912                 if (clip->boxmins[node->axis] < node->dist)
913                         SV_ClipToLinks(node->children[1], clip);
914                 node = node->children[0];
915                 goto loc0;
916         }
917         else if (clip->boxmins[node->axis] < node->dist)
918         {
919                 node = node->children[1];
920                 goto loc0;
921         }
922 }
923
924
925 /*
926 ==================
927 SV_MoveBounds
928 ==================
929 */
930 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
931 {
932 #if 0
933 // debug to test against everything
934 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
935 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
936 #else
937         int             i;
938         
939         for (i=0 ; i<3 ; i++)
940         {
941                 if (end[i] > start[i])
942                 {
943                         boxmins[i] = start[i] + mins[i] - 1;
944                         boxmaxs[i] = end[i] + maxs[i] + 1;
945                 }
946                 else
947                 {
948                         boxmins[i] = end[i] + mins[i] - 1;
949                         boxmaxs[i] = start[i] + maxs[i] + 1;
950                 }
951         }
952 #endif
953 }
954
955 /*
956 ==================
957 SV_Move
958 ==================
959 */
960 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
961 {
962         moveclip_t      clip;
963         int                     i;
964
965         memset ( &clip, 0, sizeof ( moveclip_t ) );
966
967 // clip to world
968         clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
969
970         clip.start = start;
971         clip.end = end;
972         clip.mins = mins;
973         clip.maxs = maxs;
974         clip.type = type;
975         clip.passedict = passedict;
976
977         if (type == MOVE_MISSILE)
978         {
979                 for (i=0 ; i<3 ; i++)
980                 {
981                         clip.mins2[i] = -15;
982                         clip.maxs2[i] = 15;
983                 }
984         }
985         else
986         {
987                 VectorCopy (mins, clip.mins2);
988                 VectorCopy (maxs, clip.maxs2);
989         }
990         
991 // create the bounding box of the entire move
992         SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
993
994 // clip to entities
995         SV_ClipToLinks ( sv_areanodes, &clip );
996
997         return clip.trace;
998 }