relocated the CL_SendMove call to put it at the end of network parsing (and only...
[xonotic/darkplaces.git] / cl_collision.c
index 41734b3..8920a5e 100644 (file)
@@ -29,42 +29,40 @@ typedef struct physentity_s
 physentity_t;
 */
 
-int cl_traceline_startsupercontents;
-
-float CL_TraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int hitbmodels, int *hitent, int hitsupercontentsmask)
+trace_t CL_TraceBox(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitbmodels, int *hitent, int hitsupercontentsmask, qboolean hitplayers)
 {
-       float maxfrac, maxrealfrac;
        int n;
        entity_render_t *ent;
-       float tracemins[3], tracemaxs[3];
-       trace_t trace;
-       float tempnormal[3], starttransformed[3], endtransformed[3];
+       vec3_t tracemins, tracemaxs;
+       trace_t cliptrace, trace;
+       vec3_t starttransformed, endtransformed, starttransformedmins, endtransformedmins, starttransformedmaxs, endtransformedmaxs;
+       vec3_t startmins, startmaxs, endmins, endmaxs, entmins, entmaxs;
+       vec_t *playermins, *playermaxs;
 
-       memset (&trace, 0 , sizeof(trace_t));
-       trace.fraction = 1;
-       trace.realfraction = 1;
-       VectorCopy (end, trace.endpos);
+       VectorAdd(start, mins, startmins);
+       VectorAdd(start, maxs, startmaxs);
+       VectorAdd(end, mins, endmins);
+       VectorAdd(end, maxs, endmaxs);
+
+       memset (&cliptrace, 0 , sizeof(trace_t));
+       cliptrace.fraction = 1;
+       cliptrace.realfraction = 1;
 
-       if (hitent)
-               *hitent = 0;
        Mod_CheckLoaded(cl.worldmodel);
        if (cl.worldmodel && cl.worldmodel->TraceBox)
-               cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, hitsupercontentsmask);
+               cl.worldmodel->TraceBox(cl.worldmodel, 0, &cliptrace, startmins, startmaxs, endmins, endmaxs, hitsupercontentsmask);
 
