Suit sensor and crew monitoring (#5521)
Co-authored-by: Paul Ritter <ritter.paul1@googlemail.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Medical.SuitSensor;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Medical.CrewMonitoring
|
||||
{
|
||||
[RegisterComponent]
|
||||
[Friend(typeof(CrewMonitoringConsoleSystem))]
|
||||
public class CrewMonitoringConsoleComponent : Component
|
||||
{
|
||||
public override string Name => "CrewMonitoringConsole";
|
||||
|
||||
/// <summary>
|
||||
/// List of all currently connected sensors to this console.
|
||||
/// </summary>
|
||||
public Dictionary<string, SuitSensorStatus> ConnectedSensors = new();
|
||||
|
||||
/// <summary>
|
||||
/// After what time sensor consider to be lost.
|
||||
/// </summary>
|
||||
[DataField("sensorTimeout")]
|
||||
public float SensorTimeout = 10f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using System.Linq;
|
||||
using Content.Server.DeviceNetwork.Systems;
|
||||
using Content.Server.Medical.SuitSensors;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Medical.CrewMonitoring;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Medical.CrewMonitoring
|
||||
{
|
||||
public class CrewMonitoringConsoleSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SuitSensorSystem _sensors = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
private const float UpdateRate = 3f;
|
||||
private float _updateDif;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<CrewMonitoringConsoleComponent, ComponentRemove>(OnRemove);
|
||||
SubscribeLocalEvent<CrewMonitoringConsoleComponent, PacketSentEvent>(OnPacketReceived);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
// check update rate
|
||||
_updateDif += frameTime;
|
||||
if (_updateDif < UpdateRate)
|
||||
return;
|
||||
_updateDif = 0f;
|
||||
|
||||
var consoles = EntityManager.EntityQuery<CrewMonitoringConsoleComponent>();
|
||||
foreach (var console in consoles)
|
||||
{
|
||||
UpdateTimeouts(console.Owner, console);
|
||||
UpdateUserInterface(console.Owner, console);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRemove(EntityUid uid, CrewMonitoringConsoleComponent component, ComponentRemove args)
|
||||
{
|
||||
component.ConnectedSensors.Clear();
|
||||
}
|
||||
|
||||
private void OnPacketReceived(EntityUid uid, CrewMonitoringConsoleComponent component, PacketSentEvent args)
|
||||
{
|
||||
var suitSensor = _sensors.PacketToSuitSensor(args.Data);
|
||||
if (suitSensor == null)
|
||||
return;
|
||||
|
||||
suitSensor.Timestamp = _gameTiming.CurTime;
|
||||
component.ConnectedSensors[args.SenderAddress] = suitSensor;
|
||||
}
|
||||
|
||||
private void UpdateUserInterface(EntityUid uid, CrewMonitoringConsoleComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
var ui = component.Owner.GetUIOrNull(CrewMonitoringUIKey.Key);
|
||||
if (ui == null)
|
||||
return;
|
||||
|
||||
// update all sensors info
|
||||
var allSensors = component.ConnectedSensors.Values.ToList();
|
||||
var uiState = new CrewMonitoringState(allSensors);
|
||||
ui.SetState(uiState);
|
||||
}
|
||||
|
||||
private void UpdateTimeouts(EntityUid uid, CrewMonitoringConsoleComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
foreach (var (address, sensor) in component.ConnectedSensors)
|
||||
{
|
||||
// if too many time passed - sensor just dropped connection
|
||||
var dif = _gameTiming.CurTime - sensor.Timestamp;
|
||||
if (dif.Seconds > component.SensorTimeout)
|
||||
component.ConnectedSensors.Remove(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
Content.Server/Medical/SuitSensors/SuitSensorComponent.cs
Normal file
59
Content.Server/Medical/SuitSensors/SuitSensorComponent.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Medical.SuitSensor;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Medical.SuitSensors
|
||||
{
|
||||
/// <summary>
|
||||
/// Tracking device, embedded in almost all uniforms and jumpsuits.
|
||||
/// If enabled, will report to crew monitoring console owners position and status.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
[Friend(typeof(SuitSensorSystem))]
|
||||
[ComponentProtoName("SuitSensor")]
|
||||
public sealed class SuitSensorComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Choose a random sensor mode when item is spawned.
|
||||
/// </summary>
|
||||
[DataField("randomMode")]
|
||||
public bool RandomMode = true;
|
||||
|
||||
/// <summary>
|
||||
/// If true user can't change suit sensor mode
|
||||
/// </summary>
|
||||
[DataField("controlsLocked")]
|
||||
public bool ControlsLocked = false;
|
||||
|
||||
/// <summary>
|
||||
/// Current sensor mode. Can be switched by user verbs.
|
||||
/// </summary>
|
||||
[DataField("mode")]
|
||||
public SuitSensorMode Mode = SuitSensorMode.SensorOff;
|
||||
|
||||
/// <summary>
|
||||
/// Activate sensor if user wear it in this slot.
|
||||
/// </summary>
|
||||
[DataField("activationSlot")]
|
||||
public EquipmentSlotDefines.Slots ActivationSlot = EquipmentSlotDefines.Slots.INNERCLOTHING;
|
||||
|
||||
/// <summary>
|
||||
/// How often does sensor update its owners status (in seconds).
|
||||
/// </summary>
|
||||
[DataField("updateRate")]
|
||||
public float UpdateRate = 2f;
|
||||
|
||||
/// <summary>
|
||||
/// Current user that wears suit sensor. Null if nobody wearing it.
|
||||
/// </summary>
|
||||
public EntityUid? User = null;
|
||||
|
||||
/// <summary>
|
||||
/// Last time when sensor updated owners status
|
||||
/// </summary>
|
||||
public TimeSpan LastUpdate = TimeSpan.Zero;
|
||||
}
|
||||
}
|
||||
312
Content.Server/Medical/SuitSensors/SuitSensorSystem.cs
Normal file
312
Content.Server/Medical/SuitSensors/SuitSensorSystem.cs
Normal file
@@ -0,0 +1,312 @@
|
||||
using System;
|
||||
using Content.Server.Access.Systems;
|
||||
using Content.Server.DeviceNetwork;
|
||||
using Content.Server.DeviceNetwork.Components;
|
||||
using Content.Server.DeviceNetwork.Systems;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Medical.SuitSensor;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.Verbs;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Medical.SuitSensors
|
||||
{
|
||||
public class SuitSensorSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly IdCardSystem _idCardSystem = default!;
|
||||
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
private const float UpdateRate = 0.5f;
|
||||
private float _updateDif;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<SuitSensorComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<SuitSensorComponent, EquippedEvent>(OnEquipped);
|
||||
SubscribeLocalEvent<SuitSensorComponent, UnequippedEvent>(OnUnequipped);
|
||||
SubscribeLocalEvent<SuitSensorComponent, ExaminedEvent>(OnExamine);
|
||||
SubscribeLocalEvent<SuitSensorComponent, GetInteractionVerbsEvent>(OnVerb);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
// check update rate
|
||||
_updateDif += frameTime;
|
||||
if (_updateDif < UpdateRate)
|
||||
return;
|
||||
|
||||
_updateDif -= UpdateRate;
|
||||
|
||||
var curTime = _gameTiming.CurTime;
|
||||
var sensors = EntityManager.EntityQuery<SuitSensorComponent, DeviceNetworkComponent>();
|
||||
foreach (var (sensor, device) in sensors)
|
||||
{
|
||||
// check if sensor is ready to update
|
||||
if (curTime - sensor.LastUpdate < TimeSpan.FromSeconds(sensor.UpdateRate))
|
||||
continue;
|
||||
sensor.LastUpdate = curTime;
|
||||
|
||||
// get sensor status
|
||||
var status = GetSensorState(sensor.Owner, sensor);
|
||||
if (status == null)
|
||||
continue;
|
||||
|
||||
// broadcast it to device network
|
||||
var payload = SuitSensorToPacket(status);
|
||||
_deviceNetworkSystem.QueuePacket(sensor.Owner, DeviceNetworkConstants.NullAddress, device.Frequency, payload, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, SuitSensorComponent component, MapInitEvent args)
|
||||
{
|
||||
// generate random mode
|
||||
if (component.RandomMode)
|
||||
{
|
||||
//make the sensor mode favor higher levels, except coords.
|
||||
var modesDist = new[]
|
||||
{
|
||||
SuitSensorMode.SensorOff,
|
||||
SuitSensorMode.SensorBinary, SuitSensorMode.SensorBinary,
|
||||
SuitSensorMode.SensorVitals, SuitSensorMode.SensorVitals, SuitSensorMode.SensorVitals,
|
||||
SuitSensorMode.SensorCords, SuitSensorMode.SensorCords
|
||||
};
|
||||
component.Mode = _random.Pick(modesDist);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEquipped(EntityUid uid, SuitSensorComponent component, EquippedEvent args)
|
||||
{
|
||||
if (args.Slot != component.ActivationSlot)
|
||||
return;
|
||||
|
||||
component.User = args.User;
|
||||
}
|
||||
|
||||
private void OnUnequipped(EntityUid uid, SuitSensorComponent component, UnequippedEvent args)
|
||||
{
|
||||
if (args.Slot != component.ActivationSlot)
|
||||
return;
|
||||
|
||||
component.User = null;
|
||||
}
|
||||
|
||||
private void OnExamine(EntityUid uid, SuitSensorComponent component, ExaminedEvent args)
|
||||
{
|
||||
if (!args.IsInDetailsRange)
|
||||
return;
|
||||
|
||||
string msg;
|
||||
switch (component.Mode)
|
||||
{
|
||||
case SuitSensorMode.SensorOff:
|
||||
msg = "suit-sensor-examine-off";
|
||||
break;
|
||||
case SuitSensorMode.SensorBinary:
|
||||
msg = "suit-sensor-examine-binary";
|
||||
break;
|
||||
case SuitSensorMode.SensorVitals:
|
||||
msg = "suit-sensor-examine-vitals";
|
||||
break;
|
||||
case SuitSensorMode.SensorCords:
|
||||
msg = "suit-sensor-examine-cords";
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
args.PushMarkup(Loc.GetString(msg));
|
||||
}
|
||||
|
||||
private void OnVerb(EntityUid uid, SuitSensorComponent component, GetInteractionVerbsEvent args)
|
||||
{
|
||||
// check if user can change sensor
|
||||
if (component.ControlsLocked)
|
||||
return;
|
||||
|
||||
// standard interaction checks
|
||||
if (!args.CanAccess || !args.CanInteract || !_actionBlockerSystem.CanDrop(args.User))
|
||||
return;
|
||||
|
||||
args.Verbs.UnionWith(new[]
|
||||
{
|
||||
CreateVerb(uid, component, args.User, SuitSensorMode.SensorOff),
|
||||
CreateVerb(uid, component, args.User, SuitSensorMode.SensorBinary),
|
||||
CreateVerb(uid, component, args.User, SuitSensorMode.SensorVitals),
|
||||
CreateVerb(uid, component, args.User, SuitSensorMode.SensorCords)
|
||||
});
|
||||
}
|
||||
|
||||
private Verb CreateVerb(EntityUid uid, SuitSensorComponent component, EntityUid userUid, SuitSensorMode mode)
|
||||
{
|
||||
return new Verb()
|
||||
{
|
||||
Text = GetModeName(mode),
|
||||
Disabled = component.Mode == mode,
|
||||
Priority = -(int) mode, // sort them in descending order
|
||||
Category = VerbCategory.SetSensor,
|
||||
Act = () => SetSensor(uid, mode, userUid, component)
|
||||
};
|
||||
}
|
||||
|
||||
private string GetModeName(SuitSensorMode mode)
|
||||
{
|
||||
string name;
|
||||
switch (mode)
|
||||
{
|
||||
case SuitSensorMode.SensorOff:
|
||||
name = "suit-sensor-mode-off";
|
||||
break;
|
||||
case SuitSensorMode.SensorBinary:
|
||||
name = "suit-sensor-mode-binary";
|
||||
break;
|
||||
case SuitSensorMode.SensorVitals:
|
||||
name = "suit-sensor-mode-vitals";
|
||||
break;
|
||||
case SuitSensorMode.SensorCords:
|
||||
name = "suit-sensor-mode-cords";
|
||||
break;
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
|
||||
return Loc.GetString(name);
|
||||
}
|
||||
|
||||
public void SetSensor(EntityUid uid, SuitSensorMode mode, EntityUid? userUid = null,
|
||||
SuitSensorComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
|
||||
component.Mode = mode;
|
||||
|
||||
if (userUid != null)
|
||||
{
|
||||
var msg = Loc.GetString("suit-sensor-mode-state", ("mode", GetModeName(mode)));
|
||||
_popupSystem.PopupEntity(msg, uid, Filter.Entities(userUid.Value));
|
||||
}
|
||||
}
|
||||
|
||||
public SuitSensorStatus? GetSensorState(EntityUid uid, SuitSensorComponent? sensor = null, TransformComponent? transform = null)
|
||||
{
|
||||
if (!Resolve(uid, ref sensor, ref transform))
|
||||
return null;
|
||||
|
||||
// check if sensor is enabled and worn by user
|
||||
if (sensor.Mode == SuitSensorMode.SensorOff || sensor.User == null)
|
||||
return null;
|
||||
|
||||
// try to get mobs id from ID slot
|
||||
var userName = Loc.GetString("suit-sensor-component-unknown-name");
|
||||
var userJob = Loc.GetString("suit-sensor-component-unknown-job");
|
||||
if (_idCardSystem.TryGetIdCardSlot(sensor.User.Value, out var card))
|
||||
{
|
||||
if (card.FullName != null)
|
||||
userName = card.FullName;
|
||||
if (card.JobTitle != null)
|
||||
userJob = card.JobTitle;
|
||||
}
|
||||
|
||||
// get health mob state
|
||||
var isAlive = false;
|
||||
if (EntityManager.TryGetComponent(sensor.User.Value, out MobStateComponent? mobState))
|
||||
{
|
||||
isAlive = mobState.IsAlive();
|
||||
}
|
||||
|
||||
// get mob total damage
|
||||
var totalDamage = 0;
|
||||
if (EntityManager.TryGetComponent(sensor.User.Value, out DamageableComponent? damageable))
|
||||
{
|
||||
totalDamage = damageable.TotalDamage.Int();
|
||||
}
|
||||
|
||||
// finally, form suit sensor status
|
||||
var status = new SuitSensorStatus(userName, userJob);
|
||||
switch (sensor.Mode)
|
||||
{
|
||||
case SuitSensorMode.SensorBinary:
|
||||
status.IsAlive = isAlive;
|
||||
break;
|
||||
case SuitSensorMode.SensorVitals:
|
||||
status.IsAlive = isAlive;
|
||||
status.TotalDamage = totalDamage;
|
||||
break;
|
||||
case SuitSensorMode.SensorCords:
|
||||
status.IsAlive = isAlive;
|
||||
status.TotalDamage = totalDamage;
|
||||
status.Coordinates = transform.MapPosition;
|
||||
break;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize suit sensor status into device network package.
|
||||
/// </summary>
|
||||
public NetworkPayload SuitSensorToPacket(SuitSensorStatus status)
|
||||
{
|
||||
var payload = new NetworkPayload()
|
||||
{
|
||||
[DeviceNetworkConstants.Command] = DeviceNetworkConstants.CmdUpdatedState,
|
||||
[SuitSensorConstants.NET_NAME] = status.Name,
|
||||
[SuitSensorConstants.NET_JOB] = status.Job,
|
||||
[SuitSensorConstants.NET_IS_ALIVE] = status.IsAlive,
|
||||
};
|
||||
|
||||
if (status.TotalDamage != null)
|
||||
payload.Add(SuitSensorConstants.NET_TOTAL_DAMAGE, status.TotalDamage);
|
||||
if (status.Coordinates != null)
|
||||
payload.Add(SuitSensorConstants.NET_CORDINATES, status.Coordinates);
|
||||
|
||||
return payload;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to deserialize device network message into suit sensor status
|
||||
/// </summary>
|
||||
public SuitSensorStatus? PacketToSuitSensor(NetworkPayload payload)
|
||||
{
|
||||
// check command
|
||||
if (!payload.TryGetValue(DeviceNetworkConstants.Command, out string? command))
|
||||
return null;
|
||||
if (command != DeviceNetworkConstants.CmdUpdatedState)
|
||||
return null;
|
||||
|
||||
// check name, job and alive
|
||||
if (!payload.TryGetValue(SuitSensorConstants.NET_NAME, out string? name)) return null;
|
||||
if (!payload.TryGetValue(SuitSensorConstants.NET_JOB, out string? job)) return null;
|
||||
if (!payload.TryGetValue(SuitSensorConstants.NET_IS_ALIVE, out bool? isAlive)) return null;
|
||||
|
||||
// try get total damage and cords (optionals)
|
||||
payload.TryGetValue(SuitSensorConstants.NET_TOTAL_DAMAGE, out int? totalDamage);
|
||||
payload.TryGetValue(SuitSensorConstants.NET_CORDINATES, out MapCoordinates? cords);
|
||||
|
||||
var status = new SuitSensorStatus(name, job)
|
||||
{
|
||||
IsAlive = isAlive.Value,
|
||||
TotalDamage = totalDamage,
|
||||
Coordinates = cords
|
||||
};
|
||||
return status;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user