Skip to content

Commit

Permalink
fixing errors in script
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen authored Dec 1, 2021
1 parent 46539f3 commit 0e571ba
Showing 1 changed file with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Mirage.Logging;
using UnityEngine;

namespace Mirage.Visibility.SpatialHash
Expand All @@ -12,6 +13,8 @@ internal static class SpatialHashExtensions

public class SpatialHashSystem : MonoBehaviour
{
static readonly ILogger logger = LogFactory.GetLogger<SpatialHashSystem>();

public NetworkServer Server;

/// <summary>
Expand All @@ -23,7 +26,8 @@ public class SpatialHashSystem : MonoBehaviour
[Tooltip("height and width of 1 box in grid")]
public float gridSize = 10;

public Vector2 Centre = new Vector2(0, 0);
[Tooltip("Offset of world origin, used to shift positions into bounds. Should be bottom left of world, if world positions are negative then this should be negatve.")]
public Vector2 Offset = new Vector2(0, 0);

[Tooltip("Bounds of the map used to calculate visibility. Objects out side of grid will not be visibility")]
public Vector2 Size = new Vector2(100, 100);
Expand All @@ -43,7 +47,7 @@ public void Awake()
// skip first invoke, list will be empty
InvokeRepeating(nameof(RebuildObservers), VisibilityUpdateInterval, VisibilityUpdateInterval);

Grid = new GridHolder<INetworkPlayer>(gridSize, Centre, Size);
Grid = new GridHolder<INetworkPlayer>(gridSize, Offset, Size);
});

Server.Stopped.AddListener(() =>
Expand Down Expand Up @@ -115,22 +119,32 @@ public class GridHolder<T>
public readonly int Width;
public readonly int Height;
public readonly float GridSize;
public readonly Vector2 Centre;
public readonly Vector2 Offset;
public readonly Vector2 Size;

public readonly GridPoint[] Points;

public GridHolder(float gridSize, Vector2 centre, Vector2 size)
public GridHolder(float gridSize, Vector2 offset, Vector2 size)
{
Centre = centre;
Offset = offset;
Size = size;
Width = Mathf.CeilToInt(size.x / gridSize);
Height = Mathf.CeilToInt(size.y / gridSize);
// todo check comment
// +1 so we can round up at max size of grid
// I think this is also needed because we are rounding, so if size is 100, and gridSize is 15, then we have 90 as the max
// ^ this doesn't sound right because we are doing ceil below
Width = Mathf.CeilToInt(size.x / gridSize) + 1;
Height = Mathf.CeilToInt(size.y / gridSize) + 1;
GridSize = gridSize;

Points = new GridPoint[Width * Height];
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
int ToIndex(int x, int y)
{
return x + y * Width;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddObject(Vector2 position, T obj)
{
Expand All @@ -141,7 +155,8 @@ public void AddObject(Vector2 position, T obj)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddObject(int i, int j, T obj)
{
int index = i + j * Width;
int index = ToIndex(i, j);
if (index > Points.Length) logger.LogError($"Out of bounds for ({i},{j}). Max:({Width},{Height})");
if (Points[index].objects == null)
{
Points[index].objects = new HashSet<T>();
Expand All @@ -153,20 +168,20 @@ public void AddObject(int i, int j, T obj)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public HashSet<T> GetObjects(int i, int j)
{
return Points[i + j * Width].objects;
return Points[ToIndex(i, j)].objects;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CreateSet(int i, int j)
{
Points[i + j * Width].objects = new HashSet<T>();
Points[ToIndex(i, j)].objects = new HashSet<T>();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool InBounds(Vector2 position)
{
float x = position.x - Centre.x;
float y = position.y - Centre.y;
float x = position.x - Offset.x;
float y = position.y - Offset.y;

return (0 < x && x < Size.x)
&& (0 < y && y < Size.y);
Expand Down Expand Up @@ -202,11 +217,18 @@ bool AreClose(int a, int b, int range)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToGridIndex(Vector2 position, out int x, out int y)
{
float fx = position.x - Centre.x;
float fy = position.y - Centre.y;
float fx = position.x - Offset.x;
float fy = position.y - Offset.y;

x = Mathf.RoundToInt(fx / GridSize);
y = Mathf.RoundToInt(fy / GridSize);

if (x < 0) logger.LogError($"X was Negative for pos:{position.x}");
if (y < 0) logger.LogError($"Y was Negative for pos:{position.y}");

// include equal in error, 0 indexed
if (x >= Width) logger.LogError($"X was Greater than Width({Width}) for pos:{position.x}");
if (y >= Height) logger.LogError($"Y was Greater than Width({Height}) for pos:{position.y}");
}

public void BuildObservers(HashSet<T> observers, Vector2 position, int range)
Expand All @@ -215,15 +237,18 @@ public void BuildObservers(HashSet<T> observers, Vector2 position, int range)
if (!InBounds(position))
return;

ToGridIndex(position - Centre, out int x, out int y);
ToGridIndex(position, out int x, out int y);

for (int i = x - range; i <= x + range; i++)
{
for (int j = y - range; j <= y + range; j++)
{
if (InBounds(i, j))
{
observers.UnionWith(GetObjects(i, j));
HashSet<T> obj = GetObjects(i, j);
// obj might be null if objects are never added to that grid
if (obj != null)
observers.UnionWith(obj);
}
}
}
Expand Down

0 comments on commit 0e571ba

Please sign in to comment.