doom3-bfg/neo/aas/AASFile.cpp

1582 lines
34 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#pragma hdrstop
#include "../idlib/precompiled.h"
#include "AASFile.h"
#include "AASFile_local.h"
/*
===============================================================================
idReachability
===============================================================================
*/
/*
================
Reachability_Write
================
*/
bool Reachability_Write( idFile* fp, idReachability* reach )
{
2012-11-26 18:58:24 +00:00
fp->WriteFloatString( "\t\t%d %d (%f %f %f) (%f %f %f) %d %d",
( int ) reach->travelType, ( int ) reach->toAreaNum, reach->start.x, reach->start.y, reach->start.z,
reach->end.x, reach->end.y, reach->end.z, reach->edgeNum, ( int ) reach->travelTime );
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
Reachability_Read
================
*/
bool Reachability_Read( idLexer& src, idReachability* reach )
{
2012-11-26 18:58:24 +00:00
reach->travelType = src.ParseInt();
reach->toAreaNum = src.ParseInt();
src.Parse1DMatrix( 3, reach->start.ToFloatPtr() );
src.Parse1DMatrix( 3, reach->end.ToFloatPtr() );
reach->edgeNum = src.ParseInt();
reach->travelTime = src.ParseInt();
return true;
}
/*
================
idReachability::CopyBase
================
*/
void idReachability::CopyBase( idReachability& reach )
{
2012-11-26 18:58:24 +00:00
travelType = reach.travelType;
toAreaNum = reach.toAreaNum;
start = reach.start;
end = reach.end;
edgeNum = reach.edgeNum;
travelTime = reach.travelTime;
}
/*
===============================================================================
idReachability_Special
===============================================================================
*/
/*
================
Reachability_Special_Write
================
*/
bool Reachability_Special_Write( idFile* fp, idReachability_Special* reach )
{
2012-11-26 18:58:24 +00:00
int i;
const idKeyValue* keyValue;
2012-11-26 18:58:24 +00:00
fp->WriteFloatString( "\n\t\t{\n" );
for( i = 0; i < reach->dict.GetNumKeyVals(); i++ )
{
2012-11-26 18:58:24 +00:00
keyValue = reach->dict.GetKeyVal( i );
fp->WriteFloatString( "\t\t\t\"%s\" \"%s\"\n", keyValue->GetKey().c_str(), keyValue->GetValue().c_str() );
}
fp->WriteFloatString( "\t\t}\n" );
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
Reachability_Special_Read
================
*/
bool Reachability_Special_Read( idLexer& src, idReachability_Special* reach )
{
2012-11-26 18:58:24 +00:00
idToken key, value;
2012-11-26 18:58:24 +00:00
src.ExpectTokenString( "{" );
while( src.ReadToken( &key ) )
{
if( key == "}" )
{
2012-11-26 18:58:24 +00:00
return true;
}
src.ExpectTokenType( TT_STRING, 0, &value );
reach->dict.Set( key, value );
}
return false;
}
/*
===============================================================================
idAASSettings
===============================================================================
*/
/*
============
idAASSettings::idAASSettings
============
*/
idAASSettings::idAASSettings()
{
2012-11-26 18:58:24 +00:00
numBoundingBoxes = 1;
boundingBoxes[0] = idBounds( idVec3( -16, -16, 0 ), idVec3( 16, 16, 72 ) );
usePatches = false;
writeBrushMap = false;
playerFlood = false;
noOptimize = false;
allowSwimReachabilities = false;
allowFlyReachabilities = false;
fileExtension = "aas48";
// physics settings
gravity = idVec3( 0, 0, -1066 );
gravityDir = gravity;
gravityValue = gravityDir.Normalize();
invGravityDir = -gravityDir;
maxStepHeight = 14.0f;
maxBarrierHeight = 32.0f;
maxWaterJumpHeight = 20.0f;
maxFallHeight = 64.0f;
minFloorCos = 0.7f;
// fixed travel times
tt_barrierJump = 100;
tt_startCrouching = 100;
tt_waterJump = 100;
tt_startWalkOffLedge = 100;
}
/*
============
idAASSettings::ParseBool
============
*/
bool idAASSettings::ParseBool( idLexer& src, bool& b )
{
if( !src.ExpectTokenString( "=" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
b = src.ParseBool();
return true;
}
/*
============
idAASSettings::ParseInt
============
*/
bool idAASSettings::ParseInt( idLexer& src, int& i )
{
if( !src.ExpectTokenString( "=" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
i = src.ParseInt();
return true;
}
/*
============
idAASSettings::ParseFloat
============
*/
bool idAASSettings::ParseFloat( idLexer& src, float& f )
{
if( !src.ExpectTokenString( "=" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
f = src.ParseFloat();
return true;
}
/*
============
idAASSettings::ParseVector
============
*/
bool idAASSettings::ParseVector( idLexer& src, idVec3& vec )
{
if( !src.ExpectTokenString( "=" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return ( src.Parse1DMatrix( 3, vec.ToFloatPtr() ) != 0 );
}
/*
============
idAASSettings::ParseBBoxes
============
*/
bool idAASSettings::ParseBBoxes( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
idBounds bounds;
2012-11-26 18:58:24 +00:00
numBoundingBoxes = 0;
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
while( src.ReadToken( &token ) )
{
if( token == "}" )
{
2012-11-26 18:58:24 +00:00
return true;
}
src.UnreadToken( &token );
src.Parse1DMatrix( 3, bounds[0].ToFloatPtr() );
if( !src.ExpectTokenString( "-" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
src.Parse1DMatrix( 3, bounds[1].ToFloatPtr() );
2012-11-26 18:58:24 +00:00
boundingBoxes[numBoundingBoxes++] = bounds;
}
return false;
}
/*
============
idAASSettings::FromParser
============
*/
bool idAASSettings::FromParser( idLexer& src )
{
2012-11-26 18:58:24 +00:00
idToken token;
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
// parse the file
while( 1 )
{
if( !src.ReadToken( &token ) )
{
2012-11-26 18:58:24 +00:00
break;
}
if( token == "}" )
{
2012-11-26 18:58:24 +00:00
break;
}
if( token == "bboxes" )
{
if( !ParseBBoxes( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "usePatches" )
{
if( !ParseBool( src, usePatches ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "writeBrushMap" )
{
if( !ParseBool( src, writeBrushMap ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "playerFlood" )
{
if( !ParseBool( src, playerFlood ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "allowSwimReachabilities" )
{
if( !ParseBool( src, allowSwimReachabilities ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "allowFlyReachabilities" )
{
if( !ParseBool( src, allowFlyReachabilities ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "fileExtension" )
{
2012-11-26 18:58:24 +00:00
src.ExpectTokenString( "=" );
src.ExpectTokenType( TT_STRING, 0, &token );
fileExtension = token;
}
else if( token == "gravity" )
{
2012-11-26 18:58:24 +00:00
ParseVector( src, gravity );
gravityDir = gravity;
gravityValue = gravityDir.Normalize();
invGravityDir = -gravityDir;
}
else if( token == "maxStepHeight" )
{
if( !ParseFloat( src, maxStepHeight ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "maxBarrierHeight" )
{
if( !ParseFloat( src, maxBarrierHeight ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "maxWaterJumpHeight" )
{
if( !ParseFloat( src, maxWaterJumpHeight ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "maxFallHeight" )
{
if( !ParseFloat( src, maxFallHeight ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "minFloorCos" )
{
if( !ParseFloat( src, minFloorCos ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "tt_barrierJump" )
{
if( !ParseInt( src, tt_barrierJump ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "tt_startCrouching" )
{
if( !ParseInt( src, tt_startCrouching ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "tt_waterJump" )
{
if( !ParseInt( src, tt_waterJump ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "tt_startWalkOffLedge" )
{
if( !ParseInt( src, tt_startWalkOffLedge ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "invalid token '%s'", token.c_str() );
}
}
if( numBoundingBoxes <= 0 )
{
2012-11-26 18:58:24 +00:00
src.Error( "no valid bounding box" );
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idAASSettings::FromFile
============
*/
bool idAASSettings::FromFile( const idStr& fileName )
{
2012-11-26 18:58:24 +00:00
idLexer src( LEXFL_ALLOWPATHNAMES | LEXFL_NOSTRINGESCAPECHARS | LEXFL_NOSTRINGCONCAT );
idStr name;
2012-11-26 18:58:24 +00:00
name = fileName;
2012-11-26 18:58:24 +00:00
common->Printf( "loading %s\n", name.c_str() );
if( !src.LoadFile( name ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "WARNING: couldn't load %s\n", name.c_str() );
return false;
}
if( !src.ExpectTokenString( "settings" ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "%s is not a settings file", name.c_str() );
return false;
}
if( !FromParser( src ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "failed to parse %s", name.c_str() );
return false;
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idAASSettings::FromDict
============
*/
bool idAASSettings::FromDict( const char* name, const idDict* dict )
{
2012-11-26 18:58:24 +00:00
idBounds bounds;
if( !dict->GetVector( "mins", "0 0 0", bounds[ 0 ] ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'mins' in entityDef '%s'", name );
}
if( !dict->GetVector( "maxs", "0 0 0", bounds[ 1 ] ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'maxs' in entityDef '%s'", name );
}
2012-11-26 18:58:24 +00:00
numBoundingBoxes = 1;
boundingBoxes[0] = bounds;
if( !dict->GetBool( "usePatches", "0", usePatches ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'usePatches' in entityDef '%s'", name );
}
if( !dict->GetBool( "writeBrushMap", "0", writeBrushMap ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'writeBrushMap' in entityDef '%s'", name );
}
if( !dict->GetBool( "playerFlood", "0", playerFlood ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'playerFlood' in entityDef '%s'", name );
}
if( !dict->GetBool( "allowSwimReachabilities", "0", allowSwimReachabilities ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'allowSwimReachabilities' in entityDef '%s'", name );
}
if( !dict->GetBool( "allowFlyReachabilities", "0", allowFlyReachabilities ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'allowFlyReachabilities' in entityDef '%s'", name );
}
if( !dict->GetString( "fileExtension", "", fileExtension ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'fileExtension' in entityDef '%s'", name );
}
if( !dict->GetVector( "gravity", "0 0 -1066", gravity ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'gravity' in entityDef '%s'", name );
}
gravityDir = gravity;
gravityValue = gravityDir.Normalize();
invGravityDir = -gravityDir;
if( !dict->GetFloat( "maxStepHeight", "0", maxStepHeight ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'maxStepHeight' in entityDef '%s'", name );
}
if( !dict->GetFloat( "maxBarrierHeight", "0", maxBarrierHeight ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'maxBarrierHeight' in entityDef '%s'", name );
}
if( !dict->GetFloat( "maxWaterJumpHeight", "0", maxWaterJumpHeight ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'maxWaterJumpHeight' in entityDef '%s'", name );
}
if( !dict->GetFloat( "maxFallHeight", "0", maxFallHeight ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'maxFallHeight' in entityDef '%s'", name );
}
if( !dict->GetFloat( "minFloorCos", "0", minFloorCos ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'minFloorCos' in entityDef '%s'", name );
}
if( !dict->GetInt( "tt_barrierJump", "0", tt_barrierJump ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'tt_barrierJump' in entityDef '%s'", name );
}
if( !dict->GetInt( "tt_startCrouching", "0", tt_startCrouching ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'tt_startCrouching' in entityDef '%s'", name );
}
if( !dict->GetInt( "tt_waterJump", "0", tt_waterJump ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'tt_waterJump' in entityDef '%s'", name );
}
if( !dict->GetInt( "tt_startWalkOffLedge", "0", tt_startWalkOffLedge ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "Missing 'tt_startWalkOffLedge' in entityDef '%s'", name );
}
2012-11-26 18:58:24 +00:00
return true;
}
/*
============
idAASSettings::WriteToFile
============
*/
bool idAASSettings::WriteToFile( idFile* fp ) const
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
fp->WriteFloatString( "{\n" );
fp->WriteFloatString( "\tbboxes\n\t{\n" );
for( i = 0; i < numBoundingBoxes; i++ )
{
2012-11-26 18:58:24 +00:00
fp->WriteFloatString( "\t\t(%f %f %f)-(%f %f %f)\n", boundingBoxes[i][0].x, boundingBoxes[i][0].y,
boundingBoxes[i][0].z, boundingBoxes[i][1].x, boundingBoxes[i][1].y, boundingBoxes[i][1].z );
2012-11-26 18:58:24 +00:00
}
fp->WriteFloatString( "\t}\n" );
fp->WriteFloatString( "\tusePatches = %d\n", usePatches );
fp->WriteFloatString( "\twriteBrushMap = %d\n", writeBrushMap );
fp->WriteFloatString( "\tplayerFlood = %d\n", playerFlood );
fp->WriteFloatString( "\tallowSwimReachabilities = %d\n", allowSwimReachabilities );
fp->WriteFloatString( "\tallowFlyReachabilities = %d\n", allowFlyReachabilities );
fp->WriteFloatString( "\tfileExtension = \"%s\"\n", fileExtension.c_str() );
fp->WriteFloatString( "\tgravity = (%f %f %f)\n", gravity.x, gravity.y, gravity.z );
fp->WriteFloatString( "\tmaxStepHeight = %f\n", maxStepHeight );
fp->WriteFloatString( "\tmaxBarrierHeight = %f\n", maxBarrierHeight );
fp->WriteFloatString( "\tmaxWaterJumpHeight = %f\n", maxWaterJumpHeight );
fp->WriteFloatString( "\tmaxFallHeight = %f\n", maxFallHeight );
fp->WriteFloatString( "\tminFloorCos = %f\n", minFloorCos );
fp->WriteFloatString( "\ttt_barrierJump = %d\n", tt_barrierJump );
fp->WriteFloatString( "\ttt_startCrouching = %d\n", tt_startCrouching );
fp->WriteFloatString( "\ttt_waterJump = %d\n", tt_waterJump );
fp->WriteFloatString( "\ttt_startWalkOffLedge = %d\n", tt_startWalkOffLedge );
fp->WriteFloatString( "}\n" );
return true;
}
/*
============
idAASSettings::ValidForBounds
============
*/
bool idAASSettings::ValidForBounds( const idBounds& bounds ) const
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < 3; i++ )
{
if( bounds[0][i] < boundingBoxes[0][0][i] )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( bounds[1][i] > boundingBoxes[0][1][i] )
{
2012-11-26 18:58:24 +00:00
return false;
}
}
return true;
}
/*
============
idAASSettings::ValidEntity
============
*/
bool idAASSettings::ValidEntity( const char* classname ) const
{
2012-11-26 18:58:24 +00:00
idStr use_aas;
idVec3 size;
idBounds bounds;
if( playerFlood )
{
if( !strcmp( classname, "info_player_start" ) || !strcmp( classname , "info_player_deathmatch" ) || !strcmp( classname, "func_teleporter" ) )
{
2012-11-26 18:58:24 +00:00
return true;
}
}
const idDeclEntityDef* decl = static_cast<const idDeclEntityDef*>( declManager->FindType( DECL_ENTITYDEF, classname, false ) );
if( ( decl != NULL ) && decl->dict.GetString( "use_aas", NULL, use_aas ) && !fileExtension.Icmp( use_aas ) )
{
if( decl->dict.GetVector( "mins", NULL, bounds[0] ) )
{
2012-11-26 18:58:24 +00:00
decl->dict.GetVector( "maxs", NULL, bounds[1] );
}
else if( decl->dict.GetVector( "size", NULL, size ) )
{
2012-11-26 18:58:24 +00:00
bounds[ 0 ].Set( size.x * -0.5f, size.y * -0.5f, 0.0f );
bounds[ 1 ].Set( size.x * 0.5f, size.y * 0.5f, size.z );
}
if( !ValidForBounds( bounds ) )
{
2012-11-26 18:58:24 +00:00
common->Error( "%s cannot use %s\n", classname, fileExtension.c_str() );
}
2012-11-26 18:58:24 +00:00
return true;
}
2012-11-26 18:58:24 +00:00
return false;
}
/*
===============================================================================
idAASFileLocal
===============================================================================
*/
#define AAS_LIST_GRANULARITY 1024
#define AAS_INDEX_GRANULARITY 4096
#define AAS_PLANE_GRANULARITY 4096
#define AAS_VERTEX_GRANULARITY 4096
#define AAS_EDGE_GRANULARITY 4096
/*
================
idAASFileLocal::idAASFileLocal
================
*/
idAASFileLocal::idAASFileLocal()
{
2012-11-26 18:58:24 +00:00
planeList.SetGranularity( AAS_PLANE_GRANULARITY );
vertices.SetGranularity( AAS_VERTEX_GRANULARITY );
edges.SetGranularity( AAS_EDGE_GRANULARITY );
edgeIndex.SetGranularity( AAS_INDEX_GRANULARITY );
faces.SetGranularity( AAS_LIST_GRANULARITY );
faceIndex.SetGranularity( AAS_INDEX_GRANULARITY );
areas.SetGranularity( AAS_LIST_GRANULARITY );
nodes.SetGranularity( AAS_LIST_GRANULARITY );
portals.SetGranularity( AAS_LIST_GRANULARITY );
portalIndex.SetGranularity( AAS_INDEX_GRANULARITY );
clusters.SetGranularity( AAS_LIST_GRANULARITY );
}
/*
================
idAASFileLocal::~idAASFileLocal
================
*/
idAASFileLocal::~idAASFileLocal()
{
2012-11-26 18:58:24 +00:00
int i;
idReachability* reach, *next;
for( i = 0; i < areas.Num(); i++ )
{
for( reach = areas[i].reach; reach; reach = next )
{
2012-11-26 18:58:24 +00:00
next = reach->next;
delete reach;
}
}
}
/*
================
idAASFileLocal::Clear
================
*/
void idAASFileLocal::Clear()
{
2012-11-26 18:58:24 +00:00
planeList.Clear();
vertices.Clear();
edges.Clear();
edgeIndex.Clear();
faces.Clear();
faceIndex.Clear();
areas.Clear();
nodes.Clear();
portals.Clear();
portalIndex.Clear();
clusters.Clear();
}
/*
================
idAASFileLocal::Write
================
*/
bool idAASFileLocal::Write( const idStr& fileName, unsigned int mapFileCRC )
{
2012-11-26 18:58:24 +00:00
int i, num;
idFile* aasFile;
idReachability* reach;
2012-11-26 18:58:24 +00:00
common->Printf( "[Write AAS]\n" );
common->Printf( "writing %s\n", fileName.c_str() );
2012-11-26 18:58:24 +00:00
name = fileName;
crc = mapFileCRC;
2012-11-26 18:58:24 +00:00
aasFile = fileSystem->OpenFileWrite( fileName, "fs_basepath" );
if( !aasFile )
{
2012-11-26 18:58:24 +00:00
common->Error( "Error opening %s", fileName.c_str() );
return false;
}
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "%s \"%s\"\n\n", AAS_FILEID, AAS_FILEVERSION );
aasFile->WriteFloatString( "%u\n\n", mapFileCRC );
2012-11-26 18:58:24 +00:00
// write out the settings
aasFile->WriteFloatString( "settings\n" );
settings.WriteToFile( aasFile );
2012-11-26 18:58:24 +00:00
// write out planes
aasFile->WriteFloatString( "planes %d {\n", planeList.Num() );
for( i = 0; i < planeList.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %f %f %f %f )\n", i,
planeList[i].Normal().x, planeList[i].Normal().y, planeList[i].Normal().z, planeList[i].Dist() );
2012-11-26 18:58:24 +00:00
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out vertices
aasFile->WriteFloatString( "vertices %d {\n", vertices.Num() );
for( i = 0; i < vertices.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %f %f %f )\n", i, vertices[i].x, vertices[i].y, vertices[i].z );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out edges
aasFile->WriteFloatString( "edges %d {\n", edges.Num() );
for( i = 0; i < edges.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d %d )\n", i, edges[i].vertexNum[0], edges[i].vertexNum[1] );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out edgeIndex
aasFile->WriteFloatString( "edgeIndex %d {\n", edgeIndex.Num() );
for( i = 0; i < edgeIndex.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d )\n", i, edgeIndex[i] );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out faces
aasFile->WriteFloatString( "faces %d {\n", faces.Num() );
for( i = 0; i < faces.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d %d %d %d %d %d )\n", i, faces[i].planeNum, faces[i].flags,
faces[i].areas[0], faces[i].areas[1], faces[i].firstEdge, faces[i].numEdges );
2012-11-26 18:58:24 +00:00
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out faceIndex
aasFile->WriteFloatString( "faceIndex %d {\n", faceIndex.Num() );
for( i = 0; i < faceIndex.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d )\n", i, faceIndex[i] );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out areas
aasFile->WriteFloatString( "areas %d {\n", areas.Num() );
for( i = 0; i < areas.Num(); i++ )
{
for( num = 0, reach = areas[i].reach; reach; reach = reach->next )
{
2012-11-26 18:58:24 +00:00
num++;
}
aasFile->WriteFloatString( "\t%d ( %d %d %d %d %d %d ) %d {\n", i, areas[i].flags, areas[i].contents,
areas[i].firstFace, areas[i].numFaces, areas[i].cluster, areas[i].clusterAreaNum, num );
for( reach = areas[i].reach; reach; reach = reach->next )
{
2012-11-26 18:58:24 +00:00
Reachability_Write( aasFile, reach );
switch( reach->travelType )
{
2012-11-26 18:58:24 +00:00
case TFL_SPECIAL:
Reachability_Special_Write( aasFile, static_cast<idReachability_Special*>( reach ) );
2012-11-26 18:58:24 +00:00
break;
}
aasFile->WriteFloatString( "\n" );
}
aasFile->WriteFloatString( "\t}\n" );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out nodes
aasFile->WriteFloatString( "nodes %d {\n", nodes.Num() );
for( i = 0; i < nodes.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d %d %d )\n", i, nodes[i].planeNum, nodes[i].children[0], nodes[i].children[1] );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out portals
aasFile->WriteFloatString( "portals %d {\n", portals.Num() );
for( i = 0; i < portals.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d %d %d %d %d )\n", i, portals[i].areaNum, portals[i].clusters[0],
portals[i].clusters[1], portals[i].clusterAreaNum[0], portals[i].clusterAreaNum[1] );
2012-11-26 18:58:24 +00:00
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out portalIndex
aasFile->WriteFloatString( "portalIndex %d {\n", portalIndex.Num() );
for( i = 0; i < portalIndex.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d )\n", i, portalIndex[i] );
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// write out clusters
aasFile->WriteFloatString( "clusters %d {\n", clusters.Num() );
for( i = 0; i < clusters.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
aasFile->WriteFloatString( "\t%d ( %d %d %d %d )\n", i, clusters[i].numAreas, clusters[i].numReachableAreas,
clusters[i].firstPortal, clusters[i].numPortals );
2012-11-26 18:58:24 +00:00
}
aasFile->WriteFloatString( "}\n" );
2012-11-26 18:58:24 +00:00
// close file
fileSystem->CloseFile( aasFile );
2012-11-26 18:58:24 +00:00
common->Printf( "done.\n" );
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idAASFileLocal::ParseIndex
================
*/
bool idAASFileLocal::ParseIndex( idLexer& src, idList<aasIndex_t>& indexes )
{
2012-11-26 18:58:24 +00:00
int numIndexes, i;
aasIndex_t index;
2012-11-26 18:58:24 +00:00
numIndexes = src.ParseInt();
indexes.Resize( numIndexes );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numIndexes; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
index = src.ParseInt();
src.ExpectTokenString( ")" );
indexes.Append( index );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParsePlanes
================
*/
bool idAASFileLocal::ParsePlanes( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numPlanes, i;
idPlane plane;
idVec4 vec;
2012-11-26 18:58:24 +00:00
numPlanes = src.ParseInt();
planeList.Resize( numPlanes );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numPlanes; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
if( !src.Parse1DMatrix( 4, vec.ToFloatPtr() ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
plane.SetNormal( vec.ToVec3() );
plane.SetDist( vec[3] );
planeList.Append( plane );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParseVertices
================
*/
bool idAASFileLocal::ParseVertices( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numVertices, i;
idVec3 vec;
2012-11-26 18:58:24 +00:00
numVertices = src.ParseInt();
vertices.Resize( numVertices );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numVertices; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
if( !src.Parse1DMatrix( 3, vec.ToFloatPtr() ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
vertices.Append( vec );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParseEdges
================
*/
bool idAASFileLocal::ParseEdges( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numEdges, i;
aasEdge_t edge;
2012-11-26 18:58:24 +00:00
numEdges = src.ParseInt();
edges.Resize( numEdges );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numEdges; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
edge.vertexNum[0] = src.ParseInt();
edge.vertexNum[1] = src.ParseInt();
src.ExpectTokenString( ")" );
edges.Append( edge );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParseFaces
================
*/
bool idAASFileLocal::ParseFaces( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numFaces, i;
aasFace_t face;
2012-11-26 18:58:24 +00:00
numFaces = src.ParseInt();
faces.Resize( numFaces );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numFaces; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
face.planeNum = src.ParseInt();
face.flags = src.ParseInt();
face.areas[0] = src.ParseInt();
face.areas[1] = src.ParseInt();
face.firstEdge = src.ParseInt();
face.numEdges = src.ParseInt();
src.ExpectTokenString( ")" );
faces.Append( face );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParseReachabilities
================
*/
bool idAASFileLocal::ParseReachabilities( idLexer& src, int areaNum )
{
2012-11-26 18:58:24 +00:00
int num, j;
aasArea_t* area;
2012-11-26 18:58:24 +00:00
idReachability reach, *newReach;
idReachability_Special* special;
2012-11-26 18:58:24 +00:00
area = &areas[areaNum];
2012-11-26 18:58:24 +00:00
num = src.ParseInt();
src.ExpectTokenString( "{" );
area->reach = NULL;
area->rev_reach = NULL;
area->travelFlags = AreaContentsTravelFlags( areaNum );
for( j = 0; j < num; j++ )
{
2012-11-26 18:58:24 +00:00
Reachability_Read( src, &reach );
switch( reach.travelType )
{
2012-11-26 18:58:24 +00:00
case TFL_SPECIAL:
newReach = special = new( TAG_AAS ) idReachability_Special();
2012-11-26 18:58:24 +00:00
Reachability_Special_Read( src, special );
break;
default:
newReach = new( TAG_AAS ) idReachability();
2012-11-26 18:58:24 +00:00
break;
}
newReach->CopyBase( reach );
newReach->fromAreaNum = areaNum;
newReach->next = area->reach;
area->reach = newReach;
}
src.ExpectTokenString( "}" );
return true;
}
/*
================
idAASFileLocal::LinkReversedReachability
================
*/
void idAASFileLocal::LinkReversedReachability()
{
2012-11-26 18:58:24 +00:00
int i;
idReachability* reach;
2012-11-26 18:58:24 +00:00
// link reversed reachabilities
for( i = 0; i < areas.Num(); i++ )
{
for( reach = areas[i].reach; reach; reach = reach->next )
{
2012-11-26 18:58:24 +00:00
reach->rev_next = areas[reach->toAreaNum].rev_reach;
areas[reach->toAreaNum].rev_reach = reach;
}
}
}
/*
================
idAASFileLocal::ParseAreas
================
*/
bool idAASFileLocal::ParseAreas( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numAreas, i;
aasArea_t area;
2012-11-26 18:58:24 +00:00
numAreas = src.ParseInt();
areas.Resize( numAreas );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numAreas; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
area.flags = src.ParseInt();
area.contents = src.ParseInt();
area.firstFace = src.ParseInt();
area.numFaces = src.ParseInt();
area.cluster = src.ParseInt();
area.clusterAreaNum = src.ParseInt();
src.ExpectTokenString( ")" );
areas.Append( area );
ParseReachabilities( src, i );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
LinkReversedReachability();
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idAASFileLocal::ParseNodes
================
*/
bool idAASFileLocal::ParseNodes( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numNodes, i;
aasNode_t node;
2012-11-26 18:58:24 +00:00
numNodes = src.ParseInt();
nodes.Resize( numNodes );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numNodes; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
node.planeNum = src.ParseInt();
node.children[0] = src.ParseInt();
node.children[1] = src.ParseInt();
src.ExpectTokenString( ")" );
nodes.Append( node );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParsePortals
================
*/
bool idAASFileLocal::ParsePortals( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numPortals, i;
aasPortal_t portal;
2012-11-26 18:58:24 +00:00
numPortals = src.ParseInt();
portals.Resize( numPortals );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
portal.areaNum = src.ParseInt();
portal.clusters[0] = src.ParseInt();
portal.clusters[1] = src.ParseInt();
portal.clusterAreaNum[0] = src.ParseInt();
portal.clusterAreaNum[1] = src.ParseInt();
src.ExpectTokenString( ")" );
portals.Append( portal );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::ParseClusters
================
*/
bool idAASFileLocal::ParseClusters( idLexer& src )
{
2012-11-26 18:58:24 +00:00
int numClusters, i;
aasCluster_t cluster;
2012-11-26 18:58:24 +00:00
numClusters = src.ParseInt();
clusters.Resize( numClusters );
if( !src.ExpectTokenString( "{" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( i = 0; i < numClusters; i++ )
{
2012-11-26 18:58:24 +00:00
src.ParseInt();
src.ExpectTokenString( "(" );
cluster.numAreas = src.ParseInt();
cluster.numReachableAreas = src.ParseInt();
cluster.firstPortal = src.ParseInt();
cluster.numPortals = src.ParseInt();
src.ExpectTokenString( ")" );
clusters.Append( cluster );
}
if( !src.ExpectTokenString( "}" ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
return true;
}
/*
================
idAASFileLocal::FinishAreas
================
*/
void idAASFileLocal::FinishAreas()
{
2012-11-26 18:58:24 +00:00
int i;
for( i = 0; i < areas.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
areas[i].center = AreaReachableGoal( i );
areas[i].bounds = AreaBounds( i );
}
}
/*
================
idAASFileLocal::Load
================
*/
bool idAASFileLocal::Load( const idStr& fileName, unsigned int mapFileCRC )
{
2012-11-26 18:58:24 +00:00
idLexer src( LEXFL_NOFATALERRORS | LEXFL_NOSTRINGESCAPECHARS | LEXFL_NOSTRINGCONCAT | LEXFL_ALLOWPATHNAMES );
idToken token;
int depth;
unsigned int c;
2012-11-26 18:58:24 +00:00
name = fileName;
crc = mapFileCRC;
2012-11-26 18:58:24 +00:00
common->Printf( "[Load AAS]\n" );
common->Printf( "loading %s\n", name.c_str() );
if( !src.LoadFile( name ) )
{
2012-11-26 18:58:24 +00:00
return false;
}
if( !src.ExpectTokenString( AAS_FILEID ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "Not an AAS file: '%s'", name.c_str() );
return false;
}
if( !src.ReadToken( &token ) || token != AAS_FILEVERSION )
{
2012-11-26 18:58:24 +00:00
common->Warning( "AAS file '%s' has version %s instead of %s", name.c_str(), token.c_str(), AAS_FILEVERSION );
return false;
}
if( !src.ExpectTokenType( TT_NUMBER, TT_INTEGER, &token ) )
{
2012-11-26 18:58:24 +00:00
common->Warning( "AAS file '%s' has no map file CRC", name.c_str() );
return false;
}
2012-11-26 18:58:24 +00:00
c = token.GetUnsignedLongValue();
if( mapFileCRC && c != mapFileCRC )
{
2012-11-26 18:58:24 +00:00
common->Warning( "AAS file '%s' is out of date", name.c_str() );
return false;
}
2012-11-26 18:58:24 +00:00
// clear the file in memory
Clear();
2012-11-26 18:58:24 +00:00
// parse the file
while( 1 )
{
if( !src.ReadToken( &token ) )
{
2012-11-26 18:58:24 +00:00
break;
}
if( token == "settings" )
{
if( !settings.FromParser( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "planes" )
{
if( !ParsePlanes( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "vertices" )
{
if( !ParseVertices( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "edges" )
{
if( !ParseEdges( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "edgeIndex" )
{
if( !ParseIndex( src, edgeIndex ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "faces" )
{
if( !ParseFaces( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "faceIndex" )
{
if( !ParseIndex( src, faceIndex ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "areas" )
{
if( !ParseAreas( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "nodes" )
{
if( !ParseNodes( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "portals" )
{
if( !ParsePortals( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "portalIndex" )
{
if( !ParseIndex( src, portalIndex ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else if( token == "clusters" )
{
if( !ParseClusters( src ) )
{
return false;
}
2012-11-26 18:58:24 +00:00
}
else
{
2012-11-26 18:58:24 +00:00
src.Error( "idAASFileLocal::Load: bad token \"%s\"", token.c_str() );
return false;
}
}
2012-11-26 18:58:24 +00:00
FinishAreas();
2012-11-26 18:58:24 +00:00
depth = MaxTreeDepth();
if( depth > MAX_AAS_TREE_DEPTH )
{
2012-11-26 18:58:24 +00:00
src.Error( "idAASFileLocal::Load: tree depth = %d", depth );
}
2012-11-26 18:58:24 +00:00
common->UpdateLevelLoadPacifier();
2012-11-26 18:58:24 +00:00
common->Printf( "done.\n" );
2012-11-26 18:58:24 +00:00
return true;
}
/*
================
idAASFileLocal::MemorySize
================
*/
int idAASFileLocal::MemorySize() const
{
2012-11-26 18:58:24 +00:00
int size;
2012-11-26 18:58:24 +00:00
size = planeList.Size();
size += vertices.Size();
size += edges.Size();
size += edgeIndex.Size();
size += faces.Size();
size += faceIndex.Size();
size += areas.Size();
size += nodes.Size();
size += portals.Size();
size += portalIndex.Size();
size += clusters.Size();
size += sizeof( idReachability_Walk ) * NumReachabilities();
2012-11-26 18:58:24 +00:00
return size;
}
/*
================
idAASFileLocal::PrintInfo
================
*/
void idAASFileLocal::PrintInfo() const
{
2012-11-26 18:58:24 +00:00
common->Printf( "%6d KB file size\n", MemorySize() >> 10 );
common->Printf( "%6d areas\n", areas.Num() );
common->Printf( "%6d max tree depth\n", MaxTreeDepth() );
ReportRoutingEfficiency();
}
/*
================
idAASFileLocal::NumReachabilities
================
*/
int idAASFileLocal::NumReachabilities() const
{
2012-11-26 18:58:24 +00:00
int i, num;
idReachability* reach;
2012-11-26 18:58:24 +00:00
num = 0;
for( i = 0; i < areas.Num(); i++ )
{
for( reach = areas[i].reach; reach; reach = reach->next )
{
2012-11-26 18:58:24 +00:00
num++;
}
}
return num;
}
/*
================
idAASFileLocal::ReportRoutingEfficiency
================
*/
void idAASFileLocal::ReportRoutingEfficiency() const
{
2012-11-26 18:58:24 +00:00
int numReachableAreas, total, i, n;
2012-11-26 18:58:24 +00:00
numReachableAreas = 0;
total = 0;
for( i = 0; i < clusters.Num(); i++ )
{
2012-11-26 18:58:24 +00:00
n = clusters[i].numReachableAreas;
numReachableAreas += n;
total += n * n;
}
total += numReachableAreas * portals.Num();
2012-11-26 18:58:24 +00:00
common->Printf( "%6d reachable areas\n", numReachableAreas );
common->Printf( "%6d reachabilities\n", NumReachabilities() );
common->Printf( "%6d KB max routing cache\n", ( total * 3 ) >> 10 );
}
/*
================
idAASFileLocal::DeleteReachabilities
================
*/
void idAASFileLocal::DeleteReachabilities()
{
2012-11-26 18:58:24 +00:00
int i;
idReachability* reach, *nextReach;
for( i = 0; i < areas.Num(); i++ )
{
for( reach = areas[i].reach; reach; reach = nextReach )
{
2012-11-26 18:58:24 +00:00
nextReach = reach->next;
delete reach;
}
areas[i].reach = NULL;
areas[i].rev_reach = NULL;
}
}
/*
================
idAASFileLocal::DeleteClusters
================
*/
void idAASFileLocal::DeleteClusters()
{
2012-11-26 18:58:24 +00:00
aasPortal_t portal;
aasCluster_t cluster;
2012-11-26 18:58:24 +00:00
portals.Clear();
portalIndex.Clear();
clusters.Clear();
2012-11-26 18:58:24 +00:00
// first portal is a dummy
memset( &portal, 0, sizeof( portal ) );
portals.Append( portal );
2012-11-26 18:58:24 +00:00
// first cluster is a dummy
memset( &cluster, 0, sizeof( cluster ) );
clusters.Append( cluster );
}