Skip to content

feat(resolution): 支持动态分辨率适配#294

Open
Rememorio wants to merge 2 commits into
1bananachicken:devfrom
Rememorio:codex/dynamic-resolution-scaling
Open

feat(resolution): 支持动态分辨率适配#294
Rememorio wants to merge 2 commits into
1bananachicken:devfrom
Rememorio:codex/dynamic-resolution-scaling

Conversation

@Rememorio

@Rememorio Rememorio commented Jun 6, 2026

Copy link
Copy Markdown

关联 Issue

需求来源:本地排查发现 MaaNTE 对 1280x720 依赖较强,需要支持异环在 16:9 高分辨率窗口/全屏下运行,并为非 16:9 场景提供更稳的识别兜底。无关联 Issue。

变更摘要

  • 新增统一的分辨率/截图帧坐标工具,区分输入窗口尺寸与截图帧尺寸,支持 720p 基准坐标映射、模板缩放和非 16:9 候选 ROI 映射。
  • 将钓鱼、买/卖鱼饵、咖啡、家具选择、取款商品、粉爪路线、节奏选歌等 Python CustomAction 接入动态坐标/ROI 缩放。
  • 模板匹配新增 match_template_in_region_720,可在非 16:9 下尝试 stretch / fit / fill / edge-aware 等候选模型并取最高分。
  • 启动时分辨率提示从“必须 1280x720”调整为 16:9 动态缩放提示,非 16:9 启用兜底匹配但仍建议 16:9。

验证

  • python -m compileall -q agent 通过。

  • git diff --check 通过。

  • Win32 DPI-aware 实机读取 HTGame.exe client size:1920x1080,scale (1.5, 1.5),16:9 判定通过。

  • 使用真实 1920x1080 异环登录画面截图验证齿轮模板匹配:720p 模板自动缩放后命中 [1847, 24],score 0.9622

  • 合成 1440x900 非 16:9 帧验证 stretch / fit / edge_height 三类候选映射均可命中,score 均为 1.0

  • 我已阅读并遵守 PR 规范

截图 / 日志 / 说明

  • 真实窗口验证基于本地异环 HTGame.exe,Windows 显示缩放 150% 下 DPI-aware 读取到真实 1920x1080 client size。
  • 外部窗口 resize 被游戏锁定,无法通过普通 SetWindowPos 自动切换更多尺寸;非 16:9 通过合成帧覆盖 stretch / fit / edge-aware 映射模型。

Summary by Sourcery

添加分辨率感知的屏幕工具,并迁移交互和模板匹配逻辑,以支持从 1280x720 基线到任意窗口和画面尺寸(包括非 16:9 纵横比)的动态缩放。

新功能:

  • 引入统一的屏幕和画面分辨率工具,用于将 1280x720 基线坐标和矩形映射到当前输入和截图尺寸。
  • 添加模板缩放与多策略区域映射助手,以提升在非 16:9 分辨率下的模板匹配鲁棒性。
  • 暴露基于 720p 的点击、滑动和模板匹配辅助方法,使自定义操作可以在基线坐标系中运行。

增强内容:

  • 更新钓鱼、诱饵买卖、咖啡制作、家具选择、取出物品选择以及节奏歌曲选择等动作,改为使用动态坐标与 ROI 缩放,而不是固定的 1280x720 映射。
  • 调整 PinkPaw 动作助手,使光标移动和点击通过分辨率感知的坐标映射进行。
  • 优化启动时的分辨率检查日志,以区分 720p 基线、受支持的 16:9 分辨率以及非 16:9 回退模式。
Original summary in English

Summary by Sourcery

Add a resolution-aware screen utility and migrate interaction and template matching logic to support dynamic scaling from a 1280x720 baseline to arbitrary window and frame sizes, including non-16:9 aspect ratios.

New Features:

  • Introduce unified screen and frame resolution utilities for mapping 1280x720 baseline coordinates and rectangles to current input and screenshot sizes.
  • Add template resizing and multi-strategy region mapping helpers to improve template matching robustness on non-16:9 resolutions.
  • Expose 720p-based click, swipe, and template matching helpers for custom actions to operate in baseline coordinates.

Enhancements:

  • Update fishing, bait buying/selling, coffee making, furniture selection, withdraw item selection, and rhythm song selection actions to use dynamic coordinate and ROI scaling instead of fixed 1280x720 mappings.
  • Adjust PinkPaw action helpers to route cursor movement and clicking through resolution-aware coordinate mapping.
  • Refine resolution check logging at startup to distinguish 720p baseline, supported 16:9 resolutions, and non-16:9 fallback modes.

