Try fix collision mispredicts (#16298)

This commit is contained in:
Leon Friedrich
2023-05-13 02:02:50 +12:00
committed by GitHub
parent f1a81d9a8d
commit d228f971b0
11 changed files with 134 additions and 214 deletions

View File

@@ -2,11 +2,10 @@ using Content.Shared.Movement.Components;
using Content.Shared.Movement.Systems;
using Content.Shared.Pulling.Components;
using Robust.Client.GameObjects;
using Robust.Client.Physics;
using Robust.Client.Player;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.Physics.Controllers
{
@@ -22,16 +21,51 @@ namespace Content.Client.Physics.Controllers
SubscribeLocalEvent<RelayInputMoverComponent, PlayerDetachedEvent>(OnRelayPlayerDetached);
SubscribeLocalEvent<InputMoverComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<InputMoverComponent, PlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<InputMoverComponent, UpdateIsPredictedEvent>(OnUpdatePredicted);
SubscribeLocalEvent<MovementRelayTargetComponent, UpdateIsPredictedEvent>(OnUpdateRelayTargetPredicted);
SubscribeLocalEvent<SharedPullableComponent, UpdateIsPredictedEvent>(OnUpdatePullablePredicted);
}
private void OnUpdatePredicted(EntityUid uid, InputMoverComponent component, ref UpdateIsPredictedEvent args)
{
// Enable prediction if an entity is controlled by the player
if (uid == _playerManager.LocalPlayer?.ControlledEntity)
args.IsPredicted = true;
}
private void OnUpdateRelayTargetPredicted(EntityUid uid, MovementRelayTargetComponent component, ref UpdateIsPredictedEvent args)
{
if (component.Source == _playerManager.LocalPlayer?.ControlledEntity)
args.IsPredicted = true;
}
private void OnUpdatePullablePredicted(EntityUid uid, SharedPullableComponent component, ref UpdateIsPredictedEvent args)
{
// Enable prediction if an entity is being pulled by the player.
// Disable prediction if an entity is being pulled by some non-player entity.
if (component.Puller == _playerManager.LocalPlayer?.ControlledEntity)
args.IsPredicted = true;
else if (component.Puller != null)
args.BlockPrediction = true;
// TODO recursive pulling checks?
// What if the entity is being pulled by a vehicle controlled by the player?
}
private void OnRelayPlayerAttached(EntityUid uid, RelayInputMoverComponent component, PlayerAttachedEvent args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(component.RelayEntity);
if (TryComp<InputMoverComponent>(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
}
private void OnRelayPlayerDetached(EntityUid uid, RelayInputMoverComponent component, PlayerDetachedEvent args)
{
Physics.UpdateIsPredicted(uid);
Physics.UpdateIsPredicted(component.RelayEntity);
if (TryComp<InputMoverComponent>(component.RelayEntity, out var inputMover))
SetMoveInput(inputMover, MoveButtons.None);
}
@@ -53,12 +87,8 @@ namespace Content.Client.Physics.Controllers
if (_playerManager.LocalPlayer?.ControlledEntity is not {Valid: true} player)
return;
if (TryComp<RelayInputMoverComponent>(player, out var relayMover)
&& TryComp(relayMover.RelayEntity, out MovementRelayTargetComponent? targetComp))
{
DebugTools.Assert(targetComp.Entities.Count <= 1, "Multiple relayed movers are not supported at the moment");
HandleClientsideMovement(relayMover.RelayEntity.Value, frameTime);
}
if (TryComp<RelayInputMoverComponent>(player, out var relayMover))
HandleClientsideMovement(relayMover.RelayEntity, frameTime);
HandleClientsideMovement(player, frameTime);
}
@@ -70,7 +100,6 @@ namespace Content.Client.Physics.Controllers
var relayTargetQuery = GetEntityQuery<MovementRelayTargetComponent>();
var mobMoverQuery = GetEntityQuery<MobMoverComponent>();
var pullableQuery = GetEntityQuery<SharedPullableComponent>();
var physicsQuery = GetEntityQuery<PhysicsComponent>();
var modifierQuery = GetEntityQuery<MovementSpeedModifierComponent>();
if (!moverQuery.TryGetComponent(player, out var mover) ||
@@ -98,42 +127,6 @@ namespace Content.Client.Physics.Controllers
return;
}
// Essentially we only want to set our mob to predicted so every other entity we just interpolate
// (i.e. only see what the server has sent us).
// The exception to this is joints.
body.Predict = true;
// We set joints to predicted given these can affect how our mob moves.
// I would only recommend disabling this if you make pulling not use joints anymore (someday maybe?)
if (TryComp(player, out JointComponent? jointComponent))
{
foreach (var joint in jointComponent.GetJoints.Values)
{
if (physicsQuery.TryGetComponent(joint.BodyAUid, out var physics))
physics.Predict = true;
if (physicsQuery.TryGetComponent(joint.BodyBUid, out physics))
physics.Predict = true;
}
}
// If we're being pulled then we won't predict anything and will receive server lerps so it looks way smoother.
if (pullableQuery.TryGetComponent(player, out var pullableComp))
{
if (pullableComp.Puller is {Valid: true} puller && TryComp<PhysicsComponent>(puller, out var pullerBody))
{
pullerBody.Predict = false;
body.Predict = false;
if (TryComp<SharedPullerComponent>(player, out var playerPuller) && playerPuller.Pulling != null &&
physicsQuery.TryGetComponent(playerPuller.Pulling, out var pulledBody))
{
pulledBody.Predict = false;
}
}
}
// Server-side should just be handled on its own so we'll just do this shizznit
HandleMobMovement(
player,