From d136e94e5cc310505e49ea832605a8ce9d067fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E4=BA=91?= Date: Tue, 13 May 2025 20:37:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=BC=80=E6=88=98?= =?UTF-8?q?=E5=89=8D=E7=AD=89=E5=BE=85=EF=BC=8C=E4=BF=AE=E5=A4=8D=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=BB=88=E6=AD=A2=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assets/AutoPath/盗宝团.json | 38 +++++----- .../assets/AutoPath/触发点.json | 10 +-- repo/js/AutoFriendshipTreasureHunters/main.js | 69 +++++++++++++++++-- .../manifest.json | 2 +- .../settings.json | 14 ++-- 5 files changed, 90 insertions(+), 43 deletions(-) diff --git a/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/盗宝团.json b/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/盗宝团.json index 584c1327..1313987f 100644 --- a/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/盗宝团.json +++ b/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/盗宝团.json @@ -1,20 +1,20 @@ { - "info": { - "name": "盗宝团", - "type": "collect", - "author": "HZYgrandma", - "version": "1.0", - "description": "", - "bgi_version": "0.43.4" - }, - "positions": [ - { - "id": 1, - "x": -2756.67, - "y": -3467.63, - "type": "path", - "move_mode": "dash", - "action": "" - } - ] -} \ No newline at end of file + "info": { + "name": "盗宝团", + "type": "collect", + "author": "HZYgrandma", + "version": "1.0", + "description": "", + "bgi_version": "0.43.4" + }, + "positions": [ + { + "id": 1, + "x": -2756.67, + "y": -3467.63, + "type": "path", + "move_mode": "dash", + "action": "" + } + ] + } \ No newline at end of file diff --git a/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/触发点.json b/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/触发点.json index b878a046..38e89dee 100644 --- a/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/触发点.json +++ b/repo/js/AutoFriendshipTreasureHunters/assets/AutoPath/触发点.json @@ -1,6 +1,6 @@ { "info": { - "name": "盗宝团", + "name": "触发点", "type": "collect", "author": "HZYgrandma", "version": "1.0", @@ -24,14 +24,6 @@ "move_mode": "walk", "action": "exit_and_relogin" }, - { - "id": 3, - "x": -2739.71, - "y": -3399.06, - "type": "path", - "move_mode": "dash", - "action": "" - }, { "id": 4, "x": -2738.67, diff --git a/repo/js/AutoFriendshipTreasureHunters/main.js b/repo/js/AutoFriendshipTreasureHunters/main.js index 447d6767..8f91cbbc 100644 --- a/repo/js/AutoFriendshipTreasureHunters/main.js +++ b/repo/js/AutoFriendshipTreasureHunters/main.js @@ -2,7 +2,7 @@ const DEFAULT_RUNS = 10; const DEFAULT_PERIOD = 25; const DEFAULT_BASE_RUNS = 50; const BENCHMARK_HOUR = "T04:00:00"; -const DEFAULT_OCR_TIMEOUT_SECONDS = 10; +const DEFAULT_OCR_TIMEOUT_SECONDS = 30; const DEFAULT_FIGHT_TIMEOUT_SECONDS = 120; (async function () { @@ -77,7 +77,6 @@ async function AutoPath(locationName) { } catch (error) { log.error(`执行 ${locationName} 路径时发生错误: ${error.message}`); } - await sleep(2000); } // 计算运行时长 @@ -106,6 +105,10 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) { let startFirstTime = Date.now(); for (let i = 0; i < times; i++) { await AutoPath('触发点'); + // 启动路径导航任务 + let pathTaskPromise = AutoPath('盗宝团'); + + // OCR检测 let ocrStatus = false; let ocrStartTime = Date.now(); while (Date.now() - ocrStartTime < ocrTimeout * 1000 && !ocrStatus) { @@ -119,16 +122,54 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) { || res.text.includes("鬼鬼祟祟") || res.text.includes("盗宝团")) { ocrStatus = true; + log.info("检测到突发任务触发"); break; } + } + if (!ocrStatus) { await sleep(1000); } } + if(ocrStatus){ - log.info("检测到突发任务触发") - await AutoPath('盗宝团'); + const cts = new CancellationTokenSource(); try { - const cts = new CancellationTokenSource(); + // 设置最大等待时间为15秒 + const maxWaitTime = 15000; + const waitStartTime = Date.now(); + + // 校验距离,如果距离小于10米,则认为已经到达目的地 + const targetX = -2756.67; + const targetY = -3467.63; + const maxDistance = 10; // 10米距离判定 + + // 等待角色到达指定位置附近 + let isNearTarget = false; + let pathTaskFinished = false; + + // 简单监控路径任务完成 + pathTaskPromise.then(() => { + pathTaskFinished = true; + log.info("路径任务已完成"); + }).catch(error => { + pathTaskFinished = true; + log.error(`路径任务出错: ${error}`); + }); + + // 等待角色到达目标位置或超时 + while (!isNearTarget && !pathTaskFinished && (Date.now() - waitStartTime < maxWaitTime)) { + const pos = genshin.getPositionFromMap(); + if (pos) { + const distance = Math.sqrt(Math.pow(pos.x - targetX, 2) + Math.pow(pos.y - targetY, 2)); + if (distance <= maxDistance) { + isNearTarget = true; + log.info(`已到达目标点附近,距离: ${distance.toFixed(2)}米`); + break; + } + } + await sleep(1000); + } + log.info("开始战斗..."); const battleTask = dispatcher.RunTask(new SoloTask("AutoFight"), cts); @@ -136,6 +177,7 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) { log.info(`战斗任务已结束,战斗结果:${fightResult}`); cts.cancel(); } catch (error) { + cts.cancel(); log.error(`执行过程中出错: ${error}`); } const estimatedCompletion = CalculateEstimatedCompletion(startFirstTime, i + 1, times); @@ -148,7 +190,6 @@ async function AutoFriendshipDev(times, ocrTimeout, fightTimeout) { log.info(`未识别到突发任务(岛上无贼),盗宝团好感结束`); break; } - } log.info('盗宝团好感已完成'); } @@ -207,36 +248,46 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000) { const failureKeywords = ["失败"]; const eventKeyword = ["岛上", "无贼","消灭","鬼鬼祟祟","盗宝团"]; let notFind = 0; + while (Date.now() - fightStartTime < timeout) { try { + // 简化OCR检测,只使用一个try-catch块 let result = captureGameRegion().find(RecognitionObject.ocr(850, 150, 200, 80)); let result2 = captureGameRegion().find(RecognitionObject.ocr(0, 200, 300, 300)); let text = result.text; let text2 = result2.text; + + // 检查成功关键词 for (let keyword of successKeywords) { if (text.includes(keyword)) { log.info("检测到战斗成功关键词: {0}", keyword); return true; } } + + // 检查失败关键词 for (let keyword of failureKeywords) { if (text.includes(keyword)) { log.warn("检测到战斗失败关键词: {0}", keyword); return false; } } + + // 检查事件关键词 let find = 0; for(let keyword of eventKeyword) { if (text2.includes(keyword)) { find++; } } + if(find === 0) { notFind++; log.info("未检测到任务触发关键词:{0} 次", notFind); }else{ notFind = 0; } + if (notFind > 10) { log.warn("不在任务触发区域,战斗失败"); return false; @@ -244,9 +295,13 @@ async function waitForBattleResult(timeout = 2 * 60 * 1000) { } catch (error) { log.error("OCR过程中出错: {0}", error); + // 出错后继续循环,不进行额外嵌套处理 } - await sleep(1000); // 检查间隔 + + // 统一的检查间隔 + await sleep(1000); } + log.warn("在超时时间内未检测到战斗结果"); return false; } diff --git a/repo/js/AutoFriendshipTreasureHunters/manifest.json b/repo/js/AutoFriendshipTreasureHunters/manifest.json index ebc5ed7f..13a6c53b 100644 --- a/repo/js/AutoFriendshipTreasureHunters/manifest.json +++ b/repo/js/AutoFriendshipTreasureHunters/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 1, "name": "盗宝团好感:自动好感度&刷盗宝团&卡时间", - "version": "1.2.1", + "version": "1.2.2", "tags": ["好感", "盗宝团", "突发事件"], "description": "通过突发事件【岛上无贼】刷好感度,刷盗宝团材料,小怪锄地,卡时间等,请配合战斗脚本使用。在 HZYgrandma & 愚溪的原始脚本上改编。当前版本可能无法以任意方式拾取掉落物,如果有拾取掉落物需求,请勿更新。", "authors": [ diff --git a/repo/js/AutoFriendshipTreasureHunters/settings.json b/repo/js/AutoFriendshipTreasureHunters/settings.json index 3d1439f0..489652e3 100644 --- a/repo/js/AutoFriendshipTreasureHunters/settings.json +++ b/repo/js/AutoFriendshipTreasureHunters/settings.json @@ -2,7 +2,7 @@ { "name": "qiuQiuRen", "type": "checkbox", - "label": "是否清理丘丘人【默认否,选是会清理丘丘人之后再开始盗宝团好感】" + "label": "是否清理丘丘人\n【默认否,选是会清理丘丘人之后再开始盗宝团好感】" }, { "name": "partyName", @@ -12,31 +12,31 @@ { "name": "runTimes", "type": "input-text", - "label": "(选填)运行次数【默认10次,卡时间模式默认50次】" + "label": "(选填)运行次数\n【默认10次,卡时间模式默认50次】" }, { "name": "waitTimeMode", "type": "checkbox", - "label": "卡时间模式【基于运行次数、基准日期、周期确定需要运行次数】" + "label": "卡时间模式\n【基于运行次数、基准日期、周期确定需要运行次数】" }, { "name": "waitTimeModeDay", "type": "input-text", - "label": "卡时间模式基准日期【格式参考:2025-01-01】" + "label": "卡时间模式基准日期\n【格式参考:2025-01-01】" }, { "name": "waitTimeModePeriod", "type": "input-text", - "label": "(选填)卡时间模式周期【默认:25天】" + "label": "(选填)卡时间模式周期\n【默认:25天】" }, { "name": "ocrTimeout", "type": "input-text", - "label": "OCR超时时间(选填,默认为10秒)" + "label": "OCR超时时间\n【选填,默认为30秒,如果经常提前判定未识别到任务,请适当调大】" }, { "name": "fightTimeOut", "type": "input-text", - "label": "战斗超时时间(选填,默认为120秒)" + "label": "战斗超时时间\n【选填,默认为120秒】" } ] \ No newline at end of file