mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-18 14:07:53 +03:00
## 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>
176 lines
6.2 KiB
C#
176 lines
6.2 KiB
C#
using System.Numerics;
|
|
using Content.Server.Atmos.Components;
|
|
using Content.Shared.Atmos;
|
|
using Content.Shared.Atmos.EntitySystems;
|
|
using Content.Shared.CCVar;
|
|
using JetBrains.Annotations;
|
|
using Robust.Server.GameObjects;
|
|
using Robust.Server.Player;
|
|
using Robust.Shared.Configuration;
|
|
using Robust.Shared.Enums;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Map.Components;
|
|
using Robust.Shared.Player;
|
|
|
|
namespace Content.Server.Atmos.EntitySystems
|
|
{
|
|
[UsedImplicitly]
|
|
public sealed class AtmosDebugOverlaySystem : SharedAtmosDebugOverlaySystem
|
|
{
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
|
[Dependency] private readonly IConfigurationManager _configManager = default!;
|
|
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
|
[Dependency] private readonly MapSystem _mapSystem = default!;
|
|
|
|
/// <summary>
|
|
/// Players allowed to see the atmos debug overlay.
|
|
/// To modify it see <see cref="AddObserver"/> and
|
|
/// <see cref="RemoveObserver"/>.
|
|
/// </summary>
|
|
private readonly HashSet<ICommonSession> _playerObservers = new();
|
|
|
|
/// <summary>
|
|
/// Overlay update ticks per second.
|
|
/// </summary>
|
|
private float _updateCooldown;
|
|
|
|
private List<Entity<MapGridComponent>> _grids = new();
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
|
|
}
|
|
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
|
|
}
|
|
|
|
public bool AddObserver(ICommonSession observer)
|
|
{
|
|
return _playerObservers.Add(observer);
|
|
}
|
|
|
|
public bool HasObserver(ICommonSession observer)
|
|
{
|
|
return _playerObservers.Contains(observer);
|
|
}
|
|
|
|
public bool RemoveObserver(ICommonSession observer)
|
|
{
|
|
if (!_playerObservers.Remove(observer))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var message = new AtmosDebugOverlayDisableMessage();
|
|
RaiseNetworkEvent(message, observer.Channel);
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds the given observer if it doesn't exist, removes it otherwise.
|
|
/// </summary>
|
|
/// <param name="observer">The observer to toggle.</param>
|
|
/// <returns>true if added, false if removed.</returns>
|
|
public bool ToggleObserver(ICommonSession observer)
|
|
{
|
|
if (HasObserver(observer))
|
|
{
|
|
RemoveObserver(observer);
|
|
return false;
|
|
}
|
|
|
|
AddObserver(observer);
|
|
return true;
|
|
}
|
|
|
|
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
|
|
{
|
|
if (e.NewStatus != SessionStatus.InGame)
|
|
{
|
|
RemoveObserver(e.Session);
|
|
}
|
|
}
|
|
|
|
private AtmosDebugOverlayData? ConvertTileToData(TileAtmosphere tile)
|
|
{
|
|
return new AtmosDebugOverlayData(
|
|
tile.GridIndices,
|
|
tile.Air?.Temperature ?? default,
|
|
tile.Air?.Moles,
|
|
tile.PressureDirection,
|
|
tile.LastPressureDirection,
|
|
tile.AirtightData.BlockedDirections,
|
|
tile.ExcitedGroup?.GetHashCode(),
|
|
tile.Space,
|
|
tile.MapAtmosphere,
|
|
tile.NoGridTile);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
AccumulatedFrameTime += frameTime;
|
|
_updateCooldown = 1 / _configManager.GetCVar(CCVars.NetAtmosDebugOverlayTickRate);
|
|
|
|
if (AccumulatedFrameTime < _updateCooldown)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// This is the timer from GasTileOverlaySystem
|
|
AccumulatedFrameTime -= _updateCooldown;
|
|
|
|
// Now we'll go through each player, then through each chunk in range of that player checking if the player is still in range
|
|
// If they are, check if they need the new data to send (i.e. if there's an overlay for the gas).
|
|
// Afterwards we reset all the chunk data for the next time we tick.
|
|
foreach (var session in _playerObservers)
|
|
{
|
|
if (session.AttachedEntity is not {Valid: true} entity)
|
|
continue;
|
|
|
|
var transform = Transform(entity);
|
|
var pos = _transform.GetWorldPosition(transform);
|
|
var worldBounds = Box2.CenteredAround(pos,
|
|
new Vector2(LocalViewRange, LocalViewRange));
|
|
|
|
_grids.Clear();
|
|
_mapManager.FindGridsIntersecting(transform.MapID, worldBounds, ref _grids);
|
|
|
|
foreach (var grid in _grids)
|
|
{
|
|
var uid = grid.Owner;
|
|
|
|
if (!Exists(uid))
|
|
continue;
|
|
|
|
if (!TryComp(uid, out GridAtmosphereComponent? gridAtmos))
|
|
continue;
|
|
|
|
var entityTile = _mapSystem.GetTileRef(grid, grid, transform.Coordinates).GridIndices;
|
|
var baseTile = new Vector2i(entityTile.X - LocalViewRange / 2, entityTile.Y - LocalViewRange / 2);
|
|
var debugOverlayContent = new AtmosDebugOverlayData?[LocalViewRange * LocalViewRange];
|
|
|
|
var index = 0;
|
|
for (var y = 0; y < LocalViewRange; y++)
|
|
{
|
|
for (var x = 0; x < LocalViewRange; x++)
|
|
{
|
|
var vector = new Vector2i(baseTile.X + x, baseTile.Y + y);
|
|
gridAtmos.Tiles.TryGetValue(vector, out var tile);
|
|
debugOverlayContent[index++] = tile == null ? null : ConvertTileToData(tile);
|
|
}
|
|
}
|
|
|
|
var msg = new AtmosDebugOverlayMessage(GetNetEntity(grid), baseTile, debugOverlayContent);
|
|
RaiseNetworkEvent(msg, session.Channel);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|