@sourcery-ai

sourcery-ai Bot commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

围绕 1280x720 基线添加通用的分辨率与画面缩放工具,并将其集成到常用操作与图像匹配工具中,使钓鱼、煮咖啡、家具属性选择、取款物品选择、节奏歌单选择以及 PinkPaw 鼠标操作在 16:9 高分辨率下可用,并在非 16:9 分辨率下具备兜底行为;同时相应更新启动日志。

match_template_in_region_720 兜底匹配的时序图

sequenceDiagram
    participant Caller
    participant Utils as Common.utils
    participant Screen as screen

    Caller->>Utils: match_template_in_region_720(img, region, template,...)
    Utils->>Screen: update_frame_size_from_image(img)
    Utils->>Screen: map_rect_to_frame_candidates(region)
    Screen-->>Utils: candidates: tuple[RectMapping]
    loop for each candidate
        Utils->>Utils: resize_template(template, candidate.scale_x, candidate.scale_y,...)
        Utils->>Utils: match_template_in_region(img, candidate.rect, scaled_template,..., scale_template=False)
        Utils-->>Utils: hit, score, x, y
        Utils->>Utils: update best result if score higher
    end
    Utils-->>Caller: best_hit, best_score, best_x, best_y
Loading

File-Level Changes

Change Details Files
引入以 720p 为基线的统一屏幕/画面缩放与矩形映射层,并为非 16:9 画面提供候选 ROI 映射及模板缩放辅助函数。
  • 同时跟踪输入端(游戏客户端)尺寸与截图画面尺寸,使用独立的缩放因子与统一缩放比例。
  • 添加辅助函数,用于检测基线分辨率、近似 16:9 的宽高比,并可从原始尺寸或图像对象更新屏幕/画面尺寸。
  • 提供从 1280x720 基线到输入端或画面空间的点/矩形映射工具,以及适配 fit/fill/边缘感知的候选 ROI 生成(带裁剪和去重)。
  • 实现模板缩放辅助函数,可根据显式缩放比例或当前画面缩放比例对基线模板进行缩放,并使用合适的 OpenCV 插值方式及可选的绿色蒙版处理。
agent/utils/screen.py
重构共享的自定义动作工具,使其感知画面尺寸,并添加基于 720p 的点击/滑动与高级模板匹配,包括尝试多种 ROI 映射的变体。
  • 更新截图捕获逻辑,在 screen 模块中刷新画面尺寸。
  • 新增通用点/矩形点击辅助函数(可选带移动)、基于 720p 映射的变体,以及基于基线坐标映射到画面空间的 swipe/swipe_720 辅助函数。
  • 增强 match_template_in_region:校验输入、裁剪区域、可选地预先将模板缩放到画面尺寸,并处理非有限的匹配得分。
  • 添加 match_template_in_region_720:将基线 ROI 映射到多个画面候选区域,为每个候选区域按需缩放模板,并返回跨映射中得分最高的匹配结果。
agent/custom/action/Common/utils.py
调整与钓鱼相关的自定义动作(含 CV 与不含 CV)以及鱼饵购买/卖鱼流程,使其使用基于画面的动态缩放和 720p 基线的区域/阈值。
  • 从初始截图初始化画面尺寸,并基于画面缩放计算 deadzone,而不是使用静态的 scaling_factors()。
  • 不再使用 screen.map_rect 预先映射 ROI,而是直接针对 720p 基线矩形操作,并依赖 match_template_in_region_720 与 *_720 点击辅助函数映射到当前画面。
  • 对于非 CV 钓鱼,在每次截图时基于当前画面缩放更新 deadzone 与光标移动时序。
  • 调整点击偏移(例如准备按钮附近)及条形中心等派生量,改为使用映射到画面空间的值。
agent/custom/action/AutoFish/auto_fish.py
agent/custom/action/AutoFish/auto_buy_fish_bait.py
agent/custom/action/AutoFish/auto_sell_fish.py
agent/custom/action/AutoFish/auto_fish_withoutCV.py
统一咖啡制作、家具属性选择与取款物品选择的点击行为,复用 Common 工具中的基线 720 辅助函数并支持高分辨率。
  • 用 Common.utils 中共享的 get_image、click_rect 与 click_rect_720 辅助函数替换本地 get_image 与临时实现的点击逻辑,并在需要时微调延迟和点击次数。
  • 确保手势风格的点击(先移动再点击)与滑动使用基于 720p 的映射(swipe_720),从而适配画面尺寸。
  • 简化截屏工具,将其委托给共享的 get_image,该函数也会更新画面尺寸。
