Mob Movement Major Refactor (#36847)

* Conveyor optimisations

- Optimise movement for moving stuff. Better flags + less resolves + slapped parallelrobustjob on it.
- Sleeping for entities getting conveyed into walls.

* Blocker version

* Finish

* Final

* Fix conveyor power mispredict

* Bagel save

* Revert "Bagel save"

This reverts commit 1b93fda81fb852d89b89b0beae0b80f8a61165f2.

* Conveyor resave

* Init Commit

* windows yelling at me to update commit

* working commit, need prediciton and more dehardcoding

* Project 0 warnings

* Working Commit (Near Final)

* ryder got confused commit

* I love Merge Conflicts :)

* Working commit, no prediction

* Forgot the yaml changes

* Comments and typos

* Apparently while the reduced launch mult of lube was initialized it was never used so I revered back to default

* Fixed an incorrect divisor

* bit of cleanup

* Prediciton fixed, and puddles now affect all entities

* FORGOT TO RENAME A VERY IMPORTANT VARIABLE OOPS

* Really big I forgor moment

* Even bigger I forgor moment

* four more merge conflicts to fix four more oopsies

* fixed actual divide by zero moment and also im very dumb

* Even bigger I forgor moment

* four more merge conflicts to fix four more oopsies

* fixed actual divide by zero moment and also im very dumb

* Fix all test fails

* code cleanup

* Webedit whitespace

* Code cleaup

* whitespace webedit

* whitespace webedit

* whitespace webedit

* whitespace removal

* Comments and cleanup

* Re-Added 20 warnings as per Ork's request

* Cleanups

* Spacing fix

* bugfixes and cleanup

* Small bugfix

* Fix prediction

* Mob movement rewrite

* Bandaid

* Working version

* Tentatively working

* Friction to fix cornering

* More fixes

* Refactor mob movement

Trying to cleanup relay ordering / tryupdaterelative being cooked, purge ToParent, and fix all the eye rotation shenanigans.

* Building

* Re-implement jetpacks

* Reorganise weightless movement

* More work

* Fix camera

* reh

* Revert bagel

* Revert this

* Revert held move buttons

* Puddles work but are unpredicted and unoptimized

* Fixes

* Puddle code...

* Actually dirty the slipComp for real

* Sliding component done plus an extra suggestion from ArtisticRoomba

* Atomized Commit

* Added Friction field to Reagent Prototype per design discussion

* Cleaned up Working Commit

* a

* Delete stinkers

* Fix this code smell

* Reviewed

* Funky re-save

* Our conveyance

* Better conveyor sleeping

* Remove this

* Revert "Better conveyor sleeping"

This reverts commit f5281f64bbae95b7b9feb56295c5cf931f9fb2e1.

* Revert that

Way too janky

* Also this

* a

* Working Commit - Still a lot to do

* Acceleration refactor

* Minor jetpack cleanup

* frictionnomovement no longer nullable

* Shared Mover Feels 99% done

* OffGrid/Weightless/Throwing Friction saved

* Fix merge conflicts

* Fix a debug assert

* Final Commit for today

* Some fixes

* Actually use those CCVars Properly

* Need to fix throwing

* Second to last Commit for real

* Jetpack bug fixed

* Jetpack bug fixed

* Test fail patch

* Small patch

* Skates Component cleanup + Bring Accel back to 5 (oops)

* Fix test fail oops

* yaml cleanup make dragons not fat

---------

Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
Princess Cheeseballs
2025-05-02 01:18:08 -07:00
committed by GitHub
parent 1fbc845126
commit 36030ef154
42 changed files with 799 additions and 524 deletions

View File

@@ -34,7 +34,7 @@ public sealed class EyeLerpingSystem : EntitySystem
SubscribeLocalEvent<LerpingEyeComponent, LocalPlayerDetachedEvent>(OnDetached); SubscribeLocalEvent<LerpingEyeComponent, LocalPlayerDetachedEvent>(OnDetached);
UpdatesAfter.Add(typeof(TransformSystem)); UpdatesAfter.Add(typeof(TransformSystem));
UpdatesAfter.Add(typeof(PhysicsSystem)); UpdatesAfter.Add(typeof(Robust.Client.Physics.PhysicsSystem));
UpdatesBefore.Add(typeof(SharedEyeSystem)); UpdatesBefore.Add(typeof(SharedEyeSystem));
UpdatesOutsidePrediction = true; UpdatesOutsidePrediction = true;
} }

View File

@@ -1,5 +1,6 @@
using System.Numerics; using System.Numerics;
using Content.Client.Physics.Controllers; using Content.Client.Physics.Controllers;
using Content.Client.PhysicsSystem.Controllers;
using Content.Shared.Movement.Components; using Content.Shared.Movement.Components;
using Content.Shared.NPC; using Content.Shared.NPC;
using Content.Shared.NPC.Events; using Content.Shared.NPC.Events;

View File

