"Shifting" entity IDs to prevent them from overlapping between different subs, loading another submarine now works without crashing

This commit is contained in:
Regalis
2016-06-10 20:53:35 +03:00
parent 7349cdd310
commit ee2d817e6b
14 changed files with 83 additions and 44 deletions

View File

@@ -81,7 +81,7 @@ namespace Barotrauma
//give an unique ID
bool IDfound;
id = 1;//Rand.Int(int.MaxValue);
id = submarine == null ? (ushort)1 : submarine.IdOffset;
do
{
id += 1;

View File

@@ -85,7 +85,7 @@ namespace Barotrauma
transformedPosition -= entityGrid.Submarine.Position;
}
entities.AddRange(entityGrid.GetEntities(position));
entities.AddRange(entityGrid.GetEntities(transformedPosition));
}
return entities;

View File

@@ -607,10 +607,11 @@ namespace Barotrauma
/// Has to be done after all the entities have been loaded (an entity can't
/// be linked to some other entity that hasn't been loaded yet)
/// </summary>
public static void MapLoaded()
public static void MapLoaded(Submarine sub)
{
foreach (MapEntity e in mapEntityList)
{
if (e.Submarine != sub) continue;
if (e.linkedToID == null) continue;
if (e.linkedToID.Count == 0) continue;
@@ -619,6 +620,7 @@ namespace Barotrauma
foreach (ushort i in e.linkedToID)
{
MapEntity linked = FindEntityByID(i) as MapEntity;
Debug.Assert(linked.Submarine == sub);
if (linked != null) e.linkedTo.Add(linked);
}
@@ -626,6 +628,7 @@ namespace Barotrauma
for (int i = 0; i<mapEntityList.Count; i++)
{
if (mapEntityList[i].Submarine != sub) continue;
mapEntityList[i].OnMapLoaded();
}

View File

@@ -22,9 +22,20 @@ namespace Barotrauma
{
public static string SavePath = "Submarines";
private static readonly Vector2 HiddenSubStartPosition = new Vector2(-50000.0f, 80000.0f);
//position of the "actual submarine" which is rendered wherever the SubmarineBody is
//should be in an unreachable place
public static readonly Vector2 HiddenSubPosition = new Vector2(-50000.0f, 80000.0f);
public Vector2 HiddenSubPosition
{
get;
private set;
}
public ushort IdOffset
{
get;
private set;
}
public static List<Submarine> SavedSubmarines = new List<Submarine>();
@@ -704,9 +715,9 @@ namespace Barotrauma
return doc;
}
public void Load()
public void Load(bool unloadPrevious)
{
Unload();
if (unloadPrevious) Unload();
Loading = true;
@@ -715,6 +726,19 @@ namespace Barotrauma
Description = ToolBox.GetAttributeString(doc.Root, "description", "");
HiddenSubPosition = HiddenSubStartPosition;
foreach (Submarine sub in Submarine.loaded)
{
HiddenSubPosition += Vector2.UnitY * (sub.Borders.Height + 5000.0f);
}
IdOffset = 0;
foreach (MapEntity me in MapEntity.mapEntityList)
{
IdOffset = Math.Max(IdOffset, me.ID);
}
foreach (XElement element in doc.Root.Elements())
{
string typeName = element.Name.ToString();
@@ -748,11 +772,13 @@ namespace Barotrauma
Vector2 center = Vector2.Zero;
if (Hull.hullList.Any())
var matchingHulls = Hull.hullList.FindAll(h => h.Submarine == this);
if (matchingHulls.Any())
{
Vector2 topLeft = new Vector2(Hull.hullList[0].Rect.X, Hull.hullList[0].Rect.Y);
Vector2 bottomRight = new Vector2(Hull.hullList[0].Rect.X, Hull.hullList[0].Rect.Y);
foreach (Hull hull in Hull.hullList)
Vector2 topLeft = new Vector2(matchingHulls[0].Rect.X, matchingHulls[0].Rect.Y);
Vector2 bottomRight = new Vector2(matchingHulls[0].Rect.X, matchingHulls[0].Rect.Y);
foreach (Hull hull in matchingHulls)
{
if (hull.Rect.X < topLeft.X) topLeft.X = hull.Rect.X;
if (hull.Rect.Y > topLeft.Y) topLeft.Y = hull.Rect.Y;
@@ -767,6 +793,8 @@ namespace Barotrauma
foreach (Item item in Item.ItemList)
{
if (item.Submarine != this) continue;
var wire = item.GetComponent<Items.Components.Wire>();
if (wire == null) continue;
@@ -778,7 +806,7 @@ namespace Barotrauma
for (int i = 0; i < MapEntity.mapEntityList.Count; i++)
{
if (MapEntity.mapEntityList[i].Submarine == null) continue;
if (MapEntity.mapEntityList[i].Submarine != this) continue;
MapEntity.mapEntityList[i].Move(-center);
}
@@ -799,7 +827,7 @@ namespace Barotrauma
Loading = false;
MapEntity.MapLoaded();
MapEntity.MapLoaded(this);
//WayPoint.GenerateSubWaypoints();
@@ -808,19 +836,19 @@ namespace Barotrauma
ID = ushort.MaxValue;
}
public static Submarine Load(string fileName)
public static Submarine Load(string fileName, bool unloadPrevious)
{
return Load(fileName, SavePath);
return Load(fileName, SavePath, unloadPrevious);
}
public static Submarine Load(string fileName, string folder)
public static Submarine Load(string fileName, string folder, bool unloadPrevious)
{
Unload();
if (unloadPrevious) Unload();
string path = string.IsNullOrWhiteSpace(folder) ? fileName : System.IO.Path.Combine(SavePath, fileName);
Submarine sub = new Submarine(path);
sub.Load();
sub.Load(false);
//Entity.dictionary.Add(int.MaxValue, sub);

View File

@@ -392,7 +392,7 @@ namespace Barotrauma
(Rand.Int(2) == 0) ? Borders.Y : Borders.Y - Borders.Height);
}
damagePos += submarine.Position + Submarine.HiddenSubPosition;
damagePos += submarine.Position + submarine.HiddenSubPosition;
SoundPlayer.PlayDamageSound(DamageSoundType.Pressure, 50.0f, damagePos, 10000.0f);
GameMain.GameScreen.Cam.Shake = depth * PressureDamageMultiplier * 0.1f;

View File

@@ -66,7 +66,7 @@ namespace Barotrauma
cam.Zoom = Math.Max(0.2f, cam.Zoom - CoroutineManager.UnscaledDeltaTime * 0.1f);
Vector2 cameraPos = sub.Position + Submarine.HiddenSubPosition;
Vector2 cameraPos = sub.Position + Submarine.MainSub.HiddenSubPosition;
cameraPos.Y = Math.Min(cameraPos.Y, ConvertUnits.ToDisplayUnits(Level.Loaded.ShaftBodies[0].Position.Y) - cam.WorldView.Height/2.0f);
GUI.ScreenOverlayColor = Color.Lerp(Color.TransparentBlack, Color.Black, timer/duration);

View File

@@ -357,7 +357,7 @@ namespace Barotrauma
borders.Width += outsideWaypointDist * 2;
borders.Height += outsideWaypointDist * 2;
borders.Location -= Submarine.HiddenSubPosition.ToPoint();
borders.Location -= submarine.HiddenSubPosition.ToPoint();
if (borders.Width <= outSideWaypointInterval*2)
{
@@ -379,7 +379,7 @@ namespace Barotrauma
for (float x = borders.X + outSideWaypointInterval; x < borders.Right - outSideWaypointInterval; x += outSideWaypointInterval)
{
var wayPoint = new WayPoint(
new Vector2(x, borders.Y - borders.Height * i) + Submarine.HiddenSubPosition,
new Vector2(x, borders.Y - borders.Height * i) + submarine.HiddenSubPosition,
SpawnType.Path, submarine);
if (x == borders.X + outSideWaypointInterval)
@@ -401,7 +401,7 @@ namespace Barotrauma
for (float y = borders.Y - borders.Height; y < borders.Y; y += outSideWaypointInterval)
{
wayPoint = new WayPoint(
new Vector2(borders.X + borders.Width * i, y) + Submarine.HiddenSubPosition,
new Vector2(borders.X + borders.Width * i, y) + submarine.HiddenSubPosition,
SpawnType.Path, submarine);
if (y == borders.Y - borders.Height)