Skip to content

Commit

Permalink
Minor Glimmer System Cleanup (Simple-Station#702)
Browse files Browse the repository at this point in the history
# Description

More tedium to get out of the way, I can't do much more than this since
by now in the day my adderall has worn off. This just does basic code
cleanup of the glimmer system.

No media because there's nothing to show. No changelog because this
isn't player facing.
  • Loading branch information
VMSolidus authored Aug 9, 2024
1 parent 31e3c02 commit 05364c5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 108 deletions.
6 changes: 2 additions & 4 deletions Content.Server/Psionics/Glimmer/GlimmerCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public sealed class GlimmerSetCommand : IConsoleCommand

public async void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
return;

if (!int.TryParse(args[0], out var glimmerValue))
if (args.Length != 1
|| !int.TryParse(args[0], out var glimmerValue))
return;

var entMan = IoCManager.Resolve<IEntityManager>();
Expand Down
139 changes: 49 additions & 90 deletions Content.Server/Psionics/Glimmer/GlimmerReactiveSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@
using Content.Server.Electrocution;
using Content.Server.Lightning;
using Content.Server.Explosion.EntitySystems;
using Content.Server.Construction;
using Content.Server.Ghost;
using Content.Server.Revenant.EntitySystems;
using Content.Shared.Audio;
using Content.Shared.Construction.EntitySystems;
using Content.Shared.Coordinates.Helpers;
using Content.Shared.GameTicking;
using Content.Shared.Psionics.Glimmer;
using Content.Shared.Verbs;
using Content.Shared.StatusEffect;
using Content.Shared.Damage;
using Content.Shared.Destructible;
using Content.Shared.Construction.Components;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
using Content.Shared.Weapons.Melee.Components;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Map;
using Robust.Shared.Random;
using Robust.Shared.Physics.Components;
using Robust.Shared.Utility;

namespace Content.Server.Psionics.Glimmer
Expand All @@ -39,13 +35,12 @@ public sealed class GlimmerReactiveSystem : EntitySystem
[Dependency] private readonly LightningSystem _lightning = default!;
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly EntityLookupSystem _entityLookupSystem = default!;
[Dependency] private readonly AnchorableSystem _anchorableSystem = default!;
[Dependency] private readonly SharedDestructibleSystem _destructibleSystem = default!;
[Dependency] private readonly GhostSystem _ghostSystem = default!;
[Dependency] private readonly RevenantSystem _revenantSystem = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
[Dependency] private readonly SharedPointLightSystem _pointLightSystem = default!;
private ISawmill _sawmill = default!;

public float Accumulator = 0;
public const float UpdateFrequency = 15f;
Expand Down Expand Up @@ -78,35 +73,28 @@ private void UpdateEntityState(EntityUid uid, SharedGlimmerReactiveComponent com
{
var isEnabled = true;

if (component.RequiresApcPower)
if (TryComp(uid, out ApcPowerReceiverComponent? apcPower))
isEnabled = apcPower.Powered;
if (component.RequiresApcPower
&& TryComp(uid, out ApcPowerReceiverComponent? apcPower))
isEnabled = apcPower.Powered;

_appearanceSystem.SetData(uid, GlimmerReactiveVisuals.GlimmerTier, isEnabled ? currentGlimmerTier : GlimmerTier.Minimal);

// update ambient sound
// Update ambient sound
if (TryComp(uid, out GlimmerSoundComponent? glimmerSound)
&& TryComp(uid, out AmbientSoundComponent? ambientSoundComponent)
&& glimmerSound.GetSound(currentGlimmerTier, out SoundSpecifier? spec))
&& glimmerSound.GetSound(currentGlimmerTier, out SoundSpecifier? spec)
&& spec != null)
_sharedAmbientSoundSystem.SetSound(uid, spec, ambientSoundComponent);

// Update point light
if (component.ModulatesPointLight
&& _pointLightSystem.TryGetLight(uid, out var pointLight))
{
if (spec != null)
_sharedAmbientSoundSystem.SetSound(uid, spec, ambientSoundComponent);
_pointLightSystem.SetEnabled(uid, isEnabled ? currentGlimmerTier != GlimmerTier.Minimal : false, pointLight);
_pointLightSystem.SetEnergy(uid, pointLight.Energy + glimmerTierDelta * component.GlimmerToLightEnergyFactor, pointLight);
_pointLightSystem.SetRadius(uid, pointLight.Radius + glimmerTierDelta * component.GlimmerToLightRadiusFactor, pointLight);
}

if (component.ModulatesPointLight) //SharedPointLightComponent is now being fetched via TryGetLight.
if (_pointLightSystem.TryGetLight(uid, out var pointLight))
{
_pointLightSystem.SetEnabled(uid, isEnabled ? currentGlimmerTier != GlimmerTier.Minimal : false, pointLight);
// The light energy and radius are kept updated even when off
// to prevent the need to store additional state.
//
// Note that this doesn't handle edge cases where the
// PointLightComponent is removed while the
// GlimmerReactiveComponent is still present.
_pointLightSystem.SetEnergy(uid, pointLight.Energy + glimmerTierDelta * component.GlimmerToLightEnergyFactor, pointLight);
_pointLightSystem.SetRadius(uid, pointLight.Radius + glimmerTierDelta * component.GlimmerToLightRadiusFactor, pointLight);
}

}

/// <summary>
Expand All @@ -117,7 +105,7 @@ private void UpdateEntityState(EntityUid uid, SharedGlimmerReactiveComponent com
private void OnMapInit(EntityUid uid, SharedGlimmerReactiveComponent component, MapInitEvent args)
{
if (component.RequiresApcPower && !HasComp<ApcPowerReceiverComponent>(uid))
Logger.Warning($"{ToPrettyString(uid)} had RequiresApcPower set to true but no ApcPowerReceiverComponent was found on init.");
_sawmill.Warning($"{ToPrettyString(uid)} had RequiresApcPower set to true but no ApcPowerReceiverComponent was found on init.");

UpdateEntityState(uid, component, LastGlimmerTier, (int) LastGlimmerTier);
}
Expand Down Expand Up @@ -157,21 +145,19 @@ private void OnTierChanged(EntityUid uid, SharedGlimmerReactiveComponent compone

receiver.PowerDisabled = false;
receiver.NeedsPower = false;
} else
}
else
{
receiver.NeedsPower = true;
}
}

