]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/q2map/prtfile.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / tools / quake2 / q2map / prtfile.c
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 #include "qbsp.h"
23
24 /*
25    ==============================================================================
26
27    PORTAL FILE GENERATION
28
29    Save out name.prt for qvis to read
30    ==============================================================================
31  */
32
33
34 #define PORTALFILE  "PRT1"
35
36 FILE    *pf;
37 int num_visclusters;                    // clusters the player can be in
38 int num_visportals;
39
40 void WriteFloat( FILE *f, vec_t v ){
41         if ( fabs( v - Q_rint( v ) ) < 0.001 ) {
42                 fprintf( f,"%i ",(int)Q_rint( v ) );
43         }
44         else{
45                 fprintf( f,"%f ",v );
46         }
47 }
48
49 /*
50    =================
51    WritePortalFile_r
52    =================
53  */
54 void WritePortalFile_r( node_t *node ){
55         int i, s;
56         portal_t    *p;
57         winding_t   *w;
58         vec3_t normal;
59         vec_t dist;
60
61         // decision node
62         if ( node->planenum != PLANENUM_LEAF && !node->detail_seperator ) {
63                 WritePortalFile_r( node->children[0] );
64                 WritePortalFile_r( node->children[1] );
65                 return;
66         }
67
68         if ( node->contents & CONTENTS_SOLID ) {
69                 return;
70         }
71
72         for ( p = node->portals ; p ; p = p->next[s] )
73         {
74                 w = p->winding;
75                 s = ( p->nodes[1] == node );
76                 if ( w && p->nodes[0] == node ) {
77                         if ( !Portal_VisFlood( p ) ) {
78                                 continue;
79                         }
80                         // write out to the file
81
82                         // sometimes planes get turned around when they are very near
83                         // the changeover point between different axis.  interpret the
84                         // plane the same way vis will, and flip the side orders if needed
85                         // FIXME: is this still relevent?
86                         WindingPlane( w, normal, &dist );
87                         if ( DotProduct( p->plane.normal, normal ) < 0.99 ) { // backwards...
88                                 fprintf( pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster );
89                         }
90                         else{
91                                 fprintf( pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster );
92                         }
93                         for ( i = 0 ; i < w->numpoints ; i++ )
94                         {
95                                 fprintf( pf,"(" );
96                                 WriteFloat( pf, w->p[i][0] );
97                                 WriteFloat( pf, w->p[i][1] );
98                                 WriteFloat( pf, w->p[i][2] );
99                                 fprintf( pf,") " );
100                         }
101                         fprintf( pf,"\n" );
102                 }
103         }
104
105 }
106
107 /*
108    ================
109    FillLeafNumbers_r
110
111    All of the leafs under node will have the same cluster
112    ================
113  */
114 void FillLeafNumbers_r( node_t *node, int num ){
115         if ( node->planenum == PLANENUM_LEAF ) {
116                 if ( node->contents & CONTENTS_SOLID ) {
117                         node->cluster = -1;
118                 }
119                 else{
120                         node->cluster = num;
121                 }
122                 return;
123         }
124         node->cluster = num;
125         FillLeafNumbers_r( node->children[0], num );
126         FillLeafNumbers_r( node->children[1], num );
127 }
128
129 /*
130    ================
131    NumberLeafs_r
132    ================
133  */
134 void NumberLeafs_r( node_t *node ){
135         portal_t    *p;
136
137         if ( node->planenum != PLANENUM_LEAF && !node->detail_seperator ) { // decision node
138                 node->cluster = -99;
139                 NumberLeafs_r( node->children[0] );
140                 NumberLeafs_r( node->children[1] );
141                 return;
142         }
143
144         // either a leaf or a detail cluster
145
146         if ( node->contents & CONTENTS_SOLID ) { // solid block, viewpoint never inside
147                 node->cluster = -1;
148                 return;
149         }
150
151         FillLeafNumbers_r( node, num_visclusters );
152         num_visclusters++;
153
154         // count the portals
155         for ( p = node->portals ; p ; )
156         {
157                 if ( p->nodes[0] == node ) {      // only write out from first leaf
158                         if ( Portal_VisFlood( p ) ) {
159                                 num_visportals++;
160                         }
161                         p = p->next[0];
162                 }
163                 else{
164                         p = p->next[1];
165                 }
166         }
167
168 }
169
170
171 /*
172    ================
173    CreateVisPortals_r
174    ================
175  */
176 void CreateVisPortals_r( node_t *node ){
177         // stop as soon as we get to a detail_seperator, which
178         // means that everything below is in a single cluster
179         if ( node->planenum == PLANENUM_LEAF || node->detail_seperator ) {
180                 return;
181         }
182
183         MakeNodePortal( node );
184         SplitNodePortals( node );
185
186         CreateVisPortals_r( node->children[0] );
187         CreateVisPortals_r( node->children[1] );
188 }
189
190 /*
191    ================
192    FinishVisPortals_r
193    ================
194  */
195 void FinishVisPortals2_r( node_t *node ){
196         if ( node->planenum == PLANENUM_LEAF ) {
197                 return;
198         }
199
200         MakeNodePortal( node );
201         SplitNodePortals( node );
202
203         FinishVisPortals2_r( node->children[0] );
204         FinishVisPortals2_r( node->children[1] );
205 }
206
207 void FinishVisPortals_r( node_t *node ){
208         if ( node->planenum == PLANENUM_LEAF ) {
209                 return;
210         }
211
212         if ( node->detail_seperator ) {
213                 FinishVisPortals2_r( node );
214                 return;
215         }
216
217         FinishVisPortals_r( node->children[0] );
218         FinishVisPortals_r( node->children[1] );
219 }
220
221
222 int clusterleaf;
223 void SaveClusters_r( node_t *node ){
224         if ( node->planenum == PLANENUM_LEAF ) {
225                 dleafs[clusterleaf++].cluster = node->cluster;
226                 return;
227         }
228         SaveClusters_r( node->children[0] );
229         SaveClusters_r( node->children[1] );
230 }
231
232 /*
233    ================
234    WritePortalFile
235    ================
236  */
237 void WritePortalFile( tree_t *tree ){
238         char filename[1024];
239         node_t *headnode;
240
241         Sys_FPrintf( SYS_VRB, "--- WritePortalFile ---\n" );
242
243         headnode = tree->headnode;
244         num_visclusters = 0;
245         num_visportals = 0;
246
247         FreeTreePortals_r( headnode );
248
249         MakeHeadnodePortals( tree );
250
251         CreateVisPortals_r( headnode );
252
253 // set the cluster field in every leaf and count the total number of portals
254
255         NumberLeafs_r( headnode );
256
257 // write the file
258         sprintf( filename, "%s.prt", source );
259         Sys_Printf( "writing %s\n", filename );
260         pf = fopen( filename, "w" );
261         if ( !pf ) {
262                 Error( "Error opening %s", filename );
263         }
264
265         fprintf( pf, "%s\n", PORTALFILE );
266         fprintf( pf, "%i\n", num_visclusters );
267         fprintf( pf, "%i\n", num_visportals );
268
269         Sys_FPrintf( SYS_VRB, "%5i visclusters\n", num_visclusters );
270         Sys_FPrintf( SYS_VRB, "%5i visportals\n", num_visportals );
271
272         WritePortalFile_r( headnode );
273
274         fclose( pf );
275
276         // we need to store the clusters out now because ordering
277         // issues made us do this after writebsp...
278         clusterleaf = 1;
279         SaveClusters_r( headnode );
280 }