boss handles

This commit is contained in:
dingo
2026-02-08 22:08:58 -05:00
parent 7b03b4139a
commit b5738e0db3
3 changed files with 135 additions and 8 deletions
+73 -8
View File
@@ -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<PersistedMobState>? 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<List<PersistedMobState>>(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;