Device Linking and better linking ui (#13645)

Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
Co-authored-by: Visne <39844191+Visne@users.noreply.github.com>
Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
Julian Giebel
2023-05-07 08:07:24 +02:00
committed by GitHub
parent 2fe7055de6
commit 6ebd784cb6
100 changed files with 2096 additions and 342 deletions

View File

@@ -0,0 +1,37 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.MachineLinking.System;
namespace Content.Server.DeviceLinking.Systems;
/// <summary>
/// This handles automatically linking autolinked entities at round-start.
/// </summary>
public sealed class AutoLinkSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _deviceLinkSystem = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<AutoLinkTransmitterComponent, MapInitEvent>(OnAutoLinkMapInit);
}
private void OnAutoLinkMapInit(EntityUid uid, AutoLinkTransmitterComponent component, MapInitEvent args)
{
var xform = Transform(uid);
foreach (var receiver in EntityQuery<AutoLinkReceiverComponent>())
{
if (receiver.AutoLinkChannel != component.AutoLinkChannel)
continue; // Not ours.
var rxXform = Transform(receiver.Owner);
if (rxXform.GridUid != xform.GridUid)
continue;
_deviceLinkSystem.LinkDefaults(null, uid, receiver.Owner);
}
}
}

View File

@@ -0,0 +1,114 @@
using Content.Server.DeviceLinking.Events;
using Content.Server.DeviceNetwork;
using Content.Server.DeviceNetwork.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.MachineLinking.Components;
using Content.Shared.DeviceLinking;
using Robust.Shared.Utility;
namespace Content.Server.DeviceLinking.Systems;
public sealed class DeviceLinkSystem : SharedDeviceLinkSystem
{
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SignalTransmitterComponent, MapInitEvent>(OnTransmitterStartup);
SubscribeLocalEvent<DeviceLinkSinkComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
}
/// <summary>
/// Moves existing links from machine linking to device linking to ensure linked things still work even when the map wasn't updated yet
/// </summary>
private void OnTransmitterStartup(EntityUid sourceUid, SignalTransmitterComponent transmitterComponent, MapInitEvent args)
{
if (!TryComp<DeviceLinkSourceComponent?>(sourceUid, out var sourceComponent))
return;
Dictionary<EntityUid, List<(string, string)>> outputs = new();
foreach (var (transmitterPort, receiverPorts) in transmitterComponent.Outputs)
{
foreach (var receiverPort in receiverPorts)
{
outputs.GetOrNew(receiverPort.Uid).Add((transmitterPort, receiverPort.Port));
}
}
foreach (var (sinkUid, links) in outputs)
{
SaveLinks(null, sourceUid, sinkUid, links, sourceComponent);
}
}
#region Sending & Receiving
/// <summary>
/// Sends a network payload directed at the sink entity.
/// Just raises a <see cref="SignalReceivedEvent"/> without data if the source or the sink doesn't have a <see cref="DeviceNetworkComponent"/>
/// </summary>
/// <param name="uid">The source uid that invokes the port</param>
/// <param name="port">The port to invoke</param>
/// <param name="data">Optional data to send along</param>
/// <param name="sourceComponent"></param>
public void InvokePort(EntityUid uid, string port, NetworkPayload? data = null, DeviceLinkSourceComponent? sourceComponent = null)
{
if (!Resolve(uid, ref sourceComponent) || !sourceComponent.Outputs.TryGetValue(port, out var sinks))
return;
foreach (var sinkUid in sinks)
{
if (!sourceComponent.LinkedPorts.TryGetValue(sinkUid, out var links))
continue;
foreach (var (source, sink) in links)
{
if (source != port)
continue;
//Just skip using device networking if the source or the sink doesn't support it
if (!HasComp<DeviceNetworkComponent>(uid) || !TryComp<DeviceNetworkComponent?>(sinkUid, out var sinkNetworkComponent))
{
var eventArgs = new SignalReceivedEvent(sink, uid);
RaiseLocalEvent(sinkUid, ref eventArgs);
continue;
}
var payload = new NetworkPayload()
{
[InvokedPort] = sink
};
if (data != null)
{
//Prevent overriding the invoked port
data.Remove(InvokedPort);
foreach (var (key, value) in data)
{
payload.Add(key, value);
}
}
_deviceNetworkSystem.QueuePacket(uid, sinkNetworkComponent.Address, payload, sinkNetworkComponent.ReceiveFrequency);
}
}
}
/// <summary>
/// Checks if the payload has a port defined and if the port is present on the sink.
/// Raises a <see cref="SignalReceivedEvent"/> containing the payload when the check passes
/// </summary>
private void OnPacketReceived(EntityUid uid, DeviceLinkSinkComponent component, DeviceNetworkPacketEvent args)
{
if (!args.Data.TryGetValue(InvokedPort, out string? port) || !(component.Ports?.Contains(port) ?? false))
return;
var eventArgs = new SignalReceivedEvent(port, args.Sender, args.Data);
RaiseLocalEvent(uid, ref eventArgs);
}
#endregion
}

