+ smax = (surf->extents[0] >> 4) + 1;
+ tmax = (surf->extents[1] >> 4) + 1;
+
+ for (lnum = 0; lnum < r_numdlights; lnum++)
+ {
+ if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
+ continue; // not lit by this light
+
+ softwareuntransform(r_dlight[lnum].origin, local);
+// VectorSubtract (r_dlight[lnum].origin, currentrenderentity->origin, local);
+ dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
+
+ // for comparisons to minimum acceptable light
+ maxdist = (int) r_dlight[lnum].cullradius2;
+
+ // already clamped, skip this
+ // clamp radius to avoid exceeding 32768 entry division table
+ //if (maxdist > 4194304)
+ // maxdist = 4194304;
+
+ dist2 = dist * dist;
+ dist2 += LIGHTOFFSET;
+ if (dist2 >= maxdist)
+ continue;
+
+ impact[0] = local[0] - surf->plane->normal[0] * dist;
+ impact[1] = local[1] - surf->plane->normal[1] * dist;
+ impact[2] = local[2] - surf->plane->normal[2] * dist;
+
+ impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
+ impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
+
+ s = bound(0, impacts, smax * 16) - impacts;
+ t = bound(0, impactt, tmax * 16) - impactt;
+ i = s * s + t * t + dist2;
+ if (i > maxdist)
+ continue;
+
+ // reduce calculations
+ for (s = 0, i = impacts; s < smax; s++, i -= 16)
+ sdtable[s] = i * i + dist2;
+
+ maxdist3 = maxdist - dist2;
+
+ // convert to 8.8 blocklights format
+ red = r_dlight[lnum].light[0];
+ green = r_dlight[lnum].light[1];
+ blue = r_dlight[lnum].light[2];
+ subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
+ bl = blocklights;
+ smax3 = smax * 3;
+
+ i = impactt;
+ for (t = 0;t < tmax;t++, i -= 16)
+ {
+ td = i * i;
+ // make sure some part of it is visible on this line
+ if (td < maxdist3)
+ {
+ maxdist2 = maxdist - td;
+ for (s = 0;s < smax;s++)
+ {
+ if (sdtable[s] < maxdist2)
+ {
+ k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
+ if (k > 0)
+ {
+ bl[0] += (red * k) >> 8;
+ bl[1] += (green * k) >> 8;
+ bl[2] += (blue * k) >> 8;
+ lit = true;
+ }
+ }
+ bl += 3;
+ }
+ }
+ else // skip line
+ bl += smax3;
+ }
+ }
+ return lit;