Skip to content

Commit

Permalink
Fixed light style merging. Added option -nostyles.
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Mar 18, 2024
1 parent ce5f2b0 commit 296fafd
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 102 deletions.
208 changes: 113 additions & 95 deletions src/bsp/BspMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,124 @@
#include "vis.h"


Bsp* BspMerger::merge(std::vector<Bsp*> maps, const vec3& gap, const std::string& output_name, bool noripent, bool noscript)
Bsp* BspMerger::merge(std::vector<Bsp*> maps, const vec3& gap, const std::string& output_name, bool noripent, bool noscript, bool nomergestyles)
{
if (maps.size() < 1)
if (maps.size() < 2)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0219));
return NULL;
}

skipLightStyles = nomergestyles;


if (!skipLightStyles)
{
const int start_toggle_lightstyle = 32;
std::set<int> usage_lightstyles;
std::map<unsigned char, unsigned char> remap_light_styles;

for (int i = 0; i < maps[0]->faceCount; i++)
{
for (int s = 0; s < MAX_LIGHTMAPS; s++)
{
unsigned char style = maps[0]->faces[i].nStyles[s];

if (style < 255 &&
style >= start_toggle_lightstyle &&
!usage_lightstyles.count(style))
{
usage_lightstyles.insert(style);
}
}

g_progress.tick();
}

for (size_t b = 1; b < maps.size(); b++)
{
Bsp* mapB = maps[b];


mapB->save_undo_lightmaps();

g_progress.update("Merging lightstyles", (int)(maps[0]->faceCount + maps[b]->faceCount));


int remapped_lightstyles = 0;
int remapped_lightfaces = 0;

for (int i = 0; i < mapB->faceCount; i++)
{
for (int s = 0; s < MAX_LIGHTMAPS; s++)
{
unsigned char style = mapB->faces[i].nStyles[s];

if (style < 255 &&
style >= start_toggle_lightstyle &&
usage_lightstyles.count(style))
{
if (remap_light_styles.find(style) != remap_light_styles.end())
{/*
print_log(PRINT_GREEN,"REMAP {} TO {}\n", mapB.faces[i].nStyles[s], remap_light_styles[style]);*/
mapB->faces[i].nStyles[s] = remap_light_styles[style];
remapped_lightfaces++;
continue;
}

remapped_lightstyles++;
unsigned char newstyle = 32;
for (; newstyle < 254; newstyle++)
{
if (!usage_lightstyles.count(newstyle)
&& remap_light_styles.find(style) == remap_light_styles.end())
{
break;
}
}

remap_light_styles[style] = newstyle;


//print_log(PRINT_GREEN, "REMAP2 {} TO {}\n", mapB.faces[i].nStyles[s], remap_light_styles[style]);
mapB->faces[i].nStyles[s] = newstyle;


usage_lightstyles.insert(newstyle);
}
}

g_progress.tick();
}

int remapped_lightents = 0;

for (size_t i = 0; i < mapB->ents.size(); i++)
{
if (mapB->ents[i]->keyvalues["classname"].find("light") != std::string::npos)
{
if (mapB->ents[i]->hasKey("style"))
{
int style = str_to_int(mapB->ents[i]->keyvalues["style"]);
if (style < 255 && style >= start_toggle_lightstyle && remap_light_styles.find((unsigned char)style) != remap_light_styles.end())
{
remapped_lightents++;
mapB->ents[i]->setOrAddKeyvalue("style", std::to_string(remap_light_styles[style]));
}
}
}
}

print_log(PRINT_BLUE, "MapA used {} light styles. MapB used {} light styles!\n", usage_lightstyles.size(), remap_light_styles.size());
print_log(PRINT_BLUE, "Remapped {} light styles in {} entities and {} faces!\n", remapped_lightstyles, remapped_lightents, remapped_lightfaces);

mapB->resize_all_lightmaps();

remap_light_styles.clear();
}
}


std::vector<std::vector<std::vector<MAPBLOCK>>> blocks = separate(maps, gap);


Expand Down Expand Up @@ -1065,99 +1176,6 @@ void BspMerger::merge_ents(Bsp& mapA, Bsp& mapB)
g_progress.tick();
}


const int start_toggle_lightstyle = 32;

g_progress.update("Merging lightstyles", (int)(mapA.faceCount + mapB.faceCount));

mapB.save_undo_lightmaps();

std::set<int> usage_lightstyles;
std::map<unsigned char, unsigned char> remap_light_styles;

for (int i = 0; i < mapA.faceCount; i++)
{
for (int s = 0; s < MAX_LIGHTMAPS; s++)
{
unsigned char style = mapA.faces[i].nStyles[s];

if (style < 255 &&
style >= start_toggle_lightstyle &&
!usage_lightstyles.count(style))
{
usage_lightstyles.insert(style);
}
}

g_progress.tick();
}

int remapped_lightstyles = 0;
int remapped_lightfaces = 0;

