2007-06-14 14:44:18 +00:00
|
|
|
|
2007-06-14 23:31:57 +00:00
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
2007-06-14 14:44:18 +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-14 12:37:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
2007-11-04 22:19:30 +00:00
|
|
|
using CodeImp.DoomBuilder.IO;
|
2008-01-13 21:23:59 +00:00
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
2008-04-12 16:07:10 +00:00
|
|
|
using System.Drawing;
|
2008-05-18 11:43:28 +00:00
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
2007-06-14 12:37:46 +00:00
|
|
|
|
2007-06-14 23:31:57 +00:00
|
|
|
#endregion
|
|
|
|
|
2007-06-14 12:37:46 +00:00
|
|
|
namespace CodeImp.DoomBuilder.Map
|
|
|
|
{
|
2008-05-30 08:41:13 +00:00
|
|
|
public sealed class Sector : MapElement
|
2007-06-14 12:37:46 +00:00
|
|
|
{
|
|
|
|
#region ================== Constants
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Map
|
2007-06-14 13:09:55 +00:00
|
|
|
private MapSet map;
|
2007-06-14 12:37:46 +00:00
|
|
|
|
|
|
|
// List items
|
|
|
|
private LinkedListNode<Sector> mainlistitem;
|
|
|
|
|
|
|
|
// Sidedefs
|
|
|
|
private LinkedList<Sidedef> sidedefs;
|
|
|
|
|
|
|
|
// Things
|
|
|
|
private LinkedList<Thing> things;
|
|
|
|
|
|
|
|
// Properties
|
2007-12-27 01:24:11 +00:00
|
|
|
private int index;
|
2007-06-14 12:37:46 +00:00
|
|
|
private int floorheight;
|
|
|
|
private int ceilheight;
|
|
|
|
private string floortexname;
|
|
|
|
private string ceiltexname;
|
2007-11-04 22:19:30 +00:00
|
|
|
private long longfloortexname;
|
|
|
|
private long longceiltexname;
|
2007-10-24 17:25:03 +00:00
|
|
|
private int effect;
|
2007-06-14 12:37:46 +00:00
|
|
|
private int tag;
|
2007-10-14 15:44:55 +00:00
|
|
|
private int brightness;
|
2007-06-14 12:37:46 +00:00
|
|
|
|
2007-10-20 01:04:47 +00:00
|
|
|
// Selections
|
2007-12-01 18:29:58 +00:00
|
|
|
private bool selected;
|
2008-05-01 14:10:38 +00:00
|
|
|
private bool marked;
|
2007-10-20 01:04:47 +00:00
|
|
|
|
2007-11-23 09:33:56 +00:00
|
|
|
// Cloning
|
|
|
|
private Sector clone;
|
|
|
|
|
2008-01-13 21:23:59 +00:00
|
|
|
// Triangulation
|
|
|
|
private bool updateneeded;
|
2008-05-18 11:43:28 +00:00
|
|
|
private bool triangulationneeded;
|
|
|
|
private TriangleList triverts;
|
|
|
|
private FlatVertex[] vertices;
|
2008-01-25 19:12:34 +00:00
|
|
|
|
2007-06-14 12:37:46 +00:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
2007-07-07 09:40:34 +00:00
|
|
|
public MapSet Map { get { return map; } }
|
2007-10-20 19:50:03 +00:00
|
|
|
public ICollection<Sidedef> Sidedefs { get { return sidedefs; } }
|
2007-10-24 17:25:03 +00:00
|
|
|
public ICollection<Thing> Things { get { return things; } }
|
2007-12-27 01:24:11 +00:00
|
|
|
public int Index { get { return index; } }
|
2008-02-22 17:11:29 +00:00
|
|
|
public int FloorHeight { get { return floorheight; } set { floorheight = value; } }
|
|
|
|
public int CeilHeight { get { return ceilheight; } set { ceilheight = value; } }
|
2007-10-14 15:44:55 +00:00
|
|
|
public string FloorTexture { get { return floortexname; } }
|
|
|
|
public string CeilTexture { get { return ceiltexname; } }
|
2007-11-04 22:19:30 +00:00
|
|
|
public long LongFloorTexture { get { return longfloortexname; } }
|
|
|
|
public long LongCeilTexture { get { return longceiltexname; } }
|
2008-02-22 17:11:29 +00:00
|
|
|
public int Effect { get { return effect; } set { effect = value; } }
|
2007-12-27 01:24:11 +00:00
|
|
|
public int Tag { get { return tag; } set { tag = value; if((tag < 0) || (tag > MapSet.HIGHEST_TAG)) throw new ArgumentOutOfRangeException("Tag", "Invalid tag number"); } }
|
2008-05-18 11:43:28 +00:00
|
|
|
public int Brightness { get { return brightness; } set { brightness = value; updateneeded = true; } }
|
2007-12-01 18:29:58 +00:00
|
|
|
public bool Selected { get { return selected; } set { selected = value; } }
|
2008-05-01 14:10:38 +00:00
|
|
|
public bool Marked { get { return marked; } set { marked = value; } }
|
2008-05-18 11:43:28 +00:00
|
|
|
public bool UpdateNeeded { get { return updateneeded; } set { updateneeded |= value; triangulationneeded |= value; } }
|
2007-11-23 09:33:56 +00:00
|
|
|
public Sector Clone { get { return clone; } set { clone = value; } }
|
2008-05-18 11:43:28 +00:00
|
|
|
public FlatVertex[] Vertices { get { return vertices; } }
|
2007-06-14 12:37:46 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
2008-05-10 16:09:45 +00:00
|
|
|
internal Sector(MapSet map, LinkedListNode<Sector> listitem, int index)
|
2007-06-14 12:37:46 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
this.map = map;
|
|
|
|
this.mainlistitem = listitem;
|
|
|
|
this.sidedefs = new LinkedList<Sidedef>();
|
|
|
|
this.things = new LinkedList<Thing>();
|
2007-12-27 01:24:11 +00:00
|
|
|
this.index = index;
|
2008-04-19 15:44:05 +00:00
|
|
|
this.floortexname = "-";
|
|
|
|
this.ceiltexname = "-";
|
2008-09-17 19:21:45 +00:00
|
|
|
this.longfloortexname = MapSet.EmptyLongName;
|
|
|
|
this.longceiltexname = MapSet.EmptyLongName;
|
2008-06-11 05:02:48 +00:00
|
|
|
this.triangulationneeded = true;
|
2007-06-14 12:37:46 +00:00
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2008-01-02 21:49:43 +00:00
|
|
|
// Disposer
|
2008-05-30 08:41:13 +00:00
|
|
|
public override void Dispose()
|
2007-06-14 12:37:46 +00:00
|
|
|
{
|
|
|
|
// Not already disposed?
|
|
|
|
if(!isdisposed)
|
|
|
|
{
|
|
|
|
// Already set isdisposed so that changes can be prohibited
|
|
|
|
isdisposed = true;
|
|
|
|
|
|
|
|
// Remove from main list
|
|
|
|
mainlistitem.List.Remove(mainlistitem);
|
2007-12-27 01:24:11 +00:00
|
|
|
|
|
|
|
// Register the index as free
|
|
|
|
map.AddSectorIndexHole(index);
|
2007-06-14 12:37:46 +00:00
|
|
|
|
|
|
|
// Dispose the sidedefs that are attached to this sector
|
|
|
|
// because a sidedef cannot exist without reference to its sector.
|
|
|
|
foreach(Sidedef sd in sidedefs) sd.Dispose();
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
mainlistitem = null;
|
|
|
|
sidedefs = null;
|
|
|
|
things = null;
|
|
|
|
map = null;
|
2008-05-30 08:41:13 +00:00
|
|
|
|
|
|
|
// Dispose base
|
|
|
|
base.Dispose();
|
2007-06-14 12:37:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Management
|
|
|
|
|
2007-11-12 22:43:01 +00:00
|
|
|
// This copies all properties to another sector
|
|
|
|
public void CopyPropertiesTo(Sector s)
|
|
|
|
{
|
|
|
|
// 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;
|
2008-05-01 14:18:04 +00:00
|
|
|
s.selected = selected;
|
2008-05-18 11:43:28 +00:00
|
|
|
s.updateneeded = true;
|
2008-05-30 08:41:13 +00:00
|
|
|
CopyFieldsTo(s);
|
2007-11-12 22:43:01 +00:00
|
|
|
}
|
|
|
|
|
2007-06-14 12:37:46 +00:00
|
|
|
// This attaches a sidedef and returns the listitem
|
2008-01-13 21:23:59 +00:00
|
|
|
public LinkedListNode<Sidedef> AttachSidedef(Sidedef sd)
|
|
|
|
{
|
|
|
|
updateneeded = true;
|
2008-05-18 11:43:28 +00:00
|
|
|
triangulationneeded = true;
|
2008-01-13 21:23:59 +00:00
|
|
|
return sidedefs.AddLast(sd);
|
|
|
|
}
|
2007-06-14 12:37:46 +00:00
|
|
|
|
|
|
|
// This detaches a sidedef
|
|
|
|
public void DetachSidedef(LinkedListNode<Sidedef> l)
|
|
|
|
{
|
|
|
|
// Not disposing?
|
|
|
|
if(!isdisposed)
|
|
|
|
{
|
|
|
|
// Remove sidedef
|
2008-01-13 21:23:59 +00:00
|
|
|
updateneeded = true;
|
2008-05-18 11:43:28 +00:00
|
|
|
triangulationneeded = true;
|
2007-06-14 12:37:46 +00:00
|
|
|
sidedefs.Remove(l);
|
|
|
|
|
|
|
|
// No more sidedefs left?
|
|
|
|
if(sidedefs.Count == 0)
|
|
|
|
{
|
|
|
|
// This sector is now useless, dispose it
|
|
|
|
this.Dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This attaches a thing and returns the listitem
|
|
|
|
public LinkedListNode<Thing> AttachThing(Thing t) { return things.AddLast(t); }
|
|
|
|
|
|
|
|
// This detaches a thing
|
|
|
|
public void DetachThing(LinkedListNode<Thing> l) { if(!isdisposed) things.Remove(l); }
|
2008-01-13 21:23:59 +00:00
|
|
|
|
|
|
|
// This updates the sector when changes have been made
|
|
|
|
public void UpdateCache()
|
|
|
|
{
|
|
|
|
// Update if needed
|
|
|
|
if(updateneeded)
|
|
|
|
{
|
2008-05-18 11:43:28 +00:00
|
|
|
// Triangulate again?
|
|
|
|
if(triangulationneeded || (triverts == null))
|
|
|
|
{
|
|
|
|
// Triangulate sector
|
|
|
|
triverts = General.EarClipper.PerformTriangulation(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
vertices = new FlatVertex[triverts.Count];
|
|
|
|
for(int i = 0; i < triverts.Count; i++)
|
|
|
|
{
|
|
|
|
vertices[i].x = triverts[i].x;
|
|
|
|
vertices[i].y = triverts[i].y;
|
|
|
|
vertices[i].z = 1.0f;
|
|
|
|
vertices[i].c = brightint;
|
|
|
|
vertices[i].u = triverts[i].x;
|
|
|
|
vertices[i].v = triverts[i].y;
|
|
|
|
}
|
2008-01-13 21:23:59 +00:00
|
|
|
|
|
|
|
// Updated
|
|
|
|
updateneeded = false;
|
|
|
|
}
|
|
|
|
}
|
2007-06-14 12:37:46 +00:00
|
|
|
|
|
|
|
#endregion
|
2007-06-24 18:56:43 +00:00
|
|
|
|
2008-05-16 20:00:49 +00:00
|
|
|
#region ================== Methods
|
2008-10-03 14:31:25 +00:00
|
|
|
|
|
|
|
// This creates a bounding box rectangle
|
|
|
|
// This requires the sector polygon to be up-to-date!
|
|
|
|
public RectangleF CreateBBox()
|
|
|
|
{
|
|
|
|
// Setup
|
|
|
|
float left = float.MaxValue;
|
|
|
|
float top = float.MaxValue;
|
|
|
|
float right = float.MinValue;
|
|
|
|
float bottom = float.MinValue;
|
|
|
|
|
|
|
|
// Go for vertices
|
|
|
|
foreach(FlatVertex v in 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);
|
|
|
|
}
|
|
|
|
|
2008-05-16 20:00:49 +00:00
|
|
|
// This joins the sector with another sector
|
|
|
|
// This sector will be disposed
|
|
|
|
public void Join(Sector other)
|
|
|
|
{
|
2008-05-16 21:48:23 +00:00
|
|
|
// Any sidedefs to move?
|
|
|
|
if(sidedefs.Count > 0)
|
|
|
|
{
|
|
|
|
// Change secter reference on my sidedefs
|
|
|
|
// This automatically disposes this sector
|
|
|
|
while(sidedefs != null)
|
|
|
|
sidedefs.First.Value.ChangeSector(other);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// No sidedefs attached
|
|
|
|
// Dispose manually
|
|
|
|
this.Dispose();
|
|
|
|
}
|
2008-05-16 20:00:49 +00:00
|
|
|
}
|
2008-01-25 19:12:34 +00:00
|
|
|
|
2008-05-16 20:00:49 +00:00
|
|
|
#endregion
|
|
|
|
|
2007-06-24 18:56:43 +00:00
|
|
|
#region ================== Changes
|
|
|
|
|
|
|
|
// This updates all properties
|
2007-10-24 17:25:03 +00:00
|
|
|
public void Update(int hfloor, int hceil, string tfloor, string tceil, int effect, int tag, int brightness)
|
2007-06-24 18:56:43 +00:00
|
|
|
{
|
|
|
|
// Apply changes
|
|
|
|
this.floorheight = hfloor;
|
|
|
|
this.ceilheight = hceil;
|
2007-11-04 22:19:30 +00:00
|
|
|
SetFloorTexture(tfloor);
|
|
|
|
SetCeilTexture(tceil);
|
2007-10-24 17:25:03 +00:00
|
|
|
this.effect = effect;
|
2007-06-24 18:56:43 +00:00
|
|
|
this.tag = tag;
|
2007-10-14 15:44:55 +00:00
|
|
|
this.brightness = brightness;
|
2008-05-18 11:43:28 +00:00
|
|
|
updateneeded = true;
|
2007-06-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 22:19:30 +00:00
|
|
|
// This sets texture
|
|
|
|
public void SetFloorTexture(string name)
|
|
|
|
{
|
|
|
|
floortexname = name;
|
|
|
|
longfloortexname = Lump.MakeLongName(name);
|
2008-05-18 11:43:28 +00:00
|
|
|
updateneeded = true;
|
2007-11-04 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This sets texture
|
|
|
|
public void SetCeilTexture(string name)
|
|
|
|
{
|
|
|
|
ceiltexname = name;
|
|
|
|
longceiltexname = Lump.MakeLongName(name);
|
2008-05-18 11:43:28 +00:00
|
|
|
updateneeded = true;
|
2007-11-04 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 18:56:43 +00:00
|
|
|
#endregion
|
2007-06-14 12:37:46 +00:00
|
|
|
}
|
|
|
|
}
|