StarRailCopilot/tasks/rogue/route/base.py

444 lines
15 KiB
Python
Raw Normal View History

2023-10-07 15:20:28 +00:00
from module.base.button import ClickButton
from module.base.timer import Timer
from module.base.utils import area_offset
2023-10-01 15:03:57 +00:00
from module.logger import logger
from tasks.base.page import page_rogue
from tasks.map.control.waypoint import Waypoint, ensure_waypoints
2023-10-01 15:03:57 +00:00
from tasks.map.route.base import RouteBase as RouteBase_
from tasks.rogue.assets.assets_rogue_ui import BLESSING_CONFIRM
from tasks.rogue.assets.assets_rogue_weekly import ROGUE_REPORT
2023-12-18 16:53:33 +00:00
from tasks.rogue.blessing.blessing import RogueBlessingSelector
from tasks.rogue.blessing.bonus import RogueBonusSelector
from tasks.rogue.blessing.curio import RogueCurioSelector
2023-10-04 03:06:28 +00:00
from tasks.rogue.event.event import RogueEvent
2023-10-20 09:48:22 +00:00
from tasks.rogue.event.reward import RogueReward
2023-10-01 15:03:57 +00:00
from tasks.rogue.route.exit import RogueExit
2023-10-20 09:48:22 +00:00
class RouteBase(RouteBase_, RogueExit, RogueEvent, RogueReward):
2023-10-01 15:03:57 +00:00
registered_domain_exit = None
enroute_add_item = True
2023-10-01 15:03:57 +00:00
def combat_expected_end(self):
# Curio effect, that drops curio after combat
if self.handle_blessing_popup():
return False
# Blessings after combat
2023-10-01 15:03:57 +00:00
if self.is_page_choose_blessing():
logger.info('Combat ended at is_page_choose_blessing()')
return True
if self.is_page_choose_curio():
logger.info('Combat ended at is_page_choose_curio()')
return True
if self.is_page_choose_bonus():
logger.info('Combat ended at is_page_choose_bonus()')
return True
return False
def combat_execute(self, expected_end=None):
super().combat_execute(expected_end=self.combat_expected_end)
self.clear_blessing()
2023-10-01 15:03:57 +00:00
2023-10-22 18:33:25 +00:00
def walk_additional(self) -> bool:
if self.handle_blessing_popup():
return True
return super().walk_additional()
def handle_blessing(self):
"""
Returns:
bool: If handled
"""
if self.is_page_choose_blessing():
logger.hr('Choose blessing', level=2)
selector = RogueBlessingSelector(self)
selector.recognize_and_select()
return True
if self.is_page_choose_curio():
logger.hr('Choose curio', level=2)
selector = RogueCurioSelector(self)
selector.recognize_and_select()
return True
if self.is_page_choose_bonus():
logger.hr('Choose bonus', level=2)
selector = RogueBonusSelector(self)
selector.recognize_and_select()
return True
if self.handle_blessing_popup():
return True
return False
2023-10-01 15:03:57 +00:00
def clear_blessing(self, skip_first_screenshot=True):
"""
Pages:
in: combat_expected_end()
out: is_in_main()
Returns:
bool: If cleared
2023-10-01 15:03:57 +00:00
"""
logger.info('Clear blessing')
cleared = False
2023-10-01 15:03:57 +00:00
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
# End
if self.is_in_main():
logger.info(f'clear_blessing() ended at page_main')
if cleared:
2023-10-07 15:20:28 +00:00
self.wait_until_minimap_stabled()
return cleared
2023-10-01 15:03:57 +00:00
if self.handle_blessing():
cleared = True
continue
2023-10-01 15:03:57 +00:00
def clear_occurrence(self, skip_first_screenshot=True):
"""
Pages:
in: page_rogue, occurrence
out: is_in_main()
"""
logger.info('Clear occurrence')
2023-10-07 14:20:01 +00:00
self.event_title = None
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
2023-10-01 15:03:57 +00:00
# End
if self.is_in_main():
logger.info(f'clear_occurrence() ended at page_main')
break
if self.handle_reward(interval=2):
continue
if self.is_combat_executing():
2023-10-04 03:06:28 +00:00
logger.hr('Combat', level=2)
self.combat_execute()
continue
if self.handle_blessing():
continue
2023-10-04 03:06:28 +00:00
if self.ui_page_appear(page_rogue):
if self.handle_event_continue():
continue
if self.handle_event_option():
continue
2023-10-07 15:20:28 +00:00
def wait_until_minimap_stabled(self):
logger.info('Wait until minimap stabled')
radius = self.minimap.MINIMAP_RADIUS
area = area_offset((-radius, -radius, radius, radius), offset=self.minimap.MINIMAP_CENTER)
minimap = ClickButton(area, name='MINIMAP')
self.wait_until_stable(minimap, timeout=Timer(1.5, count=5))
def clear_enemy(self, *waypoints):
waypoints = ensure_waypoints(waypoints)
if self.enroute_add_item and self.plane.is_rogue_combat:
2023-11-21 03:49:59 +00:00
for point in waypoints:
point.enroute_add_item()
return super().clear_enemy(*waypoints)
def clear_item(self, *waypoints):
"""
Shorten unexpected timer as items are randomly generated
"""
waypoints = ensure_waypoints(waypoints)
end_point = waypoints[-1]
2023-11-16 10:43:14 +00:00
if self.plane.is_rogue_combat or self.plane.is_rogue_occurrence:
end_point.unexpected_confirm = Timer(1, count=5)
return super().clear_item(*waypoints)
"""
Additional rogue methods
"""
2023-10-01 15:03:57 +00:00
def clear_elite(self, *waypoints):
logger.hr('Clear elite', level=1)
waypoints = ensure_waypoints(waypoints)
end_point = waypoints[-1]
end_point.speed = 'run_2x'
# TODO: Use techniques before BOSS
2023-10-01 15:03:57 +00:00
pass
result = super().clear_enemy(*waypoints)
# logger.attr("result",result)
if 'enemy' in result:
# runs when one elite battle finishes, and increases rogue farming count by 1
if not self.config.stored.SimulatedUniverseFarm.is_full():
self.config.stored.SimulatedUniverseFarm.add()
logger.info(
f"Cleared elite boss, increasing farming count by 1, now " + self.config.stored.SimulatedUniverseFarm.to_counter())
2023-10-01 15:03:57 +00:00
return result
def _domain_event_expected_end(self):
"""
Returns:
bool: If entered event
"""
if self.ui_page_appear(page_rogue):
return True
return False
2023-10-01 15:03:57 +00:00
def clear_event(self, *waypoints):
"""
Handle an event in DomainOccurrence, DomainEncounter, DomainTransaction
"""
logger.hr('Clear event', level=1)
waypoints = ensure_waypoints(waypoints)
end_point = waypoints[-1]
end_point.endpoint_threshold = 1.5
2023-10-04 17:58:14 +00:00
end_point.interact_radius = 7
end_point.expected_end.append(self._domain_event_expected_end)
if self.enroute_add_item and self.plane.is_rogue_occurrence:
2023-11-21 03:49:59 +00:00
for point in waypoints:
point.enroute_add_item()
2023-10-01 15:03:57 +00:00
result = self.goto(*waypoints)
self.clear_occurrence()
2023-10-01 15:03:57 +00:00
return result
def domain_reward(self, *waypoints):
"""
Get reward of the DomainElite and DomainBoss
"""
logger.hr('Clear reward', level=1)
2023-12-08 10:09:33 +00:00
if self.can_claim_domain_reward(
use_trailblaze_power=self.config.RogueWorld_UseStamina,
use_immersifier=self.config.RogueWorld_UseImmersifier,
):
2023-10-20 09:48:22 +00:00
result = self.goto(*waypoints)
2023-12-08 10:09:33 +00:00
self.claim_domain_reward(
use_trailblaze_power=self.config.RogueWorld_UseStamina,
use_immersifier=self.config.RogueWorld_UseImmersifier,
)
2023-10-20 09:48:22 +00:00
else:
result = []
2023-10-01 15:03:57 +00:00
return result
def domain_herta(self, *waypoints):
"""
Most people don't buy herta shop, skip
"""
pass
def _domain_exit_expected_end(self):
"""
Returns:
bool: If domain exited
"""
if self.is_map_loading():
logger.info('domain exit: is_map_loading()')
return True
# No loading after elite
if self.is_map_loading_black():
logger.info('domain exit: is_map_loading_black()')
return True
# Rogue cleared
if self.appear(ROGUE_REPORT, interval=2):
logger.info(f'domain exit: {ROGUE_REPORT}')
return True
if self.handle_popup_confirm():
return False
return False
def _domain_exit_wait_next(self, skip_first_screenshot=True):
"""
Pages:
in: is_map_loading()
out: page_main
or page_rogue if rogue cleared
"""
logger.info('Wait next domain')
self.device.screenshot_interval_set('combat')
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
# End
if self.is_in_main():
logger.info('Entered another domain')
self.device.screenshot_interval_set()
2023-10-07 15:20:28 +00:00
self.wait_until_minimap_stabled()
break
if self.is_page_rogue_main():
logger.info('Rogue cleared')
self.device.screenshot_interval_set()
break
if self.match_template_color(ROGUE_REPORT, interval=2):
logger.info(f'{ROGUE_REPORT} -> {BLESSING_CONFIRM}')
self.device.click(BLESSING_CONFIRM)
continue
2023-11-17 19:54:56 +00:00
if self.handle_blessing():
continue
# Confirm that leave without getting rewards
if self.handle_popup_confirm():
continue
# First-time cleared reward
if self.handle_reward():
continue
# Get Herta
if self.handle_get_character():
continue
2023-10-01 15:03:57 +00:00
def domain_single_exit(self, *waypoints):
"""
Goto a single exit, exit current domain
end_rotation is not required
"""
logger.hr('Domain single exit', level=1)
waypoints = ensure_waypoints(waypoints)
if self.enroute_add_item:
for point in waypoints:
point.enroute_add_item()
end_point = waypoints[-1]
end_point.min_speed = 'run'
end_point.interact_radius = 5
end_point.expected_end.append(self._domain_exit_expected_end)
2023-10-01 15:03:57 +00:00
result = self.goto(*waypoints)
self._domain_exit_wait_next()
2023-10-01 15:03:57 +00:00
return result
def _domain_exit_old(self):
"""
An old implementation that go along specific direction without retries
"""
logger.info(f'Using old predict_door()')
direction = self.predict_door_old()
direction_limit = 55
if direction is not None:
if abs(direction) > direction_limit:
logger.warning(f'Unexpected direction to go: {direction}, limited in {direction_limit}')
if direction > 0:
direction = direction_limit
elif direction < 0:
direction = -direction_limit
point = Waypoint(
position=(0, 0),
min_speed='run',
lock_direction=direction,
interact_radius=10000,
expected_end=[self._domain_exit_expected_end],
)
self.goto(point)
self._domain_exit_wait_next()
return True
def domain_exit(
self,
*waypoints,
end_rotation: int = None,
left_door: Waypoint = None,
right_door: Waypoint = None
):
"""
Goto domain exit, choose one door, goto door
"""
2023-10-01 15:03:57 +00:00
logger.hr('Domain exit', level=1)
# Goto the front of the two doors
2023-10-01 15:03:57 +00:00
waypoints = ensure_waypoints(waypoints)
end_point = waypoints[-1]
end_point.endpoint_threshold = 1.5
self.goto(*waypoints)
2023-10-01 15:03:57 +00:00
# Rotate camera to insight two doors
2023-10-06 14:53:13 +00:00
logger.hr('End rotation', level=2)
self.rotation_set(end_rotation, threshold=10)
# Choose a door
2023-10-04 17:58:14 +00:00
logger.hr('Find domain exit', level=2)
2023-12-08 10:09:33 +00:00
logger.info(f'Migrate={self.config.RogueDebug_DebugMode}, left_door={left_door}, right_door={right_door}')
if not self.config.RogueDebug_DebugMode and (not left_door and not right_door):
return self._domain_exit_old()
logger.info(f'Using new predict_door()')
door = self.predict_door()
2023-12-08 10:09:33 +00:00
if self.config.RogueDebug_DebugMode and self.exit_has_double_door and (not left_door or not right_door):
2023-12-06 16:41:33 +00:00
logger.critical(f'Domain exit is not defined, please record it: {self.route_func}')
exit(1)
# Goto door
if door == 'left_door':
if not left_door:
return self._domain_exit_old()
if self.domain_single_exit(left_door):
return True
else:
logger.error('Cannot goto either exit doors, try both')
if self.domain_single_exit(right_door):
return True
else:
return False
elif door == 'right_door':
if not right_door:
return self._domain_exit_old()
if self.domain_single_exit(right_door):
return True
else:
logger.error('Cannot goto either exit doors, try both')
if self.domain_single_exit(left_door):
return True
else:
return False
else:
logger.error('Cannot goto either exit doors, try both')
if not left_door:
return self._domain_exit_old()
if not right_door:
return self._domain_exit_old()
if self.domain_single_exit(left_door):
return True
elif self.domain_single_exit(right_door):
return True
else:
return False
2023-10-01 15:03:57 +00:00
"""
Route
"""
def register_domain_exit(
self,
*waypoints,
end_rotation: int = None,
left_door: Waypoint = None,
right_door: Waypoint = None
):
2023-10-01 15:03:57 +00:00
"""
Register an exit, call `domain_exit()` at route end
"""
self.registered_domain_exit = (waypoints, end_rotation, left_door, right_door)
2023-10-01 15:03:57 +00:00
def before_route(self):
self.registered_domain_exit = None
def after_route(self):
if self.registered_domain_exit is not None:
waypoints, end_rotation, left_door, right_door = self.registered_domain_exit
self.domain_exit(
*waypoints,
end_rotation=end_rotation,
left_door=left_door,
right_door=right_door,
)
2023-10-01 15:03:57 +00:00
else:
logger.info('No domain exit registered')