]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
when loading q1bsp textures, do not allow q3 shader loading to overwrite
[xonotic/darkplaces.git] / model_brush.c
index df5853232c80612826fe1acaeb7be7c0e9750a52..08040c79faa60e4d9a150c30e4bbf17f53894d60 100644 (file)
@@ -1293,6 +1293,188 @@ void Mod_Q1BSP_LightPoint(dp_model_t *model, const vec3_t p, vec3_t ambientcolor
        Mod_Q1BSP_LightPoint_RecursiveBSPNode(model, ambientcolor, diffusecolor, diffusenormal, model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, p[0], p[1], p[2] + 0.125, p[2] - 65536);
 }
 
+static const texture_t *Mod_Q1BSP_TraceLineAgainstSurfacesFindTextureOnNode(RecursiveHullCheckTraceInfo_t *t, const dp_model_t *model, const mnode_t *node, double mid[3])
+{
+       int i;
+       int j;
+       int k;
+       const msurface_t *surface;
+       float normal[3];
+       float v0[3];
+       float v1[3];
+       float edgedir[3];
+       float edgenormal[3];
+       float p[4];
+       float midf;
+       float t1;
+       float t2;
+       VectorCopy(mid, p);
+       p[3] = 1;
+       surface = model->data_surfaces + node->firstsurface;
+       for (i = 0;i < node->numsurfaces;i++, surface++)
+       {
+               // skip surfaces whose bounding box does not include the point
+//             if (!BoxesOverlap(mid, mid, surface->mins, surface->maxs))
+//                     continue;
+               // skip faces with contents we don't care about
+               if (!(t->trace->hitsupercontentsmask & surface->texture->supercontents))
+                       continue;
+               // get the surface normal - since it is flat we know any vertex normal will suffice
+               VectorCopy(model->surfmesh.data_normal3f + 3 * surface->num_firstvertex, normal);
+               // skip backfaces
+               if (DotProduct(t->dist, normal) > 0)
+                       continue;
+               // iterate edges and see if the point is outside one of them
+               for (j = 0, k = surface->num_vertices - 1;j < surface->num_vertices;k = j, j++)
+               {
+                       VectorCopy(model->surfmesh.data_vertex3f + 3 * (surface->num_firstvertex + k), v0);
+                       VectorCopy(model->surfmesh.data_vertex3f + 3 * (surface->num_firstvertex + j), v1);
+                       VectorSubtract(v0, v1, edgedir);
+                       CrossProduct(edgedir, normal, edgenormal);
+                       if (DotProduct(edgenormal, p) > DotProduct(edgenormal, v0))
+                               break;
+               }
+               // if the point is outside one of the edges, it is not within the surface
+               if (j < surface->num_vertices)
+                       continue;
+
+               // we hit a surface, this is the impact point...
+               VectorCopy(normal, t->trace->plane.normal);
+               t->trace->plane.dist = DotProduct(normal, p);
+
+               // calculate the true fraction
+               t1 = DotProduct(t->start, t->trace->plane.normal) - t->trace->plane.dist;
+               t2 = DotProduct(t->end, t->trace->plane.normal) - t->trace->plane.dist;
+               midf = t1 / (t1 - t2);
+               t->trace->realfraction = midf;
+
+               // calculate the return fraction which is nudged off the surface a bit
+               midf = (t1 - DIST_EPSILON) / (t1 - t2);
+               t->trace->fraction = bound(0, midf, 1);
+
+               if (collision_prefernudgedfraction.integer)
+                       t->trace->realfraction = t->trace->fraction;
+
+               t->trace->hittexture = surface->texture->currentframe;
+               t->trace->hitq3surfaceflags = t->trace->hittexture->surfaceflags;
+               t->trace->hitsupercontents = t->trace->hittexture->supercontents;
+               return surface->texture->currentframe;
+       }
+       return NULL;
+}
+
+static int Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(RecursiveHullCheckTraceInfo_t *t, const dp_model_t *model, const mnode_t *node, const double p1[3], const double p2[3])
+{
+       const mplane_t *plane;
+       double t1, t2;
+       int side;
+       double midf, mid[3];
+       const mleaf_t *leaf;
+
+       while (node->plane)
+       {
+               plane = node->plane;
+               if (plane->type < 3)
+               {
+                       t1 = p1[plane->type] - plane->dist;
+                       t2 = p2[plane->type] - plane->dist;
+               }
+               else
+               {
+                       t1 = DotProduct (plane->normal, p1) - plane->dist;
+                       t2 = DotProduct (plane->normal, p2) - plane->dist;
+               }
+               if (t1 < 0)
+               {
+                       if (t2 < 0)
+                       {
+                               node = node->children[1];
+                               continue;
+                       }
+                       side = 1;
+               }
+               else
+               {
+                       if (t2 >= 0)
+                       {
+                               node = node->children[0];
+                               continue;
+                       }
+                       side = 0;
+               }
+
+               // the line intersects, find intersection point
+               // LordHavoc: this uses the original trace for maximum accuracy
+               if (plane->type < 3)
+               {
+                       t1 = t->start[plane->type] - plane->dist;
+                       t2 = t->end[plane->type] - plane->dist;
+               }
+               else
+               {
+                       t1 = DotProduct (plane->normal, t->start) - plane->dist;
+                       t2 = DotProduct (plane->normal, t->end) - plane->dist;
+               }
+       
+               midf = t1 / (t1 - t2);
+               VectorMA(t->start, midf, t->dist, mid);
+
+               // recurse both sides, front side first, return if we hit a surface
+               if (Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(t, model, node->children[side], p1, mid) == HULLCHECKSTATE_DONE)
+                       return HULLCHECKSTATE_DONE;
+
+               // test each surface on the node
+               Mod_Q1BSP_TraceLineAgainstSurfacesFindTextureOnNode(t, model, node, mid);
+               if (t->trace->hittexture)
+                       return HULLCHECKSTATE_DONE;
+
+               // recurse back side
+               return Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(t, model, node->children[side ^ 1], mid, p2);
+       }
+       leaf = (const mleaf_t *)node;
+       side = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, leaf->contents);
+       if (!t->trace->startfound)
+       {
+               t->trace->startfound = true;
+               t->trace->startsupercontents |= side;
+       }
+       if (side & SUPERCONTENTS_LIQUIDSMASK)
+               t->trace->inwater = true;
+       if (side == 0)
+               t->trace->inopen = true;
+       if (side & t->trace->hitsupercontentsmask)
+       {
+               // if the first leaf is solid, set startsolid
+               if (t->trace->allsolid)
+                       t->trace->startsolid = true;
+               return HULLCHECKSTATE_SOLID;
+       }
+       else
+       {
+               t->trace->allsolid = false;
+               return HULLCHECKSTATE_EMPTY;
+       }
+}
+
+static void Mod_Q1BSP_TraceLineAgainstSurfaces(struct model_s *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
+{
+       RecursiveHullCheckTraceInfo_t rhc;
+
+       memset(&rhc, 0, sizeof(rhc));
+       memset(trace, 0, sizeof(trace_t));
+       rhc.trace = trace;
+       rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
+       rhc.trace->fraction = 1;
+       rhc.trace->realfraction = 1;
+       rhc.trace->allsolid = true;
+       rhc.hull = &model->brushq1.hulls[0]; // 0x0x0
+       VectorCopy(start, rhc.start);
+       VectorCopy(end, rhc.end);
+       VectorSubtract(rhc.end, rhc.start, rhc.dist);
+       Mod_Q1BSP_TraceLineAgainstSurfacesRecursiveBSPNode(&rhc, model, model->brush.data_nodes + rhc.hull->firstclipnode, rhc.start, rhc.end);
+       VectorMA(rhc.start, rhc.trace->fraction, rhc.dist, rhc.trace->endpos);
+}
+
 static void Mod_Q1BSP_DecompressVis(const unsigned char *in, const unsigned char *inend, unsigned char *out, unsigned char *outend)
 {
        int c;
@@ -1406,6 +1588,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
        skinframe_t *skinframe;
        miptex_t *dmiptex;
        texture_t *tx, *tx2, *anims[10], *altanims[10];
+       texture_t backuptex;
        dmiptexlump_t *m;
        unsigned char *data, *mtdata;
        const char *s;
@@ -1540,8 +1723,11 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        if (name[j] >= 'A' && name[j] <= 'Z')
                                name[j] += 'a' - 'A';
 
+               // LordHavoc: backup the texture_t because q3 shader loading overwrites it
+               backuptex = loadmodel->data_textures[i];
                if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, false, false, 0))
                        continue;
