The real movement refactor (#9645)

* The real movement refactor

* ref events

* Jetpack cleanup

* a

* Vehicles partially working

* Balance tweaks

* Restore some shitcode

* AAAAAAAA

* Even more prediction

* ECS compstate trying to fix this

* yml

* vehicles kill me

* Don't lock keys

* a

* Fix problem

* Fix sounds

* shuttle inputs

* Shuttle controls

* space brakes

* Keybinds

* Fix merge

* Handle shutdown

* Fix keys

* Bump friction

* fix buckle offset

* Fix relay and friction

* Fix jetpack turning

* contexts amirite
This commit is contained in:
metalgearsloth
2022-07-16 13:51:52 +10:00
committed by GitHub
parent e0b7b48cae
commit b9e876ca92
109 changed files with 1752 additions and 1584 deletions

View File

@@ -1,19 +0,0 @@
using Robust.Shared.Map;
namespace Content.Shared.Movement.Components
{
public interface IMobMoverComponent : IComponent
{
const float GrabRangeDefault = 0.6f;
const float PushStrengthDefault = 600.0f;
const float WeightlessStrengthDefault = 0.4f;
EntityCoordinates LastPosition { get; set; }
public float StepSoundDistance { get; set; }
float GrabRange { get; set; }
float PushStrength { get; set; }
}
}

View File

@@ -1,37 +0,0 @@
namespace Content.Shared.Movement.Components
{
// Does nothing except ensure uniqueness between mover components.
// There can only be one.
public interface IMoverComponent : IComponent
{
/// <summary>
/// Is the entity Sprinting (running)?
/// </summary>
bool Sprinting { get; }
/// <summary>
/// Can the entity currently move. Avoids having to raise move-attempt events every time a player moves.
/// Note that this value will be overridden by the action blocker system, and shouldn't just be set directly.
/// </summary>
bool CanMove { get; set; }
Angle LastGridAngle { get; set; }
/// <summary>
/// Calculated linear velocity direction of the entity.
/// </summary>
(Vector2 walking, Vector2 sprinting) VelocityDir { get; }
/// <summary>
/// Toggles one of the four cardinal directions. Each of the four directions are
/// composed into a single direction vector, <see cref="SharedPlayerInputMoverComponent.VelocityDir"/>. Enabling
/// opposite directions will cancel each other out, resulting in no direction.
/// </summary>
/// <param name="direction">Direction to toggle.</param>
/// <param name="subTick"></param>
/// <param name="enabled">If the direction is active.</param>
void SetVelocityDirection(Direction direction, ushort subTick, bool enabled);
void SetSprinting(ushort subTick, bool walking);
}
}

View File

@@ -0,0 +1,51 @@
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
namespace Content.Shared.Movement.Components
{
[RegisterComponent]
[NetworkedComponent]
public sealed class InputMoverComponent : Component
{
// This class has to be able to handle server TPS being lower than client FPS.
// While still having perfectly responsive movement client side.
// We do this by keeping track of the exact sub-tick values that inputs are pressed on the client,
// and then building a total movement vector based on those sub-tick steps.
//
// We keep track of the last sub-tick a movement input came in,
// Then when a new input comes in, we calculate the fraction of the tick the LAST input was active for
// (new sub-tick - last sub-tick)
// and then add to the total-this-tick movement vector
// by multiplying that fraction by the movement direction for the last input.
// This allows us to incrementally build the movement vector for the current tick,
// without having to keep track of some kind of list of inputs and calculating it later.
//
// We have to keep track of a separate movement vector for walking and sprinting,
// since we don't actually know our current movement speed while processing inputs.
// We change which vector we write into based on whether we were sprinting after the previous input.
// (well maybe we do but the code is designed such that MoverSystem applies movement speed)
// (and I'm not changing that)
/// <summary>
/// Should our velocity be applied to our parent?
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("toParent")]
public bool ToParent = false;
public GameTick LastInputTick;
public ushort LastInputSubTick;
public Vector2 CurTickWalkMovement;
public Vector2 CurTickSprintMovement;
public MoveButtons HeldMoveButtons = MoveButtons.None;
[ViewVariables]
public Angle LastGridAngle { get; set; } = new(0);
public bool Sprinting => (HeldMoveButtons & MoveButtons.Walk) == 0x0;
[ViewVariables(VVAccess.ReadWrite)]
public bool CanMove { get; set; } = true;
}
}

