ECLASS.CPP 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "qe3.h"
  23. #include "io.h"
  24. #include "../../renderer/tr_local.h"
  25. struct evarPrefix_t {
  26. int type;
  27. const char *prefix;
  28. };
  29. const evarPrefix_t EvarPrefixes[] = {
  30. { EVAR_STRING, "editor_var " },
  31. { EVAR_INT, "editor_int " },
  32. { EVAR_FLOAT, "editor_float " },
  33. { EVAR_BOOL, "editor_bool " },
  34. { EVAR_COLOR, "editor_color " },
  35. { EVAR_MATERIAL,"editor_mat " },
  36. { EVAR_MODEL, "editor_model " },
  37. { EVAR_GUI, "editor_gui " },
  38. { EVAR_SOUND, "editor_snd "}
  39. };
  40. const int NumEvarPrefixes = sizeof(EvarPrefixes) / sizeof(evarPrefix_t);
  41. eclass_t *eclass = NULL;
  42. eclass_t *eclass_bad = NULL;
  43. char eclass_directory[1024];
  44. // md3 cache for misc_models
  45. eclass_t *g_md3Cache = NULL;
  46. /*
  47. the classname, color triple, and bounding box are parsed out of comments
  48. A ? size means take the exact brush size.
  49. /*QUAKED <classname> (0 0 0) ?
  50. /*QUAKED <classname> (0 0 0) (-8 -8 -8) (8 8 8)
  51. Flag names can follow the size description:
  52. /*QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY
  53. */
  54. void CleanEntityList( eclass_t *&pList ) {
  55. while (pList) {
  56. eclass_t* pTemp = pList->next;
  57. delete pList;
  58. pList = pTemp;
  59. }
  60. pList = NULL;
  61. }
  62. void CleanUpEntities()
  63. {
  64. CleanEntityList(eclass);
  65. CleanEntityList(g_md3Cache);
  66. if ( eclass_bad ) {
  67. delete eclass_bad;
  68. eclass_bad = NULL;
  69. }
  70. }
  71. void ExtendBounds(idVec3 v, idVec3 &vMin, idVec3 &vMax)
  72. {
  73. for (int i = 0 ;i < 3 ;i++)
  74. {
  75. float f = v[i];
  76. if (f < vMin[i])
  77. {
  78. vMin[i] = f;
  79. }
  80. if (f > vMax[i])
  81. {
  82. vMax[i] = f;
  83. }
  84. }
  85. }
  86. bool LoadModel(const char *pLocation, eclass_t *e, idVec3 &vMin, idVec3 &vMax, const char *pSkin)
  87. {
  88. vMin[0] = vMin[1] = vMin[2] = 999999;
  89. vMax[0] = vMax[1] = vMax[2] = -999999;
  90. if (strstr(pLocation, ".ase") != NULL) // FIXME: not correct!
  91. {
  92. idBounds b;
  93. e->modelHandle = renderModelManager->FindModel( pLocation );
  94. b = e->modelHandle->Bounds( NULL );
  95. VectorCopy(b[0], vMin);
  96. VectorCopy(b[1], vMax);
  97. return true;
  98. }
  99. return false;
  100. }
  101. eclass_t *EClass_Alloc( void ) {
  102. eclass_t *e;
  103. e = new eclass_t;
  104. if ( e == NULL ) {
  105. return NULL;
  106. }
  107. e->fixedsize = false;
  108. e->unknown = false;
  109. e->mins.Zero();
  110. e->maxs.Zero();
  111. e->color.Zero();
  112. memset( &e->texdef, 0, sizeof( e->texdef ) );
  113. e->modelHandle = NULL;
  114. e->entityModel = NULL;
  115. e->nFrame = 0;
  116. e->nShowFlags = 0;
  117. e->hPlug = 0;
  118. e->next = NULL;
  119. return e;
  120. }
  121. eclass_t *EClass_InitFromDict( const idDict *d, const char *name ) {
  122. eclass_t *e;
  123. const idKeyValue *kv;
  124. // only include entityDefs with "editor_" values in them
  125. if ( !d->MatchPrefix( "editor_" ) ) {
  126. return NULL;
  127. }
  128. e = EClass_Alloc();
  129. if ( !e ) {
  130. return NULL;
  131. }
  132. e->defArgs = *d;
  133. idStr str;
  134. idStr text;
  135. idStr varname;
  136. idStr defaultStr;
  137. e->name = name;
  138. d->GetVector("editor_color", "0 0 1", e->color);
  139. d->GetString("editor_mins", "", str);
  140. if (str != "?") {
  141. d->GetVector("editor_mins", "0 0 0", e->mins);
  142. d->GetVector("editor_maxs", "0 0 0", e->maxs);
  143. e->fixedsize = true;
  144. } else {
  145. e->fixedsize = false;
  146. }
  147. d->GetString("editor_material", "", e->defMaterial);
  148. //str = d->GetString("model");
  149. //if (str.Length()) {
  150. // e->entityModel = renderModelManager->FindModel(str);
  151. //}
  152. str = "";
  153. // concatenate all editor usage comments
  154. text = "";
  155. kv = d->MatchPrefix( "editor_usage" );
  156. while( kv != NULL ) {
  157. text += kv->GetValue();
  158. if ( !kv->GetValue().Length() || ( text[ text.Length() - 1 ] != '\n' ) ) {
  159. text += "\n";
  160. }
  161. kv = d->MatchPrefix( "editor_usage", kv );
  162. }
  163. e->desc = text;
  164. str += "Spawn args:\n";
  165. for (int i = 0; i < NumEvarPrefixes; i++) {
  166. kv = d->MatchPrefix(EvarPrefixes[i].prefix);
  167. while (kv) {
  168. evar_t ev;
  169. kv->GetKey().Right( kv->GetKey().Length() - strlen(EvarPrefixes[i].prefix), ev.name );
  170. ev.desc = kv->GetValue();
  171. ev.type = EvarPrefixes[i].type;
  172. e->vars.Append(ev);
  173. kv = d->MatchPrefix(EvarPrefixes[i].prefix, kv);
  174. }
  175. }
  176. /*
  177. while( kv != NULL ) {
  178. kv->key.Right( kv->key.Length() - 11, varname );
  179. str += va( "'%s':\t %s", varname.c_str(), kv->value.c_str() );
  180. if ( d->GetString( varname, "", defaultStr ) && defaultStr.Length() ) {
  181. str += va( " Default '%s'.", defaultStr.c_str() );
  182. }
  183. str += "\n";
  184. kv = d->MatchPrefix( "editor_var ", kv );
  185. }
  186. e->comments = Mem_CopyString( str.c_str() );
  187. */
  188. // concatenate all variable comments
  189. kv = d->MatchPrefix( "editor_copy" );
  190. while (kv) {
  191. const char *temp = d->GetString(kv->GetValue());
  192. if (temp && *temp) {
  193. e->args.Set(kv->GetValue(), d->GetString(kv->GetValue()));
  194. }
  195. kv = d->MatchPrefix("editor_copy", kv);
  196. }
  197. // setup show flags
  198. e->nShowFlags = 0;
  199. if (d->GetBool("editor_rotatable")) {
  200. e->nShowFlags |= ECLASS_ROTATABLE;
  201. }
  202. if (d->GetBool("editor_showangle")) {
  203. e->nShowFlags |= ECLASS_ANGLE;
  204. }
  205. if (d->GetBool("editor_mover")) {
  206. e->nShowFlags |= ECLASS_MOVER;
  207. }
  208. if (d->GetBool("editor_env") || idStr::Icmpn(e->name, "env_", 4) == 0) {
  209. e->nShowFlags |= (ECLASS_ENV | ECLASS_ROTATABLE);
  210. if (d->GetBool("editor_ragdoll")) {
  211. e->defArgs.Set("model", "");
  212. }
  213. }
  214. if (d->GetBool("editor_combatnode")) {
  215. e->nShowFlags |= ECLASS_COMBATNODE;
  216. }
  217. if (d->GetBool("editor_light")) {
  218. e->nShowFlags |= ECLASS_LIGHT;
  219. }
  220. if ( idStr::Icmp(e->name, "light") == 0 ) {
  221. e->nShowFlags |= ECLASS_LIGHT;
  222. } else if ( idStr::Icmp(e->name, "path") == 0 ) {
  223. e->nShowFlags |= ECLASS_PATH;
  224. } else if ( idStr::Icmp(e->name, "target_null") == 0 ) {
  225. e->nShowFlags |= ECLASS_CAMERAVIEW;
  226. } else if ( idStr::Icmp(e->name, "worldspawn") == 0 ) {
  227. e->nShowFlags |= ECLASS_WORLDSPAWN;
  228. } else if ( idStr::Icmp(e->name, "speaker") == 0 ) {
  229. e->nShowFlags |= ECLASS_SPEAKER;
  230. } else if ( idStr::Icmp( e->name, "func_emitter" ) == 0 || idStr::Icmp( e->name, "func_splat" ) == 0 ) {
  231. e->nShowFlags |= ECLASS_PARTICLE;
  232. } else if ( idStr::Icmp(e->name, "func_liquid") == 0 ) {
  233. e->nShowFlags |= ECLASS_LIQUID;
  234. }
  235. return e;
  236. }
  237. void EClass_InsertSortedList(eclass_t *&pList, eclass_t *e)
  238. {
  239. eclass_t *s;
  240. if (!pList)
  241. {
  242. pList = e;
  243. return;
  244. }
  245. s = pList;
  246. if (stricmp (e->name, s->name) < 0)
  247. {
  248. e->next = s;
  249. pList = e;
  250. return;
  251. }
  252. do
  253. {
  254. if (!s->next || stricmp (e->name, s->next->name) < 0)
  255. {
  256. e->next = s->next;
  257. s->next = e;
  258. return;
  259. }
  260. s=s->next;
  261. } while (1);
  262. }
  263. /*
  264. =================
  265. Eclass_InsertAlphabetized
  266. =================
  267. */
  268. void Eclass_InsertAlphabetized (eclass_t *e)
  269. {
  270. #if 1
  271. EClass_InsertSortedList(eclass, e);
  272. #else
  273. eclass_t *s;
  274. if (!eclass)
  275. {
  276. eclass = e;
  277. return;
  278. }
  279. s = eclass;
  280. if (stricmp (e->name, s->name) < 0)
  281. {
  282. e->next = s;
  283. eclass = e;
  284. return;
  285. }
  286. do
  287. {
  288. if (!s->next || stricmp (e->name, s->next->name) < 0)
  289. {
  290. e->next = s->next;
  291. s->next = e;
  292. return;
  293. }
  294. s=s->next;
  295. } while (1);
  296. #endif
  297. }
  298. void Eclass_InitForSourceDirectory (const char *path)
  299. {
  300. int c = declManager->GetNumDecls(DECL_ENTITYDEF);
  301. for (int i = 0; i < c; i++) {
  302. const idDeclEntityDef *def = static_cast<const idDeclEntityDef *>( declManager->DeclByIndex( DECL_ENTITYDEF, i ) );
  303. if ( def ) {
  304. eclass_t *e = EClass_InitFromDict( &def->dict, def->GetName() );
  305. if ( e ) {
  306. Eclass_InsertAlphabetized (e);
  307. }
  308. }
  309. }
  310. eclass_bad = EClass_Alloc();
  311. if ( !eclass_bad ) {
  312. return;
  313. }
  314. eclass_bad->color.x = 0.0f;
  315. eclass_bad->color.y = 0.5f;
  316. eclass_bad->color.z = 0.0f;
  317. eclass_bad->fixedsize = false;
  318. eclass_bad->name = Mem_CopyString( "UKNOWN ENTITY CLASS" );
  319. }
  320. eclass_t *Eclass_ForName (const char *name, bool has_brushes)
  321. {
  322. eclass_t *e;
  323. char buff[1024];
  324. if (!name) {
  325. return eclass_bad;
  326. }
  327. for ( e = eclass; e; e = e->next ) {
  328. if ( !strcmp( name, e->name ) ) {
  329. return e;
  330. }
  331. }
  332. e = EClass_Alloc();
  333. if ( !e ) {
  334. return NULL;
  335. }
  336. e->name = Mem_CopyString( name );
  337. sprintf(buff, "%s not found in def/*.def\n", name);
  338. e->comments = Mem_CopyString( buff );
  339. e->color.x = 0.0f;
  340. e->color.y = 0.5f;
  341. e->color.z = 0.0f;
  342. e->fixedsize = !has_brushes;
  343. e->mins.x = e->mins.y = e->mins.z = -8.0f;
  344. e->maxs.x = e->maxs.y = e->maxs.z = 8.0f;
  345. Eclass_InsertAlphabetized( e );
  346. return e;
  347. }
  348. eclass_t* GetCachedModel(entity_t *pEntity, const char *pName, idVec3 &vMin, idVec3 &vMax)
  349. {
  350. eclass_t *e = NULL;
  351. if (pName == NULL || strlen(pName) == 0) {
  352. return NULL;
  353. }
  354. for (e = g_md3Cache; e ; e = e->next) {
  355. if (!strcmp (pName, e->name)) {
  356. pEntity->md3Class = e;
  357. VectorCopy(e->mins, vMin);
  358. VectorCopy(e->maxs, vMax);
  359. return e;
  360. }
  361. }
  362. e = (eclass_t*)Mem_ClearedAlloc(sizeof(*e));
  363. memset (e, 0, sizeof(*e));
  364. e->name = Mem_CopyString( pName );
  365. e->color[0] = e->color[2] = 0.85f;
  366. if (LoadModel(pName, e, vMin, vMax, NULL)) {
  367. EClass_InsertSortedList(g_md3Cache, e);
  368. VectorCopy(vMin, e->mins);
  369. VectorCopy(vMax, e->maxs);
  370. pEntity->md3Class = e;
  371. return e;
  372. }
  373. return NULL;
  374. }