2009-04-19 18:07:22 +00:00
|
|
|
|
2009-05-30 15:18:21 +00:00
|
|
|
#region ================== Copyright (c) 2009 Boris Iwanski
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
/*
|
2009-05-30 15:18:21 +00:00
|
|
|
* Copyright (c) 2009 Boris Iwanski
|
2009-04-19 18:07:22 +00:00
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.BuilderModes
|
|
|
|
{
|
2013-08-30 15:00:44 +00:00
|
|
|
[ErrorChecker("Check invalid sectors", true, 300)]
|
2009-04-19 18:07:22 +00:00
|
|
|
public class CheckClosedSectors : ErrorChecker
|
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
private const int PROGRESS_STEP = 40;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Destructor
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public CheckClosedSectors()
|
|
|
|
{
|
|
|
|
// Total progress is done when all sectors are checked
|
|
|
|
SetTotalProgress(General.Map.Map.Sectors.Count / PROGRESS_STEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// This runs the check
|
|
|
|
public override void Run()
|
|
|
|
{
|
|
|
|
int progress = 0;
|
|
|
|
int stepprogress = 0;
|
|
|
|
|
2013-09-11 09:47:53 +00:00
|
|
|
// This is a simple yet effective way to check if a sector is closed.
|
|
|
|
// Each sidedef that belongs to a sector is checked out. The lines vertices
|
|
|
|
// are used as the key in a Dictionary. While going through all the
|
|
|
|
// sidedefs the values associated to the keys are set with a bitwise OR.
|
|
|
|
//
|
|
|
|
// The idea for this method was taken from the way DEU checks for closed
|
|
|
|
// sectors
|
|
|
|
//
|
|
|
|
// The starting vertex of the side gets its 1 bit set, the ending vertex
|
|
|
|
// of the side gets its 2 bit set.
|
|
|
|
// The resulting dictionary will have 1, 2 or 3 as values (unlike the DEU
|
|
|
|
// version 0 will not appear here, since we only look at sides that belong
|
|
|
|
// to the current sector).
|
|
|
|
// Values 1 and 2 mean that a line they belong to has no side belonging to
|
|
|
|
// the current sector, thereby making the sector unclosed.
|
|
|
|
// A value of 3 usually means all lines belonging to that vertex have at least
|
|
|
|
// one side referencing the current sector. There are (rare) cases where this
|
|
|
|
// is not true, so a second check is done for all "opposite" vertices of the
|
|
|
|
// vertices with values 1 and 2: if the opposite vertex has a value of 3, but
|
|
|
|
// the line between the two vertices has no side referencing the current sector,
|
|
|
|
// then this falsely good vertex is added to the holes, too
|
2009-05-30 15:18:21 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Go for all the sectors
|
|
|
|
foreach(Sector s in General.Map.Map.Sectors)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
List<Vertex> foundholes = new List<Vertex>();
|
|
|
|
Dictionary<Vertex, int> vertices = new Dictionary<Vertex, int>();
|
|
|
|
|
|
|
|
// look at each side that belongs to the current sector
|
2015-12-28 15:01:53 +00:00
|
|
|
foreach(Sidedef sd in s.Sidedefs)
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
// Add the lines starting vertex to the Dictionary if it's not yet present
|
2015-12-28 15:01:53 +00:00
|
|
|
if(!vertices.ContainsKey(sd.Line.Start))
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
vertices.Add(sd.Line.Start, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the lines ending vertex to the Dictionary if it's not yet present
|
2015-12-28 15:01:53 +00:00
|
|
|
if(!vertices.ContainsKey(sd.Line.End))
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
vertices.Add(sd.Line.End, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable the 1 bit of the start vertex and the 2 bit of the end vertex of the side
|
|
|
|
// This is from the sides point of view, where the side is always the "right" of line,
|
|
|
|
// so start and end are flipped depending if the current side is the front of the
|
|
|
|
// line or not
|
2014-12-03 23:15:26 +00:00
|
|
|
if(sd.IsFront)
|
|
|
|
{
|
2013-09-11 09:47:53 +00:00
|
|
|
vertices[sd.Line.Start] |= 1;
|
|
|
|
vertices[sd.Line.End] |= 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vertices[sd.Line.End] |= 1;
|
|
|
|
vertices[sd.Line.Start] |= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look at all the vertices
|
2015-12-28 15:01:53 +00:00
|
|
|
foreach(KeyValuePair<Vertex, int> kvp in vertices)
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
// only look at bad vertices
|
2015-12-28 15:01:53 +00:00
|
|
|
if(kvp.Value != 3)
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
// add the current vertex to the holes list
|
2015-12-28 15:01:53 +00:00
|
|
|
if(!foundholes.Contains(kvp.Key))
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
foundholes.Add(kvp.Key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// this checks if any vertices slipped past the first test
|
|
|
|
// look at all lines that share the current vertex
|
2015-12-28 15:01:53 +00:00
|
|
|
foreach(Linedef cl in kvp.Key.Linedefs)
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
// the line does not has its front or back side referencing the current sector
|
2015-12-28 15:01:53 +00:00
|
|
|
if(!((cl.Front != null && cl.Front.Sector == s) || (cl.Back != null && cl.Back.Sector == s)))
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
// if the opposite vertex of our current vertex is marked good something must be wrong,
|
|
|
|
// since the above check tells us that the line bewtween the vertices has no side referencing
|
|
|
|
// the current sector. So add the falsely marked vertex to the holes list
|
2015-12-28 15:01:53 +00:00
|
|
|
if(vertices.ContainsKey(cl.Start) && vertices[cl.Start] == 3 && !foundholes.Contains(cl.Start))
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
foundholes.Add(cl.Start);
|
|
|
|
}
|
|
|
|
|
2015-12-28 15:01:53 +00:00
|
|
|
if(vertices.ContainsKey(cl.End) && vertices[cl.End] == 3 && !foundholes.Contains(cl.End))
|
2013-09-11 09:47:53 +00:00
|
|
|
{
|
|
|
|
foundholes.Add(cl.End);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-30 15:18:21 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Add report when holes have been found
|
|
|
|
if(foundholes.Count > 0)
|
2015-12-28 15:01:53 +00:00
|
|
|
{
|
2009-04-19 18:07:22 +00:00
|
|
|
SubmitResult(new ResultSectorUnclosed(s, foundholes));
|
2015-12-28 15:01:53 +00:00
|
|
|
}
|
|
|
|
//mxd. Add report when sector has less than 3 sidedefs
|
|
|
|
else if(s.Sidedefs.Count < 3 || s.BBox.IsEmpty)
|
|
|
|
{
|
2013-08-30 15:00:44 +00:00
|
|
|
SubmitResult(new ResultSectorInvalid(s));
|
2015-12-28 15:01:53 +00:00
|
|
|
}
|
|
|
|
//mxd. Add report when sector has less than 3 linedefs
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HashSet<Linedef> lines = new HashSet<Linedef>();
|
|
|
|
foreach(Sidedef side in s.Sidedefs)
|
|
|
|
if(side.Line != null && !lines.Contains(side.Line)) lines.Add(side.Line);
|
|
|
|
|
|
|
|
if(lines.Count < 3) SubmitResult(new ResultSectorInvalid(s));
|
|
|
|
}
|
2009-05-30 15:18:21 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// Handle thread interruption
|
|
|
|
try { Thread.Sleep(0); }
|
|
|
|
catch(ThreadInterruptedException) { return; }
|
|
|
|
|
|
|
|
// We are making progress!
|
|
|
|
if((++progress / PROGRESS_STEP) > stepprogress)
|
|
|
|
{
|
|
|
|
stepprogress = (progress / PROGRESS_STEP);
|
|
|
|
AddProgress(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|