2009-04-19 18:07:22 +00:00
#region = = = = = = = = = = = = = = = = = = Copyright ( c ) 2007 Pascal vd Heiden
/ *
* 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 .
*
* /
#endregion
#region = = = = = = = = = = = = = = = = = = Namespaces
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Globalization ;
using System.Text ;
using CodeImp.DoomBuilder.IO ;
using CodeImp.DoomBuilder.Geometry ;
using System.Drawing ;
using CodeImp.DoomBuilder.Rendering ;
using System.Collections.ObjectModel ;
using SlimDX.Direct3D9 ;
using SlimDX ;
#endregion
namespace CodeImp.DoomBuilder.Map
{
2009-06-04 20:21:31 +00:00
public sealed class Sector : SelectableElement
2009-04-19 18:07:22 +00:00
{
#region = = = = = = = = = = = = = = = = = = Constants
#endregion
#region = = = = = = = = = = = = = = = = = = Variables
// Map
private MapSet map ;
// List items
private LinkedListNode < Sector > selecteditem ;
// Sidedefs
private LinkedList < Sidedef > sidedefs ;
// Properties
private int fixedindex ;
private int floorheight ;
private int ceilheight ;
private string floortexname ;
private string ceiltexname ;
private long longfloortexname ;
private long longceiltexname ;
private int effect ;
private int tag ;
private int brightness ;
// Cloning
private Sector clone ;
private int serializedindex ;
// Triangulation
private bool updateneeded ;
private bool triangulationneeded ;
private RectangleF bbox ;
private Triangulation triangles ;
private FlatVertex [ ] flatvertices ;
private ReadOnlyCollection < LabelPositionInfo > labels ;
2009-06-04 20:21:31 +00:00
private SurfaceEntry surfaceentry ;
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Properties
public MapSet Map { get { return map ; } }
public ICollection < Sidedef > Sidedefs { get { return sidedefs ; } }
/// <summary>
/// An unique index that does not change when other sectors are removed.
/// </summary>
public int FixedIndex { get { return fixedindex ; } }
2009-06-11 21:21:20 +00:00
public int FloorHeight { get { return floorheight ; } set { BeforePropsChange ( ) ; floorheight = value ; } }
public int CeilHeight { get { return ceilheight ; } set { BeforePropsChange ( ) ; ceilheight = value ; } }
2009-04-19 18:07:22 +00:00
public string FloorTexture { get { return floortexname ; } }
public string CeilTexture { get { return ceiltexname ; } }
public long LongFloorTexture { get { return longfloortexname ; } }
public long LongCeilTexture { get { return longceiltexname ; } }
2009-06-11 21:21:20 +00:00
public int Effect { get { return effect ; } set { BeforePropsChange ( ) ; effect = value ; } }
public int Tag { get { return tag ; } set { BeforePropsChange ( ) ; tag = value ; if ( ( tag < General . Map . FormatInterface . MinTag ) | | ( tag > General . Map . FormatInterface . MaxTag ) ) throw new ArgumentOutOfRangeException ( "Tag" , "Invalid tag number" ) ; } }
public int Brightness { get { return brightness ; } set { BeforePropsChange ( ) ; brightness = value ; updateneeded = true ; } }
2009-04-19 18:07:22 +00:00
public bool UpdateNeeded { get { return updateneeded ; } set { updateneeded | = value ; triangulationneeded | = value ; } }
public RectangleF BBox { get { return bbox ; } }
internal Sector Clone { get { return clone ; } set { clone = value ; } }
internal int SerializedIndex { get { return serializedindex ; } set { serializedindex = value ; } }
public Triangulation Triangles { get { return triangles ; } }
public FlatVertex [ ] FlatVertices { get { return flatvertices ; } }
public ReadOnlyCollection < LabelPositionInfo > Labels { get { return labels ; } }
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
2009-06-05 19:03:56 +00:00
internal Sector ( MapSet map , int listindex , int index )
2009-04-19 18:07:22 +00:00
{
// Initialize
this . map = map ;
2009-06-05 19:03:56 +00:00
this . listindex = listindex ;
2009-04-19 18:07:22 +00:00
this . sidedefs = new LinkedList < Sidedef > ( ) ;
this . fixedindex = index ;
this . floortexname = "-" ;
this . ceiltexname = "-" ;
this . longfloortexname = MapSet . EmptyLongName ;
this . longceiltexname = MapSet . EmptyLongName ;
this . triangulationneeded = true ;
2009-06-05 19:03:56 +00:00
this . surfaceentry = new SurfaceEntry ( - 1 , - 1 , - 1 ) ;
2009-04-19 18:07:22 +00:00
2009-06-11 21:21:20 +00:00
if ( map = = General . Map . Map )
General . Map . UndoRedo . RecAddSector ( this ) ;
2009-04-19 18:07:22 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
// Disposer
public override void Dispose ( )
{
// Not already disposed?
if ( ! isdisposed )
{
// Already set isdisposed so that changes can be prohibited
isdisposed = true ;
2009-06-11 21:21:20 +00:00
2009-06-15 17:38:02 +00:00
// Dispose the sidedefs that are attached to this sector
// because a sidedef cannot exist without reference to its sector.
if ( map . AutoRemove )
foreach ( Sidedef sd in sidedefs ) sd . Dispose ( ) ;
else
foreach ( Sidedef sd in sidedefs ) sd . SetSectorP ( null ) ;
2009-06-11 21:21:20 +00:00
if ( map = = General . Map . Map )
General . Map . UndoRedo . RecRemSector ( this ) ;
2009-04-19 18:07:22 +00:00
// Remove from main list
2009-06-05 19:03:56 +00:00
map . RemoveSector ( listindex ) ;
2009-06-11 21:21:20 +00:00
2009-04-19 18:07:22 +00:00
// Register the index as free
map . AddSectorIndexHole ( fixedindex ) ;
2009-06-04 20:21:31 +00:00
// Free surface entry
General . Map . CRenderer2D . Surfaces . FreeSurfaces ( surfaceentry ) ;
2009-06-11 21:21:20 +00:00
2009-04-19 18:07:22 +00:00
// Clean up
sidedefs = null ;
map = null ;
2009-06-11 21:21:20 +00:00
2009-04-19 18:07:22 +00:00
// Dispose base
base . Dispose ( ) ;
}
}
#endregion
#region = = = = = = = = = = = = = = = = = = Management
2009-06-11 21:21:20 +00:00
// Call this before changing properties
protected override void BeforePropsChange ( )
{
if ( map = = General . Map . Map )
General . Map . UndoRedo . RecPrpSector ( this ) ;
}
// Serialize / deserialize (passive: this doesn't record)
2009-04-19 18:07:22 +00:00
internal void ReadWrite ( IReadWriteStream s )
{
2009-06-15 17:38:02 +00:00
if ( ! s . IsWriting )
{
BeforePropsChange ( ) ;
updateneeded = true ;
}
2009-04-19 18:07:22 +00:00
2009-06-11 21:21:20 +00:00
base . ReadWrite ( s ) ;
2009-04-19 18:07:22 +00:00
s . rwInt ( ref fixedindex ) ;
s . rwInt ( ref floorheight ) ;
s . rwInt ( ref ceilheight ) ;
s . rwString ( ref floortexname ) ;
s . rwString ( ref ceiltexname ) ;
2009-06-11 21:21:20 +00:00
s . rwLong ( ref longfloortexname ) ;
s . rwLong ( ref longceiltexname ) ;
2009-04-19 18:07:22 +00:00
s . rwInt ( ref effect ) ;
s . rwInt ( ref tag ) ;
s . rwInt ( ref brightness ) ;
}
// After deserialization
internal void PostDeserialize ( MapSet map )
{
triangles . PostDeserialize ( map ) ;
// We need to rebuild the vertex buffer,
// but the triangulation was deserialized
updateneeded = true ;
triangulationneeded = false ;
}
// This copies all properties to another sector
public void CopyPropertiesTo ( Sector s )
{
2009-06-11 21:21:20 +00:00
s . BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Copy properties
s . ceilheight = ceilheight ;
s . ceiltexname = ceiltexname ;
s . longceiltexname = longceiltexname ;
s . floorheight = floorheight ;
s . floortexname = floortexname ;
s . longfloortexname = longfloortexname ;
s . effect = effect ;
s . tag = tag ;
s . brightness = brightness ;
s . updateneeded = true ;
base . CopyPropertiesTo ( s ) ;
}
// This attaches a sidedef and returns the listitem
2009-06-11 21:21:20 +00:00
internal LinkedListNode < Sidedef > AttachSidedefP ( Sidedef sd )
2009-04-19 18:07:22 +00:00
{
updateneeded = true ;
triangulationneeded = true ;
return sidedefs . AddLast ( sd ) ;
}
// This detaches a sidedef
2009-06-11 21:21:20 +00:00
internal void DetachSidedefP ( LinkedListNode < Sidedef > l )
2009-04-19 18:07:22 +00:00
{
// Not disposing?
if ( ! isdisposed )
{
// Remove sidedef
updateneeded = true ;
triangulationneeded = true ;
sidedefs . Remove ( l ) ;
// No more sidedefs left?
2009-06-07 10:26:06 +00:00
if ( ( sidedefs . Count = = 0 ) & & map . AutoRemove )
2009-04-19 18:07:22 +00:00
{
// This sector is now useless, dispose it
this . Dispose ( ) ;
}
}
}
2009-06-04 20:21:31 +00:00
// This triangulates the sector geometry
internal void Triangulate ( )
2009-04-19 18:07:22 +00:00
{
if ( updateneeded )
{
// Triangulate again?
if ( triangulationneeded | | ( triangles = = null ) )
{
// Triangulate sector
triangles = Triangulation . Create ( this ) ;
triangulationneeded = false ;
2009-06-04 20:21:31 +00:00
updateneeded = true ;
2009-04-19 18:07:22 +00:00
// Make label positions
labels = Array . AsReadOnly < LabelPositionInfo > ( Tools . FindLabelPositions ( this ) . ToArray ( ) ) ;
2009-06-04 20:21:31 +00:00
// Number of vertices changed?
if ( ( surfaceentry ! = null ) & & ( triangles . Vertices . Count ! = surfaceentry . numvertices ) )
General . Map . CRenderer2D . Surfaces . FreeSurfaces ( surfaceentry ) ;
2009-04-19 18:07:22 +00:00
}
2009-06-04 20:21:31 +00:00
}
}
// This makes new vertices as well as floor and ceiling surfaces
internal void CreateSurfaces ( )
{
if ( updateneeded )
{
2009-04-19 18:07:22 +00:00
// Brightness color (alpha is opaque)
byte clampedbright = 0 ;
if ( ( brightness > = 0 ) & & ( brightness < = 255 ) ) clampedbright = ( byte ) brightness ;
else if ( brightness > 255 ) clampedbright = 255 ;
PixelColor brightcolor = new PixelColor ( 255 , clampedbright , clampedbright , clampedbright ) ;
int brightint = brightcolor . ToInt ( ) ;
// Make vertices
flatvertices = new FlatVertex [ triangles . Vertices . Count ] ;
for ( int i = 0 ; i < triangles . Vertices . Count ; i + + )
{
flatvertices [ i ] . x = triangles . Vertices [ i ] . x ;
flatvertices [ i ] . y = triangles . Vertices [ i ] . y ;
flatvertices [ i ] . z = 1.0f ;
flatvertices [ i ] . c = brightint ;
flatvertices [ i ] . u = triangles . Vertices [ i ] . x ;
flatvertices [ i ] . v = triangles . Vertices [ i ] . y ;
}
// Create bounding box
bbox = CreateBBox ( ) ;
2009-06-04 20:21:31 +00:00
// Make a dummy entry if we don't have one yet
if ( surfaceentry = = null ) surfaceentry = new SurfaceEntry ( - 1 , - 1 , - 1 ) ;
// Create floor vertices
FlatVertex [ ] floorvertices = new FlatVertex [ flatvertices . Length ] ;
flatvertices . CopyTo ( floorvertices , 0 ) ;
General . Plugins . OnSectorFloorSurfaceUpdate ( this , ref floorvertices ) ;
surfaceentry . floorvertices = floorvertices ;
surfaceentry . floortexture = longfloortexname ;
// Create ceiling vertices
FlatVertex [ ] ceilvertices = new FlatVertex [ flatvertices . Length ] ;
flatvertices . CopyTo ( ceilvertices , 0 ) ;
General . Plugins . OnSectorCeilingSurfaceUpdate ( this , ref ceilvertices ) ;
surfaceentry . ceilvertices = ceilvertices ;
surfaceentry . ceiltexture = longceiltexname ;
// Update entry
surfaceentry = General . Map . CRenderer2D . Surfaces . UpdateSurfaces ( surfaceentry ) ;
2009-04-19 18:07:22 +00:00
// Updated
updateneeded = false ;
}
}
2009-06-04 20:21:31 +00:00
// This updates the floor surface
2009-04-19 18:07:22 +00:00
public void UpdateFloorSurface ( )
{
2009-06-11 21:21:20 +00:00
if ( flatvertices = = null ) return ;
2009-06-04 20:21:31 +00:00
// Create floor vertices
FlatVertex [ ] floorvertices = new FlatVertex [ flatvertices . Length ] ;
flatvertices . CopyTo ( floorvertices , 0 ) ;
General . Plugins . OnSectorFloorSurfaceUpdate ( this , ref floorvertices ) ;
surfaceentry . floorvertices = floorvertices ;
surfaceentry . floortexture = longfloortexname ;
// Update entry
surfaceentry = General . Map . CRenderer2D . Surfaces . UpdateSurfaces ( surfaceentry ) ;
General . Map . CRenderer2D . Surfaces . UnlockBuffers ( ) ;
2009-04-19 18:07:22 +00:00
}
2009-06-04 20:21:31 +00:00
// This updates the ceiling surface
2009-04-19 18:07:22 +00:00
public void UpdateCeilingSurface ( )
{
2009-06-11 21:21:20 +00:00
if ( flatvertices = = null ) return ;
2009-06-04 20:21:31 +00:00
// Create ceiling vertices
FlatVertex [ ] ceilvertices = new FlatVertex [ flatvertices . Length ] ;
flatvertices . CopyTo ( ceilvertices , 0 ) ;
General . Plugins . OnSectorCeilingSurfaceUpdate ( this , ref ceilvertices ) ;
surfaceentry . ceilvertices = ceilvertices ;
surfaceentry . ceiltexture = longceiltexname ;
// Update entry
surfaceentry = General . Map . CRenderer2D . Surfaces . UpdateSurfaces ( surfaceentry ) ;
General . Map . CRenderer2D . Surfaces . UnlockBuffers ( ) ;
2009-04-19 18:07:22 +00:00
}
2009-06-04 20:21:31 +00:00
// This updates the sector when changes have been made
public void UpdateCache ( )
2009-04-19 18:07:22 +00:00
{
2009-06-04 20:21:31 +00:00
// Update if needed
if ( updateneeded )
2009-04-19 18:07:22 +00:00
{
2009-06-04 20:21:31 +00:00
Triangulate ( ) ;
CreateSurfaces ( ) ;
2009-04-19 18:07:22 +00:00
}
}
// Selected
protected override void DoSelect ( )
{
base . DoSelect ( ) ;
selecteditem = map . SelectedSectors . AddLast ( this ) ;
}
// Deselect
protected override void DoUnselect ( )
{
base . DoUnselect ( ) ;
if ( selecteditem . List ! = null ) selecteditem . List . Remove ( selecteditem ) ;
selecteditem = null ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
// This checks if the given point is inside the sector polygon
public bool Intersect ( Vector2D p )
{
uint c = 0 ;
// Go for all sidedefs
foreach ( Sidedef sd in sidedefs )
{
// Get vertices
Vector2D v1 = sd . Line . Start . Position ;
Vector2D v2 = sd . Line . End . Position ;
// Determine min/max values
float miny = Math . Min ( v1 . y , v2 . y ) ;
float maxy = Math . Max ( v1 . y , v2 . y ) ;
float maxx = Math . Max ( v1 . x , v2 . x ) ;
// Check for intersection
if ( ( p . y > miny ) & & ( p . y < = maxy ) )
{
if ( p . x < = maxx )
{
if ( v1 . y ! = v2 . y )
{
float xint = ( p . y - v1 . y ) * ( v2 . x - v1 . x ) / ( v2 . y - v1 . y ) + v1 . x ;
if ( ( v1 . x = = v2 . x ) | | ( p . x < = xint ) ) c + + ;
}
}
}
}
// Inside this polygon?
return ( ( c & 0x00000001 UL ) ! = 0 ) ;
}
// This creates a bounding box rectangle
// This requires the sector triangulation to be up-to-date!
private RectangleF CreateBBox ( )
{
// Setup
float left = float . MaxValue ;
float top = float . MaxValue ;
float right = float . MinValue ;
float bottom = float . MinValue ;
// Go for vertices
foreach ( Vector2D v in triangles . Vertices )
{
// Update rect
if ( v . x < left ) left = v . x ;
if ( v . y < top ) top = v . y ;
if ( v . x > right ) right = v . x ;
if ( v . y > bottom ) bottom = v . y ;
}
// Return rectangle
return new RectangleF ( left , top , right - left , bottom - top ) ;
}
// This joins the sector with another sector
// This sector will be disposed
public void Join ( Sector other )
{
// Any sidedefs to move?
if ( sidedefs . Count > 0 )
{
// Change secter reference on my sidedefs
// This automatically disposes this sector
while ( sidedefs ! = null )
2009-06-11 21:21:20 +00:00
sidedefs . First . Value . SetSector ( other ) ;
2009-04-19 18:07:22 +00:00
}
else
{
// No sidedefs attached
// Dispose manually
this . Dispose ( ) ;
}
General . Map . IsChanged = true ;
}
// String representation
public override string ToString ( )
{
2009-06-05 19:03:56 +00:00
return "Sector " + listindex ;
2009-04-19 18:07:22 +00:00
}
#endregion
#region = = = = = = = = = = = = = = = = = = Changes
// This updates all properties
public void Update ( int hfloor , int hceil , string tfloor , string tceil , int effect , int tag , int brightness )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
// Apply changes
this . floorheight = hfloor ;
this . ceilheight = hceil ;
SetFloorTexture ( tfloor ) ;
SetCeilTexture ( tceil ) ;
this . effect = effect ;
this . tag = tag ;
this . brightness = brightness ;
updateneeded = true ;
}
// This sets texture
public void SetFloorTexture ( string name )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
floortexname = name ;
longfloortexname = Lump . MakeLongName ( name ) ;
updateneeded = true ;
General . Map . IsChanged = true ;
}
// This sets texture
public void SetCeilTexture ( string name )
{
2009-06-11 21:21:20 +00:00
BeforePropsChange ( ) ;
2009-04-19 18:07:22 +00:00
ceiltexname = name ;
longceiltexname = Lump . MakeLongName ( name ) ;
updateneeded = true ;
General . Map . IsChanged = true ;
}
#endregion
}
}