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)
73 if (node->planenum != PLANENUM_LEAF) {
74 CountVisportals_r (node->children[0]);
75 CountVisportals_r (node->children[1]);
83 for (p = node->portals ; p ; p=p->next[s])
86 s = (p->nodes[1] == node);
87 if (w && p->nodes[0] == node)
89 if (!PortalPassable(p))
91 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
103 void WritePortalFile_r (node_t *node)
112 if (node->planenum != PLANENUM_LEAF) {
113 WritePortalFile_r (node->children[0]);
114 WritePortalFile_r (node->children[1]);
122 for (p = node->portals ; p ; p=p->next[s])
125 s = (p->nodes[1] == node);
126 if (w && p->nodes[0] == node)
128 if (!PortalPassable(p))
130 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
133 // write out to the file
135 // sometimes planes get turned around when they are very near
136 // the changeover point between different axis. interpret the
137 // plane the same way vis will, and flip the side orders if needed
138 // FIXME: is this still relevent?
139 WindingPlane (w, normal, &dist);
141 if ( DotProduct (p->plane.normal, normal) < 0.99 )
143 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[1]->cluster, p->nodes[0]->cluster);
146 fprintf (pf,"%i %i %i ",w->numpoints, p->nodes[0]->cluster, p->nodes[1]->cluster);
148 /* ydnar: added this change to make antiportals work */
149 if( p->compileFlags & C_HINT )
154 /* write the winding */
155 for (i=0 ; i<w->numpoints ; i++)
158 WriteFloat (pf, w->p[i][0]);
159 WriteFloat (pf, w->p[i][1]);
160 WriteFloat (pf, w->p[i][2]);
169 void CountSolidFaces_r (node_t *node)
176 if (node->planenum != PLANENUM_LEAF) {
177 CountSolidFaces_r (node->children[0]);
178 CountSolidFaces_r (node->children[1]);
186 for (p = node->portals ; p ; p=p->next[s])
189 s = (p->nodes[1] == node);
192 if (PortalPassable(p))
194 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
196 // write out to the file
208 void WriteFaceFile_r (node_t *node)
215 if (node->planenum != PLANENUM_LEAF) {
216 WriteFaceFile_r (node->children[0]);
217 WriteFaceFile_r (node->children[1]);
225 for (p = node->portals ; p ; p=p->next[s])
228 s = (p->nodes[1] == node);
231 if (PortalPassable(p))
233 if(p->nodes[0]->cluster == p->nodes[1]->cluster)
235 // write out to the file
237 if (p->nodes[0] == node)
239 fprintf (pf,"%i %i ",w->numpoints, p->nodes[0]->cluster);
240 for (i=0 ; i<w->numpoints ; i++)
243 WriteFloat (pf, w->p[i][0]);
244 WriteFloat (pf, w->p[i][1]);
245 WriteFloat (pf, w->p[i][2]);
252 fprintf (pf,"%i %i ",w->numpoints, p->nodes[1]->cluster);
253 for (i = w->numpoints-1; i >= 0; i--)
256 WriteFloat (pf, w->p[i][0]);
257 WriteFloat (pf, w->p[i][1]);
258 WriteFloat (pf, w->p[i][2]);
272 void NumberLeafs_r (node_t *node, int c)
277 if ( node->planenum != PLANENUM_LEAF ) {
281 if(node->has_structural_children)
285 Sys_FPrintf (SYS_ERR,"THIS CANNOT HAPPEN\n");
287 NumberLeafs_r (node->children[0], c);
288 NumberLeafs_r (node->children[1], c);
293 c = num_visclusters++;
294 NumberLeafs_r (node->children[0], c);
295 NumberLeafs_r (node->children[1], c);
302 if ( node->opaque ) {
303 // solid block, viewpoint never inside
309 c = num_visclusters++;
315 for (p = node->portals ; p ; )
317 if (p->nodes[0] == node) // only write out from first leaf
319 if (PortalPassable(p))
327 if (!PortalPassable(p))
341 void NumberClusters(tree_t *tree) {
346 Sys_FPrintf (SYS_VRB,"--- NumberClusters ---\n");
348 // set the cluster field in every leaf and count the total number of portals
349 NumberLeafs_r (tree->headnode, -1);
350 CountVisportals_r (tree->headnode);
351 CountSolidFaces_r (tree->headnode);
353 Sys_FPrintf( SYS_VRB, "%9d visclusters\n", num_visclusters );
354 Sys_FPrintf( SYS_VRB, "%9d visportals\n", num_visportals );
355 Sys_FPrintf( SYS_VRB, "%9d solidfaces\n", num_solidfaces );
363 void WritePortalFile (tree_t *tree)
367 Sys_FPrintf (SYS_VRB,"--- WritePortalFile ---\n");
370 sprintf (filename, "%s.prt", source);
371 Sys_Printf ("writing %s\n", filename);
372 pf = fopen (filename, "w");
374 Error ("Error opening %s", filename);
376 fprintf (pf, "%s\n", PORTALFILE);
377 fprintf (pf, "%i\n", num_visclusters);
378 fprintf (pf, "%i\n", num_visportals);
379 fprintf (pf, "%i\n", num_solidfaces);
381 WritePortalFile_r(tree->headnode);
382 WriteFaceFile_r(tree->headnode);