]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/bobtoolz/DMap.cpp
Merge branch 'fix-fast' into 'master'
[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 "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 {
54     m_nNextEntity = 1;
55     AddEntity("worldspawn", 0);
56 }
57
58 DMap::~DMap()
59 {
60     ClearEntities();
61 }
62
63 DEntity *DMap::AddEntity(const char *classname, int ID)
64 {
65     DEntity *newEntity;
66     if (ID == -1) {
67         newEntity = new DEntity(classname, m_nNextEntity++);
68     } else {
69         newEntity = new DEntity(classname, ID);
70     }
71
72     entityList.push_back(newEntity);
73
74     return newEntity;
75 }
76
77 void DMap::ClearEntities()
78 {
79     m_nNextEntity = 1;
80
81     for (std::list<DEntity *>::const_iterator deadEntity = entityList.begin();
82          deadEntity != entityList.end(); deadEntity++) {
83              delete *deadEntity;
84     }
85
86     entityList.clear();
87 }
88
89 DEntity *DMap::GetEntityForID(int ID)
90 {
91     DEntity *findEntity = NULL;
92
93     for (std::list<DEntity *>::const_iterator chkEntity = entityList.begin();
94          chkEntity != entityList.end(); chkEntity++) {
95         if ((*chkEntity)->m_nID == ID) {
96             findEntity = (*chkEntity);
97             break;
98         }
99     }
100
101     if (!findEntity) {
102         findEntity = AddEntity("worldspawn", ID);
103     }
104
105     return findEntity;
106 }
107
108
109 DEntity *DMap::GetWorldSpawn()
110 {
111     return GetEntityForID(0);
112 }
113
114 void DMap::BuildInRadiant(bool bAllowDestruction)
115 {
116     for (std::list<DEntity *>::const_iterator buildEntity = entityList.begin();
117          buildEntity != entityList.end(); buildEntity++) {
118              (*buildEntity)->BuildInRadiant(bAllowDestruction);
119     }
120 }
121
122 void DMap::LoadAll(bool bLoadPatches)
123 {
124     ClearEntities();
125
126     GlobalSelectionSystem().setSelectedAll(false);
127
128     class load_entities_t : public scene::Traversable::Walker {
129         DMap *m_map;
130         bool m_bLoadPatches;
131     public:
132         load_entities_t(DMap *map, bool bLoadPatches)
133                 : m_map(map), m_bLoadPatches(bLoadPatches)
134         {
135         }
136
137         bool pre(scene::Node &node) const
138         {
139             if (Node_isEntity(node)) {
140                 DEntity *loadEntity = m_map->AddEntity("", 0);
141                 loadEntity->LoadFromEntity(node, m_bLoadPatches);
142             }
143             return false;
144         }
145     } load_entities(this, bLoadPatches);
146
147     Node_getTraversable(GlobalSceneGraph().root())->traverse(load_entities);
148 }
149
150 int DMap::FixBrushes()
151 {
152     int count = 0;
153     for (std::list<DEntity *>::const_iterator fixEntity = entityList.begin();
154          fixEntity != entityList.end(); fixEntity++) {
155         count += (*fixEntity)->FixBrushes();
156     }
157
158     return count;
159 }
160
161 void
162 DMap::ResetTextures(const char *textureName, float fScale[2], float fShift[2], int rotation, const char *newTextureName,
163                     int bResetTextureName, int bResetScale[2], int bResetShift[2], int bResetRotation)
164 {
165     for (std::list<DEntity *>::const_iterator texEntity = entityList.begin();
166          texEntity != entityList.end(); texEntity++) {
167         if (string_equal_nocase("worldspawn", (*texEntity)->m_Classname)) {
168             (*texEntity)->ResetTextures(textureName, fScale, fShift, rotation, newTextureName,
169                                         bResetTextureName, bResetScale, bResetShift, bResetRotation, true);
170         } else {
171             if ((*texEntity)->ResetTextures(textureName, fScale, fShift, rotation, newTextureName,
172                                             bResetTextureName, bResetScale, bResetShift, bResetRotation, false)) {
173                 RebuildEntity(*texEntity);
174             }
175         }
176     }
177 }
178
179 void DMap::RebuildEntity(DEntity *ent)
180 {
181     ent->RemoveFromRadiant();
182     ent->BuildInRadiant(false);
183 }