1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
41 /* ydnar: to fix broken portal windings */
42 extern qboolean FixWinding( winding_t *w );
55 portal_t *AllocPortal( void ){
58 if ( numthreads == 1 ) {
61 if ( c_active_portals > c_peak_portals ) {
62 c_peak_portals = c_active_portals;
65 p = safe_malloc( sizeof( portal_t ) );
66 memset( p, 0, sizeof( portal_t ) );
71 void FreePortal( portal_t *p ){
73 FreeWinding( p->winding );
75 if ( numthreads == 1 ) {
85 returns true if the portal has non-opaque leafs on both sides
88 qboolean PortalPassable( portal_t *p ){
89 /* is this to global outside leaf? */
94 /* this should never happen */
95 if ( p->nodes[ 0 ]->planenum != PLANENUM_LEAF ||
96 p->nodes[ 1 ]->planenum != PLANENUM_LEAF ) {
97 Error( "Portal_EntityFlood: not a leaf" );
100 /* ydnar: added antiportal to supress portal generation for visibility blocking */
101 if ( p->compileFlags & C_ANTIPORTAL ) {
105 /* both leaves on either side of the portal must be passable */
106 if ( p->nodes[ 0 ]->opaque == qfalse && p->nodes[ 1 ]->opaque == qfalse ) {
110 /* otherwise this isn't a passable portal */
118 int c_badportals; /* ydnar */
125 void AddPortalToNodes( portal_t *p, node_t *front, node_t *back ){
126 if ( p->nodes[0] || p->nodes[1] ) {
127 Error( "AddPortalToNode: allready included" );
131 p->next[0] = front->portals;
135 p->next[1] = back->portals;
145 void RemovePortalFromNode( portal_t *portal, node_t *l ){
148 // remove reference to the current portal
154 Error( "RemovePortalFromNode: portal not in leaf" );
161 if ( t->nodes[0] == l ) {
164 else if ( t->nodes[1] == l ) {
168 Error( "RemovePortalFromNode: portal not bounding leaf" );
172 if ( portal->nodes[0] == l ) {
173 *pp = portal->next[0];
174 portal->nodes[0] = NULL;
176 else if ( portal->nodes[1] == l ) {
177 *pp = portal->next[1];
178 portal->nodes[1] = NULL;
182 //============================================================================
184 void PrintPortal( portal_t *p ){
189 for ( i = 0 ; i < w->numpoints ; i++ )
190 Sys_Printf( "(%5.0f,%5.0f,%5.0f)\n",w->p[i][0]
191 , w->p[i][1], w->p[i][2] );
198 The created portals will face the global outside_node
202 void MakeHeadnodePortals( tree_t *tree ){
205 portal_t *p, *portals[6];
206 plane_t bplanes[6], *pl;
209 node = tree->headnode;
211 // pad with some space so there will never be null volume leafs
212 for ( i = 0 ; i < 3 ; i++ )
214 bounds[0][i] = tree->mins[i] - SIDESPACE;
215 bounds[1][i] = tree->maxs[i] + SIDESPACE;
216 if ( bounds[0][i] >= bounds[1][i] ) {
217 Error( "Backwards tree volume" );
221 tree->outside_node.planenum = PLANENUM_LEAF;
222 tree->outside_node.brushlist = NULL;
223 tree->outside_node.portals = NULL;
224 tree->outside_node.opaque = qfalse;
226 for ( i = 0 ; i < 3 ; i++ )
227 for ( j = 0 ; j < 2 ; j++ )
235 memset( pl, 0, sizeof( *pl ) );
238 pl->dist = -bounds[j][i];
243 pl->dist = bounds[j][i];
246 p->winding = BaseWindingForPlane( pl->normal, pl->dist );
247 AddPortalToNodes( p, node, &tree->outside_node );
250 // clip the basewindings by all the other planes
251 for ( i = 0 ; i < 6 ; i++ )
253 for ( j = 0 ; j < 6 ; j++ )
258 ChopWindingInPlace( &portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON );
263 //===================================================
271 #define BASE_WINDING_EPSILON 0.001
272 #define SPLIT_WINDING_EPSILON 0.001
274 winding_t *BaseWindingForNode( node_t *node ){
281 w = BaseWindingForPlane( mapplanes[node->planenum].normal
282 , mapplanes[node->planenum].dist );
284 // clip by all the parents
285 for ( n = node->parent ; n && w ; )
287 plane = &mapplanes[n->planenum];
289 if ( n->children[0] == node ) { // take front
290 ChopWindingInPlace( &w, plane->normal, plane->dist, BASE_WINDING_EPSILON );
294 VectorSubtract( vec3_origin, plane->normal, normal );
296 ChopWindingInPlace( &w, normal, dist, BASE_WINDING_EPSILON );
305 //============================================================
311 create the new portal by taking the full plane winding for the cutting plane
312 and clipping it by all of parents of this node
315 void MakeNodePortal( node_t *node ){
316 portal_t *new_portal, *p;
322 w = BaseWindingForNode( node );
324 // clip the portal by all the other portals in the node
325 for ( p = node->portals ; p && w; p = p->next[side] )
327 if ( p->nodes[0] == node ) {
329 VectorCopy( p->plane.normal, normal );
330 dist = p->plane.dist;
332 else if ( p->nodes[1] == node ) {
334 VectorSubtract( vec3_origin, p->plane.normal, normal );
335 dist = -p->plane.dist;
338 Error( "CutNodePortals_r: mislinked portal" );
341 ChopWindingInPlace( &w, normal, dist, CLIP_EPSILON );
349 /* ydnar: adding this here to fix degenerate windings */
351 if ( FixWinding( w ) == qfalse ) {
358 if ( WindingIsTiny( w ) ) {
364 new_portal = AllocPortal();
365 new_portal->plane = mapplanes[node->planenum];
366 new_portal->onnode = node;
367 new_portal->winding = w;
368 new_portal->compileFlags = node->compileFlags;
369 AddPortalToNodes( new_portal, node->children[0], node->children[1] );
377 Move or split the portals that bound node so that the node's
378 children have portals instead of node.
381 void SplitNodePortals( node_t *node ){
382 portal_t *p, *next_portal, *new_portal;
383 node_t *f, *b, *other_node;
386 winding_t *frontwinding, *backwinding;
388 plane = &mapplanes[node->planenum];
389 f = node->children[0];
390 b = node->children[1];
392 for ( p = node->portals ; p ; p = next_portal )
394 if ( p->nodes[0] == node ) {
397 else if ( p->nodes[1] == node ) {
401 Error( "SplitNodePortals: mislinked portal" );
403 next_portal = p->next[side];
405 other_node = p->nodes[!side];
406 RemovePortalFromNode( p, p->nodes[0] );
407 RemovePortalFromNode( p, p->nodes[1] );
410 // cut the portal into two portals, one on each side of the cut plane
412 ClipWindingEpsilon( p->winding, plane->normal, plane->dist,
413 SPLIT_WINDING_EPSILON, &frontwinding, &backwinding ); /* not strict, we want to always keep one of them even if coplanar */
415 if ( frontwinding && WindingIsTiny( frontwinding ) ) {
416 if ( !f->tinyportals ) {
417 VectorCopy( frontwinding->p[0], f->referencepoint );
420 if ( !other_node->tinyportals ) {
421 VectorCopy( frontwinding->p[0], other_node->referencepoint );
423 other_node->tinyportals++;
425 FreeWinding( frontwinding );
430 if ( backwinding && WindingIsTiny( backwinding ) ) {
431 if ( !b->tinyportals ) {
432 VectorCopy( backwinding->p[0], b->referencepoint );
435 if ( !other_node->tinyportals ) {
436 VectorCopy( backwinding->p[0], other_node->referencepoint );
438 other_node->tinyportals++;
440 FreeWinding( backwinding );
445 if ( !frontwinding && !backwinding ) { // tiny windings on both sides
449 if ( !frontwinding ) {
450 FreeWinding( backwinding );
452 AddPortalToNodes( p, b, other_node );
455 AddPortalToNodes( p, other_node, b );
459 if ( !backwinding ) {
460 FreeWinding( frontwinding );
462 AddPortalToNodes( p, f, other_node );
465 AddPortalToNodes( p, other_node, f );
470 // the winding is split
471 new_portal = AllocPortal();
473 new_portal->winding = backwinding;
474 FreeWinding( p->winding );
475 p->winding = frontwinding;
478 AddPortalToNodes( p, f, other_node );
479 AddPortalToNodes( new_portal, b, other_node );
483 AddPortalToNodes( p, other_node, f );
484 AddPortalToNodes( new_portal, other_node, b );
488 node->portals = NULL;
497 void CalcNodeBounds( node_t *node ){
502 // calc mins/maxs for both leafs and nodes
503 ClearBounds( node->mins, node->maxs );
504 for ( p = node->portals ; p ; p = p->next[s] )
506 s = ( p->nodes[1] == node );
507 for ( i = 0 ; i < p->winding->numpoints ; i++ )
508 AddPointToBounds( p->winding->p[i], node->mins, node->maxs );
517 void MakeTreePortals_r( node_t *node ){
520 CalcNodeBounds( node );
521 if ( node->mins[0] >= node->maxs[0] ) {
522 Sys_Printf( "WARNING: node without a volume\n" );
523 Sys_Printf( "node has %d tiny portals\n", node->tinyportals );
524 Sys_Printf( "node reference point %1.2f %1.2f %1.2f\n", node->referencepoint[0],
525 node->referencepoint[1],
526 node->referencepoint[2] );
529 for ( i = 0 ; i < 3 ; i++ )
531 if ( node->mins[i] < MIN_WORLD_COORD || node->maxs[i] > MAX_WORLD_COORD ) {
532 if ( node->portals && node->portals->winding ) {
533 xml_Winding( "WARNING: Node With Unbounded Volume", node->portals->winding->p, node->portals->winding->numpoints, qfalse );
539 if ( node->planenum == PLANENUM_LEAF ) {
543 MakeNodePortal( node );
544 SplitNodePortals( node );
546 MakeTreePortals_r( node->children[0] );
547 MakeTreePortals_r( node->children[1] );
555 void MakeTreePortals( tree_t *tree ){
556 Sys_FPrintf( SYS_VRB, "--- MakeTreePortals ---\n" );
557 MakeHeadnodePortals( tree );
558 MakeTreePortals_r( tree->headnode );
559 Sys_FPrintf( SYS_VRB, "%9d tiny portals\n", c_tinyportals );
560 Sys_FPrintf( SYS_VRB, "%9d bad portals\n", c_badportals ); /* ydnar */
564 =========================================================
568 =========================================================
579 void FloodPortals_r( node_t *node, int dist, qboolean skybox ){
585 node->skybox = skybox;
588 if ( node->opaque ) {
592 if ( node->occupied ) {
593 if ( node->occupied > dist ) {
594 /* reduce distance! */
595 /* for better leak line */
596 /* note: node->occupied will also be true for all further nodes, then */
597 node->occupied = dist;
598 for ( p = node->portals; p; p = p->next[ s ] )
600 s = ( p->nodes[ 1 ] == node );
601 FloodPortals_r( p->nodes[ !s ], dist + 1, skybox );
608 node->occupied = dist;
610 for ( p = node->portals; p; p = p->next[ s ] )
612 s = ( p->nodes[ 1 ] == node );
613 FloodPortals_r( p->nodes[ !s ], dist + 1, skybox );
625 qboolean PlaceOccupant( node_t *headnode, vec3_t origin, entity_t *occupant, qboolean skybox ){
631 // find the leaf to start in
633 while ( node->planenum != PLANENUM_LEAF )
635 plane = &mapplanes[ node->planenum ];
636 d = DotProduct( origin, plane->normal ) - plane->dist;
638 node = node->children[ 0 ];
641 node = node->children[ 1 ];
645 if ( node->opaque ) {
648 node->occupant = occupant;
649 node->skybox = skybox;
651 FloodPortals_r( node, 1, skybox );
660 Marks all nodes that can be reached by entites
664 int FloodEntities( tree_t *tree ){
666 vec3_t origin, offset, scale, angles;
667 qboolean r, inside, skybox;
669 entity_t *e, *tripped;
674 headnode = tree->headnode;
675 Sys_FPrintf( SYS_VRB,"--- FloodEntities ---\n" );
677 tree->outside_node.occupied = 0;
681 for ( i = 1; i < numEntities; i++ )
687 GetVectorForKey( e, "origin", origin );
689 /* as a special case, allow origin-less entities */
690 if ( VectorCompare( origin, vec3_origin ) ) {
694 /* also allow bmodel entities outside, as they could be on a moving path that will go into the map */
695 if ( e->brushes != NULL || e->patches != NULL ) {
699 /* handle skybox entities */
700 value = ValueForKey( e, "classname" );
701 if ( !Q_stricmp( value, "_skybox" ) ) {
703 skyboxPresent = qtrue;
706 VectorScale( origin, -1.0f, offset );
709 VectorSet( scale, 64.0f, 64.0f, 64.0f );
710 value = ValueForKey( e, "_scale" );
711 if ( value[ 0 ] != '\0' ) {
712 s = sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
714 scale[ 1 ] = scale[ 0 ];
715 scale[ 2 ] = scale[ 0 ];
719 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
720 VectorClear( angles );
721 angles[ 2 ] = FloatForKey( e, "angle" );
722 value = ValueForKey( e, "angles" );
723 if ( value[ 0 ] != '\0' ) {
724 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
727 /* set transform matrix (thanks spog) */
728 m4x4_identity( skyboxTransform );
729 m4x4_pivoted_transform_by_vec3( skyboxTransform, offset, angles, eXYZ, scale, origin );
735 /* nudge off floor */
740 //% origin[ 2 ] += 4096;
743 r = PlaceOccupant( headnode, origin, e, skybox );
748 Sys_Printf( "Entity %i, Brush %i: Entity in solid\n", e->mapEntityNum, 0 );
750 else if ( tree->outside_node.occupied ) {
751 if ( !tripped || tree->outside_node.occupied < tripcount ) {
753 tripcount = tree->outside_node.occupied;
759 xml_Select( "Entity leaked", e->mapEntityNum, 0, qfalse );
762 Sys_FPrintf( SYS_VRB, "%9d flooded leafs\n", c_floodedleafs );
765 Sys_FPrintf( SYS_VRB, "no entities in open -- no filling\n" );
766 return FLOODENTITIES_EMPTY;
768 if ( tree->outside_node.occupied ) {
769 Sys_FPrintf( SYS_VRB, "entity reached from outside -- leak detected\n" );
770 return FLOODENTITIES_LEAKED;
773 return FLOODENTITIES_GOOD;
777 =========================================================
781 =========================================================
790 floods through leaf portals to tag leafs with an area
793 void FloodAreas_r( node_t *node ){
799 if ( node->areaportal ) {
800 if ( node->area == -1 ) {
801 node->area = c_areas;
804 /* this node is part of an area portal brush */
805 b = node->brushlist->original;
807 /* if the current area has already touched this portal, we are done */
808 if ( b->portalareas[ 0 ] == c_areas || b->portalareas[ 1 ] == c_areas ) {
812 // note the current area as bounding the portal
813 if ( b->portalareas[ 1 ] != -1 ) {
814 Sys_Printf( "WARNING: areaportal brush %i touches > 2 areas\n", b->brushNum );
817 if ( b->portalareas[ 0 ] != -1 ) {
818 b->portalareas[ 1 ] = c_areas;
821 b->portalareas[ 0 ] = c_areas;
827 if ( node->area != -1 ) {
830 if ( node->cluster == -1 ) {
834 node->area = c_areas;
836 /* ydnar: skybox nodes set the skybox area */
837 if ( node->skybox ) {
838 skyboxArea = c_areas;
841 for ( p = node->portals; p; p = p->next[ s ] )
843 s = ( p->nodes[1] == node );
845 /* ydnar: allow areaportal portals to block area flow */
846 if ( p->compileFlags & C_AREAPORTAL ) {
850 if ( !PortalPassable( p ) ) {
854 FloodAreas_r( p->nodes[ !s ] );
862 Just decend the tree, and for each node that hasn't had an
863 area set, flood fill out from there
866 void FindAreas_r( node_t *node ){
867 if ( node->planenum != PLANENUM_LEAF ) {
868 FindAreas_r( node->children[ 0 ] );
869 FindAreas_r( node->children[ 1 ] );
873 if ( node->opaque || node->areaportal || node->area != -1 ) {
877 FloodAreas_r( node );
886 void CheckAreas_r( node_t *node ){
889 if ( node->planenum != PLANENUM_LEAF ) {
890 CheckAreas_r( node->children[0] );
891 CheckAreas_r( node->children[1] );
895 if ( node->opaque ) {
899 if ( node->cluster != -1 ) {
900 if ( node->area == -1 ) {
901 Sys_Printf( "WARNING: cluster %d has area set to -1\n", node->cluster );
904 if ( node->areaportal ) {
905 b = node->brushlist->original;
907 // check if the areaportal touches two areas
908 if ( b->portalareas[0] == -1 || b->portalareas[1] == -1 ) {
909 Sys_Printf( "WARNING: areaportal brush %i doesn't touch two areas\n", b->brushNum );
917 FloodSkyboxArea_r() - ydnar
918 sets all nodes with the skybox area to skybox
921 void FloodSkyboxArea_r( node_t *node ){
922 if ( skyboxArea < 0 ) {
926 if ( node->planenum != PLANENUM_LEAF ) {
927 FloodSkyboxArea_r( node->children[ 0 ] );
928 FloodSkyboxArea_r( node->children[ 1 ] );
932 if ( node->opaque || node->area != skyboxArea ) {
936 node->skybox = qtrue;
943 mark each leaf with an area, bounded by C_AREAPORTAL
946 void FloodAreas( tree_t *tree ){
947 Sys_FPrintf( SYS_VRB,"--- FloodAreas ---\n" );
948 FindAreas_r( tree->headnode );
950 /* ydnar: flood all skybox nodes */
951 FloodSkyboxArea_r( tree->headnode );
953 /* check for areaportal brushes that don't touch two areas */
954 /* ydnar: fix this rather than just silence the warnings */
955 //% CheckAreas_r( tree->headnode );
957 Sys_FPrintf( SYS_VRB, "%9d areas\n", c_areas );
962 //======================================================
968 void FillOutside_r( node_t *node ){
969 if ( node->planenum != PLANENUM_LEAF ) {
970 FillOutside_r( node->children[0] );
971 FillOutside_r( node->children[1] );
975 // anything not reachable by an entity
976 // can be filled away
977 if ( !node->occupied ) {
978 if ( !node->opaque ) {
980 node->opaque = qtrue;
996 Fill all nodes that can't be reached by entities
999 void FillOutside( node_t *headnode ){
1003 Sys_FPrintf( SYS_VRB,"--- FillOutside ---\n" );
1004 FillOutside_r( headnode );
1005 Sys_FPrintf( SYS_VRB,"%9d solid leafs\n", c_solid );
1006 Sys_Printf( "%9d leafs filled\n", c_outside );
1007 Sys_FPrintf( SYS_VRB, "%9d inside leafs\n", c_inside );
1011 //==============================================================