Godmode refactor (#14651)

This commit is contained in:
Kara
2023-03-23 11:57:15 -07:00
committed by GitHub
parent 31320a9e9a
commit 48cefca4e4
6 changed files with 114 additions and 64 deletions

View File

@@ -6,6 +6,7 @@ using Content.Server.Atmos;
using Content.Server.Atmos.Components; using Content.Server.Atmos.Components;
using Content.Server.Cargo.Components; using Content.Server.Cargo.Components;
using Content.Server.Cargo.Systems; using Content.Server.Cargo.Systems;
using Content.Server.Damage.Components;
using Content.Server.Doors.Components; using Content.Server.Doors.Components;
using Content.Server.Doors.Systems; using Content.Server.Doors.Systems;
using Content.Server.Hands.Components; using Content.Server.Hands.Components;
@@ -123,7 +124,7 @@ public sealed partial class AdminVerbSystem
args.Verbs.Add(rejuvenate); args.Verbs.Add(rejuvenate);
} }
if (!_godmodeSystem.HasGodmode(args.Target)) if (!HasComp<GodmodeComponent>(args.Target))
{ {
Verb makeIndestructible = new() Verb makeIndestructible = new()
{ {

View File

@@ -0,0 +1,14 @@
using Content.Server.Damage.Systems;
using Content.Shared.Damage;
namespace Content.Server.Damage.Components;
/// <summary>
///
/// </summary>
[RegisterComponent, Access(typeof(GodmodeSystem))]
public sealed class GodmodeComponent : Component
{
public bool WasMovedByPressure;
public DamageSpecifier? OldDamage = null;
}

View File

@@ -1,6 +1,10 @@
using Content.Server.Atmos.Components; using Content.Server.Atmos.Components;
using Content.Server.Damage.Components;
using Content.Shared.Damage; using Content.Shared.Damage;
using Content.Shared.GameTicking; using Content.Shared.Damage.Systems;
using Content.Shared.FixedPoint;
using Content.Shared.Rejuvenate;
using Content.Shared.StatusEffect;
using JetBrains.Annotations; using JetBrains.Annotations;
namespace Content.Server.Damage.Systems namespace Content.Server.Damage.Systems
@@ -8,108 +12,85 @@ namespace Content.Server.Damage.Systems
[UsedImplicitly] [UsedImplicitly]
public sealed class GodmodeSystem : EntitySystem public sealed class GodmodeSystem : EntitySystem
{ {
private readonly Dictionary<EntityUid, OldEntityInformation> _entities = new(); [Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset); SubscribeLocalEvent<GodmodeComponent, BeforeDamageChangedEvent>(OnBeforeDamageChanged);
SubscribeLocalEvent<GodmodeComponent, BeforeStatusEffectAddedEvent>(OnBeforeStatusEffect);
SubscribeLocalEvent<GodmodeComponent, BeforeStaminaDamageEvent>(OnBeforeStaminaDamage);
} }
public void Reset(RoundRestartCleanupEvent ev) private void OnBeforeDamageChanged(EntityUid uid, GodmodeComponent component, ref BeforeDamageChangedEvent args)
{ {
_entities.Clear(); args.Cancelled = true;
} }
public bool EnableGodmode(EntityUid entity) private void OnBeforeStatusEffect(EntityUid uid, GodmodeComponent component, ref BeforeStatusEffectAddedEvent args)
{ {
if (_entities.ContainsKey(entity)) args.Cancelled = true;
{ }
return false;
}
_entities[entity] = new OldEntityInformation(entity, EntityManager); private void OnBeforeStaminaDamage(EntityUid uid, GodmodeComponent component, ref BeforeStaminaDamageEvent args)
{
args.Cancelled = true;
}
if (EntityManager.TryGetComponent(entity, out MovedByPressureComponent? moved)) public void EnableGodmode(EntityUid uid)
{
var godmode = EnsureComp<GodmodeComponent>(uid);
if (TryComp<MovedByPressureComponent>(uid, out var moved))
{ {
godmode.WasMovedByPressure = moved.Enabled;
moved.Enabled = false; moved.Enabled = false;
} }
if (EntityManager.TryGetComponent(entity, out DamageableComponent? damageable)) if (TryComp<DamageableComponent>(uid, out var damageable))
{ {
_damageableSystem.SetDamage(entity, damageable, new DamageSpecifier()); godmode.OldDamage = new(damageable.Damage);
} }
return true; // Rejuv to cover other stuff
RaiseLocalEvent(uid, new RejuvenateEvent());
} }
public bool HasGodmode(EntityUid entity) public void DisableGodmode(EntityUid uid)
{ {
return _entities.ContainsKey(entity); if (!TryComp<GodmodeComponent>(uid, out var godmode))
} return;
public bool DisableGodmode(EntityUid entity) if (TryComp<MovedByPressureComponent>(uid, out var moved))
{
if (!_entities.Remove(entity, out var old))
{ {
return false; moved.Enabled = godmode.WasMovedByPressure;
} }
if (EntityManager.TryGetComponent(entity, out MovedByPressureComponent? moved)) if (!TryComp<DamageableComponent>(uid, out var damageable))
{ return;
moved.Enabled = old.MovedByPressure;
}
if (EntityManager.TryGetComponent(entity, out DamageableComponent? damageable)) if (godmode.OldDamage != null)
{ {
if (old.Damage != null) _damageable.SetDamage(uid, damageable, godmode.OldDamage);
{
_damageableSystem.SetDamage(entity, damageable, old.Damage);
}
} }
return true;
} }
/// <summary> /// <summary>
/// Toggles godmode for a given entity. /// Toggles godmode for a given entity.
/// </summary> /// </summary>
/// <param name="entity">The entity to toggle godmode for.</param> /// <param name="uid">The entity to toggle godmode for.</param>
/// <returns>true if enabled, false if disabled.</returns> /// <returns>true if enabled, false if disabled.</returns>
public bool ToggleGodmode(EntityUid entity) public bool ToggleGodmode(EntityUid uid)
{ {
if (HasGodmode(entity)) if (HasComp<GodmodeComponent>(uid))
{ {
DisableGodmode(entity); DisableGodmode(uid);
return false; return false;
} }
else
{
EnableGodmode(entity);
return true;
}
}
public sealed class OldEntityInformation EnableGodmode(uid);
{ return true;
public OldEntityInformation(EntityUid entity, IEntityManager entityManager)
{
Entity = entity;
MovedByPressure = entityManager.HasComponent<MovedByPressureComponent>(entity);
if (entityManager.TryGetComponent(entity, out DamageableComponent? damageable))
{
Damage = damageable.Damage;
}
}
public EntityUid Entity { get; }
public bool MovedByPressure { get; }
public DamageSpecifier? Damage { get; }
} }
} }
} }

