]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/q3map2/convert_map.c
merge branch work back into trunk
[xonotic/netradiant.git] / tools / quake3 / q3map2 / convert_map.c
index ff69c8a565ec06df0f7db9d6d226b385f83a9a2a..1fabbc6366216bffaca73ceb8d1a62c3e7352d0b 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 CONVERT_MAP_C\r
-\r
-\r
-\r
-/* dependencies */\r
-#include "q3map2.h"\r
-\r
-\r
-\r
-/*\r
-ConvertBrush()\r
-exports a map brush\r
-*/\r
-\r
-#define        SNAP_FLOAT_TO_INT       4\r
-#define        SNAP_INT_TO_FLOAT       (1.0 / SNAP_FLOAT_TO_INT)\r
-\r
-static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin )\r
-{\r
-       int                             i, j;\r
-       bspBrushSide_t  *side;\r
-       side_t                  *buildSide;\r
-       bspShader_t             *shader;\r
-       char                    *texture;\r
-       bspPlane_t              *plane;\r
-       vec3_t                  pts[ 3 ];\r
-       \r
-       \r
-       /* start brush */\r
-       fprintf( f, "\t// brush %d\n", num );\r
-       fprintf( f, "\t{\n" );\r
-       \r
-       /* clear out build brush */\r
-       for( i = 0; i < buildBrush->numsides; i++ )\r
-       {\r
-               buildSide = &buildBrush->sides[ i ];\r
-               if( buildSide->winding != NULL )\r
-               {\r
-                       FreeWinding( buildSide->winding );\r
-                       buildSide->winding = NULL;\r
-               }\r
-       }\r
-       buildBrush->numsides = 0;\r
-       \r
-       /* iterate through bsp brush sides */\r
-       for( i = 0; i < brush->numSides; i++ )\r
-       {\r
-               /* get side */\r
-               side = &bspBrushSides[ brush->firstSide + i ];\r
-               \r
-               /* get shader */\r
-               if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )\r
-                       continue;\r
-               shader = &bspShaders[ side->shaderNum ];\r
-               if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )\r
-                       continue;\r
-               \r
-               /* get plane */\r
-               plane = &bspPlanes[ side->planeNum ];\r
-               \r
-               /* add build side */\r
-               buildSide = &buildBrush->sides[ buildBrush->numsides ];\r
-               buildBrush->numsides++;\r
-               \r
-               /* tag it */\r
-               buildSide->shaderInfo = ShaderInfoForShader( shader->shader );\r
-               buildSide->planenum = side->planeNum;\r
-               buildSide->winding = NULL;\r
-       }\r
-       \r
-       /* make brush windings */\r
-       if( !CreateBrushWindings( buildBrush ) )\r
-               return;\r
-       \r
-       /* iterate through build brush sides */\r
-       for( i = 0; i < buildBrush->numsides; i++ )\r
-       {\r
-               /* get build side */\r
-               buildSide = &buildBrush->sides[ i ];\r
-               \r
-               /* dummy check */\r
-               if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )\r
-                       continue;\r
-               \r
-               /* get texture name */\r
-               if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )\r
-                       texture = buildSide->shaderInfo->shader + 9;\r
-               else\r
-                       texture = buildSide->shaderInfo->shader;\r
-               \r
-               /* get plane points and offset by origin */\r
-               for( j = 0; j < 3; j++ )\r
-               {\r
-                       VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );\r
-                       //%     pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );\r
-                       //%     pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );\r
-                       //%     pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );\r
-               }\r
-               \r
-               /* print brush side */\r
-               /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */\r
-               fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s 0 0 0 0.5 0.5 0 0 0\n",\r
-                       pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],\r
-                       pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],\r
-                       pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],\r
-                       texture );\r
-       }\r
-       \r
-       /* end brush */\r
-       fprintf( f, "\t}\n\n" );\r
-}\r
-\r
-#if 0\r
-       /* iterate through the brush sides (ignore the first 6 bevel planes) */\r
-       for( i = 0; i < brush->numSides; i++ )\r
-       {\r
-               /* get side */\r
-               side = &bspBrushSides[ brush->firstSide + i ];\r
-               \r
-               /* get shader */\r
-               if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )\r
-                       continue;\r
-               shader = &bspShaders[ side->shaderNum ];\r
-               if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )\r
-                       continue;\r
-               \r
-               /* get texture name */\r
-               if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )\r
-                       texture = shader->shader + 9;\r
-               else\r
-                       texture = shader->shader;\r
-               \r
-               /* get plane */\r
-               plane = &bspPlanes[ side->planeNum ];\r
-\r
-               /* make plane points */\r
-               {\r
-                       vec3_t  vecs[ 2 ];\r
-\r
-                       \r
-                       MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );\r
-                       VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );\r
-                       VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );\r
-                       VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );\r
-               }\r
-\r
-               /* offset by origin */\r
-               for( j = 0; j < 3; j++ )\r
-                       VectorAdd( pts[ j ], origin, pts[ j ] );\r
-               \r
-               /* print brush side */\r
-               /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */\r
-               fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s 0 0 0 0.5 0.5 0 0 0\n",\r
-                       pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],\r
-                       pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],\r
-                       pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],\r
-                       texture );\r
-       }\r
-#endif\r
-\r
-\r
-\r
-/*\r
-ConvertPatch()\r
-converts a bsp patch to a map patch\r
-\r
-       {\r
-               patchDef2\r
-               {\r
-                       base_wall/concrete\r
-                       ( 9 3 0 0 0 )\r
-                       (\r
-                               ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )\r
-                               ...\r
-                       )\r
-               }\r
-       }\r
-\r
-*/\r
-\r
-static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )\r
-{\r
-       int                             x, y;\r
-       bspShader_t             *shader;\r
-       char                    *texture;\r
-       bspDrawVert_t   *dv;\r
-       vec3_t                  xyz;\r
-       \r
-       \r
-       /* only patches */\r
-       if( ds->surfaceType != MST_PATCH )\r
-               return;\r
-       \r
-       /* get shader */\r
-       if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )\r
-               return;\r
-       shader = &bspShaders[ ds->shaderNum ];\r
-       \r
-       /* get texture name */\r
-       if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )\r
-               texture = shader->shader + 9;\r
-       else\r
-               texture = shader->shader;\r
-       \r
-       /* start patch */\r
-       fprintf( f, "\t// patch %d\n", num );\r
-       fprintf( f, "\t{\n" );\r
-       fprintf( f, "\t\tpatchDef2\n" );\r
-       fprintf( f, "\t\t{\n" );\r
-       fprintf( f, "\t\t\t%s\n", texture );\r
-       fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );\r
-       fprintf( f, "\t\t\t(\n" );\r
-       \r
-       /* iterate through the verts */\r
-       for( x = 0; x < ds->patchWidth; x++ )\r
-       {\r
-               /* start row */\r
-               fprintf( f, "\t\t\t\t(" );\r
-               \r
-               /* iterate through the row */\r
-               for( y = 0; y < ds->patchHeight; y++ )\r
-               {\r
-                       /* get vert */\r
-                       dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];\r
-                       \r
-                       /* offset it */\r
-                       VectorAdd( origin, dv->xyz, xyz );\r
-                       \r
-                       /* print vertex */\r
-                       fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );\r
-               }\r
-               \r
-               /* end row */\r
-               fprintf( f, " )\n" );\r
-       }\r
-       \r
-       /* end patch */\r
-       fprintf( f, "\t\t\t)\n" );\r
-       fprintf( f, "\t\t}\n" );\r
-       fprintf( f, "\t}\n\n" );\r
-}\r
-\r
-\r
-\r
-/*\r
-ConvertModel()\r
-exports a bsp model to a map file\r
-*/\r
-\r
-static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )\r
-{\r
-       int                                     i, num;\r
-       bspBrush_t                      *brush;\r
-       bspDrawSurface_t        *ds;\r
-       \r
-       \r
-       /* convert bsp planes to map planes */\r
-       nummapplanes = numBSPPlanes;\r
-       for( i = 0; i < numBSPPlanes; i++ )\r
-       {\r
-               VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );\r
-               mapplanes[ i ].dist = bspPlanes[ i ].dist;\r
-               mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );\r
-               mapplanes[ i ].hash_chain = NULL;\r
-       }\r
-       \r
-       /* allocate a build brush */\r
-       buildBrush = AllocBrush( 512 );\r
-       buildBrush->entityNum = 0;\r
-       buildBrush->original = buildBrush;\r
-       \r
-       /* go through each brush in the model */\r
-       for( i = 0; i < model->numBSPBrushes; i++ )\r
-       {\r
-               num = i + model->firstBSPBrush;\r
-               brush = &bspBrushes[ num ];\r
-               ConvertBrush( f, num, brush, origin );\r
-       }\r
-       \r
-       /* free the build brush */\r
-       free( buildBrush );\r
-       \r
-       /* go through each drawsurf in the model */\r
-       for( i = 0; i < model->numBSPSurfaces; i++ )\r
-       {\r
-               num = i + model->firstBSPSurface;\r
-               ds = &bspDrawSurfaces[ num ];\r
-               \r
-               /* we only love patches */\r
-               if( ds->surfaceType == MST_PATCH )\r
-                       ConvertPatch( f, num, ds, origin );\r
-       }\r
-}\r
-\r
-\r
-\r
-/*\r
-ConvertEPairs()\r
-exports entity key/value pairs to a map file\r
-*/\r
-\r
-static void ConvertEPairs( FILE *f, entity_t *e )\r
-{\r
-       epair_t *ep;\r
-       \r
-       \r
-       /* walk epairs */\r
-       for( ep = e->epairs; ep != NULL; ep = ep->next )\r
-       {\r
-               /* ignore empty keys/values */\r
-               if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )\r
-                       continue;\r
-\r
-               /* ignore model keys with * prefixed values */\r
-               if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )\r
-                       continue;\r
-               \r
-               /* emit the epair */\r
-               fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );\r
-       }\r
-}\r
-\r
-\r
-\r
-/*\r
-ConvertBSPToMap()\r
-exports an quake map file from the bsp\r
-*/\r
-\r
-int ConvertBSPToMap( char *bspName )\r
-{\r
-       int                             i, modelNum;\r
-       FILE                    *f;\r
-       bspModel_t              *model;\r
-       entity_t                *e;\r
-       vec3_t                  origin;\r
-       const char              *value;\r
-       char                    name[ 1024 ], base[ 1024 ];\r
-       \r
-       \r
-       /* note it */\r
-       Sys_Printf( "--- Convert BSP to MAP ---\n" );\r
-       \r
-       /* create the bsp filename from the bsp name */\r
-       strcpy( name, bspName );\r
-       StripExtension( name );\r
-       strcat( name, "_converted.map" );\r
-       Sys_Printf( "writing %s\n", name );\r
-       \r
-       ExtractFileBase( bspName, base );\r
-       strcat( base, ".bsp" );\r
-       \r
-       /* open it */\r
-       f = fopen( name, "wb" );\r
-       if( f == NULL )\r
-               Error( "Open failed on %s\n", name );\r
-       \r
-       /* print header */\r
-       fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );\r
-       \r
-       /* walk entity list */\r
-       for( i = 0; i < numEntities; i++ )\r
-       {\r
-               /* get entity */\r
-               e = &entities[ i ];\r
-               \r
-               /* start entity */\r
-               fprintf( f, "// entity %d\n", i );\r
-               fprintf( f, "{\n" );\r
-               \r
-               /* export keys */\r
-               ConvertEPairs( f, e );\r
-               fprintf( f, "\n" );\r
-               \r
-               /* get model num */\r
-               if( i == 0 )\r
-                       modelNum = 0;\r
-               else\r
-               {\r
-                       value = ValueForKey( e, "model" );\r
-                       if( value[ 0 ] == '*' )\r
-                               modelNum = atoi( value + 1 );\r
-                       else\r
-                               modelNum = -1;\r
-               }\r
-               \r
-               /* only handle bsp models */\r
-               if( modelNum >= 0 )\r
-               {\r
-                       /* get model */\r
-                       model = &bspModels[ modelNum ];\r
-                       \r
-                       /* get entity origin */\r
-                       value = ValueForKey( e, "origin" );\r
-                       if( value[ 0 ] == '\0' )\r
-                               VectorClear( origin );\r
-                       else\r
-                               GetVectorForKey( e, "origin", origin );\r
-                       \r
-                       /* convert model */\r
-                       ConvertModel( f, model, modelNum, origin );\r
-               }\r
-               \r
-               /* end entity */\r
-               fprintf( f, "}\n\n" );\r
-       }\r
-       \r
-       /* close the file and return */\r
-       fclose( f );\r
-       \r
-       /* return to sender */\r
-       return 0;\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 CONVERT_MAP_C
+
+
+
+/* dependencies */
+#include "q3map2.h"
+
+
+
+/*
+ConvertBrush()
+exports a map brush
+*/
+
+#define        SNAP_FLOAT_TO_INT       4
+#define        SNAP_INT_TO_FLOAT       (1.0 / SNAP_FLOAT_TO_INT)
+
+static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin )
+{
+       int                             i, j;
+       bspBrushSide_t  *side;
+       side_t                  *buildSide;
+       bspShader_t             *shader;
+       char                    *texture;
+       bspPlane_t              *plane;
+       vec3_t                  pts[ 3 ];
+       
+       
+       /* start brush */
+       fprintf( f, "\t// brush %d\n", num );
+       fprintf( f, "\t{\n" );
+       
+       /* clear out build brush */
+       for( i = 0; i < buildBrush->numsides; i++ )
+       {
+               buildSide = &buildBrush->sides[ i ];
+               if( buildSide->winding != NULL )
+               {
+                       FreeWinding( buildSide->winding );
+                       buildSide->winding = NULL;
+               }
+       }
+       buildBrush->numsides = 0;
+       
+       /* iterate through bsp brush sides */
+       for( i = 0; i < brush->numSides; i++ )
+       {
+               /* get side */
+               side = &bspBrushSides[ brush->firstSide + i ];
+               
+               /* get shader */
+               if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
+                       continue;
+               shader = &bspShaders[ side->shaderNum ];
+               if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
+                       continue;
+               
+               /* get plane */
+               plane = &bspPlanes[ side->planeNum ];
+               
+               /* add build side */
+               buildSide = &buildBrush->sides[ buildBrush->numsides ];
+               buildBrush->numsides++;
+               
+               /* tag it */
+               buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
+               buildSide->planenum = side->planeNum;
+               buildSide->winding = NULL;
+       }
+       
+       /* make brush windings */
+       if( !CreateBrushWindings( buildBrush ) )
+               return;
+       
+       /* iterate through build brush sides */
+       for( i = 0; i < buildBrush->numsides; i++ )
+       {
+               /* get build side */
+               buildSide = &buildBrush->sides[ i ];
+               
+               /* dummy check */
+               if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )
+                       continue;
+               
+               /* get texture name */
+               if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )
+                       texture = buildSide->shaderInfo->shader + 9;
+               else
+                       texture = buildSide->shaderInfo->shader;
+               
+               /* get plane points and offset by origin */
+               for( j = 0; j < 3; j++ )
+               {
+                       VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
+                       //%     pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
+                       //%     pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
+                       //%     pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
+               }
+               
+               /* print brush side */
+               /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
+               fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s 0 0 0 0.5 0.5 0 0 0\n",
+                       pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
+                       pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
+                       pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
+                       texture );
+       }
+       
+       /* end brush */
+       fprintf( f, "\t}\n\n" );
+}
+
+#if 0
+       /* iterate through the brush sides (ignore the first 6 bevel planes) */
+       for( i = 0; i < brush->numSides; i++ )
+       {
+               /* get side */
+               side = &bspBrushSides[ brush->firstSide + i ];
+               
+               /* get shader */
+               if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
+                       continue;
+               shader = &bspShaders[ side->shaderNum ];
+               if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
+                       continue;
+               
+               /* get texture name */
+               if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
+                       texture = shader->shader + 9;
+               else
+                       texture = shader->shader;
+               
+               /* get plane */
+               plane = &bspPlanes[ side->planeNum ];
+
+               /* make plane points */
+               {
+                       vec3_t  vecs[ 2 ];
+
+                       
+                       MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
+                       VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
+                       VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
+                       VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
+               }
+
+               /* offset by origin */
+               for( j = 0; j < 3; j++ )
+                       VectorAdd( pts[ j ], origin, pts[ j ] );
+               
+               /* print brush side */
+               /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
+               fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) %s 0 0 0 0.5 0.5 0 0 0\n",
+                       pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
+                       pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
+                       pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
+                       texture );
+       }
+#endif
+
+
+
+/*
+ConvertPatch()
+converts a bsp patch to a map patch
+
+       {
+               patchDef2
+               {
+                       base_wall/concrete
+                       ( 9 3 0 0 0 )
+                       (
+                               ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
+                               ...
+                       )
+               }
+       }
+
+*/
+
+static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )
+{
+       int                             x, y;
+       bspShader_t             *shader;
+       char                    *texture;
+       bspDrawVert_t   *dv;
+       vec3_t                  xyz;
+       
+       
+       /* only patches */
+       if( ds->surfaceType != MST_PATCH )
+               return;
+       
+       /* get shader */
+       if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )
+               return;
+       shader = &bspShaders[ ds->shaderNum ];
+       
+       /* get texture name */
+       if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
+               texture = shader->shader + 9;
+       else
+               texture = shader->shader;
+       
+       /* start patch */
+       fprintf( f, "\t// patch %d\n", num );
+       fprintf( f, "\t{\n" );
+       fprintf( f, "\t\tpatchDef2\n" );
+       fprintf( f, "\t\t{\n" );
+       fprintf( f, "\t\t\t%s\n", texture );
+       fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
+       fprintf( f, "\t\t\t(\n" );
+       
+       /* iterate through the verts */
+       for( x = 0; x < ds->patchWidth; x++ )
+       {
+               /* start row */
+               fprintf( f, "\t\t\t\t(" );
+               
+               /* iterate through the row */
+               for( y = 0; y < ds->patchHeight; y++ )
+               {
+                       /* get vert */
+                       dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];
+                       
+                       /* offset it */
+                       VectorAdd( origin, dv->xyz, xyz );
+                       
+                       /* print vertex */
+                       fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
+               }
+               
+               /* end row */
+               fprintf( f, " )\n" );
+       }
+       
+       /* end patch */
+       fprintf( f, "\t\t\t)\n" );
+       fprintf( f, "\t\t}\n" );
+       fprintf( f, "\t}\n\n" );
+}
+
+
+
+/*
+ConvertModel()
+exports a bsp model to a map file
+*/
+
+static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
+{
+       int                                     i, num;
+       bspBrush_t                      *brush;
+       bspDrawSurface_t        *ds;
+       
+       
+       /* convert bsp planes to map planes */
+       nummapplanes = numBSPPlanes;
+       for( i = 0; i < numBSPPlanes; i++ )
+       {
+               VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
+               mapplanes[ i ].dist = bspPlanes[ i ].dist;
+               mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
+               mapplanes[ i ].hash_chain = NULL;
+       }
+       
+       /* allocate a build brush */
+       buildBrush = AllocBrush( 512 );
+       buildBrush->entityNum = 0;
+       buildBrush->original = buildBrush;
+       
+       /* go through each brush in the model */
+       for( i = 0; i < model->numBSPBrushes; i++ )
+       {
+               num = i + model->firstBSPBrush;
+               brush = &bspBrushes[ num ];
+               ConvertBrush( f, num, brush, origin );
+       }
+       
+       /* free the build brush */
+       free( buildBrush );
+       
+       /* go through each drawsurf in the model */
+       for( i = 0; i < model->numBSPSurfaces; i++ )
+       {
+               num = i + model->firstBSPSurface;
+               ds = &bspDrawSurfaces[ num ];
+               
+               /* we only love patches */
+               if( ds->surfaceType == MST_PATCH )
+                       ConvertPatch( f, num, ds, origin );
+       }
+}
+
+
+
+/*
+ConvertEPairs()
+exports entity key/value pairs to a map file
+*/
+
+static void ConvertEPairs( FILE *f, entity_t *e )
+{
+       epair_t *ep;
+       
+       
+       /* walk epairs */
+       for( ep = e->epairs; ep != NULL; ep = ep->next )
+       {
+               /* ignore empty keys/values */
+               if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )
+                       continue;
+
+               /* ignore model keys with * prefixed values */
+               if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )
+                       continue;
+               
+               /* emit the epair */
+               fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
+       }
+}
+
+
+
+/*
+ConvertBSPToMap()
+exports an quake map file from the bsp
+*/
+
+int ConvertBSPToMap( char *bspName )
+{
+       int                             i, modelNum;
+       FILE                    *f;
+       bspModel_t              *model;
+       entity_t                *e;
+       vec3_t                  origin;
+       const char              *value;
+       char                    name[ 1024 ], base[ 1024 ];
+       
+       
+       /* note it */
+       Sys_Printf( "--- Convert BSP to MAP ---\n" );
+       
+       /* create the bsp filename from the bsp name */
+       strcpy( name, bspName );
+       StripExtension( name );
+       strcat( name, "_converted.map" );
+       Sys_Printf( "writing %s\n", name );
+       
+       ExtractFileBase( bspName, base );
+       strcat( base, ".bsp" );
+       
+       /* open it */
+       f = fopen( name, "wb" );
+       if( f == NULL )
+               Error( "Open failed on %s\n", name );
+       
+       /* print header */
+       fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
+       
+       /* walk entity list */
+       for( i = 0; i < numEntities; i++ )
+       {
+               /* get entity */
+               e = &entities[ i ];
+               
+               /* start entity */
+               fprintf( f, "// entity %d\n", i );
+               fprintf( f, "{\n" );
+               
+               /* export keys */
+               ConvertEPairs( f, e );
+               fprintf( f, "\n" );
+               
+               /* get model num */
+               if( i == 0 )
+                       modelNum = 0;
+               else
+               {
+                       value = ValueForKey( e, "model" );
+                       if( value[ 0 ] == '*' )
+                               modelNum = atoi( value + 1 );
+                       else
+                               modelNum = -1;
+               }
+               
+               /* only handle bsp models */
+               if( modelNum >= 0 )
+               {
+                       /* get model */
+                       model = &bspModels[ modelNum ];
+                       
+                       /* get entity origin */
+                       value = ValueForKey( e, "origin" );
+                       if( value[ 0 ] == '\0' )
+                               VectorClear( origin );
+                       else
+                               GetVectorForKey( e, "origin", origin );
+                       
+                       /* convert model */
+                       ConvertModel( f, model, modelNum, origin );
+               }
+               
+               /* end entity */
+               fprintf( f, "}\n\n" );
+       }
+       
+       /* close the file and return */
+       fclose( f );
+       
+       /* return to sender */
+       return 0;
+}