pr_edict.c 21 KB

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