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
28 #define MSG_PREFIX "bobToolz plugin: "
30 // these classes are far less of a mess than my code was,
31 // thanq to G.DeWan 4 the prtview source on which it was based
33 CBspPortal::CBspPortal(){
34 memset( this, 0, sizeof( CBspPortal ) );
37 CBspPortal::~CBspPortal(){
41 void ClampFloat( float* p ){
43 double frac = modf( *p, &i );
49 if ( fabs( *p - ceil( *p ) ) < MAX_ROUND_ERROR ) {
50 *p = static_cast<float>( ceil( *p ) );
53 if ( fabs( *p - floor( *p ) ) < MAX_ROUND_ERROR ) {
54 *p = static_cast<float>( floor( *p ) );
58 bool CBspPortal::Build( char *def, unsigned int pointCnt, bool bInverse ){
62 point_count = pointCnt;
64 if ( point_count < 3 ) {
68 point = new CBspPoint[point_count];
70 for ( n = 0; n < point_count; n++ )
72 for (; *c != 0 && *c != '('; c++ ) ;
82 x = point_count - n - 1;
88 sscanf( c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2] );
90 ClampFloat( &point[x].p[0] );
91 ClampFloat( &point[x].p[1] );
92 ClampFloat( &point[x].p[2] );
99 memset( this, 0, sizeof( CPortals ) );
102 CPortals::~CPortals(){
106 void CPortals::Purge(){
114 void CPortals::Load(){
115 char buf[LINE_BUF + 1];
117 memset( buf, 0, LINE_BUF + 1 );
121 globalOutputStream() << MSG_PREFIX "Loading portal file " << fn << ".\n";
125 in = fopen( fn, "rt" );
128 globalOutputStream() << " ERROR - could not open file.\n";
133 if ( !fgets( buf, LINE_BUF, in ) ) {
136 globalOutputStream() << " ERROR - File ended prematurely.\n";
141 if ( strncmp( "PRT1", buf, 4 ) != 0 ) {
144 globalOutputStream() << " ERROR - File header indicates wrong file type (should be \"PRT1\").\n";
149 if ( !fgets( buf, LINE_BUF, in ) ) {
152 globalOutputStream() << " ERROR - File ended prematurely.\n";
157 sscanf( buf, "%u", &node_count );
159 if ( node_count > 0xFFFF ) {
164 globalOutputStream() << " ERROR - Extreme number of nodes, aborting.\n";
169 if ( !fgets( buf, LINE_BUF, in ) ) {
174 globalOutputStream() << " ERROR - File ended prematurely.\n";
179 unsigned int p_count;
180 sscanf( buf, "%u", &p_count );
182 if ( !fgets( buf, LINE_BUF, in ) ) {
187 globalOutputStream() << " ERROR - File ended prematurely.\n";
192 unsigned int p_count2;
193 sscanf( buf, "%u", &p_count2 );
195 node = new CBspNode[node_count];
198 for ( i = 0; i < p_count; i++ )
200 if ( !fgets( buf, LINE_BUF, in ) ) {
205 globalOutputStream() << " ERROR - File ended prematurely.\n";
210 unsigned int dummy, node1, node2;
211 sscanf( buf, "%u %u %u", &dummy, &node1, &node2 );
213 node[node1].portal_count++;
214 node[node2].portal_count++;
217 for ( i = 0; i < p_count2; i++ )
219 if ( !fgets( buf, LINE_BUF, in ) ) {
224 globalOutputStream() << " ERROR - File ended prematurely.\n";
229 unsigned int dummy, node1;
230 sscanf( buf, "%u %u", &dummy, &node1 );
232 node[node1].portal_count++;
235 for ( i = 0; i < node_count; i++ )
236 node[i].portal = new CBspPortal[node[i].portal_count];
240 in = fopen( fn, "rt" );
242 fgets( buf, LINE_BUF, in );
243 fgets( buf, LINE_BUF, in );
244 fgets( buf, LINE_BUF, in );
245 fgets( buf, LINE_BUF, in );
248 for ( n = 0; n < p_count; n++ )
250 if ( !fgets( buf, LINE_BUF, in ) ) {
255 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of " << p_count << ".\n";
260 unsigned int pCount, node1, node2;
261 sscanf( buf, "%u %u %u", &pCount, &node1, &node2 );
263 if ( !node[node1].AddPortal( buf, pCount, false ) ) {
268 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count << " is not formatted correctly.\n";
273 if ( !node[node2].AddPortal( buf, pCount, true ) ) {
278 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count << " is not formatted correctly.\n";
284 for ( n = 0; n < p_count2; n++ )
286 if ( !fgets( buf, LINE_BUF, in ) ) {
291 globalOutputStream() << " ERROR - Could not find information for portal number " << n + 1 << " of " << p_count << ".\n";
296 unsigned int pCount, node1;
297 sscanf( buf, "%u %u", &pCount, &node1 );
299 if ( !node[node1].AddPortal( buf, pCount, false ) ) {
304 globalOutputStream() << " ERROR - Information for portal number " << n + 1 << " of " << p_count << " is not formatted correctly.\n";
313 CBspNode::CBspNode(){
319 CBspNode::~CBspNode(){
320 if ( portal != NULL ) {
325 bool CBspNode::AddPortal( char *def, unsigned int pointCnt, bool bInverse ){
326 return portal[portal_next++].Build( def, pointCnt, bInverse );