StarRailCopilot/tasks/combat/fuel.py

170 lines
5.5 KiB
Python
Raw Normal View History

import module.config.server as server
2024-06-27 22:29:39 +00:00
from module.base.utils import crop, area_offset
from module.logger import logger
from module.ocr.ocr import Digit
from tasks.base.assets.assets_base_popup import POPUP_CONFIRM, POPUP_CANCEL
from tasks.base.ui import UI
from tasks.item.slider import Slider
2024-06-29 08:18:06 +00:00
from tasks.combat.assets.assets_combat_finish import COMBAT_AGAIN
2024-06-27 22:29:39 +00:00
from tasks.combat.assets.assets_combat_prepare import (
2024-06-29 08:18:06 +00:00
COMBAT_PREPARE,
2024-06-27 22:29:39 +00:00
EXTRACT_RESERVED_TRAILBLAZE_POWER,
FUEL,
FUEL_MINUS,
FUEL_PLUS,
2024-06-27 22:29:39 +00:00
FUEL_SELECTED,
OCR_FUEL,
OCR_FUEL_COUNT,
2024-06-27 22:29:39 +00:00
OCR_RESERVED_TRAILBLAZE_POWER,
OCR_EXTRACT_RESERVED_TRAILBLAZE_POWER_COUNT,
2024-06-27 22:29:39 +00:00
RESERVED_TRAILBLAZE_POWER_ENTRANCE,
RESERVED_MINUS,
RESERVED_PLUS,
RESERVED_SLIDER,
2024-06-27 22:29:39 +00:00
FUEL_ENTRANCE,
USING_FUEL,
FUEL_SLIDER
2024-06-27 22:29:39 +00:00
)
class Fuel(UI):
fuel_trailblaze_power = 60
trailblaze_max_limit = 240
2024-06-29 08:18:06 +00:00
def _use_fuel_finish(self):
"""
Two possible finish states after using fuel/extract trailblaze power:
1. COMBAT_PREPARE
2. COMBAT_AGAIN
"""
return self.appear(COMBAT_PREPARE) or self.appear(COMBAT_AGAIN)
def extract_reserved_trailblaze_power(self, current, skip_first_screenshot=True):
2024-06-27 22:29:39 +00:00
"""
Extract reserved trailblaze power from previous combat.
Returns:
int: Reserved trailblaze power
"""
logger.info('Extract reserved trailblaze power')
reserved = Digit(OCR_RESERVED_TRAILBLAZE_POWER).ocr_single_line(self.device.image)
if reserved == 0:
2024-06-27 22:29:39 +00:00
logger.info('No reserved trailblaze power')
return
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
if self.appear(POPUP_CONFIRM):
2024-06-29 08:18:06 +00:00
break
2024-06-27 22:29:39 +00:00
if self.appear_then_click(EXTRACT_RESERVED_TRAILBLAZE_POWER):
continue
if self.appear_then_click(RESERVED_TRAILBLAZE_POWER_ENTRANCE):
continue
count = min(reserved, self.trailblaze_max_limit - current)
self.set_reserved_trailblaze_power(count, total=reserved)
skip_first_screenshot = True
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
if self._use_fuel_finish():
break
2024-06-27 22:29:39 +00:00
if self.appear_then_click(POPUP_CONFIRM):
continue
if self.handle_reward():
2024-06-29 08:18:06 +00:00
continue
2024-06-27 22:29:39 +00:00
def set_reserved_trailblaze_power(self, count, total):
slider = Slider(main=self, slider=RESERVED_SLIDER)
slider.set(count, total)
self.ui_ensure_index(
count, letter=Digit(OCR_EXTRACT_RESERVED_TRAILBLAZE_POWER_COUNT, lang=server.lang),
next_button=RESERVED_PLUS, prev_button=RESERVED_MINUS,
skip_first_screenshot=True
)
def set_fuel_count(self, count, total):
slider = Slider(main=self, slider=FUEL_SLIDER)
slider.set(count, total)
self.ui_ensure_index(
count, letter=Digit(OCR_FUEL_COUNT, lang=server.lang),
next_button=FUEL_PLUS, prev_button=FUEL_MINUS,
skip_first_screenshot=True
)
def use_fuel(self, current, skip_first_screenshot=True):
need = (self.trailblaze_max_limit - current) // self.fuel_trailblaze_power
if need == 0:
logger.info(f"Current trailblaze power is near {self.trailblaze_max_limit}, no need to use fuel")
return
2024-06-27 22:29:39 +00:00
logger.info("Use Fuel")
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
2024-06-29 08:18:06 +00:00
if self.appear(POPUP_CONFIRM) and not (self.appear(FUEL_SELECTED) and self.appear(FUEL)):
2024-06-27 22:29:39 +00:00
logger.info("No fuel found")
return
if self.appear(FUEL_SELECTED):
break
if self.appear_then_click(FUEL):
continue
if self.appear_then_click(FUEL_ENTRANCE):
continue
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)
available_count = count - self.config.TrailblazePower_UseFuelUntilRemainCount
need = min(need, available_count)
if need <= 0:
2024-06-27 22:29:39 +00:00
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.appear(USING_FUEL):
2024-06-27 22:29:39 +00:00
break
if self.appear(FUEL) and self.appear_then_click(POPUP_CONFIRM):
2024-06-29 08:18:06 +00:00
continue
self.set_fuel_count(need, count)
skip_first_screenshot = True
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
if self._use_fuel_finish():
break
2024-06-27 22:29:39 +00:00
if self.appear_then_click(POPUP_CONFIRM):
continue
if self.handle_reward():
2024-06-27 22:29:39 +00:00
continue