P0-2 镜牢简单键盘寻路改为按节点类型选方向

Browse Source
config 开着 mirror_keyboard_simple_pathfinding 时 AALC 每步只按 ↑ 进
最上面的节点(11-14:3 层只进过 1 次商店)。常规寻路要 mouse_drag 拖
地图,第二段拖动只受超时限制且超时会退回主界面;连线识别还依赖本机
坐标不对的 my_crop,所以不改用常规寻路(BACKLOG 方案 B)。

install_typed_keyboard_road() 替换 search_road/mirror 两处的简单键盘
寻路:仍只按键不拖地图,用 AALC 自带 ONNX 模型整屏识别巴士右侧一列
节点,按上游 all_node_weight 从低到高按 up/right/down,每次用原
_keyboard_enter_succeeded 确认,最多 3 次,都不行调用原函数按 ↑。
每步记录下一列候选和进入的方向、类型、理由。LCP_TYPED_ROAD=0 关闭。

README 同时补上 P0-1 商店清单与本补丁的说明。

验证:排序、方向映射、逐个回退、识别失败与开关均有单测,复制的权重
表和网格参数与上游源码比对一致;全套 529 项通过。方向上无连线时按键
不会进错节点这一点需实机确认,BACKLOG 已写明对照方法。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-11-14 05:51:36 +00:00
co-authored by Claude Opus 5.5
parent baa362808c
commit 4c0038d657
5 changed files with 299 additions and 1 deletions
+176
View File
@@ -0,0 +1,176 @@
# -*- coding: utf-8 -*-
"""镜牢键盘寻路按节点类型选方向(BACKLOG P0-2)。
2026-11-14: config 开着简单键盘寻路, AALC 每步只按 ↑, 3 层只进过 1 次商店。
补丁仍只按键、不拖地图, 但先识别下一列节点类型, 按上游权重表挑方向。
"""
import ast
import sys
import types
import unittest
from pathlib import Path
from types import SimpleNamespace
from unittest import mock
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO))
from lc_mcp import aalc_patches as ap # noqa: E402
SEARCH_ROAD = REPO / 'vendor' / 'aalc' / 'tasks' / 'mirror' / 'search_road.py'
SCALE = 720 / 1440 # 本机 set_win_size=720
GAP_X = ap.UPSTREAM_ROAD_COLUMN_GAP * SCALE
GAP_Y = ap.UPSTREAM_ROAD_ROW_GAP * SCALE
BUS = (100, 400)
def node(kind, columns=1, rows=0):
"""相对巴士偏 columns 列、rows 行(向上为正)的节点。"""
return [kind, (BUS[0] + columns * GAP_X, BUS[1] - rows * GAP_Y)]
class RankNextNodesTest(unittest.TestCase):
def test_orders_next_column_by_upstream_weight(self):
nodes = [node('battle', rows=0), node('event', rows=1), node('shop', rows=-1),
node('event', columns=2), node('event', columns=0.3)]
self.assertEqual(ap.rank_next_nodes(BUS, nodes, SCALE),
[('U', 'event', 1), ('D', 'shop', 2), ('M', 'battle', 4)])
def test_ties_prefer_up_then_middle(self):
nodes = [node('battle', rows=-1), node('battle', rows=0), node('battle', rows=1)]
self.assertEqual([d for d, _, _ in ap.rank_next_nodes(BUS, nodes, SCALE)], ['U', 'M', 'D'])
def test_tolerates_detection_offsets_within_a_quarter_gap(self):
kind, (x, y) = node('shop', rows=1)
shifted = [kind, (x + GAP_X / 5, y + GAP_Y / 3)]
self.assertEqual(ap.rank_next_nodes(BUS, [shifted], SCALE), [('U', 'shop', 2)])
def test_keeps_the_better_detection_per_direction(self):
nodes = [node('risky_encounter', rows=0), node('event', rows=0)]
self.assertEqual(ap.rank_next_nodes(BUS, nodes, SCALE), [('M', 'event', 1)])
def test_rows_beyond_neighbours_and_unknown_types_are_ignored(self):
nodes = [node('event', rows=2), node('mystery', rows=0)]
self.assertEqual(ap.rank_next_nodes(BUS, nodes, SCALE), [])
def test_no_bus_or_no_nodes(self):
self.assertEqual(ap.rank_next_nodes(None, [node('event')], SCALE), [])
self.assertEqual(ap.rank_next_nodes(BUS, None, SCALE), [])
@unittest.skipUnless(SEARCH_ROAD.is_file(), 'vendor/aalc 子模块未检出')
class UpstreamConstantsTest(unittest.TestCase):
"""本地兜底常量必须与上游一致(运行时优先读上游的值)。"""
def setUp(self):
self.tree = ast.parse(SEARCH_ROAD.read_text(encoding='utf-8'))
def assignment(self, name):
for item in self.tree.body:
if isinstance(item, ast.Assign) and any(getattr(t, 'id', None) == name for t in item.targets):
return ast.literal_eval(item.value)
self.fail('上游没有 %s' % name)
def test_weights_and_grid_match_upstream(self):
self.assertEqual(self.assignment('all_node_weight'), ap.UPSTREAM_NODE_WEIGHT)
self.assertEqual(self.assignment('ROAD_COLUMN_GAP'), ap.UPSTREAM_ROAD_COLUMN_GAP)
self.assertEqual(self.assignment('ROAD_ROW_GAP'), ap.UPSTREAM_ROAD_ROW_GAP)
def test_direction_keys_match_upstream_keyboard_navigation(self):
source = SEARCH_ROAD.read_text(encoding='utf-8')
for direction, key in ap.ROAD_KEYS.items():
self.assertIn('if next_step == "%s":\n auto.key_press("%s")' % (direction, key)
if direction == 'U' else
'elif next_step == "%s":\n auto.key_press("%s")' % (direction, key),
source)
class TypedKeyboardRoadTest(unittest.TestCase):
def setUp(self):
self.lines = []
self.keys = []
self.original = mock.Mock(return_value='original')
self.entered = []
self.road = types.ModuleType('tasks.mirror.search_road')
self.road.search_road_simple_keyboard = self.original
self.road.identify_nodes = mock.Mock(return_value=[node('battle', rows=0), node('event', rows=1),
node('shop', rows=-1)])
self.road._keyboard_enter_succeeded = lambda: self.entered.pop(0)
self.mirror = types.ModuleType('tasks.mirror.mirror')
self.mirror.search_road_simple_keyboard = self.original
self.auto = SimpleNamespace(mouse_to_blank=mock.Mock(), key_press=self.keys.append,
find_element=mock.Mock(return_value=BUS))
self.cfg = SimpleNamespace(mirror_keyboard_navigation=True, set_win_size=720)
package = types.ModuleType('tasks.mirror')
package.mirror, package.search_road = self.mirror, self.road
tasks = types.ModuleType('tasks')
tasks.mirror = package
modules = {'tasks': tasks, 'tasks.mirror': package, 'tasks.mirror.mirror': self.mirror,
'tasks.mirror.search_road': self.road,
'module': types.ModuleType('module'),
'module.automation': SimpleNamespace(auto=self.auto),
'module.config': SimpleNamespace(cfg=self.cfg)}
for patch in (mock.patch.dict(sys.modules, modules), mock.patch.object(ap.time, 'sleep')):
patch.start()
self.addCleanup(patch.stop)
def install(self, env=None):
return ap.install_typed_keyboard_road(log=self.lines.append, env=env or {})
def test_replaces_both_namespaces_and_enters_the_best_direction(self):
self.assertIs(self.install(), self.original)
self.assertIs(self.mirror.search_road_simple_keyboard, self.road.search_road_simple_keyboard)
self.entered = [True]
self.assertTrue(self.mirror.search_road_simple_keyboard())
self.assertEqual(self.keys, ['up'])
self.original.assert_not_called()
log = '\n'.join(self.lines)
self.assertIn('U=event(1) D=shop(2) M=battle(4)', log)
self.assertIn('进入 U 方向的 event', log)
def test_tries_next_direction_when_one_cannot_be_entered(self):
self.install()
self.entered = [False, True]
self.assertTrue(self.road.search_road_simple_keyboard())
self.assertEqual(self.keys, ['up', 'down'])
self.assertIn('更优的方向没能进入', '\n'.join(self.lines))
def test_falls_back_to_original_when_nothing_can_be_entered(self):
self.install()
self.entered = [False, False, False]
self.assertEqual(self.road.search_road_simple_keyboard(), 'original')
self.assertEqual(self.keys, ['up', 'down', 'right'])
self.original.assert_called_once_with()
def test_falls_back_without_pressing_when_bus_or_nodes_are_missing(self):
self.install()
for bus, nodes in ((None, []), (BUS, None), (BUS, [])):
with self.subTest(bus=bus, nodes=nodes):
self.original.reset_mock()
self.auto.find_element.return_value = bus
self.road.identify_nodes.return_value = nodes
self.assertEqual(self.road.search_road_simple_keyboard(), 'original')
self.original.assert_called_once_with()
self.assertEqual(self.keys, [])
def test_recognition_error_falls_back(self):
self.install()
self.road.identify_nodes.side_effect = RuntimeError('onnx missing')
self.assertEqual(self.road.search_road_simple_keyboard(), 'original')
self.assertIn('RuntimeError', '\n'.join(self.lines))
def test_keyboard_navigation_off_keeps_upstream_behaviour(self):
self.install()
self.cfg.mirror_keyboard_navigation = False
self.assertEqual(self.road.search_road_simple_keyboard(), 'original')
self.road.identify_nodes.assert_not_called()
def test_env_switch_leaves_upstream_untouched(self):
self.assertIsNone(self.install(env={ap.TYPED_ROAD_ENV: '0'}))
self.assertIs(self.road.search_road_simple_keyboard, self.original)
self.assertIs(self.mirror.search_road_simple_keyboard, self.original)
if __name__ == '__main__':
unittest.main()