Skip to content

Commit

Permalink
Allow AI and observers to see electrified doors (#33466)
Browse files Browse the repository at this point in the history
  • Loading branch information
metalgearsloth authored and sleepyyapril committed Jan 11, 2025
1 parent fcc908f commit 736f70b
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 37 deletions.
95 changes: 95 additions & 0 deletions Content.Client/Electrocution/ElectrocutionHUDVisualizerSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using Content.Shared.Electrocution;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.Player;

namespace Content.Client.Electrocution;

/// <summary>
/// Shows the Electrocution HUD to entities with the ShowElectrocutionHUDComponent.
/// </summary>
public sealed class ElectrocutionHUDVisualizerSystem : VisualizerSystem<ElectrocutionHUDVisualsComponent>
{
[Dependency] private readonly IPlayerManager _playerMan = default!;

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

SubscribeLocalEvent<ShowElectrocutionHUDComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<ShowElectrocutionHUDComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<ShowElectrocutionHUDComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<ShowElectrocutionHUDComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
}

private void OnPlayerAttached(Entity<ShowElectrocutionHUDComponent> ent, ref LocalPlayerAttachedEvent args)
{
ShowHUD();
}

private void OnPlayerDetached(Entity<ShowElectrocutionHUDComponent> ent, ref LocalPlayerDetachedEvent args)
{
RemoveHUD();
}

private void OnInit(Entity<ShowElectrocutionHUDComponent> ent, ref ComponentInit args)
{
if (_playerMan.LocalEntity == ent)
{
ShowHUD();
}
}

private void OnShutdown(Entity<ShowElectrocutionHUDComponent> ent, ref ComponentShutdown args)
{
if (_playerMan.LocalEntity == ent)
{
RemoveHUD();
}
}

// Show the HUD to the client.
// We have to look for all current entities that can be electrified and toggle the HUD layer on if they are.
private void ShowHUD()
{
var electrifiedQuery = AllEntityQuery<ElectrocutionHUDVisualsComponent, AppearanceComponent, SpriteComponent>();
while (electrifiedQuery.MoveNext(out var uid, out var _, out var appearanceComp, out var spriteComp))
{
if (!AppearanceSystem.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, appearanceComp))
continue;

if (electrified)
spriteComp.LayerSetVisible(ElectrifiedLayers.HUD, true);
else
spriteComp.LayerSetVisible(ElectrifiedLayers.HUD, false);
}
}

// Remove the HUD from the client.
// Find all current entities that can be electrified and hide the HUD layer.
private void RemoveHUD()
{
var electrifiedQuery = AllEntityQuery<ElectrocutionHUDVisualsComponent, AppearanceComponent, SpriteComponent>();
while (electrifiedQuery.MoveNext(out var uid, out var _, out var appearanceComp, out var spriteComp))
{

spriteComp.LayerSetVisible(ElectrifiedLayers.HUD, false);
}
}

// Toggle the HUD layer if an entity becomes (de-)electrified
protected override void OnAppearanceChange(EntityUid uid, ElectrocutionHUDVisualsComponent comp, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
return;

if (!AppearanceSystem.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, args.Component))
return;

