From dcbb2d2a111bab13ac862bc9a7b5a25bef3c1e0a Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sun, 15 Aug 2021 14:03:08 +1000 Subject: [PATCH] Increase MoverController performance (#4448) Should be a decent amount; rest will come from removing IActionBlocker. --- .../Friction/TileFrictionController.cs | 15 ++++++ .../Friction/TileFrictionController.cs | 15 ++++++ .../Friction/SharedTileFrictionController.cs | 30 +++++++---- .../Movement/SharedMoverController.cs | 52 ++++++++++++++----- 4 files changed, 89 insertions(+), 23 deletions(-) create mode 100644 Content.Client/Friction/TileFrictionController.cs create mode 100644 Content.Server/Friction/TileFrictionController.cs diff --git a/Content.Client/Friction/TileFrictionController.cs b/Content.Client/Friction/TileFrictionController.cs new file mode 100644 index 0000000000..82ae1dc86a --- /dev/null +++ b/Content.Client/Friction/TileFrictionController.cs @@ -0,0 +1,15 @@ +using Content.Client.Physics.Controllers; +using Content.Shared.Friction; +using Robust.Shared.GameObjects; + +namespace Content.Client.Friction +{ + public sealed class TileFrictionController : SharedTileFrictionController + { + public override void Initialize() + { + base.Initialize(); + Mover = EntitySystem.Get().GetController(); + } + } +} diff --git a/Content.Server/Friction/TileFrictionController.cs b/Content.Server/Friction/TileFrictionController.cs new file mode 100644 index 0000000000..475c799f7e --- /dev/null +++ b/Content.Server/Friction/TileFrictionController.cs @@ -0,0 +1,15 @@ +using Content.Server.Physics.Controllers; +using Content.Shared.Friction; +using Robust.Shared.GameObjects; + +namespace Content.Server.Friction +{ + public sealed class TileFrictionController : SharedTileFrictionController + { + public override void Initialize() + { + base.Initialize(); + Mover = EntitySystem.Get().GetController(); + } + } +} diff --git a/Content.Shared/Friction/SharedTileFrictionController.cs b/Content.Shared/Friction/SharedTileFrictionController.cs index 9140a6d272..66f237185b 100644 --- a/Content.Shared/Friction/SharedTileFrictionController.cs +++ b/Content.Shared/Friction/SharedTileFrictionController.cs @@ -7,35 +7,43 @@ using Robust.Shared.Configuration; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Map; -using Robust.Shared.Physics; using Robust.Shared.Physics.Controllers; using Robust.Shared.Physics.Dynamics; namespace Content.Shared.Friction { - public sealed class SharedTileFrictionController : VirtualController + public abstract class SharedTileFrictionController : VirtualController { - [Dependency] private readonly IConfigurationManager _configManager = default!; [Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!; - private SharedBroadphaseSystem _broadPhaseSystem = default!; + protected SharedMoverController Mover = default!; private float _stopSpeed; - private float _frictionModifier; public override void Initialize() { base.Initialize(); - _broadPhaseSystem = EntitySystem.Get(); - _frictionModifier = _configManager.GetCVar(CCVars.TileFrictionModifier); - _configManager.OnValueChanged(CCVars.TileFrictionModifier, value => _frictionModifier = value); + var configManager = IoCManager.Resolve(); - _stopSpeed = _configManager.GetCVar(CCVars.StopSpeed); - _configManager.OnValueChanged(CCVars.StopSpeed, value => _stopSpeed = value); + configManager.OnValueChanged(CCVars.TileFrictionModifier, SetFrictionModifier, true); + configManager.OnValueChanged(CCVars.StopSpeed, SetStopSpeed, true); + } + + private void SetStopSpeed(float value) => _stopSpeed = value; + + private void SetFrictionModifier(float value) => _frictionModifier = value; + + public override void Shutdown() + { + base.Shutdown(); + var configManager = IoCManager.Resolve(); + + configManager.UnsubValueChanged(CCVars.TileFrictionModifier, SetFrictionModifier); + configManager.UnsubValueChanged(CCVars.StopSpeed, SetStopSpeed); } public override void UpdateBeforeMapSolve(bool prediction, PhysicsMap map, float frameTime) @@ -47,7 +55,7 @@ namespace Content.Shared.Friction // Only apply friction when it's not a mob (or the mob doesn't have control) if (prediction && !body.Predict || body.BodyStatus == BodyStatus.InAir || - SharedMoverController.UseMobMovement(_broadPhaseSystem, body, _mapManager)) continue; + Mover.UseMobMovement(body.Owner.Uid)) continue; var surfaceFriction = GetTileFriction(body); var bodyModifier = body.Owner.GetComponentOrNull()?.Modifier ?? 1.0f; diff --git a/Content.Shared/Movement/SharedMoverController.cs b/Content.Shared/Movement/SharedMoverController.cs index 0c1bc416a9..6d4f5666f5 100644 --- a/Content.Shared/Movement/SharedMoverController.cs +++ b/Content.Shared/Movement/SharedMoverController.cs @@ -1,5 +1,7 @@ +using System.Collections.Generic; using Content.Shared.ActionBlocker; using Content.Shared.CCVar; +using Content.Shared.Friction; using Content.Shared.MobState; using Content.Shared.Movement.Components; using Content.Shared.Pulling.Components; @@ -9,7 +11,6 @@ using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; using Robust.Shared.Physics; -using Robust.Shared.Physics.Broadphase; using Robust.Shared.Physics.Controllers; using Robust.Shared.Utility; @@ -23,16 +24,39 @@ namespace Content.Shared.Movement { [Dependency] private readonly IMapManager _mapManager = default!; + private ActionBlockerSystem _blocker = default!; private SharedBroadphaseSystem _broadPhaseSystem = default!; private bool _relativeMovement; + /// + /// Cache the mob movement calculation to re-use elsewhere. + /// + public Dictionary UsedMobMovement = new(); + public override void Initialize() { base.Initialize(); _broadPhaseSystem = EntitySystem.Get(); + _blocker = EntitySystem.Get(); var configManager = IoCManager.Resolve(); - configManager.OnValueChanged(CCVars.RelativeMovement, value => _relativeMovement = value, true); + configManager.OnValueChanged(CCVars.RelativeMovement, SetRelativeMovement, true); + UpdatesBefore.Add(typeof(SharedTileFrictionController)); + } + + private void SetRelativeMovement(bool value) => _relativeMovement = value; + + public override void Shutdown() + { + base.Shutdown(); + var configManager = IoCManager.Resolve(); + configManager.UnsubValueChanged(CCVars.RelativeMovement, SetRelativeMovement); + } + + public override void UpdateAfterSolve(bool prediction, float frameTime) + { + base.UpdateAfterSolve(prediction, frameTime); + UsedMobMovement.Clear(); } /// @@ -65,17 +89,19 @@ namespace Content.Shared.Movement protected void HandleMobMovement(IMoverComponent mover, PhysicsComponent physicsComponent, IMobMoverComponent mobMover) { - // TODO: Look at https://gameworksdocs.nvidia.com/PhysX/4.1/documentation/physxguide/Manual/CharacterControllers.html?highlight=controller as it has some adviceo n kinematic controllersx - if (!UseMobMovement(_broadPhaseSystem, physicsComponent, _mapManager)) + DebugTools.Assert(!UsedMobMovement.ContainsKey(mover.Owner.Uid)); + + if (!UseMobMovement(physicsComponent)) { + UsedMobMovement[mover.Owner.Uid] = false; return; } + UsedMobMovement[mover.Owner.Uid] = true; var transform = mover.Owner.Transform; + var weightless = mover.Owner.IsWeightless(physicsComponent, mapManager: _mapManager); var (walkDir, sprintDir) = mover.VelocityDir; - var weightless = transform.Owner.IsWeightless(physicsComponent, mapManager: _mapManager); - // Handle wall-pushes. if (weightless) { @@ -117,14 +143,16 @@ namespace Content.Shared.Movement physicsComponent.LinearVelocity = worldTotal; } - public static bool UseMobMovement(SharedBroadphaseSystem broadPhaseSystem, PhysicsComponent body, IMapManager mapManager) + public bool UseMobMovement(EntityUid uid) { - return (body.BodyStatus == BodyStatus.OnGround) & + return UsedMobMovement.TryGetValue(uid, out var used) && used; + } + + protected bool UseMobMovement(PhysicsComponent body) + { + return body.BodyStatus == BodyStatus.OnGround && body.Owner.HasComponent() && - EntitySystem.Get().CanMove(body.Owner) && - (!body.Owner.IsWeightless(body, mapManager: mapManager) || - body.Owner.TryGetComponent(out SharedPlayerMobMoverComponent? mover) && - IsAroundCollider(broadPhaseSystem, body.Owner.Transform, mover, body)); + _blocker.CanMove(body.Owner); } ///