Melee Executions (#30104)

* melee executions

* fix damage bug

* cleanup

* address reviews hopefully

* resistance bypass mechanic

* component changes

* self executions (not finished yet)

* self execs part two

* ok i fixed things (still not finished)

* finish everything

* review stuff

* nuke if (kind = special)

* more review stuffs

* Make suicide system much less hardcoded and make much more use of events

* Fix a dumb bug I introduced

* self execution popups

* Integration tests

* Why did they even take 0.5 blunt damage?

* More consistent integration tests

* Destructive equals true

* Allow it to dirty-dispose

* IS THIS WHAT YOU WANT?

* FRESH AND CLEAN

* modifier to multiplier

* don't jinx the integration tests

* no file-scoped namespace

* Move the rest of execution to shared, create SuicideGhostEvent

* handled

* Get rid of unused code and add a comment

* ghost before suicide

* stop cat suicides

* popup fix + small suicide change

* make it a bit better

---------

Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com>
This commit is contained in:
Scribbles0
2024-08-10 20:05:54 -07:00
committed by GitHub
parent c25c5ec666
commit 220aff21eb
26 changed files with 1048 additions and 219 deletions

View File

@@ -0,0 +1,67 @@
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Content.Shared.Mobs.Components;
using Robust.Shared.Prototypes;
using System.Linq;
namespace Content.Shared.Chat;
public sealed class SharedSuicideSystem : EntitySystem
{
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
/// <summary>
/// Applies lethal damage spread out across the damage types given.
/// </summary>
public void ApplyLethalDamage(Entity<DamageableComponent> target, DamageSpecifier damageSpecifier)
{
// Create a new damageSpecifier so that we don't make alterations to the original DamageSpecifier
// Failing to do this will permanently change a weapon's damage making it insta-kill people
var appliedDamageSpecifier = new DamageSpecifier(damageSpecifier);
if (!TryComp<MobThresholdsComponent>(target, out var mobThresholds))
return;
// Mob thresholds are sorted from alive -> crit -> dead,
// grabbing the last key will give us how much damage is needed to kill a target from zero
// The exact lethal damage amount is adjusted based on their current damage taken
var lethalAmountOfDamage = mobThresholds.Thresholds.Keys.Last() - target.Comp.TotalDamage;
var totalDamage = appliedDamageSpecifier.GetTotal();
// Removing structural because it causes issues against entities that cannot take structural damage,
// then getting the total to use in calculations for spreading out damage.
appliedDamageSpecifier.DamageDict.Remove("Structural");
// Split the total amount of damage needed to kill the target by every damage type in the DamageSpecifier
foreach (var (key, value) in appliedDamageSpecifier.DamageDict)
{
appliedDamageSpecifier.DamageDict[key] = Math.Ceiling((double) (value * lethalAmountOfDamage / totalDamage));
}
_damageableSystem.TryChangeDamage(target, appliedDamageSpecifier, true, origin: target);
}
/// <summary>
/// Applies lethal damage in a single type, specified by a single damage type.
/// </summary>
public void ApplyLethalDamage(Entity<DamageableComponent> target, ProtoId<DamageTypePrototype>? damageType)
{
if (!TryComp<MobThresholdsComponent>(target, out var mobThresholds))
return;
// Mob thresholds are sorted from alive -> crit -> dead,
// grabbing the last key will give us how much damage is needed to kill a target from zero
// The exact lethal damage amount is adjusted based on their current damage taken
var lethalAmountOfDamage = mobThresholds.Thresholds.Keys.Last() - target.Comp.TotalDamage;
// We don't want structural damage for the same reasons listed above
if (!_prototypeManager.TryIndex(damageType, out var damagePrototype) || damagePrototype.ID == "Structural")
{
Log.Error($"{nameof(SharedSuicideSystem)} could not find the damage type prototype associated with {damageType}. Falling back to Blunt");
damagePrototype = _prototypeManager.Index<DamageTypePrototype>("Blunt");
}
var damage = new DamageSpecifier(damagePrototype, lethalAmountOfDamage);
_damageableSystem.TryChangeDamage(target, damage, true, origin: target);
}
}

View File

@@ -0,0 +1,9 @@
using Content.Shared.DoAfter;
using Robust.Shared.Serialization;
namespace Content.Shared.Execution;
[Serializable, NetSerializable]
public sealed partial class ExecutionDoAfterEvent : SimpleDoAfterEvent
{
}

View File

@@ -0,0 +1,77 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Execution;
/// <summary>
/// Added to entities that can be used to execute another target.
/// </summary>
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ExecutionComponent : Component
{
/// <summary>
/// How long the execution duration lasts.
/// </summary>
[DataField, AutoNetworkedField]
public float DoAfterDuration = 5f;
/// <summary>
/// Arbitrarily chosen number to multiply damage by, used to deal reasonable amounts of damage to a victim of an execution.
/// /// </summary>
[DataField, AutoNetworkedField]
public float DamageMultiplier = 9f;
/// <summary>
/// Shown to the person performing the melee execution (attacker) upon starting a melee execution.
/// </summary>
[DataField]
public LocId InternalMeleeExecutionMessage = "execution-popup-melee-initial-internal";
/// <summary>
/// Shown to bystanders and the victim of a melee execution when a melee execution is started.
/// </summary>
[DataField]
public LocId ExternalMeleeExecutionMessage = "execution-popup-melee-initial-external";
/// <summary>
/// Shown to the attacker upon completion of a melee execution.
/// </summary>
[DataField]
public LocId CompleteInternalMeleeExecutionMessage = "execution-popup-melee-complete-internal";
/// <summary>
/// Shown to bystanders and the victim of a melee execution when a melee execution is completed.
/// </summary>
[DataField]
public LocId CompleteExternalMeleeExecutionMessage = "execution-popup-melee-complete-external";
/// <summary>
/// Shown to the person performing the self execution when starting one.
/// </summary>
[DataField]
public LocId InternalSelfExecutionMessage = "execution-popup-self-initial-internal";
/// <summary>
/// Shown to bystanders near a self execution when one is started.
/// </summary>
[DataField]
public LocId ExternalSelfExecutionMessage = "execution-popup-self-initial-external";
/// <summary>
/// Shown to the person performing a self execution upon completion of a do-after or on use of /suicide with a weapon that has the Execution component.
/// </summary>
[DataField]
public LocId CompleteInternalSelfExecutionMessage = "execution-popup-self-complete-internal";
/// <summary>
/// Shown to bystanders when a self execution is completed or a suicide via execution weapon happens nearby.
/// </summary>
[DataField]
public LocId CompleteExternalSelfExecutionMessage = "execution-popup-self-complete-external";
// Not networked because this is transient inside of a tick.
/// <summary>
/// True if it is currently executing for handlers.
/// </summary>
[DataField]
public bool Executing = false;
}

View File

@@ -0,0 +1,234 @@
using Content.Shared.ActionBlocker;
using Content.Shared.Chat;
using Content.Shared.CombatMode;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Popups;
using Content.Shared.Verbs;
using Content.Shared.Weapons.Melee;
using Content.Shared.Weapons.Melee.Events;
using Content.Shared.Interaction.Events;
using Content.Shared.Mind;
using Robust.Shared.Player;
using Robust.Shared.Audio.Systems;
namespace Content.Shared.Execution;
/// <summary>
/// Verb for violently murdering cuffed creatures.
/// </summary>
public sealed class SharedExecutionSystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _actionBlocker = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedSuicideSystem _suicide = default!;
[Dependency] private readonly SharedCombatModeSystem _combat = default!;
[Dependency] private readonly SharedExecutionSystem _execution = default!;
[Dependency] private readonly SharedMeleeWeaponSystem _melee = default!;
[Dependency] private readonly SharedMindSystem _mind = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ExecutionComponent, GetVerbsEvent<UtilityVerb>>(OnGetInteractionsVerbs);
SubscribeLocalEvent<ExecutionComponent, GetMeleeDamageEvent>(OnGetMeleeDamage);
SubscribeLocalEvent<ExecutionComponent, SuicideByEnvironmentEvent>(OnSuicideByEnvironment);
SubscribeLocalEvent<ExecutionComponent, ExecutionDoAfterEvent>(OnExecutionDoAfter);
}
private void OnGetInteractionsVerbs(EntityUid uid, ExecutionComponent comp, GetVerbsEvent<UtilityVerb> args)
{
if (args.Hands == null || args.Using == null || !args.CanAccess || !args.CanInteract)
return;
var attacker = args.User;
var weapon = args.Using.Value;
var victim = args.Target;
if (!CanBeExecuted(victim, attacker))
return;
UtilityVerb verb = new()
{
Act = () => TryStartExecutionDoAfter(weapon, victim, attacker, comp),
Impact = LogImpact.High,
Text = Loc.GetString("execution-verb-name"),
Message = Loc.GetString("execution-verb-message"),
};
args.Verbs.Add(verb);
}
private void TryStartExecutionDoAfter(EntityUid weapon, EntityUid victim, EntityUid attacker, ExecutionComponent comp)
{
if (!CanBeExecuted(victim, attacker))
return;
if (attacker == victim)
{
ShowExecutionInternalPopup(comp.InternalSelfExecutionMessage, attacker, victim, weapon);
ShowExecutionExternalPopup(comp.ExternalSelfExecutionMessage, attacker, victim, weapon);
}
else
{
ShowExecutionInternalPopup(comp.InternalMeleeExecutionMessage, attacker, victim, weapon);
ShowExecutionExternalPopup(comp.ExternalMeleeExecutionMessage, attacker, victim, weapon);
}
var doAfter =
new DoAfterArgs(EntityManager, attacker, comp.DoAfterDuration, new ExecutionDoAfterEvent(), weapon, target: victim, used: weapon)
{
BreakOnMove = true,
BreakOnDamage = true,
NeedHand = true
};
_doAfter.TryStartDoAfter(doAfter);
}
public bool CanBeExecuted(EntityUid victim, EntityUid attacker)
{
// No point executing someone if they can't take damage
if (!HasComp<DamageableComponent>(victim))
return false;
// You can't execute something that cannot die
if (!TryComp<MobStateComponent>(victim, out var mobState))
return false;
// You're not allowed to execute dead people (no fun allowed)
if (_mobState.IsDead(victim, mobState))
return false;
// You must be able to attack people to execute
if (!_actionBlocker.CanAttack(attacker, victim))
return false;
// The victim must be incapacitated to be executed
if (victim != attacker && _actionBlocker.CanInteract(victim, null))
return false;
// All checks passed
return true;
}
private void OnGetMeleeDamage(Entity<ExecutionComponent> entity, ref GetMeleeDamageEvent args)
{
if (!TryComp<MeleeWeaponComponent>(entity, out var melee) || !entity.Comp.Executing)
{
return;
}
var bonus = melee.Damage * entity.Comp.DamageMultiplier - melee.Damage;
args.Damage += bonus;
args.ResistanceBypass = true;
}
private void OnSuicideByEnvironment(Entity<ExecutionComponent> entity, ref SuicideByEnvironmentEvent args)
{
if (!TryComp<MeleeWeaponComponent>(entity, out var melee))
return;
string? internalMsg = entity.Comp.CompleteInternalSelfExecutionMessage;
string? externalMsg = entity.Comp.CompleteExternalSelfExecutionMessage;
if (!TryComp<DamageableComponent>(args.Victim, out var damageableComponent))
return;
ShowExecutionInternalPopup(internalMsg, args.Victim, args.Victim, entity, false);
ShowExecutionExternalPopup(externalMsg, args.Victim, args.Victim, entity);
_audio.PlayPredicted(melee.HitSound, args.Victim, args.Victim);
_suicide.ApplyLethalDamage((args.Victim, damageableComponent), melee.Damage);
args.Handled = true;
}
private void ShowExecutionInternalPopup(string locString, EntityUid attacker, EntityUid victim, EntityUid weapon, bool predict = true)
{
if (predict)
{
_popup.PopupClient(
Loc.GetString(locString, ("attacker", attacker), ("victim", victim), ("weapon", weapon)),
attacker,
attacker,
PopupType.MediumCaution
);
}
else
{
_popup.PopupEntity(
Loc.GetString(locString, ("attacker", attacker), ("victim", victim), ("weapon", weapon)),
attacker,
attacker,
PopupType.MediumCaution
);
}
}
private void ShowExecutionExternalPopup(string locString, EntityUid attacker, EntityUid victim, EntityUid weapon)
{
_popup.PopupEntity(
Loc.GetString(locString, ("attacker", attacker), ("victim", victim), ("weapon", weapon)),
attacker,
Filter.PvsExcept(attacker),
true,
PopupType.MediumCaution
);
}
private void OnExecutionDoAfter(Entity<ExecutionComponent> entity, ref ExecutionDoAfterEvent args)
{
if (args.Handled || args.Cancelled || args.Used == null || args.Target == null)
return;
if (!TryComp<MeleeWeaponComponent>(entity, out var meleeWeaponComp))
return;
var attacker = args.User;
var victim = args.Target.Value;
var weapon = args.Used.Value;
if (!_execution.CanBeExecuted(victim, attacker))
return;
// This is needed so the melee system does not stop it.
var prev = _combat.IsInCombatMode(attacker);
_combat.SetInCombatMode(attacker, true);
entity.Comp.Executing = true;
var internalMsg = entity.Comp.CompleteInternalMeleeExecutionMessage;
var externalMsg = entity.Comp.CompleteExternalMeleeExecutionMessage;
if (attacker == victim)
{
var suicideEvent = new SuicideEvent(victim);
RaiseLocalEvent(victim, suicideEvent);
var suicideGhostEvent = new SuicideGhostEvent(victim);
RaiseLocalEvent(victim, suicideGhostEvent);
}
else
{
_melee.AttemptLightAttack(attacker, weapon, meleeWeaponComp, victim);
}
_combat.SetInCombatMode(attacker, prev);
entity.Comp.Executing = false;
args.Handled = true;
if (attacker != victim)
{
_execution.ShowExecutionInternalPopup(internalMsg, attacker, victim, entity);
_execution.ShowExecutionExternalPopup(externalMsg, attacker, victim, entity);
}
}
}