@@ -3,15 +3,13 @@ using Content.Shared.CCVar;
using Content.Shared.Movement.Components; using Content.Shared.Movement.Components;
using Content.Shared.Movement.Pulling.Components; using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Systems; using Content.Shared.Movement.Systems;
using Robust.Client.GameObjects;
using Robust.Client.Physics; using Robust.Client.Physics;
using Robust.Client.Player; using Robust.Client.Player;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Timing; using Robust.Shared.Timing;
namespace Content.Client.Physics.Controllers; namespace Content.Client.PhysicsSystem.Controllers;
public sealed class MoverController : SharedMoverController public sealed class MoverController : SharedMoverController
{ {
@@ -101,39 +99,13 @@ public sealed class MoverController : SharedMoverController
private void HandleClientsideMovement(EntityUid player, float frameTime) private void HandleClientsideMovement(EntityUid player, float frameTime)
{ {
if (!MoverQuery.TryGetComponent(player, out var mover) || if (!MoverQuery.TryGetComponent(player, out var mover))
!XformQuery.TryGetComponent(player, out var xform))
{
return;
}
var physicsUid = player;
PhysicsComponent? body;
var xformMover = xform;
if (mover.ToParent && RelayQuery.HasComponent(xform.ParentUid))
{
if (!PhysicsQuery.TryGetComponent(xform.ParentUid, out body) ||
!XformQuery.TryGetComponent(xform.ParentUid, out xformMover))
{
return;
}
physicsUid = xform.ParentUid;
}
else if (!PhysicsQuery.TryGetComponent(player, out body))
{ {
return; return;
} }
// Server-side should just be handled on its own so we'll just do this shizznit // Server-side should just be handled on its own so we'll just do this shizznit
HandleMobMovement( HandleMobMovement((player, mover), frameTime);
player,
mover,
physicsUid,
body,
xformMover,
frameTime);
} }
protected override bool CanSound() protected override bool CanSound()

View File

@@ -386,6 +386,9 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
if (!TryComp<StepTriggerComponent>(entity, out var comp)) if (!TryComp<StepTriggerComponent>(entity, out var comp))
return; return;
// Ensure we actually have the component
EnsureComp<TileFrictionModifierComponent>(entity);
// This is the base amount of reagent needed before a puddle can be considered slippery. Is defined based on // This is the base amount of reagent needed before a puddle can be considered slippery. Is defined based on
// the sprite threshold for a puddle larger than 5 pixels. // the sprite threshold for a puddle larger than 5 pixels.
var smallPuddleThreshold = FixedPoint2.New(entity.Comp.OverflowVolume.Float() * LowThreshold); var smallPuddleThreshold = FixedPoint2.New(entity.Comp.OverflowVolume.Float() * LowThreshold);
@@ -409,7 +412,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
if (solution.Volume <= smallPuddleThreshold) if (solution.Volume <= smallPuddleThreshold)
{ {
_stepTrigger.SetActive(entity, false, comp); _stepTrigger.SetActive(entity, false, comp);
_tile.SetModifier(entity, TileFrictionController.DefaultFriction); _tile.SetModifier(entity, 1f);
return; return;
} }
@@ -461,7 +464,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
// Lower tile friction based on how slippery it is, lets items slide across a puddle of lube // Lower tile friction based on how slippery it is, lets items slide across a puddle of lube
slipComp.SlipData.SlipFriction = (float)(puddleFriction/solution.Volume); slipComp.SlipData.SlipFriction = (float)(puddleFriction/solution.Volume);
_tile.SetModifier(entity, TileFrictionController.DefaultFriction * slipComp.SlipData.SlipFriction); _tile.SetModifier(entity, slipComp.SlipData.SlipFriction);
Dirty(entity, slipComp); Dirty(entity, slipComp);
} }
@@ -479,7 +482,7 @@ public sealed partial class PuddleSystem : SharedPuddleSystem
{ {
var comp = EnsureComp<SpeedModifierContactsComponent>(uid); var comp = EnsureComp<SpeedModifierContactsComponent>(uid);
var speed = 1 - maxViscosity; var speed = 1 - maxViscosity;
_speedModContacts.ChangeModifiers(uid, speed, comp); _speedModContacts.ChangeSpeedModifiers(uid, speed, comp);
} }
else else
{ {

View File

@@ -57,48 +57,43 @@ public sealed class MoverController : SharedMoverController
return true; return true;
} }
private HashSet<EntityUid> _moverAdded = new();
private List<Entity<InputMoverComponent>> _movers = new();
private void InsertMover(Entity<InputMoverComponent> source)
{
if (TryComp(source, out MovementRelayTargetComponent? relay))
{
if (TryComp(relay.Source, out InputMoverComponent? relayMover))
{
InsertMover((relay.Source, relayMover));
}
}
// Already added
if (!_moverAdded.Add(source.Owner))
return;
_movers.Add(source);
}
public override void UpdateBeforeSolve(bool prediction, float frameTime) public override void UpdateBeforeSolve(bool prediction, float frameTime)
{ {
base.UpdateBeforeSolve(prediction, frameTime); base.UpdateBeforeSolve(prediction, frameTime);
_moverAdded.Clear();
_movers.Clear();
var inputQueryEnumerator = AllEntityQuery<InputMoverComponent>(); var inputQueryEnumerator = AllEntityQuery<InputMoverComponent>();
// Need to order mob movement so that movers don't run before their relays.
while (inputQueryEnumerator.MoveNext(out var uid, out var mover)) while (inputQueryEnumerator.MoveNext(out var uid, out var mover))
{ {
var physicsUid = uid; InsertMover((uid, mover));
}
if (RelayQuery.HasComponent(uid)) foreach (var mover in _movers)
continue; {
HandleMobMovement(mover, frameTime);
if (!XformQuery.TryGetComponent(uid, out var xform))
{
continue;
}
PhysicsComponent? body;
var xformMover = xform;
if (mover.ToParent && RelayQuery.HasComponent(xform.ParentUid))
{
if (!PhysicsQuery.TryGetComponent(xform.ParentUid, out body) ||
!XformQuery.TryGetComponent(xform.ParentUid, out xformMover))
{
continue;
}
physicsUid = xform.ParentUid;
}
else if (!PhysicsQuery.TryGetComponent(uid, out body))
{
continue;
}
HandleMobMovement(uid,
mover,
physicsUid,
body,
xformMover,
frameTime);
} }
HandleShuttleMovement(frameTime); HandleShuttleMovement(frameTime);

View File

@@ -10,8 +10,17 @@ public sealed partial class CCVars
public static readonly CVarDef<bool> RelativeMovement = public static readonly CVarDef<bool> RelativeMovement =
CVarDef.Create("physics.relative_movement", true, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER); CVarDef.Create("physics.relative_movement", true, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<float> MinFriction =
CVarDef.Create("physics.min_friction", 0.0f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<float> AirFriction =
CVarDef.Create("physics.air_friction", 0.2f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<float> OffgridFriction =
CVarDef.Create("physics.offgrid_friction", 0.05f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<float> TileFrictionModifier = public static readonly CVarDef<float> TileFrictionModifier =
CVarDef.Create("physics.tile_friction", 40.0f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER); CVarDef.Create("physics.tile_friction", 8.0f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);
public static readonly CVarDef<float> StopSpeed = public static readonly CVarDef<float> StopSpeed =
CVarDef.Create("physics.stop_speed", 0.1f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER); CVarDef.Create("physics.stop_speed", 0.1f, CVar.ARCHIVE | CVar.REPLICATED | CVar.SERVER);

View File

@@ -100,7 +100,6 @@ namespace Content.Shared.Chemistry.Reagent
[DataField] [DataField]
public bool MetamorphicChangeColor { get; private set; } = true; public bool MetamorphicChangeColor { get; private set; } = true;
/// <summary> /// <summary>
/// If not null, makes something slippery. Also defines slippery interactions like stun time and launch mult. /// If not null, makes something slippery. Also defines slippery interactions like stun time and launch mult.
/// </summary> /// </summary>

View File

@@ -1,7 +1,7 @@
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Content.Shared.Clothing.EntitySystems; using Content.Shared.Clothing.EntitySystems;
namespace Content.Shared.Clothing; namespace Content.Shared.Clothing.Components;
[RegisterComponent] [RegisterComponent]
[NetworkedComponent] [NetworkedComponent]
@@ -11,44 +11,44 @@ public sealed partial class SkatesComponent : Component
/// <summary> /// <summary>
/// the levels of friction the wearer is subected to, higher the number the more friction. /// the levels of friction the wearer is subected to, higher the number the more friction.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float Friction = 2.5f; public float Friction = 0.125f;
/// <summary> /// <summary>
/// Determines the turning ability of the wearer, Higher the number the less control of their turning ability. /// Determines the turning ability of the wearer, Higher the number the less control of their turning ability.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float? FrictionNoInput = 2.5f; public float FrictionNoInput = 0.125f;
/// <summary> /// <summary>
/// Sets the speed in which the wearer accelerates to full speed, higher the number the quicker the acceleration. /// Sets the speed in which the wearer accelerates to full speed, higher the number the quicker the acceleration.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float Acceleration = 5f; public float Acceleration = 0.25f;
/// <summary> /// <summary>
/// The minimum speed the wearer needs to be traveling to take damage from collision. /// The minimum speed the wearer needs to be traveling to take damage from collision.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float MinimumSpeed = 3f; public float MinimumSpeed = 3f;
/// <summary> /// <summary>
/// The length of time the wearer is stunned for on collision. /// The length of time the wearer is stunned for on collision.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float StunSeconds = 3f; public float StunSeconds = 3f;
/// <summary> /// <summary>
/// The time duration before another collision can take place. /// The time duration before another collision can take place.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float DamageCooldown = 2f; public float DamageCooldown = 2f;
/// <summary> /// <summary>
/// The damage per increment of speed on collision. /// The damage per increment of speed on collision.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField]
public float SpeedDamage = 1f; public float SpeedDamage = 1f;

View File

@@ -1,9 +1,9 @@
using Content.Shared.Inventory.Events;
using Content.Shared.Movement.Systems; using Content.Shared.Movement.Systems;
using Content.Shared.Damage.Systems; using Content.Shared.Damage.Systems;
using Content.Shared.Movement.Components; using Content.Shared.Inventory;
using Content.Shared.Clothing.Components;
namespace Content.Shared.Clothing; namespace Content.Shared.Clothing.EntitySystems;
/// <summary> /// <summary>
/// Changes the friction and acceleration of the wearer and also the damage on impact variables of thew wearer when hitting a static object. /// Changes the friction and acceleration of the wearer and also the damage on impact variables of thew wearer when hitting a static object.
@@ -19,26 +19,31 @@ public sealed class SkatesSystem : EntitySystem
SubscribeLocalEvent<SkatesComponent, ClothingGotEquippedEvent>(OnGotEquipped); SubscribeLocalEvent<SkatesComponent, ClothingGotEquippedEvent>(OnGotEquipped);
SubscribeLocalEvent<SkatesComponent, ClothingGotUnequippedEvent>(OnGotUnequipped); SubscribeLocalEvent<SkatesComponent, ClothingGotUnequippedEvent>(OnGotUnequipped);
SubscribeLocalEvent<SkatesComponent, InventoryRelayedEvent<RefreshFrictionModifiersEvent>>(OnRefreshFrictionModifiers);
} }
/// <summary> /// <summary>
/// When item is unequipped from the shoe slot, friction, aceleration and collide on impact return to default settings. /// When item is unequipped from the shoe slot, friction, aceleration and collide on impact return to default settings.
/// </summary> /// </summary>
public void OnGotUnequipped(EntityUid uid, SkatesComponent component, ClothingGotUnequippedEvent args) private void OnGotUnequipped(Entity<SkatesComponent> entity, ref ClothingGotUnequippedEvent args)
{ {
if (!TryComp(args.Wearer, out MovementSpeedModifierComponent? speedModifier)) _move.RefreshFrictionModifiers(args.Wearer);
return; _impact.ChangeCollide(args.Wearer, entity.Comp.DefaultMinimumSpeed, entity.Comp.DefaultStunSeconds, entity.Comp.DefaultDamageCooldown, entity.Comp.DefaultSpeedDamage);
_move.ChangeFriction(args.Wearer, MovementSpeedModifierComponent.DefaultFriction, MovementSpeedModifierComponent.DefaultFrictionNoInput, MovementSpeedModifierComponent.DefaultAcceleration, speedModifier);
_impact.ChangeCollide(args.Wearer, component.DefaultMinimumSpeed, component.DefaultStunSeconds, component.DefaultDamageCooldown, component.DefaultSpeedDamage);
} }
/// <summary> /// <summary>
/// When item is equipped into the shoe slot, friction, acceleration and collide on impact are adjusted. /// When item is equipped into the shoe slot, friction, acceleration and collide on impact are adjusted.
/// </summary> /// </summary>
private void OnGotEquipped(EntityUid uid, SkatesComponent component, ClothingGotEquippedEvent args) private void OnGotEquipped(Entity<SkatesComponent> entity, ref ClothingGotEquippedEvent args)
{ {
_move.ChangeFriction(args.Wearer, component.Friction, component.FrictionNoInput, component.Acceleration); _move.RefreshFrictionModifiers(args.Wearer);
_impact.ChangeCollide(args.Wearer, component.MinimumSpeed, component.StunSeconds, component.DamageCooldown, component.SpeedDamage); _impact.ChangeCollide(args.Wearer, entity.Comp.MinimumSpeed, entity.Comp.StunSeconds, entity.Comp.DamageCooldown, entity.Comp.SpeedDamage);
}
private void OnRefreshFrictionModifiers(Entity<SkatesComponent> ent,
ref InventoryRelayedEvent<RefreshFrictionModifiersEvent> args)
{
args.Args.ModifyFriction(ent.Comp.Friction, ent.Comp.FrictionNoInput);
args.Args.ModifyAcceleration(ent.Comp.Acceleration);
} }
} }

View File

@@ -33,6 +33,7 @@ public sealed class DamageOnHighSpeedImpactSystem : EntitySystem
if (!EntityManager.HasComponent<DamageableComponent>(uid)) if (!EntityManager.HasComponent<DamageableComponent>(uid))
return; return;
//TODO: This should solve after physics solves
var speed = args.OurBody.LinearVelocity.Length(); var speed = args.OurBody.LinearVelocity.Length();
if (speed < component.MinimumSpeed) if (speed < component.MinimumSpeed)

View File

@@ -1,6 +1,7 @@
using System.Numerics; using System.Numerics;
using Content.Shared.CCVar; using Content.Shared.CCVar;
using Content.Shared.Gravity; using Content.Shared.Gravity;
using Content.Shared.Interaction.Events;
using Content.Shared.Movement.Events; using Content.Shared.Movement.Events;
using Content.Shared.Movement.Pulling.Components; using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Systems; using Content.Shared.Movement.Systems;
@@ -8,6 +9,7 @@ using JetBrains.Annotations;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Map.Components; using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Controllers; using Robust.Shared.Physics.Controllers;
using Robust.Shared.Physics.Dynamics; using Robust.Shared.Physics.Dynamics;
@@ -21,7 +23,6 @@ namespace Content.Shared.Friction
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!; [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!; [Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly SharedMoverController _mover = default!; [Dependency] private readonly SharedMoverController _mover = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly SharedMapSystem _map = default!; [Dependency] private readonly SharedMapSystem _map = default!;
private EntityQuery<TileFrictionModifierComponent> _frictionQuery; private EntityQuery<TileFrictionModifierComponent> _frictionQuery;
@@ -30,16 +31,19 @@ namespace Content.Shared.Friction
private EntityQuery<PullableComponent> _pullableQuery; private EntityQuery<PullableComponent> _pullableQuery;
private EntityQuery<MapGridComponent> _gridQuery; private EntityQuery<MapGridComponent> _gridQuery;
private float _stopSpeed;
private float _frictionModifier; private float _frictionModifier;
public const float DefaultFriction = 0.3f; private float _minDamping;
private float _airDamping;
private float _offGridDamping;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true); Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true);
Subs.CVar(_configManager, CCVars.StopSpeed, value => _stopSpeed = value, true); Subs.CVar(_configManager, CCVars.MinFriction, value => _minDamping = value, true);
Subs.CVar(_configManager, CCVars.AirFriction, value => _airDamping = value, true);
Subs.CVar(_configManager, CCVars.OffgridFriction, value => _offGridDamping = value, true);
_frictionQuery = GetEntityQuery<TileFrictionModifierComponent>(); _frictionQuery = GetEntityQuery<TileFrictionModifierComponent>();
_xformQuery = GetEntityQuery<TransformComponent>(); _xformQuery = GetEntityQuery<TransformComponent>();
_pullerQuery = GetEntityQuery<PullerComponent>(); _pullerQuery = GetEntityQuery<PullerComponent>();
@@ -56,12 +60,9 @@ namespace Content.Shared.Friction
var uid = body.Owner; var uid = body.Owner;
// Only apply friction when it's not a mob (or the mob doesn't have control) // Only apply friction when it's not a mob (or the mob doesn't have control)
if (prediction && !body.Predict || // We may want to instead only apply friction to dynamic entities and not mobs ever.
body.BodyStatus == BodyStatus.InAir || if (prediction && !body.Predict || _mover.UseMobMovement(uid))
_mover.UseMobMovement(uid))
{
continue; continue;
}
if (body.LinearVelocity.Equals(Vector2.Zero) && body.AngularVelocity.Equals(0f)) if (body.LinearVelocity.Equals(Vector2.Zero) && body.AngularVelocity.Equals(0f))
continue; continue;
@@ -72,7 +73,15 @@ namespace Content.Shared.Friction
continue; continue;
} }
var surfaceFriction = GetTileFriction(uid, body, xform); float friction;
// If we're not touching the ground, don't use tileFriction.
// TODO: Make IsWeightless event-based; we already have grid traversals tracked so just raise events
if (body.BodyStatus == BodyStatus.InAir || _gravity.IsWeightless(uid, body, xform) || !xform.Coordinates.IsValid(EntityManager))
friction = xform.GridUid == null || !_gridQuery.HasComp(xform.GridUid) ? _offGridDamping : _airDamping;
else
friction = _frictionModifier * GetTileFriction(uid, body, xform);
var bodyModifier = 1f; var bodyModifier = 1f;
if (_frictionQuery.TryGetComponent(uid, out var frictionComp)) if (_frictionQuery.TryGetComponent(uid, out var frictionComp))
@@ -94,96 +103,39 @@ namespace Content.Shared.Friction
bodyModifier *= 0.2f; bodyModifier *= 0.2f;
} }
var friction = _frictionModifier * surfaceFriction * bodyModifier; friction *= bodyModifier;
ReduceLinearVelocity(uid, prediction, body, friction, frameTime); friction = Math.Max(_minDamping, friction);
ReduceAngularVelocity(uid, prediction, body, friction, frameTime);
PhysicsSystem.SetLinearDamping(uid, body, friction);
PhysicsSystem.SetAngularDamping(uid, body, friction);
if (body.BodyType != BodyType.KinematicController)
return;
// Physics engine doesn't apply damping to Kinematic Controllers so we have to do it here.
// BEWARE YE TRAVELLER:
// You may think you can just pass the body.LinearVelocity to the Friction function and edit it there!
// But doing so is unpredicted! And you will doom yourself to 1000 years of rubber banding!
var velocity = body.LinearVelocity;
_mover.Friction(0f, frameTime, friction, ref velocity);
PhysicsSystem.SetLinearVelocity(uid, velocity, body: body);
} }
} }
private void ReduceLinearVelocity(EntityUid uid, bool prediction, PhysicsComponent body, float friction, float frameTime)
{
var speed = body.LinearVelocity.Length();
if (speed <= 0.0f)
return;
// This is the *actual* amount that speed will drop by, we just do some multiplication around it to be easier.
var drop = 0.0f;
float control;
if (friction > 0.0f)
{
// TBH I can't really tell if this makes a difference.
if (!prediction)
{
control = speed < _stopSpeed ? _stopSpeed : speed;
}
else
{
control = speed;
}
drop += control * friction * frameTime;
}
var newSpeed = MathF.Max(0.0f, speed - drop);
newSpeed /= speed;
_physics.SetLinearVelocity(uid, body.LinearVelocity * newSpeed, body: body);
}
private void ReduceAngularVelocity(EntityUid uid, bool prediction, PhysicsComponent body, float friction, float frameTime)
{
var speed = MathF.Abs(body.AngularVelocity);
if (speed <= 0.0f)
return;
// This is the *actual* amount that speed will drop by, we just do some multiplication around it to be easier.
var drop = 0.0f;
float control;
if (friction > 0.0f)
{
// TBH I can't really tell if this makes a difference.
if (!prediction)
{
control = speed < _stopSpeed ? _stopSpeed : speed;
}
else
{
control = speed;
}
drop += control * friction * frameTime;
}
var newSpeed = MathF.Max(0.0f, speed - drop);
newSpeed /= speed;
_physics.SetAngularVelocity(uid, body.AngularVelocity * newSpeed, body: body);
}
[Pure] [Pure]
private float GetTileFriction( private float GetTileFriction(
EntityUid uid, EntityUid uid,
PhysicsComponent body, PhysicsComponent body,
TransformComponent xform) TransformComponent xform)
{ {
// TODO: Make IsWeightless event-based; we already have grid traversals tracked so just raise events var tileModifier = 1f;
if (_gravity.IsWeightless(uid, body, xform)) // If not on a grid and not in the air then return the map's friction.
return 0.0f;
if (!xform.Coordinates.IsValid(EntityManager))
return 0.0f;
// If not on a grid then return the map's friction.
if (!_gridQuery.TryGetComponent(xform.GridUid, out var grid)) if (!_gridQuery.TryGetComponent(xform.GridUid, out var grid))
{ {
return _frictionQuery.TryGetComponent(xform.MapUid, out var friction) return _frictionQuery.TryGetComponent(xform.MapUid, out var friction)
? friction.Modifier ? friction.Modifier
: DefaultFriction; : tileModifier;
} }
var tile = _map.GetTileRef(xform.GridUid.Value, grid, xform.Coordinates); var tile = _map.GetTileRef(xform.GridUid.Value, grid, xform.Coordinates);
@@ -192,21 +144,18 @@ namespace Content.Shared.Friction
if (tile.Tile.IsEmpty && if (tile.Tile.IsEmpty &&
HasComp<MapComponent>(xform.GridUid) && HasComp<MapComponent>(xform.GridUid) &&
(!TryComp<GravityComponent>(xform.GridUid, out var gravity) || gravity.Enabled)) (!TryComp<GravityComponent>(xform.GridUid, out var gravity) || gravity.Enabled))
{ return tileModifier;
return DefaultFriction;
}
// If there's an anchored ent that modifies friction then fallback to that instead.
var anc = grid.GetAnchoredEntitiesEnumerator(tile.GridIndices);
// Check for anchored ents that modify friction
var anc = _map.GetAnchoredEntitiesEnumerator(xform.GridUid.Value, grid, tile.GridIndices);
while (anc.MoveNext(out var tileEnt)) while (anc.MoveNext(out var tileEnt))
{ {
if (_frictionQuery.TryGetComponent(tileEnt, out var friction)) if (_frictionQuery.TryGetComponent(tileEnt, out var friction))
return friction.Modifier; tileModifier *= friction.Modifier;
} }
var tileDef = _tileDefinitionManager[tile.Tile.TypeId]; var tileDef = _tileDefinitionManager[tile.Tile.TypeId];
return tileDef.Friction; return tileDef.Friction * tileModifier;
} }
public void SetModifier(EntityUid entityUid, float value, TileFrictionModifierComponent? friction = null) public void SetModifier(EntityUid entityUid, float value, TileFrictionModifierComponent? friction = null)