agent/custom/action/auto_make_coffee.py
agent/custom/action/Furniture/furniture_choose_property.py
agent/custom/action/withdraw_money_choose_item.py
通过新的 screen 工具使 PinkPaw 抢劫鼠标动作与节奏模式的歌曲选择滚动与分辨率解耦。
  • 将 PinkPaw ActionHelper 的鼠标初始位置设为 map_point_to_input(640, 360),并让对外的 move_to/click 都通过基线到输入端的映射,保留私有的 _move_to_input 用于实际移动。
  • 在节奏模式歌曲选择中,每一步都刷新画面尺寸,并使用 map_rect_to_frame 映射歌单 ROI,同时利用 frame_scaling_factors 对滚动距离进行缩放。
agent/custom/action/pinkpaw/pinkpaw_core1.py
agent/custom/action/pinkpaw/pinkpaw_core2.py
agent/custom/action/rhythm/utils/song_selector.py
agent/custom/action/rhythm/feats/select_song.py
改进通用点击覆盖动作与启动分辨率检查,使其与动态分辨率方案和日志规范保持一致。
  • 将 click_override 接入 click_rect_720,以对自定义矩形参数(基于 720p 基线)使用该函数,同时保留原始 click_rect 用于识别结果框;并用 logger 调用替换 print 调试输出。
  • 扩展分辨率检查,以同时更新画面尺寸与屏幕尺寸,使用 is_baseline_size/is_supported_16_9 区分基线 720p 与一般 16:9,并调整面向用户的警告文案,说明动态缩放行为和推荐的 16:9 分辨率。
agent/custom/action/Common/click.py
agent/main.py

Possibly linked issues

  • #: Issue 要求支持 21:9 带鱼屏,本 PR 引入对非 16:9 的动态适配与兜底识别,直接响应该需求。
  • #新自动钓鱼任务买鱼饵买成鱼竿了💀: Issue 中买鱼饵误点的问题源于分辨率依赖,本 PR 为钓鱼及买卖鱼饵加入动态分辨率适配以修复该问题。

Tips and commands

