Pullability partial ECS refactor, monkey-gibbing error fix (#4695)
* Migrate lots of pulling logic out of the components and clean it up * It's buggy, but move more code out of the pullable component * Pulling system now throws less errors than it did before the refactor * Finally finish the big parts of refactoring pullability * Pulling: Handle disconnect properly if the pull joint has been removed by physics shutdown * Port2b68449328to this branch * Changes as per PR reviews * Porte801ffac45(SharedJointSystem) and fix issues encountered during testing SharedJointSystem. Also, MinLength and Stiffness appear to have suddenly gained enough meaning that pulling is broken unless they're set.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Content.Shared.Physics.Pull;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
@@ -12,311 +13,32 @@ using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Pulling.Components
|
||||
{
|
||||
// Before you try to add another type than SharedPullingStateManagementSystem, consider the can of worms you may be opening!
|
||||
[NetworkedComponent()]
|
||||
[Friend(typeof(SharedPullingStateManagementSystem))]
|
||||
public abstract class SharedPullableComponent : Component
|
||||
{
|
||||
public override string Name => "Pullable";
|
||||
|
||||
// At this point this field exists solely for the component dependency (which is mandatory).
|
||||
[ComponentDependency] private readonly PhysicsComponent? _physics = default!;
|
||||
|
||||
/// <summary>
|
||||
/// Only set in Puller->set! Only set in unison with _pullerPhysics!
|
||||
/// </summary>
|
||||
private IEntity? _puller;
|
||||
|
||||
public IPhysBody? PullerPhysics { get; private set; }
|
||||
|
||||
private DistanceJoint? _pullJoint;
|
||||
|
||||
public float? MaxDistance => _pullJoint?.MaxLength;
|
||||
|
||||
private MapCoordinates? _movingTo;
|
||||
public float? MaxDistance => PullJoint?.MaxLength;
|
||||
|
||||
/// <summary>
|
||||
/// The current entity pulling this component.
|
||||
/// Setting this performs the entire setup process for pulling.
|
||||
/// Ideally, alter using TryStartPull and TryStopPull.
|
||||
/// </summary>
|
||||
public virtual IEntity? Puller
|
||||
{
|
||||
get => _puller;
|
||||
set
|
||||
{
|
||||
if (_puller == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var eventBus = Owner.EntityManager.EventBus;
|
||||
// TODO: JESUS
|
||||
|
||||
// New value. Abandon being pulled by any existing object.
|
||||
if (_puller != null)
|
||||
{
|
||||
var oldPuller = _puller;
|
||||
var oldPullerPhysics = PullerPhysics;
|
||||
|
||||
if (_puller.TryGetComponent(out SharedPullerComponent? puller))
|
||||
{
|
||||
puller.Pulling = null;
|
||||
}
|
||||
|
||||
_puller = null;
|
||||
Dirty();
|
||||
PullerPhysics = null;
|
||||
|
||||
if (_physics != null && oldPullerPhysics != null)
|
||||
{
|
||||
var message = new PullStoppedMessage(oldPullerPhysics, _physics);
|
||||
|
||||
eventBus.RaiseLocalEvent(oldPuller.Uid, message, broadcast: false);
|
||||
|
||||
if (Owner.LifeStage <= EntityLifeStage.MapInitialized)
|
||||
eventBus.RaiseLocalEvent(Owner.Uid, message);
|
||||
|
||||
_physics.WakeBody();
|
||||
}
|
||||
|
||||
// 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 IPhysBody.", Owner);
|
||||
return;
|
||||
}
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
MovingTo = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Pulling a new object : Perform sanity checks.
|
||||
|
||||
if (!_canStartPull(value))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!value.TryGetComponent<PhysicsComponent>(out var pullerPhysics))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
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.TryStopPull();
|
||||
}
|
||||
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(pullerPhysics, _physics);
|
||||
|
||||
eventBus.RaiseLocalEvent(value.Uid, pullAttempt, broadcast: false);
|
||||
|
||||
if (pullAttempt.Cancelled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
eventBus.RaiseLocalEvent(Owner.Uid, pullAttempt);
|
||||
|
||||
if (pullAttempt.Cancelled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Pull start confirm
|
||||
|
||||
valuePuller.Pulling = Owner;
|
||||
_puller = value;
|
||||
Dirty();
|
||||
PullerPhysics = pullerPhysics;
|
||||
|
||||
var message = new PullStartedMessage(PullerPhysics, _physics);
|
||||
|
||||
eventBus.RaiseLocalEvent(_puller.Uid, message, broadcast: false);
|
||||
eventBus.RaiseLocalEvent(Owner.Uid, message);
|
||||
|
||||
var union = PullerPhysics.GetWorldAABB().Union(_physics.GetWorldAABB());
|
||||
var length = Math.Max(union.Size.X, union.Size.Y) * 0.75f;
|
||||
|
||||
_physics.WakeBody();
|
||||
_pullJoint = EntitySystem.Get<SharedJointSystem>().CreateDistanceJoint(Owner.Uid, _puller.Uid, id:$"pull-joint-{_physics.Owner.Uid}");
|
||||
// _physics.BodyType = BodyType.Kinematic; // TODO: Need to consider their original bodytype
|
||||
_pullJoint.CollideConnected = false;
|
||||
_pullJoint.Length = length * 0.75f;
|
||||
_pullJoint.MinLength = 0f;
|
||||
_pullJoint.MaxLength = length;
|
||||
_pullJoint.Stiffness = 1f;
|
||||
}
|
||||
// Code here will not run if pulling a new object was attempted and failed because of the returns from the refactor.
|
||||
}
|
||||
}
|
||||
public IEntity? Puller { get; set; }
|
||||
/// <summary>
|
||||
/// The pull joint.
|
||||
/// SharedPullingStateManagementSystem should be writing this. This means probably not you.
|
||||
/// </summary>
|
||||
public DistanceJoint? PullJoint { get; set; }
|
||||
|
||||
public bool BeingPulled => Puller != null;
|
||||
|
||||
public MapCoordinates? MovingTo
|
||||
{
|
||||
get => _movingTo;
|
||||
set
|
||||
{
|
||||
if (_movingTo == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_movingTo = value;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new PullableStopMovingMessage());
|
||||
}
|
||||
else
|
||||
{
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, new PullableMoveMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <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>())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EntitySystem.Get<SharedPullingSystem>().CanPull(puller, Owner))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_physics == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_physics.BodyType == BodyType.Static)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (puller == Owner)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!puller.IsInSameOrNoContainer(Owner))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryStartPull(IEntity puller)
|
||||
{
|
||||
if (!_canStartPull(puller))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
TryStopPull();
|
||||
|
||||
Puller = puller;
|
||||
|
||||
if(Puller != puller)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryStopPull(IEntity? user = null)
|
||||
{
|
||||
if (!BeingPulled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var msg = new StopPullingEvent(user?.Uid);
|
||||
Owner.EntityManager.EventBus.RaiseLocalEvent(Owner.Uid, msg);
|
||||
|
||||
if (msg.Cancelled) return false;
|
||||
|
||||
if (_physics != null && _pullJoint != null)
|
||||
{
|
||||
EntitySystem.Get<SharedJointSystem>().RemoveJoint(_pullJoint);
|
||||
}
|
||||
|
||||
if (user != null && user.TryGetComponent<SharedPullerComponent>(out var puller))
|
||||
{
|
||||
puller.Pulling = null;
|
||||
}
|
||||
|
||||
_pullJoint = null;
|
||||
Puller = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TogglePull(IEntity puller)
|
||||
{
|
||||
if (BeingPulled)
|
||||
{
|
||||
if (Puller == puller)
|
||||
{
|
||||
return TryStopPull();
|
||||
}
|
||||
else
|
||||
{
|
||||
TryStopPull();
|
||||
return TryStartPull(puller);
|
||||
}
|
||||
}
|
||||
|
||||
return TryStartPull(puller);
|
||||
}
|
||||
|
||||
public bool TryMoveTo(MapCoordinates to)
|
||||
{
|
||||
if (Puller == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_physics == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
MovingTo = to;
|
||||
return true;
|
||||
}
|
||||
public MapCoordinates? MovingTo { get; set; }
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
@@ -347,11 +69,19 @@ namespace Content.Shared.Pulling.Components
|
||||
Puller = entity;
|
||||
}
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceDisconnectPullable(this);
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
protected override void OnRemove()
|
||||
{
|
||||
TryStopPull();
|
||||
MovingTo = null;
|
||||
|
||||
if (Puller != null)
|
||||
{
|
||||
// This is absolute paranoia but it's also absolutely necessary. Too many puller state bugs. - 20kdc
|
||||
Logger.ErrorS("c.go.c.pulling", "PULLING STATE CORRUPTION IMMINENT IN PULLABLE {0} - OnRemove called when Puller is set!", Owner);
|
||||
}
|
||||
base.OnRemove();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,49 +1,40 @@
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Pulling;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using Robust.Shared.Log;
|
||||
using Component = Robust.Shared.GameObjects.Component;
|
||||
|
||||
namespace Content.Shared.Pulling.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[Friend(typeof(SharedPullingStateManagementSystem))]
|
||||
public class SharedPullerComponent : Component, IMoveSpeedModifier
|
||||
{
|
||||
public override string Name => "Puller";
|
||||
|
||||
private IEntity? _pulling;
|
||||
// Before changing how this is updated, please see SharedPullerSystem.RefreshMovementSpeed
|
||||
public float WalkSpeedModifier => Pulling == null ? 1.0f : 0.75f;
|
||||
|
||||
public float WalkSpeedModifier => _pulling == null ? 1.0f : 0.75f;
|
||||
|
||||
public float SprintSpeedModifier => _pulling == null ? 1.0f : 0.75f;
|
||||
public float SprintSpeedModifier => Pulling == null ? 1.0f : 0.75f;
|
||||
|
||||
[ViewVariables]
|
||||
public IEntity? Pulling
|
||||
public IEntity? Pulling { get; set; }
|
||||
|
||||
protected override void Shutdown()
|
||||
{
|
||||
get => _pulling;
|
||||
set
|
||||
{
|
||||
if (_pulling == value)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_pulling = value;
|
||||
|
||||
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? speed))
|
||||
{
|
||||
speed.RefreshMovementSpeedModifiers();
|
||||
}
|
||||
}
|
||||
EntitySystem.Get<SharedPullingStateManagementSystem>().ForceDisconnectPuller(this);
|
||||
base.Shutdown();
|
||||
}
|
||||
|
||||
protected override void OnRemove()
|
||||
{
|
||||
if (Pulling != null &&
|
||||
Pulling.TryGetComponent(out SharedPullableComponent? pullable))
|
||||
if (Pulling != null)
|
||||
{
|
||||
pullable.TryStopPull();
|
||||
// This is absolute paranoia but it's also absolutely necessary. Too many puller state bugs. - 20kdc
|
||||
Logger.ErrorS("c.go.c.pulling", "PULLING STATE CORRUPTION IMMINENT IN PULLER {0} - OnRemove called when Pulling is set!", Owner);
|
||||
}
|
||||
|
||||
base.OnRemove();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user