View File

@@ -50,6 +50,7 @@ public partial class InventorySystem
SubscribeLocalEvent<InventoryComponent, ZombificationResistanceQueryEvent>(RelayInventoryEvent); SubscribeLocalEvent<InventoryComponent, ZombificationResistanceQueryEvent>(RelayInventoryEvent);
// by-ref events // by-ref events
SubscribeLocalEvent<InventoryComponent, RefreshFrictionModifiersEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, BeforeStaminaDamageEvent>(RefRelayInventoryEvent); SubscribeLocalEvent<InventoryComponent, BeforeStaminaDamageEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, GetExplosionResistanceEvent>(RefRelayInventoryEvent); SubscribeLocalEvent<InventoryComponent, GetExplosionResistanceEvent>(RefRelayInventoryEvent);
SubscribeLocalEvent<InventoryComponent, IsWeightlessEvent>(RefRelayInventoryEvent); SubscribeLocalEvent<InventoryComponent, IsWeightlessEvent>(RefRelayInventoryEvent);

View File

@@ -62,7 +62,10 @@ namespace Content.Shared.Maps
/// </summary> /// </summary>
[DataField("barestepSounds")] public SoundSpecifier? BarestepSounds { get; private set; } = new SoundCollectionSpecifier("BarestepHard"); [DataField("barestepSounds")] public SoundSpecifier? BarestepSounds { get; private set; } = new SoundCollectionSpecifier("BarestepHard");
[DataField("friction")] public float Friction { get; set; } = 0.2f; /// <summary>
/// Base friction modifier for this tile.
/// </summary>
[DataField("friction")] public float Friction { get; set; } = 1f;
[DataField("variants")] public byte Variants { get; set; } = 1; [DataField("variants")] public byte Variants { get; set; } = 1;
@@ -91,12 +94,6 @@ namespace Content.Shared.Maps
[DataField("mobFriction")] [DataField("mobFriction")]
public float? MobFriction { get; private set; } public float? MobFriction { get; private set; }
/// <summary>
/// No-input friction override for mob mover in <see cref="SharedMoverController"/>
/// </summary>
[DataField("mobFrictionNoInput")]
public float? MobFrictionNoInput { get; private set; }
/// <summary> /// <summary>
/// Accel override for mob mover in <see cref="SharedMoverController"/> /// Accel override for mob mover in <see cref="SharedMoverController"/>
/// </summary> /// </summary>

View File

@@ -13,19 +13,19 @@ public sealed partial class FrictionContactsComponent : Component
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField, ViewVariables(VVAccess.ReadWrite)]
[AutoNetworkedField] [AutoNetworkedField]
public float MobFriction = 0.5f; public float MobFriction = 0.05f;
/// <summary> /// <summary>
/// Modified mob friction without input while on FrictionContactsComponent /// Modified mob friction without input while on FrictionContactsComponent
/// </summary> /// </summary>
[AutoNetworkedField] [AutoNetworkedField]
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField, ViewVariables(VVAccess.ReadWrite)]
public float MobFrictionNoInput = 0.05f; public float? MobFrictionNoInput = 0.05f;
/// <summary> /// <summary>
/// Modified mob acceleration while on FrictionContactsComponent /// Modified mob acceleration while on FrictionContactsComponent
/// </summary> /// </summary>
[AutoNetworkedField] [AutoNetworkedField]
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField, ViewVariables(VVAccess.ReadWrite)]
public float MobAcceleration = 2.0f; public float MobAcceleration = 0.1f;
} }

View File

@@ -29,12 +29,6 @@ namespace Content.Shared.Movement.Components
// (well maybe we do but the code is designed such that MoverSystem applies movement speed) // (well maybe we do but the code is designed such that MoverSystem applies movement speed)
// (and I'm not changing that) // (and I'm not changing that)
/// <summary>
/// Should our velocity be applied to our parent?
/// </summary>
[DataField]
public bool ToParent = false;
public GameTick LastInputTick; public GameTick LastInputTick;
public ushort LastInputSubTick; public ushort LastInputSubTick;

View File

@@ -7,6 +7,9 @@ namespace Content.Shared.Movement.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] [RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class JetpackComponent : Component public sealed partial class JetpackComponent : Component
{ {
[DataField, AutoNetworkedField]
public EntityUid? JetpackUser;
[ViewVariables(VVAccess.ReadWrite), DataField("moleUsage")] [ViewVariables(VVAccess.ReadWrite), DataField("moleUsage")]
public float MoleUsage = 0.012f; public float MoleUsage = 0.012f;
@@ -18,7 +21,7 @@ public sealed partial class JetpackComponent : Component
public float Acceleration = 1f; public float Acceleration = 1f;
[ViewVariables(VVAccess.ReadWrite), DataField("friction")] [ViewVariables(VVAccess.ReadWrite), DataField("friction")]
public float Friction = 0.3f; public float Friction = 0.25f; // same as off-grid friction
[ViewVariables(VVAccess.ReadWrite), DataField("weightlessModifier")] [ViewVariables(VVAccess.ReadWrite), DataField("weightlessModifier")]
public float WeightlessModifier = 1.2f; public float WeightlessModifier = 1.2f;

View File

@@ -8,6 +8,18 @@ namespace Content.Shared.Movement.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] [RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class JetpackUserComponent : Component public sealed partial class JetpackUserComponent : Component
{ {
[AutoNetworkedField] [DataField, AutoNetworkedField]
public EntityUid Jetpack; public EntityUid Jetpack;
[DataField, AutoNetworkedField]
public float WeightlessAcceleration;
[DataField, AutoNetworkedField]
public float WeightlessFriction;
[DataField, AutoNetworkedField]
public float WeightlessFrictionNoInput;
[DataField, AutoNetworkedField]
public float WeightlessModifier;
} }

View File

@@ -10,3 +10,8 @@ namespace Content.Shared.Movement.Components;
public sealed partial class SpeedModifiedByContactComponent : Component public sealed partial class SpeedModifiedByContactComponent : Component
{ {
} }
[NetworkedComponent, RegisterComponent] // ditto but for friction
public sealed partial class FrictionModifiedByContactComponent : Component
{
}

View File

@@ -10,6 +10,6 @@ public sealed partial class MovementRelayTargetComponent : Component
/// <summary> /// <summary>
/// The entity that is relaying to this entity. /// The entity that is relaying to this entity.
/// </summary> /// </summary>
[ViewVariables, AutoNetworkedField] [DataField, AutoNetworkedField]
public EntityUid Source; public EntityUid Source;
} }

View File

@@ -11,91 +11,146 @@ namespace Content.Shared.Movement.Components
[Access(typeof(MovementSpeedModifierSystem))] [Access(typeof(MovementSpeedModifierSystem))]
public sealed partial class MovementSpeedModifierComponent : Component public sealed partial class MovementSpeedModifierComponent : Component
{ {
// Weightless #region defaults
public const float DefaultMinimumFrictionSpeed = 0.005f;
// weightless
public const float DefaultWeightlessFriction = 1f; public const float DefaultWeightlessFriction = 1f;
public const float DefaultWeightlessFrictionNoInput = 0.2f;
public const float DefaultOffGridFriction = 0.05f;
public const float DefaultWeightlessModifier = 0.7f; public const float DefaultWeightlessModifier = 0.7f;
public const float DefaultWeightlessAcceleration = 1f; public const float DefaultWeightlessAcceleration = 1f;
// friction
public const float DefaultAcceleration = 20f; public const float DefaultAcceleration = 20f;
public const float DefaultFriction = 20f; public const float DefaultFriction = 2.5f;
public const float DefaultFrictionNoInput = 20f; public const float DefaultFrictionNoInput = 2.5f;
public const float DefaultMinimumFrictionSpeed = 0.005f;
// movement
public const float DefaultBaseWalkSpeed = 2.5f; public const float DefaultBaseWalkSpeed = 2.5f;
public const float DefaultBaseSprintSpeed = 4.5f; public const float DefaultBaseSprintSpeed = 4.5f;
#endregion
#region base values
/// <summary>
/// These base values should be defined in yaml and rarely if ever modified directly.
/// </summary>
[DataField, AutoNetworkedField]
public float BaseWalkSpeed = DefaultBaseWalkSpeed;
[DataField, AutoNetworkedField]
public float BaseSprintSpeed = DefaultBaseSprintSpeed;
/// <summary>
/// The acceleration applied to mobs when moving. If this is ever less than Friction the mob will be slower.
/// </summary>
[AutoNetworkedField, DataField]
public float BaseAcceleration = DefaultAcceleration;
/// <summary>
/// The body's base friction modifier that is applied in *all* circumstances.
/// </summary>
[AutoNetworkedField, DataField]
public float BaseFriction = DefaultFriction;
/// <summary>
/// Minimum speed a mob has to be moving before applying movement friction.
/// </summary>
[DataField]
public float MinimumFrictionSpeed = DefaultMinimumFrictionSpeed;
#endregion
#region calculated values
[ViewVariables]
public float CurrentWalkSpeed => WalkSpeedModifier * BaseWalkSpeed;
[ViewVariables]
public float CurrentSprintSpeed => SprintSpeedModifier * BaseSprintSpeed;
/// <summary>
/// The acceleration applied to mobs when moving. If this is ever less than Friction the mob will be slower.
/// </summary>
[AutoNetworkedField, DataField]
public float Acceleration;
/// <summary>
/// Modifier to the negative velocity applied for friction.
/// </summary>
[AutoNetworkedField, DataField]
public float Friction;
/// <summary>
/// The negative velocity applied for friction.
/// </summary>
[AutoNetworkedField, DataField]
public float FrictionNoInput;
#endregion
#region movement modifiers
[AutoNetworkedField, ViewVariables] [AutoNetworkedField, ViewVariables]
public float WalkSpeedModifier = 1.0f; public float WalkSpeedModifier = 1.0f;
[AutoNetworkedField, ViewVariables] [AutoNetworkedField, ViewVariables]
public float SprintSpeedModifier = 1.0f; public float SprintSpeedModifier = 1.0f;
/// <summary> #endregion
/// Minimum speed a mob has to be moving before applying movement friction.
/// </summary> #region Weightless
[ViewVariables(VVAccess.ReadWrite), DataField]
public float MinimumFrictionSpeed = DefaultMinimumFrictionSpeed;
/// <summary> /// <summary>
/// The negative velocity applied for friction when weightless and providing inputs. /// These base values should be defined in yaml and rarely if ever modified directly.
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField] [AutoNetworkedField, DataField]
public float WeightlessFriction = DefaultWeightlessFriction; public float BaseWeightlessFriction = DefaultWeightlessFriction;
/// <summary> [AutoNetworkedField, DataField]
/// The negative velocity applied for friction when weightless and not providing inputs. public float BaseWeightlessModifier = DefaultWeightlessModifier;
/// This is essentially how much their speed decreases per second.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField]
public float WeightlessFrictionNoInput = DefaultWeightlessFrictionNoInput;
/// <summary> [AutoNetworkedField, DataField]
/// The negative velocity applied for friction when weightless and not standing on a grid or mapgrid public float BaseWeightlessAcceleration = DefaultWeightlessAcceleration;
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField]
public float OffGridFriction = DefaultOffGridFriction;
/// <summary> /*
/// The movement speed modifier applied to a mob's total input velocity when weightless. * Final values
/// </summary> */
[ViewVariables(VVAccess.ReadWrite), DataField]
public float WeightlessModifier = DefaultWeightlessModifier; [ViewVariables]
public float WeightlessWalkSpeed => WeightlessModifier * BaseWalkSpeed;
[ViewVariables]
public float WeightlessSprintSpeed => WeightlessModifier * BaseSprintSpeed;
/// <summary> /// <summary>
/// The acceleration applied to mobs when moving and weightless. /// The acceleration applied to mobs when moving and weightless.
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField] [AutoNetworkedField, DataField]
public float WeightlessAcceleration = DefaultWeightlessAcceleration; public float WeightlessAcceleration;
/// <summary> /// <summary>
/// The acceleration applied to mobs when moving. /// The movement speed modifier applied to a mob's total input velocity when weightless.
/// </summary> /// </summary>
[AutoNetworkedField, ViewVariables(VVAccess.ReadWrite), DataField] [AutoNetworkedField, DataField]
public float Acceleration = DefaultAcceleration; public float WeightlessModifier;
/// <summary> /// <summary>
/// The negative velocity applied for friction. /// The negative velocity applied for friction when weightless and providing inputs.
/// </summary> /// </summary>
[AutoNetworkedField, ViewVariables(VVAccess.ReadWrite), DataField] [AutoNetworkedField, DataField]
public float Friction = DefaultFriction; public float WeightlessFriction;
/// <summary> /// <summary>
/// The negative velocity applied for friction. /// The negative velocity applied for friction when weightless and not providing inputs.
/// </summary> /// </summary>
[AutoNetworkedField, ViewVariables(VVAccess.ReadWrite), DataField] [AutoNetworkedField, DataField]
public float? FrictionNoInput; public float WeightlessFrictionNoInput;
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField] /// <summary>
public float BaseWalkSpeed { get; set; } = DefaultBaseWalkSpeed; /// The negative velocity applied for friction when weightless and not standing on a grid or mapgrid
/// </summary>
[AutoNetworkedField, DataField]
public float? OffGridFriction;
[ViewVariables(VVAccess.ReadWrite), DataField, AutoNetworkedField] #endregion
public float BaseSprintSpeed { get; set; } = DefaultBaseSprintSpeed;
[ViewVariables]
public float CurrentWalkSpeed => WalkSpeedModifier * BaseWalkSpeed;
[ViewVariables]
public float CurrentSprintSpeed => SprintSpeedModifier * BaseSprintSpeed;
} }
} }

