mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-02-15 03:30:53 +01:00
Grid Components Allocate Grids (#2597)
* Deleting a map is now routed through MapComponent deletion. * Remove map and grid deletions from map networking, just delete the entity if you want to remove the map. * Moved the chunkSize property of created grids from the networked MapData to the MapGridComponent state. * Remove unused IMapManager.DefaultMap property. * Removed MapCreationTick field from MapManager.MapCollection. * Removed _maps hashset field from MapManager. * Removed CreatedMaps array from network MapData. * MapGrid.ParentMapId is now derived from the bound TransformComponent, and isn't required in the MapGrid ctor. Removed MapGrid.CreatedTick, it can be found on MapGridComponent.CreationTick. * Remove a bunch of ApplyGameStatePre code duplication. * Completely refactored CreateGrid. * Adds AddComponentUninitialized to the ECS system. This allows you to access a component before it is initialized in a using block. * Use AddComponentUninitialized to allocate a grid after the component is allocated. * MapLoader now creates the grids after creating the map entities. * Chunksize and TileSize properties are now actually read out of the map yaml. TileSize has a public Setter. * Minor cleanup. * Moved grid allocation onto the MapGridComponent. * Final Cleanup. * Merge Fail. * Fixed test, grid was getting deleted because it was empty. * Remove DeletedChunkDatum from grid networking. * ApplyMapGridState moved from map manager to the MapGridComponent.
This commit is contained in:
@@ -5,7 +5,6 @@ using System.Linq;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Physics;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Robust.Shared.Map;
|
||||
@@ -109,54 +108,22 @@ internal partial class MapManager
|
||||
return false;
|
||||
}
|
||||
|
||||
public IMapGridInternal CreateBoundGrid(MapId mapId, MapGridComponent gridComponent)
|
||||
/// <inheritdoc />
|
||||
public void OnGridAllocated(MapGridComponent gridComponent, MapGrid mapGrid)
|
||||
{
|
||||
var mapGrid = CreateUnboundGrid(mapId);
|
||||
|
||||
BindGrid(gridComponent, mapGrid);
|
||||
return mapGrid;
|
||||
}
|
||||
|
||||
public void BindGrid(MapGridComponent gridComponent, MapGrid mapGrid)
|
||||
{
|
||||
gridComponent.Grid = mapGrid;
|
||||
gridComponent.GridIndex = mapGrid.Index;
|
||||
mapGrid.GridEntityId = gridComponent.Owner;
|
||||
|
||||
_grids.Add(mapGrid.Index, mapGrid.GridEntityId);
|
||||
Logger.InfoS("map", $"Binding grid {mapGrid.Index} to entity {gridComponent.Owner}");
|
||||
OnGridCreated?.Invoke(mapGrid.ParentMapId, mapGrid.Index);
|
||||
}
|
||||
|
||||
public MapGrid CreateUnboundGrid(MapId mapId)
|
||||
{
|
||||
var newGrid = CreateGridImpl(mapId, null, 16, false, default);
|
||||
Logger.InfoS("map", $"Creating unbound grid {newGrid.Index}");
|
||||
return (MapGrid) newGrid;
|
||||
}
|
||||
|
||||
public IEnumerable<IMapGrid> GetAllGrids()
|
||||
{
|
||||
return EntityManager.EntityQuery<IMapGridComponent>(true).Select(c => c.Grid);
|
||||
}
|
||||
|
||||
public IMapGrid CreateGrid(MapId currentMapId, GridId? gridId = null, ushort chunkSize = 16)
|
||||
public IMapGrid CreateGrid(MapId currentMapId, GridId? forcedGridId = null, ushort chunkSize = 16)
|
||||
{
|
||||
DebugTools.Assert(gridId != GridId.Invalid);
|
||||
|
||||
var mapGrid = CreateGridImpl(currentMapId, gridId, chunkSize, true, default);
|
||||
_grids.Add(mapGrid.Index, mapGrid.GridEntityId);
|
||||
Logger.InfoS("map", $"Creating new grid {mapGrid.Index}");
|
||||
|
||||
EntityManager.InitializeComponents(mapGrid.GridEntityId);
|
||||
EntityManager.StartComponents(mapGrid.GridEntityId);
|
||||
OnGridCreated?.Invoke(currentMapId, mapGrid.Index);
|
||||
return mapGrid;
|
||||
}
|
||||
|
||||
public IMapGrid GetGrid(EntityUid euid)
|
||||
{
|
||||
return GetGridComp(euid).Grid;
|
||||
return CreateGrid(currentMapId, forcedGridId, chunkSize, default);
|
||||
}
|
||||
|
||||
public IMapGrid GetGrid(GridId gridId)
|
||||
@@ -273,11 +240,6 @@ internal partial class MapManager
|
||||
OnGridRemoved?.Invoke(mapId, gridId);
|
||||
}
|
||||
|
||||
public GridId NextGridId()
|
||||
{
|
||||
return _highestGridId = new GridId(_highestGridId.Value + 1);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public event EventHandler<TileChangedEventArgs>? TileChanged;
|
||||
|
||||
@@ -326,35 +288,46 @@ internal partial class MapManager
|
||||
EntityManager.EventBus.RaiseLocalEvent(euid, new TileChangedEvent(tileRef, oldTile));
|
||||
}
|
||||
|
||||
protected IMapGrid CreateGrid(MapId currentMapId, GridId gridId, ushort chunkSize, EntityUid euid)
|
||||
protected MapGrid CreateGrid(MapId currentMapId, GridId? forcedGridId, ushort chunkSize, EntityUid forcedGridEuid)
|
||||
{
|
||||
var mapGrid = CreateGridImpl(currentMapId, gridId, chunkSize, true, euid);
|
||||
_grids.Add(mapGrid.Index, mapGrid.GridEntityId);
|
||||
Logger.InfoS("map", $"Creating new grid {mapGrid.Index}");
|
||||
var gridEnt = EntityManager.CreateEntityUninitialized(null, forcedGridEuid);
|
||||
|
||||
EntityManager.InitializeComponents(mapGrid.GridEntityId);
|
||||
EntityManager.StartComponents(mapGrid.GridEntityId);
|
||||
OnGridCreated?.Invoke(currentMapId, mapGrid.Index);
|
||||
return mapGrid;
|
||||
//TODO: Also known as Component.OnAdd ;)
|
||||
MapGrid grid;
|
||||
using (var preInit = EntityManager.AddComponentUninitialized<MapGridComponent>(gridEnt))
|
||||
{
|
||||
var actualId = GenerateGridId(forcedGridId);
|
||||
preInit.Comp.GridIndex = actualId; // Required because of MapGrid needing it in ctor
|
||||
preInit.Comp.AllocMapGrid(chunkSize, 1);
|
||||
grid = (MapGrid) preInit.Comp.Grid;
|
||||
}
|
||||
|
||||
Logger.DebugS("map", $"Binding new grid {grid.Index} to entity {grid.GridEntityId}");
|
||||
|
||||
//TODO: This is a hack to get TransformComponent.MapId working before entity states
|
||||
//are applied. After they are applied the parent may be different, but the MapId will
|
||||
//be the same. This causes TransformComponent.ParentUid of a grid to be unsafe to
|
||||
//use in transform states anytime before the state parent is properly set.
|
||||
var fallbackParentEuid = GetMapEntityIdOrThrow(currentMapId);
|
||||
EntityManager.GetComponent<TransformComponent>(gridEnt).AttachParent(fallbackParentEuid);
|
||||
|
||||
EntityManager.InitializeComponents(grid.GridEntityId);
|
||||
EntityManager.StartComponents(grid.GridEntityId);
|
||||
return grid;
|
||||
}
|
||||
|
||||
protected void InvokeGridChanged(object? sender, GridChangedEventArgs ev)
|
||||
protected internal static void InvokeGridChanged(MapManager mapManager, IMapGrid mapGrid, IReadOnlyCollection<(Vector2i position, Tile tile)> changedTiles)
|
||||
{
|
||||
GridChanged?.Invoke(sender, ev);
|
||||
var args = new GridModifiedEvent(ev.Grid, ev.Modified);
|
||||
EntityManager.EventBus.RaiseLocalEvent(ev.Grid.GridEntityId, args);
|
||||
mapManager.GridChanged?.Invoke(mapManager, new GridChangedEventArgs(mapGrid, changedTiles));
|
||||
mapManager.EntityManager.EventBus.RaiseLocalEvent(mapGrid.GridEntityId, new GridModifiedEvent(mapGrid, changedTiles));
|
||||
}
|
||||
|
||||
private IMapGridInternal CreateGridImpl(MapId currentMapId, GridId? gridId, ushort chunkSize, bool createEntity,
|
||||
EntityUid euid)
|
||||
public GridId GenerateGridId(GridId? forcedGridId)
|
||||
{
|
||||
#if DEBUG
|
||||
DebugTools.Assert(_dbgGuardRunning);
|
||||
#endif
|
||||
var actualId = forcedGridId ?? new GridId(_highestGridId.Value + 1);
|
||||
|
||||
var actualId = gridId ?? new GridId(_highestGridId.Value + 1);
|
||||
|
||||
DebugTools.Assert(actualId != GridId.Invalid);
|
||||
if(actualId == GridId.Invalid)
|
||||
throw new InvalidOperationException($"Cannot allocate a grid with an Invalid ID.");
|
||||
|
||||
if (GridExists(actualId))
|
||||
throw new InvalidOperationException($"A grid with ID {actualId} already exists");
|
||||
@@ -362,49 +335,9 @@ internal partial class MapManager
|
||||
if (_highestGridId.Value < actualId.Value)
|
||||
_highestGridId = actualId;
|
||||
|
||||
var grid = new MapGrid(this, EntityManager, actualId, chunkSize, currentMapId);
|
||||
if(forcedGridId is not null) // this function basically just passes forced gridIds through.
|
||||
Logger.DebugS("map", $"Allocating new GridId {actualId}.");
|
||||
|
||||
if (actualId != GridId.Invalid && createEntity) // nullspace default grid is not bound to an entity
|
||||
{
|
||||
// the entity may already exist from map deserialization
|
||||
IMapGridComponent? result = null;
|
||||
foreach (var comp in EntityManager.EntityQuery<MapGridComponent>(true))
|
||||
{
|
||||
if (comp.GridIndex != actualId)
|
||||
continue;
|
||||
|
||||
result = comp;
|
||||
break;
|
||||
}
|
||||
|
||||
if (result != null)
|
||||
{
|
||||
grid.GridEntityId = result.Owner;
|
||||
((MapGridComponent)result).Grid = grid;
|
||||
Logger.DebugS("map", $"Rebinding grid {actualId} to entity {grid.GridEntityId}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var gridEnt = EntityManager.CreateEntityUninitialized(null, euid);
|
||||
|
||||
grid.GridEntityId = gridEnt;
|
||||
|
||||
Logger.DebugS("map", $"Binding grid {actualId} to entity {grid.GridEntityId}");
|
||||
|
||||
var gridComp = EntityManager.AddComponent<MapGridComponent>(gridEnt);
|
||||
gridComp.GridIndex = grid.Index;
|
||||
gridComp.Grid = grid;
|
||||
|
||||
//TODO: This is a hack to get TransformComponent.MapId working before entity states
|
||||
//are applied. After they are applied the parent may be different, but the MapId will
|
||||
//be the same. This causes TransformComponent.ParentUid of a grid to be unsafe to
|
||||
//use in transform states anytime before the state parent is properly set.
|
||||
EntityManager.GetComponent<TransformComponent>(gridEnt).AttachParent(GetMapEntityIdOrThrow(currentMapId));
|
||||
}
|
||||
}
|
||||
else
|
||||
Logger.DebugS("map", $"Skipping entity binding for gridId {actualId}");
|
||||
|
||||
return grid;
|
||||
return actualId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user