v0.11.0.9
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
public class IdRemap
|
||||
{
|
||||
public static readonly IdRemap DiscardId = new IdRemap(null, -1);
|
||||
|
||||
private int maxId;
|
||||
|
||||
private List<Point> srcRanges;
|
||||
private int destOffset;
|
||||
|
||||
public IdRemap(XElement parentElement, int offset)
|
||||
{
|
||||
destOffset = offset;
|
||||
if (parentElement != null)
|
||||
{
|
||||
srcRanges = new List<Point>();
|
||||
foreach (XElement subElement in parentElement.Elements())
|
||||
{
|
||||
int id = subElement.GetAttributeInt("ID", -1);
|
||||
if (id > 0) { InsertId(id); }
|
||||
}
|
||||
maxId = GetOffsetId(srcRanges.Last().Y + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
maxId = offset + 1;
|
||||
}
|
||||
}
|
||||
|
||||
public ushort AssignMaxId()
|
||||
{
|
||||
maxId++;
|
||||
return (ushort)maxId;
|
||||
}
|
||||
|
||||
private void InsertId(int id)
|
||||
{
|
||||
for (int i=0;i<srcRanges.Count;i++)
|
||||
{
|
||||
if (srcRanges[i].X > id)
|
||||
{
|
||||
if (srcRanges[i].X == (id + 1))
|
||||
{
|
||||
srcRanges[i] = new Point(id, srcRanges[i].Y);
|
||||
if (i > 0 && srcRanges[i].X == srcRanges[i - 1].Y)
|
||||
{
|
||||
srcRanges[i - 1] = new Point(srcRanges[i - 1].X, srcRanges[i].Y);
|
||||
srcRanges.RemoveAt(i);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
srcRanges.Insert(i, new Point(id, id));
|
||||
}
|
||||
return;
|
||||
}
|
||||
else if (srcRanges[i].Y < id)
|
||||
{
|
||||
if (srcRanges[i].Y == (id - 1))
|
||||
{
|
||||
srcRanges[i] = new Point(srcRanges[i].X, id);
|
||||
if (i < (srcRanges.Count-1) && srcRanges[i].Y == srcRanges[i + 1].X)
|
||||
{
|
||||
srcRanges[i] = new Point(srcRanges[i].X, srcRanges[i + 1].Y);
|
||||
srcRanges.RemoveAt(i+1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
srcRanges.Add(new Point(id, id));
|
||||
}
|
||||
|
||||
public ushort GetOffsetId(XElement element)
|
||||
{
|
||||
return GetOffsetId(element.GetAttributeInt("ID", 0));
|
||||
}
|
||||
|
||||
public ushort GetOffsetId(int id)
|
||||
{
|
||||
if (id <= 0) { return 0; }
|
||||
if (destOffset < 0) { return 0; }
|
||||
if (srcRanges == null) { return (ushort)(id + destOffset); }
|
||||
|
||||
int currOffset = destOffset;
|
||||
for (int i=0;i<srcRanges.Count;i++)
|
||||
{
|
||||
if (id >= srcRanges[i].X && (id <= srcRanges[i].Y || (i == srcRanges.Count-1)))
|
||||
{
|
||||
return (ushort)(id - srcRanges[i].X + 1 + currOffset);
|
||||
}
|
||||
currOffset += srcRanges[i].Y - srcRanges[i].X + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ namespace Barotrauma
|
||||
TopLeft = (Top | Left), TopCenter = (CenterX | Top), TopRight = (Top | Right),
|
||||
CenterLeft = (Left | CenterY), Center = (CenterX | CenterY), CenterRight = (Right | CenterY),
|
||||
BottomLeft = (Bottom | Left), BottomCenter = (CenterX | Bottom), BottomRight = (Bottom | Right),
|
||||
Any = Left | Right | Top | Bottom | Center
|
||||
}
|
||||
|
||||
static class MathUtils
|
||||
@@ -368,7 +369,7 @@ namespace Barotrauma
|
||||
|
||||
public static bool GetLineRectangleIntersection(Vector2 a1, Vector2 a2, Rectangle rect, out Vector2 intersection)
|
||||
{
|
||||
if (GetAxisAlignedLineIntersection(a1, a2,
|
||||
if (GetAxisAlignedLineIntersection(a1, a2,
|
||||
new Vector2(rect.X, rect.Y),
|
||||
new Vector2(rect.Right, rect.Y),
|
||||
true, out intersection))
|
||||
@@ -377,14 +378,14 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
if (GetAxisAlignedLineIntersection(a1, a2,
|
||||
new Vector2(rect.X, rect.Y-rect.Height),
|
||||
new Vector2(rect.Right, rect.Y-rect.Height),
|
||||
new Vector2(rect.X, rect.Y - rect.Height),
|
||||
new Vector2(rect.Right, rect.Y - rect.Height),
|
||||
true, out intersection))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if(GetAxisAlignedLineIntersection(a1, a2,
|
||||
if (GetAxisAlignedLineIntersection(a1, a2,
|
||||
new Vector2(rect.X, rect.Y),
|
||||
new Vector2(rect.X, rect.Y - rect.Height),
|
||||
false, out intersection))
|
||||
@@ -548,8 +549,72 @@ namespace Barotrauma
|
||||
}
|
||||
|
||||
float numerator = xDiff * (lineA.Y - point.Y) - yDiff * (lineA.X - point.X);
|
||||
return (numerator*numerator) /
|
||||
(xDiff * xDiff + yDiff * yDiff);
|
||||
return (numerator * numerator) / (xDiff * xDiff + yDiff * yDiff);
|
||||
}
|
||||
|
||||
public static double LineSegmentToPointDistanceSquared(Point lineA, Point lineB, Point point)
|
||||
{
|
||||
double xDiff = lineB.X - lineA.X;
|
||||
double yDiff = lineB.Y - lineA.Y;
|
||||
|
||||
if (xDiff == 0 && yDiff == 0)
|
||||
{
|
||||
double v1 = lineA.X - point.X;
|
||||
double v2 = lineA.Y - point.Y;
|
||||
return (v1 * v1) + (v2 * v2);
|
||||
}
|
||||
|
||||
// Calculate the t that minimizes the distance.
|
||||
double t = ((point.X - lineA.X) * xDiff + (point.Y - lineA.Y) * yDiff) / (xDiff * xDiff + yDiff * yDiff);
|
||||
|
||||
// See if this represents one of the segment's
|
||||
// end points or a point in the middle.
|
||||
if (t < 0)
|
||||
{
|
||||
xDiff = point.X - lineA.X;
|
||||
yDiff = point.Y - lineA.Y;
|
||||
}
|
||||
else if (t > 1)
|
||||
{
|
||||
xDiff = point.X - lineB.X;
|
||||
yDiff = point.Y - lineB.Y;
|
||||
}
|
||||
else
|
||||
{
|
||||
xDiff = point.X - (lineA.X + t * xDiff);
|
||||
yDiff = point.Y - (lineA.Y + t * yDiff);
|
||||
}
|
||||
|
||||
return xDiff * xDiff + yDiff * yDiff;
|
||||
}
|
||||
|
||||
public static Vector2 GetClosestPointOnLineSegment(Vector2 lineA, Vector2 lineB, Vector2 point)
|
||||
{
|
||||
float xDiff = lineB.X - lineA.X;
|
||||
float yDiff = lineB.Y - lineA.Y;
|
||||
|
||||
if (xDiff == 0 && yDiff == 0)
|
||||
{
|
||||
return lineA;
|
||||
}
|
||||
|
||||
// Calculate the t that minimizes the distance.
|
||||
float t = ((point.X - lineA.X) * xDiff + (point.Y - lineA.Y) * yDiff) / (xDiff * xDiff + yDiff * yDiff);
|
||||
|
||||
// See if this represents one of the segment's
|
||||
// end points or a point in the middle.
|
||||
if (t < 0)
|
||||
{
|
||||
return lineA;
|
||||
}
|
||||
else if (t > 1)
|
||||
{
|
||||
return lineB;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Vector2(lineA.X + t * xDiff, lineA.Y + t * yDiff);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CircleIntersectsRectangle(Vector2 circlePos, float radius, Rectangle rect)
|
||||
@@ -613,27 +678,11 @@ namespace Barotrauma
|
||||
public static List<Vector2[]> TriangulateConvexHull(List<Vector2> vertices, Vector2 center)
|
||||
{
|
||||
List<Vector2[]> triangles = new List<Vector2[]>();
|
||||
|
||||
int triangleCount = vertices.Count - 2;
|
||||
|
||||
vertices.Sort(new CompareCCW(center));
|
||||
|
||||
int lastIndex = 1;
|
||||
for (int i = 0; i < triangleCount; i++)
|
||||
for (int i = 0; i < vertices.Count; i++)
|
||||
{
|
||||
Vector2[] triangleVertices = new Vector2[3];
|
||||
triangleVertices[0] = vertices[0];
|
||||
int k = 1;
|
||||
for (int j = lastIndex; j <= lastIndex + 1; j++)
|
||||
{
|
||||
triangleVertices[k] = vertices[j];
|
||||
k++;
|
||||
}
|
||||
lastIndex += 1;
|
||||
|
||||
triangles.Add(triangleVertices);
|
||||
triangles.Add(new Vector2[3] { center, vertices[i], vertices[(i + 1) % vertices.Count] });
|
||||
}
|
||||
|
||||
return triangles;
|
||||
}
|
||||
|
||||
@@ -658,7 +707,7 @@ namespace Barotrauma
|
||||
|
||||
for (int i = 1; i < points.Count; i++)
|
||||
{
|
||||
if (points[i] == currPoint) continue;
|
||||
if (points[i].NearlyEquals(currPoint)) continue;
|
||||
if (currPoint == endPoint ||
|
||||
MathUtils.VectorOrientation(currPoint, endPoint, points[i]) == -1)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user