+               loadmodel->data_textures[i] = backuptex;
 
                tx = loadmodel->data_textures + i;
                strlcpy(tx->name, name, sizeof(tx->name));
@@ -3449,6 +3635,8 @@ static int Mod_Q1BSP_CreateShadowMesh(dp_model_t *mod)
        return numshadowmeshtriangles;
 }
 
+void Mod_CollisionBIH_TraceLineAgainstSurfaces(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask);
+
 void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 {
        int i, j, k;
@@ -3506,9 +3694,10 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 
        mod->soundfromcenter = true;
        mod->TraceBox = Mod_Q1BSP_TraceBox;
-       mod->TraceLine = Mod_Q1BSP_TraceLine;
+       mod->TraceLine = Mod_Q1BSP_TraceLineAgainstSurfaces; // LordHavoc: use the surface-hitting version of TraceLine in all cases
        mod->TracePoint = Mod_Q1BSP_TracePoint;
        mod->PointSuperContents = Mod_Q1BSP_PointSuperContents;
+       mod->TraceLineAgainstSurfaces = Mod_Q1BSP_TraceLineAgainstSurfaces;
        mod->brush.TraceLineOfSight = Mod_Q1BSP_TraceLineOfSight;
        mod->brush.SuperContentsFromNativeContents = Mod_Q1BSP_SuperContentsFromNativeContents;
        mod->brush.NativeContentsFromSuperContents = Mod_Q1BSP_NativeContentsFromSuperContents;
@@ -3770,6 +3959,7 @@ void Mod_Q1BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
                        mod->TraceLine = Mod_CollisionBIH_TraceLine;
                        mod->TraceBox = Mod_CollisionBIH_TraceBox;
                        mod->TraceBrush = Mod_CollisionBIH_TraceBrush;
+                       mod->TraceLineAgainstSurfaces = Mod_CollisionBIH_TraceLineAgainstSurfaces;
                }
 
                // generate VBOs and other shared data before cloning submodels
@@ -5783,418 +5973,177 @@ static qboolean Mod_Q3BSP_TraceLineOfSight(struct model_s *model, const vec3_t s
        }
 }
 
-static void Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const vec3_t point)
+void Mod_CollisionBIH_TracePoint(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask)
 {
+       const bih_t *bih;
        const bih_leaf_t *leaf;
        const bih_node_t *node;
        const colbrushf_t *brush;
        int axis;
-       while (nodenum >= 0)
-       {
-               node = model->collision_bih.nodes + nodenum;
-               axis = node->type - BIH_SPLITX;
-               if (point[axis] <= node->backmax)
-               {
-                       if (point[axis] >= node->frontmin)
-                               Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, node->front, point);
-                       nodenum = node->back;
-               }
-               else if (point[axis] >= node->frontmin)
-                       nodenum = node->front;
-               else // no overlap with either child?  just return
-                       return;
-       }
-       if (!model->collision_bih.leafs)
-               return;
-       leaf = model->collision_bih.leafs + (-1-nodenum);
-       switch(leaf->type)
-       {
-       case BIH_BRUSH:
-               brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
-               Collision_TracePointBrushFloat(trace, point, brush);
-               break;
-       case BIH_COLLISIONTRIANGLE:
-               // collision triangle - skipped because they have no volume
-               break;
-       case BIH_RENDERTRIANGLE:
-               // render triangle - skipped because they have no volume
-               break;
-       }
-}
+       int nodenum;
+       int nodestackpos = 0;
+       int nodestack[1024];
 
