Skip to content

Commit

Permalink
Small update for fix loading
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Dec 13, 2024
1 parent 01e35f6 commit 042a53c
Show file tree
Hide file tree
Showing 25 changed files with 366 additions and 509 deletions.
558 changes: 254 additions & 304 deletions src/bsp/Bsp.cpp

Large diffs are not rendered by default.

17 changes: 3 additions & 14 deletions src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ class BspRenderer;
#define OOB_CLIP_Z 16
#define OOB_CLIP_Z_NEG 32

struct membuf : std::streambuf
{
membuf(char* begin, int len)
{
this->setg(begin, begin, begin + len);
}
};

struct LeafDebug
{
int leafIdx;
Expand All @@ -50,8 +42,8 @@ class Bsp
BSPHEADER bsp_header;
BSPHEADER_EX bsp_header_ex;

unsigned char** lumps;
unsigned char** extralumps;
std::vector<std::vector<unsigned char>> lumps;
std::vector<std::vector<unsigned char>> extralumps;

std::vector<LIGHTMAP> undo_lightmaps;

Expand Down Expand Up @@ -111,9 +103,6 @@ class Bsp

std::string bsp_path;
std::string bsp_name;

bool replacedLump[32];

vec3 save_cam_pos, save_cam_angles;

bool bsp_valid;
Expand Down Expand Up @@ -413,7 +402,7 @@ class Bsp
bool does_model_use_shared_structures(int modelIdx);

// returns the current lump contents
LumpState duplicate_lumps(int targets);
LumpState duplicate_lumps(unsigned int targets);

void replace_lumps(const LumpState& state);

Expand Down
44 changes: 28 additions & 16 deletions src/bsp/BspMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ void BspMerger::update_map_series_entity_logic(Bsp* mergedMap, std::vector<MAPBL
if (!noscript)
{
std::ofstream entFile(output_name + ".ent", std::ios::trunc);
entFile.write((const char*)mergedMap->lumps[LUMP_ENTITIES], mergedMap->bsp_header.lump[LUMP_ENTITIES].nLength - 1);
entFile.write((const char*)mergedMap->lumps[LUMP_ENTITIES].data(), mergedMap->bsp_header.lump[LUMP_ENTITIES].nLength - 1);
}
}

Expand Down Expand Up @@ -1082,19 +1082,20 @@ bool BspMerger::merge(Bsp& mapA, Bsp& mapB, bool modelMerge)
continue; // always merge
}

if (!mapA.lumps[i] && !mapB.lumps[i])
if (!mapA.lumps[i].size() && !mapB.lumps[i].size())
{

}
else if (!mapA.lumps[i] && mapB.lumps[i])
else if (!mapA.lumps[i].size() && mapB.lumps[i].size())
{
if (!modelMerge)
{
print_log(get_localized_string(LANG_0237), g_lump_names[i]);

mapA.bsp_header.lump[i].nLength = mapB.bsp_header.lump[i].nLength;
mapA.lumps[i] = new unsigned char[mapB.bsp_header.lump[i].nLength];
memcpy(mapA.lumps[i], mapB.lumps[i], mapB.bsp_header.lump[i].nLength);
mapA.lumps[i] = mapB.lumps[i];

mapA.update_lump_pointers();
// process the lump here (TODO: faster to just copy wtv needs copying)
switch (i)
{
Expand All @@ -1103,7 +1104,7 @@ bool BspMerger::merge(Bsp& mapA, Bsp& mapB, bool modelMerge)
}
}
}
else if (!mapB.lumps[i])
else if (!mapB.lumps[i].size())
{
print_log(get_localized_string(LANG_0238), g_lump_names[i]);
}
Expand Down Expand Up @@ -1324,6 +1325,7 @@ void BspMerger::merge_planes(Bsp& mapA, Bsp& mapB)
memcpy(newPlanes, &mergedPlanes[0], newLen);

mapA.replace_lump(LUMP_PLANES, newPlanes, newLen);
delete[] newPlanes;
}