View File

@@ -0,0 +1,49 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.DeviceLinking.Events;
using Content.Server.Doors.Systems;
using Content.Server.MachineLinking.System;
using Content.Shared.Doors.Components;
using JetBrains.Annotations;
namespace Content.Server.DeviceLinking.Systems
{
[UsedImplicitly]
public sealed class DoorSignalControlSystem : EntitySystem
{
[Dependency] private readonly DoorSystem _doorSystem = default!;
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DoorSignalControlComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<DoorSignalControlComponent, SignalReceivedEvent>(OnSignalReceived);
}
private void OnInit(EntityUid uid, DoorSignalControlComponent component, ComponentInit args)
{
_signalSystem.EnsureSinkPorts(uid, component.OpenPort, component.ClosePort, component.TogglePort);
}
private void OnSignalReceived(EntityUid uid, DoorSignalControlComponent component, ref SignalReceivedEvent args)
{
if (!TryComp(uid, out DoorComponent? door))
return;
if (args.Port == component.OpenPort)
{
if (door.State != DoorState.Open)
_doorSystem.TryOpen(uid, door);
}
else if (args.Port == component.ClosePort)
{
if (door.State != DoorState.Closed)
_doorSystem.TryClose(uid, door);
}
else if (args.Port == component.TogglePort)
{
_doorSystem.TryToggleDoor(uid, door);
}
}
}
}

View File

@@ -0,0 +1,40 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.MachineLinking.System;
using Content.Shared.Audio;
using Content.Shared.Interaction;
using Robust.Shared.Audio;
using Robust.Shared.Player;
namespace Content.Server.DeviceLinking.Systems
{
public sealed class SignalSwitchSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SignalSwitchComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SignalSwitchComponent, ActivateInWorldEvent>(OnActivated);
}
private void OnInit(EntityUid uid, SignalSwitchComponent component, ComponentInit args)
{
_signalSystem.EnsureSourcePorts(uid, component.OnPort, component.OffPort);
}
private void OnActivated(EntityUid uid, SignalSwitchComponent component, ActivateInWorldEvent args)
{
if (args.Handled)
return;
component.State = !component.State;
_signalSystem.InvokePort(uid, component.State ? component.OnPort : component.OffPort);
SoundSystem.Play(component.ClickSound.GetSound(), Filter.Pvs(component.Owner), component.Owner,
AudioHelpers.WithVariation(0.125f).WithVolume(8f));
args.Handled = true;
}
}
}

View File

@@ -0,0 +1,49 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.Explosion.EntitySystems;
using Content.Shared.Interaction.Events;
using Content.Shared.Timing;
namespace Content.Server.DeviceLinking.Systems;
public sealed class SignallerSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _link = default!;
[Dependency] private readonly UseDelaySystem _useDelay = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SignallerComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SignallerComponent, UseInHandEvent>(OnUseInHand);
SubscribeLocalEvent<SignallerComponent, TriggerEvent>(OnTrigger);
}
private void OnInit(EntityUid uid, SignallerComponent component, ComponentInit args)
{
_link.EnsureSourcePorts(uid, component.Port);
}
private void OnUseInHand(EntityUid uid, SignallerComponent component, UseInHandEvent args)
{
if (args.Handled)
return;
_link.InvokePort(uid, component.Port);
args.Handled = true;
}
private void OnTrigger(EntityUid uid, SignallerComponent component, TriggerEvent args)
{
// if on cooldown, do nothing
var hasUseDelay = TryComp<UseDelayComponent>(uid, out var useDelay);
if (hasUseDelay && _useDelay.ActiveDelay(uid, useDelay))
return;
// set cooldown to prevent clocks
if (hasUseDelay)
_useDelay.BeginDelay(uid, useDelay);
_link.InvokePort(uid, component.Port);
args.Handled = true;
}
}

