]> icculus.org git repositories - divverent/darkplaces.git/blob - world.c
forgot to put in extern qboolean hlbsp;
[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 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                 {
382                         Con_DPrintf("FindTouchedLeafs overflow\n");
383                         return;
384                 }
385
386                 leaf = (mleaf_t *)node;
387                 leafnum = leaf - sv.worldmodel->leafs - 1;
388
389                 ent->leafnums[ent->num_leafs] = leafnum;
390                 ent->num_leafs++;                       
391                 return;
392         }
393         
394 // NODE_MIXED
395
396         splitplane = node->plane;
397         sides = BOX_ON_PLANE_SIDE(ent->v.absmin, ent->v.absmax, splitplane);
398         
399 // recurse down the contacted sides
400         // LordHavoc: optimized recursion
401 //      if (sides & 1) SV_FindTouchedLeafs (ent, node->children[0]);
402 //      if (sides & 2) SV_FindTouchedLeafs (ent, node->children[1]);
403         switch (sides)
404         {
405         case 1:
406                 node = node->children[0];
407                 goto loc0;
408         case 2:
409                 node = node->children[1];
410                 goto loc0;
411         default: // 3
412                 if (node->children[0]->contents != CONTENTS_SOLID)
413                         SV_FindTouchedLeafs (ent, node->children[0]);
414                 node = node->children[1];
415                 goto loc0;
416         }
417 }
418
419 /*
420 ===============
421 SV_LinkEdict
422
423 ===============
424 */
425 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
426 {
427         areanode_t      *node;
428
429         if (ent->area.prev)
430                 SV_UnlinkEdict (ent);   // unlink from old position
431                 
432         if (ent == sv.edicts)
433                 return;         // don't add the world
434
435         if (ent->free)
436                 return;
437
438 // set the abs box
439
440 // LordHavoc: enabling rotating bmodels
441         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
442         {       // expand for rotation
443                 float           max, v;
444                 int                     i;
445
446                 max = DotProduct(ent->v.mins, ent->v.mins);
447                 v = DotProduct(ent->v.maxs, ent->v.maxs);
448                 if (max < v)
449                         max = v;
450                 max = sqrt(max);
451                 /*
452                 max = 0;
453                 for (i=0 ; i<3 ; i++)
454                 {
455                         v = fabs(ent->v.mins[i]);
456                         if (max < v)
457                                 max = v;
458                         v = fabs(ent->v.maxs[i]);
459                         if (max < v)
460                                 max = v;
461                 }
462                 */
463                 for (i=0 ; i<3 ; i++)
464                 {
465                         ent->v.absmin[i] = ent->v.origin[i] - max;
466                         ent->v.absmax[i] = ent->v.origin[i] + max;
467                 }
468         }
469         else
470         {
471                 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);  
472                 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
473         }
474
475 //
476 // to make items easier to pick up and allow them to be grabbed off
477 // of shelves, the abs sizes are expanded
478 //
479         if ((int)ent->v.flags & FL_ITEM)
480         {
481                 ent->v.absmin[0] -= 15;
482                 ent->v.absmin[1] -= 15;
483                 ent->v.absmax[0] += 15;
484                 ent->v.absmax[1] += 15;
485         }
486         else
487         {       // because movement is clipped an epsilon away from an actual edge,
488                 // we must fully check even when bounding boxes don't quite touch
489                 ent->v.absmin[0] -= 1;
490                 ent->v.absmin[1] -= 1;
491                 ent->v.absmin[2] -= 1;
492                 ent->v.absmax[0] += 1;
493                 ent->v.absmax[1] += 1;
494                 ent->v.absmax[2] += 1;
495         }
496
497 // link to PVS leafs
498         ent->num_leafs = 0;
499         if (ent->v.modelindex)
500                 SV_FindTouchedLeafs (ent, sv.worldmodel->nodes);
501
502         if (ent->v.solid == SOLID_NOT)
503                 return;
504
505 // find the first node that the ent's box crosses
506         node = sv_areanodes;
507         while (1)
508         {
509                 if (node->axis == -1)
510                         break;
511                 if (ent->v.absmin[node->axis] > node->dist)
512                         node = node->children[0];
513                 else if (ent->v.absmax[node->axis] < node->dist)
514                         node = node->children[1];
515                 else
516                         break;          // crosses the node
517         }
518
519 // link it in   
520
521         if (ent->v.solid == SOLID_TRIGGER)
522                 InsertLinkBefore (&ent->area, &node->trigger_edicts);
523         else
524                 InsertLinkBefore (&ent->area, &node->solid_edicts);
525         
526 // if touch_triggers, touch all entities at this node and descend for more
527         if (touch_triggers)
528                 SV_TouchLinks ( ent, sv_areanodes );
529 }
530
531
532
533 /*
534 ===============================================================================
535
536 POINT TESTING IN HULLS
537
538 ===============================================================================
539 */
540
541 /*
542 ==================
543 SV_HullPointContents
544
545 ==================
546 */
547 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
548 {
549         while (num >= 0)
550                 num = hull->clipnodes[num].children[(hull->planes[hull->clipnodes[num].planenum].type < 3 ? p[hull->planes[hull->clipnodes[num].planenum].type] : DotProduct (hull->planes[hull->clipnodes[num].planenum].normal, p)) < hull->planes[hull->clipnodes[num].planenum].dist];
551         
552         return num;
553 }
554
555 /*
556 ============
557 SV_TestEntityPosition
558
559 This could be a lot more efficient...
560 ============
561 */
562 edict_t *SV_TestEntityPosition (edict_t *ent)
563 {
564         trace_t trace;
565
566         trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, 0, ent);
567         
568         if (trace.startsolid)
569                 return sv.edicts;
570                 
571         return NULL;
572 }
573
574
575 /*
576 ===============================================================================
577
578 LINE TESTING IN HULLS
579
580 ===============================================================================
581 */
582
583 // 1/32 epsilon to keep floating point happy
584 #define DIST_EPSILON    (0.03125)
585
586 /*
587 ==================
588 SV_RecursiveHullCheck
589
590 ==================
591 */
592 /*
593 qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
594 {
595         dclipnode_t     *node;
596         mplane_t        *plane;
597         float           t1, t2;
598         float           frac;
599         int                     i;
600         vec3_t          mid;
601         int                     side;
602         float           midf;
603
604 loc0:
605 // check for empty
606         if (num < 0)
607         {
608                 if (num != CONTENTS_SOLID)
609                 {
610                         trace->allsolid = false;
611                         if (num == CONTENTS_EMPTY)
612                                 trace->inopen = true;
613                         else
614                                 trace->inwater = true;
615                 }
616                 else
617                         trace->startsolid = true;
618                 return true;            // empty
619         }
620
621         if (num < hull->firstclipnode || num > hull->lastclipnode)
622                 Sys_Error ("SV_RecursiveHullCheck: bad node number");
623
624 //
625 // find the point distances
626 //
627         node = hull->clipnodes + num;
628         plane = hull->planes + node->planenum;
629
630         t1 = PlaneDiff(p1, plane);
631         t2 = PlaneDiff(p2, plane);
632         
633 #if 1
634         if (t1 >= 0 && t2 >= 0)
635         // LordHavoc: optimized recursion
636 //              return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
637         {
638                 num = node->children[0];
639                 goto loc0;
640         }
641         if (t1 < 0 && t2 < 0)
642 //              return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
643         {
644                 num = node->children[1];
645                 goto loc0;
646         }
647 #else
648         if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
649                 return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
650         if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
651                 return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
652 #endif
653
654 // put the crosspoint DIST_EPSILON pixels on the near side
655         if (t1 < 0)
656                 frac = bound(0, (t1 + DIST_EPSILON)/(t1-t2), 1);
657         else
658                 frac = bound(0, (t1 - DIST_EPSILON)/(t1-t2), 1);
659                 
660         midf = p1f + (p2f - p1f)*frac;
661         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
662         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
663         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
664
665         side = (t1 < 0);
666
667 // move up to the node
668         if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
669                 return false;
670
671 #ifdef PARANOID
672         if (SV_HullPointContents (hull, node->children[side], mid) == CONTENTS_SOLID)
673         {
674                 Con_Printf ("mid PointInHullSolid\n");
675                 return false;
676         }
677 #endif
678
679         if (SV_HullPointContents (hull, node->children[side^1], mid) != CONTENTS_SOLID)
680 // go past the node
681                 return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
682         // mid would need to be duplicated during recursion...
683 */
684         /*
685         {
686                 p1f = midf;
687                 p1 = mid;
688                 num = node->children[side^1];
689                 goto loc0;
690         }
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                 VectorNegate (plane->normal, trace->plane.normal);
708                 trace->plane.dist = -plane->dist;
709         }
710
711         while (SV_HullPointContents (hull, hull->firstclipnode, mid) == CONTENTS_SOLID)
712         { // shouldn't really happen, but does occasionally
713                 frac -= 0.1;
714                 if (frac < 0)
715                 {
716                         trace->fraction = midf;
717                         VectorCopy (mid, trace->endpos);
718                         Con_DPrintf ("backup past 0\n");
719                         return false;
720                 }
721                 midf = p1f + (p2f - p1f)*frac;
722                 for (i=0 ; i<3 ; i++)
723                         mid[i] = p1[i] + frac*(p2[i] - p1[i]);
724         }
725
726         trace->fraction = midf;
727         VectorCopy (mid, trace->endpos);
728
729         return false;
730 }
731 */
732
733 // LordHavoc: backported from my optimizations to PM_RecursiveHullCheck in QuakeForge newtree (QW)
734 qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
735 {
736         dclipnode_t     *node;
737         mplane_t        *plane;
738         float           t1, t2;
739         float           frac;
740         int                     i;
741         vec3_t          mid;
742         int                     side;
743         float           midf;
744
745         // LordHavoc: a goto!  everyone flee in terror... :)
746 loc0:
747 // check for empty
748         if (num < 0)
749         {
750                 if (num != CONTENTS_SOLID)
751                 {
752                         trace->allsolid = false;
753                         if (num == CONTENTS_EMPTY)
754                                 trace->inopen = true;
755                         else
756                                 trace->inwater = true;
757                 }
758                 else
759                         trace->startsolid = true;
760                 return true;            // empty
761         }
762
763 // find the point distances
764         node = hull->clipnodes + num;
765         plane = hull->planes + node->planenum;
766
767         if (plane->type < 3)
768         {
769                 t1 = p1[plane->type] - plane->dist;
770                 t2 = p2[plane->type] - plane->dist;
771         }
772         else
773         {
774                 t1 = DotProduct (plane->normal, p1) - plane->dist;
775                 t2 = DotProduct (plane->normal, p2) - plane->dist;
776         }
777
778         // LordHavoc: recursion optimization
779         if (t1 >= 0 && t2 >= 0)
780         {
781                 num = node->children[0];
782                 goto loc0;
783         }
784         if (t1 < 0 && t2 < 0)
785         {
786                 num = node->children[1];
787                 goto loc0;
788         }
789
790 // put the crosspoint DIST_EPSILON pixels on the near side
791         side = (t1 < 0);
792         if (side)
793                 frac = bound(0, (t1 + DIST_EPSILON) / (t1 - t2), 1);
794         else
795                 frac = bound(0, (t1 - DIST_EPSILON) / (t1 - t2), 1);
796                 
797         midf = p1f + (p2f - p1f)*frac;
798         for (i=0 ; i<3 ; i++)
799                 mid[i] = p1[i] + frac*(p2[i] - p1[i]);
800
801 // move up to the node
802         if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
803                 return false;
804
805 #ifdef PARANOID
806         if (SV_HullPointContents (pm_hullmodel, mid, node->children[side]) == CONTENTS_SOLID)
807         {
808                 Con_Printf ("mid PointInHullSolid\n");
809                 return false;
810         }
811 #endif
812
813         // LordHavoc: warning to the clumsy, this recursion can not be optimized because mid would need to be duplicated on a stack
814         if (SV_HullPointContents (hull, node->children[side^1], mid) != CONTENTS_SOLID)
815 // go past the node
816                 return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
817         
818         if (trace->allsolid)
819                 return false;           // never got out of the solid area
820                 
821 //==================
822 // the other side of the node is solid, this is the impact point
823 //==================
824         if (!side)
825         {
826                 VectorCopy (plane->normal, trace->plane.normal);
827                 trace->plane.dist = plane->dist;
828         }
829         else
830         {
831                 VectorNegate (plane->normal, trace->plane.normal);
832                 trace->plane.dist = -plane->dist;
833         }
834
835         while (SV_HullPointContents (hull, hull->firstclipnode, mid) == CONTENTS_SOLID)
836         { // shouldn't really happen, but does occasionally
837                 frac -= 0.1;
838                 if (frac < 0)
839                 {
840                         trace->fraction = midf;
841                         VectorCopy (mid, trace->endpos);
842                         Con_DPrintf ("backup past 0\n");
843                         return false;
844                 }
845                 midf = p1f + (p2f - p1f)*frac;
846                 for (i=0 ; i<3 ; i++)
847                         mid[i] = p1[i] + frac*(p2[i] - p1[i]);
848         }
849
850         trace->fraction = midf;
851         VectorCopy (mid, trace->endpos);
852
853         return false;
854 }
855
856 qboolean SV_TestLine (hull_t *hull, int num, vec3_t p1, vec3_t p2)
857 {
858         dclipnode_t     *node;
859         mplane_t        *plane;
860         float           t1, t2, frac;
861         vec3_t          mid;
862         int                     side;
863
864 loc0:
865 // check for empty
866         if (num < 0)
867                 return num != CONTENTS_SOLID;
868
869         if (num < hull->firstclipnode || num > hull->lastclipnode)
870                 Sys_Error ("SV_RecursiveHullCheck: bad node number");
871
872 //
873 // find the point distances
874 //
875         node = hull->clipnodes + num;
876         plane = hull->planes + node->planenum;
877
878         t1 = PlaneDiff(p1, plane);
879         t2 = PlaneDiff(p2, plane);
880         
881         if (t1 >= 0 && t2 >= 0)
882         {
883                 num = node->children[0];
884                 goto loc0;
885         }
886         if (t1 < 0 && t2 < 0)
887         {
888                 num = node->children[1];
889                 goto loc0;
890         }
891
892 // put the crosspoint DIST_EPSILON pixels on the near side
893         side = (t1 < 0);
894
895         if (side)
896                 frac = bound(0, (t1 + DIST_EPSILON)/(t1-t2), 1);
897         else
898                 frac = bound(0, (t1 - DIST_EPSILON)/(t1-t2), 1);
899                 
900         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
901         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
902         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
903
904         if (node->children[side] < 0)
905         {
906                 if (node->children[side] == CONTENTS_SOLID)
907                         return false;
908                 return SV_TestLine(hull, node->children[!side], mid, p2);
909         }
910         else if (SV_TestLine(hull, node->children[side], p1, mid))
911                 return SV_TestLine(hull, node->children[!side], mid, p2);
912         else
913                 return false;
914 }
915
916
917 /*
918 ==================
919 SV_ClipMoveToEntity
920
921 Handles selection or creation of a clipping hull, and offseting (and
922 eventually rotation) of the end points
923 ==================
924 */
925 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
926 {
927         trace_t         trace;
928         vec3_t          offset;
929         vec3_t          start_l, end_l;
930         hull_t          *hull;
931
932 // fill in a default trace
933         memset (&trace, 0, sizeof(trace_t));
934         trace.fraction = 1;
935         trace.allsolid = true;
936         VectorCopy (end, trace.endpos);
937
938 // get the clipping hull
939         hull = SV_HullForEntity (ent, mins, maxs, offset);
940
941         VectorSubtract (start, offset, start_l);
942         VectorSubtract (end, offset, end_l);
943
944 // LordHavoc: enabling rotating bmodels
945         // rotate start and end into the models frame of reference
946         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
947         {
948                 vec3_t  forward, right, up;
949                 vec3_t  temp;
950
951                 AngleVectors (ent->v.angles, forward, right, up);
952
953                 VectorCopy (start_l, temp);
954                 start_l[0] = DotProduct (temp, forward);
955                 start_l[1] = -DotProduct (temp, right);
956                 start_l[2] = DotProduct (temp, up);
957
958                 VectorCopy (end_l, temp);
959                 end_l[0] = DotProduct (temp, forward);
960                 end_l[1] = -DotProduct (temp, right);
961                 end_l[2] = DotProduct (temp, up);
962         }
963
964 // trace a line through the apropriate clipping hull
965         SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);
966
967 // LordHavoc: enabling rotating bmodels
968         // rotate endpos back to world frame of reference
969         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
970         {
971                 vec3_t  a;
972                 vec3_t  forward, right, up;
973                 vec3_t  temp;
974
975                 if (trace.fraction != 1)
976                 {
977                         VectorNegate (ent->v.angles, a);
978                         AngleVectors (a, forward, right, up);
979
980                         VectorCopy (trace.endpos, temp);
981                         trace.endpos[0] = DotProduct (temp, forward);
982                         trace.endpos[1] = -DotProduct (temp, right);
983                         trace.endpos[2] = DotProduct (temp, up);
984
985                         VectorCopy (trace.plane.normal, temp);
986                         trace.plane.normal[0] = DotProduct (temp, forward);
987                         trace.plane.normal[1] = -DotProduct (temp, right);
988                         trace.plane.normal[2] = DotProduct (temp, up);
989                 }
990         }
991
992 // fix trace up by the offset
993         if (trace.fraction != 1)
994                 VectorAdd (trace.endpos, offset, trace.endpos);
995
996 // did we clip the move?
997         if (trace.fraction < 1 || trace.startsolid  )
998                 trace.ent = ent;
999
1000         return trace;
1001 }
1002
1003 //===========================================================================
1004
1005 /*
1006 ====================
1007 SV_ClipToLinks
1008
1009 Mins and maxs enclose the entire area swept by the move
1010 ====================
1011 */
1012 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
1013 {
1014         link_t          *l, *next;
1015         edict_t         *touch;
1016         trace_t         trace;
1017
1018 loc0:
1019 // touch linked edicts
1020         for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
1021         {
1022                 next = l->next;
1023                 touch = EDICT_FROM_AREA(l);
1024                 if (touch->v.solid == SOLID_NOT)
1025                         continue;
1026                 if (touch == clip->passedict)
1027                         continue;
1028                 if (touch->v.solid == SOLID_TRIGGER)
1029                         Sys_Error ("Trigger in clipping list");
1030
1031                 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
1032                         continue;
1033
1034                 if (clip->boxmins[0] > touch->v.absmax[0]
1035                 || clip->boxmins[1] > touch->v.absmax[1]
1036                 || clip->boxmins[2] > touch->v.absmax[2]
1037                 || clip->boxmaxs[0] < touch->v.absmin[0]
1038                 || clip->boxmaxs[1] < touch->v.absmin[1]
1039                 || clip->boxmaxs[2] < touch->v.absmin[2] )
1040                         continue;
1041
1042                 if (clip->passedict!=0 && clip->passedict->v.size[0] && !touch->v.size[0])
1043                         continue;       // points never interact
1044
1045         // might intersect, so do an exact clip
1046                 if (clip->trace.allsolid)
1047                         return;
1048                 if (clip->passedict)
1049                 {
1050                         if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
1051                                 continue;       // don't clip against own missiles
1052                         if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
1053                                 continue;       // don't clip against owner
1054                         // LordHavoc: corpse code
1055                         if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
1056                                 continue;
1057                         if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
1058                                 continue;
1059                 }
1060
1061                 if ((int)touch->v.flags & FL_MONSTER)
1062                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
1063                 else
1064                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
1065                 if (trace.allsolid || trace.startsolid || trace.fraction < clip->trace.fraction)
1066                 {
1067                         trace.ent = touch;
1068                         if (clip->trace.startsolid)
1069                         {
1070                                 clip->trace = trace;
1071                                 clip->trace.startsolid = true;
1072                         }
1073                         else
1074                                 clip->trace = trace;
1075                 }
1076                 else if (trace.startsolid)
1077                         clip->trace.startsolid = true;
1078         }
1079         
1080 // recurse down both sides
1081         if (node->axis == -1)
1082                 return;
1083
1084         // LordHavoc: optimized recursion
1085 //      if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
1086 //      if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
1087         if (clip->boxmaxs[node->axis] > node->dist)
1088         {
1089                 if (clip->boxmins[node->axis] < node->dist)
1090                         SV_ClipToLinks(node->children[1], clip);
1091                 node = node->children[0];
1092                 goto loc0;
1093         }
1094         else if (clip->boxmins[node->axis] < node->dist)
1095         {
1096                 node = node->children[1];
1097                 goto loc0;
1098         }
1099 }
1100
1101
1102 /*
1103 ==================
1104 SV_MoveBounds
1105 ==================
1106 */
1107 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
1108 {
1109 #if 0
1110 // debug to test against everything
1111 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
1112 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
1113 #else
1114         int             i;
1115         
1116         for (i=0 ; i<3 ; i++)
1117         {
1118                 if (end[i] > start[i])
1119                 {
1120                         boxmins[i] = start[i] + mins[i] - 1;
1121                         boxmaxs[i] = end[i] + maxs[i] + 1;
1122                 }
1123                 else
1124                 {
1125                         boxmins[i] = end[i] + mins[i] - 1;
1126                         boxmaxs[i] = start[i] + maxs[i] + 1;
1127                 }
1128         }
1129 #endif
1130 }
1131
1132 /*
1133 ==================
1134 SV_Move
1135 ==================
1136 */
1137 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
1138 {
1139         moveclip_t      clip;
1140         int                     i;
1141
1142         memset ( &clip, 0, sizeof ( moveclip_t ) );
1143
1144 // clip to world
1145         clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
1146
1147         clip.start = start;
1148         clip.end = end;
1149         clip.mins = mins;
1150         clip.maxs = maxs;
1151         clip.type = type;
1152         clip.passedict = passedict;
1153
1154         if (type == MOVE_MISSILE)
1155         {
1156                 for (i=0 ; i<3 ; i++)
1157                 {
1158                         clip.mins2[i] = -15;
1159                         clip.maxs2[i] = 15;
1160                 }
1161         }
1162         else
1163         {
1164                 VectorCopy (mins, clip.mins2);
1165                 VectorCopy (maxs, clip.maxs2);
1166         }
1167         
1168 // create the bounding box of the entire move
1169         SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
1170
1171 // clip to entities
1172         SV_ClipToLinks ( sv_areanodes, &clip );
1173
1174         return clip.trace;
1175 }