feat: 新的数据处理方式

This commit is contained in:
AncTe committed 2025-04-28 22:51:53 +08:00
1 parent f8d6160e5f
commit afde7be592
8 files changed
+8830 -58

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import { readFile, writeFile } from 'fs-extra';
import { GinkaDataset } from './types';
import { chooseFrom } from './utils';
const [outputTrain, outputEval, input, ratioStr] = process.argv.slice(2);
const ratio = parseFloat(ratioStr);
(async () => {
const data = await readFile(input, 'utf-8');
const dataJSON = JSON.parse(data) as GinkaDataset;
const keys = Object.keys(dataJSON.data);
const length = keys.length;
const toEval = chooseFrom(keys, Math.floor(length * ratio));
const toTrain = [...new Set(keys).difference(new Set(toEval))];
const trainData: GinkaDataset = {
datasetId: Math.floor(Math.random() * 1e12),
data: {}
};
toTrain.forEach(v => {
trainData.data[v] = dataJSON.data[v];
});
const evalData: GinkaDataset = {
datasetId: Math.floor(Math.random() * 1e12),
data: {}
};
toEval.forEach(v => {
evalData.data[v] = dataJSON.data[v];
});
await writeFile(outputTrain, JSON.stringify(trainData), 'utf-8');
await writeFile(outputEval, JSON.stringify(evalData), 'utf-8');
})();
+60 -36
View File
@@ -22,6 +22,7 @@ const numMap: Record<number, number> = {
};
export interface Enemy {
num: number;
hp: number;
atk: number;
def: number;
@@ -44,41 +45,7 @@ function convert(
clipped.push(row);
}
// 2. 转换怪物
const enemySet = new Set<Enemy>();
for (let nx = 0; nx < w; nx++) {
for (let ny = 0; ny < h; ny++) {
const tile = clipped[ny][nx];
if (tile === 201 || tile === 202 || tile === 203) continue;
const enemy = enemyMap[tile];
if (!enemy) continue;
enemySet.add(enemy);
}
}
const attrs = [...enemySet].map(v => (v.atk + v.def) * v.hp);
const maxAttr = Math.max(...attrs);
const minAttr = Math.min(...attrs);
const delta = maxAttr - minAttr;
for (let ny = 0; ny < w; ny++) {
for (let nx = 0; nx < h; nx++) {
const tile = clipped[ny][nx];
if (tile === 201 || tile === 202 || tile === 203) continue;
const enemy = enemyMap[tile];
if (!enemy) continue;
// 替换为弱怪/中怪/强怪
const attr = (enemy.atk + enemy.def) * enemy.hp;
const ad = attr - minAttr;
if (ad < delta / 3) {
clipped[ny][nx] = 7;
} else if (ad < (delta * 2) / 3) {
clipped[ny][nx] = 8;
} else {
clipped[ny][nx] = 9;
}
}
}
// 3. 转换一般图块
// 2. 转换一般图块
const mapping: Record<number, number> = {};
config.mapping.wall.forEach(v => (mapping[v] = 1));
config.mapping.key.forEach(v => (mapping[v] = 2));
@@ -88,12 +55,69 @@ function convert(
config.mapping.door.forEach(v => (mapping[v] = 6));
config.mapping.item.forEach(v => (mapping[v] = 12));
config.mapping.greenGem.forEach(v => (mapping[v] = 13));
const yellowGem = new Set(config.mapping.yellowGem);
for (let nx = 0; nx < w; nx++) {
for (let ny = 0; ny < h; ny++) {
const tile = clipped[ny][nx];
const enemy = enemyMap[tile];
if (yellowGem.has(tile)) {
const rand = Math.random();
if (rand < 2 / 5) {
clipped[ny][nx] = 3;
} else if (rand < 4 / 5) {
clipped[ny][nx] = 4;
} else {
clipped[ny][nx] = 13;
}
continue;
}
if (mapping[tile]) clipped[ny][nx] = mapping[tile];
else if (numMap[tile]) clipped[ny][nx] = numMap[tile];
else clipped[ny][nx] = 0;
else if (!enemy) clipped[ny][nx] = 0;
}
}
// 3. 转换怪物
const enemySet = new Set<Enemy>();
for (let nx = 0; nx < w; nx++) {
for (let ny = 0; ny < h; ny++) {
const tile = clipped[ny][nx];
const enemy = enemyMap[tile];
if (!enemy) continue;
enemySet.add({ ...enemy, num: tile });
}
}
const enemyArr = [...enemySet];
enemyArr.sort((a, b) => a.num - b.num);
if (
enemyArr.length === 3 &&
enemyArr[0].num === 201 &&
enemyArr[1].num === 202 &&
enemyArr[2].num === 203
) {
// pass
} else {
const attrs = [...enemySet].map(v => (v.atk + v.def) * v.hp);
const maxAttr = Math.max(...attrs);
const minAttr = Math.min(...attrs);
const delta = maxAttr - minAttr;
for (let ny = 0; ny < w; ny++) {
for (let nx = 0; nx < h; nx++) {
const tile = clipped[ny][nx];
if (tile < 32) continue;
const enemy = enemyMap[tile];
if (!enemy) continue;
// 替换为弱怪/中怪/强怪
const attr = (enemy.atk + enemy.def) * enemy.hp;
const ad = attr - minAttr;
if (ad < delta / 3) {
clipped[ny][nx] = 7;
} else if (ad < (delta * 2) / 3) {
clipped[ny][nx] = 8;
} else {
clipped[ny][nx] = 9;
}
}
}
}
+1
View File
@@ -18,6 +18,7 @@ export interface GinkaConfig extends BaseConfig {
redGem: number[];
blueGem: number[];
greenGem: number[];
yellowGem: number[];
item: number[];
potion: number[];
key: number[];
+23 -15
View File
@@ -109,22 +109,30 @@ export async function getAllFloors(...info: TowerInfo[]) {
join(tower.path, 'floors', `${id}.js`),
'utf-8'
);
const data = JSON.parse(
floorFile
.replaceAll("'", '"')
.slice(floorFile.indexOf('=') + 1)
);
const map = data.map as number[][];
// 裁剪地图
const { clip } = tower.config;
const area = clip.special[id] ?? clip.defaults;
try {
const data = JSON.parse(
floorFile
// .replaceAll("'", '"')
.slice(floorFile.indexOf('=') + 1)
);
return convertFloor(
map,
area,
tower.config as GinkaConfig,
enemyNumMap
);
const map = data.map as number[][];
// 裁剪地图
const { clip } = tower.config;
const area = clip.special[id] ?? clip.defaults;
return convertFloor(
map,
area,
tower.config as GinkaConfig,
enemyNumMap
);
} catch (e) {
console.log(
`Error when processing '${tower.name}' '${id}'`
);
throw e;
}
})
);
})