Pulling rework v2 (#24936)

* Pulling rework

Fixing up the FOUR systems managing pulling, all the shitcode, and also making it nicer ingame.

* More pulling cleanup

* stats

* More cleanup

* First draft

* More pulling

* weh

* Fix puller

* Pulling working

* Fix merge

* Dunked

* Self-merge time

* Fix hotkey

* Fix container changes

* oop

* Fix multi-pulling

* Move alerts cleanup.

* pulling fixes
This commit is contained in:
metalgearsloth
2024-03-19 14:30:56 +11:00
committed by GitHub
parent f8b54a06b7
commit c584f6444a
53 changed files with 797 additions and 1357 deletions

View File

@@ -1,7 +1,7 @@
using Content.Shared.ActionBlocker;
using Content.Shared.Alert;
using Content.Shared.Pulling.Components;
using Content.Shared.Pulling;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Systems;
using JetBrains.Annotations;
namespace Content.Server.Alert.Click
@@ -20,9 +20,9 @@ namespace Content.Server.Alert.Click
if (!entityManager.System<ActionBlockerSystem>().CanInteract(player, null))
return;
if (entityManager.TryGetComponent(player, out SharedPullableComponent? playerPullable))
if (entityManager.TryGetComponent(player, out PullableComponent? playerPullable))
{
entityManager.System<SharedPullingSystem>().TryStopPull(playerPullable);
entityManager.System<PullingSystem>().TryStopPull(player, playerPullable, user: player);
}
}
}

View File

@@ -1,6 +1,6 @@
using Content.Shared.Alert;
using Content.Shared.Pulling;
using Content.Shared.Pulling.Components;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Systems;
using JetBrains.Annotations;
namespace Content.Server.Alert.Click
@@ -15,12 +15,12 @@ namespace Content.Server.Alert.Click
public void AlertClicked(EntityUid player)
{
var entManager = IoCManager.Resolve<IEntityManager>();
var ps = entManager.System<PullingSystem>();
var ps = entManager.System<SharedPullingSystem>();
var playerTarget = ps.GetPulled(player);
if (playerTarget != default && entManager.TryGetComponent(playerTarget, out SharedPullableComponent? playerPullable))
if (entManager.TryGetComponent(player, out PullerComponent? puller) &&
entManager.TryGetComponent(puller.Pulling, out PullableComponent? pullableComp))
{
ps.TryStopPull(playerPullable);
ps.TryStopPull(puller.Pulling.Value, pullableComp, user: player);
}
}
}

View File

