From cf822cfae6687c8f2edf074ad9a683fede325d55 Mon Sep 17 00:00:00 2001 From: Regalis Date: Sun, 12 Jun 2016 14:23:08 +0300 Subject: [PATCH] Submarine saving fixes, impact damage in sub-sub collisions, submarine ambient sounds --- Subsurface/Source/Items/Item.cs | 2 ++ Subsurface/Source/Items/ItemSpawner.cs | 41 +++++++++++----------- Subsurface/Source/Map/Submarine.cs | 12 +++---- Subsurface/Source/Map/SubmarineBody.cs | 33 ++++++++++++----- Subsurface/Source/Networking/GameServer.cs | 8 ++--- Subsurface/Source/Screens/EditMapScreen.cs | 4 +-- Subsurface/Source/Sounds/SoundPlayer.cs | 28 +++++++++++---- 7 files changed, 80 insertions(+), 48 deletions(-) diff --git a/Subsurface/Source/Items/Item.cs b/Subsurface/Source/Items/Item.cs index 884d23797..b8e908275 100644 --- a/Subsurface/Source/Items/Item.cs +++ b/Subsurface/Source/Items/Item.cs @@ -1438,6 +1438,8 @@ namespace Barotrauma element.Add(new XAttribute("name", prefab.Name), new XAttribute("ID", ID)); + + System.Diagnostics.Debug.Assert(Submarine != null); if (ResizeHorizontal || ResizeVertical) { diff --git a/Subsurface/Source/Items/ItemSpawner.cs b/Subsurface/Source/Items/ItemSpawner.cs index c72637a8f..7899ba652 100644 --- a/Subsurface/Source/Items/ItemSpawner.cs +++ b/Subsurface/Source/Items/ItemSpawner.cs @@ -13,20 +13,20 @@ namespace Barotrauma spawnQueue = new Queue>(); } - public void QueueItem(ItemPrefab itemPrefab, Vector2 position, bool isNetworkMessage = false) - { - if (!isNetworkMessage && GameMain.Client!=null) - { - //clients aren't allowed to spawn new items unless the server says so - return; - } + //public void QueueItem(ItemPrefab itemPrefab, Vector2 position, bool isNetworkMessage = false) + //{ + // if (!isNetworkMessage && GameMain.Client!=null) + // { + // //clients aren't allowed to spawn new items unless the server says so + // return; + // } - var itemInfo = new Pair(); - itemInfo.First = itemPrefab; - itemInfo.Second = position; + // var itemInfo = new Pair(); + // itemInfo.First = itemPrefab; + // itemInfo.Second = position; - spawnQueue.Enqueue(itemInfo); - } + // spawnQueue.Enqueue(itemInfo); + //} public void QueueItem(ItemPrefab itemPrefab, Inventory inventory, bool isNetworkMessage = false) { @@ -54,16 +54,17 @@ namespace Barotrauma { var itemInfo = spawnQueue.Dequeue(); - if (itemInfo.Second is Vector2) - { - //todo: take multiple subs into account - Vector2 position = (Vector2)itemInfo.Second - Submarine.MainSub.HiddenSubPosition; + //if (itemInfo.Second is Vector2) + //{ + // //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); + // items.Add(new Item(itemInfo.First, position, null)); + // inventories.Add(null); - } - else if (itemInfo.Second is Inventory) + //} + //else + if (itemInfo.Second is Inventory) { var item = new Item(itemInfo.First, Vector2.Zero, null); diff --git a/Subsurface/Source/Map/Submarine.cs b/Subsurface/Source/Map/Submarine.cs index 2469d85fd..d481627a5 100644 --- a/Subsurface/Source/Map/Submarine.cs +++ b/Subsurface/Source/Map/Submarine.cs @@ -526,7 +526,7 @@ namespace Barotrauma foreach (MapEntity e in MapEntity.mapEntityList) { - if (e.MoveWithLevel) continue; + if (e.MoveWithLevel ||e.Submarine != this) continue; e.Save(doc); } @@ -548,17 +548,15 @@ namespace Barotrauma public static bool SaveCurrent(string filePath) { - if (!loaded.Any()) + if (Submarine.MainSub == null) { - loaded.Add(new Submarine(filePath)); + Submarine.MainSub = new Submarine(filePath); // return; } - System.Diagnostics.Debug.Assert(loaded.Count==1); + Submarine.MainSub.filePath = filePath; - loaded.First().filePath = filePath; - - return loaded.First().SaveAs(filePath); + return Submarine.MainSub.SaveAs(filePath); } public void CheckForErrors() diff --git a/Subsurface/Source/Map/SubmarineBody.cs b/Subsurface/Source/Map/SubmarineBody.cs index 1688d683e..ad6dd7500 100644 --- a/Subsurface/Source/Map/SubmarineBody.cs +++ b/Subsurface/Source/Map/SubmarineBody.cs @@ -415,12 +415,9 @@ namespace Barotrauma public bool OnCollision(Fixture f1, Fixture f2, Contact contact) { - VoronoiCell cell = f2.Body.UserData as VoronoiCell; - - if (cell == null) + Limb limb = f2.Body.UserData as Limb; + if (limb!= null) { - Limb limb = f2.Body.UserData as Limb; - if (limb == null) return true; bool collision = HandleLimbCollision(contact, limb); @@ -436,12 +433,32 @@ namespace Barotrauma return collision; } - var collisionNormal = Vector2.Normalize(ConvertUnits.ToDisplayUnits(body.Position) - cell.Center); + VoronoiCell cell = f2.Body.UserData as VoronoiCell; + if (cell != null) + { + var collisionNormal = Vector2.Normalize(ConvertUnits.ToDisplayUnits(body.Position) - cell.Center); - float wallImpact = Vector2.Dot(Velocity, -collisionNormal); + float wallImpact = Vector2.Dot(Velocity, -collisionNormal); - ApplyImpact(wallImpact, -collisionNormal, contact); + ApplyImpact(wallImpact, -collisionNormal, contact); + return true; + } + + Submarine sub = f2.Body.UserData as Submarine; + if (sub != null) + { + Debug.Assert(sub != submarine); + + Vector2 normal; + FixedArray2 points; + contact.GetWorldManifold(out normal, out points); + + ApplyImpact(Vector2.Dot(Velocity - sub.Velocity, normal) / 2.0f, normal, contact); + + return true; + } + return true; } diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 0ea8f8c99..6edcd4ef6 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -364,9 +364,9 @@ namespace Barotrauma.Networking if (!(c is AICharacter) || c.IsDead) continue; //todo: take multiple subs into account - Vector2 diff = c.WorldPosition - Submarine.MainSub.WorldPosition; + //Vector2 diff = c.WorldPosition - Submarine.MainSub.WorldPosition; - if (FarseerPhysics.ConvertUnits.ToSimUnits(diff.Length()) > NetConfig.CharacterIgnoreDistance) continue; + //if (FarseerPhysics.ConvertUnits.ToSimUnits(diff.Length()) > NetConfig.CharacterIgnoreDistance) continue; new NetworkEvent(NetworkEventType.EntityUpdate, c.ID, false); } @@ -405,9 +405,9 @@ namespace Barotrauma.Networking if (c is AICharacter) { //todo: take multiple subs into account - Vector2 diff = c.WorldPosition - Submarine.MainSub.WorldPosition; + //Vector2 diff = c.WorldPosition - Submarine.MainSub.WorldPosition; - if (FarseerPhysics.ConvertUnits.ToSimUnits(diff.Length()) > NetConfig.CharacterIgnoreDistance) continue; + //if (FarseerPhysics.ConvertUnits.ToSimUnits(diff.Length()) > NetConfig.CharacterIgnoreDistance) continue; } new NetworkEvent(NetworkEventType.ImportantEntityUpdate, c.ID, false); diff --git a/Subsurface/Source/Screens/EditMapScreen.cs b/Subsurface/Source/Screens/EditMapScreen.cs index 00076ded1..977ddc69e 100644 --- a/Subsurface/Source/Screens/EditMapScreen.cs +++ b/Subsurface/Source/Screens/EditMapScreen.cs @@ -239,11 +239,11 @@ namespace Barotrauma } else { - cam.Position = Submarine.MainSub.HiddenSubPosition; + cam.Position = Vector2.Zero; nameBox.Text = ""; descriptionBox.Text = ""; - Submarine.MainSub = new Submarine("", "", false); + Submarine.MainSub = new Submarine(Path.Combine(Submarine.SavePath, "Unnamed.sub"), "", false); } nameBox.Deselect(); diff --git a/Subsurface/Source/Sounds/SoundPlayer.cs b/Subsurface/Source/Sounds/SoundPlayer.cs index 59606fefd..5266dbf48 100644 --- a/Subsurface/Source/Sounds/SoundPlayer.cs +++ b/Subsurface/Source/Sounds/SoundPlayer.cs @@ -183,7 +183,7 @@ namespace Barotrauma startDrone = null; } - //todo: ambient sounds for multiple subs + //stop submarine ambient sounds if no sub is loaded if (Submarine.MainSub == null) { for (int i = 0; i < waterAmbienceIndexes.Length; i++) @@ -213,17 +213,31 @@ namespace Barotrauma } //how fast the sub is moving, scaled to 0.0 -> 1.0 - float movementFactor = 0.0f; - if (Submarine.MainSub != null) - { - movementFactor = (Submarine.MainSub.Velocity == Vector2.Zero) ? 0.0f : Submarine.MainSub.Velocity.Length() / 5.0f; + float movementSoundVolume = 0.0f; + foreach (Submarine sub in Submarine.Loaded) + { + float movementFactor = (sub.Velocity == Vector2.Zero) ? 0.0f : sub.Velocity.Length() / 5.0f; movementFactor = MathHelper.Clamp(movementFactor, 0.0f, 1.0f); + + if (Character.Controlled==null || Character.Controlled.Submarine != sub) + { + float dist = Vector2.Distance(GameMain.GameScreen.Cam.WorldViewCenter, sub.WorldPosition); + movementFactor = movementFactor / Math.Max(dist / 1000.0f, 1.0f); + } + + movementSoundVolume = Math.Max(movementSoundVolume, movementFactor); } + //if (Submarine.MainSub != null) + //{ + // movementFactor = (Submarine.MainSub.Velocity == Vector2.Zero) ? 0.0f : Submarine.MainSub.Velocity.Length() / 5.0f; + // movementFactor = MathHelper.Clamp(movementFactor, 0.0f, 1.0f); + //} + SoundManager.LowPassHFGain = lowpassHFGain; - waterAmbienceIndexes[0] = waterAmbiences[0].Loop(waterAmbienceIndexes[0], ambienceVolume * (1.0f-movementFactor)); - waterAmbienceIndexes[1] = waterAmbiences[1].Loop(waterAmbienceIndexes[1], ambienceVolume * movementFactor); + waterAmbienceIndexes[0] = waterAmbiences[0].Loop(waterAmbienceIndexes[0], ambienceVolume * (1.0f - movementSoundVolume)); + waterAmbienceIndexes[1] = waterAmbiences[1].Loop(waterAmbienceIndexes[1], ambienceVolume * movementSoundVolume); }