Skip to content

Commit

Permalink
feat: 检查自动瞄准&爆裂 #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebartin committed Apr 15, 2023
1 parent 2e88448 commit 18e9597
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 12 deletions.
88 changes: 86 additions & 2 deletions NIKKE/NIKKEutils.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -26,7 +28,8 @@ else {
mostSimilar: mostSimilar,
detectNikkes: detectNikkes,
NikkeToday: NikkeToday,
关闭限时礼包: 关闭限时礼包
关闭限时礼包: 关闭限时礼包,
checkAuto: checkAuto,
};
}
function 启动NIKKE() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
10 changes: 4 additions & 6 deletions NIKKE/NIKKE日常.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var {
启动NIKKE, 等待NIKKE加载, 退出NIKKE,
mostSimilar, 返回首页, 关闭限时礼包,
detectNikkes, NikkeToday
detectNikkes, NikkeToday, checkAuto
} = require('./NIKKEutils.js');
var { 模拟室 } = require('./模拟室.js');
var {
Expand Down Expand Up @@ -458,18 +458,16 @@ 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);
return false;
}
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
Expand Down
6 changes: 4 additions & 2 deletions NIKKE/模拟室.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ else {
getOcrRes: getOcrRes,
getDisplaySize: getDisplaySize,
killApp: killApp,
rgbToGray: rgbToGray,
buildRegion: buildRegion,
findContoursRect: findContoursRect,
findImageByFeature: findImageByFeature
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 18e9597

Please sign in to comment.