private void AddShockVerb(EntityUid uid, SharedGlimmerReactiveComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if(!args.CanAccess || !args.CanInteract)
return;

if (!TryComp<ApcPowerReceiverComponent>(uid, out var receiver))
return;

if (receiver.NeedsPower)
if (!args.CanAccess
|| !args.CanInteract
|| !TryComp<ApcPowerReceiverComponent>(uid, out var receiver)
|| receiver.NeedsPower)
return;

AlternativeVerb verb = new()
Expand All @@ -181,7 +167,7 @@ private void AddShockVerb(EntityUid uid, SharedGlimmerReactiveComponent componen
_sharedAudioSystem.PlayPvs(component.ShockNoises, args.User);
_electrocutionSystem.TryDoElectrocution(args.User, null, _glimmerSystem.Glimmer / 200, TimeSpan.FromSeconds((float) _glimmerSystem.Glimmer / 100), false);
},
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/Spare/poweronoff.svg.192dpi.png")),
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/Spare/poweronoff.svg.192dpi.png")),
Text = Loc.GetString("power-switch-component-toggle-verb"),
Priority = -3
};
Expand All @@ -190,10 +176,8 @@ private void AddShockVerb(EntityUid uid, SharedGlimmerReactiveComponent componen

private void OnDamageChanged(EntityUid uid, SharedGlimmerReactiveComponent component, DamageChangedEvent args)
{
if (args.Origin == null)
return;

if (!_random.Prob((float) _glimmerSystem.Glimmer / 1000))
if (args.Origin == null
|| !_random.Prob((float) _glimmerSystem.Glimmer / 1000))
return;

var tier = _glimmerSystem.GetGlimmerTier();
Expand Down Expand Up @@ -222,27 +206,23 @@ private void OnDestroyed(EntityUid uid, SharedGlimmerReactiveComponent component

private void OnUnanchorAttempt(EntityUid uid, SharedGlimmerReactiveComponent component, UnanchorAttemptEvent args)
{
if (_glimmerSystem.GetGlimmerTier() >= GlimmerTier.Dangerous)
{
_sharedAudioSystem.PlayPvs(component.ShockNoises, args.User);
_electrocutionSystem.TryDoElectrocution(args.User, null, _glimmerSystem.Glimmer / 200, TimeSpan.FromSeconds((float) _glimmerSystem.Glimmer / 100), false);
args.Cancel();
}
if (_glimmerSystem.GetGlimmerTier() < GlimmerTier.Dangerous)
return;

_sharedAudioSystem.PlayPvs(component.ShockNoises, args.User);
_electrocutionSystem.TryDoElectrocution(args.User, null, _glimmerSystem.Glimmer / 200, TimeSpan.FromSeconds((float) _glimmerSystem.Glimmer / 100), false);
args.Cancel();
}

public void BeamRandomNearProber(EntityUid prober, int targets, float range = 10f)
{
List<EntityUid> targetList = new();
foreach (var target in _entityLookupSystem.GetComponentsInRange<StatusEffectsComponent>(_transformSystem.GetMapCoordinates(prober), range))
{
if (target.AllowedEffects.Contains("Electrocution"))
targetList.Add(target.Owner);
}
foreach (var (target, status) in _entityLookupSystem.GetEntitiesInRange<StatusEffectsComponent>(_transformSystem.GetMapCoordinates(prober), range))
if (status.AllowedEffects.Contains("Electrocution"))
targetList.Add(target);

foreach(var reactive in _entityLookupSystem.GetComponentsInRange<SharedGlimmerReactiveComponent>(_transformSystem.GetMapCoordinates(prober), range))
{
targetList.Add(reactive.Owner);
}
foreach (var reactive in _entityLookupSystem.GetEntitiesInRange<SharedGlimmerReactiveComponent>(_transformSystem.GetMapCoordinates(prober), range))
targetList.Add(reactive);

_random.Shuffle(targetList);
foreach (var target in targetList)
Expand All @@ -257,10 +237,9 @@ public void BeamRandomNearProber(EntityUid prober, int targets, float range = 10

private void Beam(EntityUid prober, EntityUid target, GlimmerTier tier, bool obeyCD = true)
{
if (obeyCD && BeamCooldown != 0)
return;

if (Deleted(prober) || Deleted(target))
if (obeyCD && BeamCooldown != 0
|| Deleted(prober)
|| Deleted(target))
return;

var lxform = Transform(prober);
Expand Down Expand Up @@ -293,47 +272,27 @@ private void Beam(EntityUid prober, EntityUid target, GlimmerTier tier, bool obe

private void AnchorOrExplode(EntityUid uid)
{
var xform = Transform(uid);
if (xform.Anchored)
return;

if (!TryComp<PhysicsComponent>(uid, out var physics))
return;

var coordinates = xform.Coordinates;
var gridUid = xform.GridUid;

if (_mapManager.TryGetGrid(gridUid, out var grid))
{
var tileIndices = grid.TileIndicesFor(coordinates);
if (Transform(uid).GridUid is null)
_destructibleSystem.DestroyEntity(uid);

if (_anchorableSystem.TileFree(grid, tileIndices, physics.CollisionLayer, physics.CollisionMask) &&
_transformSystem.AnchorEntity(uid, xform))
{
return;
}
}

// Wasn't able to get a grid or a free tile, so explode.
_destructibleSystem.DestroyEntity(uid);
if (HasComp<AnchorableComponent>(uid))
_transformSystem.AnchorEntity(uid, Transform(uid));
}

private void OnMeleeThrowOnHitAttempt(Entity<SharedGlimmerReactiveComponent> ent, ref AttemptMeleeThrowOnHitEvent args)
{
var (uid, _) = ent;

if (_glimmerSystem.GetGlimmerTier() < GlimmerTier.Dangerous)
return;

args.Cancelled = true;
args.Handled = true;

_lightning.ShootRandomLightnings(uid, 10, 2, "SuperchargedLightning", 2, false);
_lightning.ShootRandomLightnings(ent, 10, 2, "SuperchargedLightning", 2, false);

// Check if the parent of the user is alive, which will be the case if the user is an item and is being held.
var zapTarget = _transformSystem.GetParentUid(args.User);
if (TryComp<MindContainerComponent>(zapTarget, out _))
_electrocutionSystem.TryDoElectrocution(zapTarget, uid, 5, TimeSpan.FromSeconds(3), true,
_electrocutionSystem.TryDoElectrocution(zapTarget, ent, 5, TimeSpan.FromSeconds(3), true,
ignoreInsulation: true);
}

Expand All @@ -360,7 +319,8 @@ public override void Update(float frameTime)
var currentGlimmerTier = _glimmerSystem.GetGlimmerTier();

var reactives = EntityQuery<SharedGlimmerReactiveComponent>();
if (currentGlimmerTier != LastGlimmerTier) {
if (currentGlimmerTier != LastGlimmerTier)
{
var glimmerTierDelta = (int) currentGlimmerTier - (int) LastGlimmerTier;
var ev = new GlimmerTierChangedEvent(LastGlimmerTier, currentGlimmerTier, glimmerTierDelta);

Expand All @@ -378,10 +338,9 @@ public override void Update(float frameTime)
_revenantSystem.MakeVisible(true);
GhostsVisible = true;
foreach (var reactive in reactives)
{
BeamRandomNearProber(reactive.Owner, 1, 12);
}
} else if (GhostsVisible == true)
}
else if (GhostsVisible == true)
{
_ghostSystem.MakeVisible(false);
_revenantSystem.MakeVisible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Content.Shared.CCVar;
using Content.Shared.Psionics.Glimmer;
using Content.Shared.GameTicking;
using Content.Server.CartridgeLoader.Cartridges;

namespace Content.Server.Psionics.Glimmer
{
Expand All @@ -17,7 +16,6 @@ public sealed class PassiveGlimmerReductionSystem : EntitySystem
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly GlimmerMonitorCartridgeSystem _cartridgeSys = default!;

/// List of glimmer values spaced by minute.
public List<int> GlimmerValues = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace Content.Server.Psionics.Glimmer
/// </summary>
public sealed partial class GlimmerSourceComponent : Component
{
[DataField("accumulator")]
[DataField]
public float Accumulator = 0f;

[DataField("active")]
[DataField]
public bool Active = true;

/// <summary>
/// Since glimmer is an int, we'll do it like this.
/// </summary>
[DataField("secondsPerGlimmer")]
[DataField]
public float SecondsPerGlimmer = 10f;

/// <summary>
/// True if it produces glimmer, false if it subtracts it.
/// </summary>
[DataField("addToGlimmer")]
[DataField]
public bool AddToGlimmer = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ public override void Update(float frameTime)
base.Update(frameTime);
foreach (var source in EntityQuery<GlimmerSourceComponent>())
{
if (!_powerReceiverSystem.IsPowered(source.Owner))
continue;

if (!source.Active)
if (!_powerReceiverSystem.IsPowered(source.Owner)
|| !source.Active)
continue;

source.Accumulator += frameTime;
Expand All @@ -70,13 +68,9 @@ public override void Update(float frameTime)
{
source.Accumulator -= source.SecondsPerGlimmer;
if (source.AddToGlimmer)
{
_glimmerSystem.Glimmer++;
}
else
{
_glimmerSystem.Glimmer--;
}
}
}
}
Expand Down

0 comments on commit 05364c5

Please sign in to comment.