Skip to content

Commit

Permalink
#15 Fix choose floor
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorReid committed Oct 29, 2023
1 parent 206596b commit 4245eeb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
10 changes: 8 additions & 2 deletions config/world_patrol/P03_XZLF/R02_LYD_R01_JYF.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ route:
- op: 'move'
data: [566, 754]
- op: 'move'
data: [596, 750]
data: [585, 750]
- op: 'move'
data: [597, 661, 2]
data: [596, 735]
- op: 'move'
data: [597, 700]
- op: 'move'
data: [597, 679, 2]
- op: 'move'
data: [597, 661]
- op: 'move'
data: [555, 660]
- op: 'move'
Expand Down
2 changes: 1 addition & 1 deletion src/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def run_app(page: ft.Page):
ctx = get_context()
page.title = gt('崩坏:星穹铁道 自动代理器', model='ui') + ' v0.5.0'
page.title = gt('崩坏:星穹铁道 自动代理器', model='ui') + ' v0.5.2'

display_part = ft.Container(content=world_patrol_view.get(page, ctx).component)

Expand Down
2 changes: 1 addition & 1 deletion src/gui/world_patrol_draft_route_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ def on_test_existed(self, e):
whitelist: WorldPatrolWhitelist = WorldPatrolWhitelist('0')
whitelist.type = 'white'
whitelist.list = [self.chosen_route_id.unique_id]
app = WorldPatrol(self.ctx, restart=True, whitelist=whitelist)
app = WorldPatrol(self.ctx, ignore_record=True, whitelist=whitelist)
app.first = False
app.execute()

Expand Down
25 changes: 14 additions & 11 deletions src/sr/app/world_patrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def save(self):

class WorldPatrol(Application):

