1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696 |
- #ifdef SYSTEM_WIN32
- #pragma warning(disable:4996)
- #endif
- #include "cmdlib.h"
- #include "filelib.h"
- #include "messages.h"
- #include "hlassert.h"
- #include "log.h"
- #include "mathlib.h"
- #include "bspfile.h"
- #include "scriplib.h"
- #include "blockmem.h"
- #include <windows.h>
- int g_max_map_miptex = DEFAULT_MAX_MAP_MIPTEX;
- int g_max_map_lightdata = DEFAULT_MAX_MAP_LIGHTDATA;
- int g_nummodels;
- dmodel_t g_dmodels[MAX_MAP_MODELS];
- int g_dmodels_checksum;
- int g_visdatasize;
- byte g_dvisdata[MAX_MAP_VISIBILITY];
- int g_dvisdata_checksum;
- int g_lightdatasize;
- byte* g_dlightdata;
- int g_dlightdata_checksum;
- int g_texdatasize;
- byte* g_dtexdata;
- int g_dtexdata_checksum;
- int g_entdatasize;
- char g_dentdata[MAX_MAP_ENTSTRING];
- int g_dentdata_checksum;
- int g_numleafs;
- dleaf_t g_dleafs[MAX_MAP_LEAFS];
- int g_dleafs_checksum;
- int g_numplanes;
- dplane_t g_dplanes[MAX_INTERNAL_MAP_PLANES];
- int g_dplanes_checksum;
- int g_numvertexes;
- dvertex_t g_dvertexes[MAX_MAP_VERTS];
- int g_dvertexes_checksum;
- int g_numnodes;
- dnode_t g_dnodes[MAX_MAP_NODES];
- int g_dnodes_checksum;
- int g_numtexinfo;
- texinfo_t g_texinfo[MAX_MAP_TEXINFO];
- int g_texinfo_checksum;
- int g_numfaces;
- dface_t g_dfaces[MAX_MAP_FACES];
- int g_dfaces_checksum;
- int g_numclipnodes;
- dclipnode_t g_dclipnodes[MAX_MAP_CLIPNODES];
- int g_dclipnodes_checksum;
- int g_numedges;
- dedge_t g_dedges[MAX_MAP_EDGES];
- int g_dedges_checksum;
- int g_nummarksurfaces;
- unsigned short g_dmarksurfaces[MAX_MAP_MARKSURFACES];
- int g_dmarksurfaces_checksum;
- int g_numsurfedges;
- int g_dsurfedges[MAX_MAP_SURFEDGES];
- int g_dsurfedges_checksum;
- int g_numentities;
- entity_t g_entities[MAX_MAP_ENTITIES];
- static int FastChecksum(const void* const buffer, int bytes)
- {
- int checksum = 0;
- char* buf = (char*)buffer;
- while (bytes--)
- {
- checksum = rotl(checksum, 4) ^ (*buf);
- buf++;
- }
- return checksum;
- }
- int CompressVis(const byte* const src, const unsigned int src_length, byte* dest, unsigned int dest_length)
- {
- unsigned int j;
- byte* dest_p = dest;
- unsigned int current_length = 0;
- for (j = 0; j < src_length; j++)
- {
- current_length++;
- hlassume(current_length <= dest_length, assume_COMPRESSVIS_OVERFLOW);
- *dest_p = src[j];
- dest_p++;
- if (src[j])
- {
- continue;
- }
- unsigned char rep = 1;
- for (j++; j < src_length; j++)
- {
- if (src[j] || rep == 255)
- {
- break;
- }
- else
- {
- rep++;
- }
- }
- current_length++;
- hlassume(current_length <= dest_length, assume_COMPRESSVIS_OVERFLOW);
- *dest_p = rep;
- dest_p++;
- j--;
- }
- return dest_p - dest;
- }
- void DecompressVis(const byte* src, byte* const dest, const unsigned int dest_length)
- {
- unsigned int current_length = 0;
- int c;
- byte* out;
- int row;
- row = (g_numleafs + 7) >> 3;
- out = dest;
- do
- {
- if (*src)
- {
- current_length++;
- hlassume(current_length <= dest_length, assume_DECOMPRESSVIS_OVERFLOW);
- *out = *src;
- out++;
- src++;
- continue;
- }
- c = src[1];
- src += 2;
- while (c)
- {
- current_length++;
- hlassume(current_length <= dest_length, assume_DECOMPRESSVIS_OVERFLOW);
- *out = 0;
- out++;
- c--;
- if (out - dest >= row)
- {
- return;
- }
- }
- }
- while (out - dest < row);
- }
- static void SwapBSPFile(const bool todisk)
- {
- int i, j, c;
- dmodel_t* d;
- dmiptexlump_t* mtl;
-
- for (i = 0; i < g_nummodels; i++)
- {
- d = &g_dmodels[i];
- for (j = 0; j < MAX_MAP_HULLS; j++)
- {
- d->headnode[j] = LittleLong(d->headnode[j]);
- }
- d->visleafs = LittleLong(d->visleafs);
- d->firstface = LittleLong(d->firstface);
- d->numfaces = LittleLong(d->numfaces);
- for (j = 0; j < 3; j++)
- {
- d->mins[j] = LittleFloat(d->mins[j]);
- d->maxs[j] = LittleFloat(d->maxs[j]);
- d->origin[j] = LittleFloat(d->origin[j]);
- }
- }
-
-
-
- for (i = 0; i < g_numvertexes; i++)
- {
- for (j = 0; j < 3; j++)
- {
- g_dvertexes[i].point[j] = LittleFloat(g_dvertexes[i].point[j]);
- }
- }
-
-
-
- for (i = 0; i < g_numplanes; i++)
- {
- for (j = 0; j < 3; j++)
- {
- g_dplanes[i].normal[j] = LittleFloat(g_dplanes[i].normal[j]);
- }
- g_dplanes[i].dist = LittleFloat(g_dplanes[i].dist);
- g_dplanes[i].type = (planetypes)LittleLong(g_dplanes[i].type);
- }
-
-
-
- for (i = 0; i < g_numtexinfo; i++)
- {
- for (j = 0; j < 8; j++)
- {
- g_texinfo[i].vecs[0][j] = LittleFloat(g_texinfo[i].vecs[0][j]);
- }
- g_texinfo[i].miptex = LittleLong(g_texinfo[i].miptex);
- g_texinfo[i].flags = LittleLong(g_texinfo[i].flags);
- }
-
-
-
- for (i = 0; i < g_numfaces; i++)
- {
- g_dfaces[i].texinfo = LittleShort(g_dfaces[i].texinfo);
- g_dfaces[i].planenum = LittleShort(g_dfaces[i].planenum);
- g_dfaces[i].side = LittleShort(g_dfaces[i].side);
- g_dfaces[i].lightofs = LittleLong(g_dfaces[i].lightofs);
- g_dfaces[i].firstedge = LittleLong(g_dfaces[i].firstedge);
- g_dfaces[i].numedges = LittleShort(g_dfaces[i].numedges);
- }
-
-
-
- for (i = 0; i < g_numnodes; i++)
- {
- g_dnodes[i].planenum = LittleLong(g_dnodes[i].planenum);
- for (j = 0; j < 3; j++)
- {
- g_dnodes[i].mins[j] = LittleShort(g_dnodes[i].mins[j]);
- g_dnodes[i].maxs[j] = LittleShort(g_dnodes[i].maxs[j]);
- }
- g_dnodes[i].children[0] = LittleShort(g_dnodes[i].children[0]);
- g_dnodes[i].children[1] = LittleShort(g_dnodes[i].children[1]);
- g_dnodes[i].firstface = LittleShort(g_dnodes[i].firstface);
- g_dnodes[i].numfaces = LittleShort(g_dnodes[i].numfaces);
- }
-
-
-
- for (i = 0; i < g_numleafs; i++)
- {
- g_dleafs[i].contents = LittleLong(g_dleafs[i].contents);
- for (j = 0; j < 3; j++)
- {
- g_dleafs[i].mins[j] = LittleShort(g_dleafs[i].mins[j]);
- g_dleafs[i].maxs[j] = LittleShort(g_dleafs[i].maxs[j]);
- }
- g_dleafs[i].firstmarksurface = LittleShort(g_dleafs[i].firstmarksurface);
- g_dleafs[i].nummarksurfaces = LittleShort(g_dleafs[i].nummarksurfaces);
- g_dleafs[i].visofs = LittleLong(g_dleafs[i].visofs);
- }
-
-
-
- for (i = 0; i < g_numclipnodes; i++)
- {
- g_dclipnodes[i].planenum = LittleLong(g_dclipnodes[i].planenum);
- g_dclipnodes[i].children[0] = LittleShort(g_dclipnodes[i].children[0]);
- g_dclipnodes[i].children[1] = LittleShort(g_dclipnodes[i].children[1]);
- }
-
-
-
- if (g_texdatasize)
- {
- mtl = (dmiptexlump_t*)g_dtexdata;
- if (todisk)
- {
- c = mtl->nummiptex;
- }
- else
- {
- c = LittleLong(mtl->nummiptex);
- }
- mtl->nummiptex = LittleLong(mtl->nummiptex);
- for (i = 0; i < c; i++)
- {
- mtl->dataofs[i] = LittleLong(mtl->dataofs[i]);
- }
- }
-
-
-
- for (i = 0; i < g_nummarksurfaces; i++)
- {
- g_dmarksurfaces[i] = LittleShort(g_dmarksurfaces[i]);
- }
-
-
-
- for (i = 0; i < g_numsurfedges; i++)
- {
- g_dsurfedges[i] = LittleLong(g_dsurfedges[i]);
- }
-
-
-
- for (i = 0; i < g_numedges; i++)
- {
- g_dedges[i].v[0] = LittleShort(g_dedges[i].v[0]);
- g_dedges[i].v[1] = LittleShort(g_dedges[i].v[1]);
- }
- }
- static int CopyLump(int lump, void* dest, int size, const dheader_t* const header)
- {
- int length, ofs;
- length = header->lumps[lump].filelen;
- ofs = header->lumps[lump].fileofs;
- if (length % size)
- {
- Error("LoadBSPFile: odd lump size");
- }
-
-
- if(lump == LUMP_TEXTURES && dest == (void*)g_dtexdata)
- { hlassume(g_max_map_miptex > length, assume_MAX_MAP_MIPTEX); }
- else if(lump == LUMP_LIGHTING && dest == (void*)g_dlightdata)
- { hlassume(g_max_map_lightdata > length, assume_MAX_MAP_LIGHTING); }
- memcpy(dest, (byte*) header + ofs, length);
- return length / size;
- }
- void LoadBSPFile(const char* const filename)
- {
- dheader_t* header;
- LoadFile(filename, (char**)&header);
- LoadBSPImage(header);
- }
- void LoadBSPImage(dheader_t* const header)
- {
- unsigned int i;
-
- for (i = 0; i < sizeof(dheader_t) / 4; i++)
- {
- ((int*)header)[i] = LittleLong(((int*)header)[i]);
- }
- if (header->version != BSPVERSION)
- {
- Error("BSP is version %i, not %i", header->version, BSPVERSION);
- }
- g_nummodels = CopyLump(LUMP_MODELS, g_dmodels, sizeof(dmodel_t), header);
- g_numvertexes = CopyLump(LUMP_VERTEXES, g_dvertexes, sizeof(dvertex_t), header);
- g_numplanes = CopyLump(LUMP_PLANES, g_dplanes, sizeof(dplane_t), header);
- g_numleafs = CopyLump(LUMP_LEAFS, g_dleafs, sizeof(dleaf_t), header);
- g_numnodes = CopyLump(LUMP_NODES, g_dnodes, sizeof(dnode_t), header);
- g_numtexinfo = CopyLump(LUMP_TEXINFO, g_texinfo, sizeof(texinfo_t), header);
- g_numclipnodes = CopyLump(LUMP_CLIPNODES, g_dclipnodes, sizeof(dclipnode_t), header);
- g_numfaces = CopyLump(LUMP_FACES, g_dfaces, sizeof(dface_t), header);
- g_nummarksurfaces = CopyLump(LUMP_MARKSURFACES, g_dmarksurfaces, sizeof(g_dmarksurfaces[0]), header);
- g_numsurfedges = CopyLump(LUMP_SURFEDGES, g_dsurfedges, sizeof(g_dsurfedges[0]), header);
- g_numedges = CopyLump(LUMP_EDGES, g_dedges, sizeof(dedge_t), header);
- g_texdatasize = CopyLump(LUMP_TEXTURES, g_dtexdata, 1, header);
- g_visdatasize = CopyLump(LUMP_VISIBILITY, g_dvisdata, 1, header);
- g_lightdatasize = CopyLump(LUMP_LIGHTING, g_dlightdata, 1, header);
- g_entdatasize = CopyLump(LUMP_ENTITIES, g_dentdata, 1, header);
- Free(header);
-
-
-
- SwapBSPFile(false);
- g_dmodels_checksum = FastChecksum(g_dmodels, g_nummodels * sizeof(g_dmodels[0]));
- g_dvertexes_checksum = FastChecksum(g_dvertexes, g_numvertexes * sizeof(g_dvertexes[0]));
- g_dplanes_checksum = FastChecksum(g_dplanes, g_numplanes * sizeof(g_dplanes[0]));
- g_dleafs_checksum = FastChecksum(g_dleafs, g_numleafs * sizeof(g_dleafs[0]));
- g_dnodes_checksum = FastChecksum(g_dnodes, g_numnodes * sizeof(g_dnodes[0]));
- g_texinfo_checksum = FastChecksum(g_texinfo, g_numtexinfo * sizeof(g_texinfo[0]));
- g_dclipnodes_checksum = FastChecksum(g_dclipnodes, g_numclipnodes * sizeof(g_dclipnodes[0]));
- g_dfaces_checksum = FastChecksum(g_dfaces, g_numfaces * sizeof(g_dfaces[0]));
- g_dmarksurfaces_checksum = FastChecksum(g_dmarksurfaces, g_nummarksurfaces * sizeof(g_dmarksurfaces[0]));
- g_dsurfedges_checksum = FastChecksum(g_dsurfedges, g_numsurfedges * sizeof(g_dsurfedges[0]));
- g_dedges_checksum = FastChecksum(g_dedges, g_numedges * sizeof(g_dedges[0]));
- g_dtexdata_checksum = FastChecksum(g_dtexdata, g_numedges * sizeof(g_dtexdata[0]));
- g_dvisdata_checksum = FastChecksum(g_dvisdata, g_visdatasize * sizeof(g_dvisdata[0]));
- g_dlightdata_checksum = FastChecksum(g_dlightdata, g_lightdatasize * sizeof(g_dlightdata[0]));
- g_dentdata_checksum = FastChecksum(g_dentdata, g_entdatasize * sizeof(g_dentdata[0]));
- }
- static void AddLump(int lumpnum, void* data, int len, dheader_t* header, FILE* bspfile)
- {
- lump_t* lump =&header->lumps[lumpnum];
- lump->fileofs = LittleLong(ftell(bspfile));
- lump->filelen = LittleLong(len);
- SafeWrite(bspfile, data, (len + 3) & ~3);
- }
- void WriteBSPFile(const char* const filename)
- {
- dheader_t outheader;
- dheader_t* header;
- FILE* bspfile;
- header = &outheader;
- memset(header, 0, sizeof(dheader_t));
- SwapBSPFile(true);
- header->version = LittleLong(BSPVERSION);
- bspfile = SafeOpenWrite(filename);
- SafeWrite(bspfile, header, sizeof(dheader_t));
-
- AddLump(LUMP_PLANES, g_dplanes, g_numplanes * sizeof(dplane_t), header, bspfile);
- AddLump(LUMP_LEAFS, g_dleafs, g_numleafs * sizeof(dleaf_t), header, bspfile);
- AddLump(LUMP_VERTEXES, g_dvertexes, g_numvertexes * sizeof(dvertex_t), header, bspfile);
- AddLump(LUMP_NODES, g_dnodes, g_numnodes * sizeof(dnode_t), header, bspfile);
- AddLump(LUMP_TEXINFO, g_texinfo, g_numtexinfo * sizeof(texinfo_t), header, bspfile);
- AddLump(LUMP_FACES, g_dfaces, g_numfaces * sizeof(dface_t), header, bspfile);
- AddLump(LUMP_CLIPNODES, g_dclipnodes, g_numclipnodes * sizeof(dclipnode_t), header, bspfile);
- AddLump(LUMP_MARKSURFACES, g_dmarksurfaces, g_nummarksurfaces * sizeof(g_dmarksurfaces[0]), header, bspfile);
- AddLump(LUMP_SURFEDGES, g_dsurfedges, g_numsurfedges * sizeof(g_dsurfedges[0]), header, bspfile);
- AddLump(LUMP_EDGES, g_dedges, g_numedges * sizeof(dedge_t), header, bspfile);
- AddLump(LUMP_MODELS, g_dmodels, g_nummodels * sizeof(dmodel_t), header, bspfile);
- AddLump(LUMP_LIGHTING, g_dlightdata, g_lightdatasize, header, bspfile);
- AddLump(LUMP_VISIBILITY,g_dvisdata, g_visdatasize, header, bspfile);
- AddLump(LUMP_ENTITIES, g_dentdata, g_entdatasize, header, bspfile);
- AddLump(LUMP_TEXTURES, g_dtexdata, g_texdatasize, header, bspfile);
- fseek(bspfile, 0, SEEK_SET);
- SafeWrite(bspfile, header, sizeof(dheader_t));
- fclose(bspfile);
- }
- #define ENTRIES(a) (sizeof(a)/sizeof(*(a)))
- #define ENTRYSIZE(a) (sizeof(*(a)))
- static int ArrayUsage(const char* const szItem, const int items, const int maxitems, const int itemsize)
- {
- float percentage = maxitems ? items * 100.0 / maxitems : 0.0;
-
-
- Log("%-12s %9i/%-9i %9i/%-9i (%4.1f%%)\n", szItem, items, maxitems, items * itemsize, maxitems * itemsize, percentage);
- return items * itemsize;
- }
- static int GlobUsage(const char* const szItem, const int itemstorage, const int maxstorage)
- {
- float percentage = maxstorage ? itemstorage * 100.0 / maxstorage : 0.0;
- Log("%-12s [variable] %9i/%-9i (%4.1f%%)\n", szItem, itemstorage, maxstorage, percentage);
- return itemstorage;
- }
- void PrintBSPFileSizes()
- {
- int numtextures = g_texdatasize ? ((dmiptexlump_t*)g_dtexdata)->nummiptex : 0;
- int totalmemory = 0;
- Log("\n");
- Log("Object names Objects/Maxobjs Memory / Maxmem Fullness\n");
- Log("------------ ------------------- ------------------- --------\n");
- totalmemory += ArrayUsage("models", g_nummodels, ENTRIES(g_dmodels), ENTRYSIZE(g_dmodels));
- totalmemory += ArrayUsage("planes", g_numplanes, MAX_MAP_PLANES, ENTRYSIZE(g_dplanes));
- totalmemory += ArrayUsage("vertexes", g_numvertexes, ENTRIES(g_dvertexes), ENTRYSIZE(g_dvertexes));
- totalmemory += ArrayUsage("nodes", g_numnodes, ENTRIES(g_dnodes), ENTRYSIZE(g_dnodes));
- totalmemory += ArrayUsage("texinfos", g_numtexinfo, ENTRIES(g_texinfo), ENTRYSIZE(g_texinfo));
- totalmemory += ArrayUsage("faces", g_numfaces, ENTRIES(g_dfaces), ENTRYSIZE(g_dfaces));
- totalmemory += ArrayUsage("clipnodes", g_numclipnodes, 32767, ENTRYSIZE(g_dclipnodes));
- totalmemory += ArrayUsage("leaves", g_numleafs, ENTRIES(g_dleafs), ENTRYSIZE(g_dleafs));
- totalmemory += ArrayUsage("marksurfaces", g_nummarksurfaces, ENTRIES(g_dmarksurfaces), ENTRYSIZE(g_dmarksurfaces));
- totalmemory += ArrayUsage("surfedges", g_numsurfedges, ENTRIES(g_dsurfedges), ENTRYSIZE(g_dsurfedges));
- totalmemory += ArrayUsage("edges", g_numedges, ENTRIES(g_dedges), ENTRYSIZE(g_dedges));
- totalmemory += GlobUsage("texdata", g_texdatasize, g_max_map_miptex);
- totalmemory += GlobUsage("lightdata", g_lightdatasize, g_max_map_lightdata);
- totalmemory += GlobUsage("visdata", g_visdatasize, sizeof(g_dvisdata));
- totalmemory += GlobUsage("entdata", g_entdatasize, sizeof(g_dentdata));
- Log("=> Total BSP file data space used: %d bytes.\n", totalmemory);
-
-
- if(!strcmp(g_Program, "hlbsp")) {
- if(g_numleafs > MAX_MAP_LEAFS) {
- Log("\n");
- Warning("Exceeded MAX_MAP_LEAFS. File write failure immediate.\n");
- } else if(g_numleafs > 8192) {
- Log("\n");
- Warning("Surpassed 8192 leafs, which is the old maximum.\n"
- " If you encounter problems when running your map,\n"
- " consider this the most likely cause. There exist no\n"
- " records about this ever causing problems, though.\n");
- }
- }
-
- Log("Textures referenced: %i\n\n", numtextures);
-
- }
- epair_t* ParseEpair()
- {
- epair_t* e;
- e = (epair_t*)Alloc(sizeof(epair_t));
- if (strlen(g_token) >= MAX_KEY - 1)
- Error("ParseEpair: Key token too long (%i > MAX_KEY)", (int)strlen(g_token));
- e->key = _strdup(g_token);
- GetToken(false);
- if (strlen(g_token) >= ZHLT3_MAX_VALUE - 1)
- Error("ParseEpar: Value token too long (%i > ZHLT3_MAX_VALUE)", (int)strlen(g_token));
- e->value = _strdup(g_token);
- return e;
- }
- #ifdef ZHLT_INFO_COMPILE_PARAMETERS
- extern void GetParamsFromEnt(entity_t* mapent);
- #endif
- int brush_copy_num[MAX_MAP_ENTITIES];
- int num_brush_copies = 0;
- void ParseBrushCopies()
- {
- if (num_brush_copies>0)
- {
- if(num_brush_copies>1)
- Log("\nFound %i phlt_copy_brush entities. Parsing...\n", num_brush_copies);
- else
- Log("\nFound 1 phlt_copy_brush entity. Parsing...\n");
-
- for (int i = 0; i < num_brush_copies; i++)
- {
- Verbose(" Parsing phlt_copy_brush %i:\n",i);
- int v = brush_copy_num[i];
- entity_t *copy_brush = &g_entities[v];
- char targetname[MAX_KEY];
- strcpy_s(targetname, ValueForKey(copy_brush, "phlt_cpm_target"));
-
- if (!strcmp(targetname,""))
- {
- Log(" ");
- Warning("No copy target for phlt_copy_brush %i (%s).\n"
- " Using default. (models/chumtoad.mdl)",i,targetname);
- SetKeyValue(copy_brush, "model", "models/chumtoad.mdl");
- }
- else
- {
- entity_t *target_ent = FindTargetEntity(targetname);
- if (!target_ent)
- {
- Log(" ");
- Warning("Could not find target %s for phlt_copy_brush %i.\n"
- " Using default. (models/chumtoad.mdl)",targetname,i);
- SetKeyValue(copy_brush, "model", "models/chumtoad.mdl");
- }
- else
- {
- char model_num[MAX_KEY];
- strcpy_s(model_num, ValueForKey(target_ent, "model"));
- if (!strcmp(model_num,""))
- {
- Log(" ");
- Warning("No model found at entity %s for phlt_copy_brush %i.\n"
- " Using default. (models/chumtoad.mdl)",targetname,i);
- SetKeyValue(copy_brush, "model", "models/chumtoad.mdl");
- }
- else
- {
- SetKeyValue(copy_brush, "model", model_num);
- }
- }
- }
- char classname[MAX_KEY];
- strcpy_s(classname, ValueForKey(copy_brush, "phlt_cpm_class"));
- Verbose(" Parsed. (Copy target: %s)\n",targetname);
- if (!strcmp(classname,""))
- {
- Log(" ");
- Warning("Created new func_illusionary as default, since no\n"
- " classname was given. However, classname should be given!");
- SetKeyValue(copy_brush, "classname", "func_illusionary");
- }
- else
- {
- Verbose(" Created new %s.\n",classname);
- SetKeyValue(copy_brush, "classname", classname);
- }
-
- RemoveKeyValue(copy_brush, "phlt_cpm_target");
- RemoveKeyValue(copy_brush, "phlt_cpm_class");
- }
- Log("Done.\n");
- }
- }
- void strins(char* srcDestStr, const int maxNewLen, const char* insStr, const int insAtPos, const bool overwrite, const char fillGap) {
- int srcDestCurPos;
- int insCurPos = 0;
- int srcDestLen = srcDestCurPos = (int)strlen(srcDestStr);
- int insLen = (int)strlen(insStr);
- int fetch = 0;
- char* srcDestStrBackup = (char*) malloc(sizeof(char) * (srcDestLen + 1));
- while(srcDestCurPos < maxNewLen) {
- if(srcDestLen < insAtPos) {
- *(srcDestStr + srcDestCurPos) = fillGap;
- srcDestCurPos++;
- } else break;
- }
- while(srcDestCurPos < maxNewLen) {
- if(insCurPos < insLen) {
- *(srcDestStr + srcDestCurPos) = *(insStr + insCurPos);
- srcDestCurPos++; insCurPos++;
- fetch++;
- }
- }
- if(overwrite) {
- Free(srcDestStrBackup);
- *(srcDestStr + srcDestCurPos) = '\0';
- return;
- }
- while(srcDestCurPos < maxNewLen) {
- if(srcDestCurPos < srcDestLen + fetch) {
- *(srcDestStr + srcDestCurPos) = *(srcDestStrBackup + srcDestCurPos - fetch);
- srcDestCurPos++;
- } else break;
- }
- Free(srcDestStrBackup);
- *(srcDestStr + srcDestCurPos) = '\0';
- }
- int wildcard_num[MAX_MAP_ENTITIES];
- int num_wildcards = 0;
- void DoWildcards() {
- if (num_wildcards > 0) {
- if(num_wildcards > 1) {
- Log("\nFound %i shlt_entity_wildcard entities. Parsing...\n", num_wildcards);
- Warning("One shlt_entity_wildcard entity should be sufficient, but found %i.", num_wildcards);
- } else {
- Log("\nFound 1 shlt_entity_wildcard entity. Parsing...\n");
- }
- for (int a = 0; a < num_wildcards; a++) {
- Verbose(" Parsing shlt_entity_wildcard %i:\n", a);
- int v = wildcard_num[a];
- entity_t* wildcard = &g_entities[v];
- int amount = IntForKey(wildcard, "wildcard_amount");
- if(amount <= 0) {
- Warning("Read too low value (%i) from Wildcard amount - Removing entity.", amount);
- wildcard->epairs = 0;
- } else {
- float delay = FloatForKey(wildcard, "delay");
- char killtarget[ZHLT3_MAX_VALUE];
- strcpy_s(killtarget, ValueForKey(wildcard, "killtarget"));
- if(delay < 0.5) {
- Warning("Delay of %.6f seconds on shlt_entity_wildcard \"%s\" might be too low!", delay, killtarget);
- }
- Verbose("Emitting %i additional info_target entities", amount - 1);
- SetKeyValue(wildcard, "classname", "trigger_auto");
- SetKeyValue(wildcard, "spawnflags", "1");
- RemoveKeyValue(wildcard, "wildcard_amount");
- int origin[3];
- GetIntVectorForKey(wildcard, "origin", origin);
- for(int i = 1; i < amount; i++) {
- entity_t* target = AddEntity("info_target", origin);
- SetKeyValue(target, "targetname", killtarget);
- }
- }
- }
- }
- }
- bool ParseEntity(int * lNumAddress, int * lListAddress)
- {
- epair_t* e;
- entity_t* mapent;
-
- if (!GetToken(true))
- {
-
- DoWildcards();
- ParseBrushCopies();
-
-
- if(num_brush_copies > 0 || num_wildcards > 0) {
- UnparseEntities();
- }
- return false;
- }
- if (strcmp(g_token, "{"))
- {
- Error("ParseEntity: { not found");
- }
- if (g_numentities == MAX_MAP_ENTITIES)
- {
- Error("g_numentities == MAX_MAP_ENTITIES");
- }
- mapent = &g_entities[g_numentities];
- g_numentities++;
- while (1)
- {
- if (!GetToken(true))
- {
- Error("ParseEntity: EOF without closing brace");
- }
- if (!strcmp(g_token, "}"))
- {
- break;
- }
- e = ParseEpair();
- e->next = mapent->epairs;
- mapent->epairs = e;
- }
- #ifdef ZHLT_INFO_COMPILE_PARAMETERS
- if (!strcmp(ValueForKey(mapent, "classname"), "info_compile_parameters"))
- {
- Log("Map entity info_compile_parameters detected, using compile settings\n");
- GetParamsFromEnt(mapent);
- }
- #endif
-
-
-
-
-
-
- if (!strcmp(ValueForKey(mapent, "classname"), "phlt_copy_brush"))
- {
- brush_copy_num[num_brush_copies] = g_numentities - 1;
- num_brush_copies++;
- }
-
- else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_entity_wildcard"))
- {
- wildcard_num[num_wildcards] = g_numentities - 1;
- num_wildcards++;
- }
-
-
- if(!strcmp(g_Program, "hlrad")) {
- if(!strcmp(ValueForKey(mapent, "classname"), "light")
- || !strcmp(ValueForKey(mapent, "classname"), "light_spot")) {
- if(!ValueForKey(mapent, "targetname")[0]) {
- *(lListAddress + *(lNumAddress)) = g_numentities - 1;
- ++*(lNumAddress);
- }
- }
- }
-
- return true;
- }
- void ParseEntities(int * lNumAddress, int * lListAddress)
- {
- g_numentities = 0;
- ParseFromMemory(g_dentdata, g_entdatasize);
-
- if(!strcmp(g_Program, "hlrad")) {
- *(lNumAddress) = 0;
- }
- while (ParseEntity(lNumAddress, lListAddress))
- {
- }
- }
- void UnparseEntities()
- {
- char* buf;
- char* end;
- epair_t* ep;
- char line[MAXTOKEN];
- int i;
- buf = g_dentdata;
- end = buf;
- *end = 0;
- for (i = 0; i < g_numentities; i++)
- {
- ep = g_entities[i].epairs;
- if (!ep)
- {
- continue;
- }
- strcat(end, "{\n");
- end += 2;
- for (ep = g_entities[i].epairs; ep; ep = ep->next)
- {
- sprintf(line, "\"%s\" \"%s\"\n", ep->key, ep->value);
- strcat(end, line);
- end += strlen(line);
- }
- strcat(end, "}\n");
- end += 2;
- if (end > buf + MAX_MAP_ENTSTRING)
- {
- Error("Entity text too long.");
- }
- }
- g_entdatasize = end - buf + 1;
- }
- void MarkEntityAsRedundant(entity_t* ent) {
- ent->epairs = 0;
-
-
-
- }
- entity_t *AddEntity(const char* const classname, int neworigin[3]) {
- if (g_numentities == MAX_MAP_ENTITIES) {
- Error("g_numentities == MAX_MAP_ENTITIES");
- }
- char value[ZHLT3_MAX_VALUE];
-
- sprintf_s(value, "%i %i %i", neworigin[0], neworigin[1], neworigin[2]);
- SetKeyValue(&g_entities[g_numentities], "classname", classname);
- SetKeyValue(&g_entities[g_numentities], "origin", value);
- g_numentities++;
- return &g_entities[g_numentities - 1];
- }
- void RemoveKeyValue(entity_t* ent, const char* const key)
- {
- epair_t* ep;
- epair_t* prevep;
- for(ep = ent->epairs; ep; ep = ep->next)
- {
- if(!strcmp(ep->key, key))
- {
- if(!prevep)
- ent->epairs = ep->next;
- else
- prevep->next = ep->next;
- Free(ep);
- return;
- }
- prevep = ep;
- }
- }
- void SetKeyValue(entity_t* ent, const char* const key, const char* const value)
- {
- epair_t* ep;
- for (ep = ent->epairs; ep; ep = ep->next)
- {
- if (!strcmp(ep->key, key))
- {
- Free(ep->value);
- ep->value = strdup(value);
- return;
- }
- }
-
- ep = (epair_t*)Alloc(sizeof(*ep));
- ep->next = ent->epairs;
- ent->epairs = ep;
- ep->key = strdup(key);
- ep->value = strdup(value);
- }
- const char* ValueForKey(const entity_t* const ent, const char* const key)
- {
- epair_t* ep;
- for (ep = ent->epairs; ep; ep = ep->next)
- {
- if (!strcmp(ep->key, key))
- {
- return ep->value;
- }
- }
- return "";
- }
- int IntForKey(const entity_t* const ent, const char* const key)
- {
- return atoi(ValueForKey(ent, key));
- }
- vec_t FloatForKey(const entity_t* const ent, const char* const key)
- {
- return atof(ValueForKey(ent, key));
- }
- bool BoolForKey(const entity_t* const ent, const char* const key)
- {
- return atoi(ValueForKey(ent, key)) != 0;
- }
- void GetVectorForKey(const entity_t* const ent, const char* const key, vec3_t vec)
- {
- const char* k;
- double v1, v2, v3;
- k = ValueForKey(ent, key);
-
- v1 = v2 = v3 = 0;
- sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
- vec[0] = v1;
- vec[1] = v2;
- vec[2] = v3;
- }
- void GetIntVectorForKey(const entity_t* const ent, const char* const key, int vec[3])
- {
- const char* k;
- double v1, v2, v3;
- k = ValueForKey(ent, key);
-
- v1 = v2 = v3 = 0;
- sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
- vec[0] = (int)v1;
- vec[1] = (int)v2;
- vec[2] = (int)v3;
- }
- entity_t *FindTargetEntity(const char* const target)
- {
- int i;
- const char* n;
- for (i = 0; i < g_numentities; i++)
- {
- n = ValueForKey(&g_entities[i], "targetname");
- if (!strcmp(n, target))
- {
- return &g_entities[i];
- }
- }
- return NULL;
- }
- void dtexdata_init()
- {
- g_dtexdata = (byte*)AllocBlock(g_max_map_miptex);
- hlassume(g_dtexdata != NULL, assume_NoMemory);
- g_dlightdata = (byte*)AllocBlock(g_max_map_lightdata);
- hlassume(g_dlightdata != NULL, assume_NoMemory);
- }
- void CDECL dtexdata_free()
- {
- FreeBlock(g_dtexdata);
- g_dtexdata = NULL;
- FreeBlock(g_dlightdata);
- g_dlightdata = NULL;
- }
- char* GetTextureByNumber(int texturenumber)
- {
- texinfo_t* info;
- miptex_t* miptex;
- int ofs;
- info = &g_texinfo[texturenumber];
- ofs = ((dmiptexlump_t*)g_dtexdata)->dataofs[info->miptex];
- miptex = (miptex_t*)(&g_dtexdata[ofs]);
- return miptex->name;
- }
- entity_t* EntityForModel(const int modnum)
- {
- int i;
- const char* s;
- char name[16];
- sprintf(name, "*%i", modnum);
-
- for (i = 0; i < g_numentities; i++)
- {
- s = ValueForKey(&g_entities[i], "model");
- if (!strcmp(s, name))
- {
- return &g_entities[i];
- }
- }
- return &g_entities[0];
- }
|