map.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import re
  2. from module.atom.click import RuleClick
  3. from module.base.timer import Timer
  4. from module.exception import GameStuckError
  5. from module.logger import logger
  6. from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
  7. from tasks.SixRealms.assets import SixRealmsAssets
  8. from tasks.SixRealms.common import MoonSeaType
  9. from tasks.SixRealms.oas_ocr import StoneOcr
  10. class MoonSeaMap(MoonSeaSkills):
  11. island_markers = {
  12. MoonSeaType.island101: SixRealmsAssets.I_ISLAND_MARKER_101,
  13. MoonSeaType.island102: SixRealmsAssets.I_ISLAND_MARKER_102,
  14. MoonSeaType.island103: SixRealmsAssets.I_ISLAND_MARKER_103,
  15. MoonSeaType.island104: SixRealmsAssets.I_ISLAND_MARKER_104,
  16. MoonSeaType.island105: SixRealmsAssets.I_ISLAND_MARKER_105,
  17. }
  18. map_ocr = StoneOcr(
  19. roi=(0, 0, 1280, 720),
  20. area=(0, 0, 1280, 720),
  21. mode="Full",
  22. method="Default",
  23. keyword="",
  24. name="map_ocr")
  25. @staticmethod
  26. def contains_any_char(string, chars):
  27. return not set(string).isdisjoint(set(chars))
  28. def _marker_boxes(self, kind):
  29. marker = self.island_markers[kind]
  30. return [tuple(match[1:]) for match in marker.match_all_any(self.device.image)]
  31. @staticmethod
  32. def _nearby_marker(boxes, roi):
  33. # 岛屿上下浮动。同类岛并排时,只跟踪原来那一列。
  34. center_x = roi[0] + roi[2] / 2
  35. nearest = min(boxes, key=lambda box: abs(box[0] + box[2] / 2 - center_x), default=None)
  36. if nearest and abs(nearest[0] + nearest[2] / 2 - center_x) <= 60:
  37. return nearest
  38. def _island_candidates(self, skip_store=True) -> dict:
  39. candidates = {}
  40. for island in self.island_markers:
  41. if skip_store and island == MoonSeaType.island101 and self.appear(self.I_M_STORE):
  42. continue
  43. boxes = self._marker_boxes(island)
  44. if boxes:
  45. candidates[island] = boxes
  46. return candidates
  47. def decide(self, prefer_store=False) -> tuple:
  48. # 转场还没结束就截图时,OCR 可能给出不在任何一列岛上的坐标。重读几次(每次约 1.2s,
  49. # 转场一般 2~4s 内结束),重新拿到对得上标牌的坐标就能自愈。
  50. for _ in range(3):
  51. self.screenshot()
  52. if self.appear(self.I_BOSS_FIRE):
  53. # 最后的boss
  54. return MoonSeaType.island106, 0, (0, 0, 0, 0)
  55. kind, rounds, roi = self._read_map_ocr()
  56. candidates = self._island_candidates()
  57. if not candidates:
  58. # 只剩宁息岛时也得进去,否则回合推进不了。
  59. candidates = self._island_candidates(skip_store=False)
  60. if prefer_store and kind != MoonSeaType.island101 and MoonSeaType.island101 in candidates:
  61. kind = MoonSeaType.island101
  62. roi = max(candidates[kind])
  63. elif kind != MoonSeaType.island100:
  64. roi = self._nearby_marker(candidates.get(kind, []), roi) or roi
  65. elif candidates:
  66. # OCR 未识别出岛型时,沿用从右向左的选岛顺序。
  67. kind, roi = max(((island, max(boxes)) for island, boxes in candidates.items()),
  68. key=lambda candidate: candidate[1][0])
  69. if not candidates.get(kind) or self._nearby_marker(candidates[kind], roi):
  70. break
  71. logger.warning('Island {} at {} matches no marker, re-read the map'.format(kind.name, roi))
  72. logger.info('Island type: {}, Residue: {}, ROI: {}'.format(kind, rounds, roi))
  73. return kind, rounds, roi
  74. def _read_map_ocr(self):
  75. isl_type = MoonSeaType.island100
  76. isl_roi = (0, 0, 0, 0)
  77. isl_num = 0
  78. results = self.map_ocr.detect_and_ocr(image=self.device.image)
  79. rx1, ry1, rw, rh = self.O_OCR_MAP.roi
  80. rx2, ry2 = rx1 + rw, ry1 + rh
  81. for result in results:
  82. # box 是四个点坐标 左上, 右上, 右下, 左下
  83. x1, y1, x2, y2 = result.box[0][0], result.box[0][1], result.box[2][0], result.box[2][1]
  84. w, h = x2 - x1, y2 - y1
  85. text = result.ocr_text
  86. if self.contains_any_char(result.ocr_text, chars='回合国') and \
  87. (x1 > 1000 and y1 + h < 300):
  88. # if text[1].isdigit():
  89. # isl_num = int(text[:2])
  90. # elif text[0].isdigit():
  91. # isl_num = int(text[0])
  92. match = re.search(r'\d{1,2}', text)
  93. if match:
  94. isl_num = int(match.group())
  95. if x1 < rx1 or x2 > rx2 or y1 < ry1 or y2 > ry2:
  96. continue
  97. if isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='宁息'):
  98. # 如果身上没有达到300块钱就不去了
  99. if self.appear(self.I_M_STORE):
  100. continue
  101. isl_type = MoonSeaType.island101
  102. isl_roi = x1, y1, w, h
  103. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='神秘'):
  104. isl_type = MoonSeaType.island102
  105. isl_roi = x1, y1, w, h
  106. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='回混范'):
  107. isl_type = MoonSeaType.island103
  108. isl_roi = x1, y1, w, h
  109. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='蜜馨屡战'):
  110. isl_type = MoonSeaType.island104
  111. isl_roi = x1, y1, w, h
  112. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='星之'):
  113. isl_type = MoonSeaType.island105
  114. isl_roi = x1, y1, w, h
  115. return isl_type, isl_num, isl_roi
  116. def enter_island(self, isl_type, isl_roi):
  117. if isl_type == MoonSeaType.island100:
  118. logger.warning('The island type was not recognized')
  119. logger.warning('Pick one at random, starting from the right')
  120. timeout = Timer(15).start()
  121. click_timer = Timer(1)
  122. entrances = (
  123. self.C_ISLAND_ENTER_1, self.C_ISLAND_ENTER_2, self.C_ISLAND_ENTER_3,
  124. self.C_ISLAND_ENTER_4, self.C_ISLAND_ENTER_5, self.C_ISLAND_ENTER_6,
  125. )
  126. entrance_index = 0
  127. while 1:
  128. self.screenshot()
  129. if not self.in_main() and self.appear(self.I_BACK_EXIT):
  130. break
  131. if timeout.reached():
  132. raise GameStuckError('Enter unknown island timeout')
  133. if not click_timer.reached():
  134. continue
  135. if self.appear_then_click(self.I_UI_CANCEL, interval=1):
  136. click_timer.reset()
  137. continue
  138. self.click(entrances[entrance_index])
  139. entrance_index = (entrance_index + 1) % len(entrances)
  140. click_timer.reset()
  141. logger.info('Entering island randomly')
  142. return
  143. timeout = Timer(15).start()
  144. click_timer = Timer(2.5)
  145. while 1:
  146. self.screenshot()
  147. on_map = self.in_main()
  148. if not on_map and self.appear(self.I_BACK_EXIT):
  149. break
  150. if timeout.reached():
  151. raise GameStuckError(f'Enter {isl_type.name} timeout')
  152. if not on_map or not click_timer.reached():
  153. continue
  154. click_timer.reset()
  155. roi = self._nearby_marker(self._marker_boxes(isl_type), isl_roi)
  156. if not roi:
  157. kind, _, box = self._read_map_ocr()
  158. if kind != isl_type or not self._nearby_marker([box], isl_roi):
  159. continue
  160. roi = box
  161. x, y, w, h = roi
  162. # 以当帧标牌中心为基准,落点在下方 15~75 像素。
  163. cx, cy = int(x + w / 2), int(y + h / 2) + 45
  164. click_area = (cx - 30, cy - 30, 60, 60)
  165. self.click(RuleClick(click_area, click_area, name='island_enter'))
  166. logger.info('Entering island')
  167. return
  168. def activate_store(self) -> bool:
  169. """
  170. 最后打boss前面激活一次商店买东西
  171. @return: 有钱够就是True
  172. """
  173. logger.info('Activating store')
  174. self.screenshot()
  175. if self.appear(self.I_M_STORE) and not self.appear(self.I_M_STORE_ACTIVITY):
  176. logger.warning('Now you have not money to buy items')
  177. logger.warning('Store is not active')
  178. return False
  179. cnt_act = 0
  180. while 1:
  181. self.screenshot()
  182. if self.appear(self.I_UI_CONFIRM):
  183. self.ui_click_until_disappear(self.I_UI_CONFIRM, interval=2)
  184. break
  185. if cnt_act >= 3:
  186. logger.warning('Store is not active')
  187. return False
  188. if self.appear_then_click(self.I_M_STORE_ACTIVITY, interval=1.5):
  189. cnt_act += 1
  190. continue
  191. if __name__ == '__main__':
  192. from module.config.config import Config
  193. from module.device.device import Device
  194. from module.base.utils import load_image
  195. c = Config('oas1')
  196. d = Device(c)
  197. t = MoonSeaMap(c, d)
  198. t.device.image = load_image(r'C:\Users\Ryland\Desktop\Desktop\34.png')
  199. match = re.search(r'\d{1,2}', '<17回合后迎战月读')
  200. if match:
  201. isl_num = int(match.group())
  202. print(isl_num)