Files
wwdpublic/Content.Shared/Clothing/EntitySystems/EmitsSoundOnMoveSystem.cs
VMSolidus 151e1b60a3 TCJ Makes A Rage Performance PR (#2298)
# Description

I got baited by Ectoplasm, so I then spent 3 hours shaving off a
sizeable chunk of this game's performance cost, including by taking 3 of
the "Top 10 frametime consumers", and reducing their performance costs
by 99% each. Along with various examples of slimming down some of the
worst EQE's.

Oh, and I fixed EmitSoundOnMove being desynced with actual movement. As
part of making EmitSoundOnMove use 99% less CPU time, it was also
synchronized with the MoverController.

# Changelog

🆑
- fix: Fixed items such as tactical webbing, bell collars, and hardsuits
being desynced with character movement.
- tweak: Made various large performance improvements.

(cherry picked from commit 684e8175443167beb0e20e3323a05b5f493b3374)
2025-04-26 11:45:35 +03:00

77 lines
2.7 KiB
C#

using Content.Shared.Clothing.Components;
using Content.Shared.Gravity;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.Events;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Physics.Components;
using Robust.Shared.Timing;
namespace Content.Shared.Clothing.Systems;
public sealed class EmitsSoundOnMoveSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedGravitySystem _gravity = default!;
[Dependency] private readonly IGameTiming _timing = default!;
private EntityQuery<PhysicsComponent> _physicsQuery;
private EntityQuery<ClothingComponent> _clothingQuery;
public override void Initialize()
{
base.Initialize();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
_clothingQuery = GetEntityQuery<ClothingComponent>();
SubscribeLocalEvent<EmitsSoundOnMoveComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<EmitsSoundOnMoveComponent, GotUnequippedEvent>(OnUnequipped);
SubscribeLocalEvent<EmitsSoundOnMoveComponent, InventoryRelayedEvent<MakeFootstepSoundEvent>>(OnFootstep);
}
private void OnEquipped(EntityUid uid, EmitsSoundOnMoveComponent component, GotEquippedEvent args)
{
component.IsSlotValid = !args.SlotFlags.HasFlag(SlotFlags.POCKET);
}
private void OnUnequipped(EntityUid uid, EmitsSoundOnMoveComponent component, GotUnequippedEvent args)
{
component.IsSlotValid = true;
}
private void OnFootstep(Entity<EmitsSoundOnMoveComponent> ent, ref InventoryRelayedEvent<MakeFootstepSoundEvent> args)
{
var uid = ent.Owner;
var component = ent.Comp;
if (!_physicsQuery.TryGetComponent(uid, out var physics)
|| !_timing.IsFirstTimePredicted)
return;
var xform = Transform(uid);
// Space does not transmit sound
if (xform.GridUid is null)
return;
if (component.RequiresGravity && _gravity.IsWeightless(uid, physics, xform))
return;
var parent = xform.ParentUid;
var isWorn = parent is { Valid: true } &&
_clothingQuery.TryGetComponent(uid, out var clothing)
&& clothing.InSlot != null
&& component.IsSlotValid;
if (component.RequiresWorn && !isWorn)
return;
var sound = component.SoundCollection;
var audioParams = sound.Params
.WithVolume(sound.Params.Volume)
.WithVariation(sound.Params.Variation ?? 0f);
_audio.PlayPredicted(sound, uid, uid, audioParams);
}
}