]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/convert_map.c
crude origin brush generation when decompiling
[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 ConvertOriginBrush( FILE *f, int num, vec3_t origin )
149 {
150         char pattern[6][3][3] = {
151                 { "+++", "+-+", "-++" },
152                 { "+++", "-++", "++-" },
153                 { "+++", "++-", "+-+" },
154                 { "---", "+--", "-+-" },
155                 { "---", "--+", "+--" },
156                 { "---", "-+-", "--+" }
157         };
158         int i;
159
160         /* start brush */
161         fprintf( f, "\t// brush %d\n", num );
162         fprintf( f, "\t{\n" );
163         fprintf( f, "\tbrushDef\n" );
164         fprintf( f, "\t{\n" );
165         /* print brush side */
166         /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
167
168         for(i = 0; i < 6; ++i)
169                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
170                                 origin[0] + (pattern[i][0][0] == '+' ? +8 : -8), origin[1] + (pattern[i][0][1] == '+' ? +8 : -8), origin[2] + (pattern[i][0][2] == '+' ? +8 : -8),
171                                 origin[0] + (pattern[i][1][0] == '+' ? +8 : -8), origin[1] + (pattern[i][1][1] == '+' ? +8 : -8), origin[2] + (pattern[i][1][2] == '+' ? +8 : -8),
172                                 origin[0] + (pattern[i][2][0] == '+' ? +8 : -8), origin[1] + (pattern[i][2][1] == '+' ? +8 : -8), origin[2] + (pattern[i][2][2] == '+' ? +8 : -8),
173                                 1/64.0, 0.0, 0.0, // TODO make these show the actual "ORIGIN" text properly
174                                 0.0, 1/64.0, 0.0,
175                                 "common/origin",
176                                 0
177                            );
178         
179         /* end brush */
180         fprintf( f, "\t}\n" );
181         fprintf( f, "\t}\n\n" );
182 }
183
184 static void ConvertBrush( FILE *f, int num, bspBrush_t *brush, vec3_t origin )
185 {
186         int                             i, j;
187         bspBrushSide_t  *side;
188         side_t                  *buildSide;
189         bspShader_t             *shader;
190         char                    *texture;
191         bspPlane_t              *plane;
192         plane_t         *buildPlane;
193         vec3_t                  pts[ 3 ];
194         bspDrawVert_t   *vert[3];
195         int valid;
196         
197         
198         /* start brush */
199         fprintf( f, "\t// brush %d\n", num );
200         fprintf( f, "\t{\n" );
201         fprintf( f, "\tbrushDef\n" );
202         fprintf( f, "\t{\n" );
203         
204         /* clear out build brush */
205         for( i = 0; i < buildBrush->numsides; i++ )
206         {
207                 buildSide = &buildBrush->sides[ i ];
208                 if( buildSide->winding != NULL )
209                 {
210                         FreeWinding( buildSide->winding );
211                         buildSide->winding = NULL;
212                 }
213         }
214         buildBrush->numsides = 0;
215         
216         /* iterate through bsp brush sides */
217         for( i = 0; i < brush->numSides; i++ )
218         {
219                 /* get side */
220                 side = &bspBrushSides[ brush->firstSide + i ];
221                 
222                 /* get shader */
223                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
224                         continue;
225                 shader = &bspShaders[ side->shaderNum ];
226                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
227                         continue;
228                 
229                 /* get plane */
230                 plane = &bspPlanes[ side->planeNum ];
231                 
232                 /* add build side */
233                 buildSide = &buildBrush->sides[ buildBrush->numsides ];
234                 buildBrush->numsides++;
235                 
236                 /* tag it */
237                 buildSide->shaderInfo = ShaderInfoForShader( shader->shader );
238                 buildSide->planenum = side->planeNum;
239                 buildSide->winding = NULL;
240         }
241         
242         /* make brush windings */
243         if( !CreateBrushWindings( buildBrush ) )
244                 return;
245         
246         /* iterate through build brush sides */
247         for( i = 0; i < buildBrush->numsides; i++ )
248         {
249                 /* get build side */
250                 buildSide = &buildBrush->sides[ i ];
251                 
252                 /* get plane */
253                 buildPlane = &mapplanes[ buildSide->planenum ];
254
255                 /* dummy check */
256                 if( buildSide->shaderInfo == NULL || buildSide->winding == NULL )
257                         continue;
258
259                 // st-texcoords -> texMat block
260                 // start out with dummy
261                 VectorSet(buildSide->texMat[0], 1/32.0, 0, 0);
262                 VectorSet(buildSide->texMat[1], 0, 1/32.0, 0);
263
264                 // find surface for this side (by brute force)
265                 // surface format:
266                 //   - meshverts point in pairs of three into verts
267                 //   - (triangles)
268                 //   - find the triangle that has most in common with our side
269                 GetBestSurfaceTriangleMatchForBrushside(buildSide, vert);
270                 valid = 0;
271
272                 if(vert[0] && vert[1] && vert[2])
273                 {
274                         int i;
275                         vec3_t texX, texY;
276                         vec3_t xy1I, xy1J, xy1K;
277                         vec2_t stI, stJ, stK;
278                         vec_t D, D0, D1, D2;
279
280                         ComputeAxisBase(buildPlane->normal, texX, texY);
281
282                         VectorSet(xy1I, DotProduct(vert[0]->xyz, texX), DotProduct(vert[0]->xyz, texY), 1);
283                         VectorSet(xy1J, DotProduct(vert[1]->xyz, texX), DotProduct(vert[1]->xyz, texY), 1);
284                         VectorSet(xy1K, DotProduct(vert[2]->xyz, texX), DotProduct(vert[2]->xyz, texY), 1);
285                         stI[0] = vert[0]->st[0]; stI[1] = vert[0]->st[1];
286                         stJ[0] = vert[1]->st[0]; stJ[1] = vert[1]->st[1];
287                         stK[0] = vert[2]->st[0]; stK[1] = vert[2]->st[1];
288
289                         //   - solve linear equations:
290                         //     - (x, y) := xyz . (texX, texY)
291                         //     - st[i] = texMat[i][0]*x + texMat[i][1]*y + texMat[i][2]
292                         //       (for three vertices)
293                         D = Det3x3(
294                                 xy1I[0], xy1I[1], 1,
295                                 xy1J[0], xy1J[1], 1,
296                                 xy1K[0], xy1K[1], 1
297                         );
298                         if(D != 0)
299                         {
300                                 for(i = 0; i < 2; ++i)
301                                 {
302                                         D0 = Det3x3(
303                                                 stI[i], xy1I[1], 1,
304                                                 stJ[i], xy1J[1], 1,
305                                                 stK[i], xy1K[1], 1
306                                         );
307                                         D1 = Det3x3(
308                                                 xy1I[0], stI[i], 1,
309                                                 xy1J[0], stJ[i], 1,
310                                                 xy1K[0], stK[i], 1
311                                         );
312                                         D2 = Det3x3(
313                                                 xy1I[0], xy1I[1], stI[i],
314                                                 xy1J[0], xy1J[1], stJ[i],
315                                                 xy1K[0], xy1K[1], stK[i]
316                                         );
317                                         VectorSet(buildSide->texMat[i], D0 / D, D1 / D, D2 / D);
318                                         valid = 1;
319                                 }
320                         }
321                         else
322                                 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",
323                                         buildPlane->normal[0], buildPlane->normal[1], buildPlane->normal[2],
324                                         vert[0]->normal[0], vert[0]->normal[1], vert[0]->normal[2],
325                                         texX[0], texX[1], texX[2], texY[0], texY[1], texY[2],
326                                         vert[0]->xyz[0], vert[0]->xyz[1], vert[0]->xyz[2], xy1I[0], xy1I[1],
327                                         vert[1]->xyz[0], vert[1]->xyz[1], vert[1]->xyz[2], xy1J[0], xy1J[1],
328                                         vert[2]->xyz[0], vert[2]->xyz[1], vert[2]->xyz[2], xy1K[0], xy1K[1]
329                                         );
330                 }
331                 else
332                         if(strncmp(buildSide->shaderInfo->shader, "textures/common/", 16))
333                                 fprintf(stderr, "no matching triangle for brushside using %s (hopefully nobody can see this side anyway)\n", buildSide->shaderInfo->shader);
334                 
335                 /* get texture name */
336                 if( !Q_strncasecmp( buildSide->shaderInfo->shader, "textures/", 9 ) )
337                         texture = buildSide->shaderInfo->shader + 9;
338                 else
339                         texture = buildSide->shaderInfo->shader;
340                 
341                 /* get plane points and offset by origin */
342                 for( j = 0; j < 3; j++ )
343                 {
344                         VectorAdd( buildSide->winding->p[ j ], origin, pts[ j ] );
345                         //%     pts[ j ][ 0 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 0 ] * SNAP_FLOAT_TO_INT + 0.5f );
346                         //%     pts[ j ][ 1 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 1 ] * SNAP_FLOAT_TO_INT + 0.5f );
347                         //%     pts[ j ][ 2 ] = SNAP_INT_TO_FLOAT * floor( pts[ j ][ 2 ] * SNAP_FLOAT_TO_INT + 0.5f );
348                 }
349                 
350                 /* print brush side */
351                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
352                 fprintf( f, "\t\t( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( %.3f %.3f %.3f ) ( ( %.8f %.8f %.8f ) ( %.8f %.8f %.8f ) ) %s %d 0 0\n",
353                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
354                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
355                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
356                         buildSide->texMat[0][0], buildSide->texMat[0][1], buildSide->texMat[0][2],
357                         buildSide->texMat[1][0], buildSide->texMat[1][1], buildSide->texMat[1][2],
358                         texture,
359                         // DEBUG: valid ? 0 : C_DETAIL
360                         0
361                         );
362                 // TODO write brush primitives format here
363         }
364         
365         /* end brush */
366         fprintf( f, "\t}\n" );
367         fprintf( f, "\t}\n\n" );
368 }
369
370 #if 0
371         /* iterate through the brush sides (ignore the first 6 bevel planes) */
372         for( i = 0; i < brush->numSides; i++ )
373         {
374                 /* get side */
375                 side = &bspBrushSides[ brush->firstSide + i ];
376                 
377                 /* get shader */
378                 if( side->shaderNum < 0 || side->shaderNum >= numBSPShaders )
379                         continue;
380                 shader = &bspShaders[ side->shaderNum ];
381                 if( !Q_stricmp( shader->shader, "default" ) || !Q_stricmp( shader->shader, "noshader" ) )
382                         continue;
383                 
384                 /* get texture name */
385                 if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
386                         texture = shader->shader + 9;
387                 else
388                         texture = shader->shader;
389                 
390                 /* get plane */
391                 plane = &bspPlanes[ side->planeNum ];
392
393                 /* make plane points */
394                 {
395                         vec3_t  vecs[ 2 ];
396
397                         
398                         MakeNormalVectors( plane->normal, vecs[ 0 ], vecs[ 1 ] );
399                         VectorMA( vec3_origin, plane->dist, plane->normal, pts[ 0 ] );
400                         VectorMA( pts[ 0 ], 256.0f, vecs[ 0 ], pts[ 1 ] );
401                         VectorMA( pts[ 0 ], 256.0f, vecs[ 1 ], pts[ 2 ] );
402                 }
403
404                 /* offset by origin */
405                 for( j = 0; j < 3; j++ )
406                         VectorAdd( pts[ j ], origin, pts[ j ] );
407                 
408                 /* print brush side */
409                 /* ( 640 24 -224 ) ( 448 24 -224 ) ( 448 -232 -224 ) common/caulk 0 48 0 0.500000 0.500000 0 0 0 */
410                 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",
411                         pts[ 0 ][ 0 ], pts[ 0 ][ 1 ], pts[ 0 ][ 2 ],
412                         pts[ 1 ][ 0 ], pts[ 1 ][ 1 ], pts[ 1 ][ 2 ],
413                         pts[ 2 ][ 0 ], pts[ 2 ][ 1 ], pts[ 2 ][ 2 ],
414                         texture );
415         }
416 #endif
417
418
419
420 /*
421 ConvertPatch()
422 converts a bsp patch to a map patch
423
424         {
425                 patchDef2
426                 {
427                         base_wall/concrete
428                         ( 9 3 0 0 0 )
429                         (
430                                 ( ( 168 168 -192 0 2 ) ( 168 168 -64 0 1 ) ( 168 168 64 0 0 ) ... )
431                                 ...
432                         )
433                 }
434         }
435
436 */
437
438 static void ConvertPatch( FILE *f, int num, bspDrawSurface_t *ds, vec3_t origin )
439 {
440         int                             x, y;
441         bspShader_t             *shader;
442         char                    *texture;
443         bspDrawVert_t   *dv;
444         vec3_t                  xyz;
445         
446         
447         /* only patches */
448         if( ds->surfaceType != MST_PATCH )
449                 return;
450         
451         /* get shader */
452         if( ds->shaderNum < 0 || ds->shaderNum >= numBSPShaders )
453                 return;
454         shader = &bspShaders[ ds->shaderNum ];
455         
456         /* get texture name */
457         if( !Q_strncasecmp( shader->shader, "textures/", 9 ) )
458                 texture = shader->shader + 9;
459         else
460                 texture = shader->shader;
461         
462         /* start patch */
463         fprintf( f, "\t// patch %d\n", num );
464         fprintf( f, "\t{\n" );
465         fprintf( f, "\t\tpatchDef2\n" );
466         fprintf( f, "\t\t{\n" );
467         fprintf( f, "\t\t\t%s\n", texture );
468         fprintf( f, "\t\t\t( %d %d 0 0 0 )\n", ds->patchWidth, ds->patchHeight );
469         fprintf( f, "\t\t\t(\n" );
470         
471         /* iterate through the verts */
472         for( x = 0; x < ds->patchWidth; x++ )
473         {
474                 /* start row */
475                 fprintf( f, "\t\t\t\t(" );
476                 
477                 /* iterate through the row */
478                 for( y = 0; y < ds->patchHeight; y++ )
479                 {
480                         /* get vert */
481                         dv = &bspDrawVerts[ ds->firstVert + (y * ds->patchWidth) + x ];
482                         
483                         /* offset it */
484                         VectorAdd( origin, dv->xyz, xyz );
485                         
486                         /* print vertex */
487                         fprintf( f, " ( %f %f %f %f %f )", xyz[ 0 ], xyz[ 1 ], xyz[ 2 ], dv->st[ 0 ], dv->st[ 1 ] );
488                 }
489                 
490                 /* end row */
491                 fprintf( f, " )\n" );
492         }
493         
494         /* end patch */
495         fprintf( f, "\t\t\t)\n" );
496         fprintf( f, "\t\t}\n" );
497         fprintf( f, "\t}\n\n" );
498 }
499
500
501
502 /*
503 ConvertModel()
504 exports a bsp model to a map file
505 */
506
507 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
508 {
509         int                                     i, num;
510         bspBrush_t                      *brush;
511         bspDrawSurface_t        *ds;
512         
513         
514         /* convert bsp planes to map planes */
515         nummapplanes = numBSPPlanes;
516         AUTOEXPAND_BY_REALLOC(mapplanes, nummapplanes, allocatedmapplanes, 1024);
517         for( i = 0; i < numBSPPlanes; i++ )
518         {
519                 VectorCopy( bspPlanes[ i ].normal, mapplanes[ i ].normal );
520                 mapplanes[ i ].dist = bspPlanes[ i ].dist;
521                 mapplanes[ i ].type = PlaneTypeForNormal( mapplanes[ i ].normal );
522                 mapplanes[ i ].hash_chain = 0;
523         }
524         
525         /* allocate a build brush */
526         buildBrush = AllocBrush( 512 );
527         buildBrush->entityNum = 0;
528         buildBrush->original = buildBrush;
529
530         if(origin[0] != 0 || origin[1] != 0 || origin[2] != 0)
531                 ConvertOriginBrush(f, -1, origin);
532         
533         /* go through each brush in the model */
534         for( i = 0; i < model->numBSPBrushes; i++ )
535         {
536                 num = i + model->firstBSPBrush;
537                 brush = &bspBrushes[ num ];
538                 ConvertBrush( f, num, brush, origin );
539         }
540         
541         /* free the build brush */
542         free( buildBrush );
543         
544         /* go through each drawsurf in the model */
545         for( i = 0; i < model->numBSPSurfaces; i++ )
546         {
547                 num = i + model->firstBSPSurface;
548                 ds = &bspDrawSurfaces[ num ];
549                 
550                 /* we only love patches */
551                 if( ds->surfaceType == MST_PATCH )
552                         ConvertPatch( f, num, ds, origin );
553         }
554 }
555
556
557
558 /*
559 ConvertEPairs()
560 exports entity key/value pairs to a map file
561 */
562
563 static void ConvertEPairs( FILE *f, entity_t *e, qboolean skip_origin )
564 {
565         epair_t *ep;
566         
567         
568         /* walk epairs */
569         for( ep = e->epairs; ep != NULL; ep = ep->next )
570         {
571                 /* ignore empty keys/values */
572                 if( ep->key[ 0 ] == '\0' || ep->value[ 0 ] == '\0' )
573                         continue;
574
575                 /* ignore model keys with * prefixed values */
576                 if( !Q_stricmp( ep->key, "model" ) && ep->value[ 0 ] == '*' )
577                         continue;
578                 
579                 /* ignore origin keys if skip_origin is set */
580                 if( skip_origin && !Q_stricmp( ep->key, "origin" ) )
581                         continue;
582                 
583                 /* emit the epair */
584                 fprintf( f, "\t\"%s\" \"%s\"\n", ep->key, ep->value );
585         }
586 }
587
588
589
590 /*
591 ConvertBSPToMap()
592 exports an quake map file from the bsp
593 */
594
595 int ConvertBSPToMap( char *bspName )
596 {
597         int                             i, modelNum;
598         FILE                    *f;
599         bspModel_t              *model;
600         entity_t                *e;
601         vec3_t                  origin;
602         const char              *value;
603         char                    name[ 1024 ], base[ 1024 ];
604         
605         
606         /* note it */
607         Sys_Printf( "--- Convert BSP to MAP ---\n" );
608         
609         /* create the bsp filename from the bsp name */
610         strcpy( name, bspName );
611         StripExtension( name );
612         strcat( name, "_converted.map" );
613         Sys_Printf( "writing %s\n", name );
614         
615         ExtractFileBase( bspName, base );
616         strcat( base, ".bsp" );
617         
618         /* open it */
619         f = fopen( name, "wb" );
620         if( f == NULL )
621                 Error( "Open failed on %s\n", name );
622         
623         /* print header */
624         fprintf( f, "// Generated by Q3Map2 (ydnar) -convert -format map\n" );
625         
626         /* walk entity list */
627         for( i = 0; i < numEntities; i++ )
628         {
629                 /* get entity */
630                 e = &entities[ i ];
631                 
632                 /* start entity */
633                 fprintf( f, "// entity %d\n", i );
634                 fprintf( f, "{\n" );
635                 
636                 /* get model num */
637                 if( i == 0 )
638                         modelNum = 0;
639                 else
640                 {
641                         value = ValueForKey( e, "model" );
642                         if( value[ 0 ] == '*' )
643                                 modelNum = atoi( value + 1 );
644                         else
645                                 modelNum = -1;
646                 }
647                 
648                 /* export keys */
649                 ConvertEPairs( f, e, modelNum >= 0 );
650                 fprintf( f, "\n" );
651                 
652                 /* only handle bsp models */
653                 if( modelNum >= 0 )
654                 {
655                         /* get model */
656                         model = &bspModels[ modelNum ];
657                         
658                         /* get entity origin */
659                         value = ValueForKey( e, "origin" );
660                         if( value[ 0 ] == '\0' )
661                                 VectorClear( origin );
662                         else
663                                 GetVectorForKey( e, "origin", origin );
664                         
665                         /* convert model */
666                         ConvertModel( f, model, modelNum, origin );
667                 }
668                 
669                 /* end entity */
670                 fprintf( f, "}\n\n" );
671         }
672         
673         /* close the file and return */
674         fclose( f );
675         
676         /* return to sender */
677         return 0;
678 }