From bf4db606138bce1d1d50b3cc0992a2aa0c0e11a9 Mon Sep 17 00:00:00 2001 From: Garux Date: Fri, 1 Sep 2023 07:19:28 +0600 Subject: [PATCH] bobToolz: remove brush ID, store brushes & patches in vector --- contrib/bobtoolz/DBrush.cpp | 3 +- contrib/bobtoolz/DBrush.h | 3 +- contrib/bobtoolz/DEntity.cpp | 138 ++++++++---------------------- contrib/bobtoolz/DEntity.h | 10 +-- contrib/bobtoolz/DShape.cpp | 7 +- contrib/bobtoolz/DTreePlanter.cpp | 7 +- 6 files changed, 45 insertions(+), 123 deletions(-) diff --git a/contrib/bobtoolz/DBrush.cpp b/contrib/bobtoolz/DBrush.cpp index 470fbc4e..3fdbc6f1 100644 --- a/contrib/bobtoolz/DBrush.cpp +++ b/contrib/bobtoolz/DBrush.cpp @@ -47,8 +47,7 @@ // Construction/Destruction ////////////////////////////////////////////////////////////////////// -DBrush::DBrush( int ID ){ - m_nBrushID = ID; +DBrush::DBrush(){ bBoundsBuilt = false; QER_entity = NULL; QER_brush = NULL; diff --git a/contrib/bobtoolz/DBrush.h b/contrib/bobtoolz/DBrush.h index 1506fdf2..6aeda074 100644 --- a/contrib/bobtoolz/DBrush.h +++ b/contrib/bobtoolz/DBrush.h @@ -91,7 +91,7 @@ public: DPlane* FindPlaneWithClosestNormal( vec_t* normal ); int FindPointsForPlane( DPlane* plane, DPoint** pnts, int maxpnts ); - DBrush( int ID = -1 ); + DBrush(); virtual ~DBrush(); bool operator==( const DBrush* other ) const; @@ -101,7 +101,6 @@ public: scene::Node* QER_brush; std::list faceList; std::list pointList; - int m_nBrushID; vec3_t bbox_min, bbox_max; bool bBoundsBuilt; }; diff --git a/contrib/bobtoolz/DEntity.cpp b/contrib/bobtoolz/DEntity.cpp index 8be4caa0..ef2b6adb 100644 --- a/contrib/bobtoolz/DEntity.cpp +++ b/contrib/bobtoolz/DEntity.cpp @@ -24,6 +24,7 @@ #include "DEntity.h" #include +#include #include "str.h" #include "DPoint.h" @@ -107,19 +108,11 @@ void DEntity::ClearPatches(){ } DPatch* DEntity::NewPatch(){ - DPatch* newPatch = new DPatch; - - patchList.push_back( newPatch ); - - return newPatch; + return patchList.emplace_back( new DPatch ); } -DBrush* DEntity::NewBrush( int ID ){ - DBrush* newBrush = new DBrush( ID ); - - brushList.push_back( newBrush ); - - return newBrush; +DBrush* DEntity::NewBrush(){ + return brushList.emplace_back( new DBrush ); } char* getNextBracket( char* s ){ @@ -191,30 +184,6 @@ bool DEntity::LoadFromPrt( char *filename ){ return true; } -DPlane* DEntity::AddFaceToBrush( vec3_t va, vec3_t vb, vec3_t vc, _QERFaceData* faceData, int ID ){ - DBrush* buildBrush = GetBrushForID( ID ); - return buildBrush->AddFace( va, vb, vc, faceData ); - // slow, dont use much -} - -DBrush* DEntity::GetBrushForID( int ID ){ - DBrush* buildBrush = NULL; - - for ( DBrush *brush : brushList ) - { - if ( brush->m_nBrushID == ID ) { - buildBrush = brush; - break; - } - } - - if ( !buildBrush ) { - buildBrush = NewBrush( ID ); - } - - return buildBrush; -} - template class BrushSelectedVisitor : public SelectionSystem::Visitor { @@ -236,8 +205,7 @@ inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){ } void DEntity_loadBrush( DEntity& entity, scene::Instance& brush ){ - DBrush* loadBrush = entity.NewBrush( static_cast( entity.brushList.size() ) ); - loadBrush->LoadFromBrush( brush, true ); + entity.NewBrush()->LoadFromBrush( brush, true ); } typedef ReferenceCaller1 DEntityLoadBrushCaller; @@ -282,22 +250,19 @@ void DEntity::LoadSelectedPatches(){ } bool* DEntity::BuildIntersectList(){ - int max = GetIDMax(); - if ( max == 0 ) { - return NULL; + if ( brushList.empty() ) { + return nullptr; } - bool* pbIntList = new bool[max]; - memset( pbIntList, 0, sizeof( bool ) * ( max ) ); + bool* pbIntList = new bool[brushList.size()] (); // () zero initialize - for ( std::list::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ ) + for ( size_t i = 0; i < brushList.size(); ++i ) { - std::list::const_iterator pB2 = pB1; - for ( pB2++; pB2 != brushList.end(); pB2++ ) + for ( size_t j = i + 1; j < brushList.size(); ++j ) { - if ( ( *pB1 )->IntersectsWith( ( *pB2 ) ) ) { - pbIntList[( *pB1 )->m_nBrushID] = true; - pbIntList[( *pB2 )->m_nBrushID] = true; + if ( brushList[i]->IntersectsWith( brushList[j] ) ) { + pbIntList[i] = true; + pbIntList[j] = true; } } } @@ -306,22 +271,19 @@ bool* DEntity::BuildIntersectList(){ } bool* DEntity::BuildDuplicateList(){ - int max = GetIDMax(); - if ( max == 0 ) { - return NULL; + if ( brushList.empty() ) { + return nullptr; } - bool* pbDupList = new bool[max]; - memset( pbDupList, 0, sizeof( bool ) * ( max ) ); + bool* pbDupList = new bool[brushList.size()] (); // () zero initialize - for ( std::list::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ ) + for ( size_t i = 0; i < brushList.size(); ++i ) { - std::list::const_iterator pB2 = pB1; - for ( pB2++; pB2 != brushList.end(); pB2++ ) + for ( size_t j = i + 1; j < brushList.size(); ++j ) { - if ( **pB1 == *pB2 ) { - pbDupList[( *pB1 )->m_nBrushID] = true; - pbDupList[( *pB2 )->m_nBrushID] = true; + if ( brushList[i]->operator==( brushList[j] ) ) { + pbDupList[i] = true; + pbDupList[j] = true; } } } @@ -339,10 +301,10 @@ void DEntity::SelectBrushes( bool *selectList ){ scene::Path path( NodeReference( GlobalSceneGraph().root() ) ); path.push( NodeReference( *QER_Entity ) ); - for ( DBrush *brush : brushList ) + for ( size_t i = 0; i < brushList.size(); ++i ) { - if ( selectList[brush->m_nBrushID] ) { - path.push( NodeReference( *brush->QER_brush ) ); + if ( selectList[i] ) { + path.push( NodeReference( *brushList[i]->QER_brush ) ); Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true ); path.pop(); } @@ -383,10 +345,9 @@ bool DEntity::LoadFromEntity( scene::Node& ent, bool bLoadPatches ) { class load_brushes_t : public scene::Traversable::Walker { DEntity* m_entity; - mutable int m_count; public: load_brushes_t( DEntity* entity ) - : m_entity( entity ), m_count( 0 ){ + : m_entity( entity ){ } bool pre( scene::Node& node ) const { scene::Path path( NodeReference( GlobalSceneGraph().root() ) ); @@ -396,12 +357,10 @@ bool DEntity::LoadFromEntity( scene::Node& ent, bool bLoadPatches ) { ASSERT_MESSAGE( instance != 0, "" ); if ( Node_isPatch( node ) ) { - DPatch* loadPatch = m_entity->NewPatch(); - loadPatch->LoadFromPatch( *instance ); + m_entity->NewPatch()->LoadFromPatch( *instance ); } else if ( Node_isBrush( node ) ) { - DBrush* loadBrush = m_entity->NewBrush( m_count++ ); - loadBrush->LoadFromBrush( *instance, true ); + m_entity->NewBrush()->LoadFromBrush( *instance, true ); } return false; } @@ -414,33 +373,14 @@ bool DEntity::LoadFromEntity( scene::Node& ent, bool bLoadPatches ) { } void DEntity::RemoveNonCheckBrushes( std::list* exclusionList, bool useDetail ){ - std::list::iterator chkBrush = brushList.begin(); - - while ( chkBrush != brushList.end() ) - { - if ( !useDetail ) { - if ( ( *chkBrush )->IsDetail() ) { - delete *chkBrush; - chkBrush = brushList.erase( chkBrush ); - continue; - } + brushList.erase( std::remove_if( brushList.begin(), brushList.end(), [&]( DBrush *brush ){ + if ( ( !useDetail && brush->IsDetail() ) + || std::any_of( exclusionList->cbegin(), exclusionList->cend(), [brush]( const Str& tex ){ return brush->HasTexture( tex.GetBuffer() ); } ) ) { + delete brush; + return true; } - - std::list::iterator eTexture; - - for ( eTexture = exclusionList->begin(); eTexture != exclusionList->end(); eTexture++ ) - { - if ( ( *chkBrush )->HasTexture( ( *eTexture ).GetBuffer() ) ) { - delete *chkBrush; - chkBrush = brushList.erase( chkBrush ); - break; - } - } - - if ( eTexture == exclusionList->end() ) { - chkBrush++; - } - } + return false; + } ), brushList.end() ); } void DEntity::ResetChecks( std::list* exclusionList ){ @@ -494,16 +434,6 @@ void DEntity::BuildInRadiant( bool allowDestruction ){ -int DEntity::GetIDMax( void ) { - int max = -1; - for ( const DBrush *brush : brushList ) { - if ( brush->m_nBrushID > max ) { - max = brush->m_nBrushID; - } - } - return max + 1; -} - void DEntity::SetClassname( const char *classname ) { m_Classname = classname; } diff --git a/contrib/bobtoolz/DEntity.h b/contrib/bobtoolz/DEntity.h index 6501848c..6d6c891d 100644 --- a/contrib/bobtoolz/DEntity.h +++ b/contrib/bobtoolz/DEntity.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include "str.h" #include "mathlib.h" @@ -62,13 +63,11 @@ public: bool ResetTextures( const char* textureName, float fScale[2], float fShift[2], int rotation, const char* newTextureName, bool bResetTextureName, bool bResetScale[2], bool bResetShift[2], bool bResetRotation, bool rebuild ); void SaveToFile( FILE* pFile ); void SetClassname( const char* classname ); - int GetIDMax(); void BuildInRadiant( bool allowDestruction ); void ResetChecks( std::list* exclusionList ); void RemoveNonCheckBrushes( std::list* exclusionList, bool useDetail ); - DPlane* AddFaceToBrush( vec3_t va, vec3_t vb, vec3_t vc, _QERFaceData* faceData, int ID ); // slow, try not to use much int GetBrushCount( void ); DBrush* FindBrushByPointer( scene::Node& brush ); // --------------------------------------------- @@ -84,8 +83,7 @@ public: // brush operations void ClearBrushes(); // clears brush list and frees memory for brushes - DBrush* GetBrushForID( int ID ); - DBrush* NewBrush( int ID = -1 ); + DBrush* NewBrush(); // --------------------------------------------- // patch operations @@ -96,9 +94,9 @@ public: // vars std::list epairList; - std::list brushList; + std::vector brushList; // new patches, wahey!!! - std::list patchList; + std::vector patchList; Str m_Classname; // --------------------------------------------- diff --git a/contrib/bobtoolz/DShape.cpp b/contrib/bobtoolz/DShape.cpp index 0a91c410..89185126 100644 --- a/contrib/bobtoolz/DShape.cpp +++ b/contrib/bobtoolz/DShape.cpp @@ -44,7 +44,6 @@ bool bFacesAll[6] = {true, true, true, true, true, true}; DShape::DShape(){ - m_nNextBrush = 0; } DShape::~DShape(){ @@ -95,7 +94,7 @@ void DShape::BuildRegularPrism( vec3_t min, vec3_t max, int nSides, bool bAlignT //---------------------------------- - DBrush* pB = m_Container.GetWorldSpawn()->NewBrush( m_nNextBrush++ ); + DBrush* pB = m_Container.GetWorldSpawn()->NewBrush(); for ( i = 1; i <= nSides; i++ ) pB->AddFace( vc[i - 1], vc[i], vd[i], GetCurrentTexture(), false ); @@ -308,10 +307,10 @@ DBrush* DShape::GetBoundingCube_Ext( vec3_t min, vec3_t max, const char *texture DBrush* DShape::GetBoundingCube( vec3_t min, vec3_t max, const char *textureName, DEntity* ent, bool* bUseFaces ){ DBrush* pB; if ( ent == NULL ) { - pB = m_Container.GetWorldSpawn()->NewBrush( m_nNextBrush++ ); + pB = m_Container.GetWorldSpawn()->NewBrush(); } else{ - pB = ent->NewBrush( m_nNextBrush++ ); + pB = ent->NewBrush(); } //----- Build Outer Bounds --------- diff --git a/contrib/bobtoolz/DTreePlanter.cpp b/contrib/bobtoolz/DTreePlanter.cpp index 354a23fb..b3993d24 100644 --- a/contrib/bobtoolz/DTreePlanter.cpp +++ b/contrib/bobtoolz/DTreePlanter.cpp @@ -148,11 +148,8 @@ bool DTreePlanter::FindDropPoint( vec3_t in, vec3_t out ) { bool found = false; vec3_t temp; vec_t dist; - int cnt = m_world.GetIDMax(); - for ( int i = 0; i < cnt; i++ ) { - DBrush* pBrush = m_world.GetBrushForID( i ); - - if ( pBrush->IntersectsWith( &p1, &p2, temp ) ) { + for ( auto *brush : m_world.brushList ) { + if ( brush->IntersectsWith( &p1, &p2, temp ) ) { vec3_t diff; vec_t tempdist; VectorSubtract( in, temp, diff );