try to eliminate MAX_MAP_PLANES limits
[divverent/netradiant.git] / tools / quake3 / q3map2 / convert_map.c
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22 ----------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define CONVERT_MAP_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 ConvertBrush()
43 exports a map brush
44 */
45
46 #define SNAP_FLOAT_TO_INT       4
47 #define SNAP_INT_TO_FLOAT       (1.0 / SNAP_FLOAT_TO_INT)
48
49 typedef vec_t vec2_t[2];
50
51 static vec_t Det3x3(vec_t a00, vec_t a01, vec_t a02,
52                     vec_t a10, vec_t a11, vec_t a12,
53                     vec_t a20, vec_t a21, vec_t a22)
54 {
55         return
56                 a00 * (a11 * a22 - a12 * a21)
57         -       a01 * (a10 * a22 - a12 * a20)
58         +       a02 * (a10 * a21 - a11 * a20);
59 }
60
61 void GetBestSurfaceTriangleMatchForBrushside(side_t *buildSide, bspDrawVert_t *bestVert[3])
62 {
63         bspDrawSurface_t *s;
64         int i;
65         int t;
66         vec_t best = 0;
67         vec_t thisarea;
68         vec3_t normdiff;
69         vec3_t v1v0, v2v0, norm;
70         bspDrawVert_t *vert[3];
71         winding_t *polygon;
72         plane_t *buildPlane = &mapplanes[buildSide->planenum];
73         int matches = 0;
74
75         // first, start out with NULLs
76         bestVert[0] = bestVert[1] = bestVert[2] = NULL;
77
78         // brute force through all surfaces
79         for(s = bspDrawSurfaces; s != bspDrawSurfaces + numBSPDrawSurfaces; ++s)
80         {
81                 if(s->surfaceType != MST_PLANAR && s->surfaceType != MST_TRIANGLE_SOUP)
82                         continue;
83                 if(strcmp(buildSide->shaderInfo->shader, bspShaders[s->shaderNum].shader))
84                         continue;
85                 for(t = 0; t + 3 <= s->numIndexes; t += 3)
86                 {
87                         vert[0] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 0]];
88                         vert[1] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 1]];
89                         vert[2] = &bspDrawVerts[s->firstVert + bspDrawIndexes[s->firstIndex + t + 2]];
90                         if(s->surfaceType == MST_PLANAR)
91                         {
92                                 VectorSubtract(vert[0]->normal, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
93                                 VectorSubtract(vert[1]->normal, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
94                                 VectorSubtract(vert[2]->normal, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
95                         }
96                         else
97                         {
98                                 // this is more prone to roundoff errors, but with embedded
99                                 // models, there is no better way
100                                 VectorSubtract(vert[1]->xyz, vert[0]->xyz, v1v0);
101                                 VectorSubtract(vert[2]->xyz, vert[0]->xyz, v2v0);
102                                 CrossProduct(v2v0, v1v0, norm);
103                                 VectorNormalize(norm, norm);
104                                 VectorSubtract(norm, buildPlane->normal, normdiff); if(VectorLength(normdiff) >= normalEpsilon) continue;
105                         }
106                         if(abs(DotProduct(vert[0]->xyz, buildPlane->normal) - buildPlane->dist) >= distanceEpsilon) continue;
107                         if(abs(DotProduct(vert[1]->xyz, buildPlane->normal) - buildPlane->dist) >= distanceEpsilon) continue;
108                         if(abs(DotProduct(vert[2]->xyz, buildPlane->normal) - buildPlane->dist) >= distanceEpsilon) continue;
109                         // Okay. Correct surface type, correct shader, correct plane. Let's start with the business...
110                         polygon = CopyWinding(buildSide->winding);
111                         for(i = 0; i < 3; ++i)
112                         {
113                                 // 0: 1, 2
114                                 // 1: 2, 0
115                                 // 2; 0, 1
116                                 vec3_t *v1 = &vert[(i+1)%3]->xyz;
117                                 vec3_t *v2 = &vert[(i+2)%3]->xyz;
118                                 vec3_t triNormal;
119                                 vec_t triDist;
120                                 vec3_t sideDirection;
121                                 // we now need to generate triNormal and triDist so that they represent the plane spanned by normal and (v2 - v1).
122                                 VectorSubtract(*v2, *v1, sideDirection);
123                                 CrossProduct(sideDirection, buildPlane->normal, triNormal);
124                                 triDist = DotProduct(*v1, triNormal);
125                                 ChopWindingInPlace(&polygon, triNormal, triDist, distanceEpsilon);
126                                 if(!polygon)
127                                         goto exwinding;
128                         }
129                         thisarea = WindingArea(polygon);
130                         if(thisarea > 0)
131                                 ++matches;
132                         if(thisarea > best)
133                         {
134                                 best = thisarea;
135                                 bestVert[0] = vert[0];
136                                 bestVert[1] = vert[1];
137                                 bestVert[2] = vert[2];
138                         }
139                         FreeWinding(polygon);
140 exwinding:
141                         ;
142                 }
143         }
144         //if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
145         //      fprintf(stderr, "brushside with %s: %d matches (%f area)\n", buildSide->shaderInfo->shader, matches, best);
146 }
147
148 static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin )
149 {
150         int                             i, j;
151         bspBrushSide_t  *side;
152         side_t                  *buildSide;
153         bspShader_t             *shader;
154         char                    *texture;
155         bspPlane_t              *plane;
156         plane_t         *buildPlane;
157         vec3_t                  pts[ 3 ];
158         bspDrawVert_t   *vert[3];
159         int valid;
160         
161         
162         /* start brush */
163         fprintf( f, "\t// brush %d\n", num );
164         fprintf( f, "\t{\n" );
165         fprintf( f, "\tbrushDef\n" );
166         fprintf( f, "\t{\n" );
167         
168         /* clear out build brush */
169         for( i = 0; i < buildBrush->numsides; i++ )
170         {
171                 buildSide = &buildBrush->sides[ i ];
172                 if( buildSide->winding != NULL )
173                 {
174                         FreeWinding( buildSide->winding );
175                         buildSide->winding = NULL;
176                 }
177         }
178         buildBrush->numsides = 0;
179         
180         /* iterate through bsp brush sides */
181         for( i = 0; i < brush->numSides; i++ )
182         {
183                 /* get side */
184                 side = &bspBrushSides[ brush->firstSide + i ];
185                 
186                 /* get shader */
187                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
188                         continue;
189                 shader = &bspShaders[ side->shaderNum ];
190                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
191                         continue;
192                 
193                 /* get plane */
194                 plane = &bspPlanes[ side->planeNum ];
195                 
196                 /* add build side */
197                 buildSide = &buildBrush->sides[ buildBrush->numsides ];
198                 buildBrush->numsides++;
199                 
200                 /* tag it */
201                 buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
202                 buildSide->planenum = side->planeNum;
203                 buildSide->winding = NULL;
204         }
205         
206         /* make brush windings */
207         if( !CreateBrushWindings( buildBrush ) )
208                 return;
209         
210         /* iterate through build brush sides */
211         for( i = 0; i < buildBrush->numsides; i++ )
212         {
213                 /* get build side */
214                 buildSide = &buildBrush->sides[ i ];
215                 
216                 /* get plane */
217                 buildPlane = &mapplanes[ buildSide->planenum ];
218
219                 /* dummy check */
220                 if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )
221                         continue;
222
223                 // st-texcoords -> texMat block
224                 // start out with dummy
225                 VectorSet(buildSide->texMat[0], 1/32.0, 0, 0);
226                 VectorSet(buildSide->texMat[1], 0, 1/32.0, 0);
227
228                 // find surface for this side (by brute force)
229                 // surface format:
230                 //   - meshverts point in pairs of three into verts
231                 //   - (triangles)
232                 //   - find the triangle that has most in common with our side
233                 GetBestSurfaceTriangleMatchForBrushside(buildSide, vert);
234                 valid = 0;
235
236                 if(vert[0] && vert[1] && vert[2])
237                 {
238                         int i;
239                         vec3_t texX, texY;
240                         vec3_t xy1I, xy1J, xy1K;
241                         vec2_t stI, stJ, stK;
242                         vec_t D, D0, D1, D2;
243
244                         ComputeAxisBase(buildPlane->normal, texX, texY);
245
246                         VectorSet(xy1I, DotProduct(vert[0]->xyz, texX), DotProduct(vert[0]->xyz, texY), 1);
247                         VectorSet(xy1J, DotProduct(vert[1]->xyz, texX), DotProduct(vert[1]->xyz, texY), 1);
248                         VectorSet(xy1K, DotProduct(vert[2]->xyz, texX), DotProduct(vert[2]->xyz, texY), 1);
249                         stI[0] = vert[0]->st[0]; stI[1] = vert[0]->st[1];
250                         stJ[0] = vert[1]->st[0]; stJ[1] = vert[1]->st[1];
251                         stK[0] = vert[2]->st[0]; stK[1] = vert[2]->st[1];
252
253                         //   - solve linear equations:
254                         //     - (x, y) := xyz . (texX, texY)
255                         //     - st[i] = texMat[i][0]*x + texMat[i][1]*y + texMat[i][2]
256                         //       (for three vertices)
257                         D = Det3x3(
258                                 xy1I[0], xy1I[1], 1,
259                                 xy1J[0], xy1J[1], 1,
260                                 xy1K[0], xy1K[1], 1
261                         );
262                         if(D != 0)
263                         {
264                                 for(i = 0; i < 2; ++i)
265                                 {
266                                         D0 = Det3x3(
267                                                 stI[i], xy1I[1], 1,
268                                                 stJ[i], xy1J[1], 1,
269                                                 stK[i], xy1K[1], 1
270                                         );
271                                         D1 = Det3x3(
272                                                 xy1I[0], stI[i], 1,
273                                                 xy1J[0], stJ[i], 1,
274                                                 xy1K[0], stK[i], 1
275                                         );
276                                         D2 = Det3x3(
277                                                 xy1I[0], xy1I[1], stI[i],
278                                                 xy1J[0], xy1J[1], stJ[i],
279                                                 xy1K[0], xy1K[1], stK[i]
280                                         );
281                                         VectorSet(buildSide->texMat[i], D0 / D, D1 / D, D2 / D);
282                                         valid = 1;
283                                 }
284                         }
285                         else
286                                 fprintf(stderr, "degenerate triangle found when solving texMat equations for\n(%f %f %f) (%f %f %f) (%f %f %f)\n( %f %f %f )\n( %f %f %f ) -> ( %f %f )\n( %f %f %f ) -> ( %f %f )\n( %f %f %f ) -> ( %f %f )\n",
287                                         buildPlane->normal[0], buildPlane->normal[1], buildPlane->normal[2],
288                                         vert[0]->normal[0], vert[0]->normal[1], vert[0]->normal[2],
289                                         texX[0], texX[1], texX[2], texY[0], texY[1], texY[2],
290                                         vert[0]->xyz[0], vert[0]->xyz[1], vert[0]->xyz[2], xy1I[0], xy1I[1],
291                                         vert[1]->xyz[0], vert[1]->xyz[1], vert[1]->xyz[2], xy1J[0], xy1J[1],
292                                         vert[2]->xyz[0], vert[2]->xyz[1], vert[2]->xyz[2], xy1K[0], xy1K[1]
293                                         );
294                 }
295                 else
296                         if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
297                                 fprintf(stderr, "no matching triangle for brushside using %s (hopefully nobody can see this side anyway)\n", buildSide->shaderInfo->shader);
298                 
299                 /* get texture name */
300                 if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )
301                         texture = buildSide->shaderInfo->shader + 9;
302                 else
303                         texture = buildSide->shaderInfo->shader;
304                 
305                 /* get plane points and offset by origin */
306                 for( j = 0; j < 3; j++ )
307                 {
308                         VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
309                         //%     pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
310                         //%     pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
311                         //%     pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
312                 }
313                 
314                 /* print brush side */
315                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
316                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
317                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
318                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
319                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
320                         buildSide->texMat[0][0], buildSide->texMat[0][1], buildSide->texMat[0][2],
321                         buildSide->texMat[1][0], buildSide->texMat[1][1], buildSide->texMat[1][2],
322                         texture,
323                         // DEBUG: valid ? 0 : C_DETAIL
324                         0
325                         );
326                 // TODO write brush primitives format here
327         }
328         
329         /* end brush */
330         fprintf( f, "\t}\n" );
331         fprintf( f, "\t}\n\n" );
332 }
333
334 #if 0
335         /* iterate through the brush sides (ignore the first 6 bevel planes) */
336         for( i = 0; i < brush->numSides; i++ )
337         {
338                 /* get side */
339                 side = &bspBrushSides[ brush->firstSide + i ];
340                 
341                 /* get shader */
342                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
343                         continue;
344                 shader = &bspShaders[ side->shaderNum ];
345                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
346                         continue;
347                 
348                 /* get texture name */
349                 if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
350                         texture = shader->shader + 9;
351                 else
352                         texture = shader->shader;
353                 
354                 /* get plane */
355                 plane = &bspPlanes[ side->planeNum ];
356
357                 /* make plane points */
358                 {
359                         vec3_t  vecs[ 2 ];
360
361                         
362                         MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
363                         VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
364                         VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
365                         VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
366                 }
367
368                 /* offset by origin */
369                 for( j = 0; j < 3; j++ )
370                         VectorAdd( pts[ j ], origin, pts[ j ] );
371                 
372                 /* print brush side */
373                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
374                 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",
375                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
376                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
377                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
378                         texture );
379         }
380 #endif
381
382
383
384 /*
385 ConvertPatch()
386 converts a bsp patch to a map patch
387
388         {
389                 patchDef2
390                 {
391                         base_wall/concrete
392                         ( 9 3 0 0 0 )
393                         (
394                                 ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
395                                 ...
396                         )
397                 }
398         }
399
400 */
401
402 static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )
403 {
404         int                             x, y;
405         bspShader_t             *shader;
406         char                    *texture;
407         bspDrawVert_t   *dv;
408         vec3_t                  xyz;
409         
410         
411         /* only patches */
412         if( ds->surfaceType != MST_PATCH )
413                 return;
414         
415         /* get shader */
416         if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )
417                 return;
418         shader = &bspShaders[ ds->shaderNum ];
419         
420         /* get texture name */
421         if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
422                 texture = shader->shader + 9;
423         else
424                 texture = shader->shader;
425         
426         /* start patch */
427         fprintf( f, "\t// patch %d\n", num );
428         fprintf( f, "\t{\n" );
429         fprintf( f, "\t\tpatchDef2\n" );
430         fprintf( f, "\t\t{\n" );
431         fprintf( f, "\t\t\t%s\n", texture );
432         fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
433         fprintf( f, "\t\t\t(\n" );
434         
435         /* iterate through the verts */
436         for( x = 0; x < ds->patchWidth; x++ )
437         {
438                 /* start row */
439                 fprintf( f, "\t\t\t\t(" );
440                 
441                 /* iterate through the row */
442                 for( y = 0; y < ds->patchHeight; y++ )
443                 {
444                         /* get vert */
445                         dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];
446                         
447                         /* offset it */
448                         VectorAdd( origin, dv->xyz, xyz );
449                         
450                         /* print vertex */
451                         fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
452                 }
453                 
454                 /* end row */
455                 fprintf( f, " )\n" );
456         }
457         
458         /* end patch */
459         fprintf( f, "\t\t\t)\n" );
460         fprintf( f, "\t\t}\n" );
461         fprintf( f, "\t}\n\n" );
462 }
463
464
465
466 /*
467 ConvertModel()
468 exports a bsp model to a map file
469 */
470
471 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
472 {
473         int                                     i, num;
474         bspBrush_t                      *brush;
475         bspDrawSurface_t        *ds;
476         
477         
478         /* convert bsp planes to map planes */
479         nummapplanes = numBSPPlanes;
480         for( i = 0; i < numBSPPlanes; i++ )
481         {
482                 VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
483                 mapplanes[ i ].dist = bspPlanes[ i ].dist;
484                 mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
485                 mapplanes[ i ].hash_chain = 0;
486         }
487         
488         /* allocate a build brush */
489         buildBrush = AllocBrush( 512 );
490         buildBrush->entityNum = 0;
491         buildBrush->original = buildBrush;
492         
493         /* go through each brush in the model */
494         for( i = 0; i < model->numBSPBrushes; i++ )
495         {
496                 num = i + model->firstBSPBrush;
497                 brush = &bspBrushes[ num ];
498                 ConvertBrush( f, num, brush, origin );
499         }
500         
501         /* free the build brush */
502         free( buildBrush );
503         
504         /* go through each drawsurf in the model */
505         for( i = 0; i < model->numBSPSurfaces; i++ )
506         {
507                 num = i + model->firstBSPSurface;
508                 ds = &bspDrawSurfaces[ num ];
509                 
510                 /* we only love patches */
511                 if( ds->surfaceType == MST_PATCH )
512                         ConvertPatch( f, num, ds, origin );
513         }
514 }
515
516
517
518 /*
519 ConvertEPairs()
520 exports entity key/value pairs to a map file
521 */
522
523 static void ConvertEPairs( FILE *f, entity_t *e )
524 {
525         epair_t *ep;
526         
527         
528         /* walk epairs */
529         for( ep = e->epairs; ep != NULL; ep = ep->next )
530         {
531                 /* ignore empty keys/values */
532                 if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )
533                         continue;
534
535                 /* ignore model keys with * prefixed values */
536                 if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )
537                         continue;
538                 
539                 /* emit the epair */
540                 fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
541         }
542 }
543
544
545
546 /*
547 ConvertBSPToMap()
548 exports an quake map file from the bsp
549 */
550
551 int ConvertBSPToMap( char *bspName )
552 {
553         int                             i, modelNum;
554         FILE                    *f;
555         bspModel_t              *model;
556         entity_t                *e;
557         vec3_t                  origin;
558         const char              *value;
559         char                    name[ 1024 ], base[ 1024 ];
560         
561         
562         /* note it */
563         Sys_Printf( "--- Convert BSP to MAP ---\n" );
564         
565         /* create the bsp filename from the bsp name */
566         strcpy( name, bspName );
567         StripExtension( name );
568         strcat( name, "_converted.map" );
569         Sys_Printf( "writing %s\n", name );
570         
571         ExtractFileBase( bspName, base );
572         strcat( base, ".bsp" );
573         
574         /* open it */
575         f = fopen( name, "wb" );
576         if( f == NULL )
577                 Error( "Open failed on %s\n", name );
578         
579         /* print header */
580         fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
581         
582         /* walk entity list */
583         for( i = 0; i < numEntities; i++ )
584         {
585                 /* get entity */
586                 e = &entities[ i ];
587                 
588                 /* start entity */
589                 fprintf( f, "// entity %d\n", i );
590                 fprintf( f, "{\n" );
591                 
592                 /* export keys */
593                 ConvertEPairs( f, e );
594                 fprintf( f, "\n" );
595                 
596                 /* get model num */
597                 if( i == 0 )
598                         modelNum = 0;
599                 else
600                 {
601                         value = ValueForKey( e, "model" );
602                         if( value[ 0 ] == '*' )
603                                 modelNum = atoi( value + 1 );
604                         else
605                                 modelNum = -1;
606                 }
607                 
608                 /* only handle bsp models */
609                 if( modelNum >= 0 )
610                 {
611                         /* get model */
612                         model = &bspModels[ modelNum ];
613                         
614                         /* get entity origin */
615                         value = ValueForKey( e, "origin" );
616                         if( value[ 0 ] == '\0' )
617                                 VectorClear( origin );
618                         else
619                                 GetVectorForKey( e, "origin", origin );
620                         
621                         /* convert model */
622                         ConvertModel( f, model, modelNum, origin );
623                 }
624                 
625                 /* end entity */
626                 fprintf( f, "}\n\n" );
627         }
628         
629         /* close the file and return */
630         fclose( f );
631         
632         /* return to sender */
633         return 0;
634 }