base.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import numpy as np
  6. import random
  7. from enum import Enum
  8. from cached_property import cached_property
  9. from datetime import timedelta, datetime
  10. from tasks.Component.SwitchSoul.switch_soul import SwitchSoul
  11. from tasks.Component.GeneralRoom.general_room import GeneralRoom
  12. from tasks.Component.GeneralInvite.general_invite import GeneralInvite
  13. from tasks.Component.ReplaceShikigami.replace_shikigami import ReplaceShikigami
  14. from tasks.Exploration.assets import ExplorationAssets
  15. from tasks.Exploration.config import ChooseRarity, AutoRotate, AttackNumber, UpType, ExplorationLevel
  16. from tasks.Component.GeneralBattle.general_battle import GeneralBattle
  17. from tasks.GameUi.game_ui import GameUi
  18. from tasks.GameUi.page import page_shikigami_records, page_main
  19. import tasks.Exploration.page as pages
  20. from tasks.RealmRaid.script_task import ScriptTask as RealmRaidScriptTask
  21. from tasks.Utils.config_enum import ShikigamiClass
  22. from module.logger import logger
  23. from module.base.timer import Timer
  24. from module.exception import RequestHumanTakeover, TaskEnd, GameStuckError
  25. from module.atom.image_grid import ImageGrid
  26. from module.atom.animate import RuleAnimate
  27. from module.base.utils import load_image
  28. class Scene(Enum):
  29. UNKNOWN = 0 #
  30. WORLD = 1 # 探索大世界
  31. ENTRANCE = 2 # 入口弹窗
  32. MAIN = 3 # 探索里面
  33. BATTLE_PREPARE = 4 # 战斗准备
  34. BATTLE_FIGHTING = 5 # 战斗中
  35. TEAM = 6 # 组队
  36. class BaseExploration(GameUi, GeneralBattle, GeneralRoom, GeneralInvite, ReplaceShikigami, SwitchSoul, ExplorationAssets):
  37. minions_cnt = 0
  38. @cached_property
  39. def _config(self):
  40. self.config.exploration.general_battle_config.lock_team_enable = True
  41. limit_time = self.config.exploration.exploration_config.limit_time
  42. self.limit_time: timedelta = timedelta(
  43. hours=limit_time.hour,
  44. minutes=limit_time.minute,
  45. seconds=limit_time.second
  46. )
  47. return self.config.model.exploration
  48. @cached_property
  49. def _match_end(self):
  50. return RuleAnimate(self.I_SWIPE_END)
  51. def is_swipe_unchanged(self, before_image, after_image, threshold: float = 15) -> bool:
  52. # 对比滑屏前后中上部背景区域(避开顶部跑马灯、下方小怪挑战图标及最右侧浮动鬼火),变化很小说明地图已滑到尽头。
  53. swipe_check_roi = (250, 140, 800, 90)
  54. before = self.I_SWIPE_END.corp(before_image, swipe_check_roi)
  55. after = self.I_SWIPE_END.corp(after_image, swipe_check_roi)
  56. diff = np.mean(np.abs(before.astype(np.int16) - after.astype(np.int16)))
  57. logger.info(f'Swipe end area diff: {diff:.2f}')
  58. return diff <= threshold
  59. def _in_chapter(self) -> bool:
  60. return (self.appear(self.I_E_SETTINGS_BUTTON)
  61. or self.appear(self.I_E_AUTO_ROTATE_ON)
  62. or self.appear(self.I_E_AUTO_ROTATE_OFF))
  63. def get_current_scene(self, reuse_screenshot: bool = True) -> Scene:
  64. if not reuse_screenshot:
  65. self.screenshot()
  66. # 结算过渡时章节背景可能命中大地图标识,优先看同一帧的入口和章节控件。
  67. if self.appear(self.I_E_EXPLORATION_CLICK):
  68. return Scene.ENTRANCE
  69. elif self._in_chapter():
  70. return Scene.MAIN
  71. elif self.appear(self.I_CHECK_EXPLORATION):
  72. return Scene.WORLD
  73. elif self.is_in_prepare(is_screenshot=False):
  74. return Scene.BATTLE_PREPARE
  75. elif self.is_in_battle(is_screenshot=False):
  76. return Scene.BATTLE_FIGHTING
  77. elif self.is_in_room(is_screenshot=False) or self.appear(self.I_CREATE_ENSURE):
  78. return Scene.TEAM
  79. # 收起章节列表时,大地图标识可能随面板隐藏。仅在没有其他场景控件时使用箭头。
  80. elif self.appear(self.I_EXP_ARROW_LEFT):
  81. return Scene.WORLD
  82. logger.info("Unknown scene")
  83. return Scene.UNKNOWN
  84. def pre_process(self):
  85. explorationConfig = self._config
  86. if explorationConfig.switch_soul_config.enable:
  87. self.goto_page(page_shikigami_records)
  88. self.run_switch_soul(explorationConfig.switch_soul_config.switch_group_team)
  89. if explorationConfig.switch_soul_config.enable_switch_by_name:
  90. self.goto_page(page_shikigami_records)
  91. self.run_switch_soul_by_name(explorationConfig.switch_soul_config.group_name,
  92. explorationConfig.switch_soul_config.team_name)
  93. # 开启加成
  94. con = self.config.exploration.exploration_config
  95. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  96. self.goto_page(page_main)
  97. self.open_buff()
  98. if con.buff_gold_50_click:
  99. self.gold_50()
  100. if con.buff_gold_100_click:
  101. self.gold_100()
  102. if con.buff_exp_50_click:
  103. self.exp_50()
  104. if con.buff_exp_100_click:
  105. self.exp_100()
  106. self.close_buff()
  107. # 探索页面(大地图始终处于主线/玩法 tab 之一,裸 page_exploration 永远被 prio 60 的 tab 页遮蔽、
  108. # 无法作为当前页抵达,故直接进入主线 tab)
  109. self.goto_page(pages.page_mainline)
  110. def post_process(self):
  111. self.wait_until_stable(self.I_UI_BACK_RED)
  112. if self.appear(self.I_UI_BACK_RED):
  113. self.ui_click_until_disappear(self.I_UI_BACK_RED)
  114. self.goto_page(page_main)
  115. con = self._config.exploration_config
  116. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  117. self.open_buff()
  118. self.gold_50(is_open=False)
  119. self.gold_100(is_open=False)
  120. self.exp_50(is_open=False)
  121. self.exp_100(is_open=False)
  122. self.close_buff()
  123. self.set_next_run(task='Exploration', success=True, finish=False)
  124. raise TaskEnd
  125. # 打开指定的章节:
  126. def open_expect_level(self):
  127. # 选关依赖主线 tab 右侧的章节列表(I_E_EXPLORATION_CLICK + 章节名 OCR)。
  128. # 探索大地图常驻「主线/玩法」之一,若停在玩法 tab 不选回主线,右边会是御魂/今日掉落等模块名,
  129. # I_E_EXPLORATION_CLICK 不出现、OCR 交集为空 → 一直滑屏卡死。故进选关前无条件确保主线 tab。
  130. self.goto_page(pages.page_mainline)
  131. target_level = self.config.exploration.exploration_config.exploration_level
  132. level_numbers = {level.value: index for index, level in enumerate(ExplorationLevel)}
  133. target_number = level_numbers[target_level]
  134. # 新版列表按章节递增排列;OCR 暂时为空时沿用上一次方向。
  135. swipe = self.S_SWIPE_LEVEL_DOWN
  136. swipeCount = 0
  137. while 1:
  138. # 判断有无目标章节
  139. self.screenshot()
  140. # 获取当前章节名
  141. results = self.O_E_EXPLORATION_LEVEL_NUMBER.detect_and_ocr(self.device.image)
  142. text1 = [result.ocr_text for result in results]
  143. # https://github.com/runhey/OnmyojiAutoScript/issues/1540
  144. text1 = [text.replace("名", "第").replace("书", "第") for text in text1]
  145. # 判断当前章节有无目标章节
  146. # 有则跳出检测
  147. if self.appear(self.I_E_EXPLORATION_CLICK) or target_level in text1:
  148. break
  149. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  150. continue
  151. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  152. continue
  153. visible_numbers = [level_numbers[text] for text in text1 if text in level_numbers]
  154. if visible_numbers:
  155. if target_number < min(visible_numbers):
  156. swipe = self.S_SWIPE_LEVEL_UP
  157. elif target_number > max(visible_numbers):
  158. swipe = self.S_SWIPE_LEVEL_DOWN
  159. self.device.click_record_clear()
  160. self.swipe(swipe)
  161. swipeCount += 1
  162. debug_info = f"Swiped {swipeCount} times ({swipe.name}), current exploration level: {text1}"
  163. logger.info(debug_info)
  164. if swipeCount >= 15:
  165. raise GameStuckError(
  166. f"Swiped too many times ({swipeCount}), seems stuck in exploration level selection"
  167. )
  168. time.sleep(1)
  169. # 选中对应章节
  170. while 1:
  171. self.screenshot()
  172. if self.appear_then_click(self.I_UI_CONFIRM, interval=1):
  173. continue
  174. if self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1):
  175. continue
  176. self.O_E_EXPLORATION_LEVEL_NUMBER.keyword = target_level
  177. if self.ocr_appear_click(self.O_E_EXPLORATION_LEVEL_NUMBER):
  178. self.wait_until_appear(self.I_E_EXPLORATION_CLICK, wait_time=3)
  179. if self.appear(self.I_E_EXPLORATION_CLICK):
  180. break
  181. if self.is_in_room():
  182. break
  183. return True
  184. # 候补:
  185. def enter_settings_and_do_operations(self):
  186. # 打开设置
  187. while 1:
  188. self.screenshot()
  189. if self.appear(self.I_E_OPEN_SETTINGS):
  190. logger.info("Open settings")
  191. break
  192. if self.is_in_battle():
  193. logger.warning('Opening settings failed due to now in battle')
  194. return
  195. if (self.appear_then_click(self.I_UI_CONFIRM, interval=1)
  196. or self.appear_then_click(self.I_UI_CONFIRM_SAMLL, interval=1)):
  197. continue
  198. if self.click(self.C_CLICK_SETTINGS, interval=2):
  199. continue
  200. # 候补出战数量识别
  201. self.screenshot()
  202. if not self.appear(self.I_E_OPEN_SETTINGS):
  203. logger.warning('Opening settings failed due to now in battle')
  204. return
  205. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  206. if cu >= 40:
  207. logger.info("Alternate number is enough")
  208. self.ui_click_until_disappear(self.I_E_SURE_BUTTON)
  209. return
  210. else:
  211. self.add_shiki()
  212. # 添加式神
  213. def add_shiki(self, screenshot=True):
  214. if screenshot:
  215. self.screenshot()
  216. if not self.appear(self.I_E_OPEN_SETTINGS):
  217. logger.warning('Opening settings failed due to now in battle')
  218. return
  219. # 先点候补式神区域,再切换稀有度,避免点击失败
  220. self.click(self.C_CLICK_STANDBY_TEAM)
  221. choose_rarity = self._config.exploration_config.choose_rarity
  222. rarity = ShikigamiClass.N if choose_rarity == ChooseRarity.N else ShikigamiClass.MATERIAL
  223. self.switch_shikigami_class(rarity)
  224. # 移动至未候补的狗粮
  225. while 1:
  226. # 慢一点
  227. time.sleep(0.5)
  228. self.screenshot()
  229. if not self.appear(self.I_E_OPEN_SETTINGS):
  230. logger.warning('Opening settings failed due to now in battle')
  231. return
  232. if self.appear(self.I_E_RATATE_EXSIT):
  233. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT)
  234. else:
  235. break
  236. while 1:
  237. # 候补出战数量识别
  238. self.screenshot()
  239. if not self.appear(self.I_E_OPEN_SETTINGS):
  240. logger.warning('Opening settings failed due to now in battle')
  241. return
  242. cu, res, total = self.O_E_ALTERNATE_NUMBER.ocr(self.device.image)
  243. if cu >= 40:
  244. break
  245. self.swipe(self.S_SWIPE_SHIKI_TO_LEFT_ONE)
  246. # 慢一点
  247. time.sleep(0.5)
  248. self.screenshot()
  249. self.click(self.L_ROTATE_1)
  250. self.device.click_record_clear()
  251. self.appear_then_click(self.I_E_SURE_BUTTON)
  252. # 找up按钮
  253. def search_up_fight(self, up_type: UpType = None):
  254. if up_type is None:
  255. up_type = self._config.exploration_config.up_type
  256. # 1. 如果选择了特定的 UP 类型 (比如达摩)
  257. if up_type != UpType.ALL:
  258. match up_type:
  259. case UpType.EXP:
  260. find_flag = self.I_UP_EXP
  261. case UpType.COIN:
  262. find_flag = self.I_UP_COIN
  263. case UpType.DARUMAA:
  264. find_flag = self.I_UP_DARUMA
  265. case _:
  266. find_flag = self.I_UP_EXP
  267. # 尝试寻找 UP 图标
  268. if self.appear(find_flag):
  269. # 获取 UP 图标的坐标和中心点
  270. x, y, w, h = find_flag.roi_front
  271. x_center, y_center = find_flag.front_center()
  272. logger.info(f'Found up type: {up_type} at {find_flag.roi_front}')
  273. # 缩小搜索范围 (ROI)
  274. # 原来左右各扩 160-200,太宽了容易甚至把隔壁怪算进来
  275. # 现在改为左右各扩 50-80,强制只找垂直线附近的战斗图标
  276. roi_back_y = max(0, y - 300) # 向上找300像素
  277. roi_back_h = y - 20 - roi_back_y #直到UP图标上方20像素截止
  278. # 左右范围缩窄:防止误触旁边的怪
  279. roi_back_x = max(0, x - 60)
  280. roi_back_w = min(1280, x + w + 60) - roi_back_x
  281. logger.info(f'Searching sword icon in narrowed area: {roi_back_x, roi_back_y, roi_back_w, roi_back_h}')
  282. matches = self.I_NORMAL_BATTLE_BUTTON.match_all(
  283. image=self.device.image,
  284. threshold=0.9,
  285. roi=[roi_back_x, roi_back_y, roi_back_w, roi_back_h]
  286. )
  287. if matches:
  288. distances = []
  289. for match in matches:
  290. # 这里假设 match[1], match[2] 是 x, y
  291. x_match = match[1] + match[3] / 2 # 战斗图标中心 X
  292. y_match = match[2] + match[4] / 2 # 战斗图标中心 Y
  293. # 这样能完美避开“距离很近但属于隔壁怪”的情况
  294. x_diff = abs(x_center - x_match)
  295. y_diff = abs(y_center - y_match)
  296. weighted_distance = (x_diff * 3) + y_diff
  297. distances.append((weighted_distance, match))
  298. # 按加权距离排序,取最正对着的一个
  299. distances.sort(key=lambda x: x[0], reverse=False)
  300. match = distances[0][1]
  301. roi_front = list(match[1:]) # x,y,w,h
  302. self.I_NORMAL_BATTLE_BUTTON.roi_front = roi_front
  303. logger.info(f"Target locked: sword at {roi_front} (aligned with UP icon)")
  304. return self.I_NORMAL_BATTLE_BUTTON
  305. # 没找到 UP 图标或对应战斗图标,返回 None 让外层逻辑去处理(滑动或退出)
  306. return None
  307. # 2. 如果是默认情况 (UpType.ALL),则只要有怪就打
  308. self.I_NORMAL_BATTLE_BUTTON.roi_back = (0, 0, 1279, 719)
  309. if self.appear(self.I_NORMAL_BATTLE_BUTTON):
  310. return self.I_NORMAL_BATTLE_BUTTON
  311. return None
  312. def activate_realm_raid(self, con_scrolls, con) -> None:
  313. # 判断是否开启突破票检测
  314. if not con_scrolls.scrolls_enable:
  315. return
  316. if self.appear(self.I_E_EXPLORATION_CLICK) and self.appear(self.I_EXP_CREATE_TEAM):
  317. cu, res, total = self.O_REALM_RAID_NUMBER1.ocr(self.device.image)
  318. else:
  319. cu, res, total = self.O_REALM_RAID_NUMBER.ocr(self.device.image)
  320. # 判断突破票数量
  321. if cu < con_scrolls.scrolls_threshold:
  322. return
  323. # 关闭加成
  324. if self.appear(self.I_RED_CLOSE):
  325. self.ui_click_until_disappear(self.I_RED_CLOSE)
  326. if self.appear(self.I_UI_CANCEL):
  327. self.ui_click_until_disappear(self.I_UI_CANCEL)
  328. if self.appear(self.I_UI_CANCEL_SAMLL):
  329. self.ui_click_until_disappear(self.I_UI_CANCEL_SAMLL)
  330. self.goto_page(page_main)
  331. if con.buff_gold_50_click or con.buff_gold_100_click or con.buff_exp_50_click or con.buff_exp_100_click:
  332. self.open_buff()
  333. self.gold_50(is_open=False)
  334. self.gold_100(is_open=False)
  335. self.exp_50(is_open=False)
  336. self.exp_100(is_open=False)
  337. self.close_buff()
  338. # 设置下次执行行时间
  339. logger.info("RealmRaid and Exploration set_next_run !")
  340. next_run = datetime.now() + con_scrolls.scrolls_cd
  341. self.set_next_run(task='Exploration', success=False, finish=False, target=next_run)
  342. self.set_next_run(task='RealmRaid', success=False, finish=False, server=False, target=datetime.now())
  343. self.set_next_run(task='MemoryScrolls', success=False, finish=False, target=datetime.now())
  344. raise TaskEnd
  345. #
  346. def check_exit(self) -> bool:
  347. # True 表示要退出这个任务
  348. if self.minions_cnt >= self._config.exploration_config.minions_cnt:
  349. logger.info('Minions count is enough, exit')
  350. return True
  351. if datetime.now() - self.start_time >= self.limit_time:
  352. logger.info('Exploration time limit out')
  353. return True
  354. self.activate_realm_raid(self._config.scrolls, self._config.exploration_config)
  355. return False
  356. def quit_explore(self):
  357. logger.info('Quit explore')
  358. self.device.click_record_remove(self.S_SWIPE_BACKGROUND_RIGHT.name)
  359. boss_timer = Timer(15)
  360. boss_timer.start()
  361. # click_yellow_button = 0 #用于保证只点一次左上返回按钮,不要直接触发连点回到主界面
  362. while 1:
  363. self.screenshot()
  364. # 探索章节标题界面
  365. if self.appear(self.I_UI_BACK_YELLOW) and self.appear(self.I_E_EXPLORATION_CLICK):
  366. break
  367. # 探索大世界界面
  368. if self.appear(self.I_CHECK_EXPLORATION) and not self._in_chapter():
  369. break
  370. # 防止BOSS打完箱子刚落地,脚本就手快点退出了
  371. if self.appear_then_click(self.I_BATTLE_REWARD, interval=1.5):
  372. logger.info("Found battle reward during exit, picking it up.")
  373. boss_timer.reset()
  374. continue
  375. if boss_timer.reached():
  376. logger.warning('Exit timeout, force clicking back button')
  377. boss_timer.reset()
  378. self.click(self.I_UI_BACK_BLUE)
  379. continue
  380. if self.appear_then_click(self.I_E_EXIT_CONFIRM, interval=0.8):
  381. continue
  382. if self.appear(self.I_BACK_YOLLOW):
  383. self.screenshot()
  384. # 复核截图期间可能出现奖励或退出确认,先交回主循环处理弹窗。
  385. if self.appear(self.I_BATTLE_REWARD) or self.appear(self.I_E_EXIT_CONFIRM):
  386. continue
  387. # https://github.com/runhey/OnmyojiAutoScript/issues/1935
  388. if not self.appear(self.I_BACK_YOLLOW) or self.appear(self.I_E_EXPLORATION_CLICK):
  389. continue
  390. if self._in_chapter() and self.click(self.I_BACK_YOLLOW, interval=3.5):
  391. continue
  392. if self.appear(self.I_EXPLORATION_TITLE) or self.appear(self.I_CHECK_EXPLORATION):
  393. continue
  394. def _hook_special_reward(self) -> bool:
  395. if self.appear(self.I_STATISTICS) and not self.appear(self.I_REWARD) and not self.appear(self.I_WIN):
  396. if self.appear_then_click(self.I_CONFIRM_CLOSE_DIFF_SOUL):
  397. return True
  398. self.click(self.C_RANDOM_CLICK, interval=1.5)
  399. return False
  400. def fire(self, button) -> bool:
  401. self.ui_click_until_disappear(button, interval=3)
  402. self.screenshot()
  403. if self._in_chapter():
  404. # 如果还在探索说明,这个是显示滑动导致挑战按钮不在范围内
  405. logger.warning('Fire button disappear, but still in exploration')
  406. return False
  407. self.run_general_battle(self._config.general_battle_config)
  408. self.minions_cnt += 1
  409. return True
  410. def wait_world_stable(self) -> bool:
  411. """用新截图确认章节列表已展开;切场或展开后交回主循环重新判断。"""
  412. if self.get_current_scene(reuse_screenshot=False) != Scene.WORLD:
  413. return False
  414. if self.appear(self.I_EXP_ARROW_RIGHT):
  415. return True
  416. self.appear_then_click(self.I_EXP_ARROW_LEFT, interval=2)
  417. return False
  418. if __name__ == "__main__":
  419. from module.config.config import Config
  420. from module.device.device import Device
  421. config = Config('oas1')
  422. device = Device(config)
  423. t = BaseExploration(config, device)
  424. t.screenshot()
  425. # IMAGE_FILE = r"C:\Users\萌萌哒\Desktop\QQ20240818-163854.png"
  426. # image = load_image(IMAGE_FILE)
  427. # t.device.image = image
  428. while 1:
  429. # print(t.search_up_fight(UpType.EXP))
  430. t.screenshot()
  431. print(t.get_current_scene())
  432. from PIL import Image
  433. # Image.fromarray(t.device.image.astype(np.uint8)).show()