Skip to content

Commit

Permalink
Hm
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepyyapril committed Nov 15, 2024
1 parent 3b0d51d commit be8a62e
Showing 1 changed file with 32 additions and 133 deletions.
165 changes: 32 additions & 133 deletions Content.Shared/Movement/Pulling/Systems/PullingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Alert;
using Content.Shared.Buckle.Components;
using Content.Shared.Cuffs.Components;
using Content.Shared.Database;
using Content.Shared.Gravity;
using Content.Shared.Hands;
using Content.Shared.Hands.EntitySystems;
using Content.Shared.Input;
using Content.Shared.Interaction;
using Content.Shared.Movement.Components;
using Content.Shared.Item;
using Content.Shared.Movement.Events;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Pulling.Events;
using Content.Shared.Movement.Systems;
using Content.Shared.Projectiles;
using Content.Shared.Popups;
using Content.Shared.Pulling.Events;
using Content.Shared.Standing;
using Content.Shared.Verbs;
using Robust.Shared.Containers;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems;
using Robust.Shared.Player;
using Robust.Shared.Timing;
using Content.Shared.Throwing;
using System.Numerics;

namespace Content.Shared.Movement.Pulling.Systems;

Expand All @@ -39,18 +35,15 @@ public sealed class PullingSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly MovementSpeedModifierSystem _modifierSystem = default!;
[Dependency] private readonly SharedJointSystem _joints = default!;
[Dependency] private readonly SharedContainerSystem _containerSystem = default!;
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly SharedInteractionSystem _interaction = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedTransformSystem _xformSys = default!;
[Dependency] private readonly ThrownItemSystem _thrownItem = default!;
[Dependency] private readonly HeldSpeedModifierSystem _clothingMoveSpeed = default!;

public override void Initialize()
{
Expand All @@ -67,7 +60,7 @@ public override void Initialize()
SubscribeLocalEvent<PullableComponent, ModifyUncuffDurationEvent>(OnModifyUncuffDuration);
SubscribeLocalEvent<PullableComponent, StopBeingPulledAlertEvent>(OnStopBeingPulledAlert);

SubscribeLocalEvent<PullerComponent, MoveInputEvent>(OnPullerMoveInput);
SubscribeLocalEvent<PullerComponent, AfterAutoHandleStateEvent>(OnAfterState);
SubscribeLocalEvent<PullerComponent, EntGotInsertedIntoContainerMessage>(OnPullerContainerInsert);
SubscribeLocalEvent<PullerComponent, EntityUnpausedEvent>(OnPullerUnpaused);
SubscribeLocalEvent<PullerComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
Expand All @@ -79,10 +72,10 @@ public override void Initialize()
SubscribeLocalEvent<PullableComponent, BuckledEvent>(OnGotBuckled);

CommandBinds.Builder
.Bind(ContentKeyFunctions.MovePulledObject, new PointerInputCmdHandler(OnRequestMovePulledObject))
.Bind(ContentKeyFunctions.ReleasePulledObject, InputCmdHandler.FromDelegate(OnReleasePulledObject, handle: false))
.Register<PullingSystem>();
}

private void OnBuckled(Entity<PullableComponent> ent, ref StrappedEvent args)
{
// Prevent people from pulling the entity they are buckled to
Expand All @@ -95,86 +88,12 @@ private void OnGotBuckled(Entity<PullableComponent> ent, ref BuckledEvent args)
StopPulling(ent, ent);
}

public override void Shutdown()
{
base.Shutdown();
CommandBinds.Unregister<PullingSystem>();
}