View File

@@ -10,6 +10,6 @@ namespace Content.Shared.Movement.Components;
[Access(typeof(SharedMoverController))] [Access(typeof(SharedMoverController))]
public sealed partial class RelayInputMoverComponent : Component public sealed partial class RelayInputMoverComponent : Component
{ {
[ViewVariables, AutoNetworkedField] [DataField, AutoNetworkedField]
public EntityUid RelayEntity; public EntityUid RelayEntity;
} }

View File

@@ -11,38 +11,57 @@ public sealed class FrictionContactsSystem : EntitySystem
[Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!; [Dependency] private readonly MovementSpeedModifierSystem _speedModifierSystem = default!;
// Comment copied from "original" SlowContactsSystem.cs (now SpeedModifierContactsSystem.cs) // Comment copied from "original" SlowContactsSystem.cs (now SpeedModifierContactsSystem.cs)
// TODO full-game-save // TODO full-game-save
// Either these need to be processed before a map is saved, or slowed/slowing entities need to update on init. // Either these need to be processed before a map is saved, or slowed/slowing entities need to update on init.
private HashSet<EntityUid> _toUpdate = new(); private readonly HashSet<EntityUid> _toUpdate = new();
private readonly HashSet<EntityUid> _toRemove = new();
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<FrictionContactsComponent, StartCollideEvent>(OnEntityEnter); SubscribeLocalEvent<FrictionContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<FrictionContactsComponent, EndCollideEvent>(OnEntityExit); SubscribeLocalEvent<FrictionContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<FrictionModifiedByContactComponent, RefreshFrictionModifiersEvent>(OnRefreshFrictionModifiers);
SubscribeLocalEvent<FrictionContactsComponent, ComponentShutdown>(OnShutdown); SubscribeLocalEvent<FrictionContactsComponent, ComponentShutdown>(OnShutdown);
UpdatesAfter.Add(typeof(SharedPhysicsSystem)); UpdatesAfter.Add(typeof(SharedPhysicsSystem));
} }
private void OnEntityEnter(EntityUid uid, FrictionContactsComponent component, ref StartCollideEvent args) public override void Update(float frameTime)
{ {
var otherUid = args.OtherEntity; base.Update(frameTime);
if (!HasComp(otherUid, typeof(MovementSpeedModifierComponent))) _toRemove.Clear();
return;
_toUpdate.Add(otherUid); foreach (var ent in _toUpdate)
{
_speedModifierSystem.RefreshFrictionModifiers(ent);
}
foreach (var ent in _toRemove)
{
RemComp<FrictionModifiedByContactComponent>(ent);
}
_toUpdate.Clear();
} }
private void OnEntityExit(EntityUid uid, FrictionContactsComponent component, ref EndCollideEvent args) public void ChangeFrictionModifiers(EntityUid uid, float friction, FrictionContactsComponent? component = null)
{ {
var otherUid = args.OtherEntity; ChangeFrictionModifiers(uid, friction, null, null, component);
}
if (!HasComp(otherUid, typeof(MovementSpeedModifierComponent))) public void ChangeFrictionModifiers(EntityUid uid, float mobFriction, float? mobFrictionNoInput, float? acceleration, FrictionContactsComponent? component = null)
{
if (!Resolve(uid, ref component))
return; return;
_toUpdate.Add(otherUid); component.MobFriction = mobFriction;
component.MobFrictionNoInput = mobFrictionNoInput;
if (acceleration.HasValue)
component.MobAcceleration = acceleration.Value;
Dirty(uid, component);
_toUpdate.UnionWith(_physics.GetContactingEntities(uid));
} }
private void OnShutdown(EntityUid uid, FrictionContactsComponent component, ComponentShutdown args) private void OnShutdown(EntityUid uid, FrictionContactsComponent component, ComponentShutdown args)
@@ -50,51 +69,70 @@ public sealed class FrictionContactsSystem : EntitySystem
if (!TryComp(uid, out PhysicsComponent? phys)) if (!TryComp(uid, out PhysicsComponent? phys))
return; return;
// Note that the entity may not be getting deleted here. E.g., glue puddles.
_toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys)); _toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys));
} }
public override void Update(float frameTime) private void OnRefreshFrictionModifiers(Entity<FrictionModifiedByContactComponent> entity, ref RefreshFrictionModifiersEvent args)
{ {
base.Update(frameTime); if (!EntityManager.TryGetComponent<PhysicsComponent>(entity, out var physicsComponent))
foreach (var uid in _toUpdate)
{
ApplyFrictionChange(uid);
}
_toUpdate.Clear();
}
private void ApplyFrictionChange(EntityUid uid)
{
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
return; return;
if (!TryComp(uid, out MovementSpeedModifierComponent? speedModifier)) var friction = 0.0f;
return; var frictionNoInput = 0.0f;
var acceleration = 0.0f;
FrictionContactsComponent? frictionComponent = TouchesFrictionContactsComponent(uid, physicsComponent); var remove = true;
var entries = 0;
if (frictionComponent == null) foreach (var ent in _physics.GetContactingEntities(entity, physicsComponent))
{ {
_speedModifierSystem.ChangeFriction(uid, MovementSpeedModifierComponent.DefaultFriction, null, MovementSpeedModifierComponent.DefaultAcceleration, speedModifier); if (!TryComp<FrictionContactsComponent>(ent, out var contacts))
}
else
{
_speedModifierSystem.ChangeFriction(uid, frictionComponent.MobFriction, frictionComponent.MobFrictionNoInput, frictionComponent.MobAcceleration, speedModifier);
}
}
private FrictionContactsComponent? TouchesFrictionContactsComponent(EntityUid uid, PhysicsComponent physicsComponent)
{
foreach (var ent in _physics.GetContactingEntities(uid, physicsComponent))
{
if (!TryComp(ent, out FrictionContactsComponent? frictionContacts))
continue; continue;
friction += contacts.MobFriction;
return frictionContacts; frictionNoInput += contacts.MobFrictionNoInput ?? contacts.MobFriction;
acceleration += contacts.MobAcceleration;
remove = false;
entries++;
} }
return null; if (entries > 0)
{
if (!MathHelper.CloseTo(friction, entries) || !MathHelper.CloseTo(frictionNoInput, entries))
{
friction /= entries;
frictionNoInput /= entries;
args.ModifyFriction(friction, frictionNoInput);
}
if (!MathHelper.CloseTo(acceleration, entries))
{
acceleration /= entries;
args.ModifyAcceleration(acceleration);
}
}
// no longer colliding with anything
if (remove)
_toRemove.Add(entity);
}
private void OnEntityExit(EntityUid uid, FrictionContactsComponent component, ref EndCollideEvent args)
{
var otherUid = args.OtherEntity;
_toUpdate.Add(otherUid);
}
private void OnEntityEnter(EntityUid uid, FrictionContactsComponent component, ref StartCollideEvent args)
{
AddModifiedEntity(args.OtherEntity);
}
public void AddModifiedEntity(EntityUid uid)
{
if (!HasComp<MovementSpeedModifierComponent>(uid))
return;
EnsureComp<FrictionModifiedByContactComponent>(uid);
_toUpdate.Add(uid);
} }
} }

View File

@@ -1,5 +1,9 @@
using System.Text.Json.Serialization.Metadata;
using Content.Shared.CCVar;
using Content.Shared.Inventory; using Content.Shared.Inventory;
using Content.Shared.Movement.Components; using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events;
using Robust.Shared.Configuration;
using Robust.Shared.Timing; using Robust.Shared.Timing;
namespace Content.Shared.Movement.Systems namespace Content.Shared.Movement.Systems
@@ -7,6 +11,71 @@ namespace Content.Shared.Movement.Systems
public sealed class MovementSpeedModifierSystem : EntitySystem public sealed class MovementSpeedModifierSystem : EntitySystem
{ {
[Dependency] private readonly IGameTiming _timing = default!; [Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IConfigurationManager _configManager = default!;
private float _frictionModifier;
private float _airDamping;
private float _offGridDamping;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MovementSpeedModifierComponent, MapInitEvent>(OnModMapInit);
Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true);
Subs.CVar(_configManager, CCVars.AirFriction, value => _airDamping = value, true);
Subs.CVar(_configManager, CCVars.OffgridFriction, value => _offGridDamping = value, true);
}
private void OnModMapInit(Entity<MovementSpeedModifierComponent> ent, ref MapInitEvent args)
{
// TODO: Dirty these smarter.
ent.Comp.WeightlessAcceleration = ent.Comp.BaseWeightlessAcceleration;
ent.Comp.WeightlessModifier = ent.Comp.BaseWeightlessModifier;
ent.Comp.WeightlessFriction = _airDamping * ent.Comp.BaseWeightlessFriction;
ent.Comp.WeightlessFrictionNoInput = _airDamping * ent.Comp.BaseWeightlessFriction;
ent.Comp.OffGridFriction = _offGridDamping * ent.Comp.BaseWeightlessFriction;
ent.Comp.Acceleration = ent.Comp.BaseAcceleration;
ent.Comp.Friction = _frictionModifier * ent.Comp.BaseFriction;
ent.Comp.FrictionNoInput = _frictionModifier * ent.Comp.BaseFriction;
Dirty(ent);
}
public void RefreshWeightlessModifiers(EntityUid uid, MovementSpeedModifierComponent? move = null)
{
if (!Resolve(uid, ref move, false))
return;
if (_timing.ApplyingState)
return;
var ev = new RefreshWeightlessModifiersEvent()
{
WeightlessAcceleration = move.BaseWeightlessAcceleration,
WeightlessAccelerationMod = 1.0f,
WeightlessModifier = move.BaseWeightlessModifier,
WeightlessFriction = move.BaseWeightlessFriction,
WeightlessFrictionMod = 1.0f,
WeightlessFrictionNoInput = move.BaseWeightlessFriction,
WeightlessFrictionNoInputMod = 1.0f,
};
RaiseLocalEvent(uid, ref ev);
if (MathHelper.CloseTo(ev.WeightlessAcceleration, move.WeightlessAcceleration) &&
MathHelper.CloseTo(ev.WeightlessModifier, move.WeightlessModifier) &&
MathHelper.CloseTo(ev.WeightlessFriction, move.WeightlessFriction) &&
MathHelper.CloseTo(ev.WeightlessFrictionNoInput, move.WeightlessFrictionNoInput))
{
return;
}
move.WeightlessAcceleration = ev.WeightlessAcceleration * ev.WeightlessAccelerationMod;
move.WeightlessModifier = ev.WeightlessModifier;
move.WeightlessFriction = _airDamping * ev.WeightlessFriction * ev.WeightlessFrictionMod;
move.WeightlessFrictionNoInput = _airDamping * ev.WeightlessFrictionNoInput * ev.WeightlessFrictionNoInputMod;
Dirty(uid, move);
}
public void RefreshMovementSpeedModifiers(EntityUid uid, MovementSpeedModifierComponent? move = null) public void RefreshMovementSpeedModifiers(EntityUid uid, MovementSpeedModifierComponent? move = null)
{ {
@@ -39,15 +108,42 @@ namespace Content.Shared.Movement.Systems
Dirty(uid, move); Dirty(uid, move);
} }
// We might want to create separate RefreshMovementFrictionModifiersEvent and RefreshMovementFrictionModifiers function that will call it public void RefreshFrictionModifiers(EntityUid uid, MovementSpeedModifierComponent? move = null)
public void ChangeFriction(EntityUid uid, float friction, float? frictionNoInput, float acceleration, MovementSpeedModifierComponent? move = null)
{ {
if (!Resolve(uid, ref move, false)) if (!Resolve(uid, ref move, false))
return; return;
move.Friction = friction; if (_timing.ApplyingState)
return;
var ev = new RefreshFrictionModifiersEvent()
{
Friction = move.BaseFriction,
FrictionNoInput = move.BaseFriction,
Acceleration = move.BaseAcceleration,
};
RaiseLocalEvent(uid, ref ev);
if (MathHelper.CloseTo(ev.Friction, move.Friction)
&& MathHelper.CloseTo(ev.FrictionNoInput, move.FrictionNoInput)
&& MathHelper.CloseTo(ev.Acceleration, move.Acceleration))
return;
move.Friction = _frictionModifier * ev.Friction;
move.FrictionNoInput = _frictionModifier * ev.FrictionNoInput;
move.Acceleration = ev.Acceleration;
Dirty(uid, move);
}
public void ChangeBaseFriction(EntityUid uid, float friction, float frictionNoInput, float acceleration, MovementSpeedModifierComponent? move = null)
{
if (!Resolve(uid, ref move, false))
return;
move.BaseFriction = friction;
move.FrictionNoInput = frictionNoInput; move.FrictionNoInput = frictionNoInput;
move.Acceleration = acceleration; move.BaseAcceleration = acceleration;
Dirty(uid, move); Dirty(uid, move);
} }
} }
@@ -75,4 +171,65 @@ namespace Content.Shared.Movement.Systems
ModifySpeed(mod, mod); ModifySpeed(mod, mod);
} }
} }
[ByRefEvent]
public record struct RefreshWeightlessModifiersEvent
{
public float WeightlessAcceleration;
public float WeightlessAccelerationMod;
public float WeightlessModifier;
public float WeightlessFriction;
public float WeightlessFrictionMod;
public float WeightlessFrictionNoInput;
public float WeightlessFrictionNoInputMod;
public void ModifyFriction(float friction, float noInput)
{
WeightlessFrictionMod *= friction;
WeightlessFrictionNoInput *= noInput;
}
public void ModifyFriction(float friction)
{
ModifyFriction(friction, friction);
}
public void ModifyAcceleration(float acceleration, float modifier)
{
WeightlessAcceleration *= acceleration;
WeightlessModifier *= modifier;
}
public void ModifyAcceleration(float modifier)
{
ModifyAcceleration(modifier, modifier);
}
}
[ByRefEvent]
public record struct RefreshFrictionModifiersEvent : IInventoryRelayEvent
{
public float Friction;
public float FrictionNoInput;
public float Acceleration;
public void ModifyFriction(float friction, float noInput)
{
Friction *= friction;
FrictionNoInput *= noInput;
}
public void ModifyFriction(float friction)
{
ModifyFriction(friction, friction);
}
public void ModifyAcceleration(float acceleration)
{
Acceleration *= acceleration;
}
SlotFlags IInventoryRelayEvent.TargetSlots => ~SlotFlags.POCKET;
}
} }

