Skip to content

Commit 10f01f0

Browse files
authored
js:锄地一条龙1.13.0 (#2470)
* js:锄地一条龙1.13.0 优化拾取识别 * Update main.js * Update main.js
1 parent 1f42649 commit 10f01f0

File tree

2 files changed

+43
-35
lines changed

2 files changed

+43
-35
lines changed

repo/js/AutoHoeingOneDragon/main.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ if (settings.activeDumperMode) { //处理泥头车信息
1111
}
1212
let gameRegion;
1313
let targetItemPath = "assets/targetItems";
14-
let mainUITemplate = file.ReadImageMatSync("assets/MainUI.png");
14+
1515
let itemFullTemplate = file.ReadImageMatSync("assets/itemFull.png");
1616
let frozenTemplate = file.ReadImageMatSync("assets/解除冰冻.png");
1717
const frozenRo = RecognitionObject.TemplateMatch(frozenTemplate, 1379, 574, 1463 - 1379, 613 - 574);
@@ -24,6 +24,15 @@ let whiteFurinaRo = RecognitionObject.TemplateMatch(whiteFurinaTemplate, 1634, 9
2424
whiteFurinaRo.Threshold = 0.99;
2525
whiteFurinaRo.InitTemplate();
2626

27+
let fIcontemplate = file.ReadImageMatSync('assets/F_Dialogue.png');
28+
let fIconRo = RecognitionObject.TemplateMatch(fIcontemplate, 1102, 335, 34, 400);
29+
fIconRo.Threshold = 0.95;
30+
fIconRo.InitTemplate();
31+
32+
let mainUITemplate = file.ReadImageMatSync("assets/MainUI.png");
33+
const mainUIRo = RecognitionObject.TemplateMatch(mainUITemplate, 0, 0, 150, 150);
34+
35+
2736
let targetItems;
2837
let doFurinaSwitch = false;
2938

@@ -799,7 +808,6 @@ async function recognizeAndInteract() {
799808
//log.info("调试-开始执行图像识别与拾取任务");
800809
let lastcenterYF = 0;
801810
let lastItemName = "";
802-
let fIcontemplate = file.ReadImageMatSync('assets/F_Dialogue.png');
803811
let thisMoveUpTime = 0;
804812
let lastMoveDown = 0;
805813
gameRegion = captureGameRegion();
@@ -880,40 +888,35 @@ async function recognizeAndInteract() {
880888
}
881889

882890
async function performTemplateMatch(centerYF) {
891+
/* 一次性切 6 种宽度(0-5 汉字) */
892+
const regions = [];
893+
for (let cn = 0; cn <= 5; cn++) { // 0~5 共 6 档
894+
const w = 12 + 28 * Math.min(cn, 5) + 2;
895+
regions[cn] = gameRegion.DeriveCrop(1219, centerYF - 15, w, 30);
896+
}
897+
883898
try {
884-
let result;
885-
let itemName = null;
886-
for (const targetItem of targetItems) {
887-
//log.info(`正在尝试匹配${targetItem.itemName}`);
888-
const cnLen = Math.min([...targetItem.itemName].filter(c => c >= '\u4e00' && c <= '\u9fff').length, 5);
889-
const recognitionObject = RecognitionObject.TemplateMatch(
890-
targetItem.template,
891-
1219,
892-
centerYF - 15,
893-
12 + 28 * cnLen + 2,
894-
30
895-
);
896-
recognitionObject.Threshold = targetItem.Threshold;
897-
recognitionObject.InitTemplate();
898-
result = gameRegion.find(recognitionObject);
899-
if (result.isExist()) {
900-
itemName = targetItem.itemName;
901-
break;
899+
for (const it of targetItems) {
900+
const cnLen = Math.min(
901+
[...it.itemName].filter(c => c >= '\u4e00' && c <= '\u9fff').length,
902+
5
903+
); // 0-5
904+
905+
if (regions[cnLen].find(it.roi).isExist()) {
906+
return it.itemName;
902907
}
903908
}
904-
return itemName;
905-
} catch (error) {
906-
log.error(`模板匹配时发生异常: ${error.message}`);
907-
return null;
909+
} catch (e) {
910+
log.error(`performTemplateMatch: ${e.message}`);
911+
} finally {
912+
regions.forEach(r => r.dispose());
908913
}
914+
return null;
909915
}
910916

911917
async function findFIcon() {
912-
let recognitionObject = RecognitionObject.TemplateMatch(fIcontemplate, 1102, 335, 34, 400);
913-
recognitionObject.Threshold = 0.95;
914-
recognitionObject.InitTemplate();
915918
try {
916-
let result = gameRegion.find(recognitionObject);
919+
let result = gameRegion.find(fIconRo);
917920
if (result.isExist()) {
918921
return Math.round(result.y + result.height / 2);
919922
}
@@ -950,7 +953,6 @@ async function loadBlacklist(merge = false) {
950953
}
951954

952955
async function isMainUI() {
953-
const recognitionObject = RecognitionObject.TemplateMatch(mainUITemplate, 0, 0, 150, 150);
954956
const maxAttempts = 1;
955957
let attempts = 0;
956958
let dodispose = false;
@@ -960,7 +962,7 @@ async function isMainUI() {
960962
dodispose = true;
961963
}
962964
try {
963-
const result = gameRegion.find(recognitionObject);
965+
const result = gameRegion.find(mainUIRo);
964966
if (result.isExist()) return true;
965967
} catch (error) {
966968
log.error(`识别图像时发生异常: ${error.message}`);
@@ -978,6 +980,7 @@ async function isMainUI() {
978980

979981
// 加载拾取物图片
980982
async function loadTargetItems() {
983+
981984
let targetItemPath;
982985
if (pickup_Mode === "模板匹配拾取,拾取狗粮和怪物材料") {
983986
targetItemPath = "assets/targetItems/";
@@ -986,26 +989,31 @@ async function loadTargetItems() {
986989
} else {
987990
return null;
988991
}
989-
992+
log.info("开始加载模板图片");
990993
const items = await readFolder(targetItemPath, false);
991994

992995
// 统一预加载模板
993996
for (const it of items) {
994997
try {
995998
it.template = file.ReadImageMatSync(it.fullPath);
996999
it.itemName = it.fileName.replace(/\.png$/i, '');
1000+
it.roi = RecognitionObject.TemplateMatch(it.template);
9971001

9981002
// 新增:解析括号中的阈值
9991003
const match = it.fullPath.match(/[(](.*?)[)]/); // 匹配英文或中文括号
1004+
let itsThreshold;
10001005
if (match) {
10011006
const val = parseFloat(match[1]);
1002-
it.Threshold = (!isNaN(val) && val >= 0 && val <= 1) ? val : 0.85;
1007+
itsThreshold = (!isNaN(val) && val >= 0 && val <= 1) ? val : 0.85;
10031008
} else {
1004-
it.Threshold = 0.85;
1009+
itsThreshold = 0.85;
10051010
}
1011+
it.roi.Threshold = itsThreshold;
1012+
it.roi.InitTemplate();
1013+
10061014
} catch (error) { }
10071015
}
1008-
1016+
log.info("模板图片加载完成");
10091017
return items;
10101018
}
10111019

repo/js/AutoHoeingOneDragon/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 1,
33
"name": "锄地一条龙",
4-
"version": "1.12.1",
4+
"version": "1.13.0",
55
"description": "一站式解决自动化锄地,支持只拾取狗粮,请仔细阅读README.md后使用",
66
"authors": [
77
{

0 commit comments

Comments
 (0)