diff --git a/legend/城镇_充值#L448,36.png b/legend/城镇_充值#L448,36.png deleted file mode 100644 index 5b63ae7..0000000 Binary files a/legend/城镇_充值#L448,36.png and /dev/null differ diff --git a/legend/城镇_礼包#L441,72.png b/legend/城镇_礼包#L441,72.png new file mode 100644 index 0000000..2ffd12a Binary files /dev/null and b/legend/城镇_礼包#L441,72.png differ diff --git a/legend/野外_资源无士兵#L214,691.png b/legend/野外_资源无士兵#L214,691.png new file mode 100644 index 0000000..93115a5 Binary files /dev/null and b/legend/野外_资源无士兵#L214,691.png differ diff --git a/src/main/java/com/example/jmacro/wjdr/JMacro.java b/src/main/java/com/example/jmacro/wjdr/JMacro.java index fcf52a4..4731f1b 100644 --- a/src/main/java/com/example/jmacro/wjdr/JMacro.java +++ b/src/main/java/com/example/jmacro/wjdr/JMacro.java @@ -108,15 +108,14 @@ public abstract class JMacro { * @param smooth 平滑移动 */ public void mouseMove(ViewPoint point, boolean smooth) { - int endX = point.getX() + (point.isAbsolute() ? 0 : getFocusRect().getLeft()); - int endY = point.getY() + (point.isAbsolute() ? 0 : getFocusRect().getTop()); + int endX = point.getX(); + int endY = point.getY(); if (smooth) { // 获取当前鼠标位置 Point mousePoint = MouseInfo.getPointerInfo().getLocation(); int startX = mousePoint.x; int startY = mousePoint.y; - // 求两点距离 double absX = Math.abs(startX - endX); double absY = Math.abs(startY - endY); @@ -156,7 +155,7 @@ public abstract class JMacro { * @param rect 矩形区域 */ public void mouseLeftClick(ViewRect rect) { - mouseLeftClick(new ViewPoint(rect.getCenter()[0], rect.getCenter()[1], true)); + mouseLeftClick(new ViewPoint(rect.getCenter()[0], rect.getCenter()[1])); } /** @@ -178,11 +177,11 @@ public abstract class JMacro { */ public void mouseLeftDrag(ViewPoint start, ViewPoint end, boolean smooth) { mouseMove(start, smooth); - delayUnstable(); + delay(); robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); - delayUnstable(); + delay(); mouseMove(end, smooth); - delayUnstable(); + delay(); robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); } @@ -347,6 +346,18 @@ public abstract class JMacro { return findPic(getFocusRect(), pic, minSimilar); } + /** + * 查找图片 + * + * @param viewRect 查找范围(不设时,取全屏) + * @param pic 待查找图片 + * @param minSimilar 相似度 + * @return 匹配图片区域 + */ + public ViewRect findPic(ViewRect viewRect, File pic, double minSimilar) { + return findPic(viewRect, ImageUtil.load(pic), minSimilar); + } + /** * 查找图片 * @@ -371,6 +382,7 @@ public abstract class JMacro { // 获取实时屏幕 BufferedImage screen = capture(robot, viewRect); + ImageUtil.show(screen); int[][] screenData = ImageUtil.getImageRGB(screen); int[][] picData = ImageUtil.getImageRGB(pic); @@ -416,6 +428,72 @@ public abstract class JMacro { return null; } + /** + * 查找图片 + * + * @param legend 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ViewRect findLegend(String legend, double minSimilar) { + return findLegend(Legend.inflate(legend), minSimilar); + } + + /** + * 查找图片 + * + * @param legend 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ViewRect findLegend(Legend legend, double minSimilar) { + return findPic(legend.getFile(), minSimilar); + } + + /** + * 查找图例 + * + * @param legend 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ViewRect findLegend(ViewRect rect, String legend, double minSimilar) { + return findLegend(rect, Legend.inflate(legend), minSimilar); + } + + /** + * 查找图例 + * + * @param legend 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ViewRect findLegend(ViewRect rect, Legend legend, double minSimilar) { + return findPic(rect, legend.getFile(), minSimilar); + } + + /** + * 查找图片 + * + * @param legend 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ViewRect waitAndFindLegend(Legend legend, double minSimilar, long seconds) { + return waitAndFindPic(legend.getFile(), minSimilar, seconds); + } + + /** + * 查找图片 + * + * @param legend 图例 + * @param minSimilar 最低相似度 + * @return 匹配图片区域 + */ + public ViewRect waitAndFindLegend(ViewRect rect, Legend legend, double minSimilar, long seconds) { + return waitAndFindPic(rect, legend.getFile(), minSimilar, seconds); + } + /** * 等待并查找图片 * @@ -743,4 +821,46 @@ public abstract class JMacro { public ViewRect waitAndMatchLegend(Legend legend, double minSimilar, long seconds) { return waitAndMatchPic(legend.getFile(), legend.getLocation(), minSimilar, seconds); } + + /** + * 将相对坐标转为绝对坐标 + * + * @return 绝对区域 + */ + public ViewRect of(int left, int top, int right, int bottom) { + int ox = getFocusRect().getLeft(); + int oy = getFocusRect().getTop(); + return new ViewRect(left + ox, top + oy, right + ox, bottom + oy); + } + + /** + * 将相对区域转为绝对区域 + * + * @param relativeRect 相对区域 + * @return 绝对区域 + */ + public ViewRect of(ViewRect relativeRect) { + return new ViewRect(relativeRect.getLeft(), relativeRect.getTop(), relativeRect.getRight(), relativeRect.getBottom()); + } + + /** + * 将相对坐标转为绝对坐标 + * + * @return 绝对坐标 + */ + public ViewPoint of(int x, int y) { + int ox = getFocusRect().getLeft(); + int oy = getFocusRect().getTop(); + return new ViewPoint(x + ox, y + oy); + } + + /** + * 将相对坐标转为绝对坐标 + * + * @param relativePoint 相对坐标 + * @return 绝对区域 + */ + public ViewPoint of(ViewPoint relativePoint) { + return of(relativePoint.getX(), relativePoint.getY()); + } } \ No newline at end of file diff --git a/src/main/java/com/example/jmacro/wjdr/JMainApplication.java b/src/main/java/com/example/jmacro/wjdr/JMainApplication.java index ab7cddd..0717cc8 100644 --- a/src/main/java/com/example/jmacro/wjdr/JMainApplication.java +++ b/src/main/java/com/example/jmacro/wjdr/JMainApplication.java @@ -68,17 +68,7 @@ public class JMainApplication extends Application { }); // 服务初始化 - JMainService.init(new JMacro() { - @Override - public ViewRect focus() { - return new ViewRect(0,0,500,500); - } - - @Override - public void run() { - - } - }, new File("legend")); + JMainService.init(new MacroForWJDR(), new File("legend")); } public static void main(String[] args) { diff --git a/src/main/java/com/example/jmacro/wjdr/base/Legend.java b/src/main/java/com/example/jmacro/wjdr/base/Legend.java index d5c8b63..6fab3b5 100644 --- a/src/main/java/com/example/jmacro/wjdr/base/Legend.java +++ b/src/main/java/com/example/jmacro/wjdr/base/Legend.java @@ -94,7 +94,7 @@ public class Legend { } else { int x = Convert.toInt(ReUtil.get("[\\S\\s]+#L([0-9]+),[0-9]+\\.png", file.getName(), 1), 0); int y = Convert.toInt(ReUtil.get("[\\S\\s]+#L[0-9]+,([0-9]+)\\.png", file.getName(), 1), 0); - newLegend.location = new ViewPoint(x, y, false); + newLegend.location = new ViewPoint(x, y); } fileCache.put(name, newLegend); diff --git a/src/main/java/com/example/jmacro/wjdr/base/ViewPoint.java b/src/main/java/com/example/jmacro/wjdr/base/ViewPoint.java index f749216..22988de 100644 --- a/src/main/java/com/example/jmacro/wjdr/base/ViewPoint.java +++ b/src/main/java/com/example/jmacro/wjdr/base/ViewPoint.java @@ -9,11 +9,6 @@ package com.example.jmacro.wjdr.base; */ public class ViewPoint { - /** - * 是否为绝对坐标,相对坐标在操作时会根据聚焦区域计算转为绝对坐标 - */ - private boolean absolute; - /** * 相对原点(左上)的x轴距离 */ @@ -34,13 +29,8 @@ public class ViewPoint { * @param y 坐标y */ public ViewPoint(int x, int y) { - this(x, y, false); - } - - public ViewPoint(int x, int y, boolean absolute) { this.x = x; this.y = y; - this.absolute = absolute; } public int getX() { @@ -59,14 +49,6 @@ public class ViewPoint { this.y = y; } - public boolean isAbsolute() { - return absolute; - } - - public void setAbsolute(boolean absolute) { - this.absolute = absolute; - } - @Override public String toString() { return "ViewPoint{" + diff --git a/src/main/java/com/example/jmacro/wjdr/tool/Location.java b/src/main/java/com/example/jmacro/wjdr/tool/Location.java index cbc421a..f4a76a9 100644 --- a/src/main/java/com/example/jmacro/wjdr/tool/Location.java +++ b/src/main/java/com/example/jmacro/wjdr/tool/Location.java @@ -100,8 +100,8 @@ public class Location extends JFrame { x = e.getX(); y = e.getY(); Logger.info("坐标采集:"); - Logger.info("屏幕坐标:[{},{}],用法:new ViewPoint({},{},true)", x, y, x, y); - Logger.info("相对坐标:[{},{}],用法:new ViewPoint({},{})", x - originX, y - originY, x - originX, y - originY); + Logger.info("屏幕坐标:[{},{}]", x, y); + Logger.info("相对坐标:[{},{}],用法:macro.of({},{})", x - originX, y - originY, x - originX, y - originY); String hexColor = Integer.toHexString(capture.getRGB(x, y) & 0xFFFFFF); Logger.info("坐标色值:[#{}]", hexColor); close(); diff --git a/src/main/java/com/example/jmacro/wjdr/wjdr/MacroForWJDR.java b/src/main/java/com/example/jmacro/wjdr/wjdr/MacroForWJDR.java index 2c05a0a..59149d4 100644 --- a/src/main/java/com/example/jmacro/wjdr/wjdr/MacroForWJDR.java +++ b/src/main/java/com/example/jmacro/wjdr/wjdr/MacroForWJDR.java @@ -75,7 +75,7 @@ public class MacroForWJDR extends JMacro { Boolean inMain = TaskUtil.retryTask(() -> { { // 定位弹框,关闭弹框 delayUnstable(); - ViewRect rect = matchLegend("城镇_充值", 0.8d); + ViewRect rect = findLegend("城镇_礼包", 0.7); if (rect != null) { mouseLeftClick(rect); Logger.info("检测到充值广告弹框,关闭弹框"); diff --git a/src/main/java/com/example/jmacro/wjdr/wjdr/task/Task_自动采矿.java b/src/main/java/com/example/jmacro/wjdr/wjdr/task/Task_自动采矿.java index 8dccfda..5562b9c 100644 --- a/src/main/java/com/example/jmacro/wjdr/wjdr/task/Task_自动采矿.java +++ b/src/main/java/com/example/jmacro/wjdr/wjdr/task/Task_自动采矿.java @@ -37,7 +37,7 @@ public class Task_自动采矿 extends BaseTask { Logger.info("点击资源搜索按钮"); macro.delayUnstable(1000); - macro.mouseLeftClick(new ViewPoint(33, 648)); + macro.mouseLeftClick(macro.of(33, 648)); Logger.info("等待搜索面板"); ViewRect 野外_搜索 = macro.waitAndMatchLegend("野外_搜索", 0.9); @@ -47,7 +47,7 @@ public class Task_自动采矿 extends BaseTask { } Logger.info("拖动面板"); - macro.mouseLeftDrag(new ViewPoint(433, 679), new ViewPoint(61, 682), true); + macro.mouseLeftDrag(macro.of(433, 679), macro.of(61, 682), true); Logger.info("开始采集资源"); String[] types = new String[]{ @@ -98,14 +98,14 @@ public class Task_自动采矿 extends BaseTask { TaskUtil.execTask(() -> { Logger.info("调整等级"); macro.delayUnstable(700); - macro.mouseLeftClick(new ViewPoint(51, 777)); + macro.mouseLeftClick(macro.of(51, 777)); }, clevel - level, 1000); } if (clevel < level) { TaskUtil.execTask(() -> { Logger.info("调整等级"); macro.delayUnstable(700); - macro.mouseLeftClick(new ViewPoint(360, 777)); + macro.mouseLeftClick(macro.of(360, 777)); }, level - clevel, 1000); } @@ -126,7 +126,7 @@ public class Task_自动采矿 extends BaseTask { macro.delayUnstable(1000); - ViewRect[] viewRects = macro.matchLegend("野外_资源出征", "野外_资源无对队列"); + ViewRect[] viewRects = macro.matchLegend("野外_资源出征", "野外_资源无对队列", "野外_资源无士兵"); if (viewRects[0] != null) { Logger.info("出征"); @@ -135,11 +135,17 @@ public class Task_自动采矿 extends BaseTask { if (viewRects[1] != null) { Logger.info("队列已满,取消采集"); macro.delayUnstable(1000); - macro.mouseLeftClick(new ViewPoint(471, 250)); + macro.mouseLeftClick(macro.of(471, 250)); Logger.info("队列已满,取消采集"); macro.delayUnstable(1000); - macro.mouseLeftClick(new ViewPoint(471, 250)); + macro.mouseLeftClick(macro.of(471, 250)); + return false; + } + if (viewRects[2] != null) { + Logger.info("士兵不足,取消采集"); + macro.delayUnstable(1000); + macro.mouseLeftClick(macro.of(32,30)); return false; }