Skip to content

Commit

Permalink
Now can open multiple maps
Browse files Browse the repository at this point in the history
Remove old code. Now use only filesystem.
Added Windows menu item. Now support multiple maps.
  • Loading branch information
Karaulov committed Nov 22, 2022
1 parent 2fd48a3 commit 015efc8
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 209 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ else()
set (CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
endif ()
add_definitions(-DUSE_FILESYSTEM)
set(CMAKE_CXX_FLAGS "-Wall -std=c++17")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-Os -fno-exceptions -w -Wfatal-errors")
Expand Down
55 changes: 32 additions & 23 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2703,7 +2703,7 @@ bool Bsp::validate()
isValid = false;
}
if (visDataLength > 0 &&
leaves[i].nVisOffset != (unsigned int)-1 && (leaves[i].nVisOffset < 0 || (unsigned int)leaves[i].nVisOffset >= visDataLength))
leaves[i].nVisOffset != -1 && (leaves[i].nVisOffset < 0 || leaves[i].nVisOffset >= visDataLength))
{
logf("Bad vis offset in leaf %d: %d / %d\n", i, leaves[i].nVisOffset, visDataLength);
isValid = false;
Expand Down Expand Up @@ -2734,12 +2734,12 @@ bool Bsp::validate()
}
for (unsigned int k = 0; k < 2; k++)
{
if (nodes[i].iChildren[k] != (unsigned int)-1 && nodes[i].iChildren[k] > 0 && (unsigned int)nodes[i].iChildren[k] >= nodeCount)
if (nodes[i].iChildren[k] != -1 && nodes[i].iChildren[k] > 0 && nodes[i].iChildren[k] >= nodeCount)
{
logf("Bad node reference in node %d child %d: %d / %d\n", i, k, nodes[i].iChildren[k], nodeCount);
isValid = false;
}
else if (~nodes[i].iChildren[k] != (unsigned int)-1 && nodes[i].iChildren[k] < 0 && (unsigned int)~nodes[i].iChildren[k] >= leafCount)
else if (~nodes[i].iChildren[k] != -1 && nodes[i].iChildren[k] < 0 && ~nodes[i].iChildren[k] >= leafCount)
{
logf("Bad leaf reference in ~node %d child %d: %d / %d\n", i, k, ~nodes[i].iChildren[k], leafCount);
isValid = false;
Expand All @@ -2748,14 +2748,14 @@ bool Bsp::validate()
}
for (unsigned int i = 0; i < clipnodeCount; i++)
{
if (clipnodes[i].iPlane < 0 || (unsigned int)clipnodes[i].iPlane >= planeCount)
if (clipnodes[i].iPlane < 0 || clipnodes[i].iPlane >= planeCount)
{
logf("Bad plane reference in clipnode %d: %d / %d\n", i, clipnodes[i].iPlane, planeCount);
isValid = false;
}
for (unsigned int k = 0; k < 2; k++)
{
if (clipnodes[i].iChildren[k] > 0 && (unsigned int)clipnodes[i].iChildren[k] >= clipnodeCount)
if (clipnodes[i].iChildren[k] > 0 && clipnodes[i].iChildren[k] >= clipnodeCount)
{
logf("Bad clipnode reference in clipnode %d child %d: %d / %d\n", i, k, clipnodes[i].iChildren[k], clipnodeCount);
isValid = false;
Expand All @@ -2764,7 +2764,7 @@ bool Bsp::validate()
}
for (unsigned int i = 0; i < ents.size(); i++)
{
if (ents[i]->getBspModelIdxForce() > 0 && (unsigned int)ents[i]->getBspModelIdxForce() >= modelCount)
if (ents[i]->getBspModelIdxForce() > 0 && ents[i]->getBspModelIdxForce() >= modelCount)
{
logf("Bad model reference in entity %d: %d / %d\n", i, ents[i]->getBspModelIdxForce(), modelCount);
isValid = false;
Expand Down Expand Up @@ -2891,7 +2891,7 @@ std::vector<STRUCTUSAGE*> Bsp::get_sorted_model_infos(int sortMode)

void Bsp::print_info(bool perModelStats, int perModelLimit, int sortMode)
{
size_t entCount = ents.size();
unsigned int entCount = (unsigned int)ents.size();

if (perModelStats)
{
Expand Down Expand Up @@ -2923,7 +2923,7 @@ void Bsp::print_info(bool perModelStats, int perModelLimit, int sortMode)
logf(" Classname Targetname Model %-10s Usage\n", countName);
logf("------------------------- ------------------------- ----- ---------- --------\n");

for (unsigned int i = 0; i < modelCount && i < (unsigned int)perModelLimit; i++)
for (int i = 0; i < modelCount && i < perModelLimit; i++)
{

int val = 0;
Expand Down Expand Up @@ -2959,7 +2959,7 @@ void Bsp::print_info(bool perModelStats, int perModelLimit, int sortMode)
print_stat("textures", textureCount, MAX_MAP_TEXTURES, false);
print_stat("lightdata", lightDataLength, MAX_MAP_LIGHTDATA, true);
print_stat("visdata", visDataLength, MAX_MAP_VISDATA, true);
print_stat("entities", (unsigned int)entCount, MAX_MAP_ENTS, false);
print_stat("entities", entCount, MAX_MAP_ENTS, false);
}
}

Expand Down Expand Up @@ -3392,7 +3392,7 @@ void Bsp::delete_hull(int hull_number, int redirect)

void Bsp::delete_hull(int hull_number, int modelIdx, int redirect)
{
if (modelIdx < 0 || (unsigned int)modelIdx >= modelCount)
if (modelIdx < 0 || modelIdx >= modelCount)
{
logf("Invalid model index %d. Must be 0-%d\n", modelIdx);
return;
Expand Down Expand Up @@ -4216,7 +4216,7 @@ int Bsp::create_clipnode_box(const vec3& mins, const vec3& maxs, BSPMODEL* targe

void Bsp::simplify_model_collision(int modelIdx, int hullIdx)
{
if (modelIdx < 0 || (unsigned int)modelIdx >= modelCount)
if (modelIdx < 0 || modelIdx >= modelCount)
{
logf("Invalid model index %d. Must be 0-%d\n", modelIdx);
return;
Expand Down Expand Up @@ -4308,14 +4308,16 @@ int Bsp::create_texinfo()
return texinfoCount - 1;
}

int Bsp::duplicate_model(int modelIdx)
void Bsp::copy_bsp_model(int modelIdx, STRUCTREMAP& remap, std::vector<BSPPLANE>& newPlanes, std::vector<vec3>& newVerts,
std::vector<BSPEDGE>& newEdges, std::vector<int>& newSurfedges, std::vector<BSPTEXTUREINFO>& newTexinfo,
std::vector<BSPFACE>& newFaces, std::vector<COLOR3>& newLightmaps, std::vector<BSPNODE>& newNodes,
std::vector<BSPCLIPNODE>& newClipnodes)
{
STRUCTUSAGE usage(this);
mark_model_structures(modelIdx, &usage, true);

STRUCTREMAP remap(this);
remap = STRUCTREMAP(this);

std::vector<BSPPLANE> newPlanes;
for (unsigned int i = 0; i < usage.count.planes; i++)
{
if (usage.planes[i])
Expand All @@ -4325,7 +4327,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<vec3> newVerts;
for (unsigned int i = 0; i < usage.count.verts; i++)
{
if (usage.verts[i])
Expand All @@ -4335,7 +4336,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<BSPEDGE> newEdges;
for (unsigned int i = 0; i < usage.count.edges; i++)
{
if (usage.edges[i])
Expand All @@ -4349,7 +4349,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<int> newSurfedges;
for (unsigned int i = 0; i < usage.count.surfEdges; i++)
{
if (usage.surfEdges[i])
Expand All @@ -4363,7 +4362,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<BSPTEXTUREINFO> newTexinfo;
for (unsigned int i = 0; i < usage.count.texInfos; i++)
{
if (usage.texInfo[i])
Expand All @@ -4373,8 +4371,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<BSPFACE> newFaces;
std::vector<COLOR3> newLightmaps;
int lightmapAppendSz = 0;
for (unsigned int i = 0; i < usage.count.faces; i++)
{
Expand Down Expand Up @@ -4410,7 +4406,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<BSPNODE> newNodes;
for (unsigned int i = 0; i < usage.count.nodes; i++)
{
if (usage.nodes[i])
Expand All @@ -4434,7 +4429,6 @@ int Bsp::duplicate_model(int modelIdx)
}
}

std::vector<BSPCLIPNODE> newClipnodes;
for (unsigned int i = 0; i < usage.count.clipnodes; i++)
{
if (usage.clipnodes[i])
Expand All @@ -4456,8 +4450,23 @@ int Bsp::duplicate_model(int modelIdx)
}
}
}
}

int Bsp::duplicate_model(int modelIdx)
{
std::vector<BSPPLANE> newPlanes;
std::vector<vec3> newVerts;
std::vector<BSPEDGE> newEdges;
std::vector<int> newSurfedges;
std::vector<BSPTEXTUREINFO> newTexinfo;
std::vector<BSPFACE> newFaces;
std::vector<COLOR3> newLightmaps;
std::vector<BSPNODE> newNodes;
std::vector<BSPCLIPNODE> newClipnodes;

STRUCTREMAP remap = STRUCTREMAP();

// MAYBE TODO: duplicate leaves(?) + marksurfs + recacl vis + update undo command lumps
copy_bsp_model(modelIdx, remap, newPlanes, newVerts, newEdges, newSurfedges, newTexinfo, newFaces, newLightmaps, newNodes, newClipnodes);

if (newClipnodes.size())
append_lump(LUMP_CLIPNODES, &newClipnodes[0], sizeof(BSPCLIPNODE) * newClipnodes.size());
Expand Down
6 changes: 6 additions & 0 deletions src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Bsp


bool is_model = false;
Bsp* parentMap = NULL;
void selectModelEnt();

int planeCount;
Expand Down Expand Up @@ -198,6 +199,11 @@ class Bsp
int create_model();
int create_texinfo();

void copy_bsp_model(int modelIdx, STRUCTREMAP& remap, std::vector<BSPPLANE>& newPlanes, std::vector<vec3>& newVerts,
std::vector<BSPEDGE>& newEdges, std::vector<int>& newSurfedges, std::vector<BSPTEXTUREINFO>& newTexinfo,
std::vector<BSPFACE>& newFaces, std::vector<COLOR3>& newLightmaps, std::vector<BSPNODE>& newNodes,
std::vector<BSPCLIPNODE>& newClipnodes);

int duplicate_model(int modelIdx);

// if the face's texinfo is not unique, a new one is created and returned. Otherwise, it's current texinfo is returned
Expand Down
2 changes: 1 addition & 1 deletion src/bsp/remap.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct STRUCTREMAP
bool* visitedFaces;

STRUCTCOUNT count; // size of each array

STRUCTREMAP() = default;
STRUCTREMAP(Bsp* map);
~STRUCTREMAP();
};
64 changes: 36 additions & 28 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ bool BspRenderer::refreshModelClipnodes(int modelIdx)
{
return false;
}
if (modelIdx < 0 || (unsigned int)modelIdx >= numRenderClipnodes)
if (modelIdx < 0 || modelIdx >= numRenderClipnodes)
{
logf("Bad model idx\n");
return false;
Expand Down Expand Up @@ -1515,7 +1515,7 @@ void BspRenderer::render(int highlightEnt, bool highlightAlwaysOnTop, int clipno
// draw highlighted ent first so other ent edges don't overlap the highlighted edges
if (highlightEnt > 0 && !highlightAlwaysOnTop)
{
if (renderEnts[highlightEnt].modelIdx >= 0 && (unsigned int)renderEnts[highlightEnt].modelIdx < map->modelCount)
if (renderEnts[highlightEnt].modelIdx >= 0 && renderEnts[highlightEnt].modelIdx < map->modelCount)
{
activeShader->pushMatrix(MAT_MODEL);
*activeShader->modelMat = renderEnts[highlightEnt].modelMat;
Expand All @@ -1537,7 +1537,7 @@ void BspRenderer::render(int highlightEnt, bool highlightAlwaysOnTop, int clipno

for (size_t i = 0, sz = map->ents.size(); i < sz; i++)
{
if (renderEnts[i].modelIdx >= 0 && (unsigned int)renderEnts[i].modelIdx < map->modelCount)
if (renderEnts[i].modelIdx >= 0 && renderEnts[i].modelIdx < map->modelCount)
{
activeShader->pushMatrix(MAT_MODEL);
*activeShader->modelMat = renderEnts[i].modelMat;
Expand Down Expand Up @@ -1570,7 +1570,7 @@ void BspRenderer::render(int highlightEnt, bool highlightAlwaysOnTop, int clipno
{
for (size_t i = 0, sz = map->ents.size(); i < sz; i++)
{
if (renderEnts[i].modelIdx >= 0 && (unsigned int)renderEnts[i].modelIdx < map->modelCount)
if (renderEnts[i].modelIdx >= 0 && renderEnts[i].modelIdx < map->modelCount)
{
if (clipnodeHull == -1 && renderModels[renderEnts[i].modelIdx].groupCount > 0)
{
Expand Down Expand Up @@ -1605,7 +1605,7 @@ void BspRenderer::render(int highlightEnt, bool highlightAlwaysOnTop, int clipno
{
activeShader->bind();

if (renderEnts[highlightEnt].modelIdx >= 0 && (unsigned int)renderEnts[highlightEnt].modelIdx < map->modelCount)
if (renderEnts[highlightEnt].modelIdx >= 0 && renderEnts[highlightEnt].modelIdx < map->modelCount)
{
activeShader->pushMatrix(MAT_MODEL);
*activeShader->modelMat = renderEnts[highlightEnt].modelMat;
Expand All @@ -1627,7 +1627,7 @@ void BspRenderer::render(int highlightEnt, bool highlightAlwaysOnTop, int clipno
void BspRenderer::drawModel(RenderEnt* ent, bool transparent, bool highlight, bool edgesOnly)
{
int modelIdx = ent ? ent->modelIdx : 0;
if (modelIdx < 0 || (unsigned int)modelIdx >= numRenderModels)
if (modelIdx < 0 || modelIdx >= numRenderModels)
{
return;
}
Expand Down Expand Up @@ -1827,7 +1827,7 @@ void BspRenderer::drawPointEntities(int highlightEnt)
colorShader->popMatrix(MAT_MODEL);
}

bool BspRenderer::pickPoly(vec3 start, const vec3& dir, int hullIdx, PickInfo& pickInfo)
bool BspRenderer::pickPoly(vec3 start, const vec3& dir, int hullIdx, PickInfo& tempPickInfo)
{
bool foundBetterPick = false;

Expand All @@ -1838,21 +1838,23 @@ bool BspRenderer::pickPoly(vec3 start, const vec3& dir, int hullIdx, PickInfo& p
start -= mapOffset;


if (pickModelPoly(start, dir, vec3(0, 0, 0), 0, hullIdx, pickInfo))
if (pickModelPoly(start, dir, vec3(0, 0, 0), 0, hullIdx, tempPickInfo))
{
pickInfo.entIdx = 0;
pickInfo.modelIdx = 0;
pickInfo.map = map;
pickInfo.ent = map->ents[0];
foundBetterPick = true;
if (tempPickInfo.map || (tempPickInfo.map && map && tempPickInfo.map == map))
{
tempPickInfo.entIdx = 0;
tempPickInfo.modelIdx = 0;
tempPickInfo.map = map;
tempPickInfo.ent = map->ents[0];
foundBetterPick = true;
}
}
int sz = (int)map->ents.size();

for (int i = 0; i < sz; i++)
{
if (renderEnts[i].modelIdx >= 0 && (unsigned int)renderEnts[i].modelIdx < map->modelCount)
if (renderEnts[i].modelIdx >= 0 && renderEnts[i].modelIdx < map->modelCount)
{

bool isSpecial = false;
for (int k = 0; k < renderModels[renderEnts[i].modelIdx].groupCount; k++)
{
Expand All @@ -1872,27 +1874,33 @@ bool BspRenderer::pickPoly(vec3 start, const vec3& dir, int hullIdx, PickInfo& p
continue;
}

if (pickModelPoly(start, dir, renderEnts[i].offset, renderEnts[i].modelIdx, hullIdx, pickInfo))
if (pickModelPoly(start, dir, renderEnts[i].offset, renderEnts[i].modelIdx, hullIdx, tempPickInfo))
{
pickInfo.entIdx = i;
pickInfo.modelIdx = renderEnts[i].modelIdx;
pickInfo.map = map;
pickInfo.ent = map->ents[i];
foundBetterPick = true;
if (!tempPickInfo.map || (tempPickInfo.map && map && tempPickInfo.map == map))
{
tempPickInfo.entIdx = i;
tempPickInfo.modelIdx = renderEnts[i].modelIdx;
tempPickInfo.map = map;
tempPickInfo.ent = map->ents[i];
foundBetterPick = true;
}
}
}
else if (i > 0 && g_render_flags & RENDER_POINT_ENTS)
{
vec3 mins = renderEnts[i].offset + renderEnts[i].pointEntCube->mins;
vec3 maxs = renderEnts[i].offset + renderEnts[i].pointEntCube->maxs;
if (pickAABB(start, dir, mins, maxs, pickInfo.bestDist))
if (pickAABB(start, dir, mins, maxs, tempPickInfo.bestDist))
{
pickInfo.entIdx = i;
pickInfo.modelIdx = -1;
pickInfo.faceIdx = -1;
pickInfo.map = map;
pickInfo.ent = map->ents[i];
foundBetterPick = true;
if (!tempPickInfo.map || (tempPickInfo.map && map && tempPickInfo.map == map))
{
tempPickInfo.entIdx = i;
tempPickInfo.modelIdx = -1;
tempPickInfo.faceIdx = -1;
tempPickInfo.map = map;
tempPickInfo.ent = map->ents[i];
foundBetterPick = true;
}
};
}
}
Expand Down
Loading

0 comments on commit 015efc8

Please sign in to comment.