Stunnable New Status and Cleanup (#38618)

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
This commit is contained in:
Princess Cheeseballs
2025-07-21 10:22:11 -07:00
committed by GitHub
parent 2b2b9b11b8
commit e85bc1bb8c
56 changed files with 620 additions and 316 deletions

View File

@@ -4,7 +4,6 @@ using Content.Shared.Alert;
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory.Events;
using Content.Shared.Item;
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Systems;
using Content.Shared.Database;
using Content.Shared.DoAfter;
@@ -14,35 +13,35 @@ using Content.Shared.Mobs.Components;
using Content.Shared.Movement.Events;
using Content.Shared.Movement.Systems;
using Content.Shared.Standing;
using Content.Shared.StatusEffect;
using Content.Shared.StatusEffectNew;
using Content.Shared.Throwing;
using Content.Shared.Whitelist;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Physics.Events;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Content.Shared.Stunnable;
public abstract partial class SharedStunSystem : EntitySystem
{
[Dependency] protected readonly ActionBlockerSystem Blocker = default!;
[Dependency] protected readonly AlertsSystem Alerts = default!;
public static readonly EntProtoId StunId = "StatusEffectStunned";
public static readonly EntProtoId KnockdownId = "StatusEffectKnockdown";
[Dependency] protected readonly IGameTiming GameTiming = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] protected readonly ActionBlockerSystem Blocker = default!;
[Dependency] protected readonly AlertsSystem Alerts = default!;
[Dependency] private readonly EntityWhitelistSystem _entityWhitelist = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] protected readonly SharedDoAfterSystem DoAfter = default!;
[Dependency] protected readonly SharedStaminaSystem Stamina = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffect = default!;
[Dependency] private readonly StatusEffectsSystem _status = default!;
public override void Initialize()
{
SubscribeLocalEvent<SlowedDownComponent, ComponentInit>(OnSlowInit);
SubscribeLocalEvent<SlowedDownComponent, ComponentShutdown>(OnSlowRemove);
SubscribeLocalEvent<SlowedDownComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
SubscribeLocalEvent<StunnedComponent, ComponentStartup>(UpdateCanMove);
SubscribeLocalEvent<StunnedComponent, ComponentShutdown>(OnStunShutdown);
@@ -61,6 +60,14 @@ public abstract partial class SharedStunSystem : EntitySystem
SubscribeLocalEvent<StunnedComponent, IsUnequippingAttemptEvent>(OnUnequipAttempt);
SubscribeLocalEvent<MobStateComponent, MobStateChangedEvent>(OnMobStateChanged);
// New Status Effect subscriptions
SubscribeLocalEvent<StunnedStatusEffectComponent, StatusEffectAppliedEvent>(OnStunEffectApplied);
SubscribeLocalEvent<StunnedStatusEffectComponent, StatusEffectRemovedEvent>(OnStunStatusRemoved);
SubscribeLocalEvent<StunnedStatusEffectComponent, StatusEffectRelayedEvent<StunEndAttemptEvent>>(OnStunEndAttempt);
SubscribeLocalEvent<KnockdownStatusEffectComponent, StatusEffectRelayedEvent<StandUpAttemptEvent>>(OnStandUpAttempt);
// Stun Appearance Data
InitializeKnockdown();
InitializeAppearance();
}
@@ -72,10 +79,6 @@ public abstract partial class SharedStunSystem : EntitySystem
private void OnMobStateChanged(EntityUid uid, MobStateComponent component, MobStateChangedEvent args)
{
if (!TryComp<StatusEffectsComponent>(uid, out var status))
{
return;
}
switch (args.NewMobState)
{
case MobState.Alive:
@@ -84,12 +87,12 @@ public abstract partial class SharedStunSystem : EntitySystem
}
case MobState.Critical:
{
_statusEffect.TryRemoveStatusEffect(uid, "Stun");
_status.TryRemoveStatusEffect(uid, StunId);
break;
}
case MobState.Dead:
{
_statusEffect.TryRemoveStatusEffect(uid, "Stun");
_status.TryRemoveStatusEffect(uid, StunId);
break;
}
case MobState.Invalid:
@@ -119,71 +122,95 @@ public abstract partial class SharedStunSystem : EntitySystem
if (_entityWhitelist.IsBlacklistPass(ent.Comp.Blacklist, args.OtherEntity))
return;
if (!TryComp<StatusEffectsComponent>(args.OtherEntity, out var status))
return;
TryStun(args.OtherEntity, ent.Comp.Duration, true, status);
TryKnockdown(args.OtherEntity, ent.Comp.Duration, ent.Comp.Refresh, ent.Comp.AutoStand);
}
private void OnSlowInit(EntityUid uid, SlowedDownComponent component, ComponentInit args)
{
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid);
}
private void OnSlowRemove(EntityUid uid, SlowedDownComponent component, ComponentShutdown args)
{
component.SprintSpeedModifier = 1f;
component.WalkSpeedModifier = 1f;
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid);
TryUpdateStunDuration(args.OtherEntity, ent.Comp.Duration);
TryKnockdown(args.OtherEntity, ent.Comp.Duration, true, force: true);
}
// TODO STUN: Make events for different things. (Getting modifiers, attempt events, informative events...)
/// <summary>
/// Stuns the entity, disallowing it from doing many interactions temporarily.
/// </summary>
public bool TryStun(EntityUid uid, TimeSpan time, bool refresh, StatusEffectsComponent? status = null)
public bool TryAddStunDuration(EntityUid uid, TimeSpan duration)
{
if (time <= TimeSpan.Zero)
if (!_status.TryAddStatusEffectDuration(uid, StunId, duration))
return false;
if (!Resolve(uid, ref status, false))
OnStunnedSuccessfully(uid, duration);
return true;
}
public bool TryUpdateStunDuration(EntityUid uid, TimeSpan? duration)
{
if (!_status.TryUpdateStatusEffectDuration(uid, StunId, duration))
return false;
if (!_statusEffect.TryAddStatusEffect<StunnedComponent>(uid, "Stun", time, refresh))
return false;
OnStunnedSuccessfully(uid, duration);
return true;
}
var ev = new StunnedEvent();
private void OnStunnedSuccessfully(EntityUid uid, TimeSpan? duration)
{
var ev = new StunnedEvent(); // todo: rename event or change how it is raised - this event is raised each time duration of stun was externally changed
RaiseLocalEvent(uid, ref ev);
_adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(uid):user} stunned for {time.Seconds} seconds");
var timeForLogs = duration.HasValue
? duration.Value.Seconds.ToString()
: "Infinite";
_adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(uid):user} stunned for {timeForLogs} seconds");
}
public bool TryAddKnockdownDuration(EntityUid uid, TimeSpan duration)
{
if (!_status.TryAddStatusEffectDuration(uid, KnockdownId, duration))
return false;
TryKnockdown(uid, duration, true, force: true);
return true;
}
public bool TryUpdateKnockdownDuration(EntityUid uid, TimeSpan? duration)
{
if (!_status.TryUpdateStatusEffectDuration(uid, KnockdownId, duration))
return false;
return TryKnockdown(uid, duration, true, force: true);
}
/// <summary>
/// Knocks down the entity, making it fall to the ground.
/// </summary>
public bool TryKnockdown(EntityUid uid, TimeSpan time, bool refresh, bool autoStand = true, bool drop = true)
public bool TryKnockdown(Entity<StandingStateComponent?> entity, TimeSpan? time, bool refresh, bool autoStand = true, bool drop = true, bool force = false)
{
if (time <= TimeSpan.Zero)
return false;
// Can't fall down if you can't actually be downed.
if (!HasComp<StandingStateComponent>(uid))
if (!Resolve(entity, ref entity.Comp, false))
return false;
var evAttempt = new KnockDownAttemptEvent(autoStand, drop);
RaiseLocalEvent(uid, ref evAttempt);
if (evAttempt.Cancelled)
return false;
// Initialize our component with the relevant data we need if we don't have it
if (EnsureComp<KnockedDownComponent>(uid, out var component))
if (!force)
{
RefreshKnockedMovement((uid, component));
CancelKnockdownDoAfter((uid, component));
var evAttempt = new KnockDownAttemptEvent(autoStand, drop);
RaiseLocalEvent(entity, ref evAttempt);
if (evAttempt.Cancelled)
return false;
autoStand = evAttempt.AutoStand;
drop = evAttempt.Drop;
}
Knockdown(entity!, time, autoStand, drop);
return true;
}
private void Knockdown(Entity<StandingStateComponent> entity, TimeSpan? time, bool refresh, bool autoStand = true, bool drop = true)
{
// Initialize our component with the relevant data we need if we don't have it
if (EnsureComp<KnockedDownComponent>(entity, out var component))
{
RefreshKnockedMovement((entity, component));
CancelKnockdownDoAfter((entity, component));
}
else
{
@@ -191,127 +218,87 @@ public abstract partial class SharedStunSystem : EntitySystem
if (drop)
{
var ev = new DropHandItemsEvent();
RaiseLocalEvent(uid, ref ev);
RaiseLocalEvent(entity, ref ev);
}
// Only update Autostand value if it's our first time being knocked down...
SetAutoStand((uid, component), evAttempt.AutoStand);
SetAutoStand((entity, component), autoStand);
}
var knockedEv = new KnockedDownEvent(time);
RaiseLocalEvent(uid, ref knockedEv);
RaiseLocalEvent(entity, ref knockedEv);
UpdateKnockdownTime((uid, component), knockedEv.Time, refresh);
Alerts.ShowAlert(uid, KnockdownAlert, null, (GameTiming.CurTime, component.NextUpdate));
_adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(uid):user} knocked down for {time.Seconds} seconds");
return true;
}
/// <summary>
/// Applies knockdown and stun to the entity temporarily.
/// </summary>
public bool TryParalyze(EntityUid uid, TimeSpan time, bool refresh,
StatusEffectsComponent? status = null)
{
if (!Resolve(uid, ref status, false))
return false;
return TryKnockdown(uid, time, refresh) && TryStun(uid, time, refresh, status);
}
/// <summary>
/// Slows down the mob's walking/running speed temporarily
/// </summary>
public bool TrySlowdown(EntityUid uid, TimeSpan time, bool refresh,
float walkSpeedMod = 1f, float sprintSpeedMod = 1f,
StatusEffectsComponent? status = null)
{
if (!Resolve(uid, ref status, false))
return false;
if (time <= TimeSpan.Zero)
return false;
if (_statusEffect.TryAddStatusEffect<SlowedDownComponent>(uid, "SlowedDown", time, refresh, status))
if (time != null)
{
var slowed = Comp<SlowedDownComponent>(uid);
// Doesn't make much sense to have the "TrySlowdown" method speed up entities now does it?
walkSpeedMod = Math.Clamp(walkSpeedMod, 0f, 1f);
sprintSpeedMod = Math.Clamp(sprintSpeedMod, 0f, 1f);
UpdateKnockdownTime((entity, component), time.Value, refresh);
_adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(entity):user} knocked down for {time.Value.Seconds} seconds");
}
else
_adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(entity):user} knocked down for an indefinite amount of time");
slowed.WalkSpeedModifier *= walkSpeedMod;
slowed.SprintSpeedModifier *= sprintSpeedMod;
Alerts.ShowAlert(entity, KnockdownAlert, null, (GameTiming.CurTime, component.NextUpdate));
}
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid);
public bool TryAddParalyzeDuration(EntityUid uid, TimeSpan duration)
{
var knockdown = TryAddKnockdownDuration(uid, duration);
var stunned = TryAddStunDuration(uid, duration);
return knockdown || stunned;
}
public bool TryUpdateParalyzeDuration(EntityUid uid, TimeSpan? duration)
{
var knockdown = TryUpdateKnockdownDuration(uid, duration);
var stunned = TryUpdateStunDuration(uid, duration);
return knockdown || stunned;
}
public bool TryUnstun(Entity<StunnedComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp, logMissing: false))
return true;
}
return false;
var ev = new StunEndAttemptEvent();
RaiseLocalEvent(entity, ref ev);
return !ev.Cancelled && RemComp<StunnedComponent>(entity);
}
/// <summary>
/// Updates the movement speed modifiers of an entity by applying or removing the <see cref="SlowedDownComponent"/>.
/// If both walk and run modifiers are approximately 1 (i.e. normal speed) and <see cref="StaminaComponent.StaminaDamage"/> is 0,
/// or if the both modifiers are 0, the slowdown component is removed to restore normal movement.
/// Otherwise, the slowdown component is created or updated with the provided modifiers,
/// and the movement speed is refreshed accordingly.
/// </summary>
/// <param name="ent">Entity whose movement speed should be updated.</param>
/// <param name="walkSpeedModifier">New walk speed modifier. Default is 1f (normal speed).</param>
/// <param name="runSpeedModifier">New run (sprint) speed modifier. Default is 1f (normal speed).</param>
public void UpdateStunModifiers(Entity<StaminaComponent?> ent,
float walkSpeedModifier = 1f,
float runSpeedModifier = 1f)
private void OnStunEffectApplied(Entity<StunnedStatusEffectComponent> entity, ref StatusEffectAppliedEvent args)
{
if (!Resolve(ent, ref ent.Comp))
if (GameTiming.ApplyingState)
return;
if (
(MathHelper.CloseTo(walkSpeedModifier, 1f) && MathHelper.CloseTo(runSpeedModifier, 1f) && ent.Comp.StaminaDamage == 0f) ||
(walkSpeedModifier == 0f && runSpeedModifier == 0f)
)
{
RemComp<SlowedDownComponent>(ent);
EnsureComp<StunnedComponent>(args.Target);
}
private void OnStunStatusRemoved(Entity<StunnedStatusEffectComponent> entity, ref StatusEffectRemovedEvent args)
{
TryUnstun(args.Target);
}
private void OnStunEndAttempt(Entity<StunnedStatusEffectComponent> entity, ref StatusEffectRelayedEvent<StunEndAttemptEvent> args)
{
if (args.Args.Cancelled)
return;
}
EnsureComp<SlowedDownComponent>(ent, out var comp);
comp.WalkSpeedModifier = walkSpeedModifier;
comp.SprintSpeedModifier = runSpeedModifier;
_movementSpeedModifier.RefreshMovementSpeedModifiers(ent);
Dirty(ent);
var ev = args.Args;
ev.Cancelled = true;
args.Args = ev;
}
/// <summary>
/// A convenience overload of <see cref="UpdateStunModifiers(EntityUid, float, float, StaminaComponent?)"/> that sets both
/// walk and run speed modifiers to the same value.
/// </summary>
/// <param name="ent">Entity whose movement speed should be updated.</param>
/// <param name="speedModifier">New walk and run speed modifier. Default is 1f (normal speed).</param>
/// <param name="component">
/// Optional <see cref="StaminaComponent"/> of the entity.
/// </param>
public void UpdateStunModifiers(Entity<StaminaComponent?> ent, float speedModifier = 1f)
private void OnStandUpAttempt(Entity<KnockdownStatusEffectComponent> entity, ref StatusEffectRelayedEvent<StandUpAttemptEvent> args)
{
UpdateStunModifiers(ent, speedModifier, speedModifier);
if (args.Args.Cancelled)
return;
var ev = args.Args;
ev.Cancelled = true;
args.Args = ev;
}
#region friction and movement listeners
private void OnRefreshMovespeed(EntityUid ent, SlowedDownComponent comp, RefreshMovementSpeedModifiersEvent args)
{
args.ModifySpeed(comp.WalkSpeedModifier, comp.SprintSpeedModifier);
}
#endregion
#region Attempt Event Handling
private void OnMoveAttempt(EntityUid uid, StunnedComponent stunned, UpdateCanMoveEvent args)