Fixed linedefs sometimes not being split in certain situations. (Backported from UDB)

This commit is contained in:
spherallic 2022-08-22 20:25:37 +02:00
parent 40f8a5ac16
commit 4673fec95a

View file

@ -2428,7 +2428,6 @@ namespace CodeImp.DoomBuilder.Map
if(verts.Count == 0 || lines.Count == 0) return true; //mxd
float splitdist2 = splitdist * splitdist;
bool splitted;
//mxd. Create blockmap
RectangleF area = CreateArea(lines);
@ -2440,70 +2439,68 @@ namespace CodeImp.DoomBuilder.Map
int bmHeight = blockmap.Size.Height;
BlockEntry[,] bmap = blockmap.Map;
do
foreach (Vertex v in verts)
{
// No split yet
splitted = false;
List<BlockEntry> blocks;
for(int w = 0; w < bmWidth; w++)
// If a vertex is in the very bottom left of it's block, and a linedef passes through the vertex it can
// happen (depending on the linedef's direction) that the vertex and the linedef are not in the same
// block. This will cause the linedef to not be split correctly. The prevent this we'll get all blocks
// surrounding the vertex, since the linedef will definitely be in at least one of them. Otherwise
// just take the block the vertex is in.
if ((blockmap.Range.Left - (int)v.Position.x) % blockmap.BlockSize == 0 && (blockmap.Range.Bottom - (int)v.Position.y) % blockmap.BlockSize == 0)
{
for(int h = 0; h < bmHeight; h++)
Rectangle r = new Rectangle((int)v.Position.x - 1, (int)v.Position.y - 1, 2, 2);
blocks = blockmap.GetSquareRange(r);
}
else
{
blocks = new List<BlockEntry>() { blockmap.GetBlockAt(v.Position) };
}
foreach (BlockEntry be in blocks)
{
if (be == null) continue;
for (int i = 0; i < be.Lines.Count; i++)
{
BlockEntry block = bmap[w, h];
if(block.Vertices.Count == 0 || block.Lines.Count == 0) continue;
Linedef l = be.Lines[i];
// Go for all the lines
foreach(Linedef l in block.Lines)
// Check if v is close enough to l for splitting
if (l.DistanceToSq(v.Position, true) <= splitdist2)
{
// Go for all the vertices
foreach(Vertex v in block.Vertices)
// Line is not already referencing v?
Vector2D deltastart = l.Start.Position - v.Position;
Vector2D deltaend = l.End.Position - v.Position;
if (((Math.Abs(deltastart.x) > 0.001f) || (Math.Abs(deltastart.y) > 0.001f)) &&
((Math.Abs(deltaend.x) > 0.001f) || (Math.Abs(deltaend.y) > 0.001f)))
{
// Check if v is close enough to l for splitting
if(l.DistanceToSq(v.Position, true) <= splitdist2)
// Split line l with vertex v
Linedef nl = l.Split(v);
if (nl == null) return false;
v.Marked = true; //mxd
// Add the new line to the list
lines.Add(nl);
blockmap.AddLinedef(nl);
// Both lines must be updated because their new length is relevant for next iterations!
l.UpdateCache();
nl.UpdateCache();
// Add both lines to changedlines
if (changedlines != null)
{
// Line is not already referencing v?
Vector2D deltastart = l.Start.Position - v.Position;
Vector2D deltaend = l.End.Position - v.Position;
if(((Math.Abs(deltastart.x) > 0.001f) || (Math.Abs(deltastart.y) > 0.001f)) &&
((Math.Abs(deltaend.x) > 0.001f) || (Math.Abs(deltaend.y) > 0.001f)))
{
// Split line l with vertex v
Linedef nl = l.Split(v);
if(nl == null) return false;
// Add the new line to the list
lines.Add(nl);
blockmap.AddLinedef(nl);
// Both lines must be updated because their new length
// is relevant for next iterations!
l.UpdateCache();
nl.UpdateCache();
// Add both lines to changedlines
if(changedlines != null)
{
changedlines.Add(l);
changedlines.Add(nl);
}
// Count the split
splitted = true;
break;
}
changedlines.Add(l);
changedlines.Add(nl);
}
}
// Will have to restart when splitted
// TODO: If we make (linked) lists from the collections first,
// we don't have to restart when splitted?
if(splitted) break;
}
}
}
}
while(splitted);
return true;
}