mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-20 23:17:43 +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>
190 lines
7.3 KiB
C#
190 lines
7.3 KiB
C#
using System.Globalization;
|
|
using Robust.Shared.Serialization;
|
|
using Robust.Shared.Serialization.Manager;
|
|
using Robust.Shared.Serialization.Markdown;
|
|
using Robust.Shared.Serialization.Markdown.Mapping;
|
|
using Robust.Shared.Serialization.Markdown.Validation;
|
|
using Robust.Shared.Serialization.Markdown.Value;
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Generic;
|
|
using Robust.Shared.Serialization.TypeSerializers.Interfaces;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Server.Atmos.Serialization;
|
|
|
|
public sealed partial class TileAtmosCollectionSerializer : ITypeSerializer<Dictionary<Vector2i, TileAtmosphere>, MappingDataNode>, ITypeCopier<Dictionary<Vector2i, TileAtmosphere>>
|
|
{
|
|
public ValidationNode Validate(ISerializationManager serializationManager, MappingDataNode node,
|
|
IDependencyCollection dependencies, ISerializationContext? context = null)
|
|
{
|
|
return serializationManager.ValidateNode<TileAtmosData>(node, context);
|
|
}
|
|
|
|
public Dictionary<Vector2i, TileAtmosphere> Read(ISerializationManager serializationManager, MappingDataNode node,
|
|
IDependencyCollection dependencies,
|
|
SerializationHookContext hookCtx, ISerializationContext? context = null,
|
|
ISerializationManager.InstantiationDelegate<Dictionary<Vector2i, TileAtmosphere>>? instanceProvider = null)
|
|
{
|
|
node.TryGetValue(new ValueDataNode("version"), out var versionNode);
|
|
var version = ((ValueDataNode?) versionNode)?.AsInt() ?? 1;
|
|
Dictionary<Vector2i, TileAtmosphere> tiles;
|
|
|
|
// Backwards compatability
|
|
if (version == 1)
|
|
{
|
|
var tile2 = node["tiles"];
|
|
|
|
var mixies = serializationManager.Read<Dictionary<Vector2i, int>?>(tile2, hookCtx, context);
|
|
var unique = serializationManager.Read<List<GasMixture>?>(node["uniqueMixes"], hookCtx, context);
|
|
|
|
tiles = new Dictionary<Vector2i, TileAtmosphere>();
|
|
|
|
if (unique != null && mixies != null)
|
|
{
|
|
foreach (var (indices, mix) in mixies)
|
|
{
|
|
try
|
|
{
|
|
tiles.Add(indices, new TileAtmosphere(EntityUid.Invalid, indices,
|
|
unique[mix].Clone()));
|
|
}
|
|
catch (ArgumentOutOfRangeException)
|
|
{
|
|
Logger.Error(
|
|
$"Error during atmos serialization! Tile at {indices} points to an unique mix ({mix}) out of range!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var dataNode = (MappingDataNode) node["data"];
|
|
var tileNode = (MappingDataNode) dataNode["tiles"];
|
|
var chunkSize = serializationManager.Read<int>(dataNode["chunkSize"], hookCtx, context);
|
|
|
|
var unique = serializationManager.Read<List<GasMixture>?>(dataNode["uniqueMixes"], hookCtx, context);
|
|
|
|
tiles = new Dictionary<Vector2i, TileAtmosphere>();
|
|
|
|
if (unique != null)
|
|
{
|
|
foreach (var (chunkNode, valueNode) in tileNode)
|
|
{
|
|
var chunkOrigin = serializationManager.Read<Vector2i>(chunkNode, hookCtx, context);
|
|
var chunk = serializationManager.Read<TileAtmosChunk>(valueNode, hookCtx, context);
|
|
|
|
foreach (var (mix, data) in chunk.Data)
|
|
{
|
|
for (var x = 0; x < chunkSize; x++)
|
|
{
|
|
for (var y = 0; y < chunkSize; y++)
|
|
{
|
|
var flag = data & (uint) (1 << (x + y * chunkSize));
|
|
|
|
if (flag == 0)
|
|
continue;
|
|
|
|
var indices = new Vector2i(x + chunkOrigin.X * chunkSize,
|
|
y + chunkOrigin.Y * chunkSize);
|
|
|
|
try
|
|
{
|
|
tiles.Add(indices, new TileAtmosphere(EntityUid.Invalid, indices,
|
|
unique[mix].Clone()));
|
|
}
|
|
catch (ArgumentOutOfRangeException)
|
|
{
|
|
Logger.Error(
|
|
$"Error during atmos serialization! Tile at {indices} points to an unique mix ({mix}) out of range!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return tiles;
|
|
}
|
|
|
|
public DataNode Write(ISerializationManager serializationManager, Dictionary<Vector2i, TileAtmosphere> value, IDependencyCollection dependencies,
|
|
bool alwaysWrite = false, ISerializationContext? context = null)
|
|
{
|
|
var uniqueMixes = new List<GasMixture>();
|
|
var tileChunks = new Dictionary<Vector2i, TileAtmosChunk>();
|
|
var chunkSize = 4;
|
|
|
|
foreach (var (gridIndices, tile) in value)
|
|
{
|
|
if (tile.Air == null) continue;
|
|
|
|
var mixIndex = uniqueMixes.IndexOf(tile.Air);
|
|
|
|
if (mixIndex == -1)
|
|
{
|
|
mixIndex = uniqueMixes.Count;
|
|
uniqueMixes.Add(tile.Air);
|
|
}
|
|
|
|
var chunkOrigin = SharedMapSystem.GetChunkIndices(gridIndices, chunkSize);
|
|
var tileChunk = tileChunks.GetOrNew(chunkOrigin);
|
|
var indices = SharedMapSystem.GetChunkRelative(gridIndices, chunkSize);
|
|
|
|
var mixFlag = tileChunk.Data.GetOrNew(mixIndex);
|
|
mixFlag |= (uint) 1 << (indices.X + indices.Y * chunkSize);
|
|
tileChunk.Data[mixIndex] = mixFlag;
|
|
}
|
|
|
|
if (uniqueMixes.Count == 0)
|
|
uniqueMixes = null;
|
|
if (tileChunks.Count == 0)
|
|
tileChunks = null;
|
|
|
|
var map = new MappingDataNode
|
|
{
|
|
{ "version", 2.ToString(CultureInfo.InvariantCulture) },
|
|
{
|
|
"data", serializationManager.WriteValue(new TileAtmosData
|
|
{
|
|
ChunkSize = chunkSize,
|
|
UniqueMixes = uniqueMixes,
|
|
TilesUniqueMixes = tileChunks,
|
|
}, alwaysWrite, context)
|
|
}
|
|
};
|
|
|
|
return map;
|
|
}
|
|
|
|
[DataDefinition]
|
|
private partial struct TileAtmosData
|
|
{
|
|
[DataField("chunkSize")] public int ChunkSize;
|
|
|
|
[DataField("uniqueMixes")] public List<GasMixture>? UniqueMixes;
|
|
|
|
[DataField("tiles")] public Dictionary<Vector2i, TileAtmosChunk>? TilesUniqueMixes;
|
|
}
|
|
|
|
[DataDefinition]
|
|
private partial record struct TileAtmosChunk()
|
|
{
|
|
/// <summary>
|
|
/// Key is unique mix and value is bitflag of the affected tiles.
|
|
/// </summary>
|
|
[IncludeDataField(customTypeSerializer: typeof(DictionarySerializer<int, uint>))]
|
|
public Dictionary<int, uint> Data = new();
|
|
}
|
|
|
|
public void CopyTo(ISerializationManager serializationManager, Dictionary<Vector2i, TileAtmosphere> source, ref Dictionary<Vector2i, TileAtmosphere> target,
|
|
IDependencyCollection dependencies,
|
|
SerializationHookContext hookCtx,
|
|
ISerializationContext? context = null)
|
|
{
|
|
target.Clear();
|
|
foreach (var (key, val) in source)
|
|
{
|
|
target.Add(key, new TileAtmosphere(val));
|
|
}
|
|
}
|
|
}
|