CatStation/Content.Server/Turrets/DeployableTurretSystem.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

203 lines
8.1 KiB
C#

using Content.Server.Destructible;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.NPC.HTN;
using Content.Server.NPC.HTN.PrimitiveTasks.Operators.Combat.Ranged;
using Content.Shared.Destructible;
using Content.Shared.DeviceNetwork;
using Content.Shared.DeviceNetwork.Components;
using Content.Shared.DeviceNetwork.Events;
using Content.Shared.Power;
using Content.Shared.Repairable;
using Content.Shared.TurretController;
using Content.Shared.Turrets;
using Content.Shared.Weapons.Ranged.Components;
using Content.Shared.Weapons.Ranged.Events;
using Content.Shared.Weapons.Ranged.Systems;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Timing;
namespace Content.Server.Turrets;
public sealed partial class DeployableTurretSystem : SharedDeployableTurretSystem
{
[Dependency] private HTNSystem _htn = default!;
[Dependency] private SharedAppearanceSystem _appearance = default!;
[Dependency] private SharedAudioSystem _audio = default!;
[Dependency] private DeviceNetworkSystem _deviceNetwork = default!;
[Dependency] private BatteryWeaponFireModesSystem _fireModes = default!;
[Dependency] private TurretTargetSettingsSystem _turretTargetingSettings = default!;
[Dependency] private IGameTiming _timing = default!;
[Dependency] private EntityQuery<DeviceNetworkComponent> _deviceQuery = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DeployableTurretComponent, AmmoShotEvent>(OnAmmoShot);
SubscribeLocalEvent<DeployableTurretComponent, ChargeChangedEvent>(OnChargeChanged);
SubscribeLocalEvent<DeployableTurretComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<DeployableTurretComponent, BreakageEventArgs>(OnBroken);
SubscribeLocalEvent<DeployableTurretComponent, RepairedEvent>(OnRepaired);
SubscribeLocalEvent<DeployableTurretComponent, BeforeBroadcastAttemptEvent>(OnBeforeBroadcast);
}
private void OnAmmoShot(Entity<DeployableTurretComponent> ent, ref AmmoShotEvent args)
{
UpdateAmmoStatus(ent);
}
private void OnChargeChanged(Entity<DeployableTurretComponent> ent, ref ChargeChangedEvent args)
{
UpdateAmmoStatus(ent);
}
private void OnPowerChanged(Entity<DeployableTurretComponent> ent, ref PowerChangedEvent args)
{
UpdateAmmoStatus(ent);
}
private void OnBroken(Entity<DeployableTurretComponent> ent, ref BreakageEventArgs args)
{
if (TryComp<AppearanceComponent>(ent, out var appearance))
_appearance.SetData(ent, DeployableTurretVisuals.Broken, true, appearance);
SetState(ent, false);
}
private void OnRepaired(Entity<DeployableTurretComponent> ent, ref RepairedEvent args)
{
if (TryComp<AppearanceComponent>(ent, out var appearance))
_appearance.SetData(ent, DeployableTurretVisuals.Broken, false, appearance);
}
[SubscribeLocalEvent]
private void OnSetArmament(Entity<DeployableTurretComponent> ent, ref DeviceNetworkPacketEvent<TurretControllerSetArmamentPayload> args)
{
if (TryComp<BatteryWeaponFireModesComponent>(ent, out var batteryWeaponFireModes))
_fireModes.TrySetFireMode((ent.Owner, batteryWeaponFireModes), args.Data.ArmamentState);
TrySetState(ent, args.Data.ArmamentState >= 0);
}
[SubscribeLocalEvent]
private void OnSetAccess(Entity<DeployableTurretComponent> ent, ref DeviceNetworkPacketEvent<TurretControllerSetAccessPayload> args)
{
if (!TryComp<TurretTargetSettingsComponent>(ent, out var targetSettings))
return;
_turretTargetingSettings.SyncAccessLevelExemptions((ent, targetSettings), args.Data.AccessExemptions);
}
[SubscribeLocalEvent]
private void OnRequest(Entity<DeployableTurretComponent> ent, ref DeviceNetworkPacketEvent<TurretControllerRequestPayload> args)
{
SendStateUpdateToDeviceNetwork(ent);
}
private void OnBeforeBroadcast(Entity<DeployableTurretComponent> ent, ref BeforeBroadcastAttemptEvent args)
{
if (!TryComp<DeviceNetworkComponent>(ent, out var deviceNetwork))
return;
var recipientDeviceNetworks = new HashSet<Device>();
// Only broadcast to connected devices
foreach (var recipient in deviceNetwork.DeviceLists)
{
if (!_deviceQuery.TryComp(recipient, out var recipientDeviceNetwork))
continue;
recipientDeviceNetworks.Add(new Device((recipient, recipientDeviceNetwork)));
}
if (recipientDeviceNetworks.Count > 0)
args.ModifiedRecipients = recipientDeviceNetworks;
}
private void SendStateUpdateToDeviceNetwork(Entity<DeployableTurretComponent> ent)
{
if (!TryComp<DeviceNetworkComponent>(ent, out var device))
return;
var payload = new TurretStatePayload
{
State = GetTurretState(ent),
};
_deviceNetwork.SendPacket((ent.Owner, device), null, ref payload);
}
protected override void SetState(Entity<DeployableTurretComponent> ent, bool enabled, EntityUid? user = null)
{
if (ent.Comp.Enabled == enabled)
return;
base.SetState(ent, enabled, user);
DirtyField(ent, ent.Comp, nameof(DeployableTurretComponent.Enabled));
// Determine how much time is remaining in the current animation and the one next in queue
var animTimeRemaining = MathF.Max((float)(ent.Comp.AnimationCompletionTime - _timing.CurTime).TotalSeconds, 0f);
var animTimeNext = ent.Comp.Enabled ? ent.Comp.DeploymentLength : ent.Comp.RetractionLength;
// End/restart any tasks the NPC was doing
// Delay the resumption of any tasks based on the total animation length (plus a buffer)
var planCooldown = animTimeRemaining + animTimeNext + 0.5f;
if (TryComp<HTNComponent>(ent, out var htn))
_htn.SetHTNEnabled((ent, htn), ent.Comp.Enabled, planCooldown);
// Play audio
_audio.PlayPvs(ent.Comp.Enabled ? ent.Comp.DeploymentSound : ent.Comp.RetractionSound, ent, new AudioParams { Volume = -10f });
}
private void UpdateAmmoStatus(Entity<DeployableTurretComponent> ent)
{
if (!HasAmmo(ent))
SetState(ent, false);
}
private DeployableTurretState GetTurretState(Entity<DeployableTurretComponent> ent, DestructibleComponent? destructable = null, HTNComponent? htn = null)
{
Resolve(ent, ref destructable, ref htn);
if (destructable?.IsBroken == true)
return DeployableTurretState.Broken;
if (htn == null || !HasAmmo(ent))
return DeployableTurretState.Disabled;
if (htn.Plan?.CurrentTask.Operator is GunOperator)
return DeployableTurretState.Firing;
if (ent.Comp.AnimationCompletionTime > _timing.CurTime)
return ent.Comp.Enabled ? DeployableTurretState.Deploying : DeployableTurretState.Retracting;
return ent.Comp.Enabled ? DeployableTurretState.Deployed : DeployableTurretState.Retracted;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<DeployableTurretComponent, DestructibleComponent, HTNComponent>();
while (query.MoveNext(out var uid, out var deployableTurret, out var destructible, out var htn))
{
// Check if the turret state has changed since the last update,
// and if it has, inform the device network
var ent = new Entity<DeployableTurretComponent>(uid, deployableTurret);
var newState = GetTurretState(ent, destructible, htn);
if (newState != deployableTurret.CurrentState)
{
deployableTurret.CurrentState = newState;
DirtyField(uid, deployableTurret, nameof(DeployableTurretComponent.CurrentState));
SendStateUpdateToDeviceNetwork(ent);
if (TryComp<AppearanceComponent>(ent, out var appearance))
_appearance.SetData(ent, DeployableTurretVisuals.Turret, newState, appearance);
}
}
}
}