Migrate pulling logic into SharedPullableComponent from PullController (#2627)

* Pulling: Migrate critical pull state from PullController to SharedPullableComponent, fixing two bugs in the process

Bug 1: PullController can be just summoned for no reason when the verb is queried
Bug 2: PullController keeps it's own independent pull state which can (and will) go out of sync (See https://github.com/space-wizards/space-station-14/issues/2619 )

* Pulling: Fix issues with previous commit (or possibly in general?) causing transferring a pull to cause alerts to go wrong

The primary problem is that there is one "pulling slot" for a puller, so SharedPullableComponent needs to stop the existing pull.

* Pulling: Remove debug logs (whoops)
This commit is contained in:
20kdc
2020-11-26 13:48:10 +00:00
committed by GitHub
parent 06b1939a60
commit 380d76f4cd
5 changed files with 163 additions and 146 deletions

View File

@@ -25,46 +25,139 @@ namespace Content.Shared.GameObjects.Components.Pulling
[ComponentDependency] private readonly IPhysicsComponent? _physics = default!;
/// <summary>
/// Only set in Puller->set! Only set in unison with _pullerPhysics!
/// </summary>
private IEntity? _puller;
private IPhysicsComponent? _pullerPhysics;
public IPhysicsComponent? PullerPhysics => _pullerPhysics;
/// <summary>
/// The current entity pulling this component.
/// Setting this performs the entire setup process for pulling.
/// </summary>
public virtual IEntity? Puller
{
get => _puller;
private set
set
{
if (_puller == value)
{
return;
}
_puller = value;
Dirty();
// New value. Abandon being pulled by any existing object.
if (_puller != null)
{
var oldPuller = _puller;
var oldPullerPhysics = _pullerPhysics;
_puller = null;
_pullerPhysics = null;
if (_physics != null)
{
var message = new PullStoppedMessage(oldPullerPhysics, _physics);
oldPuller.SendMessage(null, message);
Owner.SendMessage(null, message);
oldPuller.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
_physics.WakeBody();
_physics.TryRemoveController<PullController>();
}
// else-branch warning is handled below
}
// Now that is settled, prepare to be pulled by a new object.
if (_physics == null)
{
Logger.WarningS("c.go.c.pulling", "Well now you've done it, haven't you? SharedPullableComponent on {0} didn't have an IPhysicsComponent.", Owner);
return;
}
PullController controller;
if (value == null)
if (value != null)
{
if (_physics.TryGetController(out controller))
// Pulling a new object : Perform sanity checks.
if (!_canStartPull(value))
{
controller.StopPull();
return;
}
return;
}
if (!value.TryGetComponent<IPhysicsComponent>(out var valuePhysics))
{
return;
}
controller = _physics.EnsureController<PullController>();
controller.StartPull(value);
if (!value.TryGetComponent<SharedPullerComponent>(out var valuePuller))
{
return;
}
// Ensure that the puller is not currently pulling anything.
// If this isn't done, then it happens too late, and the start/stop messages go out of order,
// and next thing you know it thinks it's not pulling anything even though it is!
var oldPulling = valuePuller.Pulling;
if (oldPulling != null)
{
if (oldPulling.TryGetComponent(out SharedPullableComponent? pullable))
{
pullable.Puller = null;
}
else
{
Logger.WarningS("c.go.c.pulling", "Well now you've done it, haven't you? Someone transferred pulling to this component (on {0}) while presently pulling something that has no Pullable component (on {1})!", Owner, oldPulling);
return;
}
}
// Continue with pulling process.
var pullAttempt = new PullAttemptMessage(valuePhysics, _physics);
value.SendMessage(null, pullAttempt);
if (pullAttempt.Cancelled)
{
return;
}
Owner.SendMessage(null, pullAttempt);
if (pullAttempt.Cancelled)
{
return;
}
// Pull start confirm
_puller = value;
_pullerPhysics = valuePhysics;
_physics.EnsureController<PullController>().Manager = this;
var message = new PullStartedMessage(_pullerPhysics, _physics);
_puller.SendMessage(null, message);
Owner.SendMessage(null, message);
_puller.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
_physics.WakeBody();
}
// Code here will not run if pulling a new object was attempted and failed because of the returns from the refactor.
}
}
public bool BeingPulled => Puller != null;
public bool CanStartPull(IEntity puller)
/// <summary>
/// Sanity-check pull. This is called from Puller setter, so it will never deny a pull that's valid by setting Puller.
/// It might allow an impossible pull (i.e: puller has no PhysicsComponent somehow).
/// Ultimately this is only used separately to stop TryStartPull from cancelling a pull for no reason.
/// </summary>
private bool _canStartPull(IEntity puller)
{
if (!puller.HasComponent<SharedPullerComponent>())
{
@@ -96,7 +189,7 @@ namespace Content.Shared.GameObjects.Components.Pulling
public bool TryStartPull(IEntity puller)
{
if (!CanStartPull(puller))
if (!_canStartPull(puller))
{
return false;
}
@@ -201,30 +294,25 @@ namespace Content.Shared.GameObjects.Components.Pulling
return;
}
SharedAlertsComponent? pulledStatus = Owner.GetComponentOrNull<SharedAlertsComponent>();
switch (message)
{
case PullStartedMessage msg:
AddPullingStatuses(msg.Puller.Owner);
if (pulledStatus != null)
{
pulledStatus.ShowAlert(AlertType.Pulled);
}
break;
case PullStoppedMessage msg:
RemovePullingStatuses(msg.Puller.Owner);
if (pulledStatus != null)
{
pulledStatus.ClearAlert(AlertType.Pulled);
}
break;
}
}
private void AddPullingStatuses(IEntity puller)
{
if (Owner.TryGetComponent(out SharedAlertsComponent? pulledStatus))
{
pulledStatus.ShowAlert(AlertType.Pulled);
}
if (puller.TryGetComponent(out SharedAlertsComponent? ownerStatus))
{
ownerStatus.ShowAlert(AlertType.Pulling, onClickAlert: OnClickAlert);
}
}
private void OnClickAlert(ClickAlertEventArgs args)
{
EntitySystem
@@ -234,19 +322,6 @@ namespace Content.Shared.GameObjects.Components.Pulling
.TryStopPull();
}
private void RemovePullingStatuses(IEntity puller)
{
if (Owner.TryGetComponent(out SharedAlertsComponent? pulledStatus))
{
pulledStatus.ClearAlert(AlertType.Pulled);
}
if (puller.TryGetComponent(out SharedAlertsComponent? ownerStatus))
{
ownerStatus.ClearAlert(AlertType.Pulling);
}
}
public override void OnRemove()
{
TryStopPull();