View File

@@ -1,48 +1,41 @@
namespace Content.Shared.Interaction.Events
using Content.Shared.Damage;
using Content.Shared.Damage.Prototypes;
using Robust.Shared.Prototypes;
namespace Content.Shared.Interaction.Events;
/// <summary>
/// Raised Directed at an entity to check whether they will handle the suicide.
/// </summary>
public sealed class SuicideEvent : HandledEntityEventArgs
{
/// <summary>
/// Raised Directed at an entity to check whether they will handle the suicide.
/// </summary>
public sealed class SuicideEvent : EntityEventArgs
public SuicideEvent(EntityUid victim)
{
public SuicideEvent(EntityUid victim)
{
Victim = victim;
}
public void SetHandled(SuicideKind kind)
{
if (Handled)
throw new InvalidOperationException("Suicide was already handled");
Kind = kind;
}
public void BlockSuicideAttempt(bool suicideAttempt)
{
if (suicideAttempt)
AttemptBlocked = suicideAttempt;
}
public SuicideKind? Kind { get; private set; }
public EntityUid Victim { get; private set; }
public bool AttemptBlocked { get; private set; }
public bool Handled => Kind != null;
Victim = victim;
}
public enum SuicideKind
{
Special, //Doesn't damage the mob, used for "weird" suicides like gibbing
//Damage type suicides
Blunt,
Slash,
Piercing,
Heat,
Shock,
Cold,
Poison,
Radiation,
Asphyxiation,
Bloodloss
}
public DamageSpecifier? DamageSpecifier;
public ProtoId<DamageTypePrototype>? DamageType;
public EntityUid Victim { get; private set; }
}
public sealed class SuicideByEnvironmentEvent : HandledEntityEventArgs
{
public SuicideByEnvironmentEvent(EntityUid victim)
{
Victim = victim;
}
public EntityUid Victim { get; set; }
}
public sealed class SuicideGhostEvent : HandledEntityEventArgs
{
public SuicideGhostEvent(EntityUid victim)
{
Victim = victim;
}
public EntityUid Victim { get; set; }
public bool CanReturnToBody;
}

