2021-08-15 14:03:08 +10:00
|
|
|
using System.Collections.Generic;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.ActionBlocker;
|
2021-07-26 01:48:22 +10:00
|
|
|
using Content.Shared.CCVar;
|
2021-08-15 14:03:08 +10:00
|
|
|
using Content.Shared.Friction;
|
2021-11-08 15:11:58 +01:00
|
|
|
using Content.Shared.MobState.Components;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Movement.Components;
|
|
|
|
|
using Content.Shared.Pulling.Components;
|
2021-12-27 18:50:00 +01:00
|
|
|
using JetBrains.Annotations;
|
2021-07-26 01:48:22 +10:00
|
|
|
using Robust.Shared.Configuration;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
2021-06-09 09:26:46 +10:00
|
|
|
using Robust.Shared.Map;
|
2021-03-08 04:09:59 +11:00
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
using Robust.Shared.Physics;
|
|
|
|
|
using Robust.Shared.Physics.Controllers;
|
2021-07-26 01:48:22 +10:00
|
|
|
using Robust.Shared.Utility;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-06-19 10:03:24 +02:00
|
|
|
namespace Content.Shared.Movement
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles player and NPC mob movement.
|
|
|
|
|
/// NPCs are handled server-side only.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class SharedMoverController : VirtualController
|
|
|
|
|
{
|
2021-06-09 09:26:46 +10:00
|
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
[Dependency] private ActionBlockerSystem _blocker = default!;
|
|
|
|
|
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-07-26 01:48:22 +10:00
|
|
|
private bool _relativeMovement;
|
|
|
|
|
|
2021-08-15 14:03:08 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Cache the mob movement calculation to re-use elsewhere.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Dictionary<EntityUid, bool> UsedMobMovement = new();
|
|
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2021-08-15 14:03:08 +10:00
|
|
|
var configManager = IoCManager.Resolve<IConfigurationManager>();
|
|
|
|
|
configManager.OnValueChanged(CCVars.RelativeMovement, SetRelativeMovement, true);
|
|
|
|
|
UpdatesBefore.Add(typeof(SharedTileFrictionController));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetRelativeMovement(bool value) => _relativeMovement = value;
|
|
|
|
|
|
|
|
|
|
public override void Shutdown()
|
|
|
|
|
{
|
|
|
|
|
base.Shutdown();
|
2021-07-26 01:48:22 +10:00
|
|
|
var configManager = IoCManager.Resolve<IConfigurationManager>();
|
2021-08-15 14:03:08 +10:00
|
|
|
configManager.UnsubValueChanged(CCVars.RelativeMovement, SetRelativeMovement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void UpdateAfterSolve(bool prediction, float frameTime)
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterSolve(prediction, frameTime);
|
|
|
|
|
UsedMobMovement.Clear();
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
|
2021-12-27 18:50:00 +01:00
|
|
|
protected Angle GetParentGridAngle(TransformComponent xform, IMoverComponent mover)
|
|
|
|
|
{
|
|
|
|
|
if (xform.GridID == GridId.Invalid || !_mapManager.TryGetGrid(xform.GridID, out var grid))
|
|
|
|
|
return mover.LastGridAngle;
|
|
|
|
|
|
|
|
|
|
return grid.WorldRotation;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// A generic kinematic mover for entities.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected void HandleKinematicMovement(IMoverComponent mover, PhysicsComponent physicsComponent)
|
|
|
|
|
{
|
|
|
|
|
var (walkDir, sprintDir) = mover.VelocityDir;
|
|
|
|
|
|
2021-12-08 17:32:32 +01:00
|
|
|
var transform = EntityManager.GetComponent<TransformComponent>(mover.Owner);
|
2021-12-27 18:50:00 +01:00
|
|
|
var parentRotation = GetParentGridAngle(transform, mover);
|
2021-11-02 20:35:02 +07:00
|
|
|
|
2021-03-08 04:09:59 +11:00
|
|
|
// Regular movement.
|
|
|
|
|
// Target velocity.
|
2021-11-02 20:35:02 +07:00
|
|
|
var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed;
|
|
|
|
|
|
2021-11-13 14:40:27 +11:00
|
|
|
var worldTotal = _relativeMovement ? parentRotation.RotateVec(total) : total;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2021-12-27 21:10:10 +01:00
|
|
|
if (transform.GridID != GridId.Invalid)
|
2021-11-13 14:40:27 +11:00
|
|
|
mover.LastGridAngle = parentRotation;
|
2021-07-26 01:48:22 +10:00
|
|
|
|
|
|
|
|
if (worldTotal != Vector2.Zero)
|
2021-11-02 20:35:02 +07:00
|
|
|
transform.WorldRotation = worldTotal.GetDir().ToAngle();
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
_physics.SetLinearVelocity(physicsComponent, worldTotal);
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Movement while considering actionblockers, weightlessness, etc.
|
|
|
|
|
/// </summary>
|
2022-02-02 17:34:25 +11:00
|
|
|
protected void HandleMobMovement(
|
|
|
|
|
IMoverComponent mover,
|
|
|
|
|
PhysicsComponent physicsComponent,
|
|
|
|
|
IMobMoverComponent mobMover,
|
|
|
|
|
TransformComponent xform)
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-12-03 16:30:34 +01:00
|
|
|
DebugTools.Assert(!UsedMobMovement.ContainsKey(mover.Owner));
|
2021-08-15 14:03:08 +10:00
|
|
|
|
|
|
|
|
if (!UseMobMovement(physicsComponent))
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-12-03 16:30:34 +01:00
|
|
|
UsedMobMovement[mover.Owner] = false;
|
2021-03-08 04:09:59 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 16:30:34 +01:00
|
|
|
UsedMobMovement[mover.Owner] = true;
|
2022-02-02 17:34:25 +11:00
|
|
|
var weightless = mover.Owner.IsWeightless(physicsComponent, mapManager: _mapManager, entityManager: EntityManager);
|
2021-03-08 04:09:59 +11:00
|
|
|
var (walkDir, sprintDir) = mover.VelocityDir;
|
|
|
|
|
|
|
|
|
|
// Handle wall-pushes.
|
|
|
|
|
if (weightless)
|
|
|
|
|
{
|
|
|
|
|
// No gravity: is our entity touching anything?
|
2022-02-02 17:34:25 +11:00
|
|
|
var touching = IsAroundCollider(_physics, xform, mobMover, physicsComponent);
|
2021-03-08 04:09:59 +11:00
|
|
|
|
|
|
|
|
if (!touching)
|
|
|
|
|
{
|
2022-02-02 17:34:25 +11:00
|
|
|
if (xform.GridID != GridId.Invalid)
|
|
|
|
|
mover.LastGridAngle = GetParentGridAngle(xform, mover);
|
2021-11-13 14:14:19 +11:00
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
xform.WorldRotation = physicsComponent.LinearVelocity.GetDir().ToAngle();
|
2021-03-08 04:09:59 +11:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Regular movement.
|
|
|
|
|
// Target velocity.
|
2021-07-26 01:48:22 +10:00
|
|
|
// This is relative to the map / grid we're on.
|
2021-11-02 20:35:02 +07:00
|
|
|
var total = walkDir * mover.CurrentWalkSpeed + sprintDir * mover.CurrentSprintSpeed;
|
2021-03-08 04:09:59 +11:00
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
var parentRotation = GetParentGridAngle(xform, mover);
|
2021-11-13 14:14:19 +11:00
|
|
|
|
|
|
|
|
var worldTotal = _relativeMovement ? parentRotation.RotateVec(total) : total;
|
2021-07-26 01:48:22 +10:00
|
|
|
|
2021-09-29 20:07:01 +10:00
|
|
|
DebugTools.Assert(MathHelper.CloseToPercent(total.Length, worldTotal.Length));
|
2021-07-26 01:48:22 +10:00
|
|
|
|
2021-03-08 12:10:56 +11:00
|
|
|
if (weightless)
|
2021-07-26 01:48:22 +10:00
|
|
|
worldTotal *= mobMover.WeightlessStrength;
|
2021-11-02 20:35:02 +07:00
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
if (xform.GridID != GridId.Invalid)
|
2021-11-13 14:14:19 +11:00
|
|
|
mover.LastGridAngle = parentRotation;
|
2021-03-08 12:10:56 +11:00
|
|
|
|
2021-07-26 01:48:22 +10:00
|
|
|
if (worldTotal != Vector2.Zero)
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
|
|
|
|
// This should have its event run during island solver soooo
|
2022-02-02 17:34:25 +11:00
|
|
|
xform.DeferUpdates = true;
|
2022-03-02 15:18:04 +13:00
|
|
|
xform.LocalRotation = total.GetDir().ToAngle();
|
2022-02-02 17:34:25 +11:00
|
|
|
xform.DeferUpdates = false;
|
2021-03-08 04:09:59 +11:00
|
|
|
HandleFootsteps(mover, mobMover);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-02 17:34:25 +11:00
|
|
|
_physics.SetLinearVelocity(physicsComponent, worldTotal);
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
|
2021-08-15 14:03:08 +10:00
|
|
|
public bool UseMobMovement(EntityUid uid)
|
|
|
|
|
{
|
|
|
|
|
return UsedMobMovement.TryGetValue(uid, out var used) && used;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool UseMobMovement(PhysicsComponent body)
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
2021-08-15 14:03:08 +10:00
|
|
|
return body.BodyStatus == BodyStatus.OnGround &&
|
2021-12-08 17:32:32 +01:00
|
|
|
EntityManager.HasComponent<MobStateComponent>(body.Owner) &&
|
2021-09-02 12:35:16 +10:00
|
|
|
// If we're being pulled then don't mess with our velocity.
|
2021-12-08 17:32:32 +01:00
|
|
|
(!EntityManager.TryGetComponent(body.Owner, out SharedPullableComponent? pullable) || !pullable.BeingPulled) &&
|
2021-12-07 22:22:34 +11:00
|
|
|
_blocker.CanMove((body).Owner);
|
2021-03-08 04:09:59 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used for weightlessness to determine if we are near a wall.
|
|
|
|
|
/// </summary>
|
2021-11-08 12:37:32 +01:00
|
|
|
public static bool IsAroundCollider(SharedPhysicsSystem broadPhaseSystem, TransformComponent transform, IMobMoverComponent mover, IPhysBody collider)
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
|
|
|
|
var enlargedAABB = collider.GetWorldAABB().Enlarged(mover.GrabRange);
|
|
|
|
|
|
|
|
|
|
foreach (var otherCollider in broadPhaseSystem.GetCollidingEntities(transform.MapID, enlargedAABB))
|
|
|
|
|
{
|
|
|
|
|
if (otherCollider == collider) continue; // Don't try to push off of yourself!
|
|
|
|
|
|
|
|
|
|
// Only allow pushing off of anchored things that have collision.
|
|
|
|
|
if (otherCollider.BodyType != BodyType.Static ||
|
|
|
|
|
!otherCollider.CanCollide ||
|
|
|
|
|
((collider.CollisionMask & otherCollider.CollisionLayer) == 0 &&
|
|
|
|
|
(otherCollider.CollisionMask & collider.CollisionLayer) == 0) ||
|
2021-12-08 17:44:39 +01:00
|
|
|
(IoCManager.Resolve<IEntityManager>().TryGetComponent(otherCollider.Owner, out SharedPullableComponent? pullable) && pullable.BeingPulled))
|
2021-03-08 04:09:59 +11:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: Need a predicted client version that only plays for our own entity and then have server-side ignore our session (for that entity only)
|
|
|
|
|
protected virtual void HandleFootsteps(IMoverComponent mover, IMobMoverComponent mobMover) {}
|
|
|
|
|
}
|
|
|
|
|
}
|