public override void Update(float frameTime)
{
if (_net.IsClient) // Client cannot predict this
return;

var query = EntityQueryEnumerator<PullerComponent, PhysicsComponent, TransformComponent>();
while (query.MoveNext(out var puller, out var pullerComp, out var pullerPhysics, out var pullerXForm))
{
// If not pulling, reset the pushing cooldowns and exit
if (pullerComp.Pulling is not { } pulled || !TryComp<PullableComponent>(pulled, out var pulledComp))
{
pullerComp.PushingTowards = null;
pullerComp.NextPushTargetChange = TimeSpan.Zero;
continue;
}

pulledComp.BeingActivelyPushed = false; // Temporarily set to false; if the checks below pass, it will be set to true again

// If pulling but the pullee is invalid or is on a different map, stop pulling
var pulledXForm = Transform(pulled);
if (!TryComp<PhysicsComponent>(pulled, out var pulledPhysics)
|| pulledPhysics.BodyType == BodyType.Static
|| pulledXForm.MapUid != pullerXForm.MapUid)
{
StopPulling(pulled, pulledComp);
continue;
}

if (pullerComp.PushingTowards is null)
continue;

// If pushing but the target position is invalid, or the push action has expired or finished, stop pushing
if (pullerComp.NextPushStop < _timing.CurTime
|| !(pullerComp.PushingTowards.Value.ToMap(EntityManager, _xformSys) is var pushCoordinates)
|| pushCoordinates.MapId != pulledXForm.MapID)
{
pullerComp.PushingTowards = null;
pullerComp.NextPushTargetChange = TimeSpan.Zero;
continue;
}

// Actual force calculation. All the Vector2's below are in map coordinates.
var desiredDeltaPos = pushCoordinates.Position - Transform(pulled).Coordinates.ToMapPos(EntityManager, _xformSys);
if (desiredDeltaPos.LengthSquared() < 0.1f)
{
pullerComp.PushingTowards = null;
continue;
}

var velocityAndDirectionAngle = new Angle(pulledPhysics.LinearVelocity) - new Angle(desiredDeltaPos);
var currentRelativeSpeed = pulledPhysics.LinearVelocity.Length() * (float) Math.Cos(velocityAndDirectionAngle.Theta);
var desiredAcceleration = MathF.Max(0f, pullerComp.MaxPushSpeed - currentRelativeSpeed);

var desiredImpulse = pulledPhysics.Mass * desiredDeltaPos;
var maxSourceImpulse = MathF.Min(pullerComp.PushAcceleration, desiredAcceleration) * pullerPhysics.Mass;
var actualImpulse = desiredImpulse.LengthSquared() > maxSourceImpulse * maxSourceImpulse ? desiredDeltaPos.Normalized() * maxSourceImpulse : desiredImpulse;

// Ideally we'd want to apply forces instead of impulses, however...
// We cannot use ApplyForce here because it will be cleared on the next physics substep which will render it ultimately useless
// The alternative is to run this function on every physics substep, but that is way too expensive for such a minor system
_physics.ApplyLinearImpulse(pulled, actualImpulse);
if (_gravity.IsWeightless(puller, pullerPhysics, pullerXForm))
_physics.ApplyLinearImpulse(puller, -actualImpulse);

pulledComp.BeingActivelyPushed = true;
}
query.Dispose();
}

private void OnPullerMoveInput(EntityUid uid, PullerComponent component, ref MoveInputEvent args)
private void OnAfterState(Entity<PullerComponent> ent, ref AfterAutoHandleStateEvent args)
{
// Stop pushing
component.PushingTowards = null;
component.NextPushStop = TimeSpan.Zero;
if (ent.Comp.Pulling == null)
RemComp<ActivePullerComponent>(ent.Owner);
else
EnsureComp<ActivePullerComponent>(ent.Owner);
}

private void OnDropHandItems(EntityUid uid, PullerComponent pullerComp, DropHandItemsEvent args)
Expand Down Expand Up @@ -213,9 +132,9 @@ private void OnPullableContainerInsert(Entity<PullableComponent> ent, ref EntGot
TryStopPull(ent.Owner, ent.Comp);
}

