Supermatter Tweaks (#2423)

# Description

_config.GetCVar() is generally to be deprecated and replaced with the
significantly better performant Subs.CVar(), since it doesn't need to
fetch the CVar each time its used and instead keeps it cached. This is
particularly important for systems that operate on every frame.
Technically Supermatter wasn't on my list of systems to optimize, but
it's a good look since it's a system we're uniquely responsible for.

No actual functionality for the Supermatter has been changed, this is
just a code cleanup and performance pass.

# Changelog

No CL since this isn't player facing.

(cherry picked from commit 1d10885f3a24053f77f8a27736e4c198b2aac026)
This commit is contained in:
VMSolidus
2025-05-13 20:33:20 -04:00
committed by Spatison
parent e5837b1d9c
commit db610d403b
4 changed files with 52 additions and 39 deletions

View File

@@ -1,20 +1,16 @@
using System.Linq;
using System.Text;
using Content.Server.Chat.Systems;
using Content.Server.Explosion.EntitySystems;
using Content.Server.Sound.Components;
using Content.Shared._EE.CCVars;
using Content.Shared._EE.Supermatter.Components;
using Content.Shared._EE.Supermatter.Monitor;
using Content.Shared.Atmos;
using Content.Shared.Audio;
using Content.Shared.CCVar;
using Content.Shared.Chat;
using Content.Shared.Popups;
using Content.Shared.Radiation.Components;
using Content.Shared.Speech;
using Robust.Shared.Audio;
using Robust.Shared.Configuration;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
@@ -108,14 +104,14 @@ public sealed partial class SupermatterSystem
sm.Power = Math.Max(absorbedGas.Temperature * tempFactor / Atmospherics.T0C * powerRatio + sm.Power, 0);
// Irradiate stuff
if (TryComp<RadiationSourceComponent>(uid, out var rad))
if (sm.Activated && TryComp<RadiationSourceComponent>(uid, out var rad))
{
rad.Intensity =
_config.GetCVar(ECCVars.SupermatterRadsBase) +
SupermatterRadsBase +
(sm.Power
* Math.Max(0, 1f + transmissionBonus / 10f)
* 0.003f
* _config.GetCVar(ECCVars.SupermatterRadsModifier));
* SupermatterRadsModifier);
rad.Slope = Math.Clamp(rad.Intensity / 15, 0.2f, 1f);
}
@@ -323,7 +319,7 @@ public sealed partial class SupermatterSystem
{
message = Loc.GetString("supermatter-delam-cancel", ("integrity", integrity));
sm.DelamAnnounced = false;
sm.YellTimer = TimeSpan.FromSeconds(_config.GetCVar(ECCVars.SupermatterYellTimer));
sm.YellTimer = TimeSpan.FromSeconds(SupermatterYellTimer);
global = true;
SendSupermatterAnnouncement(uid, sm, message, global);
@@ -350,7 +346,7 @@ public sealed partial class SupermatterSystem
> 30 => TimeSpan.FromSeconds(10),
> 5 => TimeSpan.FromSeconds(5),
<= 5 => TimeSpan.FromSeconds(1),
_ => TimeSpan.FromSeconds(_config.GetCVar(ECCVars.SupermatterYellTimer))
_ => TimeSpan.FromSeconds(SupermatterYellTimer)
};
message = Loc.GetString(loc, ("seconds", seconds));
@@ -429,8 +425,8 @@ public sealed partial class SupermatterSystem
/// </summary>
public DelamType ChooseDelamType(EntityUid uid, SupermatterComponent sm)
{
if (_config.GetCVar(ECCVars.SupermatterDoForceDelam))
return _config.GetCVar(ECCVars.SupermatterForcedDelamType);
if (SupermatterDoForceDelam)
return SupermatterForcedDelamType;
var mix = _atmosphere.GetContainingMixture(uid, true, true);
@@ -439,13 +435,13 @@ public sealed partial class SupermatterSystem
var absorbedGas = mix.Remove(sm.GasEfficiency * mix.TotalMoles);
var moles = absorbedGas.TotalMoles;
if (_config.GetCVar(ECCVars.SupermatterDoSingulooseDelam)
&& moles >= sm.MolePenaltyThreshold * _config.GetCVar(ECCVars.SupermatterSingulooseMolesModifier))
if (SupermatterDoSingulooseDelam
&& moles >= sm.MolePenaltyThreshold * SupermatterSingulooseMolesModifier)
return DelamType.Singulo;
}
if (_config.GetCVar(ECCVars.SupermatterDoTeslooseDelam)
&& sm.Power >= sm.PowerPenaltyThreshold * _config.GetCVar(ECCVars.SupermatterTesloosePowerModifier))
if (SupermatterDoTeslooseDelam
&& sm.Power >= sm.PowerPenaltyThreshold * SupermatterTesloosePowerModifier)
return DelamType.Tesla;
//TODO: Add resonance cascade when there's crazy conditions or a destabilizing crystal

