Make anchorable not spammable (#8202)

* weh

* Make anchorable not spammable
This commit is contained in:
metalgearsloth
2022-05-18 12:35:44 +10:00
committed by GitHub
parent 473ec2ec08
commit d09ea18de5
3 changed files with 134 additions and 67 deletions

View File

@@ -1,3 +1,4 @@
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.Administration.Logs; using Content.Server.Administration.Logs;
using Content.Server.Coordinates.Helpers; using Content.Server.Coordinates.Helpers;
@@ -17,13 +18,81 @@ namespace Content.Server.Construction
[Dependency] private readonly ToolSystem _toolSystem = default!; [Dependency] private readonly ToolSystem _toolSystem = default!;
[Dependency] private readonly PullingSystem _pullingSystem = default!; [Dependency] private readonly PullingSystem _pullingSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AnchorableComponent, TryAnchorCompletedEvent>(OnAnchorComplete2);
SubscribeLocalEvent<AnchorableComponent, TryAnchorCancelledEvent>(OnAnchorCancelled2);
SubscribeLocalEvent<AnchorableComponent, TryUnanchorCompletedEvent>(OnUnanchorComplete2);
SubscribeLocalEvent<AnchorableComponent, TryUnanchorCancelledEvent>(OnUnanchorCancelled2);
}
private void OnUnanchorCancelled2(EntityUid uid, AnchorableComponent component, TryUnanchorCancelledEvent args)
{
component.CancelToken = null;
}
private void OnUnanchorComplete2(EntityUid uid, AnchorableComponent component, TryUnanchorCompletedEvent args)
{
component.CancelToken = null;
var xform = Transform(uid);
RaiseLocalEvent(uid, new BeforeUnanchoredEvent(args.User, args.Using), false);
xform.Anchored = false;
RaiseLocalEvent(uid, new UserUnanchoredEvent(args.User, args.Using), false);
_adminLogs.Add(
LogType.Action,
LogImpact.Low,
$"{EntityManager.ToPrettyString(args.User):user} unanchored {EntityManager.ToPrettyString(uid):anchored} using {EntityManager.ToPrettyString(args.Using):using}"
);
}
private void OnAnchorCancelled2(EntityUid uid, AnchorableComponent component, TryAnchorCancelledEvent args)
{
component.CancelToken = null;
}
private void OnAnchorComplete2(EntityUid uid, AnchorableComponent component, TryAnchorCompletedEvent args)
{
component.CancelToken = null;
var xform = Transform(uid);
// Snap rotation to cardinal (multiple of 90)
var rot = xform.LocalRotation;
xform.LocalRotation = Math.Round(rot / (Math.PI / 2)) * (Math.PI / 2);
if (TryComp<SharedPullableComponent>(uid, out var pullable) && pullable.Puller != null)
{
_pullingSystem.TryStopPull(pullable);
}
if (component.Snap)
xform.Coordinates = xform.Coordinates.SnapToGrid();
RaiseLocalEvent(uid, new BeforeAnchoredEvent(args.User, args.Using), false);
xform.Anchored = true;
RaiseLocalEvent(uid, new UserAnchoredEvent(args.User, args.Using), false);
_adminLogs.Add(
LogType.Action,
LogImpact.Low,
$"{EntityManager.ToPrettyString(args.User):user} anchored {EntityManager.ToPrettyString(uid):anchored} using {EntityManager.ToPrettyString(args.Using):using}"
);
}
/// <summary> /// <summary>
/// Checks if a tool can change the anchored status. /// Checks if a tool can change the anchored status.
/// </summary> /// </summary>
/// <returns>true if it is valid, false otherwise</returns> /// <returns>true if it is valid, false otherwise</returns>
private async Task<bool> Valid(EntityUid uid, EntityUid userUid, EntityUid usingUid, bool anchoring, AnchorableComponent? anchorable = null, ToolComponent? usingTool = null) private bool Valid(EntityUid uid, EntityUid userUid, EntityUid usingUid, bool anchoring, AnchorableComponent? anchorable = null, ToolComponent? usingTool = null)
{ {
if (!Resolve(uid, ref anchorable)) if (!Resolve(uid, ref anchorable) ||
anchorable.CancelToken != null)
return false; return false;
if (!Resolve(usingUid, ref usingTool)) if (!Resolve(usingUid, ref usingTool))
@@ -41,111 +110,106 @@ namespace Content.Server.Construction
if (attempt.Cancelled) if (attempt.Cancelled)
return false; return false;
return await _toolSystem.UseTool(usingUid, userUid, uid, 0f, anchorable.Delay + attempt.Delay, anchorable.Tool, toolComponent:usingTool); return true;
} }
/// <summary> /// <summary>
/// Tries to anchor the entity. /// Tries to anchor the entity.
/// </summary> /// </summary>
/// <returns>true if anchored, false otherwise</returns> /// <returns>true if anchored, false otherwise</returns>
public async Task<bool> TryAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid, private void TryAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid,
AnchorableComponent? anchorable = null, AnchorableComponent? anchorable = null,
TransformComponent? transform = null, TransformComponent? transform = null,
SharedPullableComponent? pullable = null, SharedPullableComponent? pullable = null,
ToolComponent? usingTool = null) ToolComponent? usingTool = null)
{ {
if (!Resolve(uid, ref anchorable, ref transform)) if (!Resolve(uid, ref anchorable, ref transform)) return;
return false;
// Optional resolves. // Optional resolves.
Resolve(uid, ref pullable, false); Resolve(uid, ref pullable, false);
if (!Resolve(usingUid, ref usingTool)) if (!Resolve(usingUid, ref usingTool)) return;
return false;
if (!(await Valid(uid, userUid, usingUid, true, anchorable, usingTool))) if (!Valid(uid, userUid, usingUid, true, anchorable, usingTool)) return;
{
return false;
}
// Snap rotation to cardinal (multiple of 90) anchorable.CancelToken = new CancellationTokenSource();
var rot = transform.LocalRotation;
transform.LocalRotation = Math.Round(rot / (Math.PI / 2)) * (Math.PI / 2);
if (pullable is { Puller: {} }) _toolSystem.UseTool(usingUid, userUid, uid, 0f, anchorable.Delay, usingTool.Qualities,
{ new TryAnchorCompletedEvent(), new TryAnchorCancelledEvent(), uid, cancelToken: anchorable.CancelToken.Token);
_pullingSystem.TryStopPull(pullable);
}
if (anchorable.Snap)
transform.Coordinates = transform.Coordinates.SnapToGrid();
RaiseLocalEvent(uid, new BeforeAnchoredEvent(userUid, usingUid), false);
transform.Anchored = true;
RaiseLocalEvent(uid, new UserAnchoredEvent(userUid, usingUid), false);
_adminLogs.Add(
LogType.Action,
LogImpact.Low,
$"{EntityManager.ToPrettyString(userUid):user} anchored {EntityManager.ToPrettyString(uid):anchored} using {EntityManager.ToPrettyString(usingUid):using}"
);
return true;
} }
/// <summary> /// <summary>
/// Tries to unanchor the entity. /// Tries to unanchor the entity.
/// </summary> /// </summary>
/// <returns>true if unanchored, false otherwise</returns> /// <returns>true if unanchored, false otherwise</returns>
public async Task<bool> TryUnAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid, private void TryUnAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid,
AnchorableComponent? anchorable = null, AnchorableComponent? anchorable = null,
TransformComponent? transform = null, TransformComponent? transform = null,
ToolComponent? usingTool = null) ToolComponent? usingTool = null)
{ {
if (!Resolve(uid, ref anchorable, ref transform)) if (!Resolve(uid, ref anchorable, ref transform) ||
return false; anchorable.CancelToken != null)
return;
if (!Resolve(usingUid, ref usingTool)) if (!Resolve(usingUid, ref usingTool)) return;
return false;
if (!(await Valid(uid, userUid, usingUid, false))) if (!Valid(uid, userUid, usingUid, false)) return;
{
return false;
}
RaiseLocalEvent(uid, new BeforeUnanchoredEvent(userUid, usingUid), false); anchorable.CancelToken = new CancellationTokenSource();
transform.Anchored = false; _toolSystem.UseTool(usingUid, userUid, uid, 0f, anchorable.Delay, usingTool.Qualities,
new TryUnanchorCompletedEvent(), new TryUnanchorCancelledEvent(), uid, cancelToken: anchorable.CancelToken.Token);
RaiseLocalEvent(uid, new UserUnanchoredEvent(userUid, usingUid), false);
_adminLogs.Add(
LogType.Action,
LogImpact.Low,
$"{EntityManager.ToPrettyString(userUid):user} unanchored {EntityManager.ToPrettyString(uid):anchored} using {EntityManager.ToPrettyString(usingUid):using}"
);
return true;
} }
/// <summary> /// <summary>
/// Tries to toggle the anchored status of this component's owner. /// Tries to toggle the anchored status of this component's owner.
/// </summary> /// </summary>
/// <returns>true if toggled, false otherwise</returns> /// <returns>true if toggled, false otherwise</returns>
public override async Task<bool> TryToggleAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid, public override void TryToggleAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid,
AnchorableComponent? anchorable = null, AnchorableComponent? anchorable = null,
TransformComponent? transform = null, TransformComponent? transform = null,
SharedPullableComponent? pullable = null, SharedPullableComponent? pullable = null,
ToolComponent? usingTool = null) ToolComponent? usingTool = null)
{ {
if (!Resolve(uid, ref transform)) if (!Resolve(uid, ref transform))
return false; return;
if (transform.Anchored)
{
TryUnAnchor(uid, userUid, usingUid, anchorable, transform, usingTool);
}
else
{
TryAnchor(uid, userUid, usingUid, anchorable, transform, pullable, usingTool);
}
}
private abstract class AnchorEvent : EntityEventArgs
{
public EntityUid User;
public EntityUid Using;
public readonly TransformComponent Transform = default!;
}
private sealed class TryUnanchorCompletedEvent : AnchorEvent
{
}
private sealed class TryUnanchorCancelledEvent : AnchorEvent
{
}
private sealed class TryAnchorCompletedEvent : AnchorEvent
{
}
private sealed class TryAnchorCancelledEvent : AnchorEvent
{
return transform.Anchored ?
await TryUnAnchor(uid, userUid, usingUid, anchorable, transform, usingTool) :
await TryAnchor(uid, userUid, usingUid, anchorable, transform, pullable, usingTool);
} }
} }
} }

