new DefaultObjectPool<Dictionary<NetEntity, HashSet<Vector2i>>>(
new DefaultPooledObjectPolicy<Dictionary<NetEntity, HashSet<Vector2i>>>(), 64);
+ private bool _doSessionUpdate;
+
/// <summary>
/// Overlay update interval, in seconds.
/// </summary>
/// <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;
}
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>();
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)
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;
_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)
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;
if (previousChunks != null &&
previousChunks.Contains(gIndex) &&
- value.LastUpdate != CurrentTick)
+ value.LastUpdate > LastSessionUpdate)
{
continue;
}