]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - world.c
oops forgot to commit this piece of the unsuccessful support for floating items
[xonotic/darkplaces.git] / world.c
diff --git a/world.c b/world.c
index 64a762943a029dfabb2437e05432932ba00ad811..d5444cd9f6afb57a86441e4d76f30b8db936446e 100644 (file)
--- a/world.c
+++ b/world.c
@@ -45,9 +45,9 @@ void InsertLinkAfter (link_t *l, link_t *after);
 // (type *)STRUCT_FROM_LINK(link_t *link, type, member)
 // ent = STRUCT_FROM_LINK(link,entity_t,order)
 // FIXME: remove this mess!
-//#define      STRUCT_FROM_LINK(l,t,m) ((t *)((byte *)l - (int)&(((t *)0)->m)))
+//#define      STRUCT_FROM_LINK(l,t,m) ((t *)((qbyte *)l - (int)&(((t *)0)->m)))
 
-#define        EDICT_FROM_AREA(l) ((edict_t *)((byte *)l - (int)&(((edict_t *)0)->area)))
+#define        EDICT_FROM_AREA(l) ((edict_t *)((qbyte *)l - (int)&(((edict_t *)0)->area)))
 
 //============================================================================
 
@@ -81,14 +81,31 @@ void InsertLinkAfter (link_t *l, link_t *after)
 
 typedef struct
 {
-       vec3_t          boxmins, boxmaxs;// enclose the test object along entire move
-       float           *mins, *maxs;   // size of the moving object
-       vec3_t          mins2, maxs2;   // size when clipping against mosnters
-       float           *start, *end;
+       // bounding box of entire move area
+       vec3_t          boxmins, boxmaxs;
+
+       // size of the moving object
+       vec3_t          mins, maxs;
+
+       // size when clipping against monsters
+       vec3_t          mins2, maxs2;
+
+       // size when clipping against brush models
+       vec3_t          hullmins, hullmaxs;
+
+       // start and end origin of move
+       vec3_t          start, end;
+
+       // trace results
        trace_t         trace;
+
+       // type of move (like ignoring monsters, or similar)
        int                     type;
+
+       // the edict that is moving (if any)
        edict_t         *passedict;
-} moveclip_t;
+}
+moveclip_t;
 
 
 /*
@@ -125,19 +142,18 @@ void SV_InitBoxHull (void)
        for (i=0 ; i<6 ; i++)
        {
                box_clipnodes[i].planenum = i;
-               
+
                side = i&1;
-               
+
                box_clipnodes[i].children[side] = CONTENTS_EMPTY;
                if (i != 5)
                        box_clipnodes[i].children[side^1] = i + 1;
                else
                        box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
-               
+
                box_planes[i].type = i>>1;
                box_planes[i].normal[i>>1] = 1;
        }
-       
 }
 
 
@@ -182,7 +198,8 @@ hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
 
 // decide which clipping hull to use, based on the size
        if (ent->v.solid == SOLID_BSP)
-       {       // explicit hulls in the BSP model
+       {
+               // explicit hulls in the BSP model
                if (ent->v.movetype != MOVETYPE_PUSH)
                        Host_Error ("SOLID_BSP without MOVETYPE_PUSH");
 
@@ -199,7 +216,7 @@ hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
 
                VectorSubtract (maxs, mins, size);
                // LordHavoc: FIXME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-               if (sv.worldmodel->ishlbsp)
+               if (model->ishlbsp)
                {
                        if (size[0] < 3)
                                hull = &model->hulls[0]; // 0x0x0
@@ -241,6 +258,39 @@ hull_t *SV_HullForEntity (edict_t *ent, vec3_t mins, vec3_t maxs, vec3_t offset)
        return hull;
 }
 
+void SV_RoundUpToHullSize(vec3_t inmins, vec3_t inmaxs, vec3_t outmins, vec3_t outmaxs)
+{
+       vec3_t size;
+       hull_t *hull;
+
+       VectorSubtract(inmaxs, inmins, size);
+       if (sv.worldmodel->ishlbsp)
+       {
+               if (size[0] < 3)
+                       hull = &sv.worldmodel->hulls[0]; // 0x0x0
+               else if (size[0] <= 32)
+               {
+                       if (size[2] < 54) // pick the nearest of 36 or 72
+                               hull = &sv.worldmodel->hulls[3]; // 32x32x36
+                       else
+                               hull = &sv.worldmodel->hulls[1]; // 32x32x72
+               }
+               else
+                       hull = &sv.worldmodel->hulls[2]; // 64x64x64
+       }
+       else
+       {
+               if (size[0] < 3)
+                       hull = &sv.worldmodel->hulls[0]; // 0x0x0
+               else if (size[0] <= 32)
+                       hull = &sv.worldmodel->hulls[1]; // 32x32x56
+               else
+                       hull = &sv.worldmodel->hulls[2]; // 64x64x88
+       }
+       VectorCopy(inmins, outmins);
+       VectorAdd(inmins, hull->clip_size, outmaxs);
+}
+
 /*
 ===============================================================================
 
@@ -414,11 +464,12 @@ SV_LinkEdict
 */
 void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
 {
+       model_t         *model;
        areanode_t      *node;
 
        if (ent->area.prev)
                SV_UnlinkEdict (ent);   // unlink from old position
-               
+
        if (ent == sv.edicts)
                return;         // don't add the world
 
@@ -427,6 +478,7 @@ void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
 
 // set the abs box
 
+       /*
 // LordHavoc: enabling rotating bmodels
        if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
        {
@@ -439,6 +491,7 @@ void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
                if (max < v)
                        max = v;
                max = sqrt(max);
+       */
                /*
                max = 0;
                for (i=0 ; i<3 ; i++)
@@ -451,6 +504,7 @@ void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
                                max = v;
                }
                */
+       /*
                for (i=0 ; i<3 ; i++)
                {
                        ent->v.absmin[i] = ent->v.origin[i] - max;
@@ -462,6 +516,46 @@ void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
                VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
                VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
        }
+       */
+
+       if (ent->v.solid == SOLID_BSP)
+       {
+               if (ent->v.modelindex < 0 || ent->v.modelindex > MAX_MODELS)
+                       PR_RunError("SOLID_BSP with invalid modelindex!\n");
+               model = sv.models[(int) ent->v.modelindex];
+               if (model != NULL)
+               {
+                       if (model->type != mod_brush)
+                               PR_RunError("SOLID_BSP with non-BSP model\n");
+
+                       if (ent->v.angles[0] || ent->v.angles[2] || ent->v.avelocity[0] || ent->v.avelocity[2])
+                       {
+                               VectorAdd(ent->v.origin, model->rotatedmins, ent->v.absmin);
+                               VectorAdd(ent->v.origin, model->rotatedmaxs, ent->v.absmax);
+                       }
+                       else if (ent->v.angles[1] || ent->v.avelocity[1])
+                       {
+                               VectorAdd(ent->v.origin, model->yawmins, ent->v.absmin);
+                               VectorAdd(ent->v.origin, model->yawmaxs, ent->v.absmax);
+                       }
+                       else
+                       {
+                               VectorAdd(ent->v.origin, model->normalmins, ent->v.absmin);
+                               VectorAdd(ent->v.origin, model->normalmaxs, ent->v.absmax);
+                       }
+               }
+               else
+               {
+                       // SOLID_BSP with no model is valid, mainly because some QC setup code does so temporarily
+                       VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
+                       VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
+               }
+       }
+       else
+       {
+               VectorAdd (ent->v.origin, ent->v.mins, ent->v.absmin);
+               VectorAdd (ent->v.origin, ent->v.maxs, ent->v.absmax);
+       }
 
 //
 // to make items easier to pick up and allow them to be grabbed off
@@ -471,8 +565,10 @@ void SV_LinkEdict (edict_t *ent, qboolean touch_triggers)
        {
                ent->v.absmin[0] -= 15;
                ent->v.absmin[1] -= 15;
+               ent->v.absmin[2] -= 1;
                ent->v.absmax[0] += 15;
                ent->v.absmax[1] += 15;
+               ent->v.absmax[2] += 1;
        }
        else
        {
@@ -757,7 +853,7 @@ trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t max
        VectorSubtract(start, offset, startd);
        VectorSubtract(end, offset, endd);
 
-       // rotate start and end into the models frame of reference
+       // rotate start and end into the model's frame of reference
        if (ent->v.solid == SOLID_BSP && (ent->v.angles[0] || ent->v.angles[1] || ent->v.angles[2]))
        {
                AngleVectorsFLU (ent->v.angles, forward, left, up);
@@ -803,7 +899,7 @@ trace_t SV_ClipMoveToEntity (edict_t *ent, vec3_t start, vec3_t mins, vec3_t max
                VectorAdd (trace.endpos, offset, trace.endpos);
                trace.ent = ent;
        }
-       else if (trace.startsolid)
+       else if (trace.allsolid || trace.startsolid)
                trace.ent = ent;
 
        return trace;
@@ -824,9 +920,9 @@ void SV_ClipToLinks ( areanode_t *node, moveclip_t *clip )
        edict_t         *touch;
        trace_t         trace;
 
+loc0:
        if (clip->trace.allsolid)
                return;
-loc0:
 // touch linked edicts
        for (l = node->solid_edicts.next ; l != &node->solid_edicts ; l = next)
        {
@@ -859,7 +955,7 @@ loc0:
                        if (PROG_TO_EDICT(clip->passedict->v.owner) == touch)
                                continue;       // don't clip against owner
                        // LordHavoc: corpse code
-                       if (clip->passedict->v.solid == SOLID_CORPSE && touch->v.solid == SOLID_SLIDEBOX)
+                       if (clip->passedict->v.solid == SOLID_CORPSE && (touch->v.solid == SOLID_SLIDEBOX || touch->v.solid == SOLID_CORPSE))
                                continue;
                        if (clip->passedict->v.solid == SOLID_SLIDEBOX && touch->v.solid == SOLID_CORPSE)
                                continue;
@@ -868,12 +964,40 @@ loc0:
                // might interact, so do an exact clip
                if ((int)touch->v.flags & FL_MONSTER)
                        trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins2, clip->maxs2, clip->end);
+               else if (touch->v.solid == SOLID_BSP)
+                       trace = SV_ClipMoveToEntity (touch, clip->start, clip->hullmins, clip->hullmaxs, clip->end);
                else
                        trace = SV_ClipMoveToEntity (touch, clip->start, clip->mins, clip->maxs, clip->end);
-               if (trace.allsolid || trace.startsolid || trace.fraction < clip->trace.fraction)
+               // LordHavoc: take the 'best' answers from the new trace and combine with existing data
+               if (trace.allsolid)
+                       clip->trace.allsolid = true;
+               if (trace.startsolid)
                {
-                       trace.ent = touch;
-                       if (clip->trace.startsolid)
+                       clip->trace.startsolid = true;
+                       if (!clip->trace.ent)
+                               clip->trace.ent = trace.ent;
+               }
+               if (trace.inopen)
+                       clip->trace.inopen = true;
+               if (trace.inwater)
+                       clip->trace.inwater = true;
+               if (trace.fraction < clip->trace.fraction)
+               {
+                       clip->trace.fraction = trace.fraction;
+                       VectorCopy(trace.endpos, clip->trace.endpos);
+                       clip->trace.plane = trace.plane;
+                       clip->trace.endcontents = trace.endcontents;
+                       clip->trace.ent = trace.ent;
+               }
+               /*
+               if (trace.allsolid)
+               {
+                       clip->trace = trace;
+                       return;
+               }
+               if (trace.startsolid || trace.fraction < clip->trace.fraction)
+               {
+                       if (clip->trace.startsolid)
                        {
                                clip->trace = trace;
                                clip->trace.startsolid = true;
@@ -881,8 +1005,7 @@ loc0:
                        else
                                clip->trace = trace;
                }
-               if (clip->trace.allsolid)
-                       return;
+               */
        }
 
 // recurse down both sides
@@ -916,8 +1039,8 @@ void SV_MoveBounds (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, vec3_t b
 {
 #if 0
 // debug to test against everything
-boxmins[0] = boxmins[1] = boxmins[2] = -9999;
-boxmaxs[0] = boxmaxs[1] = boxmaxs[2] = 9999;
+boxmins[0] = boxmins[1] = boxmins[2] = -999999999;
+boxmaxs[0] = boxmaxs[1] = boxmaxs[2] =  999999999;
 #else
        int             i;
 
@@ -945,23 +1068,27 @@ SV_Move
 trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, edict_t *passedict)
 {
        moveclip_t      clip;
+       vec3_t          bigmins, bigmaxs;
        int                     i;
 
        memset ( &clip, 0, sizeof ( moveclip_t ) );
 
-       clip.start = start;
-       clip.end = end;
-       clip.mins = mins;
-       clip.maxs = maxs;
+       VectorCopy(start, clip.start);
+       VectorCopy(end, clip.end);
+       VectorCopy(mins, clip.mins);
+       VectorCopy(maxs, clip.maxs);
        clip.type = type;
        clip.passedict = passedict;
 
+       SV_RoundUpToHullSize(clip.mins, clip.maxs, clip.hullmins, clip.hullmaxs);
+
        if (type == MOVE_MISSILE)
        {
+               // LordHavoc: modified this, was = -15, now = clip.mins[i] - 15
                for (i=0 ; i<3 ; i++)
                {
-                       clip.mins2[i] = -15;
-                       clip.maxs2[i] = 15;
+                       clip.mins2[i] = clip.mins[i] - 15;
+                       clip.maxs2[i] = clip.maxs[i] + 15;
                }
        }
        else
@@ -970,14 +1097,21 @@ trace_t SV_Move (vec3_t start, vec3_t mins, vec3_t maxs, vec3_t end, int type, e
                VectorCopy (clip.maxs, clip.maxs2);
        }
 
+       bigmins[0] = min(clip.mins2[0], clip.hullmins[0]);
+       bigmaxs[0] = max(clip.maxs2[0], clip.hullmaxs[0]);
+       bigmins[1] = min(clip.mins2[1], clip.hullmins[1]);
+       bigmaxs[1] = max(clip.maxs2[1], clip.hullmaxs[1]);
+       bigmins[2] = min(clip.mins2[2], clip.hullmins[2]);
+       bigmaxs[2] = max(clip.maxs2[2], clip.hullmaxs[2]);
+
        // clip to world
        clip.trace = SV_ClipMoveToEntity (sv.edicts, start, mins, maxs, end);
 
        // clip to entities
-       if (!clip.trace.allsolid)
+       //if (!clip.trace.allsolid)
        {
                // create the bounding box of the entire move
-               SV_MoveBounds ( start, clip.mins2, clip.maxs2, end, clip.boxmins, clip.boxmaxs );
+               SV_MoveBounds ( start, bigmins, bigmaxs, end, clip.boxmins, clip.boxmaxs );
 
                SV_ClipToLinks ( sv_areanodes, &clip );
        }