Decouple panels and wires (#24840)

decouple panels and wires
This commit is contained in:
Nemanja
2024-02-02 23:24:33 -05:00
committed by GitHub
parent c4e7f1ebe8
commit 5db6a3eafc
3 changed files with 125 additions and 79 deletions

View File

@@ -1,51 +1,39 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using Content.Server.Administration.Logs;
using Content.Server.Construction; using Content.Server.Construction;
using Content.Server.Construction.Components; using Content.Server.Construction.Components;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Shared.UserInterface; using Content.Server.UserInterface;
using Content.Shared.Database;
using Content.Shared.DoAfter; using Content.Shared.DoAfter;
using Content.Shared.GameTicking; using Content.Shared.GameTicking;
using Content.Shared.Hands.Components; using Content.Shared.Hands.Components;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Popups; using Content.Shared.Popups;
using Content.Shared.Tools;
using Content.Shared.Tools.Components; using Content.Shared.Tools.Components;
using Content.Shared.UserInterface;
using Content.Shared.Wires; using Content.Shared.Wires;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Random; using Robust.Shared.Random;
using SharedToolSystem = Content.Shared.Tools.Systems.SharedToolSystem;
using Content.Server.UserInterface;
namespace Content.Server.Wires; namespace Content.Server.Wires;
public sealed class WiresSystem : SharedWiresSystem public sealed class WiresSystem : SharedWiresSystem
{ {
[Dependency] private readonly IPrototypeManager _protoMan = default!; [Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly ActivatableUISystem _activatableUI = default!; [Dependency] private readonly ActivatableUISystem _activatableUI = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!; [Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!; [Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!; [Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!; [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly ConstructionSystem _construction = default!; [Dependency] private readonly ConstructionSystem _construction = default!;
// This is where all the wire layouts are stored. // This is where all the wire layouts are stored.
[ViewVariables] private readonly Dictionary<string, WireLayout> _layouts = new(); [ViewVariables] private readonly Dictionary<string, WireLayout> _layouts = new();
private const float ScrewTime = 1f;
private float _toolTime = 0f; private float _toolTime = 0f;
#region Initialization #region Initialization
@@ -56,8 +44,7 @@ public sealed class WiresSystem : SharedWiresSystem
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset); SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
// this is a broadcast event // this is a broadcast event
SubscribeLocalEvent<WiresPanelComponent, WirePanelDoAfterEvent>(OnPanelDoAfter); SubscribeLocalEvent<WiresComponent, PanelChangedEvent>(OnPanelChanged);
SubscribeLocalEvent<WiresComponent, ComponentStartup>(OnWiresStartup);
SubscribeLocalEvent<WiresComponent, WiresActionMessage>(OnWiresActionMessage); SubscribeLocalEvent<WiresComponent, WiresActionMessage>(OnWiresActionMessage);
SubscribeLocalEvent<WiresComponent, InteractUsingEvent>(OnInteractUsing); SubscribeLocalEvent<WiresComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<WiresComponent, MapInitEvent>(OnMapInit); SubscribeLocalEvent<WiresComponent, MapInitEvent>(OnMapInit);
@@ -68,6 +55,7 @@ public sealed class WiresSystem : SharedWiresSystem
SubscribeLocalEvent<ActivatableUIRequiresPanelComponent, PanelChangedEvent>(OnActivatableUIPanelChanged); SubscribeLocalEvent<ActivatableUIRequiresPanelComponent, PanelChangedEvent>(OnActivatableUIPanelChanged);
SubscribeLocalEvent<WiresPanelSecurityComponent, WiresPanelSecurityEvent>(SetWiresPanelSecurity); SubscribeLocalEvent<WiresPanelSecurityComponent, WiresPanelSecurityEvent>(SetWiresPanelSecurity);
} }
private void SetOrCreateWireLayout(EntityUid uid, WiresComponent? wires = null) private void SetOrCreateWireLayout(EntityUid uid, WiresComponent? wires = null)
{ {
if (!Resolve(uid, ref wires)) if (!Resolve(uid, ref wires))
@@ -246,11 +234,6 @@ public sealed class WiresSystem : SharedWiresSystem
position, position,
action); action);
} }
private void OnWiresStartup(EntityUid uid, WiresComponent component, ComponentStartup args)
{
EnsureComp<WiresPanelComponent>(uid);
}
#endregion #endregion
#region DoAfters #region DoAfters
@@ -464,49 +447,28 @@ public sealed class WiresSystem : SharedWiresSystem
if (args.Handled) if (args.Handled)
return; return;
if (!TryComp<ToolComponent>(args.Used, out var tool) || !TryComp<WiresPanelComponent>(uid, out var panel)) if (!TryComp<ToolComponent>(args.Used, out var tool))
return; return;
if (panel.Open && if (!IsPanelOpen(uid))
(_toolSystem.HasQuality(args.Used, "Cutting", tool) || return;
_toolSystem.HasQuality(args.Used, "Pulsing", tool)))
if (Tool.HasQuality(args.Used, "Cutting", tool) ||
Tool.HasQuality(args.Used, "Pulsing", tool))
{ {
if (TryComp<WiresPanelSecurityComponent>(uid, out var wiresPanelSecurity) &&
!wiresPanelSecurity.WiresAccessible)
return;
if (TryComp(args.User, out ActorComponent? actor)) if (TryComp(args.User, out ActorComponent? actor))
{ {
_uiSystem.TryOpen(uid, WiresUiKey.Key, actor.PlayerSession); _uiSystem.TryOpen(uid, WiresUiKey.Key, actor.PlayerSession);
args.Handled = true; args.Handled = true;
} }
} }
else if (_toolSystem.UseTool(args.Used, args.User, uid, ScrewTime, "Screwing", new WirePanelDoAfterEvent(), toolComponent: tool))
{
_adminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(args.User):user} is screwing {ToPrettyString(uid):target}'s {(panel.Open ? "open" : "closed")} maintenance panel at {Transform(uid).Coordinates:targetlocation}");
args.Handled = true;
}
} }
private void OnPanelDoAfter(EntityUid uid, WiresPanelComponent panel, WirePanelDoAfterEvent args) private void OnPanelChanged(Entity<WiresComponent> ent, ref PanelChangedEvent args)
{ {
if (args.Cancelled) if (args.Open)
return; return;
_uiSystem.TryCloseAll(ent, WiresUiKey.Key);
TogglePanel(uid, panel, !panel.Open);
UpdateAppearance(uid, panel);
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(args.User):user} screwed {ToPrettyString(uid):target}'s maintenance panel {(panel.Open ? "open" : "closed")}");
if (panel.Open)
{
_audio.PlayPvs(panel.ScrewdriverOpenSound, uid);
}
else
{
_audio.PlayPvs(panel.ScrewdriverCloseSound, uid);
_uiSystem.TryCloseAll(uid, WiresUiKey.Key);
}
} }
private void OnAttemptOpenActivatableUI(EntityUid uid, ActivatableUIRequiresPanelComponent component, ActivatableUIOpenAttemptEvent args) private void OnAttemptOpenActivatableUI(EntityUid uid, ActivatableUIRequiresPanelComponent component, ActivatableUIOpenAttemptEvent args)
@@ -665,16 +627,6 @@ public sealed class WiresSystem : SharedWiresSystem
Dirty(uid, component); Dirty(uid, component);
} }
public void TogglePanel(EntityUid uid, WiresPanelComponent component, bool open)
{
component.Open = open;
UpdateAppearance(uid, component);
Dirty(uid, component);
var ev = new PanelChangedEvent(component.Open);
RaiseLocalEvent(uid, ref ev);
}
public void SetWiresPanelSecurity(EntityUid uid, WiresPanelSecurityComponent component, WiresPanelSecurityEvent args) public void SetWiresPanelSecurity(EntityUid uid, WiresPanelSecurityComponent component, WiresPanelSecurityEvent args)
{ {
component.Examine = args.Examine; component.Examine = args.Examine;
@@ -688,12 +640,6 @@ public sealed class WiresSystem : SharedWiresSystem
} }
} }
private void UpdateAppearance(EntityUid uid, WiresPanelComponent panel)
{
if (TryComp<AppearanceComponent>(uid, out var appearance))
_appearance.SetData(uid, WiresVisuals.MaintenancePanelState, panel.Open && panel.Visible, appearance);
}
private void TryDoWireAction(EntityUid target, EntityUid user, EntityUid toolEntity, int id, WiresAction action, WiresComponent? wires = null, ToolComponent? tool = null) private void TryDoWireAction(EntityUid target, EntityUid user, EntityUid toolEntity, int id, WiresAction action, WiresComponent? wires = null, ToolComponent? tool = null)
{ {
if (!Resolve(target, ref wires) if (!Resolve(target, ref wires)
@@ -711,7 +657,7 @@ public sealed class WiresSystem : SharedWiresSystem
switch (action) switch (action)
{ {
case WiresAction.Cut: case WiresAction.Cut:
if (!_toolSystem.HasQuality(toolEntity, "Cutting", tool)) if (!Tool.HasQuality(toolEntity, "Cutting", tool))
{ {
_popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user); _popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user);
return; return;
@@ -725,7 +671,7 @@ public sealed class WiresSystem : SharedWiresSystem
break; break;
case WiresAction.Mend: case WiresAction.Mend:
if (!_toolSystem.HasQuality(toolEntity, "Cutting", tool)) if (!Tool.HasQuality(toolEntity, "Cutting", tool))
{ {
_popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user); _popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user);
return; return;
@@ -739,7 +685,7 @@ public sealed class WiresSystem : SharedWiresSystem
break; break;
case WiresAction.Pulse: case WiresAction.Pulse:
if (!_toolSystem.HasQuality(toolEntity, "Pulsing", tool)) if (!Tool.HasQuality(toolEntity, "Pulsing", tool))
{ {
_popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-multitool"), user); _popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-multitool"), user);
return; return;
@@ -798,7 +744,7 @@ public sealed class WiresSystem : SharedWiresSystem
switch (action) switch (action)
{ {
case WiresAction.Cut: case WiresAction.Cut:
if (!_toolSystem.HasQuality(toolEntity, "Cutting", tool)) if (!Tool.HasQuality(toolEntity, "Cutting", tool))
{ {
_popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user); _popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user);
break; break;
@@ -810,7 +756,7 @@ public sealed class WiresSystem : SharedWiresSystem
break; break;
} }
_toolSystem.PlayToolSound(toolEntity, tool, user); Tool.PlayToolSound(toolEntity, tool, user);
if (wire.Action == null || wire.Action.Cut(user, wire)) if (wire.Action == null || wire.Action.Cut(user, wire))
{ {
wire.IsCut = true; wire.IsCut = true;
@@ -819,7 +765,7 @@ public sealed class WiresSystem : SharedWiresSystem
UpdateUserInterface(used); UpdateUserInterface(used);
break; break;
case WiresAction.Mend: case WiresAction.Mend:
if (!_toolSystem.HasQuality(toolEntity, "Cutting", tool)) if (!Tool.HasQuality(toolEntity, "Cutting", tool))
{ {
_popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user); _popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-wirecutters"), user);
break; break;
@@ -831,7 +777,7 @@ public sealed class WiresSystem : SharedWiresSystem
break; break;
} }
_toolSystem.PlayToolSound(toolEntity, tool, user); Tool.PlayToolSound(toolEntity, tool, user);
if (wire.Action == null || wire.Action.Mend(user, wire)) if (wire.Action == null || wire.Action.Mend(user, wire))
{ {
wire.IsCut = false; wire.IsCut = false;
@@ -840,7 +786,7 @@ public sealed class WiresSystem : SharedWiresSystem
UpdateUserInterface(used); UpdateUserInterface(used);
break; break;
case WiresAction.Pulse: case WiresAction.Pulse:
if (!_toolSystem.HasQuality(toolEntity, "Pulsing", tool)) if (!Tool.HasQuality(toolEntity, "Pulsing", tool))
{ {
_popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-multitool"), user); _popupSystem.PopupCursor(Loc.GetString("wires-component-ui-on-receive-message-need-multitool"), user);
break; break;
@@ -855,7 +801,7 @@ public sealed class WiresSystem : SharedWiresSystem
wire.Action?.Pulse(user, wire); wire.Action?.Pulse(user, wire);
UpdateUserInterface(used); UpdateUserInterface(used);
_audio.PlayPvs(wires.PulseSound, used); Audio.PlayPvs(wires.PulseSound, used);
break; break;
} }

View File

@@ -1,29 +1,71 @@
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Tools.Systems; using Content.Shared.Tools.Systems;
using Robust.Shared.Prototypes; using Robust.Shared.Audio.Systems;
namespace Content.Shared.Wires; namespace Content.Shared.Wires;
public abstract class SharedWiresSystem : EntitySystem public abstract class SharedWiresSystem : EntitySystem
{ {
[Dependency] protected readonly ISharedAdminLogManager AdminLogger = default!;
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] protected readonly SharedAudioSystem Audio = default!;
[Dependency] protected readonly SharedToolSystem Tool = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<WiresPanelComponent, WirePanelDoAfterEvent>(OnPanelDoAfter);
SubscribeLocalEvent<WiresPanelComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<WiresPanelComponent, ExaminedEvent>(OnExamine); SubscribeLocalEvent<WiresPanelComponent, ExaminedEvent>(OnExamine);
} }
private void OnPanelDoAfter(EntityUid uid, WiresPanelComponent panel, WirePanelDoAfterEvent args)
{
if (args.Cancelled)
return;
TogglePanel(uid, panel, !panel.Open);
AdminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(args.User):user} screwed {ToPrettyString(uid):target}'s maintenance panel {(panel.Open ? "open" : "closed")}");
var sound = panel.Open ? panel.ScrewdriverOpenSound : panel.ScrewdriverCloseSound;
Audio.PlayPredicted(sound, uid, args.User);
}
private void OnInteractUsing(Entity<WiresPanelComponent> ent, ref InteractUsingEvent args)
{
if (!Tool.UseTool(
args.Used,
args.User,
ent,
(float) ent.Comp.OpenDelay.TotalSeconds,
ent.Comp.OpeningTool,
new WirePanelDoAfterEvent()))
{
return;
}
AdminLogger.Add(LogType.Action, LogImpact.Low,
$"{ToPrettyString(args.User):user} is screwing {ToPrettyString(ent):target}'s {(ent.Comp.Open ? "open" : "closed")} maintenance panel at {Transform(ent).Coordinates:targetlocation}");
args.Handled = true;
}
private void OnExamine(EntityUid uid, WiresPanelComponent component, ExaminedEvent args) private void OnExamine(EntityUid uid, WiresPanelComponent component, ExaminedEvent args)
{ {
using (args.PushGroup(nameof(WiresPanelComponent))) using (args.PushGroup(nameof(WiresPanelComponent)))
{ {
if (!component.Open) if (!component.Open)
{ {
args.PushMarkup(Loc.GetString("wires-panel-component-on-examine-closed")); if (!string.IsNullOrEmpty(component.ExamineTextClosed))
args.PushMarkup(Loc.GetString(component.ExamineTextClosed));
} }
else else
{ {
args.PushMarkup(Loc.GetString("wires-panel-component-on-examine-open")); if (!string.IsNullOrEmpty(component.ExamineTextOpen))
args.PushMarkup(Loc.GetString(component.ExamineTextOpen));
if (TryComp<WiresPanelSecurityComponent>(uid, out var wiresPanelSecurity) && if (TryComp<WiresPanelSecurityComponent>(uid, out var wiresPanelSecurity) &&
wiresPanelSecurity.Examine != null) wiresPanelSecurity.Examine != null)
@@ -33,4 +75,34 @@ public abstract class SharedWiresSystem : EntitySystem
} }
} }
} }
protected void UpdateAppearance(EntityUid uid, WiresPanelComponent panel)
{
if (TryComp<AppearanceComponent>(uid, out var appearance))
Appearance.SetData(uid, WiresVisuals.MaintenancePanelState, panel.Open && panel.Visible, appearance);
}
public void TogglePanel(EntityUid uid, WiresPanelComponent component, bool open)
{
component.Open = open;
UpdateAppearance(uid, component);
Dirty(uid, component);
var ev = new PanelChangedEvent(component.Open);
RaiseLocalEvent(uid, ref ev);
}
public bool IsPanelOpen(Entity<WiresPanelComponent?> entity)
{
if (!Resolve(entity, ref entity.Comp, false))
return true;
// Listen, i don't know what the fuck this component does. it's stapled on shit for airlocks
// but it looks like an almost direct duplication of WiresPanelComponent except with a shittier API.
if (TryComp<WiresPanelSecurityComponent>(entity, out var wiresPanelSecurity) &&
!wiresPanelSecurity.WiresAccessible)
return false;
return entity.Comp.Open;
}
} }

View File

@@ -1,5 +1,7 @@
using Content.Shared.Tools;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Wires; namespace Content.Shared.Wires;
@@ -27,6 +29,32 @@ public sealed partial class WiresPanelComponent : Component
[DataField("screwdriverCloseSound")] [DataField("screwdriverCloseSound")]
public SoundSpecifier ScrewdriverCloseSound = new SoundPathSpecifier("/Audio/Machines/screwdriverclose.ogg"); public SoundSpecifier ScrewdriverCloseSound = new SoundPathSpecifier("/Audio/Machines/screwdriverclose.ogg");
/// <summary>
/// Amount of times in seconds it takes to open
/// </summary>
[DataField]
public TimeSpan OpenDelay = TimeSpan.FromSeconds(1);
/// <summary>
/// The tool quality needed to open this panel.
/// </summary>
[DataField]
public ProtoId<ToolQualityPrototype> OpeningTool = "Screwing";
/// <summary>
/// Text showed on examine when the panel is closed.
/// </summary>
/// <returns></returns>
[DataField]
public LocId? ExamineTextClosed = "wires-panel-component-on-examine-closed";
/// <summary>
/// Text showed on examine when the panel is open.
/// </summary>
/// <returns></returns>
[DataField]
public LocId? ExamineTextOpen = "wires-panel-component-on-examine-open";
} }
/// <summary> /// <summary>