609 lines
36 KiB
Python
609 lines
36 KiB
Python
"""Jev chooses every scene/action; lc-mcp performs only validated candidates."""
|
||
import argparse
|
||
import json
|
||
import math
|
||
import os
|
||
import re
|
||
import subprocess
|
||
import sys
|
||
import time
|
||
import urllib.request
|
||
from pathlib import Path
|
||
|
||
from lc_agent import Bridge
|
||
|
||
STOP_FILE = Path(r'{source_root}\JEV_STOP')
|
||
|
||
SYSTEM_RULES = (
|
||
'你负责识别固定场景、路线和所有下一步决策。目标是最快完成当前一局镜牢。'
|
||
'只能选择所给候选;不要反复点击未生效的按钮。战斗选 battle 宏,P+回车,不用空格。'
|
||
'刚选路线、回车开战或上一动作是战斗时,特写或加载没有OCR文字很正常;选短等或战斗宏,不仅因OCR为空要求分析。'
|
||
'遇到事件判定先点SKIP跳过对话,跳完后再选择事件选项、罪人或进行判定;剧情同样优先SKIP。'
|
||
'事件当前有“选择后获得”且无需判定、无需战斗的选项时直接选择它,不反复滚动;避开写着战斗胜利后的选项。'
|
||
'确定确认/进入/开始战斗直接执行。地图优先可达事件和商店,减少战斗。'
|
||
'选好路线直接回车进入,关卡编队界面也直接回车开战;保留原编队,不再检查人员。'
|
||
'保留当前编队。成捆的符咒++和鸡胸为核心,略过额外观测;其他苦难避开敌方体力上限和防御等级。'
|
||
'3层起辛克莱1换3,黑兽奥提斯2换3,其他出战1换3。只在商店强化饰品。'
|
||
'商店没有出战角色可替换技能时允许刷新,当前商店最多4次;有可替换角色先按技能表替换。'
|
||
'第五层优先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 = Path(r'D:\codex\lc-mcp\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)
|
||
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)
|
||
colored = int((red | green).sum()) >= 80
|
||
if not colored and int(white.sum()) < 80:
|
||
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:
|
||
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('CONNECTING' in t.upper() for t in texts):
|
||
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
|
||
if '判定成功' in combined or '判定失败' in combined or '结果' in texts:
|
||
continuation = next((i for i in items if i['text'].replace(' ', '') == '继续'
|
||
and button_enabled(state,i)), None)
|
||
options['event_finish'] = {
|
||
'description': '判定结果已出现,快速连按回车结束事件并返回地图。',
|
||
'action': ({'tool':'click','args':{'x':continuation['x']+continuation['w']//2,
|
||
'y':continuation['y']+continuation['h']//2}}
|
||
if continuation else {'tool': 'advance_dialog', 'args': {'presses': 5}}),
|
||
}
|
||
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 '应该让谁' 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.235, 0.49, 0.745), 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 '追加的苦难' 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:
|
||
if '选择' in item['text'] and '/' in item['text'] and button_enabled(state,item):
|
||
options['hardship_confirm'] = {
|
||
'description':'选择数量足够后确认层末饰品和苦难,直接前进。',
|
||
'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 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': '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 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
|
||
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暂未识别'
|
||
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}},
|
||
}
|
||
return options
|
||
in_shop = '商店' 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:
|
||
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 = any(t.upper() in ('VICTORY', 'DEFEAT') or any(m in t for m in
|
||
('正在探', '获得E.G.O饰品', '开始战斗', '主题卡包', '应该让谁上前', '选择遭遇战奖励卡')) for t in texts)
|
||
battle = any(m in combined.upper() for m in ('WAVE', 'TURN', 'DURANTE', '胜率', '伤害'))
|
||
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 (OSError, TimeoutError):
|
||
if attempt:
|
||
raise
|
||
time.sleep(0.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 execute(bridge, action):
|
||
if action['tool'] == 'claim_final':
|
||
result = bridge.call('click', action['args'])
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(.4)
|
||
return bridge.call('keypress', {'key':'enter'})
|
||
if action['tool'] == 'advance_dialog':
|
||
for _ in range(min(5, action.get('args', {}).get('presses', 2))):
|
||
result = bridge.call('keypress', {'key': 'enter'})
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(.2)
|
||
return result
|
||
if action['tool'] == 'route':
|
||
result = bridge.call('keypress', {'key': action['args']['key']})
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(0.35)
|
||
result = bridge.call('keypress', {'key': 'enter'})
|
||
if not result.get('ok'):
|
||
return result
|
||
time.sleep(0.7)
|
||
return bridge.call('keypress', {'key': 'enter'})
|
||
if action['tool'] == 'wait':
|
||
time.sleep(0.7)
|
||
return {'ok': True, 'waited': 0.7}
|
||
if action['tool'] == 'battle':
|
||
deadline = time.monotonic() + 45
|
||
pairs = 0
|
||
while time.monotonic() < deadline and not STOP_FILE.exists():
|
||
for _ in range(2):
|
||
for key in ('p', 'enter'):
|
||
result = bridge.call('keypress', {'key': key})
|
||
if not result.get('ok'):
|
||
return result
|
||
pairs += 1
|
||
time.sleep(0.8)
|
||
state = bridge.call('get_state')
|
||
if not state.get('ok'):
|
||
return state
|
||
texts = [i['text'].replace(' ', '') for i in text_items(state)]
|
||
terminal = any(t.upper() in ('VICTORY', 'DEFEAT') or any(m in t for m in
|
||
('正在探', '获得E.G.O饰品', '开始战斗', '主题卡包',
|
||
'应该让谁上前', '选择遭遇战奖励卡', '选项事件', '选择苦难')) for t in texts)
|
||
if terminal:
|
||
return {'ok': True, 'pairs': pairs, 'terminal': True, 'screen': state.get('screen')}
|
||
return {'ok': True, 'pairs': pairs, 'terminal': False}
|
||
return bridge.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)
|
||
args = parser.parse_args()
|
||
# 防止另一个终端重复启动同一个游戏的执行器。
|
||
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
|
||
cfg = json.loads(Path(args.config).read_text(encoding='utf-8'))
|
||
log = Path(r'{source_root}\jev_mirror_log.jsonl')
|
||
status = Path(r'{source_root}\jev_mirror_status.json')
|
||
bridge = Bridge()
|
||
deadline = time.monotonic() + args.seconds
|
||
recent = []
|
||
repeats = {}
|
||
previous_battle = False
|
||
if log.exists():
|
||
last_lines = log.read_text(encoding='utf-8').splitlines()
|
||
if last_lines:
|
||
last_record = json.loads(last_lines[-1])
|
||
previous_battle = last_record.get('battle_context', last_record.get('action', {}).get('tool') == 'battle')
|
||
context_path = Path(r'{source_root}\jev_shop_context.json')
|
||
context = json.loads(context_path.read_text(encoding='utf-8')) if context_path.exists() else {'refresh_used': 0}
|
||
stable_since = time.monotonic()
|
||
last_fingerprint = None
|
||
try:
|
||
for step in range(args.steps):
|
||
if STOP_FILE.exists() or time.monotonic() > deadline:
|
||
status.write_text(json.dumps({'state': 'stopped', 'step': step}), encoding='utf-8')
|
||
return
|
||
state = bridge.call('get_state')
|
||
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())
|
||
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])
|
||
context_path.write_text(json.dumps(context), encoding='utf-8')
|
||
state['floor'] = context.get('floor', 3)
|
||
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'}}
|
||
and repeats.get((fingerprint, action_id), 0) >= 1):
|
||
button = next((i for i in text_items(state)
|
||
if i['text'].replace(' ', '') in ('进入', '开始战斗', '确认', '继续')
|
||
and button_enabled(state, i)), None)
|
||
if button:
|
||
options[key]['action'] = {'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]
|
||
# User explicitly prescribed the complete battle loop. Only scene
|
||
# transitions return to Jev; combat does not require new decisions.
|
||
if '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:
|
||
choice, latency = decide(cfg, state, options, recent)
|
||
action = options[choice]['action']
|
||
record = {'step': step, 'choice': choice, 'action': action, 'jev_ms': latency,
|
||
'screen': state.get('screen'), 'at': time.strftime('%Y-%m-%dT%H:%M:%S')}
|
||
if choice == 'requires_analysis':
|
||
record.update(state='requires_analysis', items=text_items(state))
|
||
status.write_text(json.dumps(record, ensure_ascii=False), encoding='utf-8')
|
||
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
|
||
result = execute(bridge, action)
|
||
if choice == 'shop_refresh':
|
||
context['refresh_used'] += 1
|
||
context_path.write_text(json.dumps(context), encoding='utf-8')
|
||
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'))
|
||
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')
|
||
status.write_text(json.dumps(record, ensure_ascii=False), encoding='utf-8')
|
||
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)
|
||
except Exception as error:
|
||
record = {'state': 'requires_analysis', 'error': '%s: %s' % (type(error).__name__, str(error))}
|
||
status.write_text(json.dumps(record, ensure_ascii=False), encoding='utf-8')
|
||
print(json.dumps(record, ensure_ascii=False), flush=True)
|
||
finally:
|
||
bridge.proc.stdin.close()
|
||
try:
|
||
bridge.proc.wait(timeout=5)
|
||
except subprocess.TimeoutExpired:
|
||
bridge.proc.kill()
|
||
kernel.CloseHandle(ctypes.c_void_p(lease))
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|