]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/vis.c
Merge branch 'master' into divVerent/farplanedist-sky-fix
[xonotic/netradiant.git] / tools / quake3 / q3map2 / vis.c
index e97144bcd5701dae2352b1e036ba31745110da83..b166c91fc4f576f46a1f964dad2c98b31d83e820 100644 (file)
@@ -99,10 +99,10 @@ void prl( leaf_t *l ){
    =============
  */
 int PComp( const void *a, const void *b ){
-       if ( ( *(vportal_t **)a )->nummightsee == ( *(vportal_t **)b )->nummightsee ) {
+       if ( ( *(const vportal_t *const *)a )->nummightsee == ( *(const vportal_t *const *)b )->nummightsee ) {
                return 0;
        }
-       if ( ( *(vportal_t **)a )->nummightsee < ( *(vportal_t **)b )->nummightsee ) {
+       if ( ( *(const vportal_t *const *)a )->nummightsee < ( *(const vportal_t *const *)b )->nummightsee ) {
                return -1;
        }
        return 1;
@@ -163,6 +163,7 @@ int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){
    Merges the portal visibility for a leaf
    ===============
  */
+static int clustersizehistogram[MAX_MAP_LEAFS] = {0};
 void ClusterMerge( int leafnum ){
        leaf_t      *leaf;
        byte portalvector[MAX_PORTALS / 8];
@@ -209,9 +210,8 @@ void ClusterMerge( int leafnum ){
 
        numvis++;       // count the leaf itself
 
-       totalvis += numvis;
-
-       Sys_FPrintf( SYS_VRB,"cluster %4i : %4i visible\n", leafnum, numvis );
+       //Sys_FPrintf (SYS_VRB,"cluster %4i : %4i visible\n", leafnum, numvis);
+       ++clustersizehistogram[numvis];
 
        memcpy( bspVisBytes + VIS_HEADER_SIZE + leafnum * leafbytes, uncompressed, leafbytes );
 }
@@ -302,8 +302,9 @@ void CalcFastVis( void ){
    ==================
  */
 void CalcVis( void ){
-       int i;
+       int i, minvis, maxvis;
        const char  *value;
+       double mu, sigma, totalvis, totalvis2;
 
 
        /* ydnar: rr2do2's farplane code */
@@ -353,8 +354,34 @@ void CalcVis( void ){
        for ( i = 0 ; i < portalclusters ; i++ )
                ClusterMerge( i );
 
-       Sys_Printf( "Total visible clusters: %i\n", totalvis );
-       Sys_Printf( "Average clusters visible: %i\n", totalvis / portalclusters );
+       totalvis = 0;
+       totalvis2 = 0;
+       minvis = -1;
+       maxvis = -1;
+       for ( i = 0; i < MAX_MAP_LEAFS; ++i )
+               if ( clustersizehistogram[i] ) {
+                       if ( debugCluster ) {
+                               Sys_FPrintf( SYS_VRB, "%4i clusters have exactly %4i visible clusters\n", clustersizehistogram[i], i );
+                       }
+                       /* cast is to prevent integer overflow */
+                       totalvis  += ( (double) i )                * ( (double) clustersizehistogram[i] );
+                       totalvis2 += ( (double) i ) * ( (double) i ) * ( (double) clustersizehistogram[i] );
+
+                       if ( minvis < 0 ) {
+                               minvis = i;
+                       }
+                       maxvis = i;
+               }
+
+       mu = totalvis / portalclusters;
+       sigma = sqrt( totalvis2 / portalclusters - mu * mu );
+
+       Sys_Printf( "Total clusters: %i\n", portalclusters );
+       Sys_Printf( "Total visible clusters: %.0f\n", totalvis );
+       Sys_Printf( "Average clusters visible: %.2f (%.3f%%/total)\n", mu, mu / portalclusters * 100.0 );
+       Sys_Printf( "  Standard deviation: %.2f (%.3f%%/total, %.3f%%/avg)\n", sigma, sigma / portalclusters * 100.0, sigma / mu * 100.0 );
+       Sys_Printf( "  Minimum: %i (%.3f%%/total, %.3f%%/avg)\n", minvis, minvis / (double) portalclusters * 100.0, minvis / mu * 100.0 );
+       Sys_Printf( "  Maximum: %i (%.3f%%/total, %.3f%%/avg)\n", maxvis, maxvis / (double) portalclusters * 100.0, maxvis / mu * 100.0 );
 }
 
 /*
@@ -863,7 +890,7 @@ void WritePortals( char *filename ){
    ============
  */
 void LoadPortals( char *name ){
-       int i, j, hint;
+       int i, j, flags;
        vportal_t   *p;
        leaf_t      *l;
        char magic[80];
@@ -895,6 +922,10 @@ void LoadPortals( char *name ){
        Sys_Printf( "%6i numportals\n", numportals );
        Sys_Printf( "%6i numfaces\n", numfaces );
 
+       if ( numportals > MAX_PORTALS ) {
+               Error( "MAX_PORTALS" );
+       }
+
        // these counts should take advantage of 64 bit systems automatically
        leafbytes = ( ( portalclusters + 63 ) & ~63 ) >> 3;
        leaflongs = leafbytes / sizeof( long );
@@ -929,12 +960,12 @@ void LoadPortals( char *name ){
                if ( numpoints > MAX_POINTS_ON_WINDING ) {
                        Error( "LoadPortals: portal %i has too many points", i );
                }
-               if ( (unsigned)leafnums[0] > portalclusters
-                        || (unsigned)leafnums[1] > portalclusters ) {
+               if ( leafnums[0] > portalclusters
+                        || leafnums[1] > portalclusters ) {
                        Error( "LoadPortals: reading portal %i", i );
                }
-               if ( fscanf( f, "%i ", &hint ) != 1 ) {
-                       Error( "LoadPortals: reading hint state" );
+               if ( fscanf( f, "%i ", &flags ) != 1 ) {
+                       Error( "LoadPortals: reading flags" );
                }
 
                w = p->winding = NewFixedWinding( numpoints );
@@ -954,7 +985,9 @@ void LoadPortals( char *name ){
                        for ( k = 0 ; k < 3 ; k++ )
                                w->points[j][k] = v[k];
                }
-               fscanf( f, "\n" );
+               if ( fscanf( f, "\n" ) != 0 ) {
+                       // silence gcc warning
+               }
 
                // calc plane
                PlaneFromWinding( w, &plane );
@@ -968,7 +1001,8 @@ void LoadPortals( char *name ){
                l->numportals++;
 
                p->num = i + 1;
-               p->hint = hint;
+               p->hint = ((flags & 1) != 0);
+               p->sky = ((flags & 2) != 0);
                p->winding = w;
                VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
                p->plane.dist = -plane.dist;
@@ -1029,7 +1063,9 @@ void LoadPortals( char *name ){
                        for ( k = 0 ; k < 3 ; k++ )
                                w->points[j][k] = v[k];
                }
-               fscanf( f, "\n" );
+               if ( fscanf( f, "\n" ) != 0 ) {
+                       // silence gcc warning
+               }
 
                // calc plane
                PlaneFromWinding( w, &plane );
@@ -1081,6 +1117,10 @@ int VisMain( int argc, char **argv ){
                        Sys_Printf( "merge = true\n" );
                        mergevis = qtrue;
                }
+               else if ( !strcmp( argv[i], "-mergeportals" ) ) {
+                       Sys_Printf( "mergeportals = true\n" );
+                       mergevisportals = qtrue;
+               }
                else if ( !strcmp( argv[i], "-nopassage" ) ) {
                        Sys_Printf( "nopassage = true\n" );
                        noPassageVis = qtrue;
@@ -1097,6 +1137,10 @@ int VisMain( int argc, char **argv ){
                        Sys_Printf( "saveprt = true\n" );
                        saveprt = qtrue;
                }
+               else if ( !strcmp( argv[ i ], "-v" ) ) {
+                       debugCluster = qtrue;
+                       Sys_Printf( "Extra verbous mode enabled\n" );
+               }
                else if ( !strcmp( argv[i],"-tmpin" ) ) {
                        strcpy( inbase, "/tmp" );
                }
@@ -1112,7 +1156,8 @@ int VisMain( int argc, char **argv ){
                        mergevis = qtrue;
                }
 
-               else{
+               else
+               {
                        Sys_Printf( "WARNING: Unknown option \"%s\"\n", argv[ i ] );
                }
        }
@@ -1145,8 +1190,15 @@ int VisMain( int argc, char **argv ){
        /* ydnar: for getting far plane */
        ParseEntities();
 
+       /* inject command line parameters */
+       InjectCommandLine( argv, 0, argc - 1 );
+       UnparseEntities();
+
        if ( mergevis ) {
                MergeLeaves();
+       }
+
+       if ( mergevis || mergevisportals ) {
                MergeLeafPortals();
        }