using Content.Server.GameTicking; using Content.Server.GameTicking.Rules; using Content.Server.GameTicking.Rules.Components; using Content.Shared.PowerCell; using Content.Shared.Pinpointer; using Content.Shared.Station; using Robust.Server.GameObjects; namespace Content.Server.Pinpointer; public sealed class StationMapSystem : EntitySystem { [Dependency] private readonly UserInterfaceSystem _ui = default!; [Dependency] private readonly PowerCellSystem _cell = default!; [Dependency] private readonly SharedStationSystem _station = default!; [Dependency] private readonly SharedTransformSystem _xform = default!; [Dependency] private readonly GameTicker _gameTicker = default!; public override void Initialize() { base.Initialize(); SubscribeLocalEvent(OnMapInit); SubscribeLocalEvent(OnUserParentChanged); SubscribeLocalEvent(OnNukeopsStationSelected); Subs.BuiEvents(StationMapUiKey.Key, subs => { subs.Event(OnStationMapOpened); subs.Event(OnStationMapClosed); }); } private void OnMapInit(Entity ent, ref MapInitEvent args) { if (!ent.Comp.InitializeWithStation) return; // If we ever find a need to make more exceptions like this, just turn this into an event. if (HasComp(ent)) { foreach (var rule in _gameTicker.GetActiveGameRules()) { if (TryComp(rule, out var nukeopsRule) && nukeopsRule.TargetStation != null) { ent.Comp.TargetGrid = _station.GetLargestGrid((nukeopsRule.TargetStation.Value, null)); Dirty(ent); return; } } } var station = _station.GetStationInMap(_xform.GetMapId(ent.Owner)); if (station != null) { ent.Comp.TargetGrid = _station.GetLargestGrid((station.Value, null)); Dirty(ent); } } private void OnStationMapClosed(EntityUid uid, StationMapComponent component, BoundUIClosedEvent args) { if (!Equals(args.UiKey, StationMapUiKey.Key)) return; RemCompDeferred(args.Actor); } private void OnUserParentChanged(EntityUid uid, StationMapUserComponent component, ref EntParentChangedMessage args) { _ui.CloseUi(component.Map, StationMapUiKey.Key, uid); } private void OnStationMapOpened(EntityUid uid, StationMapComponent component, BoundUIOpenedEvent args) { if (!_cell.TryUseActivatableCharge(uid)) return; var comp = EnsureComp(args.Actor); comp.Map = uid; } private void OnNukeopsStationSelected(Entity ent, ref NukeopsTargetStationSelectedEvent args) { if (args.TargetStation == null) return; if (!TryComp(ent, out var stationMap) || !TryComp(args.RuleEntity, out var ruleGrids)) return; if (Transform(ent).MapID != ruleGrids.Map) return; stationMap.TargetGrid = _station.GetLargestGrid((args.TargetStation.Value, null)); Dirty(ent); } }