PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/pr_edict.c

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