/*
================
idAASBuild::StoreFile
================
*/
bool idAASBuild::StoreFile( const idBrushBSP& bsp )
{
	aasEdge_t edge;
	aasFace_t face;
	aasArea_t area;
	aasNode_t node;
	
	common->Printf( "[Store AAS]\n" );
	
	SetupHash();
	ClearHash( bsp.GetTreeBounds() );
	
	file = new idAASFileLocal();
	
	file->Clear();
	
	SetSizeEstimate( bsp, file );
	
	// the first edge is a dummy
	memset( &edge, 0, sizeof( edge ) );
	file->edges.Append( edge );
	
	// the first face is a dummy
	memset( &face, 0, sizeof( face ) );
	file->faces.Append( face );
	
	// the first area is a dummy
	memset( &area, 0, sizeof( area ) );
	file->areas.Append( area );
	
	// the first node is a dummy
	memset( &node, 0, sizeof( node ) );
	file->nodes.Append( node );
	
	// store the tree
	StoreTree_r( bsp.GetRootNode() );
	
	// calculate area bounds and a reachable point in the area
	file->FinishAreas();
	
	ShutdownHash();
	
	common->Printf( "\r%6d areas\n", file->areas.Num() );
	
	return true;
}
/*
================
idCollisionModelManagerLocal::WriteCollisionModelForMapEntity
================
*/
bool idCollisionModelManagerLocal::WriteCollisionModelForMapEntity( const idMapEntity *mapEnt, const char *filename, const bool testTraceModel )
{
	idFile		*fp;
	idStr		name;
	cm_model_t *model;
	
	SetupHash();
	model = CollisionModelForMapEntity( mapEnt );
	model->name = filename;
	
	name = filename;
	name.SetFileExtension( CM_FILE_EXT );
	
	common->Printf( "writing %s\n", name.c_str() );
	fp = fileSystem->OpenFileWrite( name, "fs_devpath" );
	
	if( !fp )
	{
		common->Printf( "idCollisionModelManagerLocal::WriteCollisionModelForMapEntity: Error opening file %s\n", name.c_str() );
		FreeModel( model );
		return false;
	}
	
	// write file id and version
	fp->WriteFloatString( "%s \"%s\"\n\n", CM_FILEID, CM_FILEVERSION );
	
	// write the map file crc
	fp->WriteFloatString( "%u\n\n", 0 );
	
	// write the collision model
	WriteCollisionModel( fp, model );
	
	fileSystem->CloseFile( fp );
	
	if( testTraceModel )
	{
		idTraceModel trm;
		TrmFromModel( model, trm );
	}
	FreeModel( model );
	
	return true;
}
/*
================
idCollisionModelManagerLocal::GenerateCollisionMapForModel
================
*/
void idCollisionModelManagerLocal::GenerateCollisionMapForModel( const char *filename ) {
	idFile *fp;
	idStr name;
	cm_model_t *model;

	SetupHash();
	model = LoadRenderModel( filename );
	if(!model) {
		common->Warning("GenerateCollisionMapForModel: failed to generate CM for %s\n", filename);
		return;
	}
	model->name = filename;

	name = filename;
	name.SetFileExtension( CM_FILE_EXT );

	common->Printf( "writing %s\n", name.c_str() );
	fp = fileSystem->OpenFileWrite( name, "fs_devpath" );
	if ( !fp ) {
		common->Printf( "idCollisionModelManagerLocal::WriteCollisionModelForMapEntity: Error opening file %s\n", name.c_str() );
		FreeModel( model );
		return;
	}

	// write file id and version
	fp->WriteFloatString( "%s \"%s\"\n\n", CM_FILEID, CM_FILEVERSION );
	// write the map file crc
	fp->WriteFloatString( "%u\n\n", 0 );

	// write the collision model
	WriteCollisionModel( fp, model );

	fileSystem->CloseFile( fp );

	FreeModel( model );

	
}