]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/prtfile.c
new options:
[divverent/netradiant.git] / tools / quake3 / q3map2 / prtfile.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 PRTFILE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 ==============================================================================
43
44 PORTAL FILE GENERATION
45
46 Save out name.prt for qvis to read
47 ==============================================================================
48 */
49
50
51 #define PORTALFILE      "PRT1"
52
53 FILE    *pf;
54 int             num_visclusters;                                // clusters the player can be in
55 int             num_visportals;
56 int             num_solidfaces;
57
58 void WriteFloat (FILE *f, vec_t v)
59 {
60         if ( fabs(v - Q_rint(v)) < 0.001 )
61                 fprintf (f,"%i ",(int)Q_rint(v));
62         else
63                 fprintf (f,"%f ",v);
64 }
65
66 void CountVisportals_r(node_t *node)
67 {
68         int                     i, s;   
69         portal_t        *p;
70         winding_t       *w;
71         vec3_t          normal;
72         vec_t           dist;
73
74         // decision node
75         if (node->planenum != PLANENUM_LEAF) {
76                 CountVisportals_r (node->children[0]);
77                 CountVisportals_r (node->children[1]);
78                 return;
79         }
80         
81         if (node->opaque) {
82                 return;
83         }
84
85         for (p = node->portals ; p ; p=p->next[s])
86         {
87                 w = p->winding;
88                 s = (p->nodes[1] == node);
89                 if (w && p->nodes[0] == node)
90                 {
91                         if (!PortalPassable(p))
92                                 continue;
93                         if(p->nodes[0]->cluster == p->nodes[1]->cluster)
94                                 continue;
95                         ++num_visportals;
96                 }
97         }
98 }
99
100 /*
101 =================
102 WritePortalFile_r
103 =================
104 */
105 void WritePortalFile_r (node_t *node)
106 {
107         int                     i, s;   
108         portal_t        *p;
109         winding_t       *w;
110         vec3_t          normal;
111         vec_t           dist;
112
113         // decision node
114         if (node->planenum != PLANENUM_LEAF) {
115                 WritePortalFile_r (node->children[0]);
116                 WritePortalFile_r (node->children[1]);
117                 return;
118         }
119         
120         if (node->opaque) {
121                 return;
122         }
123
124         for (p = node->portals ; p ; p=p->next[s])
125         {
126                 w = p->winding;
127                 s = (p->nodes[1] == node);
128                 if (w && p->nodes[0] == node)
129                 {
130                         if (!PortalPassable(p))
131                                 continue;
132                         if(p->nodes[0]->cluster == p->nodes[1]->cluster)
133                                 continue;
134                         --num_visportals;
135                         // write out to the file
136                         
137                         // sometimes planes get turned around when they are very near
138                         // the changeover point between different axis.  interpret the
139                         // plane the same way vis will, and flip the side orders if needed
140                         // FIXME: is this still relevent?
141                         WindingPlane (w, normal, &dist);
142
143                         if ( DotProduct (p->plane.normal, normal) < 0.99 )
144                         {       // backwards...
145                                 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
146                         }
147                         else
148                                 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
149                         
150                         /* ydnar: added this change to make antiportals work */
151                         if( p->compileFlags & C_HINT )
152                                 fprintf( pf, "1 " );
153                         else
154                                 fprintf( pf, "0 " );
155                         
156                         /* write the winding */
157                         for (i=0 ; i<w->numpoints ; i++)
158                         {
159                                 fprintf (pf,"(");
160                                 WriteFloat (pf, w->p[i][0]);
161                                 WriteFloat (pf, w->p[i][1]);
162                                 WriteFloat (pf, w->p[i][2]);
163                                 fprintf (pf,") ");
164                         }
165                         fprintf (pf,"\n");
166                 }
167         }
168
169 }
170
171 void CountSolidFaces_r (node_t *node)
172 {
173         int                     i, s;   
174         portal_t        *p;
175         winding_t       *w;
176
177         // decision node
178         if (node->planenum != PLANENUM_LEAF) {
179                 CountSolidFaces_r (node->children[0]);
180                 CountSolidFaces_r (node->children[1]);
181                 return;
182         }
183         
184         if (node->opaque) {
185                 return;
186         }
187
188         for (p = node->portals ; p ; p=p->next[s])
189         {
190                 w = p->winding;
191                 s = (p->nodes[1] == node);
192                 if (w)
193                 {
194                         if (PortalPassable(p))
195                                 continue;
196                         if(p->nodes[0]->cluster == p->nodes[1]->cluster)
197                                 continue;
198                         // write out to the file
199
200                         ++num_solidfaces;
201                 }
202         }
203 }
204
205 /*
206 =================
207 WriteFaceFile_r
208 =================
209 */
210 void WriteFaceFile_r (node_t *node)
211 {
212         int                     i, s;   
213         portal_t        *p;
214         winding_t       *w;
215
216         // decision node
217         if (node->planenum != PLANENUM_LEAF) {
218                 WriteFaceFile_r (node->children[0]);
219                 WriteFaceFile_r (node->children[1]);
220                 return;
221         }
222         
223         if (node->opaque) {
224                 return;
225         }
226
227         for (p = node->portals ; p ; p=p->next[s])
228         {
229                 w = p->winding;
230                 s = (p->nodes[1] == node);
231                 if (w)
232                 {
233                         if (PortalPassable(p))
234                                 continue;
235                         if(p->nodes[0]->cluster == p->nodes[1]->cluster)
236                                 continue;
237                         // write out to the file
238
239                         if (p->nodes[0] == node)
240                         {
241                                 fprintf (pf,"%i %i ",w->numpoints, p->nodes[0]->cluster);
242                                 for (i=0 ; i<w->numpoints ; i++)
243                                 {
244                                         fprintf (pf,"(");
245                                         WriteFloat (pf, w->p[i][0]);
246                                         WriteFloat (pf, w->p[i][1]);
247                                         WriteFloat (pf, w->p[i][2]);
248                                         fprintf (pf,") ");
249                                 }
250                                 fprintf (pf,"\n");
251                         }
252                         else
253                         {
254                                 fprintf (pf,"%i %i ",w->numpoints, p->nodes[1]->cluster);
255                                 for (i = w->numpoints-1; i >= 0; i--)
256                                 {
257                                         fprintf (pf,"(");
258                                         WriteFloat (pf, w->p[i][0]);
259                                         WriteFloat (pf, w->p[i][1]);
260                                         WriteFloat (pf, w->p[i][2]);
261                                         fprintf (pf,") ");
262                                 }
263                                 fprintf (pf,"\n");
264                         }
265                 }
266         }
267 }
268
269 /*
270 ================
271 NumberLeafs_r
272 ================
273 */
274 void NumberLeafs_r (node_t *node, int c)
275 {
276         portal_t        *p;
277
278         if ( node->planenum != PLANENUM_LEAF ) {
279                 // decision node
280                 node->cluster = -99;
281
282                 if(node->has_structural_children)
283                 {
284 #if 0
285                         if(c >= 0)
286                                 Sys_FPrintf (SYS_ERR,"THIS CANNOT HAPPEN\n");
287 #endif
288                         NumberLeafs_r (node->children[0], c);
289                         NumberLeafs_r (node->children[1], c);
290                 }
291                 else
292                 {
293                         if(c < 0)
294                                 c = num_visclusters++;
295                         NumberLeafs_r (node->children[0], c);
296                         NumberLeafs_r (node->children[1], c);
297                 }
298                 return;
299         }
300         
301         node->area = -1;
302
303         if ( node->opaque ) {
304                 // solid block, viewpoint never inside
305                 node->cluster = -1;
306                 return;
307         }
308
309         if(c < 0)
310                 c = num_visclusters++;
311         
312         node->cluster = c;
313
314 #if 0
315         // count the portals
316         for (p = node->portals ; p ; )
317         {
318                 if (p->nodes[0] == node)                // only write out from first leaf
319                 {
320                         if (PortalPassable(p))
321                                 num_visportals++;
322                         else
323                                 num_solidfaces++;
324                         p = p->next[0];
325                 }
326                 else
327                 {
328                         if (!PortalPassable(p))
329                                 num_solidfaces++;
330                         p = p->next[1];         
331                 }
332         }
333 #endif
334 }
335
336
337 /*
338 ================
339 NumberClusters
340 ================
341 */
342 void NumberClusters(tree_t *tree) {
343         num_visclusters = 0;
344         num_visportals = 0;
345         num_solidfaces = 0;
346
347         Sys_FPrintf (SYS_VRB,"--- NumberClusters ---\n");
348         
349         // set the cluster field in every leaf and count the total number of portals
350         NumberLeafs_r (tree->headnode, -1);
351         CountVisportals_r (tree->headnode);
352         CountSolidFaces_r (tree->headnode);
353
354         Sys_FPrintf( SYS_VRB, "%9d visclusters\n", num_visclusters );
355         Sys_FPrintf( SYS_VRB, "%9d visportals\n", num_visportals );
356         Sys_FPrintf( SYS_VRB, "%9d solidfaces\n", num_solidfaces );
357 }
358
359 /*
360 ================
361 WritePortalFile
362 ================
363 */
364 void WritePortalFile (tree_t *tree)
365 {
366         char    filename[1024];
367
368         Sys_FPrintf (SYS_VRB,"--- WritePortalFile ---\n");
369         
370         // write the file
371         sprintf (filename, "%s.prt", source);
372         Sys_Printf ("writing %s\n", filename);
373         pf = fopen (filename, "w");
374         if (!pf)
375                 Error ("Error opening %s", filename);
376                 
377         fprintf (pf, "%s\n", PORTALFILE);
378         fprintf (pf, "%i\n", num_visclusters);
379         fprintf (pf, "%i\n", num_visportals);
380         fprintf (pf, "%i\n", num_solidfaces);
381
382         WritePortalFile_r(tree->headnode);
383         WriteFaceFile_r(tree->headnode);
384
385         fclose (pf);
386 }
387