View File

@@ -159,6 +159,12 @@ namespace Content.Shared.Damage
return damage; return damage;
} }
var before = new BeforeDamageChangedEvent(damage);
RaiseLocalEvent(uid.Value, ref before);
if (before.Cancelled)
return null;
// Apply resistances // Apply resistances
if (!ignoreResistances) if (!ignoreResistances)
{ {
@@ -283,6 +289,12 @@ namespace Content.Shared.Damage
} }
} }
/// <summary>
/// Raised before damage is done, so stuff can cancel it if necessary.
/// </summary>
[ByRefEvent]
public record struct BeforeDamageChangedEvent(DamageSpecifier Delta, bool Cancelled=false);
/// <summary> /// <summary>
/// Raised on an entity when damage is about to be dealt, /// Raised on an entity when damage is about to be dealt,
/// in case anything else needs to modify it other than the base /// in case anything else needs to modify it other than the base

View File

@@ -7,6 +7,7 @@ using Content.Shared.Damage.Events;
using Content.Shared.Database; using Content.Shared.Database;
using Content.Shared.IdentityManagement; using Content.Shared.IdentityManagement;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Rejuvenate;
using Content.Shared.Rounding; using Content.Shared.Rounding;
using Content.Shared.Stunnable; using Content.Shared.Stunnable;
using Content.Shared.Weapons.Melee.Events; using Content.Shared.Weapons.Melee.Events;
@@ -45,6 +46,7 @@ public sealed class StaminaSystem : EntitySystem
SubscribeLocalEvent<StaminaComponent, ComponentGetState>(OnStamGetState); SubscribeLocalEvent<StaminaComponent, ComponentGetState>(OnStamGetState);
SubscribeLocalEvent<StaminaComponent, ComponentHandleState>(OnStamHandleState); SubscribeLocalEvent<StaminaComponent, ComponentHandleState>(OnStamHandleState);
SubscribeLocalEvent<StaminaComponent, DisarmedEvent>(OnDisarmed); SubscribeLocalEvent<StaminaComponent, DisarmedEvent>(OnDisarmed);
SubscribeLocalEvent<StaminaComponent, RejuvenateEvent>(OnRejuvenate);
SubscribeLocalEvent<StaminaDamageOnCollideComponent, StartCollideEvent>(OnCollide); SubscribeLocalEvent<StaminaDamageOnCollideComponent, StartCollideEvent>(OnCollide);
SubscribeLocalEvent<StaminaDamageOnHitComponent, MeleeHitEvent>(OnHit); SubscribeLocalEvent<StaminaDamageOnHitComponent, MeleeHitEvent>(OnHit);
} }
@@ -111,6 +113,18 @@ public sealed class StaminaSystem : EntitySystem
return MathF.Max(0f, component.StaminaDamage - MathF.Max(0f, (float) (curTime - (component.NextUpdate + pauseTime)).TotalSeconds * component.Decay)); return MathF.Max(0f, component.StaminaDamage - MathF.Max(0f, (float) (curTime - (component.NextUpdate + pauseTime)).TotalSeconds * component.Decay));
} }
private void OnRejuvenate(EntityUid uid, StaminaComponent component, RejuvenateEvent args)
{
if (component.StaminaDamage >= component.CritThreshold)
{
ExitStamCrit(uid, component);
}
component.StaminaDamage = 0;
RemComp<ActiveStaminaComponent>(uid);
Dirty(component);
}
private void OnDisarmed(EntityUid uid, StaminaComponent component, DisarmedEvent args) private void OnDisarmed(EntityUid uid, StaminaComponent component, DisarmedEvent args)
{ {
if (args.Handled || !_random.Prob(args.PushProbability)) if (args.Handled || !_random.Prob(args.PushProbability))
@@ -209,7 +223,16 @@ public sealed class StaminaSystem : EntitySystem
public void TakeStaminaDamage(EntityUid uid, float value, StaminaComponent? component = null, EntityUid? source = null, EntityUid? with = null) public void TakeStaminaDamage(EntityUid uid, float value, StaminaComponent? component = null, EntityUid? source = null, EntityUid? with = null)
{ {
if (!Resolve(uid, ref component, false) || component.Critical) if (!Resolve(uid, ref component, false))
return;
var ev = new BeforeStaminaDamageEvent(value);
RaiseLocalEvent(uid, ref ev);
if (ev.Cancelled)
return;
// Have we already reached the point of max stamina damage?
if (component.Critical)
return; return;
var oldDamage = component.StaminaDamage; var oldDamage = component.StaminaDamage;
@@ -356,4 +379,11 @@ public sealed class StaminaSystem : EntitySystem
public float CritThreshold; public float CritThreshold;
public TimeSpan LastUpdate; public TimeSpan LastUpdate;
} }
} }
/// <summary>
/// Raised before stamina damage is dealt to allow other systems to cancel it.
/// </summary>
[ByRefEvent]
public record struct BeforeStaminaDamageEvent(float Value, bool Cancelled=false);

