2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
BobToolz plugin for GtkRadiant
|
|
|
|
Copyright (C) 2001 Gordon Biggans
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "CPortals.h"
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
#define LINE_BUF 1000
|
|
|
|
#define MSG_PREFIX "bobToolz plugin: "
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// these classes are far less of a mess than my code was,
|
2007-11-04 03:51:54 +00:00
|
|
|
// thanq to G.DeWan 4 the prtview source on which it was based
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CBspPortal::CBspPortal(){
|
|
|
|
memset( this, 0, sizeof( CBspPortal ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CBspPortal::~CBspPortal(){
|
2007-11-04 03:51:54 +00:00
|
|
|
delete[] point;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void ClampFloat( float* p ){
|
2007-11-04 03:51:54 +00:00
|
|
|
double i;
|
2012-03-17 20:01:54 +00:00
|
|
|
double frac = modf( *p, &i );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !frac ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( *p - ceil( *p ) ) < MAX_ROUND_ERROR ) {
|
|
|
|
*p = ceilf( *p );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( *p - floor( *p ) ) < MAX_ROUND_ERROR ) {
|
|
|
|
*p = floorf( *p );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bool CBspPortal::Build( char *def, unsigned int pointCnt, bool bInverse ){
|
2007-11-04 03:51:54 +00:00
|
|
|
char *c = def;
|
|
|
|
unsigned int n;
|
|
|
|
|
|
|
|
point_count = pointCnt;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( point_count < 3 ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return FALSE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
point = new CBspPoint[point_count];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( n = 0; n < point_count; n++ )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for (; *c != 0 && *c != '('; c++ ) ;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *c == 0 ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
return FALSE;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
c++;
|
|
|
|
|
|
|
|
int x;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( bInverse ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
x = point_count - n - 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:51:54 +00:00
|
|
|
x = n;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( c, "%f %f %f", &point[x].p[0], &point[x].p[1], &point[x].p[2] );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ClampFloat( &point[x].p[0] );
|
|
|
|
ClampFloat( &point[x].p[1] );
|
|
|
|
ClampFloat( &point[x].p[2] );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CPortals::CPortals(){
|
|
|
|
memset( this, 0, sizeof( CPortals ) );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CPortals::~CPortals(){
|
2007-11-04 03:51:54 +00:00
|
|
|
Purge();
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CPortals::Purge(){
|
|
|
|
if ( node ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
delete[] node;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
node = NULL;
|
|
|
|
node_count = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CPortals::Load(){
|
|
|
|
char buf[LINE_BUF + 1];
|
|
|
|
|
|
|
|
memset( buf, 0, LINE_BUF + 1 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
Purge();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( MSG_PREFIX "Loading portal file %s.\n", fn );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
FILE *in;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
in = fopen( fn, "rt" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( in == NULL ) {
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - could not open file.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File ended prematurely.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( strncmp( "PRT1", buf, 4 ) != 0 ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File header indicates wrong file type (should be \"PRT1\").\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File ended prematurely.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u", &node_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( node_count > 0xFFFF ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node_count = 0;
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - Extreme number of nodes, aborting.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node_count = 0;
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File ended prematurely.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int p_count;
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u", &p_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node_count = 0;
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File ended prematurely.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int p_count2;
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u", &p_count2 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node = new CBspNode[node_count];
|
|
|
|
|
|
|
|
unsigned int i;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < p_count; i++ )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node_count = 0;
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File ended prematurely.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int dummy, node1, node2;
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u %u %u", &dummy, &node1, &node2 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node[node1].portal_count++;
|
|
|
|
node[node2].portal_count++;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < p_count2; i++ )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node_count = 0;
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - File ended prematurely.\n" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int dummy, node1;
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u %u", &dummy, &node1 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
node[node1].portal_count++;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < node_count; i++ )
|
2007-11-04 03:51:54 +00:00
|
|
|
node[i].portal = new CBspPortal[node[i].portal_count];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
in = fopen( fn, "rt" );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fgets( buf, LINE_BUF, in );
|
|
|
|
fgets( buf, LINE_BUF, in );
|
|
|
|
fgets( buf, LINE_BUF, in );
|
|
|
|
fgets( buf, LINE_BUF, in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
unsigned int n;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( n = 0; n < p_count; n++ )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
Purge();
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int pCount, node1, node2;
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u %u %u", &pCount, &node1, &node2 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !node[node1].AddPortal( buf, pCount, FALSE ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
Purge();
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !node[node2].AddPortal( buf, pCount, TRUE ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
Purge();
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( n = 0; n < p_count2; n++ )
|
2007-11-04 03:51:54 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !fgets( buf, LINE_BUF, in ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
Purge();
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - Could not find information for portal number %d of %d.\n", n + 1, p_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int pCount, node1;
|
2012-03-17 20:01:54 +00:00
|
|
|
sscanf( buf, "%u %u", &pCount, &node1 );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !node[node1].AddPortal( buf, pCount, FALSE ) ) {
|
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
Purge();
|
|
|
|
|
2016-05-16 19:20:20 +00:00
|
|
|
Sys_FPrintf( SYS_ERR, "ERROR - Information for portal number %d of %d is not formatted correctly.\n", n + 1, p_count );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( in );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CBspNode::CBspNode(){
|
2007-11-04 03:51:54 +00:00
|
|
|
portal = NULL;
|
|
|
|
portal_count = 0;
|
|
|
|
portal_next = 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CBspNode::~CBspNode(){
|
|
|
|
if ( portal != NULL ) {
|
2007-11-04 03:51:54 +00:00
|
|
|
delete[] portal;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bool CBspNode::AddPortal( char *def, unsigned int pointCnt, bool bInverse ){
|
|
|
|
return portal[portal_next++].Build( def, pointCnt, bInverse );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|