-       if (normal)
-               VectorCopy(trace.plane.normal, normal);
-       cl_traceline_startsupercontents = trace.startsupercontents;
-       maxfrac = trace.fraction;
-       maxrealfrac = trace.realfraction;
+       if (hitent)
+               *hitent = 0;
 
        if (hitbmodels && cl_num_brushmodel_entities)
        {
-               tracemins[0] = min(start[0], end[0]);
-               tracemaxs[0] = max(start[0], end[0]);
-               tracemins[1] = min(start[1], end[1]);
-               tracemaxs[1] = max(start[1], end[1]);
-               tracemins[2] = min(start[2], end[2]);
-               tracemaxs[2] = max(start[2], end[2]);
+               tracemins[0] = min(start[0], end[0]) + mins[0];
+               tracemaxs[0] = max(start[0], end[0]) + maxs[0];
+               tracemins[1] = min(start[1], end[1]) + mins[1];
+               tracemaxs[1] = max(start[1], end[1]) + maxs[1];
+               tracemins[2] = min(start[2], end[2]) + mins[2];
+               tracemaxs[2] = max(start[2], end[2]) + maxs[2];
 
                // look for embedded bmodels
                for (n = 0;n < cl_num_brushmodel_entities;n++)
@@ -75,31 +73,111 @@ float CL_TraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t n
 
                        Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
                        Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
+                       VectorAdd(starttransformed, mins, starttransformedmins);
+                       VectorAdd(starttransformed, maxs, starttransformedmaxs);
+                       VectorAdd(endtransformed, mins, endtransformedmins);
+                       VectorAdd(endtransformed, maxs, endtransformedmaxs);
+
+                       memset (&trace, 0 , sizeof(trace_t));
+                       trace.fraction = 1;
+                       trace.realfraction = 1;
 
                        if (ent->model && ent->model->TraceBox)
-                               ent->model->TraceBox(ent->model, 0, &trace, starttransformed, starttransformed, endtransformed, endtransformed, hitsupercontentsmask);
+                               ent->model->TraceBox(ent->model, 0, &trace, starttransformedmins, starttransformedmaxs, endtransformedmins, endtransformedmaxs, hitsupercontentsmask);
 
-                       cl_traceline_startsupercontents |= trace.startsupercontents;
-                       if (maxrealfrac > trace.realfraction)
+                       // LordHavoc: take the 'best' answers from the new trace and combine with existing data
+                       if (trace.allsolid)
+                               cliptrace.allsolid = true;
+                       if (trace.startsolid)
                        {
+                               cliptrace.startsolid = true;
+                               if (cliptrace.realfraction == 1)
+                                       if (hitent)
+                                               *hitent = cl_brushmodel_entities[n];
+                       }
+                       // don't set this except on the world, because it can easily confuse
+                       // monsters underwater if there's a bmodel involved in the trace
+                       // (inopen && inwater is how they check water visibility)
+                       //if (trace.inopen)
+                       //      cliptrace.inopen = true;
+                       if (trace.inwater)
+                               cliptrace.inwater = true;
+                       if (trace.realfraction < cliptrace.realfraction)
+                       {
+                               cliptrace.fraction = trace.fraction;
+                               cliptrace.realfraction = trace.realfraction;
+                               cliptrace.plane = trace.plane;
                                if (hitent)
                                        *hitent = cl_brushmodel_entities[n];
-                               maxfrac = trace.fraction;
-                               maxrealfrac = trace.realfraction;
-                               if (normal)
+                               Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
+                       }
+                       cliptrace.startsupercontents |= trace.startsupercontents;
+               }
+       }
+       if (hitplayers)
+       {
+               tracemins[0] = min(start[0], end[0]) + mins[0];
+               tracemaxs[0] = max(start[0], end[0]) + maxs[0];
+               tracemins[1] = min(start[1], end[1]) + mins[1];
+               tracemaxs[1] = max(start[1], end[1]) + maxs[1];
+               tracemins[2] = min(start[2], end[2]) + mins[2];
+               tracemaxs[2] = max(start[2], end[2]) + maxs[2];
+
+               for (n = 1;n < cl.maxclients+1;n++)
+               {
+                       if (n != cl.playerentity)
+                       {
+                               ent = &cl_entities[n].render;
+                               // FIXME: crouch
+                               playermins = cl_playerstandmins;
+                               playermaxs = cl_playerstandmaxs;
+                               VectorAdd(ent->origin, playermins, entmins);
+                               VectorAdd(ent->origin, playermaxs, entmaxs);
+                               if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
+                                       continue;
+
+                               memset (&trace, 0 , sizeof(trace_t));
+                               trace.fraction = 1;
+                               trace.realfraction = 1;
+
+                               Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
+                               Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
+                               Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
+
+                               // LordHavoc: take the 'best' answers from the new trace and combine with existing data
+                               if (trace.allsolid)
+                                       cliptrace.allsolid = true;
+                               if (trace.startsolid)
+                               {
+                                       cliptrace.startsolid = true;
+                                       if (cliptrace.realfraction == 1)
+                                               if (hitent)
+                                                       *hitent = n;
+                               }
+                               // don't set this except on the world, because it can easily confuse
+                               // monsters underwater if there's a bmodel involved in the trace
+                               // (inopen && inwater is how they check water visibility)
+                               //if (trace.inopen)
+                               //      cliptrace.inopen = true;
+                               if (trace.inwater)
+                                       cliptrace.inwater = true;
+                               if (trace.realfraction < cliptrace.realfraction)
                                {
-                                       VectorCopy(trace.plane.normal, tempnormal);
-                                       Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
+                                       cliptrace.fraction = trace.fraction;
+                                       cliptrace.realfraction = trace.realfraction;
+                                       cliptrace.plane = trace.plane;
+                                       if (hitent)
+                                               *hitent = n;
+                                       Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
                                }
+                               cliptrace.startsupercontents |= trace.startsupercontents;
                        }
                }
        }
-       maxfrac = bound(0, maxfrac, 1);
-       maxrealfrac = bound(0, maxrealfrac, 1);
-       //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
-       if (impact)
-               VectorLerp(start, maxfrac, end, impact);
-       return maxfrac;
+       cliptrace.fraction = bound(0, cliptrace.fraction, 1);
+       cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
+       VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
+       return cliptrace;
 }
 
 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
@@ -124,7 +202,7 @@ float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, ve
 
        if (normal)
                VectorCopy(trace.plane.normal, normal);
-       cl_traceline_startsupercontents = trace.startsupercontents;
+       //cl_traceline_startsupercontents = trace.startsupercontents;
        maxfrac = trace.fraction;
        maxrealfrac = trace.realfraction;
 
@@ -156,7 +234,7 @@ float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, ve
                if (ent->model && ent->model->TraceBox)
                        ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
 
-               cl_traceline_startsupercontents |= trace.startsupercontents;
+               //cl_traceline_startsupercontents |= trace.startsupercontents;
                if (maxrealfrac > trace.realfraction)
                {
                        if (hitent)
@@ -187,8 +265,7 @@ void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
 
 int CL_PointQ1Contents(const vec3_t p)
 {
-       CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
-       return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, cl_traceline_startsupercontents);
+       return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents);
        /*
        // FIXME: check multiple brush models
        if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
@@ -199,8 +276,7 @@ int CL_PointQ1Contents(const vec3_t p)
 
 int CL_PointSuperContents(const vec3_t p)
 {
-       CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
-       return cl_traceline_startsupercontents;
+       return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
        /*
        // FIXME: check multiple brush models
        if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)