UltimateZoneBuilder/Source/Map/Linedef.cs

594 lines
16 KiB
C#
Raw Normal View History

2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#region ================== Copyright (c) 2007 Pascal vd Heiden
2007-06-13 19:39:38 +00:00
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
2007-06-14 23:31:57 +00:00
#endregion
#region ================== Namespaces
2007-06-13 19:39:38 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
2007-06-14 12:37:46 +00:00
using CodeImp.DoomBuilder.Geometry;
2007-06-24 18:56:43 +00:00
using CodeImp.DoomBuilder.Rendering;
using SlimDX.Direct3D;
2007-12-05 19:39:09 +00:00
using System.Drawing;
2007-06-13 19:39:38 +00:00
2007-06-14 23:31:57 +00:00
#endregion
2007-06-13 19:39:38 +00:00
namespace CodeImp.DoomBuilder.Map
{
2008-01-02 21:49:43 +00:00
public sealed class Linedef
2007-06-13 19:39:38 +00:00
{
#region ================== Constants
2007-12-26 00:31:32 +00:00
public const int NUM_ARGS = 5;
public static readonly byte[] EMPTY_ARGS = new byte[NUM_ARGS];
2007-06-24 18:56:43 +00:00
2007-06-13 19:39:38 +00:00
#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<Linedef> mainlistitem;
private LinkedListNode<Linedef> startvertexlistitem;
private LinkedListNode<Linedef> endvertexlistitem;
2007-06-13 19:39:38 +00:00
// Vertices
private Vertex start;
private Vertex end;
// Sidedefs
private Sidedef front;
private Sidedef back;
2007-06-14 12:37:46 +00:00
// Cache
private bool updateneeded;
2007-06-14 12:37:46 +00:00
private float lengthsq;
2008-01-13 21:23:59 +00:00
private float lengthsqinv;
2007-06-14 12:37:46 +00:00
private float length;
private float lengthinv;
2007-10-24 17:25:03 +00:00
private float angle;
2007-12-05 19:39:09 +00:00
private Rectangle rect;
2007-06-13 19:39:38 +00:00
// Properties
private int flags;
private int action;
private int tag;
private byte[] args;
2008-01-25 19:12:34 +00:00
// Additional fields
private SortedList<string, object> fields;
// Selections
2007-12-01 18:29:58 +00:00
private bool selected;
2007-06-13 19:39:38 +00:00
// Disposing
private bool isdisposed = false;
#endregion
#region ================== Properties
public MapSet Map { get { return map; } }
public Vertex Start { get { return start; } }
public Vertex End { get { return end; } }
2007-06-14 12:37:46 +00:00
public Sidedef Front { get { return front; } }
public Sidedef Back { get { return back; } }
2007-06-13 19:39:38 +00:00
public bool IsDisposed { get { return isdisposed; } }
2007-12-27 01:24:11 +00:00
public int Flags { get { return flags; } set { flags = value; } }
public int Action { get { return action; } set { action = value; } }
public int Tag { get { return tag; } set { tag = value; if((tag < 0) || (tag > MapSet.HIGHEST_TAG)) throw new ArgumentOutOfRangeException("Tag", "Invalid tag number"); } }
2007-12-01 18:29:58 +00:00
public bool Selected { get { return selected; } set { selected = value; } }
public float LengthSq { get { return lengthsq; } }
public float Length { get { return length; } }
public float LengthInv { get { return lengthinv; } }
2007-10-24 17:25:03 +00:00
public float Angle { get { return angle; } }
public int AngleDeg { get { return (int)(angle * Angle2D.PIDEG); } }
2007-12-05 19:39:09 +00:00
public Rectangle Rect { get { return rect; } }
public byte[] Args { get { return args; } }
2008-01-25 19:12:34 +00:00
public SortedList<string, object> Fields { get { return fields; } }
2007-06-13 19:39:38 +00:00
#endregion
#region ================== Constructor / Disposer
// Constructor
public Linedef(MapSet map, LinkedListNode<Linedef> listitem, Vertex start, Vertex end)
2007-06-13 19:39:38 +00:00
{
// Initialize
2007-06-14 12:37:46 +00:00
this.map = map;
this.mainlistitem = listitem;
2007-06-13 19:39:38 +00:00
this.start = start;
this.end = end;
this.updateneeded = true;
2007-06-14 12:37:46 +00:00
// Attach to vertices
startvertexlistitem = start.AttachLinedef(this);
endvertexlistitem = end.AttachLinedef(this);
2007-06-13 19:39:38 +00:00
// We have no destructor
GC.SuppressFinalize(this);
}
2008-01-02 21:49:43 +00:00
// Disposer
2007-06-13 19:39:38 +00:00
public void Dispose()
{
// Not already disposed?
if(!isdisposed)
{
2007-06-14 12:37:46 +00:00
// Already set isdisposed so that changes can be prohibited
isdisposed = true;
// Remove from main list
mainlistitem.List.Remove(mainlistitem);
2007-06-13 19:39:38 +00:00
// Detach from vertices
2007-06-14 12:37:46 +00:00
start.DetachLinedef(startvertexlistitem);
end.DetachLinedef(endvertexlistitem);
2007-06-13 19:39:38 +00:00
// Dispose sidedefs
2007-06-15 22:38:42 +00:00
if(front != null) front.Dispose();
if(back != null) back.Dispose();
2007-06-13 19:39:38 +00:00
// Clean up
2007-06-14 12:37:46 +00:00
mainlistitem = null;
startvertexlistitem = null;
endvertexlistitem = null;
2007-06-13 19:39:38 +00:00
start = null;
end = null;
front = null;
back = null;
2007-06-14 12:37:46 +00:00
map = null;
2007-06-13 19:39:38 +00:00
}
}
#endregion
2007-06-14 12:37:46 +00:00
#region ================== Management
2007-12-01 01:32:56 +00:00
// This sets new start vertex
public void SetStartVertex(Vertex v)
{
// Change start
start.DetachLinedef(startvertexlistitem);
start = v;
startvertexlistitem = start.AttachLinedef(this);
this.updateneeded = true;
}
// This sets new end vertex
public void SetEndVertex(Vertex v)
{
// Change end
end.DetachLinedef(endvertexlistitem);
end = v;
endvertexlistitem = end.AttachLinedef(this);
this.updateneeded = true;
}
2007-11-12 22:43:01 +00:00
// This copies all properties to another line
public void CopyPropertiesTo(Linedef l)
{
// Copy properties
l.action = action;
l.args = (byte[])args.Clone();
l.flags = flags;
l.tag = tag;
l.updateneeded = true;
2008-01-25 19:12:34 +00:00
if(fields != null) l.MakeFields(fields);
2007-11-12 22:43:01 +00:00
}
2007-06-14 12:37:46 +00:00
// This attaches a sidedef on the front
2007-06-24 18:56:43 +00:00
public void AttachFront(Sidedef s)
{
// No sidedef here yet?
if(front == null)
{
// Attach and recalculate
front = s;
updateneeded = true;
2007-06-24 18:56:43 +00:00
}
else throw new Exception("Linedef already has a front Sidedef.");
}
2007-06-14 12:37:46 +00:00
// This attaches a sidedef on the back
2007-06-24 18:56:43 +00:00
public void AttachBack(Sidedef s)
{
// No sidedef here yet?
if(back == null)
{
// Attach and recalculate
back = s;
updateneeded = true;
2007-06-24 18:56:43 +00:00
}
else throw new Exception("Linedef already has a back Sidedef.");
}
2007-06-14 12:37:46 +00:00
// This detaches a sidedef from the front
public void DetachSidedef(Sidedef s)
{
// Sidedef is on the front?
if(front == s)
{
// Remove sidedef reference
front = null;
updateneeded = true;
}
// Sidedef is on the back?
else if(back == s)
{
// Remove sidedef reference
back = null;
updateneeded = true;
}
else throw new Exception("Specified Sidedef is not attached to this Linedef.");
}
2007-06-14 12:37:46 +00:00
// This updates the line when changes have been made
2008-01-13 21:23:59 +00:00
public void UpdateCache()
2007-06-14 12:37:46 +00:00
{
2007-06-24 18:56:43 +00:00
Vector2D delta;
2007-12-05 19:39:09 +00:00
int l, t, r, b;
// Update if needed
if(updateneeded)
{
// Delta vector
delta = end.Position - start.Position;
// Recalculate values
lengthsq = delta.GetLengthSq();
length = (float)Math.Sqrt(lengthsq);
if(length > 0f) lengthinv = 1f / length; else lengthinv = 1f / 0.0000000001f;
2008-01-13 21:23:59 +00:00
if(lengthsq > 0f) lengthsqinv = 1f / lengthsq; else lengthsqinv = 1f / 0.0000000001f;
2007-10-24 17:25:03 +00:00
angle = delta.GetAngle();
2007-12-05 19:39:09 +00:00
l = Math.Min(start.X, end.X);
t = Math.Min(start.Y, end.Y);
r = Math.Max(start.X, end.X);
b = Math.Max(start.Y, end.Y);
rect = new Rectangle(l, t, r - l, b - t);
// Updated
updateneeded = false;
}
}
2008-01-13 21:23:59 +00:00
// This flags the line needs an update because it moved
public void NeedUpdate()
{
2008-01-13 21:23:59 +00:00
// Update this line
updateneeded = true;
2008-01-13 21:23:59 +00:00
// Update sectors as well
if(front != null) front.Sector.UpdateNeeded = true;
if(back != null) back.Sector.UpdateNeeded = true;
}
2008-01-25 19:12:34 +00:00
#endregion
#region ================== Fields
// This makes new fields
public void MakeFields()
{
if(fields != null) fields = new SortedList<string, object>();
}
// This makes fields from another list of fields
public void MakeFields(SortedList<string, object> copyfrom)
{
if(fields != null) fields = new SortedList<string, object>();
foreach(KeyValuePair<string, object> f in copyfrom) fields[f.Key] = f.Value;
}
2007-12-26 00:31:32 +00:00
2007-06-14 12:37:46 +00:00
#endregion
2007-06-24 18:56:43 +00:00
#region ================== Methods
2007-12-26 00:31:32 +00:00
// This applies single/double sided flags
public void ApplySidedFlags()
{
// Doublesided?
if((front != null) && (back != null))
{
// Apply or remove flags for doublesided line
flags &= ~General.Map.Config.SingleSidedFlags;
flags |= General.Map.Config.DoubleSidedFlags;
}
else
{
// Apply or remove flags for singlesided line
flags &= ~General.Map.Config.DoubleSidedFlags;
flags |= General.Map.Config.SingleSidedFlags;
}
}
2007-06-14 12:37:46 +00:00
// This returns the shortest distance from given coordinates to line
public float SafeDistanceToSq(Vector2D p, bool bounded)
{
Vector2D v1 = start.Position;
Vector2D v2 = end.Position;
// Calculate intersection offset
2008-01-13 21:23:59 +00:00
float u = ((p.x - v1.x) * (v2.x - v1.x) + (p.y - v1.y) * (v2.y - v1.y)) * lengthsqinv;
// Limit intersection offset to the line
if(bounded) if(u < lengthinv) u = lengthinv; else if(u > (1f - lengthinv)) u = 1f - lengthinv;
// Calculate intersection point
Vector2D i = v1 + u * (v2 - v1);
// Return distance between intersection and point
// which is the shortest distance to the line
float ldx = p.x - i.x;
float ldy = p.y - i.y;
return ldx * ldx + ldy * ldy;
}
// This returns the shortest distance from given coordinates to line
public float SafeDistanceTo(Vector2D p, bool bounded)
{
return (float)Math.Sqrt(SafeDistanceToSq(p, bounded));
}
2007-06-14 12:37:46 +00:00
// This returns the shortest distance from given coordinates to line
public float DistanceToSq(Vector2D p, bool bounded)
{
Vector2D v1 = start.Position;
Vector2D v2 = end.Position;
// Calculate intersection offset
2008-01-13 21:23:59 +00:00
float u = ((p.x - v1.x) * (v2.x - v1.x) + (p.y - v1.y) * (v2.y - v1.y)) * lengthsqinv;
2007-06-14 12:37:46 +00:00
// Limit intersection offset to the line
if(bounded) if(u < 0f) u = 0f; else if(u > 1f) u = 1f;
// Calculate intersection point
Vector2D i = v1 + u * (v2 - v1);
// Return distance between intersection and point
// which is the shortest distance to the line
float ldx = p.x - i.x;
float ldy = p.y - i.y;
return ldx * ldx + ldy * ldy;
}
// This returns the shortest distance from given coordinates to line
public float DistanceTo(Vector2D p, bool bounded)
{
return (float)Math.Sqrt(DistanceToSq(p, bounded));
}
2007-06-13 19:39:38 +00:00
2007-06-14 12:37:46 +00:00
// This tests on which side of the line the given coordinates are
// returns < 0 for front (right) side, > 0 for back (left) side and 0 if on the line
public float SideOfLine(Vector2D p)
{
Vector2D v1 = start.Position;
Vector2D v2 = end.Position;
// Calculate and return side information
return (p.y - v1.y) * (v2.x - v1.x) - (p.x - v1.x) * (v2.y - v1.y);
}
2007-12-01 01:32:56 +00:00
// This splits this line by vertex v
// Returns the new line resulting from the split
public Linedef Split(Vertex v)
{
Linedef nl;
Sidedef nsd;
// Copy linedef and change vertices
nl = map.CreateLinedef(v, end);
CopyPropertiesTo(nl);
SetEndVertex(v);
// Copy front sidedef if exists
if(front != null)
{
nsd = map.CreateSidedef(nl, true, front.Sector);
front.CopyPropertiesTo(nsd);
}
// Copy back sidedef if exists
if(back != null)
{
nsd = map.CreateSidedef(nl, false, back.Sector);
back.CopyPropertiesTo(nsd);
}
// Return result
return nl;
}
2007-06-14 12:37:46 +00:00
2007-12-01 18:29:58 +00:00
// This joins the line with another line
// This line will be disposed
public void Join(Linedef other)
{
2007-12-26 00:31:32 +00:00
Sector l1fs, l1bs, l2fs, l2bs;
// Get sector references
if(other.front != null) l1fs = other.front.Sector; else l1fs = null;
if(other.back != null) l1bs = other.back.Sector; else l1bs = null;
if(this.front != null) l2fs = this.front.Sector; else l2fs = null;
if(this.back != null) l2bs = this.back.Sector; else l2bs = null;
// Compare front sectors
if(l1fs == l2fs)
{
// Copy textures
if(other.front != null) other.front.AddTexturesTo(this.back);
if(this.front != null) this.front.AddTexturesTo(other.back);
2007-12-04 19:22:14 +00:00
2007-12-26 00:31:32 +00:00
// Change sidedefs
JoinChangeSidedefs(other, true, back);
}
// Compare back sectors
else if(l1bs == l2bs)
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.front);
if(this.back != null) this.back.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, front);
}
// Compare front and back
else if(l1fs == l2bs)
{
// Copy textures
if(other.front != null) other.front.AddTexturesTo(this.front);
if(this.back != null) this.back.AddTexturesTo(other.back);
// Change sidedefs
JoinChangeSidedefs(other, true, front);
}
// Compare back and front
else if(l1bs == l2fs)
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.back);
if(this.front != null) this.front.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, back);
}
else
{
// Other line single sided?
if(other.back == null)
{
// This line with its back to the other?
if(this.start == other.end)
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.front);
if(this.back != null) this.back.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, front);
}
else
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.back);
if(this.front != null) this.front.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, back);
}
}
// This line single sided?
if(this.back == null)
{
// Other line with its back to this?
if(other.start == this.end)
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.front);
if(this.back != null) this.back.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, front);
}
else
{
// Copy textures
if(other.front != null) other.front.AddTexturesTo(this.front);
if(this.back != null) this.back.AddTexturesTo(other.back);
// Change sidedefs
JoinChangeSidedefs(other, true, front);
}
}
else
{
// This line with its back to the other?
if(this.start == other.end)
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.front);
if(this.back != null) this.back.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, front);
}
else
{
// Copy textures
if(other.back != null) other.back.AddTexturesTo(this.back);
if(this.front != null) this.front.AddTexturesTo(other.front);
// Change sidedefs
JoinChangeSidedefs(other, false, back);
}
}
}
// If either of the two lines was selected, keep the other selected
if(this.selected) other.selected = true;
// Apply single/double sided flags
other.ApplySidedFlags();
// I got killed by the other.
2007-12-04 19:22:14 +00:00
this.Dispose();
2007-12-01 18:29:58 +00:00
}
2007-12-26 00:31:32 +00:00
// This changes sidedefs (used for joining lines)
private void JoinChangeSidedefs(Linedef other, bool front, Sidedef newside)
{
Sidedef sd;
// Change sidedefs
if(front)
{
if(other.front != null) other.front.Dispose();
}
else
{
if(other.back != null) other.back.Dispose();
}
if(newside != null)
{
sd = map.CreateSidedef(other, front, newside.Sector);
newside.CopyPropertiesTo(sd);
}
}
2007-06-13 19:39:38 +00:00
#endregion
2007-06-24 18:56:43 +00:00
#region ================== Changes
// This updates all properties
public void Update(int flags, int tag, int action, byte[] args)
{
// Apply changes
this.flags = flags;
this.tag = tag;
this.action = action;
this.args = new byte[NUM_ARGS];
args.CopyTo(this.args, 0);
this.updateneeded = true;
2007-06-24 18:56:43 +00:00
}
#endregion
2007-06-13 19:39:38 +00:00
}
}