View File

@@ -346,6 +346,12 @@ namespace Content.Shared.StatusEffect
// don't log since stuff calling this prolly doesn't care if we don't actually have it // don't log since stuff calling this prolly doesn't care if we don't actually have it
if (!Resolve(uid, ref status, false)) if (!Resolve(uid, ref status, false))
return false; return false;
var ev = new BeforeStatusEffectAddedEvent(key);
RaiseLocalEvent(uid, ref ev);
if (ev.Cancelled)
return false;
if (!_prototypeManager.TryIndex<StatusEffectPrototype>(key, out var proto)) if (!_prototypeManager.TryIndex<StatusEffectPrototype>(key, out var proto))
return false; return false;
if (!status.AllowedEffects.Contains(key) && !proto.AlwaysAllowed) if (!status.AllowedEffects.Contains(key) && !proto.AlwaysAllowed)
@@ -465,6 +471,12 @@ namespace Content.Shared.StatusEffect
} }
} }
/// <summary>
/// Raised on an entity before a status effect is added to determine if adding it should be cancelled.
/// </summary>
[ByRefEvent]
public record struct BeforeStatusEffectAddedEvent(string Key, bool Cancelled=false);
public readonly struct StatusEffectAddedEvent public readonly struct StatusEffectAddedEvent
{ {
public readonly EntityUid Uid; public readonly EntityUid Uid;