pr_edict.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // sv_edict.c -- entity dictionary
  16. #include "quakedef.h"
  17. dprograms_t *progs;
  18. dfunction_t *pr_functions;
  19. char *pr_strings;
  20. ddef_t *pr_fielddefs;
  21. ddef_t *pr_globaldefs;
  22. dstatement_t *pr_statements;
  23. globalvars_t *pr_global_struct;
  24. float *pr_globals; // same as pr_global_struct
  25. int pr_edict_size; // in bytes
  26. unsigned short pr_crc;
  27. int type_size[8] = {1,sizeof(string_t)/4,1,3,1,1,sizeof(func_t)/4,sizeof(void *)/4};
  28. ddef_t *ED_FieldAtOfs (int ofs);
  29. qboolean ED_ParseEpair (void *base, ddef_t *key, char *s);
  30. cvar_t nomonsters = {"nomonsters", "0"};
  31. cvar_t gamecfg = {"gamecfg", "0"};
  32. cvar_t scratch1 = {"scratch1", "0"};
  33. cvar_t scratch2 = {"scratch2", "0"};
  34. cvar_t scratch3 = {"scratch3", "0"};
  35. cvar_t scratch4 = {"scratch4", "0"};
  36. cvar_t savedgamecfg = {"savedgamecfg", "0", true};
  37. cvar_t saved1 = {"saved1", "0", true};
  38. cvar_t saved2 = {"saved2", "0", true};
  39. cvar_t saved3 = {"saved3", "0", true};
  40. cvar_t saved4 = {"saved4", "0", true};
  41. #define MAX_FIELD_LEN 64
  42. #define GEFV_CACHESIZE 2
  43. typedef struct {
  44. ddef_t *pcache;
  45. char field[MAX_FIELD_LEN];
  46. } gefv_cache;
  47. static gefv_cache gefvCache[GEFV_CACHESIZE] = {{NULL, ""}, {NULL, ""}};
  48. /*
  49. =================
  50. ED_ClearEdict
  51. Sets everything to NULL
  52. =================
  53. */
  54. void ED_ClearEdict (edict_t *e)
  55. {
  56. memset (&e->v, 0, progs->entityfields * 4);
  57. e->free = false;
  58. }
  59. /*
  60. =================
  61. ED_Alloc
  62. Either finds a free edict, or allocates a new one.
  63. Try to avoid reusing an entity that was recently freed, because it
  64. can cause the client to think the entity morphed into something else
  65. instead of being removed and recreated, which can cause interpolated
  66. angles and bad trails.
  67. =================
  68. */
  69. edict_t *ED_Alloc (void)
  70. {
  71. int i;
  72. edict_t *e;
  73. for ( i=svs.maxclients+1 ; i<sv.num_edicts ; i++)
  74. {
  75. e = EDICT_NUM(i);
  76. // the first couple seconds of server time can involve a lot of
  77. // freeing and allocating, so relax the replacement policy
  78. if (e->free && ( e->freetime < 2 || sv.time - e->freetime > 0.5 ) )
  79. {
  80. ED_ClearEdict (e);
  81. return e;
  82. }
  83. }
  84. if (i == MAX_EDICTS)
  85. Sys_Error ("ED_Alloc: no free edicts");
  86. sv.num_edicts++;
  87. e = EDICT_NUM(i);
  88. ED_ClearEdict (e);
  89. return e;
  90. }
  91. /*
  92. =================
  93. ED_Free
  94. Marks the edict as free
  95. FIXME: walk all entities and NULL out references to this entity
  96. =================
  97. */
  98. void ED_Free (edict_t *ed)
  99. {
  100. SV_UnlinkEdict (ed); // unlink from world bsp
  101. ed->free = true;
  102. ed->v.model = 0;
  103. ed->v.takedamage = 0;
  104. ed->v.modelindex = 0;
  105. ed->v.colormap = 0;
  106. ed->v.skin = 0;
  107. ed->v.frame = 0;
  108. VectorCopy (vec3_origin, ed->v.origin);
  109. VectorCopy (vec3_origin, ed->v.angles);
  110. ed->v.nextthink = -1;
  111. ed->v.solid = 0;
  112. ed->freetime = sv.time;
  113. }
  114. //===========================================================================
  115. /*
  116. ============
  117. ED_GlobalAtOfs
  118. ============
  119. */
  120. ddef_t *ED_GlobalAtOfs (int ofs)
  121. {
  122. ddef_t *def;
  123. int i;
  124. for (i=0 ; i<progs->numglobaldefs ; i++)
  125. {
  126. def = &pr_globaldefs[i];
  127. if (def->ofs == ofs)
  128. return def;
  129. }
  130. return NULL;
  131. }
  132. /*
  133. ============
  134. ED_FieldAtOfs
  135. ============
  136. */
  137. ddef_t *ED_FieldAtOfs (int ofs)
  138. {
  139. ddef_t *def;
  140. int i;
  141. for (i=0 ; i<progs->numfielddefs ; i++)
  142. {
  143. def = &pr_fielddefs[i];
  144. if (def->ofs == ofs)
  145. return def;
  146. }
  147. return NULL;
  148. }
  149. /*
  150. ============
  151. ED_FindField
  152. ============
  153. */
  154. ddef_t *ED_FindField (char *name)
  155. {
  156. ddef_t *def;
  157. int i;
  158. for (i=0 ; i<progs->numfielddefs ; i++)
  159. {
  160. def = &pr_fielddefs[i];
  161. if (!strcmp(pr_strings + def->s_name,name) )
  162. return def;
  163. }
  164. return NULL;
  165. }
  166. /*
  167. ============
  168. ED_FindGlobal
  169. ============
  170. */
  171. ddef_t *ED_FindGlobal (char *name)
  172. {
  173. ddef_t *def;
  174. int i;
  175. for (i=0 ; i<progs->numglobaldefs ; i++)
  176. {
  177. def = &pr_globaldefs[i];
  178. if (!strcmp(pr_strings + def->s_name,name) )
  179. return def;
  180. }
  181. return NULL;
  182. }
  183. /*
  184. ============
  185. ED_FindFunction
  186. ============
  187. */
  188. dfunction_t *ED_FindFunction (char *name)
  189. {
  190. dfunction_t *func;
  191. int i;
  192. for (i=0 ; i<progs->numfunctions ; i++)
  193. {
  194. func = &pr_functions[i];
  195. if (!strcmp(pr_strings + func->s_name,name) )
  196. return func;
  197. }
  198. return NULL;
  199. }
  200. eval_t *GetEdictFieldValue(edict_t *ed, char *field)
  201. {
  202. ddef_t *def = NULL;
  203. int i;
  204. static int rep = 0;
  205. for (i=0 ; i<GEFV_CACHESIZE ; i++)
  206. {
  207. if (!strcmp(field, gefvCache[i].field))
  208. {
  209. def = gefvCache[i].pcache;
  210. goto Done;
  211. }
  212. }
  213. def = ED_FindField (field);
  214. if (strlen(field) < MAX_FIELD_LEN)
  215. {
  216. gefvCache[rep].pcache = def;
  217. strcpy (gefvCache[rep].field, field);
  218. rep ^= 1;
  219. }
  220. Done:
  221. if (!def)
  222. return NULL;
  223. return (eval_t *)((char *)&ed->v + def->ofs*4);
  224. }
  225. /*
  226. ============
  227. PR_ValueString
  228. Returns a string describing *data in a type specific manner
  229. =============
  230. */
  231. char *PR_ValueString (etype_t type, eval_t *val)
  232. {
  233. static char line[256];
  234. ddef_t *def;
  235. dfunction_t *f;
  236. type &= ~DEF_SAVEGLOBAL;
  237. switch (type)
  238. {
  239. case ev_string:
  240. sprintf (line, "%s", pr_strings + val->string);
  241. break;
  242. case ev_entity:
  243. sprintf (line, "entity %i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)) );
  244. break;
  245. case ev_function:
  246. f = pr_functions + val->function;
  247. sprintf (line, "%s()", pr_strings + f->s_name);
  248. break;
  249. case ev_field:
  250. def = ED_FieldAtOfs ( val->_int );
  251. sprintf (line, ".%s", pr_strings + def->s_name);
  252. break;
  253. case ev_void:
  254. sprintf (line, "void");
  255. break;
  256. case ev_float:
  257. sprintf (line, "%5.1f", val->_float);
  258. break;
  259. case ev_vector:
  260. sprintf (line, "'%5.1f %5.1f %5.1f'", val->vector[0], val->vector[1], val->vector[2]);
  261. break;
  262. case ev_pointer:
  263. sprintf (line, "pointer");
  264. break;
  265. default:
  266. sprintf (line, "bad type %i", type);
  267. break;
  268. }
  269. return line;
  270. }
  271. /*
  272. ============
  273. PR_UglyValueString
  274. Returns a string describing *data in a type specific manner
  275. Easier to parse than PR_ValueString
  276. =============
  277. */
  278. char *PR_UglyValueString (etype_t type, eval_t *val)
  279. {
  280. static char line[256];
  281. ddef_t *def;
  282. dfunction_t *f;
  283. type &= ~DEF_SAVEGLOBAL;
  284. switch (type)
  285. {
  286. case ev_string:
  287. sprintf (line, "%s", pr_strings + val->string);
  288. break;
  289. case ev_entity:
  290. sprintf (line, "%i", NUM_FOR_EDICT(PROG_TO_EDICT(val->edict)));
  291. break;
  292. case ev_function:
  293. f = pr_functions + val->function;
  294. sprintf (line, "%s", pr_strings + f->s_name);
  295. break;
  296. case ev_field:
  297. def = ED_FieldAtOfs ( val->_int );
  298. sprintf (line, "%s", pr_strings + def->s_name);
  299. break;
  300. case ev_void:
  301. sprintf (line, "void");
  302. break;
  303. case ev_float:
  304. sprintf (line, "%f", val->_float);
  305. break;
  306. case ev_vector:
  307. sprintf (line, "%f %f %f", val->vector[0], val->vector[1], val->vector[2]);
  308. break;
  309. default:
  310. sprintf (line, "bad type %i", type);
  311. break;
  312. }
  313. return line;
  314. }
  315. /*
  316. ============
  317. PR_GlobalString
  318. Returns a string with a description and the contents of a global,
  319. padded to 20 field width
  320. ============
  321. */
  322. char *PR_GlobalString (int ofs)
  323. {
  324. char *s;
  325. int i;
  326. ddef_t *def;
  327. void *val;
  328. static char line[128];
  329. val = (void *)&pr_globals[ofs];
  330. def = ED_GlobalAtOfs(ofs);
  331. if (!def)
  332. sprintf (line,"%i(???)", ofs);
  333. else
  334. {
  335. s = PR_ValueString (def->type, val);
  336. sprintf (line,"%i(%s)%s", ofs, pr_strings + def->s_name, s);
  337. }
  338. i = strlen(line);
  339. for ( ; i<20 ; i++)
  340. strcat (line," ");
  341. strcat (line," ");
  342. return line;
  343. }
  344. char *PR_GlobalStringNoContents (int ofs)
  345. {
  346. int i;
  347. ddef_t *def;
  348. static char line[128];
  349. def = ED_GlobalAtOfs(ofs);
  350. if (!def)
  351. sprintf (line,"%i(???)", ofs);
  352. else
  353. sprintf (line,"%i(%s)", ofs, pr_strings + def->s_name);
  354. i = strlen(line);
  355. for ( ; i<20 ; i++)
  356. strcat (line," ");
  357. strcat (line," ");
  358. return line;
  359. }
  360. /*
  361. =============
  362. ED_Print
  363. For debugging
  364. =============
  365. */
  366. void ED_Print (edict_t *ed)
  367. {
  368. int l;
  369. ddef_t *d;
  370. int *v;
  371. int i, j;
  372. char *name;
  373. int type;
  374. if (ed->free)
  375. {
  376. Con_Printf ("FREE\n");
  377. return;
  378. }
  379. Con_Printf("\nEDICT %i:\n", NUM_FOR_EDICT(ed));
  380. for (i=1 ; i<progs->numfielddefs ; i++)
  381. {
  382. d = &pr_fielddefs[i];
  383. name = pr_strings + d->s_name;
  384. if (name[strlen(name)-2] == '_')
  385. continue; // skip _x, _y, _z vars
  386. v = (int *)((char *)&ed->v + d->ofs*4);
  387. // if the value is still all 0, skip the field
  388. type = d->type & ~DEF_SAVEGLOBAL;
  389. for (j=0 ; j<type_size[type] ; j++)
  390. if (v[j])
  391. break;
  392. if (j == type_size[type])
  393. continue;
  394. Con_Printf ("%s",name);
  395. l = strlen (name);
  396. while (l++ < 15)
  397. Con_Printf (" ");
  398. Con_Printf ("%s\n", PR_ValueString(d->type, (eval_t *)v));
  399. }
  400. }
  401. /*
  402. =============
  403. ED_Write
  404. For savegames
  405. =============
  406. */
  407. void ED_Write (FILE *f, edict_t *ed)
  408. {
  409. ddef_t *d;
  410. int *v;
  411. int i, j;
  412. char *name;
  413. int type;
  414. fprintf (f, "{\n");
  415. if (ed->free)
  416. {
  417. fprintf (f, "}\n");
  418. return;
  419. }
  420. for (i=1 ; i<progs->numfielddefs ; i++)
  421. {
  422. d = &pr_fielddefs[i];
  423. name = pr_strings + d->s_name;
  424. if (name[strlen(name)-2] == '_')
  425. continue; // skip _x, _y, _z vars
  426. v = (int *)((char *)&ed->v + d->ofs*4);
  427. // if the value is still all 0, skip the field
  428. type = d->type & ~DEF_SAVEGLOBAL;
  429. for (j=0 ; j<type_size[type] ; j++)
  430. if (v[j])
  431. break;
  432. if (j == type_size[type])
  433. continue;
  434. fprintf (f,"\"%s\" ",name);
  435. fprintf (f,"\"%s\"\n", PR_UglyValueString(d->type, (eval_t *)v));
  436. }
  437. fprintf (f, "}\n");
  438. }
  439. void ED_PrintNum (int ent)
  440. {
  441. ED_Print (EDICT_NUM(ent));
  442. }
  443. /*
  444. =============
  445. ED_PrintEdicts
  446. For debugging, prints all the entities in the current server
  447. =============
  448. */
  449. void ED_PrintEdicts (void)
  450. {
  451. int i;
  452. Con_Printf ("%i entities\n", sv.num_edicts);
  453. for (i=0 ; i<sv.num_edicts ; i++)
  454. ED_PrintNum (i);
  455. }
  456. /*
  457. =============
  458. ED_PrintEdict_f
  459. For debugging, prints a single edicy
  460. =============
  461. */
  462. void ED_PrintEdict_f (void)
  463. {
  464. int i;
  465. i = Q_atoi (Cmd_Argv(1));
  466. if (i >= sv.num_edicts)
  467. {
  468. Con_Printf("Bad edict number\n");
  469. return;
  470. }
  471. ED_PrintNum (i);
  472. }
  473. /*
  474. =============
  475. ED_Count
  476. For debugging
  477. =============
  478. */
  479. void ED_Count (void)
  480. {
  481. int i;
  482. edict_t *ent;
  483. int active, models, solid, step;
  484. active = models = solid = step = 0;
  485. for (i=0 ; i<sv.num_edicts ; i++)
  486. {
  487. ent = EDICT_NUM(i);
  488. if (ent->free)
  489. continue;
  490. active++;
  491. if (ent->v.solid)
  492. solid++;
  493. if (ent->v.model)
  494. models++;
  495. if (ent->v.movetype == MOVETYPE_STEP)
  496. step++;
  497. }
  498. Con_Printf ("num_edicts:%3i\n", sv.num_edicts);
  499. Con_Printf ("active :%3i\n", active);
  500. Con_Printf ("view :%3i\n", models);
  501. Con_Printf ("touch :%3i\n", solid);
  502. Con_Printf ("step :%3i\n", step);
  503. }
  504. /*
  505. ==============================================================================
  506. ARCHIVING GLOBALS
  507. FIXME: need to tag constants, doesn't really work
  508. ==============================================================================
  509. */
  510. /*
  511. =============
  512. ED_WriteGlobals
  513. =============
  514. */
  515. void ED_WriteGlobals (FILE *f)
  516. {
  517. ddef_t *def;
  518. int i;
  519. char *name;
  520. int type;
  521. fprintf (f,"{\n");
  522. for (i=0 ; i<progs->numglobaldefs ; i++)
  523. {
  524. def = &pr_globaldefs[i];
  525. type = def->type;
  526. if ( !(def->type & DEF_SAVEGLOBAL) )
  527. continue;
  528. type &= ~DEF_SAVEGLOBAL;
  529. if (type != ev_string
  530. && type != ev_float
  531. && type != ev_entity)
  532. continue;
  533. name = pr_strings + def->s_name;
  534. fprintf (f,"\"%s\" ", name);
  535. fprintf (f,"\"%s\"\n", PR_UglyValueString(type, (eval_t *)&pr_globals[def->ofs]));
  536. }
  537. fprintf (f,"}\n");
  538. }
  539. /*
  540. =============
  541. ED_ParseGlobals
  542. =============
  543. */
  544. void ED_ParseGlobals (char *data)
  545. {
  546. char keyname[64];
  547. ddef_t *key;
  548. while (1)
  549. {
  550. // parse key
  551. data = COM_Parse (data);
  552. if (com_token[0] == '}')
  553. break;
  554. if (!data)
  555. Sys_Error ("ED_ParseEntity: EOF without closing brace");
  556. strcpy (keyname, com_token);
  557. // parse value
  558. data = COM_Parse (data);
  559. if (!data)
  560. Sys_Error ("ED_ParseEntity: EOF without closing brace");
  561. if (com_token[0] == '}')
  562. Sys_Error ("ED_ParseEntity: closing brace without data");
  563. key = ED_FindGlobal (keyname);
  564. if (!key)
  565. {
  566. Con_Printf ("'%s' is not a global\n", keyname);
  567. continue;
  568. }
  569. if (!ED_ParseEpair ((void *)pr_globals, key, com_token))
  570. Host_Error ("ED_ParseGlobals: parse error");
  571. }
  572. }
  573. //============================================================================
  574. /*
  575. =============
  576. ED_NewString
  577. =============
  578. */
  579. char *ED_NewString (char *string)
  580. {
  581. char *new, *new_p;
  582. int i,l;
  583. l = strlen(string) + 1;
  584. new = Hunk_Alloc (l);
  585. new_p = new;
  586. for (i=0 ; i< l ; i++)
  587. {
  588. if (string[i] == '\\' && i < l-1)
  589. {
  590. i++;
  591. if (string[i] == 'n')
  592. *new_p++ = '\n';
  593. else
  594. *new_p++ = '\\';
  595. }
  596. else
  597. *new_p++ = string[i];
  598. }
  599. return new;
  600. }
  601. /*
  602. =============
  603. ED_ParseEval
  604. Can parse either fields or globals
  605. returns false if error
  606. =============
  607. */
  608. qboolean ED_ParseEpair (void *base, ddef_t *key, char *s)
  609. {
  610. int i;
  611. char string[128];
  612. ddef_t *def;
  613. char *v, *w;
  614. void *d;
  615. dfunction_t *func;
  616. d = (void *)((int *)base + key->ofs);
  617. switch (key->type & ~DEF_SAVEGLOBAL)
  618. {
  619. case ev_string:
  620. *(string_t *)d = ED_NewString (s) - pr_strings;
  621. break;
  622. case ev_float:
  623. *(float *)d = atof (s);
  624. break;
  625. case ev_vector:
  626. strcpy (string, s);
  627. v = string;
  628. w = string;
  629. for (i=0 ; i<3 ; i++)
  630. {
  631. while (*v && *v != ' ')
  632. v++;
  633. *v = 0;
  634. ((float *)d)[i] = atof (w);
  635. w = v = v+1;
  636. }
  637. break;
  638. case ev_entity:
  639. *(int *)d = EDICT_TO_PROG(EDICT_NUM(atoi (s)));
  640. break;
  641. case ev_field:
  642. def = ED_FindField (s);
  643. if (!def)
  644. {
  645. Con_Printf ("Can't find field %s\n", s);
  646. return false;
  647. }
  648. *(int *)d = G_INT(def->ofs);
  649. break;
  650. case ev_function:
  651. func = ED_FindFunction (s);
  652. if (!func)
  653. {
  654. Con_Printf ("Can't find function %s\n", s);
  655. return false;
  656. }
  657. *(func_t *)d = func - pr_functions;
  658. break;
  659. default:
  660. break;
  661. }
  662. return true;
  663. }
  664. /*
  665. ====================
  666. ED_ParseEdict
  667. Parses an edict out of the given string, returning the new position
  668. ed should be a properly initialized empty edict.
  669. Used for initial level load and for savegames.
  670. ====================
  671. */
  672. char *ED_ParseEdict (char *data, edict_t *ent)
  673. {
  674. ddef_t *key;
  675. qboolean anglehack;
  676. qboolean init;
  677. char keyname[256];
  678. int n;
  679. init = false;
  680. // clear it
  681. if (ent != sv.edicts) // hack
  682. memset (&ent->v, 0, progs->entityfields * 4);
  683. // go through all the dictionary pairs
  684. while (1)
  685. {
  686. // parse key
  687. data = COM_Parse (data);
  688. if (com_token[0] == '}')
  689. break;
  690. if (!data)
  691. Sys_Error ("ED_ParseEntity: EOF without closing brace");
  692. // anglehack is to allow QuakeEd to write single scalar angles
  693. // and allow them to be turned into vectors. (FIXME...)
  694. if (!strcmp(com_token, "angle"))
  695. {
  696. strcpy (com_token, "angles");
  697. anglehack = true;
  698. }
  699. else
  700. anglehack = false;
  701. // FIXME: change light to _light to get rid of this hack
  702. if (!strcmp(com_token, "light"))
  703. strcpy (com_token, "light_lev"); // hack for single light def
  704. strcpy (keyname, com_token);
  705. // another hack to fix heynames with trailing spaces
  706. n = strlen(keyname);
  707. while (n && keyname[n-1] == ' ')
  708. {
  709. keyname[n-1] = 0;
  710. n--;
  711. }
  712. // parse value
  713. data = COM_Parse (data);
  714. if (!data)
  715. Sys_Error ("ED_ParseEntity: EOF without closing brace");
  716. if (com_token[0] == '}')
  717. Sys_Error ("ED_ParseEntity: closing brace without data");
  718. init = true;
  719. // keynames with a leading underscore are used for utility comments,
  720. // and are immediately discarded by quake
  721. if (keyname[0] == '_')
  722. continue;
  723. key = ED_FindField (keyname);
  724. if (!key)
  725. {
  726. Con_Printf ("'%s' is not a field\n", keyname);
  727. continue;
  728. }
  729. if (anglehack)
  730. {
  731. char temp[32];
  732. strcpy (temp, com_token);
  733. sprintf (com_token, "0 %s 0", temp);
  734. }
  735. if (!ED_ParseEpair ((void *)&ent->v, key, com_token))
  736. Host_Error ("ED_ParseEdict: parse error");
  737. }
  738. if (!init)
  739. ent->free = true;
  740. return data;
  741. }
  742. /*
  743. ================
  744. ED_LoadFromFile
  745. The entities are directly placed in the array, rather than allocated with
  746. ED_Alloc, because otherwise an error loading the map would have entity
  747. number references out of order.
  748. Creates a server's entity / program execution context by
  749. parsing textual entity definitions out of an ent file.
  750. Used for both fresh maps and savegame loads. A fresh map would also need
  751. to call ED_CallSpawnFunctions () to let the objects initialize themselves.
  752. ================
  753. */
  754. void ED_LoadFromFile (char *data)
  755. {
  756. edict_t *ent;
  757. int inhibit;
  758. dfunction_t *func;
  759. ent = NULL;
  760. inhibit = 0;
  761. pr_global_struct->time = sv.time;
  762. // parse ents
  763. while (1)
  764. {
  765. // parse the opening brace
  766. data = COM_Parse (data);
  767. if (!data)
  768. break;
  769. if (com_token[0] != '{')
  770. Sys_Error ("ED_LoadFromFile: found %s when expecting {",com_token);
  771. if (!ent)
  772. ent = EDICT_NUM(0);
  773. else
  774. ent = ED_Alloc ();
  775. data = ED_ParseEdict (data, ent);
  776. // remove things from different skill levels or deathmatch
  777. if (deathmatch.value)
  778. {
  779. if (((int)ent->v.spawnflags & SPAWNFLAG_NOT_DEATHMATCH))
  780. {
  781. ED_Free (ent);
  782. inhibit++;
  783. continue;
  784. }
  785. }
  786. else if ((current_skill == 0 && ((int)ent->v.spawnflags & SPAWNFLAG_NOT_EASY))
  787. || (current_skill == 1 && ((int)ent->v.spawnflags & SPAWNFLAG_NOT_MEDIUM))
  788. || (current_skill >= 2 && ((int)ent->v.spawnflags & SPAWNFLAG_NOT_HARD)) )
  789. {
  790. ED_Free (ent);
  791. inhibit++;
  792. continue;
  793. }
  794. //
  795. // immediately call spawn function
  796. //
  797. if (!ent->v.classname)
  798. {
  799. Con_Printf ("No classname for:\n");
  800. ED_Print (ent);
  801. ED_Free (ent);
  802. continue;
  803. }
  804. // look for the spawn function
  805. func = ED_FindFunction ( pr_strings + ent->v.classname );
  806. if (!func)
  807. {
  808. Con_Printf ("No spawn function for:\n");
  809. ED_Print (ent);
  810. ED_Free (ent);
  811. continue;
  812. }
  813. pr_global_struct->self = EDICT_TO_PROG(ent);
  814. PR_ExecuteProgram (func - pr_functions);
  815. }
  816. Con_DPrintf ("%i entities inhibited\n", inhibit);
  817. }
  818. /*
  819. ===============
  820. PR_LoadProgs
  821. ===============
  822. */
  823. void PR_LoadProgs (void)
  824. {
  825. int i;
  826. // flush the non-C variable lookup cache
  827. for (i=0 ; i<GEFV_CACHESIZE ; i++)
  828. gefvCache[i].field[0] = 0;
  829. CRC_Init (&pr_crc);
  830. progs = (dprograms_t *)COM_LoadHunkFile ("progs.dat");
  831. if (!progs)
  832. Sys_Error ("PR_LoadProgs: couldn't load progs.dat");
  833. Con_DPrintf ("Programs occupy %iK.\n", com_filesize/1024);
  834. for (i=0 ; i<com_filesize ; i++)
  835. CRC_ProcessByte (&pr_crc, ((byte *)progs)[i]);
  836. // byte swap the header
  837. for (i=0 ; i<sizeof(*progs)/4 ; i++)
  838. ((int *)progs)[i] = LittleLong ( ((int *)progs)[i] );
  839. if (progs->version != PROG_VERSION)
  840. Sys_Error ("progs.dat has wrong version number (%i should be %i)", progs->version, PROG_VERSION);
  841. if (progs->crc != PROGHEADER_CRC)
  842. Sys_Error ("progs.dat system vars have been modified, progdefs.h is out of date");
  843. pr_functions = (dfunction_t *)((byte *)progs + progs->ofs_functions);
  844. pr_strings = (char *)progs + progs->ofs_strings;
  845. pr_globaldefs = (ddef_t *)((byte *)progs + progs->ofs_globaldefs);
  846. pr_fielddefs = (ddef_t *)((byte *)progs + progs->ofs_fielddefs);
  847. pr_statements = (dstatement_t *)((byte *)progs + progs->ofs_statements);
  848. pr_global_struct = (globalvars_t *)((byte *)progs + progs->ofs_globals);
  849. pr_globals = (float *)pr_global_struct;
  850. pr_edict_size = progs->entityfields * 4 + sizeof (edict_t) - sizeof(entvars_t);
  851. // byte swap the lumps
  852. for (i=0 ; i<progs->numstatements ; i++)
  853. {
  854. pr_statements[i].op = LittleShort(pr_statements[i].op);
  855. pr_statements[i].a = LittleShort(pr_statements[i].a);
  856. pr_statements[i].b = LittleShort(pr_statements[i].b);
  857. pr_statements[i].c = LittleShort(pr_statements[i].c);
  858. }
  859. for (i=0 ; i<progs->numfunctions; i++)
  860. {
  861. pr_functions[i].first_statement = LittleLong (pr_functions[i].first_statement);
  862. pr_functions[i].parm_start = LittleLong (pr_functions[i].parm_start);
  863. pr_functions[i].s_name = LittleLong (pr_functions[i].s_name);
  864. pr_functions[i].s_file = LittleLong (pr_functions[i].s_file);
  865. pr_functions[i].numparms = LittleLong (pr_functions[i].numparms);
  866. pr_functions[i].locals = LittleLong (pr_functions[i].locals);
  867. }
  868. for (i=0 ; i<progs->numglobaldefs ; i++)
  869. {
  870. pr_globaldefs[i].type = LittleShort (pr_globaldefs[i].type);
  871. pr_globaldefs[i].ofs = LittleShort (pr_globaldefs[i].ofs);
  872. pr_globaldefs[i].s_name = LittleLong (pr_globaldefs[i].s_name);
  873. }
  874. for (i=0 ; i<progs->numfielddefs ; i++)
  875. {
  876. pr_fielddefs[i].type = LittleShort (pr_fielddefs[i].type);
  877. if (pr_fielddefs[i].type & DEF_SAVEGLOBAL)
  878. Sys_Error ("PR_LoadProgs: pr_fielddefs[i].type & DEF_SAVEGLOBAL");
  879. pr_fielddefs[i].ofs = LittleShort (pr_fielddefs[i].ofs);
  880. pr_fielddefs[i].s_name = LittleLong (pr_fielddefs[i].s_name);
  881. }
  882. for (i=0 ; i<progs->numglobals ; i++)
  883. ((int *)pr_globals)[i] = LittleLong (((int *)pr_globals)[i]);
  884. }
  885. /*
  886. ===============
  887. PR_Init
  888. ===============
  889. */
  890. void PR_Init (void)
  891. {
  892. Cmd_AddCommand ("edict", ED_PrintEdict_f);
  893. Cmd_AddCommand ("edicts", ED_PrintEdicts);
  894. Cmd_AddCommand ("edictcount", ED_Count);
  895. Cmd_AddCommand ("profile", PR_Profile_f);
  896. Cvar_RegisterVariable (&nomonsters);
  897. Cvar_RegisterVariable (&gamecfg);
  898. Cvar_RegisterVariable (&scratch1);
  899. Cvar_RegisterVariable (&scratch2);
  900. Cvar_RegisterVariable (&scratch3);
  901. Cvar_RegisterVariable (&scratch4);
  902. Cvar_RegisterVariable (&savedgamecfg);
  903. Cvar_RegisterVariable (&saved1);
  904. Cvar_RegisterVariable (&saved2);
  905. Cvar_RegisterVariable (&saved3);
  906. Cvar_RegisterVariable (&saved4);
  907. }
  908. edict_t *EDICT_NUM(int n)
  909. {
  910. if (n < 0 || n >= sv.max_edicts)
  911. Sys_Error ("EDICT_NUM: bad number %i", n);
  912. return (edict_t *)((byte *)sv.edicts+ (n)*pr_edict_size);
  913. }
  914. int NUM_FOR_EDICT(edict_t *e)
  915. {
  916. int b;
  917. b = (byte *)e - (byte *)sv.edicts;
  918. b = b / pr_edict_size;
  919. if (b < 0 || b >= sv.num_edicts)
  920. Sys_Error ("NUM_FOR_EDICT: bad pointer");
  921. return b;
  922. }