CatStation/Content.Server/DeviceLinking/Systems/LogicGateSystem.cs
Rouden cc2c14f8e4
Device Network refactor part 1: API improvements (#44346)
* Refactor DeviceNetwork

* Singleton

* NetworkPayloads refactor

* Reorganize DeviceNetworkSystem

* Surveillance cameras

* Fix code

* Fix build

* Fix test fail by rewriting entire singleton logic

* Properly predict examine

* Update DeviceNetworkSystem.API.cs

* Update EntityTest.cs

* Update DeviceNetworkSystem.cs

* Alright I'll do this expensive check fine

* Fix disposals debug assert

* Implement device network static events support

* Use frozen dictionaries for performance

* Update SharedDeviceNetworkSystem.Payloads.cs

* Parallel payload handling support

* Fix some spagetti-code

* Optimize NetworkPayload cancellation

* Parallel payloads are now cancellable

* Fix screens

* Fix robotics console

* Allow new methods for shared

* Mail fix

* Turret fix

* Fix network system method usage + signal and borg fix

* finally fix atmos devices OH MY GOOOD

* Remove stupid parallelism from fax

* Fix surveiollance cameras

* Working on a single thing for 2 days straight award

* Fix the test?

* Update units.yml

* Add IDevice interface because why not

* Add more device network suffixes

* Fix camera router failing test

* yaml linter peak

* Improve docs for DeviceNetworkHandlers

* fix build

* Update CartridgeLoaderSystem.Relay.cs

* Remove parallel system and EntitySystem post init

* Add more docs and also events

* Remove unnecessary caching

* Update DevicePayloadComponentSystem.cs

* Back to entity events

* Implement DeviceData

* Remove serializable from payloads

* Update DeviceData.cs

* Add a prototype for a singleton

* Fix tests

* Fix air alarms

* Update DeviceNetworkSystem.cs

* Fix cameras

* Make camera router event-less

* Fix atmos sync payloads, add more docs

* Remove singleton + instant packets

* oops remove public access

* Fix tests?

* Why the hell overload effect makes puddle sparks

* Apparently it was redundant

* Update gates.yml

* Finish documentation

* Review

* Mark relay properties as obsolete

* Struct payloads support (#blazinglyfastmemorysafe)

* Update DeviceNetworkTest.cs

* Make device structs stuff better

* Move address prefix to DeviceNetworkComponent

* Locale fixes

* Fix tests

* Doesn't know boolean logic award

* Update AtmosAlarmableSystem.cs

* Fix address predicted examine

* Partial fix to air alarmables

* Fix deployable turrets UI

* Fix firelocks, maybe

* Apparently Activatable UI was enough lol

* Fix surveillance cameras

* Rename `QueuePacket` into `SendPacket`

* Simplify DeviceData as much as possible

* Update DeviceData.cs

* Update FaxMachineComponent.cs

* Remove DeviceData because its stupid

* Update ScreenSystem.cs

* Fix build (camera map broken still)

* Fix map

* fix usings

* Review changes

* Build fix
2026-08-31 19:42:56 +00:00

175 lines
6.1 KiB
C#

using Content.Server.DeviceLinking.Components;
using Content.Shared.DeviceLinking;
using Content.Shared.DeviceLinking.Events;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Timing;
using Content.Shared.Tools.Systems;
using Robust.Shared.Audio.Systems;
namespace Content.Server.DeviceLinking.Systems;
public sealed partial class LogicGateSystem : EntitySystem
{
[Dependency] private DeviceLinkSystem _deviceLink = default!;
[Dependency] private SharedAppearanceSystem _appearance = default!;
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private SharedPopupSystem _popup = default!;
[Dependency] private SharedToolSystem _tool = default!;
[Dependency] private UseDelaySystem _useDelay = 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);
}
public override void Update(float deltaTime)
{
var query = EntityQueryEnumerator<LogicGateComponent>();
while (query.MoveNext(out var uid, out var comp))
{
// handle momentary pulses - high when received then low the next tick
if (comp.StateA == SignalState.Momentary)
{
comp.StateA = SignalState.Low;
}
if (comp.StateB == SignalState.Momentary)
{
comp.StateB = SignalState.Low;
}
// output most likely changed so update it
UpdateOutput(uid, comp);
}
}
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;
// no sound spamming
if (TryComp<UseDelayComponent>(uid, out var useDelay)
&& !_useDelay.TryResetDelay((uid, useDelay), true))
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.
// update the state for the correct port
if (args.Port == comp.InputPortA)
{
comp.StateA = SignalState.Momentary;
_appearance.SetData(uid, LogicGateVisuals.InputA, false); //If A == High => Sets input A sprite to True
}
else if (args.Port == comp.InputPortB)
{
comp.StateB = SignalState.Momentary;
_appearance.SetData(uid, LogicGateVisuals.InputB, false); //If B == High => Sets input B sprite to True
}
UpdateOutput(uid, comp);
}
[SubscribeLocalEvent]
private void OnSignalReceived(EntityUid uid, LogicGateComponent comp, ref SignalReceivedEvent<LogicStatePayload> args)
{
var state = args.Data.State;
// update the state for the correct port
if (args.Port == comp.InputPortA)
{
comp.StateA = state;
_appearance.SetData(uid, LogicGateVisuals.InputA, state == SignalState.High); //If A == High => Sets input A sprite to True
}
else if (args.Port == comp.InputPortB)
{
comp.StateB = state;
_appearance.SetData(uid, LogicGateVisuals.InputB, state == SignalState.High); //If B == High => Sets input B sprite to True
}
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
// momentary is treated as high for the current tick, after updating it will be reset to low
var a = comp.StateA != SignalState.Low;
var b = comp.StateB != SignalState.Low;
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;
}
_appearance.SetData(uid, LogicGateVisuals.Output, output);
// only send a payload if it actually changed
if (output != comp.LastOutput)
{
comp.LastOutput = output;
_deviceLink.SendSignal(uid, comp.OutputPort, output);
}
}
}