]> icculus.org git repositories - divverent/netradiant.git/blob - plugins/entity/entity.cpp
support misc_*model as misc_model in the radiant editor (way more convenient)
[divverent/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((string_compare_nocase_n(eclass->name(), "misc_", 5) == 0 && string_equal_nocase(eclass->name() + string_length(eclass->name()) - 5, "model")) // misc_*model (also misc_model) // TODO make classname_* wrapper functions for this
54   || classname_equal(eclass->name(), "model_static"))
55   {
56     return New_MiscModel(eclass);
57   }
58   else if(classname_equal(eclass->name(), "light")
59     || classname_equal(eclass->name(), "lightJunior"))
60   {
61     return New_Light(eclass);
62   }
63   if(!eclass->fixedsize)
64   {
65     if(g_gameType == eGameTypeDoom3)
66     {
67       return New_Doom3Group(eclass);
68     }
69     else
70     {
71       return New_Group(eclass);
72     }
73   }
74   else if(!string_empty(eclass->modelpath()))
75   {
76     return New_EclassModel(eclass);
77   }
78   else
79   {
80     return New_GenericEntity(eclass);
81   }
82 }
83
84 void Entity_setName(Entity& entity, const char* name)
85 {
86   entity.setKeyValue("name", name);
87 }
88 typedef ReferenceCaller1<Entity, const char*, Entity_setName> EntitySetNameCaller;
89
90 inline Namespaced* Node_getNamespaced(scene::Node& node)
91 {
92   return NodeTypeCast<Namespaced>::cast(node);
93 }
94
95 inline scene::Node& node_for_eclass(EntityClass* eclass)
96 {
97   scene::Node& node = entity_for_eclass(eclass);
98   Node_getEntity(node)->setKeyValue("classname", eclass->name());
99
100   if(g_gameType == eGameTypeDoom3
101     && string_not_empty(eclass->name())
102     && !string_equal(eclass->name(), "worldspawn")
103     && !string_equal(eclass->name(), "UNKNOWN_CLASS"))
104   {
105     char buffer[1024];
106     strcpy(buffer, eclass->name());
107     strcat(buffer, "_1");
108     GlobalNamespace().makeUnique(buffer, EntitySetNameCaller(*Node_getEntity(node)));
109   }
110
111   Namespaced* namespaced = Node_getNamespaced(node);
112   if(namespaced != 0)
113   {
114     namespaced->setNamespace(GlobalNamespace());
115   }
116
117   return node;
118 }
119
120 EntityCreator::KeyValueChangedFunc EntityKeyValues::m_entityKeyValueChanged = 0;
121 EntityCreator::KeyValueChangedFunc KeyValue::m_entityKeyValueChanged = 0;
122 Counter* EntityKeyValues::m_counter = 0;
123
124 bool g_showNames = true;
125 bool g_showAngles = true;
126 bool g_newLightDraw = true;
127 bool g_lightRadii = false;
128
129 class ConnectEntities
130 {
131 public:
132   Entity* m_e1;
133   Entity* m_e2;
134   int m_index;
135   ConnectEntities(Entity* e1, Entity* e2, int index) : m_e1(e1), m_e2(e2), m_index(index)
136   {
137   }
138   const char *keyname()
139   {
140     StringOutputStream key(16);
141     if(m_index <= 0)
142       return "target";
143     if(m_index == 1)
144       return "killtarget";
145     key << "target" << m_index;
146     return key.c_str();
147   }
148   void connect(const char* name)
149   {
150           m_e1->setKeyValue(keyname(), name);
151           m_e2->setKeyValue("targetname", name);
152   }
153   typedef MemberCaller1<ConnectEntities, const char*, &ConnectEntities::connect> ConnectCaller;
154 };
155
156 inline Entity* ScenePath_getEntity(const scene::Path& path)
157 {
158   Entity* entity = Node_getEntity(path.top());
159   if(entity == 0)
160   {
161     entity = Node_getEntity(path.parent());
162   }
163   return entity;
164 }
165
166 class Quake3EntityCreator : public EntityCreator
167 {
168 public:
169   scene::Node& createEntity(EntityClass* eclass)
170   {
171     return node_for_eclass(eclass);
172   }
173   void setKeyValueChangedFunc(KeyValueChangedFunc func)
174   {
175     EntityKeyValues::setKeyValueChangedFunc(func);
176   }
177   void setCounter(Counter* counter)
178   {
179     EntityKeyValues::setCounter(counter);
180   }
181   void connectEntities(const scene::Path& path, const scene::Path& targetPath, int index)
182   {
183     Entity* e1 = ScenePath_getEntity(path);
184     Entity* e2 = ScenePath_getEntity(targetPath);
185
186     if(e1 == 0 || e2 == 0)
187     {
188       globalErrorStream() << "entityConnectSelected: both of the selected instances must be an entity\n";
189       return;
190     }
191
192     if(e1 == e2)
193     {
194       globalErrorStream() << "entityConnectSelected: the selected instances must not both be from the same entity\n";
195       return;
196     }
197
198
199     UndoableCommand undo("entityConnectSelected");
200
201     if(g_gameType == eGameTypeDoom3)
202     {
203       StringOutputStream key(16);
204       if(index >= 0)
205       {
206           key << "target";
207           if(index != 0)
208           {
209             key << index;
210           }
211           e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
212           key.clear();
213       }
214       else
215       {
216         for(unsigned int i = 0; ; ++i)
217         {
218           key << "target";
219           if(i != 0)
220           {
221             key << i;
222           }
223           const char* value = e1->getKeyValue(key.c_str());
224           if(string_empty(value))
225           {
226             e1->setKeyValue(key.c_str(), e2->getKeyValue("name"));
227             break;
228           }
229           key.clear();
230         }
231       }
232     }
233     else
234     {
235       ConnectEntities connector(e1, e2, index);
236       const char* value = e2->getKeyValue("targetname");
237       if(string_empty(value))
238       {
239         value = e1->getKeyValue(connector.keyname());
240       }
241       if(!string_empty(value))
242       {
243         connector.connect(value);
244       }
245       else
246       {
247         const char* type = e2->getKeyValue("classname");
248         if(string_empty(type))
249         {
250           type = "t";
251         }
252         StringOutputStream key(64);
253         key << type << "1";
254         GlobalNamespace().makeUnique(key.c_str(), ConnectEntities::ConnectCaller(connector));
255       }
256     }
257
258     SceneChangeNotify();
259   }
260   void setLightRadii(bool lightRadii)
261   {
262     g_lightRadii = lightRadii;
263   }
264   bool getLightRadii()
265   {
266     return g_lightRadii;
267   }
268   void setShowNames(bool showNames)
269   {
270     g_showNames = showNames;
271   }
272   bool getShowNames()
273   {
274     return g_showNames;
275   }
276   void setShowAngles(bool showAngles)
277   {
278     g_showAngles = showAngles;
279   }
280   bool getShowAngles()
281   {
282     return g_showAngles;
283   }
284
285   void printStatistics() const
286   {
287     StringPool_analyse(EntityKeyValues::getPool());
288   }
289 };
290
291 Quake3EntityCreator g_Quake3EntityCreator;
292
293 EntityCreator& GetEntityCreator()
294 {
295   return g_Quake3EntityCreator;
296 }
297
298
299
300 class filter_entity_classname : public EntityFilter
301 {
302   const char* m_classname;
303 public:
304   filter_entity_classname(const char* classname) : m_classname(classname)
305   {
306   }
307   bool filter(const Entity& entity) const
308   {
309     return string_equal(entity.getKeyValue("classname"), m_classname);
310   }
311 };
312
313 class filter_entity_classgroup : public EntityFilter
314 {
315   const char* m_classgroup;
316   std::size_t m_length;
317 public:
318   filter_entity_classgroup(const char* classgroup) : m_classgroup(classgroup), m_length(string_length(m_classgroup))
319   {
320   }
321   bool filter(const Entity& entity) const
322   {
323     return string_equal_n(entity.getKeyValue("classname"), m_classgroup, m_length);
324   }
325 };
326
327 filter_entity_classname g_filter_entity_world("worldspawn");
328 filter_entity_classname g_filter_entity_func_group("func_group");
329 filter_entity_classname g_filter_entity_light("light");
330 filter_entity_classname g_filter_entity_misc_model("misc_model");
331 filter_entity_classname g_filter_entity_misc_gamemodel("misc_gamemodel");
332 filter_entity_classgroup g_filter_entity_trigger("trigger_");
333 filter_entity_classgroup g_filter_entity_path("path_");
334
335 class filter_entity_doom3model : public EntityFilter
336 {
337 public:
338   bool filter(const Entity& entity) const
339   {
340     return string_equal(entity.getKeyValue("classname"), "func_static")
341       && !string_equal(entity.getKeyValue("model"), entity.getKeyValue("name"));
342   }
343 };
344
345 filter_entity_doom3model g_filter_entity_doom3model;
346
347
348 void Entity_InitFilters()
349 {
350         add_entity_filter(g_filter_entity_world, EXCLUDE_WORLD);
351         add_entity_filter(g_filter_entity_func_group, EXCLUDE_WORLD);
352         add_entity_filter(g_filter_entity_world, EXCLUDE_ENT, true);
353     add_entity_filter(g_filter_entity_trigger, EXCLUDE_TRIGGERS);
354         add_entity_filter(g_filter_entity_misc_model, EXCLUDE_MODELS);
355     add_entity_filter(g_filter_entity_misc_gamemodel, EXCLUDE_MODELS);
356         add_entity_filter(g_filter_entity_doom3model, EXCLUDE_MODELS);
357         add_entity_filter(g_filter_entity_light, EXCLUDE_LIGHTS);
358         add_entity_filter(g_filter_entity_path, EXCLUDE_PATHS);
359 }
360
361
362 #include "preferencesystem.h"
363
364 void Entity_Construct(EGameType gameType)
365 {
366   g_gameType = gameType;
367   if(g_gameType == eGameTypeDoom3)
368   {
369     g_targetable_nameKey = "name";
370
371     Static<KeyIsName>::instance().m_keyIsName = keyIsNameDoom3;
372     Static<KeyIsName>::instance().m_nameKey = "name";
373   }
374   else
375   {
376     Static<KeyIsName>::instance().m_keyIsName = keyIsNameQuake3;
377     Static<KeyIsName>::instance().m_nameKey = "targetname";
378   }
379
380   GlobalPreferenceSystem().registerPreference("SI_ShowNames", BoolImportStringCaller(g_showNames), BoolExportStringCaller(g_showNames));
381   GlobalPreferenceSystem().registerPreference("SI_ShowAngles", BoolImportStringCaller(g_showAngles), BoolExportStringCaller(g_showAngles));
382   GlobalPreferenceSystem().registerPreference("NewLightStyle", BoolImportStringCaller(g_newLightDraw), BoolExportStringCaller(g_newLightDraw));
383   GlobalPreferenceSystem().registerPreference("LightRadiuses", BoolImportStringCaller(g_lightRadii), BoolExportStringCaller(g_lightRadii));
384
385   Entity_InitFilters();
386   LightType lightType = LIGHTTYPE_DEFAULT;
387   if(g_gameType == eGameTypeRTCW)
388   {
389     lightType = LIGHTTYPE_RTCW;
390   }
391   else if(g_gameType == eGameTypeDoom3)
392   {
393     lightType = LIGHTTYPE_DOOM3;
394   }
395   Light_Construct(lightType);
396   MiscModel_construct();
397   Doom3Group_construct();
398
399   RenderablePivot::StaticShader::instance() = GlobalShaderCache().capture("$PIVOT");
400
401   GlobalShaderCache().attachRenderable(StaticRenderableConnectionLines::instance());
402 }
403
404 void Entity_Destroy()
405 {
406   GlobalShaderCache().detachRenderable(StaticRenderableConnectionLines::instance());
407
408   GlobalShaderCache().release("$PIVOT");
409
410   Doom3Group_destroy();
411   MiscModel_destroy();
412   Light_Destroy();
413 }