| 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 | // DMap.cpp: implementation of the DMap class. |
| 21 | // |
| 22 | ////////////////////////////////////////////////////////////////////// |
| 23 | |
| 24 | #include "DMap.h" |
| 25 | |
| 26 | #include "str.h" |
| 27 | #include <list> |
| 28 | |
| 29 | #include "DPoint.h" |
| 30 | #include "DPlane.h" |
| 31 | #include "DBrush.h" |
| 32 | #include "DEPair.h" |
| 33 | #include "DPatch.h" |
| 34 | #include "DEntity.h" |
| 35 | |
| 36 | #include "iundo.h" |
| 37 | |
| 38 | #include "generic/referencecounted.h" |
| 39 | |
| 40 | #include <vector> |
| 41 | #include <list> |
| 42 | #include <map> |
| 43 | #include <algorithm> |
| 44 | |
| 45 | #include "scenelib.h" |
| 46 | |
| 47 | |
| 48 | ////////////////////////////////////////////////////////////////////// |
| 49 | // Construction/Destruction |
| 50 | ////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | DMap::DMap(){ |
| 53 | m_nNextEntity = 1; |
| 54 | AddEntity( "worldspawn", 0 ); |
| 55 | } |
| 56 | |
| 57 | DMap::~DMap(){ |
| 58 | ClearEntities(); |
| 59 | } |
| 60 | |
| 61 | DEntity* DMap::AddEntity( const char *classname, int ID ){ |
| 62 | DEntity* newEntity; |
| 63 | if ( ID == -1 ) { |
| 64 | newEntity = new DEntity( classname, m_nNextEntity++ ); |
| 65 | } |
| 66 | else{ |
| 67 | newEntity = new DEntity( classname, ID ); |
| 68 | } |
| 69 | |
| 70 | entityList.push_back( newEntity ); |
| 71 | |
| 72 | return newEntity; |
| 73 | } |
| 74 | |
| 75 | void DMap::ClearEntities(){ |
| 76 | m_nNextEntity = 1; |
| 77 | |
| 78 | for ( std::list<DEntity *>::const_iterator deadEntity = entityList.begin(); deadEntity != entityList.end(); deadEntity++ ) |
| 79 | delete *deadEntity; |
| 80 | |
| 81 | entityList.clear(); |
| 82 | } |
| 83 | |
| 84 | DEntity* DMap::GetEntityForID( int ID ){ |
| 85 | DEntity* findEntity = NULL; |
| 86 | |
| 87 | for ( std::list<DEntity *>::const_iterator chkEntity = entityList.begin(); chkEntity != entityList.end(); chkEntity++ ) |
| 88 | { |
| 89 | if ( ( *chkEntity )->m_nID == ID ) { |
| 90 | findEntity = ( *chkEntity ); |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if ( !findEntity ) { |
| 96 | findEntity = AddEntity( "worldspawn", ID ); |
| 97 | } |
| 98 | |
| 99 | return findEntity; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | DEntity* DMap::GetWorldSpawn(){ |
| 104 | return GetEntityForID( 0 ); |
| 105 | } |
| 106 | |
| 107 | void DMap::BuildInRadiant( bool bAllowDestruction ){ |
| 108 | for ( std::list<DEntity *>::const_iterator buildEntity = entityList.begin(); buildEntity != entityList.end(); buildEntity++ ) |
| 109 | ( *buildEntity )->BuildInRadiant( bAllowDestruction ); |
| 110 | } |
| 111 | |
| 112 | void DMap::LoadAll( bool bLoadPatches ){ |
| 113 | ClearEntities(); |
| 114 | |
| 115 | GlobalSelectionSystem().setSelectedAll( false ); |
| 116 | |
| 117 | class load_entities_t : public scene::Traversable::Walker |
| 118 | { |
| 119 | DMap* m_map; |
| 120 | bool m_bLoadPatches; |
| 121 | public: |
| 122 | load_entities_t( DMap* map, bool bLoadPatches ) |
| 123 | : m_map( map ), m_bLoadPatches( bLoadPatches ){ |
| 124 | } |
| 125 | bool pre( scene::Node& node ) const { |
| 126 | if ( Node_isEntity( node ) ) { |
| 127 | DEntity* loadEntity = m_map->AddEntity( "", 0 ); |
| 128 | loadEntity->LoadFromEntity( node, m_bLoadPatches ); |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | } load_entities( this, bLoadPatches ); |
| 133 | |
| 134 | Node_getTraversable( GlobalSceneGraph().root() )->traverse( load_entities ); |
| 135 | } |
| 136 | |
| 137 | int DMap::FixBrushes(){ |
| 138 | int count = 0; |
| 139 | for ( std::list<DEntity *>::const_iterator fixEntity = entityList.begin(); fixEntity != entityList.end(); fixEntity++ ) |
| 140 | { |
| 141 | count += ( *fixEntity )->FixBrushes(); |
| 142 | } |
| 143 | |
| 144 | return count; |
| 145 | } |
| 146 | |
| 147 | void DMap::ResetTextures( const char* textureName, float fScale[2], float fShift[2], int rotation, const char* newTextureName, |
| 148 | int bResetTextureName, int bResetScale[2], int bResetShift[2], int bResetRotation ){ |
| 149 | for ( std::list<DEntity *>::const_iterator texEntity = entityList.begin(); texEntity != entityList.end(); texEntity++ ) |
| 150 | { |
| 151 | if ( string_equal_nocase( "worldspawn", ( *texEntity )->m_Classname ) ) { |
| 152 | ( *texEntity )->ResetTextures( textureName, fScale, fShift, rotation, newTextureName, |
| 153 | bResetTextureName, bResetScale, bResetShift, bResetRotation, true ); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | if ( ( *texEntity )->ResetTextures( textureName, fScale, fShift, rotation, newTextureName, |
| 158 | bResetTextureName, bResetScale, bResetShift, bResetRotation, false ) ) { |
| 159 | RebuildEntity( *texEntity ); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void DMap::RebuildEntity( DEntity *ent ){ |
| 166 | ent->RemoveFromRadiant(); |
| 167 | ent->BuildInRadiant( false ); |
| 168 | } |