Skip to content

Commit

Permalink
Split GasTileOverlaySystem update over two ticks (#26542)
Browse files Browse the repository at this point in the history
Split GasTileOverlaySystem update over ticks
  • Loading branch information
ElectroJr authored Mar 30, 2024
1 parent d215419 commit bb5ca72
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions Content.Server/Atmos/EntitySystems/GasTileOverlaySystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public sealed class GasTileOverlaySystem : SharedGasTileOverlaySystem
new DefaultObjectPool<Dictionary<NetEntity, HashSet<Vector2i>>>(
new DefaultPooledObjectPolicy<Dictionary<NetEntity, HashSet<Vector2i>>>(), 64);

private bool _doSessionUpdate;

/// <summary>
/// Overlay update interval, in seconds.
/// </summary>
Expand Down Expand Up @@ -192,15 +194,15 @@ public GasOverlayData GetOverlayData(GasMixture? mixture)
/// <summary>
/// Updates the visuals for a tile on some grid chunk. Returns true if the visuals have changed.
/// </summary>
private bool UpdateChunkTile(GridAtmosphereComponent gridAtmosphere, GasOverlayChunk chunk, Vector2i index, GameTick curTick)
private bool UpdateChunkTile(GridAtmosphereComponent gridAtmosphere, GasOverlayChunk chunk, Vector2i index)
{
ref var oldData = ref chunk.TileData[chunk.GetDataIndex(index)];
if (!gridAtmosphere.Tiles.TryGetValue(index, out var tile))
{
if (oldData.Equals(default))
return false;

chunk.LastUpdate = curTick;
chunk.LastUpdate = _gameTiming.CurTick;
oldData = default;
return true;
}
Expand Down Expand Up @@ -258,11 +260,11 @@ private bool UpdateChunkTile(GridAtmosphereComponent gridAtmosphere, GasOverlayC
if (!changed)
return false;

chunk.LastUpdate = curTick;
chunk.LastUpdate = _gameTiming.CurTick;
return true;
}

private void UpdateOverlayData(GameTick curTick)
private void UpdateOverlayData()
{
// TODO parallelize?
var query = EntityQueryEnumerator<GasTileOverlayComponent, GridAtmosphereComponent, MetaDataComponent>();
Expand All @@ -276,7 +278,7 @@ private void UpdateOverlayData(GameTick curTick)
if (!overlay.Chunks.TryGetValue(chunkIndex, out var chunk))
overlay.Chunks[chunkIndex] = chunk = new GasOverlayChunk(chunkIndex);

changed |= UpdateChunkTile(gam, chunk, index, curTick);
changed |= UpdateChunkTile(gam, chunk, index);
}

if (changed)
Expand All @@ -291,13 +293,28 @@ public override void Update(float frameTime)
base.Update(frameTime);
AccumulatedFrameTime += frameTime;

if (AccumulatedFrameTime < _updateInterval) return;
AccumulatedFrameTime -= _updateInterval;
if (_doSessionUpdate)
{
UpdateSessions();
return;
}

if (AccumulatedFrameTime < _updateInterval)
return;

var curTick = _gameTiming.CurTick;
AccumulatedFrameTime -= _updateInterval;

// First, update per-chunk visual data for any invalidated tiles.
UpdateOverlayData(curTick);
UpdateOverlayData();

// Then, next tick we send the data to players.
// This is to avoid doing all the work in the same tick.
_doSessionUpdate = true;
}

public void UpdateSessions()
{
_doSessionUpdate = false;

if (!PvsEnabled)
return;
Expand All @@ -315,11 +332,11 @@ public override void Update(float frameTime)
_sessions.Add(player);
}

if (_sessions.Count > 0)
{
_updateJob.CurrentTick = curTick;
_parMan.ProcessNow(_updateJob, _sessions.Count);
}
if (_sessions.Count == 0)
return;

_parMan.ProcessNow(_updateJob, _sessions.Count);
_updateJob.LastSessionUpdate = _gameTiming.CurTick;
}

public void Reset(RoundRestartCleanupEvent ev)
Expand Down Expand Up @@ -352,7 +369,7 @@ private record struct UpdatePlayerJob : IParallelRobustJob
public ObjectPool<HashSet<Vector2i>> ChunkIndexPool;
public ObjectPool<Dictionary<NetEntity, HashSet<Vector2i>>> ChunkViewerPool;

public GameTick CurrentTick;
public GameTick LastSessionUpdate;
public Dictionary<ICommonSession, Dictionary<NetEntity, HashSet<Vector2i>>> LastSentChunks;
public List<ICommonSession> Sessions;

Expand Down Expand Up @@ -415,7 +432,7 @@ public void Execute(int index)

if (previousChunks != null &&
previousChunks.Contains(gIndex) &&
value.LastUpdate != CurrentTick)
value.LastUpdate > LastSessionUpdate)
{
continue;
}
Expand Down

0 comments on commit bb5ca72

Please sign in to comment.