bspfile.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696
  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. RemoveKeyValue(copy_brush, "phlt_cpm_target");
  614. RemoveKeyValue(copy_brush, "phlt_cpm_class");
  615. }
  616. Log("Done.\n");
  617. }
  618. }
  619. //<-- PROTECTOR
  620. //SILENCER -->
  621. /*
  622. // UNFINISHED!
  623. int countdown_num[MAX_MAP_ENTITIES];
  624. int num_countdowns = 0;
  625. void UnfoldCountdownPackages() {
  626. if (num_countdowns > 0) {
  627. const int BASES = 16;
  628. const int TNAMES = 32;
  629. const int TARGETS = 16;
  630. if(num_countdowns > 1) {
  631. Log("\nFound %i shlt_countdown entities. Parsing...\n", num_countdowns);
  632. } else {
  633. Log("\nFound 1 shlt_countdown entity. Parsing...\n");
  634. }
  635. for (int a = 0; a < num_countdowns; a++) {
  636. Verbose(" Parsing shlt_countdown %i.\n", a);
  637. int v = countdown_num[a];
  638. entity_t* countdown = &g_entities[v];
  639. unsigned long units[TNAMES];
  640. char start_name[TNAMES][ZHLT3_MAX_VALUE];
  641. char base_change_target[TARGETS][ZHLT3_MAX_VALUE];
  642. char curkey[MAX_KEY];
  643. unsigned long total_units = 1;
  644. bool redundant[TNAMES];
  645. bool redundant_target[TARGETS];
  646. bool min_need = false;
  647. // Read count_units# and start_name# from 1 to 32
  648. for(int b = 0; b < max(TNAMES, TARGETS); b++) {
  649. if(b < TNAMES) {
  650. sprintf_s(curkey, "count_units%i", b + 1);
  651. units[b] = IntForKey(countdown, curkey);
  652. sprintf_s(curkey, "start_name%i", b + 1);
  653. strcpy_s(start_name[b], ValueForKey(countdown, curkey));
  654. if(units[b] < 1 || strlen(start_name[b]) == 0) {
  655. redundant[b] = true;
  656. } else {
  657. redundant[b] = false;
  658. min_need = true;
  659. if(units[b] > total_units)
  660. total_units = units[b];
  661. }
  662. }
  663. if(b < TARGETS) {
  664. sprintf_s(curkey, "base_change_target%i", b + 1);
  665. strcpy_s(base_change_target[b], ValueForKey(countdown, curkey));
  666. if(strlen(base_change_target[b]) == 0) {
  667. redundant_target[b] = true;
  668. } else redundant_target[b] = false;
  669. }
  670. }
  671. if(!min_need) {
  672. Warning("Zero targetnames specified in shlt_countdown. Skipping.");
  673. continue;
  674. }
  675. char separator_char = ValueForKey(countdown, "separator_char")[0];
  676. char dynamic_indicator[ZHLT3_MAX_VALUE];
  677. strcpy_s(dynamic_indicator, ValueForKey(countdown, "dynamic_indicator"));
  678. // Get positions of separator chars in dynamic_indicator
  679. int separators = 0;
  680. unsigned int separator_pos[TNAMES-1];
  681. unsigned int len = (int)strlen(dynamic_indicator);
  682. for(unsigned int c = 0; c < len; c++) {
  683. if(dynamic_indicator[c] == separator_char) {
  684. if(separators == TNAMES - 1)
  685. Error("More than %i unit bases/%i separator chars in shlt_countdown's time specs.", BASES, TNAMES - 1);
  686. separator_pos[separators] = c;
  687. separators++;
  688. }
  689. }
  690. if(separators % 2 == 0)
  691. Error("Even amount of separator chars (%i) in time specs do not match definition.", separators);
  692. unsigned long unit_max[BASES];
  693. unsigned long unit_digits[BASES];
  694. int unit_bases = 0;
  695. // Save the units' base sizes into unit_max[#]
  696. for(int d = 0; d < (separators+1) / 2; d++) {
  697. if(d) {
  698. if(separator_pos[2*d-1] + 1 == separator_pos[2*d])
  699. Error("Directly consecutive separator-char-pair in shlt_countdown's time specs.");
  700. unsigned int e;
  701. for(e = separator_pos[2*d-1]; e < separator_pos[2*d]; e++) {
  702. if(dynamic_indicator[e + 1] != separator_char)
  703. curkey[e - separator_pos[2*d-1]] = dynamic_indicator[e + 1];
  704. }
  705. curkey[e] = '\0';
  706. unit_max[d] = atoi(curkey);
  707. } else {
  708. // For the biggest unit base, this can be "infinite", thus
  709. // not using what we set this to later in the code. Just
  710. // make sure that this is > 0, because of the check below.
  711. unit_max[0] = 1;
  712. }
  713. if(unit_max[d] < 1)
  714. Error("Unit base #%i size of shlt_countdown < 1.", d + 1);
  715. unit_digits[d] = (int)strlen(curkey);
  716. unit_bases += 1;
  717. }
  718. if(unit_bases < 1)
  719. Error("No unit bases on shlt_countdown.");
  720. int unit_start_pos[BASES];
  721. int dynlen = (int)strlen(dynamic_indicator);
  722. // Remove the separator chars from dynamic_indicator
  723. // and maintain exact unit base positions.
  724. for(int f = 0; f < separators; f++) {
  725. int g = separator_pos[f] - f;
  726. for(int h = 0; h < dynlen - g; h++) {
  727. if(g + h + 1 == ZHLT3_MAX_VALUE)
  728. dynamic_indicator[g + h] = '\0';
  729. else dynamic_indicator[g + h] = dynamic_indicator[g + h + 1];
  730. }
  731. separator_pos[f] = g;
  732. dynlen--;
  733. }
  734. unit_start_pos[0] = separator_pos[0];
  735. for(int f = 1; f < separators; f+=2)
  736. unit_start_pos[f] = separator_pos[f*2-1];
  737. int interval_base = IntForKey(countdown, "interval_base");
  738. if(interval_base < 1) {
  739. Error("Interval base of shlt_countdown < 1.");
  740. } else if(interval_base > unit_bases) {
  741. Error("Interval base of shlt_countdown > bases.");
  742. }
  743. char finish_target[ZHLT3_MAX_VALUE];
  744. strcpy_s(finish_target, ValueForKey(countdown, "finish_target"));
  745. char finish_killtarget[ZHLT3_MAX_VALUE];
  746. strcpy_s(finish_killtarget, ValueForKey(countdown, "finish_killtarget"));
  747. float delay = FloatForKey(countdown, "delay");
  748. int activator_only = IntForKey(countdown, "activator_only");
  749. if(activator_only < 0 || activator_only > 2) {
  750. Warning("Activator_only setting in shlt_countdown entity out of bounds. Setting to default. (0)");
  751. activator_only = 0;
  752. }
  753. // Size in seconds of the biggest (left-hand) unit
  754. float biggest_unit_size = FloatForKey(countdown, "biggest_unit_size");
  755. bool count_upwards = BoolForKey(countdown, "count_upwards");
  756. char finish_message[ZHLT3_MAX_VALUE];
  757. strcpy_s(finish_message, ValueForKey(countdown, "finish_message"));
  758. char finish_message_time[128];
  759. strcpy_s(finish_message_time, ValueForKey(countdown, "finish_message_time"));
  760. if(atof(finish_message_time) < 0.0) {
  761. Warning("Finish message time < 0.0. Setting to 0.0.");
  762. strcpy_s(finish_message_time, "0.0");
  763. }
  764. char stop_message[ZHLT3_MAX_VALUE];
  765. strcpy_s(stop_message, ValueForKey(countdown, "stop_message"));
  766. char stop_message_time[128];
  767. strcpy_s(stop_message_time, ValueForKey(countdown, "stop_message_time"));
  768. if(atof(stop_message_time) < 0.0) {
  769. Warning("Stop message time < 0.0. Setting to 0.0.");
  770. strcpy_s(stop_message_time, "0.0");
  771. }
  772. // Read x1, x2, y1 and y2 and perform error checks
  773. char x1[5];
  774. strcpy_s(x1, ValueForKey(countdown, "x1"));
  775. if(atof(x1) < 0.0 && atof(x1) != -1.0) {
  776. Warning("X1 < 0.0 but not -1. Setting to -1.");
  777. strcpy_s(x1, "-1");
  778. } else if(atof(x1) > 1.0) {
  779. Warning("X1 > 1.0. Setting to 1.0.");
  780. strcpy_s(x1, "1.0");
  781. }
  782. char x2[5];
  783. strcpy_s(x2, ValueForKey(countdown, "x2"));
  784. if(atof(x2) < 0.0 && atof(x2) != -1.0) {
  785. Warning("X2 < 0.0 but not -1. Setting to -1.");
  786. strcpy_s(x2, "-1");
  787. } else if(atof(x2) > 1.0) {
  788. Warning("X2 > 1.0. Setting to 1.0.");
  789. strcpy_s(x2, "1.0");
  790. }
  791. if(atof(x1) == -1.0 && atof(x2) != -1.0) {
  792. Error("X1 is -1, while X2 is >= 0.0.");
  793. } else if(atof(x2) == -1.0 && atof(x1) != -1.0) {
  794. Error("X2 is -1, while X1 is >= 0.0.");
  795. }
  796. char y1[5];
  797. strcpy_s(y1, ValueForKey(countdown, "y1"));
  798. if(atof(y1) < 0.0 && atof(y1) != -1.0) {
  799. Warning("Y1 < 0.0 but not -1. Setting to -1.");
  800. strcpy_s(y1, "-1");
  801. } else if(atof(y1) > 1.0) {
  802. Warning("Y1 > 1.0. Setting to 1.0.");
  803. strcpy_s(y1, "1.0");
  804. }
  805. char y2[5];
  806. strcpy_s(y2, ValueForKey(countdown, "y2"));
  807. if(atof(y2) < 0.0 && atof(y2) != -1.0) {
  808. Warning("Y2 < 0.0 but not -1. Setting to -1.");
  809. strcpy_s(y2, "-1");
  810. } else if(atof(y2) > 1.0) {
  811. Warning("Y2 > 1.0. Setting to 1.0.");
  812. strcpy_s(y2, "1.0");
  813. }
  814. if(atof(y1) == -1.0 && atof(y2) != -1.0) {
  815. Error("Y1 is -1, while Y2 is >= 0.0.");
  816. } else if(atof(y2) == -1.0 && atof(y1) != -1.0) {
  817. Error("Y2 is -1, while Y1 is >= 0.0.");
  818. }
  819. // It will prove to be comfortable to have
  820. // these as both, string and float.
  821. float x1f = atof(x1);
  822. float x2f = atof(x2);
  823. float y1f = atof(y1);
  824. float y2f = atof(y2);
  825. float move_time = FloatForKey(countdown, "move_time");
  826. float move_delay = FloatForKey(countdown, "move_delay");
  827. float move_rdelay = FloatForKey(countdown, "move_rdelay");
  828. int countdown_color_from[3];
  829. GetIntVectorForKey(countdown, "countdown_color_from", countdown_color_from);
  830. int countdown_color_to[3];
  831. GetIntVectorForKey(countdown, "countdown_color_to", countdown_color_to);
  832. int finish_color[3];
  833. GetIntVectorForKey(countdown, "finish_color", finish_color);
  834. int stop_color[3];
  835. GetIntVectorForKey(countdown, "stop_color", stop_color);
  836. int channel = IntForKey(countdown, "channel");
  837. if(channel < 1) {
  838. Warning("Channel number < 1. Setting to 1.");
  839. channel = 1;
  840. } else if(channel > 4) {
  841. Warning("Channel number > 4. Setting to 4.");
  842. channel = 4;
  843. }
  844. unsigned long flash_amount = IntForKey(countdown, "flash_amount");
  845. unsigned long flash_amount2 = IntForKey(countdown, "flash_amount2");
  846. unsigned long flash_interval = IntForKey(countdown, "flash_interval");
  847. unsigned long flash_interval_base = IntForKey(countdown, "flash_interval_base");
  848. int flash_color[3];
  849. GetIntVectorForKey(countdown, "flash_color", flash_color);
  850. int flash_relativity = IntForKey(countdown, "flash_relativity");
  851. if(flash_relativity < 1) {
  852. Warning("Flash relativity option < 1. Setting to 1.");
  853. flash_relativity = 1;
  854. } else if(flash_relativity > 3) {
  855. Warning("Flash relativity option > 3. Setting to 1.");
  856. flash_relativity = 1;
  857. }
  858. bool stoptrigger_handling = BoolForKey(countdown, "stoptrigger_handling");
  859. float max_refresh_delay = FloatForKey(countdown, "max_refresh_delay");
  860. float min_refresh_delay = FloatForKey(countdown, "min_refresh_delay");
  861. float min_refresh_random = FloatForKey(countdown, "min_refresh_random");
  862. // We are going to pile all the new entities
  863. // in one spot, so read the origin as well.
  864. int origin[3];
  865. GetIntVectorForKey(countdown, "origin", origin);
  866. // We don't need you anymore!
  867. MarkEntityAsRedundant(countdown);
  868. // <- Done with the hacky reading and checking relevant keyvalues from entity
  869. // Do the actual work ->
  870. // Get the length in seconds one
  871. // unit of every base has.
  872. long double unit_time[BASES];
  873. for(int i = 0; i < unit_bases; i++) {
  874. unit_time[i] = biggest_unit_size;
  875. for(int j = 0; j < i; j++) {
  876. unit_time[i] /= unit_max[j];
  877. }
  878. }
  879. // Shift unit amounts over bases depending
  880. // on which is the interval base to match
  881. // scale of smallest (right-hand) base.
  882. interval_base = unit_bases - interval_base;
  883. flash_interval_base = unit_bases - flash_interval_base;
  884. for(int j = 0; j < unit_bases; j++) {
  885. if(!j) {
  886. for(int i = interval_base + 1; i < unit_bases; i++) {
  887. total_units *= unit_max[i];
  888. }
  889. }
  890. if(redundant[j])
  891. continue;
  892. for(int i = interval_base + 1; i < unit_bases; i++) {
  893. units[j] *= unit_max[i];
  894. }
  895. }
  896. // Calculate start values for the bases
  897. unsigned long base_cur_value[BASES];
  898. if(!count_upwards) {
  899. if(unit_bases == 1) {
  900. base_cur_value[0] = total_units;
  901. } else {
  902. long double factor = 1.0;
  903. for(int j = 2; j < unit_bases; j++) {
  904. factor *= unit_max[j];
  905. }
  906. long double this_units = (total_units / factor);
  907. unsigned long this_unitsf = (unsigned long)(this_units);
  908. base_cur_value[1] = this_unitsf % unit_max[1];
  909. base_cur_value[0] = (unsigned long)(this_units / unit_max[1]);
  910. long double multiply = this_units - this_unitsf;
  911. int i = 1;
  912. while(i < unit_bases - 1) {
  913. i++;
  914. if(i < unit_bases - 1) {
  915. base_cur_value[i] = (unsigned long)(multiply * unit_max[i]);
  916. multiply = multiply * unit_max[i] - base_cur_value[i];
  917. } else {
  918. base_cur_value[i] = ceil(multiply * unit_max[i]);
  919. }
  920. }
  921. }
  922. }
  923. Log("Highest start time: ");
  924. for(int i = 0; i < unit_bases; i++) {
  925. if(!i) {
  926. Log("%i", base_cur_value[i]);
  927. } else {
  928. char cp[ZHLT3_MAX_VALUE];
  929. itoa(unit_max[i] - 1, cp, 10);
  930. int len = (int)(strlen(cp));
  931. Log("%0*i", len, base_cur_value[i]);
  932. }
  933. if(i != unit_bases - 1) {
  934. Log(":");
  935. }
  936. }
  937. Log("\n");
  938. int mm_load = 0;
  939. unsigned long cdir = 0;
  940. if(!count_upwards)
  941. cdir = total_units;
  942. unsigned long total_units_cp = total_units;
  943. long double timeline = 0.0;
  944. bool processed = false;
  945. unsigned long offset = 0;
  946. if(flash_relativity == 3) {
  947. offset = total_units_cp % flash_interval;
  948. }
  949. // Calculate which game_texts will require a version
  950. // for the message flash effect (different color)
  951. bool* req_flashversion = (bool*) malloc(sizeof(bool) * total_units_cp);
  952. for(unsigned long i = total_units_cp; i > 0; i--) {
  953. if(((i + offset) % (flash_interval * unit_max[unit_bases - flash_interval_base])) == 0) {
  954. for(unsigned long j = 0; j < flash_amount2; j++) {
  955. if(flash_amount > 1) { // This is wrong and needs rewriting:
  956. for(unsigned long k = 0; k < flash_amount; k++) {
  957. if((req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base])) >= 0) {
  958. *(req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base])) = true;
  959. }
  960. }
  961. } else {
  962. for(unsigned long k = 0; k < flash_amount; k++) {
  963. 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) {
  964. *(req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base]) + (k * unit_max[unit_bases - flash_interval_base + 1])) = true;
  965. }
  966. }
  967. }
  968. }
  969. } else {
  970. *(req_flashversion + total_units_cp - i) = false;
  971. }
  972. }
  973. if(flash_relativity == 1) {
  974. *(req_flashversion + 0) = true;
  975. }
  976. // Determine and emit the game_texts which incarnate the
  977. // actual countdown. For this I use a counter for the
  978. // time (long double timeline) and a counter for the
  979. // smallest unit base (unsigned long total_units_cp)
  980. // simultaneously/parallel.
  981. while(!processed) {
  982. // Format cur_msg to be the current message to
  983. // be output by the current game_text entity.
  984. char cur_msg[ZHLT3_MAX_VALUE];
  985. strcpy_s(cur_msg, dynamic_indicator);
  986. int shifter = 0;
  987. for(int j = 0; j < unit_bases; j++) {
  988. char cur_val[ZHLT3_MAX_VALUE];
  989. if(j) {
  990. sprintf_s(cur_val, "%0*i", unit_digits, base_cur_value[j]);
  991. } else {
  992. itoa(base_cur_value[0], cur_val, 10);
  993. unit_digits[0] = (int)strlen(cur_val);
  994. }
  995. itoa(base_cur_value[j], cur_val, 10);
  996. strins(cur_msg, ZHLT3_MAX_VALUE - 1, cur_val, separator_pos[j] + shifter, false, ' ');
  997. shifter += unit_digits[j];
  998. }
  999. // Calculate screen position of message
  1000. float newxf = x1f;
  1001. float newyf = y1f;
  1002. if(timeline >= move_delay) {
  1003. if(timeline < (move_delay + move_time)) {
  1004. if(x1f >= 0.0) {
  1005. newxf = x1f + ((x2f - x1f) * ((timeline - move_delay) / move_time));
  1006. }
  1007. if(y1f >= 0.0) {
  1008. newyf = y1f + ((y2f - y1f) * ((timeline - move_delay) / move_time));
  1009. }
  1010. } else {
  1011. newxf = x2f;
  1012. newyf = y2f;
  1013. }
  1014. timeline += move_rdelay;
  1015. }
  1016. // Calculate color blending
  1017. int countdown_color_new[3];
  1018. for(int cchannel = 0; cchannel < 3; cchannel++) {
  1019. countdown_color_new[cchannel] = countdown_color_from[cchannel] +
  1020. ((countdown_color_from[cchannel] - countdown_color_to[cchannel]) * (1.0 / (total_units_cp / total_units)));
  1021. }
  1022. entity_t* game_text = AddEntity("game_text", origin);
  1023. // Count down by one unit - set boolean processed to
  1024. // true when trying to lower base_cur_value[0] to -1.
  1025. if(timeline / unit_time[unit_bases - 1] >= total_units_cp - total_units + 1) {
  1026. for(int j = unit_bases - 1; j >= 0; j--) {
  1027. if(base_cur_value[j] > 0) {
  1028. base_cur_value[j]--;
  1029. break;
  1030. } else {
  1031. if(!j) {
  1032. processed = true;
  1033. } else {
  1034. base_cur_value[j] = unit_max[j] - 1;
  1035. }
  1036. }
  1037. }
  1038. }
  1039. }
  1040. Free(req_flashversion);
  1041. }
  1042. Log("Done.\n");
  1043. }
  1044. }
  1045. */
  1046. //Inserts one string into another for a length of maxNewLen chars at position
  1047. //insAtPos. If overwrite is true, inserted String will overwrite source string
  1048. //from position insAtPos onwards.
  1049. void strins(char* srcDestStr, const int maxNewLen, const char* insStr, const int insAtPos, const bool overwrite, const char fillGap) {
  1050. int srcDestCurPos;
  1051. int insCurPos = 0;
  1052. int srcDestLen = srcDestCurPos = (int)strlen(srcDestStr);
  1053. int insLen = (int)strlen(insStr);
  1054. int fetch = 0;
  1055. char* srcDestStrBackup = (char*) malloc(sizeof(char) * (srcDestLen + 1));
  1056. while(srcDestCurPos < maxNewLen) {
  1057. if(srcDestLen < insAtPos) {
  1058. *(srcDestStr + srcDestCurPos) = fillGap;
  1059. srcDestCurPos++;
  1060. } else break;
  1061. }
  1062. while(srcDestCurPos < maxNewLen) {
  1063. if(insCurPos < insLen) {
  1064. *(srcDestStr + srcDestCurPos) = *(insStr + insCurPos);
  1065. srcDestCurPos++; insCurPos++;
  1066. fetch++;
  1067. }
  1068. }
  1069. if(overwrite) {
  1070. Free(srcDestStrBackup);
  1071. *(srcDestStr + srcDestCurPos) = '\0';
  1072. return;
  1073. }
  1074. while(srcDestCurPos < maxNewLen) {
  1075. if(srcDestCurPos < srcDestLen + fetch) {
  1076. *(srcDestStr + srcDestCurPos) = *(srcDestStrBackup + srcDestCurPos - fetch);
  1077. srcDestCurPos++;
  1078. } else break;
  1079. }
  1080. Free(srcDestStrBackup);
  1081. *(srcDestStr + srcDestCurPos) = '\0';
  1082. }
  1083. int wildcard_num[MAX_MAP_ENTITIES];
  1084. int num_wildcards = 0;
  1085. void DoWildcards() {
  1086. if (num_wildcards > 0) {
  1087. if(num_wildcards > 1) {
  1088. Log("\nFound %i shlt_entity_wildcard entities. Parsing...\n", num_wildcards);
  1089. Warning("One shlt_entity_wildcard entity should be sufficient, but found %i.", num_wildcards);
  1090. } else {
  1091. Log("\nFound 1 shlt_entity_wildcard entity. Parsing...\n");
  1092. }
  1093. for (int a = 0; a < num_wildcards; a++) {
  1094. Verbose(" Parsing shlt_entity_wildcard %i:\n", a);
  1095. int v = wildcard_num[a];
  1096. entity_t* wildcard = &g_entities[v];
  1097. int amount = IntForKey(wildcard, "wildcard_amount");
  1098. if(amount <= 0) {
  1099. Warning("Read too low value (%i) from Wildcard amount - Removing entity.", amount);
  1100. wildcard->epairs = 0;
  1101. } else {
  1102. float delay = FloatForKey(wildcard, "delay");
  1103. char killtarget[ZHLT3_MAX_VALUE];
  1104. strcpy_s(killtarget, ValueForKey(wildcard, "killtarget"));
  1105. if(delay < 0.5) {
  1106. Warning("Delay of %.6f seconds on shlt_entity_wildcard \"%s\" might be too low!", delay, killtarget);
  1107. }
  1108. Verbose("Emitting %i additional info_target entities", amount - 1);
  1109. SetKeyValue(wildcard, "classname", "trigger_auto");
  1110. SetKeyValue(wildcard, "spawnflags", "1");
  1111. RemoveKeyValue(wildcard, "wildcard_amount");
  1112. int origin[3];
  1113. GetIntVectorForKey(wildcard, "origin", origin);
  1114. for(int i = 1; i < amount; i++) {
  1115. entity_t* target = AddEntity("info_target", origin);
  1116. SetKeyValue(target, "targetname", killtarget);
  1117. }
  1118. }
  1119. }
  1120. }
  1121. }
  1122. //<-- SILENCER
  1123. bool ParseEntity(int * lNumAddress, int * lListAddress)
  1124. {
  1125. epair_t* e;
  1126. entity_t* mapent;
  1127. /* All entities parsed? */
  1128. if (!GetToken(true))
  1129. {
  1130. //UnfoldCountdownPackages();
  1131. DoWildcards();
  1132. ParseBrushCopies();
  1133. /* Changes to entity work? */
  1134. if(num_brush_copies > 0 /* || num_countdowns > 0 */ || num_wildcards > 0) {
  1135. UnparseEntities();
  1136. }
  1137. return false;
  1138. }
  1139. if (strcmp(g_token, "{"))
  1140. {
  1141. Error("ParseEntity: { not found");
  1142. }
  1143. if (g_numentities == MAX_MAP_ENTITIES)
  1144. {
  1145. Error("g_numentities == MAX_MAP_ENTITIES");
  1146. }
  1147. mapent = &g_entities[g_numentities];
  1148. g_numentities++;
  1149. while (1)
  1150. {
  1151. if (!GetToken(true))
  1152. {
  1153. Error("ParseEntity: EOF without closing brace");
  1154. }
  1155. if (!strcmp(g_token, "}"))
  1156. {
  1157. break;
  1158. }
  1159. e = ParseEpair();
  1160. e->next = mapent->epairs;
  1161. mapent->epairs = e;
  1162. }
  1163. #ifdef ZHLT_INFO_COMPILE_PARAMETERS // AJM
  1164. if (!strcmp(ValueForKey(mapent, "classname"), "info_compile_parameters"))
  1165. {
  1166. Log("Map entity info_compile_parameters detected, using compile settings\n");
  1167. GetParamsFromEnt(mapent);
  1168. }
  1169. #endif
  1170. // Since the classnames of the following entity types are changed
  1171. // when detected, the following if-checks can only return true in
  1172. // the ParseEntities()-call from hlbsp, since hlbsp is the first
  1173. // tool to call ParseEntities().
  1174. //PROTECTOR -->
  1175. if (!strcmp(ValueForKey(mapent, "classname"), "phlt_copy_brush"))
  1176. {
  1177. brush_copy_num[num_brush_copies] = g_numentities - 1;
  1178. num_brush_copies++;
  1179. } //SILENCER -->
  1180. /*else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_countdown"))
  1181. {
  1182. countdown_num[num_countdowns] = g_numentities - 1;
  1183. num_countdowns++;
  1184. }*/
  1185. else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_entity_wildcard"))
  1186. {
  1187. wildcard_num[num_wildcards] = g_numentities - 1;
  1188. num_wildcards++;
  1189. } //<-- SILENCER
  1190. //<-- PROTECTOR
  1191. //SILENCER -->
  1192. if(!strcmp(g_Program, "hlrad")) {
  1193. if(!strcmp(ValueForKey(mapent, "classname"), "light")
  1194. || !strcmp(ValueForKey(mapent, "classname"), "light_spot")) {
  1195. if(!ValueForKey(mapent, "targetname")[0]) {
  1196. *(lListAddress + *(lNumAddress)) = g_numentities - 1;
  1197. ++*(lNumAddress);
  1198. }
  1199. }
  1200. }
  1201. //<--SILENCER
  1202. return true;
  1203. }
  1204. // =====================================================================================
  1205. // ParseEntities
  1206. // Parses the dentdata string into entities
  1207. // =====================================================================================
  1208. void ParseEntities(int * lNumAddress, int * lListAddress)
  1209. {
  1210. g_numentities = 0;
  1211. ParseFromMemory(g_dentdata, g_entdatasize);
  1212. if(!strcmp(g_Program, "hlrad")) {
  1213. *(lNumAddress) = 0;
  1214. }
  1215. while (ParseEntity(lNumAddress, lListAddress))
  1216. {
  1217. }
  1218. }
  1219. // =====================================================================================
  1220. // UnparseEntities
  1221. // Generates the dentdata string from all the entities
  1222. // =====================================================================================
  1223. void UnparseEntities()
  1224. {
  1225. char* buf;
  1226. char* end;
  1227. epair_t* ep;
  1228. char line[MAXTOKEN];
  1229. int i;
  1230. buf = g_dentdata;
  1231. end = buf;
  1232. *end = 0;
  1233. for (i = 0; i < g_numentities; i++)
  1234. {
  1235. ep = g_entities[i].epairs;
  1236. if (!ep)
  1237. {
  1238. continue; // ent got removed
  1239. }
  1240. strcat(end, "{\n");
  1241. end += 2;
  1242. for (ep = g_entities[i].epairs; ep; ep = ep->next)
  1243. {
  1244. sprintf(line, "\"%s\" \"%s\"\n", ep->key, ep->value);
  1245. strcat(end, line);
  1246. end += strlen(line);
  1247. }
  1248. strcat(end, "}\n");
  1249. end += 2;
  1250. if (end > buf + MAX_MAP_ENTSTRING)
  1251. {
  1252. Error("Entity text too long.");
  1253. }
  1254. }
  1255. g_entdatasize = end - buf + 1;
  1256. }
  1257. // SILENCER -->
  1258. // =====================================================================================
  1259. // MarkEntityAsRedundant
  1260. // strips epairs from entity to provide for its removal in UnparseEntities()
  1261. // =====================================================================================
  1262. void MarkEntityAsRedundant(entity_t* ent) {
  1263. ent->epairs = 0;
  1264. // Do not lower g_numentities, since all we are doing with
  1265. // this, is to take some entity anywhere out of the chain of
  1266. // entities.
  1267. }
  1268. // =====================================================================================
  1269. // AddEntity
  1270. // adds an Entity at given origin
  1271. // =====================================================================================
  1272. entity_t *AddEntity(const char* const classname, int neworigin[3]) {
  1273. if (g_numentities == MAX_MAP_ENTITIES) {
  1274. Error("g_numentities == MAX_MAP_ENTITIES");
  1275. }
  1276. char value[ZHLT3_MAX_VALUE];
  1277. /* Origin-vector should be in ints - all entities **
  1278. ** in *.map have int[3]-based origin */
  1279. sprintf_s(value, "%i %i %i", neworigin[0], neworigin[1], neworigin[2]);
  1280. SetKeyValue(&g_entities[g_numentities], "classname", classname);
  1281. SetKeyValue(&g_entities[g_numentities], "origin", value);
  1282. g_numentities++;
  1283. return &g_entities[g_numentities - 1];
  1284. }
  1285. // =====================================================================================
  1286. // RemoveKeyValue
  1287. // removes a keyvalue from an entity
  1288. // =====================================================================================
  1289. void RemoveKeyValue(entity_t* ent, const char* const key)
  1290. {
  1291. epair_t* ep;
  1292. epair_t* prevep;
  1293. for(ep = ent->epairs; ep; ep = ep->next)
  1294. {
  1295. if(!strcmp(ep->key, key))
  1296. {
  1297. if(!prevep)
  1298. ent->epairs = ep->next;
  1299. else
  1300. prevep->next = ep->next;
  1301. Free(ep);
  1302. return;
  1303. }
  1304. prevep = ep;
  1305. }
  1306. }
  1307. // <-- SILENCER
  1308. // =====================================================================================
  1309. // SetKeyValue
  1310. // creates/changes a keyvalue
  1311. // =====================================================================================
  1312. void SetKeyValue(entity_t* ent, const char* const key, const char* const value)
  1313. {
  1314. epair_t* ep;
  1315. for (ep = ent->epairs; ep; ep = ep->next)
  1316. {
  1317. if (!strcmp(ep->key, key))
  1318. {
  1319. Free(ep->value);
  1320. ep->value = strdup(value);
  1321. return;
  1322. }
  1323. }
  1324. // If the key does not exist, create it and apply value
  1325. ep = (epair_t*)Alloc(sizeof(*ep));
  1326. ep->next = ent->epairs;
  1327. ent->epairs = ep;
  1328. ep->key = strdup(key);
  1329. ep->value = strdup(value);
  1330. }
  1331. // =====================================================================================
  1332. // ValueForKey
  1333. // returns the value for a passed entity and key
  1334. // =====================================================================================
  1335. const char* ValueForKey(const entity_t* const ent, const char* const key)
  1336. {
  1337. epair_t* ep;
  1338. for (ep = ent->epairs; ep; ep = ep->next)
  1339. {
  1340. if (!strcmp(ep->key, key))
  1341. {
  1342. return ep->value;
  1343. }
  1344. }
  1345. return "";
  1346. }
  1347. // =====================================================================================
  1348. // IntForKey
  1349. // =====================================================================================
  1350. int IntForKey(const entity_t* const ent, const char* const key)
  1351. {
  1352. return atoi(ValueForKey(ent, key));
  1353. }
  1354. // =====================================================================================
  1355. // FloatForKey
  1356. // =====================================================================================
  1357. vec_t FloatForKey(const entity_t* const ent, const char* const key)
  1358. {
  1359. return atof(ValueForKey(ent, key));
  1360. }
  1361. // SILENCER -->
  1362. // =====================================================================================
  1363. // BoolForKey
  1364. // =====================================================================================
  1365. bool BoolForKey(const entity_t* const ent, const char* const key)
  1366. {
  1367. return atoi(ValueForKey(ent, key)) != 0;
  1368. }
  1369. // <-- SILENCER
  1370. // =====================================================================================
  1371. // GetVectorForKey
  1372. // returns value for key in vec[0-2]
  1373. // =====================================================================================
  1374. void GetVectorForKey(const entity_t* const ent, const char* const key, vec3_t vec)
  1375. {
  1376. const char* k;
  1377. double v1, v2, v3;
  1378. k = ValueForKey(ent, key);
  1379. // scanf into doubles, then assign, so it is vec_t size independent
  1380. v1 = v2 = v3 = 0;
  1381. sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
  1382. vec[0] = v1;
  1383. vec[1] = v2;
  1384. vec[2] = v3;
  1385. }
  1386. void GetIntVectorForKey(const entity_t* const ent, const char* const key, int vec[3])
  1387. {
  1388. const char* k;
  1389. double v1, v2, v3;
  1390. k = ValueForKey(ent, key);
  1391. // scanf into doubles, then assign, so it is vec_t size independent
  1392. v1 = v2 = v3 = 0;
  1393. sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
  1394. vec[0] = (int)v1;
  1395. vec[1] = (int)v2;
  1396. vec[2] = (int)v3;
  1397. }
  1398. // =====================================================================================
  1399. // FindTargetEntity
  1400. //
  1401. // =====================================================================================
  1402. entity_t *FindTargetEntity(const char* const target)
  1403. {
  1404. int i;
  1405. const char* n;
  1406. for (i = 0; i < g_numentities; i++)
  1407. {
  1408. n = ValueForKey(&g_entities[i], "targetname");
  1409. if (!strcmp(n, target))
  1410. {
  1411. return &g_entities[i];
  1412. }
  1413. }
  1414. return NULL;
  1415. }
  1416. void dtexdata_init()
  1417. {
  1418. g_dtexdata = (byte*)AllocBlock(g_max_map_miptex);
  1419. hlassume(g_dtexdata != NULL, assume_NoMemory);
  1420. g_dlightdata = (byte*)AllocBlock(g_max_map_lightdata);
  1421. hlassume(g_dlightdata != NULL, assume_NoMemory);
  1422. }
  1423. void CDECL dtexdata_free()
  1424. {
  1425. FreeBlock(g_dtexdata);
  1426. g_dtexdata = NULL;
  1427. FreeBlock(g_dlightdata);
  1428. g_dlightdata = NULL;
  1429. }
  1430. // =====================================================================================
  1431. // GetTextureByNumber
  1432. // Touchy function, can fail with a page fault if all the data isnt kosher
  1433. // (i.e. map was compiled with missing textures)
  1434. // =====================================================================================
  1435. char* GetTextureByNumber(int texturenumber)
  1436. {
  1437. texinfo_t* info;
  1438. miptex_t* miptex;
  1439. int ofs;
  1440. info = &g_texinfo[texturenumber];
  1441. ofs = ((dmiptexlump_t*)g_dtexdata)->dataofs[info->miptex];
  1442. miptex = (miptex_t*)(&g_dtexdata[ofs]);
  1443. return miptex->name;
  1444. }
  1445. // =====================================================================================
  1446. // EntityForModel
  1447. // returns entity addy for given modelnum
  1448. // =====================================================================================
  1449. entity_t* EntityForModel(const int modnum)
  1450. {
  1451. int i;
  1452. const char* s;
  1453. char name[16];
  1454. sprintf(name, "*%i", modnum);
  1455. // search the entities for one using modnum
  1456. for (i = 0; i < g_numentities; i++)
  1457. {
  1458. s = ValueForKey(&g_entities[i], "model");
  1459. if (!strcmp(s, name))
  1460. {
  1461. return &g_entities[i];
  1462. }
  1463. }
  1464. return &g_entities[0];
  1465. }