void BspMerger::merge_textures(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1413,16 +1415,14 @@ void BspMerger::merge_textures(Bsp& mapA, Bsp& mapB)

unsigned int texHeaderSize = (unsigned int)((newTexCount + 1) * sizeof(int));
unsigned int newLen = (unsigned int)((mipTexWritePtr - newMipTexData) + texHeaderSize);
unsigned char* newTextureData = new unsigned char[newLen];
unsigned char* newTextureData = new unsigned char[newLen + sizeof(int)];

// write texture lump header
unsigned int* texHeader = (unsigned int*)(newTextureData);
texHeader[0] = newTexCount;
for (unsigned int i = 0; i < newTexCount; i++)
{
if (i + 1 < newTexCount + 1) {
texHeader[i + 1] = (mipTexOffsets[i] == -1) ? -1 : mipTexOffsets[i] + texHeaderSize;
}
texHeader[i + 1] = (mipTexOffsets[i] == -1) ? -1 : mipTexOffsets[i] + texHeaderSize;
}

memcpy(newTextureData + texHeaderSize, newMipTexData, mipTexWritePtr - newMipTexData);
Expand All @@ -1433,6 +1433,7 @@ void BspMerger::merge_textures(Bsp& mapA, Bsp& mapB)
print_log("\n");

mapA.replace_lump(LUMP_TEXTURES, newTextureData, newLen);
delete[] newTextureData;
delete[] newMipTexData;
}

Expand All @@ -1451,6 +1452,7 @@ void BspMerger::merge_vertices(Bsp& mapA, Bsp& mapB)
g_progress.tick();

mapA.replace_lump(LUMP_VERTICES, newVerts, totalVertCount * sizeof(vec3));
delete[] newVerts;
}

void BspMerger::merge_texinfo(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1505,6 +1507,7 @@ void BspMerger::merge_texinfo(Bsp& mapA, Bsp& mapB)
print_log("\n");

mapA.replace_lump(LUMP_TEXINFO, newTexinfoData, newLen);
delete[] newTexinfoData;
}

void BspMerger::merge_faces(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1568,14 +1571,15 @@ void BspMerger::merge_faces(Bsp& mapA, Bsp& mapB)
}

mapA.replace_lump(LUMP_FACES, newFaces, totalFaceCount * sizeof(BSPFACE32));
delete[] newFaces;
}

void BspMerger::merge_leaves(Bsp& mapA, Bsp& mapB)
{
thisLeafCount = mapA.bsp_header.lump[LUMP_LEAVES].nLength / sizeof(BSPLEAF32);
otherLeafCount = mapB.bsp_header.lump[LUMP_LEAVES].nLength / sizeof(BSPLEAF32);

int tthisWorldLeafCount = ((BSPMODEL*)mapA.lumps[LUMP_MODELS])->nVisLeafs + 1; // include solid leaf
int tthisWorldLeafCount = ((BSPMODEL*)mapA.lumps[LUMP_MODELS].data())->nVisLeafs + 1; // include solid leaf

g_progress.update("Merging leaves", thisLeafCount + otherLeafCount);

Expand Down Expand Up @@ -1628,6 +1632,7 @@ void BspMerger::merge_leaves(Bsp& mapA, Bsp& mapB)
memcpy(newLeavesData, &mergedLeaves[0], newLen);

mapA.replace_lump(LUMP_LEAVES, newLeavesData, newLen);
delete[] newLeavesData;
}

void BspMerger::merge_marksurfs(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1665,6 +1670,7 @@ void BspMerger::merge_marksurfs(Bsp& mapA, Bsp& mapB)
}

mapA.replace_lump(LUMP_MARKSURFACES, newSurfs, totalSurfCount * sizeof(int));
delete[] newSurfs;
}

void BspMerger::merge_edges(Bsp& mapA, Bsp& mapB)
Expand All @@ -1688,6 +1694,7 @@ void BspMerger::merge_edges(Bsp& mapA, Bsp& mapB)
}

mapA.replace_lump(LUMP_EDGES, newEdges, totalEdgeCount * sizeof(BSPEDGE32));
delete[] newEdges;
}

void BspMerger::merge_surfedges(Bsp& mapA, Bsp& mapB)
Expand All @@ -1710,6 +1717,7 @@ void BspMerger::merge_surfedges(Bsp& mapA, Bsp& mapB)
}

mapA.replace_lump(LUMP_SURFEDGES, newSurfs, totalSurfCount * sizeof(int));
delete[] newSurfs;
}

void BspMerger::merge_nodes(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1779,6 +1787,7 @@ void BspMerger::merge_nodes(Bsp& mapA, Bsp& mapB)
memcpy(newNodeData, &mergedNodes[0], newLen);

mapA.replace_lump(LUMP_NODES, newNodeData, newLen);
delete[] newNodeData;
}

void BspMerger::merge_clipnodes(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1829,6 +1838,7 @@ void BspMerger::merge_clipnodes(Bsp& mapA, Bsp& mapB)
memcpy(newClipnodeData, &mergedNodes[0], newLen);

mapA.replace_lump(LUMP_CLIPNODES, newClipnodeData, newLen);
delete[] newClipnodeData;
}

