From 18e95970ec5598bb9985f2d6afafe859629ba95c Mon Sep 17 00:00:00 2001 From: Zebartin <1500012724@pku.edu.cn> Date: Sun, 16 Apr 2023 00:48:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A3=80=E6=9F=A5=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=9E=84=E5=87=86&=E7=88=86=E8=A3=82=20#23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NIKKE/NIKKEutils.js | 88 ++++++++++++++++++- "NIKKE/NIKKE\346\227\245\345\270\270.js" | 10 +-- .../\346\250\241\346\213\237\345\256\244.js" | 6 +- utils.js | 15 +++- 4 files changed, 107 insertions(+), 12 deletions(-) diff --git a/NIKKE/NIKKEutils.js b/NIKKE/NIKKEutils.js index 79752bc..9a277dc 100644 --- a/NIKKE/NIKKEutils.js +++ b/NIKKE/NIKKEutils.js @@ -1,9 +1,11 @@ var { ocrUntilFound, clickRect, unlockIfNeed, requestScreenCaptureAuto, getDisplaySize, - killApp, findImageByFeature, findContoursRect + killApp, findImageByFeature, findContoursRect, + rgbToGray } = require('./utils.js'); var firstBoot = true; +var firstCheckAuto = true; if (typeof module === 'undefined') { auto.waitFor(); unlockIfNeed(); @@ -26,7 +28,8 @@ else { mostSimilar: mostSimilar, detectNikkes: detectNikkes, NikkeToday: NikkeToday, - 关闭限时礼包: 关闭限时礼包 + 关闭限时礼包: 关闭限时礼包, + checkAuto: checkAuto, }; } function 启动NIKKE() { @@ -312,6 +315,7 @@ function 刷刷刷() { click(width / 2, height / 2); return null; }, 50, 1000); + checkAuto(); let clickNext = ocrUntilFound(res => { if (!res.text.includes('REWARD')) { sleep(2000); @@ -455,3 +459,83 @@ function detectNikkes(originalImg, options) { console.info(`当前页面:${nikkes.map(x => x.name).join('、')}`); return nikkes; } + +function checkAuto() { + if (!firstCheckAuto) { + log('本次运行已检查自动瞄准&爆裂,不再检查'); + return; + } + sleep(5000); + log('检查自动瞄准&爆裂…'); + let autoBtn = ocrUntilFound((res, img) => res.find(e => + e.text.includes('AUT') && e.bounds != null && + e.bounds.left <= img.width / 2 + ), 40, 1000); + if (autoBtn == null) { + log('未检测到AUTO按钮'); + return; + } + let y = Math.max(autoBtn.bounds.top - autoBtn.bounds.height() * 2, 0); + let w = autoBtn.bounds.right + autoBtn.bounds.width(); + let h = autoBtn.bounds.bottom + autoBtn.bounds.height() * 2 - y; + let res = null, img = null; + for (let i = 0; i < 20; ++i) { + img = captureScreen(); + let c = img.pixel(autoBtn.bounds.right + 5, autoBtn.bounds.top); + let insideGray = Math.round(rgbToGray(c)), outsideGray = 0.0; + for (let tx of [0, w]) + for (let ty of [0, h + y]) + outsideGray += rgbToGray(img.pixel(tx, ty)); + outsideGray = Math.round(outsideGray / 4); + res = findContoursRect(img, { + thresh: random(insideGray, outsideGray), + region: [0, y, w, h], + // debug: true + }).filter(x => + x.height() >= autoBtn.bounds.height() * 2 && + x.top < autoBtn.bounds.top && + x.bottom > autoBtn.bounds.bottom && + x.centerX() < autoBtn.bounds.centerX() + ); + if ( + res.length == 2 && + res[0].right < res[1].left && + Math.abs(res[0].top - res[1].top) < 10 + ) + break; + sleep(500); + } + if (res.length != 2) { + log('自动瞄准&爆裂检查失败'); + log(res); + return; + } + let i = 0; + while (i < 2) { + let btn = res[i]; + let name = '自动' + (i == 0 ? '瞄准' : '爆裂'); + let region = [btn.left, btn.top, btn.width(), btn.height()]; + let redColor = images.findColor(img, '#d65100', { + region: region, + threshold: 80 + }); + let grayColor = images.findColor(img, '#7d8789', { + region: region, + threshold: 50 + }); + i += 1; + if (redColor != null && grayColor == null) + log(`${name}已打开`); + else if (redColor == null && grayColor != null) { + log(`${name}未打开,点击打开`); + clickRect({ bounds: btn }, 0.8, 200); + } else { + log(`${name}状态不定,重试`); + sleep(500); + i -= 1; + img = captureScreen(); + continue; + } + } + firstCheckAuto = false; +} diff --git "a/NIKKE/NIKKE\346\227\245\345\270\270.js" "b/NIKKE/NIKKE\346\227\245\345\270\270.js" index 68b9b59..8486356 100644 --- "a/NIKKE/NIKKE\346\227\245\345\270\270.js" +++ "b/NIKKE/NIKKE\346\227\245\345\270\270.js" @@ -1,7 +1,7 @@ var { 启动NIKKE, 等待NIKKE加载, 退出NIKKE, mostSimilar, 返回首页, 关闭限时礼包, - detectNikkes, NikkeToday + detectNikkes, NikkeToday, checkAuto } = require('./NIKKEutils.js'); var { 模拟室 } = require('./模拟室.js'); var { @@ -458,10 +458,8 @@ function 爬塔() { clickRect(ocrUntilFound(res => res.find(e => e.text.includes('入战')), 30, 1000)); toast('进入战斗'); for (let j = 0; j < cnt; ++j) { - sleep(9000); - if (ocrUntilFound(res => res.text.includes('AUT'), 10, 3000) == null) - break; - sleep(40 * 1000); + checkAuto(); + sleep(20 * 1000); ocrUntilFound(res => { if (res.text.includes('AUT')) { sleep(4000); @@ -469,7 +467,7 @@ function 爬塔() { } if (res.text.includes('REWARD') || res.text.includes('FAIL')) return true; - }, 30, 1000); + }, 30, 2000); sleep(1000); let endCombat = ocrUntilFound(res => res.find( e => e.text.match(/(下[^步方法]{2}|返回)/) != null diff --git "a/NIKKE/\346\250\241\346\213\237\345\256\244.js" "b/NIKKE/\346\250\241\346\213\237\345\256\244.js" index f3c6e51..812003c 100644 --- "a/NIKKE/\346\250\241\346\213\237\345\256\244.js" +++ "b/NIKKE/\346\250\241\346\213\237\345\256\244.js" @@ -6,7 +6,8 @@ var { } = require('./utils.js'); var { 启动NIKKE, 等待NIKKE加载, 退出NIKKE, - 返回首页, mostSimilar, detectNikkes + 返回首页, mostSimilar, detectNikkes, + checkAuto } = require('./NIKKEutils.js'); let width, height; let curPass = 0; @@ -588,7 +589,8 @@ function doWithOption(option, status) { clickRect(quickFight); } else { clickRect(ocrUntilFound(res => res.find(e => e.text.startsWith('进入')), 10, 300)); - sleep(20 * 1000); + checkAuto(); + sleep(10 * 1000); let result = ocrUntilFound(res => { if (!res.text.includes('STATUS')) return null; diff --git a/utils.js b/utils.js index f1edf76..27d7953 100644 --- a/utils.js +++ b/utils.js @@ -12,6 +12,7 @@ else { getOcrRes: getOcrRes, getDisplaySize: getDisplaySize, killApp: killApp, + rgbToGray: rgbToGray, buildRegion: buildRegion, findContoursRect: findContoursRect, findImageByFeature: findImageByFeature @@ -111,6 +112,14 @@ function clickRect(rect, scale, delay) { click(x, y); } +// 灰度公式:https://docs.opencv.org/3.4/de/d25/imgproc_color_conversions.html#color_convert_rgb_gray +function rgbToGray(color) { + let ret = 0.299 * colors.red(color); + ret += 0.587 * colors.green(color); + ret += 0.114 * colors.blue(color); + return ret; +} + function imgToBounds(img, point) { if (!img || !point) return null; @@ -302,9 +311,11 @@ function findContoursRect(img, options) { rect.x + rect.width + x, rect.y + rect.height + y )); - // Imgproc.rectangle(threImgMat, Point(rect.x, rect.y), Point(rect.x + rect.width, rect.y + rect.height), Scalar(150), 3); + if (options.debug) + Imgproc.rectangle(threImgMat, Point(rect.x, rect.y), Point(rect.x + rect.width, rect.y + rect.height), Scalar(150), 3); } - // images.save(images.matToImage(threImgMat), `./images/nikkerror/${thresh}_${Date.now()}.jpg`); + if (options.debug) + images.save(images.matToImage(threImgMat), `./images/nikkerror/${thresh}_${Date.now()}.jpg`); } threImg.recycle(); grayImg.recycle();