2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // world.c -- world query functions
26 entities never clip against themselves, or their owner
28 line of sight checks trace->crosscontent, but bullets don't
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
46 ===============================================================================
50 ===============================================================================
54 static hull_t box_hull;
55 static dclipnode_t box_clipnodes[6];
56 static mplane_t box_planes[6];
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.
66 void SV_InitBoxHull (void)
71 box_hull.clipnodes = box_clipnodes;
72 box_hull.planes = box_planes;
73 box_hull.firstclipnode = 0;
74 box_hull.lastclipnode = 5;
78 box_clipnodes[i].planenum = i;
82 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
84 box_clipnodes[i].children[side^1] = i + 1;
86 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
88 box_planes[i].type = i>>1;
89 box_planes[i].normal[i>>1] = 1;
99 To keep everything totally uniform, bounding boxes are turned into small
100 BSP trees instead of being compared directly.
103 hull_t *SV_HullForBox (vec3_t mins, vec3_t maxs)
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];
121 Returns a hull that can be used for testing or clipping an object of mins/maxs
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.
127 hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
131 vec3_t hullmins, hullmaxs;
134 // decide which clipping hull to use, based on the size
135 if (ent->v.solid == SOLID_BSP)
136 { // explicit hulls in the BSP model
137 if (ent->v.movetype != MOVETYPE_PUSH)
138 Host_Error ("SOLID_BSP without MOVETYPE_PUSH");
140 model = sv.models[ (int)ent->v.modelindex ];
142 // LordHavoc: fixed SOLID_BSP error message
143 if (!model || model->type != mod_brush)
145 Con_Printf ("SOLID_BSP with a non bsp model, entity dump:\n");
147 Host_Error ("SOLID_BSP with a non bsp model\n");
150 VectorSubtract (maxs, mins, size);
151 // LordHavoc: FIXME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
153 hull = &model->hulls[0];
154 else if (size[0] <= 32)
155 hull = &model->hulls[1];
157 hull = &model->hulls[2];
159 // calculate an offset value to center the origin
160 VectorSubtract (hull->clip_mins, mins, offset);
161 VectorAdd (offset, ent->v.origin, offset);
164 { // create a temp hull from bounding box sizes
166 VectorSubtract (ent->v.mins, maxs, hullmins);
167 VectorSubtract (ent->v.maxs, mins, hullmaxs);
168 hull = SV_HullForBox (hullmins, hullmaxs);
170 VectorCopy (ent->v.origin, offset);
178 ===============================================================================
182 ===============================================================================
185 typedef struct areanode_s
187 int axis; // -1 = leaf node
189 struct areanode_s *children[2];
190 link_t trigger_edicts;
195 #define AREA_NODES 32
197 static areanode_t sv_areanodes[AREA_NODES];
198 static int sv_numareanodes;
206 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
210 vec3_t mins1, maxs1, mins2, maxs2;
212 anode = &sv_areanodes[sv_numareanodes];
215 ClearLink (&anode->trigger_edicts);
216 ClearLink (&anode->solid_edicts);
218 if (depth == AREA_DEPTH)
221 anode->children[0] = anode->children[1] = NULL;
225 VectorSubtract (maxs, mins, size);
226 if (size[0] > size[1])
231 anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
232 VectorCopy (mins, mins1);
233 VectorCopy (mins, mins2);
234 VectorCopy (maxs, maxs1);
235 VectorCopy (maxs, maxs2);
237 maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
239 anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
240 anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
251 void SV_ClearWorld (void)
255 memset (sv_areanodes, 0, sizeof(sv_areanodes));
257 SV_CreateAreaNode (0, sv.worldmodel->mins, sv.worldmodel->maxs);
267 void SV_UnlinkEdict (edict_t *ent)
270 return; // not linked in anywhere
271 RemoveLink (&ent->area);
272 ent->area.prev = ent->area.next = NULL;
281 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
285 int old_self, old_other;
288 // touch linked edicts
289 for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
292 touch = EDICT_FROM_AREA(l);
295 if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
297 if (ent->v.absmin[0] > touch->v.absmax[0]
298 || ent->v.absmin[1] > touch->v.absmax[1]
299 || ent->v.absmin[2] > touch->v.absmax[2]
300 || ent->v.absmax[0] < touch->v.absmin[0]
301 || ent->v.absmax[1] < touch->v.absmin[1]
302 || ent->v.absmax[2] < touch->v.absmin[2] )
304 old_self = pr_global_struct->self;
305 old_other = pr_global_struct->other;
307 pr_global_struct->self = EDICT_TO_PROG(touch);
308 pr_global_struct->other = EDICT_TO_PROG(ent);
309 pr_global_struct->time = sv.time;
310 PR_ExecuteProgram (touch->v.touch);
312 pr_global_struct->self = old_self;
313 pr_global_struct->other = old_other;
316 // recurse down both sides
317 if (node->axis == -1)
320 // LordHavoc: optimized recursion
321 // if (ent->v.absmax[node->axis] > node->dist) SV_TouchLinks (ent, node->children[0]);
322 // if (ent->v.absmin[node->axis] < node->dist) SV_TouchLinks (ent, node->children[1]);
323 if (ent->v.absmax[node->axis] > node->dist)
325 if (ent->v.absmin[node->axis] < node->dist)
326 SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
327 node = node->children[0];
332 if (ent->v.absmin[node->axis] < node->dist)
334 node = node->children[1];
347 void SV_FindTouchedLeafs (edict_t *ent, mnode_t *node)
349 mplane_t *splitplane;
355 if (node->contents == CONTENTS_SOLID)
358 // add an efrag if the node is a leaf
360 if ( node->contents < 0)
362 if (ent->num_leafs == MAX_ENT_LEAFS)
365 leaf = (mleaf_t *)node;
366 leafnum = leaf - sv.worldmodel->leafs - 1;
368 ent->leafnums[ent->num_leafs] = leafnum;
375 splitplane = node->plane;
376 sides = BOX_ON_PLANE_SIDE(ent->v.absmin, ent->v.absmax, splitplane);
378 // recurse down the contacted sides
379 // LordHavoc: optimized recursion
380 // if (sides & 1) SV_FindTouchedLeafs (ent, node->children[0]);
381 // if (sides & 2) SV_FindTouchedLeafs (ent, node->children[1]);
385 node = node->children[0];
388 node = node->children[1];
391 if (node->children[0]->contents != CONTENTS_SOLID)
392 SV_FindTouchedLeafs (ent, node->children[0]);
393 node = node->children[1];
404 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
409 SV_UnlinkEdict (ent); // unlink from old position
411 if (ent == sv.edicts)
412 return; // don't add the world
419 // LordHavoc: enabling rotating bmodels
420 if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
421 { // expand for rotation
426 for (i=0 ; i<3 ; i++)
428 v =fabs( ent->v.mins[i]);
431 v =fabs( ent->v.maxs[i]);
435 for (i=0 ; i<3 ; i++)
437 ent->v.absmin[i] = ent->v.origin[i] - max;
438 ent->v.absmax[i] = ent->v.origin[i] + max;
443 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
444 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
448 // to make items easier to pick up and allow them to be grabbed off
449 // of shelves, the abs sizes are expanded
451 if ((int)ent->v.flags & FL_ITEM)
453 ent->v.absmin[0] -= 15;
454 ent->v.absmin[1] -= 15;
455 ent->v.absmax[0] += 15;
456 ent->v.absmax[1] += 15;
459 { // because movement is clipped an epsilon away from an actual edge,
460 // we must fully check even when bounding boxes don't quite touch
461 ent->v.absmin[0] -= 1;
462 ent->v.absmin[1] -= 1;
463 ent->v.absmin[2] -= 1;
464 ent->v.absmax[0] += 1;
465 ent->v.absmax[1] += 1;
466 ent->v.absmax[2] += 1;
471 if (ent->v.modelindex)
472 SV_FindTouchedLeafs (ent, sv.worldmodel->nodes);
474 if (ent->v.solid == SOLID_NOT)
477 // find the first node that the ent's box crosses
481 if (node->axis == -1)
483 if (ent->v.absmin[node->axis] > node->dist)
484 node = node->children[0];
485 else if (ent->v.absmax[node->axis] < node->dist)
486 node = node->children[1];
488 break; // crosses the node
493 if (ent->v.solid == SOLID_TRIGGER)
494 InsertLinkBefore (&ent->area, &node->trigger_edicts);
496 InsertLinkBefore (&ent->area, &node->solid_edicts);
498 // if touch_triggers, touch all entities at this node and descend for more
500 SV_TouchLinks ( ent, sv_areanodes );
506 ===============================================================================
508 POINT TESTING IN HULLS
510 ===============================================================================
513 // SV_HullPointContents moved to cpu_noasm.c
517 SV_TestEntityPosition
519 This could be a lot more efficient...
522 edict_t *SV_TestEntityPosition (edict_t *ent)
526 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, 0, ent);
528 if (trace.startsolid)
536 ===============================================================================
538 LINE TESTING IN HULLS
540 ===============================================================================
543 // 1/32 epsilon to keep floating point happy
544 #define DIST_EPSILON (0.03125)
548 SV_RecursiveHullCheck
552 qboolean SV_RecursiveHullCheck (hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, trace_t *trace)
567 if (num != CONTENTS_SOLID)
569 trace->allsolid = false;
570 if (num == CONTENTS_EMPTY)
571 trace->inopen = true;
573 trace->inwater = true;
576 trace->startsolid = true;
577 return true; // empty
580 if (num < hull->firstclipnode || num > hull->lastclipnode)
581 Sys_Error ("SV_RecursiveHullCheck: bad node number");
584 // find the point distances
586 node = hull->clipnodes + num;
587 plane = hull->planes + node->planenum;
591 t1 = p1[plane->type] - plane->dist;
592 t2 = p2[plane->type] - plane->dist;
596 t1 = DotProduct (plane->normal, p1) - plane->dist;
597 t2 = DotProduct (plane->normal, p2) - plane->dist;
601 if (t1 >= 0 && t2 >= 0)
602 // LordHavoc: optimized recursion
603 // return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
605 num = node->children[0];
608 if (t1 < 0 && t2 < 0)
609 // return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
611 num = node->children[1];
615 if ( (t1 >= DIST_EPSILON && t2 >= DIST_EPSILON) || (t2 > t1 && t1 >= 0) )
616 return SV_RecursiveHullCheck (hull, node->children[0], p1f, p2f, p1, p2, trace);
617 if ( (t1 <= -DIST_EPSILON && t2 <= -DIST_EPSILON) || (t2 < t1 && t1 <= 0) )
618 return SV_RecursiveHullCheck (hull, node->children[1], p1f, p2f, p1, p2, trace);
621 // put the crosspoint DIST_EPSILON pixels on the near side
623 frac = (t1 + DIST_EPSILON)/(t1-t2);
625 frac = (t1 - DIST_EPSILON)/(t1-t2);
631 midf = p1f + (p2f - p1f)*frac;
632 for (i=0 ; i<3 ; i++)
633 mid[i] = p1[i] + frac*(p2[i] - p1[i]);
637 // move up to the node
638 if (!SV_RecursiveHullCheck (hull, node->children[side], p1f, midf, p1, mid, trace) )
642 if (SV_HullPointContents (hull, node->children[side], mid) == CONTENTS_SOLID)
644 Con_Printf ("mid PointInHullSolid\n");
649 if (SV_HullPointContents (hull, node->children[side^1], mid) != CONTENTS_SOLID)
651 return SV_RecursiveHullCheck (hull, node->children[side^1], midf, p2f, mid, p2, trace);
652 // mid would need to be duplicated during recursion...
657 num = node->children[side^1];
663 return false; // never got out of the solid area
666 // the other side of the node is solid, this is the impact point
670 VectorCopy (plane->normal, trace->plane.normal);
671 trace->plane.dist = plane->dist;
675 // LordHavoc: unrolled vector operation because the compiler can't be sure vec3_origin is 0
676 // VectorSubtract (vec3_origin, plane->normal, trace->plane.normal);
677 trace->plane.normal[0] = -plane->normal[0];
678 trace->plane.normal[1] = -plane->normal[1];
679 trace->plane.normal[2] = -plane->normal[2];
680 trace->plane.dist = -plane->dist;
683 while (SV_HullPointContents (hull, hull->firstclipnode, mid) == CONTENTS_SOLID)
684 { // shouldn't really happen, but does occasionally
688 trace->fraction = midf;
689 VectorCopy (mid, trace->endpos);
690 Con_DPrintf ("backup past 0\n");
693 midf = p1f + (p2f - p1f)*frac;
694 for (i=0 ; i<3 ; i++)
695 mid[i] = p1[i] + frac*(p2[i] - p1[i]);
698 trace->fraction = midf;
699 VectorCopy (mid, trace->endpos);
709 Handles selection or creation of a clipping hull, and offseting (and
710 eventually rotation) of the end points
713 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
717 vec3_t start_l, end_l;
720 // fill in a default trace
721 memset (&trace, 0, sizeof(trace_t));
723 trace.allsolid = true;
724 VectorCopy (end, trace.endpos);
726 // get the clipping hull
727 hull = SV_HullForEntity (ent, mins, maxs, offset);
729 VectorSubtract (start, offset, start_l);
730 VectorSubtract (end, offset, end_l);
732 // LordHavoc: enabling rotating bmodels
733 // rotate start and end into the models frame of reference
734 if (ent->v.solid == SOLID_BSP &&
735 (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
737 vec3_t forward, right, up;
740 AngleVectors (ent->v.angles, forward, right, up);
742 VectorCopy (start_l, temp);
743 start_l[0] = DotProduct (temp, forward);
744 start_l[1] = -DotProduct (temp, right);
745 start_l[2] = DotProduct (temp, up);
747 VectorCopy (end_l, temp);
748 end_l[0] = DotProduct (temp, forward);
749 end_l[1] = -DotProduct (temp, right);
750 end_l[2] = DotProduct (temp, up);
753 // trace a line through the apropriate clipping hull
754 SV_RecursiveHullCheck (hull, hull->firstclipnode, 0, 1, start_l, end_l, &trace);
756 // LordHavoc: enabling rotating bmodels
757 // rotate endpos back to world frame of reference
758 if (ent->v.solid == SOLID_BSP &&
759 (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]) )
762 vec3_t forward, right, up;
765 if (trace.fraction != 1)
767 VectorSubtract (vec3_origin, ent->v.angles, a);
768 AngleVectors (a, forward, right, up);
770 VectorCopy (trace.endpos, temp);
771 trace.endpos[0] = DotProduct (temp, forward);
772 trace.endpos[1] = -DotProduct (temp, right);
773 trace.endpos[2] = DotProduct (temp, up);
775 VectorCopy (trace.plane.normal, temp);
776 trace.plane.normal[0] = DotProduct (temp, forward);
777 trace.plane.normal[1] = -DotProduct (temp, right);
778 trace.plane.normal[2] = DotProduct (temp, up);
782 // fix trace up by the offset
783 if (trace.fraction != 1)
784 VectorAdd (trace.endpos, offset, trace.endpos);
786 // did we clip the move?
787 if (trace.fraction < 1 || trace.startsolid )
793 //===========================================================================
799 Mins and maxs enclose the entire area swept by the move
802 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
809 // touch linked edicts
810 for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
813 touch = EDICT_FROM_AREA(l);
814 if (touch->v.solid == SOLID_NOT)
816 if (touch == clip->passedict)
818 if (touch->v.solid == SOLID_TRIGGER)
819 Sys_Error ("Trigger in clipping list");
821 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
824 if (clip->boxmins[0] > touch->v.absmax[0]
825 || clip->boxmins[1] > touch->v.absmax[1]
826 || clip->boxmins[2] > touch->v.absmax[2]
827 || clip->boxmaxs[0] < touch->v.absmin[0]
828 || clip->boxmaxs[1] < touch->v.absmin[1]
829 || clip->boxmaxs[2] < touch->v.absmin[2] )
832 if (clip->passedict!=0 && clip->passedict->v.size[0] && !touch->v.size[0])
833 continue; // points never interact
835 // might intersect, so do an exact clip
836 if (clip->trace.allsolid)
840 if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
841 continue; // don't clip against own missiles
842 if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
843 continue; // don't clip against owner
844 // LordHavoc: corpse code
845 if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
847 if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
851 if ((int)touch->v.flags & FL_MONSTER)
852 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
854 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
855 if (trace.allsolid || trace.startsolid ||
856 trace.fraction < clip->trace.fraction)
859 if (clip->trace.startsolid)
862 clip->trace.startsolid = true;
867 else if (trace.startsolid)
868 clip->trace.startsolid = true;
871 // recurse down both sides
872 if (node->axis == -1)
875 // LordHavoc: optimized recursion
876 // if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
877 // if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
878 if (clip->boxmaxs[node->axis] > node->dist)
880 if (clip->boxmins[node->axis] < node->dist)
881 SV_ClipToLinks(node->children[1], clip);
882 node = node->children[0];
885 else if (clip->boxmins[node->axis] < node->dist)
887 node = node->children[1];
898 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
901 // debug to test against everything
902 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
903 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
907 for (i=0 ; i<3 ; i++)
909 if (end[i] > start[i])
911 boxmins[i] = start[i] + mins[i] - 1;
912 boxmaxs[i] = end[i] + maxs[i] + 1;
916 boxmins[i] = end[i] + mins[i] - 1;
917 boxmaxs[i] = start[i] + maxs[i] + 1;
928 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
933 memset ( &clip, 0, sizeof ( moveclip_t ) );
936 clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
943 clip.passedict = passedict;
945 if (type == MOVE_MISSILE)
947 for (i=0 ; i<3 ; i++)
955 VectorCopy (mins, clip.mins2);
956 VectorCopy (maxs, clip.maxs2);
959 // create the bounding box of the entire move
960 SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
963 SV_ClipToLinks ( sv_areanodes, &clip );