void BspMerger::merge_models(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1897,6 +1907,7 @@ void BspMerger::merge_models(Bsp& mapA, Bsp& mapB)
memcpy(newModelData, &mergedModels[0], newLen);

mapA.replace_lump(LUMP_MODELS, newModelData, newLen);
delete[] newModelData;
}

void BspMerger::merge_vis(Bsp& mapA, Bsp& mapB)
Expand Down Expand Up @@ -1966,6 +1977,7 @@ void BspMerger::merge_vis(Bsp& mapA, Bsp& mapB)
print_log(get_localized_string(LANG_0244), oldLen, newVisLen);
print_log("\n");

delete[] compressedVisResize;
delete[] decompressedVis;
delete[] compressedVis;
}
Expand All @@ -1989,9 +2001,9 @@ void BspMerger::merge_lighting(Bsp& mapA, Bsp& mapB)
thisColorCount = g_limits.maxSurfaceExtent * g_limits.maxSurfaceExtent;
totalColorCount += thisColorCount;
int sz = thisColorCount * sizeof(COLOR3);
mapA.lumps[LUMP_LIGHTING] = new unsigned char[sz];
mapA.lumps[LUMP_LIGHTING].resize(sz);
mapA.bsp_header.lump[LUMP_LIGHTING].nLength = sz;
thisRad = (COLOR3*)mapA.lumps[LUMP_LIGHTING];
thisRad = (COLOR3*)mapA.lumps[LUMP_LIGHTING].data();

memset(thisRad, 255, sz);

Expand Down Expand Up @@ -2030,7 +2042,7 @@ void BspMerger::merge_lighting(Bsp& mapA, Bsp& mapB)

g_progress.tick();
mapA.replace_lump(LUMP_LIGHTING, newRad, totalColorCount * sizeof(COLOR3));

delete[] newRad;
for (int i = thisWorldFaceCount; i < thisWorldFaceCount + otherFaceCount; i++)
{
if (mapA.faces[i].nLightmapOffset >= 0)
Expand Down Expand Up @@ -2067,7 +2079,7 @@ void BspMerger::create_merge_headnodes(Bsp& mapA, Bsp& mapB, BSPPLANE separation
memcpy(newThisPlanes, mapA.planes, mapA.planeCount * sizeof(BSPPLANE));
newThisPlanes[mapA.planeCount] = separationPlane;
mapA.replace_lump(LUMP_PLANES, newThisPlanes, (mapA.planeCount + 1) * sizeof(BSPPLANE));

delete[] newThisPlanes;
int separationPlaneIdx = mapA.planeCount - 1;


Expand All @@ -2094,6 +2106,7 @@ void BspMerger::create_merge_headnodes(Bsp& mapA, Bsp& mapB, BSPPLANE separation
newThisNodes[0] = headNode;

mapA.replace_lump(LUMP_NODES, newThisNodes, (mapA.nodeCount + 1) * sizeof(BSPNODE32));
delete[] newThisNodes;
}


Expand Down Expand Up @@ -2130,7 +2143,6 @@ void BspMerger::create_merge_headnodes(Bsp& mapA, Bsp& mapB, BSPPLANE separation

std::vector<BSPCLIPNODE32> newThisClipNodes(newHeadNodes.begin(), newHeadNodes.end());
newThisClipNodes.insert(newThisClipNodes.end(), mapA.clipnodes, mapA.clipnodes + mapA.clipnodeCount);

mapA.replace_lump(LUMP_CLIPNODES, newThisClipNodes.data(), newThisClipNodes.size() * sizeof(BSPCLIPNODE32));
}
}
2 changes: 1 addition & 1 deletion src/bsp/remap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ STRUCTCOUNT::STRUCTCOUNT(Bsp* map)
clipnodes = map->bsp_header.lump[LUMP_CLIPNODES].nLength / sizeof(BSPCLIPNODE32);
verts = map->bsp_header.lump[LUMP_VERTICES].nLength / sizeof(vec3);
faces = map->bsp_header.lump[LUMP_FACES].nLength / sizeof(BSPFACE32);
textures = *((int*)(map->lumps[LUMP_TEXTURES]));
textures = *((int*)(map->lumps[LUMP_TEXTURES].data()));
markSurfs = map->bsp_header.lump[LUMP_MARKSURFACES].nLength / sizeof(int);
surfEdges = map->bsp_header.lump[LUMP_SURFEDGES].nLength / sizeof(int);
edges = map->bsp_header.lump[LUMP_EDGES].nLength / sizeof(BSPEDGE32);
Expand Down
Loading

0 comments on commit 042a53c

Please sign in to comment.