focus.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import numpy as np
  2. from rich.table import Table
  3. from rich.text import Text
  4. from cached_property import cached_property
  5. from module.logger import logger
  6. from oashya.labels import id2label, id2name
  7. from oashya.labels import CLASSINDEX as CI
  8. # get buff status
  9. from tasks.Hyakkiyakou.slave.hya_slave import HyaBuff
  10. class Focus:
  11. def __init__(self, inputs: list[tuple]):
  12. self._id: int = inputs[0]
  13. self._class: int = inputs[1]
  14. self._conf: int = inputs[2]
  15. self._cx: int = inputs[3]
  16. self._cy: int = inputs[4]
  17. self._w: int = inputs[5]
  18. self._h: int = inputs[6]
  19. self._v: float = inputs[7]
  20. # -----------------------
  21. self._omega: float = 0.
  22. self._omega_buff: float = 0.
  23. def __eq__(self, o):
  24. return self._id == o._id or o._class == self._class
  25. def __str__(self):
  26. return f"""
  27. id: {self._id}
  28. class: {self._class}
  29. label: {id2label(self._class)}
  30. name: {id2name(self._class)}
  31. conf: {self._conf}
  32. xywh: ({self._cx}, {self._cy}, {self._w}, {self._h})
  33. velocity: {self._v}"""
  34. def omega(self, z):
  35. height, width = z.shape[:2]
  36. if not (0 <= self._cy < height and 0 <= self._cx < width):
  37. self._omega = 0.
  38. return self._omega
  39. __omega = z[self._cy, self._cx]
  40. self._omega = __omega
  41. return __omega
  42. def set_omega(self, value):
  43. self._omega = value
  44. def show(self):
  45. table = Table(show_lines=True)
  46. table.add_column('Property', header_style="bright_cyan", style="cyan", no_wrap=True)
  47. table.add_column("Value", style="green")
  48. table.add_column('Property', header_style="bright_cyan", style="cyan", no_wrap=True)
  49. table.add_column("Value", style="green")
  50. table.add_row('id', str(self._id), 'conf', str(self._conf))
  51. table.add_row('class', str(self._class), 'xywh', f'({self._cx}, {self._cy}, {self._w}, {self._h})')
  52. table.add_row('label', id2label(self._class), 'velocity', str(self._v))
  53. table.add_row('name', f'{id2name(self._class)}', 'omega', str(self._omega))
  54. logger.print(table, justify='center')
  55. def update(self, focus):
  56. self._id = focus._id
  57. self._class = focus._class
  58. self._conf = focus._conf
  59. self._cx = focus._cx
  60. self._cy = focus._cy
  61. self._w = focus._w
  62. self._h = focus._h
  63. self._v = focus._v
  64. def decision(self, tracks, strategy, state, freeze: bool = False) -> list:
  65. buff_states = state[4:] if len(state) > 4 else [] # get buff status:state[4:] = buff_0 ~ buff_3,对应 HyaBuff
  66. has_prob_up = any(b == HyaBuff.BUFF_STATE6 for b in buff_states) # 是否存在“概率UP”buff(HyaBuff.BUFF_STATE6)
  67. buff_omega, buff_cx, buff_cy, buff_v, buff_class = self.omega_buff(tracks, strategy['invite_friend'], has_prob_up)
  68. if self._omega > buff_omega:
  69. buffed = False
  70. target_x = self._cx + self._v * 100 - (self._w // 2)
  71. target_y = self._cy - 40
  72. target_class = self._class # new : for class
  73. else:
  74. buffed = True
  75. target_x = buff_cx + buff_v * 100
  76. target_y = buff_cy - 40 # top left corner
  77. target_class = buff_class # new : for class
  78. # ========= 新增:利用和 gamma() 一样的区间判断当前“打的对象”是不是 SSR/SP =========
  79. is_rare_ssr_sp = False
  80. if CI.MIN_SSR <= target_class <= CI.MAX_SSR and self._omega > buff_omega:
  81. is_rare_ssr_sp = True
  82. elif CI.MIN_SP <= target_class <= CI.MAX_SP and self._omega > buff_omega:
  83. is_rare_ssr_sp = True
  84. _r = self.r(vector=state, omega=self._omega, omega_buff=self._omega_buff, is_rare_ssr_sp=is_rare_ssr_sp, freeze=freeze, is_buff=buffed)
  85. throw = True if _r > 0 else False
  86. # x, y, throw, number
  87. return [target_x, target_y, throw, 10]
  88. def omega_buff(self, tracks, invite_friend: bool, has_prob_up: bool) -> float:
  89. max_omega = 0.
  90. max_cx = 0
  91. max_cy = 0
  92. max_v = 0
  93. max_class = 0
  94. focus_is_ssr_sp = (CI.MIN_SSR <= self._class <= CI.MAX_SSR or CI.MIN_SP <= self._class <= CI.MAX_SP)
  95. for _id, _class, _conf, _cx, _cy, _w, _h, _v in tracks:
  96. _current_omega = 0.
  97. match _class:
  98. case CI.BUFF_004: _current_omega = 2.2
  99. case CI.BUFF_006: _current_omega = 2.0
  100. case CI.BUFF_007: _current_omega = 2.0 if invite_friend and _cx <= 1000 else 0
  101. case CI.BUFF_002: _current_omega = 2.0 if self._omega >= 1.5 else 0
  102. case CI.BUFF_003: _current_omega = 2.0 if self._omega >= 1.5 and _cx <= 640 else 0
  103. # case CI.BUFF_005: _current_omega = max(self._omega + 0.1, 3.0) if focus_is_ssr_sp and has_prob_up else 0 # 冻住,不许跑
  104. case _: pass
  105. if _current_omega > max_omega:
  106. max_omega = _current_omega
  107. max_cx = _cx
  108. max_cy = _cy
  109. max_v = _v
  110. max_class = _class
  111. return max_omega, max_cx, max_cy, max_v, max_class
  112. @classmethod
  113. def r(cls, vector: list, omega: float, omega_buff: float, is_rare_ssr_sp: bool, freeze: bool = False, is_buff: bool = False) -> float:
  114. _omega = max(omega, omega_buff)
  115. tau = - 140 / (max(101, vector[0]) - 100) # 时间冷却项:避免过密投豆
  116. upsilon = (vector[1] / 250 - vector[2] / 35) # 豆子数量 vs 进度:豆多进度低 -> 倾向多砸;豆少进度高 -> 惩罚多砸
  117. upsilon = 100 * (upsilon**2 if upsilon > 0 else - upsilon**2)
  118. result = _omega + tau + upsilon - 0.6
  119. # print(f"total: {result:.4f} | {_omega:.4f} | {tau:.4f} | {upsilon:.4f}")
  120. buff_states = vector[4:] if len(vector) > 4 else [] # get buff status:vector[4:] = buff_0 ~ buff_3,对应 HyaBuff
  121. has_prob_up = any(b == HyaBuff.BUFF_STATE6 for b in buff_states) # 是否存在“概率UP”buff(HyaBuff.BUFF_STATE6)
  122. # punishment:
  123. # no throwing when ssr/sp with no enhanced probability
  124. if is_rare_ssr_sp and not has_prob_up:
  125. result = -999.0 # stop throwing nonsense
  126. # punishment:
  127. # only throw ssr/sp with enhanced probability or buff when freezed
  128. if freeze:
  129. # if not (is_rare_ssr_sp and has_prob_up):
  130. # result = -999.0 # stop throwing nonsense
  131. # if is_buff:
  132. # result = 5.0
  133. result = -999.0
  134. return result