var player = _playerMan.LocalEntity;
if (electrified && HasComp<ShowElectrocutionHUDComponent>(player))
args.Sprite.LayerSetVisible(ElectrifiedLayers.HUD, true);
else
args.Sprite.LayerSetVisible(ElectrifiedLayers.HUD, false);
}
}
4 changes: 2 additions & 2 deletions Content.Server/Electrocution/ElectrocutionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void UpdateState(float frameTime)
activated.TimeLeft -= frameTime;
if (activated.TimeLeft <= 0 || !IsPowered(uid, electrified, transform))
{
_appearance.SetData(uid, ElectrifiedVisuals.IsPowered, false);
_appearance.SetData(uid, ElectrifiedVisuals.ShowSparks, false);
RemComp<ActivatedElectrifiedComponent>(uid);
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public bool TryDoElectrifiedAct(EntityUid uid, EntityUid targetUid,
return false;

EnsureComp<ActivatedElectrifiedComponent>(uid);
_appearance.SetData(uid, ElectrifiedVisuals.IsPowered, true);
_appearance.SetData(uid, ElectrifiedVisuals.ShowSparks, true);

siemens *= electrified.SiemensCoefficient;
if (!DoCommonElectrocutionAttempt(targetUid, uid, ref siemens) || siemens <= 0)
Expand Down
9 changes: 5 additions & 4 deletions Content.Server/Power/PowerWireAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed partial class PowerWireAction : BaseWireAction
[DataField("pulseTimeout")]
private int _pulseTimeout = 30;

private ElectrocutionSystem _electrocutionSystem = default!;
private ElectrocutionSystem _electrocution = default!;

public override object StatusKey { get; } = PowerWireActionKey.Status;

Expand Down Expand Up @@ -108,7 +108,8 @@ private void SetElectrified(EntityUid used, bool setting, ElectrifiedComponent?
&& !EntityManager.TryGetComponent(used, out electrified))
return;

electrified.Enabled = setting;
_electrocution.SetElectrifiedWireCut((used, electrified), setting);
_electrocution.SetElectrified((used, electrified), setting);
}

/// <returns>false if failed, true otherwise, or if the entity cannot be electrified</returns>
Expand All @@ -122,7 +123,7 @@ private bool TrySetElectrocution(EntityUid user, Wire wire, bool timed = false)
// always set this to true
SetElectrified(wire.Owner, true, electrified);

var electrifiedAttempt = _electrocutionSystem.TryDoElectrifiedAct(wire.Owner, user);
var electrifiedAttempt = _electrocution.TryDoElectrifiedAct(wire.Owner, user);

// if we were electrified, then return false
return !electrifiedAttempt;
Expand Down Expand Up @@ -163,7 +164,7 @@ public override void Initialize()
{
base.Initialize();

_electrocutionSystem = EntitySystem.Get<ElectrocutionSystem>();
_electrocution = EntityManager.System<ElectrocutionSystem>();
}

// This should add a wire into the entity's state, whether it be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Content.Shared.Electrocution;

/// <summary>
/// Handles toggling sprite layers for the electrocution HUD to show if an entity with the ElectrifiedComponent is electrified.
/// </summary>
[RegisterComponent]
public sealed partial class ElectrocutionHUDVisualsComponent : Component;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Robust.Shared.GameStates;

namespace Content.Shared.Electrocution;

/// <summary>
/// Allow an entity to see the Electrocution HUD showing electrocuted doors.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ShowElectrocutionHUDComponent : Component;
6 changes: 4 additions & 2 deletions Content.Shared/Electrocution/SharedElectrocution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace Content.Shared.Electrocution;
[Serializable, NetSerializable]
public enum ElectrifiedLayers : byte
{
Powered
Sparks,
HUD,
}

[Serializable, NetSerializable]
public enum ElectrifiedVisuals : byte
{
IsPowered
ShowSparks, // only shown when zapping someone, deactivated after a short time
IsElectrified, // if the entity is electrified or not, used for the AI HUD
}
29 changes: 29 additions & 0 deletions Content.Shared/Electrocution/SharedElectrocutionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Content.Shared.Electrocution
{
public abstract class SharedElectrocutionSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;

public override void Initialize()
{
base.Initialize();
Expand All @@ -23,6 +25,33 @@ public void SetInsulatedSiemensCoefficient(EntityUid uid, float siemensCoefficie
Dirty(uid, insulated);
}

/// <summary>
/// Sets electrified value of component and marks dirty if required.
/// </summary>
public void SetElectrified(Entity<ElectrifiedComponent> ent, bool value)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 31 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)
{
if (ent.Comp.Enabled == value)
{
return;
}

ent.Comp.Enabled = value;
Dirty(ent, ent.Comp);

_appearance.SetData(ent.Owner, ElectrifiedVisuals.IsElectrified, value);
}

public void SetElectrifiedWireCut(Entity<ElectrifiedComponent> ent, bool value)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / YAML Linter

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 44 in Content.Shared/Electrocution/SharedElectrocutionSystem.cs

View workflow job for this annotation

GitHub Actions / Test Packaging

The type or namespace name 'ElectrifiedComponent' could not be found (are you missing a using directive or an assembly reference?)
{
if (ent.Comp.IsWireCut == value)
{
return;
}

ent.Comp.IsWireCut = value;
Dirty(ent);
}

/// <param name="uid">Entity being electrocuted.</param>
/// <param name="sourceUid">Source entity of the electrocution.</param>
/// <param name="shockDamage">How much shock damage the entity takes.</param>
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Mobs/Player/observer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
skipChecks: true
- type: Ghost
- type: GhostHearing
- type: ShowElectrocutionHUD
- type: IntrinsicRadioReceiver
- type: ActiveRadio
receiveAllChannels: true
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Mobs/Player/silicon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- type: IgnoreUIRange
- type: StationAiHeld
- type: StationAiOverlay
- type: ShowElectrocutionHUD
- type: ActionGrant
actions:
- ActionJumpToCore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
shader: unshaded
- state: panel_open
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- state: electrified
sprite: Interface/Misc/ai_hud.rsi
shader: unshaded
visible: false
map: ["enum.ElectrifiedLayers.HUD"]
- type: AnimationPlayer
- type: Physics
- type: Fixtures
Expand Down Expand Up @@ -72,6 +77,7 @@
- type: DoorBolt
- type: Appearance
- type: WiresVisuals
- type: ElectrocutionHUDVisuals
- type: ApcPowerReceiver
powerLoad: 20
- type: ExtensionCableReceiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,6 @@
hard: false
- type: Sprite
sprite: Structures/Doors/Airlocks/Standard/shuttle.rsi
snapCardinals: false
layers:
- state: closed
map: ["enum.DoorVisualLayers.Base"]
- state: closed_unlit
shader: unshaded
map: ["enum.DoorVisualLayers.BaseUnlit"]
- state: welded
map: ["enum.WeldableLayers.BaseWelded"]
- state: bolted_unlit
shader: unshaded
map: ["enum.DoorVisualLayers.BaseBolted"]
- state: emergency_unlit
shader: unshaded
map: ["enum.DoorVisualLayers.BaseEmergencyAccess"]
- state: panel_open
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- type: Wires
layoutId: Docking
- type: Door
Expand Down
14 changes: 7 additions & 7 deletions Resources/Prototypes/Entities/Structures/Walls/fence_metal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
- type: Appearance
- type: GenericVisualizer
visuals:
enum.ElectrifiedVisuals.IsPowered:
enum.ElectrifiedLayers.Powered:
enum.ElectrifiedVisuals.ShowSparks:
enum.ElectrifiedLayers.Sparks:
True: { visible: True }
False: { visible: False }
- type: AnimationPlayer
Expand All @@ -94,7 +94,7 @@
- state: straight_broken
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Physics
Expand Down Expand Up @@ -149,7 +149,7 @@
- state: straight
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Fixtures
Expand Down Expand Up @@ -202,7 +202,7 @@
- state: corner
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Fixtures
Expand Down Expand Up @@ -244,7 +244,7 @@
- state: end
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Fixtures
Expand Down Expand Up @@ -275,7 +275,7 @@
map: ["enum.DoorVisualLayers.Base"]
- state: electrified
sprite: Effects/electricity.rsi
map: [ "enum.ElectrifiedLayers.Powered" ]
map: [ "enum.ElectrifiedLayers.Sparks" ]
shader: unshaded
visible: false
- type: Fixtures
Expand Down
10 changes: 5 additions & 5 deletions Resources/Prototypes/Entities/Structures/Walls/grille.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- state: grille
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Icon
Expand Down Expand Up @@ -82,8 +82,8 @@
- type: Appearance
- type: GenericVisualizer
visuals:
enum.ElectrifiedVisuals.IsPowered:
enum.ElectrifiedLayers.Powered:
enum.ElectrifiedVisuals.ShowSparks:
enum.ElectrifiedLayers.Sparks:
True: { visible: True }
False: { visible: False }
- type: AnimationPlayer
Expand Down Expand Up @@ -176,7 +176,7 @@
- state: grille_diagonal
- state: electrified_diagonal
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Icon
Expand Down Expand Up @@ -211,7 +211,7 @@
- state: ratvargrille_diagonal
- state: electrified_diagonal
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Icon
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 736f70b

Please sign in to comment.