mirror of
https://github.com/space-wizards/RobustToolbox.git
synced 2026-09-15 14:52:35 +02:00
* Removed unused method TileRef.GetStep(). Removed unused property TileRef.TileSize. Removed unused property TileRef.Tile, made the field public with the same name. Made the TileRef struct readonly. Removed property TileRef.TileDef. Removed property TileRef.LocalPos. Renamed GridTile to GridIndices. Implemented IEquatable on TileRef. Implemented Equality operators on TileRef. Added PublicAPI attribute to TileRef. Added doc comments to TileRef. * Modified Tile: Added the PublicApi annotation. Made struct readonly. Filled out doc comments. Changed auto properties to readonly fields. Renamed TileId to TypeId. Added a static Empty tile to the struct. Implemented IEquatable. * Modified MapCoordinates: Added PublicApi attribute. Implemented IEquatable. Filled out doc comments. Removed Map property. * Modified ScreenCoordinates: Filled out doc comments. Added the PublicApi attribute. Implemented IEquatable. * Modified GridCoordinates: Removed method IsValidLocation. * Removed property GridCoordinates.Grid. * Removed GridCoordinates.Map. * Removed GridCoordinates.MapId. * Removed property GridCoordinates.IsWorld. Removed constructors taking a MapId. * Removed static IoCManager calls from GridCoordinates. * Modified GridCoordinates: Added attribute PublicApi. Filled out doc comments. * Filled out doc comments for MapGrid. Filled out doc comments for MapIndices. Misc refactors for both. * added command to teleport grids. * Added method Box2.Intersect. Added method Box2.Union. Placement manager now spawns new grids to place tiles not connected to an existing grid.
232 lines
8.6 KiB
C#
232 lines
8.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Robust.Shared.GameObjects.Components.Transform;
|
|
using Robust.Shared.Interfaces.Map;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Timing;
|
|
|
|
namespace Robust.Shared.Map
|
|
{
|
|
public partial class MapManager
|
|
{
|
|
/// <summary>
|
|
/// A square section of the map.
|
|
/// </summary>
|
|
internal class Chunk : IMapChunk
|
|
{
|
|
internal GameTick LastModifiedTick { get; private set; }
|
|
private readonly MapGrid _grid;
|
|
private readonly MapIndices _gridIndices;
|
|
private readonly MapManager _mapManager;
|
|
|
|
internal readonly Tile[,] _tiles;
|
|
private readonly SnapGridCell[,] _snapGrid;
|
|
|
|
/// <summary>
|
|
/// Constructs an instance of a MapGrid chunk.
|
|
/// </summary>
|
|
/// <param name="manager"></param>
|
|
/// <param name="grid"></param>
|
|
/// <param name="x"></param>
|
|
/// <param name="y"></param>
|
|
/// <param name="chunkSize"></param>
|
|
public Chunk(MapManager manager, MapGrid grid, int x, int y, ushort chunkSize)
|
|
{
|
|
_mapManager = manager;
|
|
LastModifiedTick = _mapManager._gameTiming.CurTick;
|
|
ChunkSize = chunkSize;
|
|
_grid = grid;
|
|
_gridIndices = new MapIndices(x, y);
|
|
|
|
_tiles = new Tile[ChunkSize, ChunkSize];
|
|
_snapGrid = new SnapGridCell[ChunkSize, ChunkSize];
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ushort ChunkSize { get; }
|
|
|
|
/// <inheritdoc />
|
|
public int X => _gridIndices.X;
|
|
|
|
/// <inheritdoc />
|
|
public int Y => _gridIndices.Y;
|
|
|
|
public MapIndices Index => new MapIndices(X, Y);
|
|
|
|
/// <summary>
|
|
/// Returns the tile at the given indices. The tile indices are relative locations to the chunk origin,
|
|
/// NOT local to the grid.
|
|
/// </summary>
|
|
/// <param name="xTile">The X tile index relative to the chunk origin.</param>
|
|
/// <param name="yTile">The Y tile index relative to the chunk origin.</param>
|
|
/// <returns>A reference to a tile.</returns>
|
|
public TileRef GetTile(ushort xTile, ushort yTile)
|
|
{
|
|
// array out of bounds
|
|
if (xTile >= ChunkSize || yTile >= ChunkSize)
|
|
throw new ArgumentOutOfRangeException("Tile indices out of bounds.");
|
|
|
|
var indices = ChunkTileToGridTile(new MapIndices(xTile, yTile));
|
|
return new TileRef(_grid.ParentMapId, _grid.Index, indices.X, indices.Y, _tiles[xTile, yTile]);
|
|
}
|
|
public TileRef GetTile(MapIndices indices)
|
|
{
|
|
// array out of bounds
|
|
if (indices.X >= ChunkSize || indices.X < 0 || indices.Y >= ChunkSize || indices.Y < 0)
|
|
throw new ArgumentOutOfRangeException("Tile indices out of bounds.");
|
|
|
|
return new TileRef(_grid.ParentMapId, _grid.Index, indices.X, indices.Y, _tiles[indices.X, indices.Y]);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
[Obsolete("Enumerate over the chunk instead.")]
|
|
public IEnumerable<TileRef> GetAllTiles(bool ignoreEmpty = true)
|
|
{
|
|
for (var x = 0; x < ChunkSize; x++)
|
|
for (var y = 0; y < ChunkSize; y++)
|
|
{
|
|
if (_tiles[x, y].IsEmpty)
|
|
continue;
|
|
|
|
var indices = ChunkTileToGridTile(new MapIndices(x, y));
|
|
yield return new TileRef(_grid.ParentMapId, _grid.Index, indices.X, indices.Y, _tiles[x, y]);
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void SetTile(ushort xChunkTile, ushort yChunkTile, Tile tile)
|
|
{
|
|
if (xChunkTile >= ChunkSize || yChunkTile >= ChunkSize)
|
|
throw new ArgumentException("Tile indices out of bounds.");
|
|
|
|
// same tile, no point to continue
|
|
if (_tiles[xChunkTile, yChunkTile].TypeId == tile.TypeId)
|
|
return;
|
|
|
|
var gridTile = ChunkTileToGridTile(new MapIndices(xChunkTile, yChunkTile));
|
|
var newTileRef = new TileRef(_grid.ParentMapId, _grid.Index, gridTile.X, gridTile.Y, tile);
|
|
var oldTile = _tiles[xChunkTile, yChunkTile];
|
|
_grid.LastModifiedTick = LastModifiedTick = _mapManager._gameTiming.CurTick;
|
|
_mapManager.RaiseOnTileChanged(newTileRef, oldTile);
|
|
_grid.UpdateAABB(gridTile);
|
|
|
|
_tiles[xChunkTile, yChunkTile] = tile;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an enumerator that iterates through all grid tiles.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IEnumerator<TileRef> GetEnumerator()
|
|
{
|
|
for (var x = 0; x < ChunkSize; x++)
|
|
for (var y = 0; y < ChunkSize; y++)
|
|
{
|
|
var gridTile = ChunkTileToGridTile(new MapIndices(x, y));
|
|
yield return new TileRef(_grid.ParentMapId, _grid.Index, gridTile.X, gridTile.Y, _tiles[x, y]);
|
|
}
|
|
}
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
{
|
|
return GetEnumerator();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public MapIndices GridTileToChunkTile(MapIndices gridTile)
|
|
{
|
|
var size = ChunkSize;
|
|
var x = MathHelper.Mod(gridTile.X, size);
|
|
var y = MathHelper.Mod(gridTile.Y, size);
|
|
return new MapIndices(x, y);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void SetTile(ushort xChunkTile, ushort yChunkTile, ushort tileId, ushort tileData = 0)
|
|
{
|
|
SetTile(xChunkTile, yChunkTile, new Tile(tileId, tileData));
|
|
}
|
|
|
|
|
|
public IEnumerable<SnapGridComponent> GetSnapGridCell(ushort xCell, ushort yCell, SnapGridOffset offset)
|
|
{
|
|
var cell = _snapGrid[xCell, yCell];
|
|
List<SnapGridComponent> list;
|
|
if (offset == SnapGridOffset.Center)
|
|
{
|
|
list = cell.Center;
|
|
}
|
|
else
|
|
{
|
|
list = cell.Edge;
|
|
}
|
|
|
|
if (list != null)
|
|
{
|
|
foreach (var element in list)
|
|
{
|
|
yield return element;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddToSnapGridCell(ushort xCell, ushort yCell, SnapGridOffset offset, SnapGridComponent snap)
|
|
{
|
|
ref var cell = ref _snapGrid[xCell, yCell];
|
|
if (offset == SnapGridOffset.Center)
|
|
{
|
|
if (cell.Center == null)
|
|
{
|
|
cell.Center = new List<SnapGridComponent>(1);
|
|
}
|
|
cell.Center.Add(snap);
|
|
}
|
|
else
|
|
{
|
|
if (cell.Edge == null)
|
|
{
|
|
cell.Edge = new List<SnapGridComponent>(1);
|
|
}
|
|
cell.Edge.Add(snap);
|
|
}
|
|
}
|
|
|
|
public void RemoveFromSnapGridCell(ushort xCell, ushort yCell, SnapGridOffset offset, SnapGridComponent snap)
|
|
{
|
|
ref var cell = ref _snapGrid[xCell, yCell];
|
|
if (offset == SnapGridOffset.Center)
|
|
{
|
|
cell.Center?.Remove(snap);
|
|
}
|
|
else
|
|
{
|
|
cell.Edge?.Remove(snap);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Translates chunk tile indices to grid tile indices.
|
|
/// </summary>
|
|
/// <param name="chunkTile">The indices relative to the chunk origin.</param>
|
|
/// <returns>The indices relative to the grid origin.</returns>
|
|
private MapIndices ChunkTileToGridTile(MapIndices chunkTile)
|
|
{
|
|
return chunkTile + _gridIndices * ChunkSize;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override string ToString()
|
|
{
|
|
return $"Chunk {_gridIndices}, {ChunkSize}";
|
|
}
|
|
|
|
private struct SnapGridCell
|
|
{
|
|
public List<SnapGridComponent> Center;
|
|
public List<SnapGridComponent> Edge;
|
|
}
|
|
}
|
|
}
|
|
}
|