"Shifting" entity IDs to prevent them from overlapping between different subs, loading another submarine now works without crashing
This commit is contained in:
@@ -296,12 +296,12 @@ namespace Barotrauma
|
||||
case "edit":
|
||||
if (commands.Length>1)
|
||||
{
|
||||
Submarine.Load(string.Join(" ", commands.Skip(1)));
|
||||
Submarine.Load(string.Join(" ", commands.Skip(1)), true);
|
||||
}
|
||||
GameMain.EditMapScreen.Select();
|
||||
break;
|
||||
case "test":
|
||||
Submarine.Load("aegir mark ii");
|
||||
Submarine.Load("aegir mark ii", true);
|
||||
GameMain.DebugDraw = true;
|
||||
GameMain.LightManager.LosEnabled = false;
|
||||
GameMain.EditMapScreen.Select();
|
||||
@@ -450,10 +450,10 @@ namespace Barotrauma
|
||||
case "load":
|
||||
if (commands.Length < 2) break;
|
||||
|
||||
Submarine.Load(string.Join(" ", commands.Skip(1)));
|
||||
Submarine.Load(string.Join(" ", commands.Skip(1)), true);
|
||||
break;
|
||||
case "cleansub":
|
||||
for (int i = MapEntity.mapEntityList.Count-1; i>=0; i--)
|
||||
for (int i = MapEntity.mapEntityList.Count - 1; i >= 0; i--)
|
||||
{
|
||||
MapEntity me = MapEntity.mapEntityList[i];
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace Barotrauma
|
||||
|
||||
public static void StartTutorial(TutorialType tutorialType)
|
||||
{
|
||||
Submarine.Load("Content/Map/TutorialSub.sub", "");
|
||||
Submarine.Load("Content/Map/TutorialSub.sub", "", true);
|
||||
|
||||
|
||||
tutorialType.Initialize();
|
||||
|
||||
@@ -125,7 +125,10 @@ namespace Barotrauma
|
||||
return;
|
||||
}
|
||||
|
||||
if (reloadSub || Submarine.MainSub != submarine) submarine.Load();
|
||||
if (reloadSub || Submarine.MainSub != submarine) submarine.Load(true);
|
||||
|
||||
//var secondSub = new Submarine(submarine.FilePath, submarine.MD5Hash.Hash);
|
||||
//secondSub.Load(false);
|
||||
|
||||
if (level != null)
|
||||
{
|
||||
@@ -133,6 +136,8 @@ namespace Barotrauma
|
||||
|
||||
submarine.SetPosition(level.StartPosition - new Vector2(0.0f, 2000.0f));
|
||||
|
||||
//secondSub.SetPosition(level.EndPosition - new Vector2(0.0f, 2000.0f));
|
||||
|
||||
GameMain.GameScreen.BackgroundCreatureManager.SpawnSprites(80);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,8 +54,8 @@ namespace Barotrauma.Items.Components
|
||||
foreach (Hull hull in Hull.hullList)
|
||||
{
|
||||
Rectangle hullRect = new Rectangle(
|
||||
miniMap.X + (int)((hull.Rect.X - Submarine.HiddenSubPosition.X - item.Submarine.Borders.X) * size),
|
||||
miniMap.Y - (int)((hull.Rect.Y - Submarine.HiddenSubPosition.Y - item.Submarine.Borders.Y) * size),
|
||||
miniMap.X + (int)((hull.Rect.X - item.Submarine.HiddenSubPosition.X - item.Submarine.Borders.X) * size),
|
||||
miniMap.Y - (int)((hull.Rect.Y - item.Submarine.HiddenSubPosition.Y - item.Submarine.Borders.Y) * size),
|
||||
(int)(hull.Rect.Width * size),
|
||||
(int)(hull.Rect.Height * size));
|
||||
|
||||
|
||||
@@ -117,16 +117,16 @@ namespace Barotrauma.Items.Components
|
||||
|
||||
if (!addNode) break;
|
||||
|
||||
if (Nodes.Count > 0 && Nodes[0] == newConnection.Item.Position - Submarine.HiddenSubPosition) break;
|
||||
if (Nodes.Count > 1 && Nodes[Nodes.Count-1] == newConnection.Item.Position - Submarine.HiddenSubPosition) break;
|
||||
if (Nodes.Count > 0 && Nodes[0] == newConnection.Item.Position - item.Submarine.HiddenSubPosition) break;
|
||||
if (Nodes.Count > 1 && Nodes[Nodes.Count-1] == newConnection.Item.Position - item.Submarine.HiddenSubPosition) break;
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
Nodes.Insert(0, newConnection.Item.Position - Submarine.HiddenSubPosition);
|
||||
Nodes.Insert(0, newConnection.Item.Position - item.Submarine.HiddenSubPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
Nodes.Add(newConnection.Item.Position - Submarine.HiddenSubPosition);
|
||||
Nodes.Add(newConnection.Item.Position - item.Submarine.HiddenSubPosition);
|
||||
}
|
||||
|
||||
|
||||
@@ -200,7 +200,7 @@ namespace Barotrauma.Items.Components
|
||||
// position.Y += item.CurrentHull.Rect.Y - item.CurrentHull.Rect.Height;
|
||||
//}
|
||||
|
||||
newNodePos = RoundNode(item.Position, item.CurrentHull)-Submarine.HiddenSubPosition;
|
||||
newNodePos = RoundNode(item.Position, item.CurrentHull) - item.Submarine.HiddenSubPosition;
|
||||
|
||||
//if (Vector2.Distance(position, nodes[nodes.Count - 1]) > nodeDistance*10)
|
||||
//{
|
||||
@@ -366,7 +366,7 @@ namespace Barotrauma.Items.Components
|
||||
for (int i = 0; i < Nodes.Count; i++)
|
||||
{
|
||||
Vector2 worldPos = Nodes[i];
|
||||
if (item.Submarine != null) worldPos += item.Submarine.Position + Submarine.HiddenSubPosition;
|
||||
if (item.Submarine != null) worldPos += item.Submarine.Position + item.Submarine.HiddenSubPosition;
|
||||
worldPos.Y = -worldPos.Y;
|
||||
|
||||
GUI.DrawRectangle(spriteBatch, worldPos + new Vector2(-3, -3), new Vector2(6, 6), item.Color, true, 0.0f);
|
||||
@@ -407,7 +407,7 @@ namespace Barotrauma.Items.Components
|
||||
MapEntity.DisableSelect = true;
|
||||
//Nodes[(int)selectedNodeIndex] = GameMain.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition)-Submarine.HiddenSubPosition+Submarine.Loaded.Position;
|
||||
|
||||
Vector2 nodeWorldPos = GameMain.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition) - Submarine.HiddenSubPosition - item.Submarine.Position;// Nodes[(int)selectedNodeIndex];
|
||||
Vector2 nodeWorldPos = GameMain.EditMapScreen.Cam.ScreenToWorld(PlayerInput.MousePosition) - item.Submarine.HiddenSubPosition - item.Submarine.Position;// Nodes[(int)selectedNodeIndex];
|
||||
|
||||
nodeWorldPos.X = MathUtils.Round(nodeWorldPos.X, Submarine.GridSize.X / 2.0f);
|
||||
nodeWorldPos.Y = MathUtils.Round(nodeWorldPos.Y, Submarine.GridSize.Y / 2.0f);
|
||||
@@ -430,8 +430,8 @@ namespace Barotrauma.Items.Components
|
||||
{
|
||||
if (item.Submarine != null)
|
||||
{
|
||||
start += item.Submarine.DrawPosition + Submarine.HiddenSubPosition;
|
||||
end += item.Submarine.DrawPosition + Submarine.HiddenSubPosition;
|
||||
start += item.Submarine.DrawPosition + item.Submarine.HiddenSubPosition;
|
||||
end += item.Submarine.DrawPosition + item.Submarine.HiddenSubPosition;
|
||||
}
|
||||
|
||||
start.Y = -start.Y;
|
||||
|
||||
@@ -56,7 +56,8 @@ namespace Barotrauma
|
||||
|
||||
if (itemInfo.Second is Vector2)
|
||||
{
|
||||
Vector2 position = (Vector2)itemInfo.Second - Submarine.HiddenSubPosition;
|
||||
//todo: take multiple subs into account
|
||||
Vector2 position = (Vector2)itemInfo.Second - Submarine.MainSub.HiddenSubPosition;
|
||||
|
||||
items.Add(new Item(itemInfo.First, position, null));
|
||||
inventories.Add(null);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace Barotrauma
|
||||
transformedPosition -= entityGrid.Submarine.Position;
|
||||
}
|
||||
|
||||
entities.AddRange(entityGrid.GetEntities(position));
|
||||
entities.AddRange(entityGrid.GetEntities(transformedPosition));
|
||||
}
|
||||
|
||||
return entities;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -233,15 +233,17 @@ namespace Barotrauma
|
||||
|
||||
if (Submarine.MainSub != null)
|
||||
{
|
||||
cam.Position = Submarine.MainSub.Position + Submarine.HiddenSubPosition;
|
||||
cam.Position = Submarine.MainSub.Position + Submarine.MainSub.HiddenSubPosition;
|
||||
nameBox.Text = Submarine.MainSub.Name;
|
||||
descriptionBox.Text = ToolBox.LimitString(Submarine.MainSub.Description, 15);
|
||||
}
|
||||
else
|
||||
{
|
||||
cam.Position = Submarine.HiddenSubPosition;
|
||||
cam.Position = Submarine.MainSub.HiddenSubPosition;
|
||||
nameBox.Text = "";
|
||||
descriptionBox.Text = "";
|
||||
|
||||
Submarine.MainSub = new Submarine("", "", false);
|
||||
}
|
||||
|
||||
nameBox.Deselect();
|
||||
@@ -399,7 +401,7 @@ namespace Barotrauma
|
||||
|
||||
if (selectedSub == null) return false;
|
||||
|
||||
selectedSub.Load();
|
||||
selectedSub.Load(true);
|
||||
|
||||
nameBox.Text = selectedSub.Name;
|
||||
descriptionBox.Text = ToolBox.LimitString(selectedSub.Description,15);
|
||||
@@ -739,7 +741,7 @@ namespace Barotrauma
|
||||
if (GameMain.DebugDraw)
|
||||
{
|
||||
GUI.DrawLine(spriteBatch, new Vector2(0.0f, -cam.WorldView.Y), new Vector2(0.0f, -(cam.WorldView.Y - cam.WorldView.Height)), Color.White*0.5f, 1.0f, (int)(2.0f/cam.Zoom));
|
||||
GUI.DrawLine(spriteBatch, new Vector2(cam.WorldView.X, -Submarine.HiddenSubPosition.Y), new Vector2(cam.WorldView.Right, -Submarine.HiddenSubPosition.Y), Color.White * 0.5f, 1.0f, (int)(2.0f / cam.Zoom));
|
||||
GUI.DrawLine(spriteBatch, new Vector2(cam.WorldView.X, -Submarine.MainSub.HiddenSubPosition.Y), new Vector2(cam.WorldView.Right, -Submarine.MainSub.HiddenSubPosition.Y), Color.White * 0.5f, 1.0f, (int)(2.0f / cam.Zoom));
|
||||
}
|
||||
|
||||
Submarine.Draw(spriteBatch, true);
|
||||
|
||||
Reference in New Issue
Block a user