Re-organize all projects (#4166)
This commit is contained in:
188
Content.Server/DoAfter/DoAfter.cs
Normal file
188
Content.Server/DoAfter/DoAfter.cs
Normal file
@@ -0,0 +1,188 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Hands.Components;
|
||||
using Content.Server.Items;
|
||||
using Content.Server.Stunnable.Components;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.DoAfter
|
||||
{
|
||||
public sealed class DoAfter
|
||||
{
|
||||
public Task<DoAfterStatus> AsTask { get; }
|
||||
|
||||
private TaskCompletionSource<DoAfterStatus> Tcs { get; }
|
||||
|
||||
public DoAfterEventArgs EventArgs;
|
||||
|
||||
public TimeSpan StartTime { get; }
|
||||
|
||||
public float Elapsed { get; set; }
|
||||
|
||||
public EntityCoordinates UserGrid { get; }
|
||||
|
||||
public EntityCoordinates TargetGrid { get; }
|
||||
|
||||
public bool TookDamage { get; set; }
|
||||
|
||||
public DoAfterStatus Status => AsTask.IsCompletedSuccessfully ? AsTask.Result : DoAfterStatus.Running;
|
||||
|
||||
// NeedHand
|
||||
private readonly string? _activeHand;
|
||||
private readonly ItemComponent? _activeItem;
|
||||
|
||||
public DoAfter(DoAfterEventArgs eventArgs)
|
||||
{
|
||||
EventArgs = eventArgs;
|
||||
StartTime = IoCManager.Resolve<IGameTiming>().CurTime;
|
||||
|
||||
if (eventArgs.BreakOnUserMove)
|
||||
{
|
||||
UserGrid = eventArgs.User.Transform.Coordinates;
|
||||
}
|
||||
|
||||
if (eventArgs.BreakOnTargetMove)
|
||||
{
|
||||
// Target should never be null if the bool is set.
|
||||
TargetGrid = eventArgs.Target!.Transform.Coordinates;
|
||||
}
|
||||
|
||||
// For this we need to stay on the same hand slot and need the same item in that hand slot
|
||||
// (or if there is no item there we need to keep it free).
|
||||
if (eventArgs.NeedHand && eventArgs.User.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
_activeHand = handsComponent.ActiveHand;
|
||||
_activeItem = handsComponent.GetActiveHand;
|
||||
}
|
||||
|
||||
Tcs = new TaskCompletionSource<DoAfterStatus>();
|
||||
AsTask = Tcs.Task;
|
||||
}
|
||||
|
||||
public void Run(float frameTime)
|
||||
{
|
||||
switch (Status)
|
||||
{
|
||||
case DoAfterStatus.Running:
|
||||
break;
|
||||
case DoAfterStatus.Cancelled:
|
||||
case DoAfterStatus.Finished:
|
||||
return;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
|
||||
Elapsed += frameTime;
|
||||
|
||||
if (IsFinished())
|
||||
{
|
||||
// Do the final checks here
|
||||
if (!TryPostCheck())
|
||||
{
|
||||
Tcs.SetResult(DoAfterStatus.Cancelled);
|
||||
}
|
||||
else
|
||||
{
|
||||
Tcs.SetResult(DoAfterStatus.Finished);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (IsCancelled())
|
||||
{
|
||||
Tcs.SetResult(DoAfterStatus.Cancelled);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsCancelled()
|
||||
{
|
||||
if (EventArgs.User.Deleted || EventArgs.Target?.Deleted == true)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//https://github.com/tgstation/tgstation/blob/1aa293ea337283a0191140a878eeba319221e5df/code/__HELPERS/mobs.dm
|
||||
if (EventArgs.CancelToken.IsCancellationRequested)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO :Handle inertia in space.
|
||||
if (EventArgs.BreakOnUserMove && !EventArgs.User.Transform.Coordinates.InRange(
|
||||
EventArgs.User.EntityManager, UserGrid, EventArgs.MovementThreshold))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EventArgs.BreakOnTargetMove && !EventArgs.Target!.Transform.Coordinates.InRange(
|
||||
EventArgs.User.EntityManager, TargetGrid, EventArgs.MovementThreshold))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EventArgs.BreakOnDamage && TookDamage)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EventArgs.ExtraCheck != null && !EventArgs.ExtraCheck.Invoke())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EventArgs.BreakOnStun &&
|
||||
EventArgs.User.TryGetComponent(out StunnableComponent? stunnableComponent) &&
|
||||
stunnableComponent.Stunned)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (EventArgs.NeedHand)
|
||||
{
|
||||
if (!EventArgs.User.TryGetComponent(out HandsComponent? handsComponent))
|
||||
{
|
||||
// If we had a hand but no longer have it that's still a paddlin'
|
||||
if (_activeHand != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var currentActiveHand = handsComponent.ActiveHand;
|
||||
if (_activeHand != currentActiveHand)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var currentItem = handsComponent.GetActiveHand;
|
||||
if (_activeItem != currentItem)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryPostCheck()
|
||||
{
|
||||
return EventArgs.PostCheck?.Invoke() != false;
|
||||
}
|
||||
|
||||
private bool IsFinished()
|
||||
{
|
||||
if (Elapsed <= EventArgs.Delay)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
102
Content.Server/DoAfter/DoAfterComponent.cs
Normal file
102
Content.Server/DoAfter/DoAfterComponent.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.DoAfter;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Players;
|
||||
|
||||
namespace Content.Server.DoAfter
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class DoAfterComponent : SharedDoAfterComponent
|
||||
{
|
||||
public IReadOnlyCollection<DoAfter> DoAfters => _doAfters.Keys;
|
||||
private readonly Dictionary<DoAfter, byte> _doAfters = new();
|
||||
|
||||
// So the client knows which one to update (and so we don't send all of the do_afters every time 1 updates)
|
||||
// we'll just send them the index. Doesn't matter if it wraps around.
|
||||
private byte _runningIndex;
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
var toAdd = new List<ClientDoAfter>();
|
||||
|
||||
foreach (var doAfter in DoAfters)
|
||||
{
|
||||
// THE ALMIGHTY PYRAMID
|
||||
var clientDoAfter = new ClientDoAfter(
|
||||
_doAfters[doAfter],
|
||||
doAfter.UserGrid,
|
||||
doAfter.TargetGrid,
|
||||
doAfter.StartTime,
|
||||
doAfter.EventArgs.Delay,
|
||||
doAfter.EventArgs.BreakOnUserMove,
|
||||
doAfter.EventArgs.BreakOnTargetMove,
|
||||
doAfter.EventArgs.MovementThreshold,
|
||||
doAfter.EventArgs.Target?.Uid ?? EntityUid.Invalid);
|
||||
|
||||
toAdd.Add(clientDoAfter);
|
||||
}
|
||||
|
||||
return new DoAfterComponentState(toAdd);
|
||||
}
|
||||
|
||||
public override void HandleMessage(ComponentMessage message, IComponent? component)
|
||||
{
|
||||
base.HandleMessage(message, component);
|
||||
|
||||
switch (message)
|
||||
{
|
||||
case DamageChangedMessage msg:
|
||||
if (DoAfters.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!msg.TookDamage)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var doAfter in _doAfters.Keys)
|
||||
{
|
||||
if (doAfter.EventArgs.BreakOnDamage)
|
||||
{
|
||||
doAfter.TookDamage = true;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(DoAfter doAfter)
|
||||
{
|
||||
_doAfters.Add(doAfter, _runningIndex);
|
||||
_runningIndex++;
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public void Cancelled(DoAfter doAfter)
|
||||
{
|
||||
if (!_doAfters.TryGetValue(doAfter, out var index))
|
||||
return;
|
||||
|
||||
_doAfters.Remove(doAfter);
|
||||
SendNetworkMessage(new CancelledDoAfterMessage(index));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Call when the particular DoAfter is finished.
|
||||
/// Client should be tracking this independently.
|
||||
/// </summary>
|
||||
/// <param name="doAfter"></param>
|
||||
public void Finished(DoAfter doAfter)
|
||||
{
|
||||
if (!_doAfters.ContainsKey(doAfter))
|
||||
return;
|
||||
|
||||
_doAfters.Remove(doAfter);
|
||||
}
|
||||
}
|
||||
}
|
||||
102
Content.Server/DoAfter/DoAfterEventArgs.cs
Normal file
102
Content.Server/DoAfter/DoAfterEventArgs.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Physics;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
// ReSharper disable UnassignedReadonlyField
|
||||
|
||||
namespace Content.Server.DoAfter
|
||||
{
|
||||
public sealed class DoAfterEventArgs
|
||||
{
|
||||
// Premade checks
|
||||
public Func<bool> GetInRangeUnobstructed(CollisionGroup collisionMask = CollisionGroup.MobMask)
|
||||
{
|
||||
if (Target == null)
|
||||
{
|
||||
throw new InvalidOperationException("Can't supply a null target to DoAfterEventArgs.GetInRangeUnobstructed");
|
||||
}
|
||||
|
||||
bool Ignored(IEntity entity) => entity == User || entity == Target;
|
||||
return () => User.InRangeUnobstructed(Target, collisionMask: collisionMask, predicate: Ignored);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The entity invoking do_after
|
||||
/// </summary>
|
||||
public IEntity User { get; }
|
||||
|
||||
/// <summary>
|
||||
/// How long does the do_after require to complete
|
||||
/// </summary>
|
||||
public float Delay { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Applicable target (if relevant)
|
||||
/// </summary>
|
||||
public IEntity? Target { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Manually cancel the do_after so it no longer runs
|
||||
/// </summary>
|
||||
public CancellationToken CancelToken { get; }
|
||||
|
||||
// Break the chains
|
||||
/// <summary>
|
||||
/// Whether we need to keep our active hand as is (i.e. can't change hand or change item).
|
||||
/// This also covers requiring the hand to be free (if applicable).
|
||||
/// </summary>
|
||||
public bool NeedHand { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If do_after stops when the user moves
|
||||
/// </summary>
|
||||
public bool BreakOnUserMove { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If do_after stops when the target moves (if there is a target)
|
||||
/// </summary>
|
||||
public bool BreakOnTargetMove { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Threshold for user and target movement
|
||||
/// </summary>
|
||||
public float MovementThreshold { get; set; }
|
||||
|
||||
public bool BreakOnDamage { get; set; }
|
||||
public bool BreakOnStun { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Requires a function call once at the end (like InRangeUnobstructed).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Anything that needs a pre-check should do it itself so no DoAfterState is ever sent to the client.
|
||||
/// </remarks>
|
||||
public Func<bool>? PostCheck { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// Additional conditions that need to be met. Return false to cancel.
|
||||
/// </summary>
|
||||
public Func<bool>? ExtraCheck { get; set; }
|
||||
|
||||
public DoAfterEventArgs(
|
||||
IEntity user,
|
||||
float delay,
|
||||
CancellationToken cancelToken = default,
|
||||
IEntity? target = null)
|
||||
{
|
||||
User = user;
|
||||
Delay = delay;
|
||||
CancelToken = cancelToken;
|
||||
Target = target;
|
||||
MovementThreshold = 0.1f;
|
||||
|
||||
if (Target == null)
|
||||
{
|
||||
BreakOnTargetMove = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
82
Content.Server/DoAfter/DoAfterSystem.cs
Normal file
82
Content.Server/DoAfter/DoAfterSystem.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.DoAfter
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed class DoAfterSystem : EntitySystem
|
||||
{
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
foreach (var comp in ComponentManager.EntityQuery<DoAfterComponent>(true))
|
||||
{
|
||||
var cancelled = new List<DoAfter>(0);
|
||||
var finished = new List<DoAfter>(0);
|
||||
|
||||
foreach (var doAfter in comp.DoAfters.ToArray())
|
||||
{
|
||||
doAfter.Run(frameTime);
|
||||
|
||||
switch (doAfter.Status)
|
||||
{
|
||||
case DoAfterStatus.Running:
|
||||
break;
|
||||
case DoAfterStatus.Cancelled:
|
||||
cancelled.Add(doAfter);
|
||||
break;
|
||||
case DoAfterStatus.Finished:
|
||||
finished.Add(doAfter);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var doAfter in cancelled)
|
||||
{
|
||||
comp.Cancelled(doAfter);
|
||||
}
|
||||
|
||||
foreach (var doAfter in finished)
|
||||
{
|
||||
comp.Finished(doAfter);
|
||||
}
|
||||
|
||||
finished.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tasks that are delayed until the specified time has passed
|
||||
/// These can be potentially cancelled by the user moving or when other things happen.
|
||||
/// </summary>
|
||||
/// <param name="eventArgs"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<DoAfterStatus> DoAfter(DoAfterEventArgs eventArgs)
|
||||
{
|
||||
// Setup
|
||||
var doAfter = new DoAfter(eventArgs);
|
||||
// Caller's gonna be responsible for this I guess
|
||||
var doAfterComponent = eventArgs.User.GetComponent<DoAfterComponent>();
|
||||
doAfterComponent.Add(doAfter);
|
||||
|
||||
await doAfter.AsTask;
|
||||
|
||||
return doAfter.Status;
|
||||
}
|
||||
}
|
||||
|
||||
public enum DoAfterStatus
|
||||
{
|
||||
Running,
|
||||
Cancelled,
|
||||
Finished,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user