View File

@@ -168,15 +168,17 @@ public abstract class SharedMindSystem : EntitySystem
args.PushMarkup($"[color=yellow]{Loc.GetString("comp-mind-examined-ssd", ("ent", uid))}[/color]");
}
/// <summary>
/// Checks to see if the user's mind prevents them from suicide
/// Handles the suicide event without killing the user if true
/// </summary>
private void OnSuicide(EntityUid uid, MindContainerComponent component, SuicideEvent args)
{
if (args.Handled)
return;
if (TryComp(component.Mind, out MindComponent? mind) && mind.PreventSuicide)
{
args.BlockSuicideAttempt(true);
}
args.Handled = true;
}
public EntityUid? GetMind(EntityUid uid, MindContainerComponent? mind = null)

View File

@@ -80,7 +80,7 @@ public sealed class MeleeHitEvent : HandledEntityEventArgs
/// Raised on a melee weapon to calculate potential damage bonuses or decreases.
/// </summary>
[ByRefEvent]
public record struct GetMeleeDamageEvent(EntityUid Weapon, DamageSpecifier Damage, List<DamageModifierSet> Modifiers, EntityUid User);
public record struct GetMeleeDamageEvent(EntityUid Weapon, DamageSpecifier Damage, List<DamageModifierSet> Modifiers, EntityUid User, bool ResistanceBypass = false);
/// <summary>
/// Raised on a melee weapon to calculate the attack rate.

