]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/shaders.c
set eol-style
[xonotic/netradiant.git] / tools / quake3 / q3map2 / shaders.c
index abeada6d4ccd370737e7e256a1ae43e5d216c656..a0988938374f91075b58fcfc4c002798178257a4 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 SHADERS_C\r
-\r
-\r
-\r
-/* dependencies */\r
-#include "q3map2.h"\r
-\r
-\r
-\r
-/*\r
-AlphaMod()\r
-routines for dealing with vertex alpha modification\r
-*/\r
-\r
-void AlphaMod( alphaMod_t *am, int numVerts, bspDrawVert_t *drawVerts )\r
-{\r
-       int                             i, j;\r
-       float                   mult, add, a;\r
-       bspDrawVert_t   *dv;\r
-       alphaMod_t              *am2;\r
-       \r
-       \r
-       /* dummy check */\r
-       if( am == NULL || numVerts < 1 || drawVerts == NULL )\r
-               return;\r
-       \r
-       \r
-       /* walk vertex list */\r
-       for( i = 0; i < numVerts; i++ )\r
-       {\r
-               /* get vertex */\r
-               dv = &drawVerts[ i ];\r
-               \r
-               /* walk alphamod list */\r
-               for( am2 = am; am2 != NULL; am2 = am2->next )\r
-               {\r
-                       /* switch on type */\r
-                       switch( am->type )\r
-                       {\r
-                               case AM_DOT_PRODUCT:\r
-                                       mult = DotProduct( dv->normal, am2->data );\r
-                                       add = 0.0f;\r
-                                       break;\r
-                               \r
-                               default:\r
-                                       mult = 1.0f;\r
-                                       add = 0.0f;\r
-                                       break;\r
-                       }\r
-                       \r
-                       /* apply mod */\r
-                       for( j = 0; j < MAX_LIGHTMAPS; j++ )\r
-                       {\r
-                               a = (mult * dv->color[ j ][ 3 ]) + add;\r
-                               if( a < 0 )\r
-                                       a = 0;\r
-                               else if( a > 255 )\r
-                                       a = 255;\r
-                               dv->color[ j ][ 3 ] = a;\r
-                       }\r
-               }\r
-       }\r
-}\r
-\r
-\r
-\r
-/*\r
-TcMod*()\r
-routines for dealing with a 3x3 texture mod matrix\r
-*/\r
-\r
-void TcMod( tcMod_t mod, float st[ 2 ] )\r
-{\r
-       float   old[ 2 ];\r
-       \r
-       \r
-       old[ 0 ] = st[ 0 ];\r
-       old[ 1 ] = st[ 1 ];\r
-       st[ 0 ] = (mod[ 0 ][ 0 ] * old[ 0 ]) + (mod[ 0 ][ 1 ] * old[ 1 ]) + mod[ 0 ][ 2 ];\r
-       st[ 1 ] = (mod[ 1 ][ 0 ] * old[ 0 ]) + (mod[ 1 ][ 1 ] * old[ 1 ]) + mod[ 1 ][ 2 ];\r
-}\r
-\r
-\r
-void TcModIdentity( tcMod_t mod )\r
-{\r
-       mod[ 0 ][ 0 ] = 1.0f;   mod[ 0 ][ 1 ] = 0.0f;   mod[ 0 ][ 2 ] = 0.0f;\r
-       mod[ 1 ][ 0 ] = 0.0f;   mod[ 1 ][ 1 ] = 1.0f;   mod[ 1 ][ 2 ] = 0.0f;\r
-       mod[ 2 ][ 0 ] = 0.0f;   mod[ 2 ][ 1 ] = 0.0f;   mod[ 2 ][ 2 ] = 1.0f;   /* this row is only used for multiples, not transformation */\r
-}\r
-\r
-\r
-void TcModMultiply( tcMod_t a, tcMod_t b, tcMod_t out )\r
-{\r
-       int             i;\r
-       \r
-       \r
-       for( i = 0; i < 3; i++ )\r
-       {\r
-               out[ i ][ 0 ] = (a[ i ][ 0 ] * b[ 0 ][ 0 ]) + (a[ i ][ 1 ] * b[ 1 ][ 0 ]) + (a[ i ][ 2 ] * b[ 2 ][ 0 ]);\r
-               out[ i ][ 1 ] = (a[ i ][ 0 ] * b[ 0 ][ 1 ]) + (a[ i ][ 1 ] * b[ 1 ][ 1 ]) + (a[ i ][ 2 ] * b[ 2 ][ 1 ]);\r
-               out[ i ][ 2 ] = (a[ i ][ 0 ] * b[ 0 ][ 2 ]) + (a[ i ][ 1 ] * b[ 1 ][ 2 ]) + (a[ i ][ 2 ] * b[ 2 ][ 2 ]);\r
-       }\r
-}\r
-\r
-\r
-void TcModTranslate( tcMod_t mod, float s, float t )\r
-{\r
-       mod[ 0 ][ 2 ] += s;\r
-       mod[ 1 ][ 2 ] += t;\r
-}\r
-\r
-\r
-void TcModScale( tcMod_t mod, float s, float t )\r
-{\r
-       mod[ 0 ][ 0 ] *= s;\r
-       mod[ 1 ][ 1 ] *= t;\r
-}\r
-\r
-\r
-void TcModRotate( tcMod_t mod, float euler )\r
-{\r
-       tcMod_t old, temp;\r
-       float   radians, sinv, cosv;\r
-       \r
-       \r
-       memcpy( old, mod, sizeof( tcMod_t ) );\r
-       TcModIdentity( temp );\r
-\r
-       radians = euler / 180 * Q_PI;\r
-       sinv = sin( radians );\r
-       cosv = cos( radians );\r
-\r
-       temp[ 0 ][ 0 ] = cosv;  temp[ 0 ][ 1 ] = -sinv;\r
-       temp[ 1 ][ 0 ] = sinv;  temp[ 1 ][ 1 ] = cosv;\r
-       \r
-       TcModMultiply( old, temp, mod );\r
-}\r
-\r
-\r
-\r
-/*\r
-ApplySurfaceParm() - ydnar\r
-applies a named surfaceparm to the supplied flags\r
-*/\r
-\r
-qboolean ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags )\r
-{\r
-       int                             i, fake;\r
-       surfaceParm_t   *sp;\r
-       \r
-       \r
-       /* dummy check */\r
-       if( name == NULL )\r
-               name = "";\r
-       if( contentFlags == NULL )\r
-               contentFlags = &fake;\r
-       if( surfaceFlags == NULL )\r
-               surfaceFlags = &fake;\r
-       if( compileFlags == NULL )\r
-               compileFlags = &fake;\r
-       \r
-       /* walk the current game's surfaceparms */\r
-       sp = game->surfaceParms;\r
-       while( sp->name != NULL )\r
-       {\r
-               /* match? */\r
-               if( !Q_stricmp( name, sp->name ) )\r
-               {\r
-                       /* clear and set flags */\r
-                       *contentFlags &= ~(sp->contentFlagsClear);\r
-                       *contentFlags |= sp->contentFlags;\r
-                       *surfaceFlags &= ~(sp->surfaceFlagsClear);\r
-                       *surfaceFlags |= sp->surfaceFlags;\r
-                       *compileFlags &= ~(sp->compileFlagsClear);\r
-                       *compileFlags |= sp->compileFlags;\r
-                       \r
-                       /* return ok */\r
-                       return qtrue;\r
-               }\r
-               \r
-               /* next */\r
-               sp++;\r
-       }\r
-       \r
-       /* check custom info parms */\r
-       for( i = 0; i < numCustSurfaceParms; i++ )\r
-       {\r
-               /* get surfaceparm */\r
-               sp = &custSurfaceParms[ i ];\r
-               \r
-               /* match? */\r
-               if( !Q_stricmp( name, sp->name ) )\r
-               {\r
-                       /* clear and set flags */\r
-                       *contentFlags &= ~(sp->contentFlagsClear);\r
-                       *contentFlags |= sp->contentFlags;\r
-                       *surfaceFlags &= ~(sp->surfaceFlagsClear);\r
-                       *surfaceFlags |= sp->surfaceFlags;\r
-                       *compileFlags &= ~(sp->compileFlagsClear);\r
-                       *compileFlags |= sp->compileFlags;\r
-                       \r
-                       /* return ok */\r
-                       return qtrue;\r
-               }\r
-       }\r
-       \r
-       /* no matching surfaceparm found */\r
-       return qfalse;\r
-}\r
-\r
-\r
-\r
-/*\r
-BeginMapShaderFile() - ydnar\r
-erases and starts a new map shader script\r
-*/\r
-\r
-void BeginMapShaderFile( const char *mapFile )\r
-{\r
-       char    base[ 1024 ];\r
-       int             len;\r
-       \r
-\r
-       /* dummy check */\r
-       mapName[ 0 ] = '\0';\r
-       mapShaderFile[ 0 ] = '\0';\r
-       if( mapFile == NULL || mapFile[ 0 ] == '\0' )\r
-               return;\r
-       \r
-       /* copy map name */\r
-       strcpy( base, mapFile );\r
-       StripExtension( base );\r
-       \r
-       /* extract map name */\r
-       len = strlen( base ) - 1;\r
-       while( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )\r
-               len--;\r
-       strcpy( mapName, &base[ len + 1 ] );\r
-       base[ len ] = '\0';\r
-       if( len <= 0 )\r
-               return;\r
-       \r
-       /* append ../scripts/q3map2_<mapname>.shader */\r
-       sprintf( mapShaderFile, "%s/../%s/q3map2_%s.shader", base, game->shaderPath, mapName );\r
-       Sys_FPrintf( SYS_VRB, "Map has shader script %s\n", mapShaderFile );\r
-       \r
-       /* remove it */\r
-       remove( mapShaderFile );\r
-       \r
-       /* stop making warnings about missing images */\r
-       warnImage = qfalse;\r
-}\r
-\r
-\r
-\r
-/*\r
-WriteMapShaderFile() - ydnar\r
-writes a shader to the map shader script\r
-*/\r
-\r
-void WriteMapShaderFile( void )\r
-{\r
-       FILE                    *file;\r
-       shaderInfo_t    *si;\r
-       int                             i, num;\r
-       \r
-       \r
-       /* dummy check */\r
-       if( mapShaderFile[ 0 ] == '\0' )\r
-               return;\r
-       \r
-       /* are there any custom shaders? */\r
-       for( i = 0, num = 0; i < numShaderInfo; i++ )\r
-       {\r
-               if( shaderInfo[ i ].custom ) \r
-                       break;\r
-       }\r
-       if( i == numShaderInfo )\r
-               return;\r
-       \r
-       /* note it */\r
-       Sys_FPrintf( SYS_VRB, "--- WriteMapShaderFile ---\n");\r
-       Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile );\r
-       \r
-       /* open shader file */\r
-       file = fopen( mapShaderFile, "w" );\r
-       if( file == NULL )\r
-       {\r
-               Sys_Printf( "WARNING: Unable to open map shader file %s for writing\n", mapShaderFile );\r
-               return;\r
-       }\r
-       \r
-       /* print header */\r
-       fprintf( file,\r
-               "// Custom shader file for %s.bsp\n"\r
-               "// Generated by Q3Map2 (ydnar)\n"\r
-               "// Do not edit! This file is overwritten on recompiles.\n\n",\r
-               mapName );\r
-       \r
-       /* walk the shader list */\r
-       for( i = 0, num = 0; i < numShaderInfo; i++ )\r
-       {\r
-               /* get the shader and print it */\r
-               si = &shaderInfo[ i ];\r
-               if( si->custom == qfalse || si->shaderText == NULL || si->shaderText[ 0 ] == '\0' )\r
-                       continue;\r
-               num++;\r
-\r
-               /* print it to the file */\r
-               fprintf( file, "%s%s\n", si->shader, si->shaderText );\r
-               //%     Sys_Printf( "%s%s\n", si->shader, si->shaderText ); /* FIXME: remove debugging code */\r
-               \r
-               Sys_FPrintf( SYS_VRB, "." );\r
-       }\r
-       \r
-       /* close the shader */\r
-       fclose( file );\r
-       \r
-       Sys_FPrintf( SYS_VRB, "\n" );\r
-       \r
-       /* print some stats */\r
-       Sys_Printf( "%9d custom shaders emitted\n", num );\r
-}\r
-\r
-\r
-\r
-/*\r
-CustomShader() - ydnar\r
-sets up a custom map shader\r
-*/\r
-\r
-shaderInfo_t *CustomShader( shaderInfo_t *si, char *find, char *replace )\r
-{\r
-       shaderInfo_t    *csi;\r
-       char                    shader[ MAX_QPATH ];\r
-       char                    *s;\r
-       int                             loc;\r
-       md5_state_t             md5;\r
-       byte                    digest[ 16 ];\r
-       char                    *srcShaderText, temp[ 8192 ], shaderText[ 8192 ];       /* ydnar: fixme (make this bigger?) */\r
-       \r
-       \r
-       /* dummy check */\r
-       if( si == NULL )\r
-               return ShaderInfoForShader( "default" );\r
-       \r
-       /* default shader text source */\r
-       srcShaderText = si->shaderText;\r
-       \r
-       /* et: implicitMap */\r
-       if( si->implicitMap == IM_OPAQUE )\r
-       {\r
-               srcShaderText = temp;\r
-               sprintf( temp, "\n"\r
-                       "{ // Q3Map2 defaulted (implicitMap)\n"\r
-                       "\t{\n"\r
-                       "\t\tmap $lightmap\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t}\n"\r
-                       "\tq3map_styleMarker\n"\r
-                       "\t{\n"\r
-                       "\t\tmap %s\n"\r
-                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t}\n"\r
-                       "}\n",\r
-                       si->implicitImagePath );\r
-       }\r
-       \r
-       /* et: implicitMask */\r
-       else if( si->implicitMap == IM_MASKED )\r
-       {\r
-               srcShaderText = temp;\r
-               sprintf( temp, "\n"\r
-                       "{ // Q3Map2 defaulted (implicitMask)\n"\r
-                       "\tcull none\n"\r
-                       "\t{\n"\r
-                       "\t\tmap %s\n"\r
-                       "\t\talphaFunc GE128\n"\r
-                       "\t\tdepthWrite\n"\r
-                       "\t}\n"\r
-                       "\t{\n"\r
-                       "\t\tmap $lightmap\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t\tdepthFunc equal\n"\r
-                       "\t}\n"\r
-                       "\tq3map_styleMarker\n"\r
-                       "\t{\n"\r
-                       "\t\tmap %s\n"\r
-                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"\r
-                       "\t\tdepthFunc equal\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t}\n"\r
-                       "}\n",\r
-                       si->implicitImagePath,\r
-                       si->implicitImagePath );\r
-       }\r
-       \r
-       /* et: implicitBlend */\r
-       else if( si->implicitMap == IM_BLEND )\r
-       {\r
-               srcShaderText = temp;\r
-               sprintf( temp, "\n"\r
-                       "{ // Q3Map2 defaulted (implicitBlend)\n"\r
-                       "\tcull none\n"\r
-                       "\t{\n"\r
-                       "\t\tmap %s\n"\r
-                       "\t\tblendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA\n"\r
-                       "\t}\n"\r
-                       "\t{\n"\r
-                       "\t\tmap $lightmap\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"\r
-                       "\t}\n"\r
-                       "\tq3map_styleMarker\n"\r
-                       "}\n",\r
-                       si->implicitImagePath );\r
-       }\r
-       \r
-       /* default shader text */\r
-       else if( srcShaderText == NULL )\r
-       {\r
-               srcShaderText = temp;\r
-               sprintf( temp, "\n"\r
-                       "{ // Q3Map2 defaulted\n"\r
-                       "\t{\n"\r
-                       "\t\tmap $lightmap\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t}\n"\r
-                       "\tq3map_styleMarker\n"\r
-                       "\t{\n"\r
-                       "\t\tmap %s.tga\n"\r
-                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"\r
-                       "\t\trgbGen identity\n"\r
-                       "\t}\n"\r
-                       "}\n",\r
-                       si->shader );\r
-       }\r
-       \r
-       /* error check */\r
-       if( (strlen( mapName ) + 1 + 32) > MAX_QPATH )\r
-               Error( "Custom shader name length (%d) exceeded. Shorten your map name.\n", MAX_QPATH );\r
-       \r
-       /* do some bad find-replace */\r
-       s = strstr( srcShaderText, find );\r
-       if( s == NULL )\r
-               //%     strcpy( shaderText, srcShaderText );\r
-               return si;      /* testing just using the existing shader if this fails */\r
-       else\r
-       {\r
-               /* substitute 'find' with 'replace' */\r
-               loc = s - srcShaderText;\r
-               strcpy( shaderText, srcShaderText );\r
-               shaderText[ loc ] = '\0';\r
-               strcat( shaderText, replace );\r
-               strcat( shaderText, &srcShaderText[ loc + strlen( find ) ] );\r
-       }\r
-       \r
-       /* make md5 hash of the shader text */\r
-       md5_init( &md5 );\r
-       md5_append( &md5, shaderText, strlen( shaderText ) );\r
-       md5_finish( &md5, digest );\r
-       \r
-       /* mangle hash into a shader name */\r
-       sprintf( shader, "%s/%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", mapName,\r
-               digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ], \r
-               digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ], digest[ 15 ] );\r
-       \r
-       /* get shader */\r
-       csi = ShaderInfoForShader( shader );\r
-       \r
-       /* might be a preexisting shader */\r
-       if( csi->custom )\r
-               return csi;\r
-       \r
-       /* clone the existing shader and rename */\r
-       memcpy( csi, si, sizeof( shaderInfo_t ) );\r
-       strcpy( csi->shader, shader );\r
-       csi->custom = qtrue;\r
-       \r
-       /* store new shader text */\r
-       csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );\r
-       strcpy( csi->shaderText, shaderText );  /* LEAK! */\r
-       \r
-       /* return it */\r
-       return csi;\r
-}\r
-\r
-\r
-\r
-/*\r
-EmitVertexRemapShader()\r
-adds a vertexremapshader key/value pair to worldspawn\r
-*/\r
-\r
-void EmitVertexRemapShader( char *from, char *to )\r
-{\r
-       md5_state_t             md5;\r
-       byte                    digest[ 16 ];\r
-       char                    key[ 64 ], value[ 256 ];\r
-       \r
-       \r
-       /* dummy check */\r
-       if( from == NULL || from[ 0 ] == '\0' ||\r
-               to == NULL || to[ 0 ] == '\0' )\r
-               return;\r
-       \r
-       /* build value */\r
-       sprintf( value, "%s;%s", from, to );\r
-       \r
-       /* make md5 hash */\r
-       md5_init( &md5 );\r
-       md5_append( &md5, value, strlen( value ) );\r
-       md5_finish( &md5, digest );\r
-\r
-       /* make key (this is annoying, as vertexremapshader is precisely 17 characters,\r
-          which is one too long, so we leave off the last byte of the md5 digest) */\r
-       sprintf( key, "vertexremapshader%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",\r
-               digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ], \r
-               digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ] );       /* no: digest[ 15 ] */\r
-       \r
-       /* add key/value pair to worldspawn */\r
-       SetKeyValue( &entities[ 0 ], key, value );\r
-}\r
-\r
-\r
-\r
-/*\r
-AllocShaderInfo()\r
-allocates and initializes a new shader\r
-*/\r
-\r
-static shaderInfo_t    *AllocShaderInfo( void )\r
-{\r
-       shaderInfo_t    *si;\r
-       \r
-       \r
-       /* allocate? */\r
-       if( shaderInfo == NULL )\r
-       {\r
-               shaderInfo = safe_malloc( sizeof( shaderInfo_t ) * MAX_SHADER_INFO );\r
-               numShaderInfo = 0;\r
-       }\r
-       \r
-       /* bounds check */\r
-       if( numShaderInfo == MAX_SHADER_INFO )\r
-               Error( "MAX_SHADER_INFO exceeded. Remove some PK3 files or shader scripts from shaderlist.txt and try again." );\r
-       si = &shaderInfo[ numShaderInfo ];\r
-       numShaderInfo++;\r
-       \r
-       /* ydnar: clear to 0 first */\r
-       memset( si, 0, sizeof( shaderInfo_t ) );\r
-       \r
-       /* set defaults */\r
-       ApplySurfaceParm( "default", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );\r
-       \r
-       si->backsplashFraction = DEF_BACKSPLASH_FRACTION;\r
-       si->backsplashDistance = DEF_BACKSPLASH_DISTANCE;\r
-       \r
-       si->bounceScale = DEF_RADIOSITY_BOUNCE;\r
-       \r
-       si->lightStyle = LS_NORMAL;\r
-       \r
-       si->polygonOffset = qfalse;\r
-       \r
-       si->shadeAngleDegrees = 0.0f;\r
-       si->lightmapSampleSize = 0;\r
-       si->lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;\r
-       si->patchShadows = qfalse;\r
-       si->vertexShadows = qtrue;      /* ydnar: changed default behavior */\r
-       si->forceSunlight = qfalse;\r
-       si->vertexScale = 1.0;\r
-       si->notjunc = qfalse;\r
-       \r
-       /* ydnar: set texture coordinate transform matrix to identity */\r
-       TcModIdentity( si->mod );\r
-       \r
-       /* ydnar: lightmaps can now be > 128x128 in an externally generated tga */\r
-       si->lmCustomWidth = lmCustomSize;       //%     LIGHTMAP_WIDTH;\r
-       si->lmCustomHeight = lmCustomSize;      //%     LIGHTMAP_HEIGHT;\r
-       \r
-       /* return to sender */\r
-       return si;\r
-}\r
-\r
-\r
-\r
-/*\r
-FinishShader() - ydnar\r
-sets a shader's width and height among other things\r
-*/\r
-\r
-void FinishShader( shaderInfo_t *si )\r
-{\r
-       int             x, y;\r
-       float   st[ 2 ], o[ 2 ], dist, bestDist;\r
-       vec4_t  color, bestColor, delta;\r
-       \r
-\r
-       /* don't double-dip */\r
-       if( si->finished )\r
-               return;\r
-       \r
-       /* if they're explicitly set, copy from image size */\r
-       if( si->shaderWidth == 0 && si->shaderHeight == 0 )\r
-       {\r
-               si->shaderWidth = si->shaderImage->width;\r
-               si->shaderHeight = si->shaderImage->height;\r
-       }\r
-       \r
-       /* legacy terrain has explicit image-sized texture projection */\r
-       if( si->legacyTerrain && si->tcGen == qfalse )\r
-       {\r
-               /* set xy texture projection */\r
-               si->tcGen = qtrue;\r
-               VectorSet( si->vecs[ 0 ], (1.0f / (si->shaderWidth * 0.5f)), 0, 0 );\r
-               VectorSet( si->vecs[ 1 ], 0, (1.0f / (si->shaderHeight * 0.5f)), 0 );\r
-       }\r
-       \r
-       /* find pixel coordinates best matching the average color of the image */\r
-       bestDist = 99999999;\r
-       o[ 0 ] = 1.0f / si->shaderImage->width;\r
-       o[ 1 ] = 1.0f / si->shaderImage->height;\r
-       for( y = 0, st[ 1 ] = 0.0f; y < si->shaderImage->height; y++, st[ 1 ] += o[ 1 ] )\r
-       {\r
-               for( x = 0, st[ 0 ] = 0.0f; x < si->shaderImage->width; x++, st[ 0 ] += o[ 0 ] )\r
-               {\r
-                       /* sample the shader image */\r
-                       RadSampleImage( si->shaderImage->pixels, si->shaderImage->width, si->shaderImage->height, st, color );\r
-                       \r
-                       /* determine error squared */\r
-                       VectorSubtract( color, si->averageColor, delta );\r
-                       delta[ 3 ] = color[ 3 ] - si->averageColor[ 3 ];\r
-                       dist = delta[ 0 ] * delta[ 0 ] + delta[ 1 ] * delta[ 1 ] + delta[ 2 ] * delta[ 2 ] + delta[ 3 ] * delta[ 3 ];\r
-                       if( dist < bestDist )\r
-                       {\r
-                               VectorCopy( color, bestColor );\r
-                               bestColor[ 3 ] = color[ 3 ];\r
-                               si->stFlat[ 0 ] = st[ 0 ];\r
-                               si->stFlat[ 1 ] = st[ 1 ];\r
-                       }\r
-               }\r
-       }\r
-       \r
-       /* set to finished */\r
-       si->finished = qtrue;\r
-}\r
-\r
-\r
-\r
-/*\r
-LoadShaderImages()\r
-loads a shader's images\r
-ydnar: image.c made this a bit simpler\r
-*/\r
-\r
-static void LoadShaderImages( shaderInfo_t *si )\r
-{\r
-       int                     i, count;\r
-       float           color[ 4 ];\r
-       \r
-       \r
-       /* nodraw shaders don't need images */\r
-       if( si->compileFlags & C_NODRAW )\r
-               si->shaderImage = ImageLoad( DEFAULT_IMAGE );\r
-       else\r
-       {\r
-               /* try to load editor image first */\r
-               si->shaderImage = ImageLoad( si->editorImagePath );\r
-               \r
-               /* then try shadername */\r
-               if( si->shaderImage == NULL )\r
-                       si->shaderImage = ImageLoad( si->shader );\r
-               \r
-               /* then try implicit image path (note: new behavior!) */\r
-               if( si->shaderImage == NULL )\r
-                       si->shaderImage = ImageLoad( si->implicitImagePath );\r
-               \r
-               /* then try lightimage (note: new behavior!) */\r
-               if( si->shaderImage == NULL )\r
-                       si->shaderImage = ImageLoad( si->lightImagePath );\r
-               \r
-               /* otherwise, use default image */\r
-               if( si->shaderImage == NULL )\r
-               {\r
-                       si->shaderImage = ImageLoad( DEFAULT_IMAGE );\r
-                       if( warnImage && strcmp( si->shader, "noshader" ) )\r
-                               Sys_Printf( "WARNING: Couldn't find image for shader %s\n", si->shader );\r
-               }\r
-               \r
-               /* load light image */\r
-               si->lightImage = ImageLoad( si->lightImagePath );\r
-               \r
-               /* load normalmap image (ok if this is NULL) */\r
-               si->normalImage = ImageLoad( si->normalImagePath );\r
-               if( si->normalImage != NULL )\r
-               {\r
-                       Sys_FPrintf( SYS_VRB, "Shader %s has\n"\r
-                                                                 "    NM %s\n", si->shader, si->normalImagePath );\r
-               }\r
-       }\r
-       \r
-       /* if no light image, use shader image */\r
-       if( si->lightImage == NULL )\r
-               si->lightImage = ImageLoad( si->shaderImage->name );\r
-       \r
-       /* create default and average colors */\r
-       count = si->lightImage->width * si->lightImage->height;\r
-       VectorClear( color );\r
-       color[ 3 ] = 0.0f;\r
-       for( i = 0; i < count; i++ )\r
-       {\r
-               color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];\r
-               color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];\r
-               color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];\r
-               color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];\r
-       }\r
-       \r
-       if( VectorLength( si->color ) <= 0.0f )\r
-               ColorNormalize( color, si->color );\r
-       VectorScale( color, (1.0f / count), si->averageColor );\r
-}\r
-\r
-\r
-\r
-/*\r
-ShaderInfoForShader()\r
-finds a shaderinfo for a named shader\r
-*/\r
-\r
-shaderInfo_t *ShaderInfoForShader( const char *shaderName )\r
-{\r
-       int                             i;\r
-       shaderInfo_t    *si;\r
-       char                    shader[ MAX_QPATH ];\r
-       \r
-       \r
-       /* dummy check */\r
-       if( shaderName == NULL || shaderName[ 0 ] == '\0' )\r
-       {\r
-               Sys_Printf( "WARNING: Null or empty shader name\n" );\r
-               shaderName = "missing";\r
-       }\r
-       \r
-       /* strip off extension */\r
-       strcpy( shader, shaderName );\r
-       StripExtension( shader );\r
-       \r
-       /* search for it */\r
-       for( i = 0; i < numShaderInfo; i++ )\r
-       {\r
-               si = &shaderInfo[ i ];\r
-               if( !Q_stricmp( shader, si->shader ) )\r
-               {\r
-                       /* load image if necessary */\r
-                       if( si->shaderImage == NULL )\r
-                       {\r
-                               LoadShaderImages( si );\r
-                               FinishShader( si );\r
-                       }\r
-                       \r
-                       /* return it */\r
-                       return si;\r
-               }\r
-       }\r
-       \r
-       /* allocate a default shader */\r
-       si = AllocShaderInfo();\r
-       strcpy( si->shader, shader );\r
-       LoadShaderImages( si );\r
-       FinishShader( si );\r
-       \r
-       /* return it */\r
-       return si;\r
-}\r
-\r
-\r
-\r
-/*\r
-GetTokenAppend() - ydnar\r
-gets a token and appends its text to the specified buffer\r
-*/\r
-\r
-static int     oldScriptLine = 0;\r
-static int     tabDepth = 0;\r
-\r
-qboolean GetTokenAppend( char *buffer, qboolean crossline )\r
-{\r
-       qboolean        r;\r
-       int                     i;\r
-       \r
-       \r
-       /* get the token */\r
-       r = GetToken( crossline );\r
-       if( r == qfalse || buffer == NULL || token[ 0 ] == '\0' )\r
-               return r;\r
-       \r
-       /* pre-tabstops */\r
-       if( token[ 0 ] == '}' )\r
-               tabDepth--;\r
-       \r
-       /* append? */\r
-       if( oldScriptLine != scriptline )\r
-       {\r
-               strcat( buffer, "\n" );\r
-               for( i = 0; i < tabDepth; i++ )\r
-                       strcat( buffer, "\t" );\r
-       }\r
-       else\r
-               strcat( buffer, " " );\r
-       oldScriptLine = scriptline;\r
-       strcat( buffer, token );\r
-       \r
-       /* post-tabstops */\r
-       if( token[ 0 ] == '{' )\r
-               tabDepth++;\r
-       \r
-       /* return */\r
-       return r;\r
-}\r
-\r
-\r
-void Parse1DMatrixAppend( char *buffer, int x, vec_t *m )\r
-{\r
-       int             i;\r
-       \r
-       \r
-       if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) )\r
-               Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );\r
-       for( i = 0; i < x; i++ )\r
-       {\r
-               if( !GetTokenAppend( buffer, qfalse ) )\r
-                       Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );\r
-               m[ i ] = atof( token );\r
-       }\r
-       if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) )\r
-               Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );\r
-}\r
-\r
-\r
-\r
-\r
-/*\r
-ParseShaderFile()\r
-parses a shader file into discrete shaderInfo_t\r
-*/\r
-\r
-static void ParseShaderFile( const char *filename )\r
-{\r
-       int                             i, val;\r
-       shaderInfo_t    *si;\r
-       char                    *suffix, temp[ 1024 ];\r
-       char                    shaderText[ 8192 ];     /* ydnar: fixme (make this bigger?) */\r
-       \r
-       \r
-       /* init */\r
-       si = NULL;\r
-       shaderText[ 0 ] = '\0';\r
-       \r
-       /* load the shader */\r
-       LoadScriptFile( filename, 0 );\r
-       \r
-       /* tokenize it */\r
-       while( 1 )\r
-       {\r
-               /* copy shader text to the shaderinfo */\r
-               if( si != NULL && shaderText[ 0 ] != '\0' )\r
-               {\r
-                       strcat( shaderText, "\n" );\r
-                       si->shaderText = safe_malloc( strlen( shaderText ) + 1 );\r
-                       strcpy( si->shaderText, shaderText );\r
-                       //%     if( VectorLength( si->vecs[ 0 ] ) )\r
-                       //%             Sys_Printf( "%s\n", shaderText );\r
-               }\r
-               \r
-               /* ydnar: clear shader text buffer */\r
-               shaderText[ 0 ] = '\0';\r
-               \r
-               /* test for end of file */\r
-               if( !GetToken( qtrue ) )\r
-                       break;\r
-               \r
-               /* shader name is initial token */\r
-               si = AllocShaderInfo();\r
-               strcpy( si->shader, token );\r
-               \r
-               /* ignore ":q3map" suffix */\r
-               suffix = strstr( si->shader, ":q3map" );\r
-               if( suffix != NULL )\r
-                       *suffix = '\0';\r
-               \r
-               /* handle { } section */\r
-               if( !GetTokenAppend( shaderText, qtrue ) )\r
-                       break;\r
-               if( strcmp( token, "{" ) )\r
-               {\r
-                       if( si != NULL )\r
-                               Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",\r
-                                       filename, scriptline, token, si->shader );\r
-                       else\r
-                               Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",\r
-                                       filename, scriptline, token );\r
-               }\r
-               \r
-               while( 1 )\r
-               {\r
-                       /* get the next token */\r
-                       if( !GetTokenAppend( shaderText, qtrue ) )\r
-                               break;\r
-                       if( !strcmp( token, "}" ) )\r
-                               break;\r
-                       \r
-                       \r
-                       /* -----------------------------------------------------------------\r
-                          shader stages (passes)\r
-                          ----------------------------------------------------------------- */\r
-                       \r
-                       /* parse stage directives */\r
-                       if( !strcmp( token, "{" ) )\r
-                       {\r
-                               si->hasPasses = qtrue;\r
-                               while( 1 )\r
-                               {\r
-                                       if( !GetTokenAppend( shaderText, qtrue ) )\r
-                                               break;\r
-                                       if( !strcmp( token, "}" ) )\r
-                                               break;\r
-                                       \r
-                                       /* only care about images if we don't have a editor/light image */\r
-                                       if( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' )\r
-                                       {\r
-                                               /* digest any images */\r
-                                               if( !Q_stricmp( token, "map" ) ||\r
-                                                       !Q_stricmp( token, "clampMap" ) ||\r
-                                                       !Q_stricmp( token, "animMap" ) ||\r
-                                                       !Q_stricmp( token, "clampAnimMap" ) ||\r
-                                                       !Q_stricmp( token, "clampMap" ) ||\r
-                                                       !Q_stricmp( token, "mapComp" ) ||\r
-                                                       !Q_stricmp( token, "mapNoComp" ) )\r
-                                               {\r
-                                                       /* skip one token for animated stages */\r
-                                                       if( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) )\r
-                                                               GetTokenAppend( shaderText, qfalse );\r
-                                                       \r
-                                                       /* get an image */\r
-                                                       GetTokenAppend( shaderText, qfalse );\r
-                                                       if( token[ 0 ] != '*' && token[ 0 ] != '$' )\r
-                                                       {\r
-                                                               strcpy( si->lightImagePath, token );\r
-                                                               DefaultExtension( si->lightImagePath, ".tga" );\r
-                                                               \r
-                                                               /* debug code */\r
-                                                               //%     Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-                       \r
-                       \r
-                       /* -----------------------------------------------------------------\r
-                          surfaceparm * directives\r
-                          ----------------------------------------------------------------- */\r
-                       \r
-                       /* match surfaceparm */\r
-                       else if( !Q_stricmp( token, "surfaceparm" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               if( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )\r
-                                       Sys_Printf( "WARNING: Unknown surfaceparm: \"%s\"\n", token );\r
-                       }\r
-                       \r
-                       \r
-                       /* -----------------------------------------------------------------\r
-                          game-related shader directives\r
-                          ----------------------------------------------------------------- */\r
-                       \r
-                       /* ydnar: fogparms (for determining fog volumes) */\r
-                       else if( !Q_stricmp( token, "fogparms" ) )\r
-                               si->fogParms = qtrue;\r
-                       \r
-                       /* ydnar: polygonoffset (for no culling) */\r
-                       else if( !Q_stricmp( token, "polygonoffset" ) )\r
-                               si->polygonOffset = qtrue;\r
-                       \r
-                       /* tesssize is used to force liquid surfaces to subdivide */\r
-                       else if( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               si->subdivisions = atof( token );\r
-                       }\r
-                       \r
-                       /* cull none will set twoSided (ydnar: added disable too) */\r
-                       else if ( !Q_stricmp( token, "cull" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               if( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) )\r
-                                       si->twoSided = qtrue;\r
-                       }\r
-                       \r
-                       /* deformVertexes autosprite[ 2 ]\r
-                          we catch this so autosprited surfaces become point\r
-                          lights instead of area lights */\r
-                       else if( !Q_stricmp( token, "deformVertexes" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               \r
-                               /* deformVertexes autosprite(2) */\r
-                               if( !Q_strncasecmp( token, "autosprite", 10 ) )\r
-                               {\r
-                                       /* set it as autosprite and detail */\r
-                                       si->autosprite = qtrue;\r
-                                       ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );\r
-                                       \r
-                                       /* ydnar: gs mods: added these useful things */\r
-                                       si->noClip = qtrue;\r
-                                       si->notjunc = qtrue;\r
-                               }\r
-                               \r
-                               /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */\r
-                               if( !Q_stricmp( token, "move") )\r
-                               {\r
-                                       vec3_t  amt, mins, maxs;\r
-                                       float   base, amp;\r
-                                       \r
-                                       \r
-                                       /* get move amount */\r
-                                       GetTokenAppend( shaderText, qfalse );   amt[ 0 ] = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );   amt[ 1 ] = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );   amt[ 2 ] = atof( token );\r
-                                       \r
-                                       /* skip func */\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       \r
-                                       /* get base and amplitude */\r
-                                       GetTokenAppend( shaderText, qfalse );   base = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );   amp = atof( token );\r
-                                       \r
-                                       /* calculate */\r
-                                       VectorScale( amt, base, mins );\r
-                                       VectorMA( mins, amp, amt, maxs );\r
-                                       VectorAdd( si->mins, mins, si->mins );\r
-                                       VectorAdd( si->maxs, maxs, si->maxs );\r
-                               } \r
-                       }\r
-                       \r
-                       /* light <value> (old-style flare specification) */\r
-                       else if( !Q_stricmp( token, "light" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               strcpy( si->flareShader, "flareshader" );\r
-                       }\r
-                       \r
-                       /* ydnar: damageShader <shader> <health> (sof2 mods) */\r
-                       else if( !Q_stricmp( token, "damageShader" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               strcpy( si->damageShader, token );\r
-                               GetTokenAppend( shaderText, qfalse );   /* don't do anything with health */\r
-                       }\r
-\r
-                       /* ydnar: enemy territory implicit shaders */\r
-                       else if( !Q_stricmp( token, "implicitMap" ) )\r
-                       {\r
-                               si->implicitMap = IM_OPAQUE;\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               if( token[ 0 ] == '-' && token[ 1 ] == '\0' )\r
-                                       sprintf( si->implicitImagePath, "%s.tga", si->shader );\r
-                               else\r
-                                       strcpy( si->implicitImagePath, token );\r
-                       }\r
-\r
-                       else if( !Q_stricmp( token, "implicitMask" ) )\r
-                       {\r
-                               si->implicitMap = IM_MASKED;\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               if( token[ 0 ] == '-' && token[ 1 ] == '\0' )\r
-                                       sprintf( si->implicitImagePath, "%s.tga", si->shader );\r
-                               else\r
-                                       strcpy( si->implicitImagePath, token );\r
-                       }\r
-\r
-                       else if( !Q_stricmp( token, "implicitBlend" ) )\r
-                       {\r
-                               si->implicitMap = IM_MASKED;\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               if( token[ 0 ] == '-' && token[ 1 ] == '\0' )\r
-                                       sprintf( si->implicitImagePath, "%s.tga", si->shader );\r
-                               else\r
-                                       strcpy( si->implicitImagePath, token );\r
-                       }\r
-                       \r
-                       \r
-                       /* -----------------------------------------------------------------\r
-                          image directives\r
-                          ----------------------------------------------------------------- */\r
-                       \r
-                       /* qer_editorimage <image> */\r
-                       else if( !Q_stricmp( token, "qer_editorImage" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               strcpy( si->editorImagePath, token );\r
-                               DefaultExtension( si->editorImagePath, ".tga" );\r
-                       }\r
-                       \r
-                       /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */\r
-                       else if( !Q_stricmp( token, "q3map_normalImage" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               strcpy( si->normalImagePath, token );\r
-                               DefaultExtension( si->normalImagePath, ".tga" );\r
-                       }\r
-                       \r
-                       /* q3map_lightimage <image> */\r
-                       else if( !Q_stricmp( token, "q3map_lightImage" ) )\r
-                       {\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               strcpy( si->lightImagePath, token );\r
-                               DefaultExtension( si->lightImagePath, ".tga" );\r
-                       }\r
-                       \r
-                       /* ydnar: skyparms <outer image> <cloud height> <inner image> */\r
-                       else if( !Q_stricmp( token, "skyParms" ) )\r
-                       {\r
-                               /* get image base */\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               \r
-                               /* ignore bogus paths */\r
-                               if( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) )\r
-                               {\r
-                                       strcpy( si->skyParmsImageBase, token );\r
-                                       \r
-                                       /* use top image as sky light image */\r
-                                       if( si->lightImagePath[ 0 ] == '\0' )\r
-                                               sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase );\r
-                               }\r
-                               \r
-                               /* skip rest of line */\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                       }\r
-                       \r
-                       /* -----------------------------------------------------------------\r
-                          q3map_* directives\r
-                          ----------------------------------------------------------------- */\r
-                       \r
-                       /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>\r
-                          color will be normalized, so it doesn't matter what range you use\r
-                          intensity falls off with angle but not distance 100 is a fairly bright sun\r
-                          degree of 0 = from the east, 90 = north, etc.  altitude of 0 = sunrise/set, 90 = noon\r
-                          ydnar: sof2map has bareword 'sun' token, so we support that as well */\r
-                       else if( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) )\r
-                       {\r
-                               float           a, b;\r
-                               sun_t           *sun;\r
-                               qboolean        ext;\r
-                               \r
-                               \r
-                               /* ydnar: extended sun directive? */\r
-                               if( !Q_stricmp( token, "q3map_sunext" ) )\r
-                                       ext = qtrue;\r
-                               \r
-                               /* allocate sun */\r
-                               sun = safe_malloc( sizeof( *sun ) );\r
-                               memset( sun, 0, sizeof( *sun ) );\r
-                               \r
-                               /* get color */\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               sun->color[ 0 ] = atof( token );\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               sun->color[ 1 ] = atof( token );\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               sun->color[ 2 ] = atof( token );\r
-                               \r
-                               /* normalize it */\r
-                               VectorNormalize( sun->color, sun->color );\r
-                               \r
-                               /* scale color by brightness */\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               sun->photons = atof( token );\r
-                               \r
-                               /* get sun angle/elevation */\r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               a = atof( token );\r
-                               a = a / 180.0f * Q_PI;\r
-                               \r
-                               GetTokenAppend( shaderText, qfalse );\r
-                               b = atof( token );\r
-                               b = b / 180.0f * Q_PI;\r
-                               \r
-                               sun->direction[ 0 ] = cos( a ) * cos( b );\r
-                               sun->direction[ 1 ] = sin( a ) * cos( b );\r
-                               sun->direction[ 2 ] = sin( b );\r
-                               \r
-                               /* get filter radius from shader */\r
-                               sun->filterRadius = si->lightFilterRadius;\r
-                               \r
-                               /* ydnar: get sun angular deviance/samples */\r
-                               if( ext && TokenAvailable() )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       sun->deviance = atof( token );\r
-                                       sun->deviance = sun->deviance / 180.0f * Q_PI;\r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       sun->numSamples = atoi( token );\r
-                               }\r
-                               \r
-                               /* store sun */\r
-                               sun->next = si->sun;\r
-                               si->sun = sun;\r
-                               \r
-                               /* apply sky surfaceparm */\r
-                               ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );\r
-                               \r
-                               /* don't process any more tokens on this line */\r
-                               continue;\r
-                       }\r
-\r
-                       /* match q3map_ */\r
-                       else if( !Q_strncasecmp( token, "q3map_", 6 ) )\r
-                       {\r
-                               /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */\r
-                               if( !Q_stricmp( token, "q3map_baseShader" ) )\r
-                               {\r
-                                       shaderInfo_t    *si2;\r
-                                       qboolean                oldWarnImage;\r
-                                       \r
-                                       \r
-                                       /* get shader */\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       //%     Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );\r
-                                       oldWarnImage = warnImage;\r
-                                       warnImage = qfalse;\r
-                                       si2 = ShaderInfoForShader( token );\r
-                                       warnImage = oldWarnImage;\r
-                                       \r
-                                       /* subclass it */\r
-                                       if( si2 != NULL )\r
-                                       {\r
-                                               /* preserve name */\r
-                                               strcpy( temp, si->shader );\r
-                                               \r
-                                               /* copy shader */\r
-                                               memcpy( si, si2, sizeof( *si ) );\r
-                                               \r
-                                               /* restore name and set to unfinished */\r
-                                               strcpy( si->shader, temp );\r
-                                               si->finished = qfalse;\r
-                                       }\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */\r
-                               else if( !Q_stricmp( token, "q3map_surfacemodel" ) )\r
-                               {\r
-                                       surfaceModel_t  *model;\r
-                                       \r
-                                       \r
-                                       /* allocate new model and attach it */\r
-                                       model = safe_malloc( sizeof( *model ) );\r
-                                       memset( model, 0, sizeof( *model ) );\r
-                                       model->next = si->surfaceModel;\r
-                                       si->surfaceModel = model;\r
-                                               \r
-                                       /* get parameters */\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       strcpy( model->model, token );\r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->density = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->odds = atof( token );\r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->minScale = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->maxScale = atof( token );\r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->minAngle = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->maxAngle = atof( token );\r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       model->oriented = (token[ 0 ] == '1' ? qtrue : qfalse);\r
-                               }\r
-                               \r
-                               /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */\r
-                               else if( !Q_stricmp( token, "q3map_foliage" ) )\r
-                               {\r
-                                       foliage_t       *foliage;\r
-                                       \r
-                                       \r
-                                       /* allocate new foliage struct and attach it */\r
-                                       foliage = safe_malloc( sizeof( *foliage ) );\r
-                                       memset( foliage, 0, sizeof( *foliage ) );\r
-                                       foliage->next = si->foliage;\r
-                                       si->foliage = foliage;\r
-                                       \r
-                                       /* get parameters */\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       strcpy( foliage->model, token );\r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       foliage->scale = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       foliage->density = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       foliage->odds = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       foliage->inverseAlpha = atoi( token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_bounce <value> (fraction of light to re-emit during radiosity passes) */\r
-                               else if( !Q_stricmp( token, "q3map_bounce" ) || !Q_stricmp( token, "q3map_bounceScale" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->bounceScale = atof( token );\r
-                               }\r
-\r
-                               /* ydnar/splashdamage: q3map_skylight <value> <iterations> */\r
-                               else if( !Q_stricmp( token, "q3map_skylight" )  )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->skyLightValue = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->skyLightIterations = atoi( token );\r
-                                       \r
-                                       /* clamp */\r
-                                       if( si->skyLightValue < 0.0f )\r
-                                               si->skyLightValue = 0.0f;\r
-                                       if( si->skyLightIterations < 2 )\r
-                                               si->skyLightIterations = 2;\r
-                               }\r
-                               \r
-                               /* q3map_surfacelight <value> */\r
-                               else if( !Q_stricmp( token, "q3map_surfacelight" )  )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->value = atof( token );\r
-                               }\r
-                               \r
-                               \r
-                               /* q3map_lightStyle (sof2/jk2 lightstyle) */\r
-                               else if( !Q_stricmp( token, "q3map_lightStyle" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       val = atoi( token );\r
-                                       if( val < 0 )\r
-                                               val = 0;\r
-                                       else if( val > LS_NONE )\r
-                                               val = LS_NONE;\r
-                                       si->lightStyle = val;\r
-                               }\r
-                               \r
-                               /* wolf: q3map_lightRGB <red> <green> <blue> */\r
-                               else if( !Q_stricmp( token, "q3map_lightRGB" ) )\r
-                               {\r
-                                       VectorClear( si->color );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->color[ 0 ] = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->color[ 1 ] = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->color[ 2 ] = atof( token );\r
-                                       ColorNormalize( si->color, si->color );\r
-                               }\r
-                               \r
-                               /* q3map_lightSubdivide <value> */\r
-                               else if( !Q_stricmp( token, "q3map_lightSubdivide" )  )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lightSubdivide = atoi( token );\r
-                               }\r
-                               \r
-                               /* q3map_backsplash <percent> <distance> */\r
-                               else if( !Q_stricmp( token, "q3map_backsplash" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->backsplashFraction = atof( token ) * 0.01f;\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->backsplashDistance = atof( token );\r
-                               }\r
-                               \r
-                               /* q3map_lightmapSampleSize <value> */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapSampleSize" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lightmapSampleSize = atoi( token );\r
-                               }\r
-                               \r
-                               /* q3map_lightmapSampleSffset <value> */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lightmapSampleOffset = atof( token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_lightmapFilterRadius <self> <other> */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapFilterRadius" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lmFilterRadius = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lightFilterRadius = atof( token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_lightmapAxis [xyz] */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapAxis" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       if( !Q_stricmp( token, "x" ) )\r
-                                               VectorSet( si->lightmapAxis, 1, 0, 0 );\r
-                                       else if( !Q_stricmp( token, "y" ) )\r
-                                               VectorSet( si->lightmapAxis, 0, 1, 0 );\r
-                                       else if( !Q_stricmp( token, "z" ) )\r
-                                               VectorSet( si->lightmapAxis, 0, 0, 1 );\r
-                                       else\r
-                                       {\r
-                                               Sys_Printf( "WARNING: Unknown value for lightmap axis: %s\n", token );\r
-                                               VectorClear( si->lightmapAxis );\r
-                                       }\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapSize" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lmCustomWidth = atoi( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lmCustomHeight = atoi( token );\r
-                                       \r
-                                       /* must be a power of 2 */\r
-                                       if( ((si->lmCustomWidth - 1) & si->lmCustomWidth) ||\r
-                                               ((si->lmCustomHeight - 1) & si->lmCustomHeight) )\r
-                                       {\r
-                                               Sys_Printf( "WARNING: Non power-of-two lightmap size specified (%d, %d)\n",\r
-                                                        si->lmCustomWidth, si->lmCustomHeight );\r
-                                               si->lmCustomWidth = LIGHTMAP_WIDTH;\r
-                                               si->lmCustomHeight = LIGHTMAP_HEIGHT;\r
-                                       }\r
-                               }\r
-\r
-                               /* ydnar: q3map_lightmapGamma N (for autogenerated shaders + external tga lightmaps) */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapGamma" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->lmGamma = atof( token );\r
-                                       if( si->lmGamma < 0 )\r
-                                               si->lmGamma = 1.0;\r
-                               }\r
-                               \r
-                               /* q3map_vertexScale (scale vertex lighting by this fraction) */\r
-                               else if( !Q_stricmp( token, "q3map_vertexScale" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->vertexScale = atof( token );\r
-                               }\r
-                               \r
-                               /* q3map_flare <shader> */\r
-                               else if( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       strcpy( si->flareShader, token );\r
-                               }\r
-                               \r
-                               /* q3map_backShader <shader> */\r
-                               else if( !Q_stricmp( token, "q3map_backShader" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       strcpy( si->backShader, token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_offset <value> */\r
-                               else if( !Q_stricmp( token, "q3map_offset" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->offset = atof( token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_cloneShader <shader> */\r
-                               else if ( !Q_stricmp( token, "q3map_cloneShader" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       strcpy( si->cloneShader, token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */\r
-                               else if( !Q_stricmp( token, "q3map_fur" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->furNumLayers = atoi( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->furOffset = atof( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->furFade = atof( token );\r
-                               }\r
-                               \r
-                               /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */\r
-                               else if( !Q_stricmp( token, "q3map_terrain" ) )\r
-                               {\r
-                                       /* team arena terrain is assumed to be nonplanar, with full normal averaging,\r
-                                          passed through the metatriangle surface pipeline, with a lightmap axis on z */\r
-                                       si->legacyTerrain = qtrue;\r
-                                       si->noClip = qtrue;\r
-                                       si->notjunc = qtrue;\r
-                                       si->indexed = qtrue;\r
-                                       si->nonplanar = qtrue;\r
-                                       si->forceMeta = qtrue;\r
-                                       si->shadeAngleDegrees = 179.0f;\r
-                                       //%     VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */\r
-                               }\r
-                               \r
-                               /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */\r
-                               else if( !Q_stricmp( token, "q3map_forceMeta" ) )\r
-                               {\r
-                                       si->forceMeta = qtrue;\r
-                               }\r
-                               \r
-                               /* ydnar: gs mods: q3map_shadeAngle <degrees> */\r
-                               else if( !Q_stricmp( token, "q3map_shadeAngle" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->shadeAngleDegrees = atof( token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */\r
-                               else if( !Q_stricmp( token, "q3map_textureSize" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->shaderWidth = atoi( token );\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       si->shaderHeight = atoi( token );\r
-                               }\r
-                               \r
-                               /* ydnar: gs mods: q3map_tcGen <style> <parameters> */\r
-                               else if( !Q_stricmp( token, "q3map_tcGen" ) )\r
-                               {\r
-                                       si->tcGen = qtrue;\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       \r
-                                       /* q3map_tcGen vector <s vector> <t vector> */\r
-                                       if( !Q_stricmp( token, "vector" ) )\r
-                                       {\r
-                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );\r
-                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );\r
-                                       }\r
-                                       \r
-                                       /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */\r
-                                       else if( !Q_stricmp( token, "ivector" ) )\r
-                                       {\r
-                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );\r
-                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );\r
-                                               for( i = 0; i < 3; i++ )\r
-                                               {\r
-                                                       si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;\r
-                                                       si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;\r
-                                               }\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               Sys_Printf( "WARNING: Unknown q3map_tcGen method: %s\n", token );\r
-                                               VectorClear( si->vecs[ 0 ] );\r
-                                               VectorClear( si->vecs[ 1 ] );\r
-                                       }\r
-                               }\r
-                               \r
-                               /* ydnar: gs mods: q3map_alphaMod <style> <parameters> */\r
-                               else if( !Q_stricmp( token, "q3map_alphaMod" ) )\r
-                               {\r
-                                       alphaMod_t      *am, *am2;\r
-                                       \r
-                                       \r
-                                       /* allocate new alpha mod */\r
-                                       am = safe_malloc( sizeof( *am ) );\r
-                                       memset( am, 0, sizeof( *am ) );\r
-                                       \r
-                                       /* attach to shader */\r
-                                       if( si->alphaMod == NULL )\r
-                                               si->alphaMod = am;\r
-                                       else\r
-                                       {\r
-                                               for( am2 = si->alphaMod; am2 != NULL; am2 = am2->next )\r
-                                               {\r
-                                                       if( am2->next == NULL )\r
-                                                       {\r
-                                                               am2->next = am;\r
-                                                               break;\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                                       \r
-                                       /* get type */\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       \r
-                                       /* q3map_alphaMod dotproduct ( X Y Z ) */\r
-                                       if( !Q_stricmp( token, "dotproduct" ) )\r
-                                       {\r
-                                               am->type = AM_DOT_PRODUCT;\r
-                                               Parse1DMatrixAppend( shaderText, 3, am->data );\r
-                                       }\r
-                                       else\r
-                                               Sys_Printf( "WARNING: Unknown q3map_alphaMod method: %s\n", token );\r
-                               }\r
-                               \r
-                               /* ydnar: gs mods: q3map_tcMod <style> <parameters> */\r
-                               else if( !Q_stricmp( token, "q3map_tcMod" ) )\r
-                               {\r
-                                       float   a, b;\r
-                                       \r
-                                       \r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       \r
-                                       /* q3map_tcMod [translate | shift | offset] <s> <t> */\r
-                                       if( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) )\r
-                                       {\r
-                                               GetTokenAppend( shaderText, qfalse );\r
-                                               a = atof( token );\r
-                                               GetTokenAppend( shaderText, qfalse );\r
-                                               b = atof( token );\r
-                                               \r
-                                               TcModTranslate( si->mod, a, b );\r
-                                       }\r
-\r
-                                       /* q3map_tcMod scale <s> <t> */\r
-                                       else if( !Q_stricmp( token, "scale" ) )\r
-                                       {\r
-                                               GetTokenAppend( shaderText, qfalse );\r
-                                               a = atof( token );\r
-                                               GetTokenAppend( shaderText, qfalse );\r
-                                               b = atof( token );\r
-                                               \r
-                                               TcModScale( si->mod, a, b );\r
-                                       }\r
-                                       \r
-                                       /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */\r
-                                       else if( !Q_stricmp( token, "rotate" ) )\r
-                                       {\r
-                                               GetTokenAppend( shaderText, qfalse );\r
-                                               a = atof( token );\r
-                                               TcModRotate( si->mod, a );\r
-                                       }\r
-                                       else\r
-                                               Sys_Printf( "WARNING: Unknown q3map_tcMod method: %s\n", token );\r
-                               }\r
-                               \r
-                               /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */\r
-                               else if( !Q_stricmp( token, "q3map_fogDir" ) )\r
-                               {\r
-                                       Parse1DMatrixAppend( shaderText, 3, si->fogDir );\r
-                                       VectorNormalize( si->fogDir, si->fogDir );\r
-                               }\r
-                               \r
-                               /* q3map_globaltexture */\r
-                               else if( !Q_stricmp( token, "q3map_globaltexture" )  )\r
-                                       si->globalTexture = qtrue;\r
-                               \r
-                               /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */\r
-                               else if( !Q_stricmp( token, "q3map_nonplanar" ) )\r
-                                       si->nonplanar = qtrue;\r
-                               \r
-                               /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */\r
-                               else if( !Q_stricmp( token, "q3map_noclip" ) )\r
-                                       si->noClip = qtrue;\r
-                               \r
-                               /* q3map_notjunc */\r
-                               else if( !Q_stricmp( token, "q3map_notjunc" ) )\r
-                                       si->notjunc = qtrue;\r
-                               \r
-                               /* q3map_nofog */\r
-                               else if( !Q_stricmp( token, "q3map_nofog" ) )\r
-                                       si->noFog = qtrue;\r
-                               \r
-                               /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */\r
-                               else if( !Q_stricmp( token, "q3map_indexed" ) )\r
-                                       si->indexed = qtrue;\r
-                               \r
-                               /* ydnar: q3map_invert (inverts a drawsurface's facing) */\r
-                               else if( !Q_stricmp( token, "q3map_invert" ) )\r
-                                       si->invert = qtrue;\r
-                               \r
-                               /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */\r
-                               else if( !Q_stricmp( token, "q3map_lightmapMergable" ) )\r
-                                       si->lmMergable = qtrue;\r
-                               \r
-                               /* ydnar: q3map_nofast */\r
-                               else if( !Q_stricmp( token, "q3map_noFast" ) )\r
-                                       si->noFast = qtrue;\r
-                               \r
-                               /* q3map_patchshadows */\r
-                               else if( !Q_stricmp( token, "q3map_patchShadows" ) )\r
-                                       si->patchShadows = qtrue;\r
-                               \r
-                               /* q3map_vertexshadows */\r
-                               else if( !Q_stricmp( token, "q3map_vertexShadows" ) )\r
-                                       si->vertexShadows = qtrue;      /* ydnar */\r
-                               \r
-                               /* q3map_novertexshadows */\r
-                               else if( !Q_stricmp( token, "q3map_noVertexShadows" ) )\r
-                                       si->vertexShadows = qfalse;     /* ydnar */\r
-                               \r
-                               /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */\r
-                               else if( !Q_stricmp( token, "q3map_splotchfix" ) )\r
-                                       si->splotchFix = qtrue; /* ydnar */\r
-                               \r
-                               /* q3map_forcesunlight */\r
-                               else if( !Q_stricmp( token, "q3map_forceSunlight" ) )\r
-                                       si->forceSunlight = qtrue;\r
-                               \r
-                               /* q3map_onlyvertexlighting (sof2) */\r
-                               else if( !Q_stricmp( token, "q3map_onlyVertexLighting" ) )\r
-                                       ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );\r
-                               \r
-                               /* q3map_material (sof2) */\r
-                               else if( !Q_stricmp( token, "q3map_material" ) )\r
-                               {\r
-                                       GetTokenAppend( shaderText, qfalse );\r
-                                       sprintf( temp, "*mat_%s", token );\r
-                                       if( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )\r
-                                               Sys_Printf( "WARNING: Unknown material \"%s\"\n", token );\r
-                               }\r
-                               \r
-                               /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */\r
-                               else if( !Q_stricmp( token, "q3map_clipmodel" )  )\r
-                                       si->clipModel = qtrue;\r
-                               \r
-                               /* ydnar: q3map_styleMarker[2] */\r
-                               else if( !Q_stricmp( token, "q3map_styleMarker" ) )\r
-                                       si->styleMarker = 1;\r
-                               else if( !Q_stricmp( token, "q3map_styleMarker2" ) )    /* uses depthFunc equal */\r
-                                       si->styleMarker = 2;\r
-                               \r
-                               /* ydnar: default to searching for q3map_<surfaceparm> */\r
-                               else\r
-                               {\r
-                                       //%     Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );\r
-                                       if( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )\r
-                                               ;//%    Sys_Printf( "WARNING: Unknown q3map_* directive \"%s\"\n", token );\r
-                               }\r
-                       }\r
-                       \r
-                       \r
-                       /* -----------------------------------------------------------------\r
-                          skip\r
-                          ----------------------------------------------------------------- */\r
-                       \r
-                       /* ignore all other tokens on the line */\r
-                       while( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) );\r
-               }                       \r
-       }\r
-}\r
-\r
-\r
-\r
-/*\r
-ParseCustomInfoParms() - rr2do2\r
-loads custom info parms file for mods\r
-*/\r
-\r
-static void ParseCustomInfoParms( void )\r
-{\r
-       qboolean parsedContent, parsedSurface;\r
-       \r
-       \r
-       /* file exists? */\r
-       if( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 )\r
-               return;\r
-       \r
-       /* load it */\r
-       LoadScriptFile( "scripts/custinfoparms.txt", 0 );\r
-       \r
-       /* clear the array */\r
-       memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );\r
-       numCustSurfaceParms = 0;\r
-       parsedContent = parsedSurface = qfalse;\r
-       \r
-       /* parse custom contentflags */\r
-       MatchToken( "{" );\r
-       while ( 1 )\r
-       {\r
-               if ( !GetToken( qtrue ) )\r
-                       break;\r
-\r
-               if ( !strcmp( token, "}" ) ) {\r
-                       parsedContent = qtrue;\r
-                       break;\r
-               }\r
-\r
-               custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );\r
-               strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );\r
-               GetToken( qfalse );\r
-               sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );\r
-               numCustSurfaceParms++;\r
-       }\r
-       \r
-       /* any content? */\r
-       if( !parsedContent )\r
-       {\r
-               Sys_Printf( "WARNING: Couldn't find valid custom contentsflag section\n" );\r
-               return;\r
-       }\r
-       \r
-       /* parse custom surfaceflags */\r
-       MatchToken( "{" );\r
-       while( 1 )\r
-       {\r
-               if( !GetToken( qtrue ) )\r
-                       break;\r
-\r
-               if( !strcmp( token, "}" ) )\r
-               {\r
-                       parsedSurface = qtrue;\r
-                       break;\r
-               }\r
-\r
-               custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );\r
-               strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );\r
-               GetToken( qfalse );\r
-               sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );\r
-               numCustSurfaceParms++;\r
-       }\r
-       \r
-       /* any content? */\r
-       if( !parsedContent )\r
-               Sys_Printf( "WARNING: Couldn't find valid custom surfaceflag section\n" );\r
-}\r
-\r
-       \r
-\r
-/*\r
-LoadShaderInfo()\r
-the shaders are parsed out of shaderlist.txt from a main directory\r
-that is, if using -fs_game we ignore the shader scripts that might be in baseq3/\r
-on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir\r
-*/\r
-\r
-#define        MAX_SHADER_FILES        1024\r
-\r
-void LoadShaderInfo( void )\r
-{\r
-       int                             i, j, numShaderFiles, count;\r
-       char                    filename[ 1024 ];\r
-       char                    *shaderFiles[ MAX_SHADER_FILES ];\r
-       \r
-       \r
-       /* rr2do2: parse custom infoparms first */\r
-       if( useCustomInfoParms )\r
-               ParseCustomInfoParms();\r
-       \r
-       /* start with zero */\r
-       numShaderFiles = 0;\r
-       \r
-       /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */\r
-       sprintf( filename, "%s/shaderlist.txt", game->shaderPath );\r
-       count = vfsGetFileCount( filename );\r
-       \r
-       /* load them all */\r
-       for( i = 0; i < count; i++ )\r
-       {\r
-               /* load shader list */\r
-               sprintf( filename, "%s/shaderlist.txt", game->shaderPath );\r
-               LoadScriptFile( filename, i );\r
-               \r
-               /* parse it */\r
-               while( GetToken( qtrue ) )\r
-               {\r
-                       /* check for duplicate entries */\r
-                       for( j = 0; j < numShaderFiles; j++ )\r
-                               if( !strcmp( shaderFiles[ j ], token ) )\r
-                                       break;\r
-                       \r
-                       /* test limit */\r
-                       if( j >= MAX_SHADER_FILES )\r
-                               Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );\r
-                       \r
-                       /* new shader file */\r
-                       if( j == numShaderFiles )\r
-                       {\r
-                               shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );\r
-                               strcpy( shaderFiles[ numShaderFiles ], token );\r
-                               numShaderFiles++;\r
-                       }\r
-               }\r
-       }\r
-       \r
-       /* parse the shader files */\r
-       for( i = 0; i < numShaderFiles; i++ )\r
-       {\r
-               sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );\r
-               ParseShaderFile( filename );\r
-               free( shaderFiles[ i ] );\r
-       }\r
-       \r
-       /* emit some statistics */\r
-       Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );\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 SHADERS_C
+
+
+
+/* dependencies */
+#include "q3map2.h"
+
+
+
+/*
+AlphaMod()
+routines for dealing with vertex alpha modification
+*/
+
+void AlphaMod( alphaMod_t *am, int numVerts, bspDrawVert_t *drawVerts )
+{
+       int                             i, j;
+       float                   mult, add, a;
+       bspDrawVert_t   *dv;
+       alphaMod_t              *am2;
+       
+       
+       /* dummy check */
+       if( am == NULL || numVerts < 1 || drawVerts == NULL )
+               return;
+       
+       
+       /* walk vertex list */
+       for( i = 0; i < numVerts; i++ )
+       {
+               /* get vertex */
+               dv = &drawVerts[ i ];
+               
+               /* walk alphamod list */
+               for( am2 = am; am2 != NULL; am2 = am2->next )
+               {
+                       /* switch on type */
+                       switch( am->type )
+                       {
+                               case AM_DOT_PRODUCT:
+                                       mult = DotProduct( dv->normal, am2->data );
+                                       add = 0.0f;
+                                       break;
+                               
+                               default:
+                                       mult = 1.0f;
+                                       add = 0.0f;
+                                       break;
+                       }
+                       
+                       /* apply mod */
+                       for( j = 0; j < MAX_LIGHTMAPS; j++ )
+                       {
+                               a = (mult * dv->color[ j ][ 3 ]) + add;
+                               if( a < 0 )
+                                       a = 0;
+                               else if( a > 255 )
+                                       a = 255;
+                               dv->color[ j ][ 3 ] = a;
+                       }
+               }
+       }
+}
+
+
+
+/*
+TcMod*()
+routines for dealing with a 3x3 texture mod matrix
+*/
+
+void TcMod( tcMod_t mod, float st[ 2 ] )
+{
+       float   old[ 2 ];
+       
+       
+       old[ 0 ] = st[ 0 ];
+       old[ 1 ] = st[ 1 ];
+       st[ 0 ] = (mod[ 0 ][ 0 ] * old[ 0 ]) + (mod[ 0 ][ 1 ] * old[ 1 ]) + mod[ 0 ][ 2 ];
+       st[ 1 ] = (mod[ 1 ][ 0 ] * old[ 0 ]) + (mod[ 1 ][ 1 ] * old[ 1 ]) + mod[ 1 ][ 2 ];
+}
+
+
+void TcModIdentity( tcMod_t mod )
+{
+       mod[ 0 ][ 0 ] = 1.0f;   mod[ 0 ][ 1 ] = 0.0f;   mod[ 0 ][ 2 ] = 0.0f;
+       mod[ 1 ][ 0 ] = 0.0f;   mod[ 1 ][ 1 ] = 1.0f;   mod[ 1 ][ 2 ] = 0.0f;
+       mod[ 2 ][ 0 ] = 0.0f;   mod[ 2 ][ 1 ] = 0.0f;   mod[ 2 ][ 2 ] = 1.0f;   /* this row is only used for multiples, not transformation */
+}
+
+
+void TcModMultiply( tcMod_t a, tcMod_t b, tcMod_t out )
+{
+       int             i;
+       
+       
+       for( i = 0; i < 3; i++ )
+       {
+               out[ i ][ 0 ] = (a[ i ][ 0 ] * b[ 0 ][ 0 ]) + (a[ i ][ 1 ] * b[ 1 ][ 0 ]) + (a[ i ][ 2 ] * b[ 2 ][ 0 ]);
+               out[ i ][ 1 ] = (a[ i ][ 0 ] * b[ 0 ][ 1 ]) + (a[ i ][ 1 ] * b[ 1 ][ 1 ]) + (a[ i ][ 2 ] * b[ 2 ][ 1 ]);
+               out[ i ][ 2 ] = (a[ i ][ 0 ] * b[ 0 ][ 2 ]) + (a[ i ][ 1 ] * b[ 1 ][ 2 ]) + (a[ i ][ 2 ] * b[ 2 ][ 2 ]);
+       }
+}
+
+
+void TcModTranslate( tcMod_t mod, float s, float t )
+{
+       mod[ 0 ][ 2 ] += s;
+       mod[ 1 ][ 2 ] += t;
+}
+
+
+void TcModScale( tcMod_t mod, float s, float t )
+{
+       mod[ 0 ][ 0 ] *= s;
+       mod[ 1 ][ 1 ] *= t;
+}
+
+
+void TcModRotate( tcMod_t mod, float euler )
+{
+       tcMod_t old, temp;
+       float   radians, sinv, cosv;
+       
+       
+       memcpy( old, mod, sizeof( tcMod_t ) );
+       TcModIdentity( temp );
+
+       radians = euler / 180 * Q_PI;
+       sinv = sin( radians );
+       cosv = cos( radians );
+
+       temp[ 0 ][ 0 ] = cosv;  temp[ 0 ][ 1 ] = -sinv;
+       temp[ 1 ][ 0 ] = sinv;  temp[ 1 ][ 1 ] = cosv;
+       
+       TcModMultiply( old, temp, mod );
+}
+
+
+
+/*
+ApplySurfaceParm() - ydnar
+applies a named surfaceparm to the supplied flags
+*/
+
+qboolean ApplySurfaceParm( char *name, int *contentFlags, int *surfaceFlags, int *compileFlags )
+{
+       int                             i, fake;
+       surfaceParm_t   *sp;
+       
+       
+       /* dummy check */
+       if( name == NULL )
+               name = "";
+       if( contentFlags == NULL )
+               contentFlags = &fake;
+       if( surfaceFlags == NULL )
+               surfaceFlags = &fake;
+       if( compileFlags == NULL )
+               compileFlags = &fake;
+       
+       /* walk the current game's surfaceparms */
+       sp = game->surfaceParms;
+       while( sp->name != NULL )
+       {
+               /* match? */
+               if( !Q_stricmp( name, sp->name ) )
+               {
+                       /* clear and set flags */
+                       *contentFlags &= ~(sp->contentFlagsClear);
+                       *contentFlags |= sp->contentFlags;
+                       *surfaceFlags &= ~(sp->surfaceFlagsClear);
+                       *surfaceFlags |= sp->surfaceFlags;
+                       *compileFlags &= ~(sp->compileFlagsClear);
+                       *compileFlags |= sp->compileFlags;
+                       
+                       /* return ok */
+                       return qtrue;
+               }
+               
+               /* next */
+               sp++;
+       }
+       
+       /* check custom info parms */
+       for( i = 0; i < numCustSurfaceParms; i++ )
+       {
+               /* get surfaceparm */
+               sp = &custSurfaceParms[ i ];
+               
+               /* match? */
+               if( !Q_stricmp( name, sp->name ) )
+               {
+                       /* clear and set flags */
+                       *contentFlags &= ~(sp->contentFlagsClear);
+                       *contentFlags |= sp->contentFlags;
+                       *surfaceFlags &= ~(sp->surfaceFlagsClear);
+                       *surfaceFlags |= sp->surfaceFlags;
+                       *compileFlags &= ~(sp->compileFlagsClear);
+                       *compileFlags |= sp->compileFlags;
+                       
+                       /* return ok */
+                       return qtrue;
+               }
+       }
+       
+       /* no matching surfaceparm found */
+       return qfalse;
+}
+
+
+
+/*
+BeginMapShaderFile() - ydnar
+erases and starts a new map shader script
+*/
+
+void BeginMapShaderFile( const char *mapFile )
+{
+       char    base[ 1024 ];
+       int             len;
+       
+
+       /* dummy check */
+       mapName[ 0 ] = '\0';
+       mapShaderFile[ 0 ] = '\0';
+       if( mapFile == NULL || mapFile[ 0 ] == '\0' )
+               return;
+       
+       /* copy map name */
+       strcpy( base, mapFile );
+       StripExtension( base );
+       
+       /* extract map name */
+       len = strlen( base ) - 1;
+       while( len > 0 && base[ len ] != '/' && base[ len ] != '\\' )
+               len--;
+       strcpy( mapName, &base[ len + 1 ] );
+       base[ len ] = '\0';
+       if( len <= 0 )
+               return;
+       
+       /* append ../scripts/q3map2_<mapname>.shader */
+       sprintf( mapShaderFile, "%s/../%s/q3map2_%s.shader", base, game->shaderPath, mapName );
+       Sys_FPrintf( SYS_VRB, "Map has shader script %s\n", mapShaderFile );
+       
+       /* remove it */
+       remove( mapShaderFile );
+       
+       /* stop making warnings about missing images */
+       warnImage = qfalse;
+}
+
+
+
+/*
+WriteMapShaderFile() - ydnar
+writes a shader to the map shader script
+*/
+
+void WriteMapShaderFile( void )
+{
+       FILE                    *file;
+       shaderInfo_t    *si;
+       int                             i, num;
+       
+       
+       /* dummy check */
+       if( mapShaderFile[ 0 ] == '\0' )
+               return;
+       
+       /* are there any custom shaders? */
+       for( i = 0, num = 0; i < numShaderInfo; i++ )
+       {
+               if( shaderInfo[ i ].custom ) 
+                       break;
+       }
+       if( i == numShaderInfo )
+               return;
+       
+       /* note it */
+       Sys_FPrintf( SYS_VRB, "--- WriteMapShaderFile ---\n");
+       Sys_FPrintf( SYS_VRB, "Writing %s", mapShaderFile );
+       
+       /* open shader file */
+       file = fopen( mapShaderFile, "w" );
+       if( file == NULL )
+       {
+               Sys_Printf( "WARNING: Unable to open map shader file %s for writing\n", mapShaderFile );
+               return;
+       }
+       
+       /* print header */
+       fprintf( file,
+               "// Custom shader file for %s.bsp\n"
+               "// Generated by Q3Map2 (ydnar)\n"
+               "// Do not edit! This file is overwritten on recompiles.\n\n",
+               mapName );
+       
+       /* walk the shader list */
+       for( i = 0, num = 0; i < numShaderInfo; i++ )
+       {
+               /* get the shader and print it */
+               si = &shaderInfo[ i ];
+               if( si->custom == qfalse || si->shaderText == NULL || si->shaderText[ 0 ] == '\0' )
+                       continue;
+               num++;
+
+               /* print it to the file */
+               fprintf( file, "%s%s\n", si->shader, si->shaderText );
+               //%     Sys_Printf( "%s%s\n", si->shader, si->shaderText ); /* FIXME: remove debugging code */
+               
+               Sys_FPrintf( SYS_VRB, "." );
+       }
+       
+       /* close the shader */
+       fclose( file );
+       
+       Sys_FPrintf( SYS_VRB, "\n" );
+       
+       /* print some stats */
+       Sys_Printf( "%9d custom shaders emitted\n", num );
+}
+
+
+
+/*
+CustomShader() - ydnar
+sets up a custom map shader
+*/
+
+shaderInfo_t *CustomShader( shaderInfo_t *si, char *find, char *replace )
+{
+       shaderInfo_t    *csi;
+       char                    shader[ MAX_QPATH ];
+       char                    *s;
+       int                             loc;
+       md5_state_t             md5;
+       byte                    digest[ 16 ];
+       char                    *srcShaderText, temp[ 8192 ], shaderText[ 8192 ];       /* ydnar: fixme (make this bigger?) */
+       
+       
+       /* dummy check */
+       if( si == NULL )
+               return ShaderInfoForShader( "default" );
+       
+       /* default shader text source */
+       srcShaderText = si->shaderText;
+       
+       /* et: implicitMap */
+       if( si->implicitMap == IM_OPAQUE )
+       {
+               srcShaderText = temp;
+               sprintf( temp, "\n"
+                       "{ // Q3Map2 defaulted (implicitMap)\n"
+                       "\t{\n"
+                       "\t\tmap $lightmap\n"
+                       "\t\trgbGen identity\n"
+                       "\t}\n"
+                       "\tq3map_styleMarker\n"
+                       "\t{\n"
+                       "\t\tmap %s\n"
+                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
+                       "\t\trgbGen identity\n"
+                       "\t}\n"
+                       "}\n",
+                       si->implicitImagePath );
+       }
+       
+       /* et: implicitMask */
+       else if( si->implicitMap == IM_MASKED )
+       {
+               srcShaderText = temp;
+               sprintf( temp, "\n"
+                       "{ // Q3Map2 defaulted (implicitMask)\n"
+                       "\tcull none\n"
+                       "\t{\n"
+                       "\t\tmap %s\n"
+                       "\t\talphaFunc GE128\n"
+                       "\t\tdepthWrite\n"
+                       "\t}\n"
+                       "\t{\n"
+                       "\t\tmap $lightmap\n"
+                       "\t\trgbGen identity\n"
+                       "\t\tdepthFunc equal\n"
+                       "\t}\n"
+                       "\tq3map_styleMarker\n"
+                       "\t{\n"
+                       "\t\tmap %s\n"
+                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
+                       "\t\tdepthFunc equal\n"
+                       "\t\trgbGen identity\n"
+                       "\t}\n"
+                       "}\n",
+                       si->implicitImagePath,
+                       si->implicitImagePath );
+       }
+       
+       /* et: implicitBlend */
+       else if( si->implicitMap == IM_BLEND )
+       {
+               srcShaderText = temp;
+               sprintf( temp, "\n"
+                       "{ // Q3Map2 defaulted (implicitBlend)\n"
+                       "\tcull none\n"
+                       "\t{\n"
+                       "\t\tmap %s\n"
+                       "\t\tblendFunc GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA\n"
+                       "\t}\n"
+                       "\t{\n"
+                       "\t\tmap $lightmap\n"
+                       "\t\trgbGen identity\n"
+                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
+                       "\t}\n"
+                       "\tq3map_styleMarker\n"
+                       "}\n",
+                       si->implicitImagePath );
+       }
+       
+       /* default shader text */
+       else if( srcShaderText == NULL )
+       {
+               srcShaderText = temp;
+               sprintf( temp, "\n"
+                       "{ // Q3Map2 defaulted\n"
+                       "\t{\n"
+                       "\t\tmap $lightmap\n"
+                       "\t\trgbGen identity\n"
+                       "\t}\n"
+                       "\tq3map_styleMarker\n"
+                       "\t{\n"
+                       "\t\tmap %s.tga\n"
+                       "\t\tblendFunc GL_DST_COLOR GL_ZERO\n"
+                       "\t\trgbGen identity\n"
+                       "\t}\n"
+                       "}\n",
+                       si->shader );
+       }
+       
+       /* error check */
+       if( (strlen( mapName ) + 1 + 32) > MAX_QPATH )
+               Error( "Custom shader name length (%d) exceeded. Shorten your map name.\n", MAX_QPATH );
+       
+       /* do some bad find-replace */
+       s = strstr( srcShaderText, find );
+       if( s == NULL )
+               //%     strcpy( shaderText, srcShaderText );
+               return si;      /* testing just using the existing shader if this fails */
+       else
+       {
+               /* substitute 'find' with 'replace' */
+               loc = s - srcShaderText;
+               strcpy( shaderText, srcShaderText );
+               shaderText[ loc ] = '\0';
+               strcat( shaderText, replace );
+               strcat( shaderText, &srcShaderText[ loc + strlen( find ) ] );
+       }
+       
+       /* make md5 hash of the shader text */
+       md5_init( &md5 );
+       md5_append( &md5, shaderText, strlen( shaderText ) );
+       md5_finish( &md5, digest );
+       
+       /* mangle hash into a shader name */
+       sprintf( shader, "%s/%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", mapName,
+               digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ], 
+               digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ], digest[ 15 ] );
+       
+       /* get shader */
+       csi = ShaderInfoForShader( shader );
+       
+       /* might be a preexisting shader */
+       if( csi->custom )
+               return csi;
+       
+       /* clone the existing shader and rename */
+       memcpy( csi, si, sizeof( shaderInfo_t ) );
+       strcpy( csi->shader, shader );
+       csi->custom = qtrue;
+       
+       /* store new shader text */
+       csi->shaderText = safe_malloc( strlen( shaderText ) + 1 );
+       strcpy( csi->shaderText, shaderText );  /* LEAK! */
+       
+       /* return it */
+       return csi;
+}
+
+
+
+/*
+EmitVertexRemapShader()
+adds a vertexremapshader key/value pair to worldspawn
+*/
+
+void EmitVertexRemapShader( char *from, char *to )
+{
+       md5_state_t             md5;
+       byte                    digest[ 16 ];
+       char                    key[ 64 ], value[ 256 ];
+       
+       
+       /* dummy check */
+       if( from == NULL || from[ 0 ] == '\0' ||
+               to == NULL || to[ 0 ] == '\0' )
+               return;
+       
+       /* build value */
+       sprintf( value, "%s;%s", from, to );
+       
+       /* make md5 hash */
+       md5_init( &md5 );
+       md5_append( &md5, value, strlen( value ) );
+       md5_finish( &md5, digest );
+
+       /* make key (this is annoying, as vertexremapshader is precisely 17 characters,
+          which is one too long, so we leave off the last byte of the md5 digest) */
+       sprintf( key, "vertexremapshader%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
+               digest[ 0 ], digest[ 1 ], digest[ 2 ], digest[ 3 ], digest[ 4 ], digest[ 5 ], digest[ 6 ], digest[ 7 ], 
+               digest[ 8 ], digest[ 9 ], digest[ 10 ], digest[ 11 ], digest[ 12 ], digest[ 13 ], digest[ 14 ] );       /* no: digest[ 15 ] */
+       
+       /* add key/value pair to worldspawn */
+       SetKeyValue( &entities[ 0 ], key, value );
+}
+
+
+
+/*
+AllocShaderInfo()
+allocates and initializes a new shader
+*/
+
+static shaderInfo_t    *AllocShaderInfo( void )
+{
+       shaderInfo_t    *si;
+       
+       
+       /* allocate? */
+       if( shaderInfo == NULL )
+       {
+               shaderInfo = safe_malloc( sizeof( shaderInfo_t ) * MAX_SHADER_INFO );
+               numShaderInfo = 0;
+       }
+       
+       /* bounds check */
+       if( numShaderInfo == MAX_SHADER_INFO )
+               Error( "MAX_SHADER_INFO exceeded. Remove some PK3 files or shader scripts from shaderlist.txt and try again." );
+       si = &shaderInfo[ numShaderInfo ];
+       numShaderInfo++;
+       
+       /* ydnar: clear to 0 first */
+       memset( si, 0, sizeof( shaderInfo_t ) );
+       
+       /* set defaults */
+       ApplySurfaceParm( "default", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
+       
+       si->backsplashFraction = DEF_BACKSPLASH_FRACTION;
+       si->backsplashDistance = DEF_BACKSPLASH_DISTANCE;
+       
+       si->bounceScale = DEF_RADIOSITY_BOUNCE;
+       
+       si->lightStyle = LS_NORMAL;
+       
+       si->polygonOffset = qfalse;
+       
+       si->shadeAngleDegrees = 0.0f;
+       si->lightmapSampleSize = 0;
+       si->lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
+       si->patchShadows = qfalse;
+       si->vertexShadows = qtrue;      /* ydnar: changed default behavior */
+       si->forceSunlight = qfalse;
+       si->vertexScale = 1.0;
+       si->notjunc = qfalse;
+       
+       /* ydnar: set texture coordinate transform matrix to identity */
+       TcModIdentity( si->mod );
+       
+       /* ydnar: lightmaps can now be > 128x128 in an externally generated tga */
+       si->lmCustomWidth = lmCustomSize;       //%     LIGHTMAP_WIDTH;
+       si->lmCustomHeight = lmCustomSize;      //%     LIGHTMAP_HEIGHT;
+       
+       /* return to sender */
+       return si;
+}
+
+
+
+/*
+FinishShader() - ydnar
+sets a shader's width and height among other things
+*/
+
+void FinishShader( shaderInfo_t *si )
+{
+       int             x, y;
+       float   st[ 2 ], o[ 2 ], dist, bestDist;
+       vec4_t  color, bestColor, delta;
+       
+
+       /* don't double-dip */
+       if( si->finished )
+               return;
+       
+       /* if they're explicitly set, copy from image size */
+       if( si->shaderWidth == 0 && si->shaderHeight == 0 )
+       {
+               si->shaderWidth = si->shaderImage->width;
+               si->shaderHeight = si->shaderImage->height;
+       }
+       
+       /* legacy terrain has explicit image-sized texture projection */
+       if( si->legacyTerrain && si->tcGen == qfalse )
+       {
+               /* set xy texture projection */
+               si->tcGen = qtrue;
+               VectorSet( si->vecs[ 0 ], (1.0f / (si->shaderWidth * 0.5f)), 0, 0 );
+               VectorSet( si->vecs[ 1 ], 0, (1.0f / (si->shaderHeight * 0.5f)), 0 );
+       }
+       
+       /* find pixel coordinates best matching the average color of the image */
+       bestDist = 99999999;
+       o[ 0 ] = 1.0f / si->shaderImage->width;
+       o[ 1 ] = 1.0f / si->shaderImage->height;
+       for( y = 0, st[ 1 ] = 0.0f; y < si->shaderImage->height; y++, st[ 1 ] += o[ 1 ] )
+       {
+               for( x = 0, st[ 0 ] = 0.0f; x < si->shaderImage->width; x++, st[ 0 ] += o[ 0 ] )
+               {
+                       /* sample the shader image */
+                       RadSampleImage( si->shaderImage->pixels, si->shaderImage->width, si->shaderImage->height, st, color );
+                       
+                       /* determine error squared */
+                       VectorSubtract( color, si->averageColor, delta );
+                       delta[ 3 ] = color[ 3 ] - si->averageColor[ 3 ];
+                       dist = delta[ 0 ] * delta[ 0 ] + delta[ 1 ] * delta[ 1 ] + delta[ 2 ] * delta[ 2 ] + delta[ 3 ] * delta[ 3 ];
+                       if( dist < bestDist )
+                       {
+                               VectorCopy( color, bestColor );
+                               bestColor[ 3 ] = color[ 3 ];
+                               si->stFlat[ 0 ] = st[ 0 ];
+                               si->stFlat[ 1 ] = st[ 1 ];
+                       }
+               }
+       }
+       
+       /* set to finished */
+       si->finished = qtrue;
+}
+
+
+
+/*
+LoadShaderImages()
+loads a shader's images
+ydnar: image.c made this a bit simpler
+*/
+
+static void LoadShaderImages( shaderInfo_t *si )
+{
+       int                     i, count;
+       float           color[ 4 ];
+       
+       
+       /* nodraw shaders don't need images */
+       if( si->compileFlags & C_NODRAW )
+               si->shaderImage = ImageLoad( DEFAULT_IMAGE );
+       else
+       {
+               /* try to load editor image first */
+               si->shaderImage = ImageLoad( si->editorImagePath );
+               
+               /* then try shadername */
+               if( si->shaderImage == NULL )
+                       si->shaderImage = ImageLoad( si->shader );
+               
+               /* then try implicit image path (note: new behavior!) */
+               if( si->shaderImage == NULL )
+                       si->shaderImage = ImageLoad( si->implicitImagePath );
+               
+               /* then try lightimage (note: new behavior!) */
+               if( si->shaderImage == NULL )
+                       si->shaderImage = ImageLoad( si->lightImagePath );
+               
+               /* otherwise, use default image */
+               if( si->shaderImage == NULL )
+               {
+                       si->shaderImage = ImageLoad( DEFAULT_IMAGE );
+                       if( warnImage && strcmp( si->shader, "noshader" ) )
+                               Sys_Printf( "WARNING: Couldn't find image for shader %s\n", si->shader );
+               }
+               
+               /* load light image */
+               si->lightImage = ImageLoad( si->lightImagePath );
+               
+               /* load normalmap image (ok if this is NULL) */
+               si->normalImage = ImageLoad( si->normalImagePath );
+               if( si->normalImage != NULL )
+               {
+                       Sys_FPrintf( SYS_VRB, "Shader %s has\n"
+                                                                 "    NM %s\n", si->shader, si->normalImagePath );
+               }
+       }
+       
+       /* if no light image, use shader image */
+       if( si->lightImage == NULL )
+               si->lightImage = ImageLoad( si->shaderImage->name );
+       
+       /* create default and average colors */
+       count = si->lightImage->width * si->lightImage->height;
+       VectorClear( color );
+       color[ 3 ] = 0.0f;
+       for( i = 0; i < count; i++ )
+       {
+               color[ 0 ] += si->lightImage->pixels[ i * 4 + 0 ];
+               color[ 1 ] += si->lightImage->pixels[ i * 4 + 1 ];
+               color[ 2 ] += si->lightImage->pixels[ i * 4 + 2 ];
+               color[ 3 ] += si->lightImage->pixels[ i * 4 + 3 ];
+       }
+       
+       if( VectorLength( si->color ) <= 0.0f )
+               ColorNormalize( color, si->color );
+       VectorScale( color, (1.0f / count), si->averageColor );
+}
+
+
+
+/*
+ShaderInfoForShader()
+finds a shaderinfo for a named shader
+*/
+
+shaderInfo_t *ShaderInfoForShader( const char *shaderName )
+{
+       int                             i;
+       shaderInfo_t    *si;
+       char                    shader[ MAX_QPATH ];
+       
+       
+       /* dummy check */
+       if( shaderName == NULL || shaderName[ 0 ] == '\0' )
+       {
+               Sys_Printf( "WARNING: Null or empty shader name\n" );
+               shaderName = "missing";
+       }
+       
+       /* strip off extension */
+       strcpy( shader, shaderName );
+       StripExtension( shader );
+       
+       /* search for it */
+       for( i = 0; i < numShaderInfo; i++ )
+       {
+               si = &shaderInfo[ i ];
+               if( !Q_stricmp( shader, si->shader ) )
+               {
+                       /* load image if necessary */
+                       if( si->shaderImage == NULL )
+                       {
+                               LoadShaderImages( si );
+                               FinishShader( si );
+                       }
+                       
+                       /* return it */
+                       return si;
+               }
+       }
+       
+       /* allocate a default shader */
+       si = AllocShaderInfo();
+       strcpy( si->shader, shader );
+       LoadShaderImages( si );
+       FinishShader( si );
+       
+       /* return it */
+       return si;
+}
+
+
+
+/*
+GetTokenAppend() - ydnar
+gets a token and appends its text to the specified buffer
+*/
+
+static int     oldScriptLine = 0;
+static int     tabDepth = 0;
+
+qboolean GetTokenAppend( char *buffer, qboolean crossline )
+{
+       qboolean        r;
+       int                     i;
+       
+       
+       /* get the token */
+       r = GetToken( crossline );
+       if( r == qfalse || buffer == NULL || token[ 0 ] == '\0' )
+               return r;
+       
+       /* pre-tabstops */
+       if( token[ 0 ] == '}' )
+               tabDepth--;
+       
+       /* append? */
+       if( oldScriptLine != scriptline )
+       {
+               strcat( buffer, "\n" );
+               for( i = 0; i < tabDepth; i++ )
+                       strcat( buffer, "\t" );
+       }
+       else
+               strcat( buffer, " " );
+       oldScriptLine = scriptline;
+       strcat( buffer, token );
+       
+       /* post-tabstops */
+       if( token[ 0 ] == '{' )
+               tabDepth++;
+       
+       /* return */
+       return r;
+}
+
+
+void Parse1DMatrixAppend( char *buffer, int x, vec_t *m )
+{
+       int             i;
+       
+       
+       if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, "(" ) )
+               Error( "Parse1DMatrixAppend(): line %d: ( not found!", scriptline );
+       for( i = 0; i < x; i++ )
+       {
+               if( !GetTokenAppend( buffer, qfalse ) )
+                       Error( "Parse1DMatrixAppend(): line %d: Number not found!", scriptline );
+               m[ i ] = atof( token );
+       }
+       if( !GetTokenAppend( buffer, qtrue ) || strcmp( token, ")" ) )
+               Error( "Parse1DMatrixAppend(): line %d: ) not found!", scriptline );
+}
+
+
+
+
+/*
+ParseShaderFile()
+parses a shader file into discrete shaderInfo_t
+*/
+
+static void ParseShaderFile( const char *filename )
+{
+       int                             i, val;
+       shaderInfo_t    *si;
+       char                    *suffix, temp[ 1024 ];
+       char                    shaderText[ 8192 ];     /* ydnar: fixme (make this bigger?) */
+       
+       
+       /* init */
+       si = NULL;
+       shaderText[ 0 ] = '\0';
+       
+       /* load the shader */
+       LoadScriptFile( filename, 0 );
+       
+       /* tokenize it */
+       while( 1 )
+       {
+               /* copy shader text to the shaderinfo */
+               if( si != NULL && shaderText[ 0 ] != '\0' )
+               {
+                       strcat( shaderText, "\n" );
+                       si->shaderText = safe_malloc( strlen( shaderText ) + 1 );
+                       strcpy( si->shaderText, shaderText );
+                       //%     if( VectorLength( si->vecs[ 0 ] ) )
+                       //%             Sys_Printf( "%s\n", shaderText );
+               }
+               
+               /* ydnar: clear shader text buffer */
+               shaderText[ 0 ] = '\0';
+               
+               /* test for end of file */
+               if( !GetToken( qtrue ) )
+                       break;
+               
+               /* shader name is initial token */
+               si = AllocShaderInfo();
+               strcpy( si->shader, token );
+               
+               /* ignore ":q3map" suffix */
+               suffix = strstr( si->shader, ":q3map" );
+               if( suffix != NULL )
+                       *suffix = '\0';
+               
+               /* handle { } section */
+               if( !GetTokenAppend( shaderText, qtrue ) )
+                       break;
+               if( strcmp( token, "{" ) )
+               {
+                       if( si != NULL )
+                               Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s\nLast known shader: %s",
+                                       filename, scriptline, token, si->shader );
+                       else
+                               Error( "ParseShaderFile(): %s, line %d: { not found!\nFound instead: %s",
+                                       filename, scriptline, token );
+               }
+               
+               while( 1 )
+               {
+                       /* get the next token */
+                       if( !GetTokenAppend( shaderText, qtrue ) )
+                               break;
+                       if( !strcmp( token, "}" ) )
+                               break;
+                       
+                       
+                       /* -----------------------------------------------------------------
+                          shader stages (passes)
+                          ----------------------------------------------------------------- */
+                       
+                       /* parse stage directives */
+                       if( !strcmp( token, "{" ) )
+                       {
+                               si->hasPasses = qtrue;
+                               while( 1 )
+                               {
+                                       if( !GetTokenAppend( shaderText, qtrue ) )
+                                               break;
+                                       if( !strcmp( token, "}" ) )
+                                               break;
+                                       
+                                       /* only care about images if we don't have a editor/light image */
+                                       if( si->editorImagePath[ 0 ] == '\0' && si->lightImagePath[ 0 ] == '\0' && si->implicitImagePath[ 0 ] == '\0' )
+                                       {
+                                               /* digest any images */
+                                               if( !Q_stricmp( token, "map" ) ||
+                                                       !Q_stricmp( token, "clampMap" ) ||
+                                                       !Q_stricmp( token, "animMap" ) ||
+                                                       !Q_stricmp( token, "clampAnimMap" ) ||
+                                                       !Q_stricmp( token, "clampMap" ) ||
+                                                       !Q_stricmp( token, "mapComp" ) ||
+                                                       !Q_stricmp( token, "mapNoComp" ) )
+                                               {
+                                                       /* skip one token for animated stages */
+                                                       if( !Q_stricmp( token, "animMap" ) || !Q_stricmp( token, "clampAnimMap" ) )
+                                                               GetTokenAppend( shaderText, qfalse );
+                                                       
+                                                       /* get an image */
+                                                       GetTokenAppend( shaderText, qfalse );
+                                                       if( token[ 0 ] != '*' && token[ 0 ] != '$' )
+                                                       {
+                                                               strcpy( si->lightImagePath, token );
+                                                               DefaultExtension( si->lightImagePath, ".tga" );
+                                                               
+                                                               /* debug code */
+                                                               //%     Sys_FPrintf( SYS_VRB, "Deduced shader image: %s\n", si->lightImagePath );
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       
+                       
+                       /* -----------------------------------------------------------------
+                          surfaceparm * directives
+                          ----------------------------------------------------------------- */
+                       
+                       /* match surfaceparm */
+                       else if( !Q_stricmp( token, "surfaceparm" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               if( ApplySurfaceParm( token, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
+                                       Sys_Printf( "WARNING: Unknown surfaceparm: \"%s\"\n", token );
+                       }
+                       
+                       
+                       /* -----------------------------------------------------------------
+                          game-related shader directives
+                          ----------------------------------------------------------------- */
+                       
+                       /* ydnar: fogparms (for determining fog volumes) */
+                       else if( !Q_stricmp( token, "fogparms" ) )
+                               si->fogParms = qtrue;
+                       
+                       /* ydnar: polygonoffset (for no culling) */
+                       else if( !Q_stricmp( token, "polygonoffset" ) )
+                               si->polygonOffset = qtrue;
+                       
+                       /* tesssize is used to force liquid surfaces to subdivide */
+                       else if( !Q_stricmp( token, "tessSize" ) || !Q_stricmp( token, "q3map_tessSize" ) /* sof2 */ )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               si->subdivisions = atof( token );
+                       }
+                       
+                       /* cull none will set twoSided (ydnar: added disable too) */
+                       else if ( !Q_stricmp( token, "cull" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               if( !Q_stricmp( token, "none" ) || !Q_stricmp( token, "disable" ) || !Q_stricmp( token, "twosided" ) )
+                                       si->twoSided = qtrue;
+                       }
+                       
+                       /* deformVertexes autosprite[ 2 ]
+                          we catch this so autosprited surfaces become point
+                          lights instead of area lights */
+                       else if( !Q_stricmp( token, "deformVertexes" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               
+                               /* deformVertexes autosprite(2) */
+                               if( !Q_strncasecmp( token, "autosprite", 10 ) )
+                               {
+                                       /* set it as autosprite and detail */
+                                       si->autosprite = qtrue;
+                                       ApplySurfaceParm( "detail", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
+                                       
+                                       /* ydnar: gs mods: added these useful things */
+                                       si->noClip = qtrue;
+                                       si->notjunc = qtrue;
+                               }
+                               
+                               /* deformVertexes move <x> <y> <z> <func> <base> <amplitude> <phase> <freq> (ydnar: for particle studio support) */
+                               if( !Q_stricmp( token, "move") )
+                               {
+                                       vec3_t  amt, mins, maxs;
+                                       float   base, amp;
+                                       
+                                       
+                                       /* get move amount */
+                                       GetTokenAppend( shaderText, qfalse );   amt[ 0 ] = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );   amt[ 1 ] = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );   amt[ 2 ] = atof( token );
+                                       
+                                       /* skip func */
+                                       GetTokenAppend( shaderText, qfalse );
+                                       
+                                       /* get base and amplitude */
+                                       GetTokenAppend( shaderText, qfalse );   base = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );   amp = atof( token );
+                                       
+                                       /* calculate */
+                                       VectorScale( amt, base, mins );
+                                       VectorMA( mins, amp, amt, maxs );
+                                       VectorAdd( si->mins, mins, si->mins );
+                                       VectorAdd( si->maxs, maxs, si->maxs );
+                               } 
+                       }
+                       
+                       /* light <value> (old-style flare specification) */
+                       else if( !Q_stricmp( token, "light" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               strcpy( si->flareShader, "flareshader" );
+                       }
+                       
+                       /* ydnar: damageShader <shader> <health> (sof2 mods) */
+                       else if( !Q_stricmp( token, "damageShader" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               strcpy( si->damageShader, token );
+                               GetTokenAppend( shaderText, qfalse );   /* don't do anything with health */
+                       }
+
+                       /* ydnar: enemy territory implicit shaders */
+                       else if( !Q_stricmp( token, "implicitMap" ) )
+                       {
+                               si->implicitMap = IM_OPAQUE;
+                               GetTokenAppend( shaderText, qfalse );
+                               if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
+                                       sprintf( si->implicitImagePath, "%s.tga", si->shader );
+                               else
+                                       strcpy( si->implicitImagePath, token );
+                       }
+
+                       else if( !Q_stricmp( token, "implicitMask" ) )
+                       {
+                               si->implicitMap = IM_MASKED;
+                               GetTokenAppend( shaderText, qfalse );
+                               if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
+                                       sprintf( si->implicitImagePath, "%s.tga", si->shader );
+                               else
+                                       strcpy( si->implicitImagePath, token );
+                       }
+
+                       else if( !Q_stricmp( token, "implicitBlend" ) )
+                       {
+                               si->implicitMap = IM_MASKED;
+                               GetTokenAppend( shaderText, qfalse );
+                               if( token[ 0 ] == '-' && token[ 1 ] == '\0' )
+                                       sprintf( si->implicitImagePath, "%s.tga", si->shader );
+                               else
+                                       strcpy( si->implicitImagePath, token );
+                       }
+                       
+                       
+                       /* -----------------------------------------------------------------
+                          image directives
+                          ----------------------------------------------------------------- */
+                       
+                       /* qer_editorimage <image> */
+                       else if( !Q_stricmp( token, "qer_editorImage" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               strcpy( si->editorImagePath, token );
+                               DefaultExtension( si->editorImagePath, ".tga" );
+                       }
+                       
+                       /* ydnar: q3map_normalimage <image> (bumpmapping normal map) */
+                       else if( !Q_stricmp( token, "q3map_normalImage" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               strcpy( si->normalImagePath, token );
+                               DefaultExtension( si->normalImagePath, ".tga" );
+                       }
+                       
+                       /* q3map_lightimage <image> */
+                       else if( !Q_stricmp( token, "q3map_lightImage" ) )
+                       {
+                               GetTokenAppend( shaderText, qfalse );
+                               strcpy( si->lightImagePath, token );
+                               DefaultExtension( si->lightImagePath, ".tga" );
+                       }
+                       
+                       /* ydnar: skyparms <outer image> <cloud height> <inner image> */
+                       else if( !Q_stricmp( token, "skyParms" ) )
+                       {
+                               /* get image base */
+                               GetTokenAppend( shaderText, qfalse );
+                               
+                               /* ignore bogus paths */
+                               if( Q_stricmp( token, "-" ) && Q_stricmp( token, "full" ) )
+                               {
+                                       strcpy( si->skyParmsImageBase, token );
+                                       
+                                       /* use top image as sky light image */
+                                       if( si->lightImagePath[ 0 ] == '\0' )
+                                               sprintf( si->lightImagePath, "%s_up.tga", si->skyParmsImageBase );
+                               }
+                               
+                               /* skip rest of line */
+                               GetTokenAppend( shaderText, qfalse );
+                               GetTokenAppend( shaderText, qfalse );
+                       }
+                       
+                       /* -----------------------------------------------------------------
+                          q3map_* directives
+                          ----------------------------------------------------------------- */
+                       
+                       /* q3map_sun <red> <green> <blue> <intensity> <degrees> <elevation>
+                          color will be normalized, so it doesn't matter what range you use
+                          intensity falls off with angle but not distance 100 is a fairly bright sun
+                          degree of 0 = from the east, 90 = north, etc.  altitude of 0 = sunrise/set, 90 = noon
+                          ydnar: sof2map has bareword 'sun' token, so we support that as well */
+                       else if( !Q_stricmp( token, "sun" ) /* sof2 */ || !Q_stricmp( token, "q3map_sun" ) || !Q_stricmp( token, "q3map_sunExt" ) )
+                       {
+                               float           a, b;
+                               sun_t           *sun;
+                               qboolean        ext;
+                               
+                               
+                               /* ydnar: extended sun directive? */
+                               if( !Q_stricmp( token, "q3map_sunext" ) )
+                                       ext = qtrue;
+                               
+                               /* allocate sun */
+                               sun = safe_malloc( sizeof( *sun ) );
+                               memset( sun, 0, sizeof( *sun ) );
+                               
+                               /* get color */
+                               GetTokenAppend( shaderText, qfalse );
+                               sun->color[ 0 ] = atof( token );
+                               GetTokenAppend( shaderText, qfalse );
+                               sun->color[ 1 ] = atof( token );
+                               GetTokenAppend( shaderText, qfalse );
+                               sun->color[ 2 ] = atof( token );
+                               
+                               /* normalize it */
+                               VectorNormalize( sun->color, sun->color );
+                               
+                               /* scale color by brightness */
+                               GetTokenAppend( shaderText, qfalse );
+                               sun->photons = atof( token );
+                               
+                               /* get sun angle/elevation */
+                               GetTokenAppend( shaderText, qfalse );
+                               a = atof( token );
+                               a = a / 180.0f * Q_PI;
+                               
+                               GetTokenAppend( shaderText, qfalse );
+                               b = atof( token );
+                               b = b / 180.0f * Q_PI;
+                               
+                               sun->direction[ 0 ] = cos( a ) * cos( b );
+                               sun->direction[ 1 ] = sin( a ) * cos( b );
+                               sun->direction[ 2 ] = sin( b );
+                               
+                               /* get filter radius from shader */
+                               sun->filterRadius = si->lightFilterRadius;
+                               
+                               /* ydnar: get sun angular deviance/samples */
+                               if( ext && TokenAvailable() )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       sun->deviance = atof( token );
+                                       sun->deviance = sun->deviance / 180.0f * Q_PI;
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       sun->numSamples = atoi( token );
+                               }
+                               
+                               /* store sun */
+                               sun->next = si->sun;
+                               si->sun = sun;
+                               
+                               /* apply sky surfaceparm */
+                               ApplySurfaceParm( "sky", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
+                               
+                               /* don't process any more tokens on this line */
+                               continue;
+                       }
+
+                       /* match q3map_ */
+                       else if( !Q_strncasecmp( token, "q3map_", 6 ) )
+                       {
+                               /* ydnar: q3map_baseShader <shader> (inherit this shader's parameters) */
+                               if( !Q_stricmp( token, "q3map_baseShader" ) )
+                               {
+                                       shaderInfo_t    *si2;
+                                       qboolean                oldWarnImage;
+                                       
+                                       
+                                       /* get shader */
+                                       GetTokenAppend( shaderText, qfalse );
+                                       //%     Sys_FPrintf( SYS_VRB, "Shader %s has base shader %s\n", si->shader, token );
+                                       oldWarnImage = warnImage;
+                                       warnImage = qfalse;
+                                       si2 = ShaderInfoForShader( token );
+                                       warnImage = oldWarnImage;
+                                       
+                                       /* subclass it */
+                                       if( si2 != NULL )
+                                       {
+                                               /* preserve name */
+                                               strcpy( temp, si->shader );
+                                               
+                                               /* copy shader */
+                                               memcpy( si, si2, sizeof( *si ) );
+                                               
+                                               /* restore name and set to unfinished */
+                                               strcpy( si->shader, temp );
+                                               si->finished = qfalse;
+                                       }
+                               }
+                               
+                               /* ydnar: q3map_surfacemodel <path to model> <density> <min scale> <max scale> <min angle> <max angle> <oriented (0 or 1)> */
+                               else if( !Q_stricmp( token, "q3map_surfacemodel" ) )
+                               {
+                                       surfaceModel_t  *model;
+                                       
+                                       
+                                       /* allocate new model and attach it */
+                                       model = safe_malloc( sizeof( *model ) );
+                                       memset( model, 0, sizeof( *model ) );
+                                       model->next = si->surfaceModel;
+                                       si->surfaceModel = model;
+                                               
+                                       /* get parameters */
+                                       GetTokenAppend( shaderText, qfalse );
+                                       strcpy( model->model, token );
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->density = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->odds = atof( token );
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->minScale = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->maxScale = atof( token );
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->minAngle = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->maxAngle = atof( token );
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       model->oriented = (token[ 0 ] == '1' ? qtrue : qfalse);
+                               }
+                               
+                               /* ydnar/sd: q3map_foliage <path to model> <scale> <density> <odds> <invert alpha (1 or 0)> */
+                               else if( !Q_stricmp( token, "q3map_foliage" ) )
+                               {
+                                       foliage_t       *foliage;
+                                       
+                                       
+                                       /* allocate new foliage struct and attach it */
+                                       foliage = safe_malloc( sizeof( *foliage ) );
+                                       memset( foliage, 0, sizeof( *foliage ) );
+                                       foliage->next = si->foliage;
+                                       si->foliage = foliage;
+                                       
+                                       /* get parameters */
+                                       GetTokenAppend( shaderText, qfalse );
+                                       strcpy( foliage->model, token );
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       foliage->scale = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       foliage->density = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       foliage->odds = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       foliage->inverseAlpha = atoi( token );
+                               }
+                               
+                               /* ydnar: q3map_bounce <value> (fraction of light to re-emit during radiosity passes) */
+                               else if( !Q_stricmp( token, "q3map_bounce" ) || !Q_stricmp( token, "q3map_bounceScale" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->bounceScale = atof( token );
+                               }
+
+                               /* ydnar/splashdamage: q3map_skylight <value> <iterations> */
+                               else if( !Q_stricmp( token, "q3map_skylight" )  )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->skyLightValue = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->skyLightIterations = atoi( token );
+                                       
+                                       /* clamp */
+                                       if( si->skyLightValue < 0.0f )
+                                               si->skyLightValue = 0.0f;
+                                       if( si->skyLightIterations < 2 )
+                                               si->skyLightIterations = 2;
+                               }
+                               
+                               /* q3map_surfacelight <value> */
+                               else if( !Q_stricmp( token, "q3map_surfacelight" )  )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->value = atof( token );
+                               }
+                               
+                               
+                               /* q3map_lightStyle (sof2/jk2 lightstyle) */
+                               else if( !Q_stricmp( token, "q3map_lightStyle" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       val = atoi( token );
+                                       if( val < 0 )
+                                               val = 0;
+                                       else if( val > LS_NONE )
+                                               val = LS_NONE;
+                                       si->lightStyle = val;
+                               }
+                               
+                               /* wolf: q3map_lightRGB <red> <green> <blue> */
+                               else if( !Q_stricmp( token, "q3map_lightRGB" ) )
+                               {
+                                       VectorClear( si->color );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->color[ 0 ] = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->color[ 1 ] = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->color[ 2 ] = atof( token );
+                                       ColorNormalize( si->color, si->color );
+                               }
+                               
+                               /* q3map_lightSubdivide <value> */
+                               else if( !Q_stricmp( token, "q3map_lightSubdivide" )  )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lightSubdivide = atoi( token );
+                               }
+                               
+                               /* q3map_backsplash <percent> <distance> */
+                               else if( !Q_stricmp( token, "q3map_backsplash" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->backsplashFraction = atof( token ) * 0.01f;
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->backsplashDistance = atof( token );
+                               }
+                               
+                               /* q3map_lightmapSampleSize <value> */
+                               else if( !Q_stricmp( token, "q3map_lightmapSampleSize" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lightmapSampleSize = atoi( token );
+                               }
+                               
+                               /* q3map_lightmapSampleSffset <value> */
+                               else if( !Q_stricmp( token, "q3map_lightmapSampleOffset" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lightmapSampleOffset = atof( token );
+                               }
+                               
+                               /* ydnar: q3map_lightmapFilterRadius <self> <other> */
+                               else if( !Q_stricmp( token, "q3map_lightmapFilterRadius" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lmFilterRadius = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lightFilterRadius = atof( token );
+                               }
+                               
+                               /* ydnar: q3map_lightmapAxis [xyz] */
+                               else if( !Q_stricmp( token, "q3map_lightmapAxis" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       if( !Q_stricmp( token, "x" ) )
+                                               VectorSet( si->lightmapAxis, 1, 0, 0 );
+                                       else if( !Q_stricmp( token, "y" ) )
+                                               VectorSet( si->lightmapAxis, 0, 1, 0 );
+                                       else if( !Q_stricmp( token, "z" ) )
+                                               VectorSet( si->lightmapAxis, 0, 0, 1 );
+                                       else
+                                       {
+                                               Sys_Printf( "WARNING: Unknown value for lightmap axis: %s\n", token );
+                                               VectorClear( si->lightmapAxis );
+                                       }
+                               }
+                               
+                               /* ydnar: q3map_lightmapSize <width> <height> (for autogenerated shaders + external tga lightmaps) */
+                               else if( !Q_stricmp( token, "q3map_lightmapSize" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lmCustomWidth = atoi( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lmCustomHeight = atoi( token );
+                                       
+                                       /* must be a power of 2 */
+                                       if( ((si->lmCustomWidth - 1) & si->lmCustomWidth) ||
+                                               ((si->lmCustomHeight - 1) & si->lmCustomHeight) )
+                                       {
+                                               Sys_Printf( "WARNING: Non power-of-two lightmap size specified (%d, %d)\n",
+                                                        si->lmCustomWidth, si->lmCustomHeight );
+                                               si->lmCustomWidth = LIGHTMAP_WIDTH;
+                                               si->lmCustomHeight = LIGHTMAP_HEIGHT;
+                                       }
+                               }
+
+                               /* ydnar: q3map_lightmapGamma N (for autogenerated shaders + external tga lightmaps) */
+                               else if( !Q_stricmp( token, "q3map_lightmapGamma" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->lmGamma = atof( token );
+                                       if( si->lmGamma < 0 )
+                                               si->lmGamma = 1.0;
+                               }
+                               
+                               /* q3map_vertexScale (scale vertex lighting by this fraction) */
+                               else if( !Q_stricmp( token, "q3map_vertexScale" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->vertexScale = atof( token );
+                               }
+                               
+                               /* q3map_flare <shader> */
+                               else if( !Q_stricmp( token, "q3map_flare" ) || !Q_stricmp( token, "q3map_flareShader" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       strcpy( si->flareShader, token );
+                               }
+                               
+                               /* q3map_backShader <shader> */
+                               else if( !Q_stricmp( token, "q3map_backShader" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       strcpy( si->backShader, token );
+                               }
+                               
+                               /* ydnar: q3map_offset <value> */
+                               else if( !Q_stricmp( token, "q3map_offset" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->offset = atof( token );
+                               }
+                               
+                               /* ydnar: q3map_cloneShader <shader> */
+                               else if ( !Q_stricmp( token, "q3map_cloneShader" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       strcpy( si->cloneShader, token );
+                               }
+                               
+                               /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
+                               else if( !Q_stricmp( token, "q3map_fur" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->furNumLayers = atoi( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->furOffset = atof( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->furFade = atof( token );
+                               }
+                               
+                               /* ydnar: gs mods: legacy support for terrain/terrain2 shaders */
+                               else if( !Q_stricmp( token, "q3map_terrain" ) )
+                               {
+                                       /* team arena terrain is assumed to be nonplanar, with full normal averaging,
+                                          passed through the metatriangle surface pipeline, with a lightmap axis on z */
+                                       si->legacyTerrain = qtrue;
+                                       si->noClip = qtrue;
+                                       si->notjunc = qtrue;
+                                       si->indexed = qtrue;
+                                       si->nonplanar = qtrue;
+                                       si->forceMeta = qtrue;
+                                       si->shadeAngleDegrees = 179.0f;
+                                       //%     VectorSet( si->lightmapAxis, 0, 0, 1 ); /* ydnar 2002-09-21: turning this off for better lightmapping of cliff faces */
+                               }
+                               
+                               /* ydnar: picomodel: q3map_forceMeta (forces brush faces and/or triangle models to go through the metasurface pipeline) */
+                               else if( !Q_stricmp( token, "q3map_forceMeta" ) )
+                               {
+                                       si->forceMeta = qtrue;
+                               }
+                               
+                               /* ydnar: gs mods: q3map_shadeAngle <degrees> */
+                               else if( !Q_stricmp( token, "q3map_shadeAngle" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->shadeAngleDegrees = atof( token );
+                               }
+                               
+                               /* ydnar: q3map_textureSize <width> <height> (substitute for q3map_lightimage derivation for terrain) */
+                               else if( !Q_stricmp( token, "q3map_textureSize" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->shaderWidth = atoi( token );
+                                       GetTokenAppend( shaderText, qfalse );
+                                       si->shaderHeight = atoi( token );
+                               }
+                               
+                               /* ydnar: gs mods: q3map_tcGen <style> <parameters> */
+                               else if( !Q_stricmp( token, "q3map_tcGen" ) )
+                               {
+                                       si->tcGen = qtrue;
+                                       GetTokenAppend( shaderText, qfalse );
+                                       
+                                       /* q3map_tcGen vector <s vector> <t vector> */
+                                       if( !Q_stricmp( token, "vector" ) )
+                                       {
+                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
+                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
+                                       }
+                                       
+                                       /* q3map_tcGen ivector <1.0/s vector> <1.0/t vector> (inverse vector, easier for mappers to understand) */
+                                       else if( !Q_stricmp( token, "ivector" ) )
+                                       {
+                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 0 ] );
+                                               Parse1DMatrixAppend( shaderText, 3, si->vecs[ 1 ] );
+                                               for( i = 0; i < 3; i++ )
+                                               {
+                                                       si->vecs[ 0 ][ i ] = si->vecs[ 0 ][ i ] ? 1.0 / si->vecs[ 0 ][ i ] : 0;
+                                                       si->vecs[ 1 ][ i ] = si->vecs[ 1 ][ i ] ? 1.0 / si->vecs[ 1 ][ i ] : 0;
+                                               }
+                                       }
+                                       else
+                                       {
+                                               Sys_Printf( "WARNING: Unknown q3map_tcGen method: %s\n", token );
+                                               VectorClear( si->vecs[ 0 ] );
+                                               VectorClear( si->vecs[ 1 ] );
+                                       }
+                               }
+                               
+                               /* ydnar: gs mods: q3map_alphaMod <style> <parameters> */
+                               else if( !Q_stricmp( token, "q3map_alphaMod" ) )
+                               {
+                                       alphaMod_t      *am, *am2;
+                                       
+                                       
+                                       /* allocate new alpha mod */
+                                       am = safe_malloc( sizeof( *am ) );
+                                       memset( am, 0, sizeof( *am ) );
+                                       
+                                       /* attach to shader */
+                                       if( si->alphaMod == NULL )
+                                               si->alphaMod = am;
+                                       else
+                                       {
+                                               for( am2 = si->alphaMod; am2 != NULL; am2 = am2->next )
+                                               {
+                                                       if( am2->next == NULL )
+                                                       {
+                                                               am2->next = am;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                                       
+                                       /* get type */
+                                       GetTokenAppend( shaderText, qfalse );
+                                       
+                                       /* q3map_alphaMod dotproduct ( X Y Z ) */
+                                       if( !Q_stricmp( token, "dotproduct" ) )
+                                       {
+                                               am->type = AM_DOT_PRODUCT;
+                                               Parse1DMatrixAppend( shaderText, 3, am->data );
+                                       }
+                                       else
+                                               Sys_Printf( "WARNING: Unknown q3map_alphaMod method: %s\n", token );
+                               }
+                               
+                               /* ydnar: gs mods: q3map_tcMod <style> <parameters> */
+                               else if( !Q_stricmp( token, "q3map_tcMod" ) )
+                               {
+                                       float   a, b;
+                                       
+                                       
+                                       GetTokenAppend( shaderText, qfalse );
+                                       
+                                       /* q3map_tcMod [translate | shift | offset] <s> <t> */
+                                       if( !Q_stricmp( token, "translate" ) || !Q_stricmp( token, "shift" ) || !Q_stricmp( token, "offset" ) )
+                                       {
+                                               GetTokenAppend( shaderText, qfalse );
+                                               a = atof( token );
+                                               GetTokenAppend( shaderText, qfalse );
+                                               b = atof( token );
+                                               
+                                               TcModTranslate( si->mod, a, b );
+                                       }
+
+                                       /* q3map_tcMod scale <s> <t> */
+                                       else if( !Q_stricmp( token, "scale" ) )
+                                       {
+                                               GetTokenAppend( shaderText, qfalse );
+                                               a = atof( token );
+                                               GetTokenAppend( shaderText, qfalse );
+                                               b = atof( token );
+                                               
+                                               TcModScale( si->mod, a, b );
+                                       }
+                                       
+                                       /* q3map_tcMod rotate <s> <t> (fixme: make this communitive) */
+                                       else if( !Q_stricmp( token, "rotate" ) )
+                                       {
+                                               GetTokenAppend( shaderText, qfalse );
+                                               a = atof( token );
+                                               TcModRotate( si->mod, a );
+                                       }
+                                       else
+                                               Sys_Printf( "WARNING: Unknown q3map_tcMod method: %s\n", token );
+                               }
+                               
+                               /* q3map_fogDir (direction a fog shader fades from transparent to opaque) */
+                               else if( !Q_stricmp( token, "q3map_fogDir" ) )
+                               {
+                                       Parse1DMatrixAppend( shaderText, 3, si->fogDir );
+                                       VectorNormalize( si->fogDir, si->fogDir );
+                               }
+                               
+                               /* q3map_globaltexture */
+                               else if( !Q_stricmp( token, "q3map_globaltexture" )  )
+                                       si->globalTexture = qtrue;
+                               
+                               /* ydnar: gs mods: q3map_nonplanar (make it a nonplanar merge candidate for meta surfaces) */
+                               else if( !Q_stricmp( token, "q3map_nonplanar" ) )
+                                       si->nonplanar = qtrue;
+                               
+                               /* ydnar: gs mods: q3map_noclip (preserve original face winding, don't clip by bsp tree) */
+                               else if( !Q_stricmp( token, "q3map_noclip" ) )
+                                       si->noClip = qtrue;
+                               
+                               /* q3map_notjunc */
+                               else if( !Q_stricmp( token, "q3map_notjunc" ) )
+                                       si->notjunc = qtrue;
+                               
+                               /* q3map_nofog */
+                               else if( !Q_stricmp( token, "q3map_nofog" ) )
+                                       si->noFog = qtrue;
+                               
+                               /* ydnar: gs mods: q3map_indexed (for explicit terrain-style indexed mapping) */
+                               else if( !Q_stricmp( token, "q3map_indexed" ) )
+                                       si->indexed = qtrue;
+                               
+                               /* ydnar: q3map_invert (inverts a drawsurface's facing) */
+                               else if( !Q_stricmp( token, "q3map_invert" ) )
+                                       si->invert = qtrue;
+                               
+                               /* ydnar: gs mods: q3map_lightmapMergable (ok to merge non-planar */
+                               else if( !Q_stricmp( token, "q3map_lightmapMergable" ) )
+                                       si->lmMergable = qtrue;
+                               
+                               /* ydnar: q3map_nofast */
+                               else if( !Q_stricmp( token, "q3map_noFast" ) )
+                                       si->noFast = qtrue;
+                               
+                               /* q3map_patchshadows */
+                               else if( !Q_stricmp( token, "q3map_patchShadows" ) )
+                                       si->patchShadows = qtrue;
+                               
+                               /* q3map_vertexshadows */
+                               else if( !Q_stricmp( token, "q3map_vertexShadows" ) )
+                                       si->vertexShadows = qtrue;      /* ydnar */
+                               
+                               /* q3map_novertexshadows */
+                               else if( !Q_stricmp( token, "q3map_noVertexShadows" ) )
+                                       si->vertexShadows = qfalse;     /* ydnar */
+                               
+                               /* q3map_splotchfix (filter dark lightmap luxels on lightmapped models) */
+                               else if( !Q_stricmp( token, "q3map_splotchfix" ) )
+                                       si->splotchFix = qtrue; /* ydnar */
+                               
+                               /* q3map_forcesunlight */
+                               else if( !Q_stricmp( token, "q3map_forceSunlight" ) )
+                                       si->forceSunlight = qtrue;
+                               
+                               /* q3map_onlyvertexlighting (sof2) */
+                               else if( !Q_stricmp( token, "q3map_onlyVertexLighting" ) )
+                                       ApplySurfaceParm( "pointlight", &si->contentFlags, &si->surfaceFlags, &si->compileFlags );
+                               
+                               /* q3map_material (sof2) */
+                               else if( !Q_stricmp( token, "q3map_material" ) )
+                               {
+                                       GetTokenAppend( shaderText, qfalse );
+                                       sprintf( temp, "*mat_%s", token );
+                                       if( ApplySurfaceParm( temp, &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
+                                               Sys_Printf( "WARNING: Unknown material \"%s\"\n", token );
+                               }
+                               
+                               /* ydnar: q3map_clipmodel (autogenerate clip brushes for model triangles using this shader) */
+                               else if( !Q_stricmp( token, "q3map_clipmodel" )  )
+                                       si->clipModel = qtrue;
+                               
+                               /* ydnar: q3map_styleMarker[2] */
+                               else if( !Q_stricmp( token, "q3map_styleMarker" ) )
+                                       si->styleMarker = 1;
+                               else if( !Q_stricmp( token, "q3map_styleMarker2" ) )    /* uses depthFunc equal */
+                                       si->styleMarker = 2;
+                               
+                               /* ydnar: default to searching for q3map_<surfaceparm> */
+                               else
+                               {
+                                       //%     Sys_FPrintf( SYS_VRB, "Attempting to match %s with a known surfaceparm\n", token );
+                                       if( ApplySurfaceParm( &token[ 6 ], &si->contentFlags, &si->surfaceFlags, &si->compileFlags ) == qfalse )
+                                               ;//%    Sys_Printf( "WARNING: Unknown q3map_* directive \"%s\"\n", token );
+                               }
+                       }
+                       
+                       
+                       /* -----------------------------------------------------------------
+                          skip
+                          ----------------------------------------------------------------- */
+                       
+                       /* ignore all other tokens on the line */
+                       while( TokenAvailable() && GetTokenAppend( shaderText, qfalse ) );
+               }                       
+       }
+}
+
+
+
+/*
+ParseCustomInfoParms() - rr2do2
+loads custom info parms file for mods
+*/
+
+static void ParseCustomInfoParms( void )
+{
+       qboolean parsedContent, parsedSurface;
+       
+       
+       /* file exists? */
+       if( vfsGetFileCount( "scripts/custinfoparms.txt" ) == 0 )
+               return;
+       
+       /* load it */
+       LoadScriptFile( "scripts/custinfoparms.txt", 0 );
+       
+       /* clear the array */
+       memset( custSurfaceParms, 0, sizeof( custSurfaceParms ) );
+       numCustSurfaceParms = 0;
+       parsedContent = parsedSurface = qfalse;
+       
+       /* parse custom contentflags */
+       MatchToken( "{" );
+       while ( 1 )
+       {
+               if ( !GetToken( qtrue ) )
+                       break;
+
+               if ( !strcmp( token, "}" ) ) {
+                       parsedContent = qtrue;
+                       break;
+               }
+
+               custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
+               strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
+               GetToken( qfalse );
+               sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].contentFlags );
+               numCustSurfaceParms++;
+       }
+       
+       /* any content? */
+       if( !parsedContent )
+       {
+               Sys_Printf( "WARNING: Couldn't find valid custom contentsflag section\n" );
+               return;
+       }
+       
+       /* parse custom surfaceflags */
+       MatchToken( "{" );
+       while( 1 )
+       {
+               if( !GetToken( qtrue ) )
+                       break;
+
+               if( !strcmp( token, "}" ) )
+               {
+                       parsedSurface = qtrue;
+                       break;
+               }
+
+               custSurfaceParms[ numCustSurfaceParms ].name = safe_malloc( MAX_OS_PATH );
+               strcpy( custSurfaceParms[ numCustSurfaceParms ].name, token );
+               GetToken( qfalse );
+               sscanf( token, "%x", &custSurfaceParms[ numCustSurfaceParms ].surfaceFlags );
+               numCustSurfaceParms++;
+       }
+       
+       /* any content? */
+       if( !parsedContent )
+               Sys_Printf( "WARNING: Couldn't find valid custom surfaceflag section\n" );
+}
+
+       
+
+/*
+LoadShaderInfo()
+the shaders are parsed out of shaderlist.txt from a main directory
+that is, if using -fs_game we ignore the shader scripts that might be in baseq3/
+on linux there's an additional twist, we actually merge the stuff from ~/.q3a/ and from the base dir
+*/
+
+#define        MAX_SHADER_FILES        1024
+
+void LoadShaderInfo( void )
+{
+       int                             i, j, numShaderFiles, count;
+       char                    filename[ 1024 ];
+       char                    *shaderFiles[ MAX_SHADER_FILES ];
+       
+       
+       /* rr2do2: parse custom infoparms first */
+       if( useCustomInfoParms )
+               ParseCustomInfoParms();
+       
+       /* start with zero */
+       numShaderFiles = 0;
+       
+       /* we can pile up several shader files, the one in baseq3 and ones in the mod dir or other spots */
+       sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
+       count = vfsGetFileCount( filename );
+       
+       /* load them all */
+       for( i = 0; i < count; i++ )
+       {
+               /* load shader list */
+               sprintf( filename, "%s/shaderlist.txt", game->shaderPath );
+               LoadScriptFile( filename, i );
+               
+               /* parse it */
+               while( GetToken( qtrue ) )
+               {
+                       /* check for duplicate entries */
+                       for( j = 0; j < numShaderFiles; j++ )
+                               if( !strcmp( shaderFiles[ j ], token ) )
+                                       break;
+                       
+                       /* test limit */
+                       if( j >= MAX_SHADER_FILES )
+                               Error( "MAX_SHADER_FILES (%d) reached, trim your shaderlist.txt!", (int) MAX_SHADER_FILES );
+                       
+                       /* new shader file */
+                       if( j == numShaderFiles )
+                       {
+                               shaderFiles[ numShaderFiles ] = safe_malloc( MAX_OS_PATH );
+                               strcpy( shaderFiles[ numShaderFiles ], token );
+                               numShaderFiles++;
+                       }
+               }
+       }
+       
+       /* parse the shader files */
+       for( i = 0; i < numShaderFiles; i++ )
+       {
+               sprintf( filename, "%s/%s.shader", game->shaderPath, shaderFiles[ i ] );
+               ParseShaderFile( filename );
+               free( shaderFiles[ i ] );
+       }
+       
+       /* emit some statistics */
+       Sys_FPrintf( SYS_VRB, "%9d shaderInfo\n", numShaderInfo );
+}