logic gate stuff (#16943)

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-06-07 23:48:42 +00:00
committed by GitHub
parent d954957a11
commit 07d2430840
29 changed files with 547 additions and 205 deletions

View File

@@ -0,0 +1,36 @@
using Content.Server.DeviceLinking.Systems;
using Content.Shared.DeviceLinking;
using Content.Shared.MachineLinking;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.DeviceLinking.Components;
/// <summary>
/// An edge detector that pulses high or low output ports when the input port gets a rising or falling edge respectively.
/// </summary>
[RegisterComponent]
[Access(typeof(EdgeDetectorSystem))]
public sealed class EdgeDetectorComponent : Component
{
/// <summary>
/// Name of the input port.
/// </summary>
[DataField("inputPort", customTypeSerializer: typeof(PrototypeIdSerializer<SourcePortPrototype>))]
public string InputPort = "Input";
/// <summary>
/// Name of the rising edge output port.
/// </summary>
[DataField("outputHighPort", customTypeSerializer: typeof(PrototypeIdSerializer<TransmitterPortPrototype>))]
public string OutputHighPort = "OutputHigh";
/// <summary>
/// Name of the falling edge output port.
/// </summary>
[DataField("outputLowPort", customTypeSerializer: typeof(PrototypeIdSerializer<TransmitterPortPrototype>))]
public string OutputLowPort = "OutputLow";
// Initial state
[ViewVariables]
public SignalState State = SignalState.Low;
}

View File

@@ -0,0 +1,73 @@
using Content.Server.DeviceLinking.Systems;
using Content.Shared.DeviceLinking;
using Content.Shared.MachineLinking;
using Content.Shared.Tools;
using Robust.Shared.Audio;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.DeviceLinking.Components;
/// <summary>
/// A logic gate that sets its output port by doing an operation on its 2 input ports, A and B.
/// </summary>
[RegisterComponent]
[Access(typeof(LogicGateSystem))]
public sealed class LogicGateComponent : Component
{
/// <summary>
/// The logic gate operation to use.
/// </summary>
[DataField("gate")]
public LogicGate Gate = LogicGate.Or;
/// <summary>
/// Tool quality to use for cycling logic gate operations.
/// Cannot be pulsing since linking uses that.
/// </summary>
[DataField("cycleQuality", customTypeSerializer: typeof(PrototypeIdSerializer<ToolQualityPrototype>))]
public string CycleQuality = "Screwing";
/// <summary>
/// Sound played when cycling logic gate operations.
/// </summary>
[DataField("cycleSound")]
public SoundSpecifier CycleSound = new SoundPathSpecifier("/Audio/Machines/lightswitch.ogg");
/// <summary>
/// Name of the first input port.
/// </summary>
[DataField("inputPortA", customTypeSerializer: typeof(PrototypeIdSerializer<SourcePortPrototype>))]
public string InputPortA = "InputA";
/// <summary>
/// Name of the second input port.
/// </summary>
[DataField("inputPortB", customTypeSerializer: typeof(PrototypeIdSerializer<SourcePortPrototype>))]
public string InputPortB = "InputB";
/// <summary>
/// Name of the output port.
/// </summary>
[DataField("outputPort", customTypeSerializer: typeof(PrototypeIdSerializer<TransmitterPortPrototype>))]
public string OutputPort = "Output";
// Initial state
[ViewVariables]
public SignalState StateA = SignalState.Low;
[ViewVariables]
public SignalState StateB = SignalState.Low;
[ViewVariables]
public bool LastOutput;
}
/// <summary>
/// Last state of a signal port, used to not spam invoking ports.
/// </summary>
public enum SignalState : byte
{
Momentary, // Instantaneous pulse high, compatibility behavior
Low,
High
}

View File

@@ -1,34 +0,0 @@
using Content.Server.MachineLinking.Events;
namespace Content.Server.DeviceLinking.Components;
[RegisterComponent]
public sealed class OrGateComponent : Component
{
// Initial state
[ViewVariables]
public SignalState StateA1 = SignalState.Low;
[ViewVariables]
public SignalState StateB1 = SignalState.Low;
[ViewVariables]
public SignalState LastO1 = SignalState.Low;
[ViewVariables]
public SignalState StateA2 = SignalState.Low;
[ViewVariables]
public SignalState StateB2 = SignalState.Low;
[ViewVariables]
public SignalState LastO2 = SignalState.Low;
}
public enum SignalState
{
Momentary, // Instantaneous pulse high, compatibility behavior
Low,
High
}

View File

@@ -65,21 +65,21 @@ namespace Content.Server.DeviceLinking.Systems
}
else if (args.Port == component.InBolt)
{
if (state == SignalState.High)
if (!TryComp<DoorBoltComponent>(uid, out var bolts))
return;
// if its a pulse toggle, otherwise set bolts to high/low
bool bolt;
if (state == SignalState.Momentary)
{
if(TryComp<DoorBoltComponent>(uid, out var bolts))
_bolts.SetBoltsWithAudio(uid, bolts, true);
}
else if (state == SignalState.Momentary)
{
if (TryComp<DoorBoltComponent>(uid, out var bolts))
_bolts.SetBoltsWithAudio(uid, bolts, newBolts: !bolts.BoltsDown);
bolt = !bolts.BoltsDown;
}
else
{
if(TryComp<DoorBoltComponent>(uid, out var bolts))
_bolts.SetBoltsWithAudio(uid, bolts, false);
bolt = state == SignalState.High;
}
_bolts.SetBoltsWithAudio(uid, bolts, bolt);
}
}

