// 诅咒收益扫描：对 Lv27-30，遍历巨翅飞鱼 atk[25,35]×def[30,40]（速度固定21），
// 比较 有诅咒组(诅咒+水之波动+泥巴射击+岩崩) vs 无诅咒组(碎岩+水之波动+泥巴射击+岩崩) 的最低受伤差。
// node _analysis/curse_sweep.js
const fs = require('fs'), path = require('path');
function loadObj(rel, p) { let s = fs.readFileSync(path.join(__dirname, '..', rel), 'utf8'); s = s.replace(p || /^\s*var\s+[\w$]+\s*=\s*/, ''); return eval('(' + s + ')'); }
const data = loadObj('project/data.js'), items = loadObj('project/items.js'), enemys = loadObj('project/enemys.js'), funcs = loadObj('project/functions.js');
const FLAGS = {};
const core = {
    status: { floorId: 'WildArea', checkBlock: { needCache: false, cache: {} }, hero: { lv: 27, mana: 32, atk: 52, def: 25, mdef: 0, hpmax: 80, hp: 80, type1: 'Water', type2: 'Ground' } },
    flags: data.flags, values: data.values, material: { items, enemys }, _held: null,
    getRealStatusOrDefault(h, n) { return (h && h[n] != null) ? h[n] : core.status.hero[n]; }, getStatusOrDefault(h, n) { return core.getRealStatusOrDefault(h, n); },
    getFlag(n, d) { return FLAGS[n] == null ? d : FLAGS[n]; }, setFlag(n, v) { FLAGS[n] = v; }, hasEquip(id) { return core._held === id; }, hasSpecial() { return false; }, extractBlocks() {}, enemys: {}
};
core.enemys.hasSpecial = core.hasSpecial; core.enemys.getEnemyInfo = funcs.enemys.getEnemyInfo; core.enemys.getDamageInfo = funcs.enemys.getDamageInfo;
global.core = core; global.main = { levelChoose: [] }; global.flags = FLAGS;

// ---- 升级表 → 计算 Lv27-30 属性 ----
const LUP = data.firstData.levelUp.map(e => {
    const d = { datk: 0, ddef: 0, dhpmax: 0, dhp: 0, dmana: 0, dmdef: 0, evolve: false };
    (e.action || []).forEach(a => {
        if (a && a.type === 'setValue' && a.operator === '+=') { const v = Number(a.value); if (isNaN(v)) return;
            if (a.name === 'status:atk') d.datk += v; else if (a.name === 'status:def') d.ddef += v;
            else if (a.name === 'status:hpmax') d.dhpmax += v; else if (a.name === 'status:mana') d.dmana += v;
            else if (a.name === 'status:mdef') d.dmdef += v; }
        else if (a && a.type === 'function' && /type2\s*=\s*["']Ground["']/.test(a.function || '')) d.evolve = true;
    });
    return d;
});
const heroByLv = {};
{ const H = { lvIdx: 7, atk: 20, def: 10, mdef: 0, hpmax: 32, mana: 13, type2: null };
  while (H.lvIdx < LUP.length) { const lv = H.lvIdx - 1 + 5;
      if (lv >= 27 && lv <= 30) heroByLv[lv] = { atk: H.atk, def: H.def, mdef: H.mdef, hpmax: H.hpmax, mana: H.mana, type2: H.type2 };
      if (lv >= 30) break;
      const L = LUP[H.lvIdx]; H.atk += L.datk; H.def += L.ddef; H.mdef += L.dmdef; H.hpmax += L.dhpmax; H.mana += L.dmana; if (L.evolve) H.type2 = 'Ground'; H.lvIdx++; } }

const WITH = [' Curse', ' WaterPulse', ' MudShot', ' RockSlide'];     // 有诅咒
const WITHOUT = [' RockSmash', ' WaterPulse', ' MudShot', ' RockSlide']; // 无诅咒(碎岩)

function setHero(lv) { const s = heroByLv[lv], h = core.status.hero; h.lv = lv; h.atk = s.atk; h.def = s.def; h.mdef = s.mdef; h.hpmax = s.hpmax; h.hp = s.hpmax; h.mana = s.mana; h.type1 = 'Water'; h.type2 = s.type2; }
function minDmg(enemy, moves) { let best = null;
    for (const a of moves) for (const b of moves) { FLAGS.skill1 = a; FLAGS.skill2 = b; let i = null;
        try { i = core.enemys.getDamageInfo(enemy, null, null, null, 'WildArea'); } catch (_) {}
        if (i && i.damage != null && (best == null || i.damage < best)) best = i.damage; }
    return best; }

const fish0 = enemys['liteBowman']; // 巨翅飞鱼(含水流环 skill1, 水之波动 skill2)
const ATKS = [], DEFS = [];
for (let a = 25; a <= 35; a++) ATKS.push(a);
for (let d = 30; d <= 40; d++) DEFS.push(d);

for (const lv of [27, 28, 29, 30]) {
    setHero(lv); const s = heroByLv[lv];
    console.log('\n══════ 英雄 Lv' + lv + '：atk' + s.atk + ' def' + s.def + ' hpmax' + s.hpmax + ' 速度' + s.mana + ' (诅咒后速度×0.667=' + (s.mana * 2 / 3).toFixed(0) + ' vs 怪21) ══════');
    console.log('表内数字 = 无诅咒最低受伤 − 有诅咒最低受伤（正=诅咒更省血）。C=仅诅咒组能打过，X=都打不过');
    console.log('def\\atk ' + ATKS.map(a => String(a).padStart(5)).join(''));
    for (const def of DEFS) {
        const row = [];
        for (const atk of ATKS) {
            const e = Object.assign({}, fish0, { hp: 180, atk: atk, def: def, speed: 21 });
            const A = minDmg(e, WITH), B = minDmg(e, WITHOUT);
            let cell;
            if (A == null && B == null) cell = 'X';
            else if (B == null) cell = 'C';            // 无诅咒打不过、诅咒能打过
            else if (A == null) cell = '?';            // 反常（诅咒反而打不过）
            else cell = String(B - A);
            row.push(cell.padStart(5));
        }
        console.log(('def' + def).padEnd(7) + row.join(''));
    }
}
console.log('\n(怪=巨翅飞鱼，水/飞行，hp180，速度固定21，技能 水流环+水之波动；英雄无携带物加成)');
