using System.Numerics; using Content.Server.DeviceNetwork.Systems; using Content.Server.Power.Components; using Content.Shared.DeviceNetwork.Components; using Content.Shared.SurveillanceCamera.Components; namespace Content.Server.SurveillanceCamera; public sealed partial class SurveillanceCameraMapSystem : EntitySystem { [Dependency] private SharedTransformSystem _transform = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnCameraInit, after: [typeof(DeviceNetworkSystem)]); SubscribeLocalEvent(OnCameraMoved); SubscribeNetworkEvent(OnRequestCameraMarkerUpdate); } private void OnCameraInit(Entity ent, ref MapInitEvent args) { UpdateCameraMarker(ent); } private void OnCameraMoved(Entity ent, ref MoveEvent args) { if (!args.ParentChanged) { UpdateCameraMarker(ent); return; } var oldGridUid = _transform.GetGrid(args.OldPosition); var newGridUid = args.Component.GridUid; if (oldGridUid != newGridUid && oldGridUid is not null && !TerminatingOrDeleted(oldGridUid.Value)) { if (TryComp(oldGridUid, out var oldMapComp)) { var netEntity = GetNetEntity(ent.Owner); if (oldMapComp.Cameras.Remove(netEntity)) Dirty(oldGridUid.Value, oldMapComp); } } if (newGridUid is not null && !TerminatingOrDeleted(newGridUid.Value)) UpdateCameraMarker(ent); } private void OnRequestCameraMarkerUpdate(RequestCameraMarkerUpdateMessage args) { var cameraEntity = GetEntity(args.CameraEntity); if (TryComp(cameraEntity, out var comp) && HasComp(cameraEntity)) UpdateCameraMarker((cameraEntity, comp)); } /// /// Updates camera data in the SurveillanceCameraMapComponent for the specified camera entity. /// public void UpdateCameraMarker(Entity camera) { var (uid, comp) = camera; if (TerminatingOrDeleted(uid)) return; if (!TryComp(uid, out TransformComponent? xform) || !TryComp(uid, out DeviceNetworkComponent? deviceNet)) return; var gridUid = xform.GridUid; if (gridUid is null) return; var netEntity = GetNetEntity(uid); var mapComp = EnsureComp(gridUid.Value); var worldPos = _transform.GetWorldPosition(xform); var gridMatrix = _transform.GetInvWorldMatrix(Transform(gridUid.Value)); var localPos = Vector2.Transform(worldPos, gridMatrix); var address = deviceNet.Address; var subnet = deviceNet.ReceiveFrequencyId ?? string.Empty; var powered = CompOrNull(uid)?.Powered ?? true; var active = comp.Active && powered; var exists = mapComp.Cameras.TryGetValue(netEntity, out var existing); if (exists && existing.Position.Equals(localPos) && existing.Active == active && existing.Address == address && existing.Subnet == subnet) { return; } var visible = exists ? existing.Visible : true; mapComp.Cameras[netEntity] = new CameraMarker { Position = localPos, Active = active, Address = address, Subnet = subnet, Visible = visible }; Dirty(gridUid.Value, mapComp); } /// /// Sets the visibility state of a camera on the camera map. /// public void SetCameraVisibility(EntityUid cameraUid, bool visible) { if (!TryComp(cameraUid, out TransformComponent? xform)) return; var gridUid = xform.GridUid; if (gridUid is null || !TryComp(gridUid.Value, out var mapComp)) return; var netEntity = GetNetEntity(cameraUid); if (mapComp.Cameras.TryGetValue(netEntity, out var marker) && marker.Visible != visible) { marker.Visible = visible; mapComp.Cameras[netEntity] = marker; Dirty(gridUid.Value, mapComp); } } /// /// Checks if a camera is currently visible on the camera map. /// public bool IsCameraVisible(EntityUid cameraUid) { if (!TryComp(cameraUid, out TransformComponent? xform)) return false; var gridUid = xform.GridUid; if (gridUid is null || !TryComp(gridUid, out var mapComp)) return false; var netEntity = GetNetEntity(cameraUid); return mapComp.Cameras.TryGetValue(netEntity, out var marker) && marker.Visible; } }