map.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import cv2
  2. import re
  3. import numpy as np
  4. from numpy import uint8, fromfile
  5. from module.logger import logger
  6. from tasks.base_task import BaseTask
  7. from tasks.SixRealms.moon_sea.skills import MoonSeaSkills
  8. from tasks.SixRealms.assets import SixRealmsAssets
  9. from tasks.SixRealms.common import MoonSeaType
  10. from tasks.SixRealms.oas_ocr import StoneOcr
  11. class MoonSeaMap(MoonSeaSkills):
  12. map_ocr = StoneOcr(
  13. roi=(0, 0, 1280, 720),
  14. area=(0, 0, 1280, 720),
  15. mode="Full",
  16. method="Default",
  17. keyword="",
  18. name="map_ocr")
  19. @staticmethod
  20. def contains_any_char(string, chars):
  21. return not set(string).isdisjoint(set(chars))
  22. def decide(self) -> tuple:
  23. """
  24. 这个玩意,检测不是很准
  25. @return: 岛屿的类型,剩余多少回合, (x,y, w, h)
  26. """
  27. self.screenshot()
  28. if self.appear(self.I_BOSS_FIRE):
  29. # 最后的boss
  30. return MoonSeaType.island106, 0, (0, 0, 0, 0)
  31. isl_type = MoonSeaType.island100
  32. isl_roi = (0, 0, 0, 0)
  33. isl_num = 0
  34. results = self.map_ocr.detect_and_ocr(image=self.device.image)
  35. rx1, ry1, rw, rh = self.O_OCR_MAP.roi
  36. rx2, ry2 = rx1 + rw, ry1 + rh
  37. for result in results:
  38. # box 是四个点坐标 左上, 右上, 右下, 左下
  39. x1, y1, x2, y2 = result.box[0][0], result.box[0][1], result.box[2][0], result.box[2][1]
  40. w, h = x2 - x1, y2 - y1
  41. text = result.ocr_text
  42. if self.contains_any_char(result.ocr_text, chars='回合国') and \
  43. (x1 > 1000 and y1 + h < 300):
  44. # if text[1].isdigit():
  45. # isl_num = int(text[:2])
  46. # elif text[0].isdigit():
  47. # isl_num = int(text[0])
  48. match = re.search(r'\d{1,2}', text)
  49. if match:
  50. isl_num = int(match.group())
  51. if x1 < rx1 or x2 > rx2 or y1 < ry1 or y2 > ry2:
  52. continue
  53. if isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='宁息'):
  54. isl_type = MoonSeaType.island101
  55. isl_roi = x1, y1, w, h
  56. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='神秘'):
  57. isl_type = MoonSeaType.island102
  58. isl_roi = x1, y1, w, h
  59. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='回混范'):
  60. isl_type = MoonSeaType.island103
  61. isl_roi = x1, y1, w, h
  62. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='蜜馨屡战'):
  63. isl_type = MoonSeaType.island104
  64. isl_roi = x1, y1, w, h
  65. elif isl_type == MoonSeaType.island100 and self.contains_any_char(result.ocr_text, chars='星之'):
  66. isl_type = MoonSeaType.island105
  67. isl_roi = x1, y1, w, h
  68. logger.info('Island type: {}, Residue: {}, ROI: {}'.format(isl_type, isl_num, isl_roi))
  69. return isl_type, isl_num, isl_roi
  70. def enter_island(self, isl_type, isl_roi):
  71. if isl_type == MoonSeaType.island100:
  72. logger.warning('The island type was not recognized')
  73. logger.warning('Pick one at random, starting from the right')
  74. while 1:
  75. self.screenshot()
  76. if not self.in_main() and self.appear(self.I_BACK_EXIT):
  77. break
  78. if self.click(self.C_ISLAND_ENTER_1, interval=2):
  79. continue
  80. if self.click(self.C_ISLAND_ENTER_2, interval=2):
  81. continue
  82. if self.click(self.C_ISLAND_ENTER_3, interval=2):
  83. continue
  84. if self.click(self.C_ISLAND_ENTER_4, interval=2):
  85. continue
  86. if self.click(self.C_ISLAND_ENTER_5, interval=2):
  87. continue
  88. if self.click(self.C_ISLAND_ENTER_6, interval=2):
  89. continue
  90. logger.info('Entering island randomly')
  91. return
  92. isl_roi = [isl_roi[0]-20, isl_roi[1]-20, isl_roi[2]+40, isl_roi[3]+40]
  93. self.C_ISLAND_ENTER.roi_front = isl_roi
  94. while 1:
  95. self.screenshot()
  96. if not self.in_main() and self.appear(self.I_BACK_EXIT):
  97. break
  98. if self.click(self.C_ISLAND_ENTER, interval=1):
  99. continue
  100. logger.info('Entering island')
  101. return
  102. def activate_store(self) -> bool:
  103. """
  104. 最后打boss前面激活一次商店买东西
  105. @return: 有钱够就是True
  106. """
  107. logger.info('Activating store')
  108. self.screenshot()
  109. if self.appear(self.I_M_STORE) and not self.appear(self.I_M_STORE_ACTIVITY):
  110. logger.warning('Now you have not money to buy items')
  111. logger.warning('Store is not active')
  112. return False
  113. while 1:
  114. self.screenshot()
  115. if self.appear(self.I_UI_CONFIRM):
  116. self.ui_click_until_disappear(self.I_UI_CONFIRM, interval=2)
  117. break
  118. if self.appear_then_click(self.I_M_STORE_ACTIVITY, interval=1):
  119. continue
  120. if __name__ == '__main__':
  121. from module.config.config import Config
  122. from module.device.device import Device
  123. from module.base.utils import load_image
  124. c = Config('oas1')
  125. d = Device(c)
  126. t = MoonSeaMap(c, d)
  127. t.device.image = load_image(r'C:\Users\Ryland\Desktop\Desktop\34.png')
  128. match = re.search(r'\d{1,2}', '<17回合后迎战月读')
  129. if match:
  130. isl_num = int(match.group())
  131. print(isl_num)