View File

@@ -16,7 +16,6 @@ public abstract class SharedJetpackSystem : EntitySystem
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!; [Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!;
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!; [Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] protected readonly SharedContainerSystem Container = default!; [Dependency] protected readonly SharedContainerSystem Container = default!;
[Dependency] private readonly SharedMoverController _mover = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!; [Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly ActionContainerSystem _actionContainer = default!; [Dependency] private readonly ActionContainerSystem _actionContainer = default!;
@@ -27,26 +26,31 @@ public abstract class SharedJetpackSystem : EntitySystem
SubscribeLocalEvent<JetpackComponent, GetItemActionsEvent>(OnJetpackGetAction); SubscribeLocalEvent<JetpackComponent, GetItemActionsEvent>(OnJetpackGetAction);
SubscribeLocalEvent<JetpackComponent, DroppedEvent>(OnJetpackDropped); SubscribeLocalEvent<JetpackComponent, DroppedEvent>(OnJetpackDropped);
SubscribeLocalEvent<JetpackComponent, ToggleJetpackEvent>(OnJetpackToggle); SubscribeLocalEvent<JetpackComponent, ToggleJetpackEvent>(OnJetpackToggle);
SubscribeLocalEvent<JetpackComponent, CanWeightlessMoveEvent>(OnJetpackCanWeightlessMove);
SubscribeLocalEvent<JetpackUserComponent, RefreshWeightlessModifiersEvent>(OnJetpackUserWeightlessMovement);
SubscribeLocalEvent<JetpackUserComponent, CanWeightlessMoveEvent>(OnJetpackUserCanWeightless); SubscribeLocalEvent<JetpackUserComponent, CanWeightlessMoveEvent>(OnJetpackUserCanWeightless);
SubscribeLocalEvent<JetpackUserComponent, EntParentChangedMessage>(OnJetpackUserEntParentChanged); SubscribeLocalEvent<JetpackUserComponent, EntParentChangedMessage>(OnJetpackUserEntParentChanged);
SubscribeLocalEvent<JetpackComponent, EntGotInsertedIntoContainerMessage>(OnJetpackMoved);
SubscribeLocalEvent<GravityChangedEvent>(OnJetpackUserGravityChanged); SubscribeLocalEvent<GravityChangedEvent>(OnJetpackUserGravityChanged);
SubscribeLocalEvent<JetpackComponent, MapInitEvent>(OnMapInit); SubscribeLocalEvent<JetpackComponent, MapInitEvent>(OnMapInit);
} }
private void OnJetpackUserWeightlessMovement(Entity<JetpackUserComponent> ent, ref RefreshWeightlessModifiersEvent args)
{
// Yes this bulldozes the values but primarily for backwards compat atm.
args.WeightlessAcceleration = ent.Comp.WeightlessAcceleration;
args.WeightlessModifier = ent.Comp.WeightlessModifier;
args.WeightlessFriction = ent.Comp.WeightlessFriction;
args.WeightlessFrictionNoInput = ent.Comp.WeightlessFrictionNoInput;
}
private void OnMapInit(EntityUid uid, JetpackComponent component, MapInitEvent args) private void OnMapInit(EntityUid uid, JetpackComponent component, MapInitEvent args)
{ {
_actionContainer.EnsureAction(uid, ref component.ToggleActionEntity, component.ToggleAction); _actionContainer.EnsureAction(uid, ref component.ToggleActionEntity, component.ToggleAction);
Dirty(uid, component); Dirty(uid, component);
} }
private void OnJetpackCanWeightlessMove(EntityUid uid, JetpackComponent component, ref CanWeightlessMoveEvent args)
{
args.CanMove = true;
}
private void OnJetpackUserGravityChanged(ref GravityChangedEvent ev) private void OnJetpackUserGravityChanged(ref GravityChangedEvent ev)
{ {
var gridUid = ev.ChangedGridIndex; var gridUid = ev.ChangedGridIndex;
@@ -70,6 +74,12 @@ public abstract class SharedJetpackSystem : EntitySystem
SetEnabled(uid, component, false, args.User); SetEnabled(uid, component, false, args.User);
} }
private void OnJetpackMoved(Entity<JetpackComponent> ent, ref EntGotInsertedIntoContainerMessage args)
{
if (args.Container.Owner != ent.Comp.JetpackUser)
SetEnabled(ent, ent.Comp, false, ent.Comp.JetpackUser);
}
private void OnJetpackUserCanWeightless(EntityUid uid, JetpackUserComponent component, ref CanWeightlessMoveEvent args) private void OnJetpackUserCanWeightless(EntityUid uid, JetpackUserComponent component, ref CanWeightlessMoveEvent args)
{ {
args.CanMove = true; args.CanMove = true;
@@ -86,26 +96,33 @@ public abstract class SharedJetpackSystem : EntitySystem
} }
} }
private void SetupUser(EntityUid user, EntityUid jetpackUid) private void SetupUser(EntityUid user, EntityUid jetpackUid, JetpackComponent component)
{ {
var userComp = EnsureComp<JetpackUserComponent>(user); EnsureComp<JetpackUserComponent>(user, out var userComp);
_mover.SetRelay(user, jetpackUid); component.JetpackUser = user;
if (TryComp<PhysicsComponent>(user, out var physics)) if (TryComp<PhysicsComponent>(user, out var physics))
_physics.SetBodyStatus(user, physics, BodyStatus.InAir); _physics.SetBodyStatus(user, physics, BodyStatus.InAir);
userComp.Jetpack = jetpackUid; userComp.Jetpack = jetpackUid;
userComp.WeightlessAcceleration = component.Acceleration;
userComp.WeightlessModifier = component.WeightlessModifier;
userComp.WeightlessFriction = component.Friction;
userComp.WeightlessFrictionNoInput = component.Friction;
_movementSpeedModifier.RefreshWeightlessModifiers(user);
} }
private void RemoveUser(EntityUid uid) private void RemoveUser(EntityUid uid, JetpackComponent component)
{ {
if (!RemComp<JetpackUserComponent>(uid)) if (!RemComp<JetpackUserComponent>(uid))
return; return;
component.JetpackUser = null;
if (TryComp<PhysicsComponent>(uid, out var physics)) if (TryComp<PhysicsComponent>(uid, out var physics))
_physics.SetBodyStatus(uid, physics, BodyStatus.OnGround); _physics.SetBodyStatus(uid, physics, BodyStatus.OnGround);
RemComp<RelayInputMoverComponent>(uid); _movementSpeedModifier.RefreshWeightlessModifiers(uid);
} }
private void OnJetpackToggle(EntityUid uid, JetpackComponent component, ToggleJetpackEvent args) private void OnJetpackToggle(EntityUid uid, JetpackComponent component, ToggleJetpackEvent args)
@@ -145,42 +162,26 @@ public abstract class SharedJetpackSystem : EntitySystem
{ {
if (IsEnabled(uid) == enabled || if (IsEnabled(uid) == enabled ||
enabled && !CanEnable(uid, component)) enabled && !CanEnable(uid, component))
{
return; return;
if (user == null)
{
if (!Container.TryGetContainingContainer((uid, null, null), out var container))
return;
user = container.Owner;
} }
if (enabled) if (enabled)
{ {
SetupUser(user.Value, uid, component);
EnsureComp<ActiveJetpackComponent>(uid); EnsureComp<ActiveJetpackComponent>(uid);
} }
else else
{ {
RemoveUser(user.Value, component);
RemComp<ActiveJetpackComponent>(uid); RemComp<ActiveJetpackComponent>(uid);
} }
if (user == null)
{
Container.TryGetContainingContainer((uid, null, null), out var container);
user = container?.Owner;
}
// Can't activate if no one's using.
if (user == null && enabled)
return;
if (user != null)
{
if (enabled)
{
SetupUser(user.Value, uid);
}
else
{
RemoveUser(user.Value);
}
_movementSpeedModifier.RefreshMovementSpeedModifiers(user.Value);
}
Appearance.SetData(uid, JetpackVisuals.Enabled, enabled); Appearance.SetData(uid, JetpackVisuals.Enabled, enabled);
Dirty(uid, component); Dirty(uid, component);

View File

@@ -169,7 +169,7 @@ namespace Content.Shared.Movement.Systems
} }
// If we updated parent then cancel the accumulator and force it now. // If we updated parent then cancel the accumulator and force it now.
if (!TryUpdateRelative(mover, XformQuery.GetComponent(uid)) && mover.TargetRelativeRotation.Equals(Angle.Zero)) if (!TryUpdateRelative(uid, mover, XformQuery.GetComponent(uid)) && mover.TargetRelativeRotation.Equals(Angle.Zero))
return; return;
mover.LerpTarget = TimeSpan.Zero; mover.LerpTarget = TimeSpan.Zero;
@@ -177,7 +177,7 @@ namespace Content.Shared.Movement.Systems
Dirty(uid, mover); Dirty(uid, mover);
} }
private bool TryUpdateRelative(InputMoverComponent mover, TransformComponent xform) private bool TryUpdateRelative(EntityUid uid, InputMoverComponent mover, TransformComponent xform)
{ {
var relative = xform.GridUid; var relative = xform.GridUid;
relative ??= xform.MapUid; relative ??= xform.MapUid;
@@ -192,38 +192,42 @@ namespace Content.Shared.Movement.Systems
// Okay need to get our old relative rotation with respect to our new relative rotation // Okay need to get our old relative rotation with respect to our new relative rotation
// e.g. if we were right side up on our current grid need to get what that is on our new grid. // e.g. if we were right side up on our current grid need to get what that is on our new grid.
var currentRotation = Angle.Zero; var oldRelativeRot = Angle.Zero;
var targetRotation = Angle.Zero; var relativeRot = Angle.Zero;
// Get our current relative rotation // Get our current relative rotation
if (XformQuery.TryGetComponent(mover.RelativeEntity, out var oldRelativeXform)) if (XformQuery.TryGetComponent(mover.RelativeEntity, out var oldRelativeXform))
{ {
currentRotation = _transform.GetWorldRotation(oldRelativeXform, XformQuery) + mover.RelativeRotation; oldRelativeRot = _transform.GetWorldRotation(oldRelativeXform);
} }
if (XformQuery.TryGetComponent(relative, out var relativeXform)) if (XformQuery.TryGetComponent(relative, out var relativeXform))
{ {
// This is our current rotation relative to our new parent. // This is our current rotation relative to our new parent.
mover.RelativeRotation = (currentRotation - _transform.GetWorldRotation(relativeXform)).FlipPositive(); relativeRot = _transform.GetWorldRotation(relativeXform);
} }
// If we went from grid -> map we'll preserve our worldrotation var diff = relativeRot - oldRelativeRot;
if (relative != null && HasComp<MapComponent>(relative.Value))
// If we're going from a grid -> map then preserve the relative rotation so it's seamless if they go into space and back.
if (HasComp<MapComponent>(relative) && HasComp<MapGridComponent>(mover.RelativeEntity))
{ {
targetRotation = currentRotation.FlipPositive().Reduced(); mover.TargetRelativeRotation -= diff;
} }
// If we went from grid -> grid OR grid -> map then snap the target to cardinal and lerp there. // Snap to nearest cardinal if map -> grid
// OR just rotate to zero (depending on cvar) else if (HasComp<MapGridComponent>(relative) && HasComp<MapComponent>(mover.RelativeEntity))
else if (relative != null && MapGridQuery.HasComp(relative.Value))
{ {
if (CameraRotationLocked) var targetDir = mover.TargetRelativeRotation - diff;
targetRotation = Angle.Zero; targetDir = targetDir.GetCardinalDir().ToAngle().Reduced();
else mover.TargetRelativeRotation = targetDir;
targetRotation = mover.RelativeRotation.GetCardinalDir().ToAngle().Reduced();
} }
// Preserve target rotation in relation to the new parent.
// Regardless of what the target is don't want the eye to move at all (from the player's perspective).
mover.RelativeRotation -= diff;
mover.RelativeEntity = relative; mover.RelativeEntity = relative;
mover.TargetRelativeRotation = targetRotation; Dirty(uid, mover);
return true; return true;
} }
@@ -297,6 +301,7 @@ namespace Content.Shared.Movement.Systems
// Relayed movement just uses the same keybinds given we're moving the relayed entity // Relayed movement just uses the same keybinds given we're moving the relayed entity
// the same as us. // the same as us.
// TODO: Should move this into HandleMobMovement itself.
if (TryComp<RelayInputMoverComponent>(entity, out var relayMover)) if (TryComp<RelayInputMoverComponent>(entity, out var relayMover))
{ {
DebugTools.Assert(relayMover.RelayEntity != entity); DebugTools.Assert(relayMover.RelayEntity != entity);

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Numerics; using System.Numerics;
using Content.Shared.Bed.Sleep; using Content.Shared.Bed.Sleep;
using Content.Shared.CCVar; using Content.Shared.CCVar;
@@ -21,6 +22,7 @@ using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Controllers; using Robust.Shared.Physics.Controllers;
using Robust.Shared.Physics.Systems; using Robust.Shared.Physics.Systems;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager.Exceptions;
using Robust.Shared.Timing; using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using PullableComponent = Content.Shared.Movement.Pulling.Components.PullableComponent; using PullableComponent = Content.Shared.Movement.Pulling.Components.PullableComponent;
@@ -61,12 +63,10 @@ public abstract partial class SharedMoverController : VirtualController
private static readonly ProtoId<TagPrototype> FootstepSoundTag = "FootstepSound"; private static readonly ProtoId<TagPrototype> FootstepSoundTag = "FootstepSound";
/// <summary>
/// <see cref="CCVars.StopSpeed"/>
/// </summary>
private float _stopSpeed;
private bool _relativeMovement; private bool _relativeMovement;
private float _minDamping;
private float _airDamping;
private float _offGridDamping;
/// <summary> /// <summary>
/// Cache the mob movement calculation to re-use elsewhere. /// Cache the mob movement calculation to re-use elsewhere.
@@ -90,10 +90,14 @@ public abstract partial class SharedMoverController : VirtualController
FootstepModifierQuery = GetEntityQuery<FootstepModifierComponent>(); FootstepModifierQuery = GetEntityQuery<FootstepModifierComponent>();
MapGridQuery = GetEntityQuery<MapGridComponent>(); MapGridQuery = GetEntityQuery<MapGridComponent>();
SubscribeLocalEvent<MovementSpeedModifierComponent, TileFrictionEvent>(OnTileFriction);
InitializeInput(); InitializeInput();
InitializeRelay(); InitializeRelay();
Subs.CVar(_configManager, CCVars.RelativeMovement, value => _relativeMovement = value, true); Subs.CVar(_configManager, CCVars.RelativeMovement, value => _relativeMovement = value, true);
Subs.CVar(_configManager, CCVars.StopSpeed, value => _stopSpeed = value, true); Subs.CVar(_configManager, CCVars.MinFriction, value => _minDamping = value, true);
Subs.CVar(_configManager, CCVars.AirFriction, value => _airDamping = value, true);
Subs.CVar(_configManager, CCVars.OffgridFriction, value => _offGridDamping = value, true);
UpdatesBefore.Add(typeof(TileFrictionController)); UpdatesBefore.Add(typeof(TileFrictionController));
} }
@@ -121,157 +125,213 @@ public abstract partial class SharedMoverController : VirtualController
/// Movement while considering actionblockers, weightlessness, etc. /// Movement while considering actionblockers, weightlessness, etc.
/// </summary> /// </summary>
protected void HandleMobMovement( protected void HandleMobMovement(
EntityUid uid, Entity<InputMoverComponent> entity,
InputMoverComponent mover,
EntityUid physicsUid,
PhysicsComponent physicsComponent,
TransformComponent xform,
float frameTime) float frameTime)
{ {
var canMove = mover.CanMove; var uid = entity.Owner;
if (RelayTargetQuery.TryGetComponent(uid, out var relayTarget)) var mover = entity.Comp;
// If we're a relay then apply all of our data to the parent instead and go next.
if (RelayQuery.TryComp(uid, out var relay))
{ {
if (_mobState.IsIncapacitated(relayTarget.Source) || if (!MoverQuery.TryComp(relay.RelayEntity, out var relayTargetMover))
TryComp<SleepingComponent>(relayTarget.Source, out _) || return;
!MoverQuery.TryGetComponent(relayTarget.Source, out var relayedMover))
// Always lerp rotation so relay entities aren't cooked.
LerpRotation(uid, mover, frameTime);
var dirtied = false;
if (relayTargetMover.RelativeEntity != mover.RelativeEntity)
{ {
canMove = false; relayTargetMover.RelativeEntity = mover.RelativeEntity;
dirtied = true;
} }
else
if (relayTargetMover.RelativeRotation != mover.RelativeRotation)
{ {
mover.RelativeEntity = relayedMover.RelativeEntity; relayTargetMover.RelativeRotation = mover.RelativeRotation;
mover.RelativeRotation = relayedMover.RelativeRotation; dirtied = true;
mover.TargetRelativeRotation = relayedMover.TargetRelativeRotation;
} }
if (relayTargetMover.TargetRelativeRotation != mover.TargetRelativeRotation)
{
relayTargetMover.TargetRelativeRotation = mover.TargetRelativeRotation;
dirtied = true;
}
if (relayTargetMover.CanMove != mover.CanMove)
{
relayTargetMover.CanMove = mover.CanMove;
dirtied = true;
}
if (dirtied)
{
Dirty(relay.RelayEntity, relayTargetMover);
}
return;
} }
// Update relative movement if (!XformQuery.TryComp(entity.Owner, out var xform))
if (mover.LerpTarget < Timing.CurTime) return;
RelayTargetQuery.TryComp(uid, out var relayTarget);
var relaySource = relayTarget?.Source;
// If we're not the target of a relay then handle lerp data.
if (relaySource == null)
{ {
if (TryUpdateRelative(mover, xform)) // Update relative movement
if (mover.LerpTarget < Timing.CurTime)
{ {
Dirty(uid, mover); TryUpdateRelative(uid, mover, xform);
} }
LerpRotation(uid, mover, frameTime);
} }
LerpRotation(uid, mover, frameTime); // If we can't move then just use tile-friction / no movement handling.
if (!mover.CanMove
if (!canMove || !PhysicsQuery.TryComp(uid, out var physicsComponent)
|| physicsComponent.BodyStatus != BodyStatus.OnGround && !CanMoveInAirQuery.HasComponent(uid)
|| PullableQuery.TryGetComponent(uid, out var pullable) && pullable.BeingPulled) || PullableQuery.TryGetComponent(uid, out var pullable) && pullable.BeingPulled)
{ {
UsedMobMovement[uid] = false; UsedMobMovement[uid] = false;
return; return;
} }
UsedMobMovement[uid] = true; // If the body is in air but isn't weightless then it can't move
// Specifically don't use mover.Owner because that may be different to the actual physics body being moved. // TODO: MAKE ISWEIGHTLESS EVENT BASED
var weightless = _gravity.IsWeightless(physicsUid, physicsComponent, xform); var weightless = _gravity.IsWeightless(uid, physicsComponent, xform);
var (walkDir, sprintDir) = GetVelocityInput(mover); var inAirHelpless = false;
var touching = false;
// Handle wall-pushes. if (physicsComponent.BodyStatus != BodyStatus.OnGround && !CanMoveInAirQuery.HasComponent(uid))
if (weightless)
{ {
if (xform.GridUid != null) if (!weightless)
touching = true;
if (!touching)
{ {
var ev = new CanWeightlessMoveEvent(uid); UsedMobMovement[uid] = false;
RaiseLocalEvent(uid, ref ev, true); return;
// No gravity: is our entity touching anything?
touching = ev.CanMove;
if (!touching && TryComp<MobMoverComponent>(uid, out var mobMover))
touching |= IsAroundCollider(PhysicsSystem, xform, mobMover, physicsUid, physicsComponent);
} }
inAirHelpless = true;
} }
UsedMobMovement[uid] = true;
var moveSpeedComponent = ModifierQuery.CompOrNull(uid);
float friction;
float accel;
Vector2 wishDir;
var velocity = physicsComponent.LinearVelocity;
// Get current tile def for things like speed/friction mods // Get current tile def for things like speed/friction mods
ContentTileDefinition? tileDef = null; ContentTileDefinition? tileDef = null;
// Don't bother getting the tiledef here if we're weightless or in-air var touching = false;
// since no tile-based modifiers should be applying in that situation // Whether we use tilefriction or not
if (MapGridQuery.TryComp(xform.GridUid, out var gridComp) if (weightless || inAirHelpless)
&& _mapSystem.TryGetTileRef(xform.GridUid.Value, gridComp, xform.Coordinates, out var tile)
&& !(weightless || physicsComponent.BodyStatus == BodyStatus.InAir))
{ {
tileDef = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId]; // Find the speed we should be moving at and make sure we're not trying to move faster than that
} var walkSpeed = moveSpeedComponent?.WeightlessWalkSpeed ?? MovementSpeedModifierComponent.DefaultBaseWalkSpeed;
var sprintSpeed = moveSpeedComponent?.WeightlessSprintSpeed ?? MovementSpeedModifierComponent.DefaultBaseSprintSpeed;
// Regular movement. wishDir = AssertValidWish(mover, walkSpeed, sprintSpeed);
// Target velocity.
// This is relative to the map / grid we're on.
var moveSpeedComponent = ModifierQuery.CompOrNull(uid);
var walkSpeed = moveSpeedComponent?.CurrentWalkSpeed ?? MovementSpeedModifierComponent.DefaultBaseWalkSpeed; var ev = new CanWeightlessMoveEvent(uid);
var sprintSpeed = moveSpeedComponent?.CurrentSprintSpeed ?? MovementSpeedModifierComponent.DefaultBaseSprintSpeed; RaiseLocalEvent(uid, ref ev, true);
var total = walkDir * walkSpeed + sprintDir * sprintSpeed; touching = ev.CanMove || xform.GridUid != null || MapGridQuery.HasComp(xform.GridUid);
var parentRotation = GetParentGridAngle(mover); // If we're not on a grid, and not able to move in space check if we're close enough to a grid to touch.
var wishDir = _relativeMovement ? parentRotation.RotateVec(total) : total; if (!touching && MobMoverQuery.TryComp(uid, out var mobMover))
touching |= IsAroundCollider(PhysicsSystem, xform, mobMover, uid, physicsComponent);
DebugTools.Assert(MathHelper.CloseToPercent(total.Length(), wishDir.Length())); // If we're touching then use the weightless values
if (touching)
float friction; {
float weightlessModifier; touching = true;
float accel; if (wishDir != Vector2.Zero)
var velocity = physicsComponent.LinearVelocity; friction = moveSpeedComponent?.WeightlessFriction ?? _airDamping;
else
// Whether we use weightless friction or not. friction = moveSpeedComponent?.WeightlessFrictionNoInput ?? _airDamping;
if (weightless) }
{ // Otherwise use the off-grid values.
if (gridComp == null && !MapGridQuery.HasComp(xform.GridUid))
friction = moveSpeedComponent?.OffGridFriction ?? MovementSpeedModifierComponent.DefaultOffGridFriction;
else if (wishDir != Vector2.Zero && touching)
friction = moveSpeedComponent?.WeightlessFriction ?? MovementSpeedModifierComponent.DefaultWeightlessFriction;
else else
friction = moveSpeedComponent?.WeightlessFrictionNoInput ?? MovementSpeedModifierComponent.DefaultWeightlessFrictionNoInput; {
friction = moveSpeedComponent?.OffGridFriction ?? _offGridDamping;
}
weightlessModifier = moveSpeedComponent?.WeightlessModifier ?? MovementSpeedModifierComponent.DefaultWeightlessModifier;
accel = moveSpeedComponent?.WeightlessAcceleration ?? MovementSpeedModifierComponent.DefaultWeightlessAcceleration; accel = moveSpeedComponent?.WeightlessAcceleration ?? MovementSpeedModifierComponent.DefaultWeightlessAcceleration;
} }
else else
{ {
if (wishDir != Vector2.Zero || moveSpeedComponent?.FrictionNoInput == null) if (MapGridQuery.TryComp(xform.GridUid, out var gridComp)
&& _mapSystem.TryGetTileRef(xform.GridUid.Value, gridComp, xform.Coordinates, out var tile))
tileDef = (ContentTileDefinition) _tileDefinitionManager[tile.Tile.TypeId];
var walkSpeed = moveSpeedComponent?.CurrentWalkSpeed ?? MovementSpeedModifierComponent.DefaultBaseWalkSpeed;
var sprintSpeed = moveSpeedComponent?.CurrentSprintSpeed ?? MovementSpeedModifierComponent.DefaultBaseSprintSpeed;
wishDir = AssertValidWish(mover, walkSpeed, sprintSpeed);
if (wishDir != Vector2.Zero)
{ {
friction = tileDef?.MobFriction ?? moveSpeedComponent?.Friction ?? MovementSpeedModifierComponent.DefaultFriction; friction = moveSpeedComponent?.Friction ?? MovementSpeedModifierComponent.DefaultFriction;
friction *= tileDef?.MobFriction ?? tileDef?.Friction ?? 1f;
} }
else else
{ {
friction = tileDef?.MobFrictionNoInput ?? moveSpeedComponent.FrictionNoInput ?? MovementSpeedModifierComponent.DefaultFrictionNoInput; friction = moveSpeedComponent?.FrictionNoInput ?? MovementSpeedModifierComponent.DefaultFrictionNoInput;
friction *= tileDef?.Friction ?? 1f;
} }
weightlessModifier = 1f; accel = moveSpeedComponent?.Acceleration ?? MovementSpeedModifierComponent.DefaultAcceleration;
accel = tileDef?.MobAcceleration ?? moveSpeedComponent?.Acceleration ?? MovementSpeedModifierComponent.DefaultAcceleration; accel *= tileDef?.MobAcceleration ?? 1f;
} }
// This way friction never exceeds acceleration when you're trying to move.
// If you want to slow down an entity with "friction" you shouldn't be using this system.
if (wishDir != Vector2.Zero)
friction = Math.Min(friction, accel);
friction = Math.Max(friction, _minDamping);
var minimumFrictionSpeed = moveSpeedComponent?.MinimumFrictionSpeed ?? MovementSpeedModifierComponent.DefaultMinimumFrictionSpeed; var minimumFrictionSpeed = moveSpeedComponent?.MinimumFrictionSpeed ?? MovementSpeedModifierComponent.DefaultMinimumFrictionSpeed;
Friction(minimumFrictionSpeed, frameTime, friction, ref velocity); Friction(minimumFrictionSpeed, frameTime, friction, ref velocity);
wishDir *= weightlessModifier;
if (!weightless || touching) if (!weightless || touching)
Accelerate(ref velocity, in wishDir, accel, frameTime); Accelerate(ref velocity, in wishDir, accel, frameTime);
SetWishDir((uid, mover), wishDir); SetWishDir((uid, mover), wishDir);
PhysicsSystem.SetLinearVelocity(physicsUid, velocity, body: physicsComponent); /*
* SNAKING!!! >-( 0 ================>
* Snaking is a feature where you can move faster by strafing in a direction perpendicular to the
* direction you intend to move while still holding the movement key for the direction you're trying to move.
* Snaking only works if acceleration exceeds friction, and it's effectiveness scales as acceleration continues
* to exceed friction.
* Snaking works because friction is applied first in the direction of our current velocity, while acceleration
* is applied after in our "Wish Direction" and is capped by the dot of our wish direction and current direction.
* This means when you change direction, you're technically able to accelerate more than what the velocity cap
* allows, but friction normally eats up the extra movement you gain.
* By strafing as stated above you can increase your speed by about 1.4 (square root of 2).
* This only works if friction is low enough so be sure that anytime you are letting a mob move in a low friction
* environment you take into account the fact they can snake! Also be sure to lower acceleration as well to
* prevent jerky movement!
*/
PhysicsSystem.SetLinearVelocity(uid, velocity, body: physicsComponent);
// Ensures that players do not spiiiiiiin // Ensures that players do not spiiiiiiin
PhysicsSystem.SetAngularVelocity(physicsUid, 0, body: physicsComponent); PhysicsSystem.SetAngularVelocity(uid, 0, body: physicsComponent);
// Handle footsteps at the end // Handle footsteps at the end
if (total != Vector2.Zero) if (wishDir != Vector2.Zero)
{ {
if (!NoRotateQuery.HasComponent(uid)) if (!NoRotateQuery.HasComponent(uid))
{ {
// TODO apparently this results in a duplicate move event because "This should have its event run during // TODO apparently this results in a duplicate move event because "This should have its event run during
// island solver"??. So maybe SetRotation needs an argument to avoid raising an event? // island solver"??. So maybe SetRotation needs an argument to avoid raising an event?
var worldRot = _transform.GetWorldRotation(xform); var worldRot = _transform.GetWorldRotation(xform);
_transform.SetLocalRotation(xform, xform.LocalRotation + wishDir.ToWorldAngle() - worldRot);
_transform.SetLocalRotation(uid, xform.LocalRotation + wishDir.ToWorldAngle() - worldRot, xform);
} }
if (!weightless && MobMoverQuery.TryGetComponent(uid, out var mobMover) && if (!weightless && MobMoverQuery.TryGetComponent(uid, out var mobMover) &&
@@ -284,9 +344,9 @@ public abstract partial class SharedMoverController : VirtualController
.WithVariation(sound.Params.Variation ?? mobMover.FootstepVariation); .WithVariation(sound.Params.Variation ?? mobMover.FootstepVariation);
// If we're a relay target then predict the sound for all relays. // If we're a relay target then predict the sound for all relays.
if (relayTarget != null) if (relaySource != null)
{ {
_audio.PlayPredicted(sound, uid, relayTarget.Source, audioParams); _audio.PlayPredicted(sound, uid, relaySource.Value, audioParams);
} }
else else
{ {
@@ -334,14 +394,12 @@ public abstract partial class SharedMoverController : VirtualController
adjustment = Math.Clamp(adjustment, -angleDiff, angleDiff); adjustment = Math.Clamp(adjustment, -angleDiff, angleDiff);
} }
mover.RelativeRotation += adjustment; mover.RelativeRotation = (mover.RelativeRotation + adjustment).FlipPositive();
mover.RelativeRotation.FlipPositive();
Dirty(uid, mover); Dirty(uid, mover);
} }
else if (!angleDiff.Equals(Angle.Zero)) else if (!angleDiff.Equals(Angle.Zero))
{ {
mover.TargetRelativeRotation.FlipPositive(); mover.RelativeRotation = mover.TargetRelativeRotation.FlipPositive();
mover.RelativeRotation = mover.TargetRelativeRotation;
Dirty(uid, mover); Dirty(uid, mover);
} }
} }
@@ -353,18 +411,10 @@ public abstract partial class SharedMoverController : VirtualController
if (speed < minimumFrictionSpeed) if (speed < minimumFrictionSpeed)
return; return;
var drop = 0f; // This equation is lifted from the Physics Island solver.
// We re-use it here because Kinematic Controllers can't/shouldn't use the Physics Friction
velocity *= Math.Clamp(1.0f - frameTime * friction, 0.0f, 1.0f);
var control = MathF.Max(_stopSpeed, speed);
drop += control * friction * frameTime;
var newSpeed = MathF.Max(0f, speed - drop);
if (newSpeed.Equals(speed))
return;
newSpeed /= speed;
velocity *= newSpeed;
} }
/// <summary> /// <summary>
@@ -542,4 +592,30 @@ public abstract partial class SharedMoverController : VirtualController
sound = haveShoes ? tileDef.FootstepSounds : tileDef.BarestepSounds; sound = haveShoes ? tileDef.FootstepSounds : tileDef.BarestepSounds;
return sound != null; return sound != null;
} }
private Vector2 AssertValidWish(InputMoverComponent mover, float walkSpeed, float sprintSpeed)
{
var (walkDir, sprintDir) = GetVelocityInput(mover);
var total = walkDir * walkSpeed + sprintDir * sprintSpeed;
var parentRotation = GetParentGridAngle(mover);
var wishDir = _relativeMovement ? parentRotation.RotateVec(total) : total;
DebugTools.Assert(MathHelper.CloseToPercent(total.Length(), wishDir.Length()));
return wishDir;
}
private void OnTileFriction(Entity<MovementSpeedModifierComponent> ent, ref TileFrictionEvent args)
{
if (!TryComp<PhysicsComponent>(ent, out var physicsComponent) || !XformQuery.TryComp(ent, out var xform))
return;
// TODO: Make IsWeightless event based!!!
if (physicsComponent.BodyStatus != BodyStatus.OnGround || _gravity.IsWeightless(ent, physicsComponent, xform))
args.Modifier *= ent.Comp.BaseWeightlessFriction;
else
args.Modifier *= ent.Comp.BaseFriction;
}
} }

