(63921aa0d) Implement GetVisibleHulls method.

This commit is contained in:
Joonas Rikkonen
2019-05-16 06:49:11 +03:00
parent 97b6888274
commit af7fcb823d
37 changed files with 386 additions and 548 deletions
+36 -77
View File
@@ -78,11 +78,30 @@ namespace Barotrauma
private float[] leftDelta;
private float[] rightDelta;
public List<Gap> ConnectedGaps;
public readonly List<Gap> ConnectedGaps = new List<Gap>();
public readonly List<Gap> ConnectedGaps = new List<Gap>();
private string roomName;
[Editable, Serialize("", true, translationTextTag: "RoomName.")]
public string RoomName
{
get { return roomName; }
set
{
if (roomName == value) { return; }
roomName = value;
DisplayName = TextManager.Get(roomName, returnNull: true) ?? roomName;
}
}
public string DisplayName
{
get;
private set;
}
private string roomName;
[Editable, Serialize("", true, translationTextTag: "RoomName.")]
public string RoomName
@@ -838,9 +857,6 @@ namespace Barotrauma
OxygenPercentage = 100.0f;
FireSources = new List<FireSource>();
linkedTo = new System.Collections.ObjectModel.ObservableCollection<MapEntity>();
properties = SerializableProperty.GetProperties(this);
@@ -1205,93 +1221,36 @@ namespace Barotrauma
FireSources.Remove(fire);
}
public IEnumerable<Hull> GetConnectedHulls(int? searchDepth)
private HashSet<Hull> adjacentHulls = new HashSet<Hull>();
public IEnumerable<Hull> GetConnectedHulls(bool includingThis, int? searchDepth = null)
{
return GetAdjacentHulls(new HashSet<Hull>(), 0, searchDepth);
adjacentHulls.Clear();
int startStep = 0;
return GetAdjacentHulls(includingThis, adjacentHulls, ref startStep, searchDepth);
}
private HashSet<Hull> GetAdjacentHulls(HashSet<Hull> connectedHulls, int steps, int? searchDepth)
private HashSet<Hull> GetAdjacentHulls(bool includingThis, HashSet<Hull> connectedHulls, ref int step, int? searchDepth)
{
if (distance >= maxDistance) return float.MaxValue;
if (this == target)
if (includingThis)
{
return distance + Vector2.Distance(startPos, endPos);
connectedHulls.Add(this);
}
if (step > searchDepth.Value)
{
return connectedHulls;
}
connectedHulls.Add(this);
foreach (Gap g in ConnectedGaps)
{
if (g.ConnectedDoor != null)
{
//gap blocked if the door is not open or the predicted state is not open
if (!g.ConnectedDoor.IsOpen || (g.ConnectedDoor.PredictedState.HasValue && !g.ConnectedDoor.PredictedState.Value))
{
if (g.ConnectedDoor.OpenState < 0.1f) continue;
}
}
else if (g.Open <= 0.0f)
{
continue;
}
for (int i = 0; i < 2 && i < g.linkedTo.Count; i++)
{
if (g.linkedTo[i] is Hull hull && !connectedHulls.Contains(hull))
{
float dist = hull.GetApproximateHullDistance(g.Position, endPos, connectedHulls, target, distance + Vector2.Distance(startPos, g.Position), maxDistance);
if (dist < float.MaxValue) return dist;
step++;
hull.GetAdjacentHulls(true, connectedHulls, ref step, searchDepth);
}
}
}
return float.MaxValue;
}
/// <summary>
/// Approximate distance from this hull to the target hull, moving through open gaps without passing through walls.
/// Uses a greedy algo and may not use the most optimal path. Returns float.MaxValue if no path is found.
/// </summary>
public float GetApproximateDistance(Vector2 startPos, Vector2 endPos, Hull targetHull, float maxDistance)
{
return GetApproximateHullDistance(startPos, endPos, new HashSet<Hull>(), targetHull, 0.0f, maxDistance);
}
private float GetApproximateHullDistance(Vector2 startPos, Vector2 endPos, HashSet<Hull> connectedHulls, Hull target, float distance, float maxDistance)
{
if (distance >= maxDistance) return float.MaxValue;
if (this == target)
{
return distance + Vector2.Distance(startPos, endPos);
}
connectedHulls.Add(this);
foreach (Gap g in ConnectedGaps)
{
if (g.ConnectedDoor != null)
{
//gap blocked if the door is not open or the predicted state is not open
if (!g.ConnectedDoor.IsOpen || (g.ConnectedDoor.PredictedState.HasValue && !g.ConnectedDoor.PredictedState.Value))
{
if (g.ConnectedDoor.OpenState < 0.1f) continue;
}
}
else if (g.Open <= 0.0f)
{
continue;
}
for (int i = 0; i < 2 && i < g.linkedTo.Count; i++)
{
if (g.linkedTo[i] is Hull hull && !connectedHulls.Contains(hull))
{
hull.GetAdjacentHulls(connectedHulls, steps++, searchDepth);
}
}
}
return float.MaxValue;
return connectedHulls;
}
/// <summary>
@@ -20,12 +20,12 @@ namespace Barotrauma
protected List<ushort> linkedToID;
//observable collection because some entities may need to be notified when the collection is modified
public ObservableCollection<MapEntity> linkedTo;
public readonly ObservableCollection<MapEntity> linkedTo = new ObservableCollection<MapEntity>();
private bool flippedX, flippedY;
public bool FlippedX { get { return flippedX; } }
public bool FlippedY { get { return flippedY; } }
public bool ShouldBeSaved = true;
//the position and dimensions of the entity
@@ -41,7 +41,7 @@ namespace Barotrauma
get { return isHighlighted || ExternalHighlight; }
set { isHighlighted = value; }
}
public virtual Rectangle Rect
{
get { return rect; }
@@ -161,7 +161,7 @@ namespace Barotrauma
// Quick undo/redo for size and movement only. TODO: Remove if we do a more general implementation.
private Memento<Rectangle> rectMemento;
public MapEntity(MapEntityPrefab prefab, Submarine submarine) : base(submarine)
public MapEntity(MapEntityPrefab prefab, Submarine submarine) : base(submarine)
{
this.prefab = prefab;
Scale = prefab != null ? prefab.Scale : 1;
@@ -289,7 +289,7 @@ namespace Barotrauma
mapEntityList.Insert(i, this);
}
/// <summary>
/// Remove the entity from the entity list without removing links to other entities
/// </summary>
@@ -409,7 +409,7 @@ namespace Barotrauma
try
{
MethodInfo loadMethod = t.GetMethod("Load", new [] { typeof(XElement), typeof(Submarine) });
MethodInfo loadMethod = t.GetMethod("Load", new[] { typeof(XElement), typeof(Submarine) });
if (loadMethod == null)
{
DebugConsole.ThrowError("Could not find the method \"Load\" in " + t + ".");
@@ -499,6 +499,38 @@ namespace Barotrauma
if (linkedTo.Contains(e)) linkedTo.Remove(e);
}
/// <summary>
/// Gets all linked entities of specific type.
/// </summary>
public HashSet<T> GetLinkedEntities<T>(HashSet<T> list = null, int? maxDepth = null, Func<T, bool> filter = null) where T : MapEntity
{
list = list ?? new HashSet<T>();
int startDepth = 0;
GetLinkedEntitiesRecursive<T>(this, list, ref startDepth, maxDepth, filter);
return list;
}
/// <summary>
/// Gets all linked entities of specific type.
/// </summary>
private static void GetLinkedEntitiesRecursive<T>(MapEntity mapEntity, HashSet<T> linkedTargets, ref int depth, int? maxDepth = null, Func<T, bool> filter = null)
where T : MapEntity
{
if (depth > maxDepth) { return; }
foreach (var linkedEntity in mapEntity.linkedTo)
{
if (linkedEntity is T linkedTarget)
{
if (!linkedTargets.Contains(linkedTarget) && (filter == null || filter(linkedTarget)))
{
linkedTargets.Add(linkedTarget);
depth++;
GetLinkedEntitiesRecursive(linkedEntity, linkedTargets, ref depth, maxDepth, filter);
}
}
}
}
#region Serialized properties
// We could use NaN or nullables, but in this case the first is not preferable, because it needs to be checked every time the value is used.
// Nullable on the other requires boxing that we don't want to do too often, since it generates garbage.
@@ -115,7 +115,6 @@ namespace Barotrauma
Linkable = true
};
ep.AllowedLinks.Add("hull");
ep.Aliases = new HashSet<string> { "hull" };
List.Add(ep);
ep = new MapEntityPrefab
@@ -225,6 +225,12 @@ namespace Barotrauma
string nonTranslatedName = element.GetAttributeString("name", null) ?? element.Name.ToString();
sp.Aliases.Add(nonTranslatedName.ToLowerInvariant());
SerializableProperty.DeserializeProperties(sp, element);
if (sp.Body)
{
sp.Tags.Add("wall");
}
SerializableProperty.DeserializeProperties(sp, element);
if (sp.Body)
{