From 1fb1896f1110a788dee10ca5306bcfbf2e538da0 Mon Sep 17 00:00:00 2001 From: Garux Date: Fri, 18 Aug 2023 11:30:48 +0600 Subject: [PATCH] tweak light styles checks --- tools/quake3/q3map2/light.cpp | 2 +- tools/quake3/q3map2/light_bounce.cpp | 15 +++------------ tools/quake3/q3map2/q3map2.h | 2 ++ tools/quake3/q3map2/shaders.cpp | 2 +- tools/quake3/q3map2/surface.cpp | 5 +---- tools/quake3/q3map2/writebsp.cpp | 4 ++-- 6 files changed, 10 insertions(+), 20 deletions(-) diff --git a/tools/quake3/q3map2/light.cpp b/tools/quake3/q3map2/light.cpp index 7f5d85e4..98ece513 100644 --- a/tools/quake3/q3map2/light.cpp +++ b/tools/quake3/q3map2/light.cpp @@ -428,7 +428,7 @@ static void CreateEntityLights(){ /* set origin */ light.origin = e.vectorForKey( "origin" ); e.read_keyvalue( light.style, "_style", "style" ); - if ( light.style < LS_NORMAL || light.style >= LS_NONE ) { + if ( !style_is_valid( light.style ) ) { Error( "Invalid lightstyle (%d) on entity %zu", light.style, i ); } diff --git a/tools/quake3/q3map2/light_bounce.cpp b/tools/quake3/q3map2/light_bounce.cpp index e2967c1c..7395bdc1 100644 --- a/tools/quake3/q3map2/light_bounce.cpp +++ b/tools/quake3/q3map2/light_bounce.cpp @@ -487,10 +487,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw light.photons = value * area * areaScale; light.add = value * formFactorValueScale * areaScale; light.color = si->color; - light.style = noStyles ? LS_NORMAL : si->lightStyle; - if ( light.style < LS_NORMAL || light.style >= LS_NONE ) { - light.style = LS_NORMAL; - } + light.style = noStyles || !style_is_valid( si->lightStyle )? LS_NORMAL : si->lightStyle; /* set origin */ light.origin = minmax.origin(); @@ -542,10 +539,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw splash.si = si; splash.color = si->color; splash.falloffTolerance = falloffTolerance; - splash.style = noStyles ? LS_NORMAL : si->lightStyle; - if ( splash.style < LS_NORMAL || splash.style >= LS_NONE ) { - splash.style = LS_NORMAL; - } + splash.style = noStyles || !style_is_valid( si->lightStyle )? LS_NORMAL : si->lightStyle; /* create a regular winding */ splash.w = AllocWinding( rw->numVerts ); @@ -568,10 +562,7 @@ static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, raw light.photons = value * area * bounceScale; light.add = value * formFactorValueScale * bounceScale; light.color = color; - light.style = noStyles ? LS_NORMAL : style; - if ( light.style < LS_NORMAL || light.style >= LS_NONE ) { - light.style = LS_NORMAL; - } + light.style = noStyles || !style_is_valid( style )? LS_NORMAL : style; /* set origin */ light.origin = WindingCenter( light.w ); diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 62b86e71..b69da85b 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -187,6 +187,8 @@ enum class EBrushType #define LS_UNUSED 0xFE #define LS_NONE 0xFF +inline bool style_is_valid( int style ){ return LS_NORMAL <= style && style < LS_NONE; } + #define MAX_LIGHTMAP_SHADERS 256 /* ok to increase these at the expense of more memory */ diff --git a/tools/quake3/q3map2/shaders.cpp b/tools/quake3/q3map2/shaders.cpp index 0ada4d03..ae342483 100644 --- a/tools/quake3/q3map2/shaders.cpp +++ b/tools/quake3/q3map2/shaders.cpp @@ -1256,7 +1256,7 @@ static void ParseShaderFile( const char *filename ){ /* q3map_lightStyle (sof2/jk2 lightstyle) */ else if ( striEqual( token, "q3map_lightStyle" ) ) { text.GetToken( false ); - si->lightStyle = std::clamp( atoi( token ), 0, LS_NONE ); + si->lightStyle = std::clamp( atoi( token ), LS_NORMAL, LS_NONE ); } /* wolf: q3map_lightRGB */ diff --git a/tools/quake3/q3map2/surface.cpp b/tools/quake3/q3map2/surface.cpp index 2068514e..9bd410f5 100644 --- a/tools/quake3/q3map2/surface.cpp +++ b/tools/quake3/q3map2/surface.cpp @@ -1071,10 +1071,7 @@ mapDrawSurface_t *DrawSurfaceForFlare( int entNum, const Vector3& origin, const ds->lightmapVecs[ 0 ] = color; /* store light style */ - ds->lightStyle = lightStyle; - if ( ds->lightStyle < 0 || ds->lightStyle >= LS_NONE ) { - ds->lightStyle = LS_NORMAL; - } + ds->lightStyle = style_is_valid( lightStyle )? lightStyle : LS_NORMAL; /* fixme: fog */ diff --git a/tools/quake3/q3map2/writebsp.cpp b/tools/quake3/q3map2/writebsp.cpp index a37a74b5..1f3a83f9 100644 --- a/tools/quake3/q3map2/writebsp.cpp +++ b/tools/quake3/q3map2/writebsp.cpp @@ -266,7 +266,7 @@ void SetLightStyles(){ /* get existing style */ const int style = e.intForKey( "style" ); - if ( style < LS_NORMAL || style > LS_NONE ) { + if ( !style_is_valid( style ) ) { Error( "Invalid lightstyle (%d) on entity %zu", style, i ); } @@ -287,7 +287,7 @@ void SetLightStyles(){ } /* set explicit style */ - sprintf( value, "%d", 32 + j ); + sprintf( value, "%d", MAX_SWITCHED_LIGHTS + j ); e.setKeyValue( "style", value ); /* set old style */