From b5738e0db303bbc1713091ff51bc1d9d1d27a64c Mon Sep 17 00:00:00 2001 From: dingo Date: Sun, 8 Feb 2026 22:08:58 -0500 Subject: [PATCH] boss handles --- DungeonBuilderVNextPlan.md | 1 + Game/Dungeon/DungeonHandler.Dungeons.cs | 81 ++++++++++++++++++++++--- Game/Dungeon/DungeonSimulation.cs | 61 +++++++++++++++++++ 3 files changed, 135 insertions(+), 8 deletions(-) diff --git a/DungeonBuilderVNextPlan.md b/DungeonBuilderVNextPlan.md index c78ef0f..2d101e2 100644 --- a/DungeonBuilderVNextPlan.md +++ b/DungeonBuilderVNextPlan.md @@ -233,6 +233,7 @@ public sealed class DungeonBossState { - [x] Runtime empty-dungeon cleanup/persistence now uses all-bosses-defeated condition instead of first-boss-killed condition. - [x] Added runtime flag `BossesRespawnOnReload` (default `false`) and wired respawn behavior to preserve defeated bosses when `false`. - [x] Admin-spawned bosses now increment runtime boss target count. +- [x] Restore path now reapplies defeated-boss progress from persisted mob state (including runtime/admin-spawned boss IDs). - [ ] Persist `BossesRespawnOnReload` as authored dungeon metadata. - [ ] Replace current generators with full `DungeonGeneratorVNext` (BSP + Random Walk + bucket-constrained floors/walls). - [ ] Add VNext offline validation harness and side-by-side `/admin/dungeonbuilder/testgen` integration. diff --git a/Game/Dungeon/DungeonHandler.Dungeons.cs b/Game/Dungeon/DungeonHandler.Dungeons.cs index 05ea8ea..ba96079 100644 --- a/Game/Dungeon/DungeonHandler.Dungeons.cs +++ b/Game/Dungeon/DungeonHandler.Dungeons.cs @@ -501,15 +501,25 @@ public sealed partial class DungeonHandler { var layout = DungeonGenerator.Generate(persisted.Seed, persisted.Tier); var simulation = new DungeonSimulation(layout, persisted.Tier, _mobDefinitions, persisted.LevelBracketMin, _abilityDefinitions, _mobSpriteConfigs, _abilityAnimationMappings); + List? persistedMobStates = null; - // NOTE: We intentionally do NOT restore mob state here. - // When a dungeon is restored (all players left), ALL mobs respawn fresh. - // This gives the dungeon a "reset" behavior when empty. - // The mob state is preserved in persistence for potential future use, - // but on restore we use the freshly generated mobs from DungeonSimulation. - _log.LogInformation("Dungeon {Id} restored with all mobs respawned (was persisted with {MobCount} mob states)", + // Rehydrate runtime/admin-spawned alive mobs that are not in static layout spawns. + if (!string.IsNullOrEmpty(persisted.MobStateJson)) + { + persistedMobStates = System.Text.Json.JsonSerializer.Deserialize>(persisted.MobStateJson); + if (persistedMobStates != null) + { + foreach (var mobState in persistedMobStates.Where(m => m.IsAlive)) + { + if (simulation.Mobs.ContainsKey(mobState.Id)) continue; + simulation.TrySpawnMobFromPersistedState(mobState); + } + } + } + + _log.LogInformation("Dungeon {Id} restored from persistence with {MobCount} saved mob states", persisted.Id, - string.IsNullOrEmpty(persisted.MobStateJson) ? 0 : "saved"); + persistedMobStates?.Count ?? 0); // Restore chest state if available (chests stay opened) if (!string.IsNullOrEmpty(persisted.ChestStateJson)) @@ -543,7 +553,62 @@ public sealed partial class DungeonHandler IsPersisted = false, LastPlayerActivity = DateTime.UtcNow }; - instance.InitializeBossState(simulation.Mobs.Values.Count(m => m.Type == MobType.Boss), persisted.BossKilled); + instance.InitializeBossState(simulation.Mobs.Values.Count(m => m.Type == MobType.Boss), false); + + // Restore defeated-boss progress from persisted mob state. + if (persistedMobStates != null && !instance.BossesRespawnOnReload) + { + foreach (var mobState in persistedMobStates.Where(m => !m.IsAlive)) + { + bool isBoss = false; + + if (simulation.Mobs.TryGetValue(mobState.Id, out var simMob)) + { + isBoss = simMob.Type == MobType.Boss; + } + else if (_mobDefinitions.TryGetValue(mobState.DefinitionId, out var mobDef)) + { + isBoss = mobDef.Type == MobType.Boss; + } + + if (!isBoss) continue; + + // If this boss existed in current simulation, keep it dead. + if (simulation.Mobs.TryGetValue(mobState.Id, out var killedBoss)) + { + killedBoss.IsAlive = false; + killedBoss.Health = 0; + killedBoss.State = MobState.Dead; + } + else + { + // Preserve boss progress for runtime/admin-spawned bosses that are not in static layout. + instance.RegisterBossSpawn(mobState.Id); + } + + instance.RegisterBossDefeat(mobState.Id); + } + } + + // Backward compatibility: if persisted record only marks fully cleared, mirror that state. + if (persisted.BossKilled && !instance.AllBossesDefeated) + { + var allBossIds = simulation.Mobs.Values + .Where(m => m.Type == MobType.Boss) + .Select(m => m.Id) + .ToList(); + instance.SetDefeatedBossMobIds(allBossIds); + + foreach (var bossId in allBossIds) + { + if (simulation.Mobs.TryGetValue(bossId, out var boss)) + { + boss.IsAlive = false; + boss.Health = 0; + boss.State = MobState.Dead; + } + } + } _log.LogInformation("Restored dungeon {Id} from persistence with fresh mobs", persisted.Id); return instance; diff --git a/Game/Dungeon/DungeonSimulation.cs b/Game/Dungeon/DungeonSimulation.cs index 7163875..ad8c7d9 100644 --- a/Game/Dungeon/DungeonSimulation.cs +++ b/Game/Dungeon/DungeonSimulation.cs @@ -1466,4 +1466,65 @@ public partial class DungeonSimulation Console.WriteLine($"[DungeonSimulation] Summoned {mob.Name} (ID {mob.Id}) at ({x}, {y})"); } + /// + /// Recreate a persisted mob state that is not part of static layout spawns (for example, admin/runtime-spawned mobs). + /// + public bool TrySpawnMobFromPersistedState(PersistedMobState mobState) + { + if (!_mobDefinitions.TryGetValue(mobState.DefinitionId, out var mobDef)) + return false; + + var stats = mobDef.GetStatsForBracket(BracketMin); + + string? spriteConfig = null; + if (_spriteConfigs != null && _spriteConfigs.TryGetValue(mobDef.Id, out var config) && !string.IsNullOrEmpty(config)) + { + spriteConfig = config; + } + else + { + spriteConfig = mobDef.SpriteConfigJson; + } + + var mob = new MobInstance + { + Id = mobState.Id, + DefinitionId = mobDef.Id, + Name = mobDef.Name, + X = mobState.X, + Y = mobState.Y, + SpawnX = mobState.X, + SpawnY = mobState.Y, + Health = mobState.IsAlive ? Math.Clamp(mobState.Health, 1, stats.MaxHealth) : 0, + MaxHealth = stats.MaxHealth, + Mana = mobDef.BaseMana, + MaxMana = mobDef.BaseMana, + IsAlive = mobState.IsAlive, + Type = mobDef.Type, + Behavior = mobDef.Behavior, + PreferredRange = mobDef.PreferredRange, + FleeThreshold = mobDef.FleeThreshold, + Stats = stats, + State = mobState.IsAlive ? MobState.Idle : MobState.Dead, + StateTime = 0, + SpriteConfigJson = spriteConfig, + TintColor = mobDef.TintColor, + Scale = mobDef.Scale, + CurrentAnimation = "idle_down", + FacingDirection = "down", + Phases = mobDef.Phases + }; + + InitializeMobAbilities(mob, mobDef, stats); + Mobs[mob.Id] = mob; + + lock (_mobIdLock) + { + if (mob.Id >= _nextMobId) + _nextMobId = mob.Id + 1; + } + + return true; + } + }