]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DEntity.cpp
22aa05a18a64de40d516d217c5621238a6527f6c
[xonotic/netradiant.git] / contrib / bobtoolz / DEntity.cpp
1 /*
2    BobToolz plugin for GtkRadiant
3    Copyright (C) 2001 Gordon Biggans
4
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.
9
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.
14
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
18  */
19
20 // DEntity.cpp: implementation of the DEntity class.
21 //
22 //////////////////////////////////////////////////////////////////////
23
24 #include "DEntity.h"
25 #include "globaldefs.h"
26
27 #if GDEF_COMPILER_MSVC
28 #pragma warning(disable : 4786)
29 #endif
30
31 #include <list>
32 #include "str.h"
33
34 #include "DPoint.h"
35 #include "DPlane.h"
36 #include "DBrush.h"
37 #include "DEPair.h"
38 #include "DPatch.h"
39
40 #include "dialogs/dialogs-gtk.h"
41 #include "misc.h"
42 #include "CPortals.h"
43
44 #include "iundo.h"
45 #include "ientity.h"
46 #include "ieclass.h"
47
48 #include "generic/referencecounted.h"
49
50 #include <vector>
51 #include <list>
52 #include <map>
53 #include <algorithm>
54
55 #include "scenelib.h"
56
57
58 const char* brushEntityList[] = {
59         "worldspawn",
60         "trigger_always",
61         "trigger_hurt",
62         "trigger_multiple",
63         "trigger_push",
64         "trigger_teleport",
65         "func_bobbing",
66         "func_button",
67         "func_door",
68         "func_group",
69         "func_pendulum",
70         "func_plat",
71         "func_rotating",
72         "func_static",
73         "func_timer",
74         "func_train",
75         0
76 };
77
78 //////////////////////////////////////////////////////////////////////
79 // Construction/Destruction
80 //////////////////////////////////////////////////////////////////////
81
82 DEntity::DEntity( const char *classname, int ID ){
83         SetClassname( classname );
84         m_nID = ID;
85         QER_Entity = NULL;
86 }
87
88 DEntity::~DEntity(){
89         ClearPatches();
90         ClearBrushes();
91         ClearEPairs();
92 }
93
94 //////////////////////////////////////////////////////////////////////
95 // Implementation
96 //////////////////////////////////////////////////////////////////////
97
98 void DEntity::ClearBrushes(){
99         for ( std::list<DBrush *>::const_iterator deadBrush = brushList.begin(); deadBrush != brushList.end(); deadBrush++ )
100         {
101                 delete *deadBrush;
102         }
103         brushList.clear();
104 }
105
106 void DEntity::ClearPatches(){
107         for ( std::list<DPatch *>::const_iterator deadPatch = patchList.begin(); deadPatch != patchList.end(); deadPatch++ )
108         {
109                 delete *deadPatch;
110         }
111         patchList.clear();
112 }
113
114 DPatch* DEntity::NewPatch(){
115         DPatch* newPatch = new DPatch;
116
117         patchList.push_back( newPatch );
118
119         return newPatch;
120 }
121
122 DBrush* DEntity::NewBrush( int ID ){
123         DBrush* newBrush = new DBrush( ID );
124
125         brushList.push_back( newBrush );
126
127         return newBrush;
128 }
129
130 char* getNextBracket( char* s ){
131         char* p = s;
132         while ( *p )
133         {
134                 p++;
135                 if ( *p == '(' ) {
136                         break;
137                 }
138         }
139
140         return p;
141 }
142
143 bool DEntity::LoadFromPrt( char *filename ){
144         CPortals portals;
145         strcpy( portals.fn, filename );
146         portals.Load();
147
148         if ( portals.node_count == 0 ) {
149                 return false;
150         }
151
152         ClearBrushes();
153         ClearEPairs();
154
155         bool build = false;
156         for ( unsigned int i = 0; i < portals.node_count; i++ )
157         {
158                 build = false;
159                 DBrush* brush = NewBrush();
160
161                 for ( unsigned int j = 0; j < portals.node[i].portal_count; j++ )
162                 {
163                         for ( unsigned int k = 0; k < portals.node[i].portal[j].point_count - 2; k++ )
164                         {
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 );
170
171                                 if ( k == 0 ) {
172                                         VectorCopy( v2, normal );
173                                 }
174                                 else
175                                 {
176                                         VectorSubtract( v2, normal, v1 );
177                                         if ( VectorLength( v1 ) > 0.01 ) {
178                                                 build = true;
179                                                 break;
180                                         }
181                                 }
182                         }
183
184                         if ( !build ) {
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 );
186                         }
187                         else{
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 );
189                         }
190                 }
191                 if ( build ) {
192                         brush->BuildInRadiant( false, NULL );
193                 }
194         }
195
196         return true;
197 }
198
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
203 }
204
205 DBrush* DEntity::GetBrushForID( int ID ){
206         DBrush* buildBrush = NULL;
207
208         for ( std::list<DBrush *>::const_iterator chkBrush = brushList.begin(); chkBrush != brushList.end(); chkBrush++ )
209         {
210                 if ( ( *chkBrush )->m_nBrushID == ID ) {
211                         buildBrush = ( *chkBrush );
212                         break;
213                 }
214         }
215
216         if ( !buildBrush ) {
217                 buildBrush = NewBrush( ID );
218         }
219
220         return buildBrush;
221 }
222
223 template<typename Functor>
224 class BrushSelectedVisitor : public SelectionSystem::Visitor
225 {
226 const Functor& m_functor;
227 public:
228 BrushSelectedVisitor( const Functor& functor ) : m_functor( functor ){
229 }
230 void visit( scene::Instance& instance ) const {
231         if ( Node_isBrush( instance.path().top() ) ) {
232                 m_functor( instance );
233         }
234 }
235 };
236
237 template<typename Functor>
238 inline const Functor& Scene_forEachSelectedBrush( const Functor& functor ){
239         GlobalSelectionSystem().foreachSelected( BrushSelectedVisitor<Functor>( functor ) );
240         return functor;
241 }
242
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 );
246 }
247 typedef ReferenceCaller<DEntity, void(scene::Instance&), DEntity_loadBrush> DEntityLoadBrushCaller;
248
249 void DEntity::LoadSelectedBrushes(){
250         ClearBrushes();
251         ClearEPairs();
252
253         Scene_forEachSelectedBrush( DEntityLoadBrushCaller( *this ) );
254 }
255
256 template<typename Functor>
257 class PatchSelectedVisitor : public SelectionSystem::Visitor
258 {
259 const Functor& m_functor;
260 public:
261 PatchSelectedVisitor( const Functor& functor ) : m_functor( functor ){
262 }
263 void visit( scene::Instance& instance ) const {
264         if ( Node_isPatch( instance.path().top() ) ) {
265                 m_functor( instance );
266         }
267 }
268 };
269
270 template<typename Functor>
271 inline const Functor& Scene_forEachSelectedPatch( const Functor& functor ){
272         GlobalSelectionSystem().foreachSelected( PatchSelectedVisitor<Functor>( functor ) );
273         return functor;
274 }
275
276 void DEntity_loadPatch( DEntity& entity, scene::Instance& patch ){
277         DPatch* loadPatch = entity.NewPatch();
278         loadPatch->LoadFromPatch( patch );
279 }
280 typedef ReferenceCaller<DEntity, void(scene::Instance&), DEntity_loadPatch> DEntityLoadPatchCaller;
281
282 void DEntity::LoadSelectedPatches(){
283         ClearPatches();
284         ClearEPairs();
285
286         Scene_forEachSelectedPatch( DEntityLoadPatchCaller( *this ) );
287 }
288
289 bool* DEntity::BuildIntersectList(){
290         int max = GetIDMax();
291         if ( max == 0 ) {
292                 return NULL;
293         }
294
295         bool* pbIntList = new bool[max];
296         memset( pbIntList, 0, sizeof( bool ) * ( max ) );
297
298         for ( std::list<DBrush *>::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ )
299         {
300                 std::list<DBrush *>::const_iterator pB2 = pB1;
301                 for ( pB2++; pB2 != brushList.end(); pB2++ )
302                 {
303                         if ( ( *pB1 )->IntersectsWith( ( *pB2 ) ) ) {
304                                 pbIntList[( *pB1 )->m_nBrushID] = true;
305                                 pbIntList[( *pB2 )->m_nBrushID] = true;
306                         }
307                 }
308         }
309
310         return pbIntList;
311 }
312
313 bool* DEntity::BuildDuplicateList(){
314         int max = GetIDMax();
315         if ( max == 0 ) {
316                 return NULL;
317         }
318
319         bool* pbDupList = new bool[max];
320         memset( pbDupList, 0, sizeof( bool ) * ( max ) );
321
322         for ( std::list<DBrush *>::const_iterator pB1 = brushList.begin(); pB1 != brushList.end(); pB1++ )
323         {
324                 std::list<DBrush *>::const_iterator pB2 = pB1;
325                 for ( pB2++; pB2 != brushList.end(); pB2++ )
326                 {
327                         if ( **pB1 == *pB2 ) {
328                                 pbDupList[( *pB1 )->m_nBrushID] = true;
329                                 pbDupList[( *pB2 )->m_nBrushID] = true;
330                         }
331                 }
332         }
333
334         return pbDupList;
335 }
336
337 void DEntity::SelectBrushes( bool *selectList ){
338         if ( selectList == NULL ) {
339                 return;
340         }
341
342         GlobalSelectionSystem().setSelectedAll( false );
343
344         scene::Path path( NodeReference( GlobalSceneGraph().root() ) );
345         path.push( NodeReference( *QER_Entity ) );
346
347         for ( std::list<DBrush *>::const_iterator pBrush = brushList.begin(); pBrush != brushList.end(); pBrush++ )
348         {
349                 if ( selectList[( *pBrush )->m_nBrushID] ) {
350                         path.push( NodeReference( *( *pBrush )->QER_brush ) );
351                         Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true );
352                         path.pop();
353                 }
354         }
355 }
356
357 bool DEntity::LoadFromEntity( scene::Node& ent, bool bLoadPatches ) {
358         ClearPatches();
359         ClearBrushes();
360         ClearEPairs();
361
362         QER_Entity = &ent;
363
364         LoadEPairList( Node_getEntity( ent ) );
365
366         bool keep = false;
367         int i;
368         for ( i = 0; brushEntityList[i]; i++ )
369         {
370                 if ( string_equal_nocase( brushEntityList[i], m_Classname ) ) {
371                         keep = true;
372                         break;
373                 }
374         }
375
376         if ( !keep ) {
377                 return false;
378         }
379
380         if ( Node_getTraversable( ent ) ) {
381                 class load_brushes_t : public scene::Traversable::Walker
382                 {
383                 DEntity* m_entity;
384                 mutable int m_count;
385 public:
386                 load_brushes_t( DEntity* entity )
387                         : m_entity( entity ), m_count( 0 ){
388                 }
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, "" );
395
396                         if ( Node_isPatch( node ) ) {
397                                 DPatch* loadPatch = m_entity->NewPatch();
398                                 loadPatch->LoadFromPatch( *instance );
399                         }
400                         else if ( Node_isBrush( node ) ) {
401                                 DBrush* loadBrush = m_entity->NewBrush( m_count++ );
402                                 loadBrush->LoadFromBrush( *instance, true );
403                         }
404                         return false;
405                 }
406                 } load_brushes( this );
407
408                 Node_getTraversable( ent )->traverse( load_brushes );
409         }
410
411         return true;
412 }
413
414 void DEntity::RemoveNonCheckBrushes( std::list<Str>* exclusionList, bool useDetail ){
415         std::list<DBrush *>::iterator chkBrush = brushList.begin();
416
417         while ( chkBrush != brushList.end() )
418         {
419                 if ( !useDetail ) {
420                         if ( ( *chkBrush )->IsDetail() ) {
421                                 delete *chkBrush;
422                                 chkBrush = brushList.erase( chkBrush );
423                                 continue;
424                         }
425                 }
426
427                 std::list<Str>::iterator eTexture;
428
429                 for ( eTexture = exclusionList->begin(); eTexture != exclusionList->end(); eTexture++ )
430                 {
431                         if ( ( *chkBrush )->HasTexture( ( *eTexture ).GetBuffer() ) ) {
432                                 delete *chkBrush;
433                                 chkBrush = brushList.erase( chkBrush );
434                                 break;
435                         }
436                 }
437
438                 if ( eTexture == exclusionList->end() ) {
439                         chkBrush++;
440                 }
441         }
442 }
443
444 void DEntity::ResetChecks( std::list<Str>* exclusionList ){
445         for ( std::list<DBrush *>::const_iterator resetBrush = brushList.begin(); resetBrush != brushList.end(); resetBrush++ )
446         {
447                 ( *resetBrush )->ResetChecks( exclusionList );
448         }
449 }
450
451 int DEntity::FixBrushes(){
452         int count = 0;
453
454         for ( std::list<DBrush *>::const_iterator fixBrush = brushList.begin(); fixBrush != brushList.end(); fixBrush++ )
455         {
456                 count += ( *fixBrush )->RemoveRedundantPlanes();
457         }
458
459         return count;
460 }
461
462 void DEntity::BuildInRadiant( bool allowDestruction ){
463         bool makeEntity = strcmp( m_Classname, "worldspawn" ) ? true : false;
464
465         if ( makeEntity ) {
466                 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_Classname.GetBuffer(), !brushList.empty() || !patchList.empty() ) ) );
467
468                 for ( std::list<DEPair* >::const_iterator buildEPair = epairList.begin(); buildEPair != epairList.end(); buildEPair++ )
469                 {
470                         Node_getEntity( node )->setKeyValue( ( *buildEPair )->key, ( *buildEPair )->value );
471                 }
472
473                 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
474
475                 for ( std::list<DBrush *>::const_iterator buildBrush = brushList.begin(); buildBrush != brushList.end(); buildBrush++ )
476                         ( *buildBrush )->BuildInRadiant( allowDestruction, NULL, node.get_pointer() );
477
478                 for ( std::list<DPatch *>::const_iterator buildPatch = patchList.begin(); buildPatch != patchList.end(); buildPatch++ )
479                         ( *buildPatch )->BuildInRadiant( node.get_pointer() );
480
481                 QER_Entity = node.get_pointer();
482         }
483         else
484         {
485                 for ( std::list<DBrush *>::const_iterator buildBrush = brushList.begin(); buildBrush != brushList.end(); buildBrush++ )
486                         ( *buildBrush )->BuildInRadiant( allowDestruction, NULL );
487
488                 for ( std::list<DPatch *>::const_iterator buildPatch = patchList.begin(); buildPatch != patchList.end(); buildPatch++ )
489                         ( *buildPatch )->BuildInRadiant();
490         }
491 }
492
493
494
495 int DEntity::GetIDMax( void ) {
496         int max = -1;
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;
500                 }
501         }
502         return max + 1;
503 }
504
505 void DEntity::SetClassname( const char *classname ) {
506         m_Classname = classname;
507 }
508
509 void DEntity::SaveToFile( FILE *pFile ){
510         fprintf( pFile, "{\n" );
511
512         fprintf( pFile, "\"classname\" \"%s\"\n", (const char *)m_Classname );
513
514         for ( std::list<DEPair *>::const_iterator ep = epairList.begin(); ep != epairList.end(); ep++ )
515         {
516                 fprintf( pFile, "\"%s\" \"%s\"\n", (const char *)( *ep )->key, (const char *)( *ep )->value );
517         }
518
519         for ( std::list<DBrush *>::const_iterator bp = brushList.begin(); bp != brushList.end(); bp++ )
520         {
521                 ( *bp )->SaveToFile( pFile );
522         }
523
524         fprintf( pFile, "}\n" );
525 }
526
527 void DEntity::ClearEPairs(){
528         for ( std::list<DEPair *>::const_iterator deadEPair = epairList.begin(); deadEPair != epairList.end(); deadEPair++ )
529         {
530                 delete ( *deadEPair );
531         }
532         epairList.clear();
533 }
534
535 void DEntity::AddEPair( const char *key, const char *value ) {
536         DEPair* newEPair;
537         newEPair = FindEPairByKey( key );
538         if ( !newEPair ) {
539                 newEPair = new DEPair;
540                 newEPair->Build( key, value );
541                 epairList.push_back( newEPair );
542         }
543         else {
544                 newEPair->Build( key, value );
545         }
546 }
547
548 void DEntity::LoadEPairList( Entity *epl ){
549         class load_epairs_t : public Entity::Visitor
550         {
551         DEntity* m_entity;
552 public:
553         load_epairs_t( DEntity* entity )
554                 : m_entity( entity ){
555         }
556         void visit( const char* key, const char* value ){
557                 if ( strcmp( key, "classname" ) == 0 ) {
558                         m_entity->SetClassname( value );
559                 }
560                 else{
561                         m_entity->AddEPair( key, value );
562                 }
563         }
564
565         } load_epairs( this );
566
567         epl->forEachKeyValue( load_epairs );
568 }
569
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 ){
572         bool reset = false;
573
574         for ( std::list<DBrush *>::const_iterator resetBrush = brushList.begin(); resetBrush != brushList.end(); resetBrush++ )
575         {
576                 bool tmp = ( *resetBrush )->ResetTextures( textureName,        fScale,       fShift,       rotation, newTextureName,
577                                                                                                    bResetTextureName,  bResetScale,  bResetShift,  bResetRotation );
578
579                 if ( tmp ) {
580                         reset = true;
581                         if ( rebuild ) {
582                                 Node_getTraversable( *( *resetBrush )->QER_entity )->erase( *( *resetBrush )->QER_brush );
583                                 ( *resetBrush )->BuildInRadiant( false, NULL, ( *resetBrush )->QER_entity );
584                         }
585                 }
586         }
587
588         if ( bResetTextureName ) {
589                 for ( std::list<DPatch *>::const_iterator resetPatch = patchList.begin(); resetPatch != patchList.end(); resetPatch++ )
590                 {
591                         bool tmp = ( *resetPatch )->ResetTextures( textureName, newTextureName );
592
593                         if ( tmp ) {
594                                 reset = true;
595                                 if ( rebuild ) {
596                                         Node_getTraversable( *( *resetPatch )->QER_entity )->erase( *( *resetPatch )->QER_brush );
597                                         ( *resetPatch )->BuildInRadiant( ( *resetPatch )->QER_entity );
598                                 }
599                         }
600                 }
601         }
602
603         return reset;
604 }
605
606 DEPair* DEntity::FindEPairByKey( const char* keyname ){
607         for ( std::list<DEPair *>::const_iterator ep = epairList.begin(); ep != epairList.end(); ep++ )
608         {
609                 char* c = ( *ep )->key;
610                 if ( !strcmp( c, keyname ) ) {
611                         return *ep;
612                 }
613         }
614         return NULL;
615 }
616
617 void DEntity::RemoveFromRadiant(){
618         Node_getTraversable( GlobalSceneGraph().root() )->erase( *QER_Entity );
619
620         QER_Entity = NULL;
621 }
622
623 void DEntity::SpawnString( const char* key, const char* defaultstring, const char** out ){
624         DEPair* pEP = FindEPairByKey( key );
625         if ( pEP ) {
626                 *out = pEP->value;
627         }
628         else {
629                 *out = defaultstring;
630         }
631 }
632
633 void DEntity::SpawnInt( const char* key, const char* defaultstring, int* out ){
634         DEPair* pEP = FindEPairByKey( key );
635         if ( pEP ) {
636                 *out = atoi( pEP->value );
637         }
638         else {
639                 *out = atoi( defaultstring );
640         }
641 }
642
643 void DEntity::SpawnFloat( const char* key, const char* defaultstring, float* out ){
644         DEPair* pEP = FindEPairByKey( key );
645         if ( pEP ) {
646                 *out = static_cast<float>( atof( pEP->value ) );
647         }
648         else {
649                 *out = static_cast<float>( atof( defaultstring ) );
650         }
651 }
652
653 void DEntity::SpawnVector( const char* key, const char* defaultstring, vec_t* out ){
654         DEPair* pEP = FindEPairByKey( key );
655         if ( pEP ) {
656                 sscanf( pEP->value, "%f %f %f", &out[0], &out[1], &out[2] );
657         }
658         else {
659                 sscanf( defaultstring, "%f %f %f", &out[0], &out[1], &out[2] );
660         }
661 }
662
663 int DEntity::GetBrushCount( void ) {
664         return static_cast<int>( brushList.size() );
665 }
666
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 ) {
671                         return pBrush;
672                 }
673         }
674         return NULL;
675 }