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