Change MightSee to a bool array

This commit is contained in:
Jarrod Doyle 2025-01-27 18:09:11 +00:00
parent 54655254b9
commit 13929e44a6
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 17 additions and 7 deletions

View File

@ -12,9 +12,9 @@ public class PotentiallyVisibleSet
public readonly List<int> EdgeIndices = edgeIndices; public readonly List<int> EdgeIndices = edgeIndices;
} }
private readonly struct Edge(int destination, Poly poly) private readonly struct Edge(int mightSeeLength, int destination, Poly poly)
{ {
public readonly HashSet<int> MightSee = []; public readonly bool[] MightSee = new bool[mightSeeLength];
public readonly int Destination = destination; public readonly int Destination = destination;
public readonly Poly Poly = poly; public readonly Poly Poly = poly;
@ -78,6 +78,12 @@ public class PotentiallyVisibleSet
_graph = new Node[cells.Length]; _graph = new Node[cells.Length];
_edges = []; _edges = [];
var portalCount = 0;
foreach (var cell in cells)
{
portalCount += cell.PortalPolyCount;
}
for (var i = 0; i < cells.Length; i++) for (var i = 0; i < cells.Length; i++)
{ {
var cell = cells[i]; var cell = cells[i];
@ -111,7 +117,7 @@ public class PotentiallyVisibleSet
vs.Add(cell.Vertices[cell.Indices[indicesOffset + vIdx]]); vs.Add(cell.Vertices[cell.Indices[indicesOffset + vIdx]]);
} }
var edge = new Edge(poly.Destination, new Poly(vs, cell.Planes[poly.PlaneId])); var edge = new Edge(portalCount, poly.Destination, new Poly(vs, cell.Planes[poly.PlaneId]));
edgeIndices.Add(_edges.Count); edgeIndices.Add(_edges.Count);
_edges.Add(edge); _edges.Add(edge);
indicesOffset += poly.VertexCount; indicesOffset += poly.VertexCount;
@ -148,12 +154,13 @@ public class PotentiallyVisibleSet
while (unexploredCells.Count > 0) while (unexploredCells.Count > 0)
{ {
var cellIdx = unexploredCells.Pop(); var cellIdx = unexploredCells.Pop();
if (source.MightSee[cellIdx])
if (!source.MightSee.Add(cellIdx))
{ {
continue; // target is already explored continue; // target is already explored
} }
source.MightSee[cellIdx] = true;
// Target must be partly behind source, source must be partly in front of target, and source and target cannot face each other // Target must be partly behind source, source must be partly in front of target, and source and target cannot face each other
foreach (var targetEdgeIdx in _graph[cellIdx].EdgeIndices) foreach (var targetEdgeIdx in _graph[cellIdx].EdgeIndices)
{ {
@ -212,9 +219,12 @@ public class PotentiallyVisibleSet
foreach (var edgeIdx in _graph[cellIdx].EdgeIndices) foreach (var edgeIdx in _graph[cellIdx].EdgeIndices)
{ {
var edge = _edges[edgeIdx]; var edge = _edges[edgeIdx];
foreach (var mightSee in edge.MightSee) for (var i = 0; i < edge.MightSee.Length; i++)
{ {
visible.Add(mightSee); if (edge.MightSee[i])
{
visible.Add(i);
}
} }
} }