dungeon changes

This commit is contained in:
dingo
2026-02-08 22:21:03 -05:00
parent b5738e0db3
commit 469f1976bd
7 changed files with 621 additions and 97 deletions
+52 -53
View File
@@ -163,48 +163,7 @@ public sealed partial class DungeonHandler
var dungeonId = Guid.NewGuid();
int seed = Random.Shared.Next();
// Try V3 generator with DungeonBuilder templates first
DungeonLayout layout;
DungeonLayoutV2? layoutV2 = null;
try
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
var generator = new DungeonGeneratorV3(db, seed, tier);
layoutV2 = await generator.GenerateAsync();
// Check if V3 actually produced tiles
if (layoutV2.TileDetails.Count == 0)
{
_log.LogWarning("V3 generator produced no tiles, falling back to V2");
var generatorV2 = new DungeonGeneratorV2(db, seed, tier);
layoutV2 = await generatorV2.GenerateAsync();
}
layout = layoutV2;
_log.LogInformation("Generated dungeon - {RoomCount} rooms, {TileCount} visual tiles",
layoutV2.Rooms.Count, layoutV2.TileDetails?.Count ?? 0);
}
catch (Exception ex)
{
_log.LogWarning(ex, "Dungeon generator failed, falling back to V2");
try
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
var generatorV2 = new DungeonGeneratorV2(db, seed, tier);
layoutV2 = await generatorV2.GenerateAsync();
layout = layoutV2;
}
catch (Exception ex2)
{
_log.LogWarning(ex2, "V2 generator also failed, using V1");
layout = DungeonGenerator.Generate(seed, tier);
}
}
var (layout, layoutV2) = await GenerateDungeonLayoutWithFallbackAsync(seed, tier, ct);
var simulation = new DungeonSimulation(layout, tier, _mobDefinitions, bracketMin, _abilityDefinitions, _mobSpriteConfigs, _abilityAnimationMappings);
@@ -501,19 +460,16 @@ 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;
var mobStatePayload = DeserializePersistedMobPayload(persisted.MobStateJson);
var persistedMobStates = mobStatePayload?.Mobs;
// Rehydrate runtime/admin-spawned alive mobs that are not in static layout spawns.
if (!string.IsNullOrEmpty(persisted.MobStateJson))
if (persistedMobStates != null)
{
persistedMobStates = System.Text.Json.JsonSerializer.Deserialize<List<PersistedMobState>>(persisted.MobStateJson);
if (persistedMobStates != null)
foreach (var mobState in persistedMobStates.Where(m => m.IsAlive))
{
foreach (var mobState in persistedMobStates.Where(m => m.IsAlive))
{
if (simulation.Mobs.ContainsKey(mobState.Id)) continue;
simulation.TrySpawnMobFromPersistedState(mobState);
}
if (simulation.Mobs.ContainsKey(mobState.Id)) continue;
simulation.TrySpawnMobFromPersistedState(mobState);
}
}
@@ -553,6 +509,7 @@ public sealed partial class DungeonHandler
IsPersisted = false,
LastPlayerActivity = DateTime.UtcNow
};
instance.BossesRespawnOnReload = mobStatePayload?.BossesRespawnOnReload ?? false;
instance.InitializeBossState(simulation.Mobs.Values.Count(m => m.Type == MobType.Boss), false);
// Restore defeated-boss progress from persisted mob state.
@@ -725,6 +682,11 @@ public sealed partial class DungeonHandler
Health = m.Health,
IsAlive = m.IsAlive
}).ToList();
var mobStatePayload = new PersistedDungeonMobPayload
{
BossesRespawnOnReload = instance.BossesRespawnOnReload,
Mobs = mobStates
};
// Serialize chest state
var chestStates = instance.Simulation.Chests.Values.Select(c => new PersistedChestState
@@ -741,7 +703,7 @@ public sealed partial class DungeonHandler
existing.LastActivity = instance.LastPlayerActivity;
existing.PersistedAt = DateTime.UtcNow;
existing.BossKilled = instance.AllBossesDefeated;
existing.MobStateJson = System.Text.Json.JsonSerializer.Serialize(mobStates);
existing.MobStateJson = System.Text.Json.JsonSerializer.Serialize(mobStatePayload);
existing.ChestStateJson = System.Text.Json.JsonSerializer.Serialize(chestStates);
}
else
@@ -760,7 +722,7 @@ public sealed partial class DungeonHandler
LastActivity = instance.LastPlayerActivity,
PersistedAt = DateTime.UtcNow,
BossKilled = instance.AllBossesDefeated,
MobStateJson = System.Text.Json.JsonSerializer.Serialize(mobStates),
MobStateJson = System.Text.Json.JsonSerializer.Serialize(mobStatePayload),
ChestStateJson = System.Text.Json.JsonSerializer.Serialize(chestStates)
};
db.Set<DungeonPersistence>().Add(persisted);
@@ -776,5 +738,42 @@ public sealed partial class DungeonHandler
}
}
private PersistedDungeonMobPayload? DeserializePersistedMobPayload(string? mobStateJson)
{
if (string.IsNullOrWhiteSpace(mobStateJson))
return null;
try
{
var payload = System.Text.Json.JsonSerializer.Deserialize<PersistedDungeonMobPayload>(mobStateJson);
if (payload != null)
{
payload.Mobs ??= new List<PersistedMobState>();
return payload;
}
}
catch
{
}
try
{
var legacyMobs = System.Text.Json.JsonSerializer.Deserialize<List<PersistedMobState>>(mobStateJson);
if (legacyMobs != null)
{
return new PersistedDungeonMobPayload
{
BossesRespawnOnReload = false,
Mobs = legacyMobs
};
}
}
catch
{
}
return null;
}
#endregion
}