map.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 decide(self, prefer_store=False) -> tuple:
  39. self.screenshot()
  40. if self.appear(self.I_BOSS_FIRE):
  41. # 最后的boss
  42. return MoonSeaType.island106, 0, (0, 0, 0, 0)
  43. kind, rounds, roi = self._read_map_ocr()
  44. candidates = {}
  45. for island in self.island_markers:
  46. if island == MoonSeaType.island101 and self.appear(self.I_M_STORE):
  47. continue
  48. boxes = self._marker_boxes(island)
  49. if boxes:
  50. candidates[island] = boxes
  51. if prefer_store and kind != MoonSeaType.island101 and MoonSeaType.island101 in candidates:
  52. kind = MoonSeaType.island101
  53. roi = max(candidates[kind])
  54. elif kind != MoonSeaType.island100:
  55. roi = self._nearby_marker(candidates.get(kind, []), roi) or roi
  56. elif candidates:
  57. # OCR 未识别出岛型时,沿用从右向左的选岛顺序。
  58. kind, roi = max(((island, max(boxes)) for island, boxes in candidates.items()),
  59. key=lambda candidate: candidate[1][0])
  60. logger.info('Island type: {}, Residue: {}, ROI: {}'.format(kind, rounds, roi))
  61. return kind, rounds, roi
  62. def _read_map_ocr(self):
  63. isl_type = MoonSeaType.island100
  64. isl_roi = (0, 0, 0, 0)
  65. isl_num = 0
  66. results = self.map_ocr.detect_and_ocr(image=self.device.image)
  67. rx1, ry1, rw, rh = self.O_OCR_MAP.roi
  68. rx2, ry2 = rx1 + rw, ry1 + rh
  69. for result in results:
  70. # box 是四个点坐标 左上, 右上, 右下, 左下
  71. x1, y1, x2, y2 = result.box[0][0], result.box[0][1], result.box[2][0], result.box[2][1]
  72. w, h = x2 - x1, y2 - y1
  73. text = result.ocr_text
  74. if self.contains_any_char(result.ocr_text, chars='回合国') and \
  75. (x1 > 1000 and y1 + h < 300):
  76. # if text[1].isdigit():
  77. # isl_num = int(text[:2])
  78. # elif text[0].isdigit():
  79. # isl_num = int(text[0])
  80. match = re.search(r'\d{1,2}', text)
  81. if match:
  82. isl_num = int(match.group())
  83. if x1 < rx1 or x2 > rx2 or y1 < ry1 or y2 > ry2:
  84. continue
  85. if isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='宁息'):
  86. # 如果身上没有达到300块钱就不去了
  87. if self.appear(self.I_M_STORE):
  88. continue
  89. isl_type = MoonSeaType.island101
  90. isl_roi = x1, y1, w, h
  91. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='神秘'):
  92. isl_type = MoonSeaType.island102
  93. isl_roi = x1, y1, w, h
  94. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='回混范'):
  95. isl_type = MoonSeaType.island103
  96. isl_roi = x1, y1, w, h
  97. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='蜜馨屡战'):
  98. isl_type = MoonSeaType.island104
  99. isl_roi = x1, y1, w, h
  100. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='星之'):
  101. isl_type = MoonSeaType.island105
  102. isl_roi = x1, y1, w, h
  103. return isl_type, isl_num, isl_roi
  104. def enter_island(self, isl_type, isl_roi):
  105. if isl_type == MoonSeaType.island100:
  106. logger.warning('The island type was not recognized')
  107. logger.warning('Pick one at random, starting from the right')
  108. timeout = Timer(15).start()
  109. click_timer = Timer(1)
  110. entrances = (
  111. self.C_ISLAND_ENTER_1, self.C_ISLAND_ENTER_2, self.C_ISLAND_ENTER_3,
  112. self.C_ISLAND_ENTER_4, self.C_ISLAND_ENTER_5, self.C_ISLAND_ENTER_6,
  113. )
  114. entrance_index = 0
  115. while 1:
  116. self.screenshot()
  117. if not self.in_main() and self.appear(self.I_BACK_EXIT):
  118. break
  119. if timeout.reached():
  120. raise GameStuckError('Enter unknown island timeout')
  121. if not click_timer.reached():
  122. continue
  123. if self.appear_then_click(self.I_UI_CANCEL, interval=1):
  124. click_timer.reset()
  125. continue
  126. self.click(entrances[entrance_index])
  127. entrance_index = (entrance_index + 1) % len(entrances)
  128. click_timer.reset()
  129. logger.info('Entering island randomly')
  130. return
  131. timeout = Timer(15).start()
  132. click_timer = Timer(2.5)
  133. while 1:
  134. self.screenshot()
  135. on_map = self.in_main()
  136. if not on_map and self.appear(self.I_BACK_EXIT):
  137. break
  138. if timeout.reached():
  139. raise GameStuckError(f'Enter {isl_type.name} timeout')
  140. if not on_map or not click_timer.reached():
  141. continue
  142. click_timer.reset()
  143. roi = self._nearby_marker(self._marker_boxes(isl_type), isl_roi)
  144. if not roi:
  145. kind, _, box = self._read_map_ocr()
  146. if kind != isl_type or not self._nearby_marker([box], isl_roi):
  147. continue
  148. roi = box
  149. x, y, w, h = roi
  150. # 以当帧标牌中心为基准,落点在下方 15~75 像素。
  151. cx, cy = int(x + w / 2), int(y + h / 2) + 45
  152. click_area = (cx - 30, cy - 30, 60, 60)
  153. self.click(RuleClick(click_area, click_area, name='island_enter'))
  154. logger.info('Entering island')
  155. return
  156. def activate_store(self) -> bool:
  157. """
  158. 最后打boss前面激活一次商店买东西
  159. @return: 有钱够就是True
  160. """
  161. logger.info('Activating store')
  162. self.screenshot()
  163. if self.appear(self.I_M_STORE) and not self.appear(self.I_M_STORE_ACTIVITY):
  164. logger.warning('Now you have not money to buy items')
  165. logger.warning('Store is not active')
  166. return False
  167. cnt_act = 0
  168. while 1:
  169. self.screenshot()
  170. if self.appear(self.I_UI_CONFIRM):
  171. self.ui_click_until_disappear(self.I_UI_CONFIRM, interval=2)
  172. break
  173. if cnt_act >= 3:
  174. logger.warning('Store is not active')
  175. return False
  176. if self.appear_then_click(self.I_M_STORE_ACTIVITY, interval=1.5):
  177. cnt_act += 1
  178. continue
  179. def entry_island_random(self, area: list = None):
  180. """
  181. :param area: 搁置
  182. :return:
  183. """
  184. # if not isinstance(area, list):
  185. # area = list(area)
  186. logger.info('Entry island randomly')
  187. timeout = Timer(15).start()
  188. click_timer = Timer(1)
  189. entrances = (self.C_ISLAND_ENTER_4, self.C_ISLAND_ENTER_5, self.C_ISLAND_ENTER_6)
  190. entrance_index = 0
  191. while 1:
  192. self.screenshot()
  193. if not self.in_main() and self.appear(self.I_BACK_EXIT):
  194. break
  195. if timeout.reached():
  196. raise GameStuckError('Enter random island timeout')
  197. if not click_timer.reached():
  198. continue
  199. if self.appear_then_click(self.I_UI_CANCEL, interval=1):
  200. click_timer.reset()
  201. continue
  202. self.click(entrances[entrance_index])
  203. entrance_index = (entrance_index + 1) % len(entrances)
  204. click_timer.reset()
  205. return
  206. if __name__ == '__main__':
  207. from module.config.config import Config
  208. from module.device.device import Device
  209. from module.base.utils import load_image
  210. c = Config('oas1')
  211. d = Device(c)
  212. t = MoonSeaMap(c, d)
  213. t.device.image = load_image(r'C:\Users\Ryland\Desktop\Desktop\34.png')
  214. match = re.search(r'\d{1,2}', '<17回合后迎战月读')
  215. if match:
  216. isl_num = int(match.group())
  217. print(isl_num)