script_task.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import time
  5. import cv2
  6. import numpy as np
  7. from datetime import datetime, timedelta
  8. from numpy import uint8, fromfile
  9. from random import choice
  10. from cached_property import cached_property
  11. # Use cmd to install: ./toolkit/python.exe -m pip install -i https://pypi.org/simple/ oashya --trusted-host pypi.org
  12. # update oashya: ./toolkit/python.exe -m pip install --upgrade oashya
  13. from oashya.tracker import Tracker
  14. from oashya.labels import label2id, CLASSINDEX as CI, id2name
  15. from oashya.utils import draw_tracks
  16. from module.exception import TaskEnd
  17. from module.logger import logger
  18. from module.exception import RequestHumanTakeover
  19. from tasks.Component.SwitchOnmyoji.switch_onmyoji import SwitchOnmyoji
  20. from tasks.GameUi.game_ui import GameUi
  21. from tasks.GameUi.page import page_hyakkiyakou, page_main, page_onmyodo
  22. from tasks.Hyakkiyakou.config import InferenceEngine, ModelPrecision
  23. from tasks.Hyakkiyakou.agent.agent import Agent
  24. from tasks.Hyakkiyakou.slave.hya_slave import HyaSlave
  25. from tasks.Hyakkiyakou.debugger import Debugger
  26. def plot_save(image, boxes):
  27. color_palette = np.random.uniform(0, 255, size=(226, 3))
  28. for box in boxes:
  29. _cls = box[0]
  30. _scores = box[1]
  31. _x, _y, _w, _h = box[2]
  32. x1 = int(_x - _w / 2)
  33. y1 = int(_y - _h / 2)
  34. x2 = int(_x + _w / 2)
  35. y2 = int(_y + _h / 2)
  36. cv2.rectangle(image, (x1, y1), (x2, y2), color_palette[_cls], 2)
  37. #
  38. cv2.putText(image, f'{_cls} {_scores:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color_palette[_cls],
  39. 2)
  40. save_file = './tasks/Hyakkiyakou/temp/image.png'
  41. cv2.imwrite(save_file, image)
  42. class ScriptTask(GameUi, HyaSlave, SwitchOnmyoji):
  43. @property
  44. def _config(self):
  45. return self.config.hyakkiyakou
  46. @cached_property
  47. def tracker(self) -> Tracker:
  48. hyakkiyakou_models = self._config.hyakkiyakou_models
  49. conf = hyakkiyakou_models.conf_threshold
  50. nms = hyakkiyakou_models.iou_threshold
  51. if conf < 0.2 or conf > 1:
  52. raise RequestHumanTakeover('conf_threshold should be in [0.2, 1]')
  53. if nms < 0.2 or nms > 1:
  54. raise RequestHumanTakeover('iou_threshold should be in [0.2, 1]')
  55. inf_en = 'onnxruntime' if hyakkiyakou_models.inference_engine == InferenceEngine.ONNXRUNTIME else 'tensorrt'
  56. precision = 'fp32' if hyakkiyakou_models.model_precision == ModelPrecision.FP32 else 'int8'
  57. # 这个坑后面再补
  58. if inf_en == 'tensorrt' or precision == 'int8':
  59. raise RequestHumanTakeover('Only support onnxruntime and FP32 now')
  60. debug_info: bool = self._config.debug_config.hya_info
  61. args = {
  62. 'conf_threshold': conf,
  63. 'iou_threshold': nms,
  64. 'precision': precision,
  65. 'inference_engine': inf_en,
  66. 'debug': debug_info,
  67. }
  68. return Tracker(args=args)
  69. @cached_property
  70. def agent(self) -> Agent:
  71. hya_config = self._config.hyakkiyakou_config
  72. sp = hya_config.hya_sp
  73. ssr = hya_config.hya_ssr
  74. sr = hya_config.hya_sr
  75. r = hya_config.hya_r
  76. n = hya_config.hya_n
  77. g = hya_config.hya_g
  78. weights: list = [sp, ssr, sr, r, n, g]
  79. str_priorities: str = hya_config.hya_priorities
  80. if str_priorities == '':
  81. priorities = []
  82. else:
  83. str_priorities = str_priorities.replace(' ', '').replace(',', ',')
  84. try:
  85. priorities = [label2id(s) for s in str_priorities.split(',')]
  86. except Exception as e:
  87. logger.error(f'Priority error: {str_priorities}')
  88. logger.error(e)
  89. raise RequestHumanTakeover
  90. strategy: dict = {
  91. 'weights': weights,
  92. 'priorities': priorities,
  93. 'invite_friend': hya_config.hya_invite_friend,
  94. 'auto_bean': hya_config.hya_auto_bean
  95. }
  96. return Agent(strategy=strategy)
  97. @cached_property
  98. def debugger(self) -> Debugger:
  99. debug_config = self._config.debug_config
  100. hya_interval = debug_config.hya_interval
  101. hya_save_result = debug_config.hya_save_result
  102. if hya_interval <= 100 or hya_interval >= 1000:
  103. raise RequestHumanTakeover('screenshot_interval must be between 1000 and 10000')
  104. self.set_fast_screenshot_interval(hya_interval)
  105. return Debugger(info_enable=debug_config.hya_info,
  106. continuous_learning=debug_config.continuous_learning,
  107. hya_save_result=hya_save_result)
  108. def run(self):
  109. hya_count: int = 0
  110. self.limit_count: int = self._config.hyakkiyakou_config.hya_limit_count
  111. limit_time = self._config.hyakkiyakou_config.hya_limit_time
  112. self.limit_time: timedelta = timedelta(hours=limit_time.hour, minutes=limit_time.minute,
  113. seconds=limit_time.second)
  114. self.goto_page(page_onmyodo)
  115. self.switch_onmyoji(self._config.hyakkiyakou_config.hya_onmyoji)
  116. self.goto_page(page_hyakkiyakou)
  117. while 1:
  118. if hya_count >= self.limit_count:
  119. logger.info('Hyakkiyakou count limit out')
  120. break
  121. if datetime.now() - self.start_time >= self.limit_time:
  122. logger.info('Hyakkiyakou time limit out')
  123. break
  124. self.one()
  125. hya_count += 1
  126. logger.info(f'count: {hya_count}/{self.limit_count}')
  127. logger.info(f'time: {(datetime.now() - self.start_time).total_seconds():.1f}s/{self.limit_time.total_seconds()}s')
  128. while 1:
  129. self.screenshot()
  130. if not self.appear(self.I_HACCESS):
  131. continue
  132. if self.appear(self.I_HCLOSE_RED):
  133. break
  134. self.ui_click_until_disappear(self.I_HCLOSE_RED)
  135. self.set_next_run(task='Hyakkiyakou', success=True, finish=False)
  136. raise TaskEnd
  137. # ------------ 新增三个函数:用于检测 ------------
  138. def _rarity_score(self, class_id: int) -> int:
  139. """
  140. 返回一个用于比较的稀有度分数:
  141. SP > SSR > SR > R > N > G
  142. 没有匹配到的返回 -1
  143. """
  144. # G(呱太)
  145. if CI.MIN_G <= class_id <= CI.MAX_G:
  146. return 0
  147. # N
  148. if CI.MIN_N <= class_id <= CI.MAX_N:
  149. return 1
  150. # R(排除童男/童女)
  151. if (CI.MIN_R <= class_id <= CI.MAX_R) and class_id not in (CI.R_007, CI.R_008):
  152. return 2
  153. # SR
  154. if CI.MIN_SR <= class_id <= CI.MAX_SR:
  155. return 3
  156. # SSR
  157. if CI.MIN_SSR <= class_id <= CI.MAX_SSR:
  158. return 4
  159. # SP
  160. if CI.MIN_SP <= class_id <= CI.MAX_SP:
  161. return 5
  162. # 其他(buff/未收录 等)不参与排序
  163. return -1
  164. def _detect_current_rarity(self) -> tuple[int, int]:
  165. """
  166. 在当前截图上跑一遍 tracker,返回:
  167. (最高稀有度分数, 对应的 class_id)
  168. 如果没检测到式神,则返回 (-1, -1)
  169. """
  170. # 截一张当前图
  171. self.screenshot()
  172. # 这里 response 随便给一个默认值即可,主线战斗里是 last_action
  173. tracks = self.tracker(image=self.device.image, response=[0, 0, False, 10])
  174. best_score = -1
  175. best_class = -1
  176. for _id, _class, _conf, _cx, _cy, _w, _h, _v in tracks:
  177. score = self._rarity_score(_class)
  178. if score > best_score:
  179. best_score = score
  180. best_class = _class
  181. if best_class != -1:
  182. logger.info(
  183. f'Hyakki select: detect {id2name(_class)} '
  184. f'with rarity score {score}'
  185. )
  186. else:
  187. logger.warning('Hyakki select: no valid shikigami detected on title screen')
  188. return best_score, best_class
  189. def _select_best_boss(self):
  190. """
  191. 在鬼王选择界面依次点三个候选,识别稀有度,最终选择最高稀有度的那个。
  192. 要求:当前已经在 I_HTITLE 画面。
  193. """
  194. candidates = [self.C_HSELECT_1, self.C_HSELECT_2, self.C_HSELECT_3]
  195. logger.info('Start selecting boss')
  196. best_idx = 0
  197. best_score = -1
  198. scores = []
  199. for idx, btn in enumerate(candidates):
  200. # 点一下第 idx 个候选,让它成为当前选中的式神
  201. self.click(btn, interval=0.1)
  202. time.sleep(1) # 给界面一点刷新时间
  203. score, cls = self._detect_current_rarity()
  204. scores.append(score)
  205. if score > best_score:
  206. best_score = score
  207. best_idx = idx
  208. #所以如果三个都识别失败了会选第一个
  209. logger.info(f'Hyakki select scores: {scores}, choose index {best_idx + 1}')
  210. # 记录一下,后面如果需要兜底再点一次可以用
  211. self._best_boss_button = candidates[best_idx]
  212. # 再点一次最高稀有度那个,确保它处于选中状态
  213. self.click(self._best_boss_button, interval=0.1)
  214. time.sleep(0.5)
  215. def one(self):
  216. self.reset_state()
  217. if not self.appear(self.I_HACCESS):
  218. logger.warning('Page Error')
  219. if self._config.hyakkiyakou_config.hya_invite_friend:
  220. self.invite_friend()
  221. # start
  222. self.ui_click(self.I_HACCESS, self.I_HSTART, interval=2)
  223. self.wait_until_appear(self.I_HTITLE)
  224. # 这里改成:在三个候选中选择稀有度最高的作为鬼王
  225. self._best_boss_button = None
  226. self._select_best_boss()
  227. while 1:
  228. self.screenshot()
  229. if not self.appear(self.I_HTITLE):
  230. break
  231. if self.appear_then_click(self.I_HSTART, interval=2):
  232. continue
  233. if not self.appear(self.I_HSELECTED) and getattr(self, '_best_boss_button', None) is not None:
  234. self.click(self._best_boss_button, interval=2) # 保险:如果因为某些原因还没处于“已选中”状态,就再点一次最佳按钮
  235. continue
  236. self.device.stuck_record_add('BATTLE_STATUS_S')
  237. # 正式开始
  238. logger.hr('Start Hyakkiyakou')
  239. init_bean_flag: bool = False
  240. last_action = [0, 0, False, 10]
  241. self.hya_fs_check_timer.reset()
  242. if self._config.debug_config.hya_show:
  243. self.debugger.show_start()
  244. while 1:
  245. self.fast_screenshot(screenshot=self._config.debug_config.hya_screenshot_method)
  246. if self.appear(self.I_HEND):
  247. break
  248. if not self.appear(self.I_CHECK_RUN):
  249. continue
  250. if not init_bean_flag:
  251. init_bean_flag = True
  252. self.bean_05to10()
  253. time.sleep(0.5)
  254. #修改:在这里不再区分freeze,而是将状态传到decision用于执行冻结策略
  255. #目前被禁用了 因为冰冻状态下检测正确率约等于0 全是蝉冰雪女 =.=
  256. if not self.appear(self.I_HFREEZE):
  257. # -------------------------------------------------------
  258. freeze = self.appear(self.I_HFREEZE)
  259. self.slave_state = self.update_state()
  260. tracks = self.tracker(image=self.device.image, response=last_action)
  261. last_action = self.agent.decision(tracks=tracks, state=self.slave_state, freeze=freeze)
  262. self.do_action(last_action, state=self.slave_state)
  263. else:
  264. # TODO freeze state
  265. tracks = []
  266. # debug
  267. if self._config.debug_config.hya_show:
  268. draw_image = draw_tracks(self.device.image, tracks=tracks)
  269. self.debugger.show_sync(image=draw_image)
  270. if self._config.debug_config.continuous_learning:
  271. self.debugger.deal_learning(image=self.device.image, tracks=tracks)
  272. if self._config.debug_config.hya_info:
  273. self.debugger.show_info(tracker=self.tracker, f=self.agent.focus)
  274. logger.info('Hyakkiyakou End')
  275. if self._config.debug_config.hya_show:
  276. self.debugger.show_stop()
  277. if self._config.debug_config.hya_save_result:
  278. # 走个动画 - 修改了时间以保证能截到图
  279. time.sleep(2)
  280. self.debugger.save_result(self.device.image)
  281. self.ui_click(self.I_HEND, self.I_HACCESS)
  282. self.debugger.save_images()
  283. del self.debugger
  284. # you maybe update oashya
  285. self.tracker.clear_tracks()
  286. def do_action(self, action: list, state):
  287. x, y, throw, bean = action
  288. if not throw:
  289. return
  290. if state[0] <= 0:
  291. return
  292. self.fast_click(x=x, y=y, control_method=self._config.debug_config.hya_control_method)
  293. if __name__ == '__main__':
  294. from module.config.config import Config
  295. from module.device.device import Device
  296. c = Config('oas1')
  297. d = Device(c)
  298. t = ScriptTask(c, d)
  299. t.run()
  300. # from tasks.Hyakkiyakou.agent.tagent import test_observe, test_state
  301. # test_state()
  302. # from debugger import test_track
  303. # test_track(show=False)
  304. pass