add 硬件控制模块 (hardware_control.py) 并修复游戏状态扫描区域宽度

- 新增 wyhkm.dll 硬件盒子 COM 接口封装,支持键盘鼠标控制
- 修复 game_state_config.json 中 scan_region_width 过小导致截图越界的问题
- 添加鼠标路径录制器、硬件测试脚本等工具
- 更新多项配置默认值

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
王鹏
2026-04-15 12:15:00 +08:00
parent b4de5278ed
commit 33dc741fd9
203 changed files with 12197 additions and 247 deletions

View File

@@ -1,7 +1,8 @@
import math
import time
import logging
import pydirectinput
import random
from hardware_control import hw_ctrl
from player_position import PlayerPosition
# 游戏朝向约定:正北=0°正西=90°正南=180°正东=270°逆时针递增
@@ -81,9 +82,9 @@ class PlayerMovement:
f"[转向 {attempt + 1}] 当前 {current_heading:.1f}° → 目标 {target_heading:.1f}°,"
f"'{key}' {turn_duration:.2f}s需转 {angle_to_turn:.1f}°)"
)
pydirectinput.keyDown(key)
hw_ctrl.keyDown(key)
time.sleep(turn_duration)
pydirectinput.keyUp(key)
hw_ctrl.keyUp(key)
time.sleep(0.15) # 等待游戏刷新朝向
self.logger.warning(f"转向失败:超过最大尝试次数 {max_attempts}")
@@ -96,36 +97,35 @@ class PlayerMovement:
duration: 移动时间(秒)
"""
self.logger.info(f"向前移动 {duration:.2f}s")
pydirectinput.keyDown('w')
hw_ctrl.keyDown('w')
time.sleep(duration)
pydirectinput.keyUp('w')
hw_ctrl.keyUp('w')
def _escape_stuck(self):
"""卡死脱困组合拳:后退 + 随机转向 + 跳跃。
当连续多次检测到坐标无明显位移时调用,尝试脱离障碍物。
"""
import random
self.logger.warning("检测到角色卡死,执行脱困动作")
# 1. 松开前进键,后退 0.8s
pydirectinput.keyUp('w')
pydirectinput.keyDown('s')
hw_ctrl.keyUp('w')
hw_ctrl.keyDown('s')
time.sleep(0.8)
pydirectinput.keyUp('s')
hw_ctrl.keyUp('s')
# 2. 随机左转或右转 0.3~0.5s
turn_key = random.choice(['a', 'd'])
turn_dur = random.uniform(0.3, 0.5)
pydirectinput.keyDown(turn_key)
hw_ctrl.keyDown(turn_key)
time.sleep(turn_dur)
pydirectinput.keyUp(turn_key)
hw_ctrl.keyUp(turn_key)
# 3. 跳跃(按空格)同时向前冲 0.5s
pydirectinput.keyDown('w')
pydirectinput.press('space')
hw_ctrl.keyDown('w')
hw_ctrl.press('space')
time.sleep(0.5)
pydirectinput.keyUp('w')
hw_ctrl.keyUp('w')
self.logger.info("脱困动作完成,重新开始前进")
@@ -172,7 +172,7 @@ class PlayerMovement:
return False
# 按住 w 开始持续前进
pydirectinput.keyDown('w')
hw_ctrl.keyDown('w')
# 卡死检测:记录最近 STUCK_CHECK_COUNT 次坐标,用于判断是否停滞
recent_positions = []
try:
@@ -204,7 +204,7 @@ class PlayerMovement:
if moved < self.STUCK_MOVE_THRESHOLD:
self._escape_stuck()
recent_positions.clear()
pydirectinput.keyDown('w')
hw_ctrl.keyDown('w')
# x 和 y 都在容差范围内即视为到达
if abs(dx) <= position_tolerance and abs(dy) <= position_tolerance:
@@ -213,7 +213,7 @@ class PlayerMovement:
# 接近目标时松开 w 停下,再做一次静止判定,避免冲过头后死循环
if distance <= position_tolerance * 2:
pydirectinput.keyUp('w')
hw_ctrl.keyUp('w')
time.sleep(0.2)
final_pos = self.player_position.get_position_with_retry()
if final_pos is not None:
@@ -223,7 +223,7 @@ class PlayerMovement:
self.logger.info(f"已到达目标位置 ({target_x}, {target_y})")
return True
# 静止后仍未到达,重新按住 w 继续前进
pydirectinput.keyDown('w')
hw_ctrl.keyDown('w')
# 每隔 HEADING_CHECK_INTERVAL 次读一次朝向,减少 OCR 频率
if iteration % self.HEADING_CHECK_INTERVAL != 0:
@@ -239,12 +239,12 @@ class PlayerMovement:
if abs_diff > self.COARSE_TURN_THRESHOLD:
# 偏差过大:停步原地粗修正,再重新按住 w
pydirectinput.keyUp('w')
hw_ctrl.keyUp('w')
self.logger.info(f"偏差 {abs_diff:.1f}° 过大,停步原地修正")
if not self.turn_to_heading(required_heading):
self.logger.warning("修正转向失败,移动中止")
return False
pydirectinput.keyDown('w')
hw_ctrl.keyDown('w')
elif abs_diff > self.ANGLE_TOLERANCE:
# 小幅偏差:边走边转,同时按住 w 和转向键
@@ -254,15 +254,15 @@ class PlayerMovement:
self.logger.info(
f"边走边转:按 '{turn_key}' {turn_time:.2f}s偏差 {abs_diff:.1f}°)"
)
pydirectinput.keyDown(turn_key)
hw_ctrl.keyDown(turn_key)
time.sleep(turn_time)
pydirectinput.keyUp(turn_key)
hw_ctrl.keyUp(turn_key)
else:
# 方向已对齐,让角色多走一段再重新检测,减少 OCR 频率
time.sleep(1)
finally:
pydirectinput.keyUp('w')
hw_ctrl.keyUp('w')
self.logger.warning(f"移动失败:超过最大迭代次数 {max_iterations}")
return False