View File

@@ -8,8 +8,5 @@ namespace Content.Shared.Movement.Components;
[RegisterComponent, NetworkedComponent]
public sealed class JetpackUserComponent : Component
{
public float Acceleration = 1f;
public float Friction = 0.3f;
public float WeightlessModifier = 1.2f;
public EntityUid Jetpack;
}

View File

@@ -0,0 +1,59 @@
using Robust.Shared.GameStates;
using Robust.Shared.Map;
namespace Content.Shared.Movement.Components
{
/// <summary>
/// Has additional movement data such as footsteps and weightless grab range for an entity.
/// </summary>
[RegisterComponent]
[NetworkedComponent()]
public sealed class MobMoverComponent : Component
{
private float _stepSoundDistance;
[DataField("grabRange")] public float GrabRange = 1.0f;
[DataField("pushStrength")] public float PushStrength = 600f;
[ViewVariables(VVAccess.ReadWrite)]
public EntityCoordinates LastPosition { get; set; }
/// <summary>
/// Used to keep track of how far we have moved before playing a step sound
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float StepSoundDistance
{
get => _stepSoundDistance;
set
{
if (MathHelper.CloseToPercent(_stepSoundDistance, value)) return;
_stepSoundDistance = value;
}
}
[ViewVariables(VVAccess.ReadWrite)]
public float GrabRangeVV
{
get => GrabRange;
set
{
if (MathHelper.CloseToPercent(GrabRange, value)) return;
GrabRange = value;
Dirty();
}
}
[ViewVariables(VVAccess.ReadWrite)]
public float PushStrengthVV
{
get => PushStrength;
set
{
if (MathHelper.CloseToPercent(PushStrength, value)) return;
PushStrength = value;
Dirty();
}
}
}
}

View File

@@ -3,12 +3,27 @@ using Robust.Shared.GameStates;
namespace Content.Shared.Movement.Components
{
/// <summary>
/// Applies basic movement speed and movement modifiers for an entity.
/// If this is not present on the entity then they will use defaults for movement.
/// </summary>
[RegisterComponent]
[NetworkedComponent, Access(typeof(MovementSpeedModifierSystem))]
public sealed class MovementSpeedModifierComponent : Component
{
public const float DefaultBaseWalkSpeed = 3.0f;
public const float DefaultBaseSprintSpeed = 5.0f;
// Weightless
public const float DefaultMinimumFrictionSpeed = 0.005f;
public const float DefaultWeightlessFriction = 1f;
public const float DefaultWeightlessFrictionNoInput = 0.2f;
public const float DefaultWeightlessModifier = 0.7f;
public const float DefaultWeightlessAcceleration = 1f;
public const float DefaultAcceleration = 20f;
public const float DefaultFriction = 20f;
public const float DefaultFrictionNoInput = 20f;
public const float DefaultBaseWalkSpeed = 2.5f;
public const float DefaultBaseSprintSpeed = 4.5f;
[ViewVariables]
public float WalkSpeedModifier = 1.0f;
@@ -38,6 +53,54 @@ namespace Content.Shared.Movement.Components
}
}
/// <summary>
/// Minimum speed a mob has to be moving before applying movement friction.
/// </summary>
[DataField("minimumFrictionSpeed")]
public float MinimumFrictionSpeed = DefaultMinimumFrictionSpeed;
/// <summary>
/// The negative velocity applied for friction when weightless and providing inputs.
/// </summary>
[DataField("weightlessFriction")]
public float WeightlessFriction = DefaultWeightlessFriction;
/// <summary>
/// The negative velocity applied for friction when weightless and not providing inputs.
/// This is essentially how much their speed decreases per second.
/// </summary>
[DataField("weightlessFrictionNoInput")]
public float WeightlessFrictionNoInput = DefaultWeightlessFrictionNoInput;
/// <summary>
/// The movement speed modifier applied to a mob's total input velocity when weightless.
/// </summary>
[DataField("weightlessModifier")]
public float WeightlessModifier = DefaultWeightlessModifier;
/// <summary>
/// The acceleration applied to mobs when moving and weightless.
/// </summary>
[DataField("weightlessAcceleration")]
public float WeightlessAcceleration = DefaultWeightlessAcceleration;
/// <summary>
/// The acceleration applied to mobs when moving.
/// </summary>
[DataField("acceleration")]
public float Acceleration = DefaultAcceleration;
/// <summary>
/// The negative velocity applied for friction.
/// </summary>
[DataField("friction")]
public float Friction = DefaultFriction;
/// <summary>
/// The negative velocity applied for friction.
/// </summary>
[DataField("frictionNoInput")] public float? FrictionNoInput = null;
[DataField("baseWalkSpeed")]
public float BaseWalkSpeed { get; set; } = DefaultBaseWalkSpeed;

