2 BobToolz plugin for GtkRadiant
3 Copyright (C) 2001 Gordon Biggans
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #define MSG_PREFIX "bobToolz plugin: "
27 // these classes are far less of a mess than my code was,
28 // thanq to G.DeWan 4 the prtview source on which it was based
30 CBspPortal::CBspPortal(){
31 memset( this, 0, sizeof( CBspPortal ) );
34 CBspPortal::~CBspPortal(){
38 void ClampFloat( float* p ){
40 double frac = modf( *p, &i );
46 if ( fabs( *p - ceil( *p ) ) < MAX_ROUND_ERROR ) {
50 if ( fabs( *p - floor( *p ) ) < MAX_ROUND_ERROR ) {
55 bool CBspPortal::Build( char *def, unsigned int pointCnt, bool bInverse ){
59 point_count = pointCnt;
61 if ( point_count < 3 ) {
65 point = new CBspPoint[point_count];
67 for ( n = 0; n < point_count; n++ )
69 for (; *c != 0 && *c != '('; c++ ) ;
79 x = point_count - n - 1;
85 sscanf( c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2] );
87 ClampFloat( &point[x].p[0] );
88 ClampFloat( &point[x].p[1] );
89 ClampFloat( &point[x].p[2] );
96 memset( this, 0, sizeof( CPortals ) );
99 CPortals::~CPortals(){
103 void CPortals::Purge(){
111 void CPortals::Load(){
112 char buf[LINE_BUF + 1];
114 memset( buf, 0, LINE_BUF + 1 );
118 Sys_Printf( MSG_PREFIX "Loading portal file %s.\n", fn );
122 in = fopen( fn, "rt" );
125 Sys_Printf( " ERROR - could not open file.\n" );
130 if ( !fgets( buf, LINE_BUF, in ) ) {
133 Sys_Printf( " ERROR - File ended prematurely.\n" );
138 if ( strncmp( "PRT1", buf, 4 ) != 0 ) {
141 Sys_Printf( " ERROR - File header indicates wrong file type (should be \"PRT1\").\n" );
146 if ( !fgets( buf, LINE_BUF, in ) ) {
149 Sys_Printf( " ERROR - File ended prematurely.\n" );
154 sscanf( buf, "%u", &node_count );
156 if ( node_count > 0xFFFF ) {
161 Sys_Printf( " ERROR - Extreme number of nodes, aborting.\n" );
166 if ( !fgets( buf, LINE_BUF, in ) ) {
171 Sys_Printf( " ERROR - File ended prematurely.\n" );
176 unsigned int p_count;
177 sscanf( buf, "%u", &p_count );
179 if ( !fgets( buf, LINE_BUF, in ) ) {
184 Sys_Printf( " ERROR - File ended prematurely.\n" );
189 unsigned int p_count2;
190 sscanf( buf, "%u", &p_count2 );
192 node = new CBspNode[node_count];
195 for ( i = 0; i < p_count; i++ )
197 if ( !fgets( buf, LINE_BUF, in ) ) {
202 Sys_Printf( " ERROR - File ended prematurely.\n" );
207 unsigned int dummy, node1, node2;
208 sscanf( buf, "%u %u %u", &dummy, &node1, &node2 );
210 node[node1].portal_count++;
211 node[node2].portal_count++;
214 for ( i = 0; i < p_count2; i++ )
216 if ( !fgets( buf, LINE_BUF, in ) ) {
221 Sys_Printf( " ERROR - File ended prematurely.\n" );
226 unsigned int dummy, node1;
227 sscanf( buf, "%u %u", &dummy, &node1 );
229 node[node1].portal_count++;
232 for ( i = 0; i < node_count; i++ )
233 node[i].portal = new CBspPortal[node[i].portal_count];
237 in = fopen( fn, "rt" );
239 fgets( buf, LINE_BUF, in );
240 fgets( buf, LINE_BUF, in );
241 fgets( buf, LINE_BUF, in );
242 fgets( buf, LINE_BUF, in );
245 for ( n = 0; n < p_count; n++ )
247 if ( !fgets( buf, LINE_BUF, in ) ) {
252 Sys_Printf( " ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count );
257 unsigned int pCount, node1, node2;
258 sscanf( buf, "%u %u %u", &pCount, &node1, &node2 );
260 if ( !node[node1].AddPortal( buf, pCount, FALSE ) ) {
265 Sys_Printf( " ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count );
270 if ( !node[node2].AddPortal( buf, pCount, TRUE ) ) {
275 Sys_Printf( " ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count );
281 for ( n = 0; n < p_count2; n++ )
283 if ( !fgets( buf, LINE_BUF, in ) ) {
288 Sys_Printf( " ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count );
293 unsigned int pCount, node1;
294 sscanf( buf, "%u %u", &pCount, &node1 );
296 if ( !node[node1].AddPortal( buf, pCount, FALSE ) ) {
301 Sys_Printf( " ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count );
310 CBspNode::CBspNode(){
316 CBspNode::~CBspNode(){
317 if ( portal != NULL ) {
322 bool CBspNode::AddPortal( char *def, unsigned int pointCnt, bool bInverse ){
323 return portal[portal_next++].Build( def, pointCnt, bInverse );