Files
wwdpublic/Content.Server/Atmos/EntitySystems/GasAnalyzerSystem.cs
SimpleStation14 6e0ffe81bc Mirror: Partial atmos refactor (#312)
## Mirror of PR #22521: [Partial atmos
refactor](https://github.com/space-wizards/space-station-14/pull/22521)
from <img src="https://avatars.githubusercontent.com/u/10567778?v=4"
alt="space-wizards" width="22"/>
[space-wizards](https://github.com/space-wizards)/[space-station-14](https://github.com/space-wizards/space-station-14)

###### `18a35e7e83b2b71ee84b054d44d9ed5e595dd618`

PR opened by <img
src="https://avatars.githubusercontent.com/u/60421075?v=4"
width="16"/><a href="https://github.com/ElectroJr"> ElectroJr</a> at
2023-12-15 03:45:42 UTC

---

PR changed 43 files with 891 additions and 635 deletions.

The PR had the following labels:
- Status: Needs Review


---

<details open="true"><summary><h1>Original Body</h1></summary>

> This PR reworks how some parts of atmos code work. Originally it was
just meant to be a performance and bugfix PR, but it has ballooned in
scope. I'm not sure about some of my changes largely because I'm not
sure if some things were an oversight or an intentional decision for
some reason.
> 
> List of changes:
> - The `MolesArchived float[]` field is now read-only
> - It simply gets zeroed whenever the `GasMixture` is set to null
instead of constantly reallocating
> - Airtight query information is now cached in `TileAtmosphere`
> - This means that it should only iterate over anchored entities once
per update.
> - Previously an invalidated atmos tile would cause
`ProcessRevalidate()` to query airtight entities on the same tile six
times by calling a combination of `GridIsTileAirBlocked()`,
`NeedsVacuumFixing()`, and `GridIsTileAirBlocked()`. So this should help
significantly reduce component lookups & entity enumeration.
> - This does change some behaviour. In particular blocked directions
are now only updated if the tile was invalidated prior to the current
atmos-update, and will only ever be updated once per atmos-update.
> - AFAIK this only has an effect if the invalid tile processing is
deferred over multiple ticks, and I don't think it should cause any
issues?
> - Fixes a potential bug, where tiles might not dispose of their
excited group if their direction flags changed.
> - `MapAtmosphereComponent.Mixture` is now always immutable and no
longer nullable
> - I'm not sure why the mixture was nullable before? AFAICT the
component is meaningless if its null?
> - Space "gas" was always immutable, but there was nothing that
required planet atmospheres to be immutable. Requiring that it be
immutable gets rid of the constant gas mixture cloning.
> - I don't know if there was a reason for why they weren't immutable to
begin with.
> - Fixes lungs removing too much air from a gas mixture, resulting in
negative moles.
> - `GasMixture.Moles` is now `[Access]` restricted to the atmosphere
system.
> - This is to prevent people from improperly modifying the gas mixtures
(e.g., lungs), or accidentally modifying immutable mixtures.
> - Fixes an issue where non-grid atmosphere tiles would fail to update
their adjacent tiles, resulting in null reference exception spam
>   - Fixes #21732
>   - Fixes #21210 (probably) 
> - Disconnected atmosphere tiles, i.e., tiles that aren't on or
adjacent to a grid tile, will now get removed from the tile set.
Previously the tile set would just always increase, with tiles never
getting removed.
> - Removes various redundant component and tile-definition queries.
> - Removes some method events in favour of just using methods.
> - Map-exposded tiles now get updated when a map's atmosphere changes
(or the grid moves across maps).
> - Adds a `setmapatmos` command for adding map-wide atmospheres.
> - Fixed (non-planet) map atmospheres rendering over grids.
> 
> ## Media
> 
> This PR also includes changes to the atmos debug overlay, though I've
also split that off into a separate PR to make reviewing easier
(#22520).
> 
> Below is a video showing that atmos still seems to work, and that
trimming of disconnected tiles works:
> 
>
https://github.com/space-wizards/space-station-14/assets/60421075/4da46992-19e6-4354-8ecd-3cd67be4d0ed
> 
> For comparison, here is a video showing how current master works
(disconnected tiles never get removed):
> 
>
https://github.com/space-wizards/space-station-14/assets/60421075/54590777-e11c-41dc-b49d-fd7e53bfeed7
> 
> 🆑
> - fix: Fixed a bug where partially airtight entities (e.g., thin
windows or doors) could let air leak out into space.
> 


</details>

Co-authored-by: SimpleStation14 <Unknown>
2024-05-20 02:33:00 -04:00

298 lines
12 KiB
C#

using System.Linq;
using Content.Server.Atmos;
using Content.Server.Atmos.Components;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Popups;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Player;
using static Content.Shared.Atmos.Components.GasAnalyzerComponent;
namespace Content.Server.Atmos.EntitySystems
{
[UsedImplicitly]
public sealed class GasAnalyzerSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly AtmosphereSystem _atmo = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly UserInterfaceSystem _userInterface = default!;
[Dependency] private readonly TransformSystem _transform = default!;
/// <summary>
/// Minimum moles of a gas to be sent to the client.
/// </summary>
private const float UIMinMoles = 0.01f;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GasAnalyzerComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<GasAnalyzerComponent, GasAnalyzerDisableMessage>(OnDisabledMessage);
SubscribeLocalEvent<GasAnalyzerComponent, DroppedEvent>(OnDropped);
SubscribeLocalEvent<GasAnalyzerComponent, UseInHandEvent>(OnUseInHand);
}
public override void Update(float frameTime)
{
var query = EntityQueryEnumerator<ActiveGasAnalyzerComponent>();
while (query.MoveNext(out var uid, out var analyzer))
{
// Don't update every tick
analyzer.AccumulatedFrametime += frameTime;
if (analyzer.AccumulatedFrametime < analyzer.UpdateInterval)
continue;
analyzer.AccumulatedFrametime -= analyzer.UpdateInterval;
if (!UpdateAnalyzer(uid))
RemCompDeferred<ActiveGasAnalyzerComponent>(uid);
}
}
/// <summary>
/// Activates the analyzer when used in the world, scanning either the target entity or the tile clicked
/// </summary>
private void OnAfterInteract(EntityUid uid, GasAnalyzerComponent component, AfterInteractEvent args)
{
if (!args.CanReach)
{
_popup.PopupEntity(Loc.GetString("gas-analyzer-component-player-cannot-reach-message"), args.User, args.User);
return;
}
ActivateAnalyzer(uid, component, args.User, args.Target);
OpenUserInterface(uid, args.User, component);
args.Handled = true;
}
/// <summary>
/// Activates the analyzer with no target, so it only scans the tile the user was on when activated
/// </summary>
private void OnUseInHand(EntityUid uid, GasAnalyzerComponent component, UseInHandEvent args)
{
ActivateAnalyzer(uid, component, args.User);
args.Handled = true;
}
/// <summary>
/// Handles analyzer activation logic
/// </summary>
private void ActivateAnalyzer(EntityUid uid, GasAnalyzerComponent component, EntityUid user, EntityUid? target = null)
{
component.Target = target;
component.User = user;
if (target != null)
component.LastPosition = Transform(target.Value).Coordinates;
else
component.LastPosition = null;
component.Enabled = true;
Dirty(component);
UpdateAppearance(uid, component);
if(!HasComp<ActiveGasAnalyzerComponent>(uid))
AddComp<ActiveGasAnalyzerComponent>(uid);
UpdateAnalyzer(uid, component);
}
/// <summary>
/// Close the UI, turn the analyzer off, and don't update when it's dropped
/// </summary>
private void OnDropped(EntityUid uid, GasAnalyzerComponent component, DroppedEvent args)
{
if(args.User is var userId && component.Enabled)
_popup.PopupEntity(Loc.GetString("gas-analyzer-shutoff"), userId, userId);
DisableAnalyzer(uid, component, args.User);
}
/// <summary>
/// Closes the UI, sets the icon to off, and removes it from the update list
/// </summary>
private void DisableAnalyzer(EntityUid uid, GasAnalyzerComponent? component = null, EntityUid? user = null)
{
if (!Resolve(uid, ref component))
return;
if (user != null && TryComp<ActorComponent>(user, out var actor))
_userInterface.TryClose(uid, GasAnalyzerUiKey.Key, actor.PlayerSession);
component.Enabled = false;
Dirty(component);
UpdateAppearance(uid, component);
RemCompDeferred<ActiveGasAnalyzerComponent>(uid);
}
/// <summary>
/// Disables the analyzer when the user closes the UI
/// </summary>
private void OnDisabledMessage(EntityUid uid, GasAnalyzerComponent component, GasAnalyzerDisableMessage message)
{
if (message.Session.AttachedEntity is not {Valid: true})
return;
DisableAnalyzer(uid, component);
}
private void OpenUserInterface(EntityUid uid, EntityUid user, GasAnalyzerComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
if (!TryComp<ActorComponent>(user, out var actor))
return;
_userInterface.TryOpen(uid, GasAnalyzerUiKey.Key, actor.PlayerSession);
}
/// <summary>
/// Fetches fresh data for the analyzer. Should only be called by Update or when the user requests an update via refresh button
/// </summary>
private bool UpdateAnalyzer(EntityUid uid, GasAnalyzerComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
if (!TryComp(component.User, out TransformComponent? xform))
{
DisableAnalyzer(uid, component);
return false;
}
// check if the user has walked away from what they scanned
var userPos = xform.Coordinates;
if (component.LastPosition.HasValue)
{
// Check if position is out of range => don't update and disable
if (!component.LastPosition.Value.InRange(EntityManager, _transform, userPos, SharedInteractionSystem.InteractionRange))
{
if(component.User is { } userId && component.Enabled)
_popup.PopupEntity(Loc.GetString("gas-analyzer-shutoff"), userId, userId);
DisableAnalyzer(uid, component, component.User);
return false;
}
}
var gasMixList = new List<GasMixEntry>();
// Fetch the environmental atmosphere around the scanner. This must be the first entry
var tileMixture = _atmo.GetContainingMixture(uid, true);
if (tileMixture != null)
{
gasMixList.Add(new GasMixEntry(Loc.GetString("gas-analyzer-window-environment-tab-label"), tileMixture.Pressure, tileMixture.Temperature,
GenerateGasEntryArray(tileMixture)));
}
else
{
// No gases were found
gasMixList.Add(new GasMixEntry(Loc.GetString("gas-analyzer-window-environment-tab-label"), 0f, 0f));
}
var deviceFlipped = false;
if (component.Target != null)
{
if (Deleted(component.Target))
{
component.Target = null;
DisableAnalyzer(uid, component, component.User);
return false;
}
// gas analyzed was used on an entity, try to request gas data via event for override
var ev = new GasAnalyzerScanEvent();
RaiseLocalEvent(component.Target.Value, ev);
if (ev.GasMixtures != null)
{
foreach (var mixes in ev.GasMixtures)
{
if(mixes.Value != null)
gasMixList.Add(new GasMixEntry(mixes.Key, mixes.Value.Pressure, mixes.Value.Temperature, GenerateGasEntryArray(mixes.Value)));
}
deviceFlipped = ev.DeviceFlipped;
}
else
{
// No override, fetch manually, to handle flippable devices you must subscribe to GasAnalyzerScanEvent
if (TryComp(component.Target, out NodeContainerComponent? node))
{
foreach (var pair in node.Nodes)
{
if (pair.Value is PipeNode pipeNode)
gasMixList.Add(new GasMixEntry(pair.Key, pipeNode.Air.Pressure, pipeNode.Air.Temperature, GenerateGasEntryArray(pipeNode.Air)));
}
}
}
}
// Don't bother sending a UI message with no content, and stop updating I guess?
if (gasMixList.Count == 0)
return false;
_userInterface.TrySendUiMessage(uid, GasAnalyzerUiKey.Key,
new GasAnalyzerUserMessage(gasMixList.ToArray(),
component.Target != null ? Name(component.Target.Value) : string.Empty,
GetNetEntity(component.Target) ?? NetEntity.Invalid,
deviceFlipped));
return true;
}
/// <summary>
/// Sets the appearance based on the analyzers Enabled state
/// </summary>
private void UpdateAppearance(EntityUid uid, GasAnalyzerComponent analyzer)
{
_appearance.SetData(uid, GasAnalyzerVisuals.Enabled, analyzer.Enabled);
}
/// <summary>
/// Generates a GasEntry array for a given GasMixture
/// </summary>
private GasEntry[] GenerateGasEntryArray(GasMixture? mixture)
{
var gases = new List<GasEntry>();
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var gas = _atmo.GetGas(i);
if (mixture?[i] <= UIMinMoles)
continue;
if (mixture != null)
{
var gasName = Loc.GetString(gas.Name);
gases.Add(new GasEntry(gasName, mixture[i], gas.Color));
}
}
var gasesOrdered = gases.OrderByDescending(gas => gas.Amount);
return gasesOrdered.ToArray();
}
}
}
/// <summary>
/// Raised when the analyzer is used. An atmospherics device that does not rely on a NodeContainer or
/// wishes to override the default analyzer behaviour of fetching all nodes in the attached NodeContainer
/// should subscribe to this and return the GasMixtures as desired. A device that is flippable should subscribe
/// to this event to report if it is flipped or not. See GasFilterSystem or GasMixerSystem for an example.
/// </summary>
public sealed class GasAnalyzerScanEvent : EntityEventArgs
{
/// <summary>
/// Key is the mix name (ex "pipe", "inlet", "filter"), value is the pipe direction and GasMixture. Add all mixes that should be reported when scanned.
/// </summary>
public Dictionary<string, GasMixture?>? GasMixtures;
/// <summary>
/// If the device is flipped. Flipped is defined as when the inline input is 90 degrees CW to the side input
/// </summary>
public bool DeviceFlipped;
}