+static int Mod_Q1BSP_FindBoxClusters(model_t *model, const vec3_t mins, const vec3_t maxs, int maxclusters, int *clusterlist)
+{
+ int numclusters = 0;
+ int nodestackindex = 0;
+ mnode_t *node, *nodestack[1024];
+ if (!model->brush.num_pvsclusters)
+ return -1;
+ node = model->brush.data_nodes;
+ for (;;)
+ {
+#if 1
+ if (node->plane)
+ {
+ // node - recurse down the BSP tree
+ int side = BoxOnPlaneSide(mins, maxs, node->plane) - 1;
+ if (side < 2)
+ {
+ // box is on one side of plane, take that path
+ node = node->children[side];
+ }
+ else
+ {
+ // box crosses plane, take one path and remember the other
+ if (nodestackindex < 1024)
+ nodestack[nodestackindex++] = node->children[0];
+ node = node->children[1];
+ }
+ continue;
+ }
+ else
+ {
+ // leaf - add clusterindex to list
+ if (numclusters < maxclusters)
+ clusterlist[numclusters] = ((mleaf_t *)node)->clusterindex;
+ numclusters++;
+ }
+#else
+ if (BoxesOverlap(mins, maxs, node->mins, node->maxs))
+ {
+ if (node->plane)
+ {
+ if (nodestackindex < 1024)
+ nodestack[nodestackindex++] = node->children[0];
+ node = node->children[1];
+ continue;
+ }
+ else
+ {
+ // leaf - add clusterindex to list
+ if (numclusters < maxclusters)
+ clusterlist[numclusters] = ((mleaf_t *)node)->clusterindex;
+ numclusters++;
+ }
+ }
+#endif
+ // try another path we didn't take earlier
+ if (nodestackindex == 0)
+ break;
+ node = nodestack[--nodestackindex];
+ }
+ // return number of clusters found (even if more than the maxclusters)
+ return numclusters;
+}
+
+static int Mod_Q1BSP_BoxTouchingPVS(model_t *model, const unsigned char *pvs, const vec3_t mins, const vec3_t maxs)
+{
+ int nodestackindex = 0;
+ mnode_t *node, *nodestack[1024];
+ if (!model->brush.num_pvsclusters)
+ return true;
+ node = model->brush.data_nodes;
+ for (;;)
+ {
+#if 1
+ if (node->plane)
+ {
+ // node - recurse down the BSP tree
+ int side = BoxOnPlaneSide(mins, maxs, node->plane) - 1;
+ if (side < 2)
+ {
+ // box is on one side of plane, take that path
+ node = node->children[side];
+ }
+ else
+ {
+ // box crosses plane, take one path and remember the other
+ if (nodestackindex < 1024)
+ nodestack[nodestackindex++] = node->children[0];
+ node = node->children[1];
+ }
+ continue;
+ }
+ else
+ {
+ // leaf - check cluster bit
+ int clusterindex = ((mleaf_t *)node)->clusterindex;
+ if (CHECKPVSBIT(pvs, clusterindex))
+ {
+ // it is visible, return immediately with the news
+ return true;
+ }
+ }
+#else
+ if (BoxesOverlap(mins, maxs, node->mins, node->maxs))
+ {
+ if (node->plane)
+ {
+ if (nodestackindex < 1024)
+ nodestack[nodestackindex++] = node->children[0];
+ node = node->children[1];
+ continue;
+ }
+ else
+ {
+ // leaf - check cluster bit
+ int clusterindex = ((mleaf_t *)node)->clusterindex;
+ if (CHECKPVSBIT(pvs, clusterindex))
+ {
+ // it is visible, return immediately with the news
+ return true;
+ }
+ }
+ }
+#endif
+ // nothing to see here, try another path we didn't take earlier
+ if (nodestackindex == 0)
+ break;
+ node = nodestack[--nodestackindex];
+ }
+ // it is not visible
+ return false;
+}
+
+static int Mod_Q1BSP_BoxTouchingLeafPVS(model_t *model, const unsigned char *pvs, const vec3_t mins, const vec3_t maxs)