|
|
用途:v在 cps1lab 页面自动采集《天地を喰らうII》wof.zip 的单角色精灵图集与纯实景背景帧,并一键导出 ZIP
使用说明
## 一、安装(一次性)
1. Chrome 打开扩展商店,安装 **Tampermonkey**(篡改猴)。
2. 点击浏览器右上角的 Tampermonkey 图标 → **管理面板** → **添加新脚本**。
3. 把 `wof采集器.user.js` 的内容整段粘贴进去(覆盖模板里的示例代码),**Ctrl+S** 保存。
- 也可以在管理面板里开启「高级」→ 直接把 .user.js 文件拖进浏览器,扩展会接管安装。
4. 确认脚本处于「启用」状态,并且 `@match` 里是 `https://www.cps1lab.com/*`。
## 二、每次使用
1. 打开 <https://www.cps1lab.com/play/>。
2. 点页面上的 **LOAD**(或直接把 `wof.zip` 拖进页面),等模拟器把游戏跑起来。
3. 右上角出现深绿色 **「WOF 采集器」** 面板,第一行会显示 `精灵 N 组 | 图集 … | 背景 N 帧 | 帧号 …`。
## 三、面板按钮
| 按钮 | 作用 |
| --- | --- |
| ▶ 自动采集 | 每 0.6 秒抓一次当前画面的 OAM,自动分组去重,累加到图集 |
| ▶ 自动背景 | 每 1.5 秒抓一帧纯实景(不含 HUD)背景,最多缓存 400 帧 |
| 采集一次 | 手动抓一帧 |
| ▶ 自动推进 | 内置机器人:P1 右行连打、P2 若已加入则后置横扫;遇到死亡/标题/选人等暗底文字画面会自动按 START(含投币) |
| 导出精灵 ZIP | 把图集按索引切成单张透明 PNG,打包成 zip(含 `索引.csv`) |
| 导出背景 ZIP | 把缓存里的背景帧打包成 zip |
| 导出图集 PNG | 导出整张 2048×8192 拼图 + 需要配合 `索引.csv` |
| 清空重来 | 清空精灵与背景缓存 |
## 四、建议的采集流程
1. 开「自动推进」+「自动采集」+「自动背景」,让它自己打;
2. 想手动玩也可以:自己操作键盘,同时开着「自动采集」「自动背景」;
3. 打到没新东西(面板上「精灵 N 组」不再增长)就点「导出精灵 ZIP」;
4. 想继续换关卡,就手动续关(死亡画面按 START → 选人 → 继续),或者开「自动推进」。
## 五、注意
- **必须联网**:脚本依赖 cps1lab 这个网页模拟器提供的 `window.__emu`(OAM、调色板、gfx ROM、帧缓冲)。
- 采集是**逐帧按 OAM 分组**:场上角色越多、动作越全,抓到的组越多;同一组会被指纹自动去重。
- 背景帧来自帧缓冲 `fb32`,**不含 HUD**(HUD 在文字层),所以导出就是纯场景图。
- 若某次导出时浏览器提示"多个文件下载",同意即可(精灵是打包成一个 zip,不会散落)。
- 控制台可用 `__WOF.counts()` 查看数量、`__WOF.collect()` 手动抓一帧、`__WOF.dumpIdx()` 导出索引 JSON。
> (注:部分内容由豆包 AI 生成)
- // ==UserScript==
- // @name WOF 精灵/背景 采集器(CPS1 Lab)
- // @namespace wof-extractor
- // @version 1.1
- // @description 在 cps1lab 页面自动采集《天地を喰らうII》wof.zip 的单角色精灵图集与纯实景背景帧,并一键导出 ZIP
- // @match https://www.cps1lab.com/*
- // @match https://cps1lab.com/*
- // @run-at document-idle
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- const AWC = 2048, AHC = 8192;
- const COLLECT_EVERY_MS = 600; // 自动采集间隔
- const BG_EVERY_MS = 1500; // 自动抓背景间隔
- let emu = null, atlasCv = null, atlasCtx = null;
- let cx = 0, cy = 0, lh = 0;
- const map = {}; // 指纹去重
- const idx = []; // 索引
- const bgFrames = []; // {t,d}
- let lastFc = -1, lastBgT = -1;
- let autoCollect = false, autoPlay = false, autoBg = false;
- let timer = null;
- const stats = { collect: 0, bg: 0 };
- // ---------------- 图集 ----------------
- function initAtlas() {
- if (atlasCv) return;
- atlasCv = document.createElement('canvas');
- atlasCv.width = AWC; atlasCv.height = AHC;
- atlasCtx = atlasCv.getContext('2d');
- atlasCtx.clearRect(0, 0, AWC, AHC);
- }
- // ---------------- 单个 OAM 条目绘制 ----------------
- function drawEntry(fb, e, r) {
- const v = emu.video, pal = v.paletteCache;
- const t = e * 8;
- const x = (r[t] << 8 | r[t + 1]), y = (r[t + 2] << 8 | r[t + 3]);
- const c = (r[t + 4] << 8 | r[t + 5]), d = (r[t + 6] << 8 | r[t + 7]);
- if ((d & 0xFF00) === 0xFF00) return -1; // 结束标记
- const f = d & 31, p = (d >> 5) & 1, m = (d >> 6) & 1;
- const nx = ((d >> 8) & 15) + 1, ny = ((d >> 12) & 15) + 1;
- if (d & 0xFF00) {
- for (let ry = 0; ry < ny; ry++) for (let rx = 0; rx < nx; rx++) {
- const sx = ((x + rx * 16) & 511) - 64, sy = ((y + ry * 16) & 511) - 16;
- if (sx >= 384 || sx + 15 < 0 || sy >= 224 || sy + 15 < 0) continue;
- let ti;
- if (m) ti = p ? (c & -16) + ((c + (nx - 1) - rx) & 15) + 16 * (ny - 1 - ry)
- : (c & -16) + ((c + rx) & 15) + 16 * (ny - 1 - ry);
- else ti = p ? (c & -16) + ((c + (nx - 1) - rx) & 15) + 16 * ry
- : (c & -16) + ((c + rx) & 15) + 16 * ry;
- v.drawSpriteTileFast(fb, pal, v.graphicsRom, v.graphicsRom.length, v.tileRowBuf, ti, f, p, m, sx, sy);
- }
- } else {
- const sx2 = (x & 511) - 64, sy2 = (y & 511) - 16;
- if (sx2 >= 384 || sx2 + 15 < 0 || sy2 >= 224 || sy2 + 15 < 0) return -1;
- v.drawSpriteTileFast(fb, pal, v.graphicsRom, v.graphicsRom.length, v.tileRowBuf, c, f, p, m, sx2, sy2);
- }
- return f;
- }
- // ---------------- 采集一帧 ----------------
- function collectOnce() {
- if (!emu) return null;
- initAtlas();
- const r = emu.video.objBuffer, n = r.length / 8;
- let end = n - 1;
- for (let e = 0; e < n; e++) {
- if (((r[e * 8 + 6] << 8 | r[e * 8 + 7]) & 0xFF00) === 0xFF00) { end = e - 1; break; }
- }
- // 按 调色板号 + y 回跳 分组
- const groups = []; let cur = null, prevY = 0, prevPal = -1;
- for (let e = 0; e <= end; e++) {
- const t = e * 8;
- const y = (r[t + 2] << 8 | r[t + 3]), d = (r[t + 6] << 8 | r[t + 7]), f = d & 31;
- if (cur === null || f !== prevPal || y < prevY - 2) { if (cur) groups.push(cur); cur = { pal: f, list: [] }; }
- cur.list.push(e); prevY = y; prevPal = f;
- }
- if (cur) groups.push(cur);
- let added = 0;
- for (const g of groups) {
- const fb = new Uint32Array(384 * 224);
- for (const k of g.list) drawEntry(fb, k, r);
- let minx = 999, miny = 999, maxx = -1, maxy = -1, cnt = 0;
- for (let yy = 0; yy < 224; yy++) for (let xx = 0; xx < 384; xx++) {
- if (fb[yy * 384 + xx] !== 0) { cnt++; if (xx < minx) minx = xx; if (xx > maxx) maxx = xx; if (yy < miny) miny = yy; if (yy > maxy) maxy = yy; }
- }
- if (cnt < 60 || maxx < 0) continue;
- const codes = g.list.map(e => (r[e * 8 + 4] << 8 | r[e * 8 + 5]));
- const fp = g.pal + '#' + codes.join(',');
- if (map[fp]) continue;
- const w = maxx - minx + 1, h = maxy - miny + 1;
- const tmp = document.createElement('canvas'); tmp.width = w; tmp.height = h;
- const tid = tmp.getContext('2d').createImageData(w, h);
- for (let yy2 = 0; yy2 < h; yy2++) for (let xx2 = 0; xx2 < w; xx2++) {
- const pp = fb[(yy2 + miny) * 384 + (xx2 + minx)];
- const o = (yy2 * w + xx2) * 4;
- tid.data[o] = pp & 255; tid.data[o + 1] = (pp >> 8) & 255; tid.data[o + 2] = (pp >> 16) & 255;
- tid.data[o + 3] = pp === 0 ? 0 : 255;
- }
- tmp.getContext('2d').putImageData(tid, 0, 0);
- if (cx + w > AWC) { cx = 0; cy += lh + 3; lh = 0; }
- if (cy + h >= AHC) continue;
- atlasCtx.drawImage(tmp, cx, cy);
- map[fp] = 1;
- idx.push({ id: idx.length, x: cx, y: cy, w: w, h: h, pal: g.pal, n: g.list.length, codes: codes, codesJoin: codes.join(','), fc: emu.frameCount });
- if (h > lh) lh = h;
- cx += w + 3; added++;
- }
- stats.collect++;
- refresh();
- return added;
- }
- // ---------------- 背景帧 ----------------
- let bgCv = null;
- function grabFrame() {
- if (!emu) return null;
- if (!bgCv) { bgCv = document.createElement('canvas'); bgCv.width = emu.renderer.nativeWidth; bgCv.height = emu.renderer.nativeHeight; }
- const w = bgCv.width, h = bgCv.height, ctx2 = bgCv.getContext('2d');
- const img = ctx2.createImageData(w, h);
- const fb = emu.video.fb32;
- const src = new Uint32Array(fb.buffer ? fb.buffer : fb);
- const d8 = new Uint8ClampedArray(img.data.buffer);
- for (let i = 0, n = w * h; i < n; i++) {
- const v = src[i];
- d8[i * 4] = v & 255; d8[i * 4 + 1] = (v >> 8) & 255; d8[i * 4 + 2] = (v >> 16) & 255; d8[i * 4 + 3] = 255;
- }
- ctx2.putImageData(img, 0, 0);
- return bgCv.toDataURL('image/png');
- }
- function saveBG() {
- const t = emu.frameCount;
- if (t === lastBgT) return;
- lastBgT = t;
- bgFrames.push({ t, d: grabFrame() });
- while (bgFrames.length > 400) bgFrames.shift();
- stats.bg = bgFrames.length;
- refresh();
- }
- // ---------------- 自动驾驶(可选)---------------
- const KEYS = ['up', 'down', 'left', 'right', 'button1', 'button2', 'button3'];
- function isMenu() {
- const f = emu.video.fb32;
- let dark = 0, t = 0, white = 0;
- for (let y = 8; y < 216; y += 4) for (let x = 8; x < 376; x += 4) {
- const v = f[y * 384 + x], r = v & 255, g = (v >> 8) & 255, b = (v >> 16) & 255; t++;
- if (r < 40 && g < 40 && b < 40) dark++;
- }
- for (let y = 172; y < 200; y++) for (let x = 96; x < 220; x++) {
- const v = f[y * 384 + x], r = v & 255, g = (v >> 8) & 255, b = (v >> 16) & 255;
- if (r > 200 && g > 200 && b > 200) white++;
- }
- return (dark / t > 0.45) && white > 60;
- }
- let botFrame = 0;
- function botStep() {
- if (!emu || emu.paused) return;
- if (isMenu()) {
- const v = emu.input.virtualState[0];
- if (emu.bus.coinCtrl[0] < 2) { v.add('coin'); for (let i = 0; i < 30; i++) emu.stepFrame(); v.delete('coin'); }
- v.add('start'); for (let i = 0; i < 30; i++) emu.stepFrame(); v.delete('start'); for (let j = 0; j < 200; j++) emu.stepFrame();
- return;
- }
- // P1 前压连打,P2 后置横扫(有 2P 才生效)
- const vs = emu.input.virtualState;
- for (let p = 0; p < vs.length; p++) KEYS.forEach(k => vs[p] && vs[p].delete(k));
- const n = 60;
- for (let i = 0; i < n; i++) {
- const f = botFrame + i;
- vs[0].add('right'); if (f % 16 < 10) vs[0].add('button1');
- if (vs[1]) { if (f % 70 < 40) vs[1].add('right'); else vs[1].add('left'); if (f % 16 < 10) vs[1].add('button1'); }
- emu.stepFrame();
- }
- botFrame += n;
- }
- // ---------------- 主循环 ----------------
- function loopTick() {
- if (!emu) { tryAttach(); return; }
- if (autoPlay) { try { botStep(); } catch (e) { } }
- if (autoBg) { try { saveBG(); } catch (e) { } }
- if (autoCollect && emu.frameCount !== lastFc) { lastFc = emu.frameCount; try { collectOnce(); } catch (e) { } }
- }
- function startLoop() { if (!timer) timer = setInterval(loopTick, COLLECT_EVERY_MS); }
- // ---------------- ZIP(store,不压缩)----------------
- const CRC_TABLE = (function () {
- const t = new Uint32Array(256);
- for (let i = 0; i < 256; i++) { let c = i; for (let k = 0; k < 8; k++) c = c & 1 ? 0xEDB88320 ^ (c >>> 1) : c >>> 1; t[i] = c >>> 0; }
- return t;
- })();
- function crc32(bytes) {
- let c = 0xFFFFFFFF;
- for (let i = 0; i < bytes.length; i++) c = CRC_TABLE[(c ^ bytes[i]) & 0xFF] ^ (c >>> 8);
- return (c ^ 0xFFFFFFFF) >>> 0;
- }
- function dataUrlToBytes(d) {
- const b64 = d.split(',')[1];
- const bin = atob(b64);
- const out = new Uint8Array(bin.length);
- for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
- return out;
- }
- function textToBytes(s) {
- const out = new Uint8Array(s.length);
- for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i);
- return out;
- }
- function makeZip(files) { // files: [{name, bytes}]
- const parts = [];
- const central = [];
- let offset = 0;
- const enc = new TextEncoder();
- for (const f of files) {
- const nameBytes = enc.encode(f.name);
- const crc = crc32(f.bytes);
- const lh = new Uint8Array(30 + nameBytes.length);
- const dv = new DataView(lh.buffer);
- dv.setUint32(0, 0x04034b50, true);
- dv.setUint16(4, 20, true); dv.setUint16(6, 0x0800, true); dv.setUint16(8, 0, true);
- dv.setUint16(10, 0, true); dv.setUint16(12, 0x21, true); // 1980-01-01
- dv.setUint32(14, crc, true);
- dv.setUint32(18, f.bytes.length, true); dv.setUint32(22, f.bytes.length, true);
- dv.setUint16(26, nameBytes.length, true); dv.setUint16(28, 0, true);
- lh.set(nameBytes, 30);
- parts.push(lh, f.bytes);
- const ch = new Uint8Array(46 + nameBytes.length);
- const dv2 = new DataView(ch.buffer);
- dv2.setUint32(0, 0x02014b50, true);
- dv2.setUint16(4, 20, true); dv2.setUint16(6, 20, true); dv2.setUint16(8, 0x0800, true);
- dv2.setUint16(10, 0, true); dv2.setUint16(12, 0, true); dv2.setUint16(14, 0x21, true);
- dv2.setUint32(16, crc, true);
- dv2.setUint32(20, f.bytes.length, true); dv2.setUint32(24, f.bytes.length, true);
- dv2.setUint16(28, nameBytes.length, true);
- dv2.setUint32(42, offset, true);
- ch.set(nameBytes, 46);
- central.push(ch);
- offset += lh.length + f.bytes.length;
- }
- let cdSize = 0;
- for (const c of central) cdSize += c.length;
- const end = new Uint8Array(22);
- const dv3 = new DataView(end.buffer);
- dv3.setUint32(0, 0x06054b50, true);
- dv3.setUint16(8, files.length, true); dv3.setUint16(10, files.length, true);
- dv3.setUint32(12, cdSize, true); dv3.setUint32(16, offset, true);
- const all = new Blob([...parts, ...central, end], { type: 'application/zip' });
- return all;
- }
- function download(blob, name) {
- const a = document.createElement('a');
- a.href = URL.createObjectURL(blob);
- a.download = name; a.click();
- setTimeout(() => URL.revokeObjectURL(a.href), 3000);
- }
- // 从图集按索引切图
- function sliceSprites() {
- const out = [];
- const tmp = document.createElement('canvas');
- const tctx = tmp.getContext('2d');
- for (const e of idx) {
- tmp.width = e.w; tmp.height = e.h;
- tctx.clearRect(0, 0, e.w, e.h);
- tctx.drawImage(atlasCv, e.x, e.y, e.w, e.h, 0, 0, e.w, e.h);
- const url = tmp.toDataURL('image/png');
- out.push({ name: `sprites/s${String(e.id).padStart(4, '0')}_p${e.pal}_${e.w}x${e.h}.png`, bytes: dataUrlToBytes(url) });
- }
- let csv = 'id,palette,w,h,n,codes\n';
- for (const e of idx) csv += `${e.id},${e.pal},${e.w},${e.h},${e.n},"${e.codesJoin}"\n`;
- out.push({ name: 'sprites/索引.csv', bytes: textToBytes(csv) });
- return out;
- }
- // ---------------- UI ----------------
- let panel, logEl;
- function log(msg) {
- if (!logEl) return;
- logEl.textContent = msg;
- }
- function refresh() {
- if (!panel) return;
- const s = panel.querySelector('#wofStat');
- if (s) s.textContent = `精灵 ${idx.length} 组 | 图集 ${cx}/${cy + lh} | 背景 ${bgFrames.length} 帧 | 帧号 ${emu ? emu.frameCount : '-'}`;
- const b1 = panel.querySelector('#wofAuto');
- if (b1) b1.textContent = autoPlay ? '■ 停止自动推进' : '▶ 自动推进';
- const b2 = panel.querySelector('#wofAutoC');
- if (b2) b2.textContent = autoCollect ? '■ 停止自动采集' : '▶ 自动采集';
- const b3 = panel.querySelector('#wofAutoB');
- if (b3) b3.textContent = autoBg ? '■ 停止自动背景' : '▶ 自动背景';
- }
- function buildUI() {
- if (panel) return;
- panel = document.createElement('div');
- panel.style.cssText = `position:fixed;right:12px;top:64px;z-index:2147483000;width:266px;
- background:#111a14;color:#b7ffc9;border:1px solid #2e7d4f;border-radius:8px;padding:10px 12px;
- font:12px/1.5 ui-monospace,Menlo,monospace;box-shadow:0 6px 20px rgba(0,0,0,.5)`;
- panel.innerHTML = `
- <div style="font-weight:700;color:#6ff;letter-spacing:.5px">WOF 采集器</div>
- <div id="wofStat" style="margin:6px 0 8px;color:#8ee6a8">等待模拟器载入…</div>
- <div style="display:grid;grid-template-columns:1fr 1fr;gap:6px">
- <button id="wofAutoC" style="${btn()}">▶ 自动采集</button>
- <button id="wofAuto" style="${btn()}">▶ 自动推进</button>
- <button id="wofOnce" style="${btn()}">采集一次</button>
- <button id="wofAutoB" style="${btn()}">▶ 自动背景</button>
- <button id="wofZip" style="${btn()}">导出精灵 ZIP</button>
- <button id="wofZipBg" style="${btn()}">导出背景 ZIP</button>
- <button id="wofAtlas" style="${btn()}">导出图集 PNG</button>
- <button id="wofReset" style="${btn()}">清空重来</button>
- </div>
- <div id="wofLog" style="margin-top:8px;color:#7f9f8a;min-height:16px"></div>`;
- document.body.appendChild(panel);
- logEl = panel.querySelector('#wofLog');
- panel.querySelector('#wofAutoC').onclick = () => { autoCollect = !autoCollect; refresh(); };
- panel.querySelector('#wofAuto').onclick = () => { autoPlay = !autoPlay; refresh(); };
- panel.querySelector('#wofOnce').onclick = () => { const a = collectOnce(); log(`本次新增 ${a} 组,累计 ${idx.length}`); };
- panel.querySelector('#wofAutoB').onclick = () => { autoBg = !autoBg; refresh(); log(autoBg ? '自动背景:开' : '自动背景:关'); };
- panel.querySelector('#wofZip').onclick = () => {
- if (!idx.length) { log('还没有精灵,先采集'); return; }
- log('正在打包…');
- setTimeout(() => {
- const files = sliceSprites();
- download(makeZip(files), `wof_sprites_${idx.length}.zip`);
- log(`已导出 ${idx.length} 张精灵`);
- }, 10);
- };
- panel.querySelector('#wofZipBg').onclick = () => {
- if (!bgFrames.length) { log('还没有背景帧'); return; }
- log('正在打包背景…');
- setTimeout(() => {
- const files = bgFrames.map(f => ({ name: `bg/f${String(f.t).padStart(6, '0')}.png`, bytes: dataUrlToBytes(f.d) }));
- download(makeZip(files), `wof_bg_${bgFrames.length}.zip`);
- log(`已导出 ${bgFrames.length} 帧背景`);
- }, 10);
- };
- panel.querySelector('#wofAtlas').onclick = () => {
- if (!idx.length) { log('还没有精灵'); return; }
- const h = Math.min(AHC, cy + lh + 4);
- const c2 = document.createElement('canvas'); c2.width = AWC; c2.height = h;
- c2.getContext('2d').drawImage(atlasCv, 0, 0);
- c2.toBlob(b => download(b, `wof_atlas_${idx.length}.png`), 'image/png');
- log('已导出图集 PNG');
- };
- panel.querySelector('#wofReset').onclick = () => {
- if (!confirm('清空已采集的精灵与背景帧?')) return;
- idx.length = 0; for (const k in map) delete map[k];
- cx = 0; cy = 0; lh = 0;
- atlasCtx.clearRect(0, 0, AWC, AHC);
- bgFrames.length = 0;
- refresh(); log('已清空');
- };
- refresh();
- }
- function btn() {
- return 'background:#1d3326;color:#a9ffc6;border:1px solid #2e7d4f;border-radius:5px;padding:5px 6px;cursor:pointer;font:inherit';
- }
- // ---------------- 挂载 ----------------
- function tryAttach() {
- if (window.__emu && window.__emu.video && window.__emu.video.objBuffer) {
- emu = window.__emu;
- initAtlas();
- buildUI();
- log('已连接模拟器,可以开始了');
- startLoop();
- }
- }
- // 暴露给控制台
- window.__WOF = {
- collect: () => collectOnce(),
- bg: () => saveBG(),
- dumpIdx: () => JSON.stringify(idx),
- counts: () => ({ sprites: idx.length, bg: bgFrames.length, frame: emu && emu.frameCount }),
- zipTest: () => {
- const b = makeZip([{ name: 'a.txt', bytes: textToBytes('hello') }, { name: 'b/x.png', bytes: textToBytes('y') }]);
- return b.size; // 期望 204
- },
- zipSprites: (limit) => {
- const all = sliceSprites();
- return makeZip(limit ? all.slice(0, limit) : all).size;
- },
- runZip: (limit) => {
- const all = sliceSprites();
- const files = limit ? all.slice(0, limit) : all;
- download(makeZip(files), `wof_test_${files.length}.zip`);
- return files.length;
- }
- };
- const t = setInterval(() => {
- tryAttach();
- if (emu && panel) { clearInterval(t); }
- }, 800);
- setTimeout(buildUI, 3000);
- })();
- //(注:内容由AI生成)
复制代码
|
|