More DoAfter Changes (#14609)

* DoAfters

* Compact Clone()

* Fix mice and cuffables

* Try generalize attempt events

* moves climbabledoafter event to shared, fixes issue with climbable target

* Fix merge (cuffing)

* Make all events netserializable

* handful of doafter events moved

* moves the rest of the events to their respective shared folders

* Changes all mentions of server doafter to shared

* stop stripping cancellation

* fix merge errors

* draw paused doafters

* handle unpausing

* missing netserializable ref

* removes break on stun reference

* removes cuffing state reference

* Fix tools

* Fix door prying.

* Fix construction

* Fix dumping

* Fix wielding assert

* fix rev

* Fix test

* more test fixes

---------

Co-authored-by: keronshb <keronshb@live.com>
This commit is contained in:
Leon Friedrich
2023-04-03 13:13:48 +12:00
committed by GitHub
parent 9e66fac805
commit 19277a2276
170 changed files with 3042 additions and 2954 deletions

View File

@@ -1,102 +1,18 @@
using System.Linq;
using System.Threading;
using Content.Shared.Audio;
using Content.Shared.DoAfter;
using Content.Shared.Interaction;
using Content.Shared.Tools.Components;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Shared.Tools;
public abstract class SharedToolSystem : EntitySystem
public abstract partial class SharedToolSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
public override void Initialize()
public void InitializeMultipleTool()
{
SubscribeLocalEvent<MultipleToolComponent, ComponentStartup>(OnMultipleToolStartup);
SubscribeLocalEvent<MultipleToolComponent, ActivateInWorldEvent>(OnMultipleToolActivated);
SubscribeLocalEvent<MultipleToolComponent, ComponentGetState>(OnMultipleToolGetState);
SubscribeLocalEvent<MultipleToolComponent, ComponentHandleState>(OnMultipleToolHandleState);
SubscribeLocalEvent<ToolComponent, DoAfterEvent<ToolEventData>>(OnDoAfter);
SubscribeLocalEvent<ToolDoAfterComplete>(OnDoAfterComplete);
SubscribeLocalEvent<ToolDoAfterCancelled>(OnDoAfterCancelled);
}
private void OnDoAfter(EntityUid uid, ToolComponent component, DoAfterEvent<ToolEventData> args)
{
if (args.Handled || args.AdditionalData.Ev == null)
return;
if (args.Cancelled || !ToolFinishUse(uid, args.Args.User, args.AdditionalData.Fuel))
{
if (args.AdditionalData.CancelledEv != null)
{
if (args.AdditionalData.TargetEntity != null)
RaiseLocalEvent(args.AdditionalData.TargetEntity.Value, args.AdditionalData.CancelledEv);
else
RaiseLocalEvent(args.AdditionalData.CancelledEv);
args.Handled = true;
}
return;
}
if (args.AdditionalData.TargetEntity != null)
RaiseLocalEvent(args.AdditionalData.TargetEntity.Value, args.AdditionalData.Ev);
else
RaiseLocalEvent(args.AdditionalData.Ev);
args.Handled = true;
}
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float doAfterDelay, IEnumerable<string> toolQualitiesNeeded, ToolEventData toolEventData, float fuel = 0f, ToolComponent? toolComponent = null, Func<bool>? doAfterCheck = null, CancellationTokenSource? cancelToken = null)
{
// No logging here, after all that'd mean the caller would need to check if the component is there or not.
if (!Resolve(tool, ref toolComponent, false))
return false;
var ev = new ToolUserAttemptUseEvent(user, target);
RaiseLocalEvent(user, ref ev);
if (ev.Cancelled)
return false;
if (!ToolStartUse(tool, user, fuel, toolQualitiesNeeded, toolComponent))
return false;
if (doAfterDelay > 0f)
{
var doAfterArgs = new DoAfterEventArgs(user, doAfterDelay / toolComponent.SpeedModifier, cancelToken:cancelToken?.Token ?? default, target:target, used:tool)
{
ExtraCheck = doAfterCheck,
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true
};
_doAfterSystem.DoAfter(doAfterArgs, toolEventData);
return true;
}
return ToolFinishUse(tool, user, fuel, toolComponent);
}
public bool UseTool(EntityUid tool, EntityUid user, EntityUid? target, float doAfterDelay, string toolQualityNeeded,
ToolEventData toolEventData, float fuel = 0, ToolComponent? toolComponent = null,
Func<bool>? doAfterCheck = null)
{
return UseTool(tool, user, target, doAfterDelay, new[] { toolQualityNeeded }, toolEventData, fuel,
toolComponent, doAfterCheck);
}
private void OnMultipleToolHandleState(EntityUid uid, MultipleToolComponent component, ref ComponentHandleState args)
@@ -169,125 +85,5 @@ public abstract class SharedToolSystem : EntitySystem
if (_protoMan.TryIndex(current.Behavior.First(), out ToolQualityPrototype? quality))
multiple.CurrentQualityName = Loc.GetString(quality.Name);
}
/// <summary>
/// Whether a tool entity has the specified quality or not.
/// </summary>
public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
}
/// <summary>
/// Whether a tool entity has all specified qualities or not.
/// </summary>
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
}
private bool ToolStartUse(EntityUid tool, EntityUid user, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
return false;
var beforeAttempt = new ToolUseAttemptEvent(fuel, user);
RaiseLocalEvent(tool, beforeAttempt, false);
return !beforeAttempt.Cancelled;
}
private bool ToolFinishUse(EntityUid tool, EntityUid user, float fuel, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
var afterAttempt = new ToolUseFinishAttemptEvent(fuel, user);
RaiseLocalEvent(tool, afterAttempt, false);
if (afterAttempt.Cancelled)
return false;
if (toolComponent.UseSound != null)
PlayToolSound(tool, toolComponent);
return true;
}
public void PlayToolSound(EntityUid uid, ToolComponent? tool = null)
{
if (!Resolve(uid, ref tool))
return;
if (tool.UseSound is not {} sound)
return;
// Pass tool.Owner to Filter.Pvs to avoid a TryGetEntity call.
SoundSystem.Play(sound.GetSound(), Filter.Pvs(tool.Owner),
uid, AudioHelpers.WithVariation(0.175f).WithVolume(-5f));
}
private void OnDoAfterComplete(ToolDoAfterComplete ev)
{
// Actually finish the tool use! Depending on whether that succeeds or not, either event will be broadcast.
if(ToolFinishUse(ev.Uid, ev.UserUid, ev.Fuel))
{
if (ev.EventTarget != null)
RaiseLocalEvent(ev.EventTarget.Value, ev.CompletedEvent, false);
else
RaiseLocalEvent(ev.CompletedEvent);
}
else if(ev.CancelledEvent != null)
{
if (ev.EventTarget != null)
RaiseLocalEvent(ev.EventTarget.Value, ev.CancelledEvent, false);
else
RaiseLocalEvent(ev.CancelledEvent);
}
}
private void OnDoAfterCancelled(ToolDoAfterCancelled ev)
{
if (ev.EventTarget != null)
RaiseLocalEvent(ev.EventTarget.Value, ev.Event, false);
else
RaiseLocalEvent(ev.Event);
}
private sealed class ToolDoAfterComplete : EntityEventArgs
{
public readonly object CompletedEvent;
public readonly object? CancelledEvent;
public readonly EntityUid Uid;
public readonly EntityUid UserUid;
public readonly float Fuel;
public readonly EntityUid? EventTarget;
public ToolDoAfterComplete(object completedEvent, object? cancelledEvent, EntityUid uid, EntityUid userUid, float fuel, EntityUid? eventTarget = null)
{
CompletedEvent = completedEvent;
Uid = uid;
UserUid = userUid;
Fuel = fuel;
CancelledEvent = cancelledEvent;
EventTarget = eventTarget;
}
}
private sealed class ToolDoAfterCancelled : EntityEventArgs
{
public readonly object Event;
public readonly EntityUid? EventTarget;
public ToolDoAfterCancelled(object @event, EntityUid? eventTarget = null)
{
Event = @event;
EventTarget = eventTarget;
}
}
}