View File

@@ -0,0 +1,47 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.DeviceNetwork;
using Content.Server.MachineLinking.Events;
using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
namespace Content.Server.DeviceLinking.Systems;
public sealed class EdgeDetectorSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _deviceLink = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EdgeDetectorComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<EdgeDetectorComponent, SignalReceivedEvent>(OnSignalReceived);
}
private void OnInit(EntityUid uid, EdgeDetectorComponent comp, ComponentInit args)
{
_deviceLink.EnsureSinkPorts(uid, comp.InputPort);
_deviceLink.EnsureSourcePorts(uid, comp.OutputHighPort, comp.OutputLowPort);
}
private void OnSignalReceived(EntityUid uid, EdgeDetectorComponent comp, ref SignalReceivedEvent args)
{
// only handle signals with edges
var state = SignalState.Momentary;
if (args.Data == null ||
!args.Data.TryGetValue(DeviceNetworkConstants.LogicState, out state) ||
state == SignalState.Momentary)
return;
if (args.Port != comp.InputPort)
return;
// make sure the level changed, multiple devices sending the same level are treated as one spamming
if (comp.State != state)
{
comp.State = state;
var port = state == SignalState.High ? comp.OutputHighPort : comp.OutputLowPort;
_deviceLink.InvokePort(uid, port);
}
}
}

View File

