Implicit gas tile & decal states (#15146)

This commit is contained in:
Leon Friedrich
2023-04-06 11:43:12 +12:00
committed by GitHub
parent a2872f9306
commit a36a64e6c5
4 changed files with 60 additions and 57 deletions

View File

@@ -53,8 +53,6 @@ namespace Content.Server.Atmos.EntitySystems
private int _thresholds;
private bool _pvsEnabled;
public override void Initialize()
{
base.Initialize();
@@ -64,7 +62,6 @@ namespace Content.Server.Atmos.EntitySystems
_confMan.OnValueChanged(CVars.NetPVS, OnPvsToggle, true);
SubscribeLocalEvent<RoundRestartCleanupEvent>(Reset);
SubscribeLocalEvent<GasTileOverlayComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<GasTileOverlayComponent, ComponentStartup>(OnStartup);
}
@@ -86,10 +83,10 @@ namespace Content.Server.Atmos.EntitySystems
private void OnPvsToggle(bool value)
{
if (value == _pvsEnabled)
if (value == PvsEnabled)
return;
_pvsEnabled = value;
PvsEnabled = value;
if (value)
return;
@@ -254,7 +251,7 @@ namespace Content.Server.Atmos.EntitySystems
// First, update per-chunk visual data for any invalidated tiles.
UpdateOverlayData(curTick);
if (!_pvsEnabled)
if (!PvsEnabled)
return;
// Now we'll go through each player, then through each chunk in range of that player checking if the player is still in range
@@ -355,27 +352,5 @@ namespace Content.Server.Atmos.EntitySystems
data.Clear();
}
}
private void OnGetState(EntityUid uid, GasTileOverlayComponent component, ref ComponentGetState args)
{
if (_pvsEnabled && !args.ReplayState)
return;
// Should this be a full component state or a delta-state?
if (args.FromTick <= component.CreationTick || args.FromTick <= component.ForceTick)
{
args.State = new GasTileOverlayState(component.Chunks);
return;
}
var data = new Dictionary<Vector2i, GasOverlayChunk>();
foreach (var (index, chunk) in component.Chunks)
{
if (chunk.LastUpdate >= args.FromTick)
data[index] = chunk;
}
args.State = new GasTileOverlayState(data) { AllChunks = new(component.Chunks.Keys) };
}
}
}

View File

