using System.Collections.Generic;
using TKS.OutGame;
using UnityEngine;
namespace TKS
{
public enum TutorialMode
{
Auto = 0, // 실제 세이브로 하는 플레이 = 켬 · 메모리 세이브(자동 테스트 · 검증 캡처) = 끔
ForceOn = 1, // 학습 판 테스트 · 검증 드라이버
ForceOff = 2
}
///
/// 1장 학습 판 규칙(2026-11-12 PD 「초반 10개 스테이지는 우리 게임의 기본적인 기능을 학습할 수 있는 시나리오 구성 및 스테이지 구성」 · 개발 요청서 C5).
/// 표 = TutorialDb(tutorial_stage · tutorial_layout · tutorial_guide · tutorial_config). 전투 시작(BattleManager.Begin)이 For(stage) 로 그 판의 계획을 받아
/// 고정 편성 · 시작 자리 · 적 배치 · 구출 대상 · 턴 제한을 적용한다. 안내(손가락 · 카드 · 띠)는 TutorialDirector.
/// 자동 테스트·검증 캡처(메모리 세이브)는 1-1 을 「일반 전투 진입」으로 쓰므로 Auto 에서는 꺼진다 — 학습 판을 보려면 Mode = ForceOn.
///
public static class TutorialRules
{
public static TutorialMode Mode = TutorialMode.Auto;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
Mode = TutorialMode.Auto;
}
public static bool Enabled
{
get { return Mode == TutorialMode.ForceOn || (Mode == TutorialMode.Auto && !SaveStore.MemoryOnly); }
}
/// 그 스테이지의 학습 판 계획(표에 없거나 꺼져 있으면 null = 일반 판).
public static TutorialPlan For(BalanceConfig.StageDef stage)
{
if (!Enabled || stage == null || stage.chapter <= 0) return null;
var db = TutorialTables.Db;
if (db == null) return null;
var row = db.Stage(stage.chapter, stage.stage);
return row == null ? null : new TutorialPlan(db, row);
}
static OutGameSession Session
{
get { return OutGameRoot.Instance != null ? OutGameRoot.Instance.Session : null; }
}
/// 그 판을 깬 적이 있는가(세이브의 클리어 기록 · 세션이 없으면 false).
public static bool Cleared(int chapter, int stage)
{
var s = Session;
if (s == null || s.Save == null) return false;
var st = s.Save.Stage(chapter, stage, false);
return st != null && st.clears > 0;
}
/// 조준 중 상성 표시(적 머리 위 유리·불리)를 켜는 판인가 — tutorial_config matchup_mark_chapter·stage 부터 모든 판(연무장 포함).
public static bool MatchupMarksOn(BalanceConfig.StageDef stage)
{
var db = TutorialTables.Db;
if (db == null || stage == null || !db.Has("matchup_mark_chapter")) return false;
if (stage.chapter <= 0) return true; // 연무장 모드 전투
int ch = Mathf.RoundToInt(db.Num("matchup_mark_chapter", 1f));
int st = Mathf.RoundToInt(db.Num("matchup_mark_stage", 1f));
return stage.chapter > ch || (stage.chapter == ch && stage.stage >= st);
}
///
/// 영웅 id → 출전 칸(보유 여부와 무관 · 학습 판 고정 편성·구출 대상). 보유 중이면 그 성장 그대로, 아니면 1레벨.
/// withSkills = false 면 스킬 없이(구출 대상 = 패시브 미적용).
///
public static LineupEntry HeroEntry(BalanceConfig cfg, string id, int slot, bool withSkills)
{
var s = Session;
var db = s != null ? s.Db : OutGameData.Default;
var h = db != null && db.Heroes != null ? db.Heroes.Get(id) : null;
if (h == null || cfg == null) return null;
var save = s != null ? s.Save : null;
var st = HeroRules.Compute(db, cfg, h, save != null ? save.Level(id) : null, save);
var e = new LineupEntry
{
slot = slot, id = h.id, name = h.nameKo, type = h.type, art = h.art,
hpMult = h.hpMult * st.GrowthOf("hp"), atkMult = h.atkMult * st.GrowthOf("atk"), defMult = h.defMult * st.GrowthOf("def"),
};
if (!withSkills) return e;
if (save != null) SkillRules.FillLineup(db, save, h, e);
else if (!h.isSoldier)
{
e.general = true;
e.skillActive = h.skillActive;
e.skillPassive = h.skillPassive;
}
return e;
}
}
/// 한 판의 학습 판 계획 + 슬롯(배치 표 n_slot) ↔ 전장 유닛 연결.
public class TutorialPlan
{
public readonly TutorialDb Db;
public readonly TutorialStageRow Row;
public readonly List Layout;
readonly Dictionary units = new Dictionary();
public TutorialPlan(TutorialDb db, TutorialStageRow row)
{
Db = db;
Row = row;
Layout = db.Layout(row.chapter, row.stage);
}
public int TurnLimit
{
get { return Row.turnLimit; }
}
/// 적 배치 표가 있는 판(없으면 자동 진형 = stage.enemies).
public bool HasEnemies
{
get { return Row.enemies != null && Row.enemies.Count > 0; }
}
public TutorialLayoutRow SlotRow(int slot)
{
foreach (var r in Layout)
if (r.slot == slot) return r;
return null;
}
public Unit UnitAt(int slot)
{
Unit u;
return units.TryGetValue(slot, out u) ? u : null;
}
public int SlotOf(Unit u)
{
foreach (var kv in units)
if (kv.Value == u) return kv.Key;
return 0;
}
/// 출전 편성 — 고정 편성 판 = 그 영웅들(배치 표 슬롯 순 · 보유 여부와 무관) · 자유 편성 판 = 프리셋 그대로.
public List Lineup(BalanceConfig cfg, List preset)
{
if (!Row.FixedLineup) return preset;
var res = new List();
foreach (var r in Layout)
{
if (r.side != TutSide.Ally) continue;
var e = TutorialRules.HeroEntry(cfg, r.unitId, res.Count, true);
if (e != null) res.Add(e);
}
return res;
}
/// 아군 시작 자리 — 배치 표에 그 영웅의 ally 행이 있으면 그 좌표, 없으면 fallback(출전 자리).
public Vector2 AllyPosition(LineupEntry e, Vector2 fallback)
{
var r = AllyRow(e);
return r != null ? r.pos : fallback;
}
TutorialLayoutRow AllyRow(LineupEntry e)
{
if (e == null || string.IsNullOrEmpty(e.id)) return null;
foreach (var r in Layout)
if (r.side == TutSide.Ally && r.unitId == e.id) return r;
return null;
}
/// 출전한 아군을 슬롯에 연결하고 판 규칙(최소 체력)을 건다.
public void BindAlly(LineupEntry e, Unit u)
{
if (u == null) return;
var r = AllyRow(e);
if (r != null) units[r.slot] = u;
if (Row.noDefeat) u.MinHp = GuardHp;
}
int GuardHp
{
get { return Mathf.Max(1, Mathf.RoundToInt(Db.Num("npc_guard_hp", 1f))); }
}
/// 배치 표의 적을 만든다(자동 진형 대신 · 무장 패시브·보스 액티브 = 일반 판과 같은 규칙).
public void SpawnEnemies(BattleManager bm, BalanceConfig.StageDef stage)
{
foreach (var te in Row.enemies)
{
var e = te.spawn;
var u = bm.SpawnUnit(e.type, Faction.Enemy, e.position, e.name, e.hpMult, e.atkMult, e.isBoss, e.art);
bm.Skills.AttachEnemy(u, e, TacticalAI.BossUsesActive(stage, e));
units[te.slot] = u;
}
}
/// 구출 대상(e_side npc) — 아군 진영 · 조작 불가 · 행동 없음 · 반격 없음 · 스킬 없음 · 적 AI 의 표적.
public void SpawnNpcs(BattleManager bm)
{
foreach (var r in Layout)
{
if (r.side != TutSide.Npc) continue;
var e = TutorialRules.HeroEntry(bm.Config, r.unitId, 0, false);
if (e == null) continue;
var u = bm.SpawnUnit(e.type, Faction.Player, r.pos, e.name, e.hpMult * r.hpMult, e.atkMult * r.atkMult, false, e.art);
u.Def = Mathf.Max(0, Mathf.RoundToInt(bm.Config.GetStats(e.type).def * e.defMult));
u.HeroId = e.id ?? "";
u.Npc = true;
u.ActedThisTurn = true;
if (Row.noDefeat) u.MinHp = GuardHp;
if (u.View != null) u.View.Refresh();
units[r.slot] = u;
}
}
/// 구출 대상이 쓰러졌는가(= 실패).
public bool NpcLost
{
get
{
foreach (var r in Layout)
{
if (r.side != TutSide.Npc) continue;
var u = UnitAt(r.slot);
if (u != null && !u.IsAlive) return true;
}
return false;
}
}
}
}