From 6438e610ce786eb798ad2069b528825eb963d3f2 Mon Sep 17 00:00:00 2001 From: dingo Date: Tue, 16 Dec 2025 21:12:23 -0500 Subject: [PATCH] Altering Escape --- Game/Dungeon/DungeonHandler.cs | 87 +++++++++++++++++++++++-------- Game/Dungeon/DungeonSimulation.cs | 49 +++++++++++++++++ Game/Dungeon/PortalInstance.cs | 20 +++++++ 3 files changed, 134 insertions(+), 22 deletions(-) create mode 100644 Game/Dungeon/PortalInstance.cs diff --git a/Game/Dungeon/DungeonHandler.cs b/Game/Dungeon/DungeonHandler.cs index ecc2302..1c55e91 100644 --- a/Game/Dungeon/DungeonHandler.cs +++ b/Game/Dungeon/DungeonHandler.cs @@ -64,8 +64,7 @@ public sealed class DungeonHandler : IClientMessageHandler DungeonOpCodes.DUNGEON_INPUT_ATTACK, DungeonOpCodes.DUNGEON_INPUT_PICKUP, DungeonOpCodes.DUNGEON_INPUT_CHEST, - DungeonOpCodes.ESCAPE_START, - DungeonOpCodes.ESCAPE_CANCEL, + DungeonOpCodes.DUNGEON_INPUT_PORTAL, DungeonOpCodes.INVENTORY_REQUEST, DungeonOpCodes.EQUIP_ITEM, DungeonOpCodes.UNEQUIP_ITEM, @@ -170,12 +169,8 @@ public sealed class DungeonHandler : IClientMessageHandler HandleInputChest(session, payload); break; - case DungeonOpCodes.ESCAPE_START: - HandleEscapeStart(session); - break; - - case DungeonOpCodes.ESCAPE_CANCEL: - HandleEscapeCancel(session); + case DungeonOpCodes.DUNGEON_INPUT_PORTAL: + await HandlePortalUse(connection, session, payload, ct); break; case DungeonOpCodes.INVENTORY_REQUEST: @@ -419,6 +414,13 @@ public sealed class DungeonHandler : IClientMessageHandler y = c.Y, type = c.Type.ToString().ToLower(), opened = c.IsOpened + }), + portals = instance.Simulation.Portals.Values.Where(p => p.IsActive).Select(p => new + { + id = p.Id, + x = p.X, + y = p.Y, + type = p.Type.ToString().ToLower() }) }, ct); } @@ -522,33 +524,61 @@ public sealed class DungeonHandler : IClientMessageHandler } } - private void HandleEscapeStart(DungeonPlayerSession session) + /// + /// Handle portal use - instant extraction when player clicks/walks into portal + /// + private async Task HandlePortalUse(IConnection connection, DungeonPlayerSession session, ReadOnlyMemory payload, CancellationToken ct) { if (session.State != PlayerState.InDungeon || !session.CurrentDungeonId.HasValue) return; if (!_dungeons.TryGetValue(session.CurrentDungeonId.Value, out var dungeon)) return; - // Check if at exit - var layout = dungeon.Simulation.Layout; - if (layout.ExitPosition == null) return; + var json = System.Text.Encoding.UTF8.GetString(payload.Span); + var request = System.Text.Json.JsonSerializer.Deserialize(json); + if (request == null) return; + + // Find the portal + if (!dungeon.Simulation.Portals.TryGetValue(request.PortalId, out var portal)) + { + _log.LogWarning("Player {Name} tried to use non-existent portal {Id}", session.Character.Name, request.PortalId); + return; + } + + if (!portal.IsActive) + { + _log.LogWarning("Player {Name} tried to use inactive portal {Id}", session.Character.Name, request.PortalId); + return; + } var player = session.PlayerInstance; if (player == null) return; - float exitX = layout.ExitPosition.Value.X * 32 + 16; - float exitY = layout.ExitPosition.Value.Y * 32 + 16; - float dist = MathF.Sqrt((player.X - exitX) * (player.X - exitX) + (player.Y - exitY) * (player.Y - exitY)); + // Check distance to portal (must be within 64 pixels) + float dx = player.X - portal.X; + float dy = player.Y - portal.Y; + float dist = MathF.Sqrt(dx * dx + dy * dy); - if (dist < 48) // Within range + if (dist > 64) { - session.IsEscaping = true; - session.EscapeStartTick = _currentTick; + _log.LogDebug("Player {Name} too far from portal ({Dist})", session.Character.Name, dist); + return; } + + _log.LogInformation("Player {Name} using exit portal", session.Character.Name); + + // Send portal used confirmation + await SendAsync(connection, DungeonOpCodes.PORTAL_USED, new + { + portalId = portal.Id, + type = portal.Type.ToString().ToLower() + }, ct); + + // Extract player (keep loot, save progress) + await RemoveFromDungeonAsync(session, true, ct); + + // Return to hub + await SendAsync(connection, DungeonOpCodes.HUB_STATE, GetHubState(session.Character), ct); } - private void HandleEscapeCancel(DungeonPlayerSession session) - { - session.IsEscaping = false; - } /// /// Handle hotbar slot assignment - links a hotbar slot to an inventory slot /// @@ -1159,6 +1189,14 @@ public sealed class DungeonHandler : IClientMessageHandler y = i.Y }), + portals = sim.Portals.Values.Where(p => p.IsActive).Select(p => new + { + id = p.Id, + x = p.X, + y = p.Y, + type = p.Type.ToString().ToLower() + }), + events = tickResult == null ? null : new { damage = tickResult.DamageEvents.Select(e => new @@ -1489,3 +1527,8 @@ public class ChatSendRequest { public string Message { get; set; } = ""; } + +public class PortalUseRequest +{ + public int PortalId { get; set; } +} diff --git a/Game/Dungeon/DungeonSimulation.cs b/Game/Dungeon/DungeonSimulation.cs index 8a5e456..ab8b690 100644 --- a/Game/Dungeon/DungeonSimulation.cs +++ b/Game/Dungeon/DungeonSimulation.cs @@ -24,6 +24,7 @@ public class DungeonSimulation public ConcurrentDictionary Mobs { get; } = new(); public ConcurrentDictionary Chests { get; } = new(); public ConcurrentDictionary DroppedItems { get; } = new(); + public ConcurrentDictionary Portals { get; } = new(); // Combat tracking private readonly List _pendingDamageEvents = new(); @@ -40,6 +41,54 @@ public class DungeonSimulation SpawnMobs(); InitializeChests(); + SpawnExitPortal(); + } + + private void SpawnExitPortal() + { + // Spawn exit portal near the entrance room (but offset so players don't spawn on it) + if (Layout.Rooms.Count == 0) return; + + var entranceRoom = Layout.Rooms[0]; + + // Find a valid floor tile near the entrance that's not at the exact center + // Try positions around the room center + var offsets = new[] { (2, 0), (-2, 0), (0, 2), (0, -2), (2, 2), (-2, -2) }; + + foreach (var (dx, dy) in offsets) + { + int portalTileX = entranceRoom.CenterX + dx; + int portalTileY = entranceRoom.CenterY + dy; + + // Check if this is a valid floor tile + if (portalTileX >= 0 && portalTileX < Layout.Width && + portalTileY >= 0 && portalTileY < Layout.Height && + Layout.Tiles[portalTileX, portalTileY] == 1) // 1 = floor + { + var portal = new PortalInstance + { + Id = 1, + X = portalTileX * 32 + 16, // Center of tile in world coords + Y = portalTileY * 32 + 16, + Type = PortalType.Exit, + IsActive = true + }; + + Portals[portal.Id] = portal; + return; + } + } + + // Fallback: spawn at room center if no offset worked + var fallbackPortal = new PortalInstance + { + Id = 1, + X = entranceRoom.CenterX * 32 + 16, + Y = entranceRoom.CenterY * 32 + 16, + Type = PortalType.Exit, + IsActive = true + }; + Portals[fallbackPortal.Id] = fallbackPortal; } private void SpawnMobs() diff --git a/Game/Dungeon/PortalInstance.cs b/Game/Dungeon/PortalInstance.cs new file mode 100644 index 0000000..95327aa --- /dev/null +++ b/Game/Dungeon/PortalInstance.cs @@ -0,0 +1,20 @@ +namespace Server.Game.Dungeon; + +/// +/// Runtime instance of a portal in a dungeon +/// +public class PortalInstance +{ + public int Id { get; set; } + public float X { get; set; } + public float Y { get; set; } + public PortalType Type { get; set; } + public bool IsActive { get; set; } = true; +} + +public enum PortalType +{ + Exit, // Return to hub + NextFloor, // Go deeper (future) + Secret // Hidden area (future) +}