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 //////////////////////////////////////////////////////////////////////
27 #pragma warning(disable : 4786)
39 #include "dialogs/dialogs-gtk.h"
47 #include "generic/referencecounted.h"
57 const char* brushEntityList[] = {
77 //////////////////////////////////////////////////////////////////////
78 // Construction/Destruction
79 //////////////////////////////////////////////////////////////////////
81 DEntity::DEntity( const char *classname, int ID ){
82 SetClassname( classname );
93 //////////////////////////////////////////////////////////////////////
95 //////////////////////////////////////////////////////////////////////
97 void DEntity::ClearBrushes(){
98 for ( std::list<DBrush *>::const_iterator deadBrush = brushList.begin(); deadBrush != brushList.end(); deadBrush++ )
105 void DEntity::ClearPatches(){
106 for ( std::list<DPatch *>::const_iterator deadPatch = patchList.begin(); deadPatch != patchList.end(); deadPatch++ )
113 DPatch* DEntity::NewPatch(){
114 DPatch* newPatch = new DPatch;
116 patchList.push_back( newPatch );
121 DBrush* DEntity::NewBrush( int ID ){
122 DBrush* newBrush = new DBrush( ID );
124 brushList.push_back( newBrush );
129 char* getNextBracket( char* s ){
142 bool DEntity::LoadFromPrt( char *filename ){
144 strcpy( portals.fn, filename );
147 if ( portals.node_count == 0 ) {
155 for ( unsigned int i = 0; i < portals.node_count; i++ )
158 DBrush* brush = NewBrush();
160 for ( unsigned int j = 0; j < portals.node[i].portal_count; j++ )
162 for ( unsigned int k = 0; k < portals.node[i].portal[j].point_count - 2; k++ )
164 vec3_t v1, v2, normal, n;
165 VectorSubtract( portals.node[i].portal[j].point[k + 2].p, portals.node[i].portal[j].point[k + 1].p, v1 );
166 VectorSubtract( portals.node[i].portal[j].point[k].p, portals.node[i].portal[j].point[k + 1].p, v2 );
167 CrossProduct( v1, v2, n );
168 VectorNormalize( n, v2 );
171 VectorCopy( v2, normal );
175 VectorSubtract( v2, normal, v1 );
176 if ( VectorLength( v1 ) > 0.01 ) {
184 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 );
187 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 );
191 brush->BuildInRadiant( false, NULL );
198 DPlane* DEntity::AddFaceToBrush( vec3_t va, vec3_t vb, vec3_t vc, _QERFaceData* faceData, int ID ){
199 DBrush* buildBrush = GetBrushForID( ID );
200 return buildBrush->AddFace( va, vb, vc, faceData );
201 // slow, dont use much
204 DBrush* DEntity::GetBrushForID( int ID ){
205 DBrush* buildBrush = NULL;
207 for ( std::list<DBrush *>::const_iterator chkBrush = brushList.begin(); chkBrush != brushList.end(); chkBrush++ )
209 if ( ( *chkBrush )->m_nBrushID == ID ) {
210 buildBrush = ( *chkBrush );
216 buildBrush = NewBrush( ID );
222 template<typename Functor>
223 class BrushSelectedVisitor : public SelectionSystem::Visitor
225 const Functor& m_functor;
227 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
229 void visit( scene::Instance& instance ) const {
230 if ( Node_isBrush( instance.path().top() ) ) {
231 m_functor( instance );
236 template<typename Functor>
237 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
238 GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
242 void DEntity_loadBrush( DEntity& entity, scene::Instance& brush ){
243 DBrush* loadBrush = entity.NewBrush( static_cast<int>( entity.brushList.size() ) );
244 loadBrush->LoadFromBrush( brush, true );
246 typedef ReferenceCaller1<DEntity, scene::Instance&, DEntity_loadBrush> DEntityLoadBrushCaller;
248 void DEntity::LoadSelectedBrushes(){
252 Scene_forEachSelectedBrush( DEntityLoadBrushCaller( *this ) );
255 template<typename Functor>
256 class PatchSelectedVisitor : public SelectionSystem::Visitor
258 const Functor& m_functor;
260 PatchSelectedVisitor( const Functor& functor ) : m_functor( functor ){
262 void visit( scene::Instance& instance ) const {
263 if ( Node_isPatch( instance.path().top() ) ) {
264 m_functor( instance );
269 template<typename Functor>
270 inline const Functor& Scene_forEachSelectedPatch( const Functor& functor ){
271 GlobalSelectionSystem().foreachSelected( PatchSelectedVisitor<Functor>( functor ) );
275 void DEntity_loadPatch( DEntity& entity, scene::Instance& patch ){
276 DPatch* loadPatch = entity.NewPatch();
277 loadPatch->LoadFromPatch( patch );
279 typedef ReferenceCaller1<DEntity, scene::Instance&, DEntity_loadPatch> DEntityLoadPatchCaller;
281 void DEntity::LoadSelectedPatches(){
285 Scene_forEachSelectedPatch( DEntityLoadPatchCaller( *this ) );
288 bool* DEntity::BuildIntersectList(){
289 int max = GetIDMax();
294 bool* pbIntList = new bool[max];
295 memset( pbIntList, 0, sizeof( bool ) * ( max ) );
297 for ( std::list<DBrush *>::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ )
299 std::list<DBrush *>::const_iterator pB2 = pB1;
300 for ( pB2++; pB2 != brushList.end(); pB2++ )
302 if ( ( *pB1 )->IntersectsWith( ( *pB2 ) ) ) {
303 pbIntList[( *pB1 )->m_nBrushID] = true;
304 pbIntList[( *pB2 )->m_nBrushID] = true;
312 bool* DEntity::BuildDuplicateList(){
313 int max = GetIDMax();
318 bool* pbDupList = new bool[max];
319 memset( pbDupList, 0, sizeof( bool ) * ( max ) );
321 for ( std::list<DBrush *>::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ )
323 std::list<DBrush *>::const_iterator pB2 = pB1;
324 for ( pB2++; pB2 != brushList.end(); pB2++ )
326 if ( **pB1 == *pB2 ) {
327 pbDupList[( *pB1 )->m_nBrushID] = true;
328 pbDupList[( *pB2 )->m_nBrushID] = true;
336 void DEntity::SelectBrushes( bool *selectList ){
337 if ( selectList == NULL ) {
341 GlobalSelectionSystem().setSelectedAll( false );
343 scene::Path path( NodeReference( GlobalSceneGraph().root() ) );
344 path.push( NodeReference( *QER_Entity ) );
346 for ( std::list<DBrush *>::const_iterator pBrush = brushList.begin(); pBrush != brushList.end(); pBrush++ )
348 if ( selectList[( *pBrush )->m_nBrushID] ) {
349 path.push( NodeReference( *( *pBrush )->QER_brush ) );
350 Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true );
356 bool DEntity::LoadFromEntity( scene::Node& ent, bool bLoadPatches ) {
363 LoadEPairList( Node_getEntity( ent ) );
367 for ( i = 0; brushEntityList[i]; i++ )
369 if ( string_equal_nocase( brushEntityList[i], m_Classname ) ) {
379 if ( Node_getTraversable( ent ) ) {
380 class load_brushes_t : public scene::Traversable::Walker
385 load_brushes_t( DEntity* entity )
386 : m_entity( entity ), m_count( 0 ){
388 bool pre( scene::Node& node ) const {
389 scene::Path path( NodeReference( GlobalSceneGraph().root() ) );
390 path.push( NodeReference( *m_entity->QER_Entity ) );
391 path.push( NodeReference( node ) );
392 scene::Instance* instance = GlobalSceneGraph().find( path );
393 ASSERT_MESSAGE( instance != 0, "" );
395 if ( Node_isPatch( node ) ) {
396 DPatch* loadPatch = m_entity->NewPatch();
397 loadPatch->LoadFromPatch( *instance );
399 else if ( Node_isBrush( node ) ) {
400 DBrush* loadBrush = m_entity->NewBrush( m_count++ );
401 loadBrush->LoadFromBrush( *instance, true );
405 } load_brushes( this );
407 Node_getTraversable( ent )->traverse( load_brushes );
413 void DEntity::RemoveNonCheckBrushes( std::list<Str>* exclusionList, bool useDetail ){
414 std::list<DBrush *>::iterator chkBrush = brushList.begin();
416 while ( chkBrush != brushList.end() )
419 if ( ( *chkBrush )->IsDetail() ) {
421 chkBrush = brushList.erase( chkBrush );
426 std::list<Str>::iterator eTexture;
428 for ( eTexture = exclusionList->begin(); eTexture != exclusionList->end(); eTexture++ )
430 if ( ( *chkBrush )->HasTexture( ( *eTexture ).GetBuffer() ) ) {
432 chkBrush = brushList.erase( chkBrush );
437 if ( eTexture == exclusionList->end() ) {
443 void DEntity::ResetChecks( std::list<Str>* exclusionList ){
444 for ( std::list<DBrush *>::const_iterator resetBrush = brushList.begin(); resetBrush != brushList.end(); resetBrush++ )
446 ( *resetBrush )->ResetChecks( exclusionList );
450 int DEntity::FixBrushes(){
453 for ( std::list<DBrush *>::const_iterator fixBrush = brushList.begin(); fixBrush != brushList.end(); fixBrush++ )
455 count += ( *fixBrush )->RemoveRedundantPlanes();
461 void DEntity::BuildInRadiant( bool allowDestruction ){
462 bool makeEntity = strcmp( m_Classname, "worldspawn" ) ? true : false;
465 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_Classname.GetBuffer(), !brushList.empty() || !patchList.empty() ) ) );
467 for ( std::list<DEPair* >::const_iterator buildEPair = epairList.begin(); buildEPair != epairList.end(); buildEPair++ )
469 Node_getEntity( node )->setKeyValue( ( *buildEPair )->key, ( *buildEPair )->value );
472 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
474 for ( std::list<DBrush *>::const_iterator buildBrush = brushList.begin(); buildBrush != brushList.end(); buildBrush++ )
475 ( *buildBrush )->BuildInRadiant( allowDestruction, NULL, node.get_pointer() );
477 for ( std::list<DPatch *>::const_iterator buildPatch = patchList.begin(); buildPatch != patchList.end(); buildPatch++ )
478 ( *buildPatch )->BuildInRadiant( node.get_pointer() );
480 QER_Entity = node.get_pointer();
484 for ( std::list<DBrush *>::const_iterator buildBrush = brushList.begin(); buildBrush != brushList.end(); buildBrush++ )
485 ( *buildBrush )->BuildInRadiant( allowDestruction, NULL );
487 for ( std::list<DPatch *>::const_iterator buildPatch = patchList.begin(); buildPatch != patchList.end(); buildPatch++ )
488 ( *buildPatch )->BuildInRadiant();
494 int DEntity::GetIDMax( void ) {
496 for ( std::list<DBrush *>::const_iterator cntBrush = brushList.begin(); cntBrush != brushList.end(); cntBrush++ ) {
497 if ( ( *cntBrush )->m_nBrushID > max ) {
498 max = ( *cntBrush )->m_nBrushID;
504 void DEntity::SetClassname( const char *classname ) {
505 m_Classname = classname;
508 void DEntity::SaveToFile( FILE *pFile ){
509 fprintf( pFile, "{\n" );
511 fprintf( pFile, "\"classname\" \"%s\"\n", (const char *)m_Classname );
513 for ( std::list<DEPair *>::const_iterator ep = epairList.begin(); ep != epairList.end(); ep++ )
515 fprintf( pFile, "\"%s\" \"%s\"\n", (const char *)( *ep )->key, (const char *)( *ep )->value );
518 for ( std::list<DBrush *>::const_iterator bp = brushList.begin(); bp != brushList.end(); bp++ )
520 ( *bp )->SaveToFile( pFile );
523 fprintf( pFile, "}\n" );
526 void DEntity::ClearEPairs(){
527 for ( std::list<DEPair *>::const_iterator deadEPair = epairList.begin(); deadEPair != epairList.end(); deadEPair++ )
529 delete ( *deadEPair );
534 void DEntity::AddEPair( const char *key, const char *value ) {
536 newEPair = FindEPairByKey( key );
538 newEPair = new DEPair;
539 newEPair->Build( key, value );
540 epairList.push_back( newEPair );
543 newEPair->Build( key, value );
547 void DEntity::LoadEPairList( Entity *epl ){
548 class load_epairs_t : public Entity::Visitor
552 load_epairs_t( DEntity* entity )
553 : m_entity( entity ){
555 void visit( const char* key, const char* value ){
556 if ( strcmp( key, "classname" ) == 0 ) {
557 m_entity->SetClassname( value );
560 m_entity->AddEPair( key, value );
564 } load_epairs( this );
566 epl->forEachKeyValue( load_epairs );
569 bool DEntity::ResetTextures( const char* textureName, float fScale[2], float fShift[2], int rotation, const char* newTextureName,
570 int bResetTextureName, int bResetScale[2], int bResetShift[2], int bResetRotation, bool rebuild ){
573 for ( std::list<DBrush *>::const_iterator resetBrush = brushList.begin(); resetBrush != brushList.end(); resetBrush++ )
575 bool tmp = ( *resetBrush )->ResetTextures( textureName, fScale, fShift, rotation, newTextureName,
576 bResetTextureName, bResetScale, bResetShift, bResetRotation );
581 Node_getTraversable( *( *resetBrush )->QER_entity )->erase( *( *resetBrush )->QER_brush );
582 ( *resetBrush )->BuildInRadiant( false, NULL, ( *resetBrush )->QER_entity );
587 if ( bResetTextureName ) {
588 for ( std::list<DPatch *>::const_iterator resetPatch = patchList.begin(); resetPatch != patchList.end(); resetPatch++ )
590 bool tmp = ( *resetPatch )->ResetTextures( textureName, newTextureName );
595 Node_getTraversable( *( *resetPatch )->QER_entity )->erase( *( *resetPatch )->QER_brush );
596 ( *resetPatch )->BuildInRadiant( ( *resetPatch )->QER_entity );
605 DEPair* DEntity::FindEPairByKey( const char* keyname ){
606 for ( std::list<DEPair *>::const_iterator ep = epairList.begin(); ep != epairList.end(); ep++ )
608 char* c = ( *ep )->key;
609 if ( !strcmp( c, keyname ) ) {
616 void DEntity::RemoveFromRadiant(){
617 Node_getTraversable( GlobalSceneGraph().root() )->erase( *QER_Entity );
622 void DEntity::SpawnString( const char* key, const char* defaultstring, const char** out ){
623 DEPair* pEP = FindEPairByKey( key );
628 *out = defaultstring;
632 void DEntity::SpawnInt( const char* key, const char* defaultstring, int* out ){
633 DEPair* pEP = FindEPairByKey( key );
635 *out = atoi( pEP->value );
638 *out = atoi( defaultstring );
642 void DEntity::SpawnFloat( const char* key, const char* defaultstring, float* out ){
643 DEPair* pEP = FindEPairByKey( key );
645 *out = static_cast<float>( atof( pEP->value ) );
648 *out = static_cast<float>( atof( defaultstring ) );
652 void DEntity::SpawnVector( const char* key, const char* defaultstring, vec_t* out ){
653 DEPair* pEP = FindEPairByKey( key );
655 sscanf( pEP->value, "%f %f %f", &out[0], &out[1], &out[2] );
658 sscanf( defaultstring, "%f %f %f", &out[0], &out[1], &out[2] );
662 int DEntity::GetBrushCount( void ) {
663 return static_cast<int>( brushList.size() );
666 DBrush* DEntity::FindBrushByPointer( scene::Node& brush ) {
667 for ( std::list<DBrush *>::const_iterator listBrush = brushList.begin(); listBrush != brushList.end(); listBrush++ ) {
668 DBrush* pBrush = ( *listBrush );
669 if ( pBrush->QER_brush == &brush ) {