View File

@@ -1,4 +1,3 @@
using Content.Shared.Inventory;
using Content.Shared.Movement.Components; using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events; using Content.Shared.Movement.Events;
using Content.Shared.Gravity; using Content.Shared.Gravity;
@@ -19,15 +18,15 @@ public sealed class SpeedModifierContactsSystem : EntitySystem
// TODO full-game-save // TODO full-game-save
// Either these need to be processed before a map is saved, or slowed/slowing entities need to update on init. // Either these need to be processed before a map is saved, or slowed/slowing entities need to update on init.
private HashSet<EntityUid> _toUpdate = new(); private readonly HashSet<EntityUid> _toUpdate = new();
private HashSet<EntityUid> _toRemove = new(); private readonly HashSet<EntityUid> _toRemove = new();
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<SpeedModifierContactsComponent, StartCollideEvent>(OnEntityEnter); SubscribeLocalEvent<SpeedModifierContactsComponent, StartCollideEvent>(OnEntityEnter);
SubscribeLocalEvent<SpeedModifierContactsComponent, EndCollideEvent>(OnEntityExit); SubscribeLocalEvent<SpeedModifierContactsComponent, EndCollideEvent>(OnEntityExit);
SubscribeLocalEvent<SpeedModifiedByContactComponent, RefreshMovementSpeedModifiersEvent>(MovementSpeedCheck); SubscribeLocalEvent<SpeedModifiedByContactComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovementSpeedModifiers);
SubscribeLocalEvent<SpeedModifierContactsComponent, ComponentShutdown>(OnShutdown); SubscribeLocalEvent<SpeedModifierContactsComponent, ComponentShutdown>(OnShutdown);
UpdatesAfter.Add(typeof(SharedPhysicsSystem)); UpdatesAfter.Add(typeof(SharedPhysicsSystem));
@@ -52,17 +51,16 @@ public sealed class SpeedModifierContactsSystem : EntitySystem
_toUpdate.Clear(); _toUpdate.Clear();
} }
public void ChangeModifiers(EntityUid uid, float speed, SpeedModifierContactsComponent? component = null) public void ChangeSpeedModifiers(EntityUid uid, float speed, SpeedModifierContactsComponent? component = null)
{ {
ChangeModifiers(uid, speed, speed, component); ChangeSpeedModifiers(uid, speed, speed, component);
} }
public void ChangeModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, SpeedModifierContactsComponent? component = null) public void ChangeSpeedModifiers(EntityUid uid, float walkSpeed, float sprintSpeed, SpeedModifierContactsComponent? component = null)
{ {
if (!Resolve(uid, ref component)) if (!Resolve(uid, ref component))
{
return; return;
}
component.WalkSpeedModifier = walkSpeed; component.WalkSpeedModifier = walkSpeed;
component.SprintSpeedModifier = sprintSpeed; component.SprintSpeedModifier = sprintSpeed;
Dirty(uid, component); Dirty(uid, component);
@@ -78,7 +76,7 @@ public sealed class SpeedModifierContactsSystem : EntitySystem
_toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys)); _toUpdate.UnionWith(_physics.GetContactingEntities(uid, phys));
} }
private void MovementSpeedCheck(EntityUid uid, SpeedModifiedByContactComponent component, RefreshMovementSpeedModifiersEvent args) private void OnRefreshMovementSpeedModifiers(EntityUid uid, SpeedModifiedByContactComponent component, RefreshMovementSpeedModifiersEvent args)
{ {
if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent)) if (!EntityManager.TryGetComponent<PhysicsComponent>(uid, out var physicsComponent))
return; return;
@@ -110,12 +108,12 @@ public sealed class SpeedModifierContactsSystem : EntitySystem
} }
// SpeedModifierContactsComponent takes priority over SlowedOverSlipperyComponent, effectively overriding the slippery slow. // SpeedModifierContactsComponent takes priority over SlowedOverSlipperyComponent, effectively overriding the slippery slow.
if (TryComp<SlipperyComponent>(ent, out var slipperyComponent) && speedModified == false) if (HasComp<SlipperyComponent>(ent) && speedModified == false)
{ {
var evSlippery = new GetSlowedOverSlipperyModifierEvent(); var evSlippery = new GetSlowedOverSlipperyModifierEvent();
RaiseLocalEvent(uid, ref evSlippery); RaiseLocalEvent(uid, ref evSlippery);
if (evSlippery.SlowdownModifier != 1) if (MathHelper.CloseTo(evSlippery.SlowdownModifier, 1))
{ {
walkSpeed += evSlippery.SlowdownModifier; walkSpeed += evSlippery.SlowdownModifier;
sprintSpeed += evSlippery.SlowdownModifier; sprintSpeed += evSlippery.SlowdownModifier;
@@ -130,7 +128,7 @@ public sealed class SpeedModifierContactsSystem : EntitySystem
} }
} }
if (entries > 0) if (entries > 0 && (!MathHelper.CloseTo(walkSpeed, entries) || !MathHelper.CloseTo(sprintSpeed, entries)))
{ {
walkSpeed /= entries; walkSpeed /= entries;
sprintSpeed /= entries; sprintSpeed /= entries;

View File

@@ -119,6 +119,7 @@ public sealed class SlipperySystem : EntitySystem
{ {
var sliding = EnsureComp<SlidingComponent>(other); var sliding = EnsureComp<SlidingComponent>(other);
sliding.CollidingEntities.Add(uid); sliding.CollidingEntities.Add(uid);
// Why the fuck does this assertion stack overflow every once in a while
DebugTools.Assert(_physics.GetContactingEntities(other, physics).Contains(uid)); DebugTools.Assert(_physics.GetContactingEntities(other, physics).Contains(uid));
} }
} }