View File

@@ -0,0 +1,286 @@
using Content.Shared.DoAfter;
using Content.Shared.Tools.Components;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Shared.Tools;
public abstract partial class SharedToolSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
public override void Initialize()
{
InitializeMultipleTool();
SubscribeLocalEvent<ToolComponent, ToolDoAfterEvent>(OnDoAfter);
}
private void OnDoAfter(EntityUid uid, ToolComponent tool, ToolDoAfterEvent args)
{
PlayToolSound(uid, tool, args.User);
var ev = args.WrappedEvent;
ev.DoAfter = args.DoAfter;
if (args.OriginalTarget != null)
RaiseLocalEvent(args.OriginalTarget.Value, (object) ev);
else
RaiseLocalEvent((object) ev);
}
public void PlayToolSound(EntityUid uid, ToolComponent tool, EntityUid? user)
{
if (tool.UseSound == null)
return;
_audioSystem.PlayPredicted(tool.UseSound, uid, user, tool.UseSound.Params.WithVariation(0.175f).AddVolume(-5f));
}
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
/// </summary>
/// <param name="tool">The tool to use</param>
/// <param name="user">The entity using the tool</param>
/// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
/// event. If null, the event will be broadcast</param>
/// <param name="doAfterDelay">The base tool use delay (seconds). This will be modified by the tool's quality</param>
/// <param name="toolQualitiesNeeded">The qualities needed for this tool to work.</param>
/// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
/// will be directed at the tool target.</param>
/// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
float doAfterDelay,
IEnumerable<string> toolQualitiesNeeded,
DoAfterEvent doAfterEv,
float fuel = 0f,
ToolComponent? toolComponent = null)
{
return UseTool(tool,
user,
target,
TimeSpan.FromSeconds(doAfterDelay),
toolQualitiesNeeded,
doAfterEv,
out _,
fuel,
toolComponent);
}
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
/// </summary>
/// <param name="tool">The tool to use</param>
/// <param name="user">The entity using the tool</param>
/// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
/// event. If null, the event will be broadcast</param>
/// <param name="delay">The base tool use delay. This will be modified by the tool's quality</param>
/// <param name="toolQualitiesNeeded">The qualities needed for this tool to work.</param>
/// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
/// will be directed at the tool target.</param>
/// <param name="id">The id of the DoAfter that was created. This may be null even if the function returns true in
/// the event that this tool-use cancelled an existing DoAfter</param>
/// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
TimeSpan delay,
IEnumerable<string> toolQualitiesNeeded,
DoAfterEvent doAfterEv,
out DoAfterId? id,
float fuel = 0f,
ToolComponent? toolComponent = null)
{
id = null;
if (!Resolve(tool, ref toolComponent, false))
return false;
if (!CanStartToolUse(tool, user, target, fuel, toolQualitiesNeeded, toolComponent))
return false;
var toolEvent = new ToolDoAfterEvent(fuel, doAfterEv, target);
var doAfterArgs = new DoAfterArgs(user, delay / toolComponent.SpeedModifier, toolEvent, tool, target: target, used: tool)
{
BreakOnDamage = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = true,
AttemptFrequency = fuel <= 0 ? AttemptFrequency.Never : AttemptFrequency.EveryTick
};
_doAfterSystem.TryStartDoAfter(doAfterArgs, out id);
return true;
}
/// <summary>
/// Attempts to use a tool on some entity, which will start a DoAfter. Returns true if an interaction occurred.
/// Note that this does not mean the interaction was successful, you need to listen for the DoAfter event.
/// </summary>
/// <param name="tool">The tool to use</param>
/// <param name="user">The entity using the tool</param>
/// <param name="target">The entity that the tool is being used on. This is also the entity that will receive the
/// event. If null, the event will be broadcast</param>
/// <param name="doAfterDelay">The base tool use delay (seconds). This will be modified by the tool's quality</param>
/// <param name="toolQualityNeeded">The quality needed for this tool to work.</param>
/// <param name="doAfterEv">The event that will be raised when the tool has finished (including cancellation). Event
/// will be directed at the tool target.</param>
/// <param name="id">The id of the DoAfter that was created. This may be null even if the function returns true in
/// the event that this tool-use cancelled an existing DoAfter</param>
/// <param name="fuel">Amount of fuel that should be taken from the tool.</param>
/// <param name="toolComponent">The tool component.</param>
/// <returns>Returns true if any interaction takes place.</returns>
public bool UseTool(
EntityUid tool,
EntityUid user,
EntityUid? target,
float doAfterDelay,
string toolQualityNeeded,
DoAfterEvent doAfterEv,
float fuel = 0,
ToolComponent? toolComponent = null)
{
return UseTool(tool,
user,
target,
TimeSpan.FromSeconds(doAfterDelay),
new[] { toolQualityNeeded },
doAfterEv,
out _,
fuel,
toolComponent);
}
/// <summary>
/// Whether a tool entity has the specified quality or not.
/// </summary>
public bool HasQuality(EntityUid uid, string quality, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.Contains(quality);
}
/// <summary>
/// Whether a tool entity has all specified qualities or not.
/// </summary>
public bool HasAllQualities(EntityUid uid, IEnumerable<string> qualities, ToolComponent? tool = null)
{
return Resolve(uid, ref tool, false) && tool.Qualities.ContainsAll(qualities);
}
private bool CanStartToolUse(EntityUid tool, EntityUid user, EntityUid? target, float fuel, IEnumerable<string> toolQualitiesNeeded, ToolComponent? toolComponent = null)
{
if (!Resolve(tool, ref toolComponent))
return false;
var ev = new ToolUserAttemptUseEvent(target);
RaiseLocalEvent(user, ref ev);
if (ev.Cancelled)
return false;
if (!toolComponent.Qualities.ContainsAll(toolQualitiesNeeded))
return false;
var beforeAttempt = new ToolUseAttemptEvent(fuel, user);
RaiseLocalEvent(tool, beforeAttempt, false);
return !beforeAttempt.Cancelled;
}
#region DoAfterEvents
[Serializable, NetSerializable]
protected sealed class ToolDoAfterEvent : DoAfterEvent
{
[DataField("fuel")]
public readonly float Fuel;
/// <summary>
/// Entity that the wrapped do after event will get directed at. If null, event will be broadcast.
/// </summary>
[DataField("target")]
public readonly EntityUid? OriginalTarget;
[DataField("wrappedEvent")]
public readonly DoAfterEvent WrappedEvent = default!;
private ToolDoAfterEvent()
{
}
public ToolDoAfterEvent(float fuel, DoAfterEvent wrappedEvent, EntityUid? originalTarget)
{
DebugTools.Assert(wrappedEvent.GetType().HasCustomAttribute<NetSerializableAttribute>(), "Tool event is not serializable");
Fuel = fuel;
WrappedEvent = wrappedEvent;
OriginalTarget = originalTarget;
}
public override DoAfterEvent Clone()
{
var evClone = WrappedEvent.Clone();
// Most DoAfter events are immutable
if (evClone == WrappedEvent)
return this;
return new ToolDoAfterEvent(Fuel, evClone, OriginalTarget);
}
}
[Serializable, NetSerializable]
protected sealed class LatticeCuttingCompleteEvent : DoAfterEvent
{
[DataField("coordinates", required:true)]
public readonly EntityCoordinates Coordinates;
private LatticeCuttingCompleteEvent()
{
}
public LatticeCuttingCompleteEvent(EntityCoordinates coordinates)
{
Coordinates = coordinates;
}
public override DoAfterEvent Clone() => this;
}
[Serializable, NetSerializable]
protected sealed class TilePryingDoAfterEvent : DoAfterEvent
{
[DataField("coordinates", required:true)]
public readonly EntityCoordinates Coordinates;
private TilePryingDoAfterEvent()
{
}
public TilePryingDoAfterEvent(EntityCoordinates coordinates)
{
Coordinates = coordinates;
}
public override DoAfterEvent Clone() => this;
}
}
[Serializable, NetSerializable]
public sealed class CableCuttingFinishedEvent : SimpleDoAfterEvent
{
}
#endregion

View File

@@ -0,0 +1,13 @@
using Content.Shared.DoAfter;
using Robust.Shared.Serialization;
namespace Content.Shared.Tools.Systems;
/// <summary>
/// Raised after welding do_after has finished. It doesn't guarantee success,
/// use <see cref="WeldableChangedEvent"/> to get updated status.
/// </summary>
[Serializable, NetSerializable]
public sealed class WeldFinishedEvent : SimpleDoAfterEvent
{
}