using System.Collections.Generic;
using UnityEngine;
using EerieVillage.Map.Data;
namespace EerieVillage.Map.Generation
{
///
/// BT13-Dev-Map — 거점·Start Point·Goal 절차 생성 Planner (PD 2026-05-19 단순화).
/// spec §8 정합. PD §15 OVERRIDE (2026-05-18):
/// §15-5 OVERRIDE — 공중 발판 거점 허용 (allowFloatingSpawnPoints=true)
/// §15-6 — Start 좌측 고정
/// PD 2026-05-19 — §15-8 OVERRIDE 폐기 (baseEnemyPrefab·flyingEnemyPrefabs·enemyPrefabs[] 가중치 random 제거).
/// monsters 리스트 (List) random pick 만 사용.
/// 이전 본문 백업: 공유/개발팀_백업/EerieVillage/BT13-Dev-Map_20260519/SpawnPointPlanner.bak_20260519_0050.cs
///
public static class SpawnPointPlanner
{
public static void Plan(MapGenerationConfig config, MapBlueprint blueprint)
{
if (config == null || blueprint == null || blueprint.groundSurfaceY == null) return;
int length = config.lengthTiles;
if (length <= 0) return;
// Start Point — 좌측 고정 (§15-6)
int startX = Mathf.Clamp(config.startMarginTiles, 0, length - 1);
int startGroundY = blueprint.groundSurfaceY[startX];
blueprint.startPoint = new Vector2(startX + 0.5f, startGroundY + 1.5f);
// Goal Point — 우측 고정 (§15-9 — BossRoom 직전)
int effectiveGoalLength = length;
if (config.bossRoomEnabled)
{
effectiveGoalLength = length - config.bossRoomLengthTiles;
}
int goalX = Mathf.Clamp(effectiveGoalLength - config.goalMarginTiles, 0, length - 1);
int goalGroundY = blueprint.groundSurfaceY[goalX];
blueprint.goalPoint = new Vector2(goalX + 0.5f, goalGroundY + 1.5f);
// 거점 후보 수
int desiredCount = Mathf.FloorToInt(length * config.spawnDensity / Mathf.Max(1, config.minSpawnSpacingTiles));
int finalCount = Mathf.Min(desiredCount, config.maxSpawnPerMap);
if (finalCount <= 0) return;
int marginFromStart = 8;
int marginFromGoal = 8;
int minSpacing = config.minSpawnSpacingTiles;
var candidates = new List();
for (int x = startX + marginFromStart; x < goalX - marginFromGoal; x++)
{
if (IsFlatGroundRun(blueprint, x, 3))
{
candidates.Add(new SpawnCandidate
{
x = x,
y = blueprint.groundSurfaceY[x] + 1,
isFloating = false
});
}
}
// §15-5 OVERRIDE — 공중 발판 후보 추가
if (config.allowFloatingSpawnPoints)
{
var platformRuns = GroupPlatformRuns(blueprint.platforms);
foreach (var run in platformRuns)
{
if (run.width < 3) continue;
int cx = run.startX + run.width / 2;
if (cx < startX + marginFromStart || cx > goalX - marginFromGoal) continue;
candidates.Add(new SpawnCandidate
{
x = cx,
y = run.y + 1,
isFloating = true
});
}
}
if (candidates.Count == 0) return;
// Shuffle (Fisher-Yates)
for (int i = candidates.Count - 1; i > 0; i--)
{
int j = Random.Range(0, i + 1);
(candidates[i], candidates[j]) = (candidates[j], candidates[i]);
}
// minSpacing 정합 picking
var picked = new List();
foreach (var cand in candidates)
{
bool tooClose = false;
foreach (var p in picked)
{
if (Mathf.Abs(cand.x - p.x) < minSpacing) { tooClose = true; break; }
}
if (!tooClose) picked.Add(cand);
if (picked.Count >= finalCount) break;
}
picked.Sort((a, b) => a.x.CompareTo(b.x));
// PD 2026-05-19 — monsters 리스트 random pick (가중치·flying ratio·legacy fallback 모두 제거)
List monsters = config.catalog != null ? config.catalog.monsters : null;
if (monsters == null || monsters.Count == 0) return;
foreach (var cand in picked)
{
GameObject prefab = PickRandomMonster(monsters);
if (prefab == null) continue;
blueprint.spawnPoints.Add(new SpawnPointPlace
{
position = new Vector2(cand.x + 0.5f, cand.y),
enemyPrefab = prefab,
isFlying = false // PD 2026-05-19 — flying 구분 폐기
});
}
}
static GameObject PickRandomMonster(List monsters)
{
// null 항목 제외 list 영역
List valid = null;
for (int i = 0; i < monsters.Count; i++)
{
if (monsters[i] != null)
{
if (valid == null) valid = new List();
valid.Add(monsters[i]);
}
}
if (valid == null || valid.Count == 0) return null;
return valid[Random.Range(0, valid.Count)];
}
struct SpawnCandidate
{
public int x;
public int y;
public bool isFloating;
}
struct PlatformRun
{
public int startX;
public int width;
public int y;
}
static List GroupPlatformRuns(List platforms)
{
var runs = new List();
if (platforms == null || platforms.Count == 0) return runs;
var byY = new Dictionary>();
foreach (var p in platforms)
{
if (!byY.TryGetValue(p.y, out var list))
{
list = new List();
byY[p.y] = list;
}
list.Add(p.x);
}
foreach (var kv in byY)
{
var xs = kv.Value;
xs.Sort();
int runStart = xs[0];
int runLen = 1;
for (int i = 1; i < xs.Count; i++)
{
if (xs[i] == xs[i - 1] + 1)
{
runLen++;
}
else
{
runs.Add(new PlatformRun { startX = runStart, width = runLen, y = kv.Key });
runStart = xs[i];
runLen = 1;
}
}
runs.Add(new PlatformRun { startX = runStart, width = runLen, y = kv.Key });
}
return runs;
}
static bool IsFlatGroundRun(MapBlueprint blueprint, int x, int minRun)
{
int length = blueprint.groundSurfaceY.Length;
int baseY = blueprint.groundSurfaceY[x];
for (int i = 1; i < minRun; i++)
{
int ix = x + i;
if (ix >= length) return false;
if (blueprint.groundSurfaceY[ix] != baseY) return false;
}
return true;
}
}
}