Spatial mapping
This commit is contained in:
+111
-10
@@ -44,6 +44,9 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
private Dictionary<int, MobAbilityAnimationMapping> _abilityAnimationMappings = new();
|
||||
private Dictionary<int, string?> _mobSpriteConfigs = new(); // MobDefinitionId -> SpriteConfig JSON
|
||||
private bool _initialized = false;
|
||||
private volatile bool _spatialStateFilterEnabled = true;
|
||||
private volatile float _spatialStateRadiusPx = 32f * 28f; // 28 tiles
|
||||
private volatile int _spatialStateIntervalTicks = 1;
|
||||
|
||||
|
||||
// Tick management
|
||||
@@ -74,6 +77,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
_craftingHandler = craftingHandler;
|
||||
_broadcastService = broadcastService;
|
||||
_redis = redis;
|
||||
LoadSpatialStateTuningFromEnvironment();
|
||||
|
||||
// Generate unique pod ID (use hostname in K8s, or random for local dev)
|
||||
_podId = Environment.GetEnvironmentVariable("HOSTNAME")
|
||||
@@ -81,6 +85,25 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
?? $"local-{Guid.NewGuid().ToString()[..8]}";
|
||||
}
|
||||
|
||||
private void LoadSpatialStateTuningFromEnvironment()
|
||||
{
|
||||
var enabledRaw = Environment.GetEnvironmentVariable("DUNGEON_SPATIAL_FILTER_ENABLED");
|
||||
if (!string.IsNullOrWhiteSpace(enabledRaw))
|
||||
{
|
||||
_spatialStateFilterEnabled = enabledRaw == "1" || enabledRaw.Equals("true", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
if (float.TryParse(Environment.GetEnvironmentVariable("DUNGEON_UPDATE_RADIUS_PX"), out var radiusPx))
|
||||
{
|
||||
_spatialStateRadiusPx = Math.Clamp(radiusPx, 128f, 8192f);
|
||||
}
|
||||
|
||||
if (int.TryParse(Environment.GetEnvironmentVariable("DUNGEON_SPATIAL_INTERVAL_TICKS"), out var interval))
|
||||
{
|
||||
_spatialStateIntervalTicks = Math.Clamp(interval, 1, 20);
|
||||
}
|
||||
}
|
||||
|
||||
public IReadOnlyCollection<int> SupportedOpCodes => new[]
|
||||
{
|
||||
DungeonOpCodes.HUB_STATE,
|
||||
@@ -2741,6 +2764,49 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
if (dungeon == null) return;
|
||||
|
||||
var sim = dungeon.Simulation;
|
||||
var selfPlayer = sim.Players.GetValueOrDefault(session.CharacterId);
|
||||
bool spatialFiltering = _spatialStateFilterEnabled && selfPlayer != null;
|
||||
bool sendSpatialThisTick = !spatialFiltering ||
|
||||
_spatialStateIntervalTicks <= 1 ||
|
||||
(_currentTick % _spatialStateIntervalTicks == 0);
|
||||
float radiusPx = _spatialStateRadiusPx;
|
||||
float radiusSq = radiusPx * radiusPx;
|
||||
|
||||
bool IsNearby(float x, float y)
|
||||
{
|
||||
if (!spatialFiltering || selfPlayer == null) return true;
|
||||
float dx = x - selfPlayer.X;
|
||||
float dy = y - selfPlayer.Y;
|
||||
return (dx * dx) + (dy * dy) <= radiusSq;
|
||||
}
|
||||
|
||||
var visiblePlayers = sim.Players.Values
|
||||
.Where(p => !spatialFiltering || p.CharacterId == session.CharacterId || IsNearby(p.X, p.Y))
|
||||
.ToList();
|
||||
var visiblePlayerIds = visiblePlayers.Select(p => p.CharacterId).ToHashSet();
|
||||
|
||||
var nearbyOrRelevantMobs = sim.Mobs.Values
|
||||
.Where(m => !spatialFiltering || IsNearby(m.X, m.Y) || m.TargetId == session.CharacterId)
|
||||
.ToList();
|
||||
var visibleMobs = sendSpatialThisTick
|
||||
? nearbyOrRelevantMobs
|
||||
: nearbyOrRelevantMobs.Where(m => m.TargetId == session.CharacterId).ToList();
|
||||
var visibleMobIds = visibleMobs.Select(m => m.Id).ToHashSet();
|
||||
|
||||
bool IsSelfPlayerEvent(EntityType type, int id)
|
||||
{
|
||||
return type == EntityType.Player && (ulong)Math.Max(0, id) == session.CharacterId;
|
||||
}
|
||||
|
||||
bool IsVisiblePlayerEvent(EntityType type, int id)
|
||||
{
|
||||
return type == EntityType.Player && visiblePlayerIds.Contains((ulong)Math.Max(0, id));
|
||||
}
|
||||
|
||||
bool IsVisibleMobEvent(EntityType type, int id)
|
||||
{
|
||||
return type == EntityType.Mob && visibleMobIds.Contains(id);
|
||||
}
|
||||
|
||||
var state = new
|
||||
{
|
||||
@@ -2749,7 +2815,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
|
||||
// NOTE: tiles are NOT sent every tick - they're static and sent once on DUNGEON_ENTER_RESULT
|
||||
|
||||
players = sim.Players.Values.Select(p => new
|
||||
players = visiblePlayers.Select(p => new
|
||||
{
|
||||
id = p.CharacterId,
|
||||
name = p.CharacterName,
|
||||
@@ -2766,7 +2832,7 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
isInCombat = p.IsInCombat
|
||||
}),
|
||||
|
||||
mobs = sim.Mobs.Values.Select(m => new
|
||||
mobs = visibleMobs.Select(m => new
|
||||
{
|
||||
id = m.Id,
|
||||
name = m.Name,
|
||||
@@ -2782,7 +2848,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
scale = m.Scale
|
||||
}),
|
||||
|
||||
chests = sim.Chests.Values.Select(c => new
|
||||
chests = (sendSpatialThisTick ? sim.Chests.Values.Where(c => !spatialFiltering || IsNearby(c.X, c.Y)) : Enumerable.Empty<ChestInstance>())
|
||||
.Select(c => new
|
||||
{
|
||||
id = c.Id,
|
||||
x = c.X,
|
||||
@@ -2790,7 +2857,8 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
opened = c.IsOpened
|
||||
}),
|
||||
|
||||
droppedItems = sim.DroppedItems.Values.Select(i => new
|
||||
droppedItems = (sendSpatialThisTick ? sim.DroppedItems.Values.Where(i => !spatialFiltering || IsNearby(i.X, i.Y)) : Enumerable.Empty<DroppedItem>())
|
||||
.Select(i => new
|
||||
{
|
||||
id = i.Id,
|
||||
itemId = i.ItemDefinitionId,
|
||||
@@ -2799,7 +2867,10 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
y = i.Y
|
||||
}),
|
||||
|
||||
portals = sim.Portals.Values.Where(p => p.IsActive).Select(p => new
|
||||
portals = (sendSpatialThisTick
|
||||
? sim.Portals.Values.Where(p => p.IsActive && (!spatialFiltering || IsNearby(p.X, p.Y)))
|
||||
: sim.Portals.Values.Where(p => p.IsActive && p.Type == PortalType.Exit))
|
||||
.Select(p => new
|
||||
{
|
||||
id = p.Id,
|
||||
x = p.X,
|
||||
@@ -2822,7 +2893,15 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
|
||||
events = tickResult == null ? null : new
|
||||
{
|
||||
damage = tickResult.DamageEvents.Select(e => new
|
||||
damage = tickResult.DamageEvents
|
||||
.Where(e =>
|
||||
IsSelfPlayerEvent(e.SourceType, e.SourceId) ||
|
||||
IsSelfPlayerEvent(e.TargetType, e.TargetId) ||
|
||||
IsVisiblePlayerEvent(e.SourceType, e.SourceId) ||
|
||||
IsVisiblePlayerEvent(e.TargetType, e.TargetId) ||
|
||||
IsVisibleMobEvent(e.SourceType, e.SourceId) ||
|
||||
IsVisibleMobEvent(e.TargetType, e.TargetId))
|
||||
.Select(e => new
|
||||
{
|
||||
sourceType = e.SourceType.ToString().ToLower(),
|
||||
sourceId = e.SourceId,
|
||||
@@ -2833,7 +2912,15 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
wasResisted = e.WasResisted,
|
||||
wasWeakness = e.WasWeakness
|
||||
}),
|
||||
deaths = tickResult.DeathEvents.Select(e => new
|
||||
deaths = tickResult.DeathEvents
|
||||
.Where(e =>
|
||||
IsSelfPlayerEvent(e.EntityType, e.EntityId) ||
|
||||
IsSelfPlayerEvent(e.KillerType, e.KillerId) ||
|
||||
IsVisiblePlayerEvent(e.EntityType, e.EntityId) ||
|
||||
IsVisiblePlayerEvent(e.KillerType, e.KillerId) ||
|
||||
IsVisibleMobEvent(e.EntityType, e.EntityId) ||
|
||||
IsVisibleMobEvent(e.KillerType, e.KillerId))
|
||||
.Select(e => new
|
||||
{
|
||||
entityType = e.EntityType.ToString().ToLower(),
|
||||
entityId = e.EntityId,
|
||||
@@ -2843,7 +2930,13 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
killerName = e.KillerName,
|
||||
xp = e.XpReward
|
||||
}),
|
||||
projectileHits = tickResult.ProjectileHitEvents.Select(e => new
|
||||
projectileHits = tickResult.ProjectileHitEvents
|
||||
.Where(e =>
|
||||
IsSelfPlayerEvent(e.TargetType, e.TargetId) ||
|
||||
IsVisiblePlayerEvent(e.TargetType, e.TargetId) ||
|
||||
IsVisibleMobEvent(e.TargetType, e.TargetId) ||
|
||||
(!spatialFiltering || IsNearby(e.X, e.Y)))
|
||||
.Select(e => new
|
||||
{
|
||||
projectileId = e.ProjectileId,
|
||||
targetType = e.TargetType.ToString().ToLower(),
|
||||
@@ -2851,14 +2944,22 @@ public sealed partial class DungeonHandler : IClientMessageHandler
|
||||
x = e.X,
|
||||
y = e.Y
|
||||
}),
|
||||
projectileExpired = tickResult.ProjectileExpiredEvents.Select(e => new
|
||||
projectileExpired = tickResult.ProjectileExpiredEvents
|
||||
.Where(e => !spatialFiltering || IsNearby(e.X, e.Y))
|
||||
.Select(e => new
|
||||
{
|
||||
projectileId = e.ProjectileId,
|
||||
x = e.X,
|
||||
y = e.Y,
|
||||
reason = e.Reason
|
||||
}),
|
||||
abilities = tickResult.AbilityEvents.Select(e => new
|
||||
abilities = tickResult.AbilityEvents
|
||||
.Where(e =>
|
||||
(e.SourceType == EntityType.Player && (ulong)Math.Max(0, e.SourceId) == session.CharacterId) ||
|
||||
(e.TargetId.HasValue && (ulong)Math.Max(0, e.TargetId.Value) == session.CharacterId) ||
|
||||
(e.SourceType == EntityType.Mob && visibleMobIds.Contains(e.SourceId)) ||
|
||||
(!spatialFiltering || IsNearby(e.TargetX, e.TargetY)))
|
||||
.Select(e => new
|
||||
{
|
||||
sourceType = e.SourceType.ToString().ToLower(),
|
||||
sourceId = e.SourceId,
|
||||
|
||||
@@ -494,6 +494,29 @@ public partial class DungeonHandler
|
||||
{
|
||||
return _dungeons.Values.Where(d => d.HasPendingTileUpdates);
|
||||
}
|
||||
|
||||
public (bool enabled, float radiusPx, int intervalTicks) GetSpatialUpdateTuning()
|
||||
{
|
||||
return (_spatialStateFilterEnabled, _spatialStateRadiusPx, _spatialStateIntervalTicks);
|
||||
}
|
||||
|
||||
public void SetSpatialUpdateRangePx(float radiusPx)
|
||||
{
|
||||
_spatialStateRadiusPx = Math.Clamp(radiusPx, 128f, 8192f);
|
||||
_log.LogInformation("Updated spatial state radius to {RadiusPx}px", _spatialStateRadiusPx);
|
||||
}
|
||||
|
||||
public void SetSpatialUpdateIntervalTicks(int intervalTicks)
|
||||
{
|
||||
_spatialStateIntervalTicks = Math.Clamp(intervalTicks, 1, 20);
|
||||
_log.LogInformation("Updated spatial state interval to {IntervalTicks} ticks", _spatialStateIntervalTicks);
|
||||
}
|
||||
|
||||
public void SetSpatialFilterEnabled(bool enabled)
|
||||
{
|
||||
_spatialStateFilterEnabled = enabled;
|
||||
_log.LogInformation("Updated spatial state filtering enabled={Enabled}", _spatialStateFilterEnabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build tile update payload for broadcasting to clients
|
||||
|
||||
Reference in New Issue
Block a user