1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 ----------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
42 ==============================================================================
44 PORTAL FILE GENERATION
46 Save out name.prt for qvis to read
47 ==============================================================================
51 #define PORTALFILE "PRT1"
54 int num_visclusters; // clusters the player can be in
58 void WriteFloat (FILE *f, vec_t v)
60 if ( fabs(v - Q_rint(v)) < 0.001 )
61 fprintf (f,"%i ",(int)Q_rint(v));
66 void CountVisportals_r(node_t *node)
75 if (node->planenum != PLANENUM_LEAF) {
76 CountVisportals_r (node->children[0]);
77 CountVisportals_r (node->children[1]);
85 for (p = node->portals ; p ; p=p->next[s])
88 s = (p->nodes[1] == node);
89 if (w && p->nodes[0] == node)
91 if (!PortalPassable(p))
93 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
105 void WritePortalFile_r (node_t *node)
114 if (node->planenum != PLANENUM_LEAF) {
115 WritePortalFile_r (node->children[0]);
116 WritePortalFile_r (node->children[1]);
124 for (p = node->portals ; p ; p=p->next[s])
127 s = (p->nodes[1] == node);
128 if (w && p->nodes[0] == node)
130 if (!PortalPassable(p))
132 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
135 // write out to the file
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);
143 if ( DotProduct (p->plane.normal, normal) < 0.99 )
145 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
148 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
150 /* ydnar: added this change to make antiportals work */
151 if( p->compileFlags & C_HINT )
156 /* write the winding */
157 for (i=0 ; i<w->numpoints ; i++)
160 WriteFloat (pf, w->p[i][0]);
161 WriteFloat (pf, w->p[i][1]);
162 WriteFloat (pf, w->p[i][2]);
171 void CountSolidFaces_r (node_t *node)
178 if (node->planenum != PLANENUM_LEAF) {
179 CountSolidFaces_r (node->children[0]);
180 CountSolidFaces_r (node->children[1]);
188 for (p = node->portals ; p ; p=p->next[s])
191 s = (p->nodes[1] == node);
194 if (PortalPassable(p))
196 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
198 // write out to the file
210 void WriteFaceFile_r (node_t *node)
217 if (node->planenum != PLANENUM_LEAF) {
218 WriteFaceFile_r (node->children[0]);
219 WriteFaceFile_r (node->children[1]);
227 for (p = node->portals ; p ; p=p->next[s])
230 s = (p->nodes[1] == node);
233 if (PortalPassable(p))
235 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
237 // write out to the file
239 if (p->nodes[0] == node)
241 fprintf (pf,"%i %i ",w->numpoints, p->nodes[0]->cluster);
242 for (i=0 ; i<w->numpoints ; i++)
245 WriteFloat (pf, w->p[i][0]);
246 WriteFloat (pf, w->p[i][1]);
247 WriteFloat (pf, w->p[i][2]);
254 fprintf (pf,"%i %i ",w->numpoints, p->nodes[1]->cluster);
255 for (i = w->numpoints-1; i >= 0; i--)
258 WriteFloat (pf, w->p[i][0]);
259 WriteFloat (pf, w->p[i][1]);
260 WriteFloat (pf, w->p[i][2]);
274 void NumberLeafs_r (node_t *node, int c)
278 if ( node->planenum != PLANENUM_LEAF ) {
282 if(node->has_structural_children)
286 Sys_FPrintf (SYS_ERR,"THIS CANNOT HAPPEN\n");
288 NumberLeafs_r (node->children[0], c);
289 NumberLeafs_r (node->children[1], c);
294 c = num_visclusters++;
295 NumberLeafs_r (node->children[0], c);
296 NumberLeafs_r (node->children[1], c);
303 if ( node->opaque ) {
304 // solid block, viewpoint never inside
310 c = num_visclusters++;
316 for (p = node->portals ; p ; )
318 if (p->nodes[0] == node) // only write out from first leaf
320 if (PortalPassable(p))
328 if (!PortalPassable(p))
342 void NumberClusters(tree_t *tree) {
347 Sys_FPrintf (SYS_VRB,"--- NumberClusters ---\n");
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);
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 );
364 void WritePortalFile (tree_t *tree)
368 Sys_FPrintf (SYS_VRB,"--- WritePortalFile ---\n");
371 sprintf (filename, "%s.prt", source);
372 Sys_Printf ("writing %s\n", filename);
373 pf = fopen (filename, "w");
375 Error ("Error opening %s", filename);
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);
382 WritePortalFile_r(tree->headnode);
383 WriteFaceFile_r(tree->headnode);