-static void Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const vec3_t start, const vec3_t end, const vec3_t linestart, const vec3_t lineend)
-{
-       const bih_leaf_t *leaf;
-       const bih_node_t *node;
-       const colbrushf_t *brush;
-       const int *e;
-       const texture_t *texture;
-       int axis;
-#define BIHLINECLIP
-#ifdef BIHLINECLIP
-       int sideflags;
-       vec_t frontdist1;
-       vec_t frontdist2;
-       vec_t frontfrac;
-       vec_t backdist1;
-       vec_t backdist2;
-       vec_t backfrac;
-       vec3_t clipped, newstart, newend;
-#endif
-       vec3_t segmentmins;
-       vec3_t segmentmaxs;
-       segmentmins[0] = min(start[0], end[0]);
-       segmentmins[1] = min(start[1], end[1]);
-       segmentmins[2] = min(start[2], end[2]);
-       segmentmaxs[0] = max(start[0], end[0]);
-       segmentmaxs[1] = max(start[1], end[1]);
-       segmentmaxs[2] = max(start[2], end[2]);
-       while (nodenum >= 0)
-       {
-               node = model->collision_bih.nodes + nodenum;
-#if 0
-               if (!BoxesOverlap(segmentmins, segmentmaxs, node->mins, node->maxs))
-                       return;
+       memset(trace, 0, sizeof(*trace));
+       trace->fraction = 1;
+       trace->realfraction = 1;
+       trace->hitsupercontentsmask = hitsupercontentsmask;
+
+       bih = &model->collision_bih;
+       nodenum = bih->rootnode;
+       nodestack[nodestackpos++] = nodenum;
+       while (nodestackpos)
+       {
+               nodenum = nodestack[--nodestackpos];
+               node = bih->nodes + nodenum;
+#if 1
+               if (!BoxesOverlap(start, start, node->mins, node->maxs))
+                       continue;
 #endif
-               axis = node->type - BIH_SPLITX;
-#if 0
-               if (segmentmins[axis] <= node->backmax)
+               if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024)
                {
-                       if (segmentmaxs[axis] >= node->frontmin)
-                               Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-                       nodenum = node->back;
+                       axis = node->type - BIH_SPLITX;
+                       if (start[axis] >= node->frontmin)
+                               nodestack[nodestackpos++] = node->front;
+                       if (start[axis] <= node->backmax)
+                               nodestack[nodestackpos++] = node->back;
                }
-               else if (segmentmaxs[axis] >= node->frontmin)
-                       nodenum = node->front;
-               else
-                       return; // trace falls between children
-#else
-               frontdist1 = start[axis] - node->frontmin;
-               frontdist2 = end[axis] - node->frontmin;
-               backdist1 = start[axis] - node->backmax;
-               backdist2 = end[axis] - node->backmax;
-               sideflags = 0;
-               if (frontdist1 < 0)
-                       sideflags |= 1;
-               if (frontdist2 < 0)
-                       sideflags |= 2;
-               if (backdist1 < 0)
-                       sideflags |= 4;
-               if (backdist2 < 0)
-                       sideflags |= 8;
-#if 0
-               if (sideflags & 12)
-               {
-                       if ((sideflags & 3) != 3)
-                               Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-                       nodenum = node->back;
-               }
-               else if ((sideflags & 3) != 3)
-                       nodenum = node->front;
-               else
-                       return; // trace falls between children
-#else
-               switch(sideflags)
+               else if (node->type == BIH_UNORDERED)
                {
-               case 0:
-                       // start end START END
-                       nodenum = node->front;
-                       continue;
-               case 1:
-                       // START end START END
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->front;
-                       break;
-               case 2:
-#ifdef BIHLINECLIP
-                       // start END START END
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->front;
-                       break;
-               case 3:
-                       // START END START END
-                       return; // line falls in gap between children
-               case 4:
-                       // start end start END
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 5:
-                       // START end start END
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped, end, linestart, lineend);
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 6:
-                       // start END start END
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped, linestart, lineend);
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 7:
-                       // START END start END
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newend); end = newend;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 8:
-                       // start end START end
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 9:
-                       // START end START end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped, end, linestart, lineend);
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 10:
-                       // start END START end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped, linestart, lineend);
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 11:
-                       // START END START end
-#ifdef BIHLINECLIP
-                       backfrac = backdist1 / (backdist1 - backdist2);
-                       VectorLerp(start, backfrac, end, newstart); start = newstart;
-                       segmentmins[0] = min(start[0], end[0]);
-                       segmentmins[1] = min(start[1], end[1]);
-                       segmentmins[2] = min(start[2], end[2]);
-                       segmentmaxs[0] = max(start[0], end[0]);
-                       segmentmaxs[1] = max(start[1], end[1]);
-                       segmentmaxs[2] = max(start[2], end[2]);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 12:
-                       // start end start end
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-                       nodenum = node->back;
-                       break;
-               case 13:
-                       // START end start end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, clipped, end, linestart, lineend);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 14:
-                       // start END start end
-#ifdef BIHLINECLIP
-                       frontfrac = frontdist1 / (frontdist1 - frontdist2);
-                       VectorLerp(start, frontfrac, end, clipped);
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, clipped, linestart, lineend);
-#else
-                       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, node->front, start, end, linestart, lineend);
-#endif
-                       nodenum = node->back;
-                       break;
-               case 15:
-                       // START END start end
-                       nodenum = node->back;
-                       continue;
-               }
-#endif
-#endif
-       }
-       if (!model->collision_bih.leafs)
-               return;
-       leaf = model->collision_bih.leafs + (-1-nodenum);
+                       for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++)
+                       {
+                               leaf = bih->leafs + node->children[axis];
 #if 1
-       if (!BoxesOverlap(segmentmins, segmentmaxs, leaf->mins, leaf->maxs))
-               return;
+                               if (!BoxesOverlap(start, start, leaf->mins, leaf->maxs))
+                                       continue;
 #endif
-       switch(leaf->type)
-       {
-       case BIH_BRUSH:
-               brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
-               Collision_TraceLineBrushFloat(trace, linestart, lineend, brush, brush);
-               break;
-       case BIH_COLLISIONTRIANGLE:
-               if (!mod_q3bsp_curves_collisions.integer)
-                       return;
-               e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceLineTriangleFloat(trace, linestart, lineend, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
-       case BIH_RENDERTRIANGLE:
-               e = model->surfmesh.data_element3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceLineTriangleFloat(trace, linestart, lineend, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
+                               switch(leaf->type)
+                               {
+                               case BIH_BRUSH:
+                                       brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
+                                       Collision_TracePointBrushFloat(trace, start, brush);
+                                       break;
+                               case BIH_COLLISIONTRIANGLE:
+                                       // collision triangle - skipped because they have no volume
+                                       break;
+                               case BIH_RENDERTRIANGLE:
+                                       // render triangle - skipped because they have no volume
+                                       break;
+                               }
+                       }
+               }
        }
 }
 
