68 lines
2.3 KiB
C#
68 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using Vintagestory.API.MathTools;
|
|
|
|
namespace ClaimLink;
|
|
|
|
internal static class ClaimLinkBufferIndex
|
|
{
|
|
private static readonly Dictionary<long, HashSet<int>> groupIdsByRegion = new();
|
|
private static readonly Dictionary<int, List<Cuboidi>> buffersByGroupId = new();
|
|
|
|
internal static void Rebuild()
|
|
{
|
|
groupIdsByRegion.Clear();
|
|
buffersByGroupId.Clear();
|
|
|
|
int regionSize = ClaimLinkModSystem.WorldManager.RegionSize;
|
|
if (regionSize <= 0)
|
|
return;
|
|
|
|
foreach (int groupId in ClaimLinkModSystem.Registry.All)
|
|
{
|
|
List<Cuboidi> buffers = ClaimLinkBuffer.GetBuffer(groupId);
|
|
buffersByGroupId[groupId] = buffers;
|
|
|
|
HashSet<long> regionKeys = new();
|
|
foreach (Cuboidi buffer in buffers)
|
|
{
|
|
int minRegionX = buffer.MinX / regionSize;
|
|
int maxRegionX = buffer.MaxX / regionSize;
|
|
int minRegionZ = buffer.MinZ / regionSize;
|
|
int maxRegionZ = buffer.MaxZ / regionSize;
|
|
|
|
for (int rx = minRegionX; rx <= maxRegionX; rx++)
|
|
for (int rz = minRegionZ; rz <= maxRegionZ; rz++)
|
|
regionKeys.Add(ClaimLinkModSystem.WorldManager.MapRegionIndex2D(rx, rz));
|
|
}
|
|
|
|
foreach (long regionKey in regionKeys)
|
|
{
|
|
if (!groupIdsByRegion.TryGetValue(regionKey, out HashSet<int>? groupIds))
|
|
groupIdsByRegion[regionKey] = groupIds = new();
|
|
groupIds.Add(groupId);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static IEnumerable<int> CandidateGroupIds(BlockPos pos)
|
|
{
|
|
int regionSize = ClaimLinkModSystem.WorldManager.RegionSize;
|
|
if (regionSize <= 0)
|
|
return System.Array.Empty<int>();
|
|
|
|
long regionKey = ClaimLinkModSystem.WorldManager.MapRegionIndex2D(
|
|
pos.X / regionSize,
|
|
pos.Z / regionSize
|
|
);
|
|
|
|
return groupIdsByRegion.TryGetValue(regionKey, out HashSet<int>? groupIds)
|
|
? groupIds
|
|
: System.Array.Empty<int>();
|
|
}
|
|
|
|
internal static List<Cuboidi> CachedBuffer(int groupId) =>
|
|
buffersByGroupId.TryGetValue(groupId, out List<Cuboidi>? buffers)
|
|
? buffers
|
|
: new List<Cuboidi>();
|
|
}
|