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