js,OCR购买食材 (#508)

This commit is contained in:
JJMdzh
2025-04-01 21:20:24 +08:00
committed by GitHub
parent cb470f178b
commit 97f3b6522c
15 changed files with 796 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
{
"info": {
"name": "咖啡厅露泽店主阿鲁埃",
"type": "collect",
"author": "吉吉喵",
"version": "1.0",
"description": "",
"bgiVersion": "0.42.3"
},
"positions": [
{
"id": 1,
"x": 4649.4873046875,
"y": 3467.8935546875,
"action": "",
"move_mode": "walk",
"action_params": "",
"type": "teleport"
},
{
"id": 2,
"x": 4627.564453125,
"y": 3470.357421875,
"action": "",
"move_mode": "walk",
"action_params": "",
"type": "path"
},
{
"id": 3,
"x": 4604.40869140625,
"y": 3510.25,
"action": "stop_flying",
"move_mode": "fly",
"action_params": "",
"type": "path"
},
{
"id": 4,
"x": 4600.888671875,
"y": 3514.0810546875,
"action": "",
"move_mode": "walk",
"action_params": "",
"type": "target",
"locked": false
},
{
"id": 5,
"x": 4599.888671875,
"y": 3516.081054687501,
"action": "",
"move_mode": "walk",
"action_params": "",
"type": "orientation"
}
]
}

View File

@@ -0,0 +1,40 @@
{
"info": {
"name": "达莫维百货店主布希柯",
"type": "collect",
"author": "吉吉喵",
"version": "1.0",
"description": "达莫维百货 店主布希柯",
"bgiVersion": "0.35.1"
},
"positions": [
{
"id": 1,
"action": "",
"move_mode": "walk",
"type": "teleport",
"x": 4514.18,
"y": 3630.4,
"action_params": "",
"locked": false
},
{
"id": 2,
"x": 4514.2802734375,
"y": 3604.35498046875,
"type": "path",
"move_mode": "fly",
"action": "stop_flying",
"action_params": ""
},
{
"id": 3,
"x": 4470.1357421875,
"y": 3562.072265625,
"type": "path",
"move_mode": "fly",
"action": "",
"action_params": ""
}
]
}

View File

@@ -0,0 +1,3 @@
{"macroEvents":[{"type":6,"mouseX":0,"mouseY":-120,"time":0},
{"type":6,"mouseX":0,"mouseY":0,"time":100}],
"info":{"name":"","description":"","x":0,"y":0,"width":1920,"height":1080,"recordDpi":1}}

View File

@@ -0,0 +1,37 @@
{
"info": {
"name": "璃月万民堂老板卯师傅",
"type": "collect",
"author": "吉吉喵",
"version": "",
"description": "璃月万民堂老板卯师傅",
"bgiVersion": "0.42.3"
},
"positions": [
{
"id": 1,
"x": 267.9150390625,
"y": -665.1591796875,
"action": "",
"move_mode": "walk",
"type": "teleport",
"locked": false
},
{
"id": 2,
"x": 228.953125,
"y": -663.4853515625,
"action": "",
"move_mode": "walk",
"type": "path"
},
{
"id": 3,
"x": 227.083984375,
"y": -668.10791015625,
"action": "",
"move_mode": "walk",
"type": "target"
}
]
}

View File

@@ -0,0 +1,55 @@
{
"info": {
"name": "璃月万民堂老板卯师傅",
"type": "collect",
"author": "吉吉喵",
"version": "",
"description": "璃月万民堂老板卯师傅备选",
"bgiVersion": "0.42.3"
},
"positions": [
{
"id": 1,
"x": 267.9150390625,
"y": -665.1591796875,
"action": "",
"move_mode": "walk",
"type": "teleport",
"locked": false
},
{
"id": 2,
"x": 232.9345703125,
"y": -663.775390625,
"action": "",
"move_mode": "walk",
"type": "path"
},
{
"id": 3,
"x": 231.1435546875,
"y": -671.79248046875,
"action": "",
"move_mode": "walk",
"type": "target",
"locked": false
},
{
"id": 4,
"x": 226.2607421875,
"y": -672.33740234375,
"action": "",
"move_mode": "walk",
"type": "target",
"locked": false
},
{
"id": 5,
"x": 227.591796875,
"y": -671.0654296875,
"action": "",
"move_mode": "walk",
"type": "target"
}
]
}

View File

@@ -0,0 +1,28 @@
{
"info": {
"name": "璃月杂货商东升",
"type": "collect",
"author": "吉吉喵",
"version": "1.0",
"description": "购买杂货",
"bgiVersion": "0.42.0"
},
"positions": [
{
"id": 1,
"action": "force_tp",
"move_mode": "walk",
"type": "teleport",
"x": 267.92,
"y": -665.01
},
{
"id": 2,
"x": 257.6142578125,
"y": -681.96826171875,
"type": "path",
"move_mode": "walk",
"action": ""
}
]
}

View File

@@ -0,0 +1,36 @@
{
"info": {
"name": "稻妻九十九物店主葵",
"type": "collect",
"author": "吉吉喵",
"version": "1.0",
"description": "购买杂货",
"bgiVersion": "0.42.0"
},
"positions": [
{
"id": 1,
"action": "force_tp",
"move_mode": "walk",
"type": "teleport",
"x": -4402.56,
"y": -3052.88
},
{
"id": 2,
"x": -4409.03125,
"y": -3067.2666015625,
"type": "path",
"move_mode": "walk",
"action": ""
},
{
"id": 3,
"x": -4403.646484375,
"y": -3072.0263671875,
"type": "path",
"move_mode": "walk",
"action": ""
}
]
}

View File

@@ -0,0 +1,44 @@
{
"info": {
"name": "稻妻志村屋店主志村堪兵卫",
"type": "collect",
"author": "吉吉喵",
"version": "1.0",
"description": "购买鱼肉+螃蟹",
"bgiVersion": "0.42.0"
},
"positions": [
{
"id": 1,
"action": "force_tp",
"move_mode": "walk",
"type": "teleport",
"x": -4402.56,
"y": -3052.88
},
{
"id": 2,
"x": -4409.03125,
"y": -3067.2666015625,
"type": "path",
"move_mode": "walk",
"action": ""
},
{
"id": 3,
"x": -4421.849609375,
"y": -3084.564453125,
"type": "path",
"move_mode": "walk",
"action": ""
},
{
"id": 4,
"x": -4425.07421875,
"y": -3082.072265625,
"type": "target",
"move_mode": "walk",
"action": ""
}
]
}

View File

@@ -0,0 +1,36 @@
{
"info": {
"name": "蒙德杂货商布兰琪",
"type": "collect",
"author": "吉吉喵",
"version": "1.0",
"description": "蒙德杂货商布兰琪",
"bgiVersion": "0.42.3"
},
"positions": [
{
"id": 1,
"x": -867.693359375,
"y": 2281.393310546875,
"action": "",
"move_mode": "walk",
"type": "teleport"
},
{
"id": 2,
"x": -886.5966796875,
"y": 2259.00244140625,
"action": "",
"move_mode": "walk",
"type": "path"
},
{
"id": 3,
"x": -894.9638671875,
"y": 2264.875244140625,
"action": "",
"move_mode": "walk",
"type": "target"
}
]
}

View File

@@ -0,0 +1 @@
{"info":{"name":"","type":""},"positions":[{"x":2788.5654296875,"y":-506.36279296875,"type":"teleport","move_mode":"walk"},{"x":2792.4580078125,"y":-519.3681640625,"type":"path","move_mode":"walk"},{"x":2799.9716796875,"y":-520.26806640625,"type":"path","move_mode":"walk"},{"x":2810.134765625,"y":-511.20947265625,"type":"path","move_mode":"walk"}]}

View File

@@ -0,0 +1,28 @@
{
"info": {
"name": "须弥奥摩斯港鱼贩布特罗斯",
"type": "collect",
"author": "吉吉喵",
"version": "",
"description": "须弥奥摩斯港鱼贩布特罗斯",
"bgiVersion": "0.42.3"
},
"positions": [
{
"id": 1,
"x": 2679.8427734375,
"y": -1935.0341796875,
"action": "",
"move_mode": "walk",
"type": "teleport"
},
{
"id": 2,
"x": 2658.712890625,
"y": -1930.0478515625,
"action": "",
"move_mode": "walk",
"type": "target"
}
]
}

View File

@@ -0,0 +1 @@
{"info":{"name":"","type":""},"positions":[{"x":4095.98828125,"y":-2025.9638671875,"type":"teleport","move_mode":"walk"},{"x":4099.64013671875,"y":-2010.21337890625,"type":"path","move_mode":"walk"},{"x":4095.404296875,"y":-1997.890625,"type":"path","move_mode":"walk"},{"x":4099.6630859375,"y":-1978.88427734375,"type":"path","move_mode":"walk"},{"x":4091.326171875,"y":-1980.173828125,"type":"path","move_mode":"walk"}]}

View File

@@ -0,0 +1,324 @@
// 定义所有食材及其对应的路径文件和 NPC
const mondstadtGroceryFilePath = `assets/蒙德杂货商布兰琪.json`;
const liyueGroceryFilePath = `assets/璃月杂货商东升.json`;
const liyueWanminFilePath = `assets/璃月万民堂老板卯师傅.json`;
const groceryFilePath = `assets/稻妻九十九物店主葵.json`;
const charcoalFilePath = `assets/稻妻志村屋店主志村勘兵卫.json`;
const fengdanGroceryFilePath = `assets/枫丹杂货商布希柯.json`;
const cafeLuzheFilePath = `assets/咖啡厅露泽店主阿鲁埃.json`;
const sumiCityFishPath = `assets/须弥城鱼贩珀姆.json`;
const omosPortFishPath = `assets/须弥奥摩斯港鱼贩布特罗斯.json`;
const azaleVillMerPath = `assets/须弥阿如村商人阿扎莱.json`;
// 定义所有可能的食材,注意料理名字长度可能超过识图范围
const ingredients = [
"枫达", "盐", "洋葱", "牛奶", "番茄", "卷心菜", "土豆", "小麦", "胡椒","稻米", "豆腐", "杏仁", "鱼肉", "螃蟹", "虾仁", "咖啡豆", "秃秃豆", "发酵果实汁"
];
// 筛选出用户选择的食材及其对应的路径文件和 NPC
const selectedIngredients = []; // 在函数外部声明一次
const selectedPaths = new Map();
const ingredientPaths = {
"枫达": [fengdanGroceryFilePath, cafeLuzheFilePath],
"盐": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"洋葱": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"牛奶": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"番茄": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"卷心菜": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"土豆": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"小麦": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"胡椒": [mondstadtGroceryFilePath, liyueGroceryFilePath, groceryFilePath, fengdanGroceryFilePath],
"稻米": [liyueGroceryFilePath, groceryFilePath],
"虾仁": [liyueGroceryFilePath, groceryFilePath, sumiCityFishPath, omosPortFishPath],
"豆腐": [liyueGroceryFilePath, groceryFilePath],
"杏仁": [liyueGroceryFilePath, fengdanGroceryFilePath],
"鱼肉": [liyueWanminFilePath, charcoalFilePath, sumiCityFishPath, omosPortFishPath, azaleVillMerPath],
"螃蟹": [liyueWanminFilePath, charcoalFilePath, sumiCityFishPath, omosPortFishPath],
"秃秃豆": [fengdanGroceryFilePath, azaleVillMerPath],
"咖啡豆": [cafeLuzheFilePath],
"发酵果实汁": [fengdanGroceryFilePath]
};
// 定义所有NPC名注意名字长度可能超过识图范围
const npcNames = {
[mondstadtGroceryFilePath]: ["布兰琪"],
[liyueGroceryFilePath]: ["东升"],
[liyueWanminFilePath]: ["卵师傅", "卯师傅"],
[groceryFilePath]: ["葵"],
[charcoalFilePath]: ["志村勘"],
[fengdanGroceryFilePath]: ["布希柯"],
[cafeLuzheFilePath]: ["阿鲁埃"],
[sumiCityFishPath]: ["珀姆"],
[omosPortFishPath]: ["布特罗斯"],
[azaleVillMerPath]: ["阿扎莱"]
};
for (const ingredient of ingredients) {
if (settings[ingredient]) {
selectedIngredients.push(ingredient);
ingredientPaths[ingredient].forEach(path => {
if (!selectedPaths.has(path)) {
selectedPaths.set(path, []);
}
selectedPaths.get(path).push(ingredient);
});
}
}
if (selectedIngredients.length === 0) {
log.error("未选择任何食材,退出任务");
throw new Error("未选择任何食材,任务终止"); // 抛出异常以终止任务
}
// 汇总即将购买的食材信息
const purchaseSummary = selectedIngredients.join(", ");
log.info(`即将购买: ${purchaseSummary}`);
// 定义一个函数用于模拟按键操作
async function simulateKeyOperations(key, duration) {
keyDown(key);
await sleep(duration);
keyUp(key);
await sleep(500); // 释放按键后等待 500 毫秒
}
// 定义一个函数用于识别并点击用户选择的食材
async function clickSelectedIngredients(selectedIngredients, filePath, npcNames) {
log.info(`加载路径文件: ${filePath}`);
await pathingScript.runFile(filePath);
await sleep(1000);
log.info("路径文件执行完成");
// 识别并交互 NPC
let attempts = 0;
const maxAttempts = 5; // 最大尝试次数
const npcxRange = { min: 1190, max: 1320 }; // X轴固定区间
const npcyRanges = [478, 514, 552]; // 可能的Y轴坐标
const tolerance = 10; // 容错区间
const npctolerance = 10; // 容错区间
let npcOcrResult = { success: false }; // 初始化 npcOcrResult
// 判断 Y 坐标是否在容错范围内
function isYWithinTolerance(y, targetY, tolerance) {
return y >= targetY - tolerance && y <= targetY + tolerance;
}
// 执行点击操作
async function performClickOperations(filePath) {
if (filePath === liyueGroceryFilePath || filePath === groceryFilePath || filePath === sumiCityFishPath) {
log.info("执行璃月杂货商或稻妻九十九物店主的点击操作");
await click(1300, 650); await sleep(1000);
await click(1300, 650); await sleep(1000);
await click(1600, 1020); await sleep(1000);
} else {
log.info("执行其他路径文件的点击操作");
await click(1300, 580); await sleep(1000);
await click(1300, 580); await sleep(1000);
await click(1600, 1020); await sleep(1000);
}
}
while (attempts < maxAttempts) {
attempts++;
log.info(`尝试识别 NPC尝试次数: ${attempts}`);
for (const npcName of npcNames) {
log.info(`尝试识别 NPC: ${npcName}`);
npcOcrResult = await performOcr(npcName, npcxRange, { min: 470, max: 602 }, tolerance);
if (npcOcrResult.success) {
if (
isYWithinTolerance(npcOcrResult.y, 478, npctolerance) ||
isYWithinTolerance(npcOcrResult.y, 514, npctolerance)
) {
// 如果 Y 坐标在 478 或 514 的容错范围内,直接按下 F 键
log.info(`直接按下 F 键与 NPC ${npcName} 交互...`);
keyPress("F");
await sleep(2000);
// 执行点击操作
await performClickOperations(filePath);
break; // 成功交互,退出内层循环
} else if (isYWithinTolerance(npcOcrResult.y, 552, npctolerance)) {
// 如果 Y 坐标在 552 的容错范围内,调整当前路径
log.info(`Y 坐标在 552 的容错范围内,调整当前路径`);
await simulateKeyOperations("S", 600); // 后退 600 毫秒
await simulateKeyOperations("W", 800); // 前进 800 毫秒
} else {
log.error(`识别到的 Y 坐标 ${npcOcrResult.y} 不在预期范围内,尝试次数: ${attempts}`);
}
break; // 成功识别到 NPC退出内层循环
} else {
if (attempts === 1) {
// 第一次失败时,尝试调整位置
await simulateKeyOperations("S", 600); // 后退 600 毫秒
await simulateKeyOperations("W", 800); // 前进 800 毫秒
} else if (attempts === 2) {
// 第二次失败时,重新加载路径文件
log.info("重新加载路径文件");
await pathingScript.runFile(filePath);
await sleep(1000);
}
log.error(`OCR 识别未找到 NPC: ${npcName},尝试次数: ${attempts}`);
}
}
if (npcOcrResult.success) {
break; // 成功识别到 NPC退出外层循环
}
}
if (attempts >= maxAttempts) {
log.error(`识别 NPC 失败 ${maxAttempts} 次,放弃该路线`);
return; // 放弃该路线
}
// 记录已购买的食材
const purchasedIngredients = new Set();
// 继续后续操作
const ingredientXRange = { min: 220, max: 390 }; // X坐标范围
const ingredientYRange = { min: 95, max: 950 }; // Y坐标范围
const ingredientTolerance = 10; // 容错区间
const clickOffset = 30; // 点击坐标容错
let allIngredientsFound = false; // 标记是否所有食材都已找到
let scrollAttempts = 0;
const maxScrollAttempts = 3; // 最大翻页次数
while (!allIngredientsFound && scrollAttempts < maxScrollAttempts) {
allIngredientsFound = true; // 假设本轮所有食材都已找到,若后续发现未找到则修改为 false
for (const ingredient of selectedIngredients) {
if (purchasedIngredients.has(ingredient)) {
log.info(`跳过已购买的食材: ${ingredient}`);
continue; // 跳过已购买的食材
}
const ocrResult = await performOcr(ingredient, ingredientXRange, ingredientYRange, ingredientTolerance);
if (ocrResult.success) {
log.info(`识别到 '${ingredient}',坐标: x=${ocrResult.x}, y=${ocrResult.y}`);
await click(ocrResult.x, ocrResult.y + clickOffset);
await sleep(1000);
// 模拟购买操作的后续点击
await click(1600, 1020); await sleep(1000); // 购买
await click(1181, 600); await sleep(200); // 选择100个
await click(1320, 780); await sleep(1000); // 最终确认
await click(1320, 780); await sleep(1000); // 点击空白
// 记录已购买的食材
purchasedIngredients.add(ingredient);
} else {
log.error(`OCR 识别未找到 '${ingredient}'`);
allIngredientsFound = false; // 本轮有食材未找到
}
}
if (!allIngredientsFound) {
log.info(`在当前页面未找到所有食材,尝试翻页`);
await PageScroll(1); // 每轮翻页滑动1次
await sleep(600);
scrollAttempts++;
}
}
if (!allIngredientsFound) {
log.error(`在所有页面中未找到所有食材,跳过该路径`);
}
// 最后点击退出按钮
log.info("点击退出按钮...");
await click(1845, 45); // 退出
await sleep(2000);
}
// 自动执行划页操作
async function PageScroll(scrollCount) {
try {
const clickX = 1200; // 假设点击的起始坐标
const clickY = 900;
const totalDistance = 500; // 假设每次滑动的总距离
const stepDistance = 15; // 每步移动的距离
for (let i = 0; i < scrollCount; ++i) {
log.info(`开始第 ${i + 1} 次滑动`);
// 如果点击坐标为 (0, 0),则跳过点击
if (clickX !== 0 || clickY !== 0) {
moveMouseTo(clickX, clickY); // 移动到指定坐标
await sleep(100);
}
// 按住鼠标左键
leftButtonDown();
// 将鼠标移动到目标位置,模拟更自然的拖动操作
log.info("移动鼠标");
const steps = totalDistance / stepDistance; // 分成若干步移动
for (let j = 0; j < steps; j++) {
moveMouseBy(0, -stepDistance); // 每次移动 stepDistance 像素
await sleep(10); // 每次移动后延迟10毫秒
}
// 释放鼠标左键
await sleep(700);
leftButtonUp();
await sleep(100);
}
} catch (error) {
log.error(`执行划页操作时发生错误:${error.message}`);
}
}
// 定义一个函数用于执行OCR识别
async function performOcr(targetText, xRange, yRange, tolerance) {
// 调整区域范围以包含容错区间
const adjustedXMin = xRange.min - tolerance;
const adjustedXMax = xRange.max + tolerance;
const adjustedYMin = yRange.min - tolerance;
const adjustedYMax = yRange.max + tolerance;
/*/ log.info(`
adjustedXMin: ${adjustedXMin}
adjustedXMax: ${adjustedXMax}
adjustedYMin: ${adjustedYMin}
adjustedYMax: ${adjustedYMax}
`);*/
// 在捕获的区域内进行OCR识别
const ra = captureGameRegion();
const resList = ra.findMulti(RecognitionObject.ocr(
adjustedXMin, adjustedYMin,
adjustedXMax - adjustedXMin, adjustedYMax - adjustedYMin
));
// log.info(`OCR 识别数量: ${resList.count}`);
// 遍历识别结果,检查是否找到目标文本
for (let i = 0; i < resList.count; i++) {
if (resList[i].text.includes(targetText)) {
// 如果找到目标文本,直接返回坐标
return { success: true, x: resList[i].x, y: resList[i].y }; // 找到符合条件的文本,返回坐标
}
}
return { success: false }; // 未找到符合条件的文本
}
// 主函数
async function AutoPath() {
log.info("开始执行自动寻路任务");
// 加载路径文件和 NPC 名称
for (const [path, ingredients] of selectedPaths) {
const npcName = npcNames[path];
await clickSelectedIngredients(ingredients, path, npcName);
}
}
// 执行主函数
(async function () {
setGameMetrics(1920, 1080, 1);
await genshin.returnMainUi();
await AutoPath();
})();

View File

@@ -0,0 +1,13 @@
{
"manifest_version": 1,
"name": "选择购买食材OCR",
"version": "1.20401",
"description": "寻路杂货商更新OCR版对NPC和材料文字识别可自定义购买食材。一般食材杂货在蒙德、璃月、稻妻、枫丹杂货商购买10鱼肉、10螃蟹在卯师傅、珀姆、布特罗斯、阿扎莱和志村勘兵卫购买。",
"authors": [
{
"name": "吉吉喵"
}
],
"settings_ui": "settings.json",
"main": "main.js"
}

View File

@@ -0,0 +1,92 @@
[
{
"name": "咖啡豆",
"type": "checkbox",
"label": "选择食材:\n============\n咖啡豆"
},
{
"name": "秃秃豆",
"type": "checkbox",
"label": "-----------------\n秃秃豆"
},
{
"name": "枫达",
"type": "checkbox",
"label": "-----------------\n枫达"
},
{
"name": "发酵果实汁",
"type": "checkbox",
"label": "-----------------\n发酵果实汁"
},
{
"name": "鱼肉",
"type": "checkbox",
"label": "-----------------\n鱼肉"
},
{
"name": "螃蟹",
"type": "checkbox",
"label": "-----------------\n螃蟹"
},
{
"name": "稻米",
"type": "checkbox",
"label": "-----------------\n稻米"
},
{
"name": "虾仁",
"type": "checkbox",
"label": "-----------------\n虾仁"
},
{
"name": "豆腐",
"type": "checkbox",
"label": "-----------------\n豆腐"
},
{
"name": "杏仁",
"type": "checkbox",
"label": "-----------------\n杏仁"
},
{
"name": "盐",
"type": "checkbox",
"label": "-----------------\n盐"
},
{
"name": "洋葱",
"type": "checkbox",
"label": "-----------------\n洋葱"
},
{
"name": "牛奶",
"type": "checkbox",
"label": "-----------------\n牛奶"
},
{
"name": "番茄",
"type": "checkbox",
"label": "-----------------\n番茄"
},
{
"name": "卷心菜",
"type": "checkbox",
"label": "-----------------\n卷心菜"
},
{
"name": "土豆",
"type": "checkbox",
"label": "-----------------\n土豆"
},
{
"name": "小麦",
"type": "checkbox",
"label": "-----------------\n小麦"
},
{
"name": "胡椒",
"type": "checkbox",
"label": "-----------------\n胡椒"
}
]