-static void Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace_t *trace, dp_model_t *model, int nodenum, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const vec3_t segmentmins, const vec3_t segmentmaxs)
+void Mod_CollisionBIH_TraceLineShared(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask, const bih_t *bih)
 {
        const bih_leaf_t *leaf;
        const bih_node_t *node;
        const colbrushf_t *brush;
        const int *e;
        const texture_t *texture;
-       int axis;
-       while (nodenum >= 0)
+       vec3_t nodebigmins, nodebigmaxs, nodestart, nodeend, sweepnodemins, sweepnodemaxs;
+       vec_t d1, d2, d3, d4, f, nodestackline[1024][6];
+       int axis, nodenum, nodestackpos = 0, nodestack[1024];
+
+       if (VectorCompare(start, end))
        {
-               node = model->collision_bih.nodes + nodenum;
-               axis = node->type - BIH_SPLITX;
-#if 1
-               if (!BoxesOverlap(segmentmins, segmentmaxs, node->mins, node->maxs))
-                       return;
-#endif
-#if 0
-               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, node->front, thisbrush_start, thisbrush_end, segmentmins, segmentmaxs);
-               nodenum = node->back;
-               continue;
-#endif
-               if (segmentmins[axis] <= node->backmax)
-               {
-                       if (segmentmaxs[axis] >= node->frontmin)
-                               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, node->front, thisbrush_start, thisbrush_end, segmentmins, segmentmaxs);
-                       nodenum = node->back;
-               }
-               else if (segmentmaxs[axis] >= node->frontmin)
-                       nodenum = node->front;
-               else
-                       return; // trace falls between children
-       }
-       if (!model->collision_bih.leafs)
-               return;
-       leaf = model->collision_bih.leafs + (-1-nodenum);
-#if 1
-       if (!BoxesOverlap(segmentmins, segmentmaxs, leaf->mins, leaf->maxs))
+               Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask);
                return;
-#endif
-       switch(leaf->type)
-       {
-       case BIH_BRUSH:
-               brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
-               Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, brush, brush);
-               break;
-       case BIH_COLLISIONTRIANGLE:
-               if (!mod_q3bsp_curves_collisions.integer)
-                       return;
-               e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
-       case BIH_RENDERTRIANGLE:
-               e = model->surfmesh.data_element3i + 3*leaf->itemindex;
-               texture = model->data_textures + leaf->textureindex;
-               Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
-               break;
        }
-}
 