View File

@@ -0,0 +1,13 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Movement.Components;
/// <summary>
/// Raises the engine movement inputs for a particular entity onto the designated entity
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class RelayInputMoverComponent : Component
{
[ViewVariables]
public EntityUid? RelayEntity;
}

View File

@@ -1,24 +0,0 @@
namespace Content.Shared.Movement.Components
{
[RegisterComponent]
[ComponentReference(typeof(IMoverComponent))]
public sealed class SharedDummyInputMoverComponent : Component, IMoverComponent
{
public bool IgnorePaused => false;
public bool CanMove { get; set; } = true;
public Angle LastGridAngle { get => Angle.Zero; set {} }
public bool Sprinting => false;
public (Vector2 walking, Vector2 sprinting) VelocityDir => (Vector2.Zero, Vector2.Zero);
public void SetVelocityDirection(Direction direction, ushort subTick, bool enabled)
{
}
public void SetSprinting(ushort subTick, bool walking)
{
}
}
}

View File

@@ -1,257 +0,0 @@
using Content.Shared.CCVar;
using Robust.Shared.Configuration;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
namespace Content.Shared.Movement.Components
{
[RegisterComponent]
[ComponentReference(typeof(IMoverComponent))]
[NetworkedComponent()]
public sealed class SharedPlayerInputMoverComponent : Component, IMoverComponent
{
// This class has to be able to handle server TPS being lower than client FPS.
// While still having perfectly responsive movement client side.
// We do this by keeping track of the exact sub-tick values that inputs are pressed on the client,
// and then building a total movement vector based on those sub-tick steps.
//
// We keep track of the last sub-tick a movement input came in,
// Then when a new input comes in, we calculate the fraction of the tick the LAST input was active for
// (new sub-tick - last sub-tick)
// and then add to the total-this-tick movement vector
// by multiplying that fraction by the movement direction for the last input.
// This allows us to incrementally build the movement vector for the current tick,
// without having to keep track of some kind of list of inputs and calculating it later.
//
// We have to keep track of a separate movement vector for walking and sprinting,
// since we don't actually know our current movement speed while processing inputs.
// We change which vector we write into based on whether we were sprinting after the previous input.
// (well maybe we do but the code is designed such that MoverSystem applies movement speed)
// (and I'm not changing that)
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
public GameTick _lastInputTick;
public ushort _lastInputSubTick;
public Vector2 CurTickWalkMovement;
public Vector2 CurTickSprintMovement;
private MoveButtons _heldMoveButtons = MoveButtons.None;
[ViewVariables]
public Angle LastGridAngle { get; set; } = new(0);
public bool Sprinting => !HasFlag(_heldMoveButtons, MoveButtons.Walk);
[ViewVariables(VVAccess.ReadWrite)]
public bool CanMove { get; set; } = true;
/// <summary>
/// Calculated linear velocity direction of the entity.
/// </summary>
[ViewVariables]
public (Vector2 walking, Vector2 sprinting) VelocityDir
{
get
{
if (!_gameTiming.InSimulation)
{
// Outside of simulation we'll be running client predicted movement per-frame.
// So return a full-length vector as if it's a full tick.
// Physics system will have the correct time step anyways.
var immediateDir = DirVecForButtons(_heldMoveButtons);
return Sprinting ? (Vector2.Zero, immediateDir) : (immediateDir, Vector2.Zero);
}
Vector2 walk;
Vector2 sprint;
float remainingFraction;
if (_gameTiming.CurTick > _lastInputTick)
{
walk = Vector2.Zero;
sprint = Vector2.Zero;
remainingFraction = 1;
}
else
{
walk = CurTickWalkMovement;
sprint = CurTickSprintMovement;
remainingFraction = (ushort.MaxValue - _lastInputSubTick) / (float) ushort.MaxValue;
}
var curDir = DirVecForButtons(_heldMoveButtons) * remainingFraction;
if (Sprinting)
{
sprint += curDir;
}
else
{
walk += curDir;
}
// Logger.Info($"{curDir}{walk}{sprint}");
return (walk, sprint);
}
}
/// <summary>
/// Whether or not the player can move diagonally.
/// </summary>
[ViewVariables]
public bool DiagonalMovementEnabled => _configurationManager.GetCVar<bool>(CCVars.GameDiagonalMovement);
/// <inheritdoc />
protected override void Initialize()
{
base.Initialize();
LastGridAngle = _entityManager.GetComponent<TransformComponent>(Owner).Parent?.WorldRotation ?? new Angle(0);
}
/// <summary>
/// Toggles one of the four cardinal directions. Each of the four directions are
/// composed into a single direction vector, <see cref="VelocityDir"/>. Enabling
/// opposite directions will cancel each other out, resulting in no direction.
/// </summary>
/// <param name="direction">Direction to toggle.</param>
/// <param name="subTick"></param>
/// <param name="enabled">If the direction is active.</param>
public void SetVelocityDirection(Direction direction, ushort subTick, bool enabled)
{
// Logger.Info($"[{_gameTiming.CurTick}/{subTick}] {direction}: {enabled}");
var bit = direction switch
{
Direction.East => MoveButtons.Right,
Direction.North => MoveButtons.Up,
Direction.West => MoveButtons.Left,
Direction.South => MoveButtons.Down,
_ => throw new ArgumentException(nameof(direction))
};
SetMoveInput(subTick, enabled, bit);
}
private void SetMoveInput(ushort subTick, bool enabled, MoveButtons bit)
{
// Modifies held state of a movement button at a certain sub tick and updates current tick movement vectors.
if (_gameTiming.CurTick > _lastInputTick)
{
CurTickWalkMovement = Vector2.Zero;
CurTickSprintMovement = Vector2.Zero;
_lastInputTick = _gameTiming.CurTick;
_lastInputSubTick = 0;
}
if (subTick >= _lastInputSubTick)
{
var fraction = (subTick - _lastInputSubTick) / (float) ushort.MaxValue;
ref var lastMoveAmount = ref Sprinting ? ref CurTickSprintMovement : ref CurTickWalkMovement;
lastMoveAmount += DirVecForButtons(_heldMoveButtons) * fraction;
_lastInputSubTick = subTick;
}
if (enabled)
{
_heldMoveButtons |= bit;
}
else
{
_heldMoveButtons &= ~bit;
}
Dirty();
}
public void SetSprinting(ushort subTick, bool walking)
{
// Logger.Info($"[{_gameTiming.CurTick}/{subTick}] Sprint: {enabled}");
SetMoveInput(subTick, walking, MoveButtons.Walk);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is MoverComponentState state)
{
_heldMoveButtons = state.Buttons;
_lastInputTick = GameTick.Zero;
_lastInputSubTick = 0;
CanMove = state.CanMove;
}
}
public override ComponentState GetComponentState()
{
return new MoverComponentState(_heldMoveButtons, CanMove);
}
/// <summary>
/// Retrieves the normalized direction vector for a specified combination of movement keys.
/// </summary>
private Vector2 DirVecForButtons(MoveButtons buttons)
{
// key directions are in screen coordinates
// _moveDir is in world coordinates
// if the camera is moved, this needs to be changed
var x = 0;
x -= HasFlag(buttons, MoveButtons.Left) ? 1 : 0;
x += HasFlag(buttons, MoveButtons.Right) ? 1 : 0;
var y = 0;
if (DiagonalMovementEnabled || x == 0)
{
y -= HasFlag(buttons, MoveButtons.Down) ? 1 : 0;
y += HasFlag(buttons, MoveButtons.Up) ? 1 : 0;
}
var vec = new Vector2(x, y);
// can't normalize zero length vector
if (vec.LengthSquared > 1.0e-6)
{
// Normalize so that diagonals aren't faster or something.
vec = vec.Normalized;
}
return vec;
}
[Serializable, NetSerializable]
private sealed class MoverComponentState : ComponentState
{
public MoveButtons Buttons { get; }
public readonly bool CanMove;
public MoverComponentState(MoveButtons buttons, bool canMove)
{
Buttons = buttons;
CanMove = canMove;
}
}
[Flags]
private enum MoveButtons : byte
{
None = 0,
Up = 1,
Down = 2,
Left = 4,
Right = 8,
Walk = 16,
}
private static bool HasFlag(MoveButtons buttons, MoveButtons flag)
{
return (buttons & flag) == flag;
}
}
}

