// returns a surfID
static SurfaceHandle_t FindIntersectionSurfaceAtNode( mnode_t *node, float t, 
	Vector& c, LightVecState_t& state )
{
	SurfaceHandle_t surfID = SurfaceHandleFromIndex( node->firstsurface );
	for (int i=0 ; i<node->numsurfaces ; ++i, ++surfID)
	{
		// Don't immediately return when we hit sky; 
		// we may actually hit another surface
		if (MSurf_Flags( surfID ) & SURFDRAW_SKY)
		{
			state.m_nSkySurfID = surfID;
			continue;
		}

		// Don't let water surfaces affect us
		if (MSurf_Flags( surfID ) & SURFDRAW_WATERSURFACE)
			continue;

		// Check this surface to see if there's an intersection
		if (FindIntersectionAtSurface( surfID, t, c, state ))
		{
			return surfID;
		}
	}

	return SURFACE_HANDLE_INVALID;
}
// returns surfID
static int FASTCALL FindIntersectionSurfaceAtLeaf( mleaf_t *pLeaf, 
					float start, float end, Vector& c, LightVecState_t& state )
{
	Vector pt;
	int closestSurfID = -1;

	// Adds displacements in the leaf to a list of displacements to test at the end
	AddDisplacementsInLeafToTestList( pLeaf, state );

	// Add non-displacement surfaces
	// Since there's no BSP tree here, we gotta test *all* surfaces! (blech)
	for ( int i = 0; i < pLeaf->nummarksurfaces; i++ )
	{
		int surfID = host_state.worldmodel->brush.marksurfaces[pLeaf->firstmarksurface + i];
		ASSERT_SURF_VALID( surfID );

		// Don't add surfaces that have displacement; they are handled above
		// In fact, don't even set the vis frame; we need it unset for translucent
		// displacement code
		if ( SurfaceHasDispInfo(surfID) )
			continue;

		if ( MSurf_Flags( surfID ) & (SURFDRAW_NODE | SURFDRAW_NODRAW | SURFDRAW_WATERSURFACE) )
			continue;

		cplane_t* pPlane = &MSurf_Plane( surfID );

		// Backface cull...
		if (DotProduct( pPlane->normal, state.m_Ray.m_Delta ) > 0.f)
			continue;

		float startDotN = DotProduct( state.m_Ray.m_Start, pPlane->normal );
		float deltaDotN = DotProduct( state.m_Ray.m_Delta, pPlane->normal );

		float front = startDotN + start * deltaDotN - pPlane->dist;
		float back = startDotN + end * deltaDotN - pPlane->dist;
		
		int side = front < 0.f;

		// Blow it off if it doesn't split the plane...
		if ( (back < 0.f) == side )
			continue;

		// Don't test a surface that is farther away from the closest found intersection
		float frac = front / (front-back);
		if (frac >= state.m_HitFrac)
			continue;

		float mid = start * (1.0f - frac) + end * frac;

		// Check this surface to see if there's an intersection
		if (FindIntersectionAtSurface( surfID, mid, c, state ))
		{
			closestSurfID = surfID;
		}
	}

	// Return the closest surface hit
	return closestSurfID;
}