rewrote RecursiveHullCheck, no longer gets stuck on angle changes, and is generally...
[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 link_s
34 {
35         struct link_s   *prev, *next;
36 } link_t;
37 */
38
39
40 void ClearLink (link_t *l);
41 void RemoveLink (link_t *l);
42 void InsertLinkBefore (link_t *l, link_t *before);
43 void InsertLinkAfter (link_t *l, link_t *after);
44
45 // (type *)STRUCT_FROM_LINK(link_t *link, type, member)
46 // ent = STRUCT_FROM_LINK(link,entity_t,order)
47 // FIXME: remove this mess!
48 //#define       STRUCT_FROM_LINK(l,t,m) ((t *)((byte *)l - (int)&(((t *)0)->m)))
49
50 #define EDICT_FROM_AREA(l) ((edict_t *)((byte *)l - (int)&(((edict_t *)0)->area)))
51
52 //============================================================================
53
54 // ClearLink is used for new headnodes
55 void ClearLink (link_t *l)
56 {
57         l->prev = l->next = l;
58 }
59
60 void RemoveLink (link_t *l)
61 {
62         l->next->prev = l->prev;
63         l->prev->next = l->next;
64 }
65
66 void InsertLinkBefore (link_t *l, link_t *before)
67 {
68         l->next = before;
69         l->prev = before->prev;
70         l->prev->next = l;
71         l->next->prev = l;
72 }
73 void InsertLinkAfter (link_t *l, link_t *after)
74 {
75         l->next = after->next;
76         l->prev = after;
77         l->prev->next = l;
78         l->next->prev = l;
79 }
80
81
82 typedef struct
83 {
84         vec3_t          boxmins, boxmaxs;// enclose the test object along entire move
85         float           *mins, *maxs;   // size of the moving object
86         vec3_t          mins2, maxs2;   // size when clipping against mosnters
87         float           *start, *end;
88         trace_t         trace;
89         int                     type;
90         edict_t         *passedict;
91 } moveclip_t;
92
93
94 /*
95 ===============================================================================
96
97 HULL BOXES
98
99 ===============================================================================
100 */
101
102
103 static  hull_t          box_hull;
104 static  dclipnode_t     box_clipnodes[6];
105 static  mplane_t        box_planes[6];
106
107 /*
108 ===================
109 SV_InitBoxHull
110
111 Set up the planes and clipnodes so that the six floats of a bounding box
112 can just be stored out and get a proper hull_t structure.
113 ===================
114 */
115 void SV_InitBoxHull (void)
116 {
117         int             i;
118         int             side;
119
120         box_hull.clipnodes = box_clipnodes;
121         box_hull.planes = box_planes;
122         box_hull.firstclipnode = 0;
123         box_hull.lastclipnode = 5;
124
125         for (i=0 ; i<6 ; i++)
126         {
127                 box_clipnodes[i].planenum = i;
128                 
129                 side = i&1;
130                 
131                 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
132                 if (i != 5)
133                         box_clipnodes[i].children[side^1] = i + 1;
134                 else
135                         box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
136                 
137                 box_planes[i].type = i>>1;
138                 box_planes[i].normal[i>>1] = 1;
139         }
140         
141 }
142
143
144 /*
145 ===================
146 SV_HullForBox
147
148 To keep everything totally uniform, bounding boxes are turned into small
149 BSP trees instead of being compared directly.
150 ===================
151 */
152 hull_t  *SV_HullForBox (vec3_t mins, vec3_t maxs)
153 {
154         box_planes[0].dist = maxs[0];
155         box_planes[1].dist = mins[0];
156         box_planes[2].dist = maxs[1];
157         box_planes[3].dist = mins[1];
158         box_planes[4].dist = maxs[2];
159         box_planes[5].dist = mins[2];
160
161         return &box_hull;
162 }
163
164
165
166 /*
167 ================
168 SV_HullForEntity
169
170 Returns a hull that can be used for testing or clipping an object of mins/maxs
171 size.
172 Offset is filled in to contain the adjustment that must be added to the
173 testing object's origin to get a point to use with the returned hull.
174 ================
175 */
176 hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
177 {
178         model_t         *model;
179         vec3_t          size;
180         vec3_t          hullmins, hullmaxs;
181         hull_t          *hull;
182
183 // decide which clipping hull to use, based on the size
184         if (ent->v.solid == SOLID_BSP)
185         {       // explicit hulls in the BSP model
186                 if (ent->v.movetype != MOVETYPE_PUSH)
187                         Host_Error ("SOLID_BSP without MOVETYPE_PUSH");
188
189                 model = sv.models[ (int)ent->v.modelindex ];
190
191                 // LordHavoc: fixed SOLID_BSP error message
192                 if (!model || model->type != mod_brush)
193                 {
194                         Con_Printf ("SOLID_BSP with a non bsp model, entity dump:\n");
195                         ED_Print (ent);
196                         Host_Error ("SOLID_BSP with a non bsp model\n");
197                 }
198
199                 VectorSubtract (maxs, mins, size);
200                 // LordHavoc: FIXME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
201                 if (hlbsp)
202                 {
203                         if (size[0] < 3)
204                                 hull = &model->hulls[0]; // 0x0x0
205                         else if (size[0] <= 32)
206                         {
207                                 if (size[2] < 54) // pick the nearest of 36 or 72
208                                         hull = &model->hulls[3]; // 32x32x36
209                                 else
210                                         hull = &model->hulls[1]; // 32x32x72
211                         }
212                         else
213                                 hull = &model->hulls[2]; // 64x64x64
214                 }
215                 else
216                 {
217                         if (size[0] < 3)
218                                 hull = &model->hulls[0]; // 0x0x0
219                         else if (size[0] <= 32)
220                                 hull = &model->hulls[1]; // 32x32x56
221                         else
222                                 hull = &model->hulls[2]; // 64x64x88
223                 }
224
225 // calculate an offset value to center the origin
226                 VectorSubtract (hull->clip_mins, mins, offset);
227                 VectorAdd (offset, ent->v.origin, offset);
228         }
229         else
230         {       // create a temp hull from bounding box sizes
231
232                 VectorSubtract (ent->v.mins, maxs, hullmins);
233                 VectorSubtract (ent->v.maxs, mins, hullmaxs);
234                 hull = SV_HullForBox (hullmins, hullmaxs);
235
236                 VectorCopy (ent->v.origin, offset);
237         }
238
239
240         return hull;
241 }
242
243 /*
244 ===============================================================================
245
246 ENTITY AREA CHECKING
247
248 ===============================================================================
249 */
250
251 typedef struct areanode_s
252 {
253         int             axis;           // -1 = leaf node
254         float   dist;
255         struct areanode_s       *children[2];
256         link_t  trigger_edicts;
257         link_t  solid_edicts;
258 } areanode_t;
259
260 #define AREA_DEPTH      4
261 #define AREA_NODES      32
262
263 static  areanode_t      sv_areanodes[AREA_NODES];
264 static  int                     sv_numareanodes;
265
266 /*
267 ===============
268 SV_CreateAreaNode
269
270 ===============
271 */
272 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
273 {
274         areanode_t      *anode;
275         vec3_t          size;
276         vec3_t          mins1, maxs1, mins2, maxs2;
277
278         anode = &sv_areanodes[sv_numareanodes];
279         sv_numareanodes++;
280
281         ClearLink (&anode->trigger_edicts);
282         ClearLink (&anode->solid_edicts);
283
284         if (depth == AREA_DEPTH)
285         {
286                 anode->axis = -1;
287                 anode->children[0] = anode->children[1] = NULL;
288                 return anode;
289         }
290
291         VectorSubtract (maxs, mins, size);
292         if (size[0] > size[1])
293                 anode->axis = 0;
294         else
295                 anode->axis = 1;
296
297         anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
298         VectorCopy (mins, mins1);
299         VectorCopy (mins, mins2);
300         VectorCopy (maxs, maxs1);
301         VectorCopy (maxs, maxs2);
302
303         maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
304
305         anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
306         anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
307
308         return anode;
309 }
310
311 /*
312 ===============
313 SV_ClearWorld
314
315 ===============
316 */
317 void SV_ClearWorld (void)
318 {
319         SV_InitBoxHull ();
320
321         memset (sv_areanodes, 0, sizeof(sv_areanodes));
322         sv_numareanodes = 0;
323         SV_CreateAreaNode (0, sv.worldmodel->normalmins, sv.worldmodel->normalmaxs);
324 }
325
326
327 /*
328 ===============
329 SV_UnlinkEdict
330
331 ===============
332 */
333 void SV_UnlinkEdict (edict_t *ent)
334 {
335         if (!ent->area.prev)
336                 return;         // not linked in anywhere
337         RemoveLink (&ent->area);
338         ent->area.prev = ent->area.next = NULL;
339 }
340
341
342 /*
343 ====================
344 SV_TouchLinks
345 ====================
346 */
347 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
348 {
349         link_t          *l, *next;
350         edict_t         *touch;
351         int                     old_self, old_other;
352
353 loc0:
354 // touch linked edicts
355         for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
356         {
357                 next = l->next;
358                 touch = EDICT_FROM_AREA(l);
359                 if (touch == ent)
360                         continue;
361                 if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
362                         continue;
363                 if (ent->v.absmin[0] > touch->v.absmax[0]
364                  || ent->v.absmin[1] > touch->v.absmax[1]
365                  || ent->v.absmin[2] > touch->v.absmax[2]
366                  || ent->v.absmax[0] < touch->v.absmin[0]
367                  || ent->v.absmax[1] < touch->v.absmin[1]
368                  || ent->v.absmax[2] < touch->v.absmin[2])
369                         continue;
370                 old_self = pr_global_struct->self;
371                 old_other = pr_global_struct->other;
372
373                 pr_global_struct->self = EDICT_TO_PROG(touch);
374                 pr_global_struct->other = EDICT_TO_PROG(ent);
375                 pr_global_struct->time = sv.time;
376                 PR_ExecuteProgram (touch->v.touch, "");
377
378                 pr_global_struct->self = old_self;
379                 pr_global_struct->other = old_other;
380         }
381
382 // recurse down both sides
383         if (node->axis == -1)
384                 return;
385
386         // LordHavoc: optimized recursion
387 //      if (ent->v.absmax[node->axis] > node->dist) SV_TouchLinks (ent, node->children[0]);
388 //      if (ent->v.absmin[node->axis] < node->dist) SV_TouchLinks (ent, node->children[1]);
389         if (ent->v.absmax[node->axis] > node->dist)
390         {
391                 if (ent->v.absmin[node->axis] < node->dist)
392                         SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
393                 node = node->children[0];
394                 goto loc0;
395         }
396         else
397         {
398                 if (ent->v.absmin[node->axis] < node->dist)
399                 {
400                         node = node->children[1];
401                         goto loc0;
402                 }
403         }
404 }
405
406
407 /*
408 ===============
409 SV_LinkEdict
410
411 ===============
412 */
413 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
414 {
415         areanode_t      *node;
416
417         if (ent->area.prev)
418                 SV_UnlinkEdict (ent);   // unlink from old position
419                 
420         if (ent == sv.edicts)
421                 return;         // don't add the world
422
423         if (ent->free)
424                 return;
425
426 // set the abs box
427
428 // LordHavoc: enabling rotating bmodels
429         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
430         {
431                 // expand for rotation
432                 float           max, v;
433                 int                     i;
434
435                 max = DotProduct(ent->v.mins, ent->v.mins);
436                 v = DotProduct(ent->v.maxs, ent->v.maxs);
437                 if (max < v)
438                         max = v;
439                 max = sqrt(max);
440                 /*
441                 max = 0;
442                 for (i=0 ; i<3 ; i++)
443                 {
444                         v = fabs(ent->v.mins[i]);
445                         if (max < v)
446                                 max = v;
447                         v = fabs(ent->v.maxs[i]);
448                         if (max < v)
449                                 max = v;
450                 }
451                 */
452                 for (i=0 ; i<3 ; i++)
453                 {
454                         ent->v.absmin[i] = ent->v.origin[i] - max;
455                         ent->v.absmax[i] = ent->v.origin[i] + max;
456                 }
457         }
458         else
459         {
460                 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
461                 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
462         }
463
464 //
465 // to make items easier to pick up and allow them to be grabbed off
466 // of shelves, the abs sizes are expanded
467 //
468         if ((int)ent->v.flags & FL_ITEM)
469         {
470                 ent->v.absmin[0] -= 15;
471                 ent->v.absmin[1] -= 15;
472                 ent->v.absmax[0] += 15;
473                 ent->v.absmax[1] += 15;
474         }
475         else
476         {
477                 // because movement is clipped an epsilon away from an actual edge,
478                 // we must fully check even when bounding boxes don't quite touch
479                 ent->v.absmin[0] -= 1;
480                 ent->v.absmin[1] -= 1;
481                 ent->v.absmin[2] -= 1;
482                 ent->v.absmax[0] += 1;
483                 ent->v.absmax[1] += 1;
484                 ent->v.absmax[2] += 1;
485         }
486
487         if (ent->v.solid == SOLID_NOT)
488                 return;
489
490 // find the first node that the ent's box crosses
491         node = sv_areanodes;
492         while (1)
493         {
494                 if (node->axis == -1)
495                         break;
496                 if (ent->v.absmin[node->axis] > node->dist)
497                         node = node->children[0];
498                 else if (ent->v.absmax[node->axis] < node->dist)
499                         node = node->children[1];
500                 else
501                         break;          // crosses the node
502         }
503
504 // link it in
505
506         if (ent->v.solid == SOLID_TRIGGER)
507                 InsertLinkBefore (&ent->area, &node->trigger_edicts);
508         else
509                 InsertLinkBefore (&ent->area, &node->solid_edicts);
510
511 // if touch_triggers, touch all entities at this node and descend for more
512         if (touch_triggers)
513                 SV_TouchLinks ( ent, sv_areanodes );
514 }
515
516
517
518 /*
519 ===============================================================================
520
521 POINT TESTING IN HULLS
522
523 ===============================================================================
524 */
525
526 /*
527 ==================
528 SV_HullPointContents
529
530 ==================
531 */
532 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
533 {
534         while (num >= 0)
535                 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];
536
537         return num;
538 }
539
540 /*
541 ============
542 SV_TestEntityPosition
543
544 This could be a lot more efficient...
545 ============
546 */
547 edict_t *SV_TestEntityPosition (edict_t *ent)
548 {
549         trace_t trace;
550
551         trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, MOVE_NORMAL, ent);
552
553         if (trace.startsolid)
554                 return sv.edicts;
555                 
556         return NULL;
557 }
558
559
560 /*
561 ===============================================================================
562
563 LINE TESTING IN HULLS
564
565 ===============================================================================
566 */
567
568 // 1/32 epsilon to keep floating point happy
569 //#define       DIST_EPSILON    (0.03125)
570 #define DIST_EPSILON (0.125)
571
572 #define HULLCHECKSTATE_EMPTY 0
573 #define HULLCHECKSTATE_SOLID 1
574 #define HULLCHECKSTATE_DONE 2
575
576 // LordHavoc: FIXME: this is not thread safe, if threading matters here, pass
577 // this as a struct to RecursiveHullCheck, RecursiveHullCheck_Impact, etc...
578 RecursiveHullCheckTraceInfo_t RecursiveHullCheckInfo;
579 #define RHC RecursiveHullCheckInfo
580
581 void SV_RecursiveHullCheck_Impact (mplane_t *plane, int side)
582 {
583         // LordHavoc: using doubles for extra accuracy
584         double t1, t2, frac;
585
586         // LordHavoc: now that we have found the impact, recalculate the impact
587         // point from scratch for maximum accuracy, with an epsilon bias on the
588         // surface distance
589         frac = plane->dist;
590         if (side)
591         {
592                 frac -= DIST_EPSILON;
593                 VectorNegate (plane->normal, RHC.trace->plane.normal);
594                 RHC.trace->plane.dist = -plane->dist;
595         }
596         else
597         {
598                 frac += DIST_EPSILON;
599                 VectorCopy (plane->normal, RHC.trace->plane.normal);
600                 RHC.trace->plane.dist = plane->dist;
601         }
602
603         if (plane->type < 3)
604         {
605                 t1 = RHC.start[plane->type] - frac;
606                 t2 = RHC.start[plane->type] + RHC.dist[plane->type] - frac;
607         }
608         else
609         {
610                 t1 = plane->normal[0] * RHC.start[0] + plane->normal[1] * RHC.start[1] + plane->normal[2] * RHC.start[2] - frac;
611                 t2 = plane->normal[0] * (RHC.start[0] + RHC.dist[0]) + plane->normal[1] * (RHC.start[1] + RHC.dist[1]) + plane->normal[2] * (RHC.start[2] + RHC.dist[2]) - frac;
612         }
613
614         frac = t1 / (t1 - t2);
615         frac = bound(0.0f, frac, 1.0f);
616
617         RHC.trace->fraction = frac;
618         RHC.trace->endpos[0] = RHC.start[0] + frac * RHC.dist[0];
619         RHC.trace->endpos[1] = RHC.start[1] + frac * RHC.dist[1];
620         RHC.trace->endpos[2] = RHC.start[2] + frac * RHC.dist[2];
621 }
622
623 int SV_RecursiveHullCheck (int num, float p1f, float p2f, vec3_t p1, vec3_t p2)
624 {
625         dclipnode_t     *node;
626         vec3_t          mid;
627         int                     side;
628         float           midf;
629         // LordHavoc: FIXME: this is not thread safe...  if threading matters here,
630         // remove the static prefixes
631         static int ret;
632         static mplane_t *plane;
633         static float t1, t2, frac;
634
635         // LordHavoc: a goto!  everyone flee in terror... :)
636 loc0:
637         // check for empty
638         if (num < 0)
639         {
640                 RHC.trace->endcontents = num;
641                 if (RHC.trace->startcontents)
642                 {
643                         if (num == RHC.trace->startcontents)
644                                 RHC.trace->allsolid = false;
645                         else
646                         {
647                                 // if the first leaf is solid, set startsolid
648                                 if (RHC.trace->allsolid)
649                                         RHC.trace->startsolid = true;
650                                 return HULLCHECKSTATE_SOLID;
651                         }
652                         return HULLCHECKSTATE_EMPTY;
653                 }
654                 else
655                 {
656                         if (num != CONTENTS_SOLID)
657                         {
658                                 RHC.trace->allsolid = false;
659                                 if (num == CONTENTS_EMPTY)
660                                         RHC.trace->inopen = true;
661                                 else
662                                         RHC.trace->inwater = true;
663                         }
664                         else
665                         {
666                                 // if the first leaf is solid, set startsolid
667                                 if (RHC.trace->allsolid)
668                                         RHC.trace->startsolid = true;
669                                 return HULLCHECKSTATE_SOLID;
670                         }
671                         return HULLCHECKSTATE_EMPTY;
672                 }
673         }
674
675         // find the point distances
676         node = RHC.hull->clipnodes + num;
677
678         plane = RHC.hull->planes + node->planenum;
679         if (plane->type < 3)
680         {
681                 t1 = p1[plane->type] - plane->dist;
682                 t2 = p2[plane->type] - plane->dist;
683         }
684         else
685         {
686                 t1 = DotProduct (plane->normal, p1) - plane->dist;
687                 t2 = DotProduct (plane->normal, p2) - plane->dist;
688         }
689
690         // LordHavoc: rearranged the side/frac code
691         if (t1 >= 0)
692         {
693                 if (t2 >= 0)
694                 {
695                         num = node->children[0];
696                         goto loc0;
697                 }
698                 // put the crosspoint DIST_EPSILON pixels on the near side
699                 side = 0;
700         }
701         else
702         {
703                 if (t2 < 0)
704                 {
705                         num = node->children[1];
706                         goto loc0;
707                 }
708                 // put the crosspoint DIST_EPSILON pixels on the near side
709                 side = 1;
710         }
711
712         frac = t1 / (t1 - t2);
713         frac = bound(0.0f, frac, 1.0f);
714
715         midf = p1f + ((p2f - p1f) * frac);
716         mid[0] = RHC.start[0] + midf * RHC.dist[0];
717         mid[1] = RHC.start[1] + midf * RHC.dist[1];
718         mid[2] = RHC.start[2] + midf * RHC.dist[2];
719
720         // front side first
721         ret = SV_RecursiveHullCheck (node->children[side], p1f, midf, p1, mid);
722         if (ret != HULLCHECKSTATE_EMPTY)
723                 return ret; // solid or done
724         ret = SV_RecursiveHullCheck (node->children[!side], midf, p2f, mid, p2);
725         if (ret != HULLCHECKSTATE_SOLID)
726                 return ret; // empty or done
727
728         // front is air and back is solid, this is the impact point...
729         SV_RecursiveHullCheck_Impact(RHC.hull->planes + node->planenum, side);
730
731         return HULLCHECKSTATE_DONE;
732 }
733
734 /*
735 qboolean SV_RecursiveHullCheckContentBoundary (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
736 {
737         dclipnode_t     *node;
738         mplane_t        *plane;
739         float           t1, t2;
740         float           frac;
741         int                     i;
742         vec3_t          mid;
743         int                     side;
744         float           midf;
745
746         // LordHavoc: a goto!  everyone flee in terror... :)
747 loc0:
748 // check for empty
749         if (num < 0)
750         {
751                 if (num != trace->startcontents)
752                         trace->startsolid = true;
753                 else
754                         trace->allsolid = false;
755                 return true;            // empty
756         }
757
758 // find the point distances
759         node = hull->clipnodes + num;
760         plane = hull->planes + node->planenum;
761
762         if (plane->type < 3)
763         {
764                 t1 = p1[plane->type] - plane->dist;
765                 t2 = p2[plane->type] - plane->dist;
766         }
767         else
768         {
769                 t1 = DotProduct (plane->normal, p1) - plane->dist;
770                 t2 = DotProduct (plane->normal, p2) - plane->dist;
771         }
772
773         // LordHavoc: rearranged the side/frac code
774         // LordHavoc: recursion optimization
775         if (t1 >= 0)
776         {
777                 if (t2 >= 0)
778                 {
779                         num = node->children[0];
780                         goto loc0;
781                 }
782                 // put the crosspoint DIST_EPSILON pixels on the near side
783                 side = 0;
784         }
785         else
786         {
787                 if (t2 < 0)
788                 {
789                         num = node->children[1];
790                         goto loc0;
791                 }
792                 // put the crosspoint DIST_EPSILON pixels on the near side
793                 side = 1;
794         }
795
796         frac = t1 / (t1 - t2);
797         frac = bound(0.0f, frac, 1.0f);
798
799         midf = p1f + ((p2f - p1f) * frac);
800         mid[0] = p1[0] + ((p2[0] - p1[0]) * frac);
801         mid[1] = p1[1] + ((p2[1] - p1[1]) * frac);
802         mid[2] = p1[2] + ((p2[2] - p1[2]) * frac);
803
804 // move up to the node
805         if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
806                 return false;
807
808 */
809         /*
810 #ifdef PARANOID
811         if (SV_HullPointContents (pm_hullmodel, mid, node->children[side]) != trace->startcontents)
812         {
813                 Con_Printf ("mid PointInHullSolid\n");
814                 return false;
815         }
816 #endif
817         */
818 /*
819
820         // LordHavoc: warning to the clumsy, this recursion can not be optimized because mid would need to be duplicated on a stack
821         if (SV_HullPointContents (hull, node->children[side^1], mid) == trace->startcontents)
822 // go past the node
823                 return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
824
825         if (trace->allsolid)
826                 return false;           // never got out of the solid area
827
828 //==================
829 // the other side of the node is solid, this is the impact point
830 //==================
831         if (!side)
832         {
833                 VectorCopy (plane->normal, trace->plane.normal);
834                 trace->plane.dist = plane->dist;
835         }
836         else
837         {
838                 VectorNegate (plane->normal, trace->plane.normal);
839                 trace->plane.dist = -plane->dist;
840         }
841
842 */
843         /*
844         while (SV_HullPointContents (hull, hull->firstclipnode, mid) != trace->startcontents)
845         {
846                 // shouldn't really happen, but does occasionally
847                 frac -= 0.1;
848                 if (frac < 0)
849                 {
850                         trace->fraction = midf;
851                         VectorCopy (mid, trace->endpos);
852                         Con_DPrintf ("backup past 0\n");
853                         return false;
854                 }
855                 midf = p1f + (p2f - p1f)*frac;
856                 mid[0] = p1[0] + frac*(p2[0] - p1[0]);
857                 mid[1] = p1[1] + frac*(p2[1] - p1[1]);
858                 mid[2] = p1[2] + frac*(p2[2] - p1[2]);
859         }
860         */
861 /*
862
863         frac = t1;
864         if (side)
865                 frac += DIST_EPSILON;
866         else
867                 frac -= DIST_EPSILON;
868
869         frac /= (t1 - t2);
870         frac = bound(0.0f, frac, 1.0f);
871
872         trace->fraction = p1f + (p2f - p1f)*frac;
873         trace->endpos[0] = p1[0] + frac*(p2[0] - p1[0]);
874         trace->endpos[1] = p1[1] + frac*(p2[1] - p1[1]);
875         trace->endpos[2] = p1[2] + frac*(p2[2] - p1[2]);
876
877         return false;
878 }
879 */
880
881 qboolean SV_TestLine (hull_t *hull, int num, vec3_t p1, vec3_t p2)
882 {
883         dclipnode_t     *node;
884         mplane_t        *plane;
885         float           t1, t2, frac;
886         vec3_t          mid;
887         int                     side;
888
889 loc0:
890 // check for empty
891         if (num < 0)
892                 return num != CONTENTS_SOLID;
893
894         if (num < hull->firstclipnode || num > hull->lastclipnode)
895                 Sys_Error ("SV_RecursiveHullCheck: bad node number");
896
897 //
898 // find the point distances
899 //
900         node = hull->clipnodes + num;
901         if (node->children[0] < 0)
902         {
903                 if (node->children[0] == CONTENTS_SOLID)
904                         return false;
905                 if (node->children[1] < 0)
906                         return node->children[1] != CONTENTS_SOLID;
907         }
908         else if (node->children[1] == CONTENTS_SOLID)
909                 return false;
910
911         plane = hull->planes + node->planenum;
912
913         if (plane->type < 3)
914         {
915                 t1 = p1[plane->type] - plane->dist;
916                 t2 = p2[plane->type] - plane->dist;
917         }
918         else
919         {
920                 t1 = DotProduct (plane->normal, p1) - plane->dist;
921                 t2 = DotProduct (plane->normal, p2) - plane->dist;
922         }
923
924         if (t1 >= 0)
925         {
926                 if (t2 >= 0)
927                 {
928                         num = node->children[0];
929                         goto loc0;
930                 }
931                 side = 0;
932         }
933         else
934         {
935                 if (t2 < 0)
936                 {
937                         num = node->children[1];
938                         goto loc0;
939                 }
940                 side = 1;
941         }
942
943         if (node->children[side] < 0)
944         {
945                 if (node->children[side] == CONTENTS_SOLID)
946                         return false;
947
948                 if (node->children[!side] < 0)
949                         return node->children[!side] != CONTENTS_SOLID;
950                 else
951                 {
952                         frac = t1 / (t1 - t2);
953                         frac = bound(0, frac, 1);
954
955                         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
956                         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
957                         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
958
959                         return SV_TestLine(hull, node->children[!side], mid, p2);
960                 }
961         }
962         else
963         {
964                 if (node->children[!side] < 0)
965                 {
966                         if (node->children[!side] == CONTENTS_SOLID)
967                                 return false;
968
969                         frac = t1 / (t1 - t2);
970                         frac = bound(0, frac, 1);
971
972                         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
973                         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
974                         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
975
976                         return SV_TestLine(hull, node->children[side], p1, mid);
977                 }
978                 else
979                 {
980                         frac = t1 / (t1 - t2);
981                         frac = bound(0, frac, 1);
982
983                         mid[0] = p1[0] + frac*(p2[0] - p1[0]);
984                         mid[1] = p1[1] + frac*(p2[1] - p1[1]);
985                         mid[2] = p1[2] + frac*(p2[2] - p1[2]);
986
987                         if (SV_TestLine(hull, node->children[side], p1, mid))
988                                 return SV_TestLine(hull, node->children[!side], mid, p2);
989                         else
990                                 return false;
991                 }
992         }
993 }
994
995
996 /*
997 ==================
998 SV_ClipMoveToEntity
999
1000 Handles selection or creation of a clipping hull, and offseting (and
1001 eventually rotation) of the end points
1002 ==================
1003 */
1004 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
1005 {
1006         trace_t         trace;
1007         vec3_t          offset;
1008         vec3_t          start_l, end_l;
1009         hull_t          *hull;
1010
1011 // fill in a default trace
1012         memset (&trace, 0, sizeof(trace_t));
1013         trace.fraction = 1;
1014         trace.allsolid = true;
1015         VectorCopy (end, trace.endpos);
1016
1017 // get the clipping hull
1018         hull = SV_HullForEntity (ent, mins, maxs, offset);
1019
1020         VectorSubtract (start, offset, start_l);
1021         VectorSubtract (end, offset, end_l);
1022
1023 // LordHavoc: enabling rotating bmodels
1024         // rotate start and end into the models frame of reference
1025         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
1026         {
1027                 vec3_t  forward, right, up;
1028                 vec3_t  temp;
1029
1030                 AngleVectors (ent->v.angles, forward, right, up);
1031
1032                 VectorCopy (start_l, temp);
1033                 start_l[0] = DotProduct (temp, forward);
1034                 start_l[1] = -DotProduct (temp, right);
1035                 start_l[2] = DotProduct (temp, up);
1036
1037                 VectorCopy (end_l, temp);
1038                 end_l[0] = DotProduct (temp, forward);
1039                 end_l[1] = -DotProduct (temp, right);
1040                 end_l[2] = DotProduct (temp, up);
1041         }
1042
1043 // trace a line through the apropriate clipping hull
1044         VectorCopy(start_l, RecursiveHullCheckInfo.start);
1045         VectorSubtract(end_l, start_l, RecursiveHullCheckInfo.dist);
1046         RecursiveHullCheckInfo.hull = hull;
1047         RecursiveHullCheckInfo.trace = &trace;
1048         SV_RecursiveHullCheck (hull->firstclipnode, 0, 1, start_l, end_l);
1049
1050 // LordHavoc: enabling rotating bmodels
1051         // rotate endpos back to world frame of reference
1052         if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
1053         {
1054                 vec3_t  a;
1055                 vec3_t  forward, right, up;
1056                 vec3_t  temp;
1057
1058                 if (trace.fraction != 1)
1059                 {
1060                         VectorNegate (ent->v.angles, a);
1061                         AngleVectors (a, forward, right, up);
1062
1063                         VectorCopy (trace.endpos, temp);
1064                         trace.endpos[0] = DotProduct (temp, forward);
1065                         trace.endpos[1] = -DotProduct (temp, right);
1066                         trace.endpos[2] = DotProduct (temp, up);
1067
1068                         VectorCopy (trace.plane.normal, temp);
1069                         trace.plane.normal[0] = DotProduct (temp, forward);
1070                         trace.plane.normal[1] = -DotProduct (temp, right);
1071                         trace.plane.normal[2] = DotProduct (temp, up);
1072                 }
1073         }
1074
1075 // fix trace up by the offset
1076         if (trace.fraction != 1)
1077                 VectorAdd (trace.endpos, offset, trace.endpos);
1078
1079 // did we clip the move?
1080         if (trace.fraction < 1 || trace.startsolid  )
1081                 trace.ent = ent;
1082
1083         return trace;
1084 }
1085
1086 //===========================================================================
1087
1088 /*
1089 ====================
1090 SV_ClipToLinks
1091
1092 Mins and maxs enclose the entire area swept by the move
1093 ====================
1094 */
1095 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
1096 {
1097         link_t          *l, *next;
1098         edict_t         *touch;
1099         trace_t         trace;
1100
1101 loc0:
1102 // touch linked edicts
1103         for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
1104         {
1105                 next = l->next;
1106                 touch = EDICT_FROM_AREA(l);
1107                 if (touch->v.solid == SOLID_NOT)
1108                         continue;
1109                 if (touch == clip->passedict)
1110                         continue;
1111                 if (touch->v.solid == SOLID_TRIGGER)
1112                         Sys_Error ("Trigger in clipping list");
1113
1114                 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
1115                         continue;
1116
1117                 if (clip->boxmins[0] > touch->v.absmax[0]
1118                  || clip->boxmins[1] > touch->v.absmax[1]
1119                  || clip->boxmins[2] > touch->v.absmax[2]
1120                  || clip->boxmaxs[0] < touch->v.absmin[0]
1121                  || clip->boxmaxs[1] < touch->v.absmin[1]
1122                  || clip->boxmaxs[2] < touch->v.absmin[2])
1123                         continue;
1124
1125                 if (clip->passedict != NULL && clip->passedict->v.size[0] && !touch->v.size[0])
1126                         continue;       // points never interact
1127
1128         // might intersect, so do an exact clip
1129                 if (clip->trace.allsolid)
1130                         return;
1131                 if (clip->passedict)
1132                 {
1133                         if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
1134                                 continue;       // don't clip against own missiles
1135                         if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
1136                                 continue;       // don't clip against owner
1137                         // LordHavoc: corpse code
1138                         if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
1139                                 continue;
1140                         if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
1141                                 continue;
1142                 }
1143
1144                 if ((int)touch->v.flags & FL_MONSTER)
1145                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
1146                 else
1147                         trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
1148                 if (trace.allsolid || trace.startsolid || trace.fraction < clip->trace.fraction)
1149                 {
1150                         trace.ent = touch;
1151                         if (clip->trace.startsolid)
1152                         {
1153                                 clip->trace = trace;
1154                                 clip->trace.startsolid = true;
1155                         }
1156                         else
1157                                 clip->trace = trace;
1158                 }
1159                 else if (trace.startsolid)
1160                         clip->trace.startsolid = true;
1161         }
1162
1163 // recurse down both sides
1164         if (node->axis == -1)
1165                 return;
1166
1167         // LordHavoc: optimized recursion
1168 //      if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
1169 //      if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
1170         if (clip->boxmaxs[node->axis] > node->dist)
1171         {
1172                 if (clip->boxmins[node->axis] < node->dist)
1173                         SV_ClipToLinks(node->children[1], clip);
1174                 node = node->children[0];
1175                 goto loc0;
1176         }
1177         else if (clip->boxmins[node->axis] < node->dist)
1178         {
1179                 node = node->children[1];
1180                 goto loc0;
1181         }
1182 }
1183
1184
1185 /*
1186 ==================
1187 SV_MoveBounds
1188 ==================
1189 */
1190 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
1191 {
1192 #if 0
1193 // debug to test against everything
1194 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
1195 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
1196 #else
1197         int             i;
1198         
1199         for (i=0 ; i<3 ; i++)
1200         {
1201                 if (end[i] > start[i])
1202                 {
1203                         boxmins[i] = start[i] + mins[i] - 1;
1204                         boxmaxs[i] = end[i] + maxs[i] + 1;
1205                 }
1206                 else
1207                 {
1208                         boxmins[i] = end[i] + mins[i] - 1;
1209                         boxmaxs[i] = start[i] + maxs[i] + 1;
1210                 }
1211         }
1212 #endif
1213 }
1214
1215 /*
1216 ==================
1217 SV_Move
1218 ==================
1219 */
1220 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
1221 {
1222         moveclip_t      clip;
1223         int                     i;
1224
1225         memset ( &clip, 0, sizeof ( moveclip_t ) );
1226
1227 // clip to world
1228         clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
1229
1230         clip.start = start;
1231         clip.end = end;
1232         clip.mins = mins;
1233         clip.maxs = maxs;
1234         clip.type = type;
1235         clip.passedict = passedict;
1236
1237         if (type == MOVE_MISSILE)
1238         {
1239                 for (i=0 ; i<3 ; i++)
1240                 {
1241                         clip.mins2[i] = -15;
1242                         clip.maxs2[i] = 15;
1243                 }
1244         }
1245         else
1246         {
1247                 VectorCopy (mins, clip.mins2);
1248                 VectorCopy (maxs, clip.maxs2);
1249         }
1250
1251 // create the bounding box of the entire move
1252         SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
1253
1254 // clip to entities
1255         SV_ClipToLinks ( sv_areanodes, &clip );
1256
1257         return clip.trace;
1258 }