View File

@@ -38,7 +38,7 @@ public abstract class SharedStunSystem : EntitySystem
/// Friction modifier for knocked down players. /// Friction modifier for knocked down players.
/// Doesn't make them faster but makes them slow down... slower. /// Doesn't make them faster but makes them slow down... slower.
/// </summary> /// </summary>
public const float KnockDownModifier = 0.4f; public const float KnockDownModifier = 0.2f;
public override void Initialize() public override void Initialize()
{ {

View File

@@ -29,7 +29,10 @@ public sealed class ThrowingSystem : EntitySystem
public const float FlyTimePercentage = 0.8f; public const float FlyTimePercentage = 0.8f;
private const float TileFrictionMod = 1.5f;
private float _frictionModifier; private float _frictionModifier;
private float _airDamping;
[Dependency] private readonly IGameTiming _gameTiming = default!; [Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!; [Dependency] private readonly SharedGravitySystem _gravity = default!;
@@ -45,6 +48,7 @@ public sealed class ThrowingSystem : EntitySystem
base.Initialize(); base.Initialize();
Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true); Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = value, true);
Subs.CVar(_configManager, CCVars.AirFriction, value => _airDamping = value, true);
} }
public void TryThrow( public void TryThrow(
@@ -159,7 +163,7 @@ public sealed class ThrowingSystem : EntitySystem
}; };
// if not given, get the default friction value for distance calculation // if not given, get the default friction value for distance calculation
var tileFriction = friction ?? _frictionModifier * TileFrictionController.DefaultFriction; var tileFriction = friction ?? _frictionModifier * TileFrictionMod;
if (tileFriction == 0f) if (tileFriction == 0f)
compensateFriction = false; // cannot calculate this if there is no friction compensateFriction = false; // cannot calculate this if there is no friction
@@ -202,6 +206,7 @@ public sealed class ThrowingSystem : EntitySystem
// else let the item land on the cursor and from where it slides a little further. // else let the item land on the cursor and from where it slides a little further.
// This is an exact formula we get from exponentially decaying velocity after landing. // This is an exact formula we get from exponentially decaying velocity after landing.
// If someone changes how tile friction works at some point, this will have to be adjusted. // If someone changes how tile friction works at some point, this will have to be adjusted.
// This doesn't actually compensate for air friction, but it's low enough it shouldn't matter.
var throwSpeed = compensateFriction ? direction.Length() / (flyTime + 1 / tileFriction) : baseThrowSpeed; var throwSpeed = compensateFriction ? direction.Length() / (flyTime + 1 / tileFriction) : baseThrowSpeed;
var impulseVector = direction.Normalized() * throwSpeed * physics.Mass; var impulseVector = direction.Normalized() * throwSpeed * physics.Mass;
_physics.ApplyLinearImpulse(uid, impulseVector, body: physics); _physics.ApplyLinearImpulse(uid, impulseVector, body: physics);

