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

438 lines
15 KiB
Python
Raw Normal View History

2021-08-13 02:32:05 +08:00
from module.campaign.campaign_base import CampaignBase
2021-06-29 23:08:59 +08:00
from module.campaign.run import CampaignRun
2023-02-16 22:06:56 +08:00
from module.combat.assets import BATTLE_PREPARATION
2021-06-29 23:08:59 +08:00
from module.equipment.assets import *
from module.equipment.fleet_equipment import FleetEquipment
from module.exception import CampaignEnd, ScriptError
from module.handler.assets import AUTO_SEARCH_MAP_OPTION_OFF
from module.logger import logger
2021-08-13 00:53:07 +08:00
from module.map.assets import FLEET_PREPARATION, MAP_PREPARATION
from module.retire.assets import (
DOCK_CHECK,
TEMPLATE_BOGUE, TEMPLATE_HERMES, TEMPLATE_LANGLEY, TEMPLATE_RANGER,
TEMPLATE_CASSIN_1, TEMPLATE_CASSIN_2, TEMPLATE_DOWNES_1, TEMPLATE_DOWNES_2,
TEMPLATE_AULICK, TEMPLATE_FOOTE
)
from module.retire.dock import Dock
from module.retire.scanner import ShipScanner
from module.ui.assets import BACK_ARROW
from module.ui.page import page_fleet
2021-07-05 02:24:38 +08:00
SIM_VALUE = 0.92
2021-06-29 23:08:59 +08:00
2021-08-14 01:18:15 +08:00
class GemsCampaignOverride(CampaignBase):
2021-06-30 02:15:57 +08:00
2021-08-13 00:53:07 +08:00
def handle_combat_low_emotion(self):
"""
2021-08-14 02:10:36 +08:00
Overwrite info_handler.handle_combat_low_emotion()
If change vanguard is enabled, withdraw combat and change flagship and vanguard
"""
if self.config.GemsFarming_ChangeVanguard == 'disabled':
result = self.handle_popup_confirm('IGNORE_LOW_EMOTION')
if result:
# Avoid clicking AUTO_SEARCH_MAP_OPTION_OFF
self.interval_reset(AUTO_SEARCH_MAP_OPTION_OFF)
return result
if self.handle_popup_cancel('IGNORE_LOW_EMOTION'):
self.config.GEMS_EMOTION_TRIGGRED = True
logger.hr('EMOTION WITHDRAW')
2021-08-14 01:18:15 +08:00
while 1:
self.device.screenshot()
2021-08-14 01:18:15 +08:00
if self.handle_story_skip():
continue
if self.handle_popup_cancel('IGNORE_LOW_EMOTION'):
continue
if self.appear(BATTLE_PREPARATION, offset=(20, 20), interval=2):
self.device.click(BACK_ARROW)
continue
if self.handle_auto_search_exit():
continue
if self.is_in_stage():
break
2021-08-14 21:48:21 +08:00
if self.is_in_map():
self.withdraw()
break
2021-08-14 01:18:15 +08:00
2023-10-19 21:36:19 +08:00
if self.appear(FLEET_PREPARATION, offset=(20, 50), interval=2) \
or self.appear(MAP_PREPARATION, offset=(20, 20), interval=2):
self.enter_map_cancel()
break
raise CampaignEnd('Emotion withdraw')
2021-08-14 01:18:15 +08:00
2021-06-30 02:15:57 +08:00
2024-04-19 14:13:18 +08:00
class GemsFarming(CampaignRun, FleetEquipment, Dock):
2021-06-30 02:15:57 +08:00
2021-08-13 00:53:07 +08:00
def load_campaign(self, name, folder='campaign_main'):
super().load_campaign(name, folder)
class GemsCampaign(GemsCampaignOverride, self.module.Campaign):
pass
self.campaign = GemsCampaign(device=self.campaign.device, config=self.campaign.config)
self.campaign.config.override(Emotion_Mode='ignore')
self.campaign.config.override(EnemyPriority_EnemyScaleBalanceWeight='S1_enemy_first')
2021-08-13 00:53:07 +08:00
@property
def change_flagship(self):
return 'ship' in self.config.GemsFarming_ChangeFlagship
@property
def change_flagship_equip(self):
return 'equip' in self.config.GemsFarming_ChangeFlagship
@property
def change_vanguard(self):
return 'ship' in self.config.GemsFarming_ChangeVanguard
@property
def change_vanguard_equip(self):
return 'equip' in self.config.GemsFarming_ChangeVanguard
2024-04-19 15:40:55 +08:00
@property
def fleet_to_attack(self):
if self.config.Fleet_FleetOrder == 'fleet1_standby_fleet2_all':
2024-04-19 15:40:55 +08:00
return self.config.Fleet_Fleet2
else:
return self.config.Fleet_Fleet1
2021-06-30 02:15:57 +08:00
def flagship_change(self):
"""
2022-06-25 20:04:45 +08:00
Change flagship and flagship's equipment
If config.GemsFarming_CommonCV == 'any', only change auxiliary equipment
Returns:
bool: True if flagship changed.
"""
if self.config.GemsFarming_CommonCV == 'any':
2021-08-14 01:18:15 +08:00
index_list = range(3, 5)
else:
2021-08-14 01:18:15 +08:00
index_list = range(0, 5)
logger.hr('Change flagship', level=1)
logger.attr('ChangeFlagship', self.config.GemsFarming_ChangeFlagship)
2024-04-19 15:40:55 +08:00
self.fleet_enter(self.fleet_to_attack)
if self.change_flagship_equip:
logger.hr('Record flagship equipment', level=2)
self.fleet_enter_ship(FLEET_DETAIL_ENTER_FLAGSHIP)
self.ship_equipment_record_image(index_list=index_list)
self.ship_equipment_take_off()
self.fleet_back()
2021-06-30 02:15:57 +08:00
logger.hr('Change flagship', level=2)
success = self.flagship_change_execute()
2021-06-29 23:08:59 +08:00
if self.change_flagship_equip:
logger.hr('Equip flagship equipment', level=2)
self.fleet_enter_ship(FLEET_DETAIL_ENTER_FLAGSHIP)
self.ship_equipment_take_off()
self.ship_equipment_take_on_image(index_list=index_list)
self.fleet_back()
2021-08-14 01:18:15 +08:00
return success
2021-07-06 16:48:52 +08:00
def vanguard_change(self):
"""
2022-06-25 20:04:45 +08:00
Change vanguard and vanguard's equipment
Returns:
bool: True if vanguard changed
"""
logger.hr('Change vanguard', level=1)
logger.attr('ChangeVanguard', self.config.GemsFarming_ChangeVanguard)
2024-04-19 15:40:55 +08:00
self.fleet_enter(self.fleet_to_attack)
if self.change_vanguard_equip:
logger.hr('Record vanguard equipment', level=2)
self.fleet_enter_ship(FLEET_DETAIL_ENTER)
self.ship_equipment_record_image()
self.ship_equipment_take_off()
self.fleet_back()
2021-07-06 16:48:52 +08:00
logger.hr('Change vanguard', level=2)
success = self.vanguard_change_execute()
2021-07-06 16:48:52 +08:00
if self.change_vanguard_equip:
logger.hr('Equip vanguard equipment', level=2)
self.fleet_enter_ship(FLEET_DETAIL_ENTER)
self.ship_equipment_take_off()
self.ship_equipment_take_on_image()
self.fleet_back()
return success
2021-06-30 02:15:57 +08:00
def _ship_change_confirm(self, button):
2021-06-30 02:15:57 +08:00
self.dock_select_one(button)
self.dock_filter_set()
self.dock_sort_method_dsc_set()
2021-06-30 02:15:57 +08:00
self.dock_select_confirm(check_button=page_fleet.check_button)
2021-06-29 23:08:59 +08:00
def get_common_rarity_cv(self):
"""
Get a common rarity cv by config.GemsFarming_CommonCV
2022-10-18 00:08:42 +08:00
If config.GemsFarming_CommonCV == 'any', return a common lv1 ~ lv33 cv
2021-06-29 23:08:59 +08:00
Returns:
2022-10-18 00:08:42 +08:00
Ship:
2021-06-29 23:08:59 +08:00
"""
2021-08-14 22:31:11 +08:00
logger.hr('FINDING FLAGSHIP')
2021-07-06 15:50:33 +08:00
2024-04-19 15:40:55 +08:00
scanner = ShipScanner(level=(1, 31), emotion=(10, 150),
fleet=self.fleet_to_attack, status='free')
scanner.disable('rarity')
if self.config.GemsFarming_CommonCV == 'any':
self.dock_sort_method_dsc_set(False)
ships = scanner.scan(self.device.image)
if ships:
# Don't need to change current
return ships
scanner.set_limitation(fleet=0)
return scanner.scan(self.device.image, output=False)
2021-08-13 17:48:44 +08:00
else:
template = {
'BOGUE': TEMPLATE_BOGUE,
'HERMES': TEMPLATE_HERMES,
'LANGLEY': TEMPLATE_LANGLEY,
'RANGER': TEMPLATE_RANGER
}[f'{self.config.GemsFarming_CommonCV.upper()}']
2021-07-06 15:50:33 +08:00
2021-08-13 17:48:44 +08:00
self.dock_sort_method_dsc_set()
ships = scanner.scan(self.device.image)
if ships:
# Don't need to change current
return ships
scanner.set_limitation(fleet=0)
candidates = [ship for ship in scanner.scan(self.device.image, output=False)
if template.match(self.image_crop(ship.button, copy=False), similarity=SIM_VALUE)]
if candidates:
return candidates
2021-08-14 22:31:11 +08:00
logger.info('No specific CV was found, try reversed order.')
2021-08-13 17:48:44 +08:00
self.dock_sort_method_dsc_set(False)
2021-07-06 15:50:33 +08:00
candidates = [ship for ship in scanner.scan(self.device.image)
if template.match(self.image_crop(ship.button, copy=False), similarity=SIM_VALUE)]
2021-06-30 02:15:57 +08:00
return candidates
2021-06-30 02:15:57 +08:00
def get_common_rarity_dd(self):
"""
2022-10-18 00:08:42 +08:00
Get a common rarity dd with level is 100 (70 for servers except CN) and emotion > 10
Returns:
2022-10-18 00:08:42 +08:00
Ship:
"""
2021-08-14 22:31:11 +08:00
logger.hr('FINDING VANGUARD')
if self.config.SERVER in ['cn']:
max_level = 100
else:
max_level = 70
scanner = ShipScanner(level=(max_level, max_level), emotion=(10, 150),
2024-04-19 15:40:55 +08:00
fleet=self.fleet_to_attack, status='free')
scanner.disable('rarity')
2022-08-29 12:56:01 +08:00
self.dock_sort_method_dsc_set()
ships = scanner.scan(self.device.image)
if ships:
# Don't need to change current
return ships
scanner.set_limitation(fleet=0)
if self.config.GemsFarming_CommonDD == 'any':
return scanner.scan(self.device.image, output=False)
candidates = self.find_candidates(self.get_templates(self.config.GemsFarming_CommonDD), scanner)
if candidates:
return candidates
logger.info('No specific DD was found, try reversed order.')
self.dock_sort_method_dsc_set(False)
candidates = self.find_candidates(self.get_templates(self.config.GemsFarming_CommonDD), scanner)
return candidates
def find_candidates(self, template, scanner):
"""
Find candidates based on template matching using a scanner.
"""
candidates = []
for item in template:
candidates = [ship for ship in scanner.scan(self.device.image, output=False)
if item.match(self.image_crop(ship.button, copy=False), similarity=SIM_VALUE)]
if candidates:
break
return candidates
@staticmethod
def get_templates(common_dd):
"""
Returns the corresponding template list based on CommonDD
"""
if common_dd == 'any':
return [
TEMPLATE_CASSIN_1, TEMPLATE_CASSIN_2,
TEMPLATE_DOWNES_1, TEMPLATE_DOWNES_2,
TEMPLATE_AULICK,
TEMPLATE_FOOTE
]
elif common_dd == 'aulick_or_foote':
return [
TEMPLATE_AULICK,
TEMPLATE_FOOTE
]
elif common_dd == 'cassin_or_downes':
return [
TEMPLATE_CASSIN_1, TEMPLATE_CASSIN_2,
TEMPLATE_DOWNES_1, TEMPLATE_DOWNES_2
]
else:
logger.error(f'Invalid CommonDD setting: {common_dd}')
raise ScriptError(f'Invalid CommonDD setting: {common_dd}')
2021-06-29 23:08:59 +08:00
def flagship_change_execute(self):
"""
Returns:
bool: If success.
Pages:
in: page_fleet
out: page_fleet
"""
self.ui_click(FLEET_ENTER_FLAGSHIP,
appear_button=page_fleet.check_button, check_button=DOCK_CHECK, skip_first_screenshot=True)
self.dock_filter_set(
2021-08-09 16:50:36 +08:00
index='cv', rarity='common', extra='enhanceable', sort='total')
2021-07-06 16:48:52 +08:00
self.dock_favourite_set(False)
2021-06-29 23:08:59 +08:00
ship = self.get_common_rarity_cv()
2022-08-29 12:56:01 +08:00
if ship:
self._ship_change_confirm(min(ship, key=lambda s: (s.level, -s.emotion)).button)
2021-06-30 02:15:57 +08:00
2021-06-29 23:08:59 +08:00
logger.info('Change flagship success')
return True
else:
logger.info('Change flagship failed, no CV in common rarity.')
self.dock_filter_set()
2021-06-29 23:08:59 +08:00
self.ui_back(check_button=page_fleet.check_button)
return False
2021-08-12 19:28:57 +08:00
def vanguard_change_execute(self):
"""
Returns:
bool: If success.
Pages:
in: page_fleet
out: page_fleet
"""
self.ui_click(FLEET_ENTER,
appear_button=page_fleet.check_button, check_button=DOCK_CHECK, skip_first_screenshot=True)
self.dock_filter_set(
2021-11-16 23:39:03 +08:00
index='dd', rarity='common', faction='eagle', extra='can_limit_break')
2021-08-12 19:28:57 +08:00
self.dock_favourite_set(False)
ship = self.get_common_rarity_dd()
2022-08-29 12:56:01 +08:00
if ship:
self._ship_change_confirm(max(ship, key=lambda s: s.emotion).button)
2021-08-12 19:28:57 +08:00
logger.info('Change vanguard ship success')
return True
else:
logger.info('Change vanguard ship failed, no DD in common rarity.')
self.dock_filter_set()
2021-08-12 19:28:57 +08:00
self.ui_back(check_button=page_fleet.check_button)
return False
2021-08-14 01:18:15 +08:00
2021-06-30 18:04:31 +08:00
_trigger_lv32 = False
2021-08-12 05:19:39 +08:00
_trigger_emotion = False
2021-06-30 18:04:31 +08:00
def triggered_stop_condition(self, oil_check=True):
# Lv32 limit
if self.change_flagship and self.campaign.config.LV32_TRIGGERED:
2021-06-30 18:04:31 +08:00
self._trigger_lv32 = True
2021-08-14 22:31:11 +08:00
logger.hr('TRIGGERED LV32 LIMIT')
2021-06-30 18:04:31 +08:00
return True
2021-08-14 01:18:15 +08:00
if self.campaign.map_is_auto_search and self.campaign.config.GEMS_EMOTION_TRIGGRED:
2021-08-12 05:19:39 +08:00
self._trigger_emotion = True
2021-08-14 22:31:11 +08:00
logger.hr('TRIGGERED EMOTION LIMIT')
2021-08-12 05:19:39 +08:00
return True
2021-06-30 18:04:31 +08:00
return super().triggered_stop_condition(oil_check=oil_check)
def run(self, name, folder='campaign_main', mode='normal', total=0):
"""
Args:
name (str): Name of .py file.
folder (str): Name of the file folder under campaign.
mode (str): `normal` or `hard`
total (int):
"""
self.config.STOP_IF_REACH_LV32 = self.change_flagship
self.config.RETIRE_KEEP_COMMON_CV = True
2021-08-12 05:19:39 +08:00
while 1:
self._trigger_lv32 = False
is_limit = self.config.StopCondition_RunCount
2021-08-14 01:18:15 +08:00
2021-09-28 02:17:20 +08:00
try:
super().run(name=name, folder=folder, total=total)
except CampaignEnd as e:
if e.args[0] == 'Emotion withdraw':
self._trigger_emotion = True
else:
raise e
2021-08-12 05:19:39 +08:00
# End
if self._trigger_lv32 or self._trigger_emotion:
success = True
if self.change_flagship:
success = self.flagship_change()
if self.change_vanguard:
success = success and self.vanguard_change()
if is_limit and self.config.StopCondition_RunCount <= 0:
logger.hr('Triggered stop condition: Run count')
self.config.StopCondition_RunCount = 0
self.config.Scheduler_Enable = False
break
2021-08-14 02:10:36 +08:00
2021-08-12 05:19:39 +08:00
self._trigger_lv32 = False
self._trigger_emotion = False
self.campaign.config.LV32_TRIGGERED = False
2021-08-12 06:14:24 +08:00
self.campaign.config.GEMS_EMOTION_TRIGGRED = False
# Scheduler
if self.config.task_switched():
self.campaign.ensure_auto_search_exit()
self.config.task_stop()
elif not success:
self.campaign.ensure_auto_search_exit()
self.config.task_delay(minute=30)
self.config.task_stop()
2021-08-12 05:19:39 +08:00
continue
else:
break