From b0580a905082bb799a668d3e6d02f9b38dd6203f Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Thu, 18 Apr 2019 12:05:11 +0300 Subject: [PATCH] (7e43eae73) Optimized Item.ApplyStatusEffects & ItemComponent.PlaySound: check if the item has effects/sounds of the correct type from a bool array instead of doing a dictionary lookup. A small thing, but the methods are called so frequently that it adds up to a lot. --- .../Source/Items/Components/ItemComponent.cs | 11 ++-- .../Items/Components/Holdable/RangedWeapon.cs | 6 +- .../Source/Items/Components/ItemComponent.cs | 1 + .../Items/Components/Signal/LightComponent.cs | 1 + .../BarotraumaShared/Source/Items/Item.cs | 59 ++++++++++++++----- 5 files changed, 57 insertions(+), 21 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/Components/ItemComponent.cs b/Barotrauma/BarotraumaClient/Source/Items/Components/ItemComponent.cs index d2e5c4a67..a552ed0ee 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Components/ItemComponent.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Components/ItemComponent.cs @@ -48,6 +48,7 @@ namespace Barotrauma.Items.Components partial class ItemComponent : ISerializableEntity { + private bool[] hasSoundsOfType; private Dictionary> sounds; private Dictionary soundSelectionModes; @@ -183,6 +184,8 @@ namespace Barotrauma.Items.Components private SoundChannel loopingSoundChannel; public void PlaySound(ActionType type, Vector2 position, Character user = null) { + if (!hasSoundsOfType[(int)type]) { return; } + if (loopingSound != null) { if (Vector3.DistanceSquared(GameMain.SoundManager.ListenerPosition, new Vector3(position.X, position.Y, 0.0f)) > loopingSound.Range * loopingSound.Range) @@ -224,10 +227,9 @@ namespace Barotrauma.Items.Components } return; } - - if (!sounds.TryGetValue(type, out List matchingSounds)) return; - + ItemSound itemSound = null; + var matchingSounds = sounds[type]; if (loopingSoundChannel == null || !loopingSoundChannel.IsPlaying) { SoundSelectionMode soundSelectionMode = soundSelectionModes[type]; @@ -262,7 +264,7 @@ namespace Barotrauma.Items.Components private void PlaySound(ItemSound itemSound, Vector2 position, Character user = null) { - if (Vector3.DistanceSquared(GameMain.SoundManager.ListenerPosition, new Vector3(position.X, position.Y, 0.0f)) > itemSound.Range * itemSound.Range) + if (Vector2.DistanceSquared(new Vector2(GameMain.SoundManager.ListenerPosition.X, GameMain.SoundManager.ListenerPosition.Y), position) > itemSound.Range * itemSound.Range) { return; } @@ -451,6 +453,7 @@ namespace Barotrauma.Items.Components { soundList = new List(); sounds.Add(itemSound.Type, soundList); + hasSoundsOfType[(int)itemSound.Type] = true; } soundList.Add(itemSound); diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/RangedWeapon.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/RangedWeapon.cs index 966bd5761..b15306910 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/RangedWeapon.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Holdable/RangedWeapon.cs @@ -122,10 +122,12 @@ namespace Barotrauma.Items.Components foreach (Item subItem in containedSubItems) { projectile = subItem.GetComponent(); - //apply OnUse statuseffects to the container in case it has to react to it somehow //(play a sound, spawn more projectiles, reduce condition...) - subItem.GetComponent()?.Item.ApplyStatusEffects(ActionType.OnUse, deltaTime); + if (subItem.Condition > 0.0f) + { + subItem.GetComponent()?.Item.ApplyStatusEffects(ActionType.OnUse, deltaTime); + } if (projectile != null) break; } } diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs index 50d9d7ce1..831c07c39 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/ItemComponent.cs @@ -219,6 +219,7 @@ namespace Barotrauma.Items.Components requiredSkills = new List(); #if CLIENT + hasSoundsOfType = new bool[Enum.GetValues(typeof(ActionType)).Length]; sounds = new Dictionary>(); #endif diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs index b7d2c38c2..56de0dfcd 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs @@ -133,6 +133,7 @@ namespace Barotrauma.Items.Components set { + if (base.IsActive == value) { return; } base.IsActive = value; #if CLIENT if (light == null) return; diff --git a/Barotrauma/BarotraumaShared/Source/Items/Item.cs b/Barotrauma/BarotraumaShared/Source/Items/Item.cs index 2a1b62490..9f3015ea9 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Item.cs @@ -76,6 +76,7 @@ namespace Barotrauma private List repairables; //a dictionary containing lists of the status effects in all the components of the item + private bool[] hasStatusEffectsOfType; private Dictionary> statusEffectLists; public Dictionary SerializableProperties { get; protected set; } @@ -557,12 +558,15 @@ namespace Barotrauma } } + hasStatusEffectsOfType = new bool[Enum.GetValues(typeof(ActionType)).Length]; foreach (ItemComponent ic in components) { if (ic.statusEffectLists == null) continue; if (statusEffectLists == null) + { statusEffectLists = new Dictionary>(); + } //go through all the status effects of the component //and add them to the corresponding statuseffect list @@ -573,6 +577,7 @@ namespace Barotrauma { statusEffectList = new List(); statusEffectLists.Add(actionType, statusEffectList); + hasStatusEffectsOfType[(int)actionType] = true; } foreach (StatusEffect effect in componentEffectList) @@ -923,14 +928,9 @@ namespace Barotrauma public void ApplyStatusEffects(ActionType type, float deltaTime, Character character = null, Limb limb = null, bool isNetworkEvent = false) { - if (statusEffectLists == null) return; - - if (!statusEffectLists.TryGetValue(type, out List statusEffects)) return; - - bool broken = condition <= 0.0f; - foreach (StatusEffect effect in statusEffects) + if (!hasStatusEffectsOfType[(int)type]) { return; } + foreach (StatusEffect effect in statusEffectLists[type]) { - if (broken && effect.type != ActionType.OnBroken) continue; ApplyStatusEffect(effect, type, deltaTime, character, limb, isNetworkEvent, false); } } @@ -1047,6 +1047,8 @@ namespace Barotrauma aiTarget.SoundRange -= deltaTime * 1000.0f; } + bool broken = condition <= 0.0f; + if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsServer) { sendConditionUpdateTimer -= deltaTime; @@ -1081,17 +1083,16 @@ namespace Barotrauma if (!ic.IsActive) continue; - if (condition > 0.0f) + if (broken) { - ic.Update(deltaTime, cam); - -#if CLIENT - if (ic.IsActive) ic.PlaySound(ActionType.OnActive, WorldPosition); -#endif + ic.UpdateBroken(deltaTime, cam); } else { - ic.UpdateBroken(deltaTime, cam); + ic.Update(deltaTime, cam); +#if CLIENT + if (ic.IsActive) ic.PlaySound(ActionType.OnActive, WorldPosition); +#endif } } @@ -1123,6 +1124,10 @@ namespace Barotrauma container = container.Container; } } + if (!broken) + { + ApplyStatusEffects(!waterProof && inWater ? ActionType.InWater : ActionType.NotInWater, deltaTime); + } ApplyStatusEffects(!waterProof && inWater ? ActionType.InWater : ActionType.NotInWater, deltaTime); if (body == null || !body.Enabled || !inWater || ParentInventory != null || Removed) { return; } @@ -1159,6 +1164,30 @@ namespace Barotrauma } } + FindHull(); + + if (Submarine == null && prevSub != null) + { + body.SetTransform(body.SimPosition + prevSub.SimPosition, body.Rotation); + } + else if (Submarine != null && prevSub == null) + { + body.SetTransform(body.SimPosition - Submarine.SimPosition, body.Rotation); + } + + Vector2 displayPos = ConvertUnits.ToDisplayUnits(body.SimPosition); + rect.X = (int)(displayPos.X - rect.Width / 2.0f); + rect.Y = (int)(displayPos.Y + rect.Height / 2.0f); + + if (Math.Abs(body.LinearVelocity.X) > NetConfig.MaxPhysicsBodyVelocity || + Math.Abs(body.LinearVelocity.Y) > NetConfig.MaxPhysicsBodyVelocity) + { + body.LinearVelocity = new Vector2( + MathHelper.Clamp(body.LinearVelocity.X, -NetConfig.MaxPhysicsBodyVelocity, NetConfig.MaxPhysicsBodyVelocity), + MathHelper.Clamp(body.LinearVelocity.Y, -NetConfig.MaxPhysicsBodyVelocity, NetConfig.MaxPhysicsBodyVelocity)); + } + } + /// /// Applies buoyancy, drag and angular drag caused by water /// @@ -1202,7 +1231,7 @@ namespace Barotrauma if (GameMain.NetworkMember != null && GameMain.NetworkMember.IsClient) { return true; } - if (ImpactTolerance > 0.0f && impact > ImpactTolerance) + if (ImpactTolerance > 0.0f && condition > 0.0f && impact > ImpactTolerance) { ApplyStatusEffects(ActionType.OnImpact, 1.0f); #if SERVER