Skip to content

Commit 491c331

Browse files
committed
fix: stop 检测
1 parent 837f1a1 commit 491c331

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

custom/action/executor.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,22 @@ def _perform_action(
313313
ctrl,
314314
ts,
315315
target_frame - bullet_threshold,
316-
context_tasker_stopping=lambda: False,
316+
context_tasker_stopping=lambda: context.tasker.stopping,
317317
)
318318
if self._leaked:
319319
return # 漏怪,跳过本动作的暂停/逐帧/操作
320+
if context.tasker.stopping:
321+
return
320322

321323
# 到达 bullet 阈值 → 暂停(所有暂停都用模板验证)
322-
self._pause(context, ctrl)
324+
self._pause(context, ctrl, lambda: context.tasker.stopping)
325+
if context.tasker.stopping:
326+
return
323327

324328
# 逐帧步进到目标
325-
self._step_to_frames(ctrl, ts, target_frame)
329+
self._step_to_frames(ctrl, ts, target_frame, lambda: context.tasker.stopping)
330+
if context.tasker.stopping:
331+
return
326332

327333
# 执行动作(此时游戏已暂停)
328334
if action.action_type == ActionType.DEPLOY:
@@ -360,6 +366,7 @@ def _wait_until_frames(
360366
if context_tasker_stopping():
361367
return
362368
img = ctrl.post_screencap().wait().get()
369+
now = time.time()
363370
lf = ts.update(img)
364371
if lf is not None:
365372
current = ts.total_elapsed_frames
@@ -376,7 +383,6 @@ def _wait_until_frames(
376383
if current >= target_frame:
377384
return
378385
# 漏怪检测:血量图标变红(BattleHpFlag2),低频(1s),不影响计时
379-
now = time.time()
380386
if now - last_leak_check >= 1.0:
381387
last_leak_check = now
382388
if self._detect_leak(context, img):
@@ -395,21 +401,23 @@ def _detect_leak(self, context: Context, img: np.ndarray) -> bool:
395401

396402
# --- 暂停状态机(经 AFA 热键 + 模板验证) ---
397403

398-
def _pause(self, context: Context, ctrl: Controller) -> None:
404+
def _pause(
405+
self,
406+
context: Context,
407+
ctrl: Controller,
408+
context_tasker_stopping: Callable[[], bool],
409+
) -> None:
399410
"""暂停游戏并验证:循环发 ESC 直到模板匹配到暂停标志。
400411
401412
幂等:已暂停则不发。所有暂停都验证——pause invariant 是帧级执行的基础,
402413
一旦没真暂停后续逐帧/部署/技能全错。
403414
"""
404415
if self._paused:
405-
img = ctrl.post_screencap().wait().get()
406-
reco = context.run_recognition("BattlePaused", img)
407-
if reco and reco.hit:
408-
return
409-
logger.warning("内部暂停状态与游戏画面不一致,重新尝试暂停")
410-
self._paused = False
416+
return
411417
max_retries = 20
412418
for i in range(max_retries):
419+
if context_tasker_stopping():
420+
return
413421
afa_hotkey.tap_key(afa_hotkey.VK_F)
414422
self._paused = True
415423
time.sleep(config.GENERAL_WAIT_MS / 1000)
@@ -427,6 +435,7 @@ def _resume(self) -> None:
427435
return
428436
afa_hotkey.tap_key(afa_hotkey.VK_SPACE) # AFA: 松开暂停(Space 脉冲)
429437
self._paused = False
438+
logger.debug("恢复运行(Space)")
430439

431440
# --- 倍速状态机(pipeline 节点识别速度按钮并点击) ---
432441

@@ -444,13 +453,21 @@ def _set_speed(self, context: Context, speed: int) -> None:
444453
self._speed = speed
445454
logger.debug("倍速 → %dx", speed)
446455

447-
def _step_to_frames(self, ctrl: Controller, ts: TimeSource, target_frame: int) -> None:
456+
def _step_to_frames(
457+
self,
458+
ctrl: Controller,
459+
ts: TimeSource,
460+
target_frame: int,
461+
context_tasker_stopping: Callable[[], bool],
462+
) -> None:
448463
"""逐帧步进到累计帧 target_frame(游戏须已暂停)。
449464
450465
发 AFA R 键(Action33ms),AFA 要求光标在游戏客户区内。
451466
"""
452467
max_steps = 90
453468
for _ in range(max_steps):
469+
if context_tasker_stopping():
470+
return
454471
img = ctrl.post_screencap().wait().get()
455472
lf = ts.update(img)
456473
if lf is None:

0 commit comments

Comments
 (0)