-void Mod_CollisionBIH_TracePoint(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, int hitsupercontentsmask)
-{
+       nodenum = bih->rootnode;
+
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start);
+
+       // push first node
+       nodestackline[nodestackpos][0] = start[0];
+       nodestackline[nodestackpos][1] = start[1];
+       nodestackline[nodestackpos][2] = start[2];
+       nodestackline[nodestackpos][3] = end[0];
+       nodestackline[nodestackpos][4] = end[1];
+       nodestackline[nodestackpos][5] = end[2];
+       nodestack[nodestackpos++] = nodenum;
+       while (nodestackpos)
+       {
+               nodenum = nodestack[--nodestackpos];
+               node = bih->nodes + nodenum;
+               VectorCopy(nodestackline[nodestackpos], nodestart);
+               VectorCopy(nodestackline[nodestackpos] + 3, nodeend);
+               sweepnodemins[0] = min(nodestart[0], nodeend[0]); sweepnodemins[1] = min(nodestart[1], nodeend[1]); sweepnodemins[2] = min(nodestart[2], nodeend[2]); sweepnodemaxs[0] = max(nodestart[0], nodeend[0]); sweepnodemaxs[1] = max(nodestart[1], nodeend[1]); sweepnodemaxs[2] = max(nodestart[2], nodeend[2]);
+               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, node->mins, node->maxs))
+                       continue;
+               if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024)
+               {
+                       // recurse children of the split
+                       axis = node->type - BIH_SPLITX;
+                       d1 = node->backmax - nodestart[axis];
+                       d2 = node->backmax - nodeend[axis];
+                       d3 = nodestart[axis] - node->frontmin;
+                       d4 = nodeend[axis] - node->frontmin;
+                       switch((d1 < 0) | ((d2 < 0) << 1) | ((d3 < 0) << 2) | ((d4 < 0) << 3))
+                       {
+                       case  0: /* >>>> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  1: /* <>>> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  2: /* ><>> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  3: /* <<>> */                                                                                                                                                                                                                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  4: /* >><> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  5: /* <><> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  6: /* ><<> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  7: /* <<<> */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  8: /* >>>< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  9: /* <>>< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 10: /* ><>< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 11: /* <<>< */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 12: /* >><< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 13: /* <><< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 14: /* ><<< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 15: /* <<<< */                                                                                                                                                                                                                                                                                                                                                                                                   break;
+                       }
+               }
+               else if (node->type == BIH_UNORDERED)
+               {
+                       // calculate sweep bounds for this node
+                       // copy node bounds into local variables
+                       VectorCopy(node->mins, nodebigmins);
+                       VectorCopy(node->maxs, nodebigmaxs);
+                       // clip line to this node bounds
+                       axis = 0; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 1; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 2; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       // some of the line intersected the enlarged node box
+                       // calculate sweep bounds for this node
+                       sweepnodemins[0] = min(nodestart[0], nodeend[0]); sweepnodemins[1] = min(nodestart[1], nodeend[1]); sweepnodemins[2] = min(nodestart[2], nodeend[2]); sweepnodemaxs[0] = max(nodestart[0], nodeend[0]); sweepnodemaxs[1] = max(nodestart[1], nodeend[1]); sweepnodemaxs[2] = max(nodestart[2], nodeend[2]);
+                       for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++)
+                       {
+                               leaf = bih->leafs + node->children[axis];
+                               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, leaf->mins, leaf->maxs))
+                                       continue;
+                               switch(leaf->type)
+                               {
+                               case BIH_BRUSH:
+                                       brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
+                                       Collision_TraceLineBrushFloat(trace, start, end, brush, brush);
+                                       break;
+                               case BIH_COLLISIONTRIANGLE:
+                                       if (!mod_q3bsp_curves_collisions.integer)
+                                               continue;
+                                       e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceLineTriangleFloat(trace, start, end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               case BIH_RENDERTRIANGLE:
+                                       e = model->surfmesh.data_element3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceLineTriangleFloat(trace, start, end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               }
+                       }
+               }
+       }
 }
 
 void Mod_CollisionBIH_TraceLine(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
@@ -6204,78 +6153,156 @@ void Mod_CollisionBIH_TraceLine(dp_model_t *model, const frameblend_t *frameblen
                Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask);
                return;
        }
-
-       memset(trace, 0, sizeof(*trace));
-       trace->fraction = 1;
-       trace->realfraction = 1;
-       trace->hitsupercontentsmask = hitsupercontentsmask;
-       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end);
+       Mod_CollisionBIH_TraceLineShared(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, &model->collision_bih);
 }
 
-void Mod_CollisionBIH_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+void Mod_CollisionBIH_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *thisbrush_start, colbrushf_t *thisbrush_end, int hitsupercontentsmask)
 {
-       float segmentmins[3], segmentmaxs[3];
-       colboxbrushf_t thisbrush_start, thisbrush_end;
-       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
+       const bih_t *bih;
+       const bih_leaf_t *leaf;
+       const bih_node_t *node;
+       const colbrushf_t *brush;
+       const int *e;
+       const texture_t *texture;
+       vec3_t start, end, startmins, startmaxs, endmins, endmaxs, mins, maxs;
+       vec3_t nodebigmins, nodebigmaxs, nodestart, nodeend, sweepnodemins, sweepnodemaxs;
+       vec_t d1, d2, d3, d4, f, nodestackline[1024][6];
+       int axis, nodenum, nodestackpos = 0, nodestack[1024];
 
-       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxmins, boxmaxs))
+       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(thisbrush_start->mins, thisbrush_start->maxs) && VectorCompare(thisbrush_end->mins, thisbrush_end->maxs))
        {
-               vec3_t shiftstart, shiftend;
-               VectorAdd(start, boxmins, shiftstart);
-               VectorAdd(end, boxmins, shiftend);
-               if (VectorCompare(start, end))
-                       Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, shiftstart, hitsupercontentsmask);
+               if (VectorCompare(thisbrush_start->mins, thisbrush_end->mins))
+                       Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, thisbrush_start->mins, hitsupercontentsmask);
                else
-                       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask);
+                       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, thisbrush_start->mins, thisbrush_end->mins, hitsupercontentsmask);
                return;
        }
 
