DoAfter Refactor (#13225)
Co-authored-by: DrSmugleaf <drsmugleaf@gmail.com>
This commit is contained in:
@@ -9,8 +9,6 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
[RegisterComponent]
|
||||
public sealed class BiomassReclaimerComponent : Component
|
||||
{
|
||||
public CancellationTokenSource? CancelToken;
|
||||
|
||||
/// <summary>
|
||||
/// This gets set for each mob it processes.
|
||||
/// When it hits 0, there is a chance for the reclaimer to either spill blood or throw an item.
|
||||
|
||||
@@ -17,6 +17,7 @@ using Content.Server.Construction;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Materials;
|
||||
using Content.Server.Mind.Components;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Humanoid;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Mobs.Components;
|
||||
@@ -96,8 +97,7 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
SubscribeLocalEvent<BiomassReclaimerComponent, UpgradeExamineEvent>(OnUpgradeExamine);
|
||||
SubscribeLocalEvent<BiomassReclaimerComponent, PowerChangedEvent>(OnPowerChanged);
|
||||
SubscribeLocalEvent<BiomassReclaimerComponent, SuicideEvent>(OnSuicide);
|
||||
SubscribeLocalEvent<ReclaimSuccessfulEvent>(OnReclaimSuccessful);
|
||||
SubscribeLocalEvent<ReclaimCancelledEvent>(OnReclaimCancelled);
|
||||
SubscribeLocalEvent<BiomassReclaimerComponent, DoAfterEvent>(OnDoAfter);
|
||||
}
|
||||
|
||||
private void OnSuicide(EntityUid uid, BiomassReclaimerComponent component, SuicideEvent args)
|
||||
@@ -146,20 +146,14 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
}
|
||||
private void OnAfterInteractUsing(EntityUid uid, BiomassReclaimerComponent component, AfterInteractUsingEvent args)
|
||||
{
|
||||
if (!args.CanReach)
|
||||
return;
|
||||
|
||||
if (component.CancelToken != null || args.Target == null)
|
||||
if (!args.CanReach || args.Target == null)
|
||||
return;
|
||||
|
||||
if (!HasComp<MobStateComponent>(args.Used) || !CanGib(uid, args.Used, component))
|
||||
return;
|
||||
|
||||
component.CancelToken = new CancellationTokenSource();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, 7f, component.CancelToken.Token, args.Target, args.Used)
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, 7f, target:args.Target, used:args.Used)
|
||||
{
|
||||
BroadcastFinishedEvent = new ReclaimSuccessfulEvent(args.User, args.Used, uid),
|
||||
BroadcastCancelledEvent = new ReclaimCancelledEvent(uid),
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
BreakOnStun = true,
|
||||
@@ -200,21 +194,15 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
args.AddPercentageUpgrade("biomass-reclaimer-component-upgrade-biomass-yield", component.YieldPerUnitMass / component.BaseYieldPerUnitMass);
|
||||
}
|
||||
|
||||
private void OnReclaimSuccessful(ReclaimSuccessfulEvent args)
|
||||
private void OnDoAfter(EntityUid uid, BiomassReclaimerComponent component, DoAfterEvent args)
|
||||
{
|
||||
if (!TryComp<BiomassReclaimerComponent>(args.Reclaimer, out var reclaimer))
|
||||
if (args.Handled || args.Cancelled || args.Args.Target == null || HasComp<BiomassReclaimerComponent>(args.Args.Target.Value))
|
||||
return;
|
||||
|
||||
_adminLogger.Add(LogType.Action, LogImpact.Extreme, $"{ToPrettyString(args.User):player} used a biomass reclaimer to gib {ToPrettyString(args.Target):target} in {ToPrettyString(args.Reclaimer):reclaimer}");
|
||||
reclaimer.CancelToken = null;
|
||||
StartProcessing(args.Target, reclaimer);
|
||||
}
|
||||
_adminLogger.Add(LogType.Action, LogImpact.Extreme, $"{ToPrettyString(args.Args.User):player} used a biomass reclaimer to gib {ToPrettyString(args.Args.Target.Value):target} in {ToPrettyString(uid):reclaimer}");
|
||||
StartProcessing(args.Args.Target.Value, component);
|
||||
|
||||
private void OnReclaimCancelled(ReclaimCancelledEvent args)
|
||||
{
|
||||
if (!TryComp<BiomassReclaimerComponent>(args.Reclaimer, out var reclaimer))
|
||||
return;
|
||||
reclaimer.CancelToken = null;
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void StartProcessing(EntityUid toProcess, BiomassReclaimerComponent component, PhysicsComponent? physics = null)
|
||||
@@ -266,28 +254,5 @@ namespace Content.Server.Medical.BiomassReclaimer
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private readonly struct ReclaimCancelledEvent
|
||||
{
|
||||
public readonly EntityUid Reclaimer;
|
||||
|
||||
public ReclaimCancelledEvent(EntityUid reclaimer)
|
||||
{
|
||||
Reclaimer = reclaimer;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly struct ReclaimSuccessfulEvent
|
||||
{
|
||||
public readonly EntityUid User;
|
||||
public readonly EntityUid Target;
|
||||
public readonly EntityUid Reclaimer;
|
||||
public ReclaimSuccessfulEvent(EntityUid user, EntityUid target, EntityUid reclaimer)
|
||||
{
|
||||
User = user;
|
||||
Target = target;
|
||||
Reclaimer = reclaimer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +45,6 @@ namespace Content.Server.Medical.Components
|
||||
[DataField("selfHealPenaltyMultiplier")]
|
||||
public float SelfHealPenaltyMultiplier = 3f;
|
||||
|
||||
public CancellationTokenSource? CancelToken = null;
|
||||
|
||||
/// <summary>
|
||||
/// Sound played on healing begin
|
||||
/// </summary>
|
||||
|
||||
@@ -20,10 +20,7 @@ namespace Content.Server.Medical.Components
|
||||
/// </summary>
|
||||
[DataField("scanDelay")]
|
||||
public float ScanDelay = 0.8f;
|
||||
/// <summary>
|
||||
/// Token for interrupting scanning do after.
|
||||
/// </summary>
|
||||
public CancellationTokenSource? CancelToken;
|
||||
|
||||
public BoundUserInterface? UserInterface => Owner.GetUIOrNull(HealthAnalyzerUiKey.Key);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Threading;
|
||||
using Content.Server.Atmos;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Atmos.Piping.Components;
|
||||
@@ -14,18 +13,19 @@ using Content.Server.NodeContainer;
|
||||
using Content.Server.NodeContainer.NodeGroups;
|
||||
using Content.Server.NodeContainer.Nodes;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Tools;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Chemistry;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.Containers.ItemSlots;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Emag.Systems;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Medical.Cryogenics;
|
||||
using Content.Shared.MedicalScanner;
|
||||
using Content.Shared.Tools;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Server.GameObjects;
|
||||
@@ -44,7 +44,7 @@ public sealed partial class CryoPodSystem: SharedCryoPodSystem
|
||||
[Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
[Dependency] private readonly ToolSystem _toolSystem = default!;
|
||||
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly MetaDataSystem _metaDataSystem = default!;
|
||||
[Dependency] private readonly ReactiveSystem _reactiveSystem = default!;
|
||||
@@ -57,8 +57,7 @@ public sealed partial class CryoPodSystem: SharedCryoPodSystem
|
||||
SubscribeLocalEvent<CryoPodComponent, ComponentInit>(OnComponentInit);
|
||||
SubscribeLocalEvent<CryoPodComponent, GetVerbsEvent<AlternativeVerb>>(AddAlternativeVerbs);
|
||||
SubscribeLocalEvent<CryoPodComponent, GotEmaggedEvent>(OnEmagged);
|
||||
SubscribeLocalEvent<CryoPodComponent, DoInsertCryoPodEvent>(DoInsertCryoPod);
|
||||
SubscribeLocalEvent<CryoPodComponent, DoInsertCancelledCryoPodEvent>(DoInsertCancelCryoPod);
|
||||
SubscribeLocalEvent<CryoPodComponent, DoAfterEvent>(OnDoAfter);
|
||||
SubscribeLocalEvent<CryoPodComponent, CryoPodPryFinished>(OnCryoPodPryFinished);
|
||||
SubscribeLocalEvent<CryoPodComponent, CryoPodPryInterrupted>(OnCryoPodPryInterrupted);
|
||||
|
||||
@@ -130,32 +129,30 @@ public sealed partial class CryoPodSystem: SharedCryoPodSystem
|
||||
private void HandleDragDropOn(EntityUid uid, CryoPodComponent cryoPodComponent, ref DragDropTargetEvent args)
|
||||
{
|
||||
if (cryoPodComponent.BodyContainer.ContainedEntity != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (cryoPodComponent.DragDropCancelToken != null)
|
||||
{
|
||||
cryoPodComponent.DragDropCancelToken.Cancel();
|
||||
cryoPodComponent.DragDropCancelToken = null;
|
||||
return;
|
||||
}
|
||||
|
||||
cryoPodComponent.DragDropCancelToken = new CancellationTokenSource();
|
||||
var doAfterArgs = new DoAfterEventArgs(args.User, cryoPodComponent.EntryDelay, cryoPodComponent.DragDropCancelToken.Token, uid, args.Dragged)
|
||||
var doAfterArgs = new DoAfterEventArgs(args.User, cryoPodComponent.EntryDelay, target:args.Dragged, used:uid)
|
||||
{
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
NeedHand = false,
|
||||
TargetFinishedEvent = new DoInsertCryoPodEvent(args.Dragged),
|
||||
TargetCancelledEvent = new DoInsertCancelledCryoPodEvent()
|
||||
};
|
||||
_doAfterSystem.DoAfter(doAfterArgs);
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnDoAfter(EntityUid uid, CryoPodComponent component, DoAfterEvent args)
|
||||
{
|
||||
if (args.Cancelled || args.Handled || args.Args.Target == null)
|
||||
return;
|
||||
|
||||
InsertBody(uid, args.Args.Target.Value, component);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnActivateUIAttempt(EntityUid uid, CryoPodComponent cryoPodComponent, ActivatableUIOpenAttemptEvent args)
|
||||
{
|
||||
if (args.Cancelled)
|
||||
@@ -190,9 +187,8 @@ public sealed partial class CryoPodSystem: SharedCryoPodSystem
|
||||
return;
|
||||
cryoPodComponent.IsPrying = true;
|
||||
|
||||
_toolSystem.UseTool(args.Used, args.User, uid, 0f,
|
||||
cryoPodComponent.PryDelay, "Prying",
|
||||
new CryoPodPryFinished(), new CryoPodPryInterrupted(), uid);
|
||||
var toolEvData = new ToolEventData(new CryoPodPryFinished(), targetEntity:uid);
|
||||
_toolSystem.UseTool(args.Used, args.User, uid, cryoPodComponent.PryDelay, new [] {"Prying"}, toolEvData);
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
@@ -200,7 +196,7 @@ public sealed partial class CryoPodSystem: SharedCryoPodSystem
|
||||
|
||||
private void OnExamined(EntityUid uid, CryoPodComponent component, ExaminedEvent args)
|
||||
{
|
||||
var container = _itemSlotsSystem.GetItemOrNull(component.Owner, component.SolutionContainerName);
|
||||
var container = _itemSlotsSystem.GetItemOrNull(uid, component.SolutionContainerName);
|
||||
if (args.IsInDetailsRange && container != null && _solutionContainerSystem.TryGetFitsInDispenser(container.Value, out var containerSolution))
|
||||
{
|
||||
args.PushMarkup(Loc.GetString("cryo-pod-examine", ("beaker", Name(container.Value))));
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Threading;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Server.DoAfter;
|
||||
@@ -7,6 +6,7 @@ using Content.Server.Stack;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Mobs;
|
||||
@@ -35,51 +35,39 @@ public sealed class HealingSystem : EntitySystem
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<HealingComponent, UseInHandEvent>(OnHealingUse);
|
||||
SubscribeLocalEvent<HealingComponent, AfterInteractEvent>(OnHealingAfterInteract);
|
||||
SubscribeLocalEvent<HealingCancelledEvent>(OnHealingCancelled);
|
||||
SubscribeLocalEvent<DamageableComponent, HealingCompleteEvent>(OnHealingComplete);
|
||||
SubscribeLocalEvent<DamageableComponent, DoAfterEvent<HealingData>>(OnDoAfter);
|
||||
}
|
||||
|
||||
private void OnHealingComplete(EntityUid uid, DamageableComponent component, HealingCompleteEvent args)
|
||||
private void OnDoAfter(EntityUid uid, DamageableComponent component, DoAfterEvent<HealingData> args)
|
||||
{
|
||||
if (_mobStateSystem.IsDead(uid))
|
||||
if (args.Handled || args.Cancelled || _mobStateSystem.IsDead(uid) || args.Args.Used == null)
|
||||
return;
|
||||
|
||||
if (TryComp<StackComponent>(args.Component.Owner, out var stack) && stack.Count < 1)
|
||||
if (component.DamageContainerID is not null && !component.DamageContainerID.Equals(component.DamageContainerID))
|
||||
return;
|
||||
|
||||
if (component.DamageContainerID is not null &&
|
||||
!component.DamageContainerID.Equals(component.DamageContainerID))
|
||||
return;
|
||||
// Heal some bloodloss damage.
|
||||
if (args.AdditionalData.HealingComponent.BloodlossModifier != 0)
|
||||
_bloodstreamSystem.TryModifyBleedAmount(uid, args.AdditionalData.HealingComponent.BloodlossModifier);
|
||||
|
||||
if (args.Component.BloodlossModifier != 0)
|
||||
{
|
||||
// Heal some bloodloss damage.
|
||||
_bloodstreamSystem.TryModifyBleedAmount(uid, args.Component.BloodlossModifier);
|
||||
}
|
||||
var healed = _damageable.TryChangeDamage(uid, args.AdditionalData.HealingComponent.Damage, true, origin: args.Args.User);
|
||||
|
||||
var healed = _damageable.TryChangeDamage(uid, args.Component.Damage, true, origin: args.User);
|
||||
|
||||
// Reverify that we can heal the damage.
|
||||
if (healed == null)
|
||||
return;
|
||||
|
||||
_stacks.Use(args.Component.Owner, 1, stack);
|
||||
// Reverify that we can heal the damage.
|
||||
_stacks.Use(args.Args.Used.Value, 1, args.AdditionalData.Stack);
|
||||
|
||||
if (uid != args.Args.User)
|
||||
_adminLogger.Add(LogType.Healed, $"{EntityManager.ToPrettyString(args.Args.User):user} healed {EntityManager.ToPrettyString(uid):target} for {healed.Total:damage} damage");
|
||||
|
||||
if (uid != args.User)
|
||||
_adminLogger.Add(LogType.Healed, $"{EntityManager.ToPrettyString(args.User):user} healed {EntityManager.ToPrettyString(uid):target} for {healed.Total:damage} damage");
|
||||
else
|
||||
_adminLogger.Add(LogType.Healed, $"{EntityManager.ToPrettyString(args.User):user} healed themselves for {healed.Total:damage} damage");
|
||||
_adminLogger.Add(LogType.Healed, $"{EntityManager.ToPrettyString(args.Args.User):user} healed themselves for {healed.Total:damage} damage");
|
||||
|
||||
if (args.Component.HealingEndSound != null)
|
||||
{
|
||||
_audio.PlayPvs(args.Component.HealingEndSound, uid,
|
||||
AudioHelpers.WithVariation(0.125f, _random).WithVolume(-5f));
|
||||
}
|
||||
}
|
||||
if (args.AdditionalData.HealingComponent.HealingEndSound != null)
|
||||
_audio.PlayPvs(args.AdditionalData.HealingComponent.HealingEndSound, uid, AudioHelpers.WithVariation(0.125f, _random).WithVolume(-5f));
|
||||
|
||||
private static void OnHealingCancelled(HealingCancelledEvent ev)
|
||||
{
|
||||
ev.Component.CancelToken = null;
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OnHealingUse(EntityUid uid, HealingComponent component, UseInHandEvent args)
|
||||
@@ -102,15 +90,7 @@ public sealed class HealingSystem : EntitySystem
|
||||
|
||||
private bool TryHeal(EntityUid uid, EntityUid user, EntityUid target, HealingComponent component)
|
||||
{
|
||||
if (component.CancelToken != null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_mobStateSystem.IsDead(target))
|
||||
return false;
|
||||
|
||||
if (!TryComp<DamageableComponent>(target, out var targetDamage))
|
||||
if (_mobStateSystem.IsDead(target) || !TryComp<DamageableComponent>(target, out var targetDamage))
|
||||
return false;
|
||||
|
||||
if (targetDamage.TotalDamage == 0)
|
||||
@@ -119,28 +99,22 @@ public sealed class HealingSystem : EntitySystem
|
||||
if (component.DamageContainerID is not null && !component.DamageContainerID.Equals(targetDamage.DamageContainerID))
|
||||
return false;
|
||||
|
||||
if (user != target &&
|
||||
!_interactionSystem.InRangeUnobstructed(user, target, popup: true))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (TryComp<StackComponent>(component.Owner, out var stack) && stack.Count < 1)
|
||||
if (user != target && !_interactionSystem.InRangeUnobstructed(user, target, popup: true))
|
||||
return false;
|
||||
|
||||
component.CancelToken = new CancellationTokenSource();
|
||||
if (!TryComp<StackComponent>(uid, out var stack) || stack.Count < 1)
|
||||
return false;
|
||||
|
||||
if (component.HealingBeginSound != null)
|
||||
{
|
||||
_audio.PlayPvs(component.HealingBeginSound, uid,
|
||||
AudioHelpers.WithVariation(0.125f, _random).WithVolume(-5f));
|
||||
}
|
||||
_audio.PlayPvs(component.HealingBeginSound, uid, AudioHelpers.WithVariation(0.125f, _random).WithVolume(-5f));
|
||||
|
||||
var delay = user != target
|
||||
? component.Delay
|
||||
: component.Delay * GetScaledHealingPenalty(user, component);
|
||||
|
||||
_doAfter.DoAfter(new DoAfterEventArgs(user, delay, component.CancelToken.Token, target)
|
||||
var healingData = new HealingData(component, stack);
|
||||
|
||||
var doAfterEventArgs = new DoAfterEventArgs(user, delay, target: target, used: uid)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnTargetMove = true,
|
||||
@@ -148,22 +122,11 @@ public sealed class HealingSystem : EntitySystem
|
||||
// not being able to heal your own ticking damage would be frustrating.
|
||||
BreakOnStun = true,
|
||||
NeedHand = true,
|
||||
TargetFinishedEvent = new HealingCompleteEvent
|
||||
{
|
||||
User = user,
|
||||
Component = component,
|
||||
},
|
||||
BroadcastCancelledEvent = new HealingCancelledEvent
|
||||
{
|
||||
Component = component,
|
||||
},
|
||||
// Juusstt in case damageble gets removed it avoids having to re-cancel the token. Won't need this when DoAfterEvent<T> gets added.
|
||||
PostCheck = () =>
|
||||
{
|
||||
component.CancelToken = null;
|
||||
return true;
|
||||
},
|
||||
});
|
||||
PostCheck = () => true
|
||||
};
|
||||
|
||||
_doAfter.DoAfter(doAfterEventArgs, healingData);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -179,27 +142,18 @@ public sealed class HealingSystem : EntitySystem
|
||||
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))
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
private sealed class HealingCompleteEvent : EntityEventArgs
|
||||
private record struct HealingData(HealingComponent HealingComponent, StackComponent Stack)
|
||||
{
|
||||
public EntityUid User;
|
||||
public HealingComponent Component = default!;
|
||||
}
|
||||
|
||||
private sealed class HealingCancelledEvent : EntityEventArgs
|
||||
{
|
||||
public HealingComponent Component = default!;
|
||||
public HealingComponent HealingComponent = HealingComponent;
|
||||
public StackComponent Stack = Stack;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
using System.Threading;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Medical.Components;
|
||||
using Content.Server.Disease;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.IdentityManagement;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Audio;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Player;
|
||||
using static Content.Shared.MedicalScanner.SharedHealthAnalyzerComponent;
|
||||
|
||||
namespace Content.Server.Medical
|
||||
@@ -20,14 +18,14 @@ namespace Content.Server.Medical
|
||||
[Dependency] private readonly DiseaseSystem _disease = default!;
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<HealthAnalyzerComponent, ActivateInWorldEvent>(HandleActivateInWorld);
|
||||
SubscribeLocalEvent<HealthAnalyzerComponent, AfterInteractEvent>(OnAfterInteract);
|
||||
SubscribeLocalEvent<TargetScanSuccessfulEvent>(OnTargetScanSuccessful);
|
||||
SubscribeLocalEvent<ScanCancelledEvent>(OnScanCancelled);
|
||||
SubscribeLocalEvent<HealthAnalyzerComponent, DoAfterEvent>(OnDoAfter);
|
||||
}
|
||||
|
||||
private void HandleActivateInWorld(EntityUid uid, HealthAnalyzerComponent healthAnalyzer, ActivateInWorldEvent args)
|
||||
@@ -37,33 +35,13 @@ namespace Content.Server.Medical
|
||||
|
||||
private void OnAfterInteract(EntityUid uid, HealthAnalyzerComponent healthAnalyzer, AfterInteractEvent args)
|
||||
{
|
||||
if (healthAnalyzer.CancelToken != null)
|
||||
{
|
||||
healthAnalyzer.CancelToken.Cancel();
|
||||
healthAnalyzer.CancelToken = null;
|
||||
if (args.Target == null || !args.CanReach || !HasComp<MobStateComponent>(args.Target))
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.Target == null)
|
||||
return;
|
||||
|
||||
if (!args.CanReach)
|
||||
return;
|
||||
|
||||
if (healthAnalyzer.CancelToken != null)
|
||||
return;
|
||||
|
||||
if (!HasComp<MobStateComponent>(args.Target))
|
||||
return;
|
||||
|
||||
healthAnalyzer.CancelToken = new CancellationTokenSource();
|
||||
|
||||
_audio.PlayPvs(healthAnalyzer.ScanningBeginSound, uid);
|
||||
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, healthAnalyzer.ScanDelay, healthAnalyzer.CancelToken.Token, target: args.Target)
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(args.User, healthAnalyzer.ScanDelay, target: args.Target, used:uid)
|
||||
{
|
||||
BroadcastFinishedEvent = new TargetScanSuccessfulEvent(args.User, args.Target, healthAnalyzer),
|
||||
BroadcastCancelledEvent = new ScanCancelledEvent(healthAnalyzer),
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
BreakOnStun = true,
|
||||
@@ -71,37 +49,47 @@ namespace Content.Server.Medical
|
||||
});
|
||||
}
|
||||
|
||||
private void OnTargetScanSuccessful(TargetScanSuccessfulEvent args)
|
||||
private void OnDoAfter(EntityUid uid, HealthAnalyzerComponent component, DoAfterEvent args)
|
||||
{
|
||||
args.Component.CancelToken = null;
|
||||
if (args.Handled || args.Cancelled || args.Args.Target == null)
|
||||
return;
|
||||
|
||||
_audio.PlayPvs(args.Component.ScanningEndSound, args.User);
|
||||
_audio.PlayPvs(component.ScanningEndSound, args.Args.User);
|
||||
|
||||
UpdateScannedUser(args.Component.Owner, args.User, args.Target, args.Component);
|
||||
UpdateScannedUser(uid, args.Args.User, args.Args.Target.Value, component);
|
||||
// Below is for the traitor item
|
||||
// Piggybacking off another component's doafter is complete CBT so I gave up
|
||||
// and put it on the same component
|
||||
if (string.IsNullOrEmpty(args.Component.Disease) || args.Target == null)
|
||||
return;
|
||||
|
||||
_disease.TryAddDisease(args.Target.Value, args.Component.Disease);
|
||||
|
||||
if (args.User == args.Target)
|
||||
if (string.IsNullOrEmpty(component.Disease))
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("disease-scanner-gave-self", ("disease", args.Component.Disease)),
|
||||
args.User, args.User);
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
_popupSystem.PopupEntity(Loc.GetString("disease-scanner-gave-other", ("target", Identity.Entity(args.Target.Value, EntityManager)), ("disease", args.Component.Disease)),
|
||||
args.User, args.User);
|
||||
|
||||
_disease.TryAddDisease(args.Args.Target.Value, component.Disease);
|
||||
|
||||
if (args.Args.User == args.Args.Target)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("disease-scanner-gave-self", ("disease", component.Disease)),
|
||||
args.Args.User, args.Args.User);
|
||||
}
|
||||
|
||||
|
||||
else
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("disease-scanner-gave-other", ("target", Identity.Entity(args.Args.Target.Value, EntityManager)),
|
||||
("disease", component.Disease)), args.Args.User, args.Args.User);
|
||||
}
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
private void OpenUserInterface(EntityUid user, HealthAnalyzerComponent healthAnalyzer)
|
||||
{
|
||||
if (!TryComp<ActorComponent>(user, out var actor))
|
||||
if (!TryComp<ActorComponent>(user, out var actor) || healthAnalyzer.UserInterface == null)
|
||||
return;
|
||||
|
||||
healthAnalyzer.UserInterface?.Open(actor.PlayerSession);
|
||||
_uiSystem.OpenUi(healthAnalyzer.UserInterface ,actor.PlayerSession);
|
||||
}
|
||||
|
||||
public void UpdateScannedUser(EntityUid uid, EntityUid user, EntityUid? target, HealthAnalyzerComponent? healthAnalyzer)
|
||||
@@ -116,35 +104,7 @@ namespace Content.Server.Medical
|
||||
return;
|
||||
|
||||
OpenUserInterface(user, healthAnalyzer);
|
||||
healthAnalyzer.UserInterface?.SendMessage(new HealthAnalyzerScannedUserMessage(target));
|
||||
}
|
||||
|
||||
private static void OnScanCancelled(ScanCancelledEvent args)
|
||||
{
|
||||
args.HealthAnalyzer.CancelToken = null;
|
||||
}
|
||||
|
||||
private sealed class ScanCancelledEvent : EntityEventArgs
|
||||
{
|
||||
public readonly HealthAnalyzerComponent HealthAnalyzer;
|
||||
public ScanCancelledEvent(HealthAnalyzerComponent healthAnalyzer)
|
||||
{
|
||||
HealthAnalyzer = healthAnalyzer;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TargetScanSuccessfulEvent : EntityEventArgs
|
||||
{
|
||||
public EntityUid User { get; }
|
||||
public EntityUid? Target { get; }
|
||||
public HealthAnalyzerComponent Component { get; }
|
||||
|
||||
public TargetScanSuccessfulEvent(EntityUid user, EntityUid? target, HealthAnalyzerComponent component)
|
||||
{
|
||||
User = user;
|
||||
Target = target;
|
||||
Component = component;
|
||||
}
|
||||
_uiSystem.SendUiMessage(healthAnalyzer.UserInterface, new HealthAnalyzerScannedUserMessage(target));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@ namespace Content.Server.Medical.Components
|
||||
{
|
||||
public bool IsActive = false;
|
||||
|
||||
public CancellationTokenSource? CancelToken;
|
||||
|
||||
[DataField("delay")]
|
||||
public float Delay = 2.5f;
|
||||
|
||||
|
||||
@@ -8,10 +8,9 @@ using Content.Shared.Damage;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Inventory.Events;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.Player;
|
||||
using System.Threading;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Content.Shared.Mobs.Systems;
|
||||
using Content.Shared.DoAfter;
|
||||
|
||||
namespace Content.Server.Medical
|
||||
{
|
||||
@@ -29,8 +28,7 @@ namespace Content.Server.Medical
|
||||
SubscribeLocalEvent<WearingStethoscopeComponent, GetVerbsEvent<InnateVerb>>(AddStethoscopeVerb);
|
||||
SubscribeLocalEvent<StethoscopeComponent, GetItemActionsEvent>(OnGetActions);
|
||||
SubscribeLocalEvent<StethoscopeComponent, StethoscopeActionEvent>(OnStethoscopeAction);
|
||||
SubscribeLocalEvent<ListenSuccessfulEvent>(OnListenSuccess);
|
||||
SubscribeLocalEvent<ListenCancelledEvent>(OnListenCancelled);
|
||||
SubscribeLocalEvent<StethoscopeComponent, DoAfterEvent>(OnDoAfter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,7 +79,7 @@ namespace Content.Server.Medical
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
StartListening(uid, args.Target, stetho); // start doafter
|
||||
StartListening(component.Stethoscope, uid, args.Target, stetho); // start doafter
|
||||
},
|
||||
Text = Loc.GetString("stethoscope-verb"),
|
||||
IconTexture = "Clothing/Neck/Misc/stethoscope.rsi/icon.png",
|
||||
@@ -93,7 +91,7 @@ namespace Content.Server.Medical
|
||||
|
||||
private void OnStethoscopeAction(EntityUid uid, StethoscopeComponent component, StethoscopeActionEvent args)
|
||||
{
|
||||
StartListening(args.Performer, args.Target, component);
|
||||
StartListening(uid, args.Performer, args.Target, component);
|
||||
}
|
||||
|
||||
private void OnGetActions(EntityUid uid, StethoscopeComponent component, GetItemActionsEvent args)
|
||||
@@ -101,27 +99,11 @@ namespace Content.Server.Medical
|
||||
args.Actions.Add(component.Action);
|
||||
}
|
||||
|
||||
// doafter succeeded / failed
|
||||
private void OnListenSuccess(ListenSuccessfulEvent ev)
|
||||
{
|
||||
ev.Component.CancelToken = null;
|
||||
ExamineWithStethoscope(ev.User, ev.Target);
|
||||
}
|
||||
|
||||
private void OnListenCancelled(ListenCancelledEvent ev)
|
||||
{
|
||||
if (ev.Component == null)
|
||||
return;
|
||||
ev.Component.CancelToken = null;
|
||||
}
|
||||
// construct the doafter and start it
|
||||
private void StartListening(EntityUid user, EntityUid target, StethoscopeComponent comp)
|
||||
private void StartListening(EntityUid scope, EntityUid user, EntityUid target, StethoscopeComponent comp)
|
||||
{
|
||||
comp.CancelToken = new CancellationTokenSource();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, comp.Delay, comp.CancelToken.Token, target: target)
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, comp.Delay, target: target, used:scope)
|
||||
{
|
||||
BroadcastFinishedEvent = new ListenSuccessfulEvent(user, target, comp),
|
||||
BroadcastCancelledEvent = new ListenCancelledEvent(user, comp),
|
||||
BreakOnTargetMove = true,
|
||||
BreakOnUserMove = true,
|
||||
BreakOnStun = true,
|
||||
@@ -129,6 +111,14 @@ namespace Content.Server.Medical
|
||||
});
|
||||
}
|
||||
|
||||
private void OnDoAfter(EntityUid uid, StethoscopeComponent component, DoAfterEvent args)
|
||||
{
|
||||
if (args.Handled || args.Cancelled || args.Args.Target == null)
|
||||
return;
|
||||
|
||||
ExamineWithStethoscope(args.Args.User, args.Args.Target.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a value based on the total oxyloss of the target.
|
||||
/// Could be expanded in the future with reagent effects etc.
|
||||
@@ -165,34 +155,6 @@ namespace Content.Server.Medical
|
||||
};
|
||||
return msg;
|
||||
}
|
||||
|
||||
// events for the doafter
|
||||
private sealed class ListenSuccessfulEvent : EntityEventArgs
|
||||
{
|
||||
public EntityUid User;
|
||||
public EntityUid Target;
|
||||
public StethoscopeComponent Component;
|
||||
|
||||
public ListenSuccessfulEvent(EntityUid user, EntityUid target, StethoscopeComponent component)
|
||||
{
|
||||
User = user;
|
||||
Target = target;
|
||||
Component = component;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class ListenCancelledEvent : EntityEventArgs
|
||||
{
|
||||
public EntityUid Uid;
|
||||
public StethoscopeComponent Component;
|
||||
|
||||
public ListenCancelledEvent(EntityUid uid, StethoscopeComponent component)
|
||||
{
|
||||
Uid = uid;
|
||||
Component = component;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public sealed class StethoscopeActionEvent : EntityTargetActionEvent {}
|
||||
|
||||
Reference in New Issue
Block a user