using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using EerieVillage.Map.Data;
namespace EerieVillage.Map.Generation
{
///
/// BT13-Dev-Map — 공중 발판 절차 생성 Planner (PD 2026-05-19 단순화).
/// spec §6 발판 배치 룰 + §4-3 step 5 + §4-5 ValidateJumpReach 정합.
/// 실측 점프 (단일): H_max=5.62m·D_max=14.98m·safety 90%.
/// PD §15-2 OVERRIDE (2026-05-18) 이중 점프: H_max=11.24m·D_max≈29.96m.
/// SAFE_VERTICAL_HEIGHT=10.11m·SAFE_HORIZONTAL_DISTANCE=11.56m (90% margin · 보수).
/// PD 2026-05-19 — 발판 2종 (platformShort·platformLong) 폭 기준 선택.
/// 이전 본문 백업: 공유/개발팀_백업/EerieVillage/BT13-Dev-Map_20260519/PlatformPlanner.bak_20260519_0050.cs
///
public static class PlatformPlanner
{
// §15-2 PD OVERRIDE (2026-05-18) 이중 점프 — 90% safety margin
public const float SAFE_VERTICAL_HEIGHT = 10.11f;
public const float SAFE_HORIZONTAL_DISTANCE = 11.56f;
// 발판 종류 선택 임계값 — 폭 ≤ 임계 = short · 초과 = long
const int SHORT_LONG_WIDTH_THRESHOLD = 3;
public static void Plan(MapGenerationConfig config, MapBlueprint blueprint)
{
if (config == null || blueprint == null || blueprint.groundSurfaceY == null) return;
int length = config.lengthTiles;
int startBoundary = config.startMarginTiles + 5;
int goalBoundary = length - config.goalMarginTiles - 2;
// §15-9 OVERRIDE — BossRoom 청크는 발판 배치 skip
if (config.bossRoomEnabled)
{
int bossRoomStart = length - config.bossRoomLengthTiles;
goalBoundary = Mathf.Min(goalBoundary, bossRoomStart - 2);
}
int x = startBoundary;
int retryGuard = 0;
const int MAX_RETRIES = 5;
float prevAnchorX = startBoundary;
float prevAnchorY = blueprint.groundSurfaceY[Mathf.Clamp(startBoundary, 0, length - 1)];
while (x < goalBoundary)
{
float densityMul = GetChunkDensityMultiplier(config, x, length);
float effectiveDensity = config.platformDensity * densityMul;
if (Random.value < effectiveDensity)
{
int width = Random.Range(config.minPlatformWidthTiles, config.maxPlatformWidthTiles + 1);
int heightOffset = Random.Range(config.minPlatformHeight, config.maxPlatformHeight + 1);
int groundY = blueprint.groundSurfaceY[Mathf.Clamp(x, 0, length - 1)];
int platformY = groundY + heightOffset;
bool canReach = ValidateJumpReach(prevAnchorX, prevAnchorY, x, platformY);
if (canReach)
{
AddPlatformTiles(blueprint, config.catalog, x, platformY, width);
prevAnchorX = x + (width - 1) * 0.5f;
prevAnchorY = platformY;
int gap = Random.Range(config.minPlatformGapTiles, config.maxPlatformGapTiles + 1);
x += width + gap;
blueprint.heightTiles = Mathf.Max(blueprint.heightTiles, platformY + 1);
retryGuard = 0;
continue;
}
else if (retryGuard < MAX_RETRIES)
{
retryGuard++;
x += 1;
continue;
}
retryGuard = 0;
}
x += 1;
}
}
public static bool ValidateJumpReach(float fromX, float fromY, float toX, float toY)
{
float dx = Mathf.Abs(toX - fromX);
float dy = toY - fromY;
if (dx > SAFE_HORIZONTAL_DISTANCE) return false;
if (dy > SAFE_VERTICAL_HEIGHT) return false;
return true;
}
static float GetChunkDensityMultiplier(MapGenerationConfig config, int x, int length)
{
float ratio = (float)x / length;
if (ratio < 0.33f) return config.introPlatformDensityMultiplier;
if (ratio < 0.66f) return config.midPlatformDensityMultiplier;
return config.bossPrePlatformDensityMultiplier;
}
///
/// PD 2026-05-19 — 발판 2종 (platformShort·platformLong) 폭 기준 선택.
/// 폭 ≤ SHORT_LONG_WIDTH_THRESHOLD(3): platformShort 우선
/// 폭 > 임계: platformLong 우선
/// fallback: 다른 한 종이 null 시 보유 종 사용.
///
static void AddPlatformTiles(MapBlueprint blueprint, MapResourceCatalog catalog, int startX, int y, int width)
{
if (catalog == null) return;
TileBase tile = PickPlatformTile(catalog, width);
if (tile == null) return;
for (int i = 0; i < width; i++)
{
int tx = startX + i;
// edgeKind = 0/1/2 보존 — Apply 단계 호환성 유지
int edgeKind;
if (width == 1) edgeKind = 1;
else if (i == 0) edgeKind = 0;
else if (i == width - 1) edgeKind = 2;
else edgeKind = 1;
blueprint.platforms.Add(new PlatformTilePlace { x = tx, y = y, tile = tile, edgeKind = edgeKind });
}
}
static TileBase PickPlatformTile(MapResourceCatalog catalog, int width)
{
bool preferShort = width <= SHORT_LONG_WIDTH_THRESHOLD;
TileBase first = preferShort ? catalog.platformShort : catalog.platformLong;
TileBase second = preferShort ? catalog.platformLong : catalog.platformShort;
if (first != null) return first;
if (second != null) return second;
return null;
}
}
}