+       bih = &model->collision_bih;
+       nodenum = bih->rootnode;
+
        // box trace, performed as brush trace
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
-       segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
-       segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1;
-       segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1;
-       segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1;
-       segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1;
-       segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1;
+
+       // calculate tracebox-like parameters for efficient culling
+       VectorMAM(0.5f, thisbrush_start->mins, 0.5f, thisbrush_start->maxs, start);
+       VectorMAM(0.5f, thisbrush_end->mins, 0.5f, thisbrush_end->maxs, end);
+       VectorSubtract(thisbrush_start->mins, start, startmins);
+       VectorSubtract(thisbrush_start->maxs, start, startmaxs);
+       VectorSubtract(thisbrush_end->mins, end, endmins);
+       VectorSubtract(thisbrush_end->maxs, end, endmaxs);
+       mins[0] = min(startmins[0], endmins[0]);
+       mins[1] = min(startmins[1], endmins[1]);
+       mins[2] = min(startmins[2], endmins[2]);
+       maxs[0] = max(startmaxs[0], endmaxs[0]);
+       maxs[1] = max(startmaxs[1], endmaxs[1]);
+       maxs[2] = max(startmaxs[2], endmaxs[2]);
+
+       // push first node
+       nodestackline[nodestackpos][0] = start[0];
+       nodestackline[nodestackpos][1] = start[1];
+       nodestackline[nodestackpos][2] = start[2];
+       nodestackline[nodestackpos][3] = end[0];
+       nodestackline[nodestackpos][4] = end[1];
+       nodestackline[nodestackpos][5] = end[2];
+       nodestack[nodestackpos++] = nodenum;
+       while (nodestackpos)
+       {
+               nodenum = nodestack[--nodestackpos];
+               node = bih->nodes + nodenum;
+               VectorCopy(nodestackline[nodestackpos], nodestart);
+               VectorCopy(nodestackline[nodestackpos] + 3, nodeend);
+               sweepnodemins[0] = min(nodestart[0], nodeend[0]) + mins[0]; sweepnodemins[1] = min(nodestart[1], nodeend[1]) + mins[1]; sweepnodemins[2] = min(nodestart[2], nodeend[2]) + mins[2]; sweepnodemaxs[0] = max(nodestart[0], nodeend[0]) + maxs[0]; sweepnodemaxs[1] = max(nodestart[1], nodeend[1]) + maxs[1]; sweepnodemaxs[2] = max(nodestart[2], nodeend[2]) + maxs[2];
+               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, node->mins, node->maxs))
+                       continue;
+               if (node->type <= BIH_SPLITZ && nodestackpos+2 <= 1024)
+               {
+                       // recurse children of the split
+                       axis = node->type - BIH_SPLITX;
+                       d1 = node->backmax - nodestart[axis] - mins[axis];
+                       d2 = node->backmax - nodeend[axis] - mins[axis];
+                       d3 = nodestart[axis] - node->frontmin + maxs[axis];
+                       d4 = nodeend[axis] - node->frontmin + maxs[axis];
+                       switch((d1 < 0) | ((d2 < 0) << 1) | ((d3 < 0) << 2) | ((d4 < 0) << 3))
+                       {
+                       case  0: /* >>>> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  1: /* <>>> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  2: /* ><>> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  3: /* <<>> */                                                                                                                                                                                                                      VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  4: /* >><> */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  5: /* <><> */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  6: /* ><<> */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  7: /* <<<> */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  8: /* >>>< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case  9: /* <>>< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 10: /* ><>< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 11: /* <<>< */                                                                                                                                                                                                  f = d3 / (d3 - d4); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->front; break;
+                       case 12: /* >><< */                     VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 13: /* <><< */ f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos]); VectorCopy(              nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 14: /* ><<< */ f = d1 / (d1 - d2); VectorCopy(nodestart,             nodestackline[nodestackpos]); VectorLerp(nodestart, f, nodeend, nodestackline[nodestackpos] + 3); nodestack[nodestackpos++] = node->back;                                                                                                                                                                                                   break;
+                       case 15: /* <<<< */                                                                                                                                                                                                                                                                                                                                                                                                   break;
+                       }
+               }
+               else if (node->type == BIH_UNORDERED)
+               {
+                       // calculate sweep bounds for this node
+                       // copy node bounds into local variables and expand to get Minkowski Sum of the two shapes
+                       VectorSubtract(node->mins, maxs, nodebigmins);
+                       VectorSubtract(node->maxs, mins, nodebigmaxs);
+                       // clip line to this node bounds
+                       axis = 0; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 1; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       axis = 2; d1 = nodestart[axis] - nodebigmins[axis]; d2 = nodeend[axis] - nodebigmins[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); } d1 = nodebigmaxs[axis] - nodestart[axis]; d2 = nodebigmaxs[axis] - nodeend[axis]; if (d1 < 0) { if (d2 < 0) continue; f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodestart); } else if (d2 < 0) { f = d1 / (d1 - d2); VectorLerp(nodestart, f, nodeend, nodeend); }
+                       // some of the line intersected the enlarged node box
+                       // calculate sweep bounds for this node
+                       sweepnodemins[0] = min(nodestart[0], nodeend[0]) + mins[0]; sweepnodemins[1] = min(nodestart[1], nodeend[1]) + mins[1]; sweepnodemins[2] = min(nodestart[2], nodeend[2]) + mins[2]; sweepnodemaxs[0] = max(nodestart[0], nodeend[0]) + maxs[0]; sweepnodemaxs[1] = max(nodestart[1], nodeend[1]) + maxs[1]; sweepnodemaxs[2] = max(nodestart[2], nodeend[2]) + maxs[2];
+                       for (axis = 0;axis < BIH_MAXUNORDEREDCHILDREN && node->children[axis] >= 0;axis++)
+                       {
+                               leaf = bih->leafs + node->children[axis];
+                               if (!BoxesOverlap(sweepnodemins, sweepnodemaxs, leaf->mins, leaf->maxs))
+                                       continue;
+                               switch(leaf->type)
+                               {
+                               case BIH_BRUSH:
+                                       brush = model->brush.data_brushes[leaf->itemindex].colbrushf;
+                                       Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, brush, brush);
+                                       break;
+                               case BIH_COLLISIONTRIANGLE:
+                                       if (!mod_q3bsp_curves_collisions.integer)
+                                               continue;
+                                       e = model->brush.data_collisionelement3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->brush.data_collisionvertex3f + e[0] * 3, model->brush.data_collisionvertex3f + e[1] * 3, model->brush.data_collisionvertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               case BIH_RENDERTRIANGLE:
+                                       e = model->surfmesh.data_element3i + 3*leaf->itemindex;
+                                       texture = model->data_textures + leaf->textureindex;
+                                       Collision_TraceBrushTriangleFloat(trace, thisbrush_start, thisbrush_end, model->surfmesh.data_vertex3f + e[0] * 3, model->surfmesh.data_vertex3f + e[1] * 3, model->surfmesh.data_vertex3f + e[2] * 3, texture->supercontents, texture->surfaceflags, texture);
+                                       break;
+                               }
+                       }
+               }
+       }
+}
+
+void Mod_CollisionBIH_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+{
+       colboxbrushf_t thisbrush_start, thisbrush_end;
+       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
+
+       // box trace, performed as brush trace
        VectorAdd(start, boxmins, boxstartmins);
        VectorAdd(start, boxmaxs, boxstartmaxs);
        VectorAdd(end, boxmins, boxendmins);
        VectorAdd(end, boxmaxs, boxendmaxs);
        Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
        Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
-       Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, &thisbrush_start.brush, &thisbrush_end.brush, segmentmins, segmentmaxs);
+       Mod_CollisionBIH_TraceBrush(model, frameblend, skeleton, trace, &thisbrush_start.brush, &thisbrush_end.brush, hitsupercontentsmask);
 }
 
