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 struct link_s *prev, *next;
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);
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)))
50 #define EDICT_FROM_AREA(l) ((edict_t *)((byte *)l - (int)&(((edict_t *)0)->area)))
52 //============================================================================
54 // ClearLink is used for new headnodes
55 void ClearLink (link_t *l)
57 l->prev = l->next = l;
60 void RemoveLink (link_t *l)
62 l->next->prev = l->prev;
63 l->prev->next = l->next;
66 void InsertLinkBefore (link_t *l, link_t *before)
69 l->prev = before->prev;
73 void InsertLinkAfter (link_t *l, link_t *after)
75 l->next = after->next;
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
95 ===============================================================================
99 ===============================================================================
103 static hull_t box_hull;
104 static dclipnode_t box_clipnodes[6];
105 static mplane_t box_planes[6];
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.
115 void SV_InitBoxHull (void)
120 box_hull.clipnodes = box_clipnodes;
121 box_hull.planes = box_planes;
122 box_hull.firstclipnode = 0;
123 box_hull.lastclipnode = 5;
125 for (i=0 ; i<6 ; i++)
127 box_clipnodes[i].planenum = i;
131 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
133 box_clipnodes[i].children[side^1] = i + 1;
135 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
137 box_planes[i].type = i>>1;
138 box_planes[i].normal[i>>1] = 1;
148 To keep everything totally uniform, bounding boxes are turned into small
149 BSP trees instead of being compared directly.
152 hull_t *SV_HullForBox (vec3_t mins, vec3_t maxs)
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];
170 Returns a hull that can be used for testing or clipping an object of mins/maxs
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.
176 hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
180 vec3_t hullmins, hullmaxs;
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");
189 model = sv.models[ (int)ent->v.modelindex ];
190 Mod_CheckLoaded(model);
192 // LordHavoc: fixed SOLID_BSP error message
193 if (!model || model->type != mod_brush)
195 Con_Printf ("SOLID_BSP with a non bsp model, entity dump:\n");
197 Host_Error ("SOLID_BSP with a non bsp model\n");
200 VectorSubtract (maxs, mins, size);
201 // LordHavoc: FIXME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
202 if (sv.worldmodel->ishlbsp)
205 hull = &model->hulls[0]; // 0x0x0
206 else if (size[0] <= 32)
208 if (size[2] < 54) // pick the nearest of 36 or 72
209 hull = &model->hulls[3]; // 32x32x36
211 hull = &model->hulls[1]; // 32x32x72
214 hull = &model->hulls[2]; // 64x64x64
219 hull = &model->hulls[0]; // 0x0x0
220 else if (size[0] <= 32)
221 hull = &model->hulls[1]; // 32x32x56
223 hull = &model->hulls[2]; // 64x64x88
226 // calculate an offset value to center the origin
227 VectorSubtract (hull->clip_mins, mins, offset);
228 VectorAdd (offset, ent->v.origin, offset);
231 { // create a temp hull from bounding box sizes
233 VectorSubtract (ent->v.mins, maxs, hullmins);
234 VectorSubtract (ent->v.maxs, mins, hullmaxs);
235 hull = SV_HullForBox (hullmins, hullmaxs);
237 VectorCopy (ent->v.origin, offset);
245 ===============================================================================
249 ===============================================================================
252 typedef struct areanode_s
254 int axis; // -1 = leaf node
256 struct areanode_s *children[2];
257 link_t trigger_edicts;
262 #define AREA_NODES 32
264 static areanode_t sv_areanodes[AREA_NODES];
265 static int sv_numareanodes;
273 areanode_t *SV_CreateAreaNode (int depth, vec3_t mins, vec3_t maxs)
277 vec3_t mins1, maxs1, mins2, maxs2;
279 anode = &sv_areanodes[sv_numareanodes];
282 ClearLink (&anode->trigger_edicts);
283 ClearLink (&anode->solid_edicts);
285 if (depth == AREA_DEPTH)
288 anode->children[0] = anode->children[1] = NULL;
292 VectorSubtract (maxs, mins, size);
293 if (size[0] > size[1])
298 anode->dist = 0.5 * (maxs[anode->axis] + mins[anode->axis]);
299 VectorCopy (mins, mins1);
300 VectorCopy (mins, mins2);
301 VectorCopy (maxs, maxs1);
302 VectorCopy (maxs, maxs2);
304 maxs1[anode->axis] = mins2[anode->axis] = anode->dist;
306 anode->children[0] = SV_CreateAreaNode (depth+1, mins2, maxs2);
307 anode->children[1] = SV_CreateAreaNode (depth+1, mins1, maxs1);
318 void SV_ClearWorld (void)
322 memset (sv_areanodes, 0, sizeof(sv_areanodes));
324 Mod_CheckLoaded(sv.worldmodel);
325 SV_CreateAreaNode (0, sv.worldmodel->normalmins, sv.worldmodel->normalmaxs);
335 void SV_UnlinkEdict (edict_t *ent)
338 return; // not linked in anywhere
339 RemoveLink (&ent->area);
340 ent->area.prev = ent->area.next = NULL;
349 void SV_TouchLinks ( edict_t *ent, areanode_t *node )
353 int old_self, old_other;
356 // touch linked edicts
357 for (l = node->trigger_edicts.next ; l != &node->trigger_edicts ; l = next)
360 touch = EDICT_FROM_AREA(l);
363 if (!touch->v.touch || touch->v.solid != SOLID_TRIGGER)
365 if (ent->v.absmin[0] > touch->v.absmax[0]
366 || ent->v.absmin[1] > touch->v.absmax[1]
367 || ent->v.absmin[2] > touch->v.absmax[2]
368 || ent->v.absmax[0] < touch->v.absmin[0]
369 || ent->v.absmax[1] < touch->v.absmin[1]
370 || ent->v.absmax[2] < touch->v.absmin[2])
372 old_self = pr_global_struct->self;
373 old_other = pr_global_struct->other;
375 pr_global_struct->self = EDICT_TO_PROG(touch);
376 pr_global_struct->other = EDICT_TO_PROG(ent);
377 pr_global_struct->time = sv.time;
378 PR_ExecuteProgram (touch->v.touch, "");
380 pr_global_struct->self = old_self;
381 pr_global_struct->other = old_other;
384 // recurse down both sides
385 if (node->axis == -1)
388 // LordHavoc: optimized recursion
389 // if (ent->v.absmax[node->axis] > node->dist) SV_TouchLinks (ent, node->children[0]);
390 // if (ent->v.absmin[node->axis] < node->dist) SV_TouchLinks (ent, node->children[1]);
391 if (ent->v.absmax[node->axis] > node->dist)
393 if (ent->v.absmin[node->axis] < node->dist)
394 SV_TouchLinks(ent, node->children[1]); // order reversed to reduce code
395 node = node->children[0];
400 if (ent->v.absmin[node->axis] < node->dist)
402 node = node->children[1];
415 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
420 SV_UnlinkEdict (ent); // unlink from old position
422 if (ent == sv.edicts)
423 return; // don't add the world
430 // LordHavoc: enabling rotating bmodels
431 if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
433 // expand for rotation
437 max = DotProduct(ent->v.mins, ent->v.mins);
438 v = DotProduct(ent->v.maxs, ent->v.maxs);
444 for (i=0 ; i<3 ; i++)
446 v = fabs(ent->v.mins[i]);
449 v = fabs(ent->v.maxs[i]);
454 for (i=0 ; i<3 ; i++)
456 ent->v.absmin[i] = ent->v.origin[i] - max;
457 ent->v.absmax[i] = ent->v.origin[i] + max;
462 VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
463 VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
467 // to make items easier to pick up and allow them to be grabbed off
468 // of shelves, the abs sizes are expanded
470 if ((int)ent->v.flags & FL_ITEM)
472 ent->v.absmin[0] -= 15;
473 ent->v.absmin[1] -= 15;
474 ent->v.absmax[0] += 15;
475 ent->v.absmax[1] += 15;
479 // because movement is clipped an epsilon away from an actual edge,
480 // we must fully check even when bounding boxes don't quite touch
481 ent->v.absmin[0] -= 1;
482 ent->v.absmin[1] -= 1;
483 ent->v.absmin[2] -= 1;
484 ent->v.absmax[0] += 1;
485 ent->v.absmax[1] += 1;
486 ent->v.absmax[2] += 1;
489 if (ent->v.solid == SOLID_NOT)
492 // find the first node that the ent's box crosses
496 if (node->axis == -1)
498 if (ent->v.absmin[node->axis] > node->dist)
499 node = node->children[0];
500 else if (ent->v.absmax[node->axis] < node->dist)
501 node = node->children[1];
503 break; // crosses the node
508 if (ent->v.solid == SOLID_TRIGGER)
509 InsertLinkBefore (&ent->area, &node->trigger_edicts);
511 InsertLinkBefore (&ent->area, &node->solid_edicts);
513 // if touch_triggers, touch all entities at this node and descend for more
515 SV_TouchLinks ( ent, sv_areanodes );
521 ===============================================================================
523 POINT TESTING IN HULLS
525 ===============================================================================
534 int SV_HullPointContents (hull_t *hull, int num, vec3_t p)
537 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];
544 SV_TestEntityPosition
546 This could be a lot more efficient...
549 edict_t *SV_TestEntityPosition (edict_t *ent)
553 trace = SV_Move (ent->v.origin, ent->v.mins, ent->v.maxs, ent->v.origin, MOVE_NORMAL, ent);
555 if (trace.startsolid)
563 ===============================================================================
565 LINE TESTING IN HULLS
567 ===============================================================================
570 // 1/32 epsilon to keep floating point happy
571 #define DIST_EPSILON (0.03125)
573 #define HULLCHECKSTATE_EMPTY 0
574 #define HULLCHECKSTATE_SOLID 1
575 #define HULLCHECKSTATE_DONE 2
577 // LordHavoc: FIXME: this is not thread safe, if threading matters here, pass
578 // this as a struct to RecursiveHullCheck, RecursiveHullCheck_Impact, etc...
579 RecursiveHullCheckTraceInfo_t RecursiveHullCheckInfo;
580 #define RHC RecursiveHullCheckInfo
582 void SV_RecursiveHullCheck_Impact (mplane_t *plane, int side)
584 // LordHavoc: using doubles for extra accuracy
587 // LordHavoc: now that we have found the impact, recalculate the impact
588 // point from scratch for maximum accuracy, with an epsilon bias on the
593 frac -= DIST_EPSILON;
594 VectorNegate (plane->normal, RHC.trace->plane.normal);
595 RHC.trace->plane.dist = -plane->dist;
599 frac += DIST_EPSILON;
600 VectorCopy (plane->normal, RHC.trace->plane.normal);
601 RHC.trace->plane.dist = plane->dist;
606 t1 = RHC.start[plane->type] - frac;
607 t2 = RHC.start[plane->type] + RHC.dist[plane->type] - frac;
611 t1 = plane->normal[0] * RHC.start[0] + plane->normal[1] * RHC.start[1] + plane->normal[2] * RHC.start[2] - frac;
612 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;
615 frac = t1 / (t1 - t2);
616 frac = bound(0.0f, frac, 1.0);
618 RHC.trace->fraction = frac;
619 RHC.trace->endpos[0] = RHC.start[0] + frac * RHC.dist[0];
620 RHC.trace->endpos[1] = RHC.start[1] + frac * RHC.dist[1];
621 RHC.trace->endpos[2] = RHC.start[2] + frac * RHC.dist[2];
624 int SV_RecursiveHullCheck (int num, double p1f, double p2f, double p1[3], double p2[3])
629 // LordHavoc: FIXME: this is not thread safe... if threading matters here,
630 // remove the static prefixes
632 static mplane_t *plane;
633 static double t1, t2, frac;
635 // LordHavoc: a goto! everyone flee in terror... :)
640 RHC.trace->endcontents = num;
641 if (RHC.trace->startcontents)
643 if (num == RHC.trace->startcontents)
644 RHC.trace->allsolid = false;
647 // if the first leaf is solid, set startsolid
648 if (RHC.trace->allsolid)
649 RHC.trace->startsolid = true;
650 return HULLCHECKSTATE_SOLID;
652 return HULLCHECKSTATE_EMPTY;
656 if (num != CONTENTS_SOLID)
658 RHC.trace->allsolid = false;
659 if (num == CONTENTS_EMPTY)
660 RHC.trace->inopen = true;
662 RHC.trace->inwater = true;
666 // if the first leaf is solid, set startsolid
667 if (RHC.trace->allsolid)
668 RHC.trace->startsolid = true;
669 return HULLCHECKSTATE_SOLID;
671 return HULLCHECKSTATE_EMPTY;
675 // find the point distances
676 node = RHC.hull->clipnodes + num;
678 plane = RHC.hull->planes + node->planenum;
681 t1 = p1[plane->type] - plane->dist;
682 t2 = p2[plane->type] - plane->dist;
686 t1 = DotProduct (plane->normal, p1) - plane->dist;
687 t2 = DotProduct (plane->normal, p2) - plane->dist;
690 // LordHavoc: rearranged the side/frac code
695 num = node->children[0];
698 // put the crosspoint DIST_EPSILON pixels on the near side
705 num = node->children[1];
708 // put the crosspoint DIST_EPSILON pixels on the near side
712 frac = t1 / (t1 - t2);
713 frac = bound(0.0f, frac, 1.0);
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];
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
728 // front is air and back is solid, this is the impact point...
729 SV_RecursiveHullCheck_Impact(RHC.hull->planes + node->planenum, side);
731 return HULLCHECKSTATE_DONE;
738 Handles selection or creation of a clipping hull, and offseting (and
739 eventually rotation) of the end points
742 trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end)
745 vec3_t offset, start_l, end_l;
746 double startd[3], endd[3];
749 // fill in a default trace
750 memset (&trace, 0, sizeof(trace_t));
752 trace.allsolid = true;
753 VectorCopy (end, trace.endpos);
755 // get the clipping hull
756 hull = SV_HullForEntity (ent, mins, maxs, offset);
758 VectorSubtract (start, offset, start_l);
759 VectorSubtract (end, offset, end_l);
761 // LordHavoc: enabling rotating bmodels
762 // rotate start and end into the models frame of reference
763 if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
765 vec3_t forward, right, up;
768 AngleVectors (ent->v.angles, forward, right, up);
770 VectorCopy (start_l, temp);
771 start_l[0] = DotProduct (temp, forward);
772 start_l[1] = -DotProduct (temp, right);
773 start_l[2] = DotProduct (temp, up);
775 VectorCopy (end_l, temp);
776 end_l[0] = DotProduct (temp, forward);
777 end_l[1] = -DotProduct (temp, right);
778 end_l[2] = DotProduct (temp, up);
781 VectorCopy(start_l, startd);
782 VectorCopy(end_l, endd);
784 // trace a line through the appropriate clipping hull
785 VectorCopy(startd, RecursiveHullCheckInfo.start);
786 VectorSubtract(endd, startd, RecursiveHullCheckInfo.dist);
787 RecursiveHullCheckInfo.hull = hull;
788 RecursiveHullCheckInfo.trace = &trace;
789 SV_RecursiveHullCheck (hull->firstclipnode, 0, 1, startd, endd);
791 // LordHavoc: enabling rotating bmodels
792 // rotate endpos back to world frame of reference
793 if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
796 vec3_t forward, right, up;
799 if (trace.fraction != 1)
801 VectorNegate (ent->v.angles, a);
802 AngleVectors (a, forward, right, up);
804 VectorCopy (trace.endpos, temp);
805 trace.endpos[0] = DotProduct (temp, forward);
806 trace.endpos[1] = -DotProduct (temp, right);
807 trace.endpos[2] = DotProduct (temp, up);
809 VectorCopy (trace.plane.normal, temp);
810 trace.plane.normal[0] = DotProduct (temp, forward);
811 trace.plane.normal[1] = -DotProduct (temp, right);
812 trace.plane.normal[2] = DotProduct (temp, up);
816 // fix trace up by the offset
817 if (trace.fraction != 1)
818 VectorAdd (trace.endpos, offset, trace.endpos);
820 // did we clip the move?
821 if (trace.fraction < 1 || trace.startsolid )
827 //===========================================================================
833 Mins and maxs enclose the entire area swept by the move
836 void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
843 // touch linked edicts
844 for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
847 touch = EDICT_FROM_AREA(l);
848 if (touch->v.solid == SOLID_NOT)
850 if (touch == clip->passedict)
852 if (touch->v.solid == SOLID_TRIGGER)
853 Sys_Error ("Trigger in clipping list");
855 if (clip->type == MOVE_NOMONSTERS && touch->v.solid != SOLID_BSP)
858 if (clip->boxmins[0] > touch->v.absmax[0]
859 || clip->boxmins[1] > touch->v.absmax[1]
860 || clip->boxmins[2] > touch->v.absmax[2]
861 || clip->boxmaxs[0] < touch->v.absmin[0]
862 || clip->boxmaxs[1] < touch->v.absmin[1]
863 || clip->boxmaxs[2] < touch->v.absmin[2])
866 if (clip->passedict != NULL && clip->passedict->v.size[0] && !touch->v.size[0])
867 continue; // points never interact
869 // might intersect, so do an exact clip
870 if (clip->trace.allsolid)
874 if (PROG_TO_EDICT(touch->v.owner) == clip->passedict)
875 continue; // don't clip against own missiles
876 if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
877 continue; // don't clip against owner
878 // LordHavoc: corpse code
879 if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
881 if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
885 if ((int)touch->v.flags & FL_MONSTER)
886 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
888 trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
889 if (trace.allsolid || trace.startsolid || trace.fraction < clip->trace.fraction)
892 if (clip->trace.startsolid)
895 clip->trace.startsolid = true;
900 else if (trace.startsolid)
901 clip->trace.startsolid = true;
904 // recurse down both sides
905 if (node->axis == -1)
908 // LordHavoc: optimized recursion
909 // if (clip->boxmaxs[node->axis] > node->dist) SV_ClipToLinks(node->children[0], clip);
910 // if (clip->boxmins[node->axis] < node->dist) SV_ClipToLinks(node->children[1], clip);
911 if (clip->boxmaxs[node->axis] > node->dist)
913 if (clip->boxmins[node->axis] < node->dist)
914 SV_ClipToLinks(node->children[1], clip);
915 node = node->children[0];
918 else if (clip->boxmins[node->axis] < node->dist)
920 node = node->children[1];
931 void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t boxmins, vec3_t boxmaxs)
934 // debug to test against everything
935 boxmins[0] = boxmins[1] = boxmins[2] = -9999;
936 boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
940 for (i=0 ; i<3 ; i++)
942 if (end[i] > start[i])
944 boxmins[i] = start[i] + mins[i] - 1;
945 boxmaxs[i] = end[i] + maxs[i] + 1;
949 boxmins[i] = end[i] + mins[i] - 1;
950 boxmaxs[i] = start[i] + maxs[i] + 1;
961 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
966 memset ( &clip, 0, sizeof ( moveclip_t ) );
969 clip.trace = SV_ClipMoveToEntity ( sv.edicts, start, mins, maxs, end );
976 clip.passedict = passedict;
978 if (type == MOVE_MISSILE)
980 for (i=0 ; i<3 ; i++)
988 VectorCopy (mins, clip.mins2);
989 VectorCopy (maxs, clip.maxs2);
992 // create the bounding box of the entire move
993 SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
996 SV_ClipToLinks ( sv_areanodes, &clip );