"""Validated scene actions for Jev decisions and fixed speedrun macros.""" import argparse import json import math import os import re import subprocess import sys import time import urllib.error import urllib.request 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 prepare_run, save_json, record_claim, claim_is_current, complete_run REPO_ROOT = Path(__file__).resolve().parents[1] 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。' '事件当前有“选择后获得”且无需判定、无需战斗的选项时直接选择它,不反复滚动;避开写着战斗胜利后的选项。' '确定确认/进入/开始战斗直接执行。地图优先可达事件和商店,减少战斗。' '选好路线直接回车进入,关卡编队界面也直接回车开战;保留原编队,不再检查人员。' '保留当前编队。开局三级饰品三选一必须选择标准负载电池,再确认领取;不能当成额外观测拒绝。' '成捆的符咒++、标准负载电池和鸡胸为核心,保留不可出售或合成;仅略过单独的额外观测。其他苦难避开敌方体力上限和防御等级。' '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 button_enabled(state, item): try: from PIL import Image import numpy as np x, y, w, h = (int(item[k]) for k in ('x', 'y', 'w', 'h')) with Image.open(state['screen']['path']) as image: crop = np.array(image.convert('L').crop((x, y, x + w, y + h))) return float(np.quantile(crop, 0.9)) > 85 except (OSError, KeyError, ImportError): return False def map_candidates(state): """Only observe reachable nodes; Jev chooses their route.""" import cv2 import numpy as np from PIL import Image assets = REPO_ROOT / 'vendor' / 'aalc' / 'assets' / 'images' / 'default' / 'share' / 'mirror' with Image.open(state['screen']['path']) as image: rgb = np.array(image.convert('RGB')) gray = cv2.cvtColor(rgb, cv2.COLOR_RGB2GRAY) scale = min(rgb.shape[0] / 1440, rgb.shape[1] / 2560) with Image.open(assets / 'mybus_default_distance.png') as image: bus = cv2.cvtColor(np.array(image.convert('RGB')), cv2.COLOR_RGB2GRAY) bus = cv2.resize(bus, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA) _, 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 = cv2.imread(str(assets / 'road_in_mir' / (arrow_name+'.png')), 0) template = cv2.resize(template, None, fx=scale, fy=scale, interpolation=cv2.INTER_AREA) 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','集中战斗')): with Image.open(assets / 'road_in_mir' / (name + '.png')) as image: template = np.array(image.convert('L')) template = cv2.resize(template, None, fx=2*scale, fy=2*scale) _, desc = orb.detectAndCompute(template, None) 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('REC' in t.upper() 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'] == '购买': options['buy_swap'] = { 'description': '已核对本层所需技能替换 %s→%s,点击购买完成。' % target, '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 from PIL import Image with Image.open(state['screen']['path']) as image: border = np.array(image.convert('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 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: for index, item in enumerate(items): if item['text'].replace(' ', '') == '刷新' and button_enabled(state, item): 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, 'action': ({'tool': 'keypress', 'args': {'key': 'enter'}} if label in ('进入', '开始战斗') else {'tool': 'click_text', 'args': {'text': item['text']}}), } 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 from PIL import Image with Image.open(state['screen']['path']) as image: rgb = np.array(image.convert('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 ('判定', '获得', '成功', '全体', '战斗胜利')): 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): body = {'model': 'jev-1.13-free', 'state': '目标:按用户已配置的队伍和速通规则推进当前镜牢。当前楼层:%s\n当前游戏 OCR(坐标仅供辨识):\n' % state.get('floor', '尚未识别') + '\n'.join('%s @%d,%d' % (i['text'], i['x'], i['y']) for i in text_items(state) if not i['text'].upper().startswith('STEAM')) + '\n截图视觉与可用控件观察(不是预先做出的决定):\n' + '\n'.join(val['description'] for key,val in options.items() if key != 'requires_analysis') + '\n最近动作和执行结果:' + json.dumps(recent[-4:], ensure_ascii=False), 'questions': {'action': {'type': 'choice', 'instructions': SYSTEM_RULES, 'criteria': {key: val['description'] for key, val in options.items()}}}} request = urllib.request.Request( 'https://opencode.ai/zen/v1/systemone', data=json.dumps(body, ensure_ascii=False).encode('utf-8'), headers={'Authorization': 'Bearer ' + cfg['apiKey'], 'Content-Type': 'application/json', 'User-Agent': 'Mozilla/5.0'}, method='POST') start = time.monotonic() for attempt in range(2): try: with urllib.request.urlopen(request, timeout=7) as response: result = json.load(response) break except urllib.error.HTTPError as error: if error.code in (401,403,429) or attempt: raise time.sleep(.3) except (OSError, TimeoutError): if attempt: raise time.sleep(.3) answer = result.get('answers', {}).get('action', {}) choice = answer.get('choice') if answer.get('type') != 'choice' or choice not in options: raise ValueError('Jev returned an unsupported candidate') return choice, round((time.monotonic() - start) * 1000) 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} return bridge.call(tool, args) 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 main(): parser = argparse.ArgumentParser() parser.add_argument('config') parser.add_argument('--steps', type=int, default=250) parser.add_argument('--seconds', type=int, default=1200) 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') # 防止另一个终端重复启动同一个游戏的执行器。 import ctypes kernel = ctypes.windll.kernel32 kernel.CreateMutexW.restype = ctypes.c_void_p kernel.CreateMutexW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_wchar_p] lease = kernel.CreateMutexW(None, False, 'Local\\LimbusMirrorJevController') if not lease or kernel.GetLastError() == 183: print(json.dumps({'state':'already_running'}), flush=True) if lease: kernel.CloseHandle(ctypes.c_void_p(lease)) 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 decision_fallback_reason = None try: RUNTIME_HOME.mkdir(parents=True, exist_ok=True) cfg = json.loads(Path(args.config).read_text(encoding='utf-8-sig')) cfg['apiKey'] = os.environ.get('OPENCODE_API_KEY') or cfg.get('apiKey') if not cfg['apiKey'] and os.environ.get('LC_FIXED_FALLBACK') != '1': raise RuntimeError('Set OPENCODE_API_KEY or enable 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) bridge = Bridge(cancelled=lambda: STOP_FILE.exists() or time.monotonic() >= deadline) 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 = 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 = tuple(item['text'] for item in text_items(state) if item.get('score', 0) > 0.93 and not item['text'].replace('.', '').isdigit() and not re.fullmatch(r'[0-9:]+', item['text'])) 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. 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 else: if os.environ.get('LC_FIXED_FALLBACK') == '1' or decision_fallback_reason: choice, latency = fixed_fallback(options), 0 else: try: choice, latency = decide(cfg, state, options, recent) except urllib.error.HTTPError as error: if error.code not in (401,403,429): raise decision_fallback_reason='Jev HTTP %d; deterministic fallback for this session' % error.code choice, latency=fixed_fallback(options),0 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')} if decision_fallback_reason: record['decision_fallback_reason']=decision_fallback_reason 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': context['refresh_used'] += 1 save_json(context_path, context) started_battle = choice.startswith('button_') 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: kernel.CloseHandle(ctypes.c_void_p(lease)) if __name__ == '__main__': main()