Skip to content

Commit

Permalink
Make APCs explode (when turned on) after a random delay during a powe…
Browse files Browse the repository at this point in the history
…r grid check. (new-frontiers-14#24)

* Make APCs explode after a random delay during a power grid check.

* Change buzz message according to review

* Make message hidden in chat

* Increase delay between buzzes

* Named parameters

---------

Co-authored-by: TsjipTsjip <[email protected]>
  • Loading branch information
2 people authored and dvir001 committed Aug 24, 2024
1 parent 2814fd6 commit e9de36d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
17 changes: 17 additions & 0 deletions Content.Server/Power/Components/ElectricalOverloadComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Content.Shared.Explosion;

namespace Content.Server.Power.Components;

[RegisterComponent]
public sealed partial class ElectricalOverloadComponent : Component
{
[ValidatePrototypeId<ExplosionPrototype>]
[DataField]
public string ExplosionOnOverload = "Default";

[ViewVariables]
public DateTime ExplodeAt = DateTime.MaxValue;

[ViewVariables]
public DateTime NextBuzz = DateTime.MaxValue;
}
4 changes: 4 additions & 0 deletions Content.Server/Power/EntitySystems/ApcSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public void ApcToggleBreaker(EntityUid uid, ApcComponent? apc = null, PowerNetwo
apc.MainBreakerEnabled = !apc.MainBreakerEnabled;
battery.CanDischarge = apc.MainBreakerEnabled;

RaiseLocalEvent(uid, new ApcToggledMainBreakerEvent(apc.MainBreakerEnabled));

UpdateUIState(uid, apc);
_audio.PlayPvs(apc.OnReceiveMessageSound, uid, AudioParams.Default.WithVolume(-2f));
}
Expand Down Expand Up @@ -228,3 +230,5 @@ private void OnToolUseAttempt(EntityUid uid, ApcComponent component, ToolUseAtte

[ByRefEvent]
public record struct ApcToggleMainBreakerAttemptEvent(bool Cancelled);

public record struct ApcToggledMainBreakerEvent(bool Enabled);
65 changes: 65 additions & 0 deletions Content.Server/Power/EntitySystems/ElectricalOverloadSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Content.Server.Chat.Systems;
using Content.Server.Explosion.EntitySystems;
using Content.Server.Power.Components;
using Robust.Shared.Random;

namespace Content.Server.Power.EntitySystems;

public sealed class ElectricalOverloadSystem : EntitySystem
{
[Dependency] private readonly ExplosionSystem _explosionSystem = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<ElectricalOverloadComponent, ApcToggledMainBreakerEvent>(OnApcToggleMainBreaker);
}

private void OnApcToggleMainBreaker(EntityUid uid, ElectricalOverloadComponent component, ApcToggledMainBreakerEvent args)
{
if (args.Enabled)
{
// Toggled on, means explode!
component.ExplodeAt = DateTime.Now + TimeSpan.FromSeconds(_random.NextDouble(25, 35));
component.NextBuzz = DateTime.Now + TimeSpan.FromSeconds(_random.NextDouble(3, 5));
}
else
{
// Toggled off, means cancel explosion.
component.ExplodeAt = DateTime.MaxValue;
component.NextBuzz = DateTime.MaxValue;
}
}

public override void Update(float frameTime)
{
base.Update(frameTime);

var enumerator = EntityQueryEnumerator<ElectricalOverloadComponent>();
while (enumerator.MoveNext(out var entity, out var component))
{
if (component.ExplodeAt > DateTime.Now)
{
if (component.NextBuzz > DateTime.Now)
continue;

component.NextBuzz = DateTime.Now + TimeSpan.FromSeconds(_random.NextDouble(7, 15));
_chatSystem.TrySendInGameICMessage(
entity,
Loc.GetString("electrical-overload-system-buzz"),
InGameICChatType.Emote,
hideChat: true,
ignoreActionBlocker: true
);
continue;
}

_explosionSystem.QueueExplosion(entity, component.ExplosionOnOverload, 2f, 0.5f, 2f, 1f, int.MaxValue, false, null, true);
// if the device survives, we add a bit of randomness to the next explosion time
component.ExplodeAt = DateTime.Now + TimeSpan.FromSeconds(_random.NextDouble(3, 10));
}
}
}
10 changes: 9 additions & 1 deletion Content.Server/StationEvents/Events/PowerGridCheckRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ protected override void Started(EntityUid uid, PowerGridCheckRuleComponent compo
var query = AllEntityQuery<ApcComponent, TransformComponent>();
while (query.MoveNext(out var apcUid ,out var apc, out var transform))
{
if (apc.MainBreakerEnabled && CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation)
if (apc.MainBreakerEnabled &&
CompOrNull<StationMemberComponent>(transform.GridUid)?.Station == chosenStation)
{
component.Powered.Add(apcUid);
}
}

RobustRandom.Shuffle(component.Powered);
Expand All @@ -53,6 +56,8 @@ protected override void Ended(EntityUid uid, PowerGridCheckRuleComponent compone
if(!apcComponent.MainBreakerEnabled)
_apcSystem.ApcToggleBreaker(entity, apcComponent);
}

RemComp<ElectricalOverloadComponent>(entity);
}

// Can't use the default EndAudio
Expand Down Expand Up @@ -88,7 +93,10 @@ protected override void ActiveTick(EntityUid uid, PowerGridCheckRuleComponent co
if (TryComp<ApcComponent>(selected, out var apcComponent))
{
if (apcComponent.MainBreakerEnabled)
{
_apcSystem.ApcToggleBreaker(selected, apcComponent);
AddComp<ElectricalOverloadComponent>(selected);
}
}
component.Unpowered.Add(selected);
}
Expand Down
1 change: 1 addition & 0 deletions Resources/Locale/en-US/power/overload.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
electrical-overload-system-buzz = buzzes

0 comments on commit e9de36d

Please sign in to comment.