Files
tbd-station-14/Content.Shared/Friction/TileFrictionController.cs
Princess Cheeseballs 36030ef154 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>
2025-05-02 18:18:08 +10:00

171 lines
7.4 KiB
C#

using System.Numerics;
using Content.Shared.CCVar;
using Content.Shared.Gravity;
using Content.Shared.Interaction.Events;
using Content.Shared.Movement.Events;
using Content.Shared.Movement.Pulling.Components;
using Content.Shared.Movement.Systems;
using JetBrains.Annotations;
using Robust.Shared.Configuration;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Controllers;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Systems;
namespace Content.Shared.Friction
{
public sealed class TileFrictionController : VirtualController
{
[Dependency] private readonly IConfigurationManager _configManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly SharedMoverController _mover = default!;
[Dependency] private readonly SharedMapSystem _map = default!;
private EntityQuery<TileFrictionModifierComponent> _frictionQuery;
private EntityQuery<TransformComponent> _xformQuery;
private EntityQuery<PullerComponent> _pullerQuery;
private EntityQuery<PullableComponent> _pullableQuery;
private EntityQuery<MapGridComponent> _gridQuery;
private float _frictionModifier;
private float _minDamping;
private float _airDamping;
private float _offGridDamping;
public override void Initialize()
{
base.Initialize();
Subs.CVar(_configManager, CCVars.TileFrictionModifier, value => _frictionModifier = 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>();
_xformQuery = GetEntityQuery<TransformComponent>();
_pullerQuery = GetEntityQuery<PullerComponent>();
_pullableQuery = GetEntityQuery<PullableComponent>();
_gridQuery = GetEntityQuery<MapGridComponent>();
}
public override void UpdateBeforeMapSolve(bool prediction, PhysicsMapComponent mapComponent, float frameTime)
{
base.UpdateBeforeMapSolve(prediction, mapComponent, frameTime);
foreach (var body in mapComponent.AwakeBodies)
{
var uid = body.Owner;
// Only apply friction when it's not a mob (or the mob doesn't have control)
// We may want to instead only apply friction to dynamic entities and not mobs ever.
if (prediction && !body.Predict || _mover.UseMobMovement(uid))
continue;
if (body.LinearVelocity.Equals(Vector2.Zero) && body.AngularVelocity.Equals(0f))
continue;
if (!_xformQuery.TryGetComponent(uid, out var xform))
{
Log.Error($"Unable to get transform for {ToPrettyString(uid)} in tilefrictioncontroller");
continue;
}
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;
if (_frictionQuery.TryGetComponent(uid, out var frictionComp))
{
bodyModifier = frictionComp.Modifier;
}
var ev = new TileFrictionEvent(bodyModifier);
RaiseLocalEvent(uid, ref ev);
bodyModifier = ev.Modifier;
// If we're sandwiched between 2 pullers reduce friction
// Might be better to make this dynamic and check how many are in the pull chain?
// Either way should be much faster for now.
if (_pullerQuery.TryGetComponent(uid, out var puller) && puller.Pulling != null &&
_pullableQuery.TryGetComponent(uid, out var pullable) && pullable.BeingPulled)
{
bodyModifier *= 0.2f;
}
friction *= bodyModifier;
friction = Math.Max(_minDamping, friction);
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);
}
}
[Pure]
private float GetTileFriction(
EntityUid uid,
PhysicsComponent body,
TransformComponent xform)
{
var tileModifier = 1f;
// If not on a grid and not in the air then return the map's friction.
if (!_gridQuery.TryGetComponent(xform.GridUid, out var grid))
{
return _frictionQuery.TryGetComponent(xform.MapUid, out var friction)
? friction.Modifier
: tileModifier;
}
var tile = _map.GetTileRef(xform.GridUid.Value, grid, xform.Coordinates);
// If it's a map but on an empty tile then just assume it has gravity.
if (tile.Tile.IsEmpty &&
HasComp<MapComponent>(xform.GridUid) &&
(!TryComp<GravityComponent>(xform.GridUid, out var gravity) || gravity.Enabled))
return tileModifier;
// Check for anchored ents that modify friction
var anc = _map.GetAnchoredEntitiesEnumerator(xform.GridUid.Value, grid, tile.GridIndices);
while (anc.MoveNext(out var tileEnt))
{
if (_frictionQuery.TryGetComponent(tileEnt, out var friction))
tileModifier *= friction.Modifier;
}
var tileDef = _tileDefinitionManager[tile.Tile.TypeId];
return tileDef.Friction * tileModifier;
}
public void SetModifier(EntityUid entityUid, float value, TileFrictionModifierComponent? friction = null)
{
if (!Resolve(entityUid, ref friction) || value.Equals(friction.Modifier))
return;
friction.Modifier = value;
Dirty(entityUid, friction);
}
}
}