UltimateZoneBuilder/Source/Map/MapSet.cs

702 lines
17 KiB
C#
Raw Normal View History

2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
2007-06-13 19:39:38 +00:00
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* 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.
*
*/
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Namespaces
2007-06-13 19:39:38 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
2007-06-14 12:37:46 +00:00
using CodeImp.DoomBuilder.Geometry;
using SlimDX.Direct3D;
using CodeImp.DoomBuilder.Rendering;
using SlimDX;
2007-10-20 19:50:03 +00:00
using System.Drawing;
2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#endregion
2007-06-13 19:39:38 +00:00
namespace CodeImp.DoomBuilder.Map
{
2007-11-12 22:43:01 +00:00
public sealed class MapSet : IDisposable
2007-06-13 19:39:38 +00:00
{
#region ================== Variables
2007-06-14 12:37:46 +00:00
// Map structures
private LinkedList<Vertex> vertices;
private LinkedList<Linedef> linedefs;
private LinkedList<Sidedef> sidedefs;
private LinkedList<Sector> sectors;
private LinkedList<Thing> things;
2007-06-13 19:39:38 +00:00
// Disposing
private bool isdisposed = false;
#endregion
#region ================== Properties
2007-06-14 12:37:46 +00:00
public ICollection<Vertex> Vertices { get { return vertices; } }
public ICollection<Linedef> Linedefs { get { return linedefs; } }
public ICollection<Sidedef> Sidedefs { get { return sidedefs; } }
public ICollection<Sector> Sectors { get { return sectors; } }
public ICollection<Thing> Things { get { return things; } }
2007-06-13 19:39:38 +00:00
public bool IsDisposed { get { return isdisposed; } }
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Constructor / Disposer
2007-06-14 12:37:46 +00:00
// Constructor for new empty map
public MapSet()
2007-06-13 19:39:38 +00:00
{
// Initialize
2007-06-14 12:37:46 +00:00
vertices = new LinkedList<Vertex>();
linedefs = new LinkedList<Linedef>();
sidedefs = new LinkedList<Sidedef>();
sectors = new LinkedList<Sector>();
things = new LinkedList<Thing>();
2007-06-13 19:39:38 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Diposer
public void Dispose()
{
2007-06-14 12:37:46 +00:00
ArrayList list;
2007-06-13 19:39:38 +00:00
// Not already disposed?
if(!isdisposed)
{
2007-06-14 12:37:46 +00:00
// Already set isdisposed so that changes can be prohibited
isdisposed = true;
// Dispose all things
list = new ArrayList(things);
foreach(Thing t in list) t.Dispose();
// Dispose all sectors
list = new ArrayList(sectors);
foreach(Sector s in list) s.Dispose();
// Dispose all sidedefs
list = new ArrayList(sidedefs);
foreach(Sidedef sd in list) sd.Dispose();
// Dispose all linedefs
list = new ArrayList(linedefs);
foreach(Linedef l in list) l.Dispose();
2007-06-13 19:39:38 +00:00
2007-06-14 12:37:46 +00:00
// Dispose all vertices
list = new ArrayList(vertices);
foreach(Vertex v in list) v.Dispose();
// Clean up
vertices = null;
linedefs = null;
sidedefs = null;
sectors = null;
things = null;
2007-06-13 19:39:38 +00:00
// Done
isdisposed = true;
}
}
#endregion
2007-06-14 12:37:46 +00:00
#region ================== Management
// This makes a deep copy and returns a new MapSet
public MapSet Clone()
{
2007-11-23 09:53:50 +00:00
Linedef nl;
Sidedef nd;
// Create the map set
MapSet newset = new MapSet();
// Go for all vertices
foreach(Vertex v in vertices)
{
// Make new vertex
2007-11-23 09:33:56 +00:00
v.Clone = newset.CreateVertex(v.X, v.Y);
}
// Go for all sectors
foreach(Sector s in sectors)
{
// Make new sector
Sector ns = newset.CreateSector();
2007-11-23 09:33:56 +00:00
s.Clone = ns;
// Copy properties
s.CopyPropertiesTo(ns);
}
2007-11-23 09:53:50 +00:00
// Go for all linedefs
foreach(Linedef l in linedefs)
{
2007-11-23 09:53:50 +00:00
// Make new linedef
nl = newset.CreateLinedef(l.Start.Clone, l.End.Clone);
// Copy properties
2007-11-23 09:53:50 +00:00
l.CopyPropertiesTo(nl);
// Linedef has a front side?
if(l.Front != null)
{
// Make new sidedef
nd = newset.CreateSidedef(nl, true, l.Front.Sector.Clone);
// Copy properties
l.Front.CopyPropertiesTo(nd);
}
// Linedef has a back side?
if(l.Back != null)
{
// Make new sidedef
nd = newset.CreateSidedef(nl, false, l.Back.Sector.Clone);
// Copy properties
l.Back.CopyPropertiesTo(nd);
}
}
// Go for all things
foreach(Thing t in things)
{
// Make new thing
2007-06-24 18:56:43 +00:00
Thing nt = newset.CreateThing();
// Copy properties
t.CopyPropertiesTo(nt);
}
2007-11-23 09:33:56 +00:00
// Remove clone references
foreach(Vertex v in vertices) v.Clone = null;
foreach(Sector s in sectors) s.Clone = null;
// Return the new set
return newset;
}
2007-06-14 12:37:46 +00:00
// This creates a new vertex
public Vertex CreateVertex(int x, int y)
2007-06-14 12:37:46 +00:00
{
LinkedListNode<Vertex> listitem;
Vertex v;
// Make a list item
listitem = new LinkedListNode<Vertex>(null);
// Make the vertex
v = new Vertex(this, listitem, x, y);
2007-06-14 12:37:46 +00:00
listitem.Value = v;
// Add vertex to the list
vertices.AddLast(listitem);
// Return result
return v;
}
// This creates a new linedef
public Linedef CreateLinedef(Vertex start, Vertex end)
{
LinkedListNode<Linedef> listitem;
Linedef l;
// Make a list item
listitem = new LinkedListNode<Linedef>(null);
// Make the linedef
l = new Linedef(this, listitem, start, end);
listitem.Value = l;
// Add linedef to the list
linedefs.AddLast(listitem);
// Return result
return l;
}
// This creates a new sidedef
public Sidedef CreateSidedef(Linedef l, bool front, Sector s)
{
LinkedListNode<Sidedef> listitem;
Sidedef sd;
// Make a list item
listitem = new LinkedListNode<Sidedef>(null);
// Make the sidedef
sd = new Sidedef(this, listitem, l, front, s);
listitem.Value = sd;
// Add sidedef to the list
sidedefs.AddLast(listitem);
// Return result
return sd;
}
// This creates a new sector
public Sector CreateSector()
{
LinkedListNode<Sector> listitem;
Sector s;
// Make a list item
listitem = new LinkedListNode<Sector>(null);
// Make the sector
s = new Sector(this, listitem);
listitem.Value = s;
// Add sector to the list
sectors.AddLast(listitem);
// Return result
return s;
}
// This creates a new thing
2007-06-24 18:56:43 +00:00
public Thing CreateThing()
2007-06-14 12:37:46 +00:00
{
LinkedListNode<Thing> listitem;
Thing t;
// Make a list item
listitem = new LinkedListNode<Thing>(null);
// Make the thing
2007-06-24 18:56:43 +00:00
t = new Thing(this, listitem);
2007-06-14 12:37:46 +00:00
listitem.Value = t;
// Add thing to the list
things.AddLast(listitem);
// Return result
return t;
}
#endregion
#region ================== Updating
// This updates all structures if needed
public void Update()
{
// Update all linedefs
foreach(Linedef l in linedefs) l.Update();
}
// This updates all structures after a
// configuration or settings change
public void UpdateConfiguration()
{
// Update all things
foreach(Thing t in things) t.UpdateConfiguration();
}
#endregion
2007-06-14 12:37:46 +00:00
#region ================== Static Tools
2007-12-01 01:32:56 +00:00
// This joins nearby vertices from two collections. This does NOT join vertices
// within the same collection, only if they exist in both collections.
// The vertex from the second collection is moved to match the first vertex.
// When keepsecond is true, the vertex in the second collection is kept,
// otherwise the vertex in the first collection is kept.
// Returns the number of joins made
public static int JoinVertices(ICollection<Vertex> set1, ICollection<Vertex> set2, bool keepsecond, float joindist)
{
float joindist2 = joindist * joindist;
int joinsdone = 0;
bool joined;
do
{
// No joins yet
joined = false;
// Go for all vertices in the first set
foreach(Vertex v1 in set1)
{
// Go for all vertices in the second set
foreach(Vertex v2 in set2)
{
// Check if vertices are close enough
if(v1.DistanceToSq(v2.Position) <= joindist2)
{
// Check if not the same vertex
if(v1 != v2)
{
// Move the second vertex to match the first
v2.Move(v1.Position);
// Check which one to keep
if(keepsecond)
{
// Join the first into the second
// Second is kept, first is removed
v1.Join(v2);
set1.Remove(v1);
set2.Remove(v1);
}
else
{
// Join the second into the first
// First is kept, second is removed
v2.Join(v1);
set1.Remove(v2);
set2.Remove(v2);
}
// Count the join
joinsdone++;
joined = true;
break;
}
}
}
// Will have to restart when joined
if(joined) break;
}
}
while(joined);
// Return result
return joinsdone;
}
// This splits the given lines with the given vertices
// Returns the number of splits made
public static int SplitLinesByVertices(ICollection<Linedef> lines, ICollection<Vertex> verts, float splitdist)
{
float splitdist2 = splitdist * splitdist;
int splitsdone = 0;
bool splitted;
Linedef nl;
do
{
// No split yet
splitted = false;
// Go for all the lines
foreach(Linedef l in lines)
{
// Go for all the vertices
foreach(Vertex v in verts)
{
// Check if v is close enough to l for splitting
if(l.DistanceToSq(v.Position, true) <= splitdist2)
{
// Line is not already referencing v?
if((l.Start != v) && (l.End != v))
{
// Split line l with vertex v
nl = l.Split(v);
// Add the new line to the list
lines.Add(nl);
// Both lines must be updated because their new length
// is relevant for next iterations!
l.Update();
nl.Update();
// Count the split
splitsdone++;
splitted = true;
break;
}
}
}
// Will have to restart when splitted
if(splitted) break;
}
}
while(splitted);
// Return result
return splitsdone;
}
2007-06-14 12:37:46 +00:00
// This finds the line closest to the specified position
public static Linedef NearestLinedef(ICollection<Linedef> selection, Vector2D pos)
{
Linedef closest = null;
float distance = float.MaxValue;
float d;
// Go for all linedefs in selection
foreach(Linedef l in selection)
{
// Calculate distance and check if closer than previous find
d = l.SafeDistanceToSq(pos, true);
2007-06-14 12:37:46 +00:00
if(d < distance)
{
// This one is closer
closest = l;
distance = d;
}
}
// Return result
return closest;
}
2007-10-20 19:50:03 +00:00
// This finds the line closest to the specified position
public static Linedef NearestLinedefRange(ICollection<Linedef> selection, Vector2D pos, float maxrange)
{
Linedef closest = null;
float distance = float.MaxValue;
float maxrangesq = maxrange * maxrange;
float d;
// Go for all linedefs in selection
foreach(Linedef l in selection)
{
// Calculate distance and check if closer than previous find
d = l.SafeDistanceToSq(pos, true);
2007-10-20 19:50:03 +00:00
if((d <= maxrangesq) && (d < distance))
{
// This one is closer
closest = l;
distance = d;
}
}
// Return result
return closest;
}
2007-06-14 12:37:46 +00:00
// This finds the vertex closest to the specified position
public static Vertex NearestVertex(ICollection<Vertex> selection, Vector2D pos)
{
Vertex closest = null;
float distance = float.MaxValue;
float d;
2007-10-20 19:50:03 +00:00
2007-06-14 12:37:46 +00:00
// Go for all vertices in selection
foreach(Vertex v in selection)
{
// Calculate distance and check if closer than previous find
d = v.DistanceToSq(pos);
if(d < distance)
{
// This one is closer
closest = v;
distance = d;
}
}
// Return result
return closest;
}
2007-10-20 19:50:03 +00:00
// This finds the vertex closest to the specified position
public static Vertex NearestVertexSquareRange(ICollection<Vertex> selection, Vector2D pos, float maxrange)
{
RectangleF range = RectangleF.FromLTRB(pos.x - maxrange, pos.y - maxrange, pos.x + maxrange, pos.y + maxrange);
Vertex closest = null;
float distance = float.MaxValue;
float d;
// Go for all vertices in selection
foreach(Vertex v in selection)
{
// Within range?
if((v.Position.x >= range.Left) && (v.Position.x <= range.Right))
{
if((v.Position.y >= range.Top) && (v.Position.y <= range.Bottom))
{
// Close than previous find?
d = Math.Abs(v.Position.x - pos.x) + Math.Abs(v.Position.y - pos.y);
if(d < distance)
{
// This one is closer
closest = v;
distance = d;
}
}
}
}
// Return result
return closest;
}
// This finds the thing closest to the specified position
public static Thing NearestThingSquareRange(ICollection<Thing> selection, Vector2D pos, float maxrange)
{
RectangleF range = RectangleF.FromLTRB(pos.x - maxrange, pos.y - maxrange, pos.x + maxrange, pos.y + maxrange);
Thing closest = null;
float distance = float.MaxValue;
float d;
// Go for all vertices in selection
foreach(Thing t in selection)
{
// Within range?
if((t.Position.x >= (range.Left - t.Size)) && (t.Position.x <= (range.Right + t.Size)))
{
if((t.Position.y >= (range.Top - t.Size)) && (t.Position.y <= (range.Bottom + t.Size)))
{
// Close than previous find?
d = Math.Abs(t.Position.x - pos.x) + Math.Abs(t.Position.y - pos.y);
if(d < distance)
{
// This one is closer
closest = t;
distance = d;
}
}
}
}
// Return result
return closest;
}
2007-06-14 12:37:46 +00:00
#endregion
#region ================== Tools
2007-12-01 01:32:56 +00:00
// This makes a list of lines related to vertex selection
// A line is unstable when one vertex is selected and the other isn't.
public ICollection<Linedef> LinedefsFromSelectedVertices(bool includestable, bool includeunstable)
{
List<Linedef> list = new List<Linedef>();
// Go for all lines
foreach(Linedef l in linedefs)
{
// Check if this is to be included
if((includestable && ((l.Start.Selected > 0) && (l.End.Selected > 0))) ||
(includeunstable && ((l.Start.Selected > 0) || (l.End.Selected > 0))) )
{
// Add to list
list.Add(l);
}
}
// Return result
return list;
}
// This returns all vertices not in verts collection
public ICollection<Vertex> InvertedCollection(ICollection<Vertex> verts)
{
List<Vertex> list = new List<Vertex>();
// Go for all vertices
foreach(Vertex v in vertices) if(!verts.Contains(v)) list.Add(v);
// Return result
return list;
}
// This returns all linedefs not in lines collection
public ICollection<Linedef> InvertedCollection(ICollection<Linedef> lines)
{
List<Linedef> list = new List<Linedef>();
// Go for all lines
foreach(Linedef l in linedefs) if(!lines.Contains(l)) list.Add(l);
// Return result
return list;
}
2007-06-14 12:37:46 +00:00
// This finds the line closest to the specified position
public Linedef NearestLinedef(Vector2D pos) { return MapSet.NearestLinedef(linedefs, pos); }
2007-06-13 19:39:38 +00:00
2007-10-20 19:50:03 +00:00
// This finds the line closest to the specified position
public Linedef NearestLinedefRange(Vector2D pos, float maxrange) { return MapSet.NearestLinedefRange(linedefs, pos, maxrange); }
2007-06-14 12:37:46 +00:00
// This finds the vertex closest to the specified position
public Vertex NearestVertex(Vector2D pos) { return MapSet.NearestVertex(vertices, pos); }
2007-10-20 19:50:03 +00:00
// This finds the vertex closest to the specified position
public Vertex NearestVertexSquareRange(Vector2D pos, float maxrange) { return MapSet.NearestVertexSquareRange(vertices, pos, maxrange); }
// This finds the thing closest to the specified position
public Thing NearestThingSquareRange(Vector2D pos, float maxrange) { return MapSet.NearestThingSquareRange(things, pos, maxrange); }
2007-12-01 01:32:56 +00:00
// This finds the closest unselected linedef that is not connected to the given vertex
public Linedef NearestUnselectedUnreferencedLinedef(Vector2D pos, float maxrange, Vertex v, out float distance)
{
Linedef closest = null;
distance = float.MaxValue;
float maxrangesq = maxrange * maxrange;
float d;
// Go for all linedefs in selection
foreach(Linedef l in linedefs)
{
// Calculate distance and check if closer than previous find
d = l.SafeDistanceToSq(pos, true);
if((d <= maxrangesq) && (d < distance))
{
// Check if not selected
// Check if linedef is not connected to v
if((l.Start != v) && (l.End != v))
{
// This one is closer
closest = l;
distance = d;
}
}
}
// Return result
return closest;
}
// This performs sidedefs compression
public void CompressSidedefs()
{
// TODO: Make this happen
}
2007-10-21 04:07:36 +00:00
// This removes unused vertices
public void RemoveUnusedVertices()
{
LinkedListNode<Vertex> vn, vc;
// Go for all vertices
vn = vertices.First;
while(vn != null)
{
vc = vn;
vn = vc.Next;
if(vc.Value.Linedefs.Count == 0) vertices.Remove(vc);
}
}
2007-11-04 22:19:30 +00:00
2007-06-13 19:39:38 +00:00
#endregion
}
}