bspfile.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "cmdlib.h"
  19. #include "mathlib.h"
  20. #include "bspfile.h"
  21. #include "scriplib.h"
  22. void GetLeafNums (void);
  23. //=============================================================================
  24. int nummodels;
  25. dmodel_t dmodels[MAX_MAP_MODELS];
  26. int visdatasize;
  27. byte dvisdata[MAX_MAP_VISIBILITY];
  28. dvis_t *dvis = (dvis_t *)dvisdata;
  29. int lightdatasize;
  30. byte dlightdata[MAX_MAP_LIGHTING];
  31. int entdatasize;
  32. char dentdata[MAX_MAP_ENTSTRING];
  33. int numleafs;
  34. dleaf_t dleafs[MAX_MAP_LEAFS];
  35. int numplanes;
  36. dplane_t dplanes[MAX_MAP_PLANES];
  37. int numvertexes;
  38. dvertex_t dvertexes[MAX_MAP_VERTS];
  39. int numnodes;
  40. dnode_t dnodes[MAX_MAP_NODES];
  41. int numtexinfo;
  42. texinfo_t texinfo[MAX_MAP_TEXINFO];
  43. int numfaces;
  44. dface_t dfaces[MAX_MAP_FACES];
  45. int numedges;
  46. dedge_t dedges[MAX_MAP_EDGES];
  47. int numleaffaces;
  48. unsigned short dleaffaces[MAX_MAP_LEAFFACES];
  49. int numleafbrushes;
  50. unsigned short dleafbrushes[MAX_MAP_LEAFBRUSHES];
  51. int numsurfedges;
  52. int dsurfedges[MAX_MAP_SURFEDGES];
  53. int numbrushes;
  54. dbrush_t dbrushes[MAX_MAP_BRUSHES];
  55. int numbrushsides;
  56. dbrushside_t dbrushsides[MAX_MAP_BRUSHSIDES];
  57. int numareas;
  58. darea_t dareas[MAX_MAP_AREAS];
  59. int numareaportals;
  60. dareaportal_t dareaportals[MAX_MAP_AREAPORTALS];
  61. byte dpop[256];
  62. /*
  63. ===============
  64. CompressVis
  65. ===============
  66. */
  67. int CompressVis (byte *vis, byte *dest)
  68. {
  69. int j;
  70. int rep;
  71. int visrow;
  72. byte *dest_p;
  73. dest_p = dest;
  74. // visrow = (r_numvisleafs + 7)>>3;
  75. visrow = (dvis->numclusters + 7)>>3;
  76. for (j=0 ; j<visrow ; j++)
  77. {
  78. *dest_p++ = vis[j];
  79. if (vis[j])
  80. continue;
  81. rep = 1;
  82. for ( j++; j<visrow ; j++)
  83. if (vis[j] || rep == 255)
  84. break;
  85. else
  86. rep++;
  87. *dest_p++ = rep;
  88. j--;
  89. }
  90. return dest_p - dest;
  91. }
  92. /*
  93. ===================
  94. DecompressVis
  95. ===================
  96. */
  97. void DecompressVis (byte *in, byte *decompressed)
  98. {
  99. int c;
  100. byte *out;
  101. int row;
  102. // row = (r_numvisleafs+7)>>3;
  103. row = (dvis->numclusters+7)>>3;
  104. out = decompressed;
  105. do
  106. {
  107. if (*in)
  108. {
  109. *out++ = *in++;
  110. continue;
  111. }
  112. c = in[1];
  113. if (!c)
  114. Error ("DecompressVis: 0 repeat");
  115. in += 2;
  116. while (c)
  117. {
  118. *out++ = 0;
  119. c--;
  120. }
  121. } while (out - decompressed < row);
  122. }
  123. //=============================================================================
  124. /*
  125. =============
  126. SwapBSPFile
  127. Byte swaps all data in a bsp file.
  128. =============
  129. */
  130. void SwapBSPFile (qboolean todisk)
  131. {
  132. int i, j;
  133. dmodel_t *d;
  134. // models
  135. for (i=0 ; i<nummodels ; i++)
  136. {
  137. d = &dmodels[i];
  138. d->firstface = LittleLong (d->firstface);
  139. d->numfaces = LittleLong (d->numfaces);
  140. d->headnode = LittleLong (d->headnode);
  141. for (j=0 ; j<3 ; j++)
  142. {
  143. d->mins[j] = LittleFloat(d->mins[j]);
  144. d->maxs[j] = LittleFloat(d->maxs[j]);
  145. d->origin[j] = LittleFloat(d->origin[j]);
  146. }
  147. }
  148. //
  149. // vertexes
  150. //
  151. for (i=0 ; i<numvertexes ; i++)
  152. {
  153. for (j=0 ; j<3 ; j++)
  154. dvertexes[i].point[j] = LittleFloat (dvertexes[i].point[j]);
  155. }
  156. //
  157. // planes
  158. //
  159. for (i=0 ; i<numplanes ; i++)
  160. {
  161. for (j=0 ; j<3 ; j++)
  162. dplanes[i].normal[j] = LittleFloat (dplanes[i].normal[j]);
  163. dplanes[i].dist = LittleFloat (dplanes[i].dist);
  164. dplanes[i].type = LittleLong (dplanes[i].type);
  165. }
  166. //
  167. // texinfos
  168. //
  169. for (i=0 ; i<numtexinfo ; i++)
  170. {
  171. for (j=0 ; j<8 ; j++)
  172. texinfo[i].vecs[0][j] = LittleFloat (texinfo[i].vecs[0][j]);
  173. texinfo[i].flags = LittleLong (texinfo[i].flags);
  174. texinfo[i].value = LittleLong (texinfo[i].value);
  175. texinfo[i].nexttexinfo = LittleLong (texinfo[i].nexttexinfo);
  176. }
  177. //
  178. // faces
  179. //
  180. for (i=0 ; i<numfaces ; i++)
  181. {
  182. dfaces[i].texinfo = LittleShort (dfaces[i].texinfo);
  183. dfaces[i].planenum = LittleShort (dfaces[i].planenum);
  184. dfaces[i].side = LittleShort (dfaces[i].side);
  185. dfaces[i].lightofs = LittleLong (dfaces[i].lightofs);
  186. dfaces[i].firstedge = LittleLong (dfaces[i].firstedge);
  187. dfaces[i].numedges = LittleShort (dfaces[i].numedges);
  188. }
  189. //
  190. // nodes
  191. //
  192. for (i=0 ; i<numnodes ; i++)
  193. {
  194. dnodes[i].planenum = LittleLong (dnodes[i].planenum);
  195. for (j=0 ; j<3 ; j++)
  196. {
  197. dnodes[i].mins[j] = LittleShort (dnodes[i].mins[j]);
  198. dnodes[i].maxs[j] = LittleShort (dnodes[i].maxs[j]);
  199. }
  200. dnodes[i].children[0] = LittleLong (dnodes[i].children[0]);
  201. dnodes[i].children[1] = LittleLong (dnodes[i].children[1]);
  202. dnodes[i].firstface = LittleShort (dnodes[i].firstface);
  203. dnodes[i].numfaces = LittleShort (dnodes[i].numfaces);
  204. }
  205. //
  206. // leafs
  207. //
  208. for (i=0 ; i<numleafs ; i++)
  209. {
  210. dleafs[i].contents = LittleLong (dleafs[i].contents);
  211. dleafs[i].cluster = LittleShort (dleafs[i].cluster);
  212. dleafs[i].area = LittleShort (dleafs[i].area);
  213. for (j=0 ; j<3 ; j++)
  214. {
  215. dleafs[i].mins[j] = LittleShort (dleafs[i].mins[j]);
  216. dleafs[i].maxs[j] = LittleShort (dleafs[i].maxs[j]);
  217. }
  218. dleafs[i].firstleafface = LittleShort (dleafs[i].firstleafface);
  219. dleafs[i].numleaffaces = LittleShort (dleafs[i].numleaffaces);
  220. dleafs[i].firstleafbrush = LittleShort (dleafs[i].firstleafbrush);
  221. dleafs[i].numleafbrushes = LittleShort (dleafs[i].numleafbrushes);
  222. }
  223. //
  224. // leaffaces
  225. //
  226. for (i=0 ; i<numleaffaces ; i++)
  227. dleaffaces[i] = LittleShort (dleaffaces[i]);
  228. //
  229. // leafbrushes
  230. //
  231. for (i=0 ; i<numleafbrushes ; i++)
  232. dleafbrushes[i] = LittleShort (dleafbrushes[i]);
  233. //
  234. // surfedges
  235. //
  236. for (i=0 ; i<numsurfedges ; i++)
  237. dsurfedges[i] = LittleLong (dsurfedges[i]);
  238. //
  239. // edges
  240. //
  241. for (i=0 ; i<numedges ; i++)
  242. {
  243. dedges[i].v[0] = LittleShort (dedges[i].v[0]);
  244. dedges[i].v[1] = LittleShort (dedges[i].v[1]);
  245. }
  246. //
  247. // brushes
  248. //
  249. for (i=0 ; i<numbrushes ; i++)
  250. {
  251. dbrushes[i].firstside = LittleLong (dbrushes[i].firstside);
  252. dbrushes[i].numsides = LittleLong (dbrushes[i].numsides);
  253. dbrushes[i].contents = LittleLong (dbrushes[i].contents);
  254. }
  255. //
  256. // areas
  257. //
  258. for (i=0 ; i<numareas ; i++)
  259. {
  260. dareas[i].numareaportals = LittleLong (dareas[i].numareaportals);
  261. dareas[i].firstareaportal = LittleLong (dareas[i].firstareaportal);
  262. }
  263. //
  264. // areasportals
  265. //
  266. for (i=0 ; i<numareaportals ; i++)
  267. {
  268. dareaportals[i].portalnum = LittleLong (dareaportals[i].portalnum);
  269. dareaportals[i].otherarea = LittleLong (dareaportals[i].otherarea);
  270. }
  271. //
  272. // brushsides
  273. //
  274. for (i=0 ; i<numbrushsides ; i++)
  275. {
  276. dbrushsides[i].planenum = LittleShort (dbrushsides[i].planenum);
  277. dbrushsides[i].texinfo = LittleShort (dbrushsides[i].texinfo);
  278. }
  279. //
  280. // visibility
  281. //
  282. if (todisk)
  283. j = dvis->numclusters;
  284. else
  285. j = LittleLong(dvis->numclusters);
  286. dvis->numclusters = LittleLong (dvis->numclusters);
  287. for (i=0 ; i<j ; i++)
  288. {
  289. dvis->bitofs[i][0] = LittleLong (dvis->bitofs[i][0]);
  290. dvis->bitofs[i][1] = LittleLong (dvis->bitofs[i][1]);
  291. }
  292. }
  293. dheader_t *header;
  294. int CopyLump (int lump, void *dest, int size)
  295. {
  296. int length, ofs;
  297. length = header->lumps[lump].filelen;
  298. ofs = header->lumps[lump].fileofs;
  299. if (length % size)
  300. Error ("LoadBSPFile: odd lump size");
  301. memcpy (dest, (byte *)header + ofs, length);
  302. return length / size;
  303. }
  304. /*
  305. =============
  306. LoadBSPFile
  307. =============
  308. */
  309. void LoadBSPFile (char *filename)
  310. {
  311. int i;
  312. //
  313. // load the file header
  314. //
  315. LoadFile (filename, (void **)&header);
  316. // swap the header
  317. for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
  318. ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
  319. if (header->ident != IDBSPHEADER)
  320. Error ("%s is not a IBSP file", filename);
  321. if (header->version != BSPVERSION)
  322. Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
  323. nummodels = CopyLump (LUMP_MODELS, dmodels, sizeof(dmodel_t));
  324. numvertexes = CopyLump (LUMP_VERTEXES, dvertexes, sizeof(dvertex_t));
  325. numplanes = CopyLump (LUMP_PLANES, dplanes, sizeof(dplane_t));
  326. numleafs = CopyLump (LUMP_LEAFS, dleafs, sizeof(dleaf_t));
  327. numnodes = CopyLump (LUMP_NODES, dnodes, sizeof(dnode_t));
  328. numtexinfo = CopyLump (LUMP_TEXINFO, texinfo, sizeof(texinfo_t));
  329. numfaces = CopyLump (LUMP_FACES, dfaces, sizeof(dface_t));
  330. numleaffaces = CopyLump (LUMP_LEAFFACES, dleaffaces, sizeof(dleaffaces[0]));
  331. numleafbrushes = CopyLump (LUMP_LEAFBRUSHES, dleafbrushes, sizeof(dleafbrushes[0]));
  332. numsurfedges = CopyLump (LUMP_SURFEDGES, dsurfedges, sizeof(dsurfedges[0]));
  333. numedges = CopyLump (LUMP_EDGES, dedges, sizeof(dedge_t));
  334. numbrushes = CopyLump (LUMP_BRUSHES, dbrushes, sizeof(dbrush_t));
  335. numbrushsides = CopyLump (LUMP_BRUSHSIDES, dbrushsides, sizeof(dbrushside_t));
  336. numareas = CopyLump (LUMP_AREAS, dareas, sizeof(darea_t));
  337. numareaportals = CopyLump (LUMP_AREAPORTALS, dareaportals, sizeof(dareaportal_t));
  338. visdatasize = CopyLump (LUMP_VISIBILITY, dvisdata, 1);
  339. lightdatasize = CopyLump (LUMP_LIGHTING, dlightdata, 1);
  340. entdatasize = CopyLump (LUMP_ENTITIES, dentdata, 1);
  341. CopyLump (LUMP_POP, dpop, 1);
  342. free (header); // everything has been copied out
  343. //
  344. // swap everything
  345. //
  346. SwapBSPFile (false);
  347. }
  348. /*
  349. =============
  350. LoadBSPFileTexinfo
  351. Only loads the texinfo lump, so qdata can scan for textures
  352. =============
  353. */
  354. void LoadBSPFileTexinfo (char *filename)
  355. {
  356. int i;
  357. FILE *f;
  358. int length, ofs;
  359. header = malloc(sizeof(dheader_t));
  360. f = fopen (filename, "rb");
  361. fread (header, sizeof(dheader_t), 1, f);
  362. // swap the header
  363. for (i=0 ; i< sizeof(dheader_t)/4 ; i++)
  364. ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
  365. if (header->ident != IDBSPHEADER)
  366. Error ("%s is not a IBSP file", filename);
  367. if (header->version != BSPVERSION)
  368. Error ("%s is version %i, not %i", filename, header->version, BSPVERSION);
  369. length = header->lumps[LUMP_TEXINFO].filelen;
  370. ofs = header->lumps[LUMP_TEXINFO].fileofs;
  371. fseek (f, ofs, SEEK_SET);
  372. fread (texinfo, length, 1, f);
  373. fclose (f);
  374. numtexinfo = length / sizeof(texinfo_t);
  375. free (header); // everything has been copied out
  376. SwapBSPFile (false);
  377. }
  378. //============================================================================
  379. FILE *wadfile;
  380. dheader_t outheader;
  381. void AddLump (int lumpnum, void *data, int len)
  382. {
  383. lump_t *lump;
  384. lump = &header->lumps[lumpnum];
  385. lump->fileofs = LittleLong( ftell(wadfile) );
  386. lump->filelen = LittleLong(len);
  387. SafeWrite (wadfile, data, (len+3)&~3);
  388. }
  389. /*
  390. =============
  391. WriteBSPFile
  392. Swaps the bsp file in place, so it should not be referenced again
  393. =============
  394. */
  395. void WriteBSPFile (char *filename)
  396. {
  397. header = &outheader;
  398. memset (header, 0, sizeof(dheader_t));
  399. SwapBSPFile (true);
  400. header->ident = LittleLong (IDBSPHEADER);
  401. header->version = LittleLong (BSPVERSION);
  402. wadfile = SafeOpenWrite (filename);
  403. SafeWrite (wadfile, header, sizeof(dheader_t)); // overwritten later
  404. AddLump (LUMP_PLANES, dplanes, numplanes*sizeof(dplane_t));
  405. AddLump (LUMP_LEAFS, dleafs, numleafs*sizeof(dleaf_t));
  406. AddLump (LUMP_VERTEXES, dvertexes, numvertexes*sizeof(dvertex_t));
  407. AddLump (LUMP_NODES, dnodes, numnodes*sizeof(dnode_t));
  408. AddLump (LUMP_TEXINFO, texinfo, numtexinfo*sizeof(texinfo_t));
  409. AddLump (LUMP_FACES, dfaces, numfaces*sizeof(dface_t));
  410. AddLump (LUMP_BRUSHES, dbrushes, numbrushes*sizeof(dbrush_t));
  411. AddLump (LUMP_BRUSHSIDES, dbrushsides, numbrushsides*sizeof(dbrushside_t));
  412. AddLump (LUMP_LEAFFACES, dleaffaces, numleaffaces*sizeof(dleaffaces[0]));
  413. AddLump (LUMP_LEAFBRUSHES, dleafbrushes, numleafbrushes*sizeof(dleafbrushes[0]));
  414. AddLump (LUMP_SURFEDGES, dsurfedges, numsurfedges*sizeof(dsurfedges[0]));
  415. AddLump (LUMP_EDGES, dedges, numedges*sizeof(dedge_t));
  416. AddLump (LUMP_MODELS, dmodels, nummodels*sizeof(dmodel_t));
  417. AddLump (LUMP_AREAS, dareas, numareas*sizeof(darea_t));
  418. AddLump (LUMP_AREAPORTALS, dareaportals, numareaportals*sizeof(dareaportal_t));
  419. AddLump (LUMP_LIGHTING, dlightdata, lightdatasize);
  420. AddLump (LUMP_VISIBILITY, dvisdata, visdatasize);
  421. AddLump (LUMP_ENTITIES, dentdata, entdatasize);
  422. AddLump (LUMP_POP, dpop, sizeof(dpop));
  423. fseek (wadfile, 0, SEEK_SET);
  424. SafeWrite (wadfile, header, sizeof(dheader_t));
  425. fclose (wadfile);
  426. }
  427. //============================================================================
  428. /*
  429. =============
  430. PrintBSPFileSizes
  431. Dumps info about current file
  432. =============
  433. */
  434. void PrintBSPFileSizes (void)
  435. {
  436. if (!num_entities)
  437. ParseEntities ();
  438. printf ("%5i models %7i\n"
  439. ,nummodels, (int)(nummodels*sizeof(dmodel_t)));
  440. printf ("%5i brushes %7i\n"
  441. ,numbrushes, (int)(numbrushes*sizeof(dbrush_t)));
  442. printf ("%5i brushsides %7i\n"
  443. ,numbrushsides, (int)(numbrushsides*sizeof(dbrushside_t)));
  444. printf ("%5i planes %7i\n"
  445. ,numplanes, (int)(numplanes*sizeof(dplane_t)));
  446. printf ("%5i texinfo %7i\n"
  447. ,numtexinfo, (int)(numtexinfo*sizeof(texinfo_t)));
  448. printf ("%5i entdata %7i\n", num_entities, entdatasize);
  449. printf ("\n");
  450. printf ("%5i vertexes %7i\n"
  451. ,numvertexes, (int)(numvertexes*sizeof(dvertex_t)));
  452. printf ("%5i nodes %7i\n"
  453. ,numnodes, (int)(numnodes*sizeof(dnode_t)));
  454. printf ("%5i faces %7i\n"
  455. ,numfaces, (int)(numfaces*sizeof(dface_t)));
  456. printf ("%5i leafs %7i\n"
  457. ,numleafs, (int)(numleafs*sizeof(dleaf_t)));
  458. printf ("%5i leaffaces %7i\n"
  459. ,numleaffaces, (int)(numleaffaces*sizeof(dleaffaces[0])));
  460. printf ("%5i leafbrushes %7i\n"
  461. ,numleafbrushes, (int)(numleafbrushes*sizeof(dleafbrushes[0])));
  462. printf ("%5i surfedges %7i\n"
  463. ,numsurfedges, (int)(numsurfedges*sizeof(dsurfedges[0])));
  464. printf ("%5i edges %7i\n"
  465. ,numedges, (int)(numedges*sizeof(dedge_t)));
  466. printf (" lightdata %7i\n", lightdatasize);
  467. printf (" visdata %7i\n", visdatasize);
  468. }
  469. //============================================
  470. int num_entities;
  471. entity_t entities[MAX_MAP_ENTITIES];
  472. void StripTrailing (char *e)
  473. {
  474. char *s;
  475. s = e + strlen(e)-1;
  476. while (s >= e && *s <= 32)
  477. {
  478. *s = 0;
  479. s--;
  480. }
  481. }
  482. /*
  483. =================
  484. ParseEpair
  485. =================
  486. */
  487. epair_t *ParseEpair (void)
  488. {
  489. epair_t *e;
  490. e = malloc (sizeof(epair_t));
  491. memset (e, 0, sizeof(epair_t));
  492. if (strlen(token) >= MAX_KEY-1)
  493. Error ("ParseEpar: token too long");
  494. e->key = copystring(token);
  495. GetToken (false);
  496. if (strlen(token) >= MAX_VALUE-1)
  497. Error ("ParseEpar: token too long");
  498. e->value = copystring(token);
  499. // strip trailing spaces
  500. StripTrailing (e->key);
  501. StripTrailing (e->value);
  502. return e;
  503. }
  504. /*
  505. ================
  506. ParseEntity
  507. ================
  508. */
  509. qboolean ParseEntity (void)
  510. {
  511. epair_t *e;
  512. entity_t *mapent;
  513. if (!GetToken (true))
  514. return false;
  515. if (strcmp (token, "{") )
  516. Error ("ParseEntity: { not found");
  517. if (num_entities == MAX_MAP_ENTITIES)
  518. Error ("num_entities == MAX_MAP_ENTITIES");
  519. mapent = &entities[num_entities];
  520. num_entities++;
  521. do
  522. {
  523. if (!GetToken (true))
  524. Error ("ParseEntity: EOF without closing brace");
  525. if (!strcmp (token, "}") )
  526. break;
  527. e = ParseEpair ();
  528. e->next = mapent->epairs;
  529. mapent->epairs = e;
  530. } while (1);
  531. return true;
  532. }
  533. /*
  534. ================
  535. ParseEntities
  536. Parses the dentdata string into entities
  537. ================
  538. */
  539. void ParseEntities (void)
  540. {
  541. num_entities = 0;
  542. ParseFromMemory (dentdata, entdatasize);
  543. while (ParseEntity ())
  544. {
  545. }
  546. }
  547. /*
  548. ================
  549. UnparseEntities
  550. Generates the dentdata string from all the entities
  551. ================
  552. */
  553. void UnparseEntities (void)
  554. {
  555. char *buf, *end;
  556. epair_t *ep;
  557. char line[2048];
  558. int i;
  559. char key[1024], value[1024];
  560. buf = dentdata;
  561. end = buf;
  562. *end = 0;
  563. for (i=0 ; i<num_entities ; i++)
  564. {
  565. ep = entities[i].epairs;
  566. if (!ep)
  567. continue; // ent got removed
  568. strcat (end,"{\n");
  569. end += 2;
  570. for (ep = entities[i].epairs ; ep ; ep=ep->next)
  571. {
  572. strcpy (key, ep->key);
  573. StripTrailing (key);
  574. strcpy (value, ep->value);
  575. StripTrailing (value);
  576. sprintf (line, "\"%s\" \"%s\"\n", key, value);
  577. strcat (end, line);
  578. end += strlen(line);
  579. }
  580. strcat (end,"}\n");
  581. end += 2;
  582. if (end > buf + MAX_MAP_ENTSTRING)
  583. Error ("Entity text too long");
  584. }
  585. entdatasize = end - buf + 1;
  586. }
  587. void PrintEntity (entity_t *ent)
  588. {
  589. epair_t *ep;
  590. printf ("------- entity %p -------\n", ent);
  591. for (ep=ent->epairs ; ep ; ep=ep->next)
  592. {
  593. printf ("%s = %s\n", ep->key, ep->value);
  594. }
  595. }
  596. void SetKeyValue (entity_t *ent, char *key, char *value)
  597. {
  598. epair_t *ep;
  599. for (ep=ent->epairs ; ep ; ep=ep->next)
  600. if (!strcmp (ep->key, key) )
  601. {
  602. free (ep->value);
  603. ep->value = copystring(value);
  604. return;
  605. }
  606. ep = malloc (sizeof(*ep));
  607. ep->next = ent->epairs;
  608. ent->epairs = ep;
  609. ep->key = copystring(key);
  610. ep->value = copystring(value);
  611. }
  612. char *ValueForKey (entity_t *ent, char *key)
  613. {
  614. epair_t *ep;
  615. for (ep=ent->epairs ; ep ; ep=ep->next)
  616. if (!strcmp (ep->key, key) )
  617. return ep->value;
  618. return "";
  619. }
  620. vec_t FloatForKey (entity_t *ent, char *key)
  621. {
  622. char *k;
  623. k = ValueForKey (ent, key);
  624. return atof(k);
  625. }
  626. void GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
  627. {
  628. char *k;
  629. double v1, v2, v3;
  630. k = ValueForKey (ent, key);
  631. // scanf into doubles, then assign, so it is vec_t size independent
  632. v1 = v2 = v3 = 0;
  633. sscanf (k, "%lf %lf %lf", &v1, &v2, &v3);
  634. vec[0] = v1;
  635. vec[1] = v2;
  636. vec[2] = v3;
  637. }