From 41df518a08543ea1e0a36e237a685274ce89d29a Mon Sep 17 00:00:00 2001 From: ikpil Date: Tue, 30 Jan 2024 00:40:20 +0900 Subject: [PATCH] fix: SOH issue step1 (#41) - https://github.com/ikpil/DotRecast/issues/41 --- src/DotRecast.Detour.Crowd/DtCrowd.cs | 8 +++-- src/DotRecast.Detour.Crowd/DtProximityGrid.cs | 33 +++++++++++++++---- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtCrowd.cs b/src/DotRecast.Detour.Crowd/DtCrowd.cs index 0b7ef02..f2e180e 100644 --- a/src/DotRecast.Detour.Crowd/DtCrowd.cs +++ b/src/DotRecast.Detour.Crowd/DtCrowd.cs @@ -894,10 +894,12 @@ namespace DotRecast.Detour.Crowd { result.Clear(); - var proxAgents = new HashSet(); - int nids = grid.QueryItems(pos.X - range, pos.Z - range, pos.X + range, pos.Z + range, ref proxAgents); - foreach (DtCrowdAgent ag in proxAgents) + int MAX_NEIS = 32; + var ids = new DtCrowdAgent[MAX_NEIS]; + int nids = grid.QueryItems(pos.X - range, pos.Z - range, pos.X + range, pos.Z + range, ids, ids.Length); + for (int i = 0; i < nids; ++i) { + var ag = ids[i]; if (ag == skip) { continue; diff --git a/src/DotRecast.Detour.Crowd/DtProximityGrid.cs b/src/DotRecast.Detour.Crowd/DtProximityGrid.cs index d10bee0..36ffd0e 100644 --- a/src/DotRecast.Detour.Crowd/DtProximityGrid.cs +++ b/src/DotRecast.Detour.Crowd/DtProximityGrid.cs @@ -83,30 +83,51 @@ namespace DotRecast.Detour.Crowd } } - // 해당 셀 사이즈의 크기로 x ~ y 영역을 찾아, 군집 에이전트를 가져오는 코드 - public int QueryItems(float minx, float miny, float maxx, float maxy, ref HashSet result) + public int QueryItems(float minx, float miny, float maxx, float maxy, DtCrowdAgent[] ids, int maxIds) { int iminx = (int)MathF.Floor(minx * _invCellSize); int iminy = (int)MathF.Floor(miny * _invCellSize); int imaxx = (int)MathF.Floor(maxx * _invCellSize); int imaxy = (int)MathF.Floor(maxy * _invCellSize); + int n = 0; + for (int y = iminy; y <= imaxy; ++y) { for (int x = iminx; x <= imaxx; ++x) { long key = CombineKey(x, y); - if (_items.TryGetValue(key, out var ids)) + bool hasPool = _items.TryGetValue(key, out var pool); + if (!hasPool) { - for (int i = 0; i < ids.Count; ++i) + continue; + } + + for (int idx = 0; idx < pool.Count; ++idx) + { + var item = pool[idx]; + + // Check if the id exists already. + int end = n; + int i = 0; + while (i != end && ids[i] != item) { - result.Add(ids[i]); + ++i; + } + + // Item not found, add it. + if (i == n) + { + ids[n++] = item; + + if (n >= maxIds) + return n; } } } } - return result.Count; + return n; } public IEnumerable<(long, int)> GetItemCounts()