From e9758080d12e4f6a2e2dd03e4f2ff08aa85ef2ed Mon Sep 17 00:00:00 2001 From: Hengyu Date: Fri, 28 Jun 2024 03:08:48 +0800 Subject: [PATCH] Add: use fuel & extract trailblaze power --- tasks/combat/assets/assets_combat_prepare.py | 80 ++++++++++++++++++++ tasks/combat/combat.py | 20 ++++- tasks/combat/prepare.py | 79 ++++++++++++++++++- 3 files changed, 177 insertions(+), 2 deletions(-) diff --git a/tasks/combat/assets/assets_combat_prepare.py b/tasks/combat/assets/assets_combat_prepare.py index 8363a4da0..b94e4030f 100644 --- a/tasks/combat/assets/assets_combat_prepare.py +++ b/tasks/combat/assets/assets_combat_prepare.py @@ -20,6 +20,66 @@ COMBAT_PREPARE = ButtonWrapper( button=(956, 640, 1225, 676), ), ) +EXTRACT_RESERVED_TRAILBLAZE_POWER = ButtonWrapper( + name='EXTRACT_RESERVED_TRAILBLAZE_POWER', + share=Button( + file='./assets/share/combat/prepare/EXTRACT_RESERVED_TRAILBLAZE_POWER.png', + area=(909, 506, 929, 526), + search=(889, 486, 949, 546), + color=(91, 91, 91), + button=(909, 506, 929, 526), + ), +) +FUEL = ButtonWrapper( + name='FUEL', + share=Button( + file='./assets/share/combat/prepare/FUEL.png', + area=(592, 276, 688, 388), + search=(572, 256, 708, 408), + color=(113, 90, 123), + button=(592, 276, 688, 388), + ), +) +FUEL_ENTRANCE = ButtonWrapper( + name='FUEL_ENTRANCE', + share=Button( + file='./assets/share/combat/prepare/FUEL_ENTRANCE.png', + area=(1035, 26, 1056, 48), + search=(1015, 6, 1076, 68), + color=(188, 180, 226), + button=(1035, 26, 1056, 48), + ), +) +FUEL_SELECTED = ButtonWrapper( + name='FUEL_SELECTED', + share=Button( + file='./assets/share/combat/prepare/FUEL_SELECTED.png', + area=(587, 271, 692, 395), + search=(474, 271, 811, 396), + color=(130, 111, 137), + button=(587, 271, 692, 395), + ), +) +OCR_FUEL = ButtonWrapper( + name='OCR_FUEL', + share=Button( + file='./assets/share/combat/prepare/OCR_FUEL.png', + area=(605, 369, 678, 386), + search=(585, 349, 698, 406), + color=(66, 66, 66), + button=(605, 369, 678, 386), + ), +) +OCR_RESERVED_TRAILBLAZE_POWER = ButtonWrapper( + name='OCR_RESERVED_TRAILBLAZE_POWER', + share=Button( + file='./assets/share/combat/prepare/OCR_RESERVED_TRAILBLAZE_POWER.png', + area=(883, 29, 992, 44), + search=(863, 9, 1012, 64), + color=(51, 65, 65), + button=(883, 29, 992, 44), + ), +) OCR_TRAILBLAZE_POWER = ButtonWrapper( name='OCR_TRAILBLAZE_POWER', share=Button( @@ -50,6 +110,26 @@ OCR_WAVE_COUNT = ButtonWrapper( button=(911, 555, 1151, 581), ), ) +RESERVED_TRAILBLAZE_POWER = ButtonWrapper( + name='RESERVED_TRAILBLAZE_POWER', + share=Button( + file='./assets/share/combat/prepare/RESERVED_TRAILBLAZE_POWER.png', + area=(480, 276, 576, 388), + search=(474, 271, 811, 396), + color=(111, 115, 148), + button=(480, 276, 576, 388), + ), +) +RESERVED_TRAILBLAZE_POWER_ENTRANCE = ButtonWrapper( + name='RESERVED_TRAILBLAZE_POWER_ENTRANCE', + share=Button( + file='./assets/share/combat/prepare/RESERVED_TRAILBLAZE_POWER_ENTRANCE.png', + area=(895, 26, 916, 48), + search=(875, 6, 936, 68), + color=(154, 213, 214), + button=(895, 26, 916, 48), + ), +) WAVE_MINUS = ButtonWrapper( name='WAVE_MINUS', share=Button( diff --git a/tasks/combat/combat.py b/tasks/combat/combat.py index d7355e97e..bdbffebbd 100644 --- a/tasks/combat/combat.py +++ b/tasks/combat/combat.py @@ -53,7 +53,10 @@ class Combat(CombatInteract, CombatPrepare, CombatState, CombatTeam, CombatSuppo # Check limits if self.config.stored.TrailblazePower.value < self.combat_wave_cost: - logger.info('Trailblaze power exhausted, cannot continue combat') + if self.config.Dungeon_ExtractReservedTrailblazePower or self.config.Dungeon_UseFuel: + return self._try_get_more_trablaize_power(self.config.stored.TrailblazePower, self.combat_wave_cost) + else: + logger.info('Trailblaze power exhausted, cannot continue combat') return False if self.combat_waves <= 0: logger.info('Combat wave limited, cannot continue combat') @@ -214,6 +217,8 @@ class Combat(CombatInteract, CombatPrepare, CombatState, CombatTeam, CombatSuppo if current >= self.combat_wave_cost * self.combat_waves: logger.info(f'Current has {current}, combat costs {self.combat_wave_cost}, can run again') return True + elif self.config.Dungeon_ExtractReservedTrailblazePower or self.config.Dungeon_UseFuel: + return self._try_get_more_trablaize_power(current, self.combat_wave_cost * self.combat_waves) else: logger.info(f'Current has {current}, combat costs {self.combat_wave_cost}, can not run again') return False @@ -224,10 +229,23 @@ class Combat(CombatInteract, CombatPrepare, CombatState, CombatTeam, CombatSuppo if current >= self.combat_wave_cost: logger.info(f'Current has {current}, combat costs {self.combat_wave_cost}, can run again') return True + elif self.config.Dungeon_ExtractReservedTrailblazePower or self.config.Dungeon_UseFuel: + return self._try_get_more_trablaize_power(current, self.combat_wave_cost * self.combat_waves) else: logger.info(f'Current has {current}, combat costs {self.combat_wave_cost}, can not run again') return False + def _try_get_more_trablaize_power(self, current, cost): + if self.config.Dungeon_ExtractReservedTrailblazePower: + logger.info('Extract reserved trailblaze power to get more trailblaze power') + self.combat_extract_reserved_trailblaze_power() + current = self.combat_get_trailblaze_power() + if self.config.Dungeon_UseFuel: + logger.info('Use fuel to get more trailblaze power') + self.combat_use_fuel() + current = self.combat_get_trailblaze_power() + return current >= cost + def _combat_should_reenter(self): """ Returns: diff --git a/tasks/combat/prepare.py b/tasks/combat/prepare.py index 0b47a2c41..a78bd5862 100644 --- a/tasks/combat/prepare.py +++ b/tasks/combat/prepare.py @@ -2,14 +2,22 @@ import re import module.config.server as server from module.base.timer import Timer -from module.base.utils import color_similar, get_color +from module.base.utils import color_similar, get_color, crop, area_offset from module.logger import logger from module.ocr.ocr import Digit, DigitCounter +from tasks.base.assets.assets_base_popup import POPUP_CONFIRM, POPUP_CANCEL from tasks.base.ui import UI from tasks.combat.assets.assets_combat_prepare import ( + EXTRACT_RESERVED_TRAILBLAZE_POWER, + FUEL, + FUEL_SELECTED, + OCR_FUEL, OCR_TRAILBLAZE_POWER, + OCR_RESERVED_TRAILBLAZE_POWER, OCR_WAVE_COST, OCR_WAVE_COUNT, + RESERVED_TRAILBLAZE_POWER_ENTRANCE, + FUEL_ENTRANCE, WAVE_MINUS, WAVE_PLUS, WAVE_SLIDER ) @@ -148,3 +156,72 @@ class CombatPrepare(UI): logger.warning(f'Get combat wave cost timeout, assume it costs {cost}') self.combat_wave_cost = cost return cost + + def combat_extract_reserved_trailblaze_power(self, skip_first_screenshot=True): + """ + Extract reserved trailblaze power from previous combat. + + Returns: + int: Reserved trailblaze power + """ + logger.info('Extract reserved trailblaze power') + current = Digit(OCR_RESERVED_TRAILBLAZE_POWER).ocr_single_line(self.device.image) + if current == 0: + logger.info('No reserved trailblaze power') + return + + while 1: + if skip_first_screenshot: + skip_first_screenshot = False + else: + self.device.screenshot() + + if self.appear_then_click(EXTRACT_RESERVED_TRAILBLAZE_POWER): + continue + if self.appear_then_click(RESERVED_TRAILBLAZE_POWER_ENTRANCE): + continue + if self.appear_then_click(POPUP_CONFIRM): + continue + if self.handle_reward(): + break + + def combat_use_fuel(self, skip_first_screenshot=True): + logger.info("Use Fuel") + while 1: + if skip_first_screenshot: + skip_first_screenshot = False + else: + self.device.screenshot() + + if self.appear(FUEL_SELECTED): + break + if self.appear_then_click(FUEL): + continue + if self.appear_then_click(FUEL_ENTRANCE): + continue + if self.handle_reward(): + break + + offset = FUEL_SELECTED.button_offset + count = Digit(OCR_FUEL).ocr_single_line(crop(self.device.image, area_offset(OCR_FUEL.area, offset)), + direct_ocr=True) + if count <= self.config.Dungeon_UseFuelUntilRemainCount: + logger.info("Fuel remain is under the threshold, stop using fuel") + while 1: + self.device.screenshot() + if self.appear_then_click(POPUP_CANCEL): + return + + skip_first_screenshot = True + + while 1: + if skip_first_screenshot: + skip_first_screenshot = False + else: + self.device.screenshot() + + if self.handle_reward(): + break + # by default, use one fuel each time + if self.appear_then_click(POPUP_CONFIRM): + continue