2020-10-16 20:35:09 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2021-07-31 03:14:00 +02:00
|
|
|
using Content.Shared.Alert;
|
2021-04-05 14:08:45 +02:00
|
|
|
using Content.Shared.GameTicking;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Content.Shared.Input;
|
|
|
|
|
using Content.Shared.Physics.Pull;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Pulling.Components;
|
2021-12-05 18:09:01 +01:00
|
|
|
using Content.Shared.Verbs;
|
2020-10-16 20:35:09 +02:00
|
|
|
using JetBrains.Annotations;
|
2021-03-13 14:24:06 +11:00
|
|
|
using Robust.Shared.Containers;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.Input.Binding;
|
|
|
|
|
using Robust.Shared.Map;
|
2022-07-15 13:37:58 +12:00
|
|
|
using Robust.Shared.Physics;
|
2023-05-27 14:15:15 +10:00
|
|
|
using Robust.Shared.Physics.Events;
|
|
|
|
|
using Robust.Shared.Physics.Systems;
|
2020-10-16 20:35:09 +02:00
|
|
|
using Robust.Shared.Players;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Shared.Pulling
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2021-10-04 16:10:54 +01:00
|
|
|
public abstract partial class SharedPullingSystem : EntitySystem
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
2021-10-04 16:10:54 +01:00
|
|
|
[Dependency] private readonly SharedPullingStateManagementSystem _pullSm = default!;
|
2022-01-05 00:19:23 -08:00
|
|
|
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
|
2023-05-27 14:15:15 +10:00
|
|
|
[Dependency] private readonly SharedJointSystem _joints = default!;
|
2021-10-04 16:10:54 +01:00
|
|
|
|
2020-10-26 12:11:46 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// A mapping of pullers to the entity that they are pulling.
|
|
|
|
|
/// </summary>
|
2021-12-04 12:59:44 +01:00
|
|
|
private readonly Dictionary<EntityUid, EntityUid> _pullers =
|
2020-11-27 11:00:49 +01:00
|
|
|
new();
|
2020-10-16 20:35:09 +02:00
|
|
|
|
2022-03-18 15:40:02 +11:00
|
|
|
private readonly HashSet<SharedPullableComponent> _moving = new();
|
|
|
|
|
private readonly HashSet<SharedPullableComponent> _stoppedMoving = new();
|
|
|
|
|
|
|
|
|
|
public IReadOnlySet<SharedPullableComponent> Moving => _moving;
|
|
|
|
|
|
2020-10-16 20:35:09 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
2021-12-30 03:12:04 +01:00
|
|
|
UpdatesOutsidePrediction = true;
|
|
|
|
|
|
2021-06-29 15:56:07 +02:00
|
|
|
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
|
2020-10-16 20:35:09 +02:00
|
|
|
SubscribeLocalEvent<PullStartedMessage>(OnPullStarted);
|
|
|
|
|
SubscribeLocalEvent<PullStoppedMessage>(OnPullStopped);
|
2021-03-13 14:24:06 +11:00
|
|
|
SubscribeLocalEvent<EntInsertedIntoContainerMessage>(HandleContainerInsert);
|
2022-07-15 13:37:58 +12:00
|
|
|
SubscribeLocalEvent<SharedPullableComponent, JointRemovedEvent>(OnJointRemoved);
|
2023-05-27 14:15:15 +10:00
|
|
|
SubscribeLocalEvent<SharedPullableComponent, CollisionChangeEvent>(OnPullableCollisionChange);
|
2020-10-16 20:35:09 +02:00
|
|
|
|
2021-07-31 03:14:00 +02:00
|
|
|
SubscribeLocalEvent<SharedPullableComponent, PullStartedMessage>(PullableHandlePullStarted);
|
|
|
|
|
SubscribeLocalEvent<SharedPullableComponent, PullStoppedMessage>(PullableHandlePullStopped);
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
SubscribeLocalEvent<SharedPullableComponent, GetVerbsEvent<Verb>>(AddPullVerbs);
|
2021-10-05 14:29:03 +11:00
|
|
|
|
2020-10-16 20:35:09 +02:00
|
|
|
CommandBinds.Builder
|
|
|
|
|
.Bind(ContentKeyFunctions.MovePulledObject, new PointerInputCmdHandler(HandleMovePulledObject))
|
|
|
|
|
.Register<SharedPullingSystem>();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-27 14:15:15 +10:00
|
|
|
private void OnPullableCollisionChange(EntityUid uid, SharedPullableComponent component, ref CollisionChangeEvent args)
|
|
|
|
|
{
|
|
|
|
|
if (component.PullJointId != null && !args.CanCollide)
|
|
|
|
|
{
|
|
|
|
|
_joints.RemoveJoint(uid, component.PullJointId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 13:37:58 +12:00
|
|
|
private void OnJointRemoved(EntityUid uid, SharedPullableComponent component, JointRemovedEvent args)
|
|
|
|
|
{
|
2023-06-03 00:20:09 +12:00
|
|
|
if (component.Puller != args.OtherEntity)
|
2022-07-15 13:37:58 +12:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Do we have some other join with our Puller?
|
|
|
|
|
// or alternatively:
|
|
|
|
|
// TODO track the relevant joint.
|
|
|
|
|
|
|
|
|
|
if (TryComp(uid, out JointComponent? joints))
|
|
|
|
|
{
|
2022-07-29 14:13:43 +12:00
|
|
|
foreach (var jt in joints.GetJoints.Values)
|
2022-07-15 13:37:58 +12:00
|
|
|
{
|
|
|
|
|
if (jt.BodyAUid == component.Puller || jt.BodyBUid == component.Puller)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No more joints with puller -> force stop pull.
|
|
|
|
|
_pullSm.ForceDisconnectPullable(component);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 15:30:59 +13:00
|
|
|
private void AddPullVerbs(EntityUid uid, SharedPullableComponent component, GetVerbsEvent<Verb> args)
|
2021-10-05 14:29:03 +11:00
|
|
|
{
|
2022-06-21 07:00:11 -07:00
|
|
|
if (!args.CanAccess || !args.CanInteract)
|
2021-10-05 14:29:03 +11:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Are they trying to pull themselves up by their bootstraps?
|
|
|
|
|
if (args.User == args.Target)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
//TODO VERB ICONS add pulling icon
|
|
|
|
|
if (component.Puller == args.User)
|
|
|
|
|
{
|
2023-06-28 16:03:16 -04:00
|
|
|
Verb verb = new()
|
|
|
|
|
{
|
|
|
|
|
Text = Loc.GetString("pulling-verb-get-data-text-stop-pulling"),
|
|
|
|
|
Act = () => TryStopPull(component, args.User),
|
|
|
|
|
DoContactInteraction = false // pulling handle its own contact interaction.
|
|
|
|
|
};
|
2021-10-05 14:29:03 +11:00
|
|
|
args.Verbs.Add(verb);
|
|
|
|
|
}
|
|
|
|
|
else if (CanPull(args.User, args.Target))
|
|
|
|
|
{
|
2023-06-28 16:03:16 -04:00
|
|
|
Verb verb = new()
|
|
|
|
|
{
|
|
|
|
|
Text = Loc.GetString("pulling-verb-get-data-text"),
|
|
|
|
|
Act = () => TryStartPull(args.User, args.Target),
|
|
|
|
|
DoContactInteraction = false // pulling handle its own contact interaction.
|
|
|
|
|
};
|
2021-10-05 14:29:03 +11:00
|
|
|
args.Verbs.Add(verb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-31 03:14:00 +02:00
|
|
|
// Raise a "you are being pulled" alert if the pulled entity has alerts.
|
2021-12-04 12:59:44 +01:00
|
|
|
private void PullableHandlePullStarted(EntityUid uid, SharedPullableComponent component, PullStartedMessage args)
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2021-12-03 16:30:34 +01:00
|
|
|
if (args.Pulled.Owner != uid)
|
2021-07-31 03:14:00 +02:00
|
|
|
return;
|
2022-03-19 14:18:43 +11:00
|
|
|
|
2023-06-28 16:03:16 -04:00
|
|
|
_alertsSystem.ShowAlert(uid, AlertType.Pulled);
|
2021-07-31 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
2021-12-04 12:59:44 +01:00
|
|
|
private void PullableHandlePullStopped(EntityUid uid, SharedPullableComponent component, PullStoppedMessage args)
|
2021-07-31 03:14:00 +02:00
|
|
|
{
|
2021-12-03 16:30:34 +01:00
|
|
|
if (args.Pulled.Owner != uid)
|
2021-07-31 03:14:00 +02:00
|
|
|
return;
|
|
|
|
|
|
2023-06-28 16:03:16 -04:00
|
|
|
_alertsSystem.ClearAlert(uid, AlertType.Pulled);
|
2021-07-31 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-06 00:28:23 +10:00
|
|
|
public bool IsPulled(EntityUid uid, SharedPullableComponent? component = null)
|
|
|
|
|
{
|
|
|
|
|
return Resolve(uid, ref component, false) && component.BeingPulled;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 15:40:02 +11:00
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
base.Update(frameTime);
|
|
|
|
|
|
|
|
|
|
_moving.ExceptWith(_stoppedMoving);
|
|
|
|
|
_stoppedMoving.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 15:56:07 +02:00
|
|
|
public void Reset(RoundRestartCleanupEvent ev)
|
2021-04-05 14:08:45 +02:00
|
|
|
{
|
|
|
|
|
_pullers.Clear();
|
2022-03-18 15:40:02 +11:00
|
|
|
_moving.Clear();
|
|
|
|
|
_stoppedMoving.Clear();
|
2021-04-05 14:08:45 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-16 20:35:09 +02:00
|
|
|
private void OnPullStarted(PullStartedMessage message)
|
|
|
|
|
{
|
|
|
|
|
SetPuller(message.Puller.Owner, message.Pulled.Owner);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPullStopped(PullStoppedMessage message)
|
|
|
|
|
{
|
|
|
|
|
RemovePuller(message.Puller.Owner);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 15:40:02 +11:00
|
|
|
protected void OnPullableMove(EntityUid uid, SharedPullableComponent component, PullableMoveMessage args)
|
|
|
|
|
{
|
|
|
|
|
_moving.Add(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnPullableStopMove(EntityUid uid, SharedPullableComponent component, PullableStopMovingMessage args)
|
|
|
|
|
{
|
|
|
|
|
_stoppedMoving.Add(component);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 14:24:06 +11:00
|
|
|
// TODO: When Joint networking is less shitcodey fix this to use a dedicated joints message.
|
|
|
|
|
private void HandleContainerInsert(EntInsertedIntoContainerMessage message)
|
|
|
|
|
{
|
2023-06-28 16:03:16 -04:00
|
|
|
if (TryComp(message.Entity, out SharedPullableComponent? pullable))
|
2021-03-13 14:24:06 +11:00
|
|
|
{
|
2021-10-04 16:10:54 +01:00
|
|
|
TryStopPull(pullable);
|
2021-03-13 14:24:06 +11:00
|
|
|
}
|
|
|
|
|
|
2023-06-28 16:03:16 -04:00
|
|
|
if (TryComp(message.Entity, out SharedPullerComponent? puller))
|
2021-03-13 14:24:06 +11:00
|
|
|
{
|
|
|
|
|
if (puller.Pulling == null) return;
|
|
|
|
|
|
2023-06-28 16:03:16 -04:00
|
|
|
if (!TryComp(puller.Pulling.Value, out SharedPullableComponent? pulling))
|
2021-03-13 14:24:06 +11:00
|
|
|
return;
|
|
|
|
|
|
2021-10-04 16:10:54 +01:00
|
|
|
TryStopPull(pulling);
|
2021-03-13 14:24:06 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 20:35:09 +02:00
|
|
|
private bool HandleMovePulledObject(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
|
|
|
|
|
{
|
2021-12-05 18:09:01 +01:00
|
|
|
if (session?.AttachedEntity is not { } player ||
|
|
|
|
|
!player.IsValid())
|
2020-10-16 20:35:09 +02:00
|
|
|
return false;
|
2021-12-04 12:59:44 +01:00
|
|
|
|
2020-10-16 20:35:09 +02:00
|
|
|
if (!TryGetPulled(player, out var pulled))
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-06-28 16:03:16 -04:00
|
|
|
if (!TryComp(pulled.Value, out SharedPullableComponent? pullable))
|
2020-10-16 20:35:09 +02:00
|
|
|
return false;
|
|
|
|
|
|
2023-06-28 16:03:16 -04:00
|
|
|
if (_containerSystem.IsEntityInContainer(player))
|
2022-03-19 14:18:43 +11:00
|
|
|
return false;
|
|
|
|
|
|
2021-11-07 02:16:49 +00:00
|
|
|
TryMoveTo(pullable, coords);
|
2020-10-16 20:35:09 +02:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 12:59:44 +01:00
|
|
|
private void SetPuller(EntityUid puller, EntityUid pulled)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
_pullers[puller] = pulled;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 12:59:44 +01:00
|
|
|
private bool RemovePuller(EntityUid puller)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
return _pullers.Remove(puller);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-05 18:09:01 +01:00
|
|
|
public EntityUid GetPulled(EntityUid by)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
return _pullers.GetValueOrDefault(by);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 12:59:44 +01:00
|
|
|
public bool TryGetPulled(EntityUid by, [NotNullWhen(true)] out EntityUid? pulled)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
return (pulled = GetPulled(by)) != null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-04 12:59:44 +01:00
|
|
|
public bool IsPulling(EntityUid puller)
|
2020-10-16 20:35:09 +02:00
|
|
|
{
|
|
|
|
|
return _pullers.ContainsKey(puller);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|