]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/leakfile.c
Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[xonotic/netradiant.git] / tools / quake3 / q3map2 / leakfile.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 LEAKFILE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42    ==============================================================================
43
44    LEAK FILE GENERATION
45
46    Save out name.line for qe3 to read
47    ==============================================================================
48  */
49
50
51 /*
52    =============
53    LeakFile
54
55    Finds the shortest possible chain of portals
56    that leads from the outside leaf to a specifically
57    occupied leaf
58
59    TTimo: builds a polyline xml node
60    =============
61  */
62 xmlNodePtr LeakFile( tree_t *tree ){
63         vec3_t mid;
64         FILE    *linefile;
65         char filename[1024];
66         node_t  *node;
67         int count;
68         xmlNodePtr xml_node, point;
69
70         if ( !tree->outside_node.occupied ) {
71                 return NULL;
72         }
73
74         Sys_FPrintf( SYS_VRB,"--- LeakFile ---\n" );
75
76         //
77         // write the points to the file
78         //
79         sprintf( filename, "%s.lin", source );
80         linefile = fopen( filename, "w" );
81         if ( !linefile ) {
82                 Error( "Couldn't open %s\n", filename );
83         }
84
85         xml_node = xmlNewNode( NULL, (xmlChar*)"polyline" );
86
87         count = 0;
88         node = &tree->outside_node;
89         while ( node->occupied > 1 )
90         {
91                 int next;
92                 portal_t    *p, *nextportal = NULL;
93                 node_t      *nextnode = NULL;
94                 int s;
95
96                 // find the best portal exit
97                 next = node->occupied;
98                 for ( p = node->portals ; p ; p = p->next[!s] )
99                 {
100                         s = ( p->nodes[0] == node );
101                         if ( p->nodes[s]->occupied
102                                  && p->nodes[s]->occupied < next ) {
103                                 nextportal = p;
104                                 nextnode = p->nodes[s];
105                                 next = nextnode->occupied;
106                         }
107                 }
108                 node = nextnode;
109                 WindingCenter( nextportal->winding, mid );
110                 fprintf( linefile, "%f %f %f\n", mid[0], mid[1], mid[2] );
111                 point = xml_NodeForVec( mid );
112                 xmlAddChild( xml_node, point );
113                 count++;
114         }
115         // add the occupant center
116         GetVectorForKey( node->occupant, "origin", mid );
117
118         fprintf( linefile, "%f %f %f\n", mid[0], mid[1], mid[2] );
119         point = xml_NodeForVec( mid );
120         xmlAddChild( xml_node, point );
121         Sys_FPrintf( SYS_VRB, "%9d point linefile\n", count + 1 );
122
123         fclose( linefile );
124
125         return xml_node;
126 }