View File

@@ -113,8 +113,6 @@
components: components:
- type: Clickable - type: Clickable
- type: Slippery - type: Slippery
- type: TileFrictionModifier
modifier: 0.3
- type: Transform - type: Transform
noRot: true noRot: true
anchored: true anchored: true

View File

@@ -517,7 +517,7 @@
baseSprintSpeed : 4 baseSprintSpeed : 4
weightlessAcceleration: 1.5 weightlessAcceleration: 1.5
weightlessFriction: 1 weightlessFriction: 1
weightlessModifier: 1 baseWeightlessModifier: 1
- type: Damageable - type: Damageable
damageContainer: Biological damageContainer: Biological
damageModifierSet: Moth damageModifierSet: Moth

View File

@@ -49,7 +49,7 @@
- type: MovementSpeedModifier - type: MovementSpeedModifier
baseWalkSpeed: 3 baseWalkSpeed: 3
baseSprintSpeed: 5 baseSprintSpeed: 5
weightlessModifier: 1.5 baseWeightlessModifier: 1.5
- type: Sprite - type: Sprite
sprite: Mobs/Demons/behonker.rsi sprite: Mobs/Demons/behonker.rsi
layers: layers:

View File

@@ -9,3 +9,4 @@
- type: NoSlip - type: NoSlip
- type: MovementAlwaysTouching - type: MovementAlwaysTouching
- type: CanMoveInAir - type: CanMoveInAir
- type: MovementSpeedModifier

View File

@@ -41,7 +41,7 @@
- type: MovementSpeedModifier - type: MovementSpeedModifier
baseWalkSpeed: 3 baseWalkSpeed: 3
baseSprintSpeed: 5 baseSprintSpeed: 5
weightlessModifier: 1.5 baseWeightlessModifier: 1.5
- type: RandomSprite - type: RandomSprite
available: available:
- enum.DamageStateVisualLayers.Base: - enum.DamageStateVisualLayers.Base:

View File

@@ -50,7 +50,7 @@
- type: MovementSpeedModifier - type: MovementSpeedModifier
weightlessAcceleration: 1.5 # Move around more easily in space. weightlessAcceleration: 1.5 # Move around more easily in space.
weightlessFriction: 1 weightlessFriction: 1
weightlessModifier: 1 baseWeightlessModifier: 1
- type: Flammable - type: Flammable
damage: damage:
types: types:
@@ -118,7 +118,7 @@
state: "creampie_moth" state: "creampie_moth"
visible: false visible: false
- type: Inventory - type: Inventory
speciesId: moth speciesId: moth
femaleDisplacements: femaleDisplacements:
jumpsuit: jumpsuit:
sizeMaps: sizeMaps:
@@ -135,7 +135,7 @@
- type: HumanoidAppearance - type: HumanoidAppearance
species: Moth species: Moth
- type: Inventory - type: Inventory
speciesId: moth speciesId: moth
femaleDisplacements: femaleDisplacements:
jumpsuit: jumpsuit:
sizeMaps: sizeMaps:

View File

@@ -14,6 +14,8 @@
- type: Physics - type: Physics
bodyType: Dynamic bodyType: Dynamic
linearDamping: 0 linearDamping: 0
- type: TileFrictionModifier
modifier: 0
- type: PointLight - type: PointLight
radius: 3 radius: 3
color: red color: red
@@ -80,11 +82,11 @@
gravityState: true gravityState: true
- type: InputMover - type: InputMover
- type: MovementSpeedModifier - type: MovementSpeedModifier
weightlessAcceleration: 5 baseWeightlessAcceleration: 5
weightlessModifier: 2 baseWeightlessModifier: 2
weightlessFriction: 0 baseWeightlessFriction: 0
friction: 0 baseFriction: 0
frictionNoInput: 0 offGridFriction: 0
- type: CanMoveInAir - type: CanMoveInAir
- type: MovementAlwaysTouching - type: MovementAlwaysTouching
- type: NoSlip - type: NoSlip

View File

@@ -50,3 +50,5 @@
coldDamage: {} coldDamage: {}
coldDamageThreshold: 0 coldDamageThreshold: 0
- type: FrictionContacts - type: FrictionContacts
- type: TileFrictionModifier
modifier: 0.05

View File

@@ -25,13 +25,6 @@
name: jetpack name: jetpack
description: It's a jetpack. It can hold 5 L of gas. description: It's a jetpack. It can hold 5 L of gas.
components: components:
- type: InputMover
toParent: true
- type: MovementSpeedModifier
weightlessAcceleration: 1
weightlessFriction: 0.3
weightlessModifier: 1.2
- type: CanMoveInAir
- type: Sprite - type: Sprite
sprite: Objects/Tanks/Jetpacks/blue.rsi sprite: Objects/Tanks/Jetpacks/blue.rsi
state: icon state: icon

View File

@@ -1059,7 +1059,7 @@
collection: FootstepCarpet collection: FootstepCarpet
barestepSounds: barestepSounds:
collection: BarestepCarpet collection: BarestepCarpet
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemArcadeBlue itemDrop: FloorTileItemArcadeBlue
heatCapacity: 10000 heatCapacity: 10000
@@ -1074,7 +1074,7 @@
collection: FootstepCarpet collection: FootstepCarpet
barestepSounds: barestepSounds:
collection: BarestepCarpet collection: BarestepCarpet
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemArcadeBlue2 itemDrop: FloorTileItemArcadeBlue2
heatCapacity: 10000 heatCapacity: 10000
@@ -1089,7 +1089,7 @@
collection: FootstepCarpet collection: FootstepCarpet
barestepSounds: barestepSounds:
collection: BarestepCarpet collection: BarestepCarpet
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemArcadeRed itemDrop: FloorTileItemArcadeRed
heatCapacity: 10000 heatCapacity: 10000
@@ -1104,7 +1104,7 @@
collection: FootstepCarpet collection: FootstepCarpet
barestepSounds: barestepSounds:
collection: BarestepCarpet collection: BarestepCarpet
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemEighties itemDrop: FloorTileItemEighties
heatCapacity: 10000 heatCapacity: 10000
@@ -1119,7 +1119,7 @@
collection: FootstepCarpet collection: FootstepCarpet
barestepSounds: barestepSounds:
collection: BarestepCarpet collection: BarestepCarpet
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemCarpetClown itemDrop: FloorTileItemCarpetClown
heatCapacity: 10000 heatCapacity: 10000
@@ -1134,7 +1134,7 @@
collection: FootstepCarpet collection: FootstepCarpet
barestepSounds: barestepSounds:
collection: BarestepCarpet collection: BarestepCarpet
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemCarpetOffice itemDrop: FloorTileItemCarpetOffice
heatCapacity: 10000 heatCapacity: 10000
@@ -1153,7 +1153,7 @@
deconstructTools: [ Prying ] deconstructTools: [ Prying ]
footstepSounds: footstepSounds:
collection: FootstepFloor collection: FootstepFloor
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemBoxing itemDrop: FloorTileItemBoxing
heatCapacity: 10000 heatCapacity: 10000
@@ -1172,7 +1172,7 @@
deconstructTools: [ Prying ] deconstructTools: [ Prying ]
footstepSounds: footstepSounds:
collection: FootstepFloor collection: FootstepFloor
friction: 0.25 friction: 1.25
itemDrop: FloorTileItemGym itemDrop: FloorTileItemGym
heatCapacity: 10000 heatCapacity: 10000
@@ -1789,7 +1789,7 @@
footstepSounds: footstepSounds:
collection: FootstepBlood collection: FootstepBlood
itemDrop: FloorTileItemFlesh itemDrop: FloorTileItemFlesh
friction: 0.05 #slippy friction: 0.25 #slippy
heatCapacity: 10000 heatCapacity: 10000
- type: tile - type: tile
@@ -1991,9 +1991,8 @@
deconstructTools: [ Prying ] deconstructTools: [ Prying ]
friction: 0.05 friction: 0.05
heatCapacity: 10000 heatCapacity: 10000
mobFriction: 0.5 mobFriction: 0.05
mobFrictionNoInput: 0.05 mobAcceleration: 0.1
mobAcceleration: 2
itemDrop: FloorTileItemAstroIce itemDrop: FloorTileItemAstroIce
- type: tile - type: tile

View File

@@ -138,9 +138,7 @@
friction: 0.05 friction: 0.05
heatCapacity: 10000 heatCapacity: 10000
weather: true weather: true
mobFriction: 0.5 mobAcceleration: 0.1
mobFrictionNoInput: 0.05
mobAcceleration: 2
indestructible: true indestructible: true
# Dug snow # Dug snow

View File

@@ -6,7 +6,7 @@
isSubfloor: true isSubfloor: true
footstepSounds: footstepSounds:
collection: FootstepPlating collection: FootstepPlating
friction: 0.3 friction: 1.5
heatCapacity: 10000 heatCapacity: 10000
- type: tile - type: tile
@@ -22,7 +22,7 @@
isSubfloor: true isSubfloor: true
footstepSounds: footstepSounds:
collection: FootstepPlating collection: FootstepPlating
friction: 0.3 friction: 1.5
heatCapacity: 10000 heatCapacity: 10000
- type: tile - type: tile
@@ -33,7 +33,7 @@
isSubfloor: true isSubfloor: true
footstepSounds: footstepSounds:
collection: FootstepPlating collection: FootstepPlating
friction: 0.3 friction: 1.5
heatCapacity: 10000 heatCapacity: 10000
- type: tile - type: tile
@@ -44,7 +44,7 @@
isSubfloor: true isSubfloor: true
footstepSounds: footstepSounds:
collection: FootstepPlating collection: FootstepPlating
friction: 0.3 friction: 1.5
heatCapacity: 10000 heatCapacity: 10000
- type: tile - type: tile
@@ -55,7 +55,7 @@
isSubfloor: true isSubfloor: true
footstepSounds: footstepSounds:
collection: FootstepPlating collection: FootstepPlating
friction: 0.15 #a little less then actual snow friction: 0.75 #a little less then actual snow
heatCapacity: 10000 heatCapacity: 10000
- type: tile - type: tile
@@ -68,7 +68,7 @@
weather: true weather: true
footstepSounds: footstepSounds:
collection: FootstepCatwalk collection: FootstepCatwalk
friction: 0.3 friction: 1.5
isSpace: true isSpace: true
itemDrop: PartRodMetal1 itemDrop: PartRodMetal1
heatCapacity: 10000 heatCapacity: 10000
@@ -83,7 +83,7 @@
weather: true weather: true
footstepSounds: footstepSounds:
collection: FootstepPlating collection: FootstepPlating
friction: 0.3 friction: 1.5
isSpace: true isSpace: true
itemDrop: PartRodMetal1 itemDrop: PartRodMetal1
heatCapacity: 10000 heatCapacity: 10000