靠近npc逻辑调整

This commit is contained in:
ray
2025-11-04 15:07:10 +08:00
parent 6337567ddf
commit 9db83deab6

132
main.py
View File

@@ -213,8 +213,12 @@ while True:
move_to(rw, mb)
continue
elif detections['npc4'] is not None:
if sq(detections['npc4'], rw) < 50:
print("离npc4很近 直接进入")
npc4_pos = detections['npc4']
current_distance = sq(npc4_pos, rw)
# 如果已经非常接近,直接进入
if current_distance < 30:
print(f"离npc4很近 (距离={current_distance:.1f}),直接进入")
keyboard.send_data("DD")
time.sleep(0.15)
keyboard.release()
@@ -227,18 +231,132 @@ while True:
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
time.sleep(1)
continue
else:
print("离npc4有点远 点击进入")
move_to(rw, detections['npc4'])
# 循环靠近npc4直到足够接近或达到最大尝试次数
print(f"开始靠近npc4当前距离={current_distance:.1f}")
max_attempts = 15 # 最大尝试次数
attempt = 0
last_distance = current_distance
stuck_count = 0 # 卡住计数器
while attempt < max_attempts:
attempt += 1
# 重新获取当前位置和npc4位置
im_opencv = get_image.get_frame()
if im_opencv is None:
print("⚠️ 无法获取图像帧")
break
detections_temp = {
'center': None, 'next': None,
'npc1': None, 'npc2': None, 'npc3': None, 'npc4': None,
'boss': None, 'zhaozi': None,
'daojv': [], 'gw': []
}
detections_temp = yolo_shibie(im_opencv[1], detections_temp, model0)
# 如果npc4丢失重新检测
if detections_temp['npc4'] is None:
print(f"⚠️ npc4丢失重新检测 (尝试 {attempt}/{max_attempts})")
time.sleep(0.5)
continue
npc4_pos = detections_temp['npc4']
current_distance = sq(npc4_pos, rw)
print(f" 尝试 {attempt}/{max_attempts}: 距离npc4={current_distance:.1f}")
# 如果已经足够接近,退出循环
if current_distance < 35:
print(f"✅ 已足够接近npc4 (距离={current_distance:.1f})")
break
# 检测是否卡住(距离不再减小)
if abs(current_distance - last_distance) < 5:
stuck_count += 1
if stuck_count >= 3:
print(f"⚠️ 检测到卡住,尝试微调移动")
# 尝试向npc4方向微调
dx = npc4_pos[0] - rw[0]
dy = npc4_pos[1] - rw[1]
# 计算方向向量,但只移动一小段距离
step_size = 50 # 小步移动
distance_to_move = min(step_size, current_distance * 0.3)
if abs(dx) > abs(dy):
# 主要横向移动
target_x = rw[0] + int(dx / abs(dx) * distance_to_move) if dx != 0 else rw[0]
target_y = rw[1]
else:
# 主要纵向移动
target_x = rw[0]
target_y = rw[1] + int(dy / abs(dy) * distance_to_move) if dy != 0 else rw[1]
mb = (target_x, target_y)
move_to(rw, mb)
time.sleep(0.3)
stuck_count = 0
else:
# 正常移动,但使用小步长
step_size = min(80, current_distance * 0.4) # 移动距离为目标距离的40%最多80像素
dx = npc4_pos[0] - rw[0]
dy = npc4_pos[1] - rw[1]
# 归一化方向向量
if abs(dx) > 0.1 or abs(dy) > 0.1:
direction_mag = math.sqrt(dx*dx + dy*dy)
target_x = rw[0] + int(dx / direction_mag * step_size)
target_y = rw[1] + int(dy / direction_mag * step_size)
mb = (target_x, target_y)
move_to(rw, mb)
time.sleep(0.2) # 短暂等待,让角色移动
else:
# 距离在减小,正常移动
stuck_count = 0
step_size = min(100, current_distance * 0.5) # 移动距离为目标距离的50%最多100像素
dx = npc4_pos[0] - rw[0]
dy = npc4_pos[1] - rw[1]
# 归一化方向向量
if abs(dx) > 0.1 or abs(dy) > 0.1:
direction_mag = math.sqrt(dx*dx + dy*dy)
target_x = rw[0] + int(dx / direction_mag * step_size)
target_y = rw[1] + int(dy / direction_mag * step_size)
mb = (target_x, target_y)
move_to(rw, mb)
time.sleep(0.2) # 短暂等待,让角色移动
last_distance = current_distance
# 短暂延迟,让角色位置更新
time.sleep(0.3)
# 移动完成后,检查是否可以进入
final_distance = sq(npc4_pos, rw)
print(f"靠近完成,最终距离={final_distance:.1f}")
if final_distance < 50:
print("距离足够近,尝试进入")
keyboard.send_data("DD")
time.sleep(0.15)
keyboard.release()
time.sleep(1)
im_opencv = get_image.get_frame() # [RGB,PIL]
im_opencv = get_image.get_frame()
if im_opencv is None:
print("⚠️ 无法获取图像帧")
continue
if shizi.daoying(im_opencv[0]):
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
time.sleep(1)
continue
else:
print(f"⚠️ 未能足够接近npc4 (距离={final_distance:.1f}),尝试点击进入")
move_to(rw, npc4_pos)
time.sleep(1)
im_opencv = get_image.get_frame()
if im_opencv is None:
print("⚠️ 无法获取图像帧")
continue
if shizi.daoying(im_opencv[0]):
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
time.sleep(1)
continue
elif shizi.tiaozhan(im_opencv[0]): # 开启挑战
print('进入塔4')
mouse_gui.send_data_absolute(left + 1100, top + 600, may=1)