@@ -19,7 +19,6 @@ using Content.Shared.Jittering;
using Content.Shared.Maps;
using Content.Shared.Mobs;
using Content.Shared.Popups;
using Content.Shared.Pulling.Components;
using Content.Shared.Speech.EntitySystems;
using Content.Shared.StatusEffect;
using Content.Shared.Stunnable;
@@ -32,6 +31,8 @@ using Robust.Shared.Physics.Events;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using PullableComponent = Content.Shared.Movement.Pulling.Components.PullableComponent;
using PullerComponent = Content.Shared.Movement.Pulling.Components.PullerComponent;
namespace Content.Server.Electrocution;
@@ -475,14 +476,14 @@ public sealed class ElectrocutionSystem : SharedElectrocutionSystem
all.Add((entity, depth));
visited.Add(entity);
if (TryComp<SharedPullableComponent>(entity, out var pullable) &&
if (TryComp<PullableComponent>(entity, out var pullable) &&
pullable.Puller is { Valid: true } pullerId &&
!visited.Contains(pullerId))
{
GetChainedElectrocutionTargetsRecurse(pullerId, depth + 1, visited, all);
}
if (TryComp<SharedPullerComponent>(entity, out var puller) &&
if (TryComp<PullerComponent>(entity, out var puller) &&
puller.Pulling is { Valid: true } pullingId &&
!visited.Contains(pullingId))
{

View File

@@ -1,6 +1,5 @@
using System.Numerics;
using Content.Server.Inventory;
using Content.Server.Pulling;
using Content.Server.Stack;
using Content.Server.Stunnable;
using Content.Shared.ActionBlocker;
@@ -13,9 +12,9 @@ using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Input;
using Content.Shared.Inventory.VirtualItem;
using Content.Shared.Physics.Pull;
using Content.Shared.Popups;
using Content.Shared.Pulling.Components;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Events;
using Content.Shared.Movement.Pulling.Systems;
using Content.Shared.Stacks;
using Content.Shared.Throwing;
using Robust.Shared.Audio;
@@ -88,9 +87,8 @@ namespace Content.Server.Hands.Systems
return;
// Break any pulls
if (TryComp(uid, out SharedPullerComponent? puller) && puller.Pulling is EntityUid pulled &&
TryComp(pulled, out SharedPullableComponent? pullable))
_pullingSystem.TryStopPull(pullable);
if (TryComp(uid, out PullerComponent? puller) && TryComp(puller.Pulling, out PullableComponent? pullable))
_pullingSystem.TryStopPull(puller.Pulling.Value, pullable);
if (!_handsSystem.TryDrop(uid, component.ActiveHand!, null, checkActionBlocker: false))
return;
@@ -130,13 +128,13 @@ namespace Content.Server.Hands.Systems
private void HandlePullStarted(EntityUid uid, HandsComponent component, PullStartedMessage args)
{
if (args.Puller.Owner != uid)
if (args.PullerUid != uid)
return;
if (TryComp<SharedPullerComponent>(args.Puller.Owner, out var pullerComp) && !pullerComp.NeedsHands)
if (TryComp<PullerComponent>(args.PullerUid, out var pullerComp) && !pullerComp.NeedsHands)
return;
if (!_virtualItemSystem.TrySpawnVirtualItemInHand(args.Pulled.Owner, uid))
if (!_virtualItemSystem.TrySpawnVirtualItemInHand(args.PulledUid, uid))
{
DebugTools.Assert("Unable to find available hand when starting pulling??");
}
@@ -144,7 +142,7 @@ namespace Content.Server.Hands.Systems
private void HandlePullStopped(EntityUid uid, HandsComponent component, PullStoppedMessage args)
{
if (args.Puller.Owner != uid)
if (args.PullerUid != uid)
return;
// Try find hand that is doing this pull.
@@ -153,8 +151,10 @@ namespace Content.Server.Hands.Systems
{
if (hand.HeldEntity == null
|| !TryComp(hand.HeldEntity, out VirtualItemComponent? virtualItem)
|| virtualItem.BlockingEntity != args.Pulled.Owner)
|| virtualItem.BlockingEntity != args.PulledUid)
{
continue;
}
QueueDel(hand.HeldEntity.Value);
break;

View File

@@ -1,4 +1,5 @@
using Content.Shared.Pulling;
using PullingSystem = Content.Shared.Movement.Pulling.Systems.PullingSystem;
namespace Content.Server.NPC.HTN.Preconditions;
@@ -7,14 +8,14 @@ namespace Content.Server.NPC.HTN.Preconditions;
/// </summary>
public sealed partial class PulledPrecondition : HTNPrecondition
{
private SharedPullingSystem _pulling = default!;
private PullingSystem _pulling = default!;
[ViewVariables(VVAccess.ReadWrite)] [DataField("isPulled")] public bool IsPulled = true;
public override void Initialize(IEntitySystemManager sysManager)
{
base.Initialize(sysManager);
_pulling = sysManager.GetEntitySystem<SharedPullingSystem>();
_pulling = sysManager.GetEntitySystem<PullingSystem>();
}
public override bool IsMet(NPCBlackboard blackboard)

View File

@@ -6,11 +6,10 @@ using Content.Shared.Objectives.Systems;
using Robust.Shared.Containers;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Content.Shared.Pulling.Components;
using Content.Shared.Objectives;
using Content.Shared.Mind.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Mobs.Components;
using Content.Shared.Movement.Pulling.Components;
namespace Content.Server.Objectives.Systems;
@@ -100,19 +99,19 @@ public sealed class StealConditionSystem : EntitySystem
var count = 0;
//check pulling object
if (TryComp<SharedPullerComponent>(mind.OwnedEntity, out var pull)) //TO DO: to make the code prettier? don't like the repetition
if (TryComp<PullerComponent>(mind.OwnedEntity, out var pull)) //TO DO: to make the code prettier? don't like the repetition
{
var pullid = pull.Pulling;
if (pullid != null)
var pulledEntity = pull.Pulling;
if (pulledEntity != null)
{
// check if this is the item
if (CheckStealTarget(pullid.Value, condition)) count++;
if (CheckStealTarget(pulledEntity.Value, condition)) count++;
//we don't check the inventories of sentient entity
if (!TryComp<MindContainerComponent>(pullid, out var pullMind))
if (!HasComp<MindContainerComponent>(pulledEntity))
{
// if it is a container check its contents
if (_containerQuery.TryGetComponent(pullid, out var containerManager))
if (_containerQuery.TryGetComponent(pulledEntity, out var containerManager))
stack.Push(containerManager);
}
}

View File

@@ -1,207 +0,0 @@
using System.Numerics;
using Content.Shared.ActionBlocker;
using Content.Shared.Gravity;
using Content.Shared.Pulling;
using Content.Shared.Pulling.Components;
using Content.Shared.Rotatable;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Controllers;
namespace Content.Server.Physics.Controllers
{
public sealed class PullController : VirtualController
{
// Parameterization for pulling:
// Speeds. Note that the speed is mass-independent (multiplied by mass).
// Instead, tuning to mass is done via the mass values below.
// Note that setting the speed too high results in overshoots (stabilized by drag, but bad)
private const float AccelModifierHigh = 15f;
private const float AccelModifierLow = 60.0f;
// High/low-mass marks. Curve is constant-lerp-constant, i.e. if you can even pull an item,
// you'll always get at least AccelModifierLow and no more than AccelModifierHigh.
private const float AccelModifierHighMass = 70.0f; // roundstart saltern emergency closet
private const float AccelModifierLowMass = 5.0f; // roundstart saltern emergency crowbar
// Used to control settling (turns off pulling).
private const float MaximumSettleVelocity = 0.1f;
private const float MaximumSettleDistance = 0.1f;
// Settle shutdown control.
// Mustn't be too massive, as that causes severe mispredicts *and can prevent it ever resolving*.
// Exists to bleed off "I pulled my crowbar" overshoots.
// Minimum velocity for shutdown to be necessary. This prevents stuff getting stuck b/c too much shutdown.
private const float SettleMinimumShutdownVelocity = 0.25f;
// Distance in which settle shutdown multiplier is at 0. It then scales upwards linearly with closer distances.
private const float SettleShutdownDistance = 1.0f;
// Velocity change of -LinearVelocity * frameTime * this
private const float SettleShutdownMultiplier = 20.0f;
// How much you must move for the puller movement check to actually hit.
private const float MinimumMovementDistance = 0.005f;
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
[Dependency] private readonly SharedPullingSystem _pullableSystem = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
// TODO: Move this stuff to pullingsystem
/// <summary>
/// If distance between puller and pulled entity lower that this threshold,
/// pulled entity will not change its rotation.
/// Helps with small distance jittering
/// </summary>
private const float ThresholdRotDistance = 1;
/// <summary>
/// If difference between puller and pulled angle lower that this threshold,
/// pulled entity will not change its rotation.
/// Helps with diagonal movement jittering
/// As of further adjustments, should divide cleanly into 90 degrees
/// </summary>
private const float ThresholdRotAngle = 22.5f;
public override void Initialize()
{
UpdatesAfter.Add(typeof(MoverController));
SubscribeLocalEvent<SharedPullerComponent, MoveEvent>(OnPullerMove);
base.Initialize();
}
private void OnPullerMove(EntityUid uid, SharedPullerComponent component, ref MoveEvent args)
{
if (component.Pulling is not { } pullable || !TryComp<SharedPullableComponent>(pullable, out var pullableComponent))
return;
UpdatePulledRotation(uid, pullable);
if (args.NewPosition.EntityId == args.OldPosition.EntityId &&
(args.NewPosition.Position - args.OldPosition.Position).LengthSquared() < MinimumMovementDistance * MinimumMovementDistance)
return;
if (TryComp<PhysicsComponent>(pullable, out var physics))
PhysicsSystem.WakeBody(pullable, body: physics);
_pullableSystem.StopMoveTo(pullableComponent);
}
private void UpdatePulledRotation(EntityUid puller, EntityUid pulled)
{
// TODO: update once ComponentReference works with directed event bus.
if (!TryComp(pulled, out RotatableComponent? rotatable))
return;
if (!rotatable.RotateWhilePulling)
return;
var xforms = GetEntityQuery<TransformComponent>();
var pulledXform = xforms.GetComponent(pulled);
var pullerXform = xforms.GetComponent(puller);
var pullerData = TransformSystem.GetWorldPositionRotation(pullerXform, xforms);
var pulledData = TransformSystem.GetWorldPositionRotation(pulledXform, xforms);
var dir = pullerData.WorldPosition - pulledData.WorldPosition;
if (dir.LengthSquared() > ThresholdRotDistance * ThresholdRotDistance)
{
var oldAngle = pulledData.WorldRotation;
var newAngle = Angle.FromWorldVec(dir);
var diff = newAngle - oldAngle;
if (Math.Abs(diff.Degrees) > ThresholdRotAngle / 2f)
{
// Ok, so this bit is difficult because ideally it would look like it's snapping to sane angles.
// Otherwise PIANO DOOR STUCK! happens.
// But it also needs to work with station rotation / align to the local parent.
// So...
var baseRotation = pulledData.WorldRotation - pulledXform.LocalRotation;
var localRotation = newAngle - baseRotation;
var localRotationSnapped = Angle.FromDegrees(Math.Floor((localRotation.Degrees / ThresholdRotAngle) + 0.5f) * ThresholdRotAngle);
TransformSystem.SetLocalRotation(pulledXform, localRotationSnapped);
}
}
}
public override void UpdateBeforeSolve(bool prediction, float frameTime)
{
base.UpdateBeforeSolve(prediction, frameTime);
foreach (var pullable in _pullableSystem.Moving)
{
// There's a 1-frame delay between stopping moving something and it leaving the Moving set.
// This can include if leaving the Moving set due to not being pulled anymore,
// or due to being deleted.
if (pullable.Deleted)
continue;
if (pullable.MovingTo == null)
continue;
if (pullable.Puller is not {Valid: true} puller)
continue;
var pullableEnt = pullable.Owner;
var pullableXform = Transform(pullableEnt);
var pullerXform = Transform(puller);
// Now that's over with...
var pullerPosition = pullerXform.MapPosition;
var movingTo = pullable.MovingTo.Value.ToMap(EntityManager, _transform);
if (movingTo.MapId != pullerPosition.MapId)
{
_pullableSystem.StopMoveTo(pullable);
continue;
}
if (!TryComp<PhysicsComponent>(pullableEnt, out var physics) ||
physics.BodyType == BodyType.Static ||
movingTo.MapId != pullableXform.MapID)
{
_pullableSystem.StopMoveTo(pullable);
continue;
}
var movingPosition = movingTo.Position;
var ownerPosition = pullableXform.MapPosition.Position;
var diff = movingPosition - ownerPosition;
var diffLength = diff.Length();
if (diffLength < MaximumSettleDistance && physics.LinearVelocity.Length() < MaximumSettleVelocity)
{
PhysicsSystem.SetLinearVelocity(pullableEnt, Vector2.Zero, body: physics);
_pullableSystem.StopMoveTo(pullable);
continue;
}
var impulseModifierLerp = Math.Min(1.0f, Math.Max(0.0f, (physics.Mass - AccelModifierLowMass) / (AccelModifierHighMass - AccelModifierLowMass)));
var impulseModifier = MathHelper.Lerp(AccelModifierLow, AccelModifierHigh, impulseModifierLerp);
var multiplier = diffLength < 1 ? impulseModifier * diffLength : impulseModifier;
// Note the implication that the real rules of physics don't apply to pulling control.
var accel = diff.Normalized() * multiplier;
// Now for the part where velocity gets shutdown...
if (diffLength < SettleShutdownDistance && physics.LinearVelocity.Length() >= SettleMinimumShutdownVelocity)
{
// Shutdown velocity increases as we get closer to centre
var scaling = (SettleShutdownDistance - diffLength) / SettleShutdownDistance;
accel -= physics.LinearVelocity * SettleShutdownMultiplier * scaling;
}
PhysicsSystem.WakeBody(pullableEnt, body: physics);
var impulse = accel * physics.Mass * frameTime;
PhysicsSystem.ApplyLinearImpulse(pullableEnt, impulse, body: physics);
// if the puller is weightless or can't move, then we apply the inverse impulse (Newton's third law).
// doing it under gravity produces an unsatisfying wiggling when pulling.
// If player can't move, assume they are on a chair and we need to prevent pull-moving.
if ((_gravity.IsWeightless(puller) && pullerXform.GridUid == null) || !_actionBlockerSystem.CanMove(puller))
{
PhysicsSystem.WakeBody(puller);
PhysicsSystem.ApplyLinearImpulse(puller, -impulse);
}
}
}
}
}

View File

@@ -1,48 +0,0 @@
using Content.Shared.Input;
using Content.Shared.Pulling;
using Content.Shared.Pulling.Components;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;
namespace Content.Server.Pulling
{
[UsedImplicitly]
public sealed class PullingSystem : SharedPullingSystem
{
public override void Initialize()
{
base.Initialize();
UpdatesAfter.Add(typeof(PhysicsSystem));
SubscribeLocalEvent<SharedPullableComponent, PullableMoveMessage>(OnPullableMove);
SubscribeLocalEvent<SharedPullableComponent, PullableStopMovingMessage>(OnPullableStopMove);
CommandBinds.Builder
.Bind(ContentKeyFunctions.ReleasePulledObject, InputCmdHandler.FromDelegate(HandleReleasePulledObject))
.Register<PullingSystem>();
}
private void HandleReleasePulledObject(ICommonSession? session)
{
if (session?.AttachedEntity is not {Valid: true} player)
{
return;
}
if (!TryGetPulled(player, out var pulled))
{
return;
}
if (!EntityManager.TryGetComponent(pulled.Value, out SharedPullableComponent? pullable))
{
return;
}
TryStopPull(pullable);
}
}
}

View File

@@ -1,6 +1,6 @@
using Content.Server.Xenoarchaeology.XenoArtifacts.Triggers.Components;
using Content.Shared.Interaction;
using Content.Shared.Physics.Pull;
using Content.Shared.Movement.Pulling.Events;
using Content.Shared.Weapons.Melee.Events;
namespace Content.Server.Xenoarchaeology.XenoArtifacts.Triggers.Systems;
@@ -22,7 +22,7 @@ public sealed class ArtifactInteractionTriggerSystem : EntitySystem
if (!component.PullActivation)
return;
_artifactSystem.TryActivateArtifact(uid, args.Puller.Owner);
_artifactSystem.TryActivateArtifact(uid, args.PullerUid);
}
private void OnAttack(EntityUid uid, ArtifactInteractionTriggerComponent component, AttackedEvent args)

View File

@@ -24,6 +24,7 @@ using Content.Shared.Humanoid;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Systems;
using Content.Shared.NPC.Components;
using Content.Shared.NPC.Systems;
@@ -31,7 +32,6 @@ using Content.Shared.Nutrition.AnimalHusbandry;
using Content.Shared.Nutrition.Components;
using Content.Shared.Popups;
using Content.Shared.Roles;
using Content.Shared.Pulling.Components;
using Content.Shared.Weapons.Melee;
using Content.Shared.Zombies;
using Content.Shared.Prying.Components;
@@ -59,7 +59,6 @@ namespace Content.Server.Zombies
[Dependency] private readonly IChatManager _chatMan = default!;
[Dependency] private readonly MindSystem _mind = default!;
[Dependency] private readonly SharedRoleSystem _roles = default!;
[Dependency] private readonly MobThresholdSystem _mobThreshold = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
/// <summary>
@@ -264,7 +263,9 @@ namespace Content.Server.Zombies
RemComp(target, handsComp);
}
RemComp<SharedPullerComponent>(target);
// Sloth: What the fuck?
// How long until compregistry lmao.
RemComp<PullerComponent>(target);
// No longer waiting to become a zombie:
// Requires deferral because this is (probably) the event which called ZombifyEntity in the first place.