View File

@@ -1,24 +1,17 @@
using Content.Server.AlertLevel;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Atmos.Piping.Components;
using Content.Server.Chat.Systems;
using Content.Server.Decals;
using Content.Server.DoAfter;
using Content.Server.Explosion.EntitySystems;
using Content.Server.Kitchen.Components;
using Content.Server.Lightning;
using Content.Server.Lightning.Components;
using Content.Server.Popups;
using Content.Server.Radio.EntitySystems;
using Content.Server.Speech;
using Content.Server.Station.Systems;
using Content.Shared._EE.CCVars;
using Content.Shared._EE.Supermatter.Components;
using Content.Shared._EE.Supermatter.Monitor;
using Content.Shared.Atmos;
using Content.Shared.Audio;
using Content.Shared.Body.Components;
using Content.Shared.CCVar;
using Content.Shared.DoAfter;
using Content.Shared.Examine;
using Content.Shared.Interaction;
@@ -47,9 +40,6 @@ public sealed partial class SupermatterSystem : EntitySystem
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedAmbientSoundSystem _ambient = default!;
[Dependency] private readonly LightningSystem _lightning = default!;
[Dependency] private readonly AlertLevelSystem _alert = default!;
[Dependency] private readonly StationSystem _station = default!;
[Dependency] private readonly MapSystem _map = default!;
[Dependency] private readonly DoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly PopupSystem _popup = default!;
@@ -57,14 +47,35 @@ public sealed partial class SupermatterSystem : EntitySystem
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
// As a performance optimization, we store the CVars here so that fetching them repeatedly on every frame isn't necessary.
public float SupermatterSingulooseMolesModifier { get; private set; }
public bool SupermatterDoSingulooseDelam { get; private set; }
public float SupermatterTesloosePowerModifier { get; private set; }
public bool SupermatterDoTeslooseDelam { get; private set; }
public bool SupermatterDoForceDelam { get; private set; }
public DelamType SupermatterForcedDelamType { get; private set; }
public float SupermatterRadsBase { get; private set; }
public float SupermatterRadsModifier { get; private set; }
public float SupermatterYellTimer { get; private set; }
public override void Initialize()
{
base.Initialize();
// CVar subscriptions
Subs.CVar(_config, ECCVars.SupermatterSingulooseMolesModifier, value => SupermatterSingulooseMolesModifier = value, true);
Subs.CVar(_config, ECCVars.SupermatterDoSingulooseDelam, value => SupermatterDoSingulooseDelam = value, true);
Subs.CVar(_config, ECCVars.SupermatterTesloosePowerModifier, value => SupermatterTesloosePowerModifier = value, true);
Subs.CVar(_config, ECCVars.SupermatterDoTeslooseDelam, value => SupermatterDoTeslooseDelam = value, true);
Subs.CVar(_config, ECCVars.SupermatterDoForceDelam, value => SupermatterDoForceDelam = value, true);
Subs.CVar(_config, ECCVars.SupermatterForcedDelamType, value => SupermatterForcedDelamType = value, true);
Subs.CVar(_config, ECCVars.SupermatterRadsBase, value => SupermatterRadsBase = value, true);
Subs.CVar(_config, ECCVars.SupermatterRadsModifier, value => SupermatterRadsModifier = value, true);
Subs.CVar(_config, ECCVars.SupermatterYellTimer, value => SupermatterYellTimer = value, true);
// Event subscriptions
SubscribeLocalEvent<SupermatterComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<SupermatterComponent, AtmosDeviceUpdateEvent>(OnSupermatterUpdated);
SubscribeLocalEvent<SupermatterComponent, StartCollideEvent>(OnCollideEvent);
SubscribeLocalEvent<SupermatterComponent, InteractHandEvent>(OnHandInteract);
SubscribeLocalEvent<SupermatterComponent, InteractUsingEvent>(OnItemInteract);
@@ -72,24 +83,25 @@ public sealed partial class SupermatterSystem : EntitySystem
SubscribeLocalEvent<SupermatterComponent, SupermatterDoAfterEvent>(OnGetSliver);
}
private HashSet<Entity<SupermatterComponent>> _activeSupermatterCrystals = new();
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var sm in EntityManager.EntityQuery<SupermatterComponent>())
foreach (var ent in _activeSupermatterCrystals)
{
if (!sm.Activated)
continue;
if (!ent.Comp.Activated)
_activeSupermatterCrystals.Remove(ent);
var uid = sm.Owner;
AnnounceCoreDamage(uid, sm);
AnnounceCoreDamage(ent.Owner, ent.Comp);
}
}
private void OnMapInit(EntityUid uid, SupermatterComponent sm, MapInitEvent args)
{
// Set the yell timer
sm.YellTimer = TimeSpan.FromSeconds(_config.GetCVar(ECCVars.SupermatterYellTimer));
sm.YellTimer = TimeSpan.FromSeconds(SupermatterYellTimer);
// Set the Sound
_ambient.SetAmbience(uid, true);
@@ -121,7 +133,10 @@ public sealed partial class SupermatterSystem : EntitySystem
private void OnCollideEvent(EntityUid uid, SupermatterComponent sm, ref StartCollideEvent args)
{
if (!sm.Activated)
{
sm.Activated = true;
_activeSupermatterCrystals.Add((uid, sm));
}
var target = args.OtherEntity;
if (args.OtherBody.BodyType == BodyType.Static
@@ -165,7 +180,10 @@ public sealed partial class SupermatterSystem : EntitySystem
private void OnHandInteract(EntityUid uid, SupermatterComponent sm, ref InteractHandEvent args)
{
if (!sm.Activated)
{
sm.Activated = true;
_activeSupermatterCrystals.Add((uid, sm));
}
var target = args.User;
@@ -183,7 +201,10 @@ public sealed partial class SupermatterSystem : EntitySystem
private void OnItemInteract(EntityUid uid, SupermatterComponent sm, ref InteractUsingEvent args)
{
if (!sm.Activated)
{
sm.Activated = true;
_activeSupermatterCrystals.Add((uid, sm));
}
if (sm.SliverRemoved)
return;

View File

@@ -2,7 +2,6 @@ using Content.Shared._EE.Supermatter.Monitor;
using Content.Shared.Atmos;
using Content.Shared.DoAfter;
using Content.Shared.Radio;
using Content.Shared.Speech;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
@@ -19,7 +18,7 @@ public sealed partial class SupermatterComponent : Component
/// The SM will only cycle if activated.
/// </summary>
[DataField]
public bool Activated = false;
public bool Activated;
/// <summary>
/// The current status of the singularity, used for alert sounds and the monitoring console
@@ -453,7 +452,4 @@ public sealed partial class GasFact
}
[Serializable, NetSerializable]
public sealed partial class SupermatterDoAfterEvent : SimpleDoAfterEvent
{
}
public sealed partial class SupermatterDoAfterEvent : SimpleDoAfterEvent { }

View File

@@ -128,5 +128,5 @@
volume: 2500
moles:
- 0 # oxygen
- 18710.71051 # nitrogen
- 9350.0 # nitrogen
temperature: 72