AI Changes

This commit is contained in:
dingo
2026-01-08 13:27:46 -05:00
parent d37535f05a
commit ae304c62d3
3 changed files with 279 additions and 275 deletions
+272 -273
View File
@@ -536,7 +536,257 @@ public class DungeonGeneratorV3
return room;
}
private async Task PlaceObjectsAsync(DungeonLayoutV2 layout, List<DungeonRoomV2> rooms)
{
Console.WriteLine($"[DungeonGenV3] Placing objects (decor:{_decorationDensity}%, trap:{_trapDensity}%, container:{_containerDensity}%)");
foreach (var room in rooms)
{
// Skip entrance/exit for traps
bool canHaveTraps = room.Type != RoomType.Entrance;
// Calculate target counts based on room size and density
int floorTiles = room.Width * room.Height;
int decorTarget = (floorTiles * _decorationDensity) / 1000;
int trapTarget = canHaveTraps ? (floorTiles * _trapDensity) / 1000 : 0;
int containerTarget = (floorTiles * _containerDensity) / 1000;
// Place decorations
for (int i = 0; i < decorTarget && _decorationTemplates.Count > 0; i++)
{
var pos = FindValidObjectPosition(layout, room);
if (pos.HasValue)
{
var obj = SelectWeightedTemplate(_decorationTemplates);
PlaceObject(layout, pos.Value.x, pos.Value.y, obj);
}
}
// Place containers (chests, etc.)
for (int i = 0; i < containerTarget && _containerTemplates.Count > 0; i++)
{
var pos = FindValidObjectPosition(layout, room);
if (pos.HasValue)
{
var obj = SelectWeightedTemplate(_containerTemplates);
PlaceObject(layout, pos.Value.x, pos.Value.y, obj);
}
}
// Place traps
for (int i = 0; i < trapTarget && _trapTemplates.Count > 0; i++)
{
var pos = FindValidObjectPosition(layout, room);
if (pos.HasValue)
{
var obj = SelectWeightedTemplate(_trapTemplates);
PlaceObject(layout, pos.Value.x, pos.Value.y, obj);
}
}
}
}
private (int x, int y)? FindValidObjectPosition(DungeonLayoutV2 layout, DungeonRoomV2 room)
{
for (int attempt = 0; attempt < 20; attempt++)
{
int x = room.X + _rng.Next(1, room.Width - 1);
int y = room.Y + _rng.Next(1, room.Height - 1);
if (x >= 0 && x < MapWidth && y >= 0 && y < MapHeight &&
layout.Walkable[x, y] &&
!layout.Objects.Any(o => o.X == x && o.Y == y))
{
return (x, y);
}
}
return null;
}
private void PlaceObject(DungeonLayoutV2 layout, int x, int y, ObjectTemplate? obj)
{
if (obj == null) return;
// Determine if it's a trap based on type
bool isTrap = obj.Type == ObjectTemplateType.Trap || obj.Type == ObjectTemplateType.Hazard;
layout.Objects.Add(new PlacedObject
{
X = x,
Y = y,
ObjectTemplateId = obj.Id,
Name = obj.Name,
Type = obj.Type.ToString(),
SpriteSheetId = obj.SpriteSheetId ?? 0,
FrameIndex = obj.FrameIndex,
BlocksMovement = obj.BlocksMovement,
IsInteractable = obj.IsInteractable,
IsTrap = isTrap,
TrapDamage = obj.Damage,
LootTableId = obj.LootTableId
});
if (obj.BlocksMovement)
{
layout.Walkable[x, y] = false;
}
if (!UsedObjectTemplates.Contains(obj.Name))
UsedObjectTemplates.Add(obj.Name);
}
private void SetSpawnPoints(DungeonLayoutV2 layout, List<DungeonRoomV2> rooms)
{
// Find entrance room for player spawn
var entrance = rooms.FirstOrDefault(r => r.Type == RoomType.Entrance);
if (entrance != null)
{
if (entrance.PlayerSpawnX.HasValue && entrance.PlayerSpawnY.HasValue)
{
layout.PlayerSpawnX = entrance.PlayerSpawnX.Value;
layout.PlayerSpawnY = entrance.PlayerSpawnY.Value;
}
else
{
layout.PlayerSpawnX = entrance.X + entrance.Width / 2;
layout.PlayerSpawnY = entrance.Y + entrance.Height / 2;
}
// Set legacy SpawnPoint too
layout.SpawnPoint = new DungeonPoint { X = layout.PlayerSpawnX, Y = layout.PlayerSpawnY };
}
// Find exit room for portal
var exit = rooms.FirstOrDefault(r => r.Type == RoomType.Exit);
if (exit != null)
{
if (exit.ExitX.HasValue && exit.ExitY.HasValue)
{
layout.ExitX = exit.ExitX.Value;
layout.ExitY = exit.ExitY.Value;
}
else
{
layout.ExitX = exit.X + exit.Width / 2;
layout.ExitY = exit.Y + exit.Height / 2;
}
}
int mobId = 0;
int roomIndex = 0;
// Collect mob spawns from all rooms
foreach (var room in rooms)
{
// Add spawns from template - convert RoomMobSpawn to DungeonMobSpawn
foreach (var spawn in room.MobSpawns)
{
var mobType = spawn.Type?.ToLower() switch
{
"boss" => DungeonMobSpawnType.Boss,
"elite" => DungeonMobSpawnType.Elite,
_ => DungeonMobSpawnType.Normal
};
for (int i = 0; i < spawn.Count; i++)
{
layout.MobSpawns.Add(new DungeonMobSpawn
{
Id = mobId++,
X = spawn.X,
Y = spawn.Y,
MobType = mobType,
RoomIndex = roomIndex,
IsAlive = true
});
}
}
// Add procedural spawns for rooms without template spawns
if (room.MobSpawns.Count == 0 && room.Type != RoomType.Entrance)
{
int mobCount = room.Type switch
{
RoomType.Boss => 1,
RoomType.Treasure => _rng.Next(1, 3),
_ => _rng.Next(2, 5)
};
var mobType = room.Type == RoomType.Boss ? DungeonMobSpawnType.Boss : DungeonMobSpawnType.Normal;
for (int i = 0; i < mobCount; i++)
{
int mx = room.X + _rng.Next(1, room.Width - 1);
int my = room.Y + _rng.Next(1, room.Height - 1);
layout.MobSpawns.Add(new DungeonMobSpawn
{
Id = mobId++,
X = mx,
Y = my,
MobType = mobType,
RoomIndex = roomIndex,
IsAlive = true
});
}
}
roomIndex++;
}
layout.TotalMobs = layout.MobSpawns.Count;
Console.WriteLine($"[DungeonGenV3] Spawn points: Player=({layout.PlayerSpawnX},{layout.PlayerSpawnY}), Exit=({layout.ExitX},{layout.ExitY}), Mobs={layout.MobSpawns.Count}");
}
// Helper methods
private T? SelectWeightedTemplate<T>(List<T> templates) where T : class
{
if (templates.Count == 0) return null;
if (templates.Count == 1) return templates[0];
// Get weight property via reflection or assume equal weights
int totalWeight = 0;
var weights = new List<int>();
foreach (var t in templates)
{
int weight = 100;
var weightProp = t.GetType().GetProperty("Weight");
if (weightProp != null)
{
weight = (int)(weightProp.GetValue(t) ?? 100);
}
weights.Add(weight);
totalWeight += weight;
}
int roll = _rng.Next(totalWeight);
int cumulative = 0;
for (int i = 0; i < templates.Count; i++)
{
cumulative += weights[i];
if (roll < cumulative)
return templates[i];
}
return templates[^1];
}
private List<int> ParseIds(string? json)
{
if (string.IsNullOrEmpty(json)) return new List<int>();
try
{
return JsonSerializer.Deserialize<List<int>>(json) ?? new List<int>();
}
catch
{
return new List<int>();
}
}
private async Task StampRoomTemplateAsync(DungeonLayoutV2 layout, DungeonRoomV2 room, BuilderRoomTemplate template)
{
Console.WriteLine($"[DungeonGenV3] Stamping room template '{template.Name}' at ({room.X},{room.Y})");
@@ -1024,288 +1274,21 @@ public class DungeonGeneratorV3
} }
}
}
}
// Fallback: use the FIRST non-walkable tile consistently
var wallTiles = _tileDefinitions.Values.Where(t => !t.IsWalkable).OrderBy(t => t.Id).ToList();
if (wallTiles.Count > 0)
{
return wallTiles[0];
}
return null;
}
}
private async Task PlaceObjectsAsync(DungeonLayoutV2 layout, List<DungeonRoomV2> rooms)
{
Console.WriteLine($"[DungeonGenV3] Placing objects (decor:{_decorationDensity}%, trap:{_trapDensity}%, container:{_containerDensity}%)");
foreach (var room in rooms)
{
// Skip entrance/exit for traps
bool canHaveTraps = room.Type != RoomType.Entrance;
// Calculate target counts based on room size and density
int floorTiles = room.Width * room.Height;
int decorTarget = (floorTiles * _decorationDensity) / 1000;
int trapTarget = canHaveTraps ? (floorTiles * _trapDensity) / 1000 : 0;
int containerTarget = (floorTiles * _containerDensity) / 1000;
// Place decorations
for (int i = 0; i < decorTarget && _decorationTemplates.Count > 0; i++)
{
var pos = FindValidObjectPosition(layout, room);
if (pos.HasValue)
{
var obj = SelectWeightedTemplate(_decorationTemplates);
PlaceObject(layout, pos.Value.x, pos.Value.y, obj);
}
}
// Place containers (chests, etc.)
for (int i = 0; i < containerTarget && _containerTemplates.Count > 0; i++)
{
var pos = FindValidObjectPosition(layout, room);
if (pos.HasValue)
{
var obj = SelectWeightedTemplate(_containerTemplates);
PlaceObject(layout, pos.Value.x, pos.Value.y, obj);
}
}
// Place traps
for (int i = 0; i < trapTarget && _trapTemplates.Count > 0; i++)
{
var pos = FindValidObjectPosition(layout, room);
if (pos.HasValue)
{
var obj = SelectWeightedTemplate(_trapTemplates);
PlaceObject(layout, pos.Value.x, pos.Value.y, obj);
}
}
}
}
private (int x, int y)? FindValidObjectPosition(DungeonLayoutV2 layout, DungeonRoomV2 room)
{
for (int attempt = 0; attempt < 20; attempt++)
{
int x = room.X + _rng.Next(1, room.Width - 1);
int y = room.Y + _rng.Next(1, room.Height - 1);
if (x >= 0 && x < MapWidth && y >= 0 && y < MapHeight &&
layout.Walkable[x, y] &&
!layout.Objects.Any(o => o.X == x && o.Y == y))
{
return (x, y);
}
}
return null;
}
private void PlaceObject(DungeonLayoutV2 layout, int x, int y, ObjectTemplate? obj)
{
if (obj == null) return;
// Determine if it's a trap based on type
bool isTrap = obj.Type == ObjectTemplateType.Trap || obj.Type == ObjectTemplateType.Hazard;
layout.Objects.Add(new PlacedObject
{
X = x,
Y = y,
ObjectTemplateId = obj.Id,
Name = obj.Name,
Type = obj.Type.ToString(),
SpriteSheetId = obj.SpriteSheetId ?? 0,
FrameIndex = obj.FrameIndex,
BlocksMovement = obj.BlocksMovement,
IsInteractable = obj.IsInteractable,
IsTrap = isTrap,
TrapDamage = obj.Damage,
LootTableId = obj.LootTableId
});
if (obj.BlocksMovement)
{
layout.Walkable[x, y] = false;
}
if (!UsedObjectTemplates.Contains(obj.Name))
UsedObjectTemplates.Add(obj.Name);
}
private void SetSpawnPoints(DungeonLayoutV2 layout, List<DungeonRoomV2> rooms)
{
// Find entrance room for player spawn
var entrance = rooms.FirstOrDefault(r => r.Type == RoomType.Entrance);
if (entrance != null)
{
if (entrance.PlayerSpawnX.HasValue && entrance.PlayerSpawnY.HasValue)
{
layout.PlayerSpawnX = entrance.PlayerSpawnX.Value;
layout.PlayerSpawnY = entrance.PlayerSpawnY.Value;
}
else
{
layout.PlayerSpawnX = entrance.X + entrance.Width / 2;
layout.PlayerSpawnY = entrance.Y + entrance.Height / 2;
}
// Set legacy SpawnPoint too
layout.SpawnPoint = new DungeonPoint { X = layout.PlayerSpawnX, Y = layout.PlayerSpawnY };
}
// Find exit room for portal
var exit = rooms.FirstOrDefault(r => r.Type == RoomType.Exit);
if (exit != null)
{
if (exit.ExitX.HasValue && exit.ExitY.HasValue)
{
layout.ExitX = exit.ExitX.Value;
layout.ExitY = exit.ExitY.Value;
}
else
{
layout.ExitX = exit.X + exit.Width / 2;
layout.ExitY = exit.Y + exit.Height / 2;
}
}
int mobId = 0;
int roomIndex = 0;
// Collect mob spawns from all rooms
foreach (var room in rooms)
{
// Add spawns from template - convert RoomMobSpawn to DungeonMobSpawn
foreach (var spawn in room.MobSpawns)
{
var mobType = spawn.Type?.ToLower() switch
{
"boss" => DungeonMobSpawnType.Boss,
"elite" => DungeonMobSpawnType.Elite,
_ => DungeonMobSpawnType.Normal
};
for (int i = 0; i < spawn.Count; i++)
{
layout.MobSpawns.Add(new DungeonMobSpawn
{
Id = mobId++,
X = spawn.X,
Y = spawn.Y,
MobType = mobType,
RoomIndex = roomIndex,
IsAlive = true
});
}
}
// Add procedural spawns for rooms without template spawns
if (room.MobSpawns.Count == 0 && room.Type != RoomType.Entrance)
{
int mobCount = room.Type switch
{
RoomType.Boss => 1,
RoomType.Treasure => _rng.Next(1, 3),
_ => _rng.Next(2, 5)
};
var mobType = room.Type == RoomType.Boss ? DungeonMobSpawnType.Boss : DungeonMobSpawnType.Normal;
for (int i = 0; i < mobCount; i++)
{
int mx = room.X + _rng.Next(1, room.Width - 1);
int my = room.Y + _rng.Next(1, room.Height - 1);
layout.MobSpawns.Add(new DungeonMobSpawn
{
Id = mobId++,
X = mx,
Y = my,
MobType = mobType,
RoomIndex = roomIndex,
IsAlive = true
});
}
}
roomIndex++;
}
layout.TotalMobs = layout.MobSpawns.Count;
Console.WriteLine($"[DungeonGenV3] Spawn points: Player=({layout.PlayerSpawnX},{layout.PlayerSpawnY}), Exit=({layout.ExitX},{layout.ExitY}), Mobs={layout.MobSpawns.Count}");
}
// Helper methods
private T? SelectWeightedTemplate<T>(List<T> templates) where T : class
{
if (templates.Count == 0) return null;
if (templates.Count == 1) return templates[0];
// Get weight property via reflection or assume equal weights
int totalWeight = 0;
var weights = new List<int>();
foreach (var t in templates)
{
int weight = 100;
var weightProp = t.GetType().GetProperty("Weight");
if (weightProp != null)
{
weight = (int)(weightProp.GetValue(t) ?? 100);
}
weights.Add(weight);
totalWeight += weight;
}
int roll = _rng.Next(totalWeight);
int cumulative = 0;
for (int i = 0; i < templates.Count; i++)
{
cumulative += weights[i];
if (roll < cumulative)
return templates[i];
}
return templates[^1];
}
private List<int> ParseIds(string? json)
{
if (string.IsNullOrEmpty(json)) return new List<int>();
try
{
return JsonSerializer.Deserialize<List<int>>(json) ?? new List<int>();
}
catch
{
return new List<int>();
}
}
// JSON helper classes
private class RoomTilePlacement
{
public int X { get; set; }
public int Y { get; set; }
public int SheetId { get; set; }
public int FrameIndex { get; set; }
}
private class TemplateTilePlacement
{
public int X { get; set; }
public int Y { get; set; }
public int SheetId { get; set; }
public int FrameIndex { get; set; }
public int? TileDefId { get; set; }
}
}
// Extended classes for DungeonLayoutV2 if not already defined
public class PlacedObject
@@ -1323,3 +1306,19 @@ public class PlacedObject
public int TrapDamage { get; set; }
public int? LootTableId { get; set; }
}
public class RoomTilePlacement
{
public int X { get; set; }
public int Y { get; set; }
public int SheetId { get; set; }
public int FrameIndex { get; set; }
}
public class TemplateTilePlacement
{
public int X { get; set; }
public int Y { get; set; }
public int SheetId { get; set; }
public int FrameIndex { get; set; }
public int? TileDefId { get; set; }
}
+4 -1
View File
@@ -967,7 +967,10 @@ public class DungeonSimulation
WasResisted = result.WasResisted,
WasWeakness = result.WasWeakness
});
if (mob.Health > 0)
{
mob.State = MobState.Damaged;
}
if (mob.Health <= 0)
{
mob.Health = 0;
+3 -1
View File
@@ -217,5 +217,7 @@ public enum MobState
Kiting, // Moving away while attacking (skirmisher)
Summoning, // In summoning animation
Healing, // Healing self or ally
Dead
Dead,
Walking,
Damaged
}