1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2006 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)
61 if (c_active_portals > c_peak_portals)
62 c_peak_portals = c_active_portals;
64 p = safe_malloc (sizeof(portal_t));
65 memset (p, 0, sizeof(portal_t));
70 void FreePortal (portal_t *p)
73 FreeWinding (p->winding);
83 returns true if the portal has non-opaque leafs on both sides
86 qboolean PortalPassable( portal_t *p )
88 /* is this to global outside leaf? */
92 /* this should never happen */
93 if( p->nodes[ 0 ]->planenum != PLANENUM_LEAF ||
94 p->nodes[ 1 ]->planenum != PLANENUM_LEAF )
95 Error( "Portal_EntityFlood: not a leaf" );
97 /* ydnar: added antiportal to supress portal generation for visibility blocking */
98 if( p->compileFlags & C_ANTIPORTAL )
101 /* both leaves on either side of the portal must be passable */
102 if( p->nodes[ 0 ]->opaque == qfalse && p->nodes[ 1 ]->opaque == qfalse )
105 /* otherwise this isn't a passable portal */
113 int c_badportals; /* ydnar */
120 void AddPortalToNodes (portal_t *p, node_t *front, node_t *back)
122 if (p->nodes[0] || p->nodes[1])
123 Error ("AddPortalToNode: allready included");
126 p->next[0] = front->portals;
130 p->next[1] = back->portals;
140 void RemovePortalFromNode (portal_t *portal, node_t *l)
144 // remove reference to the current portal
150 Error ("RemovePortalFromNode: portal not in leaf");
155 if (t->nodes[0] == l)
157 else if (t->nodes[1] == l)
160 Error ("RemovePortalFromNode: portal not bounding leaf");
163 if (portal->nodes[0] == l)
165 *pp = portal->next[0];
166 portal->nodes[0] = NULL;
168 else if (portal->nodes[1] == l)
170 *pp = portal->next[1];
171 portal->nodes[1] = NULL;
175 //============================================================================
177 void PrintPortal (portal_t *p)
183 for (i=0 ; i<w->numpoints ; i++)
184 Sys_Printf ("(%5.0f,%5.0f,%5.0f)\n",w->p[i][0]
185 , w->p[i][1], w->p[i][2]);
192 The created portals will face the global outside_node
196 void MakeHeadnodePortals (tree_t *tree)
200 portal_t *p, *portals[6];
201 plane_t bplanes[6], *pl;
204 node = tree->headnode;
206 // pad with some space so there will never be null volume leafs
207 for (i=0 ; i<3 ; i++)
209 bounds[0][i] = tree->mins[i] - SIDESPACE;
210 bounds[1][i] = tree->maxs[i] + SIDESPACE;
211 if ( bounds[0][i] >= bounds[1][i] ) {
212 Error( "Backwards tree volume" );
216 tree->outside_node.planenum = PLANENUM_LEAF;
217 tree->outside_node.brushlist = NULL;
218 tree->outside_node.portals = NULL;
219 tree->outside_node.opaque = qfalse;
221 for (i=0 ; i<3 ; i++)
222 for (j=0 ; j<2 ; j++)
230 memset (pl, 0, sizeof(*pl));
234 pl->dist = -bounds[j][i];
239 pl->dist = bounds[j][i];
242 p->winding = BaseWindingForPlane (pl->normal, pl->dist);
243 AddPortalToNodes (p, node, &tree->outside_node);
246 // clip the basewindings by all the other planes
247 for (i=0 ; i<6 ; i++)
249 for (j=0 ; j<6 ; j++)
253 ChopWindingInPlace (&portals[i]->winding, bplanes[j].normal, bplanes[j].dist, ON_EPSILON);
258 //===================================================
266 #define BASE_WINDING_EPSILON 0.001
267 #define SPLIT_WINDING_EPSILON 0.001
269 winding_t *BaseWindingForNode (node_t *node)
277 w = BaseWindingForPlane (mapplanes[node->planenum].normal
278 , mapplanes[node->planenum].dist);
280 // clip by all the parents
281 for (n=node->parent ; n && w ; )
283 plane = &mapplanes[n->planenum];
285 if (n->children[0] == node)
287 ChopWindingInPlace (&w, plane->normal, plane->dist, BASE_WINDING_EPSILON);
291 VectorSubtract (vec3_origin, plane->normal, normal);
293 ChopWindingInPlace (&w, normal, dist, BASE_WINDING_EPSILON);
302 //============================================================
308 create the new portal by taking the full plane winding for the cutting plane
309 and clipping it by all of parents of this node
312 void MakeNodePortal (node_t *node)
314 portal_t *new_portal, *p;
320 w = BaseWindingForNode (node);
322 // clip the portal by all the other portals in the node
323 for (p = node->portals ; p && w; p = p->next[side])
325 if (p->nodes[0] == node)
328 VectorCopy (p->plane.normal, normal);
329 dist = p->plane.dist;
331 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");
340 ChopWindingInPlace (&w, normal, dist, CLIP_EPSILON);
349 /* ydnar: adding this here to fix degenerate windings */
351 if( FixWinding( w ) == qfalse )
359 if (WindingIsTiny (w))
366 new_portal = AllocPortal ();
367 new_portal->plane = mapplanes[node->planenum];
368 new_portal->onnode = node;
369 new_portal->winding = w;
370 new_portal->compileFlags = node->compileFlags;
371 AddPortalToNodes (new_portal, node->children[0], node->children[1]);
379 Move or split the portals that bound node so that the node's
380 children have portals instead of node.
383 void SplitNodePortals (node_t *node)
385 portal_t *p, *next_portal, *new_portal;
386 node_t *f, *b, *other_node;
389 winding_t *frontwinding, *backwinding;
391 plane = &mapplanes[node->planenum];
392 f = node->children[0];
393 b = node->children[1];
395 for (p = node->portals ; p ; p = next_portal)
397 if (p->nodes[0] == node)
399 else if (p->nodes[1] == node)
402 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);
415 if (frontwinding && WindingIsTiny(frontwinding))
418 VectorCopy(frontwinding->p[0], f->referencepoint);
420 if (!other_node->tinyportals)
421 VectorCopy(frontwinding->p[0], other_node->referencepoint);
422 other_node->tinyportals++;
424 FreeWinding (frontwinding);
429 if (backwinding && WindingIsTiny(backwinding))
432 VectorCopy(backwinding->p[0], b->referencepoint);
434 if (!other_node->tinyportals)
435 VectorCopy(backwinding->p[0], other_node->referencepoint);
436 other_node->tinyportals++;
438 FreeWinding (backwinding);
443 if (!frontwinding && !backwinding)
444 { // tiny windings on both sides
450 FreeWinding (backwinding);
452 AddPortalToNodes (p, b, other_node);
454 AddPortalToNodes (p, other_node, b);
459 FreeWinding (frontwinding);
461 AddPortalToNodes (p, f, other_node);
463 AddPortalToNodes (p, other_node, f);
467 // the winding is split
468 new_portal = AllocPortal ();
470 new_portal->winding = backwinding;
471 FreeWinding (p->winding);
472 p->winding = frontwinding;
476 AddPortalToNodes (p, f, other_node);
477 AddPortalToNodes (new_portal, b, other_node);
481 AddPortalToNodes (p, other_node, f);
482 AddPortalToNodes (new_portal, other_node, b);
486 node->portals = NULL;
495 void CalcNodeBounds (node_t *node)
501 // calc mins/maxs for both leafs and nodes
502 ClearBounds (node->mins, node->maxs);
503 for (p = node->portals ; p ; p = p->next[s])
505 s = (p->nodes[1] == node);
506 for (i=0 ; i<p->winding->numpoints ; i++)
507 AddPointToBounds (p->winding->p[i], node->mins, node->maxs);
516 void MakeTreePortals_r (node_t *node)
520 CalcNodeBounds (node);
521 if (node->mins[0] >= node->maxs[0])
523 Sys_Printf ("WARNING: node without a volume\n");
524 Sys_Printf("node has %d tiny portals\n", node->tinyportals);
525 Sys_Printf("node reference point %1.2f %1.2f %1.2f\n", node->referencepoint[0],
526 node->referencepoint[1],
527 node->referencepoint[2]);
530 for (i=0 ; i<3 ; i++)
532 if (node->mins[i] < MIN_WORLD_COORD || node->maxs[i] > MAX_WORLD_COORD)
534 if(node->portals && node->portals->winding)
535 xml_Winding("WARNING: Node With Unbounded Volume", node->portals->winding->p, node->portals->winding->numpoints, qfalse);
540 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)
557 Sys_FPrintf (SYS_VRB, "--- MakeTreePortals ---\n");
558 MakeHeadnodePortals (tree);
559 MakeTreePortals_r (tree->headnode);
560 Sys_FPrintf( SYS_VRB, "%9d tiny portals\n", c_tinyportals );
561 Sys_FPrintf( SYS_VRB, "%9d bad portals\n", c_badportals ); /* ydnar */
565 =========================================================
569 =========================================================
580 void FloodPortals_r( node_t *node, int dist, qboolean skybox )
587 node->skybox = skybox;
589 if( node->occupied || node->opaque )
593 node->occupied = dist;
595 for( p = node->portals; p; p = p->next[ s ] )
597 s = (p->nodes[ 1 ] == node);
598 FloodPortals_r( p->nodes[ !s ], dist + 1, skybox );
610 qboolean PlaceOccupant( node_t *headnode, vec3_t origin, entity_t *occupant, qboolean skybox )
617 // find the leaf to start in
619 while( node->planenum != PLANENUM_LEAF )
621 plane = &mapplanes[ node->planenum ];
622 d = DotProduct( origin, plane->normal ) - plane->dist;
624 node = node->children[ 0 ];
626 node = node->children[ 1 ];
631 node->occupant = occupant;
632 node->skybox = skybox;
634 FloodPortals_r( node, 1, skybox );
643 Marks all nodes that can be reached by entites
647 qboolean FloodEntities( tree_t *tree )
650 vec3_t origin, offset, scale, angles;
651 qboolean r, inside, tripped, skybox;
657 headnode = tree->headnode;
658 Sys_FPrintf( SYS_VRB,"--- FloodEntities ---\n" );
660 tree->outside_node.occupied = 0;
664 for( i = 1; i < numEntities; i++ )
670 GetVectorForKey( e, "origin", origin );
671 if( VectorCompare( origin, vec3_origin ) )
674 /* handle skybox entities */
675 value = ValueForKey( e, "classname" );
676 if( !Q_stricmp( value, "_skybox" ) )
679 skyboxPresent = qtrue;
682 VectorScale( origin, -1.0f, offset );
685 VectorSet( scale, 64.0f, 64.0f, 64.0f );
686 value = ValueForKey( e, "_scale" );
687 if( value[ 0 ] != '\0' )
689 s = sscanf( value, "%f %f %f", &scale[ 0 ], &scale[ 1 ], &scale[ 2 ] );
692 scale[ 1 ] = scale[ 0 ];
693 scale[ 2 ] = scale[ 0 ];
697 /* get "angle" (yaw) or "angles" (pitch yaw roll) */
698 VectorClear( angles );
699 angles[ 2 ] = FloatForKey( e, "angle" );
700 value = ValueForKey( e, "angles" );
701 if( value[ 0 ] != '\0' )
702 sscanf( value, "%f %f %f", &angles[ 1 ], &angles[ 2 ], &angles[ 0 ] );
704 /* set transform matrix (thanks spog) */
705 m4x4_identity( skyboxTransform );
706 m4x4_pivoted_transform_by_vec3( skyboxTransform, offset, angles, eXYZ, scale, origin );
711 /* nudge off floor */
716 //% origin[ 2 ] += 4096;
719 r = PlaceOccupant( headnode, origin, e, skybox );
722 if( (!r || tree->outside_node.occupied) && !tripped )
724 xml_Select( "Entity leaked", e->mapEntityNum, 0, qfalse );
729 Sys_FPrintf( SYS_VRB, "%9d flooded leafs\n", c_floodedleafs );
732 Sys_FPrintf( SYS_VRB, "no entities in open -- no filling\n" );
733 else if( tree->outside_node.occupied )
734 Sys_FPrintf( SYS_VRB, "entity reached from outside -- no filling\n" );
736 return (qboolean) (inside && !tree->outside_node.occupied);
740 =========================================================
744 =========================================================
753 floods through leaf portals to tag leafs with an area
756 void FloodAreas_r( node_t *node )
763 if( node->areaportal )
765 if( node->area == -1 )
766 node->area = c_areas;
768 /* this node is part of an area portal brush */
769 b = node->brushlist->original;
771 /* if the current area has already touched this portal, we are done */
772 if( b->portalareas[ 0 ] == c_areas || b->portalareas[ 1 ] == c_areas )
775 // note the current area as bounding the portal
776 if( b->portalareas[ 1 ] != -1 )
778 Sys_Printf( "WARNING: areaportal brush %i touches > 2 areas\n", b->brushNum );
781 if( b->portalareas[ 0 ] != -1 )
782 b->portalareas[ 1 ] = c_areas;
784 b->portalareas[ 0 ] = c_areas;
789 if( node->area != -1 )
791 if( node->cluster == -1 )
794 node->area = c_areas;
796 /* ydnar: skybox nodes set the skybox area */
798 skyboxArea = c_areas;
800 for( p = node->portals; p; p = p->next[ s ] )
802 s = (p->nodes[1] == node);
804 /* ydnar: allow areaportal portals to block area flow */
805 if( p->compileFlags & C_AREAPORTAL )
808 if( !PortalPassable( p ) )
811 FloodAreas_r( p->nodes[ !s ] );
819 Just decend the tree, and for each node that hasn't had an
820 area set, flood fill out from there
823 void FindAreas_r( node_t *node )
825 if( node->planenum != PLANENUM_LEAF )
827 FindAreas_r( node->children[ 0 ] );
828 FindAreas_r( node->children[ 1 ] );
832 if( node->opaque || node->areaportal || node->area != -1 )
835 FloodAreas_r( node );
844 void CheckAreas_r (node_t *node)
848 if (node->planenum != PLANENUM_LEAF)
850 CheckAreas_r (node->children[0]);
851 CheckAreas_r (node->children[1]);
858 if (node->cluster != -1)
859 if (node->area == -1)
860 Sys_Printf("WARNING: cluster %d has area set to -1\n", node->cluster);
861 if (node->areaportal)
863 b = node->brushlist->original;
865 // check if the areaportal touches two areas
866 if (b->portalareas[0] == -1 || b->portalareas[1] == -1)
867 Sys_Printf ("WARNING: areaportal brush %i doesn't touch two areas\n", b->brushNum);
874 FloodSkyboxArea_r() - ydnar
875 sets all nodes with the skybox area to skybox
878 void FloodSkyboxArea_r( node_t *node )
883 if( node->planenum != PLANENUM_LEAF )
885 FloodSkyboxArea_r( node->children[ 0 ] );
886 FloodSkyboxArea_r( node->children[ 1 ] );
890 if( node->opaque || node->area != skyboxArea )
893 node->skybox = qtrue;
900 mark each leaf with an area, bounded by C_AREAPORTAL
903 void FloodAreas( tree_t *tree )
905 Sys_FPrintf( SYS_VRB,"--- FloodAreas ---\n" );
906 FindAreas_r( tree->headnode );
908 /* ydnar: flood all skybox nodes */
909 FloodSkyboxArea_r( tree->headnode );
911 /* check for areaportal brushes that don't touch two areas */
912 /* ydnar: fix this rather than just silence the warnings */
913 //% CheckAreas_r( tree->headnode );
915 Sys_FPrintf( SYS_VRB, "%9d areas\n", c_areas );
920 //======================================================
926 void FillOutside_r (node_t *node)
928 if (node->planenum != PLANENUM_LEAF)
930 FillOutside_r (node->children[0]);
931 FillOutside_r (node->children[1]);
935 // anything not reachable by an entity
936 // can be filled away
937 if (!node->occupied) {
938 if ( !node->opaque ) {
940 node->opaque = qtrue;
954 Fill all nodes that can't be reached by entities
957 void FillOutside (node_t *headnode)
962 Sys_FPrintf( SYS_VRB,"--- FillOutside ---\n" );
963 FillOutside_r( headnode );
964 Sys_FPrintf( SYS_VRB,"%9d solid leafs\n", c_solid );
965 Sys_Printf( "%9d leafs filled\n", c_outside );
966 Sys_FPrintf( SYS_VRB, "%9d inside leafs\n", c_inside );
970 //==============================================================