mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Struct enumerator for GetAllGrids (#2624)
This commit is contained in:
31
Robust.Shared/Map/GridEnumerator.cs
Normal file
31
Robust.Shared/Map/GridEnumerator.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Robust.Shared.Map;
|
||||
|
||||
public struct GridEnumerator
|
||||
{
|
||||
private Dictionary<GridId, EntityUid>.Enumerator _enumerator;
|
||||
private EntityQuery<MapGridComponent> _query;
|
||||
|
||||
internal GridEnumerator(Dictionary<GridId, EntityUid>.Enumerator enumerator, EntityQuery<MapGridComponent> query)
|
||||
{
|
||||
_enumerator = enumerator;
|
||||
_query = query;
|
||||
}
|
||||
|
||||
public bool MoveNext([NotNullWhen(true)] out IMapGrid? grid)
|
||||
{
|
||||
if (!_enumerator.MoveNext())
|
||||
{
|
||||
grid = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var (_, uid) = _enumerator.Current;
|
||||
|
||||
grid = _query.GetComponent(uid).Grid;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,11 @@ namespace Robust.Shared.Map
|
||||
/// </summary>
|
||||
public interface IMapManager : IPauseManager
|
||||
{
|
||||
/// <summary>
|
||||
/// A faster version of <see cref="GetAllGrids"/>
|
||||
/// </summary>
|
||||
GridEnumerator GetAllGridsEnumerator();
|
||||
|
||||
IEnumerable<IMapGrid> GetAllGrids();
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -116,9 +116,20 @@ internal partial class MapManager
|
||||
OnGridCreated?.Invoke(mapGrid.ParentMapId, mapGrid.Index);
|
||||
}
|
||||
|
||||
public GridEnumerator GetAllGridsEnumerator()
|
||||
{
|
||||
var query = EntityManager.GetEntityQuery<MapGridComponent>();
|
||||
return new GridEnumerator(_grids.GetEnumerator(), query);
|
||||
}
|
||||
|
||||
public IEnumerable<IMapGrid> GetAllGrids()
|
||||
{
|
||||
return EntityManager.EntityQuery<IMapGridComponent>(true).Select(c => c.Grid);
|
||||
var compQuery = EntityManager.GetEntityQuery<MapGridComponent>();
|
||||
|
||||
foreach (var (_, uid) in _grids)
|
||||
{
|
||||
yield return compQuery.GetComponent(uid).Grid;
|
||||
}
|
||||
}
|
||||
|
||||
public IMapGrid CreateGrid(MapId currentMapId, GridId? forcedGridId = null, ushort chunkSize = 16)
|
||||
|
||||
@@ -62,8 +62,12 @@ internal sealed class NetworkedMapManager : MapManager, INetworkedMapManager
|
||||
public GameStateMapData? GetStateData(GameTick fromTick)
|
||||
{
|
||||
var gridDatums = new Dictionary<GridId, GameStateMapData.GridDatum>();
|
||||
foreach (MapGrid grid in GetAllGrids())
|
||||
var enumerator = GetAllGridsEnumerator();
|
||||
|
||||
while (enumerator.MoveNext(out var iGrid))
|
||||
{
|
||||
var grid = (MapGrid)iGrid;
|
||||
|
||||
if (grid.LastTileModifiedTick < fromTick)
|
||||
continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user