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 ;
2008-05-11 00:42:34 +00:00
using SlimDX.Direct3D9 ;
2007-07-07 09:40:34 +00:00
using CodeImp.DoomBuilder.Rendering ;
using SlimDX ;
2007-10-20 19:50:03 +00:00
using System.Drawing ;
2007-12-26 00:31:32 +00:00
using CodeImp.DoomBuilder.Editing ;
2008-04-19 15:44:05 +00:00
using CodeImp.DoomBuilder.IO ;
2008-09-23 17:46:34 +00:00
using CodeImp.DoomBuilder.Types ;
2008-12-06 13:20:47 +00:00
using System.IO ;
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
{
2008-01-02 21:49:43 +00:00
public sealed class MapSet
2007-06-13 19:39:38 +00:00
{
2007-12-27 01:24:11 +00:00
#region = = = = = = = = = = = = = = = = = = Constants
2008-05-06 05:41:36 +00:00
// Stiching distance
public const float STITCH_DISTANCE = 0.001f ;
2008-09-23 17:46:34 +00:00
// Virtual sector identification
// This contains a character that is invalid in the UDMF standard, but valid
// in our parser, so that it can only be used by Doom Builder and will never
// conflict with any other valid UDMF field.
internal const string VIRTUAL_SECTOR_FIELD = "!virtual_sector" ;
2007-12-27 01:24:11 +00:00
#endregion
2007-06-13 19:39:38 +00:00
#region = = = = = = = = = = = = = = = = = = Variables
2007-12-27 01:24:11 +00:00
// Sector indexing
private List < int > indexholes ;
private int lastsectorindex ;
2009-02-20 15:59:55 +00:00
// Sidedef indexing for (de)serialization
private Sidedef [ ] sidedefindices ;
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 ;
2009-03-30 07:45:39 +00:00
// Selected elements
private LinkedList < Vertex > sel_vertices ;
private LinkedList < Linedef > sel_linedefs ;
private LinkedList < Sector > sel_sectors ;
private LinkedList < Thing > sel_things ;
2009-03-30 19:02:56 +00:00
private SelectionType sel_type ;
2009-03-30 07:45:39 +00:00
2008-09-23 17:46:34 +00:00
// Statics
2008-09-17 19:21:45 +00:00
private static long emptylongname ;
2008-09-23 17:46:34 +00:00
private static UniValue virtualsectorvalue ;
2008-04-19 15:44:05 +00:00
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 ; } }
2009-03-30 07:45:39 +00:00
internal LinkedList < Vertex > SelectedVertices { get { return sel_vertices ; } }
internal LinkedList < Linedef > SelectedLinedefs { get { return sel_linedefs ; } }
internal LinkedList < Sector > SelectedSectors { get { return sel_sectors ; } }
internal LinkedList < Thing > SelectedThings { get { return sel_things ; } }
2009-03-30 19:02:56 +00:00
public SelectionType SelectionType { get { return sel_type ; } set { sel_type = value ; } }
2008-09-23 17:46:34 +00:00
2008-09-17 19:21:45 +00:00
public static long EmptyLongName { get { return emptylongname ; } }
2008-09-23 17:46:34 +00:00
public static string VirtualSectorField { get { return VIRTUAL_SECTOR_FIELD ; } }
public static UniValue VirtualSectorValue { get { return virtualsectorvalue ; } }
2007-07-07 09:40:34 +00:00
2009-02-20 15:59:55 +00:00
internal Sidedef [ ] SidedefIndices { get { return sidedefindices ; } }
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
2008-05-10 16:09:45 +00:00
internal 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 > ( ) ;
2009-03-30 07:45:39 +00:00
sel_vertices = new LinkedList < Vertex > ( ) ;
sel_linedefs = new LinkedList < Linedef > ( ) ;
sel_sectors = new LinkedList < Sector > ( ) ;
sel_things = new LinkedList < Thing > ( ) ;
2007-12-27 01:24:11 +00:00
indexholes = new List < int > ( ) ;
lastsectorindex = 0 ;
2007-06-13 19:39:38 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
2008-12-06 13:20:47 +00:00
// Constructor for map to deserialize
internal MapSet ( MemoryStream stream )
{
// Initialize
vertices = new LinkedList < Vertex > ( ) ;
linedefs = new LinkedList < Linedef > ( ) ;
sidedefs = new LinkedList < Sidedef > ( ) ;
sectors = new LinkedList < Sector > ( ) ;
things = new LinkedList < Thing > ( ) ;
2009-03-30 07:45:39 +00:00
sel_vertices = new LinkedList < Vertex > ( ) ;
sel_linedefs = new LinkedList < Linedef > ( ) ;
sel_sectors = new LinkedList < Sector > ( ) ;
sel_things = new LinkedList < Thing > ( ) ;
2008-12-06 13:20:47 +00:00
indexholes = new List < int > ( ) ;
lastsectorindex = 0 ;
// Deserialize
Deserialize ( stream ) ;
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
2008-01-02 21:49:43 +00:00
// Disposer
2008-05-10 16:09:45 +00:00
internal void Dispose ( )
2007-06-13 19:39:38 +00:00
{
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 ;
2009-03-30 07:45:39 +00:00
sel_vertices = null ;
sel_linedefs = null ;
sel_sectors = null ;
sel_things = null ;
2007-12-27 01:24:11 +00:00
indexholes = null ;
2007-06-14 12:37:46 +00:00
2007-06-13 19:39:38 +00:00
// Done
isdisposed = true ;
}
}
2008-09-17 19:21:45 +00:00
// Static initializer
internal static void Initialize ( )
{
emptylongname = Lump . MakeLongName ( "-" ) ;
2008-09-23 17:46:34 +00:00
virtualsectorvalue = new UniValue ( ( int ) UniversalType . Integer , ( int ) 0 ) ;
2008-09-17 19:21:45 +00:00
}
2007-06-13 19:39:38 +00:00
#endregion
2007-06-14 12:37:46 +00:00
#region = = = = = = = = = = = = = = = = = = Management
2007-06-14 13:09:55 +00:00
// 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 ;
2007-06-14 13:09:55 +00:00
// Create the map set
MapSet newset = new MapSet ( ) ;
// Go for all vertices
foreach ( Vertex v in vertices )
{
// Make new vertex
2008-05-05 22:01:27 +00:00
v . Clone = newset . CreateVertex ( v . Position ) ;
2008-05-01 14:18:04 +00:00
v . CopyPropertiesTo ( v . Clone ) ;
2007-06-14 13:09:55 +00:00
}
// Go for all sectors
foreach ( Sector s in sectors )
{
// Make new sector
2008-05-01 14:18:04 +00:00
s . Clone = newset . CreateSector ( ) ;
s . CopyPropertiesTo ( s . Clone ) ;
2007-06-14 13:09:55 +00:00
}
2007-11-23 09:53:50 +00:00
// Go for all linedefs
foreach ( Linedef l in linedefs )
2007-06-14 13:09:55 +00:00
{
2007-11-23 09:53:50 +00:00
// Make new linedef
nl = newset . CreateLinedef ( l . Start . Clone , l . End . Clone ) ;
l . CopyPropertiesTo ( nl ) ;
// Linedef has a front side?
if ( l . Front ! = null )
{
// Make new sidedef
nd = newset . CreateSidedef ( nl , true , l . Front . Sector . Clone ) ;
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 ) ;
l . Back . CopyPropertiesTo ( nd ) ;
}
2007-06-14 13:09:55 +00:00
}
// Go for all things
foreach ( Thing t in things )
{
// Make new thing
2007-06-24 18:56:43 +00:00
Thing nt = newset . CreateThing ( ) ;
2007-06-14 13:09:55 +00:00
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 ;
2007-06-14 13:09:55 +00:00
// Return the new set
return newset ;
}
2008-09-23 17:46:34 +00:00
// This makes a deep copy of the marked geometry and binds missing sectors to a virtual sector
internal MapSet CloneMarked ( )
{
Sector virtualsector = null ;
// Create the map set
MapSet newset = new MapSet ( ) ;
// Get marked geometry
ICollection < Vertex > mvertices = GetMarkedVertices ( true ) ;
ICollection < Linedef > mlinedefs = GetMarkedLinedefs ( true ) ;
ICollection < Sector > msectors = GetMarkedSectors ( true ) ;
ICollection < Thing > mthings = GetMarkedThings ( true ) ;
// Go for all vertices
foreach ( Vertex v in mvertices )
{
// Make new vertex
v . Clone = newset . CreateVertex ( v . Position ) ;
v . CopyPropertiesTo ( v . Clone ) ;
}
// Go for all sectors
foreach ( Sector s in msectors )
{
// Make new sector
s . Clone = newset . CreateSector ( ) ;
s . CopyPropertiesTo ( s . Clone ) ;
}
// Go for all linedefs
foreach ( Linedef l in mlinedefs )
{
// Make new linedef
Linedef nl = newset . CreateLinedef ( l . Start . Clone , l . End . Clone ) ;
l . CopyPropertiesTo ( nl ) ;
// Linedef has a front side?
if ( l . Front ! = null )
{
Sidedef nd ;
// Sector on front side marked?
if ( l . Front . Sector . Marked )
{
// Make new sidedef
nd = newset . CreateSidedef ( nl , true , l . Front . Sector . Clone ) ;
}
else
{
// Make virtual sector if needed
if ( virtualsector = = null )
{
virtualsector = newset . CreateSector ( ) ;
l . Front . Sector . CopyPropertiesTo ( virtualsector ) ;
virtualsector . Fields [ VIRTUAL_SECTOR_FIELD ] = new UniValue ( virtualsectorvalue ) ;
}
// Make new sidedef that links to the virtual sector
nd = newset . CreateSidedef ( nl , true , virtualsector ) ;
}
l . Front . CopyPropertiesTo ( nd ) ;
}
// Linedef has a back side?
if ( l . Back ! = null )
{
Sidedef nd ;
// Sector on front side marked?
if ( l . Back . Sector . Marked )
{
// Make new sidedef
nd = newset . CreateSidedef ( nl , false , l . Back . Sector . Clone ) ;
}
else
{
// Make virtual sector if needed
if ( virtualsector = = null )
{
virtualsector = newset . CreateSector ( ) ;
l . Back . Sector . CopyPropertiesTo ( virtualsector ) ;
virtualsector . Fields [ VIRTUAL_SECTOR_FIELD ] = new UniValue ( virtualsectorvalue ) ;
}
// Make new sidedef that links to the virtual sector
nd = newset . CreateSidedef ( nl , false , virtualsector ) ;
}
l . Back . CopyPropertiesTo ( nd ) ;
}
}
// Go for all things
foreach ( Thing t in mthings )
{
// Make new thing
Thing nt = newset . CreateThing ( ) ;
t . CopyPropertiesTo ( nt ) ;
}
// 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 13:09:55 +00:00
2007-06-14 12:37:46 +00:00
// This creates a new vertex
2008-05-05 22:01:27 +00:00
public Vertex CreateVertex ( Vector2D pos )
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
2008-05-05 22:01:27 +00:00
v = new Vertex ( this , listitem , pos ) ;
2007-06-14 12:37:46 +00:00
listitem . Value = v ;
// Add vertex to the list
vertices . AddLast ( listitem ) ;
// Return result
return v ;
}
2008-12-06 13:20:47 +00:00
// This creates a new vertex
private Vertex CreateVertex ( IReadWriteStream stream )
{
LinkedListNode < Vertex > listitem ;
Vertex v ;
// Make a list item
listitem = new LinkedListNode < Vertex > ( null ) ;
// Make the vertex
v = new Vertex ( this , listitem , stream ) ;
listitem . Value = v ;
// Add vertex to the list
vertices . AddLast ( listitem ) ;
// Return result
return v ;
}
2007-06-14 12:37:46 +00:00
// 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 ;
}
2008-12-06 13:20:47 +00:00
// This creates a new linedef
private Linedef CreateLinedef ( Vertex start , Vertex end , IReadWriteStream stream )
{
LinkedListNode < Linedef > listitem ;
Linedef l ;
// Make a list item
listitem = new LinkedListNode < Linedef > ( null ) ;
// Make the linedef
l = new Linedef ( this , listitem , start , end , stream ) ;
listitem . Value = l ;
// Add linedef to the list
linedefs . AddLast ( listitem ) ;
// Return result
return l ;
}
2007-06-14 12:37:46 +00:00
// 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 ;
}
2008-12-06 13:20:47 +00:00
// This creates a new sidedef
private Sidedef CreateSidedef ( Linedef l , bool front , Sector s , IReadWriteStream stream )
{
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 , stream ) ;
listitem . Value = sd ;
// Add sidedef to the list
sidedefs . AddLast ( listitem ) ;
// Return result
return sd ;
}
2007-06-14 12:37:46 +00:00
// This creates a new sector
public Sector CreateSector ( )
2007-12-27 01:24:11 +00:00
{
int index ;
// Do we have any index holes we can use?
if ( indexholes . Count > 0 )
{
// Take one of the index holes
index = indexholes [ indexholes . Count - 1 ] ;
indexholes . RemoveAt ( indexholes . Count - 1 ) ;
}
else
{
// Make a new index
index = lastsectorindex + + ;
}
// Make the sector
return CreateSector ( index ) ;
}
// This creates a new sector
public Sector CreateSector ( int index )
2007-06-14 12:37:46 +00:00
{
LinkedListNode < Sector > listitem ;
Sector s ;
// Make a list item
listitem = new LinkedListNode < Sector > ( null ) ;
// Make the sector
2007-12-27 01:24:11 +00:00
s = new Sector ( this , listitem , index ) ;
2007-06-14 12:37:46 +00:00
listitem . Value = s ;
// Add sector to the list
sectors . AddLast ( listitem ) ;
// Return result
return s ;
}
2008-12-06 13:20:47 +00:00
// This creates a new sector
private Sector CreateSector ( IReadWriteStream stream )
{
LinkedListNode < Sector > listitem ;
Sector s ;
// Make a list item
listitem = new LinkedListNode < Sector > ( null ) ;
// Make the sector
s = new Sector ( this , listitem , stream ) ;
listitem . Value = s ;
// Add sector to the list
sectors . AddLast ( listitem ) ;
// Return result
return s ;
}
2007-06-14 12:37:46 +00:00
// 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 ;
}
2007-07-07 09:40:34 +00:00
2007-12-27 01:24:11 +00:00
// This adds a sector index hole
public void AddSectorIndexHole ( int index )
{
indexholes . Add ( index ) ;
}
2007-07-07 09:40:34 +00:00
#endregion
2008-12-06 13:20:47 +00:00
#region = = = = = = = = = = = = = = = = = = Serialization
// This serializes the MapSet
internal MemoryStream Serialize ( )
{
2009-02-20 15:59:55 +00:00
MemoryStream stream = new MemoryStream ( 20000000 ) ; // Yes that is about 20 MB.
2008-12-06 13:20:47 +00:00
SerializerStream serializer = new SerializerStream ( stream ) ;
2009-02-20 12:29:37 +00:00
// Index the sidedefs
int sidedefindex = 0 ;
foreach ( Sidedef sd in sidedefs )
sd . SerializedIndex = sidedefindex + + ;
2009-02-20 18:51:31 +00:00
serializer . Begin ( ) ;
2008-12-06 13:20:47 +00:00
// Write private data
serializer . wInt ( lastsectorindex ) ;
serializer . wInt ( indexholes . Count ) ;
foreach ( int i in indexholes ) serializer . wInt ( i ) ;
// Write map data
WriteVertices ( serializer ) ;
WriteSectors ( serializer ) ;
WriteLinedefs ( serializer ) ;
WriteSidedefs ( serializer ) ;
WriteThings ( serializer ) ;
2009-02-20 18:51:31 +00:00
serializer . End ( ) ;
2009-02-20 15:59:55 +00:00
// Reallocate to keep only the used memory
2009-02-20 18:51:31 +00:00
stream . Capacity = ( int ) stream . Length ;
2009-02-20 15:59:55 +00:00
2008-12-06 13:20:47 +00:00
return stream ;
}
// This serializes things
private void WriteThings ( SerializerStream stream )
{
stream . wInt ( things . Count ) ;
// Go for all things
foreach ( Thing t in things )
{
t . ReadWrite ( stream ) ;
}
}
// This serializes vertices
private void WriteVertices ( SerializerStream stream )
{
stream . wInt ( vertices . Count ) ;
// Go for all vertices
int index = 0 ;
foreach ( Vertex v in vertices )
{
v . SerializedIndex = index + + ;
v . ReadWrite ( stream ) ;
}
}
// This serializes linedefs
private void WriteLinedefs ( SerializerStream stream )
{
stream . wInt ( linedefs . Count ) ;
// Go for all lines
int index = 0 ;
foreach ( Linedef l in linedefs )
{
l . SerializedIndex = index + + ;
stream . wInt ( l . Start . SerializedIndex ) ;
stream . wInt ( l . End . SerializedIndex ) ;
l . ReadWrite ( stream ) ;
}
}
// This serializes sidedefs
private void WriteSidedefs ( SerializerStream stream )
{
stream . wInt ( sidedefs . Count ) ;
// Go for all sidedefs
foreach ( Sidedef sd in sidedefs )
{
stream . wInt ( sd . Line . SerializedIndex ) ;
stream . wInt ( sd . Sector . SerializedIndex ) ;
stream . wBool ( sd . IsFront ) ;
sd . ReadWrite ( stream ) ;
}
}
// This serializes sectors
private void WriteSectors ( SerializerStream stream )
{
stream . wInt ( sectors . Count ) ;
// Go for all sectors
int index = 0 ;
foreach ( Sector s in sectors )
{
s . SerializedIndex = index + + ;
s . ReadWrite ( stream ) ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Deserialization
// This serializes the MapSet
private void Deserialize ( MemoryStream stream )
{
stream . Seek ( 0 , SeekOrigin . Begin ) ;
DeserializerStream deserializer = new DeserializerStream ( stream ) ;
2009-02-20 18:51:31 +00:00
deserializer . Begin ( ) ;
2008-12-06 13:20:47 +00:00
// Read private data
int c ;
deserializer . rInt ( out lastsectorindex ) ;
deserializer . rInt ( out c ) ;
indexholes = new List < int > ( c ) ;
for ( int i = 0 ; i < c ; i + + )
{
int index ; deserializer . rInt ( out index ) ;
indexholes . Add ( index ) ;
}
// Read map data
Vertex [ ] verticesarray = ReadVertices ( deserializer ) ;
Sector [ ] sectorsarray = ReadSectors ( deserializer ) ;
Linedef [ ] linedefsarray = ReadLinedefs ( deserializer , verticesarray ) ;
ReadSidedefs ( deserializer , linedefsarray , sectorsarray ) ;
ReadThings ( deserializer ) ;
2009-02-20 18:51:31 +00:00
deserializer . End ( ) ;
2009-02-20 15:59:55 +00:00
// Make table of sidedef indices
sidedefindices = new Sidedef [ sidedefs . Count ] ;
foreach ( Sidedef sd in sidedefs )
sidedefindices [ sd . SerializedIndex ] = sd ;
// Call PostDeserialize
foreach ( Sector s in sectors )
s . PostDeserialize ( this ) ;
2008-12-06 13:20:47 +00:00
}
2009-02-20 15:59:55 +00:00
2008-12-06 13:20:47 +00:00
// This deserializes things
private void ReadThings ( DeserializerStream stream )
{
int c ; stream . rInt ( out c ) ;
// Go for all things
for ( int i = 0 ; i < c ; i + + )
{
Thing t = CreateThing ( ) ;
t . ReadWrite ( stream ) ;
}
}
// This deserializes vertices
private Vertex [ ] ReadVertices ( DeserializerStream stream )
{
int c ; stream . rInt ( out c ) ;
Vertex [ ] array = new Vertex [ c ] ;
// Go for all vertices
for ( int i = 0 ; i < c ; i + + )
{
array [ i ] = CreateVertex ( stream ) ;
}
return array ;
}
// This deserializes linedefs
private Linedef [ ] ReadLinedefs ( DeserializerStream stream , Vertex [ ] verticesarray )
{
int c ; stream . rInt ( out c ) ;
Linedef [ ] array = new Linedef [ c ] ;
// Go for all lines
for ( int i = 0 ; i < c ; i + + )
{
int start , end ;
stream . rInt ( out start ) ;
stream . rInt ( out end ) ;
array [ i ] = CreateLinedef ( verticesarray [ start ] , verticesarray [ end ] , stream ) ;
}
return array ;
}
// This deserializes sidedefs
private void ReadSidedefs ( DeserializerStream stream , Linedef [ ] linedefsarray , Sector [ ] sectorsarray )
{
int c ; stream . rInt ( out c ) ;
// Go for all sidedefs
for ( int i = 0 ; i < c ; i + + )
{
int lineindex , sectorindex ;
bool front ;
stream . rInt ( out lineindex ) ;
stream . rInt ( out sectorindex ) ;
stream . rBool ( out front ) ;
CreateSidedef ( linedefsarray [ lineindex ] , front , sectorsarray [ sectorindex ] , stream ) ;
}
}
// This deserializes sectors
private Sector [ ] ReadSectors ( DeserializerStream stream )
{
int c ; stream . rInt ( out c ) ;
Sector [ ] array = new Sector [ c ] ;
// Go for all sectors
for ( int i = 0 ; i < c ; i + + )
{
array [ i ] = CreateSector ( stream ) ;
}
return array ;
}
#endregion
2007-09-17 21:22:46 +00:00
#region = = = = = = = = = = = = = = = = = = Updating
2007-07-07 09:40:34 +00:00
// This updates all structures if needed
public void Update ( )
2008-01-13 21:23:59 +00:00
{
// Update all!
Update ( true , true ) ;
}
// This updates all structures if needed
public void Update ( bool dolines , bool dosectors )
2007-07-07 09:40:34 +00:00
{
// Update all linedefs
2008-01-13 21:23:59 +00:00
if ( dolines ) foreach ( Linedef l in linedefs ) l . UpdateCache ( ) ;
// Update all sectors
if ( dosectors ) foreach ( Sector s in sectors ) s . UpdateCache ( ) ;
2007-07-07 09:40:34 +00:00
}
2007-10-21 22:41:46 +00:00
// This updates all structures after a
// configuration or settings change
public void UpdateConfiguration ( )
{
// Update all things
foreach ( Thing t in things ) t . UpdateConfiguration ( ) ;
}
2007-07-07 09:40:34 +00:00
#endregion
2007-12-01 18:29:58 +00:00
#region = = = = = = = = = = = = = = = = = = Selection
2009-03-30 19:02:56 +00:00
// This checks a flag in a selection type
private bool InSelectionType ( SelectionType value , SelectionType bits )
{
return ( value & bits ) = = bits ;
}
// This converts the selection to a different selection
// NOTE: This function uses the markings to convert the selection
public void ConvertSelection ( SelectionType target )
{
ConvertSelection ( SelectionType . All , target ) ;
}
// This converts the selection to a different selection
// NOTE: This function uses the markings to convert the selection
public void ConvertSelection ( SelectionType source , SelectionType target )
{
ICollection < Linedef > lines ;
ICollection < Vertex > verts ;
ClearAllMarks ( false ) ;
switch ( target )
{
// Convert geometry selection to vertices only
case SelectionType . Vertices :
if ( InSelectionType ( source , SelectionType . Linedefs ) ) MarkSelectedLinedefs ( true , true ) ;
if ( InSelectionType ( source , SelectionType . Sectors ) ) General . Map . Map . MarkSelectedSectors ( true , true ) ;
verts = General . Map . Map . GetVerticesFromLinesMarks ( true ) ;
foreach ( Vertex v in verts ) v . Selected = true ;
verts = General . Map . Map . GetVerticesFromSectorsMarks ( true ) ;
foreach ( Vertex v in verts ) v . Selected = true ;
General . Map . Map . ClearSelectedSectors ( ) ;
General . Map . Map . ClearSelectedLinedefs ( ) ;
break ;
// Convert geometry selection to linedefs only
case SelectionType . Linedefs :
if ( InSelectionType ( source , SelectionType . Vertices ) ) MarkSelectedVertices ( true , true ) ;
if ( ! InSelectionType ( source , SelectionType . Linedefs ) ) ClearSelectedLinedefs ( ) ;
lines = General . Map . Map . LinedefsFromMarkedVertices ( false , true , false ) ;
foreach ( Linedef l in lines ) l . Selected = true ;
if ( InSelectionType ( source , SelectionType . Sectors ) )
{
foreach ( Sector s in General . Map . Map . Sectors )
{
if ( s . Selected )
{
foreach ( Sidedef sd in s . Sidedefs )
sd . Line . Selected = true ;
}
}
}
General . Map . Map . ClearSelectedSectors ( ) ;
General . Map . Map . ClearSelectedVertices ( ) ;
break ;
// Convert geometry selection to sectors only
case SelectionType . Sectors :
if ( InSelectionType ( source , SelectionType . Vertices ) ) MarkSelectedVertices ( true , true ) ;
if ( ! InSelectionType ( source , SelectionType . Linedefs ) ) ClearSelectedLinedefs ( ) ;
lines = LinedefsFromMarkedVertices ( false , true , false ) ;
foreach ( Linedef l in lines ) l . Selected = true ;
ClearMarkedSectors ( true ) ;
foreach ( Linedef l in linedefs )
{
if ( ! l . Selected )
{
if ( l . Front ! = null ) l . Front . Sector . Marked = false ;
if ( l . Back ! = null ) l . Back . Sector . Marked = false ;
}
}
ClearSelectedLinedefs ( ) ;
ClearSelectedVertices ( ) ;
if ( InSelectionType ( source , SelectionType . Sectors ) )
{
foreach ( Sector s in General . Map . Map . Sectors )
{
if ( s . Marked | | s . Selected )
{
s . Selected = true ;
foreach ( Sidedef sd in s . Sidedefs )
sd . Line . Selected = true ;
}
}
}
else
{
foreach ( Sector s in General . Map . Map . Sectors )
{
if ( s . Marked )
{
s . Selected = true ;
foreach ( Sidedef sd in s . Sidedefs )
sd . Line . Selected = true ;
}
else
{
s . Selected = false ;
}
}
}
break ;
default :
throw new ArgumentException ( "Unsupported selection target conversion" ) ;
break ;
}
// New selection type
sel_type = target ;
}
2007-12-01 18:29:58 +00:00
// This clears all selected items
public void ClearAllSelected ( )
{
ClearSelectedVertices ( ) ;
ClearSelectedThings ( ) ;
ClearSelectedLinedefs ( ) ;
ClearSelectedSectors ( ) ;
}
// This clears selected vertices
public void ClearSelectedVertices ( )
{
2009-03-30 07:45:39 +00:00
sel_vertices . Clear ( ) ;
2007-12-01 18:29:58 +00:00
foreach ( Vertex v in vertices ) v . Selected = false ;
}
// This clears selected things
public void ClearSelectedThings ( )
{
2009-03-30 07:45:39 +00:00
sel_things . Clear ( ) ;
2007-12-01 18:29:58 +00:00
foreach ( Thing t in things ) t . Selected = false ;
}
// This clears selected linedefs
public void ClearSelectedLinedefs ( )
{
2009-03-30 07:45:39 +00:00
sel_linedefs . Clear ( ) ;
2007-12-01 18:29:58 +00:00
foreach ( Linedef l in linedefs ) l . Selected = false ;
}
// This clears selected sectors
public void ClearSelectedSectors ( )
{
2009-03-30 07:45:39 +00:00
sel_sectors . Clear ( ) ;
2007-12-01 18:29:58 +00:00
foreach ( Sector s in sectors ) s . Selected = false ;
}
2008-05-15 12:54:02 +00:00
// Returns a collection of vertices that match a selected state
public ICollection < Vertex > GetSelectedVertices ( bool selected )
{
2009-03-30 07:45:39 +00:00
if ( selected )
{
2009-04-02 08:18:09 +00:00
return new List < Vertex > ( sel_vertices ) ;
2009-03-30 07:45:39 +00:00
}
else
{
List < Vertex > list = new List < Vertex > ( vertices . Count - sel_vertices . Count ) ;
foreach ( Vertex v in vertices ) if ( ! v . Selected ) list . Add ( v ) ;
return list ;
}
2008-05-15 12:54:02 +00:00
}
2008-05-01 14:10:38 +00:00
// Returns a collection of things that match a selected state
2008-05-15 12:54:02 +00:00
public ICollection < Thing > GetSelectedThings ( bool selected )
2008-05-01 14:10:38 +00:00
{
2009-03-30 07:45:39 +00:00
if ( selected )
{
2009-04-02 08:18:09 +00:00
return new List < Thing > ( sel_things ) ;
2009-03-30 07:45:39 +00:00
}
else
{
List < Thing > list = new List < Thing > ( things . Count - sel_things . Count ) ;
foreach ( Thing t in things ) if ( ! t . Selected ) list . Add ( t ) ;
return list ;
}
2008-05-01 14:10:38 +00:00
}
// Returns a collection of linedefs that match a selected state
2008-05-15 12:54:02 +00:00
public ICollection < Linedef > GetSelectedLinedefs ( bool selected )
2008-05-01 14:10:38 +00:00
{
2009-03-30 07:45:39 +00:00
if ( selected )
{
2009-04-02 08:18:09 +00:00
return new List < Linedef > ( sel_linedefs ) ;
2009-03-30 07:45:39 +00:00
}
else
{
List < Linedef > list = new List < Linedef > ( linedefs . Count - sel_linedefs . Count ) ;
foreach ( Linedef l in linedefs ) if ( ! l . Selected ) list . Add ( l ) ;
return list ;
}
2008-05-01 14:10:38 +00:00
}
// Returns a collection of sectors that match a selected state
2008-05-15 12:54:02 +00:00
public ICollection < Sector > GetSelectedSectors ( bool selected )
2008-05-01 14:10:38 +00:00
{
2009-03-30 07:45:39 +00:00
if ( selected )
{
2009-04-02 08:18:09 +00:00
return new List < Sector > ( sel_sectors ) ;
2009-03-30 07:45:39 +00:00
}
else
{
List < Sector > list = new List < Sector > ( sectors . Count - sel_sectors . Count ) ;
foreach ( Sector s in sectors ) if ( ! s . Selected ) list . Add ( s ) ;
return list ;
}
2008-05-01 14:10:38 +00:00
}
2008-09-22 18:27:50 +00:00
// This selects geometry based on the marking
public void SelectMarkedGeometry ( bool mark , bool select )
{
SelectMarkedVertices ( mark , select ) ;
SelectMarkedLinedefs ( mark , select ) ;
SelectMarkedSectors ( mark , select ) ;
SelectMarkedThings ( mark , select ) ;
}
// This selects geometry based on the marking
public void SelectMarkedVertices ( bool mark , bool select )
{
foreach ( Vertex v in vertices ) if ( v . Marked = = mark ) v . Selected = select ;
}
// This selects geometry based on the marking
public void SelectMarkedLinedefs ( bool mark , bool select )
{
foreach ( Linedef l in linedefs ) if ( l . Marked = = mark ) l . Selected = select ;
}
// This selects geometry based on the marking
public void SelectMarkedSectors ( bool mark , bool select )
{
foreach ( Sector s in sectors ) if ( s . Marked = = mark ) s . Selected = select ;
}
// This selects geometry based on the marking
public void SelectMarkedThings ( bool mark , bool select )
{
foreach ( Thing t in things ) if ( t . Marked = = mark ) t . Selected = select ;
}
2008-10-16 09:33:35 +00:00
// This selects geometry by group
public void SelectVerticesByGroup ( int groupmask )
{
foreach ( SelectableElement e in vertices ) e . SelectByGroup ( groupmask ) ;
}
// This selects geometry by group
public void SelectLinedefsByGroup ( int groupmask )
{
foreach ( SelectableElement e in linedefs ) e . SelectByGroup ( groupmask ) ;
}
// This selects geometry by group
public void SelectSectorsByGroup ( int groupmask )
{
foreach ( SelectableElement e in sectors ) e . SelectByGroup ( groupmask ) ;
}
2008-09-22 18:27:50 +00:00
2008-10-16 09:33:35 +00:00
// This selects geometry by group
public void SelectThingsByGroup ( int groupmask )
{
foreach ( SelectableElement e in things ) e . SelectByGroup ( groupmask ) ;
}
// This adds the current selection to a group
public void AddSelectionToGroup ( int groupmask )
{
foreach ( SelectableElement e in vertices )
if ( e . Selected ) e . AddToGroup ( groupmask ) ;
foreach ( SelectableElement e in linedefs )
if ( e . Selected ) e . AddToGroup ( groupmask ) ;
foreach ( SelectableElement e in sectors )
if ( e . Selected ) e . AddToGroup ( groupmask ) ;
foreach ( SelectableElement e in things )
if ( e . Selected ) e . AddToGroup ( groupmask ) ;
}
2008-05-01 14:10:38 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Marking
// This clears all marks
2008-09-22 18:27:50 +00:00
public void ClearAllMarks ( bool mark )
2008-05-01 14:10:38 +00:00
{
2008-09-22 18:27:50 +00:00
ClearMarkedVertices ( mark ) ;
ClearMarkedThings ( mark ) ;
ClearMarkedLinedefs ( mark ) ;
ClearMarkedSectors ( mark ) ;
ClearMarkedSidedefs ( mark ) ;
2008-05-01 14:10:38 +00:00
}
// This clears marked vertices
public void ClearMarkedVertices ( bool mark )
{
foreach ( Vertex v in vertices ) v . Marked = mark ;
}
// This clears marked things
public void ClearMarkedThings ( bool mark )
{
foreach ( Thing t in things ) t . Marked = mark ;
}
// This clears marked linedefs
public void ClearMarkedLinedefs ( bool mark )
{
foreach ( Linedef l in linedefs ) l . Marked = mark ;
}
2008-05-01 19:31:49 +00:00
// This clears marked sidedefs
public void ClearMarkedSidedefs ( bool mark )
{
foreach ( Sidedef s in sidedefs ) s . Marked = mark ;
}
2008-05-01 14:10:38 +00:00
// This clears marked sectors
public void ClearMarkedSectors ( bool mark )
{
foreach ( Sector s in sectors ) s . Marked = mark ;
}
2008-09-22 18:27:50 +00:00
// This inverts all marks
public void InvertAllMarks ( )
{
InvertMarkedVertices ( ) ;
InvertMarkedThings ( ) ;
InvertMarkedLinedefs ( ) ;
InvertMarkedSectors ( ) ;
InvertMarkedSidedefs ( ) ;
}
// This inverts marked vertices
public void InvertMarkedVertices ( )
{
foreach ( Vertex v in vertices ) v . Marked = ! v . Marked ;
}
// This inverts marked things
public void InvertMarkedThings ( )
{
foreach ( Thing t in things ) t . Marked = ! t . Marked ;
}
// This inverts marked linedefs
public void InvertMarkedLinedefs ( )
{
foreach ( Linedef l in linedefs ) l . Marked = ! l . Marked ;
}
// This inverts marked sidedefs
public void InvertMarkedSidedefs ( )
{
foreach ( Sidedef s in sidedefs ) s . Marked = ! s . Marked ;
}
// This inverts marked sectors
public void InvertMarkedSectors ( )
{
foreach ( Sector s in sectors ) s . Marked = ! s . Marked ;
}
2008-05-01 14:10:38 +00:00
// Returns a collection of vertices that match a marked state
2008-05-05 18:21:13 +00:00
public List < Vertex > GetMarkedVertices ( bool mark )
2007-12-01 18:29:58 +00:00
{
2008-04-17 05:36:48 +00:00
List < Vertex > list = new List < Vertex > ( vertices . Count > > 1 ) ;
2008-05-01 14:10:38 +00:00
foreach ( Vertex v in vertices ) if ( v . Marked = = mark ) list . Add ( v ) ;
return list ;
}
// Returns a collection of things that match a marked state
2008-05-05 18:21:13 +00:00
public List < Thing > GetMarkedThings ( bool mark )
2008-05-01 14:10:38 +00:00
{
List < Thing > list = new List < Thing > ( things . Count > > 1 ) ;
foreach ( Thing t in things ) if ( t . Marked = = mark ) list . Add ( t ) ;
2007-12-01 18:29:58 +00:00
return list ;
}
2008-05-01 14:10:38 +00:00
// Returns a collection of linedefs that match a marked state
2008-05-05 18:21:13 +00:00
public List < Linedef > GetMarkedLinedefs ( bool mark )
2008-05-01 14:10:38 +00:00
{
List < Linedef > list = new List < Linedef > ( linedefs . Count > > 1 ) ;
foreach ( Linedef l in linedefs ) if ( l . Marked = = mark ) list . Add ( l ) ;
return list ;
}
2008-09-22 18:27:50 +00:00
// Returns a collection of sidedefs that match a marked state
public List < Sidedef > GetMarkedSidedefs ( bool mark )
{
List < Sidedef > list = new List < Sidedef > ( sidedefs . Count > > 1 ) ;
foreach ( Sidedef s in sidedefs ) if ( s . Marked = = mark ) list . Add ( s ) ;
return list ;
}
2008-05-01 14:10:38 +00:00
// Returns a collection of sectors that match a marked state
2008-05-05 18:21:13 +00:00
public List < Sector > GetMarkedSectors ( bool mark )
2008-05-01 14:10:38 +00:00
{
List < Sector > list = new List < Sector > ( sectors . Count > > 1 ) ;
foreach ( Sector s in sectors ) if ( s . Marked = = mark ) list . Add ( s ) ;
return list ;
}
// This creates a marking from selection
public void MarkSelectedVertices ( bool selected , bool mark )
{
2008-05-01 19:31:49 +00:00
foreach ( Vertex v in vertices ) if ( v . Selected = = selected ) v . Marked = mark ;
2008-05-01 14:10:38 +00:00
}
// This creates a marking from selection
public void MarkSelectedLinedefs ( bool selected , bool mark )
{
2008-05-01 19:31:49 +00:00
foreach ( Linedef l in linedefs ) if ( l . Selected = = selected ) l . Marked = mark ;
2008-05-01 14:10:38 +00:00
}
// This creates a marking from selection
public void MarkSelectedSectors ( bool selected , bool mark )
{
2008-05-01 19:31:49 +00:00
foreach ( Sector s in sectors ) if ( s . Selected = = selected ) s . Marked = mark ;
2008-05-01 14:10:38 +00:00
}
// This creates a marking from selection
public void MarkSelectedThings ( bool selected , bool mark )
{
2008-05-01 19:31:49 +00:00
foreach ( Thing t in things ) if ( t . Selected = = selected ) t . Marked = mark ;
}
/// <summary>
/// This marks the front and back sidedefs on linedefs with the matching mark
/// </summary>
public void MarkSidedefsFromLinedefs ( bool matchmark , bool setmark )
{
foreach ( Linedef l in linedefs )
{
if ( l . Marked = = matchmark )
{
if ( l . Front ! = null ) l . Front . Marked = setmark ;
if ( l . Back ! = null ) l . Back . Marked = setmark ;
}
}
2008-05-01 14:10:38 +00:00
}
2008-09-22 18:27:50 +00:00
/// <summary>
/// This marks the sidedefs that make up the sectors with the matching mark
/// </summary>
public void MarkSidedefsFromSectors ( bool matchmark , bool setmark )
{
foreach ( Sidedef sd in sidedefs )
{
if ( sd . Sector . Marked = = matchmark ) sd . Marked = setmark ;
}
}
2008-05-01 14:10:38 +00:00
/// <summary>
/// Returns a collection of vertices that match a marked state on the linedefs
/// </summary>
public ICollection < Vertex > GetVerticesFromLinesMarks ( bool mark )
2007-12-26 00:31:32 +00:00
{
2008-04-17 05:36:48 +00:00
List < Vertex > list = new List < Vertex > ( vertices . Count > > 1 ) ;
2007-12-26 00:31:32 +00:00
foreach ( Vertex v in vertices )
{
foreach ( Linedef l in v . Linedefs )
{
2008-05-01 14:10:38 +00:00
if ( l . Marked = = mark )
2007-12-26 00:31:32 +00:00
{
list . Add ( v ) ;
break ;
}
}
}
return list ;
}
2008-05-01 14:10:38 +00:00
/// <summary>
/// Returns a collection of vertices that match a marked state on the linedefs
/// The difference with GetVerticesFromLinesMarks is that in this method
/// ALL linedefs of a vertex must match the specified marked state.
/// </summary>
public ICollection < Vertex > GetVerticesFromAllLinesMarks ( bool mark )
2008-02-19 18:54:04 +00:00
{
2008-04-17 05:36:48 +00:00
List < Vertex > list = new List < Vertex > ( vertices . Count > > 1 ) ;
2008-02-19 18:54:04 +00:00
foreach ( Vertex v in vertices )
{
bool qualified = true ;
foreach ( Linedef l in v . Linedefs )
{
2008-05-01 14:10:38 +00:00
if ( l . Marked ! = mark )
2008-02-19 18:54:04 +00:00
{
qualified = false ;
break ;
}
}
if ( qualified ) list . Add ( v ) ;
}
return list ;
}
2008-05-01 14:10:38 +00:00
/// <summary>
/// Returns a collection of vertices that match a marked state on the linedefs
/// </summary>
public ICollection < Vertex > GetVerticesFromSectorsMarks ( bool mark )
2007-12-26 00:31:32 +00:00
{
2008-04-17 05:36:48 +00:00
List < Vertex > list = new List < Vertex > ( vertices . Count > > 1 ) ;
2007-12-26 00:31:32 +00:00
foreach ( Vertex v in vertices )
{
foreach ( Linedef l in v . Linedefs )
{
2008-05-01 14:10:38 +00:00
if ( ( ( l . Front ! = null ) & & ( l . Front . Sector . Marked = = mark ) ) | |
( ( l . Back ! = null ) & & ( l . Back . Sector . Marked = = mark ) ) )
2007-12-26 00:31:32 +00:00
{
list . Add ( v ) ;
break ;
}
}
}
return list ;
}
2008-09-22 18:27:50 +00:00
2008-09-23 17:46:34 +00:00
/// <summary>
/// This marks all selected geometry, including sidedefs from sectors.
/// When sidedefsfromsectors is true, then the sidedefs are marked according to the
/// marked sectors. Otherwise the sidedefs are marked according to the marked linedefs.
/// </summary>
2009-04-18 09:58:09 +00:00
public void MarkAllSelectedGeometry ( bool mark , bool linedefsfromvertices , bool verticesfromlinedefs , bool sectorsfromlinedefs , bool sidedefsfromsectors )
2008-09-22 18:27:50 +00:00
{
General . Map . Map . ClearAllMarks ( ! mark ) ;
// Direct vertices
General . Map . Map . MarkSelectedVertices ( true , mark ) ;
// Direct linedefs
General . Map . Map . MarkSelectedLinedefs ( true , mark ) ;
// Linedefs from vertices
2008-09-23 17:46:34 +00:00
// We do this before "vertices from lines" because otherwise we get lines marked that we didn't select
2009-04-18 09:58:09 +00:00
if ( linedefsfromvertices )
{
ICollection < Linedef > lines = General . Map . Map . LinedefsFromMarkedVertices ( ! mark , mark , ! mark ) ;
foreach ( Linedef l in lines ) l . Marked = mark ;
}
2008-09-23 17:46:34 +00:00
// Vertices from linedefs
2009-04-18 09:58:09 +00:00
if ( verticesfromlinedefs )
{
ICollection < Vertex > verts = General . Map . Map . GetVerticesFromLinesMarks ( mark ) ;
foreach ( Vertex v in verts ) v . Marked = mark ;
}
2008-09-22 18:27:50 +00:00
// Mark sectors from linedefs (note: this must be the first to mark
// sectors, because this clears the sector marks!)
2009-04-18 09:58:09 +00:00
if ( sectorsfromlinedefs )
2008-09-22 18:27:50 +00:00
{
2009-04-18 09:58:09 +00:00
General . Map . Map . ClearMarkedSectors ( mark ) ;
foreach ( Linedef l in General . Map . Map . Linedefs )
2008-09-22 18:27:50 +00:00
{
2009-04-18 09:58:09 +00:00
if ( ! l . Selected )
{
if ( l . Front ! = null ) l . Front . Sector . Marked = ! mark ;
if ( l . Back ! = null ) l . Back . Sector . Marked = ! mark ;
}
2008-09-22 18:27:50 +00:00
}
}
2009-04-18 09:58:09 +00:00
2008-09-22 18:27:50 +00:00
// Direct sectors
General . Map . Map . MarkSelectedSectors ( true , mark ) ;
// Direct things
General . Map . Map . MarkSelectedThings ( true , mark ) ;
2008-09-23 17:46:34 +00:00
// Sidedefs from linedefs or sectors
if ( sidedefsfromsectors )
General . Map . Map . MarkSidedefsFromSectors ( true , mark ) ;
else
General . Map . Map . MarkSidedefsFromLinedefs ( true , mark ) ;
2008-09-22 18:27:50 +00:00
}
2008-05-01 14:10:38 +00:00
2007-12-01 18:29:58 +00:00
#endregion
2009-03-09 16:00:22 +00:00
#region = = = = = = = = = = = = = = = = = = Indexing
2009-01-26 14:09:11 +00:00
2009-03-09 16:00:22 +00:00
/// <summary>
/// Returns the vertex at the specified index. Returns null when index is out of range. This is a O(n) operation.
/// </summary>
public Vertex GetVertexByIndex ( int index )
{
if ( index < vertices . Count )
return General . GetByIndex < Vertex > ( vertices , index ) ;
else
return null ;
}
/// <summary>
/// Returns the linedef at the specified index. Returns null when index is out of range. This is a O(n) operation.
/// </summary>
public Linedef GetLinedefByIndex ( int index )
{
if ( index < linedefs . Count )
return General . GetByIndex < Linedef > ( linedefs , index ) ;
else
return null ;
}
/// <summary>
/// Returns the sidedef at the specified index. Returns null when index is out of range. This is a O(n) operation.
/// </summary>
public Sidedef GetSidedefByIndex ( int index )
{
if ( index < sidedefs . Count )
return General . GetByIndex < Sidedef > ( sidedefs , index ) ;
else
return null ;
}
/// <summary>
/// Returns the sector at the specified index. Returns null when index is out of range. This is a O(n) operation.
/// </summary>
public Sector GetSectorByIndex ( int index )
{
if ( index < sectors . Count )
return General . GetByIndex < Sector > ( sectors , index ) ;
else
return null ;
}
/// <summary>
/// Returns the thing at the specified index. Returns null when index is out of range. This is a O(n) operation.
/// </summary>
2009-03-10 06:46:02 +00:00
public Thing GetThingByIndex ( int index )
2009-03-09 16:00:22 +00:00
{
if ( index < things . Count )
return General . GetByIndex < Thing > ( things , index ) ;
else
return null ;
}
/// <summary>
/// Returns the index of the specified vertex. Returns -1 when the vertex is not in this map. This is a O(n) operation.
/// </summary>
public int GetIndexForVertex ( Vertex v )
{
int index = 0 ;
2009-03-10 06:46:02 +00:00
foreach ( Vertex vn in vertices )
2009-03-09 16:00:22 +00:00
{
if ( object . ReferenceEquals ( vn , v ) ) return index ;
index + + ;
}
return - 1 ;
}
/// <summary>
/// Returns the index of the specified linedef. Returns -1 when the linedef is not in this map. This is a O(n) operation.
/// </summary>
public int GetIndexForLinedef ( Linedef l )
{
int index = 0 ;
2009-03-10 06:46:02 +00:00
foreach ( Linedef ln in linedefs )
2009-03-09 16:00:22 +00:00
{
if ( object . ReferenceEquals ( ln , l ) ) return index ;
index + + ;
}
return - 1 ;
}
/// <summary>
/// Returns the index of the specified sidedef. Returns -1 when the sidedef is not in this map. This is a O(n) operation.
/// </summary>
public int GetIndexForSidedef ( Sidedef sd )
{
int index = 0 ;
2009-03-10 06:46:02 +00:00
foreach ( Sidedef sn in sidedefs )
2009-03-09 16:00:22 +00:00
{
if ( object . ReferenceEquals ( sn , sd ) ) return index ;
index + + ;
}
return - 1 ;
}
/// <summary>
/// Returns the index of the specified sector. Returns -1 when the sector is not in this map. This is a O(n) operation.
/// </summary>
public int GetIndexForSector ( Sector s )
{
int index = 0 ;
2009-03-10 06:46:02 +00:00
foreach ( Sector sn in sectors )
2009-03-09 16:00:22 +00:00
{
if ( object . ReferenceEquals ( sn , s ) ) return index ;
index + + ;
}
return - 1 ;
}
/// <summary>
/// Returns the index of the specified thing. Returns -1 when the thing is not in this map. This is a O(n) operation.
/// </summary>
public int GetIndexForThing ( Thing t )
{
int index = 0 ;
2009-03-10 06:46:02 +00:00
foreach ( Thing tn in things )
2009-03-09 16:00:22 +00:00
{
if ( object . ReferenceEquals ( tn , t ) ) return index ;
index + + ;
}
return - 1 ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Areas
2009-01-26 14:09:11 +00:00
// This creates an initial, undefined area
public static RectangleF CreateEmptyArea ( )
{
return new RectangleF ( float . MaxValue / 2 , float . MaxValue / 2 , - float . MaxValue , - float . MaxValue ) ;
}
2007-12-05 19:39:09 +00:00
// This creates an area from vertices
2008-05-05 22:01:27 +00:00
public static RectangleF CreateArea ( ICollection < Vertex > verts )
2007-12-05 19:39:09 +00:00
{
2008-05-05 22:01:27 +00:00
float l = float . MaxValue ;
float t = float . MaxValue ;
float r = float . MinValue ;
float b = float . MinValue ;
2007-12-05 19:39:09 +00:00
// Go for all vertices
foreach ( Vertex v in verts )
{
// Adjust boundaries by vertices
2008-05-05 22:01:27 +00:00
if ( v . Position . x < l ) l = v . Position . x ;
if ( v . Position . x > r ) r = v . Position . x ;
if ( v . Position . y < t ) t = v . Position . y ;
if ( v . Position . y > b ) b = v . Position . y ;
2007-12-05 19:39:09 +00:00
}
// Return a rect
2008-05-05 22:01:27 +00:00
return new RectangleF ( l , t , r - l , b - t ) ;
2007-12-05 19:39:09 +00:00
}
2007-12-07 15:48:32 +00:00
2009-01-17 14:40:12 +00:00
// This increases and existing area with the given vertices
public static RectangleF IncreaseArea ( RectangleF area , ICollection < Vertex > verts )
{
float l = area . Left ;
float t = area . Top ;
float r = area . Right ;
float b = area . Bottom ;
// Go for all vertices
foreach ( Vertex v in verts )
{
// Adjust boundaries by vertices
if ( v . Position . x < l ) l = v . Position . x ;
if ( v . Position . x > r ) r = v . Position . x ;
if ( v . Position . y < t ) t = v . Position . y ;
if ( v . Position . y > b ) b = v . Position . y ;
}
// Return a rect
return new RectangleF ( l , t , r - l , b - t ) ;
}
2009-01-30 16:14:11 +00:00
// This increases and existing area with the given vertices
public static RectangleF IncreaseArea ( RectangleF area , ICollection < Thing > things )
{
float l = area . Left ;
float t = area . Top ;
float r = area . Right ;
float b = area . Bottom ;
// Go for all vertices
foreach ( Thing th in things )
{
// Adjust boundaries by vertices
if ( th . Position . x < l ) l = th . Position . x ;
if ( th . Position . x > r ) r = th . Position . x ;
if ( th . Position . y < t ) t = th . Position . y ;
if ( th . Position . y > b ) b = th . Position . y ;
}
// Return a rect
return new RectangleF ( l , t , r - l , b - t ) ;
}
2009-01-26 14:09:11 +00:00
// This increases and existing area with the given vertices
public static RectangleF IncreaseArea ( RectangleF area , ICollection < Vector2D > verts )
{
float l = area . Left ;
float t = area . Top ;
float r = area . Right ;
float b = area . Bottom ;
// Go for all vertices
foreach ( Vector2D v in verts )
{
// Adjust boundaries by vertices
if ( v . x < l ) l = v . x ;
if ( v . x > r ) r = v . x ;
if ( v . y < t ) t = v . y ;
if ( v . y > b ) b = v . y ;
}
// Return a rect
return new RectangleF ( l , t , r - l , b - t ) ;
}
// This increases and existing area with the given vertices
public static RectangleF IncreaseArea ( RectangleF area , Vector2D vert )
{
float l = area . Left ;
float t = area . Top ;
float r = area . Right ;
float b = area . Bottom ;
// Adjust boundaries by vertices
if ( vert . x < l ) l = vert . x ;
if ( vert . x > r ) r = vert . x ;
if ( vert . y < t ) t = vert . y ;
if ( vert . y > b ) b = vert . y ;
// Return a rect
return new RectangleF ( l , t , r - l , b - t ) ;
}
2007-12-26 00:31:32 +00:00
// This creates an area from linedefs
2008-05-05 22:01:27 +00:00
public static RectangleF CreateArea ( ICollection < Linedef > lines )
2007-12-07 15:48:32 +00:00
{
2008-05-05 22:01:27 +00:00
float l = float . MaxValue ;
float t = float . MaxValue ;
float r = float . MinValue ;
float b = float . MinValue ;
2007-12-07 15:48:32 +00:00
2007-12-26 00:31:32 +00:00
// Go for all linedefs
2007-12-07 15:48:32 +00:00
foreach ( Linedef ld in lines )
{
// Adjust boundaries by vertices
2008-05-05 22:01:27 +00:00
if ( ld . Start . Position . x < l ) l = ld . Start . Position . x ;
if ( ld . Start . Position . x > r ) r = ld . Start . Position . x ;
if ( ld . Start . Position . y < t ) t = ld . Start . Position . y ;
if ( ld . Start . Position . y > b ) b = ld . Start . Position . y ;
if ( ld . End . Position . x < l ) l = ld . End . Position . x ;
if ( ld . End . Position . x > r ) r = ld . End . Position . x ;
if ( ld . End . Position . y < t ) t = ld . End . Position . y ;
if ( ld . End . Position . y > b ) b = ld . End . Position . y ;
2007-12-07 15:48:32 +00:00
}
// Return a rect
2008-05-05 22:01:27 +00:00
return new RectangleF ( l , t , r - l , b - t ) ;
2007-12-07 15:48:32 +00:00
}
2007-12-05 19:39:09 +00:00
// This filters lines by a square area
2008-05-05 22:01:27 +00:00
public static ICollection < Linedef > FilterByArea ( ICollection < Linedef > lines , ref RectangleF area )
2007-12-05 19:39:09 +00:00
{
ICollection < Linedef > newlines = new List < Linedef > ( lines . Count ) ;
// Go for all lines
foreach ( Linedef l in lines )
{
// Check the cs field bits
2007-12-07 15:48:32 +00:00
if ( ( GetCSFieldBits ( l . Start , ref area ) & GetCSFieldBits ( l . End , ref area ) ) = = 0 )
2007-12-05 19:39:09 +00:00
{
// The line could be in the area
newlines . Add ( l ) ;
}
}
// Return result
return newlines ;
}
// This returns the cohen-sutherland field bits for a vertex in a rectangle area
2008-05-05 22:01:27 +00:00
private static int GetCSFieldBits ( Vertex v , ref RectangleF area )
2007-12-05 19:39:09 +00:00
{
2008-01-13 21:23:59 +00:00
int bits = 0 ;
2008-05-05 22:01:27 +00:00
if ( v . Position . y < area . Top ) bits | = 0x01 ;
if ( v . Position . y > area . Bottom ) bits | = 0x02 ;
if ( v . Position . x < area . Left ) bits | = 0x04 ;
if ( v . Position . x > area . Right ) bits | = 0x08 ;
2007-12-05 19:39:09 +00:00
return bits ;
}
// This filters vertices by a square area
2008-05-05 22:01:27 +00:00
public static ICollection < Vertex > FilterByArea ( ICollection < Vertex > verts , ref RectangleF area )
2007-12-05 19:39:09 +00:00
{
ICollection < Vertex > newverts = new List < Vertex > ( verts . Count ) ;
// Go for all verts
foreach ( Vertex v in verts )
{
// Within rect?
2008-05-05 22:01:27 +00:00
if ( ( v . Position . x > = area . Left ) & &
( v . Position . x < = area . Right ) & &
( v . Position . y > = area . Top ) & &
( v . Position . y < = area . Bottom ) )
2007-12-05 19:39:09 +00:00
{
// The vertex is in the area
newverts . Add ( v ) ;
}
}
// Return result
return newverts ;
}
#endregion
2007-12-26 00:31:32 +00:00
#region = = = = = = = = = = = = = = = = = = Stitching
2008-05-01 14:10:38 +00:00
/// <summary>
/// Stitches marked geometry with non-marked geometry. Returns the number of stitches made.
/// </summary>
public int StitchGeometry ( )
2007-12-26 00:31:32 +00:00
{
ICollection < Linedef > movinglines ;
ICollection < Linedef > fixedlines ;
ICollection < Vertex > nearbyfixedverts ;
2008-05-01 14:10:38 +00:00
ICollection < Vertex > movingverts ;
ICollection < Vertex > fixedverts ;
2008-05-05 22:01:27 +00:00
RectangleF editarea ;
2007-12-26 00:31:32 +00:00
int stitches = 0 ;
int stitchundo ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Find vertices
movingverts = General . Map . Map . GetMarkedVertices ( true ) ;
fixedverts = General . Map . Map . GetMarkedVertices ( false ) ;
2007-12-26 00:31:32 +00:00
2008-05-01 14:10:38 +00:00
// Find lines that moved during the drag
movinglines = LinedefsFromMarkedVertices ( false , true , true ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Find all non-moving lines
fixedlines = LinedefsFromMarkedVertices ( true , false , false ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Determine area in which we are editing
editarea = MapSet . CreateArea ( movinglines ) ;
2009-01-17 14:40:12 +00:00
editarea = MapSet . IncreaseArea ( editarea , movingverts ) ;
editarea . Inflate ( 1.0f , 1.0f ) ;
2008-05-01 14:10:38 +00:00
// Join nearby vertices
2008-05-06 05:41:36 +00:00
stitches + = MapSet . JoinVertices ( fixedverts , movingverts , true , MapSet . STITCH_DISTANCE ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Update cached values of lines because we need their length/angle
Update ( true , false ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Split moving lines with unselected vertices
nearbyfixedverts = MapSet . FilterByArea ( fixedverts , ref editarea ) ;
2008-05-06 05:41:36 +00:00
stitches + = MapSet . SplitLinesByVertices ( movinglines , nearbyfixedverts , MapSet . STITCH_DISTANCE , movinglines ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Split non-moving lines with selected vertices
fixedlines = MapSet . FilterByArea ( fixedlines , ref editarea ) ;
2008-05-06 05:41:36 +00:00
stitches + = MapSet . SplitLinesByVertices ( fixedlines , movingverts , MapSet . STITCH_DISTANCE , movinglines ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Remove looped linedefs
stitches + = MapSet . RemoveLoopedLinedefs ( movinglines ) ;
2009-01-17 14:40:12 +00:00
2008-05-01 14:10:38 +00:00
// Join overlapping lines
stitches + = MapSet . JoinOverlappingLines ( movinglines ) ;
2009-01-17 14:40:12 +00:00
2007-12-26 00:31:32 +00:00
return stitches ;
}
#endregion
2007-12-05 19:39:09 +00:00
#region = = = = = = = = = = = = = = = = = = Geometry Tools
2007-06-14 12:37:46 +00:00
2008-09-23 17:46:34 +00:00
// This removes any virtual sectors in the map
// Returns the number of sectors removed
public int RemoveVirtualSectors ( )
{
int count = 0 ;
LinkedListNode < Sector > n = sectors . First ;
// Go for all sectors
while ( n ! = null )
{
LinkedListNode < Sector > nn = n . Next ;
// Remove when virtual
if ( n . Value . Fields . ContainsKey ( VIRTUAL_SECTOR_FIELD ) )
{
n . Value . Dispose ( ) ;
count + + ;
}
n = nn ;
}
return count ;
}
2007-12-01 18:29:58 +00:00
// This joins overlapping lines together
// Returns the number of joins made
public static int JoinOverlappingLines ( ICollection < Linedef > lines )
{
int joinsdone = 0 ;
bool joined ;
do
{
// No joins yet
joined = false ;
// Go for all the lines
foreach ( Linedef l1 in lines )
{
2007-12-04 19:22:14 +00:00
// Check if these vertices have lines that overlap
foreach ( Linedef l2 in l1 . Start . Linedefs )
2007-12-01 18:29:58 +00:00
{
// Sharing vertices?
2007-12-04 19:22:14 +00:00
if ( ( l1 . End = = l2 . End ) | |
( l1 . End = = l2 . Start ) )
2007-12-01 18:29:58 +00:00
{
// Not the same line?
if ( l1 ! = l2 )
{
// Merge these two linedefs
2008-05-01 19:31:49 +00:00
//while(lines.Remove(l1));
//l1.Join(l2);
while ( lines . Remove ( l2 ) ) ;
l2 . Join ( l1 ) ;
2007-12-04 19:22:14 +00:00
joinsdone + + ;
joined = true ;
break ;
}
}
}
// Will have to restart when joined
if ( joined ) break ;
// Check if these vertices have lines that overlap
foreach ( Linedef l2 in l1 . End . Linedefs )
{
// Sharing vertices?
if ( ( l1 . Start = = l2 . End ) | |
( l1 . Start = = l2 . Start ) )
{
// Not the same line?
if ( l1 ! = l2 )
{
// Merge these two linedefs
2008-05-01 19:31:49 +00:00
//while(lines.Remove(l1));
//l1.Join(l2);
while ( lines . Remove ( l2 ) ) ;
l2 . Join ( l1 ) ;
2007-12-01 18:29:58 +00:00
joinsdone + + ;
joined = true ;
break ;
}
}
}
// Will have to restart when joined
if ( joined ) break ;
}
}
while ( joined ) ;
// Return result
return joinsdone ;
}
// This removes looped linedefs (linedefs which reference the same vertex for start and end)
// Returns the number of linedefs removed
public static int RemoveLoopedLinedefs ( ICollection < Linedef > lines )
{
int linesremoved = 0 ;
bool removedline ;
do
{
// Nothing removed yet
removedline = false ;
// Go for all the lines
foreach ( Linedef l in lines )
{
// Check if referencing the same vertex twice
if ( l . Start = = l . End )
{
// Remove this line
2007-12-04 19:22:14 +00:00
while ( lines . Remove ( l ) ) ;
2007-12-01 18:29:58 +00:00
l . Dispose ( ) ;
linesremoved + + ;
removedline = true ;
break ;
}
}
}
while ( removedline ) ;
// Return result
return linesremoved ;
}
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 ;
}
2008-05-05 18:21:13 +00:00
// This corrects lines that have a back sidedef but no front
// sidedef by flipping them. Returns the number of flips made.
public static int FlipBackwardLinedefs ( ICollection < Linedef > lines )
{
int flipsdone = 0 ;
// Examine all lines
foreach ( Linedef l in lines )
{
// Back side but no front side?
if ( ( l . Back ! = null ) & & ( l . Front = = null ) )
{
// Flip that linedef!
l . FlipVertices ( ) ;
l . FlipSidedefs ( ) ;
flipsdone + + ;
}
}
// Return result
return flipsdone ;
}
2007-12-01 01:32:56 +00:00
// This splits the given lines with the given vertices
2007-12-04 19:22:14 +00:00
// All affected lines will be added to changedlines
2007-12-01 01:32:56 +00:00
// Returns the number of splits made
2007-12-04 19:22:14 +00:00
public static int SplitLinesByVertices ( ICollection < Linedef > lines , ICollection < Vertex > verts , float splitdist , ICollection < Linedef > changedlines )
2007-12-01 01:32:56 +00:00
{
float splitdist2 = splitdist * splitdist ;
int splitsdone = 0 ;
bool splitted ;
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?
2008-05-05 22:01:27 +00:00
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 ) ) )
2007-12-01 01:32:56 +00:00
{
// Split line l with vertex v
2008-05-07 22:46:15 +00:00
Linedef nl = l . Split ( v ) ;
2007-12-01 01:32:56 +00:00
// Add the new line to the list
lines . Add ( nl ) ;
// Both lines must be updated because their new length
// is relevant for next iterations!
2008-01-13 21:23:59 +00:00
l . UpdateCache ( ) ;
nl . UpdateCache ( ) ;
2007-12-04 19:22:14 +00:00
// Add both lines to changedlines
2008-05-07 22:46:15 +00:00
if ( changedlines ! = null )
{
changedlines . Add ( l ) ;
changedlines . Add ( nl ) ;
}
2007-12-05 19:39:09 +00:00
2007-12-01 01:32:56 +00:00
// Count the split
splitsdone + + ;
splitted = true ;
break ;
}
}
}
// Will have to restart when splitted
2008-01-13 21:23:59 +00:00
// TODO: If we make (linked) lists from the collections first,
// we don't have to restart when splitted?
2007-12-01 01:32:56 +00:00
if ( splitted ) break ;
}
}
while ( splitted ) ;
// Return result
return splitsdone ;
}
2008-12-02 15:47:06 +00:00
// This finds the side closest to the specified position
public static Sidedef NearestSidedef ( ICollection < Sidedef > selection , Vector2D pos )
{
Sidedef closest = null ;
float distance = float . MaxValue ;
// Go for all sidedefs in selection
foreach ( Sidedef sd in selection )
{
// Calculate distance and check if closer than previous find
float d = sd . Line . SafeDistanceToSq ( pos , true ) ;
if ( d < distance )
{
// This one is closer
closest = sd ;
distance = d ;
}
}
// Return result
return closest ;
}
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 ;
// Go for all linedefs in selection
foreach ( Linedef l in selection )
{
// Calculate distance and check if closer than previous find
2008-11-27 06:53:38 +00:00
float 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
2007-10-21 22:41:46 +00:00
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
2008-04-07 14:33:41 +00:00
// This finds the thing closest to the specified position
public static Thing NearestThing ( ICollection < Thing > selection , Vector2D pos )
{
Thing closest = null ;
float distance = float . MaxValue ;
float d ;
// Go for all things in selection
foreach ( Thing t in selection )
{
// Calculate distance and check if closer than previous find
d = t . DistanceToSq ( pos ) ;
if ( d < distance )
{
// This one is closer
closest = t ;
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 ;
}
2007-10-23 05:08:02 +00:00
// 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
2008-05-05 22:22:53 +00:00
// This snaps all vertices to the map format accuracy
public void SnapAllToAccuracy ( )
{
foreach ( Vertex v in vertices ) v . SnapToAccuracy ( ) ;
2008-06-01 20:25:46 +00:00
foreach ( Thing t in things ) t . SnapToAccuracy ( ) ;
2008-05-05 22:22:53 +00:00
}
2007-12-27 01:24:11 +00:00
// This returns the next unused tag number
public int GetNewTag ( )
{
2009-04-08 19:11:40 +00:00
Dictionary < int , bool > usedtags = new Dictionary < int , bool > ( ) ;
2007-12-27 01:24:11 +00:00
// Check all sectors
foreach ( Sector s in sectors ) usedtags [ s . Tag ] = true ;
// Check all lines
foreach ( Linedef l in linedefs ) usedtags [ l . Tag ] = true ;
// Check all things
foreach ( Thing t in things ) usedtags [ t . Tag ] = true ;
// Now find the first unused index
2009-04-11 20:19:02 +00:00
for ( int i = 1 ; i < = General . Map . FormatInterface . MaxTag ; i + + )
2009-04-08 19:11:40 +00:00
if ( ! usedtags . ContainsKey ( i ) ) return i ;
2007-12-27 01:24:11 +00:00
// Problem: all tags used!
// Lets ignore this problem for now, who needs 65-thousand tags?!
return 0 ;
}
2009-04-08 19:11:40 +00:00
2008-05-01 14:10:38 +00:00
// This makes a list of lines related to marked vertices
// A line is unstable when one vertex is marked and the other isn't.
public ICollection < Linedef > LinedefsFromMarkedVertices ( bool includeunselected , bool includestable , bool includeunstable )
2007-12-01 01:32:56 +00:00
{
2007-12-05 19:39:09 +00:00
List < Linedef > list = new List < Linedef > ( ( linedefs . Count / 2 ) + 1 ) ;
2007-12-01 01:32:56 +00:00
// Go for all lines
foreach ( Linedef l in linedefs )
{
// Check if this is to be included
2008-05-01 14:10:38 +00:00
if ( ( includestable & & ( l . Start . Marked & & l . End . Marked ) ) | |
( includeunstable & & ( l . Start . Marked ^ l . End . Marked ) ) | |
( includeunselected & & ( ! l . Start . Marked & & ! l . End . Marked ) ) )
2007-12-01 01:32:56 +00:00
{
// Add to list
list . Add ( l ) ;
}
}
// Return result
return list ;
}
2008-02-19 18:54:04 +00:00
// This makes a list of unstable lines from the given vertices.
// A line is unstable when one vertex is selected and the other isn't.
public static ICollection < Linedef > UnstableLinedefsFromVertices ( ICollection < Vertex > verts )
{
Dictionary < Linedef , Linedef > lines = new Dictionary < Linedef , Linedef > ( ) ;
// Go for all vertices
foreach ( Vertex v in verts )
{
// Go for all lines
foreach ( Linedef l in v . Linedefs )
{
// If the line exists in the list
if ( lines . ContainsKey ( l ) )
{
// Remove it
lines . Remove ( l ) ;
}
// Otherwise add it
else
{
// Add the line
lines . Add ( l , l ) ;
}
}
}
// Return result
return new List < Linedef > ( lines . Values ) ;
}
2007-12-01 01:32:56 +00:00
2007-06-14 12:37:46 +00:00
// This finds the line closest to the specified position
2007-06-14 13:09:55 +00:00
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
2007-06-14 13:09:55 +00:00
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 ) ; }
2007-10-23 05:08:02 +00:00
// 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 ;
}
2007-10-13 14:05:45 +00:00
// This performs sidedefs compression
2009-04-13 15:21:45 +00:00
// Note: Only use this for saving, because this messes up the expected data structure horribly.
internal void CompressSidedefs ( )
2007-10-13 14:05:45 +00:00
{
2009-04-13 15:21:45 +00:00
Dictionary < uint , List < Sidedef > > storedsides = new Dictionary < uint , List < Sidedef > > ( sidedefs . Count ) ;
int originalsidescount = sidedefs . Count ;
double starttime = General . Clock . GetCurrentTime ( ) ;
LinkedListNode < Sidedef > sn = sidedefs . First ;
while ( sn ! = null )
{
Sidedef stored = null ;
LinkedListNode < Sidedef > nextsn = sn . Next ;
// Check if checksum is stored
bool samesidedef = false ;
uint checksum = sn . Value . GetChecksum ( ) ;
bool checksumstored = storedsides . ContainsKey ( checksum ) ;
if ( checksumstored )
{
List < Sidedef > othersides = storedsides [ checksum ] ;
foreach ( Sidedef os in othersides )
{
// They must be in the same sector
if ( sn . Value . Sector = = os . Sector )
{
// Check if sidedefs are really the same
stored = os ;
MemoryStream sidemem = new MemoryStream ( 1024 ) ;
SerializerStream sidedata = new SerializerStream ( sidemem ) ;
MemoryStream othermem = new MemoryStream ( 1024 ) ;
SerializerStream otherdata = new SerializerStream ( othermem ) ;
sn . Value . ReadWrite ( sidedata ) ;
os . ReadWrite ( otherdata ) ;
if ( sidemem . Length = = othermem . Length )
{
samesidedef = true ;
sidemem . Seek ( 0 , SeekOrigin . Begin ) ;
othermem . Seek ( 0 , SeekOrigin . Begin ) ;
for ( int i = 0 ; i < sidemem . Length ; i + + )
{
if ( sidemem . ReadByte ( ) ! = othermem . ReadByte ( ) )
{
samesidedef = false ;
break ;
}
}
}
if ( samesidedef ) break ;
}
}
}
// Same sidedef?
if ( samesidedef )
{
// Replace with stored sidedef
bool isfront = sn . Value . IsFront ;
sn . Value . Line . DetachSidedef ( sn . Value ) ;
if ( isfront )
sn . Value . Line . AttachFront ( stored ) ;
else
sn . Value . Line . AttachBack ( stored ) ;
// Remove the sidedef
sn . Value . ChangeSector ( null ) ;
sidedefs . Remove ( sn ) ;
}
else
{
// Store this new one
if ( checksumstored )
{
storedsides [ checksum ] . Add ( sn . Value ) ;
}
else
{
List < Sidedef > newlist = new List < Sidedef > ( 4 ) ;
newlist . Add ( sn . Value ) ;
storedsides . Add ( checksum , newlist ) ;
}
}
// Next
sn = nextsn ;
}
// Output info
double endtime = General . Clock . GetCurrentTime ( ) ;
double deltatimesec = ( endtime - starttime ) / 1000.0d ;
float ratio = 100.0f - ( ( ( float ) sidedefs . Count / ( float ) originalsidescount ) * 100.0f ) ;
General . WriteLogLine ( "Sidedefs compressed: " + sidedefs . Count + " remaining out of " + originalsidescount + " (" + ratio . ToString ( "########0.00" ) + "%) in " + deltatimesec . ToString ( "########0.00" ) + " seconds" ) ;
2007-10-13 14:05:45 +00:00
}
2008-12-31 14:08:40 +00:00
// This converts flags and activations to UDMF fields
internal void TranslateToUDMF ( )
{
foreach ( Linedef l in linedefs ) l . TranslateToUDMF ( ) ;
foreach ( Thing t in things ) t . TranslateToUDMF ( ) ;
}
// This converts UDMF fields back into flags and activations
// NOTE: Only converts the marked items
internal void TranslateFromUDMF ( )
{
foreach ( Linedef l in linedefs ) if ( l . Marked ) l . TranslateFromUDMF ( ) ;
foreach ( Thing t in things ) if ( t . Marked ) t . TranslateFromUDMF ( ) ;
}
2007-10-13 14:05:45 +00:00
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
}
}