feat: Adds proper OSI tracking up to 120 tiles. (#2281)

### Summary

Fixes tracking skill to 10 tiles per 10% up to 120 tiles.
This commit is contained in:
Kamron Batman 2025-11-29 09:55:52 -08:00 • committed by GitHub
parent ad26ab6260
commit 5fb0e806c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 417 additions and 66 deletions

View file

@ -51,7 +51,7 @@ public static class Tracking
public static void AddInfo(Mobile tracker, Mobile target)
{
var info = new TrackingInfo(tracker, target);
var info = new TrackingInfo(target);
_table[tracker] = info;
}
@ -80,11 +80,9 @@ public static class Tracking
public Point2D _location;
public readonly Map _map;
public readonly Mobile _target;
public Mobile _tracker;
public TrackingInfo(Mobile tracker, Mobile target)
public TrackingInfo(Mobile target)
{
_tracker = tracker;
_target = target;
_location = new Point2D(target);
_map = target.Map;
@ -164,7 +162,8 @@ public class TrackWhoGump : DynamicGump
from.CheckSkill(SkillName.Tracking, 21.1, 100.0); // Passive gain
var range = 10 + (int)(from.Skills.Tracking.Value / 10);
// 10 tiles base + 10 tiles per 10 skill
var range = 10 + (int)from.Skills.Tracking.Value / 10 * 10;
var mobs = GetClosestMobs(from, range, type);
@ -231,13 +230,19 @@ public class TrackWhoGump : DynamicGump
var loc = from.Location;
// We only track the closest 12
// Using a simple array with count is sufficient since we're single-threaded
var mobs = new Mobile[MaxClosest];
Span<double> distances = stackalloc double[MaxClosest];
distances.Fill(double.MaxValue); // Fill with max values
var total = 0;
var maxDistance = double.MaxValue;
var count = 0;
foreach (var m in from.GetMobilesInRange(range))
foreach (var (m, minDistance) in from.GetMobilesInRangeByDistance(range))
{
if (count == MaxClosest && minDistance > maxDistance)
{
break;
}
if (m == from || Core.AOS && !m.Alive ||
m.Hidden && m.AccessLevel != AccessLevel.Player && from.AccessLevel <= m.AccessLevel ||
!IsValidMobileType(m, type) || !CheckDifficulty(from, m))
@ -245,30 +250,51 @@ public class TrackWhoGump : DynamicGump
continue;
}
total++;
var distance = m.GetDistanceToSqrt(loc);
for (var i = 0; i < MaxClosest; i++)
if (count >= MaxClosest && distance >= maxDistance)
{
continue;
}
var searchLimit = Math.Min(count, MaxClosest - 1);
var insertIndex = searchLimit;
for (var i = 0; i < searchLimit; i++)
{
if (distance < distances[i])
{
// Shift down the rest
for (int j = MaxClosest - 1; j > i; j--)
{
mobs[j] = mobs[j - 1];
distances[j] = distances[j - 1];
}
mobs[i] = m;
distances[i] = distance;
insertIndex = i;
break;
}
}
// Shift elements to make room for insertion
if (insertIndex < searchLimit)
{
var shiftCount = searchLimit - insertIndex;
Array.Copy(mobs, insertIndex, mobs, insertIndex + 1, shiftCount);
distances.Slice(insertIndex, shiftCount).CopyTo(distances[(insertIndex + 1)..]);
}
mobs[insertIndex] = m;
distances[insertIndex] = distance;
if (count < MaxClosest)
{
count++;
}
if (count == MaxClosest)
{
maxDistance = distances[MaxClosest - 1];
}
}
if (total < MaxClosest)
// Only resize if we found fewer than MaxClosest
if (count < MaxClosest)
{
Array.Resize(ref mobs, total);
Array.Resize(ref mobs, count);
}
return mobs;