]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_collision.c
more q3bsp work (and no it still doesn't work right)
[xonotic/darkplaces.git] / cl_collision.c
index ea667b654e2d8106558c00f1c481537762776499..657a0ff44496149f39ddde6b7850342476d9d431 100644 (file)
@@ -1,5 +1,6 @@
 
 #include "quakedef.h"
+#include "cl_collision.h"
 
 /*
 // not yet used
@@ -28,70 +29,35 @@ typedef struct physentity_s
 physentity_t;
 */
 
-static entity_render_t *traceline_entity[MAX_EDICTS];
-static int traceline_entities;
+int cl_traceline_startsupercontents;
 
-// builds list of entities for TraceLine to check later
-void CL_TraceLine_ScanForBModels(void)
+float CL_TraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int hitbmodels, entity_render_t **hitent, int hitsupercontentsmask)
 {
-       int i;
+       float maxfrac;
+       int n;
        entity_render_t *ent;
-       model_t *model;
-       traceline_entities = 0;
-       for (i = 1;i < MAX_EDICTS;i++)
-       {
-               ent = &cl_entities[i].render;
-               model = ent->model;
-               // look for embedded brush models only
-               if (model && model->name[0] == '*')
-               {
-                       if (model->type == mod_brush)
-                       {
-                               traceline_entity[traceline_entities++] = ent;
-                               if (ent->angles[0] || ent->angles[2])
-                               {
-                                       // pitch or roll
-                                       VectorAdd(ent->origin, model->rotatedmins, ent->mins);
-                                       VectorAdd(ent->origin, model->rotatedmaxs, ent->maxs);
-                               }
-                               else if (ent->angles[1])
-                               {
-                                       // yaw
-                                       VectorAdd(ent->origin, model->yawmins, ent->mins);
-                                       VectorAdd(ent->origin, model->yawmaxs, ent->maxs);
-                               }
-                               else
-                               {
-                                       VectorAdd(ent->origin, model->normalmins, ent->mins);
-                                       VectorAdd(ent->origin, model->normalmaxs, ent->maxs);
-                               }
-                       }
-               }
-       }
-}
-
-int cl_traceline_endcontents;
-
-float CL_TraceLine (vec3_t start, vec3_t end, vec3_t impact, vec3_t normal, int contents, int hitbmodels)
-{
-       double maxfrac;
+       float tracemins[3], tracemaxs[3];
        trace_t trace;
+       matrix4x4_t matrix, imatrix;
+       float tempnormal[3], starttransformed[3], endtransformed[3];
 
+       if (hitent)
+               *hitent = NULL;
        Mod_CheckLoaded(cl.worldmodel);
-       Collision_ClipTrace(&trace, NULL, cl.worldmodel, vec3_origin, vec3_origin, vec3_origin, vec3_origin, start, vec3_origin, vec3_origin, end);
+       if (cl.worldmodel && cl.worldmodel->brush.TraceBox)
+               cl.worldmodel->brush.TraceBox(cl.worldmodel, &trace, start, start, end, end, hitsupercontentsmask);
 
        if (impact)
-               VectorCopy (trace.endpos, impact);
+               VectorLerp(start, trace.fraction, end, impact);
        if (normal)
-               VectorCopy (trace.plane.normal, normal);
-       cl_traceline_endcontents = trace.endcontents;
+               VectorCopy(trace.plane.normal, normal);
+       cl_traceline_startsupercontents = trace.startsupercontents;
        maxfrac = trace.fraction;
+       if (hitent && trace.fraction < 1)
+               *hitent = &cl_entities[0].render;
 
-       if (hitbmodels && traceline_entities)
+       if (hitbmodels && cl_num_brushmodel_entities)
        {
-               int n;
-               entity_render_t *ent;
-               double tracemins[3], tracemaxs[3];
                tracemins[0] = min(start[0], end[0]);
                tracemaxs[0] = max(start[0], end[0]);
                tracemins[1] = min(start[1], end[1]);
@@ -100,27 +66,73 @@ float CL_TraceLine (vec3_t start, vec3_t end, vec3_t impact, vec3_t normal, int
                tracemaxs[2] = max(start[2], end[2]);
 
                // look for embedded bmodels
-               for (n = 0;n < traceline_entities;n++)
+               for (n = 0;n < cl_num_brushmodel_entities;n++)
                {
-                       ent = traceline_entity[n];
+                       ent = cl_brushmodel_entities[n];
                        if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
                         || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
                         || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
-                               continue;
+                               continue;
 
-                       Collision_ClipTrace(&trace, ent, ent->model, ent->origin, ent->angles, ent->mins, ent->maxs, start, vec3_origin, vec3_origin, end);
+                       Matrix4x4_CreateFromQuakeEntity(&matrix, ent->origin[0], ent->origin[1], ent->origin[2], ent->angles[0], ent->angles[1], ent->angles[2], 1);
+                       Matrix4x4_Invert_Simple(&imatrix, &matrix);
+                       Matrix4x4_Transform(&imatrix, start, starttransformed);
+                       Matrix4x4_Transform(&imatrix, end, endtransformed);
 
-                       if (trace.allsolid || trace.startsolid || trace.fraction < maxfrac)
+                       if (ent->model && ent->model->brush.TraceBox)
+                               ent->model->brush.TraceBox(ent->model, &trace, starttransformed, starttransformed, endtransformed, endtransformed, hitsupercontentsmask);
+
+                       if (trace.allsolid || trace.startsolid || maxfrac > trace.fraction)
                        {
-                               maxfrac = trace.fraction;
-                               if (impact)
-                                       VectorCopy(trace.endpos, impact);
-                               if (normal)
-                                       VectorCopy(trace.plane.normal, normal);
-                               cl_traceline_endcontents = trace.endcontents;
+                               cl_traceline_startsupercontents = trace.startsupercontents;
+                               if (hitent)
+                                       *hitent = ent;
+                               if (maxfrac > trace.fraction)
+                               {
+                                       maxfrac = trace.fraction;
+                                       if (impact)
+                                               VectorLerp(start, trace.fraction, end, impact);
+                                       if (normal)
+                                       {
+                                               VectorCopy(trace.plane.normal, tempnormal);
+                                               Matrix4x4_Transform3x3(&matrix, tempnormal, normal);
+                                       }
+                               }
                        }
                }
        }
+       if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __LINE__, __FILE__);
        return maxfrac;
 }
 
+void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
+{
+       // FIXME: check multiple brush models
+       if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
+               cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, 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);
+       /*
+       // FIXME: check multiple brush models
+       if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
+               return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
+       return 0;
+       */
+}
+
+int CL_PointSuperContents(const vec3_t p)
+{
+       CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
+       return cl_traceline_startsupercontents;
+       /*
+       // FIXME: check multiple brush models
+       if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
+               return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
+       return 0;
+       */
+}
+