📄 portals.c
字号:
*new_portal = *p;
new_portal->winding = backwinding;
FreeWinding (p->winding);
p->winding = frontwinding;
if (side == 0)
{
AddPortalToNodes (p, f, other_node);
AddPortalToNodes (new_portal, b, other_node);
}
else
{
AddPortalToNodes (p, other_node, f);
AddPortalToNodes (new_portal, other_node, b);
}
}
node->portals = NULL;
}
/*
================
CalcNodeBounds
================
*/
void CalcNodeBounds (node_t *node)
{
portal_t *p;
int s;
int i;
// calc mins/maxs for both leafs and nodes
ClearBounds (node->mins, node->maxs);
for (p = node->portals ; p ; p = p->next[s])
{
s = (p->nodes[1] == node);
for (i=0 ; i<p->winding->numpoints ; i++)
AddPointToBounds (p->winding->p[i], node->mins, node->maxs);
}
}
/*
==================
MakeTreePortals_r
==================
*/
void MakeTreePortals_r (node_t *node)
{
int i;
CalcNodeBounds (node);
if (node->mins[0] >= node->maxs[0])
{
_printf ("WARNING: node without a volume\n");
_printf("node has %d tiny portals\n", node->tinyportals);
_printf("node reference point %1.2f %1.2f %1.2f\n", node->referencepoint[0],
node->referencepoint[1],
node->referencepoint[2]);
}
for (i=0 ; i<3 ; i++)
{
if (node->mins[i] < MIN_WORLD_COORD || node->maxs[i] > MAX_WORLD_COORD)
{
_printf ("WARNING: node with unbounded volume\n");
break;
}
}
if (node->planenum == PLANENUM_LEAF)
return;
MakeNodePortal (node);
SplitNodePortals (node);
MakeTreePortals_r (node->children[0]);
MakeTreePortals_r (node->children[1]);
}
/*
==================
MakeTreePortals
==================
*/
void MakeTreePortals (tree_t *tree)
{
qprintf( "----- MakeTreePortals -----\n");
MakeHeadnodePortals (tree);
MakeTreePortals_r (tree->headnode);
qprintf("%6d tiny portals\n", c_tinyportals);
}
/*
=========================================================
FLOOD ENTITIES
=========================================================
*/
int c_floodedleafs;
/*
=============
FloodPortals_r
=============
*/
void FloodPortals_r (node_t *node, int dist) {
portal_t *p;
int s;
if ( node->occupied ) {
return;
}
if ( node->opaque ) {
return;
}
c_floodedleafs++;
node->occupied = dist;
for (p=node->portals ; p ; p = p->next[s]) {
s = (p->nodes[1] == node);
FloodPortals_r (p->nodes[!s], dist+1);
}
}
/*
=============
PlaceOccupant
=============
*/
qboolean PlaceOccupant (node_t *headnode, vec3_t origin, entity_t *occupant)
{
node_t *node;
vec_t d;
plane_t *plane;
// find the leaf to start in
node = headnode;
while (node->planenum != PLANENUM_LEAF)
{
plane = &mapplanes[node->planenum];
d = DotProduct (origin, plane->normal) - plane->dist;
if (d >= 0)
node = node->children[0];
else
node = node->children[1];
}
if ( node->opaque )
return qfalse;
node->occupant = occupant;
FloodPortals_r (node, 1);
return qtrue;
}
/*
=============
FloodEntities
Marks all nodes that can be reached by entites
=============
*/
qboolean FloodEntities( tree_t *tree ) {
int i;
vec3_t origin;
const char *cl;
qboolean inside;
node_t *headnode;
headnode = tree->headnode;
qprintf ("--- FloodEntities ---\n");
inside = qfalse;
tree->outside_node.occupied = 0;
c_floodedleafs = 0;
for (i=1 ; i<num_entities ; i++)
{
GetVectorForKey (&entities[i], "origin", origin);
if (VectorCompare(origin, vec3_origin))
continue;
cl = ValueForKey (&entities[i], "classname");
origin[2] += 1; // so objects on floor are ok
if (PlaceOccupant (headnode, origin, &entities[i]))
inside = qtrue;
}
qprintf("%5i flooded leafs\n", c_floodedleafs );
if (!inside)
{
qprintf ("no entities in open -- no filling\n");
}
else if (tree->outside_node.occupied)
{
qprintf ("entity reached from outside -- no filling\n");
}
return (qboolean)(inside && !tree->outside_node.occupied);
}
/*
=========================================================
FLOOD AREAS
=========================================================
*/
int c_areas;
/*
=============
FloodAreas_r
=============
*/
void FloodAreas_r (node_t *node)
{
portal_t *p;
int s;
bspbrush_t *b;
if ( node->areaportal ) {
//
if ( node->area == -1 ) {
node->area = c_areas;
}
// this node is part of an area portal brush
b = node->brushlist->original;
// if the current area has allready touched this
// portal, we are done
if (b->portalareas[0] == c_areas || b->portalareas[1] == c_areas)
return;
// note the current area as bounding the portal
if (b->portalareas[1] != -1)
{
_printf ("WARNING: areaportal brush %i touches > 2 areas\n", b->brushnum );
return;
}
if (b->portalareas[0] != -1) {
b->portalareas[1] = c_areas;
} else {
b->portalareas[0] = c_areas;
}
return;
}
if (node->area != -1) {
return; // allready got it
}
if ( node->cluster == -1 ) {
return;
}
node->area = c_areas;
for (p=node->portals ; p ; p = p->next[s])
{
s = (p->nodes[1] == node);
if ( !Portal_Passable(p) )
continue;
FloodAreas_r (p->nodes[!s]);
}
}
/*
=============
FindAreas_r
Just decend the tree, and for each node that hasn't had an
area set, flood fill out from there
=============
*/
void FindAreas_r (node_t *node)
{
if (node->planenum != PLANENUM_LEAF)
{
FindAreas_r (node->children[0]);
FindAreas_r (node->children[1]);
return;
}
if (node->opaque)
return;
if (node->areaportal)
return;
if (node->area != -1)
return; // allready got it
FloodAreas_r (node);
c_areas++;
}
/*
=============
CheckAreas_r
=============
*/
void CheckAreas_r (node_t *node)
{
bspbrush_t *b;
if (node->planenum != PLANENUM_LEAF)
{
CheckAreas_r (node->children[0]);
CheckAreas_r (node->children[1]);
return;
}
if (node->opaque)
return;
if (node->cluster != -1)
if (node->area == -1)
_printf("WARNING: cluster %d has area set to -1\n", node->cluster);
if (node->areaportal)
{
b = node->brushlist->original;
// check if the areaportal touches two areas
if (b->portalareas[0] == -1 || b->portalareas[1] == -1)
_printf ("WARNING: areaportal brush %i doesn't touch two areas\n", b->brushnum);
}
}
/*
=============
FloodAreas
Mark each leaf with an area, bounded by CONTENTS_AREAPORTAL
=============
*/
void FloodAreas (tree_t *tree)
{
qprintf ("--- FloodAreas ---\n");
FindAreas_r( tree->headnode );
// check for areaportal brushes that don't touch two areas
CheckAreas_r( tree->headnode );
qprintf ("%5i areas\n", c_areas);
}
//======================================================
int c_outside;
int c_inside;
int c_solid;
void FillOutside_r (node_t *node)
{
if (node->planenum != PLANENUM_LEAF)
{
FillOutside_r (node->children[0]);
FillOutside_r (node->children[1]);
return;
}
// anything not reachable by an entity
// can be filled away
if (!node->occupied) {
if ( !node->opaque ) {
c_outside++;
node->opaque = qtrue;
} else {
c_solid++;
}
} else {
c_inside++;
}
}
/*
=============
FillOutside
Fill all nodes that can't be reached by entities
=============
*/
void FillOutside (node_t *headnode)
{
c_outside = 0;
c_inside = 0;
c_solid = 0;
qprintf ("--- FillOutside ---\n");
FillOutside_r (headnode);
qprintf ("%5i solid leafs\n", c_solid);
qprintf ("%5i leafs filled\n", c_outside);
qprintf ("%5i inside leafs\n", c_inside);
}
//==============================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -