UltimateZoneBuilder/Source/Map/Sidedef.cs

403 lines
9.8 KiB
C#
Raw Normal View History

2007-06-14 23:31:57 +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.
*
*/
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-11-04 22:19:30 +00:00
using CodeImp.DoomBuilder.IO;
using CodeImp.DoomBuilder.Geometry;
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
{
public sealed class Sidedef : MapElement
2007-06-13 19:39:38 +00:00
{
#region ================== Constants
#endregion
#region ================== Variables
2007-06-14 12:37:46 +00:00
// Map
private MapSet map;
2007-06-14 12:37:46 +00:00
// List items
private LinkedListNode<Sidedef> mainlistitem;
private LinkedListNode<Sidedef> sectorlistitem;
// Owner
private Linedef linedef;
// Sector
private Sector sector;
// Properties
private int offsetx;
private int offsety;
private string texnamehigh;
private string texnamemid;
private string texnamelow;
2007-11-04 22:19:30 +00:00
private long longtexnamehigh;
private long longtexnamemid;
private long longtexnamelow;
2008-01-25 19:12:34 +00:00
2009-02-20 12:29:37 +00:00
// Clone
private int serializedindex;
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Properties
public MapSet Map { get { return map; } }
public bool IsFront { get { return (this == linedef.Front); } }
public Linedef Line { get { return linedef; } }
2007-06-14 12:37:46 +00:00
public Sidedef Other { get { if(this == linedef.Front) return linedef.Back; else return linedef.Front; } }
public Sector Sector { get { return sector; } }
public float Angle { get { if(IsFront) return linedef.Angle; else return Angle2D.Normalized(linedef.Angle + Angle2D.PI); } }
2007-12-27 01:24:11 +00:00
public int OffsetX { get { return offsetx; } set { offsetx = value; } }
public int OffsetY { get { return offsety; } set { offsety = value; } }
2007-10-14 15:44:55 +00:00
public string HighTexture { get { return texnamehigh; } }
public string MiddleTexture { get { return texnamemid; } }
public string LowTexture { get { return texnamelow; } }
2007-11-04 22:19:30 +00:00
public long LongHighTexture { get { return longtexnamehigh; } }
public long LongMiddleTexture { get { return longtexnamemid; } }
public long LongLowTexture { get { return longtexnamelow; } }
2009-02-20 12:29:37 +00:00
internal int SerializedIndex { get { return serializedindex; } set { serializedindex = value; } }
2007-10-14 15:44:55 +00:00
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
internal Sidedef(MapSet map, LinkedListNode<Sidedef> listitem, Linedef l, bool front, Sector s)
2007-06-13 19:39:38 +00:00
{
// Initialize
2007-06-14 12:37:46 +00:00
this.map = map;
this.mainlistitem = listitem;
this.linedef = l;
this.sector = s;
2008-04-19 15:44:05 +00:00
this.texnamehigh = "-";
this.texnamemid = "-";
this.texnamelow = "-";
this.longtexnamehigh = MapSet.EmptyLongName;
this.longtexnamemid = MapSet.EmptyLongName;
this.longtexnamelow = MapSet.EmptyLongName;
2007-06-14 12:37:46 +00:00
// Attach to the linedef
if(front) l.AttachFront(this); else l.AttachBack(this);
// Attach to sector
sectorlistitem = s.AttachSidedef(this);
2007-06-13 19:39:38 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
// Constructor
internal Sidedef(MapSet map, LinkedListNode<Sidedef> listitem, Linedef l, bool front, Sector s, IReadWriteStream stream)
{
// Initialize
this.map = map;
this.mainlistitem = listitem;
this.linedef = l;
this.sector = s;
// Attach to the linedef
if(front) l.AttachFront(this); else l.AttachBack(this);
// Attach to sector
sectorlistitem = s.AttachSidedef(this);
ReadWrite(stream);
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
public override void Dispose()
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
2007-06-13 19:39:38 +00:00
isdisposed = true;
2007-06-14 12:37:46 +00:00
// Remove from main list
mainlistitem.List.Remove(mainlistitem);
// Detach from linedef
linedef.DetachSidedef(this);
// Detach from sector
sector.DetachSidedef(sectorlistitem);
// Clean up
mainlistitem = null;
sectorlistitem = null;
linedef = null;
map = null;
sector = null;
// Dispose base
base.Dispose();
2007-06-13 19:39:38 +00:00
}
}
#endregion
#region ================== Management
2007-06-13 19:39:38 +00:00
// Serialize / deserialize
internal void ReadWrite(IReadWriteStream s)
{
base.ReadWrite(s);
s.rwInt(ref offsetx);
s.rwInt(ref offsety);
s.rwString(ref texnamehigh);
s.rwString(ref texnamemid);
s.rwString(ref texnamelow);
//s.rwLong(ref longtexnamehigh);
//s.rwLong(ref longtexnamemid);
//s.rwLong(ref longtexnamelow);
if(!s.IsWriting)
{
longtexnamehigh = Lump.MakeLongName(texnamehigh);
longtexnamemid = Lump.MakeLongName(texnamemid);
longtexnamelow = Lump.MakeLongName(texnamelow);
}
}
// This copies all properties to another sidedef
public void CopyPropertiesTo(Sidedef s)
{
// Copy properties
s.offsetx = offsetx;
s.offsety = offsety;
2007-11-12 22:43:01 +00:00
s.texnamehigh = texnamehigh;
s.texnamemid = texnamemid;
s.texnamelow = texnamelow;
s.longtexnamehigh = longtexnamehigh;
s.longtexnamemid = longtexnamemid;
s.longtexnamelow = longtexnamelow;
base.CopyPropertiesTo(s);
}
/// <summary>
/// Returns the index of this sidedef. This is a O(n) operation.
/// </summary>
public int GetIndex()
{
return map.GetIndexForSidedef(this);
}
2007-12-01 18:29:58 +00:00
// This copies textures to another sidedef
// And possibly also the offsets
public void AddTexturesTo(Sidedef s)
{
int copyoffsets = 0;
2007-12-26 00:31:32 +00:00
// s cannot be null
if(s == null) return;
2007-12-01 18:29:58 +00:00
// Upper texture set?
if((texnamehigh.Length > 0) && (texnamehigh[0] != '-'))
{
// Copy upper texture
s.texnamehigh = texnamehigh;
s.longtexnamehigh = longtexnamehigh;
// Counts as a half coice for copying offsets
copyoffsets += 1;
}
// Middle texture set?
if((texnamemid.Length > 0) && (texnamemid[0] != '-'))
{
// Copy middle texture
s.texnamemid = texnamemid;
s.longtexnamemid = longtexnamemid;
// Counts for copying offsets
copyoffsets += 2;
}
// Lower texture set?
if((texnamelow.Length > 0) && (texnamelow[0] != '-'))
{
// Copy middle texture
s.texnamelow = texnamelow;
s.longtexnamelow = longtexnamelow;
// Counts as a half coice for copying offsets
copyoffsets += 1;
}
// Copy offsets also?
if(copyoffsets >= 2)
{
// Copy offsets
s.offsetx = offsetx;
s.offsety = offsety;
}
2009-01-03 22:18:59 +00:00
General.Map.IsChanged = true;
2007-12-01 18:29:58 +00:00
}
2007-06-13 19:39:38 +00:00
#endregion
2007-06-24 18:56:43 +00:00
2007-12-26 14:29:36 +00:00
#region ================== Methods
2008-05-08 12:04:20 +00:00
// This removes textures that are not required
public void RemoveUnneededTextures(bool removemiddle)
{
RemoveUnneededTextures(removemiddle, false);
}
// This removes textures that are not required
public void RemoveUnneededTextures(bool removemiddle, bool force)
2008-05-08 12:04:20 +00:00
{
// Check if the line or sectors have no action or tags because
// if they do, any texture on this side could be needed
if(((linedef.Tag <= 0) && (linedef.Action == 0) && (sector.Tag <= 0) &&
((Other == null) || (Other.sector.Tag <= 0))) ||
force)
2008-05-08 12:04:20 +00:00
{
if(!HighRequired())
{
this.texnamehigh = "-";
this.longtexnamehigh = MapSet.EmptyLongName;
General.Map.IsChanged = true;
}
if(!MiddleRequired() && removemiddle)
{
this.texnamemid = "-";
this.longtexnamemid = MapSet.EmptyLongName;
General.Map.IsChanged = true;
}
if(!LowRequired())
{
this.texnamelow = "-";
this.longtexnamelow = MapSet.EmptyLongName;
General.Map.IsChanged = true;
}
2008-05-08 12:04:20 +00:00
}
}
2007-12-26 14:29:36 +00:00
// This checks if a texture is required
public bool HighRequired()
{
// Doublesided?
if(Other != null)
{
// Texture is required when ceiling of other side is lower
return (Other.sector.CeilHeight < this.sector.CeilHeight);
}
else
{
return false;
}
}
// This checks if a texture is required
public bool MiddleRequired()
{
// Texture is required when the line is singlesided
return (Other == null);
}
// This checks if a texture is required
public bool LowRequired()
{
// Doublesided?
if(Other != null)
{
// Texture is required when floor of other side is higher
return (Other.sector.FloorHeight > this.sector.FloorHeight);
}
else
{
return false;
}
}
#endregion
2007-06-24 18:56:43 +00:00
#region ================== Changes
// This updates all properties
public void Update(int offsetx, int offsety, string thigh, string tmid, string tlow)
{
// Apply changes
this.offsetx = offsetx;
this.offsety = offsety;
2007-11-04 22:19:30 +00:00
SetTextureHigh(thigh);
SetTextureMid(tmid);
SetTextureLow(tlow);
}
// This sets texture
public void SetTextureHigh(string name)
{
texnamehigh = name;
longtexnamehigh = Lump.MakeLongName(name);
2009-01-03 22:18:59 +00:00
General.Map.IsChanged = true;
2007-11-04 22:19:30 +00:00
}
// This sets texture
public void SetTextureMid(string name)
{
texnamemid = name;
longtexnamemid = Lump.MakeLongName(name);
2009-01-03 22:18:59 +00:00
General.Map.IsChanged = true;
2007-11-04 22:19:30 +00:00
}
// This sets texture
public void SetTextureLow(string name)
{
texnamelow = name;
longtexnamelow = Lump.MakeLongName(name);
2009-01-03 22:18:59 +00:00
General.Map.IsChanged = true;
2007-06-24 18:56:43 +00:00
}
2007-12-27 01:24:11 +00:00
// This changes sector
public void ChangeSector(Sector newsector)
{
// Detach from sector
sector.DetachSidedef(sectorlistitem);
// Change sector
sector = newsector;
// Attach to sector
sectorlistitem = sector.AttachSidedef(this);
2009-01-03 22:18:59 +00:00
General.Map.IsChanged = true;
2007-12-27 01:24:11 +00:00
}
2007-06-24 18:56:43 +00:00
#endregion
2007-06-13 19:39:38 +00:00
}
}