+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);
+}
+