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
20 // DEntity.cpp: implementation of the DEntity class.
22 //////////////////////////////////////////////////////////////////////
25 #include "globaldefs.h"
27 #if GDEF_COMPILER_MSVC
28 #pragma warning(disable : 4786)
40 #include "dialogs/dialogs-gtk.h"
48 #include "generic/referencecounted.h"
58 const char* brushEntityList[] = {
78 //////////////////////////////////////////////////////////////////////
79 // Construction/Destruction
80 //////////////////////////////////////////////////////////////////////
82 DEntity::DEntity( const char *classname, int ID ){
83 SetClassname( classname );
94 //////////////////////////////////////////////////////////////////////
96 //////////////////////////////////////////////////////////////////////
98 void DEntity::ClearBrushes(){
99 for ( std::list<DBrush *>::const_iterator deadBrush = brushList.begin(); deadBrush != brushList.end(); deadBrush++ )
106 void DEntity::ClearPatches(){
107 for ( std::list<DPatch *>::const_iterator deadPatch = patchList.begin(); deadPatch != patchList.end(); deadPatch++ )
114 DPatch* DEntity::NewPatch(){
115 DPatch* newPatch = new DPatch;
117 patchList.push_back( newPatch );
122 DBrush* DEntity::NewBrush( int ID ){
123 DBrush* newBrush = new DBrush( ID );
125 brushList.push_back( newBrush );
130 char* getNextBracket( char* s ){
143 bool DEntity::LoadFromPrt( char *filename ){
145 strcpy( portals.fn, filename );
148 if ( portals.node_count == 0 ) {
156 for ( unsigned int i = 0; i < portals.node_count; i++ )
159 DBrush* brush = NewBrush();
161 for ( unsigned int j = 0; j < portals.node[i].portal_count; j++ )
163 for ( unsigned int k = 0; k < portals.node[i].portal[j].point_count - 2; k++ )
165 vec3_t v1, v2, normal, n;
166 VectorSubtract( portals.node[i].portal[j].point[k + 2].p, portals.node[i].portal[j].point[k + 1].p, v1 );
167 VectorSubtract( portals.node[i].portal[j].point[k].p, portals.node[i].portal[j].point[k + 1].p, v2 );
168 CrossProduct( v1, v2, n );
169 VectorNormalize( n, v2 );
172 VectorCopy( v2, normal );
176 VectorSubtract( v2, normal, v1 );
177 if ( VectorLength( v1 ) > 0.01 ) {
185 brush->AddFace( portals.node[i].portal[j].point[2].p, portals.node[i].portal[j].point[1].p, portals.node[i].portal[j].point[0].p, "textures/common/caulk", false );
188 brush->AddFace( portals.node[i].portal[j].point[0].p, portals.node[i].portal[j].point[1].p, portals.node[i].portal[j].point[2].p, "textures/common/caulk", false );
192 brush->BuildInRadiant( false, NULL );
199 DPlane* DEntity::AddFaceToBrush( vec3_t va, vec3_t vb, vec3_t vc, _QERFaceData* faceData, int ID ){
200 DBrush* buildBrush = GetBrushForID( ID );
201 return buildBrush->AddFace( va, vb, vc, faceData );
202 // slow, dont use much
205 DBrush* DEntity::GetBrushForID( int ID ){
206 DBrush* buildBrush = NULL;
208 for ( std::list<DBrush *>::const_iterator chkBrush = brushList.begin(); chkBrush != brushList.end(); chkBrush++ )
210 if ( ( *chkBrush )->m_nBrushID == ID ) {
211 buildBrush = ( *chkBrush );
217 buildBrush = NewBrush( ID );
223 template<typename Functor>
224 class BrushSelectedVisitor : public SelectionSystem::Visitor
226 const Functor& m_functor;
228 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
230 void visit( scene::Instance& instance ) const {
231 if ( Node_isBrush( instance.path().top() ) ) {
232 m_functor( instance );
237 template<typename Functor>
238 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
239 GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
243 void DEntity_loadBrush( DEntity& entity, scene::Instance& brush ){
244 DBrush* loadBrush = entity.NewBrush( static_cast<int>( entity.brushList.size() ) );
245 loadBrush->LoadFromBrush( brush, true );
247 typedef ReferenceCaller1<DEntity, scene::Instance&, DEntity_loadBrush> DEntityLoadBrushCaller;
249 void DEntity::LoadSelectedBrushes(){
253 Scene_forEachSelectedBrush( DEntityLoadBrushCaller( *this ) );
256 template<typename Functor>
257 class PatchSelectedVisitor : public SelectionSystem::Visitor
259 const Functor& m_functor;
261 PatchSelectedVisitor( const Functor& functor ) : m_functor( functor ){
263 void visit( scene::Instance& instance ) const {
264 if ( Node_isPatch( instance.path().top() ) ) {
265 m_functor( instance );
270 template<typename Functor>
271 inline const Functor& Scene_forEachSelectedPatch( const Functor& functor ){
272 GlobalSelectionSystem().foreachSelected( PatchSelectedVisitor<Functor>( functor ) );
276 void DEntity_loadPatch( DEntity& entity, scene::Instance& patch ){
277 DPatch* loadPatch = entity.NewPatch();
278 loadPatch->LoadFromPatch( patch );
280 typedef ReferenceCaller1<DEntity, scene::Instance&, DEntity_loadPatch> DEntityLoadPatchCaller;
282 void DEntity::LoadSelectedPatches(){
286 Scene_forEachSelectedPatch( DEntityLoadPatchCaller( *this ) );
289 bool* DEntity::BuildIntersectList(){
290 int max = GetIDMax();
295 bool* pbIntList = new bool[max];
296 memset( pbIntList, 0, sizeof( bool ) * ( max ) );
298 for ( std::list<DBrush *>::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ )
300 std::list<DBrush *>::const_iterator pB2 = pB1;
301 for ( pB2++; pB2 != brushList.end(); pB2++ )
303 if ( ( *pB1 )->IntersectsWith( ( *pB2 ) ) ) {
304 pbIntList[( *pB1 )->m_nBrushID] = true;
305 pbIntList[( *pB2 )->m_nBrushID] = true;
313 bool* DEntity::BuildDuplicateList(){
314 int max = GetIDMax();
319 bool* pbDupList = new bool[max];
320 memset( pbDupList, 0, sizeof( bool ) * ( max ) );
322 for ( std::list<DBrush *>::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ )
324 std::list<DBrush *>::const_iterator pB2 = pB1;
325 for ( pB2++; pB2 != brushList.end(); pB2++ )
327 if ( **pB1 == *pB2 ) {
328 pbDupList[( *pB1 )->m_nBrushID] = true;
329 pbDupList[( *pB2 )->m_nBrushID] = true;
337 void DEntity::SelectBrushes( bool *selectList ){
338 if ( selectList == NULL ) {
342 GlobalSelectionSystem().setSelectedAll( false );
344 scene::Path path( NodeReference( GlobalSceneGraph().root() ) );
345 path.push( NodeReference( *QER_Entity ) );
347 for ( std::list<DBrush *>::const_iterator pBrush = brushList.begin(); pBrush != brushList.end(); pBrush++ )
349 if ( selectList[( *pBrush )->m_nBrushID] ) {
350 path.push( NodeReference( *( *pBrush )->QER_brush ) );
351 Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true );
357 bool DEntity::LoadFromEntity( scene::Node& ent, bool bLoadPatches ) {
364 LoadEPairList( Node_getEntity( ent ) );
368 for ( i = 0; brushEntityList[i]; i++ )
370 if ( string_equal_nocase( brushEntityList[i], m_Classname ) ) {
380 if ( Node_getTraversable( ent ) ) {
381 class load_brushes_t : public scene::Traversable::Walker
386 load_brushes_t( DEntity* entity )
387 : m_entity( entity ), m_count( 0 ){
389 bool pre( scene::Node& node ) const {
390 scene::Path path( NodeReference( GlobalSceneGraph().root() ) );
391 path.push( NodeReference( *m_entity->QER_Entity ) );
392 path.push( NodeReference( node ) );
393 scene::Instance* instance = GlobalSceneGraph().find( path );
394 ASSERT_MESSAGE( instance != 0, "" );
396 if ( Node_isPatch( node ) ) {
397 DPatch* loadPatch = m_entity->NewPatch();
398 loadPatch->LoadFromPatch( *instance );
400 else if ( Node_isBrush( node ) ) {
401 DBrush* loadBrush = m_entity->NewBrush( m_count++ );
402 loadBrush->LoadFromBrush( *instance, true );
406 } load_brushes( this );
408 Node_getTraversable( ent )->traverse( load_brushes );
414 void DEntity::RemoveNonCheckBrushes( std::list<Str>* exclusionList, bool useDetail ){
415 std::list<DBrush *>::iterator chkBrush = brushList.begin();
417 while ( chkBrush != brushList.end() )
420 if ( ( *chkBrush )->IsDetail() ) {
422 chkBrush = brushList.erase( chkBrush );
427 std::list<Str>::iterator eTexture;
429 for ( eTexture = exclusionList->begin(); eTexture != exclusionList->end(); eTexture++ )
431 if ( ( *chkBrush )->HasTexture( ( *eTexture ).GetBuffer() ) ) {
433 chkBrush = brushList.erase( chkBrush );
438 if ( eTexture == exclusionList->end() ) {
444 void DEntity::ResetChecks( std::list<Str>* exclusionList ){
445 for ( std::list<DBrush *>::const_iterator resetBrush = brushList.begin(); resetBrush != brushList.end(); resetBrush++ )
447 ( *resetBrush )->ResetChecks( exclusionList );
451 int DEntity::FixBrushes(){
454 for ( std::list<DBrush *>::const_iterator fixBrush = brushList.begin(); fixBrush != brushList.end(); fixBrush++ )
456 count += ( *fixBrush )->RemoveRedundantPlanes();
462 void DEntity::BuildInRadiant( bool allowDestruction ){
463 bool makeEntity = strcmp( m_Classname, "worldspawn" ) ? true : false;
466 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_Classname.GetBuffer(), !brushList.empty() || !patchList.empty() ) ) );
468 for ( std::list<DEPair* >::const_iterator buildEPair = epairList.begin(); buildEPair != epairList.end(); buildEPair++ )
470 Node_getEntity( node )->setKeyValue( ( *buildEPair )->key, ( *buildEPair )->value );
473 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
475 for ( std::list<DBrush *>::const_iterator buildBrush = brushList.begin(); buildBrush != brushList.end(); buildBrush++ )
476 ( *buildBrush )->BuildInRadiant( allowDestruction, NULL, node.get_pointer() );
478 for ( std::list<DPatch *>::const_iterator buildPatch = patchList.begin(); buildPatch != patchList.end(); buildPatch++ )
479 ( *buildPatch )->BuildInRadiant( node.get_pointer() );
481 QER_Entity = node.get_pointer();
485 for ( std::list<DBrush *>::const_iterator buildBrush = brushList.begin(); buildBrush != brushList.end(); buildBrush++ )
486 ( *buildBrush )->BuildInRadiant( allowDestruction, NULL );
488 for ( std::list<DPatch *>::const_iterator buildPatch = patchList.begin(); buildPatch != patchList.end(); buildPatch++ )
489 ( *buildPatch )->BuildInRadiant();
495 int DEntity::GetIDMax( void ) {
497 for ( std::list<DBrush *>::const_iterator cntBrush = brushList.begin(); cntBrush != brushList.end(); cntBrush++ ) {
498 if ( ( *cntBrush )->m_nBrushID > max ) {
499 max = ( *cntBrush )->m_nBrushID;
505 void DEntity::SetClassname( const char *classname ) {
506 m_Classname = classname;
509 void DEntity::SaveToFile( FILE *pFile ){
510 fprintf( pFile, "{\n" );
512 fprintf( pFile, "\"classname\" \"%s\"\n", (const char *)m_Classname );
514 for ( std::list<DEPair *>::const_iterator ep = epairList.begin(); ep != epairList.end(); ep++ )
516 fprintf( pFile, "\"%s\" \"%s\"\n", (const char *)( *ep )->key, (const char *)( *ep )->value );
519 for ( std::list<DBrush *>::const_iterator bp = brushList.begin(); bp != brushList.end(); bp++ )
521 ( *bp )->SaveToFile( pFile );
524 fprintf( pFile, "}\n" );
527 void DEntity::ClearEPairs(){
528 for ( std::list<DEPair *>::const_iterator deadEPair = epairList.begin(); deadEPair != epairList.end(); deadEPair++ )
530 delete ( *deadEPair );
535 void DEntity::AddEPair( const char *key, const char *value ) {
537 newEPair = FindEPairByKey( key );
539 newEPair = new DEPair;
540 newEPair->Build( key, value );
541 epairList.push_back( newEPair );
544 newEPair->Build( key, value );
548 void DEntity::LoadEPairList( Entity *epl ){
549 class load_epairs_t : public Entity::Visitor
553 load_epairs_t( DEntity* entity )
554 : m_entity( entity ){
556 void visit( const char* key, const char* value ){
557 if ( strcmp( key, "classname" ) == 0 ) {
558 m_entity->SetClassname( value );
561 m_entity->AddEPair( key, value );
565 } load_epairs( this );
567 epl->forEachKeyValue( load_epairs );
570 bool DEntity::ResetTextures( const char* textureName, float fScale[2], float fShift[2], int rotation, const char* newTextureName,
571 int bResetTextureName, int bResetScale[2], int bResetShift[2], int bResetRotation, bool rebuild ){
574 for ( std::list<DBrush *>::const_iterator resetBrush = brushList.begin(); resetBrush != brushList.end(); resetBrush++ )
576 bool tmp = ( *resetBrush )->ResetTextures( textureName, fScale, fShift, rotation, newTextureName,
577 bResetTextureName, bResetScale, bResetShift, bResetRotation );
582 Node_getTraversable( *( *resetBrush )->QER_entity )->erase( *( *resetBrush )->QER_brush );
583 ( *resetBrush )->BuildInRadiant( false, NULL, ( *resetBrush )->QER_entity );
588 if ( bResetTextureName ) {
589 for ( std::list<DPatch *>::const_iterator resetPatch = patchList.begin(); resetPatch != patchList.end(); resetPatch++ )
591 bool tmp = ( *resetPatch )->ResetTextures( textureName, newTextureName );
596 Node_getTraversable( *( *resetPatch )->QER_entity )->erase( *( *resetPatch )->QER_brush );
597 ( *resetPatch )->BuildInRadiant( ( *resetPatch )->QER_entity );
606 DEPair* DEntity::FindEPairByKey( const char* keyname ){
607 for ( std::list<DEPair *>::const_iterator ep = epairList.begin(); ep != epairList.end(); ep++ )
609 char* c = ( *ep )->key;
610 if ( !strcmp( c, keyname ) ) {
617 void DEntity::RemoveFromRadiant(){
618 Node_getTraversable( GlobalSceneGraph().root() )->erase( *QER_Entity );
623 void DEntity::SpawnString( const char* key, const char* defaultstring, const char** out ){
624 DEPair* pEP = FindEPairByKey( key );
629 *out = defaultstring;
633 void DEntity::SpawnInt( const char* key, const char* defaultstring, int* out ){
634 DEPair* pEP = FindEPairByKey( key );
636 *out = atoi( pEP->value );
639 *out = atoi( defaultstring );
643 void DEntity::SpawnFloat( const char* key, const char* defaultstring, float* out ){
644 DEPair* pEP = FindEPairByKey( key );
646 *out = static_cast<float>( atof( pEP->value ) );
649 *out = static_cast<float>( atof( defaultstring ) );
653 void DEntity::SpawnVector( const char* key, const char* defaultstring, vec_t* out ){
654 DEPair* pEP = FindEPairByKey( key );
656 sscanf( pEP->value, "%f %f %f", &out[0], &out[1], &out[2] );
659 sscanf( defaultstring, "%f %f %f", &out[0], &out[1], &out[2] );
663 int DEntity::GetBrushCount( void ) {
664 return static_cast<int>( brushList.size() );
667 DBrush* DEntity::FindBrushByPointer( scene::Node& brush ) {
668 for ( std::list<DBrush *>::const_iterator listBrush = brushList.begin(); listBrush != brushList.end(); listBrush++ ) {
669 DBrush* pBrush = ( *listBrush );
670 if ( pBrush->QER_brush == &brush ) {