@@ -0,0 +1,133 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.DeviceNetwork;
using Content.Server.MachineLinking.Events;
using Content.Shared.DeviceLinking;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Tools;
using Content.Shared.Popups;
using Robust.Shared.Audio;
using Robust.Shared.Utility;
using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
namespace Content.Server.DeviceLinking.Systems;
public sealed class LogicGateSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _deviceLink = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly SharedToolSystem _tool = default!;
private readonly int GateCount = Enum.GetValues(typeof(LogicGate)).Length;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LogicGateComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<LogicGateComponent, ExaminedEvent>(OnExamined);
SubscribeLocalEvent<LogicGateComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<LogicGateComponent, SignalReceivedEvent>(OnSignalReceived);
}
private void OnInit(EntityUid uid, LogicGateComponent comp, ComponentInit args)
{
_deviceLink.EnsureSinkPorts(uid, comp.InputPortA, comp.InputPortB);
_deviceLink.EnsureSourcePorts(uid, comp.OutputPort);
}
private void OnExamined(EntityUid uid, LogicGateComponent comp, ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
args.PushMarkup(Loc.GetString("logic-gate-examine", ("gate", comp.Gate.ToString().ToUpper())));
}
private void OnInteractUsing(EntityUid uid, LogicGateComponent comp, InteractUsingEvent args)
{
if (args.Handled || !_tool.HasQuality(args.Used, comp.CycleQuality))
return;
// cycle through possible gates
var gate = (int) comp.Gate;
gate = ++gate % GateCount;
comp.Gate = (LogicGate) gate;
// since gate changed the output probably has too, update it
UpdateOutput(uid, comp);
// notify the user
_audio.PlayPvs(comp.CycleSound, uid);
var msg = Loc.GetString("logic-gate-cycle", ("gate", comp.Gate.ToString().ToUpper()));
_popup.PopupEntity(msg, uid, args.User);
_appearance.SetData(uid, LogicGateVisuals.Gate, comp.Gate);
}
private void OnSignalReceived(EntityUid uid, LogicGateComponent comp, ref SignalReceivedEvent args)
{
// default to momentary for compatibility with non-logic signals.
// currently only door status and logic gates have logic signal state.
var state = SignalState.Momentary;
args.Data?.TryGetValue(DeviceNetworkConstants.LogicState, out state);
// update the state for the correct port
if (args.Port == comp.InputPortA)
{
comp.StateA = state;
}
else if (args.Port == comp.InputPortB)
{
comp.StateB = state;
}
UpdateOutput(uid, comp);
}
/// <summary>
/// Handle the logic for a logic gate, invoking the port if the output changed.
/// </summary>
private void UpdateOutput(EntityUid uid, LogicGateComponent comp)
{
// get the new output value now that it's changed
var a = comp.StateA == SignalState.High;
var b = comp.StateB == SignalState.High;
var output = false;
switch (comp.Gate)
{
case LogicGate.Or:
output = a || b;
break;
case LogicGate.And:
output = a && b;
break;
case LogicGate.Xor:
output = a != b;
break;
case LogicGate.Nor:
output = !(a || b);
break;
case LogicGate.Nand:
output = !(a && b);
break;
case LogicGate.Xnor:
output = a == b;
break;
}
// only send a payload if it actually changed
if (output != comp.LastOutput)
{
comp.LastOutput = output;
var data = new NetworkPayload
{
[DeviceNetworkConstants.LogicState] = output ? SignalState.High : SignalState.Low
};
_deviceLink.InvokePort(uid, comp.OutputPort, data);
}
}
}

View File

@@ -1,84 +0,0 @@
using Content.Server.DeviceLinking.Components;
using Content.Server.DeviceNetwork;
using Content.Server.MachineLinking.Events;
using JetBrains.Annotations;
using Robust.Shared.Utility;
using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
namespace Content.Server.DeviceLinking.Systems
{
[UsedImplicitly]
public sealed class OrGateSystem : EntitySystem
{
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<OrGateComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<OrGateComponent, SignalReceivedEvent>(OnSignalReceived);
}
private void OnInit(EntityUid uid, OrGateComponent component, ComponentInit args)
{
_signalSystem.EnsureSinkPorts(uid, "A1", "B1", "A2", "B2");
_signalSystem.EnsureSourcePorts(uid, "O1", "O2");
}
private void OnSignalReceived(EntityUid uid, OrGateComponent component, ref SignalReceivedEvent args)
{
var state = SignalState.Momentary;
args.Data?.TryGetValue(DeviceNetworkConstants.LogicState, out state);
switch (args.Port)
{
case "A1":
component.StateA1 = state;
break;
case "B1":
component.StateB1 = state;
break;
case "A2":
component.StateA2 = state;
break;
case "B2":
component.StateB2 = state;
break;
}
// O1 = A1 || B1
var v1 = SignalState.Low;
if (component.StateA1 == SignalState.High || component.StateB1 == SignalState.High)
v1 = SignalState.High;
if (v1 != component.LastO1)
{
var data = new NetworkPayload
{
[DeviceNetworkConstants.LogicState] = v1
};
_signalSystem.InvokePort(uid, "O1", data);
}
component.LastO1 = v1;
// O2 = A2 || B2
var v2 = SignalState.Low;
if (component.StateA2 == SignalState.High || component.StateB2 == SignalState.High)
v2 = SignalState.High;
if (v2 != component.LastO2)
{
var data = new NetworkPayload
{
[DeviceNetworkConstants.LogicState] = v2
};
_signalSystem.InvokePort(uid, "O2", data);
}
component.LastO2 = v2;
}
}
}