Station maps (#13027)

This commit is contained in:
metalgearsloth
2023-04-13 16:21:24 +10:00
committed by GitHub
parent fc94d5245e
commit be4e69b0c0
45 changed files with 1210 additions and 153 deletions

View File

@@ -60,9 +60,8 @@ namespace Content.Server.Medical.CrewMonitoring
return;
// update all sensors info
var xform = Transform(uid);
var allSensors = component.ConnectedSensors.Values.ToList();
var uiState = new CrewMonitoringState(allSensors, xform.WorldPosition, component.Snap, component.Precision);
var uiState = new CrewMonitoringState(allSensors, component.Snap, component.Precision);
ui.SetState(uiState);
}
}

View File

@@ -1,4 +1,5 @@
using Content.Shared.Medical.SuitSensor;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Server.Medical.SuitSensors
{
@@ -53,9 +54,10 @@ namespace Content.Server.Medical.SuitSensors
public EntityUid? User = null;
/// <summary>
/// Last time when sensor updated owners status
/// Next time when sensor updated owners status
/// </summary>
public TimeSpan LastUpdate = TimeSpan.Zero;
[DataField("nextUpdate", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan NextUpdate = TimeSpan.Zero;
/// <summary>
/// The station this suit sensor belongs to. If it's null the suit didn't spawn on a station and the sensor doesn't work.

View File

@@ -21,23 +21,21 @@ namespace Content.Server.Medical.SuitSensors
{
public sealed class SuitSensorSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly CrewMonitoringServerSystem _monitoringServerSystem = default!;
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly IdCardSystem _idCardSystem = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly CrewMonitoringServerSystem _monitoringServerSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
[Dependency] private readonly SharedTransformSystem _xform = default!;
private const float UpdateRate = 1f;
private float _updateDif;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SuitSensorComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<SuitSensorComponent, EntityUnpausedEvent>(OnUnpaused);
SubscribeLocalEvent<SuitSensorComponent, GotEquippedEvent>(OnEquipped);
SubscribeLocalEvent<SuitSensorComponent, GotUnequippedEvent>(OnUnequipped);
SubscribeLocalEvent<SuitSensorComponent, ExaminedEvent>(OnExamine);
@@ -46,30 +44,29 @@ namespace Content.Server.Medical.SuitSensors
SubscribeLocalEvent<SuitSensorComponent, EntGotRemovedFromContainerMessage>(OnRemove);
}
private void OnUnpaused(EntityUid uid, SuitSensorComponent component, ref EntityUnpausedEvent args)
{
component.NextUpdate += args.PausedTime;
}
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)
var sensors = EntityManager.EntityQueryEnumerator<SuitSensorComponent, DeviceNetworkComponent>();
while (sensors.MoveNext(out var sensor, out var device))
{
if (!device.TransmitFrequency.HasValue || !sensor.StationId.HasValue)
if (device.TransmitFrequency is null || !sensor.StationId.HasValue)
continue;
// check if sensor is ready to update
if (curTime - sensor.LastUpdate < sensor.UpdateRate)
if (curTime < sensor.NextUpdate)
continue;
// Add a random offset to the next update time that isn't longer than the sensors update rate
sensor.LastUpdate = curTime.Add(TimeSpan.FromSeconds(_random.Next(0, sensor.UpdateRate.Seconds)));
// TODO: This would cause imprecision at different tick rates.
sensor.NextUpdate = curTime + sensor.UpdateRate;
// get sensor status
var status = GetSensorState(sensor.Owner, sensor);
@@ -278,9 +275,6 @@ namespace Content.Server.Medical.SuitSensors
totalDamage = damageable.TotalDamage.Int();
// finally, form suit sensor status
var xForm = Transform(sensor.User.Value);
var xFormQuery = GetEntityQuery<TransformComponent>();
var coords = _xform.GetMoverCoordinates(xForm, xFormQuery);
var status = new SuitSensorStatus(userName, userJob);
switch (sensor.Mode)
{
@@ -294,7 +288,26 @@ namespace Content.Server.Medical.SuitSensors
case SuitSensorMode.SensorCords:
status.IsAlive = isAlive;
status.TotalDamage = totalDamage;
status.Coordinates = coords;
EntityCoordinates coordinates;
var xformQuery = GetEntityQuery<TransformComponent>();
if (transform.GridUid != null)
{
coordinates = new EntityCoordinates(transform.GridUid.Value,
_transform.GetInvWorldMatrix(xformQuery.GetComponent(transform.GridUid.Value), xformQuery)
.Transform(_transform.GetWorldPosition(transform, xformQuery)));
}
else if (transform.MapUid != null)
{
coordinates = new EntityCoordinates(transform.MapUid.Value,
_transform.GetWorldPosition(transform, xformQuery));
}
else
{
coordinates = EntityCoordinates.Invalid;
}
status.Coordinates = coordinates;
break;
}
@@ -317,7 +330,7 @@ namespace Content.Server.Medical.SuitSensors
if (status.TotalDamage != null)
payload.Add(SuitSensorConstants.NET_TOTAL_DAMAGE, status.TotalDamage);
if (status.Coordinates != null)
payload.Add(SuitSensorConstants.NET_CORDINATES, status.Coordinates);
payload.Add(SuitSensorConstants.NET_COORDINATES, status.Coordinates);
return payload;
@@ -341,7 +354,7 @@ namespace Content.Server.Medical.SuitSensors
// try get total damage and cords (optionals)
payload.TryGetValue(SuitSensorConstants.NET_TOTAL_DAMAGE, out int? totalDamage);
payload.TryGetValue(SuitSensorConstants.NET_CORDINATES, out EntityCoordinates? cords);
payload.TryGetValue(SuitSensorConstants.NET_COORDINATES, out EntityCoordinates? cords);
var status = new SuitSensorStatus(name, job)
{

View File

@@ -0,0 +1,158 @@
using Content.Shared.Pinpointer;
using Content.Shared.Tag;
using Robust.Shared.GameStates;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Components;
namespace Content.Server.Pinpointer;
/// <summary>
/// Handles data to be used for in-grid map displays.
/// </summary>
public sealed class NavMapSystem : SharedNavMapSystem
{
[Dependency] private readonly TagSystem _tags = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<AnchorStateChangedEvent>(OnAnchorChange);
SubscribeLocalEvent<ReAnchorEvent>(OnReAnchor);
SubscribeLocalEvent<NavMapComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<NavMapComponent, GridSplitEvent>(OnNavMapSplit);
}
private void OnNavMapSplit(EntityUid uid, NavMapComponent component, ref GridSplitEvent args)
{
var physicsQuery = GetEntityQuery<PhysicsComponent>();
var tagQuery = GetEntityQuery<TagComponent>();
var gridQuery = GetEntityQuery<MapGridComponent>();
foreach (var grid in args.NewGrids)
{
var newComp = EnsureComp<NavMapComponent>(grid);
RefreshGrid(newComp, gridQuery.GetComponent(grid), physicsQuery, tagQuery);
}
RefreshGrid(component, gridQuery.GetComponent(uid), physicsQuery, tagQuery);
}
private void RefreshGrid(NavMapComponent component, MapGridComponent grid, EntityQuery<PhysicsComponent> physicsQuery, EntityQuery<TagComponent> tagQuery)
{
component.Chunks.Clear();
var tiles = grid.GetAllTilesEnumerator();
while (tiles.MoveNext(out var tile))
{
var chunkOrigin = SharedMapSystem.GetChunkIndices(tile.Value.GridIndices, ChunkSize);
if (!component.Chunks.TryGetValue(chunkOrigin, out var chunk))
{
chunk = new NavMapChunk(chunkOrigin);
}
RefreshTile(grid, component, chunk, tile.Value.GridIndices, physicsQuery, tagQuery);
}
}
private void OnGetState(EntityUid uid, NavMapComponent component, ref ComponentGetState args)
{
var data = new Dictionary<Vector2i, int>(component.Chunks.Count);
foreach (var (index, chunk) in component.Chunks)
{
data.Add(index, chunk.TileData);
}
// TODO: Diffs
args.State = new NavMapComponentState()
{
TileData = data,
};
}
private void OnReAnchor(ref ReAnchorEvent ev)
{
if (TryComp<MapGridComponent>(ev.OldGrid, out var oldGrid) &&
TryComp<NavMapComponent>(ev.OldGrid, out var navMap))
{
var chunkOrigin = SharedMapSystem.GetChunkIndices(ev.TilePos, ChunkSize);
if (navMap.Chunks.TryGetValue(chunkOrigin, out var chunk))
{
var physicsQuery = GetEntityQuery<PhysicsComponent>();
var tagQuery = GetEntityQuery<TagComponent>();
RefreshTile(oldGrid, navMap, chunk, ev.TilePos, physicsQuery, tagQuery);
}
}
HandleAnchor(ev.Xform);
}
private void OnAnchorChange(ref AnchorStateChangedEvent ev)
{
HandleAnchor(ev.Transform);
}
private void HandleAnchor(TransformComponent xform)
{
if (!TryComp<NavMapComponent>(xform.GridUid, out var navMap) ||
!TryComp<MapGridComponent>(xform.GridUid, out var grid))
return;
var tile = grid.LocalToTile(xform.Coordinates);
var chunkOrigin = SharedMapSystem.GetChunkIndices(tile, ChunkSize);
var physicsQuery = GetEntityQuery<PhysicsComponent>();
var tagQuery = GetEntityQuery<TagComponent>();
if (!navMap.Chunks.TryGetValue(chunkOrigin, out var chunk))
{
chunk = new NavMapChunk(chunkOrigin);
navMap.Chunks[chunkOrigin] = chunk;
}
RefreshTile(grid, navMap, chunk, tile, physicsQuery, tagQuery);
}
private void RefreshTile(MapGridComponent grid, NavMapComponent component, NavMapChunk chunk, Vector2i tile,
EntityQuery<PhysicsComponent> physicsQuery,
EntityQuery<TagComponent> tagQuery)
{
var relative = SharedMapSystem.GetChunkRelative(tile, ChunkSize);
var existing = chunk.TileData;
var flag = GetFlag(relative);
chunk.TileData &= ~flag;
var enumerator = grid.GetAnchoredEntitiesEnumerator(tile);
// TODO: Use something to get convex poly.
while (enumerator.MoveNext(out var ent))
{
if (!physicsQuery.TryGetComponent(ent, out var body) ||
!body.CanCollide ||
!body.Hard ||
body.BodyType != BodyType.Static ||
(!_tags.HasTag(ent.Value, "Wall", tagQuery) &&
!_tags.HasTag(ent.Value, "Window", tagQuery)))
{
continue;
}
chunk.TileData |= flag;
break;
}
if (chunk.TileData == 0)
{
component.Chunks.Remove(chunk.Origin);
}
if (existing == chunk.TileData)
return;
Dirty(component);
}
}

View File

@@ -6,7 +6,7 @@ using Content.Server.Shuttles.Events;
namespace Content.Server.Pinpointer
{
public sealed class ServerPinpointerSystem : SharedPinpointerSystem
public sealed class PinpointerSystem : SharedPinpointerSystem
{
[Dependency] private readonly SharedTransformSystem _transform = default!;

View File

@@ -0,0 +1,17 @@
namespace Content.Server.Pinpointer;
[RegisterComponent]
public sealed class StationMapComponent : Component
{
}
/// <summary>
/// Added to an entity using station map so when its parent changes we reset it.
/// </summary>
[RegisterComponent]
public sealed class StationMapUserComponent : Component
{
[DataField("mapUid")]
public EntityUid Map;
}

View File

@@ -0,0 +1,43 @@
using Content.Shared.Interaction.Events;
using Content.Shared.Pinpointer;
using Robust.Server.GameObjects;
namespace Content.Server.Pinpointer;
public sealed class StationMapSystem : EntitySystem
{
[Dependency] private readonly UserInterfaceSystem _ui = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StationMapUserComponent, EntParentChangedMessage>(OnUserParentChanged);
SubscribeLocalEvent<StationMapComponent, BoundUIOpenedEvent>(OnStationMapOpened);
SubscribeLocalEvent<StationMapComponent, BoundUIClosedEvent>(OnStationMapClosed);
}
private void OnStationMapClosed(EntityUid uid, StationMapComponent component, BoundUIClosedEvent args)
{
if (!Equals(args.UiKey, StationMapUiKey.Key) || args.Session.AttachedEntity == null)
return;
RemCompDeferred<StationMapUserComponent>(args.Session.AttachedEntity.Value);
}
private void OnUserParentChanged(EntityUid uid, StationMapUserComponent component, ref EntParentChangedMessage args)
{
if (TryComp<ActorComponent>(uid, out var actor))
{
_ui.TryClose(component.Map, StationMapUiKey.Key, actor.PlayerSession);
}
}
private void OnStationMapOpened(EntityUid uid, StationMapComponent component, BoundUIOpenedEvent args)
{
if (args.Session.AttachedEntity == null)
return;
var comp = EnsureComp<StationMapUserComponent>(args.Session.AttachedEntity.Value);
comp.Map = uid;
}
}