转交与互斥(原未提交改动,合入选队分支后提交): - MCP 初始化说明和工具描述引导常规日常/领奖/普通镜牢走 task_status -> run_task -> 轮询;jev_mirror_agent 默认转交 AALC, 明确的速通配置、决策模型或未完成速通局仍走逐步模式。 - 新增 ~/lc_mcp/game.lock:run_task 子进程与模块合成脚本互斥, 合成脚本另查 task_status,状态不明时不接管游戏。 - all 任务的 HDR 提示无 GUI 时直接确认并记录;预检按窗口位置 决定是否计标题栏,set_windows=false 跳过;levelup card/weakest 同步失败返回非零。急停提示改为 End 键。 镜牢结果(对接文档指出的缺口): - 预检拒绝空队列和没有适用当前难度队伍的配置:AALC 会一局不打 就以 0 退出。可用判定与 Mirror_task 的固定用途规则一致。 - 监听 mirror_signal 统计实际完成局数;少于应打局数记 [ERROR] 并以退出码 4 结束,task_status 为 failed。 - mirror 指定 count 时临时关闭 infinite_dungeons(否则次数变成 9999),原值写入恢复文件,结束或强杀后写回。 验证:Linux / Python 3.12 全套 507 项通过;git diff --check 通过。 用上游真实 Mirror_task 回放了无可用队伍和正常完成两种情况; Windows 实机仍需回归。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1111 lines
68 KiB
Python
1111 lines
68 KiB
Python
"""Delegate ordinary mirror runs to AALC; retain Jev control for special speedruns."""
|
||
import argparse
|
||
import functools
|
||
import json
|
||
import os
|
||
import re
|
||
import time
|
||
from pathlib import Path
|
||
|
||
from mcp_session import Session as Bridge, SessionCancelled, SessionTimeout
|
||
from game_scenes import battle_is_terminal, battle_is_visible, death_retry_screen
|
||
from run_state import (RESUMABLE, load_json, prepare_run, save_json, record_claim,
|
||
claim_is_current, complete_run)
|
||
from decision_router import DecisionRouter, DecisionRequired
|
||
from jev_client import request_answer
|
||
from lc_scene_aliases import normalize
|
||
|
||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||
MIRROR_ASSETS = REPO_ROOT / 'vendor' / 'aalc' / 'assets' / 'images' / 'default' / 'share' / 'mirror'
|
||
RUNTIME_HOME = Path(os.environ.get('LC_JEV_HOME', str(Path.home() / 'lc_mcp' / 'jev')))
|
||
STOP_FILE = RUNTIME_HOME / 'JEV_STOP'
|
||
|
||
SYSTEM_RULES = (
|
||
'你负责识别固定场景、路线和所有下一步决策。目标是最快完成当前一局镜牢。'
|
||
'只能选择所给候选;不要反复点击未生效的按钮。战斗选 battle 宏,P+回车,不用空格。'
|
||
'刚选路线、回车开战或上一动作是战斗时,特写或加载没有OCR文字很正常;选短等或战斗宏,不仅因OCR为空要求分析。'
|
||
'遇到事件判定先点SKIP跳过对话,跳完后再选择事件选项、罪人或进行判定;剧情同样优先SKIP。'
|
||
'事件当前有“选择后获得”且无需判定、无需战斗的选项时直接选择它,不反复滚动;避开写着战斗胜利后的选项。'
|
||
'确定确认/进入/开始战斗直接执行。地图优先可达事件和商店,减少战斗。'
|
||
'战斗奖励优先经费,纯P无需囤积E.G.O资源;可附带破裂合成材料的经费奖励优先。'
|
||
'选好路线直接回车进入,关卡编队界面也直接回车开战;保留原编队,不再检查人员。'
|
||
'候选路线已由程序核对可达和节点类型,无需再确认已完成的开局配置。多个同类型节点都可达时默认正前方,不因没有事件节点而要求分析。'
|
||
'保留当前编队。开局三级饰品三选一必须选择标准负载电池,再确认领取;不能当成额外观测拒绝。'
|
||
'成捆的符咒++、标准负载电池和鸡胸为核心,保留不可出售或合成;仅略过单独的额外观测。其他苦难避开敌方体力上限和防御等级。'
|
||
'3层起辛克莱1换3,黑兽奥提斯2换3,其他出战1换3。只在商店强化饰品。'
|
||
'快感尽量在2、3层商店合成,并当场强化至二级++;4层仅补做。观测允许领取破裂体系雷枝,之后到商店强化,保留核心和技能预算。'
|
||
'商店没有出战角色可替换技能时允许刷新,当前商店最多4次;有可替换角色先按技能表替换。'
|
||
'第一至第四层禁止选择2号线卡包,第五层必选2号线;刷新次数为零时允许使用卡包观测。层末选完奖励连续回车进入下一层。离开商店也用回车;未生效再点当前按钮。'
|
||
'卡包信息不清先点对应放大镜查看;核心饰品齐全时按通关速度选择现有卡包,不为可选饰品反复刷新。'
|
||
'信息不足、重复操作仍无效、苦难无法明确核对,或你判断需要主助手分析时选 requires_analysis。'
|
||
)
|
||
|
||
|
||
def text_items(state):
|
||
return state.get('items') or []
|
||
|
||
|
||
def scene_fingerprint(state):
|
||
"""Ignore changing clocks and counters when checking failed confirmations."""
|
||
return tuple(item['text'] for item in text_items(state)
|
||
if item.get('score', 0) > .93
|
||
and not re.fullmatch(r'[0-9:.]+', item['text'].replace(' ', '')))
|
||
|
||
|
||
_SCREEN_CACHE = {}
|
||
|
||
|
||
def screen_image(path, mode):
|
||
"""按模式解码当前截图并缓存: 一帧里多次判色/裁剪只解码一次整张 PNG (1080p 约 75ms/次)。
|
||
键含修改时间和大小, 同路径写入新内容时不会命中旧图。"""
|
||
from PIL import Image
|
||
st = os.stat(path)
|
||
key = (str(path), st.st_mtime_ns, st.st_size, mode)
|
||
image = _SCREEN_CACHE.get(key)
|
||
if image is None:
|
||
if len(_SCREEN_CACHE) >= 4:
|
||
_SCREEN_CACHE.clear()
|
||
with Image.open(path) as source:
|
||
image = source.convert(mode)
|
||
_SCREEN_CACHE[key] = image
|
||
return image
|
||
|
||
|
||
def button_enabled(state, item):
|
||
try:
|
||
import numpy as np
|
||
x, y, w, h = (int(item[k]) for k in ('x', 'y', 'w', 'h'))
|
||
crop = np.array(screen_image(state['screen']['path'], 'L').crop((x, y, x + w, y + h)))
|
||
return float(np.quantile(crop, 0.9)) > 85
|
||
except (OSError, KeyError, ImportError):
|
||
return False
|
||
|
||
|
||
def purchased_slot(state, item):
|
||
"""OCR may omit the orange Purchased badge above a bought skill slot."""
|
||
try:
|
||
import cv2
|
||
import numpy as np
|
||
template_path = REPO_ROOT / 'assets' / 'ui' / 'purchased_english.png'
|
||
template = np.array(screen_image(template_path, 'L'))
|
||
width = state['screen'].get('w', 1920)
|
||
height = state['screen'].get('h', 1080)
|
||
if width != 1920:
|
||
template = cv2.resize(template, None, fx=width/1920, fy=width/1920)
|
||
cx = item['x'] + item['w']/2
|
||
area = np.array(screen_image(state['screen']['path'], 'L').crop(
|
||
(round(cx-width*.075), round(item['y']-height*.2),
|
||
round(cx+width*.075), round(item['y']-height*.09))))
|
||
if area.shape[0] < template.shape[0] or area.shape[1] < template.shape[1]:
|
||
return False
|
||
return float(cv2.matchTemplate(area, template, cv2.TM_CCOEFF_NORMED).max()) >= .9
|
||
except (OSError, KeyError, ImportError):
|
||
return False
|
||
|
||
|
||
@functools.lru_cache(maxsize=64)
|
||
def _map_template(kind, name, scale):
|
||
"""地图识别模板, 按截图比例缩放; 窗口尺寸不变时每帧复用, 不再重复读盘和计算特征点。
|
||
返回的数组是共享缓存, 调用方不能原地修改。"""
|
||
import cv2
|
||
import numpy as np
|
||
from PIL import Image
|
||
path = MIRROR_ASSETS / name
|
||
if kind == 'bus':
|
||
with Image.open(path) as image:
|
||
gray = cv2.cvtColor(np.array(image.convert('RGB')), cv2.COLOR_RGB2GRAY)
|
||
return cv2.resize(gray, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
||
if kind == 'arrow':
|
||
# 等价于 cv2.imread(path, 0); imread 在 Windows 上打不开含中文的路径
|
||
gray = cv2.imdecode(np.fromfile(str(path), dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
|
||
return cv2.resize(gray, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
|
||
# 节点图标: ORB 描述子只取决于模板和参数
|
||
with Image.open(path) as image:
|
||
gray = cv2.resize(np.array(image.convert('L')), None, fx=2*scale, fy=2*scale)
|
||
return cv2.ORB_create(nfeatures=1000, scaleFactor=1.2, edgeThreshold=10).detectAndCompute(gray, None)[1]
|
||
|
||
|
||
def map_candidates(state):
|
||
"""Only observe reachable nodes; Jev chooses their route."""
|
||
import cv2
|
||
import numpy as np
|
||
rgb = np.array(screen_image(state['screen']['path'], 'RGB'))
|
||
gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY)
|
||
scale = min(rgb.shape[0] / 1440, rgb.shape[1] / 2560)
|
||
bus = _map_template('bus', 'mybus_default_distance.png', scale)
|
||
_, confidence, _, location = cv2.minMaxLoc(cv2.matchTemplate(gray, bus, cv2.TM_CCOEFF_NORMED))
|
||
if confidence < 0.88:
|
||
return {}
|
||
bx, by = location[0] + bus.shape[1] // 2, location[1] + bus.shape[0] // 2
|
||
result = {}
|
||
orb = cv2.ORB_create(nfeatures=1000, scaleFactor=1.2, edgeThreshold=10)
|
||
for key, dy, direction in (('w', -400, '上方'), ('d', 50, '正前方'), ('s', 450, '下方')):
|
||
x, y = int(bx + 500 * scale), int(by + dy * scale)
|
||
if not (0 < x < rgb.shape[1] and 0 < y < rgb.shape[0]*.82):
|
||
continue
|
||
radius = int(125 * scale)
|
||
# Event and shop nodes remain purple even when reachable. Their
|
||
# connecting arrow, rather than node color, indicates availability.
|
||
ax, ay = int(bx + 250*scale), int(by + dy*scale/2)
|
||
arrow_roi = gray[max(0,ay-radius):min(gray.shape[0],ay+radius),
|
||
max(0,ax-radius):min(gray.shape[1],ax+radius)]
|
||
arrow_name = {'w':'up','d':'mid','s':'down'}[key]
|
||
template = _map_template('arrow', 'road_in_mir/%s.png' % arrow_name, scale)
|
||
arrow_reachable = False
|
||
if arrow_roi.shape[0] >= template.shape[0] and arrow_roi.shape[1] >= template.shape[1]:
|
||
_, arrow_score, _, arrow_pos = cv2.minMaxLoc(cv2.matchTemplate(arrow_roi, template, cv2.TM_CCOEFF_NORMED))
|
||
sample = arrow_roi[arrow_pos[1]:arrow_pos[1]+template.shape[0],
|
||
arrow_pos[0]:arrow_pos[0]+template.shape[1]]
|
||
arrow_reachable = arrow_score > .85 and float(np.quantile(sample,.9)) > 90
|
||
roi = rgb[max(0,y-radius):min(rgb.shape[0],y+radius), max(0,x-radius):min(rgb.shape[1],x+radius)]
|
||
hsv = cv2.cvtColor(roi, cv2.COLOR_RGB2HSV)
|
||
red = ((hsv[:,:,0] < 12) | (hsv[:,:,0] > 168)) & (hsv[:,:,1] > 150) & (hsv[:,:,2] > 150)
|
||
green = (hsv[:,:,0] > 27) & (hsv[:,:,0] < 85) & (hsv[:,:,1] > 110) & (hsv[:,:,2] > 150)
|
||
white = (hsv[:,:,1] < 100) & (hsv[:,:,2] > 175)
|
||
cyan = (hsv[:,:,0] >= 80) & (hsv[:,:,0] <= 110) & (hsv[:,:,1] > 110) & (hsv[:,:,2] > 150)
|
||
colored = int((red | green | cyan).sum()) >= 80
|
||
if not colored and int(white.sum()) < 80 and not arrow_reachable:
|
||
continue
|
||
target = cv2.resize(cv2.cvtColor(roi, cv2.COLOR_RGB2GRAY), None, fx=2, fy=2)
|
||
_, target_desc = orb.detectAndCompute(target, None)
|
||
scores = []
|
||
if target_desc is not None:
|
||
for name, label in (('shop','商店'),('event','事件'),('battle','普通战斗'),
|
||
('risky_encounter','精英战斗'),('focused_encounter','集中战斗')):
|
||
desc = _map_template('icon', 'road_in_mir/%s.png' % name, scale)
|
||
if desc is not None:
|
||
matches = cv2.BFMatcher(cv2.NORM_HAMMING).knnMatch(desc, target_desc, k=2)
|
||
score = sum(len(pair) == 2 and pair[0].distance < 0.7*pair[1].distance for pair in matches)
|
||
scores.append((score, label))
|
||
score, label = max(scores, default=(0, '类型未识别'))
|
||
if not colored and score < 8 and not arrow_reachable:
|
||
continue
|
||
if score < 8:
|
||
label = '类型未识别'
|
||
result['route_' + key] = {
|
||
'description': '%s有高亮可达节点:%s,图标匹配数%d,中心(%d,%d)。按方向键选中后直接回车进入,无需二次确认。' % (direction,label,score,x,y),
|
||
'action': {'tool': 'route', 'args': {'key': key}},
|
||
}
|
||
return result
|
||
|
||
|
||
def candidates(state, previous_battle=False, refresh_used=0):
|
||
items = text_items(state)
|
||
texts = [item['text'].replace(' ', '') for item in items]
|
||
combined = '\n'.join(texts)
|
||
width = state.get('screen', {}).get('w', 1920)
|
||
height = state.get('screen', {}).get('h', 1080)
|
||
options = {'requires_analysis': {
|
||
'description': '你判断信息不足或需要主助手分析:暂停并返回当前游戏截图和问题。',
|
||
'action': {'tool': 'requires_analysis', 'args': {}},
|
||
}, 'wait': {
|
||
'description': '短暂等待0.7秒后重新读取画面;CONNECTING或界面动画尚未完成时用,不发送输入。',
|
||
'action': {'tool': 'wait', 'args': {'seconds': 0.7}},
|
||
}}
|
||
# Refresh requests leave the previous stock visible until networking ends.
|
||
# Never buy, refresh again, or leave based on that stale inventory.
|
||
if any(re.search(r'CON+ECTING|LOADING', t.upper()) for t in texts):
|
||
return options
|
||
if state.get('claim_requested'):
|
||
# After claiming, never let the generic Enter action start another run.
|
||
if ('探索状态' in texts and '进入' in texts and '探索中' not in texts
|
||
or '驾驶席' in texts and '罪人' in texts):
|
||
options['run_complete'] = {'description': '奖励领取后已返回大厅,结束本局。',
|
||
'action': {'tool': 'run_complete', 'args': {}}}
|
||
else:
|
||
confirm = next((i for i in items if i['text'].replace(' ', '') in ('确认','继续')
|
||
and button_enabled(state, i)), None)
|
||
if confirm:
|
||
options['modal_confirm'] = {'description': '确认最终领取弹窗。',
|
||
'action': {'tool': 'click', 'args': {'x': confirm['x']+confirm['w']//2,
|
||
'y': confirm['y']+confirm['h']//2}}}
|
||
return options
|
||
if death_retry_screen(texts):
|
||
return options
|
||
if '服务器发生错误' in combined:
|
||
retry = next((i for i in items if i['text'].replace(' ', '') == '重试'), None)
|
||
if retry:
|
||
options['retry_network'] = {'description': '重试当前游戏服务器错误弹窗,不操作背景商店。',
|
||
'action': {'tool': 'click', 'args': {'x': retry['x']+retry['w']//2, 'y': retry['y']+retry['h']//2}}}
|
||
return options
|
||
if '探索完成' in texts:
|
||
claim = next((i for i in items if i['text'].replace(' ', '') == '领取'
|
||
and button_enabled(state,i)), None)
|
||
if claim:
|
||
options['claim_final'] = {
|
||
'description':'最终探索完成:点击领取并按回车确认奖励。',
|
||
'action':{'tool':'claim_final','args':{'x':claim['x']+claim['w']//2,
|
||
'y':claim['y']+claim['h']//2}},
|
||
}
|
||
return options
|
||
gift_headers = sum(t == '获得E.G.O饰品' for t in texts)
|
||
if gift_headers == 1 and '拒绝饰品' in texts and '选择' in texts:
|
||
core = any(name in combined for name in ('成捆的符咒','标准负载电池','干巴柴涩鸡胸肉','干松紧致鸡胸肉','快感','雷枝'))
|
||
if core and '雷枝' not in combined:
|
||
return options
|
||
if not core:
|
||
target = next(i for i in items if i['text'].replace(' ','') == '拒绝饰品')
|
||
options['decline_extra_observation'] = {'description': '拒绝单张无关额外饰品。',
|
||
'action': {'tool':'click','args':{'x':target['x']+target['w']//2,'y':target['y']+target['h']//2}}}
|
||
return options
|
||
if any(t in ('判定成功', '判定失败', '结果') for t in texts):
|
||
continuation = next((i for i in items if i['text'].replace(' ', '') in ('继续', '开始战斗')
|
||
and button_enabled(state,i)), None)
|
||
options['event_finish'] = {
|
||
'description': '判定结果已出现,快速连按回车结束事件并返回地图。',
|
||
'action': {'tool':'advance_dialog','args':{'presses':5}},
|
||
}
|
||
if continuation:
|
||
options['event_finish']['fallback_action']={'tool':'click','args':{
|
||
'x':continuation['x']+continuation['w']//2,'y':continuation['y']+continuation['h']//2}}
|
||
return options
|
||
for index, item in enumerate(items):
|
||
if item['text'].replace(' ', '').upper() == 'SKIP' and button_enabled(state, item):
|
||
options['skip_dialog'] = {
|
||
'description': '先点击SKIP跳过当前对话,再读取事件选项或判定;当前不选择罪人或进行判定。',
|
||
'action': {'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
|
||
'y': item['y'] + item['h']//2}},
|
||
}
|
||
return options
|
||
if any(re.fullmatch(r'REC[●.]*', t, re.I) for t in texts) and '选项' not in texts and not any(t in combined for t in ('选择一位罪人', '进行判定', '判定成功', '判定失败', '结果')):
|
||
options['event_advance'] = {'description':'事件选项已经确定,按回车快速推进后续文本。', 'action':{'tool':'advance_dialog','args':{'presses':5}}}
|
||
return options
|
||
if '应该让谁' in combined and '选择一位罪人' not in combined and '进行判定' not in combined:
|
||
options['event_advance'] = {
|
||
'description': '对话已经跳过,按回车继续到选择罪人的判定界面。',
|
||
'action': {'tool': 'advance_dialog', 'args': {}},
|
||
}
|
||
return options
|
||
for index, item in enumerate(items):
|
||
if item['text'].replace(' ', '').lstrip('×xX') == '关闭':
|
||
options['close_%d' % index] = {
|
||
'description': '关闭当前饰品查看/强化窗口,返回商店继续换技能;电池不是当前必要强化目标。',
|
||
'action': {'tool': 'keypress', 'args': {'key': 'esc'}},
|
||
}
|
||
if '迷宫进度' in texts:
|
||
for index, item in enumerate(items):
|
||
if item['text'].replace(' ', '') == '继续':
|
||
options['resume_game'] = {
|
||
'description': '关闭迷宫进度菜单并继续当前商店操作。',
|
||
'action': {'tool': 'click', 'args': {'x': item['x']+item['w']//2,
|
||
'y': item['y']+item['h']//2}},
|
||
}
|
||
return options
|
||
if any('是否消耗' in t and '技能替换' in t for t in texts):
|
||
header = next((t for t in texts if '替换' in t and '技能' in t and '是否' not in t), '')
|
||
skills = sorted([i for i in items if re.fullmatch(r'SKILL\s*[123]', i['text'])
|
||
and height*.28 < i['y'] < height*.5], key=lambda i: i['x'])
|
||
target = ('SKILL2', 'SKILL3') if '奥提斯' in header else (
|
||
('SKILL1', 'SKILL2') if '辛克莱' in header and state.get('floor', 3) < 3 else ('SKILL1', 'SKILL3'))
|
||
if tuple(i['text'].replace(' ', '') for i in skills) == target:
|
||
for item in items:
|
||
if item['text'] in ('购买', '确认') and button_enabled(state, item):
|
||
options['buy_swap'] = {
|
||
'description': '已核对本层所需技能替换 %s→%s,点击购买完成。' % target,
|
||
'action': {'tool': 'keypress', 'args': {'key': 'enter'}},
|
||
'fallback_action': {'tool': 'click', 'args': {'x': item['x']+item['w']//2,
|
||
'y': item['y']+item['h']//2}},
|
||
}
|
||
return options
|
||
if any('当前持有技能' in t for t in texts):
|
||
header = next((t for t in texts if '替换' in t and '技能' in t), '')
|
||
target = ('SKILL2', 'SKILL3') if '奥提斯' in header else (
|
||
('SKILL1', 'SKILL2') if '辛克莱' in header and state.get('floor', 3) < 3 else ('SKILL1', 'SKILL3'))
|
||
target_selected = False
|
||
for number, fraction in enumerate((0.244, 0.499, 0.752), 1):
|
||
cx = round(width*fraction)
|
||
skill_texts = [i for i in items if 'SKILL' in i['text'].upper()
|
||
and abs(i['x']+i['w']/2 - (cx + width*.075)) < width*.05
|
||
and height*.3 < i['y'] < height*.7]
|
||
skill_texts.sort(key=lambda i: i['y'])
|
||
if len(skill_texts) == 2:
|
||
if tuple(i['text'].upper().replace(' ', '') for i in skill_texts) != target:
|
||
continue
|
||
detail = skill_texts[0]['text'] + ' → ' + skill_texts[1]['text']
|
||
options['skill_swap_%d' % number] = {
|
||
'description': header + ':第%d项 %s;按楼层技能表选这一项。' % (number, detail),
|
||
'action': {'tool': 'click', 'args': {'x': cx, 'y': round(height*.45)}},
|
||
}
|
||
try:
|
||
import cv2
|
||
import numpy as np
|
||
border = np.array(screen_image(state['screen']['path'], 'RGB').crop(
|
||
(int(cx-width*.12), int(height*.31), int(cx-width*.107), int(height*.69))))
|
||
hsv = cv2.cvtColor(border, cv2.COLOR_RGB2HSV)
|
||
target_selected = float(((hsv[:,:,0] > 8) & (hsv[:,:,0] < 35) &
|
||
(hsv[:,:,1] > 200) & (hsv[:,:,2] > 220)).mean()) > .18
|
||
except (KeyError, OSError, ImportError):
|
||
pass
|
||
for item in items:
|
||
if target_selected and item['text'].replace(' ', '') == '确认' and button_enabled(state, item):
|
||
options['confirm_swap'] = {
|
||
'description': '确认已经选中的技能替换,支付本项费用。未选项目时应先点技能替换项。',
|
||
'action': {'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
|
||
'y': item['y'] + item['h']//2}},
|
||
}
|
||
return options
|
||
if any('获得E.G.O饰品!' in t or '获得E.G.O饰品!' in t for t in texts):
|
||
item=next((i for i in items if i['text'].replace(' ','')=='确认' and width*.4 < i['x']+i['w']/2 < width*.6),None)
|
||
if item:
|
||
options['modal_confirm']={'description':'按回车快速确认当前获得饰品弹窗。','action':{'tool':'keypress','args':{'key':'enter'}}}
|
||
options['modal_confirm']['fallback_action']={'tool':'click','args':{'x':item['x']+item['w']//2,'y':item['y']+item['h']//2}}
|
||
return options
|
||
if '追加的苦难' in combined and '获得E.G.O' in combined:
|
||
for number, fraction in enumerate((.188, .395, .60, .813), 1):
|
||
cx = round(width*fraction)
|
||
details = ';'.join(i['text'] for i in items
|
||
if abs(i['x']+i['w']/2-cx) < width*.102
|
||
and height*.22 < i['y'] < height*.77)
|
||
if details:
|
||
options['hardship_%d' % number] = {
|
||
'description': '层末第%d张饰品与苦难:%s。必需饰品优先;其他避开体力上限和防御等级。' % (number,details),
|
||
'action': {'tool':'click', 'args':{'x':cx, 'y':round(height*.43)}},
|
||
}
|
||
for item in items:
|
||
count=re.search(r'(\d+)\s*/\s*(\d+)',item['text'])
|
||
if ('选择' in item['text'] and count and int(count[2])>0
|
||
and int(count[1])>=int(count[2]) and button_enabled(state,item)):
|
||
options['hardship_confirm'] = {
|
||
'description':'选择数量足够后确认层末饰品和苦难,直接前进。',
|
||
'action':{'tool':'advance_dialog','args':{'presses':3}},
|
||
'fallback_action':{'tool':'click','args':{'x':item['x']+item['w']//2,'y':item['y']+item['h']//2}},
|
||
}
|
||
return options
|
||
# Nested confirmation/detail windows override the reward/pack behind them.
|
||
modal_question = ('确认' in texts and any('取消' in t for t in texts)
|
||
and any('吗?' in t or '吗?' in t or '要离开商店吗' in t for t in texts))
|
||
pack_detail = '出现头目' in combined and '详细内容' in combined
|
||
if modal_question or pack_detail:
|
||
buttons = [i for i in items if i['text'].replace(' ', '') == '确认'
|
||
and button_enabled(state, i)]
|
||
if buttons:
|
||
item = min(buttons, key=lambda i: i['y'])
|
||
options['modal_confirm'] = {
|
||
'description': '确认当前最上层弹窗;不操作背景奖励卡或卡包。',
|
||
'action': ({'tool': 'keypress', 'args': {'key': 'enter'}}
|
||
if any('要离开商店吗' in t for t in texts) else
|
||
{'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
|
||
'y': item['y'] + item['h']//2}}),
|
||
}
|
||
return options
|
||
if any('选择遭遇战奖励卡' in t for t in texts):
|
||
for index,item in enumerate(items):
|
||
if height*.48 < item['y'] < height*.65 and any(t in item['text'] for t in ('经费', '资源', '体力', '理智')):
|
||
options['reward_card_anchor_%d'%index]={'description':'当前奖励卡:'+item['text'], 'action':{'tool':'click','args':{'x':item['x']+item['w']//2,'y':round(item['y']-height*.15)}}}
|
||
for number, fraction in enumerate((0.355, 0.514, 0.674), 1):
|
||
cx = round(width*fraction)
|
||
detail = ';'.join(i['text'] for i in items
|
||
if abs(i['x']+i['w']/2-cx) < width*.07 and height*.48 < i['y'] < height*.65)
|
||
if detail:
|
||
options['reward_card_%d' % number] = {
|
||
'description': '遭遇战奖励卡第%d张:%s。优先减少额外操作,略过无关饰品。' % (number, detail),
|
||
'action': {'tool': 'click', 'args': {'x': cx, 'y': round(height*.43)}},
|
||
}
|
||
if not any('0/1' in t for t in texts):
|
||
for index, item in enumerate(items):
|
||
if item['text'].replace(' ', '') == '确认' and button_enabled(state, item):
|
||
options['confirm_reward'] = {
|
||
'description': '奖励卡已选中,确认领取。',
|
||
'action': {'tool': 'keypress', 'args': {'key': 'enter'}},
|
||
}
|
||
return options
|
||
# A confirmation dialog blocks the controls underneath it. Keep its
|
||
# observed coordinates instead of re-running an ambiguous text search.
|
||
confirmations = [(index, item) for index, item in enumerate(items)
|
||
if item['text'].replace(' ', '') == '确认']
|
||
modal = confirmations and (any('取消' in t for t in texts) or
|
||
any('吗?' in t or '吗?' in t for t in texts))
|
||
if modal:
|
||
for index, item in confirmations:
|
||
if button_enabled(state, item):
|
||
options['confirm_%d' % index] = {
|
||
'description': '当前确认弹窗的“确认”按钮;确认本次操作,背景控件被弹窗遮挡。',
|
||
'action': {'tool': 'click', 'args': {'x': item['x'] + item['w'] // 2,
|
||
'y': item['y'] + item['h'] // 2}},
|
||
}
|
||
return options
|
||
# The blessing's tier-III Battery choice stays mandatory. Thunderbranch
|
||
# is allowed in separate observations, not as a replacement for Battery.
|
||
gift_selection = (any(re.match(r'^(?:选择|可选择)(?:E\.?G\.?O\.?)?饰品', t, re.I) for t in texts)
|
||
or '饰品观测' in combined or '拒绝饰品' in texts)
|
||
if gift_selection:
|
||
battery = next((i for i in items if re.search(r'(标准负载|标准负荷|Standard-duty).*?(电池|Battery)', i['text'], re.I)), None)
|
||
thunder = next((i for i in items if re.fullmatch(r'雷枝(?:\+{1,2})?|Thunderbranch(?:\+{1,2})?', i['text'].replace(' ',''), re.I)), None)
|
||
unselected = any(re.search(r'(?:选择|可选).*?0/\d', t) for t in texts)
|
||
confirm = next((i for i in items if i['text'].replace(' ', '') in ('确认', '选择')
|
||
and button_enabled(state, i)), None)
|
||
if battery:
|
||
if confirm and not unselected:
|
||
options['confirm_startup_battery'] = {
|
||
'description': '电池选项已可确认,领取开局必需的标准负载电池。',
|
||
'action': {'tool': 'click', 'args': {'x': confirm['x']+confirm['w']//2,
|
||
'y': confirm['y']+confirm['h']//2}},
|
||
}
|
||
else:
|
||
options['select_startup_battery'] = {
|
||
'description': '选择开局必需饰品标准负载电池;此三选一界面不可拒绝。',
|
||
'action': {'tool': 'click', 'args': {'x': battery['x']+battery['w']//2,
|
||
'y': battery['y']+battery['h']//2}},
|
||
}
|
||
elif thunder and (gift_headers == 1 or state.get('battery_obtained')
|
||
or any(re.search(r'(额外|追加).*观测', t) for t in texts)):
|
||
if confirm and not unselected:
|
||
options['confirm_thunderbranch'] = {'description':'确认领取用户允许的雷枝,之后在商店强化。',
|
||
'action':{'tool':'keypress','args':{'key':'enter'}},
|
||
'fallback_action':{'tool':'click','args':{'x':confirm['x']+confirm['w']//2,'y':confirm['y']+confirm['h']//2}}}
|
||
else:
|
||
options['select_thunderbranch'] = {'description':'选择破裂体系雷枝;这是用户允许的观测例外。',
|
||
'action':{'tool':'click','args':{'x':thunder['x']+thunder['w']//2,'y':thunder['y']+thunder['h']//2}}}
|
||
# Unrecognized choices need analysis; never silently skip a required gift.
|
||
# A separately labelled extra observation may still be declined.
|
||
elif any(re.search(r'(额外|追加).*观测', t) for t in texts):
|
||
refuse = next((i for i in items if i['text'].replace(' ', '') == '拒绝饰品'
|
||
and button_enabled(state, i)), None)
|
||
if refuse:
|
||
options['decline_extra_observation'] = {
|
||
'description': '拒绝明确标注的额外观测,不影响开局三级饰品选择。',
|
||
'action': {'tool': 'click', 'args': {'x': refuse['x']+refuse['w']//2,
|
||
'y': refuse['y']+refuse['h']//2}},
|
||
}
|
||
return options
|
||
if any('选择' in t and '层主题卡包' in t for t in texts):
|
||
# This screen has five clipped bags; title OCR anchors each visible bag.
|
||
for number, fraction in enumerate((0.17, 0.335, 0.5, 0.665, 0.83), 1):
|
||
cx = round(width * fraction)
|
||
titles = [i['text'] for i in items if abs(i['x'] + i['w']/2 - cx) < width*.06
|
||
and height*.60 < i['y'] < height*.68]
|
||
label = '/'.join(titles) or '标题OCR暂未识别'
|
||
line_two = bool(re.search(r'(?:2|二)号线|LINE\s*2\b', label, re.I))
|
||
floor = state.get('floor', 1)
|
||
if floor < 5 and line_two:
|
||
continue
|
||
if floor == 5 and not line_two:
|
||
continue
|
||
options['pack_inspect_%d' % number] = {
|
||
'description': '查看第%d个卡包“%s”的放大镜详情,核对怪物和通关奖励。' % (number, label),
|
||
'action': {'tool': 'click', 'args': {'x': round(cx + width*.042), 'y': round(height*.303)}},
|
||
}
|
||
if titles:
|
||
options['pack_drag_%d' % number] = {
|
||
'description': '选择第%d个卡包“%s”:长按卡包中部并缓慢向下拖到底。' % (number, label),
|
||
'action': {'tool': 'drag', 'args': {'from_x': cx, 'from_y': round(height*.45),
|
||
'to_x': cx, 'to_y': round(height*.91), 'duration': 1.8}},
|
||
}
|
||
if state.get('floor', 1) == 5:
|
||
required = next((k for k in options if k.startswith('pack_drag_')), None)
|
||
if required:
|
||
options['required_pack'] = options[required]
|
||
return {k:v for k,v in options.items() if k in ('wait','requires_analysis','required_pack')}
|
||
refresh = next((i for i in items if '刷新' in i['text'] and button_enabled(state, i)), None)
|
||
zero = any(re.search(r'0/3', t) for t in texts)
|
||
if refresh and not zero:
|
||
options['pack_refresh'] = {'description':'第五层二号线尚未出现,使用剩余卡包刷新。',
|
||
'action':{'tool':'click','args':{'x':refresh['x']+refresh['w']//2,'y':refresh['y']+refresh['h']//2}}}
|
||
return options
|
||
in_shop = any(t in ('商店','特殊商店') for t in texts) and '刷新' in texts
|
||
party = ('辛克莱', '鸿璐', '希斯克利夫', '奥提斯', '格里高尔', '良秀', '浮士德')
|
||
swaps = [(index, item) for index, item in enumerate(items)
|
||
if '替换' in item['text'] and any(name in item['text'] for name in party)
|
||
and not purchased_slot(state, item)
|
||
and not any('已购买' in sold['text']
|
||
and abs(sold['x']+sold['w']/2-item['x']-item['w']/2) < width*.07
|
||
and 0 < item['y']-sold['y'] < height*.25 for sold in items)]
|
||
if in_shop and state.get('floor', 1) >= 2 and not state.get('symbol_upgraded', False):
|
||
return options
|
||
if in_shop:
|
||
pending = 'thrill' if state.get('thrill_pending') else 'thunderbranch' if state.get('thunderbranch_pending') else None
|
||
if pending:
|
||
name='快感++' if pending=='thrill' else '雷枝'
|
||
options['requires_analysis']['description']='离开商店前需在强化面板处理%s,付款后核对实际库存;保留符咒++及关键技能预算。' % name
|
||
item=next((i for i in items if i['text'].replace(' ','')=='强化饰品' and button_enabled(state,i)),None)
|
||
if item:
|
||
options['prepare_'+pending]={'description':'打开商店强化面板,处理'+name+'。',
|
||
'action':{'tool':'click','args':{'x':item['x']+item['w']//2,'y':item['y']+item['h']//2}}}
|
||
return options
|
||
for index, item in swaps:
|
||
options['swap_%d' % index] = {
|
||
'description': '商店出战角色技能替换入口:' + item['text'] + ';打开后核对技能表。',
|
||
'action': {'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
|
||
'y': max(0, item['y'] - round(height*.095))}},
|
||
}
|
||
if not swaps and refresh_used < 4:
|
||
refreshes = [item for item in items if item['text'].replace(' ', '') == '刷新'
|
||
and button_enabled(state, item)]
|
||
if refreshes:
|
||
# The keyword panel has another Refresh further right.
|
||
# Refresh stock, never overwrite this with keyword Refresh.
|
||
item = min(refreshes, key=lambda value: value['x'])
|
||
options['shop_refresh'] = {
|
||
'description': '没有出战角色可替换技能,刷新商店;本店已刷新%d/4次。' % refresh_used,
|
||
'action': {'tool': 'click', 'args': {'x': item['x'] + item['w'] // 2,
|
||
'y': item['y'] + item['h'] // 2}},
|
||
}
|
||
end_screen = battle_is_terminal(state)
|
||
battle = battle_is_visible(state)
|
||
if not end_screen and (battle or previous_battle):
|
||
options['battle'] = {'description': '仍处于战斗或特写:执行短批次 P→回车,两次后检查结算,不分析拼点。',
|
||
'action': {'tool': 'battle', 'args': {'rounds': 2}}}
|
||
known_buttons = ('确认', '继续', '进入', '开始战斗', 'SKIP', '进行判定',
|
||
'离开', '下一层', '前往下一层', '领取奖励', '结束探索', 'TOUCHTOSTART', '选择')
|
||
for index, item in enumerate(items):
|
||
label = item['text'].replace(' ', '')
|
||
if in_shop and (swaps or 'shop_refresh' in options) and label == '离开':
|
||
continue
|
||
if label in known_buttons and button_enabled(state, item):
|
||
options['button_%d' % index] = {
|
||
'description': '当前可见且非灰色按钮:%s;点击 OCR 中心。' % label,
|
||
# 直接点已核对过的这一项: 重新按文字搜索要多一次截图+OCR, 还可能命中背景里的同名按钮
|
||
'action': ({'tool': 'keypress', 'args': {'key': 'enter'}} if label in ('进入', '开始战斗', '进行判定', '离开')
|
||
else {'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
|
||
'y': item['y'] + item['h']//2}}),
|
||
}
|
||
if options['button_%d' % index]['action']['tool'] == 'keypress':
|
||
options['button_%d' % index]['fallback_action'] = {
|
||
'tool': 'click', 'args': {'x': item['x'] + item['w']//2,
|
||
'y': item['y'] + item['h']//2}}
|
||
if (not battle and not in_shop and state.get('screen', {}).get('path')
|
||
and Path(state['screen']['path']).is_file()
|
||
and not any(t in combined for t in ('确认', '继续', '获得E.G.O', 'SKIP', '选项'))):
|
||
options.update(map_candidates(state))
|
||
if any(t in combined for t in ('应该让谁', '选择一位罪人')):
|
||
for index, item in enumerate(items):
|
||
if item['text'] in ('极高', '高') and item['y'] > height * 0.8 and item['w'] < width * 0.07:
|
||
options['sinner_%d' % index] = {
|
||
'description': '选择预计成功率%s的罪人卡,卡中心x=%d。' % (item['text'], item['x'] + item['w'] // 2),
|
||
'action': {'tool': 'click', 'args': {'x': item['x'] + item['w'] // 2,
|
||
'y': min(height - 35, item['y'] + int(height * 0.06))}},
|
||
}
|
||
if '选择一位罪人' in combined:
|
||
try:
|
||
import cv2
|
||
import numpy as np
|
||
rgb = np.array(screen_image(state['screen']['path'], 'RGB'))
|
||
for number in range(12):
|
||
cx = round(width*(.061 + number*.0542))
|
||
crop = rgb[round(height*.841):round(height*.878), max(0,cx-round(width*.026)):cx+round(width*.026)]
|
||
hsv = cv2.cvtColor(crop, cv2.COLOR_RGB2HSV)
|
||
gold = (hsv[:,:,0] > 18) & (hsv[:,:,0] < 36) & (hsv[:,:,1] > 160) & (hsv[:,:,2] > 180)
|
||
if int(gold.sum()) > 100:
|
||
options['sinner_card_%d' % number] = {
|
||
'description': '第%d张罪人卡的成功率标识为金黄色(高/极高),选择此卡。' % (number+1),
|
||
'action': {'tool': 'click', 'args': {'x': cx, 'y': round(height*.915)}},
|
||
}
|
||
except (OSError, KeyError, ImportError):
|
||
pass
|
||
if '选项' in texts:
|
||
if not any('选择后' in t and '获得' in t and '判定' not in t for t in texts):
|
||
options['event_scroll'] = {
|
||
'description': '向下拖动事件选项列表的滚动条,查看是否还有无需判定、无需战斗的直接离开选项。',
|
||
'action': {'tool': 'drag', 'args': {'from_x': round(width*.945), 'from_y': round(height*.255),
|
||
'to_x': round(width*.945), 'to_y': round(height*.68), 'duration': .8}},
|
||
}
|
||
for index, item in enumerate(items):
|
||
if item['x'] > width * 0.52 and height * 0.23 < item['y'] < height * 0.75 and item['h'] > height * 0.025 and item.get('score', 0) > 0.93:
|
||
if (not any(t in item['text'] for t in ('判定', '获得', '成功', '全体', '战斗胜利'))
|
||
and not re.search(r'check\b|\bgain\b|\be\.?g\.?o\.?\s*gift|(?:advantage|guilt)check',
|
||
item['text'], re.I)):
|
||
outcome = ';'.join(i['text'] for i in items if i['x'] > width*.52
|
||
and item['y']+item['h'] < i['y'] < item['y']+height*.085)
|
||
options['event_%d' % index] = {'description': '当前事件选项:' + item['text'] + ';结果:' + outcome,
|
||
'action': {'tool': 'click_text', 'args': {'text': item['text']}}}
|
||
return options
|
||
|
||
|
||
def decide(cfg, state, options, recent):
|
||
actions = [key for key in options if key not in ('wait', 'requires_analysis')]
|
||
route_scene = actions and all(key.startswith('route_') for key in actions)
|
||
sinner_scene = actions and all(key.startswith(('sinner_', 'button_')) for key in actions) and any(key.startswith('sinner_') for key in actions)
|
||
if route_scene or sinner_scene:
|
||
permitted = options
|
||
if sinner_scene:
|
||
# All explicit maximum-probability cards are legal. An uncertain
|
||
# colour-only estimate must not compete with an OCR-labelled rate.
|
||
rated = [(key, re.search(r'预计成功率(极高|非常高|高)', options[key]['description']))
|
||
for key in actions if key.startswith('sinner_')]
|
||
rated = [(key, match[1]) for key, match in rated if match]
|
||
if rated:
|
||
best = 0 if any(rate != '高' for _, rate in rated) else 1
|
||
permitted = {key: options[key] for key, rate in rated
|
||
if (0 if rate != '高' else 1) == best}
|
||
# These candidates have already been validated against the live scene.
|
||
# Party setup, gifts and fusion do not affect this immediate choice.
|
||
instructions = (
|
||
'目标是最快完成镜牢,只选择所给候选。候选已核对当前画面,不需要再次核对编队或开局配置。'
|
||
+ ('地图优先商店、事件,再普通战斗,避免精英或集中战斗。同类型默认正前方 route_d;没有事件也直接选择可达战斗。'
|
||
if route_scene else
|
||
'当前是事件判定。已有进行判定按钮则选择该按钮;否则选择明确标注极高成功率的 sinner 候选,极高优于高;同档任选一个。不需要额外角色技能信息。')
|
||
+ '仅在候选互相矛盾或没有有效动作时选择 requires_analysis。')
|
||
scene = ('镜牢地图,当前高亮可达节点如下:' if route_scene else '事件选择罪人界面,已识别当前可选择的罪人和成功率如下:')
|
||
description = scene + ';'.join(options[key]['description'] for key in actions)
|
||
answer = request_answer(cfg, description,
|
||
{key: val['description'] for key, val in permitted.items()}, instructions)
|
||
if permitted is not options and answer.get('probabilities') is not None:
|
||
# The provider only scored permitted cards. The surrounding router
|
||
# validates a distribution across its original candidate set.
|
||
answer['candidate_scope'] = list(permitted)
|
||
answer['probabilities'] = {key: answer['probabilities'].get(key, 0) for key in options}
|
||
return answer
|
||
description = ('当前楼层:%s\n当前游戏文字:\n' % state.get('floor', '尚未识别') +
|
||
'\n'.join(dict.fromkeys(i['text'] for i in text_items(state)
|
||
if not i['text'].upper().startswith('STEAM'))) +
|
||
'\n最近执行结果:' + json.dumps(recent[-2:], ensure_ascii=False))
|
||
return request_answer(cfg, description,
|
||
{key: val['description'] for key, val in options.items()}, SYSTEM_RULES)
|
||
|
||
|
||
def unambiguous_routine(options):
|
||
"""Execute one validated confirmation; choices still go through the model."""
|
||
judges = [key for key, value in options.items() if key.startswith('button_')
|
||
and '进行判定' in value['description']]
|
||
if len(judges) == 1:
|
||
return judges[0]
|
||
actions = [key for key in options if key not in ('wait', 'requires_analysis')]
|
||
if len(actions) != 1:
|
||
return None
|
||
key = actions[0]
|
||
safe = ('modal_confirm', 'select_startup_battery', 'confirm_startup_battery',
|
||
'confirm_thunderbranch', 'confirm_reward', 'hardship_confirm',
|
||
'confirm_swap', 'buy_swap', 'claim_final', 'run_complete', 'retry_network', 'shop_refresh')
|
||
if key in safe:
|
||
return key
|
||
if key.startswith('skill_swap_'):
|
||
return key
|
||
if key.startswith('button_') and options[key]['action']['tool'] in ('click', 'keypress'):
|
||
description = options[key]['description']
|
||
if any(label in description for label in ('确认', '继续', '进入', '开始战斗', '离开', '下一层', '进行判定')):
|
||
return key
|
||
return None
|
||
|
||
|
||
def fixed_fallback(options):
|
||
for key in ('run_complete','required_pack','pack_refresh','select_startup_battery','confirm_startup_battery','select_thunderbranch','confirm_thunderbranch','prepare_thrill','prepare_thunderbranch','decline_extra_observation','retry_network','buy_swap','confirm_swap','confirm_reward','hardship_confirm','modal_confirm','claim_final','close_progress'):
|
||
if key in options:
|
||
return key
|
||
judge=next((k for k in options if k.startswith('button_') and '进行判定' in options[k]['description']),None)
|
||
if judge:
|
||
return judge
|
||
for prefix in ('skill_swap_', 'swap_'):
|
||
keys=[k for k in options if k.startswith(prefix)]
|
||
if keys:
|
||
return keys[0]
|
||
sinners=[k for k in options if k.startswith('sinner_') and not k.startswith('sinner_card_')]
|
||
if sinners:
|
||
return min(sinners,key=lambda k: 0 if any(t in options[k]['description'] for t in ('极高','非常高')) else 1)
|
||
cards=[k for k in options if k.startswith('sinner_card_')]
|
||
if cards:
|
||
return cards[0]
|
||
if 'shop_refresh' in options:
|
||
return 'shop_refresh'
|
||
routes=[k for k in options if k.startswith('route_')]
|
||
if routes:
|
||
return min(routes,key=lambda k: 0 if '商店' in options[k]['description'] else 1 if '事件' in options[k]['description'] else 3 if '精英' in options[k]['description'] or '集中' in options[k]['description'] else 2)
|
||
events=[k for k in options if k.startswith('event_') and k!='event_scroll']
|
||
safe=[k for k in events if any(t in options[k]['description'] for t in ('离开','继续走','无视','不理','走开')) and '战斗胜利' not in options[k]['description']]
|
||
if safe:
|
||
return safe[0]
|
||
combat = ('战斗胜利', '进入战斗', '触发遭遇战', '开始战斗')
|
||
direct=[k for k in events if '结果:' in options[k]['description']
|
||
and options[k]['description'].split('结果:',1)[1].strip()
|
||
and not any(t in options[k]['description'] for t in ('判定',)+combat)]
|
||
if direct:
|
||
return direct[0]
|
||
if len(events)==1 and '判定' in options[events[0]]['description'] and not any(t in options[events[0]]['description'] for t in combat):
|
||
return events[0]
|
||
if 'event_scroll' in options:
|
||
return 'event_scroll'
|
||
rewards=[k for k in options if k.startswith('reward_card_')]
|
||
if rewards:
|
||
return min(rewards,key=lambda k: 0 if '经费' in options[k]['description'] else 1)
|
||
buttons=[k for k in options if k.startswith('button_')]
|
||
if len(buttons)==1:
|
||
return buttons[0]
|
||
for text in ('确认','继续','进入','开始战斗','离开'):
|
||
match=next((k for k in buttons if text in options[k]['description']),None)
|
||
if match:
|
||
return match
|
||
return 'requires_analysis'
|
||
|
||
|
||
def execute(bridge, action, deadline=None):
|
||
def call(tool, args=None):
|
||
if STOP_FILE.exists() or deadline is not None and time.monotonic() >= deadline:
|
||
return {'ok':False,'stopped':True}
|
||
result = bridge.call(tool, args)
|
||
return normalize(result) if tool == 'get_state' else result
|
||
if action['tool'] == 'claim_final':
|
||
result = call('click', action['args'])
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(.4)
|
||
return call('keypress', {'key':'enter'})
|
||
if action['tool'] == 'advance_dialog':
|
||
for _ in range(min(5, action.get('args', {}).get('presses', 2))):
|
||
result = call('keypress', {'key': 'enter'})
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(.2)
|
||
return result
|
||
if action['tool'] == 'route':
|
||
result = call('keypress', {'key': action['args']['key']})
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(0.35)
|
||
result = call('keypress', {'key': 'enter'})
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(0.7)
|
||
return call('keypress', {'key': 'enter'})
|
||
if action['tool'] == 'wait':
|
||
time.sleep(0.7)
|
||
return {'ok': True, 'waited': 0.7}
|
||
if action['tool'] == 'battle':
|
||
battle_deadline = min(deadline if deadline is not None else float('inf'), time.monotonic() + 45)
|
||
pairs = 0
|
||
while time.monotonic() < battle_deadline and not STOP_FILE.exists():
|
||
for _ in range(2):
|
||
for key in ('p', 'enter'):
|
||
result = call('keypress', {'key': key})
|
||
if not result.get('ok'):
|
||
result['pairs'] = pairs
|
||
return result
|
||
pairs += 1
|
||
time.sleep(0.8)
|
||
state = call('get_state')
|
||
if not state.get('ok'):
|
||
return state
|
||
if battle_is_terminal(state):
|
||
return {'ok': True, 'pairs': pairs, 'terminal': True, 'screen': state.get('screen')}
|
||
stopped = STOP_FILE.exists() or deadline is not None and time.monotonic() >= deadline
|
||
return {'ok': not stopped, 'stopped': stopped, 'pairs': pairs, 'terminal': False}
|
||
return call(action['tool'], action.get('args'))
|
||
|
||
|
||
def acquire_single_instance():
|
||
"""Windows 命名互斥量, 防止另一个终端重复启动同一个游戏的执行器。
|
||
返回释放函数; 已有实例在跑(或创建失败)时返回 None。"""
|
||
import ctypes
|
||
# use_last_error: 直接调 GetLastError 可能读到 ctypes 自身调用留下的值
|
||
kernel = ctypes.WinDLL('kernel32', use_last_error=True)
|
||
kernel.CreateMutexW.restype = ctypes.c_void_p
|
||
kernel.CreateMutexW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_wchar_p]
|
||
kernel.CloseHandle.argtypes = [ctypes.c_void_p]
|
||
lease = kernel.CreateMutexW(None, False, 'Local\\LimbusMirrorJevController')
|
||
if not lease:
|
||
return None
|
||
if ctypes.get_last_error() == 183: # ERROR_ALREADY_EXISTS
|
||
kernel.CloseHandle(lease)
|
||
return None
|
||
return lambda: kernel.CloseHandle(lease)
|
||
|
||
|
||
def execution_workflow(args, executor_decider=None):
|
||
"""Keep explicit speedrun requirements and unfinished runs on their old path."""
|
||
if args.workflow != 'auto':
|
||
return args.workflow
|
||
if (args.config or args.decision_engine or executor_decider is not None
|
||
or os.environ.get('LC_FIXED_FALLBACK') == '1'):
|
||
return 'speedrun'
|
||
previous = None if args.new_run else load_json(RUNTIME_HOME / 'mirror_current_run.json')
|
||
if previous and previous.get('workflow') == 'aalc':
|
||
return 'aalc'
|
||
if args.resume or previous and previous.get('status') in RESUMABLE:
|
||
return 'speedrun'
|
||
return 'aalc'
|
||
|
||
|
||
def delegate_mirror(new_run=False, resume=False):
|
||
"""Start or inspect AALC once; the calling agent monitors task_status afterward."""
|
||
record = {'workflow': 'aalc', 'next_call': {'tool': 'task_status', 'args': {'lines': 30}},
|
||
'note': '启动或接管监控不代表通关。继续轮询 task_status,检查 errors 和 pending_restore;'
|
||
'停止用 stop_task 或急停键(默认 End)。调用超时或用户停止后不自动重试或转为逐步输入。'}
|
||
bridge = None
|
||
try:
|
||
if STOP_FILE.exists():
|
||
return dict(record, state='stopped', reason='stop_requested')
|
||
# An explicit workflow switch must not silently discard a special route.
|
||
previous = None if new_run else load_json(RUNTIME_HOME / 'mirror_current_run.json')
|
||
if previous and previous.get('status') in RESUMABLE:
|
||
return dict(record, state='requires_analysis', reason='unfinished_speedrun',
|
||
error='存在未完成的速通局;使用 --workflow speedrun --resume,明确新开任务时用 --new-run')
|
||
bridge = Bridge(cancelled=STOP_FILE.exists, raise_on_error=False)
|
||
current = bridge.call('task_status', {'lines': 30})
|
||
state = current.get('state')
|
||
if not current.get('ok') or state not in ('idle', 'running', 'succeeded', 'failed', 'stopped'):
|
||
return dict(record, state='requires_analysis', task_status=current)
|
||
if (resume and previous and previous.get('workflow') == 'aalc' and previous.get('task_log')
|
||
and current.get('log') != previous['task_log']):
|
||
return dict(record, state='requires_analysis', reason='task_changed', task_status=current)
|
||
if state == 'running':
|
||
return dict(record, state='monitoring' if current.get('task') == 'mirror' else 'busy',
|
||
task_status=current)
|
||
if state == 'stopped' or current.get('exit_code') == 130:
|
||
if not new_run:
|
||
return dict(record, state='stopped', task_status=current, reason='user_stopped')
|
||
if resume:
|
||
return dict(record, state='requires_analysis' if current.get('errors') or current.get('pending_restore')
|
||
or current.get('task') != 'mirror' else state, task_status=current)
|
||
if state == 'failed' or current.get('errors') or current.get('pending_restore'):
|
||
if not new_run or current.get('pending_restore'):
|
||
return dict(record, state='requires_analysis', task_status=current)
|
||
started = bridge.call('run_task', {'task': 'mirror', 'count': 1})
|
||
if not started.get('ok'):
|
||
return dict(record, state='requires_analysis', result=started)
|
||
record['result'] = started
|
||
run, _ = prepare_run(RUNTIME_HOME, new_run=True)
|
||
run.update(status='delegated', workflow='aalc', task_log=started.get('log'))
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
return dict(record, state='delegated')
|
||
except Exception as error:
|
||
record.update(state='stopped' if isinstance(error, SessionCancelled) else 'requires_analysis',
|
||
error='%s: %s' % (type(error).__name__, error))
|
||
if isinstance(error, (SessionCancelled, SessionTimeout)):
|
||
record.update(outcome_unknown=error.outcome_unknown, pending_tool=getattr(error, 'tool', None))
|
||
return record
|
||
finally:
|
||
if bridge is not None:
|
||
bridge.close()
|
||
|
||
|
||
def main(executor_decider=None):
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument('config', nargs='?', help='Optional Jev JSON config for stepwise speedrun control')
|
||
parser.add_argument('--workflow', choices=('auto', 'aalc', 'speedrun'), default='auto',
|
||
help='Default: AALC for ordinary mirror runs; explicit model/config or unfinished speedrun keeps stepwise control')
|
||
parser.add_argument('--decision-engine', choices=('auto', 'jev', 'executor', 'fixed'))
|
||
parser.add_argument('--steps', type=int, default=250, help='Stepwise speedrun action budget')
|
||
parser.add_argument('--seconds', type=int, default=1200, help='Stepwise speedrun time budget')
|
||
mode = parser.add_mutually_exclusive_group()
|
||
mode.add_argument('--new-run', action='store_true', help='Start fresh state for a new dungeon')
|
||
mode.add_argument('--resume', action='store_true', help='Require an unfinished run with matching state')
|
||
args = parser.parse_args()
|
||
if args.steps <= 0 or args.seconds <= 0:
|
||
parser.error('--steps and --seconds must be positive')
|
||
try:
|
||
workflow = execution_workflow(args, executor_decider)
|
||
except (OSError, ValueError) as error:
|
||
parser.error(str(error))
|
||
release = acquire_single_instance()
|
||
if release is None:
|
||
print(json.dumps({'state':'already_running'}), flush=True)
|
||
return
|
||
if workflow == 'aalc':
|
||
try:
|
||
record = delegate_mirror(new_run=args.new_run, resume=args.resume)
|
||
save_json(RUNTIME_HOME / 'aalc_mirror_status.json', record)
|
||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||
finally:
|
||
release()
|
||
return
|
||
log = RUNTIME_HOME / 'jev_mirror_log.jsonl'
|
||
status = RUNTIME_HOME / 'jev_mirror_status.json'
|
||
deadline = time.monotonic() + args.seconds
|
||
bridge = None
|
||
run = None
|
||
recent = []
|
||
repeats = {}
|
||
context_path = RUNTIME_HOME / 'jev_shop_context.json'
|
||
stable_since = time.monotonic()
|
||
last_fingerprint = None
|
||
unknown_since = None
|
||
try:
|
||
RUNTIME_HOME.mkdir(parents=True, exist_ok=True)
|
||
cfg = json.loads(Path(args.config).read_text(encoding='utf-8-sig')) if args.config else {}
|
||
cfg['apiKey'] = os.environ.get('OPENCODE_API_KEY') or cfg.get('apiKey')
|
||
if args.decision_engine:
|
||
cfg['decisionEngine'] = args.decision_engine
|
||
router = DecisionRouter(cfg, decide, executor_decider=executor_decider,
|
||
fixed_decider=fixed_fallback, rules=SYSTEM_RULES,
|
||
force_fixed=os.environ.get('LC_FIXED_FALLBACK') == '1')
|
||
if STOP_FILE.exists():
|
||
save_json(status, {'state': 'stopped', 'reason': 'stop_requested'})
|
||
return
|
||
run, context = prepare_run(RUNTIME_HOME, args.new_run, args.resume)
|
||
previous_battle = context.get('battle_context', False)
|
||
# 工具失败按 {'ok': False} 返回: 截图暂不可用、按钮已消失等情况观察后继续, 而不是整局退出
|
||
bridge = Bridge(cancelled=lambda: STOP_FILE.exists() or time.monotonic() >= deadline,
|
||
raise_on_error=False)
|
||
for step in range(args.steps):
|
||
if STOP_FILE.exists() or time.monotonic() > deadline:
|
||
run['status'] = 'stopped'
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
save_json(status, {'state': 'stopped', 'step': step, 'run_id': run['run_id']})
|
||
return
|
||
observation_started = time.monotonic()
|
||
state = normalize(bridge.call('get_state'))
|
||
observation_ms = round((time.monotonic()-observation_started)*1000)
|
||
if not state.get('ok'):
|
||
state = {'ok': False, 'items': [{'text': 'MCP 截图暂不可用;游戏窗口可能正在重建。' + str(state.get('error','')),
|
||
'x':0,'y':0,'w':0,'h':0,'score':1.0}]}
|
||
fingerprint = scene_fingerprint(state)
|
||
if any('正在探' in i['text'] or '主题卡包' in i['text'] for i in text_items(state)):
|
||
context['refresh_used'] = 0
|
||
for item in text_items(state):
|
||
match = re.search(r'(?:第|选择)([1-5])层', item['text'].replace(' ', ''))
|
||
if match:
|
||
context['floor'] = int(match[1])
|
||
save_json(context_path, context)
|
||
state['floor'] = context.get('floor', 1)
|
||
state['symbol_upgraded'] = context.get('symbol_upgraded', False)
|
||
state['battery_obtained'] = context.get('battery_obtained', False)
|
||
state['thunderbranch_pending'] = context.get('thunderbranch_pending',False)
|
||
state['thrill_pending'] = context.get('thrill_obtained',False) and not context.get('thrill_upgraded',False)
|
||
state['claim_requested'] = claim_is_current(run, context)
|
||
options = candidates(state, previous_battle, context['refresh_used'])
|
||
if fingerprint != last_fingerprint:
|
||
stable_since = time.monotonic()
|
||
last_fingerprint = fingerprint
|
||
stable_seconds = round(time.monotonic() - stable_since)
|
||
if stable_seconds > 30:
|
||
recent.append({'observation':'画面已%d秒没有有效改变,请你判断是否需要主助手排查。' % stable_seconds})
|
||
for key in list(options):
|
||
action_id = json.dumps(options[key]['action'], sort_keys=True)
|
||
if ((options[key]['action'] == {'tool': 'keypress', 'args': {'key': 'enter'}}
|
||
or options[key]['action']['tool']=='advance_dialog')
|
||
and repeats.get((fingerprint, action_id), 0) >= 1):
|
||
fallback = options[key].get('fallback_action')
|
||
button = next((i for i in text_items(state)
|
||
if i['text'].replace(' ', '') in ('进入', '开始战斗', '确认', '继续', '进行判定')
|
||
and button_enabled(state, i)), None)
|
||
if fallback or button:
|
||
options[key]['action'] = fallback or {'tool': 'click', 'args': {
|
||
'x': button['x'] + button['w']//2, 'y': button['y'] + button['h']//2}}
|
||
options[key]['description'] += ' 回车未生效,改为点击当前按钮。'
|
||
action_id = json.dumps(options[key]['action'], sort_keys=True)
|
||
if key not in ('requires_analysis', 'wait', 'battle') and repeats.get((fingerprint, action_id), 0) >= 2:
|
||
del options[key]
|
||
if len(options) == 2:
|
||
unknown_since = unknown_since or time.monotonic()
|
||
else:
|
||
unknown_since = None
|
||
# User explicitly prescribed the complete battle loop. Only scene
|
||
# transitions return to Jev; combat does not require new decisions.
|
||
decision = None
|
||
if any(re.search(r'CON+ECTING|LOADING', i['text'].upper()) for i in text_items(state)):
|
||
choice, latency = 'wait', 0
|
||
elif len(options)==2 and time.monotonic() - unknown_since < 10:
|
||
choice, latency = 'wait', 0
|
||
elif 'event_finish' in options:
|
||
choice, latency = 'event_finish', 0
|
||
elif 'skip_dialog' in options:
|
||
choice, latency = 'skip_dialog', 0
|
||
elif 'event_advance' in options:
|
||
choice, latency = 'event_advance', 0
|
||
elif 'battle' in options:
|
||
choice, latency = 'battle', 0
|
||
elif unambiguous_routine(options):
|
||
choice, latency = unambiguous_routine(options), 0
|
||
else:
|
||
try:
|
||
decision = router.choose(state, options, recent, run['run_id'])
|
||
choice, latency = decision.choice, decision.jev_ms
|
||
except DecisionRequired as required:
|
||
record = {'state': 'requires_decision', 'step': step, 'run_id': run['run_id'],
|
||
'decision_engine': 'executor', 'decision_request': required.request,
|
||
'ocr_ms': observation_ms, 'screen': state.get('screen'),
|
||
'at': time.strftime('%Y-%m-%dT%H:%M:%S')}
|
||
if 'failed_jev_ms' in required.request:
|
||
record['failed_jev_ms'] = required.request['failed_jev_ms']
|
||
run['status'] = 'requires_decision'
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
save_json(RUNTIME_HOME / 'decision_request.json', required.request)
|
||
save_json(status, record)
|
||
with log.open('a', encoding='utf-8') as output:
|
||
output.write(json.dumps(record, ensure_ascii=False) + '\n')
|
||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||
return
|
||
action = options[choice]['action']
|
||
record = {'step': step, 'run_id': run['run_id'], 'choice': choice, 'action': action, 'jev_ms': latency,
|
||
'ocr_ms':observation_ms,'screen': state.get('screen'), 'at': time.strftime('%Y-%m-%dT%H:%M:%S')}
|
||
record['decision_engine'] = decision.engine if decision else 'rules'
|
||
if decision:
|
||
record.update(decision_ms=decision.latency_ms + (decision.failed_jev_ms or 0), confidence=decision.confidence,
|
||
probabilities=decision.probabilities, model=decision.model)
|
||
if decision.failed_jev_ms is not None:
|
||
record['failed_jev_ms'] = decision.failed_jev_ms
|
||
if decision.fallback_reason:
|
||
record['decision_fallback_reason'] = decision.fallback_reason
|
||
if decision.candidate_scope is not None:
|
||
record['provider_candidate_scope'] = decision.candidate_scope
|
||
if choice == 'run_complete':
|
||
complete_run(RUNTIME_HOME, run, context, state.get('screen'))
|
||
record['state'] = 'completed'
|
||
save_json(status, record)
|
||
with log.open('a', encoding='utf-8') as output:
|
||
output.write(json.dumps(record, ensure_ascii=False) + '\n')
|
||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||
return
|
||
if choice == 'requires_analysis':
|
||
record.update(state='requires_analysis', items=text_items(state))
|
||
run['status'] = 'requires_analysis'
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
save_json(status, record)
|
||
with log.open('a', encoding='utf-8') as output:
|
||
output.write(json.dumps(record, ensure_ascii=False) + '\n')
|
||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||
return
|
||
action_id = json.dumps(action, sort_keys=True)
|
||
repeats[(fingerprint, action_id)] = repeats.get((fingerprint, action_id), 0) + 1
|
||
action_started = time.monotonic()
|
||
result = execute(bridge, action, deadline=deadline)
|
||
record['action_ms'] = round((time.monotonic()-action_started)*1000)
|
||
record['finished_at'] = time.strftime('%Y-%m-%dT%H:%M:%S')
|
||
if result.get('stopped'):
|
||
record.update(result=result,state='stopped',battle_context=previous_battle)
|
||
with log.open('a',encoding='utf-8') as output:
|
||
output.write(json.dumps(record,ensure_ascii=False)+'\n')
|
||
run['status'] = 'stopped'
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
save_json(status, record)
|
||
return
|
||
if choice == 'claim_final' and result.get('ok'):
|
||
record_claim(RUNTIME_HOME, run, context, state.get('screen'))
|
||
if choice in ('confirm_startup_battery','confirm_thunderbranch') and result.get('ok'):
|
||
context['battery_obtained' if choice == 'confirm_startup_battery' else 'thunderbranch_pending'] = True
|
||
save_json(context_path, context)
|
||
if choice=='modal_confirm' and result.get('ok') and any(i['text'].replace(' ','')=='快感' for i in text_items(state)) and any('获得E.G.O' in i['text'].replace(' ','') for i in text_items(state)):
|
||
context['thrill_obtained']=True
|
||
save_json(context_path, context)
|
||
if choice == 'shop_refresh' and result.get('ok'):
|
||
context['refresh_used'] += 1
|
||
save_json(context_path, context)
|
||
started_battle = choice.startswith('button_') and bool(result.get('ok')) and any(
|
||
i['text'].replace(' ', '') == '开始战斗' for i in text_items(state))
|
||
previous_battle = ((action['tool'] == 'battle' and not result.get('terminal')) or started_battle
|
||
or (previous_battle and action['tool'] == 'wait'))
|
||
context['battle_context'] = previous_battle
|
||
save_json(context_path, context)
|
||
record.update(result=result, state='running', battle_context=previous_battle)
|
||
with log.open('a', encoding='utf-8') as output:
|
||
output.write(json.dumps(record, ensure_ascii=False) + '\n')
|
||
save_json(status, record)
|
||
recent.append({'action': action, 'ok': result.get('ok'), 'unchanged_attempt': repeats[(fingerprint, action_id)]})
|
||
print(json.dumps({k:v for k,v in record.items() if k != 'items'}, ensure_ascii=False), flush=True)
|
||
time.sleep(0.35)
|
||
run['status'] = 'stopped'
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
save_json(status, {'state': 'stopped', 'reason': 'step_limit', 'run_id': run['run_id']})
|
||
except Exception as error:
|
||
state = 'stopped' if isinstance(error, SessionCancelled) else 'requires_analysis'
|
||
record = {'state': state, 'error': '%s: %s' % (type(error).__name__, str(error))}
|
||
if isinstance(error, (SessionCancelled, SessionTimeout)):
|
||
record['outcome_unknown'] = error.outcome_unknown
|
||
record['pending_tool'] = getattr(error, 'tool', None)
|
||
if run is not None:
|
||
run['status'] = state
|
||
record['run_id'] = run['run_id']
|
||
save_json(RUNTIME_HOME / 'mirror_current_run.json', run)
|
||
save_json(status, record)
|
||
with log.open('a', encoding='utf-8') as output:
|
||
output.write(json.dumps(record, ensure_ascii=False) + '\n')
|
||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||
finally:
|
||
try:
|
||
if bridge is not None:
|
||
bridge.close()
|
||
finally:
|
||
release()
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|