@@ -41,7 +41,6 @@ namespace Content.Server.Decals
private ObjectPool<Dictionary<EntityUid, HashSet<Vector2i>>> _chunkViewerPool =
new DefaultObjectPool<Dictionary<EntityUid, HashSet<Vector2i>>>(
new DefaultPooledObjectPolicy<Dictionary<EntityUid, HashSet<Vector2i>>>(), 64);
private bool _pvsEnabled;
public override void Initialize()
{
@@ -49,7 +48,6 @@ namespace Content.Server.Decals
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
SubscribeLocalEvent<TileChangedEvent>(OnTileChanged);
SubscribeLocalEvent<DecalGridComponent, ComponentGetState>(OnGetState);
SubscribeNetworkEvent<RequestDecalPlacementEvent>(OnDecalPlacementRequest);
SubscribeNetworkEvent<RequestDecalRemovalEvent>(OnDecalRemovalRequest);
@@ -60,10 +58,10 @@ namespace Content.Server.Decals
private void OnPvsToggle(bool value)
{
if (value == _pvsEnabled)
if (value == PvsEnabled)
return;
_pvsEnabled = value;
PvsEnabled = value;
if (value)
return;
@@ -80,28 +78,6 @@ namespace Content.Server.Decals
}
}
private void OnGetState(EntityUid uid, DecalGridComponent component, ref ComponentGetState args)
{
if (_pvsEnabled && !args.ReplayState)
return;
// Should this be a full component state or a delta-state?
if (args.FromTick <= component.CreationTick || args.FromTick <= component.ForceTick)
{
args.State = new DecalGridState(component.ChunkCollection.ChunkCollection);
return;
}
var data = new Dictionary<Vector2i, DecalChunk>();
foreach (var (index, chunk) in component.ChunkCollection.ChunkCollection)
{
if (chunk.LastModified >= args.FromTick)
data[index] = chunk;
}
args.State = new DecalGridState(data) { AllChunks = new(component.ChunkCollection.ChunkCollection.Keys) };
}
private void OnGridSplit(ref PostGridSplitEvent ev)
{
if (!TryComp(ev.OldGrid, out DecalGridComponent? oldComp))
@@ -408,13 +384,13 @@ namespace Content.Server.Decals
Dirty(decals);
}
if (!_pvsEnabled)
if (!PvsEnabled)
{
_dirtyChunks.Clear();
return;
}
if (_pvsEnabled)
if (PvsEnabled)
{
var players = _playerManager.ServerSessions.Where(x => x.Status == SessionStatus.InGame).ToArray();
var opts = new ParallelOptions { MaxDegreeOfParallelism = _parMan.ParallelProcessCount };

View File

@@ -1,4 +1,6 @@
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.Prototypes;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
@@ -8,6 +10,7 @@ namespace Content.Shared.Atmos.EntitySystems
{
public const byte ChunkSize = 8;
protected float AccumulatedFrameTime;
protected bool PvsEnabled;
[Dependency] protected readonly IPrototypeManager ProtoMan = default!;
@@ -19,6 +22,7 @@ namespace Content.Shared.Atmos.EntitySystems
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GasTileOverlayComponent, ComponentGetState>(OnGetState);
List<int> visibleGases = new();
@@ -32,6 +36,28 @@ namespace Content.Shared.Atmos.EntitySystems
VisibleGasId = visibleGases.ToArray();
}
private void OnGetState(EntityUid uid, GasTileOverlayComponent component, ref ComponentGetState args)
{
if (PvsEnabled && !args.ReplayState)
return;
// Should this be a full component state or a delta-state?
if (args.FromTick <= component.CreationTick || args.FromTick <= component.ForceTick)
{
args.State = new GasTileOverlayState(component.Chunks);
return;
}
var data = new Dictionary<Vector2i, GasOverlayChunk>();
foreach (var (index, chunk) in component.Chunks)
{
if (chunk.LastUpdate >= args.FromTick)
data[index] = chunk;
}
args.State = new GasTileOverlayState(data) { AllChunks = new(component.Chunks.Keys) };
}
public static Vector2i GetGasChunkIndices(Vector2i indices)
{
return new((int) MathF.Floor((float) indices.X / ChunkSize), (int) MathF.Floor((float) indices.Y / ChunkSize));

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
@@ -11,6 +12,8 @@ namespace Content.Shared.Decals
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
[Dependency] protected readonly IMapManager MapManager = default!;
protected bool PvsEnabled;
// Note that this constant is effectively baked into all map files, because of how they save the grid decal component.
// So if this ever needs changing, the maps need converting.
public const int ChunkSize = 32;
@@ -22,13 +25,36 @@ namespace Content.Shared.Decals
SubscribeLocalEvent<GridInitializeEvent>(OnGridInitialize);
SubscribeLocalEvent<DecalGridComponent, ComponentStartup>(OnCompStartup);
SubscribeLocalEvent<DecalGridComponent, ComponentGetState>(OnGetState);
}
private void OnGetState(EntityUid uid, DecalGridComponent component, ref ComponentGetState args)
{
if (PvsEnabled && !args.ReplayState)
return;
// Should this be a full component state or a delta-state?
if (args.FromTick <= component.CreationTick || args.FromTick <= component.ForceTick)
{
args.State = new DecalGridState(component.ChunkCollection.ChunkCollection);
return;
}
var data = new Dictionary<Vector2i, DecalChunk>();
foreach (var (index, chunk) in component.ChunkCollection.ChunkCollection)
{
if (chunk.LastModified >= args.FromTick)
data[index] = chunk;
}
args.State = new DecalGridState(data) { AllChunks = new(component.ChunkCollection.ChunkCollection.Keys) };
}
private void OnGridInitialize(GridInitializeEvent msg)
{
EnsureComp<DecalGridComponent>(msg.EntityUid);
}
private void OnCompStartup(EntityUid uid, DecalGridComponent component, ComponentStartup args)
{
foreach (var (indices, decals) in component.ChunkCollection.ChunkCollection)