1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-09 18:39:04 +08:00
AzurLaneAutoScript/module/handler/enemy_searching.py

148 lines
4.7 KiB
Python
Raw Normal View History

2020-03-29 01:22:46 +08:00
from module.base.timer import Timer
from module.base.utils import red_overlay_transparency, get_color
from module.exception import CampaignEnd
2020-03-29 01:22:46 +08:00
from module.handler.assets import *
2020-05-15 14:18:14 +08:00
from module.handler.info_handler import InfoHandler
2020-03-29 01:22:46 +08:00
from module.logger import logger
from module.map.assets import *
from module.ui.assets import EVENT_CHECK, SP_CHECK, CAMPAIGN_CHECK
2020-03-29 01:22:46 +08:00
2020-05-15 14:18:14 +08:00
class EnemySearchingHandler(InfoHandler):
2020-03-29 01:22:46 +08:00
MAP_ENEMY_SEARCHING_OVERLAY_TRANSPARENCY_THRESHOLD = 0.5 # Usually (0.70, 0.80).
MAP_ENEMY_SEARCHING_TIMEOUT_SECOND = 5
in_stage_timer = Timer(0.5, count=2)
stage_entrance = None
map_is_100_percent_clear = False # Will be override in fast_forward.py
2020-03-29 01:22:46 +08:00
def enemy_searching_color_initial(self):
2020-03-29 01:22:46 +08:00
MAP_ENEMY_SEARCHING.load_color(self.device.image)
def enemy_searching_appear(self):
2020-03-29 01:22:46 +08:00
return red_overlay_transparency(
MAP_ENEMY_SEARCHING.color, get_color(self.device.image, MAP_ENEMY_SEARCHING.area)
) > self.MAP_ENEMY_SEARCHING_OVERLAY_TRANSPARENCY_THRESHOLD
def handle_enemy_flashing(self):
self.device.sleep(1.2)
2020-03-29 01:22:46 +08:00
def handle_in_stage(self):
if self.is_in_stage():
if self.in_stage_timer.reached():
logger.info('In stage.')
self.ensure_no_info_bar(timeout=1.2)
raise CampaignEnd('In stage.')
else:
return False
2020-03-29 01:22:46 +08:00
else:
if self.appear(MAP_PREPARATION, offset=(20, 20)) or self.appear(FLEET_PREPARATION, offset=(20, 20)):
self.device.click(MAP_PREPARATION_CANCEL)
self.in_stage_timer.reset()
2020-03-29 01:22:46 +08:00
return False
def is_in_stage(self):
appear = [self.appear(check, offset=(20, 20)) for check in [CAMPAIGN_CHECK, EVENT_CHECK, SP_CHECK]]
if not any(appear):
return False
# campaign_extract_name_image in CampaignOcr.
2020-09-20 00:22:29 +08:00
try:
2021-01-28 00:35:15 +08:00
if hasattr(self, 'campaign_extract_name_image') \
and not len(self.campaign_extract_name_image(self.device.image)):
2020-09-20 00:22:29 +08:00
return False
except IndexError:
return False
return True
2020-03-29 01:22:46 +08:00
def is_in_map(self):
return self.appear(IN_MAP)
def is_event_animation(self):
"""
Animation in events after cleared an enemy.
Returns:
bool: If animation appearing.
"""
return False
def handle_auto_search_exit(self, drop=None):
"""
A placeholder, will be override in AutoSearchHandler.
AutoSearchHandler inherits EnemySearchingHandler,
but handle_in_map_with_enemy_searching() requires handle_auto_search_exit() to handle unexpected situation.
"""
return False
def handle_in_map_with_enemy_searching(self, drop=None):
"""
Args:
drop (DropImage):
Returns:
bool: If handled.
"""
2020-03-29 01:22:46 +08:00
if not self.is_in_map():
return False
timeout = Timer(self.MAP_ENEMY_SEARCHING_TIMEOUT_SECOND)
appeared = False
while 1:
self.device.screenshot()
if self.is_event_animation():
continue
if self.is_in_map():
timeout.start()
else:
timeout.reset()
# Stage might ends,
# although here expects an enemy searching animation.
if self.handle_in_stage():
return True
if self.handle_auto_search_exit(drop=drop):
continue
if self.handle_vote_popup():
timeout.limit = 10
timeout.reset()
continue
if self.handle_story_skip():
self.ensure_no_story()
timeout.limit = 10
timeout.reset()
if self.handle_guild_popup_cancel():
timeout.limit = 10
timeout.reset()
continue
if self.handle_urgent_commission(drop=drop):
timeout.limit = 10
timeout.reset()
continue
# End
if self.enemy_searching_appear():
2020-03-29 01:22:46 +08:00
appeared = True
else:
if appeared:
self.handle_enemy_flashing()
self.device.sleep(0.3)
logger.info('Enemy searching appeared.')
2020-03-29 01:22:46 +08:00
break
self.enemy_searching_color_initial()
2020-03-29 01:22:46 +08:00
if timeout.reached():
logger.info('Enemy searching timeout.')
break
2020-11-26 17:52:24 +08:00
self.device.screenshot()
2020-03-29 01:22:46 +08:00
return True
def handle_in_map_no_enemy_searching(self):
2020-03-29 01:22:46 +08:00
if not self.is_in_map():
return False
self.device.sleep((1, 1.2))
return True