focus.py 6.3 KB

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