entity.cpp 9.6 KB

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