1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678 |
- #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> //SILENCER
- //=============================================================================
- 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; // (dmiptexlump_t)
- 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];
- /*
- * ===============
- * FastChecksum
- * ===============
- */
- 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;
- }
- /*
- * ===============
- * CompressVis
- * ===============
- */
- 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;
- }
- // =====================================================================================
- // DecompressVis
- //
- // =====================================================================================
- 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);
- }
- //
- // =====================================================================================
- //
- // =====================================================================================
- // SwapBSPFile
- // byte swaps all data in a bsp file
- // =====================================================================================
- static void SwapBSPFile(const bool todisk)
- {
- int i, j, c;
- dmodel_t* d;
- dmiptexlump_t* mtl;
- // models
- 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]);
- }
- }
- //
- // vertexes
- //
- for (i = 0; i < g_numvertexes; i++)
- {
- for (j = 0; j < 3; j++)
- {
- g_dvertexes[i].point[j] = LittleFloat(g_dvertexes[i].point[j]);
- }
- }
- //
- // planes
- //
- 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);
- }
- //
- // texinfos
- //
- 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);
- }
- //
- // faces
- //
- 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);
- }
- //
- // nodes
- //
- 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);
- }
- //
- // leafs
- //
- 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);
- }
- //
- // clipnodes
- //
- 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]);
- }
- //
- // miptex
- //
- 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]);
- }
- }
- //
- // marksurfaces
- //
- for (i = 0; i < g_nummarksurfaces; i++)
- {
- g_dmarksurfaces[i] = LittleShort(g_dmarksurfaces[i]);
- }
- //
- // surfedges
- //
- for (i = 0; i < g_numsurfedges; i++)
- {
- g_dsurfedges[i] = LittleLong(g_dsurfedges[i]);
- }
- //
- // edges
- //
- 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]);
- }
- }
- // =====================================================================================
- // CopyLump
- // balh
- // =====================================================================================
- 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");
- }
-
- //special handling for tex and lightdata to keep things from exploding - KGP
- 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;
- }
- // =====================================================================================
- // LoadBSPFile
- // balh
- // =====================================================================================
- void LoadBSPFile(const char* const filename)
- {
- dheader_t* header;
- LoadFile(filename, (char**)&header);
- LoadBSPImage(header);
- }
- // =====================================================================================
- // LoadBSPImage
- // balh
- // =====================================================================================
- void LoadBSPImage(dheader_t* const header)
- {
- unsigned int i;
- // swap the header
- 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); // everything has been copied out
- //
- // swap everything
- //
- 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]));
- }
- //
- // =====================================================================================
- //
- // =====================================================================================
- // AddLump
- // balh
- // =====================================================================================
- 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);
- }
- // =====================================================================================
- // WriteBSPFile
- // Swaps the bsp file in place, so it should not be referenced again
- // =====================================================================================
- 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)); // overwritten later
- // LUMP TYPE DATA LENGTH HEADER BSPFILE
- 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)))
- // =====================================================================================
- // ArrayUsage
- // blah
- // =====================================================================================
- 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;
-
- //SILENCER: Increased max spacing value from 7 to 9, since values have grown larger.
- Log("%-12s %9i/%-9i %9i/%-9i (%4.1f%%)\n", szItem, items, maxitems, items * itemsize, maxitems * itemsize, percentage);
- return items * itemsize;
- }
- // =====================================================================================
- // GlobUsage
- // print out global ussage line in chart
- // =====================================================================================
- 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;
- }
- // =====================================================================================
- // PrintBSPFileSizes
- // Dumps info about current file
- // =====================================================================================
- 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);
-
- //SILENCER -->
- if(!strcmp(g_Program, "hlbsp")) { //Only warn in 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");
- }
- }
- //<-- SILENCER
- Log("Textures referenced: %i\n\n", numtextures);
-
- }
- // =====================================================================================
- // ParseEpair
- // entity key/value pairs
- // =====================================================================================
- 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;
- }
- /*
- * ================
- * ParseEntity
- * ================
- */
- #ifdef ZHLT_INFO_COMPILE_PARAMETERS
- // AJM: each tool should have its own version of GetParamsFromEnt which parseentity calls
- extern void GetParamsFromEnt(entity_t* mapent);
- #endif
- //PROTECTOR -->
- 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);
- }
- }
- Log("Done.\n");
- }
- }
- //<-- PROTECTOR
- //SILENCER -->
- /*
- // UNFINISHED!
- int countdown_num[MAX_MAP_ENTITIES];
- int num_countdowns = 0;
- void UnfoldCountdownPackages() {
- if (num_countdowns > 0) {
- const int BASES = 16;
- const int TNAMES = 32;
- const int TARGETS = 16;
- if(num_countdowns > 1) {
- Log("\nFound %i shlt_countdown entities. Parsing...\n", num_countdowns);
- } else {
- Log("\nFound 1 shlt_countdown entity. Parsing...\n");
- }
- for (int a = 0; a < num_countdowns; a++) {
- Verbose(" Parsing shlt_countdown %i.\n", a);
- int v = countdown_num[a];
- entity_t* countdown = &g_entities[v];
- unsigned long units[TNAMES];
- char start_name[TNAMES][ZHLT3_MAX_VALUE];
- char base_change_target[TARGETS][ZHLT3_MAX_VALUE];
- char curkey[MAX_KEY];
- unsigned long total_units = 1;
- bool redundant[TNAMES];
- bool redundant_target[TARGETS];
- bool min_need = false;
-
- // Read count_units# and start_name# from 1 to 32
- for(int b = 0; b < max(TNAMES, TARGETS); b++) {
- if(b < TNAMES) {
- sprintf_s(curkey, "count_units%i", b + 1);
- units[b] = IntForKey(countdown, curkey);
- sprintf_s(curkey, "start_name%i", b + 1);
- strcpy_s(start_name[b], ValueForKey(countdown, curkey));
- if(units[b] < 1 || strlen(start_name[b]) == 0) {
- redundant[b] = true;
- } else {
- redundant[b] = false;
- min_need = true;
- if(units[b] > total_units)
- total_units = units[b];
- }
- }
- if(b < TARGETS) {
- sprintf_s(curkey, "base_change_target%i", b + 1);
- strcpy_s(base_change_target[b], ValueForKey(countdown, curkey));
- if(strlen(base_change_target[b]) == 0) {
- redundant_target[b] = true;
- } else redundant_target[b] = false;
- }
- }
-
- if(!min_need) {
- Warning("Zero targetnames specified in shlt_countdown. Skipping.");
- continue;
- }
-
- char separator_char = ValueForKey(countdown, "separator_char")[0];
-
- char dynamic_indicator[ZHLT3_MAX_VALUE];
- strcpy_s(dynamic_indicator, ValueForKey(countdown, "dynamic_indicator"));
-
- // Get positions of separator chars in dynamic_indicator
- int separators = 0;
- unsigned int separator_pos[TNAMES-1];
- unsigned int len = (int)strlen(dynamic_indicator);
- for(unsigned int c = 0; c < len; c++) {
- if(dynamic_indicator[c] == separator_char) {
- if(separators == TNAMES - 1)
- Error("More than %i unit bases/%i separator chars in shlt_countdown's time specs.", BASES, TNAMES - 1);
- separator_pos[separators] = c;
- separators++;
- }
- }
-
- if(separators % 2 == 0)
- Error("Even amount of separator chars (%i) in time specs do not match definition.", separators);
-
- unsigned long unit_max[BASES];
- unsigned long unit_digits[BASES];
- int unit_bases = 0;
-
- // Save the units' base sizes into unit_max[#]
- for(int d = 0; d < (separators+1) / 2; d++) {
- if(d) {
- if(separator_pos[2*d-1] + 1 == separator_pos[2*d])
- Error("Directly consecutive separator-char-pair in shlt_countdown's time specs.");
- unsigned int e;
- for(e = separator_pos[2*d-1]; e < separator_pos[2*d]; e++) {
- if(dynamic_indicator[e + 1] != separator_char)
- curkey[e - separator_pos[2*d-1]] = dynamic_indicator[e + 1];
- }
- curkey[e] = '\0';
- unit_max[d] = atoi(curkey);
- } else {
- // For the biggest unit base, this can be "infinite", thus
- // not using what we set this to later in the code. Just
- // make sure that this is > 0, because of the check below.
- unit_max[0] = 1;
- }
- if(unit_max[d] < 1)
- Error("Unit base #%i size of shlt_countdown < 1.", d + 1);
- unit_digits[d] = (int)strlen(curkey);
- unit_bases += 1;
- }
-
- if(unit_bases < 1)
- Error("No unit bases on shlt_countdown.");
-
- int unit_start_pos[BASES];
- int dynlen = (int)strlen(dynamic_indicator);
-
- // Remove the separator chars from dynamic_indicator
- // and maintain exact unit base positions.
- for(int f = 0; f < separators; f++) {
- int g = separator_pos[f] - f;
- for(int h = 0; h < dynlen - g; h++) {
- if(g + h + 1 == ZHLT3_MAX_VALUE)
- dynamic_indicator[g + h] = '\0';
- else dynamic_indicator[g + h] = dynamic_indicator[g + h + 1];
- }
- separator_pos[f] = g;
- dynlen--;
- }
-
- unit_start_pos[0] = separator_pos[0];
- for(int f = 1; f < separators; f+=2)
- unit_start_pos[f] = separator_pos[f*2-1];
-
- int interval_base = IntForKey(countdown, "interval_base");
- if(interval_base < 1) {
- Error("Interval base of shlt_countdown < 1.");
- } else if(interval_base > unit_bases) {
- Error("Interval base of shlt_countdown > bases.");
- }
- char finish_target[ZHLT3_MAX_VALUE];
- strcpy_s(finish_target, ValueForKey(countdown, "finish_target"));
-
- char finish_killtarget[ZHLT3_MAX_VALUE];
- strcpy_s(finish_killtarget, ValueForKey(countdown, "finish_killtarget"));
-
- float delay = FloatForKey(countdown, "delay");
-
- int activator_only = IntForKey(countdown, "activator_only");
- if(activator_only < 0 || activator_only > 2) {
- Warning("Activator_only setting in shlt_countdown entity out of bounds. Setting to default. (0)");
- activator_only = 0;
- }
-
- // Size in seconds of the biggest (left-hand) unit
- float biggest_unit_size = FloatForKey(countdown, "biggest_unit_size");
-
- bool count_upwards = BoolForKey(countdown, "count_upwards");
-
- char finish_message[ZHLT3_MAX_VALUE];
- strcpy_s(finish_message, ValueForKey(countdown, "finish_message"));
-
- char finish_message_time[128];
- strcpy_s(finish_message_time, ValueForKey(countdown, "finish_message_time"));
- if(atof(finish_message_time) < 0.0) {
- Warning("Finish message time < 0.0. Setting to 0.0.");
- strcpy_s(finish_message_time, "0.0");
- }
-
- char stop_message[ZHLT3_MAX_VALUE];
- strcpy_s(stop_message, ValueForKey(countdown, "stop_message"));
-
- char stop_message_time[128];
- strcpy_s(stop_message_time, ValueForKey(countdown, "stop_message_time"));
- if(atof(stop_message_time) < 0.0) {
- Warning("Stop message time < 0.0. Setting to 0.0.");
- strcpy_s(stop_message_time, "0.0");
- }
-
- // Read x1, x2, y1 and y2 and perform error checks
- char x1[5];
- strcpy_s(x1, ValueForKey(countdown, "x1"));
- if(atof(x1) < 0.0 && atof(x1) != -1.0) {
- Warning("X1 < 0.0 but not -1. Setting to -1.");
- strcpy_s(x1, "-1");
- } else if(atof(x1) > 1.0) {
- Warning("X1 > 1.0. Setting to 1.0.");
- strcpy_s(x1, "1.0");
- }
-
- char x2[5];
- strcpy_s(x2, ValueForKey(countdown, "x2"));
- if(atof(x2) < 0.0 && atof(x2) != -1.0) {
- Warning("X2 < 0.0 but not -1. Setting to -1.");
- strcpy_s(x2, "-1");
- } else if(atof(x2) > 1.0) {
- Warning("X2 > 1.0. Setting to 1.0.");
- strcpy_s(x2, "1.0");
- }
-
- if(atof(x1) == -1.0 && atof(x2) != -1.0) {
- Error("X1 is -1, while X2 is >= 0.0.");
- } else if(atof(x2) == -1.0 && atof(x1) != -1.0) {
- Error("X2 is -1, while X1 is >= 0.0.");
- }
-
- char y1[5];
- strcpy_s(y1, ValueForKey(countdown, "y1"));
- if(atof(y1) < 0.0 && atof(y1) != -1.0) {
- Warning("Y1 < 0.0 but not -1. Setting to -1.");
- strcpy_s(y1, "-1");
- } else if(atof(y1) > 1.0) {
- Warning("Y1 > 1.0. Setting to 1.0.");
- strcpy_s(y1, "1.0");
- }
-
- char y2[5];
- strcpy_s(y2, ValueForKey(countdown, "y2"));
- if(atof(y2) < 0.0 && atof(y2) != -1.0) {
- Warning("Y2 < 0.0 but not -1. Setting to -1.");
- strcpy_s(y2, "-1");
- } else if(atof(y2) > 1.0) {
- Warning("Y2 > 1.0. Setting to 1.0.");
- strcpy_s(y2, "1.0");
- }
-
- if(atof(y1) == -1.0 && atof(y2) != -1.0) {
- Error("Y1 is -1, while Y2 is >= 0.0.");
- } else if(atof(y2) == -1.0 && atof(y1) != -1.0) {
- Error("Y2 is -1, while Y1 is >= 0.0.");
- }
-
- // It will prove to be comfortable to have
- // these as both, string and float.
- float x1f = atof(x1);
- float x2f = atof(x2);
- float y1f = atof(y1);
- float y2f = atof(y2);
-
- float move_time = FloatForKey(countdown, "move_time");
- float move_delay = FloatForKey(countdown, "move_delay");
- float move_rdelay = FloatForKey(countdown, "move_rdelay");
-
- int countdown_color_from[3];
- GetIntVectorForKey(countdown, "countdown_color_from", countdown_color_from);
-
- int countdown_color_to[3];
- GetIntVectorForKey(countdown, "countdown_color_to", countdown_color_to);
-
- int finish_color[3];
- GetIntVectorForKey(countdown, "finish_color", finish_color);
-
- int stop_color[3];
- GetIntVectorForKey(countdown, "stop_color", stop_color);
-
- int channel = IntForKey(countdown, "channel");
- if(channel < 1) {
- Warning("Channel number < 1. Setting to 1.");
- channel = 1;
- } else if(channel > 4) {
- Warning("Channel number > 4. Setting to 4.");
- channel = 4;
- }
-
- unsigned long flash_amount = IntForKey(countdown, "flash_amount");
- unsigned long flash_amount2 = IntForKey(countdown, "flash_amount2");
- unsigned long flash_interval = IntForKey(countdown, "flash_interval");
- unsigned long flash_interval_base = IntForKey(countdown, "flash_interval_base");
-
- int flash_color[3];
- GetIntVectorForKey(countdown, "flash_color", flash_color);
-
- int flash_relativity = IntForKey(countdown, "flash_relativity");
- if(flash_relativity < 1) {
- Warning("Flash relativity option < 1. Setting to 1.");
- flash_relativity = 1;
- } else if(flash_relativity > 3) {
- Warning("Flash relativity option > 3. Setting to 1.");
- flash_relativity = 1;
- }
-
- bool stoptrigger_handling = BoolForKey(countdown, "stoptrigger_handling");
-
- float max_refresh_delay = FloatForKey(countdown, "max_refresh_delay");
- float min_refresh_delay = FloatForKey(countdown, "min_refresh_delay");
- float min_refresh_random = FloatForKey(countdown, "min_refresh_random");
-
- // We are going to pile all the new entities
- // in one spot, so read the origin as well.
- int origin[3];
- GetIntVectorForKey(countdown, "origin", origin);
-
- // We don't need you anymore!
- MarkEntityAsRedundant(countdown);
-
- // <- Done with the hacky reading and checking relevant keyvalues from entity
- // Do the actual work ->
-
- // Get the length in seconds one
- // unit of every base has.
- long double unit_time[BASES];
- for(int i = 0; i < unit_bases; i++) {
- unit_time[i] = biggest_unit_size;
- for(int j = 0; j < i; j++) {
- unit_time[i] /= unit_max[j];
- }
- }
-
- // Shift unit amounts over bases depending
- // on which is the interval base to match
- // scale of smallest (right-hand) base.
- interval_base = unit_bases - interval_base;
- flash_interval_base = unit_bases - flash_interval_base;
- for(int j = 0; j < unit_bases; j++) {
- if(!j) {
- for(int i = interval_base + 1; i < unit_bases; i++) {
- total_units *= unit_max[i];
- }
- }
- if(redundant[j])
- continue;
- for(int i = interval_base + 1; i < unit_bases; i++) {
- units[j] *= unit_max[i];
- }
- }
-
- // Calculate start values for the bases
- unsigned long base_cur_value[BASES];
- if(!count_upwards) {
- if(unit_bases == 1) {
- base_cur_value[0] = total_units;
- } else {
- long double factor = 1.0;
- for(int j = 2; j < unit_bases; j++) {
- factor *= unit_max[j];
- }
- long double this_units = (total_units / factor);
- unsigned long this_unitsf = (unsigned long)(this_units);
- base_cur_value[1] = this_unitsf % unit_max[1];
- base_cur_value[0] = (unsigned long)(this_units / unit_max[1]);
- long double multiply = this_units - this_unitsf;
- int i = 1;
- while(i < unit_bases - 1) {
- i++;
- if(i < unit_bases - 1) {
- base_cur_value[i] = (unsigned long)(multiply * unit_max[i]);
- multiply = multiply * unit_max[i] - base_cur_value[i];
- } else {
- base_cur_value[i] = ceil(multiply * unit_max[i]);
- }
- }
- }
- }
-
- Log("Highest start time: ");
- for(int i = 0; i < unit_bases; i++) {
- if(!i) {
- Log("%i", base_cur_value[i]);
- } else {
- char cp[ZHLT3_MAX_VALUE];
- itoa(unit_max[i] - 1, cp, 10);
- int len = (int)(strlen(cp));
- Log("%0*i", len, base_cur_value[i]);
- }
- if(i != unit_bases - 1) {
- Log(":");
- }
- }
- Log("\n");
-
- int mm_load = 0;
- unsigned long cdir = 0;
- if(!count_upwards)
- cdir = total_units;
-
- unsigned long total_units_cp = total_units;
- long double timeline = 0.0;
- bool processed = false;
-
- unsigned long offset = 0;
- if(flash_relativity == 3) {
- offset = total_units_cp % flash_interval;
- }
-
- // Calculate which game_texts will require a version
- // for the message flash effect (different color)
- bool* req_flashversion = (bool*) malloc(sizeof(bool) * total_units_cp);
- for(unsigned long i = total_units_cp; i > 0; i--) {
- if(((i + offset) % (flash_interval * unit_max[unit_bases - flash_interval_base])) == 0) {
- for(unsigned long j = 0; j < flash_amount2; j++) {
- if(flash_amount > 1) { // This is wrong and needs rewriting:
- for(unsigned long k = 0; k < flash_amount; k++) {
- if((req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base])) >= 0) {
- *(req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base])) = true;
- }
- }
- } else {
- for(unsigned long k = 0; k < flash_amount; k++) {
- 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) {
- *(req_flashversion + total_units_cp - i + (j * unit_max[unit_bases - flash_interval_base]) + (k * unit_max[unit_bases - flash_interval_base + 1])) = true;
- }
- }
- }
- }
- } else {
- *(req_flashversion + total_units_cp - i) = false;
- }
- }
-
- if(flash_relativity == 1) {
- *(req_flashversion + 0) = true;
- }
-
- // Determine and emit the game_texts which incarnate the
- // actual countdown. For this I use a counter for the
- // time (long double timeline) and a counter for the
- // smallest unit base (unsigned long total_units_cp)
- // simultaneously/parallel.
- while(!processed) {
- // Format cur_msg to be the current message to
- // be output by the current game_text entity.
- char cur_msg[ZHLT3_MAX_VALUE];
- strcpy_s(cur_msg, dynamic_indicator);
- int shifter = 0;
- for(int j = 0; j < unit_bases; j++) {
- char cur_val[ZHLT3_MAX_VALUE];
- if(j) {
- sprintf_s(cur_val, "%0*i", unit_digits, base_cur_value[j]);
- } else {
- itoa(base_cur_value[0], cur_val, 10);
- unit_digits[0] = (int)strlen(cur_val);
- }
- itoa(base_cur_value[j], cur_val, 10);
- strins(cur_msg, ZHLT3_MAX_VALUE - 1, cur_val, separator_pos[j] + shifter, false, ' ');
- shifter += unit_digits[j];
- }
-
- // Calculate screen position of message
- float newxf = x1f;
- float newyf = y1f;
- if(timeline >= move_delay) {
- if(timeline < (move_delay + move_time)) {
- if(x1f >= 0.0) {
- newxf = x1f + ((x2f - x1f) * ((timeline - move_delay) / move_time));
- }
- if(y1f >= 0.0) {
- newyf = y1f + ((y2f - y1f) * ((timeline - move_delay) / move_time));
- }
- } else {
- newxf = x2f;
- newyf = y2f;
- }
- timeline += move_rdelay;
- }
-
- // Calculate color blending
- int countdown_color_new[3];
- for(int cchannel = 0; cchannel < 3; cchannel++) {
- countdown_color_new[cchannel] = countdown_color_from[cchannel] +
- ((countdown_color_from[cchannel] - countdown_color_to[cchannel]) * (1.0 / (total_units_cp / total_units)));
- }
-
- entity_t* game_text = AddEntity("game_text", origin);
-
- // Count down by one unit - set boolean processed to
- // true when trying to lower base_cur_value[0] to -1.
- if(timeline / unit_time[unit_bases - 1] >= total_units_cp - total_units + 1) {
- for(int j = unit_bases - 1; j >= 0; j--) {
- if(base_cur_value[j] > 0) {
- base_cur_value[j]--;
- break;
- } else {
- if(!j) {
- processed = true;
- } else {
- base_cur_value[j] = unit_max[j] - 1;
- }
- }
- }
- }
- }
- Free(req_flashversion);
- }
- Log("Done.\n");
- }
- }
- */
- //Inserts one string into another for a length of maxNewLen chars at position
- //insAtPos. If overwrite is true, inserted String will overwrite source string
- //from position insPos onwards.
- 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);
- }
- }
- }
- }
- }
- //<-- SILENCER
- bool ParseEntity()
- {
- epair_t* e;
- entity_t* mapent;
- /* All entities parsed? */
- if (!GetToken(true))
- {
- //UnfoldCountdownPackages();
- DoWildcards();
- ParseBrushCopies();
-
- /* Changes to entity work? */
- if(num_brush_copies > 0 /* || num_countdowns > 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 // AJM
- if (!strcmp(ValueForKey(mapent, "classname"), "info_compile_parameters"))
- {
- Log("Map entity info_compile_parameters detected, using compile settings\n");
- GetParamsFromEnt(mapent);
- }
- #endif
-
- // Since the classnames of the following entity types are changed
- // when detected, the following if-checks can only return true in
- // the ParseEntities()-call from hlbsp, since hlbsp is the first
- // tool to call ParseEntities().
- //PROTECTOR -->
- if (!strcmp(ValueForKey(mapent, "classname"), "phlt_copy_brush"))
- {
- brush_copy_num[num_brush_copies] = g_numentities-1;
- num_brush_copies++;
- } //SILENCER -->
- /*else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_countdown"))
- {
- countdown_num[num_countdowns] = g_numentities-1;
- num_countdowns++;
- }*/
- else if(!strcmp(ValueForKey(mapent, "classname"), "shlt_entity_wildcard"))
- {
- wildcard_num[num_wildcards] = g_numentities-1;
- num_wildcards++;
- } //<-- SILENCER
- //<-- PROTECTOR
- return true;
- }
- // =====================================================================================
- // ParseEntities
- // Parses the dentdata string into entities
- // =====================================================================================
- void ParseEntities()
- {
- g_numentities = 0;
- ParseFromMemory(g_dentdata, g_entdatasize);
- while (ParseEntity())
- {
- }
- }
- // =====================================================================================
- // UnparseEntities
- // Generates the dentdata string from all the entities
- // =====================================================================================
- 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; // ent got removed
- }
- 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;
- }
- // SILENCER -->
- // =====================================================================================
- // MarkEntityAsRedundant
- // strips epairs from entity to provide for its removal in UnparseEntities()
- // =====================================================================================
- void MarkEntityAsRedundant(entity_t* ent) {
- ent->epairs = 0;
- // Do not lower g_numentities, since all we are doing with
- // this, is to take some entity anywhere out of the chain of
- // entities.
- }
- // =====================================================================================
- // AddEntity
- // adds an Entity at given origin
- // =====================================================================================
- 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];
- /* Origin-vector should be in ints - all entities **
- ** in *.map have int[3]-based origin */
- 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];
- }
- // =====================================================================================
- // RemoveKeyValue
- // removes a keyvalue from an entity
- // =====================================================================================
- 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;
- }
- }
- // <-- SILENCER
- // =====================================================================================
- // SetKeyValue
- // creates/changes a keyvalue
- // =====================================================================================
- 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;
- }
- }
- // If the key does not exist, create it and apply value
- ep = (epair_t*)Alloc(sizeof(*ep));
- ep->next = ent->epairs;
- ent->epairs = ep;
- ep->key = strdup(key);
- ep->value = strdup(value);
- }
- // =====================================================================================
- // ValueForKey
- // returns the value for a passed entity and key
- // =====================================================================================
- 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 "";
- }
- // =====================================================================================
- // IntForKey
- // =====================================================================================
- int IntForKey(const entity_t* const ent, const char* const key)
- {
- return atoi(ValueForKey(ent, key));
- }
- // =====================================================================================
- // FloatForKey
- // =====================================================================================
- vec_t FloatForKey(const entity_t* const ent, const char* const key)
- {
- return atof(ValueForKey(ent, key));
- }
- // SILENCER -->
- // =====================================================================================
- // BoolForKey
- // =====================================================================================
- bool BoolForKey(const entity_t* const ent, const char* const key)
- {
- return atoi(ValueForKey(ent, key)) != 0;
- }
- // <-- SILENCER
- // =====================================================================================
- // GetVectorForKey
- // returns value for key in vec[0-2]
- // =====================================================================================
- 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);
- // scanf into doubles, then assign, so it is vec_t size independent
- 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);
- // scanf into doubles, then assign, so it is vec_t size independent
- v1 = v2 = v3 = 0;
- sscanf(k, "%lf %lf %lf", &v1, &v2, &v3);
- vec[0] = (int)v1;
- vec[1] = (int)v2;
- vec[2] = (int)v3;
- }
- // =====================================================================================
- // FindTargetEntity
- //
- // =====================================================================================
- 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;
- }
- // =====================================================================================
- // GetTextureByNumber
- // Touchy function, can fail with a page fault if all the data isnt kosher
- // (i.e. map was compiled with missing textures)
- // =====================================================================================
- 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;
- }
- // =====================================================================================
- // EntityForModel
- // returns entity addy for given modelnum
- // =====================================================================================
- entity_t* EntityForModel(const int modnum)
- {
- int i;
- const char* s;
- char name[16];
- sprintf(name, "*%i", modnum);
- // search the entities for one using 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];
- }
|