]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/entity.cpp
16dd77494aa99ef5f9346830e0c5c8fe33b97fff
[xonotic/netradiant.git] / plugins / entity / entity.cpp
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "entity.h"
23
24 #include "ifilter.h"
25 #include "selectable.h"
26 #include "namespace.h"
27
28 #include "scenelib.h"
29 #include "entitylib.h"
30 #include "eclasslib.h"
31 #include "pivot.h"
32
33 #include "targetable.h"
34 #include "uniquenames.h"
35 #include "namekeys.h"
36 #include "stream/stringstream.h"
37 #include "filters.h"
38
39
40 #include "miscmodel.h"
41 #include "light.h"
42 #include "group.h"
43 #include "eclassmodel.h"
44 #include "generic.h"
45 #include "doom3group.h"
46
47
48
49 EGameType g_gameType;
50
51 inline scene::Node& entity_for_eclass(EntityClass* eclass)
52 {
53   if(classname_equal(eclass->name(), "misc_model")
54   || classname_equal(eclass->name(), "misc_gamemodel")
55   || classname_equal(eclass->name(), "model_static"))
56   {
57     return New_MiscModel(eclass);
58   }
59   else if(classname_equal(eclass->name(), "light")
60     || classname_equal(eclass->name(), "lightJunior"))
61   {
62     return New_Light(eclass);
63   }
64   if(!eclass->fixedsize)
65   {
66     if(g_gameType == eGameTypeDoom3)
67     {
68       return New_Doom3Group(eclass);
69     }
70     else
71     {
72       return New_Group(eclass);
73     }
74   }
75   else if(!string_empty(eclass->modelpath()))
76   {
77     return New_EclassModel(eclass);
78   }
79   else
80   {
81     return New_GenericEntity(eclass);
82   }
83 }
84
85 void Entity_setName(Entity& entity, const char* name)
86 {
87   entity.setKeyValue("name", name);
88 }
89 typedef ReferenceCaller1<Entity, const char*, Entity_setName> EntitySetNameCaller;
90
91 inline Namespaced* Node_getNamespaced(scene::Node& node)
92 {
93   return NodeTypeCast<Namespaced>::cast(node);
94 }
95
96 inline scene::Node& node_for_eclass(EntityClass* eclass)
97 {
98   scene::Node& node = entity_for_eclass(eclass);
99   Node_getEntity(node)->setKeyValue("classname", eclass->name());
100
101   if(g_gameType == eGameTypeDoom3
102     && string_not_empty(eclass->name())
103     && !string_equal(eclass->name(), "worldspawn")
104     && !string_equal(eclass->name(), "UNKNOWN_CLASS"))
105   {
106     char buffer[1024];
107     strcpy(buffer, eclass->name());
108     strcat(buffer, "_1");
109     GlobalNamespace().makeUnique(buffer, EntitySetNameCaller(*Node_getEntity(node)));
110   }
111
112   Namespaced* namespaced = Node_getNamespaced(node);
113   if(namespaced != 0)
114   {
115     namespaced->setNamespace(GlobalNamespace());
116   }
117
118   return node;
119 }
120
121 EntityCreator::KeyValueChangedFunc EntityKeyValues::m_entityKeyValueChanged = 0;
122 EntityCreator::KeyValueChangedFunc KeyValue::m_entityKeyValueChanged = 0;
123 Counter* EntityKeyValues::m_counter = 0;
124
125 bool g_showNames = true;
126 bool g_showAngles = true;
127 bool g_newLightDraw = true;
128 bool g_lightRadii = false;
129
130 class ConnectEntities
131 {
132 public:
133   Entity* m_e1;
134   Entity* m_e2;
135   int m_index;
136   ConnectEntities(Entity* e1, Entity* e2, int index) : m_e1(e1), m_e2(e2), m_index(index)
137   {
138   }
139   const char *keyname()
140   {
141     StringOutputStream key(16);
142     if(m_index <= 0)
143       return "target";
144     if(m_index == 1)
145       return "killtarget";
146     key << "target" << m_index;
147     return key.c_str();
148   }
149   void connect(const char* name)
150   {
151           m_e1->setKeyValue(keyname(), name);
152           m_e2->setKeyValue("targetname", name);
153   }
154   typedef MemberCaller1<ConnectEntities, const char*, &ConnectEntities::connect> ConnectCaller;
155 };
156
157 inline Entity* ScenePath_getEntity(const scene::Path& path)
158 {
159   Entity* entity = Node_getEntity(path.top());
160   if(entity == 0)
161   {
162     entity = Node_getEntity(path.parent());
163   }
164   return entity;
165 }
166
167 class Quake3EntityCreator : public EntityCreator
168 {
169 public:
170   scene::Node& createEntity(EntityClass* eclass)
171   {
172     return node_for_eclass(eclass);
173   }
174   void setKeyValueChangedFunc(KeyValueChangedFunc func)
175   {
176     EntityKeyValues::setKeyValueChangedFunc(func);
177   }
178   void setCounter(Counter* counter)
179   {
180     EntityKeyValues::setCounter(counter);
181   }
182   void connectEntities(const scene::Path& path, const scene::Path& targetPath, int index)
183   {
184     Entity* e1 = ScenePath_getEntity(path);
185     Entity* e2 = ScenePath_getEntity(targetPath);
186
187     if(e1 == 0 || e2 == 0)
188     {
189       globalErrorStream() << "entityConnectSelected: both of the selected instances must be an entity\n";
190       return;
191     }
192
193     if(e1 == e2)
194     {
195       globalErrorStream() << "entityConnectSelected: the selected instances must not both be from the same entity\n";
196       return;
197     }
198
199
200     UndoableCommand undo("entityConnectSelected");
201
202     if(g_gameType == eGameTypeDoom3)
203     {
204       StringOutputStream key(16);
205       if(index >= 0)
206       {
207           key << "target";
208           if(index != 0)
209           {
210             key << index;
211           }
212           e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
213           key.clear();
214       }
215       else
216       {
217         for(unsigned int i = 0; ; ++i)
218         {
219           key << "target";
220           if(i != 0)
221           {
222             key << i;
223           }
224           const char* value = e1->getKeyValue(key.c_str());
225           if(string_empty(value))
226           {
227             e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
228             break;
229           }
230           key.clear();
231         }
232       }
233     }
234     else
235     {
236       ConnectEntities connector(e1, e2, index);
237       const char* value = e2->getKeyValue("targetname");
238       if(string_empty(value))
239       {
240         value = e1->getKeyValue(connector.keyname());
241       }
242       if(!string_empty(value))
243       {
244         connector.connect(value);
245       }
246       else
247       {
248         const char* type = e2->getKeyValue("classname");
249         if(string_empty(type))
250         {
251           type = "t";
252         }
253         StringOutputStream key(64);
254         key << type << "1";
255         GlobalNamespace().makeUnique(key.c_str(), ConnectEntities::ConnectCaller(connector));
256       }
257     }
258
259     SceneChangeNotify();
260   }
261   void setLightRadii(bool lightRadii)
262   {
263     g_lightRadii = lightRadii;
264   }
265   bool getLightRadii()
266   {
267     return g_lightRadii;
268   }
269   void setShowNames(bool showNames)
270   {
271     g_showNames = showNames;
272   }
273   bool getShowNames()
274   {
275     return g_showNames;
276   }
277   void setShowAngles(bool showAngles)
278   {
279     g_showAngles = showAngles;
280   }
281   bool getShowAngles()
282   {
283     return g_showAngles;
284   }
285
286   void printStatistics() const
287   {
288     StringPool_analyse(EntityKeyValues::getPool());
289   }
290 };
291
292 Quake3EntityCreator g_Quake3EntityCreator;
293
294 EntityCreator& GetEntityCreator()
295 {
296   return g_Quake3EntityCreator;
297 }
298
299
300
301 class filter_entity_classname : public EntityFilter
302 {
303   const char* m_classname;
304 public:
305   filter_entity_classname(const char* classname) : m_classname(classname)
306   {
307   }
308   bool filter(const Entity& entity) const
309   {
310     return string_equal(entity.getKeyValue("classname"), m_classname);
311   }
312 };
313
314 class filter_entity_classgroup : public EntityFilter
315 {
316   const char* m_classgroup;
317   std::size_t m_length;
318 public:
319   filter_entity_classgroup(const char* classgroup) : m_classgroup(classgroup), m_length(string_length(m_classgroup))
320   {
321   }
322   bool filter(const Entity& entity) const
323   {
324     return string_equal_n(entity.getKeyValue("classname"), m_classgroup, m_length);
325   }
326 };
327
328 filter_entity_classname g_filter_entity_world("worldspawn");
329 filter_entity_classname g_filter_entity_func_group("func_group");
330 filter_entity_classname g_filter_entity_light("light");
331 filter_entity_classname g_filter_entity_misc_model("misc_model");
332 filter_entity_classname g_filter_entity_misc_gamemodel("misc_gamemodel");
333 filter_entity_classgroup g_filter_entity_trigger("trigger_");
334 filter_entity_classgroup g_filter_entity_path("path_");
335
336 class filter_entity_doom3model : public EntityFilter
337 {
338 public:
339   bool filter(const Entity& entity) const
340   {
341     return string_equal(entity.getKeyValue("classname"), "func_static")
342       && !string_equal(entity.getKeyValue("model"), entity.getKeyValue("name"));
343   }
344 };
345
346 filter_entity_doom3model g_filter_entity_doom3model;
347
348
349 void Entity_InitFilters()
350 {
351         add_entity_filter(g_filter_entity_world, EXCLUDE_WORLD);
352         add_entity_filter(g_filter_entity_func_group, EXCLUDE_WORLD);
353         add_entity_filter(g_filter_entity_world, EXCLUDE_ENT, true);
354     add_entity_filter(g_filter_entity_trigger, EXCLUDE_TRIGGERS);
355         add_entity_filter(g_filter_entity_misc_model, EXCLUDE_MODELS);
356     add_entity_filter(g_filter_entity_misc_gamemodel, EXCLUDE_MODELS);
357         add_entity_filter(g_filter_entity_doom3model, EXCLUDE_MODELS);
358         add_entity_filter(g_filter_entity_light, EXCLUDE_LIGHTS);
359         add_entity_filter(g_filter_entity_path, EXCLUDE_PATHS);
360 }
361
362
363 #include "preferencesystem.h"
364
365 void Entity_Construct(EGameType gameType)
366 {
367   g_gameType = gameType;
368   if(g_gameType == eGameTypeDoom3)
369   {
370     g_targetable_nameKey = "name";
371
372     Static<KeyIsName>::instance().m_keyIsName = keyIsNameDoom3;
373     Static<KeyIsName>::instance().m_nameKey = "name";
374   }
375   else
376   {
377     Static<KeyIsName>::instance().m_keyIsName = keyIsNameQuake3;
378     Static<KeyIsName>::instance().m_nameKey = "targetname";
379   }
380
381   GlobalPreferenceSystem().registerPreference("SI_ShowNames", BoolImportStringCaller(g_showNames), BoolExportStringCaller(g_showNames));
382   GlobalPreferenceSystem().registerPreference("SI_ShowAngles", BoolImportStringCaller(g_showAngles), BoolExportStringCaller(g_showAngles));
383   GlobalPreferenceSystem().registerPreference("NewLightStyle", BoolImportStringCaller(g_newLightDraw), BoolExportStringCaller(g_newLightDraw));
384   GlobalPreferenceSystem().registerPreference("LightRadiuses", BoolImportStringCaller(g_lightRadii), BoolExportStringCaller(g_lightRadii));
385
386   Entity_InitFilters();
387   LightType lightType = LIGHTTYPE_DEFAULT;
388   if(g_gameType == eGameTypeRTCW)
389   {
390     lightType = LIGHTTYPE_RTCW;
391   }
392   else if(g_gameType == eGameTypeDoom3)
393   {
394     lightType = LIGHTTYPE_DOOM3;
395   }
396   Light_Construct(lightType);
397   MiscModel_construct();
398   Doom3Group_construct();
399
400   RenderablePivot::StaticShader::instance() = GlobalShaderCache().capture("$PIVOT");
401
402   GlobalShaderCache().attachRenderable(StaticRenderableConnectionLines::instance());
403 }
404
405 void Entity_Destroy()
406 {
407   GlobalShaderCache().detachRenderable(StaticRenderableConnectionLines::instance());
408
409   GlobalShaderCache().release("$PIVOT");
410
411   Doom3Group_destroy();
412   MiscModel_destroy();
413   Light_Destroy();
414 }