View File

@@ -0,0 +1,115 @@
using Content.Server.DeviceLinking.Components;
using Content.Shared.DeviceLinking;
using Content.Shared.Interaction;
using Content.Shared.Verbs;
using Robust.Shared.Utility;
namespace Content.Server.DeviceLinking.Systems
{
public sealed class TwoWayLeverSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
const string _leftToggleImage = "rotate_ccw.svg.192dpi.png";
const string _rightToggleImage = "rotate_cw.svg.192dpi.png";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<TwoWayLeverComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<TwoWayLeverComponent, ActivateInWorldEvent>(OnActivated);
SubscribeLocalEvent<TwoWayLeverComponent, GetVerbsEvent<InteractionVerb>>(OnGetInteractionVerbs);
}
private void OnInit(EntityUid uid, TwoWayLeverComponent component, ComponentInit args)
{
_signalSystem.EnsureSourcePorts(uid, component.LeftPort, component.RightPort, component.MiddlePort);
}
private void OnActivated(EntityUid uid, TwoWayLeverComponent component, ActivateInWorldEvent args)
{
if (args.Handled)
return;
component.State = component.State switch
{
TwoWayLeverState.Middle => component.NextSignalLeft ? TwoWayLeverState.Left : TwoWayLeverState.Right,
TwoWayLeverState.Right => TwoWayLeverState.Middle,
TwoWayLeverState.Left => TwoWayLeverState.Middle,
_ => throw new ArgumentOutOfRangeException()
};
StateChanged(uid, component);
args.Handled = true;
}
private void OnGetInteractionVerbs(EntityUid uid, TwoWayLeverComponent component, GetVerbsEvent<InteractionVerb> args)
{
if (!args.CanAccess || !args.CanInteract || (args.Hands == null))
return;
InteractionVerb verbLeft = new()
{
Act = () =>
{
component.State = component.State switch
{
TwoWayLeverState.Middle => TwoWayLeverState.Left,
TwoWayLeverState.Right => TwoWayLeverState.Middle,
_ => throw new ArgumentOutOfRangeException()
};
StateChanged(uid, component);
},
Category = VerbCategory.Lever,
Message = Loc.GetString("two-way-lever-cant"),
Disabled = component.State == TwoWayLeverState.Left,
Icon = new SpriteSpecifier.Texture(new ($"/Textures/Interface/VerbIcons/{_leftToggleImage}")),
Text = Loc.GetString("two-way-lever-left"),
};
args.Verbs.Add(verbLeft);
InteractionVerb verbRight = new()
{
Act = () =>
{
component.State = component.State switch
{
TwoWayLeverState.Left => TwoWayLeverState.Middle,
TwoWayLeverState.Middle => TwoWayLeverState.Right,
_ => throw new ArgumentOutOfRangeException()
};
StateChanged(uid, component);
},
Category = VerbCategory.Lever,
Message = Loc.GetString("two-way-lever-cant"),
Disabled = component.State == TwoWayLeverState.Right,
Icon = new SpriteSpecifier.Texture(new ($"/Textures/Interface/VerbIcons/{_rightToggleImage}")),
Text = Loc.GetString("two-way-lever-right"),
};
args.Verbs.Add(verbRight);
}
private void StateChanged(EntityUid uid, TwoWayLeverComponent component)
{
if (component.State == TwoWayLeverState.Middle)
component.NextSignalLeft = !component.NextSignalLeft;
if (TryComp(uid, out AppearanceComponent? appearance))
_appearance.SetData(uid, TwoWayLeverVisuals.State, component.State, appearance);
var port = component.State switch
{
TwoWayLeverState.Left => component.LeftPort,
TwoWayLeverState.Right => component.RightPort,
TwoWayLeverState.Middle => component.MiddlePort,
_ => throw new ArgumentOutOfRangeException()
};
_signalSystem.InvokePort(uid, port);
}
}
}