for (int i = 0; i < mapB.faceCount; i++)
{
for (int s = 0; s < MAX_LIGHTMAPS; s++)
{
unsigned char style = mapB.faces[i].nStyles[s];

if (style < 255 &&
style >= start_toggle_lightstyle &&
usage_lightstyles.count(style))
{
if (remap_light_styles.find(style) != remap_light_styles.end())
{/*
print_log(PRINT_GREEN,"REMAP {} TO {}\n", mapB.faces[i].nStyles[s], remap_light_styles[style]);*/
mapB.faces[i].nStyles[s] = remap_light_styles[style];
remapped_lightfaces++;
continue;
}

remapped_lightstyles++;
unsigned char newstyle = 32;
for (; newstyle < 254; newstyle++)
{
if (!usage_lightstyles.count(newstyle)
&& remap_light_styles.find(style) == remap_light_styles.end())
{
break;
}
}

remap_light_styles[style] = newstyle;


//print_log(PRINT_GREEN, "REMAP2 {} TO {}\n", mapB.faces[i].nStyles[s], remap_light_styles[style]);
mapB.faces[i].nStyles[s] = newstyle;
}
}

g_progress.tick();
}

int remapped_lightents = 0;

for (size_t i = 0; i < mapB.ents.size(); i++)
{
if (mapB.ents[i]->keyvalues["classname"].find("light") != std::string::npos)
{
if (mapB.ents[i]->hasKey("style"))
{
int style = str_to_int(mapB.ents[i]->keyvalues["style"]);
if (style < 255 && style >= start_toggle_lightstyle && remap_light_styles.find((unsigned char)style) != remap_light_styles.end())
{
remapped_lightents++;
mapB.ents[i]->setOrAddKeyvalue("style", std::to_string(remap_light_styles[style]));
}
}
}
}

print_log(PRINT_BLUE, "MapA used {} light styles. MapB used {} light styles!\n", usage_lightstyles.size(), remap_light_styles.size());
print_log(PRINT_BLUE, "Remapped {} light styles in {} entities and {} faces!\n", remapped_lightstyles, remapped_lightents, remapped_lightfaces);

mapB.resize_all_lightmaps();

for (size_t i = 0; i < mapB.ents.size(); i++)
{
if (mapB.ents[i]->keyvalues["classname"] == "worldspawn")
Expand Down
4 changes: 3 additions & 1 deletion src/bsp/BspMerger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class BspMerger
// merges all maps into one
// noripent - don't change any entity logic
// noscript - don't add support for the bspguy map script (worse performance + buggy, but simpler)
Bsp* merge(std::vector<Bsp*> maps, const vec3& gap, const std::string& output_name, bool noripent, bool noscript);
Bsp* merge(std::vector<Bsp*> maps, const vec3& gap, const std::string& output_name, bool noripent, bool noscript, bool nomergestyles);


// wrapper around BSP data merging for nicer console output
Expand Down Expand Up @@ -89,4 +89,6 @@ class BspMerger
int thisMarkSurfCount = 0;
int thisEdgeCount = 0;
int thisVertCount = 0;

bool skipLightStyles = false;
};
4 changes: 3 additions & 1 deletion src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9003,6 +9003,7 @@ void Gui::drawMergeWindow()
static bool Optimize = false;
static bool DeleteHull2 = false;
static bool NoRipent = false;
static bool NoStyles = false;
static bool NoScript = true;

bool addNew = false;
Expand Down Expand Up @@ -9055,6 +9056,7 @@ void Gui::drawMergeWindow()
ImGui::Checkbox(get_localized_string(LANG_0830).c_str(), &DeleteHull2);
ImGui::Checkbox(get_localized_string(LANG_0831).c_str(), &NoRipent);
ImGui::Checkbox(get_localized_string(LANG_0832).c_str(), &NoScript);
ImGui::Checkbox("Skip lightstyles merging", &NoStyles);

if (ImGui::Button(get_localized_string(LANG_1122).c_str(), ImVec2(120, 0)))
{
Expand Down Expand Up @@ -9117,7 +9119,7 @@ void Gui::drawMergeWindow()
print_log("\n");
}
BspMerger merger;
Bsp* result = merger.merge(maps, vec3(), outPath, NoRipent, NoScript);
Bsp* result = merger.merge(maps, vec3(), outPath, NoRipent, NoScript, NoStyles);

print_log("\n");
if (result->isValid()) result->write(outPath);
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int test()
removed.print_delete_stats(1);

BspMerger merger;
Bsp* result = merger.merge(maps, vec3(1, 1, 1), "yabma_move", false, false);
Bsp* result = merger.merge(maps, vec3(1, 1, 1), "yabma_move", false, false,false);
print_log("\n");
if (result)
{
Expand Down Expand Up @@ -237,7 +237,7 @@ int merge_maps(CommandLine& cli)
std::string output_name = cli.hasOption("-o") ? cli.getOption("-o") : cli.bspfile;

BspMerger merger;
Bsp* result = merger.merge(maps, gap, output_name, cli.hasOption("-noripent"), cli.hasOption("-noscript"));
Bsp* result = merger.merge(maps, gap, output_name, cli.hasOption("-noripent"), cli.hasOption("-noscript"), cli.hasOption("-nostyles"));

print_log("\n");
if (result->validate() && result->isValid()) result->write(output_name);
Expand Down
3 changes: 0 additions & 3 deletions src/qtools/rad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ float CalculatePointVecsProduct(const volatile float* point, const volatile floa
return (float)val;
}




//
//bool GetFaceExtentsX(Bsp* bsp, int facenum, int mins_out[2], int maxs_out[2])
//{
Expand Down

0 comments on commit 296fafd

Please sign in to comment.