Interacting with Sourcery

  • 触发新评审: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的评审评论即可。
  • 从评审评论生成 GitHub issue: 在某条评审评论下回复,请求 Sourcery 从该评论创建一个 issue。你也可以直接回复 @sourcery-ai issue 来从该评论生成一个 issue。
  • 生成 pull request 标题: 在 pull request 标题中任意位置写上 @sourcery-ai 即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 以(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary 即可在该位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 以在任意时间(重新)生成摘要。
  • 生成 reviewer's guide: 在 pull request 中评论 @sourcery-ai guide 即可随时(重新)生成 reviewer's guide。
  • 解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve 以一次性解决所有 Sourcery 评论。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 撤销所有 Sourcery 评审: 在 pull request 中评论 @sourcery-ai dismiss 以撤销所有现有 Sourcery 评审。特别适用于你希望以一次全新的评审开始 —— 记得再评论 @sourcery-ai review 触发新评审!

Customizing Your Experience

打开你的 dashboard 以:

  • 启用或禁用诸如 Sourcery 生成的 pull request 摘要、reviewer's guide 等评审功能。
  • 更改评审语言。
  • 添加、移除或编辑自定义评审说明。
  • 调整其他评审设置。

Getting Help

Original review guide in English

Reviewer's Guide

Add a generalized resolution and frame scaling utility around a 1280x720 baseline, then integrate it into common actions and image-matching utilities so fishing, coffee-making, furniture selection, withdrawals, rhythm song selection, and PinkPaw mouse actions work under 16:9 high resolutions and have fallback behavior for non-16:9; also update startup logging accordingly.

Sequence diagram for match_template_in_region_720 fallback matching

sequenceDiagram
    participant Caller
    participant Utils as Common.utils
    participant Screen as screen

    Caller->>Utils: match_template_in_region_720(img, region, template,...)
    Utils->>Screen: update_frame_size_from_image(img)
    Utils->>Screen: map_rect_to_frame_candidates(region)
    Screen-->>Utils: candidates: tuple[RectMapping]
    loop for each candidate
        Utils->>Utils: resize_template(template, candidate.scale_x, candidate.scale_y,...)
        Utils->>Utils: match_template_in_region(img, candidate.rect, scaled_template,..., scale_template=False)
        Utils-->>Utils: hit, score, x, y
        Utils->>Utils: update best result if score higher
    end
    Utils-->>Caller: best_hit, best_score, best_x, best_y
Loading

File-Level Changes

Change Details Files
Introduce a unified screen/frame scaling and rect-mapping layer with 720p baseline and candidate ROI mappings for non-16:9 frames, including template resizing helpers.
  • Track both input (game client) size and screenshot frame size, with separate scaling factors and uniform scales.
  • Add helpers to test baseline resolution and approximate 16:9 aspect ratio, and update screen/frame size from raw sizes or image objects.
  • Provide mapping utilities for points/rects from 1280x720 baseline to input or frame space, plus fit/fill/edge-aware candidate ROI generation with clamping and deduping.
  • Implement template resize helpers that scale baseline templates according to explicit scales or current frame scale with appropriate OpenCV interpolation and optional green-mask handling.
agent/utils/screen.py
Refactor shared custom-action utilities to be frame-size aware and add 720p-based click/swipe and advanced template matching, including a variant that tries multiple ROI mappings.
  • Update screenshot capture to refresh frame size in the screen module.
  • Add generic point/rect click helpers (optionally with move), 720p-mapped variants, and swipe/swipe_720 helpers that work off baseline coordinates mapped to frame space.
  • Strengthen match_template_in_region to validate inputs, clamp regions, optionally pre-scale templates to frame size, and handle non-finite match scores.
  • Add match_template_in_region_720 that maps a baseline ROI to multiple frame candidates, resizes templates per candidate, and returns the best-scoring match across mappings.
agent/custom/action/Common/utils.py
Adapt fishing-related custom actions (with and without CV) and bait/buy/sell flows to use dynamic frame-based scaling and 720p baseline regions/thresholds.
  • Initialize frame size from an initial screenshot and compute deadzone based on frame scaling rather than static scaling_factors().
  • Stop pre-mapping ROIs with screen.map_rect; instead operate on baseline 720p rects and rely on match_template_in_region_720 and *_720 click helpers to map to the current frame.
  • For non-CV fishing, update deadzone and cursor movement timing based on current frame scaling on each screenshot.
  • Adjust click offsets (e.g., around prepare button) and derived quantities like bar centers to use mapped frame-space values.
agent/custom/action/AutoFish/auto_fish.py
agent/custom/action/AutoFish/auto_buy_fish_bait.py
agent/custom/action/AutoFish/auto_sell_fish.py
agent/custom/action/AutoFish/auto_fish_withoutCV.py
Unify click behavior for coffee-making, furniture property selection, and withdrawal item selection to reuse Common utils with baseline-720 helpers and support higher resolutions.
  • Replace local get_image and ad-hoc click implementations with shared get_image, click_rect, and click_rect_720 helpers from Common.utils, tuning delay and click repetition where needed.
  • Ensure gesture-style clicks (move-before-click) and swipes use 720p-based mapping (swipe_720) so they adapt to frame size.
  • Simplify screencap utilities to delegate to the shared get_image, which also updates frame dimensions.
agent/custom/action/auto_make_coffee.py
agent/custom/action/Furniture/furniture_choose_property.py
agent/custom/action/withdraw_money_choose_item.py
Make PinkPaw heist mouse actions and rhythm song selection scrolls resolution-independent via the new screen utilities.
  • Initialize PinkPaw ActionHelper mouse position from map_point_to_input(640, 360) and funnel public move_to/click through baseline-to-input mapping, keeping a private _move_to_input for actual movement.
  • In rhythm song selection, refresh frame size for each step and map the song list ROI and scroll distance using map_rect_to_frame plus frame_scaling_factors for scroll delta scaling.
agent/custom/action/pinkpaw/pinkpaw_core1.py
agent/custom/action/pinkpaw/pinkpaw_core2.py
agent/custom/action/rhythm/utils/song_selector.py
agent/custom/action/rhythm/feats/select_song.py
Improve the generic click override action and startup resolution check to align with the dynamic-resolution approach and logging standards.
  • Wire click_override to use click_rect_720 for custom rect parameters (baseline 720p) and keep raw click_rect for recognition-driven boxes; replace print debugging with logger calls.
  • Extend resolution check to update frame size alongside screen size, distinguish baseline 720p from generic 16:9 via is_baseline_size/is_supported_16_9, and adjust user-facing warning text to describe dynamic scaling and recommended 16:9 resolutions.
agent/custom/action/Common/click.py
agent/main.py

Possibly linked issues

  • #: Issue 要求支持21:9 带鱼屏,PR 引入非16:9 动态适配和兜底识别,直接响应该需求。
  • #新自动钓鱼任务买鱼饵买成鱼竿了💀: Issue 的买鱼饵误点问题源于分辨率依赖,PR 为钓鱼与买卖鱼饵加入动态分辨率适配以修复。

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我在这里给了一些整体性的反馈:

  • match_template_in_region_720 中,即使缩放因子重复,模板在每个候选上都会被重新缩放;可以考虑在循环内部按 (scale_x, scale_y) 缓存已缩放的模板,以避免在热点路径上进行重复的 cv2.resize 调用。
  • 新增的 screen.map_rect_to_frame_candidates(尤其是 edge_height/edge_width 分支)引入了多个“魔法常量”(比如 0.35/0.65 的中心阈值);把这些常量收集到带名称的常量中,或者简要说明这些取值的依据,可以让映射行为更易于维护和调优。
  • 新增的点击/滑动辅助函数现在同时支持原始坐标和 720 基线坐标;你可能需要在对外的辅助函数上通过断言或文档说明预期的坐标空间(例如 click_rect vs click_rect_720, swipe vs swipe_720),以避免未来调用方误用。
供 AI Agent 使用的提示
Please address the comments from this code review:

## Overall Comments
- In `match_template_in_region_720`, the template is resized on every candidate even when scale factors repeat; consider caching resized templates per `(scale_x, scale_y)` within the loop to avoid redundant `cv2.resize` calls on hot paths.
- The new `screen.map_rect_to_frame_candidates` (especially the `edge_height`/`edge_width` branches) introduces several magic constants (like the 0.35/0.65 center thresholds); collecting these into named constants or briefly explaining the rationale would make the mapping behavior easier to maintain and tune.
- The new click/swipe helpers now support both raw and 720-baseline coordinates; you might want to assert or document expected coordinate space on the public helpers (e.g., `click_rect` vs `click_rect_720`, `swipe` vs `swipe_720`) to avoid accidental misuse in future call sites.

Sourcery 对开源项目免费——如果你喜欢我们的评审,请考虑分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've left some high level feedback:

  • In match_template_in_region_720, the template is resized on every candidate even when scale factors repeat; consider caching resized templates per (scale_x, scale_y) within the loop to avoid redundant cv2.resize calls on hot paths.
  • The new screen.map_rect_to_frame_candidates (especially the edge_height/edge_width branches) introduces several magic constants (like the 0.35/0.65 center thresholds); collecting these into named constants or briefly explaining the rationale would make the mapping behavior easier to maintain and tune.
  • The new click/swipe helpers now support both raw and 720-baseline coordinates; you might want to assert or document expected coordinate space on the public helpers (e.g., click_rect vs click_rect_720, swipe vs swipe_720) to avoid accidental misuse in future call sites.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `match_template_in_region_720`, the template is resized on every candidate even when scale factors repeat; consider caching resized templates per `(scale_x, scale_y)` within the loop to avoid redundant `cv2.resize` calls on hot paths.
- The new `screen.map_rect_to_frame_candidates` (especially the `edge_height`/`edge_width` branches) introduces several magic constants (like the 0.35/0.65 center thresholds); collecting these into named constants or briefly explaining the rationale would make the mapping behavior easier to maintain and tune.
- The new click/swipe helpers now support both raw and 720-baseline coordinates; you might want to assert or document expected coordinate space on the public helpers (e.g., `click_rect` vs `click_rect_720`, `swipe` vs `swipe_720`) to avoid accidental misuse in future call sites.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Watanabehato

Copy link
Copy Markdown
Collaborator

加下开发群谢谢喵1092630280

@Rememorio

Copy link
Copy Markdown
Author

已处理 Sourcery 的三点建议:

  • match_template_in_region_720 内按 (scale_x, scale_y, green_mask) 缓存缩放后的模板,避免候选 ROI 重复 cv2.resize
  • 将 edge-aware 映射中的 0.35 / 0.65 和 scale key 精度提取为命名常量,并补充候选映射 docstring。
  • click_rect / click_rect_720swipe / swipe_720 等公开 helper 补充坐标空间说明,区分当前坐标系与 720p baseline 坐标。

补充验证:

  • python -m compileall -q agent
  • git diff --check
  • 真实 1920x1080 截图齿轮模板匹配仍命中 [1847, 24],score 0.9622
  • 合成 1440x900 非 16:9 帧中 stretch / fit / edge_height 候选映射均命中

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants