Make melee damage not go through MeleeHitEvent.cs (#16881)

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Nemanja
2023-05-28 03:03:25 -04:00
committed by GitHub
parent 0053ddb8f8
commit dd044f4a91
9 changed files with 73 additions and 48 deletions

View File

@@ -6,6 +6,7 @@ using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems; using Content.Server.Power.EntitySystems;
using Content.Server.Power.NodeGroups; using Content.Server.Power.NodeGroups;
using Content.Server.Weapons.Melee;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes; using Content.Shared.Damage.Prototypes;
using Content.Shared.Database; using Content.Shared.Database;
@@ -37,6 +38,7 @@ public sealed class ElectrocutionSystem : SharedElectrocutionSystem
[Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffects = default!; [Dependency] private readonly StatusEffectsSystem _statusEffects = default!;
[Dependency] private readonly SharedJitteringSystem _jittering = default!; [Dependency] private readonly SharedJitteringSystem _jittering = default!;
[Dependency] private readonly MeleeWeaponSystem _meleeWeapon = default!;
[Dependency] private readonly SharedStunSystem _stun = default!; [Dependency] private readonly SharedStunSystem _stun = default!;
[Dependency] private readonly SharedStutteringSystem _stuttering = default!; [Dependency] private readonly SharedStutteringSystem _stuttering = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!; [Dependency] private readonly SharedPopupSystem _popup = default!;
@@ -161,12 +163,8 @@ public sealed class ElectrocutionSystem : SharedElectrocutionSystem
if (!electrified.OnAttacked) if (!electrified.OnAttacked)
return; return;
//Dont shock if the attacker used a toy if (_meleeWeapon.GetDamage(args.Used).Total == 0)
if (EntityManager.TryGetComponent<MeleeWeaponComponent>(args.Used, out var meleeWeaponComponent))
{
if (meleeWeaponComponent.Damage.Total == 0)
return; return;
}
TryDoElectrifiedAct(uid, args.User, 1, electrified); TryDoElectrifiedAct(uid, args.User, 1, electrified);
} }

View File

@@ -2,6 +2,7 @@ using Content.Server.Power.Components;
using Content.Server.Power.Events; using Content.Server.Power.Events;
using Content.Server.Stunnable.Components; using Content.Server.Stunnable.Components;
using Content.Shared.Audio; using Content.Shared.Audio;
using Content.Shared.Damage;
using Content.Shared.Damage.Events; using Content.Shared.Damage.Events;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction.Events; using Content.Shared.Interaction.Events;
@@ -27,15 +28,16 @@ namespace Content.Server.Stunnable.Systems
SubscribeLocalEvent<StunbatonComponent, UseInHandEvent>(OnUseInHand); SubscribeLocalEvent<StunbatonComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<StunbatonComponent, ExaminedEvent>(OnExamined); SubscribeLocalEvent<StunbatonComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<StunbatonComponent, StaminaDamageOnHitAttemptEvent>(OnStaminaHitAttempt); SubscribeLocalEvent<StunbatonComponent, StaminaDamageOnHitAttemptEvent>(OnStaminaHitAttempt);
SubscribeLocalEvent<StunbatonComponent, MeleeHitEvent>(OnMeleeHit); SubscribeLocalEvent<StunbatonComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
} }
private void OnMeleeHit(EntityUid uid, StunbatonComponent component, MeleeHitEvent args) private void OnGetMeleeDamage(EntityUid uid, StunbatonComponent component, ref GetMeleeDamageEvent args)
{ {
if (!component.Activated) return; if (!component.Activated)
return;
// Don't apply damage if it's activated; just do stamina damage. // Don't apply damage if it's activated; just do stamina damage.
args.BonusDamage -= args.BaseDamage; args.Damage = new DamageSpecifier();
} }
private void OnStaminaHitAttempt(EntityUid uid, StunbatonComponent component, ref StaminaDamageOnHitAttemptEvent args) private void OnStaminaHitAttempt(EntityUid uid, StunbatonComponent component, ref StaminaDamageOnHitAttemptEvent args)

View File

@@ -44,13 +44,13 @@ namespace Content.Server.Tools
SubscribeLocalEvent<WelderComponent, ToolDoAfterEvent>(OnWelderDoAfter); SubscribeLocalEvent<WelderComponent, ToolDoAfterEvent>(OnWelderDoAfter);
SubscribeLocalEvent<WelderComponent, ComponentShutdown>(OnWelderShutdown); SubscribeLocalEvent<WelderComponent, ComponentShutdown>(OnWelderShutdown);
SubscribeLocalEvent<WelderComponent, ComponentGetState>(OnWelderGetState); SubscribeLocalEvent<WelderComponent, ComponentGetState>(OnWelderGetState);
SubscribeLocalEvent<WelderComponent, MeleeHitEvent>(OnMeleeHit); SubscribeLocalEvent<WelderComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
} }
private void OnMeleeHit(EntityUid uid, WelderComponent component, MeleeHitEvent args) private void OnGetMeleeDamage(EntityUid uid, WelderComponent component, ref GetMeleeDamageEvent args)
{ {
if (!args.Handled && component.Lit) if (component.Lit)
args.BonusDamage += component.LitMeleeDamageBonus; args.Damage += component.LitMeleeDamageBonus;
} }
public (FixedPoint2 fuel, FixedPoint2 capacity) GetWelderFuelAndCapacity(EntityUid uid, WelderComponent? welder = null, SolutionContainerManagerComponent? solutionContainer = null) public (FixedPoint2 fuel, FixedPoint2 capacity) GetWelderFuelAndCapacity(EntityUid uid, WelderComponent? welder = null, SolutionContainerManagerComponent? solutionContainer = null)

View File

@@ -28,7 +28,7 @@ public sealed class EnergySwordSystem : EntitySystem
base.Initialize(); base.Initialize();
SubscribeLocalEvent<EnergySwordComponent, MapInitEvent>(OnMapInit); SubscribeLocalEvent<EnergySwordComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<EnergySwordComponent, MeleeHitEvent>(OnMeleeHit); SubscribeLocalEvent<EnergySwordComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
SubscribeLocalEvent<EnergySwordComponent, UseInHandEvent>(OnUseInHand); SubscribeLocalEvent<EnergySwordComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<EnergySwordComponent, InteractUsingEvent>(OnInteractUsing); SubscribeLocalEvent<EnergySwordComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<EnergySwordComponent, IsHotEvent>(OnIsHotEvent); SubscribeLocalEvent<EnergySwordComponent, IsHotEvent>(OnIsHotEvent);
@@ -42,13 +42,13 @@ public sealed class EnergySwordSystem : EntitySystem
comp.BladeColor = _random.Pick(comp.ColorOptions); comp.BladeColor = _random.Pick(comp.ColorOptions);
} }
private void OnMeleeHit(EntityUid uid, EnergySwordComponent comp, MeleeHitEvent args) private void OnGetMeleeDamage(EntityUid uid, EnergySwordComponent comp, ref GetMeleeDamageEvent args)
{ {
if (!comp.Activated) if (!comp.Activated)
return; return;
// Overrides basic blunt damage with burn+slash as set in yaml // Overrides basic blunt damage with burn+slash as set in yaml
args.BonusDamage = comp.LitDamageBonus; args.Damage = comp.LitDamageBonus;
} }
private void OnUseInHand(EntityUid uid, EnergySwordComponent comp, UseInHandEvent args) private void OnUseInHand(EntityUid uid, EnergySwordComponent comp, UseInHandEvent args)

View File

@@ -62,16 +62,7 @@ public sealed class MeleeWeaponSystem : SharedMeleeWeaponSystem
if (!args.CanInteract || !args.CanAccess || component.HideFromExamine) if (!args.CanInteract || !args.CanAccess || component.HideFromExamine)
return; return;
var getDamage = new MeleeHitEvent(new List<EntityUid>(), args.User, uid, component.Damage); var damageSpec = GetDamage(uid, component);
getDamage.IsHit = false;
RaiseLocalEvent(uid, getDamage);
var damageSpec = GetDamage(component);
if (damageSpec == null)
damageSpec = new DamageSpecifier();
damageSpec += getDamage.BonusDamage;
if (damageSpec.Total == FixedPoint2.Zero) if (damageSpec.Total == FixedPoint2.Zero)
return; return;
@@ -115,10 +106,6 @@ public sealed class MeleeWeaponSystem : SharedMeleeWeaponSystem
return true; return true;
} }
private DamageSpecifier? GetDamage(MeleeWeaponComponent component)
{
return component.Damage.Total > FixedPoint2.Zero ? component.Damage : null;
}
protected override void Popup(string message, EntityUid? uid, EntityUid? user) protected override void Popup(string message, EntityUid? uid, EntityUid? user)
{ {

View File

@@ -0,0 +1,17 @@
using Content.Shared.Damage;
namespace Content.Shared.Weapons.Melee.Components;
/// <summary>
/// This is used for adding in bonus damage via <see cref="GetMeleeWeaponEvent"/>
/// This exists only for event relays and doing entity shenanigans.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class BonusMeleeDamageComponent : Component
{
/// <summary>
/// The damage that will be applied.
/// </summary>
[DataField("bonusDamage", required: true)]
public DamageSpecifier BonusDamage = default!;
}

View File

@@ -66,3 +66,10 @@ public sealed class MeleeHitEvent : HandledEntityEventArgs
BaseDamage = baseDamage; BaseDamage = baseDamage;
} }
} }
/// <summary>
/// Raised on a melee weapon to calculate potential damage bonuses or decreases.
/// </summary>
/// <param name="Damage"></param>
[ByRefEvent]
public record struct GetMeleeDamageEvent(DamageSpecifier Damage);

View File

@@ -73,6 +73,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
SubscribeLocalEvent<MeleeWeaponComponent, HandDeselectedEvent>(OnMeleeDropped); SubscribeLocalEvent<MeleeWeaponComponent, HandDeselectedEvent>(OnMeleeDropped);
SubscribeLocalEvent<MeleeWeaponComponent, HandSelectedEvent>(OnMeleeSelected); SubscribeLocalEvent<MeleeWeaponComponent, HandSelectedEvent>(OnMeleeSelected);
SubscribeLocalEvent<MeleeWeaponComponent, GunShotEvent>(OnMeleeShot); SubscribeLocalEvent<MeleeWeaponComponent, GunShotEvent>(OnMeleeShot);
SubscribeLocalEvent<BonusMeleeDamageComponent, GetMeleeDamageEvent>(OnGetBonusMeleeDamage);
SubscribeAllEvent<HeavyAttackEvent>(OnHeavyAttack); SubscribeAllEvent<HeavyAttackEvent>(OnHeavyAttack);
SubscribeAllEvent<LightAttackEvent>(OnLightAttack); SubscribeAllEvent<LightAttackEvent>(OnLightAttack);
@@ -140,6 +141,11 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
Dirty(component); Dirty(component);
} }
private void OnGetBonusMeleeDamage(EntityUid uid, BonusMeleeDamageComponent component, ref GetMeleeDamageEvent args)
{
args.Damage += component.BonusDamage;
}
private void OnStopAttack(StopAttackEvent msg, EntitySessionEventArgs args) private void OnStopAttack(StopAttackEvent msg, EntitySessionEventArgs args)
{ {
var user = args.SenderSession.AttachedEntity; var user = args.SenderSession.AttachedEntity;
@@ -270,6 +276,17 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
component.Range = state.Range; component.Range = state.Range;
} }
public DamageSpecifier GetDamage(EntityUid uid, MeleeWeaponComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return new DamageSpecifier();
var ev = new GetMeleeDamageEvent(new (component.Damage));
RaiseLocalEvent(uid, ref ev);
return ev.Damage;
}
public bool TryGetWeapon(EntityUid entity, out EntityUid weaponUid, [NotNullWhen(true)] out MeleeWeaponComponent? melee) public bool TryGetWeapon(EntityUid entity, out EntityUid weaponUid, [NotNullWhen(true)] out MeleeWeaponComponent? melee)
{ {
weaponUid = default; weaponUid = default;
@@ -469,7 +486,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
protected virtual void DoLightAttack(EntityUid user, LightAttackEvent ev, EntityUid meleeUid, MeleeWeaponComponent component, ICommonSession? session) protected virtual void DoLightAttack(EntityUid user, LightAttackEvent ev, EntityUid meleeUid, MeleeWeaponComponent component, ICommonSession? session)
{ {
var damage = component.Damage * GetModifier(component, true); var damage = GetDamage(meleeUid, component) * GetModifier(component, true);
// For consistency with wide attacks stuff needs damageable. // For consistency with wide attacks stuff needs damageable.
if (Deleted(ev.Target) || if (Deleted(ev.Target) ||
@@ -543,7 +560,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
{ {
Audio.PlayPredicted(hitEvent.HitSoundOverride, meleeUid, user); Audio.PlayPredicted(hitEvent.HitSoundOverride, meleeUid, user);
} }
else if (component.Damage.Total.Equals(FixedPoint2.Zero) && component.HitSound != null) else if (GetDamage(meleeUid, component).Total.Equals(FixedPoint2.Zero) && component.HitSound != null)
{ {
Audio.PlayPredicted(component.HitSound, meleeUid, user); Audio.PlayPredicted(component.HitSound, meleeUid, user);
} }
@@ -576,7 +593,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
var direction = targetMap.Position - userPos; var direction = targetMap.Position - userPos;
var distance = Math.Min(component.Range, direction.Length); var distance = Math.Min(component.Range, direction.Length);
var damage = component.Damage * GetModifier(component, false); var damage = GetDamage(meleeUid, component) * GetModifier(component, false);
var entities = ev.Entities; var entities = ev.Entities;
if (entities.Count == 0) if (entities.Count == 0)

View File

@@ -40,7 +40,7 @@ public sealed class WieldableSystem : EntitySystem
SubscribeLocalEvent<MeleeRequiresWieldComponent, AttemptMeleeEvent>(OnMeleeAttempt); SubscribeLocalEvent<MeleeRequiresWieldComponent, AttemptMeleeEvent>(OnMeleeAttempt);
SubscribeLocalEvent<GunRequiresWieldComponent, AttemptShootEvent>(OnShootAttempt); SubscribeLocalEvent<GunRequiresWieldComponent, AttemptShootEvent>(OnShootAttempt);
SubscribeLocalEvent<IncreaseDamageOnWieldComponent, MeleeHitEvent>(OnMeleeHit); SubscribeLocalEvent<IncreaseDamageOnWieldComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
} }
private void OnMeleeAttempt(EntityUid uid, MeleeRequiresWieldComponent component, ref AttemptMeleeEvent args) private void OnMeleeAttempt(EntityUid uid, MeleeRequiresWieldComponent component, ref AttemptMeleeEvent args)
@@ -243,16 +243,13 @@ public sealed class WieldableSystem : EntitySystem
AttemptUnwield(args.BlockingEntity, component, args.User); AttemptUnwield(args.BlockingEntity, component, args.User);
} }
private void OnMeleeHit(EntityUid uid, IncreaseDamageOnWieldComponent component, MeleeHitEvent args) private void OnGetMeleeDamage(EntityUid uid, IncreaseDamageOnWieldComponent component, ref GetMeleeDamageEvent args)
{
if (EntityManager.TryGetComponent<WieldableComponent>(uid, out var wield))
{ {
if (!TryComp<WieldableComponent>(uid, out var wield))
return;
if (!wield.Wielded) if (!wield.Wielded)
return; return;
}
if (args.Handled)
return;
args.BonusDamage += component.BonusDamage; args.Damage += component.BonusDamage;
} }
} }