transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / contrib / bobtoolz / DMap.cpp
1 /*\r
2 BobToolz plugin for GtkRadiant\r
3 Copyright (C) 2001 Gordon Biggans\r
4 \r
5 This library is free software; you can redistribute it and/or\r
6 modify it under the terms of the GNU Lesser General Public\r
7 License as published by the Free Software Foundation; either\r
8 version 2.1 of the License, or (at your option) any later version.\r
9 \r
10 This library is distributed in the hope that it will be useful,\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
13 Lesser General Public License for more details.\r
14 \r
15 You should have received a copy of the GNU Lesser General Public\r
16 License along with this library; if not, write to the Free Software\r
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
18 */\r
19 \r
20 // DMap.cpp: implementation of the DMap class.\r
21 //\r
22 //////////////////////////////////////////////////////////////////////\r
23 \r
24 #include "StdAfx.h"\r
25 #include "DMap.h"\r
26 \r
27 \r
28 //////////////////////////////////////////////////////////////////////\r
29 // Construction/Destruction\r
30 //////////////////////////////////////////////////////////////////////\r
31 \r
32 DMap::DMap()\r
33 {\r
34         m_nNextEntity = 1;\r
35         AddEntity("worldspawn", 0);\r
36 }\r
37 \r
38 DMap::~DMap()\r
39 {\r
40         ClearEntities();\r
41 }\r
42 \r
43 DEntity* DMap::AddEntity(char *classname, int ID)\r
44 {\r
45         DEntity* newEntity;\r
46         if(ID == -1)\r
47                 newEntity = new DEntity(classname, m_nNextEntity++);\r
48         else\r
49                 newEntity = new DEntity(classname, ID);\r
50 \r
51         entityList.push_back(newEntity);\r
52         \r
53         return newEntity;\r
54 }\r
55 \r
56 void DMap::ClearEntities()\r
57 {\r
58         m_nNextEntity = 1;\r
59 \r
60         for(list<DEntity *>::const_iterator deadEntity=entityList.begin(); deadEntity!=entityList.end(); deadEntity++)\r
61                 delete *deadEntity;\r
62 \r
63         entityList.clear();\r
64 }\r
65 \r
66 DEntity* DMap::GetEntityForID(int ID)\r
67 {\r
68         DEntity* findEntity = NULL;\r
69 \r
70         for(list<DEntity *>::const_iterator chkEntity=entityList.begin(); chkEntity!=entityList.end(); chkEntity++)\r
71         {\r
72                 if((*chkEntity)->m_nID == ID)\r
73                 {\r
74                         findEntity = (*chkEntity);\r
75                         break;\r
76                 }\r
77         }\r
78 \r
79         if(!findEntity)\r
80                 findEntity = AddEntity("worldspawn", ID);\r
81 \r
82         return findEntity;\r
83 }\r
84 \r
85 \r
86 DEntity* DMap::GetWorldSpawn()\r
87 {\r
88         return GetEntityForID(0);\r
89 }\r
90 \r
91 void DMap::BuildInRadiant(bool bAllowDestruction)\r
92 {\r
93         for(list<DEntity *>::const_iterator buildEntity=entityList.begin(); buildEntity!=entityList.end(); buildEntity++)\r
94                 (*buildEntity)->BuildInRadiant(bAllowDestruction);\r
95 }\r
96 \r
97 void DMap::LoadAll(bool bLoadPatches)\r
98 {\r
99         ClearEntities();\r
100 \r
101         g_FuncTable.m_pfnDeselectAllBrushes();\r
102 \r
103         int count = g_FuncTable.m_pfnGetEntityCount();\r
104 \r
105         for(int i = 0; i < count; i++)\r
106         {\r
107                 DEntity* loadEntity;\r
108 \r
109                 if(i == 0)\r
110                         loadEntity = GetWorldSpawn();\r
111                 else\r
112                         loadEntity = AddEntity("", m_nNextEntity++);\r
113 \r
114                 if(!loadEntity->LoadFromEntity(i, bLoadPatches))\r
115                 {\r
116                         delete loadEntity;\r
117                         entityList.pop_back();\r
118                 }\r
119         }\r
120 }\r
121 \r
122 int DMap::FixBrushes(bool rebuild)\r
123 {\r
124         int count = 0;\r
125         for(list<DEntity *>::const_iterator fixEntity=entityList.begin(); fixEntity!=entityList.end(); fixEntity++)\r
126         {\r
127                 int cnt;\r
128 \r
129                 if(!stricmp("worldspawn", (*fixEntity)->m_Classname))\r
130                         cnt = (*fixEntity)->FixBrushes(rebuild);\r
131                 else\r
132                 {\r
133                         cnt = (*fixEntity)->FixBrushes(FALSE);\r
134                         \r
135                         if(cnt && rebuild)\r
136                                 RebuildEntity(*fixEntity);\r
137                 }\r
138 \r
139                 count += cnt;\r
140         }\r
141 \r
142         return count;\r
143 }\r
144 \r
145 void DMap::ResetTextures( const char* textureName, float fScale[2],      float fShift[2],      int rotation, const char* newTextureName, \r
146                           int bResetTextureName,  int bResetScale[2],  int bResetShift[2],  int bResetRotation)\r
147 {\r
148         for(list<DEntity *>::const_iterator texEntity=entityList.begin(); texEntity!=entityList.end(); texEntity++)\r
149         {\r
150                 if(!stricmp("worldspawn", (*texEntity)->m_Classname))\r
151                         (*texEntity)->ResetTextures(textureName,        fScale,       fShift,       rotation, newTextureName, \r
152                                   bResetTextureName,  bResetScale,  bResetShift,  bResetRotation, TRUE);\r
153                 else\r
154                 {\r
155                         if((*texEntity)->ResetTextures( textureName,        fScale,       fShift,       rotation, newTextureName, \r
156                                       bResetTextureName,  bResetScale,  bResetShift,  bResetRotation, FALSE))\r
157                                 RebuildEntity(*texEntity);\r
158                 }\r
159         }       \r
160 }\r
161 \r
162 void DMap::RebuildEntity(DEntity *ent)\r
163 {\r
164         ent->RemoveFromRadiant();\r
165         ent->BuildInRadiant(FALSE);\r
166 }\r