View File

@@ -1,3 +1,4 @@
using System.Threading;
using Content.Shared.Construction.EntitySystems; using Content.Shared.Construction.EntitySystems;
using Content.Shared.Tools; using Content.Shared.Tools;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
@@ -19,7 +20,9 @@ namespace Content.Shared.Construction.Components
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("delay")] [DataField("delay")]
public float Delay = 0.5f; public float Delay = 1f;
public CancellationTokenSource? CancelToken = null;
} }
public abstract class BaseAnchoredAttemptEvent : CancellableEntityEventArgs public abstract class BaseAnchoredAttemptEvent : CancellableEntityEventArgs

View File

@@ -15,7 +15,7 @@ public abstract class SharedAnchorableSystem : EntitySystem
SubscribeLocalEvent<AnchorableComponent, InteractUsingEvent>(OnInteractUsing, after: new[] { typeof(SharedConstructionSystem) }); SubscribeLocalEvent<AnchorableComponent, InteractUsingEvent>(OnInteractUsing, after: new[] { typeof(SharedConstructionSystem) });
} }
private async void OnInteractUsing(EntityUid uid, AnchorableComponent anchorable, InteractUsingEvent args) private void OnInteractUsing(EntityUid uid, AnchorableComponent anchorable, InteractUsingEvent args)
{ {
if (args.Handled) if (args.Handled)
return; return;
@@ -25,16 +25,16 @@ public abstract class SharedAnchorableSystem : EntitySystem
return; return;
args.Handled = true; args.Handled = true;
await TryToggleAnchor(uid, args.User, args.Used, anchorable, usingTool: usedTool); TryToggleAnchor(uid, args.User, args.Used, anchorable, usingTool: usedTool);
} }
public virtual async Task<bool> TryToggleAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid, public virtual void TryToggleAnchor(EntityUid uid, EntityUid userUid, EntityUid usingUid,
AnchorableComponent? anchorable = null, AnchorableComponent? anchorable = null,
TransformComponent? transform = null, TransformComponent? transform = null,
SharedPullableComponent? pullable = null, SharedPullableComponent? pullable = null,
ToolComponent? usingTool = null) ToolComponent? usingTool = null)
{ {
// Thanks tool system. // Thanks tool system.
return false; return;
} }
} }