def __init__(self, ctx: Context, restart: bool = False, whitelist: WorldPatrolWhitelist = None):
def __init__(self, ctx: Context, restart: bool = False, whitelist: WorldPatrolWhitelist = None,
ignore_record: bool = False):
super().__init__(ctx)
self.route_list = []
self.first: bool = True
Expand All @@ -162,19 +163,21 @@ def __init__(self, ctx: Context, restart: bool = False, whitelist: WorldPatrolWh
self.route_iterator: Iterator = None
self.whitelist: WorldPatrolWhitelist = whitelist
self.current_route_idx: int = -1
self.ignore_record: bool = ignore_record

def init_app(self):
self.route_list = load_all_route_id(self.whitelist)
if self.whitelist is not None:
log.info('使用白名单 %s' % self.whitelist.id)
log.info('共加载 %d 条线路', len(self.route_list))
try:
sr = game_config.get().server_region
utc_offset = game_config_const.SERVER_TIME_OFFSET.get(sr)
self.record = WorldPatrolRecord(os_utils.get_dt(utc_offset), restart=self.restart)
log.info('之前已完成线路 %d 条', len(self.record.finished))
except Exception:
log.info('读取运行记录失败 重新开始', exc_info=True)
if not self.ignore_record:
try:
sr = game_config.get().server_region
utc_offset = game_config_const.SERVER_TIME_OFFSET.get(sr)
self.record = WorldPatrolRecord(os_utils.get_dt(utc_offset), restart=self.restart)
log.info('之前已完成线路 %d 条', len(self.record.finished))
except Exception:
log.info('读取运行记录失败 重新开始', exc_info=True)

self.current_route_idx = -1

Expand Down Expand Up @@ -203,6 +206,8 @@ def run(self) -> int:

if self.run_one_route(route_id):
self.first = False
if not self.ignore_record:
self.save_record(route_id)

return Operation.WAIT

Expand All @@ -226,8 +231,7 @@ def run_one_route(self, route_id: WorldPatrolRouteId) -> bool:
return False
else:
log.info('传送完成 开始寻路')
# self.save_record(route_id) # 只测试传送点OCR时开启
# return True
# return True # 只测试传送点OCR时开启

last_region = route.tp.region
lm_info = large_map.analyse_large_map(last_region, self.ctx.ih)
Expand Down Expand Up @@ -267,7 +271,6 @@ def run_one_route(self, route_id: WorldPatrolRouteId) -> bool:
log.error('错误的锄大地指令 %s 即将跳过本次路线 %s', route_item['op'], route_id)
return False

self.save_record(route_id)
return True

def save_record(self, route_id: WorldPatrolRouteId):
Expand Down
2 changes: 1 addition & 1 deletion src/sr/image/sceenshot/large_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def get_active_region_name(screen: MatLike, ocr: OcrMatcher) -> str:
left, right, top, bottom = cv2_utils.get_four_corner(bw)
rect = (left[0] - 10, top[1] - 10, right[0] + 10, bottom[1] + 10)
to_ocr: MatLike = cv2_utils.crop_image(bw, rect)[0]
cv2_utils.show_image(to_ocr, win_name='get_active_region_name')
# cv2_utils.show_image(to_ocr, win_name='get_active_region_name')
lang = game_config.get().lang
strict_one_line: bool = lang in [game_config_const.LANG_CN]
return ocr.ocr_for_single_line(to_ocr, strict_one_line=strict_one_line)
Expand Down
10 changes: 5 additions & 5 deletions src/sr/operation/unit/choose_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def run(self) -> int:

# 需要选择层数
if self.region.floor != 0:
floor_str = large_map.get_active_floor(screen, self.ctx.ocr)
log.info('当前层数 %s', floor_str)
if floor_str is None:
current_floor_str = large_map.get_active_floor(screen, self.ctx.ocr)
log.info('当前层数 %s', current_floor_str)
if current_floor_str is None:
log.error('未找到当前选择的层数')
target_floor_str = gt('%d层' % self.region.floor, 'ocr')
if target_floor_str != floor_str:
if not str_utils.find_by_lcs(target_floor_str, current_floor_str, 1):
cl = self.click_target_floor(screen, target_floor_str)
time.sleep(0.5)
if not cl:
Expand Down Expand Up @@ -130,7 +130,7 @@ def click_target_floor(self, screen, target_floor_str: str) -> bool:
"""
part, _ = cv2_utils.crop_image(screen, large_map.FLOOR_LIST_PART)
return self.ctx.controller.click_ocr(part, target_floor_str, click_offset=large_map.FLOOR_LIST_PART[:2],
same_word=True)
lcs_percent=1)

def check_tp_and_cancel(self, screen) -> bool:
"""
Expand Down
9 changes: 9 additions & 0 deletions src/sr/operation/unit/move_directly.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MoveDirectly(Operation):
rec_pos_interval: float = 0.5 # 间隔多少秒记录一次坐标
stuck_distance: float = 20 # 移动距离多少以内认为是被困
arrival_distance: float = 10 # 多少距离内认为是到达目的地
fail_after_no_battle: float = 120 # 多少秒无战斗后退出 通常不会有路线这么久都遇不到怪 只能是卡死了 然后脱困算法又让角色产生些位移

def __init__(self, ctx: Context,
lm_info: LargeMapInfo,
Expand All @@ -46,6 +47,7 @@ def __init__(self, ctx: Context,
self.no_pos_times = 0 # 累计算不到坐标的次数
self.stop_afterwards = stop_afterwards # 最后是否停止前进
self.last_auto_fight_fail: bool = False # 上一次索敌是否失败 只有小地图背景污染严重时候出现
self.last_battle_time = time.time()
self.last_no_pos_time = 0 # 上一次算不到坐标的时间 目前算坐标太快了 可能地图还在缩放中途就已经失败 所以稍微隔点时间再记录算不到坐标

self.run_mode = game_config.get().run_mode
Expand Down Expand Up @@ -73,6 +75,11 @@ def run(self) -> bool:
# self.ctx.controller.move('w')
# time.sleep(0.5) # 等待人物转过来再截图
now_time = time.time()

if now_time - self.last_battle_time > MoveDirectly.fail_after_no_battle:
log.error('移动执行超时')
return Operation.FAIL

screen = self.screenshot()

# 可能被怪攻击了
Expand All @@ -81,12 +88,14 @@ def run(self) -> bool:
self.ctx.controller.stop_moving_forward()
fight = EnterAutoFight(self.ctx)
fight.execute()
self.last_battle_time = time.time()
self.last_rec_time = time.time() # 战斗可能很久 需要重置一下记录坐标时间
return Operation.WAIT

mm = mini_map.cut_mini_map(screen)
# 根据小地图判断是否被怪锁定 是的话停下来处理敌人
if self.check_enemy_and_attack(mm):
self.last_battle_time = time.time()
self.last_rec_time = time.time() # 战斗可能很久 需要重置一下记录坐标时间
return Operation.WAIT

Expand Down

0 comments on commit 4245eeb

Please sign in to comment.