2002-09-19 16:58:48 +00:00
|
|
|
/* 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
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "brush.h"
|
2002-09-19 16:58:48 +00:00
|
|
|
#include "bsp5.h"
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "draw.h"
|
2002-09-20 21:48:34 +00:00
|
|
|
#include "options.h"
|
2010-08-18 08:52:13 +00:00
|
|
|
#include "portals.h"
|
|
|
|
#include "outside.h"
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2010-09-01 01:06:54 +00:00
|
|
|
/** \addtogroup qfbsp_outside
|
|
|
|
*/
|
|
|
|
//@{
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
int outleafs;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
/** Find the leaf node in which the point is.
|
|
|
|
|
|
|
|
\param node The root of the bsp tree.
|
|
|
|
\param point The point's location.
|
|
|
|
\return The leaf node in which the point is.
|
|
|
|
*/
|
2004-02-05 04:11:39 +00:00
|
|
|
static node_t *
|
2010-09-02 04:01:45 +00:00
|
|
|
PointInLeaf (node_t *node, const vec3_t point)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
vec_t d;
|
|
|
|
|
2004-02-05 04:11:39 +00:00
|
|
|
while (!node->contents) {
|
|
|
|
d = DotProduct (planes[node->planenum].normal, point);
|
|
|
|
node = node->children[d <= planes[node->planenum].dist];
|
|
|
|
}
|
|
|
|
return node;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
/** Set the distance to a node from all reachable nodes.
|
|
|
|
|
2010-09-01 01:06:01 +00:00
|
|
|
\param n The current node.
|
|
|
|
\param dist The distance to the original node.
|
2010-08-30 03:04:45 +00:00
|
|
|
*/
|
2003-02-04 23:26:26 +00:00
|
|
|
static void
|
|
|
|
FloodEntDist_r (node_t *n, int dist)
|
|
|
|
{
|
|
|
|
portal_t *p;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
n->o_dist = dist;
|
|
|
|
|
|
|
|
for (p = n->portals; p; p = p->next[s]) {
|
|
|
|
s = (p->nodes[1] == n);
|
|
|
|
|
|
|
|
if (p->nodes[!s]->o_dist)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((p->nodes[0]->contents == CONTENTS_SOLID) ||
|
|
|
|
(p->nodes[1]->contents == CONTENTS_SOLID))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((p->nodes[0]->contents == CONTENTS_SKY) ||
|
|
|
|
(p->nodes[1]->contents == CONTENTS_SKY))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FloodEntDist_r (p->nodes[!s], dist + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
/** Try to place an entity in the map.
|
|
|
|
|
|
|
|
The entity must be in open space.
|
|
|
|
|
|
|
|
\param num The entity number.
|
|
|
|
\param point The entity's origin.
|
|
|
|
\param headnode The root of the map's bsp tree.
|
|
|
|
\return true if the entity could be placed, false otherwise.
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static qboolean
|
2010-09-02 04:01:45 +00:00
|
|
|
PlaceOccupant (int num, const vec3_t point, node_t *headnode)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
node_t *n;
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
n = PointInLeaf (headnode, point);
|
|
|
|
if (n->contents == CONTENTS_SOLID)
|
|
|
|
return false;
|
|
|
|
n->occupied = num;
|
2003-02-04 23:26:26 +00:00
|
|
|
|
|
|
|
FloodEntDist_r (n, 1);
|
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-02 04:01:45 +00:00
|
|
|
const portal_t *prevleaknode;
|
2002-09-19 17:14:23 +00:00
|
|
|
FILE *leakfile;
|
2002-09-19 18:51:19 +00:00
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
/** Write the coords for points joining two portals to the point file.
|
|
|
|
|
|
|
|
\param n2 The second portal.
|
|
|
|
\note The first portal is set by the preceeding call.
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2010-09-02 04:01:45 +00:00
|
|
|
MarkLeakTrail (const portal_t *n2)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
float len;
|
2002-09-19 18:51:19 +00:00
|
|
|
int i, j;
|
2010-09-02 04:01:45 +00:00
|
|
|
const portal_t *n1;
|
2002-09-19 18:51:19 +00:00
|
|
|
vec3_t p1, p2, dir;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
n1 = prevleaknode;
|
|
|
|
prevleaknode = n2;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (!n1)
|
|
|
|
return;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +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++)
|
2002-09-19 16:58:48 +00:00
|
|
|
p1[j] = (p1[j] + n2->winding->points[i][j]) / 2;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +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++)
|
2002-09-19 16:58:48 +00:00
|
|
|
p2[j] = (p2[j] + n1->winding->points[i][j]) / 2;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
VectorSubtract (p2, p1, dir);
|
|
|
|
len = VectorLength (dir);
|
2002-09-25 21:35:49 +00:00
|
|
|
_VectorNormalize (dir);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2003-09-03 22:00:08 +00:00
|
|
|
if (!leakfile)
|
|
|
|
leakfile = fopen (options.pointfile, "w");
|
|
|
|
if (!leakfile)
|
|
|
|
Sys_Error ("Couldn't open %s\n", options.pointfile);
|
|
|
|
|
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;
|
2002-09-19 16:58:48 +00:00
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-01 01:06:01 +00:00
|
|
|
/** Mark the trail from outside to the entity.
|
|
|
|
|
|
|
|
Try to use the shortest path from the outside node to the entity.
|
|
|
|
*/
|
2010-09-01 00:52:39 +00:00
|
|
|
static void
|
|
|
|
MarkLeakTrail2 (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int next, side;
|
2010-09-02 04:01:45 +00:00
|
|
|
const node_t *n, *nextnode;
|
|
|
|
const portal_t *p, *p2;
|
2010-09-01 00:52:39 +00:00
|
|
|
vec3_t wc;
|
2010-09-02 04:01:45 +00:00
|
|
|
const vec_t *v;
|
2010-09-01 00:52:39 +00:00
|
|
|
|
|
|
|
leakfile = fopen (options.pointfile, "w");
|
|
|
|
if (!leakfile)
|
|
|
|
Sys_Error ("Couldn't open %s\n", options.pointfile);
|
|
|
|
|
|
|
|
n = &outside_node;
|
|
|
|
next = -1;
|
|
|
|
|
|
|
|
while ((n->o_dist > 1) || (next == -1)) {
|
|
|
|
nextnode = 0;
|
|
|
|
p2 = 0;
|
|
|
|
for (p = n->portals; p; p = p->next[side]) {
|
|
|
|
side = (p->nodes[1] == n);
|
|
|
|
if ((next == -1)
|
|
|
|
|| ((p->nodes[!side]->o_dist < next)
|
|
|
|
&& p->nodes[!side]->o_dist)) {
|
|
|
|
nextnode = p->nodes[!side];
|
|
|
|
next = nextnode->o_dist;
|
|
|
|
p2 = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!nextnode)
|
|
|
|
break;
|
|
|
|
n = nextnode;
|
|
|
|
|
|
|
|
VectorZero (wc);
|
|
|
|
for (i = 0; i < p2->winding->numpoints; i++)
|
|
|
|
VectorAdd (wc, p2->winding->points[i], wc);
|
|
|
|
VectorScale (wc, 1.0 / i, wc);
|
|
|
|
fprintf (leakfile, "%g %g %g", wc[0], wc[1], wc[2]);
|
|
|
|
}
|
|
|
|
v = entities[n->occupied].origin;
|
|
|
|
fprintf (leakfile, "%g %g %g\n", v[0], v[1], v[2]);
|
|
|
|
|
|
|
|
fclose (leakfile);
|
|
|
|
}
|
|
|
|
|
2002-09-19 18:51:19 +00:00
|
|
|
int hit_occupied;
|
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
/** Recurse through the map setting the outside nodes to solid.
|
|
|
|
|
|
|
|
Recursively traverses the portals of the start node.
|
2002-09-19 16:58:48 +00:00
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
\param l The start node.
|
|
|
|
\param fill If false, just check, don't fill
|
|
|
|
\return \c true if an occupied leaf is reached, otherwise
|
|
|
|
\c false.
|
2002-09-19 16:58:48 +00:00
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static qboolean
|
2002-09-19 18:51:19 +00:00
|
|
|
RecursiveFillOutside (node_t *l, qboolean fill)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
portal_t *p;
|
|
|
|
int s;
|
2002-09-20 17:02:53 +00:00
|
|
|
qboolean res = false;
|
2002-09-19 16:58:48 +00:00
|
|
|
|
|
|
|
if (l->contents == CONTENTS_SOLID || l->contents == CONTENTS_SKY)
|
|
|
|
return false;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
if (l->valid == valid)
|
|
|
|
return false;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 22:48:30 +00:00
|
|
|
if (l->occupied) {
|
2010-09-02 04:01:45 +00:00
|
|
|
const vec_t *v;
|
2002-09-19 22:48:30 +00:00
|
|
|
hit_occupied = l->occupied;
|
2002-09-20 17:02:53 +00:00
|
|
|
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 22:48:30 +00:00
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
l->valid = valid;
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// fill it and it's neighbors
|
2002-09-19 16:58:48 +00:00
|
|
|
if (fill)
|
|
|
|
l->contents = CONTENTS_SOLID;
|
|
|
|
outleafs++;
|
|
|
|
|
2002-09-19 17:14:23 +00:00
|
|
|
for (p = l->portals; p;) {
|
2002-09-19 16:58:48 +00:00
|
|
|
s = (p->nodes[0] == l);
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
if (RecursiveFillOutside (p->nodes[s], fill)) {
|
|
|
|
// leaked, so stop filling
|
2010-09-01 00:52:39 +00:00
|
|
|
if (options.smart_leak)
|
|
|
|
return true;
|
2003-09-03 22:00:08 +00:00
|
|
|
if (!options.hullnum) {
|
2002-09-19 16:58:48 +00:00
|
|
|
MarkLeakTrail (p);
|
|
|
|
DrawLeaf (l, 2);
|
|
|
|
}
|
2002-09-20 17:02:53 +00:00
|
|
|
res = true;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
p = p->next[!s];
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-20 17:02:53 +00:00
|
|
|
return res;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
/** Remove faces from filled in leafs.
|
|
|
|
|
|
|
|
Recursively traverses the portals of the start node.
|
|
|
|
|
|
|
|
\param node Start node.
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-09-19 18:51:19 +00:00
|
|
|
ClearOutFaces (node_t *node)
|
2002-09-19 16:58:48 +00:00
|
|
|
{
|
2002-09-19 17:14:23 +00:00
|
|
|
face_t **fp;
|
|
|
|
|
|
|
|
if (node->planenum != -1) {
|
2002-09-19 16:58:48 +00:00
|
|
|
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
|
2003-09-08 03:00:53 +00:00
|
|
|
FreeWinding ((*fp)->points);
|
|
|
|
(*fp)->points = 0;
|
2002-09-19 16:58:48 +00:00
|
|
|
}
|
|
|
|
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 16:58:48 +00:00
|
|
|
{
|
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
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
qprintf ("----- FillOutside ----\n");
|
|
|
|
|
2002-09-20 21:48:34 +00:00
|
|
|
if (options.nofill) {
|
2002-09-19 16:58:48 +00:00
|
|
|
printf ("skipped\n");
|
|
|
|
return false;
|
|
|
|
}
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
// Place the map's entities in the map. inside will be true if at least
|
|
|
|
// one entitie could be placed.
|
2002-09-19 16:58:48 +00:00
|
|
|
inside = false;
|
2002-09-19 17:14:23 +00:00
|
|
|
for (i = 1; i < num_entities; i++) {
|
2002-09-20 17:02:53 +00:00
|
|
|
if (!_VectorCompare (entities[i].origin, vec3_origin)) {
|
2002-09-19 16:58:48 +00:00
|
|
|
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 "
|
2002-09-20 21:48:34 +00:00
|
|
|
"performed\n", options.hullnum);
|
2002-09-19 16:58:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = !(outside_node.portals->nodes[1] == &outside_node);
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// first check to see if an occupied leaf is hit
|
2002-09-19 16:58:48 +00:00
|
|
|
outleafs = 0;
|
|
|
|
valid++;
|
|
|
|
|
|
|
|
prevleaknode = NULL;
|
2002-09-19 17:14:23 +00:00
|
|
|
|
|
|
|
if (RecursiveFillOutside (outside_node.portals->nodes[s], false)) {
|
2003-09-03 22:00:08 +00:00
|
|
|
if (leakfile)
|
2003-02-04 23:26:26 +00:00
|
|
|
fclose (leakfile);
|
2003-09-03 22:00:08 +00:00
|
|
|
leakfile = 0;
|
|
|
|
if (!options.hullnum) {
|
|
|
|
v = entities[hit_occupied].origin;
|
2003-09-08 19:50:19 +00:00
|
|
|
printf ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
|
|
|
printf ("reached occupant at: (%4.0f,%4.0f,%4.0f) %s\n",
|
|
|
|
v[0], v[1], v[2],
|
|
|
|
ValueForKey (&entities[hit_occupied], "classname"));
|
|
|
|
printf ("no filling performed\n");
|
|
|
|
printf ("leak file written to %s\n", options.pointfile);
|
|
|
|
printf ("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
|
2003-02-04 23:26:26 +00:00
|
|
|
}
|
2010-09-01 00:52:39 +00:00
|
|
|
if (options.smart_leak)
|
|
|
|
MarkLeakTrail2 ();
|
|
|
|
|
2003-09-03 22:00:08 +00:00
|
|
|
// remove faces from filled in leafs
|
|
|
|
ClearOutFaces (node);
|
2002-09-19 16:58:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-09-23 16:27:17 +00:00
|
|
|
// now go back and fill things in
|
2002-09-19 16:58:48 +00:00
|
|
|
valid++;
|
|
|
|
RecursiveFillOutside (outside_node.portals->nodes[s], true);
|
|
|
|
|
2010-08-30 03:04:45 +00:00
|
|
|
// remove faces from filled in leafs
|
2002-09-19 16:58:48 +00:00
|
|
|
ClearOutFaces (node);
|
2002-09-19 17:14:23 +00:00
|
|
|
|
2002-09-19 16:58:48 +00:00
|
|
|
qprintf ("%4i outleafs\n", outleafs);
|
|
|
|
return true;
|
|
|
|
}
|
2010-09-01 01:06:54 +00:00
|
|
|
|
|
|
|
//@}
|