]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/map/write.cpp
Backing out r347 and r345. Keeping r346.
[xonotic/netradiant.git] / plugins / map / write.cpp
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 //
23 // writes quake3 map format from internal objects
24 //
25
26 static int g_count_entities;
27 static int g_count_brushes;
28
29 #include "plugin.h"
30 extern int g_MapVersion;
31
32 void Float_Write(float data, IDataStream *out)
33 {
34   if (data == (int)data)
35     out->printf("%i ", (int)data);
36   else
37     out->printf("%f ", data);
38 }
39
40 void Patch_Write(patchMesh_t *pPatch, IDataStream *out)
41 {
42   int i, j;
43   const char *str;
44
45   // write shader name and matrix dimensions
46   str = pPatch->pShader->getName();
47   if (strchr(str, ' '))
48   {
49     Sys_FPrintf(SYS_WRN, "WARNING: Patch_Write: shader names with spaces are not allowed, ignoring '%s'\n", str);
50     str = SHADER_NOT_FOUND;
51   }
52   if(!strncmp(str, "textures/", 9)) str+=9;
53   out->printf("patchDef2\n{\n%s\n( %i %i 0 0 0 )\n",
54     str, pPatch->width, pPatch->height);
55
56   // write matrix
57   out->printf("(\n");
58   for(i=0; i<pPatch->width; i++)
59   {
60     out->printf("( ");
61     for(j=0; j<pPatch->height; j++)
62     {
63       out->printf("( ");
64
65       Float_Write(pPatch->ctrl[i][j].xyz[0], out);
66       Float_Write(pPatch->ctrl[i][j].xyz[1], out);
67       Float_Write(pPatch->ctrl[i][j].xyz[2], out);
68       Float_Write(pPatch->ctrl[i][j].st[0], out);
69       Float_Write(pPatch->ctrl[i][j].st[1], out);
70
71       out->printf(") ");
72     }
73     out->printf(")\n");
74   }
75   out->printf(")\n}\n");
76 }
77
78 void Face_Write (face_t *face, IDataStream *out, bool bAlternateTexdef = false)
79 {
80   int i, j;
81   const char *str;
82
83   // write planepts
84   for(i=0; i<3; i++)
85   {
86     out->printf("( ");
87     for(j=0; j<3; j++)
88     {
89       Float_Write(face->planepts[i][j], out);
90     }
91     out->printf(") ");
92   }
93
94   if(bAlternateTexdef)
95   {
96     // write alternate texdef
97     out->printf("( ( ");
98     for (i=0;i<3;i++)
99       Float_Write(face->brushprimit_texdef.coords[0][i], out);
100     out->printf(") ( ");
101     for (i=0;i<3;i++)
102       Float_Write(face->brushprimit_texdef.coords[1][i], out);
103     out->printf(") ) ");
104   }
105
106   // write shader name
107   str = face->texdef.GetName();
108   if (strchr(str, ' '))
109   {
110     Sys_FPrintf(SYS_WRN, "WARNING: Face_Write: shader names with spaces are not allowed, ignoring '%s'\n", str);
111     str = SHADER_NOT_FOUND;
112   }
113   if(!strncmp(str, "textures/", 9)) str+=9;
114
115   // Strip all remaining paths.
116   // FIXME: Hydra - this is actually a HalfLife specific bit, not Q2 map format specific.
117   if (g_MapVersion == MAPVERSION_HL)
118   {
119     char *pos;
120         while ( (pos = (char*)strchr( str, '/' )) != NULL ) {
121                 str = pos+1; // to speed optimize, change the "while" to an "if"
122         }
123   }
124   out->printf("%s ", str);
125
126   if(!bAlternateTexdef)
127   {
128     // write texdef
129     out->printf("%i %i %i %f %f ",
130       (int)face->texdef.shift[0],
131       (int)face->texdef.shift[1],
132       (int)face->texdef.rotate,
133       face->texdef.scale[0],
134       face->texdef.scale[1]);
135   }
136
137   if (g_MapVersion == MAPVERSION_Q3)
138   {
139   // write surface flags
140   out->printf("%i %i %i\n",
141     face->texdef.contents,
142     face->texdef.flags,
143     face->texdef.value);
144   }
145
146   if ( (g_MapVersion == MAPVERSION_HL) || (g_MapVersion == MAPVERSION_Q2) )
147   {
148     // write surface flags if non-zero values.
149     if (face->texdef.contents || face->texdef.flags || face->texdef.value)
150     {
151       out->printf("%i %i %i\n",
152         face->texdef.contents,
153         face->texdef.flags,
154         face->texdef.value);
155     }
156     else
157     {
158       out->printf("\n");
159     }
160   }
161
162 }
163
164 void Primitive_Write (brush_t *pBrush, IDataStream *out)
165 {
166   if ( (g_MapVersion == MAPVERSION_Q2) && (pBrush->patchBrush) )
167   {
168     Sys_FPrintf(SYS_WRN, "WARNING: Primitive_Write: Patches are not supported in Quake2, ignoring Brush %d\n", g_count_brushes++);
169   }
170   else
171   {
172     out->printf("// brush %i\n", g_count_brushes++);
173     out->printf("{\n");
174     if(pBrush->patchBrush)
175       Patch_Write(pBrush->pPatch, out);
176     else if(pBrush->bBrushDef)
177     {
178       out->printf("brushDef\n{\n");
179       for(face_t *face = pBrush->brush_faces; face != NULL; face = face->next)
180         Face_Write (face, out, true);
181       out->printf("}\n");
182     }
183     else
184       for(face_t *face = pBrush->brush_faces; face != NULL; face = face->next)
185         Face_Write (face, out);
186     out->printf("}\n");
187   }
188 }
189
190 void Entity_Write(entity_t *pEntity, IDataStream *out)
191 {
192   epair_t *pEpair;
193   CPtrArray *brushes = (CPtrArray*)pEntity->pData;
194   out->printf("// entity %i\n", g_count_entities++);
195   out->printf("{\n");
196   for(pEpair = pEntity->epairs; pEpair != NULL; pEpair = pEpair->next)
197     out->printf("\"%s\" \"%s\"\n", pEpair->key, pEpair->value);
198   g_count_brushes = 0;
199   for(int i=0; i<brushes->GetSize(); i++)
200     Primitive_Write((brush_t*)brushes->GetAt(i), out);
201   out->printf("}\n");
202 }
203
204 void Map_Write (CPtrArray *map, IDataStream *out)
205 {
206   g_count_entities = 0;
207   for(int i=0; i<map->GetSize(); i++)
208     Entity_Write((entity_t*)map->GetAt(i), out);
209 }
210
211 void Map_WriteQ3 (CPtrArray *map, IDataStream *out)
212 {
213   g_MapVersion = MAPVERSION_Q3;
214   Map_Write (map,out);
215 }
216
217 void Map_WriteHL (CPtrArray *map, IDataStream *out)
218 {
219   g_MapVersion = MAPVERSION_HL;
220   Map_Write (map,out);
221 }
222
223 void Map_WriteQ2 (CPtrArray *map, IDataStream *out)
224 {
225   g_MapVersion = MAPVERSION_Q2;
226   Map_Write (map,out);
227 }