Move chunk enumerators to engine (#4901)

* Move chunk enumerators to engine

* notes

* Cleanup
This commit is contained in:
metalgearsloth
2024-02-23 17:51:34 +11:00
committed by GitHub
parent a77eee5658
commit a5d4b8096f
6 changed files with 78 additions and 50 deletions

View File

@@ -35,11 +35,11 @@ END TEMPLATE-->
### Breaking changes
*None yet*
* Moved ChunkIndicesEnumerator to engine and to a re-useable namespace at Robust.Shared/Maps.
### New features
*None yet*
* Added an Enlarged method for Box2Rotated.
### Bugfixes

View File

@@ -1,46 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Robust.Shared.Maths;
namespace Robust.Server.GameStates;
public struct ChunkIndicesEnumerator
{
private Vector2i _bottomLeft;
private Vector2i _topRight;
private int _x;
private int _y;
public ChunkIndicesEnumerator(Vector2 viewPos, float range, float chunkSize)
{
var rangeVec = new Vector2(range, range);
_bottomLeft = ((viewPos - rangeVec) / chunkSize).Floored();
// Also floor this as we get the whole chunk anyway.
_topRight = ((viewPos + rangeVec) / chunkSize).Floored();
_x = _bottomLeft.X;
_y = _bottomLeft.Y;
}
public bool MoveNext([NotNullWhen(true)] out Vector2i? chunkIndices)
{
if (_y > _topRight.Y)
{
_x++;
_y = _bottomLeft.Y;
}
if (_x > _topRight.X)
{
chunkIndices = null;
return false;
}
chunkIndices = new Vector2i(_x, _y);
_y++;
return true;
}
}

View File

@@ -8,6 +8,7 @@ using Prometheus;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.Map.Components;
using Robust.Shared.Map.Enumerators;
using Robust.Shared.Maths;
using Robust.Shared.Player;
using Robust.Shared.Utility;

View File

@@ -3,6 +3,7 @@ using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using JetBrains.Annotations;
using Robust.Shared.Utility;
namespace Robust.Shared.Maths
@@ -57,7 +58,17 @@ namespace Robust.Shared.Maths
}
/// <summary>
/// calculates the smallest AABB that will encompass the rotated box. The AABB is in local space.
/// Enlarges the box by the specified value.
/// </summary>
[Pure]
public readonly Box2Rotated Enlarged(float value)
{
var box = Box.Enlarged(value);
return new Box2Rotated(box, Rotation, Origin);
}
/// <summary>
/// Calculates the smallest AABB that will encompass the rotated box. The AABB is in local space.
/// </summary>
public readonly Box2 CalcBoundingBox()
{

View File

@@ -9,7 +9,10 @@ namespace Robust.Shared.Map.Enumerators;
internal struct ChunkEnumerator
{
public static ChunkEnumerator Empty => new(new Dictionary<Vector2i, MapChunk>(), Box2.Empty, 16);
/// <summary>
/// An empty enumerator that will return nothing.
/// </summary>
public static ChunkEnumerator Empty => new(new Dictionary<Vector2i, MapChunk>(), Box2.Empty, 1);
private Dictionary<Vector2i, MapChunk> _chunks;
private Vector2i _chunkLB;

View File

@@ -0,0 +1,59 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using Robust.Shared.Maths;
namespace Robust.Shared.Map.Enumerators;
/// <summary>
/// Generic iterator for chunk indices for the specified bounds with the specified chunk size.
/// </summary>
public struct ChunkIndicesEnumerator
{
private readonly Vector2i _chunkLB;
private readonly Vector2i _chunkRT;
private int _xIndex;
private int _yIndex;
public ChunkIndicesEnumerator(Vector2 viewPos, float range, float chunkSize)
{
var rangeVec = new Vector2(range, range);
_chunkLB = ((viewPos - rangeVec) / chunkSize).Floored();
// Also floor this as we get the whole chunk anyway.
_chunkRT = ((viewPos + rangeVec) / chunkSize).Floored();
_xIndex = _chunkLB.X;
_yIndex = _chunkLB.Y;
}
public ChunkIndicesEnumerator(Box2 localAABB, int chunkSize)
{
_chunkLB = (localAABB.BottomLeft / chunkSize).Floored();
_chunkRT = (localAABB.TopRight / chunkSize).Floored();
_xIndex = _chunkLB.X;
_yIndex = _chunkLB.Y;
}
public bool MoveNext([NotNullWhen(true)] out Vector2i? indices)
{
if (_yIndex > _chunkRT.Y)
{
_yIndex = _chunkLB.Y;
_xIndex++;
}
if (_xIndex > _chunkRT.X)
{
indices = null;
return false;
}
indices = new Vector2i(_xIndex, _yIndex);
_yIndex++;
return true;
}
}