// 最终清扫：任何「含透明像素的瓦片」都不得出现在任何地图的 ground 层（否则露黑底）
import puppeteer from 'puppeteer-core';
import fs from 'node:fs';
const browser = await puppeteer.launch({ executablePath: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', headless: true });
const page = await browser.newPage();
// 把 tileset.png 读成 dataURL 注入页面分析 alpha
const b64 = fs.readFileSync('public/assets/tileset.png').toString('base64');
const transparentIds = await page.evaluate(async (data) => {
  const img = new Image();
  await new Promise((res) => { img.onload = res; img.src = data; });
  const c = document.createElement('canvas');
  c.width = img.width; c.height = img.height;
  const ctx = c.getContext('2d');
  ctx.drawImage(img, 0, 0);
  const d = ctx.getImageData(0, 0, img.width, img.height).data;
  const out = [];
  for (let id = 0; id < 128; id++) {
    const ox = (id % 16) * 16, oy = Math.floor(id / 16) * 16;
    let trans = false;
    for (let y = 0; y < 16 && !trans; y++)
      for (let x = 0; x < 16; x++)
        if (d[((oy + y) * img.width + ox + x) * 4 + 3] < 255) { trans = true; break; }
    if (trans) out.push(id);
  }
  return out;
}, 'data:image/png;base64,' + b64);
await browser.close();
console.log('含透明像素的瓦片 id:', transparentIds.join(','));
const maps = ['house','plaza','bakery','shop','clinic','lake','field','well','fog','white'];
let bad = 0;
for (const key of maps) {
  const m = JSON.parse(fs.readFileSync(`public/assets/maps/${key}.json`, 'utf8'));
  const g = m.layers.find((l) => l.name === 'ground');
  const hits = new Set();
  for (const gid of g.data) {
    if (gid === 0) { hits.add('EMPTY'); continue; }
    const id = gid - 1;
    if (transparentIds.includes(id)) hits.add(id);
  }
  if (hits.size) { bad++; console.log(`FAIL ${key}: ground 层含透明瓦片/空格 ->`, [...hits].join(',')); }
  else console.log(`OK   ${key}`);
}
process.exit(bad ? 1 : 0);