View File

@@ -1,88 +0,0 @@
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
namespace Content.Shared.Movement.Components
{
/// <summary>
/// The basic player mover with footsteps and grabbing
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IMobMoverComponent))]
[NetworkedComponent()]
public sealed class SharedPlayerMobMoverComponent : Component, IMobMoverComponent
{
private float _stepSoundDistance;
[DataField("grabRange")]
private float _grabRange = IMobMoverComponent.GrabRangeDefault;
[DataField("pushStrength")]
private float _pushStrength = IMobMoverComponent.PushStrengthDefault;
[ViewVariables(VVAccess.ReadWrite)]
public EntityCoordinates LastPosition { get; set; }
/// <summary>
/// Used to keep track of how far we have moved before playing a step sound
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float StepSoundDistance
{
get => _stepSoundDistance;
set
{
if (MathHelper.CloseToPercent(_stepSoundDistance, value)) return;
_stepSoundDistance = value;
}
}
[ViewVariables(VVAccess.ReadWrite)]
public float GrabRange
{
get => _grabRange;
set
{
if (MathHelper.CloseToPercent(_grabRange, value)) return;
_grabRange = value;
Dirty();
}
}
[ViewVariables(VVAccess.ReadWrite)]
public float PushStrength
{
get => _pushStrength;
set
{
if (MathHelper.CloseToPercent(_pushStrength, value)) return;
_pushStrength = value;
Dirty();
}
}
public override ComponentState GetComponentState()
{
return new PlayerMobMoverComponentState(_grabRange, _pushStrength);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not PlayerMobMoverComponentState playerMoverState) return;
GrabRange = playerMoverState.GrabRange;
PushStrength = playerMoverState.PushStrength;
}
[Serializable, NetSerializable]
private sealed class PlayerMobMoverComponentState : ComponentState
{
public float GrabRange;
public float PushStrength;
public PlayerMobMoverComponentState(float grabRange, float pushStrength)
{
GrabRange = grabRange;
PushStrength = pushStrength;
}
}
}
}