Files
tbd-station-14/Content.Server/Medical/HealingSystem.cs
nikthechampiongr 362d56981f Simplify DoAfterArgs behavior for movement and distance checks (#25226)
* Merge BreakOnWeightlessMove and BreakOnMove. Provide different theshold for weightless movement.

* Adjust WeightlessMovementThresholds. Put a thing I forgot to put in the doafterargs.

* Make DoAfterArgs only use OnMove to determine whether to check for
movement and MoveThreshold to determine the threshold regardless of
weightlessness. Gave DistanceThreshold a default value which will always
be checked now.

* Fix issue introduced by merge.

* Use interaction system for determining whether a distance is within range

* Fix incorrect doafter args introduced by previous merge.
Forgor to commit these.

* Exorcise ghost.

The execution system should have been deleted when I merged previously.
For a reason I cannot comprehend it came back, but only the execution
system.

* Exorcise ghost Pt. 2

* Allow for movement check to be overriden in zero g and adjust doafter args where needed.

You can now override checking for movement in zero g with the BreakOnWeightlessMove bool. By default it will check.
The following doafters were made to ignore the movement check in zero g:
- Healing yourself with healing items,
- Removing embedded projectiles,
- Using tools like welders and crowbars

* Adjust distance for cuffing/uncuffing to work. Make injections not break on weightless movement.

* Fix evil incorrect and uneeded comments
2024-03-19 21:09:00 +11:00

231 lines
8.7 KiB
C#

using Content.Server.Administration.Logs;
using Content.Server.Body.Components;
using Content.Server.Body.Systems;
using Content.Server.Chemistry.Containers.EntitySystems;
using Content.Server.Medical.Components;
using Content.Server.Popups;
using Content.Server.Stack;
using Content.Shared.Audio;
using Content.Shared.Damage;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Medical;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Stacks;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Random;
namespace Content.Server.Medical;
public sealed class HealingSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly BloodstreamSystem _bloodstreamSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly StackSystem _stacks = default!;
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
[Dependency] private readonly MobThresholdSystem _mobThresholdSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HealingComponent, UseInHandEvent>(OnHealingUse);
SubscribeLocalEvent<HealingComponent, AfterInteractEvent>(OnHealingAfterInteract);
SubscribeLocalEvent<DamageableComponent, HealingDoAfterEvent>(OnDoAfter);
}
private void OnDoAfter(Entity<DamageableComponent> entity, ref HealingDoAfterEvent args)
{
var dontRepeat = false;
if (!TryComp(args.Used, out HealingComponent? healing))
return;
if (args.Handled || args.Cancelled)
return;
if (healing.DamageContainers is not null &&
entity.Comp.DamageContainerID is not null &&
!healing.DamageContainers.Contains(entity.Comp.DamageContainerID))
{
return;
}
// Heal some bloodloss damage.
if (healing.BloodlossModifier != 0)
{
if (!TryComp<BloodstreamComponent>(entity, out var bloodstream))
return;
var isBleeding = bloodstream.BleedAmount > 0;
_bloodstreamSystem.TryModifyBleedAmount(entity.Owner, healing.BloodlossModifier);
if (isBleeding != bloodstream.BleedAmount > 0)
{
dontRepeat = true;
_popupSystem.PopupEntity(Loc.GetString("medical-item-stop-bleeding"), entity, args.User);
}
}
// Restores missing blood
if (healing.ModifyBloodLevel != 0)
_bloodstreamSystem.TryModifyBloodLevel(entity.Owner, healing.ModifyBloodLevel);
var healed = _damageable.TryChangeDamage(entity.Owner, healing.Damage, true, origin: args.Args.User);
if (healed == null && healing.BloodlossModifier != 0)
return;
var total = healed?.GetTotal() ?? FixedPoint2.Zero;
// Re-verify that we can heal the damage.
if (TryComp<StackComponent>(args.Used.Value, out var stackComp))
{
_stacks.Use(args.Used.Value, 1, stackComp);
if (_stacks.GetCount(args.Used.Value, stackComp) <= 0)
dontRepeat = true;
}
else
{
QueueDel(args.Used.Value);
}
if (entity.Owner != args.User)
{
_adminLogger.Add(LogType.Healed,
$"{EntityManager.ToPrettyString(args.User):user} healed {EntityManager.ToPrettyString(entity.Owner):target} for {total:damage} damage");
}
else
{
_adminLogger.Add(LogType.Healed,
$"{EntityManager.ToPrettyString(args.User):user} healed themselves for {total:damage} damage");
}
_audio.PlayPvs(healing.HealingEndSound, entity.Owner, AudioHelpers.WithVariation(0.125f, _random).WithVolume(1f));
// Logic to determine the whether or not to repeat the healing action
args.Repeat = (HasDamage(entity.Comp, healing) && !dontRepeat);
if (!args.Repeat && !dontRepeat)
_popupSystem.PopupEntity(Loc.GetString("medical-item-finished-using", ("item", args.Used)), entity.Owner, args.User);
args.Handled = true;
}
private bool HasDamage(DamageableComponent component, HealingComponent healing)
{
var damageableDict = component.Damage.DamageDict;
var healingDict = healing.Damage.DamageDict;
foreach (var type in healingDict)
{
if (damageableDict[type.Key].Value > 0)
{
return true;
}
}
return false;
}
private void OnHealingUse(Entity<HealingComponent> entity, ref UseInHandEvent args)
{
if (args.Handled)
return;
if (TryHeal(entity, args.User, args.User, entity.Comp))
args.Handled = true;
}
private void OnHealingAfterInteract(Entity<HealingComponent> entity, ref AfterInteractEvent args)
{
if (args.Handled || !args.CanReach || args.Target == null)
return;
if (TryHeal(entity, args.User, args.Target.Value, entity.Comp))
args.Handled = true;
}
private bool TryHeal(EntityUid uid, EntityUid user, EntityUid target, HealingComponent component)
{
if (!TryComp<DamageableComponent>(target, out var targetDamage))
return false;
if (component.DamageContainers is not null &&
targetDamage.DamageContainerID is not null &&
!component.DamageContainers.Contains(targetDamage.DamageContainerID))
{
return false;
}
if (user != target && !_interactionSystem.InRangeUnobstructed(user, target, popup: true))
return false;
if (TryComp<StackComponent>(uid, out var stack) && stack.Count < 1)
return false;
var anythingToDo =
HasDamage(targetDamage, component) ||
component.ModifyBloodLevel > 0 // Special case if healing item can restore lost blood...
&& TryComp<BloodstreamComponent>(target, out var bloodstream)
&& _solutionContainerSystem.ResolveSolution(target, bloodstream.BloodSolutionName, ref bloodstream.BloodSolution, out var bloodSolution)
&& bloodSolution.Volume < bloodSolution.MaxVolume; // ...and there is lost blood to restore.
if (!anythingToDo)
{
_popupSystem.PopupEntity(Loc.GetString("medical-item-cant-use", ("item", uid)), uid, user);
return false;
}
_audio.PlayPvs(component.HealingBeginSound, uid,
AudioHelpers.WithVariation(0.125f, _random).WithVolume(1f));
var isNotSelf = user != target;
var delay = isNotSelf
? component.Delay
: component.Delay * GetScaledHealingPenalty(user, component);
var doAfterEventArgs =
new DoAfterArgs(EntityManager, user, delay, new HealingDoAfterEvent(), target, target: target, used: uid)
{
// Didn't break on damage as they may be trying to prevent it and
// not being able to heal your own ticking damage would be frustrating.
NeedHand = true,
BreakOnMove = true,
BreakOnWeightlessMove = false,
};
_doAfter.TryStartDoAfter(doAfterEventArgs);
return true;
}
/// <summary>
/// Scales the self-heal penalty based on the amount of damage taken
/// </summary>
/// <param name="uid"></param>
/// <param name="component"></param>
/// <returns></returns>
public float GetScaledHealingPenalty(EntityUid uid, HealingComponent component)
{
var output = component.Delay;
if (!TryComp<MobThresholdsComponent>(uid, out var mobThreshold) ||
!TryComp<DamageableComponent>(uid, out var damageable))
return output;
if (!_mobThresholdSystem.TryGetThresholdForState(uid, MobState.Critical, out var amount, mobThreshold))
return 1;
var percentDamage = (float) (damageable.TotalDamage / amount);
//basically make it scale from 1 to the multiplier.
var modifier = percentDamage * (component.SelfHealPenaltyMultiplier - 1) + 1;
return Math.Max(modifier, 1);
}
}