Skip to content

Commit

Permalink
Merge pull request #94 from smjes3/fix-sub-time
Browse files Browse the repository at this point in the history
- 优化逻辑,新增怪物识别超时配置
  • Loading branch information
linruowuyin authored Mar 10, 2024
2 parents 7a7067d + 4cc237c commit 36b9077
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 113 deletions.
5 changes: 5 additions & 0 deletions Honkai_Star_Rail.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ def get_questions_for_slot(slot: str) -> list:
"title": "跑图时使用疾跑模式?(实验性功能,默认关闭,未测试过不同角色的影响,请自行斟酌打开)",
"choices": {'关闭疾跑': False, '开启疾跑': True},
"config_key": "auto_run_in_map"
},
{
"title": "设置识别怪物超时时间,默认为15秒。需要自定义设置可以直接修改config.json中的detect_fight_status_time为指定的秒数",
"choices": {'较短识别时间(5秒)': 5, '较长识别时间(15秒)': 15},
"config_key": "detect_fight_status_time"
}
]

Expand Down
92 changes: 46 additions & 46 deletions map/map_9-9_7.json → map/map_3-1_0.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
{
"name": "过期邮包",
"author": "lrwy",
"start": [
{
"check": 1
},
{
"map": 1
},
{
"picture\\map_4-2_point_3.png": 1.5
},
{
"picture\\orientation_1.png": 1.5
},
{
"picture\\orientation_4.png": 1.5
},
{
"picture\\map_3-1.png": 2
},
{
"picture\\map_9-7.png": 2
},
{
"picture\\transfer.png": 1.5
}
],
"map": [
{
"check": 1
},
{
"w": 8
},
{
"d": 1
},
{
"f": 2
},
{
"fighting": 2
}
]
{
"name": "过期邮包",
"author": "lrwy",
"start": [
{
"check": 1
},
{
"map": 1
},
{
"picture\\map_4-2_point_3.png": 1.5
},
{
"picture\\orientation_1.png": 1.5
},
{
"picture\\orientation_4.png": 1.5
},
{
"picture\\map_3-1.png": 2
},
{
"picture\\map_9-7.png": 2
},
{
"picture\\transfer.png": 1.5
}
],
"map": [
{
"check": 1
},
{
"w": 8
},
{
"d": 1
},
{
"f": 2
},
{
"fighting": 2
}
]
}
68 changes: 34 additions & 34 deletions map/map_9-9_8.json → map/map_3-1_01.json
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
{
"name": "过期邮包2",
"author": "lrwy",
"start": [
{
"check": 1
},
{
"picture\\buy.png": 1.5
},
{
"picture\\parcel.png": 1.5
},
{
"picture\\max.png": 1
},
{
"picture\\round.png": 1.5
}
],
"map": [
{
"check": 1
},
{
"fighting": 2
},
{
"esc": 1
},
{
"fighting": 2
}
]
{
"name": "过期邮包2",
"author": "lrwy",
"start": [
{
"check": 1
},
{
"picture\\buy.png": 1.5
},
{
"picture\\parcel.png": 1.5
},
{
"picture\\max.png": 1
},
{
"picture\\round.png": 1.5
}
],
"map": [
{
"check": 1
},
{
"fighting": 2
},
{
"esc": 1
},
{
"fighting": 2
}
]
}
61 changes: 49 additions & 12 deletions utils/calculated.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import win32con
import win32gui
import random
import atexit
from datetime import datetime
from PIL import ImageGrab
from pynput.keyboard import Controller as KeyboardController
Expand All @@ -20,6 +21,8 @@
class Calculated:
def __init__(self):
self.cfg = ConfigurationManager()
atexit.register(self.error_stop)
win32api.SetConsoleCtrlHandler(self.error_stop, True)
self._config = None
self._last_updated = None
self.keyboard = KeyboardController()
Expand All @@ -45,12 +48,18 @@ def __init__(self):
self.attack_once = False # 检测fighting时仅攻击一次,避免连续攻击
self.esc_btn = KeyboardKey.esc # esc键
self.shift_btn = KeyboardKey.shift_l # shift左键
self.alt_btn = KeyboardKey.alt_l # alt左键

self.total_fight_time = 0 # 总计战斗时间
self.error_fight_cnt = 0 # 异常战斗<1秒的计数
self.error_fight_threshold = 1 # 异常战斗为战斗时间<1秒
self.tatol_save_time = 0 # 疾跑节约时间

self.total_fight_cnt = 0 # 战斗次数计数
self.total_no_fight_cnt = 0 # 非战斗次数计数

def error_stop(self):
for i in [self.shift_btn, self.alt_btn]:
self.keyboard.release(i)

def _check_window_visibility(self):
"""
Expand Down Expand Up @@ -110,7 +119,7 @@ def keyboard_press(self, key_name: str, delay: float=0):
time.sleep(delay)
self.keyboard.release(key_name)

def mouse_press(self, x, y, end_x=None, end_y=None, delay: float = 0.4):
def mouse_press(self, x, y, end_x=None, end_y=None, delay: float = 0.05):
"""
说明:
鼠标点击
Expand Down Expand Up @@ -379,7 +388,7 @@ def click_target_with_alt(self, target_path, threshold, flag=True):
self.click_target(target_path, threshold, flag)
win32api.keybd_event(win32con.VK_MENU, 0, win32con.KEYEVENTF_KEYUP, 0)

def detect_fight_status(self, timeout=10):
def detect_fight_status(self, timeout=5):
start_time = time.time()
action_executed = False
self.attack_once = False
Expand Down Expand Up @@ -430,7 +439,8 @@ def fight_elapsed(self):
返回:
是否识别到敌人
"""
fight_status = self.detect_fight_status()
detect_fight_status_time = self.cfg.CONFIG.get("detect_fight_status_time", 15)
fight_status = self.detect_fight_status(timeout=detect_fight_status_time)
if not fight_status:
# 识别超时,此处可能无敌人
return False
Expand All @@ -451,13 +461,16 @@ def fight_elapsed(self):
elapsed_minutes = int(elapsed_time // 60)
elapsed_seconds = elapsed_time % 60
formatted_time = f"{elapsed_minutes}分钟{elapsed_seconds:.2f}秒"
self.total_fight_cnt += 1
colored_message = (f"战斗完成,单场用时\033[1;92m『{formatted_time}\033[0m")
log.info(colored_message)
match_details = f"匹配度: {result['max_val']:.2f} ({points[0]}, {points[1]})"
log.info(match_details)

self.rotate()
time.sleep(3)
while not self.on_main_interface(timeout=2):
time.sleep(0.1)
time.sleep(1)
return True

if not auto_switch and elapsed_time > 5:
Expand Down Expand Up @@ -494,6 +507,7 @@ def fighting(self):
fight_status = self.fight_elapsed()

if not fight_status:
self.total_no_fight_cnt += 1
log.info(f'未进入战斗')
time.sleep(0.5)

Expand Down Expand Up @@ -614,6 +628,7 @@ def auto_map(self, map, old=True, rotate=False):
log.info(f"执行{map_filename}文件:{map_index + 1}/{total_map_count} {map}")
key, value = next(iter(map.items()))
self.monthly_pass_check() # 行进前识别是否接近月卡时间
self._last_step_run = False # 初始化上一次为走路
if key == "space" or key == "r":
self.handle_space_or_r(value, key)
elif key == "f":
Expand Down Expand Up @@ -706,15 +721,19 @@ def handle_move(self, value, key):
run_in_road = True
temp_value = value
value = round((value - 1) / 1.53, 4) + 1
# log.info(f"按键时间修改为{value}")
self.tatol_save_time += (temp_value - value)
elif value <= 1 and allow_run and add_time:
self._last_step_run = True
elif value <= 1 and allow_run and add_time and self._last_step_run:
value = value + 0.07
add_time = False
self._last_step_run = False
elif value <= 2:
self._last_step_run = False
pass
self.keyboard.release(self.shift_btn)
self.keyboard.release(key)
time.sleep(0.05)
if allow_run:
time.sleep(0.02)

def mouse_move(self, x):
scaling = 1.0
Expand Down Expand Up @@ -897,12 +916,12 @@ def run_mapload_check(self, error_count=0, max_error_count=10, threshold = 0.9):
self.run_blackscreen_cal_time()
break
elif self.on_main_interface(check_list=[self.main_ui, self.finish2_ui, self.finish2_1_ui, self.finish2_2_ui, self.finish3_ui], threshold=threshold):
time.sleep(2)
time.sleep(1)
if self.on_main_interface(check_list=[self.main_ui, self.finish2_ui, self.finish2_1_ui, self.finish2_2_ui, self.finish3_ui], threshold=threshold):
log.info(f"连续检测到主界面,地图加载标记为结束")
break
elif self.on_interface(check_list=[self.finish5_ui], timeout=3, interface_desc='模拟宇宙积分奖励界面'):
time.sleep(2)
time.sleep(1)
if self.on_interface(check_list=[self.finish5_ui], timeout=3, interface_desc='模拟宇宙积分奖励界面'):
log.info(f"连续检测到模拟宇宙积分奖励界面,地图加载标记为结束")
break
Expand All @@ -916,7 +935,7 @@ def run_mapload_check(self, error_count=0, max_error_count=10, threshold = 0.9):
loading_time = end_time - start_time
if error_count < max_error_count:
log.info(f'地图载毕,用时 {loading_time:.1f} 秒')
time.sleep(2) # 增加2秒等待防止人物未加载错轴
time.sleep(1) # 增加1秒等待防止人物未加载错轴

def run_dreambuild_check(self, error_count=0, max_error_count=10):
"""
Expand All @@ -941,6 +960,14 @@ def run_dreambuild_check(self, error_count=0, max_error_count=10):
log.info(f'移动模块成功,用时 {loading_time:.1f} 秒')
time.sleep(0.5) #短暂延迟后开始下一步

def back_to_main(self):
"""
检测并回到主界面
"""
while not self.on_main_interface(timeout=2): # 检测是否出现左上角灯泡,即主界面检测
pyautogui.press('esc')
time.sleep(2)

def on_main_interface(self, check_list=[], timeout=60, threshold=0.9):
"""
说明:
Expand Down Expand Up @@ -993,4 +1020,14 @@ def handle_shutdown(self):
log.info("下班喽!I'm free!")
os.system("shutdown /s /f /t 0")
else:
log.info("锄地结束!")
log.info("锄地结束!")

def allow_buy_item(self):
"""
购买物品检测
"""
round_disable = cv.imread("./picture/round_disable.png")
if self.on_interface(check_list=[round_disable], timeout=5, interface_desc='无法购买', threshold=0.95):
return False
else:
return True
3 changes: 2 additions & 1 deletion utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def init_config_file(self, real_width, real_height):
"script_debug": False,
"auto_shutdown": False,
"auto_final_fight_e": False,
"auto_run_in_map": False
"auto_run_in_map": False,
"detect_fight_status_time": 15
}
)
)
Expand Down
Loading

0 comments on commit 36b9077

Please sign in to comment.