quakeforge/tools/qfbsp/source/outside.c

249 lines
5.2 KiB
C
Raw Normal View History

/* Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See file, 'COPYING', for details.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "QF/sys.h"
#include "bsp5.h"
2002-09-19 17:14:23 +00:00
int outleafs;
2002-09-19 17:14:23 +00:00
node_t *
2002-09-19 18:51:19 +00:00
PointInLeaf (node_t *node, vec3_t point)
{
2002-09-19 17:14:23 +00:00
vec_t d;
if (node->contents)
return node;
2002-09-19 17:14:23 +00:00
2002-09-19 18:51:19 +00:00
d = DotProduct (planes[node->planenum].normal, point)
- planes[node->planenum].dist;
2002-09-19 17:14:23 +00:00
if (d > 0)
return PointInLeaf (node->children[0], point);
2002-09-19 17:14:23 +00:00
return PointInLeaf (node->children[1], point);
}
2002-09-19 17:14:23 +00:00
qboolean
2002-09-19 18:51:19 +00:00
PlaceOccupant (int num, vec3_t point, node_t *headnode)
{
2002-09-19 17:14:23 +00:00
node_t *n;
n = PointInLeaf (headnode, point);
if (n->contents == CONTENTS_SOLID)
return false;
n->occupied = num;
return true;
}
2002-09-19 17:14:23 +00:00
portal_t *prevleaknode;
FILE *leakfile;
2002-09-19 18:51:19 +00:00
2002-09-19 17:14:23 +00:00
void
2002-09-19 18:51:19 +00:00
MarkLeakTrail (portal_t *n2)
{
2002-09-19 17:14:23 +00:00
float len;
2002-09-19 18:51:19 +00:00
int i, j;
2002-09-19 17:14:23 +00:00
portal_t *n1;
2002-09-19 18:51:19 +00:00
vec3_t p1, p2, dir;
if (hullnum)
return;
n1 = prevleaknode;
prevleaknode = n2;
2002-09-19 17:14:23 +00:00
if (!n1)
return;
2002-09-19 17:14:23 +00:00
VectorCopy (n2->winding->points[0], p1);
2002-09-19 17:14:23 +00:00
for (i = 1; i < n2->winding->numpoints; i++) {
for (j = 0; j < 3; j++)
p1[j] = (p1[j] + n2->winding->points[i][j]) / 2;
}
2002-09-19 17:14:23 +00:00
VectorCopy (n1->winding->points[0], p2);
2002-09-19 17:14:23 +00:00
for (i = 1; i < n1->winding->numpoints; i++) {
for (j = 0; j < 3; j++)
p2[j] = (p2[j] + n1->winding->points[i][j]) / 2;
}
2002-09-19 17:14:23 +00:00
VectorSubtract (p2, p1, dir);
len = VectorLength (dir);
VectorNormalize (dir);
2002-09-19 17:14:23 +00:00
while (len > 2) {
fprintf (leakfile, "%f %f %f\n", p1[0], p1[1], p1[2]);
for (i = 0; i < 3; i++)
p1[i] += dir[i] * 2;
len -= 2;
}
}
2002-09-19 18:51:19 +00:00
int hit_occupied;
int backdraw;
/*
==================
RecursiveFillOutside
If fill is false, just check, don't fill
Returns true if an occupied leaf is reached
==================
*/
2002-09-19 17:14:23 +00:00
qboolean
2002-09-19 18:51:19 +00:00
RecursiveFillOutside (node_t *l, qboolean fill)
{
2002-09-19 17:14:23 +00:00
portal_t *p;
int s;
qboolean res = false;
if (l->contents == CONTENTS_SOLID || l->contents == CONTENTS_SKY)
return false;
2002-09-19 17:14:23 +00:00
if (l->valid == valid)
return false;
2002-09-19 17:14:23 +00:00
if (l->occupied) {
vec_t *v;
hit_occupied = l->occupied;
v = entities[hit_occupied].origin;
qprintf ("reached occupant at: (%4.0f,%4.0f,%4.0f) %s\n", v[0], v[1],
v[2], ValueForKey (&entities[hit_occupied], "classname"));
res = true;
}
2002-09-19 17:14:23 +00:00
l->valid = valid;
// fill it and it's neighbors
if (fill)
l->contents = CONTENTS_SOLID;
outleafs++;
2002-09-19 17:14:23 +00:00
for (p = l->portals; p;) {
s = (p->nodes[0] == l);
2002-09-19 17:14:23 +00:00
if (RecursiveFillOutside (p->nodes[s], fill)) { // leaked, so stop
// filling
if (backdraw-- > 0) {
MarkLeakTrail (p);
DrawLeaf (l, 2);
}
res = true;
}
p = p->next[!s];
}
2002-09-19 17:14:23 +00:00
return res;
}
2002-09-19 17:14:23 +00:00
void
2002-09-19 18:51:19 +00:00
ClearOutFaces (node_t *node)
{
2002-09-19 17:14:23 +00:00
face_t **fp;
if (node->planenum != -1) {
ClearOutFaces (node->children[0]);
ClearOutFaces (node->children[1]);
return;
}
if (node->contents != CONTENTS_SOLID)
return;
2002-09-19 17:14:23 +00:00
for (fp = node->markfaces; *fp; fp++) {
// mark all the original faces that are removed
(*fp)->numpoints = 0;
}
node->faces = NULL;
}
//=============================================================================
2002-09-19 17:14:23 +00:00
qboolean
2002-09-19 18:51:19 +00:00
FillOutside (node_t *node)
{
2002-09-19 18:51:19 +00:00
int i, s;
2002-09-19 17:14:23 +00:00
qboolean inside;
2002-09-19 18:51:19 +00:00
vec_t *v;
2002-09-19 17:14:23 +00:00
qprintf ("----- FillOutside ----\n");
2002-09-19 17:14:23 +00:00
if (nofill) {
printf ("skipped\n");
return false;
}
2002-09-19 17:14:23 +00:00
inside = false;
2002-09-19 17:14:23 +00:00
for (i = 1; i < num_entities; i++) {
if (!_VectorCompare (entities[i].origin, vec3_origin)) {
if (PlaceOccupant (i, entities[i].origin, node))
inside = true;
}
}
2002-09-19 17:14:23 +00:00
if (!inside) {
2002-09-19 18:51:19 +00:00
printf ("Hullnum %i: No entities in empty space -- no filling "
"performed\n", hullnum);
return false;
}
s = !(outside_node.portals->nodes[1] == &outside_node);
// first check to see if an occupied leaf is hit
outleafs = 0;
valid++;
prevleaknode = NULL;
2002-09-19 17:14:23 +00:00
if (!hullnum) {
leakfile = fopen (pointfilename, "w");
if (!leakfile)
Sys_Error ("Couldn't open %s\n", pointfilename);
}
2002-09-19 17:14:23 +00:00
if (RecursiveFillOutside (outside_node.portals->nodes[s], false)) {
v = entities[hit_occupied].origin;
qprintf ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
2002-09-19 17:14:23 +00:00
qprintf ("reached occupant at: (%4.0f,%4.0f,%4.0f)\n", v[0], v[1],
v[2]);
qprintf ("no filling performed\n");
if (!hullnum)
fclose (leakfile);
2002-09-19 17:14:23 +00:00
qprintf ("leak file written to %s\n", pointfilename);
qprintf ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
return false;
}
if (!hullnum)
fclose (leakfile);
// now go back and fill things in
valid++;
RecursiveFillOutside (outside_node.portals->nodes[s], true);
2002-09-19 17:14:23 +00:00
// remove faces from filled in leafs
ClearOutFaces (node);
2002-09-19 17:14:23 +00:00
qprintf ("%4i outleafs\n", outleafs);
return true;
}