View File

@@ -67,6 +67,12 @@ public sealed partial class MeleeWeaponComponent : Component
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
public bool AutoAttack;
/// <summary>
/// If true, attacks will bypass armor resistances.
/// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
public bool ResistanceBypass = false;
/// <summary>
/// Base damage for this weapon. Can be modified via heavy damage or other means.
/// </summary>

View File

@@ -216,7 +216,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
if (!Resolve(uid, ref component, false))
return new DamageSpecifier();
var ev = new GetMeleeDamageEvent(uid, new (component.Damage), new(), user);
var ev = new GetMeleeDamageEvent(uid, new(component.Damage), new(), user, component.ResistanceBypass);
RaiseLocalEvent(uid, ref ev);
return DamageSpecifier.ApplyModifierSets(ev.Damage, ev.Modifiers);
@@ -244,6 +244,17 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
return ev.DamageModifier * ev.Multipliers;
}
public bool GetResistanceBypass(EntityUid uid, EntityUid user, MeleeWeaponComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
var ev = new GetMeleeDamageEvent(uid, new(component.Damage), new(), user, component.ResistanceBypass);
RaiseLocalEvent(uid, ref ev);
return ev.ResistanceBypass;
}
public bool TryGetWeapon(EntityUid entity, out EntityUid weaponUid, [NotNullWhen(true)] out MeleeWeaponComponent? melee)
{
weaponUid = default;
@@ -441,6 +452,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
// If I do not come back later to fix Light Attacks being Heavy Attacks you can throw me in the spider pit -Errant
var damage = GetDamage(meleeUid, user, component) * GetHeavyDamageModifier(meleeUid, user, component);
var target = GetEntity(ev.Target);
var resistanceBypass = GetResistanceBypass(meleeUid, user, component);
// For consistency with wide attacks stuff needs damageable.
if (Deleted(target) ||
@@ -497,7 +509,7 @@ public abstract class SharedMeleeWeaponSystem : EntitySystem
RaiseLocalEvent(target.Value, attackedEvent);
var modifiedDamage = DamageSpecifier.ApplyModifierSets(damage + hitEvent.BonusDamage + attackedEvent.BonusDamage, hitEvent.ModifiersList);
var damageResult = Damageable.TryChangeDamage(target, modifiedDamage, origin:user);
var damageResult = Damageable.TryChangeDamage(target, modifiedDamage, origin:user, ignoreResistances:resistanceBypass);
if (damageResult is {Empty: false})
{