From b49f0df05e0e6650d433e7149a0fd7cbfc9a3dbc Mon Sep 17 00:00:00 2001 From: Slava0135 <40753025+Slava0135@users.noreply.github.com> Date: Sun, 6 Aug 2023 16:44:41 +0300 Subject: [PATCH] fix reflected projectiles dealing stamina damage (#17648) --- .../Projectiles/ProjectileSystem.cs | 43 +++++++++---------- .../Damage/Systems/StaminaSystem.cs | 18 +++----- .../Projectiles/ProjectileCollideEvent.cs | 7 --- .../Projectiles/SharedProjectileSystem.cs | 26 ++++++----- .../Weapons/Reflect/SharedReflectSystem.cs | 6 +-- 5 files changed, 46 insertions(+), 54 deletions(-) delete mode 100644 Content.Shared/Projectiles/ProjectileCollideEvent.cs diff --git a/Content.Server/Projectiles/ProjectileSystem.cs b/Content.Server/Projectiles/ProjectileSystem.cs index e8a7bc0e45..345f10e852 100644 --- a/Content.Server/Projectiles/ProjectileSystem.cs +++ b/Content.Server/Projectiles/ProjectileSystem.cs @@ -31,55 +31,52 @@ public sealed class ProjectileSystem : SharedProjectileSystem if (args.OurFixture.ID != ProjectileFixture || !args.OtherFixture.Hard || component.DamagedEntity) return; - var otherEntity = args.OtherEntity; + var target = args.OtherEntity; // it's here so this check is only done once before possible hit var attemptEv = new ProjectileReflectAttemptEvent(uid, component, false); - RaiseLocalEvent(otherEntity, ref attemptEv); + RaiseLocalEvent(target, ref attemptEv); if (attemptEv.Cancelled) { - SetShooter(component, otherEntity); + SetShooter(component, target); return; } - var otherName = ToPrettyString(otherEntity); + var ev = new ProjectileHitEvent(target); + RaiseLocalEvent(uid, ref ev); + + var otherName = ToPrettyString(target); var direction = args.OurBody.LinearVelocity.Normalized(); - var modifiedDamage = _damageableSystem.TryChangeDamage(otherEntity, component.Damage, component.IgnoreResistances, origin: component.Shooter); - var deleted = Deleted(otherEntity); + var modifiedDamage = _damageableSystem.TryChangeDamage(target, component.Damage, component.IgnoreResistances, origin: component.Shooter); + var deleted = Deleted(target); if (modifiedDamage is not null && EntityManager.EntityExists(component.Shooter)) { if (modifiedDamage.Total > FixedPoint2.Zero && !deleted) { - RaiseNetworkEvent(new ColorFlashEffectEvent(Color.Red, new List { otherEntity }), Filter.Pvs(otherEntity, entityManager: EntityManager)); + RaiseNetworkEvent(new ColorFlashEffectEvent(Color.Red, new List { target }), Filter.Pvs(target, entityManager: EntityManager)); } _adminLogger.Add(LogType.BulletHit, - HasComp(otherEntity) ? LogImpact.Extreme : LogImpact.High, + HasComp(target) ? LogImpact.Extreme : LogImpact.High, $"Projectile {ToPrettyString(uid):projectile} shot by {ToPrettyString(component.Shooter):user} hit {otherName:target} and dealt {modifiedDamage.Total:damage} damage"); } if (!deleted) { - _guns.PlayImpactSound(otherEntity, modifiedDamage, component.SoundHit, component.ForceSound); - _sharedCameraRecoil.KickCamera(otherEntity, direction); + _guns.PlayImpactSound(target, modifiedDamage, component.SoundHit, component.ForceSound); + _sharedCameraRecoil.KickCamera(target, direction); } - var ev = new ProjectileCollideEvent(uid, false); - RaiseLocalEvent(args.OtherEntity, ref ev); + component.DamagedEntity = true; - if (!ev.Cancelled) + if (component.DeleteOnCollide) { - component.DamagedEntity = true; + QueueDel(uid); + } - if (component.DeleteOnCollide) - { - QueueDel(uid); - } - - if (component.ImpactEffect != null && TryComp(uid, out var xform)) - { - RaiseNetworkEvent(new ImpactEffectEvent(component.ImpactEffect, xform.Coordinates), Filter.Pvs(xform.Coordinates, entityMan: EntityManager)); - } + if (component.ImpactEffect != null && TryComp(uid, out var xform)) + { + RaiseNetworkEvent(new ImpactEffectEvent(component.ImpactEffect, xform.Coordinates), Filter.Pvs(xform.Coordinates, entityMan: EntityManager)); } } } diff --git a/Content.Shared/Damage/Systems/StaminaSystem.cs b/Content.Shared/Damage/Systems/StaminaSystem.cs index 439876d4cd..cb9ba4e242 100644 --- a/Content.Shared/Damage/Systems/StaminaSystem.cs +++ b/Content.Shared/Damage/Systems/StaminaSystem.cs @@ -7,13 +7,13 @@ using Content.Shared.Damage.Events; using Content.Shared.Database; using Content.Shared.IdentityManagement; using Content.Shared.Popups; +using Content.Shared.Projectiles; using Content.Shared.Rejuvenate; using Content.Shared.Rounding; using Content.Shared.Stunnable; using Content.Shared.Weapons.Melee.Events; using JetBrains.Annotations; using Robust.Shared.GameStates; -using Robust.Shared.Physics.Events; using Robust.Shared.Player; using Robust.Shared.Random; using Robust.Shared.Serialization; @@ -31,8 +31,6 @@ public sealed partial class StaminaSystem : EntitySystem [Dependency] private readonly SharedPopupSystem _popup = default!; [Dependency] private readonly SharedStunSystem _stunSystem = default!; - private const string CollideFixture = "projectile"; - /// /// How much of a buffer is there between the stun duration and when stuns can be re-applied. /// @@ -52,7 +50,7 @@ public sealed partial class StaminaSystem : EntitySystem SubscribeLocalEvent(OnDisarmed); SubscribeLocalEvent(OnRejuvenate); - SubscribeLocalEvent(OnCollide); + SubscribeLocalEvent(OnCollide); SubscribeLocalEvent(OnHit); } @@ -144,7 +142,7 @@ public sealed partial class StaminaSystem : EntitySystem return; var damage = args.PushProbability * component.CritThreshold; - TakeStaminaDamage(uid, damage, component, source:args.Source); + TakeStaminaDamage(uid, damage, component, source: args.Source); // We need a better method of getting if the entity is going to resist stam damage, both this and the lines in the foreach at the end of OnHit() are awful if (!component.Critical) @@ -204,7 +202,7 @@ public sealed partial class StaminaSystem : EntitySystem foreach (var (ent, comp) in toHit) { var oldDamage = comp.StaminaDamage; - TakeStaminaDamage(ent, damage / toHit.Count, comp, source:args.User, with:args.Weapon); + TakeStaminaDamage(ent, damage / toHit.Count, comp, source: args.User, with: args.Weapon); if (comp.StaminaDamage.Equals(oldDamage)) { _popup.PopupClient(Loc.GetString("stamina-resist"), ent, args.User); @@ -212,11 +210,9 @@ public sealed partial class StaminaSystem : EntitySystem } } - private void OnCollide(EntityUid uid, StaminaDamageOnCollideComponent component, ref StartCollideEvent args) + private void OnCollide(EntityUid uid, StaminaDamageOnCollideComponent component, ref ProjectileHitEvent args) { - if (!args.OurFixture.ID.Equals(CollideFixture)) return; - - TakeStaminaDamage(args.OtherEntity, component.Damage, source:args.OurEntity); + TakeStaminaDamage(args.Target, component.Damage, source: uid); } private void SetStaminaAlert(EntityUid uid, StaminaComponent? component = null) @@ -413,4 +409,4 @@ public sealed partial class StaminaSystem : EntitySystem /// Raised before stamina damage is dealt to allow other systems to cancel it. /// [ByRefEvent] -public record struct BeforeStaminaDamageEvent(float Value, bool Cancelled=false); +public record struct BeforeStaminaDamageEvent(float Value, bool Cancelled = false); diff --git a/Content.Shared/Projectiles/ProjectileCollideEvent.cs b/Content.Shared/Projectiles/ProjectileCollideEvent.cs deleted file mode 100644 index 0f3c463a3e..0000000000 --- a/Content.Shared/Projectiles/ProjectileCollideEvent.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Content.Shared.Projectiles; - -/// -/// Raised directed on what a projectile collides with. Can have its deletion cancelled. -/// -[ByRefEvent] -public record struct ProjectileCollideEvent(EntityUid OtherEntity, bool Cancelled); diff --git a/Content.Shared/Projectiles/SharedProjectileSystem.cs b/Content.Shared/Projectiles/SharedProjectileSystem.cs index c6629c5e7c..541a2dfbee 100644 --- a/Content.Shared/Projectiles/SharedProjectileSystem.cs +++ b/Content.Shared/Projectiles/SharedProjectileSystem.cs @@ -28,7 +28,7 @@ namespace Content.Shared.Projectiles { base.Initialize(); SubscribeLocalEvent(PreventCollision); - SubscribeLocalEvent(OnEmbedProjectileCollide); + SubscribeLocalEvent(OnEmbedProjectileHit); SubscribeLocalEvent(OnEmbedThrowDoHit); SubscribeLocalEvent(OnEmbedActivate); SubscribeLocalEvent(OnEmbedRemove); @@ -80,14 +80,14 @@ namespace Content.Shared.Projectiles Embed(uid, args.Target, component); } - private void OnEmbedProjectileCollide(EntityUid uid, EmbeddableProjectileComponent component, ref ProjectileCollideEvent args) + private void OnEmbedProjectileHit(EntityUid uid, EmbeddableProjectileComponent component, ref ProjectileHitEvent args) { - Embed(uid, args.OtherEntity, component); + Embed(uid, args.Target, component); // Raise a specific event for projectiles. if (TryComp(uid, out var projectile)) { - var ev = new ProjectileEmbedEvent(projectile.Shooter, projectile.Weapon, args.OtherEntity); + var ev = new ProjectileEmbedEvent(projectile.Shooter, projectile.Weapon, args.Target); RaiseLocalEvent(uid, ref ev); } } @@ -142,10 +142,16 @@ namespace Content.Shared.Projectiles Coordinates = coordinates; } } -} -/// -/// Raised when entity is just about to be hit with projectile but can reflect it -/// -[ByRefEvent] -public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled); + /// + /// Raised when entity is just about to be hit with projectile but can reflect it + /// + [ByRefEvent] + public record struct ProjectileReflectAttemptEvent(EntityUid ProjUid, ProjectileComponent Component, bool Cancelled); + + /// + /// Raised when projectile hits other entity + /// + [ByRefEvent] + public readonly record struct ProjectileHitEvent(EntityUid Target); +} diff --git a/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs b/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs index 302fd8ac9a..80ee7b97b1 100644 --- a/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs +++ b/Content.Shared/Weapons/Reflect/SharedReflectSystem.cs @@ -38,21 +38,21 @@ public abstract class SharedReflectSystem : EntitySystem SubscribeLocalEvent(OnHandReflectProjectile); SubscribeLocalEvent(OnHandsReflectHitscan); - SubscribeLocalEvent(OnReflectCollide); + SubscribeLocalEvent(OnReflectCollide); SubscribeLocalEvent(OnReflectHitscan); SubscribeLocalEvent(OnReflectEquipped); SubscribeLocalEvent(OnReflectUnequipped); } - private void OnReflectCollide(EntityUid uid, ReflectComponent component, ref ProjectileCollideEvent args) + private void OnReflectCollide(EntityUid uid, ReflectComponent component, ref ProjectileReflectAttemptEvent args) { if (args.Cancelled) { return; } - if (TryReflectProjectile(uid, args.OtherEntity, reflect: component)) + if (TryReflectProjectile(uid, args.ProjUid, reflect: component)) args.Cancelled = true; }