bspfile.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  1. #ifdef SYSTEM_WIN32
  2. #pragma warning(disable:4996)
  3. #endif
  4. #include "cmdlib.h"
  5. #include "filelib.h"
  6. #include "messages.h"
  7. #include "hlassert.h"
  8. #include "log.h"
  9. #include "mathlib.h"
  10. #include "bspfile.h"
  11. #include "scriplib.h"
  12. #include "blockmem.h"
  13. #include <windows.h> //SILENCER
  14. //=============================================================================
  15. int g_max_map_miptex = DEFAULT_MAX_MAP_MIPTEX;
  16. int g_max_map_lightdata = DEFAULT_MAX_MAP_LIGHTDATA;
  17. int g_nummodels;
  18. dmodel_t g_dmodels[MAX_MAP_MODELS];
  19. int g_dmodels_checksum;
  20. int g_visdatasize;
  21. byte g_dvisdata[MAX_MAP_VISIBILITY];
  22. int g_dvisdata_checksum;
  23. int g_lightdatasize;
  24. byte* g_dlightdata;
  25. int g_dlightdata_checksum;
  26. int g_texdatasize;
  27. byte* g_dtexdata; // (dmiptexlump_t)
  28. int g_dtexdata_checksum;
  29. int g_entdatasize;
  30. char g_dentdata[MAX_MAP_ENTSTRING];
  31. int g_dentdata_checksum;
  32. int g_numleafs;
  33. dleaf_t g_dleafs[MAX_MAP_LEAFS];
  34. int g_dleafs_checksum;
  35. int g_numplanes;
  36. dplane_t g_dplanes[MAX_INTERNAL_MAP_PLANES];
  37. int g_dplanes_checksum;
  38. int g_numvertexes;
  39. dvertex_t g_dvertexes[MAX_MAP_VERTS];
  40. int g_dvertexes_checksum;
  41. int g_numnodes;
  42. dnode_t g_dnodes[MAX_MAP_NODES];
  43. int g_dnodes_checksum;
  44. int g_numtexinfo;
  45. texinfo_t g_texinfo[MAX_MAP_TEXINFO];
  46. int g_texinfo_checksum;
  47. int g_numfaces;
  48. dface_t g_dfaces[MAX_MAP_FACES];
  49. int g_dfaces_checksum;
  50. int g_numclipnodes;
  51. dclipnode_t g_dclipnodes[MAX_MAP_CLIPNODES];
  52. int g_dclipnodes_checksum;
  53. int g_numedges;
  54. dedge_t g_dedges[MAX_MAP_EDGES];
  55. int g_dedges_checksum;
  56. int g_nummarksurfaces;
  57. unsigned short g_dmarksurfaces[MAX_MAP_MARKSURFACES];
  58. int g_dmarksurfaces_checksum;
  59. int g_numsurfedges;
  60. int g_dsurfedges[MAX_MAP_SURFEDGES];
  61. int g_dsurfedges_checksum;
  62. int g_numentities;
  63. entity_t g_entities[MAX_MAP_ENTITIES];
  64. /*
  65. * ===============
  66. * FastChecksum
  67. * ===============
  68. */
  69. static int FastChecksum(const void* const buffer, int bytes)
  70. {
  71. int checksum = 0;
  72. char* buf = (char*)buffer;
  73. while (bytes--)
  74. {
  75. checksum = rotl(checksum, 4) ^ (*buf);
  76. buf++;
  77. }
  78. return checksum;
  79. }
  80. /*
  81. * ===============
  82. * CompressVis
  83. * ===============
  84. */
  85. int CompressVis(const byte* const src, const unsigned int src_length, byte* dest, unsigned int dest_length)
  86. {
  87. unsigned int j;
  88. byte* dest_p = dest;
  89. unsigned int current_length = 0;
  90. for (j = 0; j < src_length; j++)
  91. {
  92. current_length++;
  93. hlassume(current_length <= dest_length, assume_COMPRESSVIS_OVERFLOW);
  94. *dest_p = src[j];
  95. dest_p++;
  96. if (src[j])
  97. {
  98. continue;
  99. }
  100. unsigned char rep = 1;
  101. for (j++; j < src_length; j++)
  102. {
  103. if (src[j] || rep == 255)
  104. {
  105. break;
  106. }
  107. else
  108. {
  109. rep++;
  110. }
  111. }
  112. current_length++;
  113. hlassume(current_length <= dest_length, assume_COMPRESSVIS_OVERFLOW);
  114. *dest_p = rep;
  115. dest_p++;
  116. j--;
  117. }
  118. return dest_p - dest;
  119. }
  120. // =====================================================================================
  121. // DecompressVis
  122. //
  123. // =====================================================================================
  124. void DecompressVis(const byte* src, byte* const dest, const unsigned int dest_length)
  125. {
  126. unsigned int current_length = 0;
  127. int c;
  128. byte* out;
  129. int row;
  130. row = (g_numleafs + 7) >> 3;
  131. out = dest;
  132. do
  133. {
  134. if (*src)
  135. {
  136. current_length++;
  137. hlassume(current_length <= dest_length, assume_DECOMPRESSVIS_OVERFLOW);
  138. *out = *src;
  139. out++;
  140. src++;
  141. continue;
  142. }
  143. c = src[1];
  144. src += 2;
  145. while (c)
  146. {
  147. current_length++;
  148. hlassume(current_length <= dest_length, assume_DECOMPRESSVIS_OVERFLOW);
  149. *out = 0;
  150. out++;
  151. c--;
  152. if (out - dest >= row)
  153. {
  154. return;
  155. }
  156. }
  157. }
  158. while (out - dest < row);
  159. }
  160. //
  161. // =====================================================================================
  162. //
  163. // =====================================================================================
  164. // SwapBSPFile
  165. // byte swaps all data in a bsp file
  166. // =====================================================================================
  167. static void SwapBSPFile(const bool todisk)
  168. {
  169. int i, j, c;
  170. dmodel_t* d;
  171. dmiptexlump_t* mtl;
  172. // models
  173. for (i = 0; i < g_nummodels; i++)
  174. {
  175. d = &g_dmodels[i];
  176. for (j = 0; j < MAX_MAP_HULLS; j++)
  177. {
  178. d->headnode[j] = LittleLong(d->headnode[j]);
  179. }
  180. d->visleafs = LittleLong(d->visleafs);
  181. d->firstface = LittleLong(d->firstface);
  182. d->numfaces = LittleLong(d->numfaces);
  183. for (j = 0; j < 3; j++)
  184. {
  185. d->mins[j] = LittleFloat(d->mins[j]);
  186. d->maxs[j] = LittleFloat(d->maxs[j]);
  187. d->origin[j] = LittleFloat(d->origin[j]);
  188. }
  189. }
  190. //
  191. // vertexes
  192. //
  193. for (i = 0; i < g_numvertexes; i++)
  194. {
  195. for (j = 0; j < 3; j++)
  196. {
  197. g_dvertexes[i].point[j] = LittleFloat(g_dvertexes[i].point[j]);
  198. }
  199. }
  200. //
  201. // planes
  202. //
  203. for (i = 0; i < g_numplanes; i++)
  204. {
  205. for (j = 0; j < 3; j++)
  206. {
  207. g_dplanes[i].normal[j] = LittleFloat(g_dplanes[i].normal[j]);
  208. }
  209. g_dplanes[i].dist = LittleFloat(g_dplanes[i].dist);
  210. g_dplanes[i].type = (planetypes)LittleLong(g_dplanes[i].type);
  211. }
  212. //
  213. // texinfos
  214. //
  215. for (i = 0; i < g_numtexinfo; i++)
  216. {
  217. for (j = 0; j < 8; j++)
  218. {
  219. g_texinfo[i].vecs[0][j] = LittleFloat(g_texinfo[i].vecs[0][j]);
  220. }
  221. g_texinfo[i].miptex = LittleLong(g_texinfo[i].miptex);
  222. g_texinfo[i].flags = LittleLong(g_texinfo[i].flags);
  223. }
  224. //
  225. // faces
  226. //
  227. for (i = 0; i < g_numfaces; i++)
  228. {
  229. g_dfaces[i].texinfo = LittleShort(g_dfaces[i].texinfo);
  230. g_dfaces[i].planenum = LittleShort(g_dfaces[i].planenum);
  231. g_dfaces[i].side = LittleShort(g_dfaces[i].side);
  232. g_dfaces[i].lightofs = LittleLong(g_dfaces[i].lightofs);
  233. g_dfaces[i].firstedge = LittleLong(g_dfaces[i].firstedge);
  234. g_dfaces[i].numedges = LittleShort(g_dfaces[i].numedges);
  235. }
  236. //
  237. // nodes
  238. //
  239. for (i = 0; i < g_numnodes; i++)
  240. {
  241. g_dnodes[i].planenum = LittleLong(g_dnodes[i].planenum);
  242. for (j = 0; j < 3; j++)
  243. {
  244. g_dnodes[i].mins[j] = LittleShort(g_dnodes[i].mins[j]);
  245. g_dnodes[i].maxs[j] = LittleShort(g_dnodes[i].maxs[j]);
  246. }
  247. g_dnodes[i].children[0] = LittleShort(g_dnodes[i].children[0]);
  248. g_dnodes[i].children[1] = LittleShort(g_dnodes[i].children[1]);
  249. g_dnodes[i].firstface = LittleShort(g_dnodes[i].firstface);
  250. g_dnodes[i].numfaces = LittleShort(g_dnodes[i].numfaces);
  251. }
  252. //
  253. // leafs
  254. //
  255. for (i = 0; i < g_numleafs; i++)
  256. {
  257. g_dleafs[i].contents = LittleLong(g_dleafs[i].contents);
  258. for (j = 0; j < 3; j++)
  259. {
  260. g_dleafs[i].mins[j] = LittleShort(g_dleafs[i].mins[j]);
  261. g_dleafs[i].maxs[j] = LittleShort(g_dleafs[i].maxs[j]);
  262. }
  263. g_dleafs[i].firstmarksurface = LittleShort(g_dleafs[i].firstmarksurface);
  264. g_dleafs[i].nummarksurfaces = LittleShort(g_dleafs[i].nummarksurfaces);
  265. g_dleafs[i].visofs = LittleLong(g_dleafs[i].visofs);
  266. }
  267. //
  268. // clipnodes
  269. //
  270. for (i = 0; i < g_numclipnodes; i++)
  271. {
  272. g_dclipnodes[i].planenum = LittleLong(g_dclipnodes[i].planenum);
  273. g_dclipnodes[i].children[0] = LittleShort(g_dclipnodes[i].children[0]);
  274. g_dclipnodes[i].children[1] = LittleShort(g_dclipnodes[i].children[1]);
  275. }
  276. //
  277. // miptex
  278. //
  279. if (g_texdatasize)
  280. {
  281. mtl = (dmiptexlump_t*)g_dtexdata;
  282. if (todisk)
  283. {
  284. c = mtl->nummiptex;
  285. }
  286. else
  287. {
  288. c = LittleLong(mtl->nummiptex);
  289. }
  290. mtl->nummiptex = LittleLong(mtl->nummiptex);
  291. for (i = 0; i < c; i++)
  292. {
  293. mtl->dataofs[i] = LittleLong(mtl->dataofs[i]);
  294. }
  295. }
  296. //
  297. // marksurfaces
  298. //
  299. for (i = 0; i < g_nummarksurfaces; i++)
  300. {
  301. g_dmarksurfaces[i] = LittleShort(g_dmarksurfaces[i]);
  302. }
  303. //
  304. // surfedges
  305. //
  306. for (i = 0; i < g_numsurfedges; i++)
  307. {
  308. g_dsurfedges[i] = LittleLong(g_dsurfedges[i]);
  309. }
  310. //
  311. // edges
  312. //
  313. for (i = 0; i < g_numedges; i++)
  314. {
  315. g_dedges[i].v[0] = LittleShort(g_dedges[i].v[0]);
  316. g_dedges[i].v[1] = LittleShort(g_dedges[i].v[1]);
  317. }
  318. }
  319. // =====================================================================================
  320. // CopyLump
  321. // balh
  322. // =====================================================================================
  323. static int CopyLump(int lump, void* dest, int size, const dheader_t* const header)
  324. {
  325. int length, ofs;
  326. length = header->lumps[lump].filelen;
  327. ofs = header->lumps[lump].fileofs;
  328. if (length % size)
  329. {
  330. Error("LoadBSPFile: odd lump size");
  331. }
  332. //special handling for tex and lightdata to keep things from exploding - KGP
  333. if(lump == LUMP_TEXTURES && dest == (void*)g_dtexdata)
  334. { hlassume(g_max_map_miptex > length, assume_MAX_MAP_MIPTEX); }
  335. else if(lump == LUMP_LIGHTING && dest == (void*)g_dlightdata)
  336. { hlassume(g_max_map_lightdata > length, assume_MAX_MAP_LIGHTING); }
  337. memcpy(dest, (byte*) header + ofs, length);
  338. return length / size;
  339. }
  340. // =====================================================================================
  341. // LoadBSPFile
  342. // balh
  343. // =====================================================================================
  344. void LoadBSPFile(const char* const filename)
  345. {
  346. dheader_t* header;
  347. LoadFile(filename, (char**)&header);
  348. LoadBSPImage(header);
  349. }
  350. // =====================================================================================
  351. // LoadBSPImage
  352. // balh
  353. // =====================================================================================
  354. void LoadBSPImage(dheader_t* const header)
  355. {
  356. unsigned int i;
  357. // swap the header
  358. for (i = 0; i < sizeof(dheader_t) / 4; i++)
  359. {
  360. ((int*)header)[i] = LittleLong(((int*)header)[i]);
  361. }
  362. if (header->version != BSPVERSION)
  363. {
  364. Error("BSP is version %i, not %i", header->version, BSPVERSION);
  365. }
  366. g_nummodels = CopyLump(LUMP_MODELS, g_dmodels, sizeof(dmodel_t), header);
  367. g_numvertexes = CopyLump(LUMP_VERTEXES, g_dvertexes, sizeof(dvertex_t), header);
  368. g_numplanes = CopyLump(LUMP_PLANES, g_dplanes, sizeof(dplane_t), header);
  369. g_numleafs = CopyLump(LUMP_LEAFS, g_dleafs, sizeof(dleaf_t), header);
  370. g_numnodes = CopyLump(LUMP_NODES, g_dnodes, sizeof(dnode_t), header);
  371. g_numtexinfo = CopyLump(LUMP_TEXINFO, g_texinfo, sizeof(texinfo_t), header);
  372. g_numclipnodes = CopyLump(LUMP_CLIPNODES, g_dclipnodes, sizeof(dclipnode_t), header);
  373. g_numfaces = CopyLump(LUMP_FACES, g_dfaces, sizeof(dface_t), header);
  374. g_nummarksurfaces = CopyLump(LUMP_MARKSURFACES, g_dmarksurfaces, sizeof(g_dmarksurfaces[0]), header);
  375. g_numsurfedges = CopyLump(LUMP_SURFEDGES, g_dsurfedges, sizeof(g_dsurfedges[0]), header);
  376. g_numedges = CopyLump(LUMP_EDGES, g_dedges, sizeof(dedge_t), header);
  377. g_texdatasize = CopyLump(LUMP_TEXTURES, g_dtexdata, 1, header);
  378. g_visdatasize = CopyLump(LUMP_VISIBILITY, g_dvisdata, 1, header);
  379. g_lightdatasize = CopyLump(LUMP_LIGHTING, g_dlightdata, 1, header);
  380. g_entdatasize = CopyLump(LUMP_ENTITIES, g_dentdata, 1, header);
  381. Free(header); // everything has been copied out
  382. //
  383. // swap everything
  384. //
  385. SwapBSPFile(false);
  386. g_dmodels_checksum = FastChecksum(g_dmodels, g_nummodels * sizeof(g_dmodels[0]));
  387. g_dvertexes_checksum = FastChecksum(g_dvertexes, g_numvertexes * sizeof(g_dvertexes[0]));
  388. g_dplanes_checksum = FastChecksum(g_dplanes, g_numplanes * sizeof(g_dplanes[0]));
  389. g_dleafs_checksum = FastChecksum(g_dleafs, g_numleafs * sizeof(g_dleafs[0]));
  390. g_dnodes_checksum = FastChecksum(g_dnodes, g_numnodes * sizeof(g_dnodes[0]));
  391. g_texinfo_checksum = FastChecksum(g_texinfo, g_numtexinfo * sizeof(g_texinfo[0]));
  392. g_dclipnodes_checksum = FastChecksum(g_dclipnodes, g_numclipnodes * sizeof(g_dclipnodes[0]));
  393. g_dfaces_checksum = FastChecksum(g_dfaces, g_numfaces * sizeof(g_dfaces[0]));
  394. g_dmarksurfaces_checksum = FastChecksum(g_dmarksurfaces, g_nummarksurfaces * sizeof(g_dmarksurfaces[0]));
  395. g_dsurfedges_checksum = FastChecksum(g_dsurfedges, g_numsurfedges * sizeof(g_dsurfedges[0]));
  396. g_dedges_checksum = FastChecksum(g_dedges, g_numedges * sizeof(g_dedges[0]));
  397. g_dtexdata_checksum = FastChecksum(g_dtexdata, g_numedges * sizeof(g_dtexdata[0]));
  398. g_dvisdata_checksum = FastChecksum(g_dvisdata, g_visdatasize * sizeof(g_dvisdata[0]));
  399. g_dlightdata_checksum = FastChecksum(g_dlightdata, g_lightdatasize * sizeof(g_dlightdata[0]));
  400. g_dentdata_checksum = FastChecksum(g_dentdata, g_entdatasize * sizeof(g_dentdata[0]));
  401. }
  402. //
  403. // =====================================================================================
  404. //
  405. // =====================================================================================
  406. // AddLump
  407. // balh
  408. // =====================================================================================
  409. static void AddLump(int lumpnum, void* data, int len, dheader_t* header, FILE* bspfile)
  410. {
  411. lump_t* lump =&header->lumps[lumpnum];
  412. lump->fileofs = LittleLong(ftell(bspfile));
  413. lump->filelen = LittleLong(len);
  414. SafeWrite(bspfile, data, (len + 3) & ~3);
  415. }
  416. // =====================================================================================
  417. // WriteBSPFile
  418. // Swaps the bsp file in place, so it should not be referenced again
  419. // =====================================================================================
  420. void WriteBSPFile(const char* const filename)
  421. {
  422. dheader_t outheader;
  423. dheader_t* header;
  424. FILE* bspfile;
  425. header = &outheader;
  426. memset(header, 0, sizeof(dheader_t));
  427. SwapBSPFile(true);
  428. header->version = LittleLong(BSPVERSION);
  429. bspfile = SafeOpenWrite(filename);
  430. SafeWrite(bspfile, header, sizeof(dheader_t)); // overwritten later
  431. // LUMP TYPE DATA LENGTH HEADER BSPFILE
  432. AddLump(LUMP_PLANES, g_dplanes, g_numplanes * sizeof(dplane_t), header, bspfile);
  433. AddLump(LUMP_LEAFS, g_dleafs, g_numleafs * sizeof(dleaf_t), header, bspfile);
  434. AddLump(LUMP_VERTEXES, g_dvertexes, g_numvertexes * sizeof(dvertex_t), header, bspfile);
  435. AddLump(LUMP_NODES, g_dnodes, g_numnodes * sizeof(dnode_t), header, bspfile);
  436. AddLump(LUMP_TEXINFO, g_texinfo, g_numtexinfo * sizeof(texinfo_t), header, bspfile);
  437. AddLump(LUMP_FACES, g_dfaces, g_numfaces * sizeof(dface_t), header, bspfile);
  438. AddLump(LUMP_CLIPNODES, g_dclipnodes, g_numclipnodes * sizeof(dclipnode_t), header, bspfile);
  439. AddLump(LUMP_MARKSURFACES, g_dmarksurfaces, g_nummarksurfaces * sizeof(g_dmarksurfaces[0]), header, bspfile);
  440. AddLump(LUMP_SURFEDGES, g_dsurfedges, g_numsurfedges * sizeof(g_dsurfedges[0]), header, bspfile);
  441. AddLump(LUMP_EDGES, g_dedges, g_numedges * sizeof(dedge_t), header, bspfile);
  442. AddLump(LUMP_MODELS, g_dmodels, g_nummodels * sizeof(dmodel_t), header, bspfile);
  443. AddLump(LUMP_LIGHTING, g_dlightdata, g_lightdatasize, header, bspfile);
  444. AddLump(LUMP_VISIBILITY,g_dvisdata, g_visdatasize, header, bspfile);
  445. AddLump(LUMP_ENTITIES, g_dentdata, g_entdatasize, header, bspfile);
  446. AddLump(LUMP_TEXTURES, g_dtexdata, g_texdatasize, header, bspfile);
  447. fseek(bspfile, 0, SEEK_SET);
  448. SafeWrite(bspfile, header, sizeof(dheader_t));
  449. fclose(bspfile);
  450. }
  451. //
  452. // =====================================================================================
  453. //
  454. #define ENTRIES(a) (sizeof(a)/sizeof(*(a)))
  455. #define ENTRYSIZE(a) (sizeof(*(a)))
  456. // =====================================================================================
  457. // ArrayUsage
  458. // blah
  459. // =====================================================================================
  460. static int ArrayUsage(const char* const szItem, const int items, const int maxitems, const int itemsize)
  461. {
  462. float percentage = maxitems ? items * 100.0 / maxitems : 0.0;
  463. //SILENCER: Increased max spacing value from 7 to 9, since values have grown larger.
  464. Log("%-12s %9i/%-9i %9i/%-9i (%4.1f%%)\n", szItem, items, maxitems, items * itemsize, maxitems * itemsize, percentage);
  465. return items * itemsize;
  466. }
  467. // =====================================================================================
  468. // GlobUsage
  469. // print out global ussage line in chart
  470. // =====================================================================================
  471. static int GlobUsage(const char* const szItem, const int itemstorage, const int maxstorage)
  472. {
  473. float percentage = maxstorage ? itemstorage * 100.0 / maxstorage : 0.0;
  474. Log("%-12s [variable] %9i/%-9i (%4.1f%%)\n", szItem, itemstorage, maxstorage, percentage);
  475. return itemstorage;
  476. }
  477. // =====================================================================================
  478. // PrintBSPFileSizes
  479. // Dumps info about current file
  480. // =====================================================================================
  481. void PrintBSPFileSizes()
  482. {
  483. int numtextures = g_texdatasize ? ((dmiptexlump_t*)g_dtexdata)->nummiptex : 0;
  484. int totalmemory = 0;
  485. Log("\n");
  486. Log("Object names Objects/Maxobjs Memory / Maxmem Fullness\n");
  487. Log("------------ ------------------- ------------------- --------\n");
  488. totalmemory += ArrayUsage("models", g_nummodels, ENTRIES(g_dmodels), ENTRYSIZE(g_dmodels));
  489. totalmemory += ArrayUsage("planes", g_numplanes, MAX_MAP_PLANES, ENTRYSIZE(g_dplanes));
  490. totalmemory += ArrayUsage("vertexes", g_numvertexes, ENTRIES(g_dvertexes), ENTRYSIZE(g_dvertexes));
  491. totalmemory += ArrayUsage("nodes", g_numnodes, ENTRIES(g_dnodes), ENTRYSIZE(g_dnodes));
  492. totalmemory += ArrayUsage("texinfos", g_numtexinfo, ENTRIES(g_texinfo), ENTRYSIZE(g_texinfo));
  493. totalmemory += ArrayUsage("faces", g_numfaces, ENTRIES(g_dfaces), ENTRYSIZE(g_dfaces));
  494. totalmemory += ArrayUsage("clipnodes", g_numclipnodes, 32767, ENTRYSIZE(g_dclipnodes));
  495. totalmemory += ArrayUsage("leaves", g_numleafs, ENTRIES(g_dleafs), ENTRYSIZE(g_dleafs));
  496. totalmemory += ArrayUsage("marksurfaces", g_nummarksurfaces, ENTRIES(g_dmarksurfaces), ENTRYSIZE(g_dmarksurfaces));
  497. totalmemory += ArrayUsage("surfedges", g_numsurfedges, ENTRIES(g_dsurfedges), ENTRYSIZE(g_dsurfedges));
  498. totalmemory += ArrayUsage("edges", g_numedges, ENTRIES(g_dedges), ENTRYSIZE(g_dedges));
  499. totalmemory += GlobUsage("texdata", g_texdatasize, g_max_map_miptex);
  500. totalmemory += GlobUsage("lightdata", g_lightdatasize, g_max_map_lightdata);
  501. totalmemory += GlobUsage("visdata", g_visdatasize, sizeof(g_dvisdata));
  502. totalmemory += GlobUsage("entdata", g_entdatasize, sizeof(g_dentdata));
  503. Log("=> Total BSP file data space used: %d bytes.\n", totalmemory);
  504. //SILENCER -->
  505. if(!strcmp(g_Program, "hlbsp")) { //Only warn in HLBSP
  506. if(g_numleafs > MAX_MAP_LEAFS) {
  507. Log("\n");
  508. Warning("Exceeded MAX_MAP_LEAFS. File write failure immediate.\n");
  509. } else if(g_numleafs > 8192) {
  510. Log("\n");
  511. Warning("Surpassed 8192 leafs, which is the old maximum.\n"
  512. " If you encounter problems when running your map,\n"
  513. " consider this the most likely cause. There exist no\n"
  514. " records about this ever causing problems, though.\n");
  515. }
  516. }
  517. //<-- SILENCER
  518. Log("Textures referenced: %i\n\n", numtextures);
  519. }
  520. // =====================================================================================
  521. // ParseEpair
  522. // entity key/value pairs
  523. // =====================================================================================
  524. epair_t* ParseEpair()
  525. {
  526. epair_t* e;
  527. e = (epair_t*)Alloc(sizeof(epair_t));
  528. if (strlen(g_token) >= MAX_KEY - 1)
  529. Error("ParseEpair: Key token too long (%i > MAX_KEY)", (int)strlen(g_token));
  530. e->key = _strdup(g_token);
  531. GetToken(false);
  532. if (strlen(g_token) >= ZHLT3_MAX_VALUE - 1)
  533. Error("ParseEpar: Value token too long (%i > ZHLT3_MAX_VALUE)", (int)strlen(g_token));
  534. e->value = _strdup(g_token);
  535. return e;
  536. }
  537. /*
  538. * ================
  539. * ParseEntity
  540. * ================
  541. */
  542. #ifdef ZHLT_INFO_COMPILE_PARAMETERS
  543. // AJM: each tool should have its own version of GetParamsFromEnt which parseentity calls
  544. extern void GetParamsFromEnt(entity_t* mapent);
  545. #endif
  546. //PROTECTOR -->
  547. int brush_copy_num[MAX_MAP_ENTITIES];
  548. int num_brush_copies = 0;
  549. void ParseBrushCopies()
  550. {
  551. if (num_brush_copies>0)
  552. {
  553. if(num_brush_copies>1)
  554. Log("\nFound %i phlt_copy_brush entities. Parsing...\n", num_brush_copies);
  555. else
  556. Log("\nFound 1 phlt_copy_brush entity. Parsing...\n");
  557. for (int i = 0; i < num_brush_copies; i++)
  558. {
  559. Verbose(" Parsing phlt_copy_brush %i:\n",i);
  560. int v = brush_copy_num[i];
  561. entity_t *copy_brush = &g_entities[v];
  562. char targetname[MAX_KEY];
  563. strcpy_s(targetname, ValueForKey(copy_brush, "phlt_cpm_target"));
  564. if (!strcmp(targetname,""))
  565. {
  566. Log(" ");
  567. Warning("No copy target for phlt_copy_brush %i (%s).\n"
  568. " Using default. (models/chumtoad.mdl)",i,targetname);
  569. SetKeyValue(copy_brush, "model", "models/chumtoad.mdl");
  570. }
  571. else
  572. {
  573. entity_t *target_ent = FindTargetEntity(targetname);
  574. if (!target_ent)
  575. {
  576. Log(" ");
  577. Warning("Could not find target %s for phlt_copy_brush %i.\n"
  578. " Using default. (models/chumtoad.mdl)",targetname,i);
  579. SetKeyValue(copy_brush, "model", "models/chumtoad.mdl");
  580. }
  581. else
  582. {
  583. char model_num[MAX_KEY];
  584. strcpy_s(model_num, ValueForKey(target_ent, "model"));
  585. if (!strcmp(model_num,""))
  586. {
  587. Log(" ");
  588. Warning("No model found at entity %s for phlt_copy_brush %i.\n"
  589. " Using default. (models/chumtoad.mdl)",targetname,i);
  590. SetKeyValue(copy_brush, "model", "models/chumtoad.mdl");
  591. }
  592. else
  593. {
  594. SetKeyValue(copy_brush, "model", model_num);
  595. }
  596. }
  597. }
  598. char classname[MAX_KEY];
  599. strcpy_s(classname, ValueForKey(copy_brush, "phlt_cpm_class"));
  600. Verbose(" Parsed. (Copy target: %s)\n",targetname);
  601. if (!strcmp(classname,""))
  602. {
  603. Log(" ");
  604. Warning("Created new func_illusionary as default, since no\n"
  605. " classname was given. However, classname should be given!");
  606. SetKeyValue(copy_brush, "classname", "func_illusionary");
  607. }
  608. else
  609. {
  610. Verbose(" Created new %s.\n",classname);
  611. SetKeyValue(copy_brush, "classname", classname);
  612. }
  613. }
  614. Log("Done.\n");
  615. }
  616. }
  617. //<-- PROTECTOR
  618. //SILENCER -->
  619. /*
  620. // UNFINISHED!
  621. int countdown_num[MAX_MAP_ENTITIES];
  622. int num_countdowns = 0;
  623. void UnfoldCountdownPackages() {
  624. if (num_countdowns > 0) {
  625. const int BASES = 16;
  626. const int TNAMES = 32;
  627. const int TARGETS = 16;
  628. if(num_countdowns > 1) {
  629. Log("\nFound %i shlt_countdown entities. Parsing...\n", num_countdowns);
  630. } else {
  631. Log("\nFound 1 shlt_countdown entity. Parsing...\n");
  632. }
  633. for (int a = 0; a < num_countdowns; a++) {
  634. Verbose(" Parsing shlt_countdown %i.\n", a);
  635. int v = countdown_num[a];
  636. entity_t* countdown = &g_entities[v];
  637. unsigned long units[TNAMES];
  638. char start_name[TNAMES][ZHLT3_MAX_VALUE];
  639. char base_change_target[TARGETS][ZHLT3_MAX_VALUE];
  640. char curkey[MAX_KEY];
  641. unsigned long total_units = 1;
  642. bool redundant[TNAMES];
  643. bool redundant_target[TARGETS];
  644. bool min_need = false;
  645. // Read count_units# and start_name# from 1 to 32
  646. for(int b = 0; b < max(TNAMES, TARGETS); b++) {
  647. if(b < TNAMES) {
  648. sprintf_s(curkey, "count_units%i", b + 1);
  649. units[b] = IntForKey(countdown, curkey);
  650. sprintf_s(curkey, "start_name%i", b + 1);
  651. strcpy_s(start_name[b], ValueForKey(countdown, curkey));
  652. if(units[b] < 1 || strlen(start_name[b]) == 0) {
  653. redundant[b] = true;
  654. } else {
  655. redundant[b] = false;
  656. min_need = true;
  657. if(units[b] > total_units)
  658. total_units = units[b];
  659. }
  660. }
  661. if(b < TARGETS) {
  662. sprintf_s(curkey, "base_change_target%i", b + 1);
  663. strcpy_s(base_change_target[b], ValueForKey(countdown, curkey));
  664. if(strlen(base_change_target[b]) == 0) {
  665. redundant_target[b] = true;
  666. } else redundant_target[b] = false;
  667. }
  668. }
  669. if(!min_need) {
  670. Warning("Zero targetnames specified in shlt_countdown. Skipping.");
  671. continue;
  672. }
  673. char separator_char = ValueForKey(countdown, "separator_char")[0];
  674. char dynamic_indicator[ZHLT3_MAX_VALUE];
  675. strcpy_s(dynamic_indicator, ValueForKey(countdown, "dynamic_indicator"));
  676. // Get positions of separator chars in dynamic_indicator
  677. int separators = 0;
  678. unsigned int separator_pos[TNAMES-1];
  679. unsigned int len = (int)strlen(dynamic_indicator);
  680. for(unsigned int c = 0; c < len; c++) {
  681. if(dynamic_indicator[c] == separator_char) {
  682. if(separators == TNAMES - 1)
  683. Error("More than %i unit bases/%i separator chars in shlt_countdown's time specs.", BASES, TNAMES - 1);
  684. separator_pos[separators] = c;
  685. separators++;
  686. }
  687. }
  688. if(separators % 2 == 0)
  689. Error("Even amount of separator chars (%i) in time specs do not match definition.", separators);
  690. unsigned long unit_max[BASES];
  691. unsigned long unit_digits[BASES];
  692. int unit_bases = 0;
  693. // Save the units' base sizes into unit_max[#]
  694. for(int d = 0; d < (separators+1) / 2; d++) {
  695. if(d) {
  696. if(separator_pos[2*d-1] + 1 == separator_pos[2*d])
  697. Error("Directly consecutive separator-char-pair in shlt_countdown's time specs.");
  698. unsigned int e;
  699. for(e = separator_pos[2*d-1]; e < separator_pos[2*d]; e++) {
  700. if(dynamic_indicator[e + 1] != separator_char)
  701. curkey[e - separator_pos[2*d-1]] = dynamic_indicator[e + 1];
  702. }
  703. curkey[e] = '\0';
  704. unit_max[d] = atoi(curkey);
  705. } else {
  706. // For the biggest unit base, this can be "infinite", thus
  707. // not using what we set this to later in the code. Just
  708. // make sure that this is > 0, because of the check below.
  709. unit_max[0] = 1;
  710. }
  711. if(unit_max[d] < 1)
  712. Error("Unit base #%i size of shlt_countdown < 1.", d + 1);
  713. unit_digits[d] = (int)strlen(curkey);
  714. unit_bases += 1;
  715. }
  716. if(unit_bases < 1)
  717. Error("No unit bases on shlt_countdown.");
  718. int unit_start_pos[BASES];
  719. int dynlen = (int)strlen(dynamic_indicator);
  720. // Remove the separator chars from dynamic_indicator
  721. // and maintain exact unit base positions.
  722. for(int f = 0; f < separators; f++) {
  723. int g = separator_pos[f] - f;
  724. for(int h = 0; h < dynlen - g; h++) {
  725. if(g + h + 1 == ZHLT3_MAX_VALUE)
  726. dynamic_indicator[g + h] = '\0';
  727. else dynamic_indicator[g + h] = dynamic_indicator[g + h + 1];
  728. }
  729. separator_pos[f] = g;
  730. dynlen--;
  731. }
  732. unit_start_pos[0] = separator_pos[0];
  733. for(int f = 1; f < separators; f+=2)
  734. unit_start_pos[f] = separator_pos[f*2-1];
  735. int interval_base = IntForKey(countdown, "interval_base");
  736. if(interval_base < 1) {
  737. Error("Interval base of shlt_countdown < 1.");
  738. } else if(interval_base > unit_bases) {
  739. Error("Interval base of shlt_countdown > bases.");
  740. }
  741. char finish_target[ZHLT3_MAX_VALUE];
  742. strcpy_s(finish_target, ValueForKey(countdown, "finish_target"));
  743. char finish_killtarget[ZHLT3_MAX_VALUE];
  744. strcpy_s(finish_killtarget, ValueForKey(countdown, "finish_killtarget"));
  745. float delay = FloatForKey(countdown, "delay");
  746. int activator_only = IntForKey(countdown, "activator_only");
  747. if(activator_only < 0 || activator_only > 2) {
  748. Warning("Activator_only setting in shlt_countdown entity out of bounds. Setting to default. (0)");
  749. activator_only = 0;
  750. }
  751. // Size in seconds of the biggest (left-hand) unit
  752. float biggest_unit_size = FloatForKey(countdown, "biggest_unit_size");
  753. bool count_upwards = BoolForKey(countdown, "count_upwards");
  754. char finish_message[ZHLT3_MAX_VALUE];
  755. strcpy_s(finish_message, ValueForKey(countdown, "finish_message"));
  756. char finish_message_time[128];
  757. strcpy_s(finish_message_time, ValueForKey(countdown, "finish_message_time"));
  758. if(atof(finish_message_time) < 0.0) {
  759. Warning("Finish message time < 0.0. Setting to 0.0.");
  760. strcpy_s(finish_message_time, "0.0");
  761. }
  762. char stop_message[ZHLT3_MAX_VALUE];
  763. strcpy_s(stop_message, ValueForKey(countdown, "stop_message"));
  764. char stop_message_time[128];
  765. strcpy_s(stop_message_time, ValueForKey(countdown, "stop_message_time"));
  766. if(atof(stop_message_time) < 0.0) {
  767. Warning("Stop message time < 0.0. Setting to 0.0.");
  768. strcpy_s(stop_message_time, "0.0");
  769. }
  770. // Read x1, x2, y1 and y2 and perform error checks
  771. char x1[5];
  772. strcpy_s(x1, ValueForKey(countdown, "x1"));
  773. if(atof(x1) < 0.0 && atof(x1) != -1.0) {
  774. Warning("X1 < 0.0 but not -1. Setting to -1.");
  775. strcpy_s(x1, "-1");
  776. } else if(atof(x1) > 1.0) {
  777. Warning("X1 > 1.0. Setting to 1.0.");
  778. strcpy_s(x1, "1.0");
  779. }
  780. char x2[5];
  781. strcpy_s(x2, ValueForKey(countdown, "x2"));
  782. if(atof(x2) < 0.0 && atof(x2) != -1.0) {
  783. Warning("X2 < 0.0 but not -1. Setting to -1.");
  784. strcpy_s(x2, "-1");
  785. } else if(atof(x2) > 1.0) {
  786. Warning("X2 > 1.0. Setting to 1.0.");
  787. strcpy_s(x2, "1.0");
  788. }
  789. if(atof(x1) == -1.0 && atof(x2) != -1.0) {
  790. Error("X1 is -1, while X2 is >= 0.0.");
  791. } else if(atof(x2) == -1.0 && atof(x1) != -1.0) {
  792. Error("X2 is -1, while X1 is >= 0.0.");
  793. }
  794. char y1[5];
  795. strcpy_s(y1, ValueForKey(countdown, "y1"));
  796. if(atof(y1) < 0.0 && atof(y1) != -1.0) {
  797. Warning("Y1 < 0.0 but not -1. Setting to -1.");
  798. strcpy_s(y1, "-1");
  799. } else if(atof(y1) > 1.0) {
  800. Warning("Y1 > 1.0. Setting to 1.0.");
  801. strcpy_s(y1, "1.0");
  802. }
  803. char y2[5];
  804. strcpy_s(y2, ValueForKey(countdown, "y2"));
  805. if(atof(y2) < 0.0 && atof(y2) != -1.0) {
  806. Warning("Y2 < 0.0 but not -1. Setting to -1.");
  807. strcpy_s(y2, "-1");
  808. } else if(atof(y2) > 1.0) {
  809. Warning("Y2 > 1.0. Setting to 1.0.");
  810. strcpy_s(y2, "1.0");
  811. }
  812. if(atof(y1) == -1.0 && atof(y2) != -1.0) {
  813. Error("Y1 is -1, while Y2 is >= 0.0.");
  814. } else if(atof(y2) == -1.0 && atof(y1) != -1.0) {
  815. Error("Y2 is -1, while Y1 is >= 0.0.");
  816. }
  817. // It will prove to be comfortable to have
  818. // these as both, string and float.
  819. float x1f = atof(x1);
  820. float x2f = atof(x2);
  821. float y1f = atof(y1);
  822. float y2f = atof(y2);
  823. float move_time = FloatForKey(countdown, "move_time");
  824. float move_delay = FloatForKey(countdown, "move_delay");
  825. float move_rdelay = FloatForKey(countdown, "move_rdelay");
  826. int countdown_color_from[3];
  827. GetIntVectorForKey(countdown, "countdown_color_from", countdown_color_from);
  828. int countdown_color_to[3];
  829. GetIntVectorForKey(countdown, "countdown_color_to", countdown_color_to);
  830. int finish_color[3];
  831. GetIntVectorForKey(countdown, "finish_color", finish_color);
  832. int stop_color[3];
  833. GetIntVectorForKey(countdown, "stop_color", stop_color);
  834. int channel = IntForKey(countdown, "channel");
  835. if(channel < 1) {
  836. Warning("Channel number < 1. Setting to 1.");
  837. channel = 1;
  838. } else if(channel > 4) {
  839. Warning("Channel number > 4. Setting to 4.");
  840. channel = 4;
  841. }
  842. unsigned long flash_amount = IntForKey(countdown, "flash_amount");
  843. unsigned long flash_amount2 = IntForKey(countdown, "flash_amount2");
  844. unsigned long flash_interval = IntForKey(countdown, "flash_interval");
  845. unsigned long flash_interval_base = IntForKey(countdown, "flash_interval_base");
  846. int flash_color[3];
  847. GetIntVectorForKey(countdown, "flash_color", flash_color);
  848. int flash_relativity = IntForKey(countdown, "flash_relativity");
  849. if(flash_relativity < 1) {
  850. Warning("Flash relativity option < 1. Setting to 1.");
  851. flash_relativity = 1;
  852. } else if(flash_relativity > 3) {
  853. Warning("Flash relativity option > 3. Setting to 1.");
  854. flash_relativity = 1;
  855. }
  856. bool stoptrigger_handling = BoolForKey(countdown, "stoptrigger_handling");
  857. float max_refresh_delay = FloatForKey(countdown, "max_refresh_delay");
  858. float min_refresh_delay = FloatForKey(countdown, "min_refresh_delay");
  859. float min_refresh_random = FloatForKey(countdown, "min_refresh_random");
  860. // We are going to pile all the new entities
  861. // in one spot, so read the origin as well.
  862. int origin[3];
  863. GetIntVectorForKey(countdown, "origin", origin);
  864. // We don't need you anymore!
  865. MarkEntityAsRedundant(countdown);
  866. // <- Done with the hacky reading and checking relevant keyvalues from entity
  867. // Do the actual work ->
  868. // Get the length in seconds one
  869. // unit of every base has.
  870. long double unit_time[BASES];
  871. for(int i = 0; i < unit_bases; i++) {
  872. unit_time[i] = biggest_unit_size;
  873. for(int j = 0; j < i; j++) {
  874. unit_time[i] /= unit_max[j];
  875. }
  876. }
  877. // Shift unit amounts over bases depending
  878. // on which is the interval base to match
  879. // scale of smallest (right-hand) base.
  880. interval_base = unit_bases - interval_base;
  881. flash_interval_base = unit_bases - flash_interval_base;
  882. for(int j = 0; j < unit_bases; j++) {
  883. if(!j) {
  884. for(int i = interval_base + 1; i < unit_bases; i++) {
  885. total_units *= unit_max[i];
  886. }
  887. }
  888. if(redundant[j])
  889. continue;
  890. for(int i = interval_base + 1; i < unit_bases; i++) {
  891. units[j] *= unit_max[i];
  892. }
  893. }
  894. // Calculate start values for the bases
  895. unsigned long base_cur_value[BASES];
  896. if(!count_upwards) {
  897. if(unit_bases == 1) {
  898. base_cur_value[0] = total_units;
  899. } else {
  900. long double factor = 1.0;
  901. for(int j = 2; j < unit_bases; j++) {
  902. factor *= unit_max[j];
  903. }
  904. long double this_units = (total_units / factor);
  905. unsigned long this_unitsf = (unsigned long)(this_units);
  906. base_cur_value[1] = this_unitsf % unit_max[1];
  907. base_cur_value[0] = (unsigned long)(this_units / unit_max[1]);
  908. long double multiply = this_units - this_unitsf;
  909. int i = 1;
  910. while(i < unit_bases - 1) {
  911. i++;
  912. if(i < unit_bases - 1) {
  913. base_cur_value[i] = (unsigned long)(multiply * unit_max[i]);
  914. multiply = multiply * unit_max[i] - base_cur_value[i];
  915. } else {
  916. base_cur_value[i] = ceil(multiply * unit_max[i]);
  917. }
  918. }
  919. }
  920. }
  921. Log("Highest start time: ");
  922. for(int i = 0; i < unit_bases; i++) {
  923. if(!i) {
  924. Log("%i", base_cur_value[i]);
  925. } else {
  926. char cp[ZHLT3_MAX_VALUE];
  927. itoa(unit_max[i] - 1, cp, 10);
  928. int len = (int)(strlen(cp));
  929. Log("%0*i", len, base_cur_value[i]);
  930. }
  931. if(i != unit_bases - 1) {
  932. Log(":");
  933. }
  934. }
  935. Log("\n");
  936. int mm_load = 0;
  937. unsigned long cdir = 0;
  938. if(!count_upwards)
  939. cdir = total_units;
  940. unsigned long total_units_cp = total_units;
  941. long double timeline = 0.0;
  942. bool processed = false;
  943. unsigned long offset = 0;
  944. if(flash_relativity == 3) {
  945. offset = total_units_cp % flash_interval;
  946. }
  947. // Calculate which game_texts will require a version
  948. // for the message flash effect (different color)
  949. bool* req_flashversion = (bool*) malloc(sizeof(bool) * total_units_cp);
  950. for(unsigned long i = total_units_cp; i > 0; i--) {
  951. if(((i + offset) % (flash_interval * unit_max[unit_bases - flash_interval_base])) == 0) {
  952. for(unsigned long j = 0; j < flash_amount2; j++) {
  953. if(flash_amount > 1) { // This is wrong and needs rewriting:
  954. for(unsigned long k = 0; k < flash_amount; k++) {
  955. if((req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base])) >= 0) {
  956. *(req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base])) = true;
  957. }
  958. }
  959. } else {
  960. for(unsigned long k = 0; k < flash_amount; k++) {
  961. if((req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base]) + (k * unit_max[unit_bases - flash_interval_base + 1])) >= 0) {
  962. *(req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base]) + (k * unit_max[unit_bases - flash_interval_base + 1])) = true;
  963. }
  964. }
  965. }
  966. }
  967. } else {
  968. *(req_flashversion + total_units_cp - i) = false;
  969. }
  970. }
  971. if(flash_relativity == 1) {
  972. *(req_flashversion + 0) = true;
  973. }
  974. // Determine and emit the game_texts which incarnate the
  975. // actual countdown. For this I use a counter for the
  976. // time (long double timeline) and a counter for the
  977. // smallest unit base (unsigned long total_units_cp)
  978. // simultaneously/parallel.
  979. while(!processed) {
  980. // Format cur_msg to be the current message to
  981. // be output by the current game_text entity.
  982. char cur_msg[ZHLT3_MAX_VALUE];
  983. strcpy_s(cur_msg, dynamic_indicator);
  984. int shifter = 0;
  985. for(int j = 0; j < unit_bases; j++) {
  986. char cur_val[ZHLT3_MAX_VALUE];
  987. if(j) {
  988. sprintf_s(cur_val, "%0*i", unit_digits, base_cur_value[j]);
  989. } else {
  990. itoa(base_cur_value[0], cur_val, 10);
  991. unit_digits[0] = (int)strlen(cur_val);
  992. }
  993. itoa(base_cur_value[j], cur_val, 10);
  994. strins(cur_msg, ZHLT3_MAX_VALUE - 1, cur_val, separator_pos[j] + shifter, false, ' ');
  995. shifter += unit_digits[j];
  996. }
  997. // Calculate screen position of message
  998. float newxf = x1f;
  999. float newyf = y1f;
  1000. if(timeline >= move_delay) {
  1001. if(timeline < (move_delay + move_time)) {
  1002. if(x1f >= 0.0) {
  1003. newxf = x1f + ((x2f - x1f) * ((timeline - move_delay) / move_time));
  1004. }
  1005. if(y1f >= 0.0) {
  1006. newyf = y1f + ((y2f - y1f) * ((timeline - move_delay) / move_time));
  1007. }
  1008. } else {
  1009. newxf = x2f;
  1010. newyf = y2f;
  1011. }
  1012. timeline += move_rdelay;
  1013. }
  1014. // Calculate color blending
  1015. int countdown_color_new[3];
  1016. for(int cchannel = 0; cchannel < 3; cchannel++) {
  1017. countdown_color_new[cchannel] = countdown_color_from[cchannel] +
  1018. ((countdown_color_from[cchannel] - countdown_color_to[cchannel]) * (1.0 / (total_units_cp / total_units)));
  1019. }
  1020. entity_t* game_text = AddEntity("game_text", origin);
  1021. // Count down by one unit - set boolean processed to
  1022. // true when trying to lower base_cur_value[0] to -1.
  1023. if(timeline / unit_time[unit_bases - 1] >= total_units_cp - total_units + 1) {
  1024. for(int j = unit_bases - 1; j >= 0; j--) {
  1025. if(base_cur_value[j] > 0) {
  1026. base_cur_value[j]--;
  1027. break;
  1028. } else {
  1029. if(!j) {
  1030. processed = true;
  1031. } else {
  1032. base_cur_value[j] = unit_max[j] - 1;
  1033. }
  1034. }
  1035. }
  1036. }
  1037. }
  1038. Free(req_flashversion);
  1039. }
  1040. Log("Done.\n");
  1041. }
  1042. }
  1043. */
  1044. //Inserts one string into another for a length of maxNewLen chars at position
  1045. //insAtPos. If overwrite is true, inserted String will overwrite source string
  1046. //from position insPos onwards.
  1047. void strins(char* srcDestStr, const int maxNewLen, const char* insStr, const int insAtPos, const bool overwrite, const char fillGap) {
  1048. int srcDestCurPos;
  1049. int insCurPos = 0;
  1050. int srcDestLen = srcDestCurPos = (int)strlen(srcDestStr);
  1051. int insLen = (int)strlen(insStr);
  1052. int fetch = 0;
  1053. char* srcDestStrBackup = (char*) malloc(sizeof(char) * (srcDestLen + 1));
  1054. while(srcDestCurPos < maxNewLen) {
  1055. if(srcDestLen < insAtPos) {
  1056. *(srcDestStr + srcDestCurPos) = fillGap;
  1057. srcDestCurPos++;
  1058. } else break;
  1059. }
  1060. while(srcDestCurPos < maxNewLen) {
  1061. if(insCurPos < insLen) {
  1062. *(srcDestStr + srcDestCurPos) = *(insStr + insCurPos);
  1063. srcDestCurPos++; insCurPos++;
  1064. fetch++;
  1065. }
  1066. }
  1067. if(overwrite) {
  1068. Free(srcDestStrBackup);
  1069. *(srcDestStr + srcDestCurPos) = '\0';
  1070. return;
  1071. }
  1072. while(srcDestCurPos < maxNewLen) {
  1073. if(srcDestCurPos < srcDestLen + fetch) {
  1074. *(srcDestStr + srcDestCurPos) = *(srcDestStrBackup + srcDestCurPos - fetch);
  1075. srcDestCurPos++;
  1076. } else break;
  1077. }
  1078. Free(srcDestStrBackup);
  1079. *(srcDestStr + srcDestCurPos) = '\0';
  1080. }
  1081. int wildcard_num[MAX_MAP_ENTITIES];
  1082. int num_wildcards = 0;
  1083. void DoWildcards() {
  1084. if (num_wildcards > 0) {
  1085. if(num_wildcards > 1) {
  1086. Log("\nFound %i shlt_entity_wildcard entities. Parsing...\n", num_wildcards);
  1087. Warning("One shlt_entity_wildcard entity should be sufficient, but found %i.", num_wildcards);
  1088. } else {
  1089. Log("\nFound 1 shlt_entity_wildcard entity. Parsing...\n");
  1090. }
  1091. for (int a = 0; a < num_wildcards; a++) {
  1092. Verbose(" Parsing shlt_entity_wildcard %i:\n", a);
  1093. int v = wildcard_num[a];
  1094. entity_t* wildcard = &g_entities[v];
  1095. int amount = IntForKey(wildcard, "wildcard_amount");
  1096. if(amount <= 0) {
  1097. Warning("Read too low value (%i) from Wildcard amount - Removing entity.", amount);
  1098. wildcard->epairs = 0;
  1099. } else {
  1100. float delay = FloatForKey(wildcard, "delay");
  1101. char killtarget[ZHLT3_MAX_VALUE];
  1102. strcpy_s(killtarget, ValueForKey(wildcard, "killtarget"));
  1103. if(delay < 0.5) {
  1104. Warning("Delay of %.6f seconds on shlt_entity_wildcard \"%s\" might be too low!", delay, killtarget);
  1105. }
  1106. Verbose("Emitting %i additional info_target entities", amount - 1);
  1107. SetKeyValue(wildcard, "classname", "trigger_auto");
  1108. SetKeyValue(wildcard, "spawnflags", "1");
  1109. RemoveKeyValue(wildcard, "wildcard_amount");
  1110. int origin[3];
  1111. GetIntVectorForKey(wildcard, "origin", origin);
  1112. for(int i = 1; i < amount; i++) {
  1113. entity_t* target = AddEntity("info_target", origin);
  1114. SetKeyValue(target, "targetname", killtarget);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. //<-- SILENCER
  1121. bool ParseEntity()
  1122. {
  1123. epair_t* e;
  1124. entity_t* mapent;
  1125. /* All entities parsed? */
  1126. if (!GetToken(true))
  1127. {
  1128. //UnfoldCountdownPackages();
  1129. DoWildcards();
  1130. ParseBrushCopies();
  1131. /* Changes to entity work? */
  1132. if(num_brush_copies > 0 /* || num_countdowns > 0 */ || num_wildcards > 0) {
  1133. UnparseEntities();
  1134. }
  1135. return false;
  1136. }
  1137. if (strcmp(g_token, "{"))
  1138. {
  1139. Error("ParseEntity: { not found");
  1140. }
  1141. if (g_numentities == MAX_MAP_ENTITIES)
  1142. {
  1143. Error("g_numentities == MAX_MAP_ENTITIES");
  1144. }
  1145. mapent = &g_entities[g_numentities];
  1146. g_numentities++;
  1147. while (1)
  1148. {
  1149. if (!GetToken(true))
  1150. {
  1151. Error("ParseEntity: EOF without closing brace");
  1152. }
  1153. if (!strcmp(g_token, "}"))
  1154. {
  1155. break;
  1156. }
  1157. e = ParseEpair();
  1158. e->next = mapent->epairs;
  1159. mapent->epairs = e;
  1160. }
  1161. #ifdef ZHLT_INFO_COMPILE_PARAMETERS // AJM
  1162. if (!strcmp(ValueForKey(mapent, "classname"), "info_compile_parameters"))
  1163. {
  1164. Log("Map entity info_compile_parameters detected, using compile settings\n");
  1165. GetParamsFromEnt(mapent);
  1166. }
  1167. #endif
  1168. // Since the classnames of the following entity types are changed
  1169. // when detected, the following if-checks can only return true in
  1170. // the ParseEntities()-call from hlbsp, since hlbsp is the first
  1171. // tool to call ParseEntities().
  1172. //PROTECTOR -->
  1173. if (!strcmp(ValueForKey(mapent, "classname"), "phlt_copy_brush"))
  1174. {
  1175. brush_copy_num[num_brush_copies] = g_numentities-1;
  1176. num_brush_copies++;
  1177. } //SILENCER -->
  1178. /*else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_countdown"))
  1179. {
  1180. countdown_num[num_countdowns] = g_numentities-1;
  1181. num_countdowns++;
  1182. }*/
  1183. else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_entity_wildcard"))
  1184. {
  1185. wildcard_num[num_wildcards] = g_numentities-1;
  1186. num_wildcards++;
  1187. } //<-- SILENCER
  1188. //<-- PROTECTOR
  1189. return true;
  1190. }
  1191. // =====================================================================================
  1192. // ParseEntities
  1193. // Parses the dentdata string into entities
  1194. // =====================================================================================
  1195. void ParseEntities()
  1196. {
  1197. g_numentities = 0;
  1198. ParseFromMemory(g_dentdata, g_entdatasize);
  1199. while (ParseEntity())
  1200. {
  1201. }
  1202. }
  1203. // =====================================================================================
  1204. // UnparseEntities
  1205. // Generates the dentdata string from all the entities
  1206. // =====================================================================================
  1207. void UnparseEntities()
  1208. {
  1209. char* buf;
  1210. char* end;
  1211. epair_t* ep;
  1212. char line[MAXTOKEN];
  1213. int i;
  1214. buf = g_dentdata;
  1215. end = buf;
  1216. *end = 0;
  1217. for (i = 0; i < g_numentities; i++)
  1218. {
  1219. ep = g_entities[i].epairs;
  1220. if (!ep)
  1221. {
  1222. continue; // ent got removed
  1223. }
  1224. strcat(end, "{\n");
  1225. end += 2;
  1226. for (ep = g_entities[i].epairs; ep; ep = ep->next)
  1227. {
  1228. sprintf(line, "\"%s\" \"%s\"\n", ep->key, ep->value);
  1229. strcat(end, line);
  1230. end += strlen(line);
  1231. }
  1232. strcat(end, "}\n");
  1233. end += 2;
  1234. if (end > buf + MAX_MAP_ENTSTRING)
  1235. {
  1236. Error("Entity text too long.");
  1237. }
  1238. }
  1239. g_entdatasize = end - buf + 1;
  1240. }
  1241. // SILENCER -->
  1242. // =====================================================================================
  1243. // MarkEntityAsRedundant
  1244. // strips epairs from entity to provide for its removal in UnparseEntities()
  1245. // =====================================================================================
  1246. void MarkEntityAsRedundant(entity_t* ent) {
  1247. ent->epairs = 0;
  1248. // Do not lower g_numentities, since all we are doing with
  1249. // this, is to take some entity anywhere out of the chain of
  1250. // entities.
  1251. }
  1252. // =====================================================================================
  1253. // AddEntity
  1254. // adds an Entity at given origin
  1255. // =====================================================================================
  1256. entity_t *AddEntity(const char* const classname, int neworigin[3]) {
  1257. if (g_numentities == MAX_MAP_ENTITIES) {
  1258. Error("g_numentities == MAX_MAP_ENTITIES");
  1259. }
  1260. char value[ZHLT3_MAX_VALUE];
  1261. /* Origin-vector should be in ints - all entities **
  1262. ** in *.map have int[3]-based origin */
  1263. sprintf_s(value, "%i %i %i", neworigin[0], neworigin[1], neworigin[2]);
  1264. SetKeyValue(&g_entities[g_numentities], "classname", classname);
  1265. SetKeyValue(&g_entities[g_numentities], "origin", value);
  1266. g_numentities++;
  1267. return &g_entities[g_numentities - 1];
  1268. }
  1269. // =====================================================================================
  1270. // RemoveKeyValue
  1271. // removes a keyvalue from an entity
  1272. // =====================================================================================
  1273. void RemoveKeyValue(entity_t* ent, const char* const key)
  1274. {
  1275. epair_t* ep;
  1276. epair_t* prevep;
  1277. for(ep = ent->epairs; ep; ep = ep->next)
  1278. {
  1279. if(!strcmp(ep->key, key))
  1280. {
  1281. if(!prevep)
  1282. ent->epairs = ep->next;
  1283. else
  1284. prevep->next = ep->next;
  1285. Free(ep);
  1286. return;
  1287. }
  1288. prevep = ep;
  1289. }
  1290. }
  1291. // <-- SILENCER
  1292. // =====================================================================================
  1293. // SetKeyValue
  1294. // creates/changes a keyvalue
  1295. // =====================================================================================
  1296. void SetKeyValue(entity_t* ent, const char* const key, const char* const value)
  1297. {
  1298. epair_t* ep;
  1299. for (ep = ent->epairs; ep; ep = ep->next)
  1300. {
  1301. if (!strcmp(ep->key, key))
  1302. {
  1303. Free(ep->value);
  1304. ep->value = strdup(value);
  1305. return;
  1306. }
  1307. }
  1308. // If the key does not exist, create it and apply value
  1309. ep = (epair_t*)Alloc(sizeof(*ep));
  1310. ep->next = ent->epairs;
  1311. ent->epairs = ep;
  1312. ep->key = strdup(key);
  1313. ep->value = strdup(value);
  1314. }
  1315. // =====================================================================================
  1316. // ValueForKey
  1317. // returns the value for a passed entity and key
  1318. // =====================================================================================
  1319. const char* ValueForKey(const entity_t* const ent, const char* const key)
  1320. {
  1321. epair_t* ep;
  1322. for (ep = ent->epairs; ep; ep = ep->next)
  1323. {
  1324. if (!strcmp(ep->key, key))
  1325. {
  1326. return ep->value;
  1327. }
  1328. }
  1329. return "";
  1330. }
  1331. // =====================================================================================
  1332. // IntForKey
  1333. // =====================================================================================
  1334. int IntForKey(const entity_t* const ent, const char* const key)
  1335. {
  1336. return atoi(ValueForKey(ent, key));
  1337. }
  1338. // =====================================================================================
  1339. // FloatForKey
  1340. // =====================================================================================
  1341. vec_t FloatForKey(const entity_t* const ent, const char* const key)
  1342. {
  1343. return atof(ValueForKey(ent, key));
  1344. }
  1345. // SILENCER -->
  1346. // =====================================================================================
  1347. // BoolForKey
  1348. // =====================================================================================
  1349. bool BoolForKey(const entity_t* const ent, const char* const key)
  1350. {
  1351. return atoi(ValueForKey(ent, key)) != 0;
  1352. }
  1353. // <-- SILENCER
  1354. // =====================================================================================
  1355. // GetVectorForKey
  1356. // returns value for key in vec[0-2]
  1357. // =====================================================================================
  1358. void GetVectorForKey(const entity_t* const ent, const char* const key, vec3_t vec)
  1359. {
  1360. const char* k;
  1361. double v1, v2, v3;
  1362. k = ValueForKey(ent, key);
  1363. // scanf into doubles, then assign, so it is vec_t size independent
  1364. v1 = v2 = v3 = 0;
  1365. sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
  1366. vec[0] = v1;
  1367. vec[1] = v2;
  1368. vec[2] = v3;
  1369. }
  1370. void GetIntVectorForKey(const entity_t* const ent, const char* const key, int vec[3])
  1371. {
  1372. const char* k;
  1373. double v1, v2, v3;
  1374. k = ValueForKey(ent, key);
  1375. // scanf into doubles, then assign, so it is vec_t size independent
  1376. v1 = v2 = v3 = 0;
  1377. sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
  1378. vec[0] = (int)v1;
  1379. vec[1] = (int)v2;
  1380. vec[2] = (int)v3;
  1381. }
  1382. // =====================================================================================
  1383. // FindTargetEntity
  1384. //
  1385. // =====================================================================================
  1386. entity_t *FindTargetEntity(const char* const target)
  1387. {
  1388. int i;
  1389. const char* n;
  1390. for (i = 0; i < g_numentities; i++)
  1391. {
  1392. n = ValueForKey(&g_entities[i], "targetname");
  1393. if (!strcmp(n, target))
  1394. {
  1395. return &g_entities[i];
  1396. }
  1397. }
  1398. return NULL;
  1399. }
  1400. void dtexdata_init()
  1401. {
  1402. g_dtexdata = (byte*)AllocBlock(g_max_map_miptex);
  1403. hlassume(g_dtexdata != NULL, assume_NoMemory);
  1404. g_dlightdata = (byte*)AllocBlock(g_max_map_lightdata);
  1405. hlassume(g_dlightdata != NULL, assume_NoMemory);
  1406. }
  1407. void CDECL dtexdata_free()
  1408. {
  1409. FreeBlock(g_dtexdata);
  1410. g_dtexdata = NULL;
  1411. FreeBlock(g_dlightdata);
  1412. g_dlightdata = NULL;
  1413. }
  1414. // =====================================================================================
  1415. // GetTextureByNumber
  1416. // Touchy function, can fail with a page fault if all the data isnt kosher
  1417. // (i.e. map was compiled with missing textures)
  1418. // =====================================================================================
  1419. char* GetTextureByNumber(int texturenumber)
  1420. {
  1421. texinfo_t* info;
  1422. miptex_t* miptex;
  1423. int ofs;
  1424. info = &g_texinfo[texturenumber];
  1425. ofs = ((dmiptexlump_t*)g_dtexdata)->dataofs[info->miptex];
  1426. miptex = (miptex_t*)(&g_dtexdata[ofs]);
  1427. return miptex->name;
  1428. }
  1429. // =====================================================================================
  1430. // EntityForModel
  1431. // returns entity addy for given modelnum
  1432. // =====================================================================================
  1433. entity_t* EntityForModel(const int modnum)
  1434. {
  1435. int i;
  1436. const char* s;
  1437. char name[16];
  1438. sprintf(name, "*%i", modnum);
  1439. // search the entities for one using modnum
  1440. for (i = 0; i < g_numentities; i++)
  1441. {
  1442. s = ValueForKey(&g_entities[i], "model");
  1443. if (!strcmp(s, name))
  1444. {
  1445. return &g_entities[i];
  1446. }
  1447. }
  1448. return &g_entities[0];
  1449. }