-void Mod_CollisionBIH_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *start, colbrushf_t *end, int hitsupercontentsmask)
-{
-       float segmentmins[3], segmentmaxs[3];
-
-       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(start->mins, start->maxs) && VectorCompare(end->mins, end->maxs))
-       {
-               if (VectorCompare(start->mins, end->mins))
-                       Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start->mins, hitsupercontentsmask);
-               else
-                       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start->mins, end->mins, hitsupercontentsmask);
-               return;
-       }
-
-       // box trace, performed as brush trace
-       memset(trace, 0, sizeof(*trace));
-       trace->fraction = 1;
-       trace->realfraction = 1;
-       trace->hitsupercontentsmask = hitsupercontentsmask;
-       segmentmins[0] = min(start->mins[0], end->mins[0]);
-       segmentmins[1] = min(start->mins[1], end->mins[1]);
-       segmentmins[2] = min(start->mins[2], end->mins[2]);
-       segmentmaxs[0] = max(start->maxs[0], end->maxs[0]);
-       segmentmaxs[1] = max(start->maxs[1], end->maxs[1]);
-       segmentmaxs[2] = max(start->maxs[2], end->maxs[2]);
-       Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, segmentmins, segmentmaxs);
-}
 
 int Mod_CollisionBIH_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
 {
@@ -6297,7 +6324,7 @@ void Mod_CollisionBIH_TracePoint_Mesh(dp_model_t *model, const frameblend_t *fra
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
 #if 0
-       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end);
+       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        hitsupercontents = trace->hitsupercontents;
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
@@ -6318,7 +6345,7 @@ int Mod_CollisionBIH_PointSuperContents_Mesh(struct model_s *model, int frame, c
        trace.fraction = 1;
        trace.realfraction = 1;
        trace.hitsupercontentsmask = 0;
-       Mod_CollisionBIH_TraceLine_RecursiveBIHNode(&trace, model, model->collision_bih.rootnode, start, end, start, end);
+       Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        return trace.hitsupercontents;
 #else
        return 0;
@@ -6549,7 +6576,7 @@ static void Mod_Q3BSP_TracePoint(dp_model_t *model, const frameblend_t *frameble
        trace->realfraction = 1;
        trace->hitsupercontentsmask = hitsupercontentsmask;
        if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TracePoint_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start);
+               Mod_CollisionBIH_TracePoint(model, frameblend, skeleton, trace, start, hitsupercontentsmask);
        else if (model->brush.submodel)
        {
                for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
@@ -6584,7 +6611,7 @@ static void Mod_Q3BSP_TraceLine(dp_model_t *model, const frameblend_t *frameblen
        segmentmaxs[1] = max(start[1], end[1]) + 1;
        segmentmaxs[2] = max(start[2], end[2]) + 1;
        if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TraceLine_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, start, end);
+               Mod_CollisionBIH_TraceLine(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        else if (model->brush.submodel)
        {
                for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
@@ -6599,60 +6626,6 @@ static void Mod_Q3BSP_TraceLine(dp_model_t *model, const frameblend_t *frameblen
                Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace, model, model->brush.data_nodes, start, end, 0, 1, start, end, ++markframe, segmentmins, segmentmaxs);
 }
 
-static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
-{
-       int i;
-       float segmentmins[3], segmentmaxs[3];
-       msurface_t *surface;
-       q3mbrush_t *brush;
-       colboxbrushf_t thisbrush_start, thisbrush_end;
-       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
-
-       if (mod_q3bsp_optimizedtraceline.integer && VectorCompare(boxmins, boxmaxs))
-       {
-               vec3_t shiftstart, shiftend;
-               VectorAdd(start, boxmins, shiftstart);
-               VectorAdd(end, boxmins, shiftend);
-               if (VectorCompare(start, end))
-                       Mod_Q3BSP_TracePoint(model, frameblend, skeleton, trace, shiftstart, hitsupercontentsmask);
-               else
-                       Mod_Q3BSP_TraceLine(model, frameblend, skeleton, trace, shiftstart, shiftend, hitsupercontentsmask);
-               return;
-       }
-
-       // box trace, performed as brush trace
-       memset(trace, 0, sizeof(*trace));
-       trace->fraction = 1;
-       trace->realfraction = 1;
-       trace->hitsupercontentsmask = hitsupercontentsmask;
-       segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
-       segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1;
-       segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1;
-       segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1;
-       segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1;
-       segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1;
-       VectorAdd(start, boxmins, boxstartmins);
-       VectorAdd(start, boxmaxs, boxstartmaxs);
-       VectorAdd(end, boxmins, boxendmins);
-       VectorAdd(end, boxmaxs, boxendmaxs);
-       Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
-       Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
-       if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, &thisbrush_start.brush, &thisbrush_end.brush, segmentmins, segmentmaxs);
-       else if (model->brush.submodel)
-       {
-               for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
-                       if (brush->colbrushf && BoxesOverlap(segmentmins, segmentmaxs, brush->colbrushf->mins, brush->colbrushf->maxs))
-                               Collision_TraceBrushBrushFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, brush->colbrushf, brush->colbrushf);
-               if (mod_q3bsp_curves_collisions.integer)
-                       for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
-                               if (surface->num_collisiontriangles && BoxesOverlap(segmentmins, segmentmaxs, surface->mins, surface->maxs))
-                                       Collision_TraceBrushTriangleMeshFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, surface->num_collisiontriangles, surface->deprecatedq3data_collisionelement3i, surface->deprecatedq3data_collisionvertex3f, surface->deprecatedq3num_collisionbboxstride, surface->deprecatedq3data_collisionbbox6f, surface->texture->supercontents, surface->texture->surfaceflags, surface->texture, segmentmins, segmentmaxs);
-       }
-       else
-               Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model, model->brush.data_nodes, &thisbrush_start.brush, &thisbrush_end.brush, ++markframe, segmentmins, segmentmaxs);
-}
-
 void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, colbrushf_t *start, colbrushf_t *end, int hitsupercontentsmask)
 {
        float segmentmins[3], segmentmaxs[3];
@@ -6681,7 +6654,7 @@ void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, con
        segmentmaxs[1] = max(start->maxs[1], end->maxs[1]);
        segmentmaxs[2] = max(start->maxs[2], end->maxs[2]);
        if (mod_collision_bih.integer)
-               Mod_CollisionBIH_TraceBrush_RecursiveBIHNode(trace, model, model->collision_bih.rootnode, start, end, segmentmins, segmentmaxs);
+               Mod_CollisionBIH_TraceBrush(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask);
        else if (model->brush.submodel)
        {
                for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
@@ -6696,6 +6669,21 @@ void Mod_Q3BSP_TraceBrush(dp_model_t *model, const frameblend_t *frameblend, con
                Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace, model, model->brush.data_nodes, start, end, ++markframe, segmentmins, segmentmaxs);
 }
 
+static void Mod_Q3BSP_TraceBox(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
+{
+       colboxbrushf_t thisbrush_start, thisbrush_end;
+       vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
+
+       // box trace, performed as brush trace
+       VectorAdd(start, boxmins, boxstartmins);
+       VectorAdd(start, boxmaxs, boxstartmaxs);
+       VectorAdd(end, boxmins, boxendmins);
+       VectorAdd(end, boxmaxs, boxendmaxs);
+       Collision_BrushForBox(&thisbrush_start, boxstartmins, boxstartmaxs, 0, 0, NULL);
+       Collision_BrushForBox(&thisbrush_end, boxendmins, boxendmaxs, 0, 0, NULL);
+       Mod_Q3BSP_TraceBrush(model, frameblend, skeleton, trace, &thisbrush_start.brush, &thisbrush_end.brush, hitsupercontentsmask);
+}
+
 static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
 {
        int i;
@@ -6734,6 +6722,12 @@ static int Mod_Q3BSP_PointSuperContents(struct model_s *model, int frame, const
        return supercontents;
 }
 
+void Mod_CollisionBIH_TraceLineAgainstSurfaces(dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, trace_t *trace, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
+{
+       Mod_CollisionBIH_TraceLineShared(model, frameblend, skeleton, trace, start, end, hitsupercontentsmask, &model->render_bih);
+}
+
+
 bih_t *Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces, bih_t *out)
 {
        int j;
@@ -6848,7 +6842,7 @@ bih_t *Mod_MakeCollisionBIH(dp_model_t *model, qboolean userendersurfaces, bih_t
        }
 
        // allocate buffers for the produced and temporary data
-       bihmaxnodes = bihnumleafs - 1;
+       bihmaxnodes = bihnumleafs + 1;
        bihnodes = (bih_node_t *)Mem_Alloc(loadmodel->mempool, sizeof(bih_node_t) * bihmaxnodes);
        temp_leafsort = (int *)Mem_Alloc(loadmodel->mempool, sizeof(int) * bihnumleafs * 2);
        temp_leafsortscratch = temp_leafsort + bihnumleafs;
@@ -6968,6 +6962,7 @@ void Mod_Q3BSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
        mod->TraceLine = Mod_Q3BSP_TraceLine;
        mod->TracePoint = Mod_Q3BSP_TracePoint;
        mod->PointSuperContents = Mod_Q3BSP_PointSuperContents;
+       mod->TraceLineAgainstSurfaces = Mod_CollisionBIH_TraceLine;
        mod->brush.TraceLineOfSight = Mod_Q3BSP_TraceLineOfSight;
        mod->brush.SuperContentsFromNativeContents = Mod_Q3BSP_SuperContentsFromNativeContents;
        mod->brush.NativeContentsFromSuperContents = Mod_Q3BSP_NativeContentsFromSuperContents;
@@ -7266,6 +7261,7 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceBrush = Mod_CollisionBIH_TraceBrush;
        loadmodel->TraceLine = Mod_CollisionBIH_TraceLine;
        loadmodel->TracePoint = Mod_CollisionBIH_TracePoint_Mesh;
+       loadmodel->TraceLineAgainstSurfaces = Mod_CollisionBIH_TraceLine;
        loadmodel->PointSuperContents = Mod_CollisionBIH_PointSuperContents_Mesh;
        loadmodel->brush.TraceLineOfSight = NULL;
        loadmodel->brush.SuperContentsFromNativeContents = NULL;
@@ -8256,6 +8252,7 @@ void Mod_OBJ_Load(dp_model_t *mod, void *buffer, void *bufferend)
        loadmodel->TraceLine = Mod_OBJ_TraceLine;
        loadmodel->TracePoint = Mod_OBJ_TracePoint;
        loadmodel->PointSuperContents = Mod_OBJ_PointSuperContents;
+       loadmodel->TraceLineAgainstSurfaces = Mod_OBJ_TraceLineAgainstSurfaces;
        loadmodel->brush.TraceLineOfSight = Mod_OBJ_TraceLineOfSight;
        loadmodel->brush.SuperContentsFromNativeContents = Mod_OBJ_SuperContentsFromNativeContents;
        loadmodel->brush.NativeContentsFromSuperContents = Mod_OBJ_NativeContentsFromSuperContents;