]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/portals.c
set eol-style
[xonotic/netradiant.git] / tools / quake3 / q3map2 / portals.c
index 0b3cdfd3b20102320f3e4930b5d64c535b03d898..59ed6daf03e3ce5c27f010a3da3d6417724b8b25 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-\r
-----------------------------------------------------------------------------------\r
-\r
-This code has been altered significantly from its original form, to support\r
-several games based on the Quake III Arena engine, in the form of "Q3Map2."\r
-\r
-------------------------------------------------------------------------------- */\r
-\r
-\r
-\r
-/* marker */\r
-#define PORTALS_C\r
-\r
-\r
-\r
-/* dependencies */\r
-#include "q3map2.h"\r
-\r
-\r
-\r
-/* ydnar: to fix broken portal windings */\r
-extern qboolean FixWinding( winding_t *w );\r
-\r
-\r
-int            c_active_portals;\r
-int            c_peak_portals;\r
-int            c_boundary;\r
-int            c_boundary_sides;\r
-\r
-/*\r
-===========\r
-AllocPortal\r
-===========\r
-*/\r
-portal_t *AllocPortal (void)\r
-{\r
-       portal_t        *p;\r
-       \r
-       if (numthreads == 1)\r
-               c_active_portals++;\r
-       if (c_active_portals > c_peak_portals)\r
-               c_peak_portals = c_active_portals;\r
-       \r
-       p = safe_malloc (sizeof(portal_t));\r
-       memset (p, 0, sizeof(portal_t));\r
-       \r
-       return p;\r
-}\r
-\r
-void FreePortal (portal_t *p)\r
-{\r
-       if (p->winding)\r
-               FreeWinding (p->winding);\r
-       if (numthreads == 1)\r
-               c_active_portals--;\r
-       free (p);\r
-}\r
-\r
-\r
-\r
-/*\r
-PortalPassable\r
-returns true if the portal has non-opaque leafs on both sides\r
-*/\r
-\r
-qboolean PortalPassable( portal_t *p )\r
-{\r
-       /* is this to global outside leaf? */\r
-       if( !p->onnode )\r
-               return qfalse;\r
-       \r
-       /* this should never happen */\r
-       if( p->nodes[ 0 ]->planenum != PLANENUM_LEAF ||\r
-               p->nodes[ 1 ]->planenum != PLANENUM_LEAF )\r
-               Error( "Portal_EntityFlood: not a leaf" );\r
-       \r
-       /* ydnar: added antiportal to supress portal generation for visibility blocking */\r
-       if( p->compileFlags & C_ANTIPORTAL )\r
-               return qfalse;\r
-       \r
-       /* both leaves on either side of the portal must be passable */\r
-       if( p->nodes[ 0 ]->opaque == qfalse && p->nodes[ 1 ]->opaque == qfalse )\r
-               return qtrue;\r
-       \r
-       /* otherwise this isn't a passable portal */\r
-       return qfalse;\r
-}\r
-\r
-\r
-\r
-\r
-int            c_tinyportals;\r
-int            c_badportals;   /* ydnar */\r
-\r
-/*\r
-=============\r
-AddPortalToNodes\r
-=============\r
-*/\r
-void AddPortalToNodes (portal_t *p, node_t *front, node_t *back)\r
-{\r
-       if (p->nodes[0] || p->nodes[1])\r
-               Error ("AddPortalToNode: allready included");\r
-\r
-       p->nodes[0] = front;\r
-       p->next[0] = front->portals;\r
-       front->portals = p;\r
-       \r
-       p->nodes[1] = back;\r
-       p->next[1] = back->portals;\r
-       back->portals = p;\r
-}\r
-\r
-\r
-/*\r
-=============\r
-RemovePortalFromNode\r
-=============\r
-*/\r
-void RemovePortalFromNode (portal_t *portal, node_t *l)\r
-{\r
-       portal_t        **pp, *t;\r
-       \r
-// remove reference to the current portal\r
-       pp = &l->portals;\r
-       while (1)\r
-       {\r
-               t = *pp;\r
-               if (!t)\r
-                       Error ("RemovePortalFromNode: portal not in leaf");     \r
-\r
-               if ( t == portal )\r
-                       break;\r
-\r
-               if (t->nodes[0] == l)\r
-                       pp = &t->next[0];\r
-               else if (t->nodes[1] == l)\r
-                       pp = &t->next[1];\r
-               else\r
-                       Error ("RemovePortalFromNode: portal not bounding leaf");\r
-       }\r
-       \r
-       if (portal->nodes[0] == l)\r
-       {\r
-               *pp = portal->next[0];\r
-               portal->nodes[0] = NULL;\r
-       }\r
-       else if (portal->nodes[1] == l)\r
-       {\r
-               *pp = portal->next[1];  \r
-               portal->nodes[1] = NULL;\r
-       }\r
-}\r
-\r
-//============================================================================\r
-\r
-void PrintPortal (portal_t *p)\r
-{\r
-       int                     i;\r
-       winding_t       *w;\r
-       \r
-       w = p->winding;\r
-       for (i=0 ; i<w->numpoints ; i++)\r
-               Sys_Printf ("(%5.0f,%5.0f,%5.0f)\n",w->p[i][0]\r
-               , w->p[i][1], w->p[i][2]);\r
-}\r
-\r
-/*\r
-================\r
-MakeHeadnodePortals\r
-\r
-The created portals will face the global outside_node\r
-================\r
-*/\r
-#define        SIDESPACE       8\r
-void MakeHeadnodePortals (tree_t *tree)\r
-{\r
-       vec3_t          bounds[2];\r
-       int                     i, j, n;\r
-       portal_t        *p, *portals[6];\r
-       plane_t         bplanes[6], *pl;\r
-       node_t *node;\r
-\r
-       node = tree->headnode;\r
-\r
-// pad with some space so there will never be null volume leafs\r
-       for (i=0 ; i<3 ; i++)\r
-       {\r
-               bounds[0][i] = tree->mins[i] - SIDESPACE;\r
-               bounds[1][i] = tree->maxs[i] + SIDESPACE;\r
-               if ( bounds[0][i] >= bounds[1][i] ) {\r
-                       Error( "Backwards tree volume" );\r
-               }\r
-       }\r
-       \r
-       tree->outside_node.planenum = PLANENUM_LEAF;\r
-       tree->outside_node.brushlist = NULL;\r
-       tree->outside_node.portals = NULL;\r
-       tree->outside_node.opaque = qfalse;\r
-\r
-       for (i=0 ; i<3 ; i++)\r
-               for (j=0 ; j<2 ; j++)\r
-               {\r
-                       n = j*3 + i;\r
-\r
-                       p = AllocPortal ();\r
-                       portals[n] = p;\r
-                       \r
-                       pl = &bplanes[n];\r
-                       memset (pl, 0, sizeof(*pl));\r
-                       if (j)\r
-                       {\r
-                               pl->normal[i] = -1;\r
-                               pl->dist = -bounds[j][i];\r
-                       }\r
-                       else\r
-                       {\r
-                               pl->normal[i] = 1;\r
-                               pl->dist = bounds[j][i];\r
-                       }\r
-                       p->plane = *pl;\r
-                       p->winding = BaseWindingForPlane (pl->normal, pl->dist);\r
-                       AddPortalToNodes (p, node, &tree->outside_node);\r
-               }\r
-               \r
-// clip the basewindings by all the other planes\r
-       for (i=0 ; i<6 ; i++)\r
-       {\r
-               for (j=0 ; j<6 ; j++)\r
-               {\r
-                       if (j == i)\r
-                               continue;\r
-                       ChopWindingInPlace (&portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON);\r
-               }\r
-       }\r
-}\r
-\r
-//===================================================\r
-\r
-\r
-/*\r
-================\r
-BaseWindingForNode\r
-================\r
-*/\r
-#define        BASE_WINDING_EPSILON    0.001\r
-#define        SPLIT_WINDING_EPSILON   0.001\r
-\r
-winding_t      *BaseWindingForNode (node_t *node)\r
-{\r
-       winding_t       *w;\r
-       node_t          *n;\r
-       plane_t         *plane;\r
-       vec3_t          normal;\r
-       vec_t           dist;\r
-\r
-       w = BaseWindingForPlane (mapplanes[node->planenum].normal\r
-               , mapplanes[node->planenum].dist);\r
-\r
-       // clip by all the parents\r
-       for (n=node->parent ; n && w ; )\r
-       {\r
-               plane = &mapplanes[n->planenum];\r
-\r
-               if (n->children[0] == node)\r
-               {       // take front\r
-                       ChopWindingInPlace (&w, plane->normal, plane->dist, BASE_WINDING_EPSILON);\r
-               }\r
-               else\r
-               {       // take back\r
-                       VectorSubtract (vec3_origin, plane->normal, normal);\r
-                       dist = -plane->dist;\r
-                       ChopWindingInPlace (&w, normal, dist, BASE_WINDING_EPSILON);\r
-               }\r
-               node = n;\r
-               n = n->parent;\r
-       }\r
-\r
-       return w;\r
-}\r
-\r
-//============================================================\r
-\r
-/*\r
-==================\r
-MakeNodePortal\r
-\r
-create the new portal by taking the full plane winding for the cutting plane\r
-and clipping it by all of parents of this node\r
-==================\r
-*/\r
-void MakeNodePortal (node_t *node)\r
-{\r
-       portal_t        *new_portal, *p;\r
-       winding_t       *w;\r
-       vec3_t          normal;\r
-       float           dist;\r
-       int                     side;\r
-\r
-       w = BaseWindingForNode (node);\r
-\r
-       // clip the portal by all the other portals in the node\r
-       for (p = node->portals ; p && w; p = p->next[side])     \r
-       {\r
-               if (p->nodes[0] == node)\r
-               {\r
-                       side = 0;\r
-                       VectorCopy (p->plane.normal, normal);\r
-                       dist = p->plane.dist;\r
-               }\r
-               else if (p->nodes[1] == node)\r
-               {\r
-                       side = 1;\r
-                       VectorSubtract (vec3_origin, p->plane.normal, normal);\r
-                       dist = -p->plane.dist;\r
-               }\r
-               else\r
-                       Error ("CutNodePortals_r: mislinked portal");\r
-\r
-               ChopWindingInPlace (&w, normal, dist, CLIP_EPSILON);\r
-       }\r
-\r
-       if (!w)\r
-       {\r
-               return;\r
-       }\r
-       \r
-       \r
-       /* ydnar: adding this here to fix degenerate windings */\r
-       #if 0\r
-       if( FixWinding( w ) == qfalse )\r
-       {\r
-               c_badportals++;\r
-               FreeWinding( w );\r
-               return;\r
-       }\r
-       #endif\r
-       \r
-       if (WindingIsTiny (w))\r
-       {\r
-               c_tinyportals++;\r
-               FreeWinding (w);\r
-               return;\r
-       }\r
-\r
-       new_portal = AllocPortal ();\r
-       new_portal->plane = mapplanes[node->planenum];\r
-       new_portal->onnode = node;\r
-       new_portal->winding = w;\r
-       new_portal->compileFlags = node->compileFlags;\r
-       AddPortalToNodes (new_portal, node->children[0], node->children[1]);\r
-}\r
-\r
-\r
-/*\r
-==============\r
-SplitNodePortals\r
-\r
-Move or split the portals that bound node so that the node's\r
-children have portals instead of node.\r
-==============\r
-*/\r
-void SplitNodePortals (node_t *node)\r
-{\r
-       portal_t        *p, *next_portal, *new_portal;\r
-       node_t          *f, *b, *other_node;\r
-       int                     side;\r
-       plane_t         *plane;\r
-       winding_t       *frontwinding, *backwinding;\r
-\r
-       plane = &mapplanes[node->planenum];\r
-       f = node->children[0];\r
-       b = node->children[1];\r
-\r
-       for (p = node->portals ; p ; p = next_portal)   \r
-       {\r
-               if (p->nodes[0] == node)\r
-                       side = 0;\r
-               else if (p->nodes[1] == node)\r
-                       side = 1;\r
-               else\r
-                       Error ("SplitNodePortals: mislinked portal");\r
-               next_portal = p->next[side];\r
-\r
-               other_node = p->nodes[!side];\r
-               RemovePortalFromNode (p, p->nodes[0]);\r
-               RemovePortalFromNode (p, p->nodes[1]);\r
-\r
-//\r
-// cut the portal into two portals, one on each side of the cut plane\r
-//\r
-               ClipWindingEpsilon (p->winding, plane->normal, plane->dist,\r
-                       SPLIT_WINDING_EPSILON, &frontwinding, &backwinding);\r
-\r
-               if (frontwinding && WindingIsTiny(frontwinding))\r
-               {\r
-                       if (!f->tinyportals)\r
-                               VectorCopy(frontwinding->p[0], f->referencepoint);\r
-                       f->tinyportals++;\r
-                       if (!other_node->tinyportals)\r
-                               VectorCopy(frontwinding->p[0], other_node->referencepoint);\r
-                       other_node->tinyportals++;\r
-\r
-                       FreeWinding (frontwinding);\r
-                       frontwinding = NULL;\r
-                       c_tinyportals++;\r
-               }\r
-\r
-               if (backwinding && WindingIsTiny(backwinding))\r
-               {\r
-                       if (!b->tinyportals)\r
-                               VectorCopy(backwinding->p[0], b->referencepoint);\r
-                       b->tinyportals++;\r
-                       if (!other_node->tinyportals)\r
-                               VectorCopy(backwinding->p[0], other_node->referencepoint);\r
-                       other_node->tinyportals++;\r
-\r
-                       FreeWinding (backwinding);\r
-                       backwinding = NULL;\r
-                       c_tinyportals++;\r
-               }\r
-\r
-               if (!frontwinding && !backwinding)\r
-               {       // tiny windings on both sides\r
-                       continue;\r
-               }\r
-\r
-               if (!frontwinding)\r
-               {\r
-                       FreeWinding (backwinding);\r
-                       if (side == 0)\r
-                               AddPortalToNodes (p, b, other_node);\r
-                       else\r
-                               AddPortalToNodes (p, other_node, b);\r
-                       continue;\r
-               }\r
-               if (!backwinding)\r
-               {\r
-                       FreeWinding (frontwinding);\r
-                       if (side == 0)\r
-                               AddPortalToNodes (p, f, other_node);\r
-                       else\r
-                               AddPortalToNodes (p, other_node, f);\r
-                       continue;\r
-               }\r
-               \r
-       // the winding is split\r
-               new_portal = AllocPortal ();\r
-               *new_portal = *p;\r
-               new_portal->winding = backwinding;\r
-               FreeWinding (p->winding);\r
-               p->winding = frontwinding;\r
-\r
-               if (side == 0)\r
-               {\r
-                       AddPortalToNodes (p, f, other_node);\r
-                       AddPortalToNodes (new_portal, b, other_node);\r
-               }\r
-               else\r
-               {\r
-                       AddPortalToNodes (p, other_node, f);\r
-                       AddPortalToNodes (new_portal, other_node, b);\r
-               }\r
-       }\r
-\r
-       node->portals = NULL;\r
-}\r
-\r
-\r
-/*\r
-================\r
-CalcNodeBounds\r
-================\r
-*/\r
-void CalcNodeBounds (node_t *node)\r
-{\r
-       portal_t        *p;\r
-       int                     s;\r
-       int                     i;\r
-\r
-       // calc mins/maxs for both leafs and nodes\r
-       ClearBounds (node->mins, node->maxs);\r
-       for (p = node->portals ; p ; p = p->next[s])    \r
-       {\r
-               s = (p->nodes[1] == node);\r
-               for (i=0 ; i<p->winding->numpoints ; i++)\r
-                       AddPointToBounds (p->winding->p[i], node->mins, node->maxs);\r
-       }\r
-}\r
-\r
-/*\r
-==================\r
-MakeTreePortals_r\r
-==================\r
-*/\r
-void MakeTreePortals_r (node_t *node)\r
-{\r
-       int             i;\r
-\r
-       CalcNodeBounds (node);\r
-       if (node->mins[0] >= node->maxs[0])\r
-       {\r
-               Sys_Printf ("WARNING: node without a volume\n");\r
-               Sys_Printf("node has %d tiny portals\n", node->tinyportals);\r
-               Sys_Printf("node reference point %1.2f %1.2f %1.2f\n", node->referencepoint[0],\r
-                                                                                                                       node->referencepoint[1],\r
-                                                                                                                       node->referencepoint[2]);\r
-       }\r
-\r
-       for (i=0 ; i<3 ; i++)\r
-       {\r
-               if (node->mins[i] < MIN_WORLD_COORD || node->maxs[i] > MAX_WORLD_COORD)\r
-               {\r
-      if(node->portals && node->portals->winding)\r
-        xml_Winding("WARNING: Node With Unbounded Volume", node->portals->winding->p, node->portals->winding->numpoints, qfalse);\r
-\r
-                       break;\r
-               }\r
-       }\r
-       if (node->planenum == PLANENUM_LEAF)\r
-               return;\r
-\r
-       MakeNodePortal (node);\r
-       SplitNodePortals (node);\r
-\r
-       MakeTreePortals_r (node->children[0]);\r
-       MakeTreePortals_r (node->children[1]);\r
-}\r
-\r
-/*\r
-==================\r
-MakeTreePortals\r
-==================\r
-*/\r
-void MakeTreePortals (tree_t *tree)\r
-{\r
-       Sys_FPrintf (SYS_VRB, "--- MakeTreePortals ---\n");\r
-       MakeHeadnodePortals (tree);\r
-       MakeTreePortals_r (tree->headnode);\r
-       Sys_FPrintf( SYS_VRB, "%9d tiny portals\n", c_tinyportals );\r
-       Sys_FPrintf( SYS_VRB, "%9d bad portals\n", c_badportals );      /* ydnar */\r
-}\r
-\r
-/*\r
-=========================================================\r
-\r
-FLOOD ENTITIES\r
-\r
-=========================================================\r
-*/\r
-\r
-int            c_floodedleafs;\r
-\r
-/*\r
-=============\r
-FloodPortals_r\r
-=============\r
-*/\r
-\r
-void FloodPortals_r( node_t *node, int dist, qboolean skybox )\r
-{\r
-       int                     s;\r
-       portal_t        *p;\r
-       \r
-       \r
-       if( skybox )\r
-               node->skybox = skybox;\r
-       \r
-       if( node->occupied || node->opaque )\r
-               return;\r
-       \r
-       c_floodedleafs++;\r
-       node->occupied = dist;\r
-       \r
-       for( p = node->portals; p; p = p->next[ s ] )\r
-       {\r
-               s = (p->nodes[ 1 ] == node);\r
-               FloodPortals_r( p->nodes[ !s ], dist + 1, skybox );\r
-       }\r
-}\r
-\r
-\r
-\r
-/*\r
-=============\r
-PlaceOccupant\r
-=============\r
-*/\r
-\r
-qboolean PlaceOccupant( node_t *headnode, vec3_t origin, entity_t *occupant, qboolean skybox )\r
-{\r
-       vec_t   d;\r
-       node_t  *node;\r
-       plane_t *plane;\r
-       \r
-       \r
-       // find the leaf to start in\r
-       node = headnode;\r
-       while( node->planenum != PLANENUM_LEAF )\r
-       {\r
-               plane = &mapplanes[ node->planenum ];\r
-               d = DotProduct( origin, plane->normal ) - plane->dist;\r
-               if( d >= 0 )\r
-                       node = node->children[ 0 ];\r
-               else\r
-                       node = node->children[ 1 ];\r
-       }\r
-       \r
-       if( node->opaque )\r
-               return qfalse;\r
-       node->occupant = occupant;\r
-       node->skybox = skybox;\r
-       \r
-       FloodPortals_r( node, 1, skybox );\r
-       \r
-       return qtrue;\r
-}\r
-\r
-/*\r
-=============\r
-FloodEntities\r
-\r
-Marks all nodes that can be reached by entites\r
-=============\r
-*/\r
-\r
-qboolean FloodEntities( tree_t *tree )\r
-{\r
-       int                     i, s;\r
-       vec3_t          origin, offset, scale, angles;\r
-       qboolean        r, inside, tripped, skybox;\r
-       node_t          *headnode;\r
-       entity_t        *e;\r
-       const char      *value;\r
-       \r
-       \r
-       headnode = tree->headnode;\r
-       Sys_FPrintf( SYS_VRB,"--- FloodEntities ---\n" );\r
-       inside = qfalse;\r
-       tree->outside_node.occupied = 0;\r
-       \r
-       tripped = qfalse;\r
-       c_floodedleafs = 0;\r
-       for( i = 1; i < numEntities; i++ )\r
-       {\r
-               /* get entity */\r
-               e = &entities[ i ];\r
-               \r
-               /* get origin */\r
-               GetVectorForKey( e, "origin", origin );\r
-               if( VectorCompare( origin, vec3_origin ) ) \r
-                       continue;\r
-               \r
-               /* handle skybox entities */\r
-               value = ValueForKey( e, "classname" );\r
-               if( !Q_stricmp( value, "_skybox" ) )\r
-               {\r
-                       skybox = qtrue;\r
-                       skyboxPresent = qtrue;\r
-                       \r
-                       /* invert origin */\r
-                       VectorScale( origin, -1.0f, offset );\r
-                       \r
-                       /* get scale */\r
-                       VectorSet( scale, 64.0f, 64.0f, 64.0f );\r
-                       value = ValueForKey( e, "_scale" );\r
-                       if( value[ 0 ] != '\0' )\r
-                       {\r
-                               s = sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );\r
-                               if( s == 1 )\r
-                               {\r
-                                       scale[ 1 ] = scale[ 0 ];\r
-                                       scale[ 2 ] = scale[ 0 ];\r
-                               }\r
-                       }\r
-                       \r
-                       /* get "angle" (yaw) or "angles" (pitch yaw roll) */\r
-                       VectorClear( angles );\r
-                       angles[ 2 ] = FloatForKey( e, "angle" );\r
-                       value = ValueForKey( e, "angles" );\r
-                       if( value[ 0 ] != '\0' )\r
-                               sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );\r
-                       \r
-                       /* set transform matrix (thanks spog) */\r
-                       m4x4_identity( skyboxTransform );\r
-                       m4x4_pivoted_transform_by_vec3( skyboxTransform, offset, angles, eXYZ, scale, origin );\r
-               }\r
-               else\r
-                       skybox = qfalse;\r
-               \r
-               /* nudge off floor */\r
-               origin[ 2 ] += 1;\r
-               \r
-               /* debugging code */\r
-               //%     if( i == 1 )\r
-               //%             origin[ 2 ] += 4096;\r
-               \r
-               /* find leaf */\r
-               r = PlaceOccupant( headnode, origin, e, skybox );\r
-               if( r )\r
-                       inside = qtrue;\r
-               if( (!r || tree->outside_node.occupied) && !tripped )\r
-               {\r
-                       xml_Select( "Entity leaked", e->mapEntityNum, 0, qfalse );\r
-                       tripped = qtrue;\r
-               }\r
-       }\r
-       \r
-       Sys_FPrintf( SYS_VRB, "%9d flooded leafs\n", c_floodedleafs );\r
-       \r
-       if( !inside )\r
-               Sys_FPrintf( SYS_VRB, "no entities in open -- no filling\n" );\r
-       else if( tree->outside_node.occupied )\r
-               Sys_FPrintf( SYS_VRB, "entity reached from outside -- no filling\n" );\r
-       \r
-       return (qboolean) (inside && !tree->outside_node.occupied);\r
-}\r
-\r
-/*\r
-=========================================================\r
-\r
-FLOOD AREAS\r
-\r
-=========================================================\r
-*/\r
-\r
-int            c_areas;\r
-\r
-\r
-\r
-/*\r
-FloodAreas_r()\r
-floods through leaf portals to tag leafs with an area\r
-*/\r
-\r
-void FloodAreas_r( node_t *node )\r
-{\r
-       int                     s;\r
-       portal_t        *p;\r
-       brush_t         *b;\r
-       \r
-       \r
-       if( node->areaportal )\r
-       {\r
-               if( node->area == -1 )\r
-                       node->area = c_areas;\r
-               \r
-               /* this node is part of an area portal brush */\r
-               b = node->brushlist->original;\r
-\r
-               /* if the current area has already touched this portal, we are done */\r
-               if( b->portalareas[ 0 ] == c_areas || b->portalareas[ 1 ] == c_areas )\r
-                       return;\r
-               \r
-               // note the current area as bounding the portal\r
-               if( b->portalareas[ 1 ] != -1 )\r
-               {\r
-                       Sys_Printf( "WARNING: areaportal brush %i touches > 2 areas\n", b->brushNum );\r
-                       return;\r
-               }\r
-               if( b->portalareas[ 0 ] != -1 )\r
-                       b->portalareas[ 1 ] = c_areas;\r
-               else\r
-                       b->portalareas[ 0 ] = c_areas;\r
-               \r
-               return;\r
-       }\r
-\r
-       if( node->area != -1 )\r
-               return; \r
-       if( node->cluster == -1 )\r
-               return;\r
-\r
-       node->area = c_areas;\r
-       \r
-       /* ydnar: skybox nodes set the skybox area */\r
-       if( node->skybox )\r
-               skyboxArea = c_areas;\r
-       \r
-       for( p = node->portals; p; p = p->next[ s ] )\r
-       {\r
-               s = (p->nodes[1] == node);\r
-               \r
-               /* ydnar: allow areaportal portals to block area flow */\r
-               if( p->compileFlags & C_AREAPORTAL )\r
-                       continue;\r
-               \r
-               if( !PortalPassable( p ) )\r
-                       continue;\r
-               \r
-               FloodAreas_r( p->nodes[ !s ] );\r
-       }\r
-}\r
-\r
-/*\r
-=============\r
-FindAreas_r\r
-\r
-Just decend the tree, and for each node that hasn't had an\r
-area set, flood fill out from there\r
-=============\r
-*/\r
-void FindAreas_r( node_t *node )\r
-{\r
-       if( node->planenum != PLANENUM_LEAF )\r
-       {\r
-               FindAreas_r( node->children[ 0 ] );\r
-               FindAreas_r( node->children[ 1 ] );\r
-               return;\r
-       }\r
-       \r
-       if( node->opaque || node->areaportal || node->area != -1 )\r
-               return;\r
-       \r
-       FloodAreas_r( node );\r
-       c_areas++;\r
-}\r
-\r
-/*\r
-=============\r
-CheckAreas_r\r
-=============\r
-*/\r
-void CheckAreas_r (node_t *node)\r
-{\r
-       brush_t *b;\r
-\r
-       if (node->planenum != PLANENUM_LEAF)\r
-       {\r
-               CheckAreas_r (node->children[0]);\r
-               CheckAreas_r (node->children[1]);\r
-               return;\r
-       }\r
-\r
-       if (node->opaque)\r
-               return;\r
-\r
-       if (node->cluster != -1)\r
-               if (node->area == -1)\r
-                       Sys_Printf("WARNING: cluster %d has area set to -1\n", node->cluster);\r
-       if (node->areaportal)\r
-       {\r
-               b = node->brushlist->original;\r
-\r
-               // check if the areaportal touches two areas\r
-               if (b->portalareas[0] == -1 || b->portalareas[1] == -1)\r
-                       Sys_Printf ("WARNING: areaportal brush %i doesn't touch two areas\n", b->brushNum);\r
-       }\r
-}\r
-\r
-\r
-\r
-/*\r
-FloodSkyboxArea_r() - ydnar\r
-sets all nodes with the skybox area to skybox\r
-*/\r
-\r
-void FloodSkyboxArea_r( node_t *node )\r
-{\r
-       if( skyboxArea < 0 )\r
-               return;\r
-       \r
-       if( node->planenum != PLANENUM_LEAF )\r
-       {\r
-               FloodSkyboxArea_r( node->children[ 0 ] );\r
-               FloodSkyboxArea_r( node->children[ 1 ] );\r
-               return;\r
-       }\r
-       \r
-       if( node->opaque || node->area != skyboxArea )\r
-               return;\r
-       \r
-       node->skybox = qtrue;\r
-}\r
-\r
-\r
-\r
-/*\r
-FloodAreas()\r
-mark each leaf with an area, bounded by C_AREAPORTAL\r
-*/\r
-\r
-void FloodAreas( tree_t *tree )\r
-{\r
-       Sys_FPrintf( SYS_VRB,"--- FloodAreas ---\n" );\r
-       FindAreas_r( tree->headnode );\r
-       \r
-       /* ydnar: flood all skybox nodes */\r
-       FloodSkyboxArea_r( tree->headnode );\r
-       \r
-       /* check for areaportal brushes that don't touch two areas */\r
-       /* ydnar: fix this rather than just silence the warnings */\r
-       //%     CheckAreas_r( tree->headnode );\r
-\r
-       Sys_FPrintf( SYS_VRB, "%9d areas\n", c_areas );\r
-}\r
-\r
-\r
-\r
-//======================================================\r
-\r
-int            c_outside;\r
-int            c_inside;\r
-int            c_solid;\r
-\r
-void FillOutside_r (node_t *node)\r
-{\r
-       if (node->planenum != PLANENUM_LEAF)\r
-       {\r
-               FillOutside_r (node->children[0]);\r
-               FillOutside_r (node->children[1]);\r
-               return;\r
-       }\r
-\r
-       // anything not reachable by an entity\r
-       // can be filled away\r
-       if (!node->occupied) {\r
-               if ( !node->opaque ) {\r
-                       c_outside++;\r
-                       node->opaque = qtrue;\r
-               } else {\r
-                       c_solid++;\r
-               }\r
-       } else {\r
-               c_inside++;\r
-       }\r
-\r
-}\r
-\r
-/*\r
-=============\r
-FillOutside\r
-\r
-Fill all nodes that can't be reached by entities\r
-=============\r
-*/\r
-void FillOutside (node_t *headnode)\r
-{\r
-       c_outside = 0;\r
-       c_inside = 0;\r
-       c_solid = 0;\r
-       Sys_FPrintf( SYS_VRB,"--- FillOutside ---\n" );\r
-       FillOutside_r( headnode );\r
-       Sys_FPrintf( SYS_VRB,"%9d solid leafs\n", c_solid );\r
-       Sys_Printf( "%9d leafs filled\n", c_outside );\r
-       Sys_FPrintf( SYS_VRB, "%9d inside leafs\n", c_inside );\r
-}\r
-\r
-\r
-//==============================================================\r
-\r
+/*
+Copyright (C) 1999-2007 id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant 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.
+
+GtkRadiant 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 GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+----------------------------------------------------------------------------------
+
+This code has been altered significantly from its original form, to support
+several games based on the Quake III Arena engine, in the form of "Q3Map2."
+
+------------------------------------------------------------------------------- */
+
+
+
+/* marker */
+#define PORTALS_C
+
+
+
+/* dependencies */
+#include "q3map2.h"
+
+
+
+/* ydnar: to fix broken portal windings */
+extern qboolean FixWinding( winding_t *w );
+
+
+int            c_active_portals;
+int            c_peak_portals;
+int            c_boundary;
+int            c_boundary_sides;
+
+/*
+===========
+AllocPortal
+===========
+*/
+portal_t *AllocPortal (void)
+{
+       portal_t        *p;
+       
+       if (numthreads == 1)
+               c_active_portals++;
+       if (c_active_portals > c_peak_portals)
+               c_peak_portals = c_active_portals;
+       
+       p = safe_malloc (sizeof(portal_t));
+       memset (p, 0, sizeof(portal_t));
+       
+       return p;
+}
+
+void FreePortal (portal_t *p)
+{
+       if (p->winding)
+               FreeWinding (p->winding);
+       if (numthreads == 1)
+               c_active_portals--;
+       free (p);
+}
+
+
+
+/*
+PortalPassable
+returns true if the portal has non-opaque leafs on both sides
+*/
+
+qboolean PortalPassable( portal_t *p )
+{
+       /* is this to global outside leaf? */
+       if( !p->onnode )
+               return qfalse;
+       
+       /* this should never happen */
+       if( p->nodes[ 0 ]->planenum != PLANENUM_LEAF ||
+               p->nodes[ 1 ]->planenum != PLANENUM_LEAF )
+               Error( "Portal_EntityFlood: not a leaf" );
+       
+       /* ydnar: added antiportal to supress portal generation for visibility blocking */
+       if( p->compileFlags & C_ANTIPORTAL )
+               return qfalse;
+       
+       /* both leaves on either side of the portal must be passable */
+       if( p->nodes[ 0 ]->opaque == qfalse && p->nodes[ 1 ]->opaque == qfalse )
+               return qtrue;
+       
+       /* otherwise this isn't a passable portal */
+       return qfalse;
+}
+
+
+
+
+int            c_tinyportals;
+int            c_badportals;   /* ydnar */
+
+/*
+=============
+AddPortalToNodes
+=============
+*/
+void AddPortalToNodes (portal_t *p, node_t *front, node_t *back)
+{
+       if (p->nodes[0] || p->nodes[1])
+               Error ("AddPortalToNode: allready included");
+
+       p->nodes[0] = front;
+       p->next[0] = front->portals;
+       front->portals = p;
+       
+       p->nodes[1] = back;
+       p->next[1] = back->portals;
+       back->portals = p;
+}
+
+
+/*
+=============
+RemovePortalFromNode
+=============
+*/
+void RemovePortalFromNode (portal_t *portal, node_t *l)
+{
+       portal_t        **pp, *t;
+       
+// remove reference to the current portal
+       pp = &l->portals;
+       while (1)
+       {
+               t = *pp;
+               if (!t)
+                       Error ("RemovePortalFromNode: portal not in leaf");     
+
+               if ( t == portal )
+                       break;
+
+               if (t->nodes[0] == l)
+                       pp = &t->next[0];
+               else if (t->nodes[1] == l)
+                       pp = &t->next[1];
+               else
+                       Error ("RemovePortalFromNode: portal not bounding leaf");
+       }
+       
+       if (portal->nodes[0] == l)
+       {
+               *pp = portal->next[0];
+               portal->nodes[0] = NULL;
+       }
+       else if (portal->nodes[1] == l)
+       {
+               *pp = portal->next[1];  
+               portal->nodes[1] = NULL;
+       }
+}
+
+//============================================================================
+
+void PrintPortal (portal_t *p)
+{
+       int                     i;
+       winding_t       *w;
+       
+       w = p->winding;
+       for (i=0 ; i<w->numpoints ; i++)
+               Sys_Printf ("(%5.0f,%5.0f,%5.0f)\n",w->p[i][0]
+               , w->p[i][1], w->p[i][2]);
+}
+
+/*
+================
+MakeHeadnodePortals
+
+The created portals will face the global outside_node
+================
+*/
+#define        SIDESPACE       8
+void MakeHeadnodePortals (tree_t *tree)
+{
+       vec3_t          bounds[2];
+       int                     i, j, n;
+       portal_t        *p, *portals[6];
+       plane_t         bplanes[6], *pl;
+       node_t *node;
+
+       node = tree->headnode;
+
+// pad with some space so there will never be null volume leafs
+       for (i=0 ; i<3 ; i++)
+       {
+               bounds[0][i] = tree->mins[i] - SIDESPACE;
+               bounds[1][i] = tree->maxs[i] + SIDESPACE;
+               if ( bounds[0][i] >= bounds[1][i] ) {
+                       Error( "Backwards tree volume" );
+               }
+       }
+       
+       tree->outside_node.planenum = PLANENUM_LEAF;
+       tree->outside_node.brushlist = NULL;
+       tree->outside_node.portals = NULL;
+       tree->outside_node.opaque = qfalse;
+
+       for (i=0 ; i<3 ; i++)
+               for (j=0 ; j<2 ; j++)
+               {
+                       n = j*3 + i;
+
+                       p = AllocPortal ();
+                       portals[n] = p;
+                       
+                       pl = &bplanes[n];
+                       memset (pl, 0, sizeof(*pl));
+                       if (j)
+                       {
+                               pl->normal[i] = -1;
+                               pl->dist = -bounds[j][i];
+                       }
+                       else
+                       {
+                               pl->normal[i] = 1;
+                               pl->dist = bounds[j][i];
+                       }
+                       p->plane = *pl;
+                       p->winding = BaseWindingForPlane (pl->normal, pl->dist);
+                       AddPortalToNodes (p, node, &tree->outside_node);
+               }
+               
+// clip the basewindings by all the other planes
+       for (i=0 ; i<6 ; i++)
+       {
+               for (j=0 ; j<6 ; j++)
+               {
+                       if (j == i)
+                               continue;
+                       ChopWindingInPlace (&portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON);
+               }
+       }
+}
+
+//===================================================
+
+
+/*
+================
+BaseWindingForNode
+================
+*/
+#define        BASE_WINDING_EPSILON    0.001
+#define        SPLIT_WINDING_EPSILON   0.001
+
+winding_t      *BaseWindingForNode (node_t *node)
+{
+       winding_t       *w;
+       node_t          *n;
+       plane_t         *plane;
+       vec3_t          normal;
+       vec_t           dist;
+
+       w = BaseWindingForPlane (mapplanes[node->planenum].normal
+               , mapplanes[node->planenum].dist);
+
+       // clip by all the parents
+       for (n=node->parent ; n && w ; )
+       {
+               plane = &mapplanes[n->planenum];
+
+               if (n->children[0] == node)
+               {       // take front
+                       ChopWindingInPlace (&w, plane->normal, plane->dist, BASE_WINDING_EPSILON);
+               }
+               else
+               {       // take back
+                       VectorSubtract (vec3_origin, plane->normal, normal);
+                       dist = -plane->dist;
+                       ChopWindingInPlace (&w, normal, dist, BASE_WINDING_EPSILON);
+               }
+               node = n;
+               n = n->parent;
+       }
+
+       return w;
+}
+
+//============================================================
+
+/*
+==================
+MakeNodePortal
+
+create the new portal by taking the full plane winding for the cutting plane
+and clipping it by all of parents of this node
+==================
+*/
+void MakeNodePortal (node_t *node)
+{
+       portal_t        *new_portal, *p;
+       winding_t       *w;
+       vec3_t          normal;
+       float           dist;
+       int                     side;
+
+       w = BaseWindingForNode (node);
+
+       // clip the portal by all the other portals in the node
+       for (p = node->portals ; p && w; p = p->next[side])     
+       {
+               if (p->nodes[0] == node)
+               {
+                       side = 0;
+                       VectorCopy (p->plane.normal, normal);
+                       dist = p->plane.dist;
+               }
+               else if (p->nodes[1] == node)
+               {
+                       side = 1;
+                       VectorSubtract (vec3_origin, p->plane.normal, normal);
+                       dist = -p->plane.dist;
+               }
+               else
+                       Error ("CutNodePortals_r: mislinked portal");
+
+               ChopWindingInPlace (&w, normal, dist, CLIP_EPSILON);
+       }
+
+       if (!w)
+       {
+               return;
+       }
+       
+       
+       /* ydnar: adding this here to fix degenerate windings */
+       #if 0
+       if( FixWinding( w ) == qfalse )
+       {
+               c_badportals++;
+               FreeWinding( w );
+               return;
+       }
+       #endif
+       
+       if (WindingIsTiny (w))
+       {
+               c_tinyportals++;
+               FreeWinding (w);
+               return;
+       }
+
+       new_portal = AllocPortal ();
+       new_portal->plane = mapplanes[node->planenum];
+       new_portal->onnode = node;
+       new_portal->winding = w;
+       new_portal->compileFlags = node->compileFlags;
+       AddPortalToNodes (new_portal, node->children[0], node->children[1]);
+}
+
+
+/*
+==============
+SplitNodePortals
+
+Move or split the portals that bound node so that the node's
+children have portals instead of node.
+==============
+*/
+void SplitNodePortals (node_t *node)
+{
+       portal_t        *p, *next_portal, *new_portal;
+       node_t          *f, *b, *other_node;
+       int                     side;
+       plane_t         *plane;
+       winding_t       *frontwinding, *backwinding;
+
+       plane = &mapplanes[node->planenum];
+       f = node->children[0];
+       b = node->children[1];
+
+       for (p = node->portals ; p ; p = next_portal)   
+       {
+               if (p->nodes[0] == node)
+                       side = 0;
+               else if (p->nodes[1] == node)
+                       side = 1;
+               else
+                       Error ("SplitNodePortals: mislinked portal");
+               next_portal = p->next[side];
+
+               other_node = p->nodes[!side];
+               RemovePortalFromNode (p, p->nodes[0]);
+               RemovePortalFromNode (p, p->nodes[1]);
+
+//
+// cut the portal into two portals, one on each side of the cut plane
+//
+               ClipWindingEpsilon (p->winding, plane->normal, plane->dist,
+                       SPLIT_WINDING_EPSILON, &frontwinding, &backwinding);
+
+               if (frontwinding && WindingIsTiny(frontwinding))
+               {
+                       if (!f->tinyportals)
+                               VectorCopy(frontwinding->p[0], f->referencepoint);
+                       f->tinyportals++;
+                       if (!other_node->tinyportals)
+                               VectorCopy(frontwinding->p[0], other_node->referencepoint);
+                       other_node->tinyportals++;
+
+                       FreeWinding (frontwinding);
+                       frontwinding = NULL;
+                       c_tinyportals++;
+               }
+
+               if (backwinding && WindingIsTiny(backwinding))
+               {
+                       if (!b->tinyportals)
+                               VectorCopy(backwinding->p[0], b->referencepoint);
+                       b->tinyportals++;
+                       if (!other_node->tinyportals)
+                               VectorCopy(backwinding->p[0], other_node->referencepoint);
+                       other_node->tinyportals++;
+
+                       FreeWinding (backwinding);
+                       backwinding = NULL;
+                       c_tinyportals++;
+               }
+
+               if (!frontwinding && !backwinding)
+               {       // tiny windings on both sides
+                       continue;
+               }
+
+               if (!frontwinding)
+               {
+                       FreeWinding (backwinding);
+                       if (side == 0)
+                               AddPortalToNodes (p, b, other_node);
+                       else
+                               AddPortalToNodes (p, other_node, b);
+                       continue;
+               }
+               if (!backwinding)
+               {
+                       FreeWinding (frontwinding);
+                       if (side == 0)
+                               AddPortalToNodes (p, f, other_node);
+                       else
+                               AddPortalToNodes (p, other_node, f);
+                       continue;
+               }
+               
+       // the winding is split
+               new_portal = AllocPortal ();
+               *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])
+       {
+               Sys_Printf ("WARNING: node without a volume\n");
+               Sys_Printf("node has %d tiny portals\n", node->tinyportals);
+               Sys_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)
+               {
+      if(node->portals && node->portals->winding)
+        xml_Winding("WARNING: Node With Unbounded Volume", node->portals->winding->p, node->portals->winding->numpoints, qfalse);
+
+                       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)
+{
+       Sys_FPrintf (SYS_VRB, "--- MakeTreePortals ---\n");
+       MakeHeadnodePortals (tree);
+       MakeTreePortals_r (tree->headnode);
+       Sys_FPrintf( SYS_VRB, "%9d tiny portals\n", c_tinyportals );
+       Sys_FPrintf( SYS_VRB, "%9d bad portals\n", c_badportals );      /* ydnar */
+}
+
+/*
+=========================================================
+
+FLOOD ENTITIES
+
+=========================================================
+*/
+
+int            c_floodedleafs;
+
+/*
+=============
+FloodPortals_r
+=============
+*/
+
+void FloodPortals_r( node_t *node, int dist, qboolean skybox )
+{
+       int                     s;
+       portal_t        *p;
+       
+       
+       if( skybox )
+               node->skybox = skybox;
+       
+       if( node->occupied || 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, skybox );
+       }
+}
+
+
+
+/*
+=============
+PlaceOccupant
+=============
+*/
+
+qboolean PlaceOccupant( node_t *headnode, vec3_t origin, entity_t *occupant, qboolean skybox )
+{
+       vec_t   d;
+       node_t  *node;
+       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;
+       node->skybox = skybox;
+       
+       FloodPortals_r( node, 1, skybox );
+       
+       return qtrue;
+}
+
+/*
+=============
+FloodEntities
+
+Marks all nodes that can be reached by entites
+=============
+*/
+
+qboolean FloodEntities( tree_t *tree )
+{
+       int                     i, s;
+       vec3_t          origin, offset, scale, angles;
+       qboolean        r, inside, tripped, skybox;
+       node_t          *headnode;
+       entity_t        *e;
+       const char      *value;
+       
+       
+       headnode = tree->headnode;
+       Sys_FPrintf( SYS_VRB,"--- FloodEntities ---\n" );
+       inside = qfalse;
+       tree->outside_node.occupied = 0;
+       
+       tripped = qfalse;
+       c_floodedleafs = 0;
+       for( i = 1; i < numEntities; i++ )
+       {
+               /* get entity */
+               e = &entities[ i ];
+               
+               /* get origin */
+               GetVectorForKey( e, "origin", origin );
+               if( VectorCompare( origin, vec3_origin ) ) 
+                       continue;
+               
+               /* handle skybox entities */
+               value = ValueForKey( e, "classname" );
+               if( !Q_stricmp( value, "_skybox" ) )
+               {
+                       skybox = qtrue;
+                       skyboxPresent = qtrue;
+                       
+                       /* invert origin */
+                       VectorScale( origin, -1.0f, offset );
+                       
+                       /* get scale */
+                       VectorSet( scale, 64.0f, 64.0f, 64.0f );
+                       value = ValueForKey( e, "_scale" );
+                       if( value[ 0 ] != '\0' )
+                       {
+                               s = sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
+                               if( s == 1 )
+                               {
+                                       scale[ 1 ] = scale[ 0 ];
+                                       scale[ 2 ] = scale[ 0 ];
+                               }
+                       }
+                       
+                       /* get "angle" (yaw) or "angles" (pitch yaw roll) */
+                       VectorClear( angles );
+                       angles[ 2 ] = FloatForKey( e, "angle" );
+                       value = ValueForKey( e, "angles" );
+                       if( value[ 0 ] != '\0' )
+                               sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
+                       
+                       /* set transform matrix (thanks spog) */
+                       m4x4_identity( skyboxTransform );
+                       m4x4_pivoted_transform_by_vec3( skyboxTransform, offset, angles, eXYZ, scale, origin );
+               }
+               else
+                       skybox = qfalse;
+               
+               /* nudge off floor */
+               origin[ 2 ] += 1;
+               
+               /* debugging code */
+               //%     if( i == 1 )
+               //%             origin[ 2 ] += 4096;
+               
+               /* find leaf */
+               r = PlaceOccupant( headnode, origin, e, skybox );
+               if( r )
+                       inside = qtrue;
+               if( (!r || tree->outside_node.occupied) && !tripped )
+               {
+                       xml_Select( "Entity leaked", e->mapEntityNum, 0, qfalse );
+                       tripped = qtrue;
+               }
+       }
+       
+       Sys_FPrintf( SYS_VRB, "%9d flooded leafs\n", c_floodedleafs );
+       
+       if( !inside )
+               Sys_FPrintf( SYS_VRB, "no entities in open -- no filling\n" );
+       else if( tree->outside_node.occupied )
+               Sys_FPrintf( SYS_VRB, "entity reached from outside -- no filling\n" );
+       
+       return (qboolean) (inside && !tree->outside_node.occupied);
+}
+
+/*
+=========================================================
+
+FLOOD AREAS
+
+=========================================================
+*/
+
+int            c_areas;
+
+
+
+/*
+FloodAreas_r()
+floods through leaf portals to tag leafs with an area
+*/
+
+void FloodAreas_r( node_t *node )
+{
+       int                     s;
+       portal_t        *p;
+       brush_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 already 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 )
+               {
+                       Sys_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; 
+       if( node->cluster == -1 )
+               return;
+
+       node->area = c_areas;
+       
+       /* ydnar: skybox nodes set the skybox area */
+       if( node->skybox )
+               skyboxArea = c_areas;
+       
+       for( p = node->portals; p; p = p->next[ s ] )
+       {
+               s = (p->nodes[1] == node);
+               
+               /* ydnar: allow areaportal portals to block area flow */
+               if( p->compileFlags & C_AREAPORTAL )
+                       continue;
+               
+               if( !PortalPassable( 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 || node->areaportal || node->area != -1 )
+               return;
+       
+       FloodAreas_r( node );
+       c_areas++;
+}
+
+/*
+=============
+CheckAreas_r
+=============
+*/
+void CheckAreas_r (node_t *node)
+{
+       brush_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)
+                       Sys_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)
+                       Sys_Printf ("WARNING: areaportal brush %i doesn't touch two areas\n", b->brushNum);
+       }
+}
+
+
+
+/*
+FloodSkyboxArea_r() - ydnar
+sets all nodes with the skybox area to skybox
+*/
+
+void FloodSkyboxArea_r( node_t *node )
+{
+       if( skyboxArea < 0 )
+               return;
+       
+       if( node->planenum != PLANENUM_LEAF )
+       {
+               FloodSkyboxArea_r( node->children[ 0 ] );
+               FloodSkyboxArea_r( node->children[ 1 ] );
+               return;
+       }
+       
+       if( node->opaque || node->area != skyboxArea )
+               return;
+       
+       node->skybox = qtrue;
+}
+
+
+
+/*
+FloodAreas()
+mark each leaf with an area, bounded by C_AREAPORTAL
+*/
+
+void FloodAreas( tree_t *tree )
+{
+       Sys_FPrintf( SYS_VRB,"--- FloodAreas ---\n" );
+       FindAreas_r( tree->headnode );
+       
+       /* ydnar: flood all skybox nodes */
+       FloodSkyboxArea_r( tree->headnode );
+       
+       /* check for areaportal brushes that don't touch two areas */
+       /* ydnar: fix this rather than just silence the warnings */
+       //%     CheckAreas_r( tree->headnode );
+
+       Sys_FPrintf( SYS_VRB, "%9d 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;
+       Sys_FPrintf( SYS_VRB,"--- FillOutside ---\n" );
+       FillOutside_r( headnode );
+       Sys_FPrintf( SYS_VRB,"%9d solid leafs\n", c_solid );
+       Sys_Printf( "%9d leafs filled\n", c_outside );
+       Sys_FPrintf( SYS_VRB, "%9d inside leafs\n", c_inside );
+}
+
+
+//==============================================================
+