CatStation/Content.Shared/Medical/Healing/HealingSystem.cs
Prole 502af6955d
Predict Forensics V2.0 (Ready for Review) (#40693)
* What sitting on code does to a branch + no mo owner v2

* Replacing ViewVariables with AutoNetworkedField + Missed One V2

* Removing Virtual Methods & Renaming ForensicsSystem to SharedForensicsSystem (Also made it abstract) V2

* IProduceWidgets Review Changes & some of my own V2

* Lil Cleanup V2

* Part 1 of Review Changes (Only thing left is the BUI stuff and the constructor) V2

* It now WORKS!!! (Still needs the BUI and constuctor changes, ill figure it out later)

* Test (Removal of ServerSide VomitSystem.cs)

* Ok...

* Moved System BloodstreamSystem.cs to SharedBloodstreamSystem.cs & deleted client BloodStreamSystem.cs to make SharedBloodstreamSystem.cs sealed. (No Unregistered errors)

* Prediction

* Fix?

* No mo log error?

* Revert "No mo log error?"

This reverts commit 50c15a3deab6af9dffd7baed45820bae5558e41f.

* Constructor going up

* No ForensicScannerBoundUserInterface.cs (It doesnt wanna scan anymore but it doesnt crash)

* For Conflicts

* Guh

* Yea

* Organization & no ErrorHell (maybe)

* Dirtying

* Dirtying take 2

* Revert "Dirtying take 2"

This reverts commit 5735012e21f43808db3ae010fce268eac24d0583.

* the dirties dont do much here

* For Conflict

* Readded

* Another Conflict Commit Fix

* Conflict Fix Thrice

* Empty Space

* Keep it simple

* Add it back in

* Move BloodstreamSystem stuff thats causing the error back to system. This'll prob fix the conflict

* Conflict fix?

* Trust the fix

* Looked at other prediction PR's, they had better code.

* To not mix up the system in client

* Removing Conflict

* For conflict

* For the conflic fix, will be removed later

* Scuffed Fix

* Removal & Adding the Decouple

* Took some HEAVY pointers from SirWarocks work. The Roadblock has been CLEARED, DOAFTERS WORK NOW!!

* Cleanup & Optimizing, mostly comps.

* More Optimization, most of this is just imported Warock Code, his QOL included. Did not want to remove SolutionDNA's tho, mostly due to a Log.Debug in OnPrint.

* Forgot that the Print Refreshes, more of Warocks work.

* Removing Loc.GetString

* All of BloodStream to Shared, hopefully no errors.

* Sketchy Dirty Removed

* Removed Timer.Spawn, added FrameUpdate cause anything else only updated when the window opened, & a bit of cleanup.

* No mo Mispredict

* GenerateDnaEvent Predicted

* Forgot the user

* I knew it was real, bloodstutter is REAL and now GONE.

* Cant dirty NewEntityBloodData due to potential stutter, so its safer to put it all back into server. again...

* Clearer

* Thank you leahcat, no more identity leak.

* Routine Maintenance

* sealing the class

* it checks for this now?!

* While im here, FileScope Namespaces & removing unused namespaces

* Update Attribute Type (SirWarock Import), it works and is much easier to read.

* Forever Fixing Conflicts

* Readonly Genocide & Partial Maxing

* LGTM, BloodstreamSystem moved to SharedBloodstreamSystem!

* Sohn

* If OG Github back push this commit

* One Billionth Commit Fix

* Removed client/server stuff

* Made implicit

* NewLine & comment on why AutoGenCompState is true

* Updatin the rest of the Popups to use Entity

* I think that's what they meant? It looks cleaner at least.

* Mass Attribute for LocalEvents that are safe to change.

* Putting it back as abstract

* maybe?

* Guhh, rolling back empty client/server of BloodStream

* Removed Client/Server BloodstreamSystems, changed access modifier from protected to private, & renamed SharedBloodstreamSystem to BloodstreamSystem

* From Protected to Private

* Removed comment & made it explicit on what changed to true

* Guh, hopefully thats all of it...

* Forgot

* Review

* Test Push

* Line 316 in SharedForensicsSystem was causing the errors, better to play it safe

* Review Changes

* Update Content.Shared/Forensics/Events.cs

Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com>

* The only thing SolutionDNAs does is search and clears itself. I see no reason to keep it

* The print.disabled didnt properly disable when cleared. So ill just use the old if check

* Figured out the rest of the SubscribeLocalEvents and networking BloodData cause its fully in Shared

---------

Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com>
2026-08-08 22:48:36 +00:00

255 lines
9.6 KiB
C#

using Content.Shared.Administration.Logs;
using Content.Shared.Body.Components;
using Content.Shared.Body.Systems;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Damage.Components;
using Content.Shared.Damage.Systems;
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.FixedPoint;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Popups;
using Content.Shared.Stacks;
using Robust.Shared.Audio.Systems;
namespace Content.Shared.Medical.Healing;
public sealed partial class HealingSystem : EntitySystem
{
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private ISharedAdminLogManager _adminLogger = default!;
[Dependency] private DamageableSystem _damageable = default!;
[Dependency] private BloodstreamSystem _bloodstreamSystem = default!;
[Dependency] private SharedDoAfterSystem _doAfter = default!;
[Dependency] private SharedStackSystem _stacks = default!;
[Dependency] private SharedInteractionSystem _interactionSystem = default!;
[Dependency] private MobThresholdSystem _mobThresholdSystem = default!;
[Dependency] private SharedPopupSystem _popupSystem = default!;
[Dependency] private SharedSolutionContainerSystem _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> target, ref HealingDoAfterEvent args)
{
if (args.Handled || args.Cancelled)
return;
if (!TryComp(args.Used, out HealingComponent? healing))
return;
if (!TryComp<InjurableComponent>(target, out var injurable))
return;
if (healing.DamageContainers is not null &&
injurable.DamageContainer is not null &&
!healing.DamageContainers.Contains(injurable.DamageContainer.Value))
{
return;
}
TryComp<BloodstreamComponent>(target, out var bloodstream);
// Heal some bloodloss damage.
if (healing.BloodlossModifier != 0 && bloodstream != null)
{
var isBleeding = bloodstream.BleedAmount > 0;
_bloodstreamSystem.TryModifyBleedAmount((target.Owner, bloodstream), healing.BloodlossModifier);
if (isBleeding != bloodstream.BleedAmount > 0)
{
var popup = (args.User == target.Owner)
? Loc.GetString("medical-item-stop-bleeding-self")
: Loc.GetString("medical-item-stop-bleeding", ("target", Identity.Entity(target.Owner, EntityManager)));
_popupSystem.PopupEntity(popup, target, args.User);
}
}
// Restores missing blood
if (healing.ModifyBloodLevel != 0 && bloodstream != null)
_bloodstreamSystem.TryModifyBloodLevel((target.Owner, bloodstream), healing.ModifyBloodLevel);
if (!_damageable.TryChangeDamage(target.Owner, healing.Damage * _damageable.UniversalTopicalsHealModifier, out var healed, true, origin: args.Args.User) && healing.BloodlossModifier != 0)
return;
var total = healed.GetTotal();
// Re-verify that we can heal the damage.
var dontRepeat = false;
if (TryComp<StackComponent>(args.Used.Value, out var stackComp))
{
_stacks.ReduceCount((args.Used.Value, stackComp), 1);
if (_stacks.GetCount((args.Used.Value, stackComp)) <= 0)
dontRepeat = true;
}
else
{
PredictedQueueDel(args.Used.Value);
}
if (target.Owner != args.User)
{
_adminLogger.Add(LogType.Healed,
$"{ToPrettyString(args.User):user} healed {ToPrettyString(target.Owner):target} for {total:damage} damage");
}
else
{
_adminLogger.Add(LogType.Healed,
$"{ToPrettyString(args.User):user} healed themselves for {total:damage} damage");
}
_audio.PlayPredicted(healing.HealingEndSound, target.Owner, args.User);
// Logic to determine the whether or not to repeat the healing action
args.Repeat = HasDamage((args.Used.Value, healing), target) && !dontRepeat;
args.Handled = true;
if (!args.Repeat)
{
_popupSystem.PopupEntity(Loc.GetString("medical-item-finished-using", ("item", args.Used)), target.Owner, args.User);
return;
}
// Update our self heal delay so it shortens as we heal more damage.
if (args.User == target.Owner)
args.Args.Delay = healing.Delay * GetScaledHealingPenalty(target.Owner, healing.SelfHealPenaltyMultiplier);
}
private bool HasDamage(Entity<HealingComponent> healing, Entity<DamageableComponent> target)
{
var damageableDict = _damageable.GetAllDamage(target.AsNullable()).DamageDict;
var healingDict = healing.Comp.Damage.DamageDict;
foreach (var type in healingDict)
{
if (damageableDict.TryGetValue(type.Key, out var amount) && amount > 0)
{
return true;
}
}
if (TryComp<BloodstreamComponent>(target, out var bloodstream))
{
// Is ent missing blood that we can restore?
if (healing.Comp.ModifyBloodLevel > 0
&& _solutionContainerSystem.ResolveSolution(target.Owner, bloodstream.BloodSolutionName, ref bloodstream.BloodSolution, out var bloodSolution)
&& _bloodstreamSystem.GetBloodLevel((target, bloodstream)) < 1)
{
return true;
}
// Is ent bleeding and can we stop it?
if (healing.Comp.BloodlossModifier < 0 && bloodstream.BleedAmount > 0)
{
return true;
}
}
return false;
}
private void OnHealingUse(Entity<HealingComponent> healing, ref UseInHandEvent args)
{
if (args.Handled)
return;
if (TryHeal(healing, args.User, args.User))
args.Handled = true;
}
private void OnHealingAfterInteract(Entity<HealingComponent> healing, ref AfterInteractEvent args)
{
if (args.Handled || !args.CanReach || args.Target == null)
return;
if (TryHeal(healing, args.Target.Value, args.User))
args.Handled = true;
}
private bool TryHeal(Entity<HealingComponent> healing, Entity<DamageableComponent?> target, EntityUid user)
{
if (!Resolve(target, ref target.Comp, false))
return false;
if (!TryComp<InjurableComponent>(target, out var injurable))
return false;
if (healing.Comp.DamageContainers is not null &&
injurable.DamageContainer is not null &&
!healing.Comp.DamageContainers.Contains(injurable.DamageContainer.Value))
{
return false;
}
if (user != target.Owner && !_interactionSystem.InRangeUnobstructed(user, target.Owner, popup: true))
return false;
if (TryComp<StackComponent>(healing, out var stack) && stack.Count < 1)
return false;
if (!HasDamage(healing, target!))
{
_popupSystem.PopupEntity(Loc.GetString("medical-item-cant-use", ("item", healing.Owner)), healing, user);
return false;
}
_audio.PlayPredicted(healing.Comp.HealingBeginSound, healing, user);
var isNotSelf = user != target.Owner;
if (isNotSelf)
{
var msg = Loc.GetString("medical-item-popup-target", ("user", Identity.Entity(user, EntityManager)), ("item", healing.Owner));
_popupSystem.PopupEntity(msg, target, target, PopupType.Medium);
}
var delay = isNotSelf
? healing.Comp.Delay
: healing.Comp.Delay * GetScaledHealingPenalty(target, healing.Comp.SelfHealPenaltyMultiplier);
var doAfterEventArgs =
new DoAfterArgs(EntityManager, user, delay, new HealingDoAfterEvent(), target, target: target, used: healing)
{
// 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="ent">Entity we're healing</param>
/// <param name="mod">Maximum modifier we can have.</param>
/// <returns>Modifier we multiply our healing time by</returns>
public float GetScaledHealingPenalty(Entity<DamageableComponent?, MobThresholdsComponent?> ent, float mod)
{
if (!Resolve(ent, ref ent.Comp1, ref ent.Comp2, false))
return mod;
if (!_mobThresholdSystem.TryGetThresholdForState(ent, MobState.Critical, out var amount, ent.Comp2))
return 1;
var percentDamage = (float)(_damageable.GetTotalDamage(ent) / amount);
//basically make it scale from 1 to the multiplier.
var output = percentDamage * (mod - 1) + 1;
return Math.Max(output, 1);
}
}