Debian Codegraph @

plugins/entity/entity.cpp

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