private void OnPullableCollide(Entity<PullableComponent> ent, ref StartCollideEvent args)
private void OnModifyUncuffDuration(Entity<PullableComponent> ent, ref ModifyUncuffDurationEvent args)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 135 in Content.Shared/Movement/Pulling/Systems/PullingSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'ModifyUncuffDurationEvent' could not be found (are you missing a using directive or an assembly reference?)
{
if (!ent.Comp.BeingActivelyPushed || ent.Comp.Puller == null || args.OtherEntity == ent.Comp.Puller)
if (!ent.Comp.BeingPulled)
return;

// We don't care if the person is being uncuffed by someone else
Expand All @@ -241,7 +160,7 @@ public override void Shutdown()

private void OnPullerUnpaused(EntityUid uid, PullerComponent component, ref EntityUnpausedEvent args)
{
component.NextPushTargetChange += args.PausedTime;
component.NextThrow += args.PausedTime;
}

private void OnVirtualItemDeleted(EntityUid uid, PullerComponent component, VirtualItemDeletedEvent args)
Expand Down Expand Up @@ -293,6 +212,14 @@ private void AddPullVerbs(EntityUid uid, PullableComponent component, GetVerbsEv

private void OnRefreshMovespeed(EntityUid uid, PullerComponent component, RefreshMovementSpeedModifiersEvent args)
{
if (TryComp<HeldSpeedModifierComponent>(component.Pulling, out var heldMoveSpeed) && component.Pulling.HasValue)
{
var (walkMod, sprintMod) =
_clothingMoveSpeed.GetHeldMovementSpeedModifiers(component.Pulling.Value, heldMoveSpeed);
args.ModifySpeed(walkMod, sprintMod);
return;
}

args.ModifySpeed(component.WalkSpeedModifier, component.SprintSpeedModifier);
}

Expand Down Expand Up @@ -361,9 +288,11 @@ private void StopPulling(EntityUid pullableUid, PullableComponent pullableComp)
}

var oldPuller = pullableComp.Puller;
if (oldPuller != null)
RemComp<ActivePullerComponent>(oldPuller.Value);

pullableComp.PullJointId = null;
pullableComp.Puller = null;
pullableComp.BeingActivelyPushed = false;
Dirty(pullableUid, pullableComp);

// No more joints with puller -> force stop pull.
Expand Down Expand Up @@ -392,47 +321,14 @@ public bool IsPulled(EntityUid uid, PullableComponent? component = null)
return Resolve(uid, ref component, false) && component.BeingPulled;
}

private bool OnRequestMovePulledObject(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
{
if (session?.AttachedEntity is not { } player
|| !player.IsValid()
|| !TryComp<PullerComponent>(player, out var pullerComp))
return false;

var pulled = pullerComp.Pulling;
if (!HasComp<PullableComponent>(pulled)
|| _containerSystem.IsEntityInContainer(player)
|| _timing.CurTime < pullerComp.NextPushTargetChange)
return false;

pullerComp.NextPushTargetChange = _timing.CurTime + pullerComp.PushChangeCooldown;
pullerComp.NextPushStop = _timing.CurTime + pullerComp.PushDuration;

// Cap the distance
var range = pullerComp.MaxPushRange;
var fromUserCoords = coords.WithEntityId(player, EntityManager);
var userCoords = new EntityCoordinates(player, Vector2.Zero);

if (!userCoords.InRange(EntityManager, _xformSys, fromUserCoords, range))
{
var userDirection = fromUserCoords.Position - userCoords.Position;
fromUserCoords = userCoords.Offset(userDirection.Normalized() * range);
}

pullerComp.PushingTowards = fromUserCoords;
Dirty(player, pullerComp);

return false;
}

public bool IsPulling(EntityUid puller, PullerComponent? component = null)
{
return Resolve(puller, ref component, false) && component.Pulling != null;
}

private void OnReleasePulledObject(ICommonSession? session)
{
if (session?.AttachedEntity is not {Valid: true} player)
if (session?.AttachedEntity is not { Valid: true } player)
{
return;
}
Expand Down Expand Up @@ -565,9 +461,13 @@ public bool TryStartPull(EntityUid pullerUid, EntityUid pullableUid,
// Use net entity so it's consistent across client and server.
pullableComp.PullJointId = $"pull-joint-{GetNetEntity(pullableUid)}";

EnsureComp<ActivePullerComponent>(pullerUid);
pullerComp.Pulling = pullableUid;
pullableComp.Puller = pullerUid;

// store the pulled entity's physics FixedRotation setting in case we change it
pullableComp.PrevFixedRotation = pullablePhysics.FixedRotation;

// joint state handling will manage its own state
if (!_timing.ApplyingState)
{
Expand All @@ -586,10 +486,9 @@ public bool TryStartPull(EntityUid pullerUid, EntityUid pullableUid,
_physics.SetFixedRotation(pullableUid, pullableComp.FixedRotationOnPull, body: pullablePhysics);
}

pullableComp.PrevFixedRotation = pullablePhysics.FixedRotation;

// Messaging
var message = new PullStartedMessage(pullerUid, pullableUid);
_modifierSystem.RefreshMovementSpeedModifiers(pullerUid);
_alertsSystem.ShowAlert(pullerUid, pullerComp.PullingAlert);
_alertsSystem.ShowAlert(pullableUid, pullableComp.PulledAlert);

Expand Down Expand Up @@ -623,4 +522,4 @@ public bool TryStopPull(EntityUid pullableUid, PullableComponent pullable, Entit
StopPulling(pullableUid, pullable);
return true;
}
}
}

0 comments on commit be8a62e

Please sign in to comment.