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