Add world portal editing/runtime handling and include tileSheets in WORLD_SPAWN
This commit is contained in:
+186
-1
@@ -19,12 +19,14 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
private const float MoveWalk = 120f;
|
||||
private const float MoveRun = 180f;
|
||||
private const float StepSec = 0.05f;
|
||||
private const long PortalCooldownTicks = 8; // 0.8s at 10hz
|
||||
|
||||
private readonly ILogger<WorldHandler> _log;
|
||||
private readonly ISessionRegistry _sessions;
|
||||
private readonly IMessageSerializer _ser;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly ConcurrentDictionary<Guid, SessionState> _players = new();
|
||||
private readonly ConcurrentDictionary<ulong, long> _portalCooldownByCharacter = new();
|
||||
private readonly JsonSerializerOptions _json = new() { PropertyNameCaseInsensitive = true };
|
||||
|
||||
private Task? _tickTask;
|
||||
@@ -161,11 +163,28 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
var initial = await ChunkUpdatesAsync(db, region, s, ct);
|
||||
var tileSheets = await db.TileMapSheets
|
||||
.AsNoTracking()
|
||||
.Where(ts => ts.IsEnabled)
|
||||
.OrderBy(ts => ts.Id)
|
||||
.Select(ts => new
|
||||
{
|
||||
id = ts.Id,
|
||||
name = ts.Name,
|
||||
path = ts.CdnPath,
|
||||
tileW = ts.TileWidth,
|
||||
tileH = ts.TileHeight,
|
||||
cols = ts.Columns,
|
||||
sheetW = ts.SheetWidth,
|
||||
sheetH = ts.SheetHeight
|
||||
})
|
||||
.ToArrayAsync(ct);
|
||||
await SendAsync(conn, OpCodes.WORLD_SPAWN, new
|
||||
{
|
||||
success = true,
|
||||
player = new { id = s.CharacterId, name = s.Name, x = s.X, y = s.Y, regionId = s.RegionId, zoneX = s.ZoneX, zoneY = s.ZoneY, chunkX = s.ChunkX, chunkY = s.ChunkY, isVip = s.IsVip },
|
||||
region = new { id = region.Id, name = region.Name, zonesX = region.ZonesPerRegionX, zonesY = region.ZonesPerRegionY, chunksPerZoneX = region.ChunksPerZoneX, chunksPerZoneY = region.ChunksPerZoneY, chunkWidthTiles = region.ChunkWidthTiles, chunkHeightTiles = region.ChunkHeightTiles },
|
||||
tileSheets,
|
||||
initialChunks = initial.Where(u => u.type == "chunk_full").Select(u => u.payload),
|
||||
nearbyPlayers = VisiblePlayers(s).Select(p => new { id = p.CharacterId, name = p.Name, x = p.X, y = p.Y, facing = p.Facing, speed = p.Speed, isMoving = p.IsMoving })
|
||||
}, ct);
|
||||
@@ -206,6 +225,7 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
s.ZoneX = zx; s.ZoneY = zy; s.ChunkX = cx; s.ChunkY = cy;
|
||||
s.VelX = dx * speed; s.VelY = dy * speed; s.Speed = speed; s.IsMoving = MathF.Abs(dx) > 0.001f || MathF.Abs(dy) > 0.001f;
|
||||
s.Facing = Facing(req.Facing, dx, dy, s.Facing); s.Seq = req.Seq;
|
||||
await TryTriggerPortalAsync(db, region, s, ct);
|
||||
}
|
||||
|
||||
private async Task TickLoopAsync(CancellationToken ct)
|
||||
@@ -245,8 +265,10 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
self = new { x = s.X, y = s.Y, velocityX = s.VelX, velocityY = s.VelY, regionId = s.RegionId, zoneX = s.ZoneX, zoneY = s.ZoneY, chunkX = s.ChunkX, chunkY = s.ChunkY },
|
||||
players = VisiblePlayers(s).Select(p => new { id = p.CharacterId, name = p.Name, x = p.X, y = p.Y, facing = p.Facing, speed = p.Speed, isMoving = p.IsMoving, regionId = p.RegionId, zoneX = p.ZoneX, zoneY = p.ZoneY, chunkX = p.ChunkX, chunkY = p.ChunkY }),
|
||||
chunkUpdates = updates.Select(u => u.payload),
|
||||
portalEvent = s.PendingPortalEvent,
|
||||
errors = errs
|
||||
}, ct);
|
||||
s.PendingPortalEvent = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,7 +313,7 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
return (0, new { regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = 0, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>(), Array.Empty<object>());
|
||||
|
||||
var tiles = await db.WorldChunkTiles.AsNoTracking().Where(t => t.WorldChunkId == chunk.Id).OrderBy(t => t.TileY).ThenBy(t => t.TileX).Select(t => new { id = t.Id, x = t.TileX, y = t.TileY, sheetId = t.SheetId, frameIndex = t.FrameIndex, isWalkable = t.IsWalkable, isInvisiblePaywall = t.IsInvisiblePaywall }).ToArrayAsync(ct);
|
||||
var objects = await db.WorldChunkObjects.AsNoTracking().Include(o => o.ObjectDefinition).Where(o => o.WorldChunkId == chunk.Id).OrderBy(o => o.TileY).ThenBy(o => o.TileX).Select(o => new { id = o.Id, x = o.TileX, y = o.TileY, objectDefinitionId = o.ObjectDefinitionId, objectName = o.ObjectDefinition != null ? o.ObjectDefinition.Name : $"Object {o.ObjectDefinitionId}", rotation = o.Rotation, metadataJson = o.MetadataJson }).ToArrayAsync(ct);
|
||||
var objects = await db.WorldChunkObjects.AsNoTracking().Include(o => o.ObjectDefinition).Where(o => o.WorldChunkId == chunk.Id).OrderBy(o => o.TileY).ThenBy(o => o.TileX).Select(o => new { id = o.Id, x = o.TileX, y = o.TileY, objectDefinitionId = o.ObjectDefinitionId, objectName = o.ObjectDefinition != null ? o.ObjectDefinition.Name : $"Object {o.ObjectDefinitionId}", interactionType = o.ObjectDefinition != null ? o.ObjectDefinition.InteractionType.ToString() : null, rotation = o.Rotation, metadataJson = o.MetadataJson }).ToArrayAsync(ct);
|
||||
var npcs = await db.WorldChunkNpcSpawns.AsNoTracking().Include(n => n.NpcDefinition).Where(n => n.WorldChunkId == chunk.Id).OrderBy(n => n.TileY).ThenBy(n => n.TileX).Select(n => new { id = n.Id, x = n.TileX, y = n.TileY, npcDefinitionId = n.NpcDefinitionId, npcName = n.NpcDefinition != null ? n.NpcDefinition.Name : $"NPC {n.NpcDefinitionId}", rotation = n.Rotation, metadataJson = n.MetadataJson }).ToArrayAsync(ct);
|
||||
var mobs = await db.WorldChunkMobSpawns.AsNoTracking().Include(m => m.MobDefinition).Where(m => m.WorldChunkId == chunk.Id).OrderBy(m => m.TileY).ThenBy(m => m.TileX).Select(m => new { id = m.Id, x = m.TileX, y = m.TileY, mobDefinitionId = m.MobDefinitionId, mobName = m.MobDefinition != null ? m.MobDefinition.Name : $"Mob {m.MobDefinitionId}", rotation = m.Rotation, spawnCount = m.SpawnCount, respawnSeconds = m.RespawnSeconds, metadataJson = m.MetadataJson }).ToArrayAsync(ct);
|
||||
return (chunk.Version, new { id = chunk.Id, regionId = region.Id, zoneX, zoneY, chunkX, chunkY, version = chunk.Version, width = region.ChunkWidthTiles, height = region.ChunkHeightTiles }, tiles.Cast<object>().ToArray(), objects.Cast<object>().ToArray(), npcs.Cast<object>().ToArray(), mobs.Cast<object>().ToArray());
|
||||
@@ -310,6 +332,152 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
return true;
|
||||
}
|
||||
|
||||
private async Task TryTriggerPortalAsync(GameDbContext db, WorldRegion region, SessionState s, CancellationToken ct)
|
||||
{
|
||||
var nowTick = Interlocked.Read(ref _tick);
|
||||
if (_portalCooldownByCharacter.TryGetValue(s.CharacterId, out var untilTick) && untilTick > nowTick)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var portalObject = await ResolvePortalObjectAtPositionAsync(db, region, s.X, s.Y, ct);
|
||||
if (portalObject == null || string.IsNullOrWhiteSpace(portalObject.MetadataJson))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryParsePortalMetadata(portalObject.MetadataJson, out var portalMeta))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_portalCooldownByCharacter[s.CharacterId] = nowTick + PortalCooldownTicks;
|
||||
|
||||
if (portalMeta.PortalType.Equals("world", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
if (TryResolveWorldPortalTarget(portalMeta, out var targetRegionId, out var targetZoneX, out var targetZoneY, out var targetChunkX, out var targetChunkY, out var targetTileX, out var targetTileY))
|
||||
{
|
||||
var targetRegion = await db.WorldRegions.AsNoTracking().FirstOrDefaultAsync(r => r.Id == targetRegionId && r.IsEnabled, ct);
|
||||
if (targetRegion != null)
|
||||
{
|
||||
var nx = WorldPxX(targetRegion, targetZoneX, targetChunkX, targetTileX);
|
||||
var ny = WorldPxY(targetRegion, targetZoneY, targetChunkY, targetTileY);
|
||||
s.RegionId = targetRegion.Id;
|
||||
s.X = nx;
|
||||
s.Y = ny;
|
||||
var (zx, zy, cx, cy) = ToChunkPos(targetRegion, s.X, s.Y);
|
||||
s.ZoneX = zx; s.ZoneY = zy; s.ChunkX = cx; s.ChunkY = cy;
|
||||
s.PendingPortalEvent = new
|
||||
{
|
||||
type = "world_teleport",
|
||||
sourceObjectId = portalObject.Id,
|
||||
regionId = s.RegionId,
|
||||
zoneX = s.ZoneX,
|
||||
zoneY = s.ZoneY,
|
||||
chunkX = s.ChunkX,
|
||||
chunkY = s.ChunkY,
|
||||
x = s.X,
|
||||
y = s.Y
|
||||
};
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
s.Errors.Add("portal_world_target_invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
s.PendingPortalEvent = new
|
||||
{
|
||||
type = "dungeon_request",
|
||||
sourceObjectId = portalObject.Id,
|
||||
portalType = portalMeta.PortalType,
|
||||
dungeonTemplateId = portalMeta.DungeonTemplateId,
|
||||
randomTier = portalMeta.RandomTier,
|
||||
levelBracketMin = portalMeta.LevelBracketMin,
|
||||
levelBracketMax = portalMeta.LevelBracketMax
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<WorldChunkObject?> ResolvePortalObjectAtPositionAsync(GameDbContext db, WorldRegion region, float wx, float wy, CancellationToken ct)
|
||||
{
|
||||
var gx = (int)MathF.Floor(wx / TileSize);
|
||||
var gy = (int)MathF.Floor(wy / TileSize);
|
||||
if (gx < 0 || gy < 0) return null;
|
||||
var zoneW = region.ChunksPerZoneX * region.ChunkWidthTiles;
|
||||
var zoneH = region.ChunksPerZoneY * region.ChunkHeightTiles;
|
||||
if (zoneW <= 0 || zoneH <= 0) return null;
|
||||
var zx = gx / zoneW;
|
||||
var zy = gy / zoneH;
|
||||
if (zx < 0 || zy < 0 || zx >= region.ZonesPerRegionX || zy >= region.ZonesPerRegionY) return null;
|
||||
var lzx = gx - (zx * zoneW);
|
||||
var lzy = gy - (zy * zoneH);
|
||||
var cx = lzx / region.ChunkWidthTiles;
|
||||
var cy = lzy / region.ChunkHeightTiles;
|
||||
var tx = lzx % region.ChunkWidthTiles;
|
||||
var ty = lzy % region.ChunkHeightTiles;
|
||||
|
||||
var zone = await db.WorldZones.AsNoTracking().FirstOrDefaultAsync(z => z.RegionId == region.Id && z.ZoneX == zx && z.ZoneY == zy && z.IsEnabled, ct);
|
||||
if (zone == null) return null;
|
||||
var chunk = await db.WorldChunks.AsNoTracking().FirstOrDefaultAsync(c => c.WorldZoneId == zone.Id && c.ChunkX == cx && c.ChunkY == cy, ct);
|
||||
if (chunk == null) return null;
|
||||
|
||||
var objects = await db.WorldChunkObjects
|
||||
.AsNoTracking()
|
||||
.Include(o => o.ObjectDefinition)
|
||||
.Where(o => o.WorldChunkId == chunk.Id)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return objects.FirstOrDefault(o =>
|
||||
{
|
||||
var w = Math.Max(1, o.ObjectDefinition?.Width ?? 1);
|
||||
var h = Math.Max(1, o.ObjectDefinition?.Height ?? 1);
|
||||
var isTeleport = o.ObjectDefinition?.InteractionType == ObjectInteractionType.Teleport ||
|
||||
(o.MetadataJson?.Contains("portalType", StringComparison.OrdinalIgnoreCase) ?? false);
|
||||
if (!isTeleport) return false;
|
||||
return tx >= o.TileX && ty >= o.TileY && tx < (o.TileX + w) && ty < (o.TileY + h);
|
||||
});
|
||||
}
|
||||
|
||||
private static bool TryParsePortalMetadata(string metadataJson, out WorldPortalMetadata metadata)
|
||||
{
|
||||
metadata = new WorldPortalMetadata();
|
||||
try
|
||||
{
|
||||
var parsed = JsonSerializer.Deserialize<WorldPortalMetadata>(metadataJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
||||
if (parsed == null || string.IsNullOrWhiteSpace(parsed.PortalType))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
metadata = parsed;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryResolveWorldPortalTarget(
|
||||
WorldPortalMetadata m,
|
||||
out int targetRegionId,
|
||||
out int targetZoneX,
|
||||
out int targetZoneY,
|
||||
out int targetChunkX,
|
||||
out int targetChunkY,
|
||||
out int targetTileX,
|
||||
out int targetTileY)
|
||||
{
|
||||
targetRegionId = m.TargetRegionId ?? 0;
|
||||
targetZoneX = m.TargetZoneX ?? 0;
|
||||
targetZoneY = m.TargetZoneY ?? 0;
|
||||
targetChunkX = m.TargetChunkX ?? 0;
|
||||
targetChunkY = m.TargetChunkY ?? 0;
|
||||
targetTileX = m.TargetTileX ?? 0;
|
||||
targetTileY = m.TargetTileY ?? 0;
|
||||
return targetRegionId > 0;
|
||||
}
|
||||
|
||||
private async Task<WorldChunkTile?> ResolveTileAsync(GameDbContext db, WorldRegion region, float wx, float wy, CancellationToken ct)
|
||||
{
|
||||
var gx = (int)MathF.Floor(wx / TileSize);
|
||||
@@ -491,6 +659,7 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
public uint Seq { get; set; }
|
||||
public Dictionary<string, long> ChunkVersions { get; } = new(StringComparer.Ordinal);
|
||||
public List<string> Errors { get; } = new();
|
||||
public object? PendingPortalEvent { get; set; }
|
||||
}
|
||||
|
||||
private sealed class SpawnReq
|
||||
@@ -519,4 +688,20 @@ public sealed class WorldHandler : IClientMessageHandler
|
||||
public bool IsRunning { get; set; }
|
||||
public string? Facing { get; set; }
|
||||
}
|
||||
|
||||
private sealed class WorldPortalMetadata
|
||||
{
|
||||
public string PortalType { get; set; } = "";
|
||||
public int? TargetRegionId { get; set; }
|
||||
public int? TargetZoneX { get; set; }
|
||||
public int? TargetZoneY { get; set; }
|
||||
public int? TargetChunkX { get; set; }
|
||||
public int? TargetChunkY { get; set; }
|
||||
public int? TargetTileX { get; set; }
|
||||
public int? TargetTileY { get; set; }
|
||||
public int? DungeonTemplateId { get; set; }
|
||||
public int? RandomTier { get; set; }
|
||||
public int? LevelBracketMin { get; set; }
|
||||
public int? LevelBracketMax { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user