Skip to content

Commit

Permalink
tuning autocombat
Browse files Browse the repository at this point in the history
  • Loading branch information
ok-oldking committed Jun 15, 2024
1 parent 8839961 commit a4ff0a7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 51 deletions.
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ def calculate_pc_exe_path(running_path):
# 'use_proxy': True
# },
'about': """
<h3>OK白荆</h3>
<h3>OK-WW</h3>
<p>免费开源软件 <a href="https://github.com/ok-oldking/ok-baijing">https://github.com/ok-oldking/ok-baijing</></p>
<p>报告问题BUG <a href="https://github.com/ok-oldking/ok-baijing/issues/new?assignees=ok-oldking&labels=bug&projects=&template=%E6%8A%A5%E5%91%8Abug-.md&title=%5BBUG%5D">https://github.com/ok-oldking/ok-baijing/issues/new?assignees=ok-oldking&labels=bug&projects=&template=%E6%8A%A5%E5%91%8Abug-.md&title=%5BBUG%5D</></p>
<p>视频演示 <a href="https://www.bilibili.com/video/BV1K7421f7KT/">https://www.bilibili.com/video/BV1K7421f7KT/</a></p>
<p>QQ群:<a href="https://qm.qq.com/q/aGO7eBJ2Uw">594495691</a></p>
""",
'supported_screen_ratio': '16:9',
'screenshots_folder': "screenshots",
'gui_title': 'OK白荆', # Optional
'gui_title': 'OK-WW', # Optional
# 'coco_feature_folder': get_path(__file__, 'assets/coco_feature'), # required if using feature detection
'log_file': 'logs/ok-script.log', # Optional, auto rotating every day
'error_log_file': 'logs/ok-script_error.log',
Expand Down
16 changes: 14 additions & 2 deletions src/char/BaseChar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from ok.color.Color import white_color, calculate_colorfulness
from ok.logging.Logger import get_logger
from src.task.AutoCombatTask import AutoCombatTask


class Priority(IntEnum):
Expand All @@ -19,7 +18,8 @@ class Priority(IntEnum):


class BaseChar:
def __init__(self, task: AutoCombatTask, index, res_cd=0):

def __init__(self, task, index, res_cd=0):
self.white_off_threshold = 0.002
self.task = task
self.sleep_adjust = 0.001
Expand All @@ -34,6 +34,15 @@ def __init__(self, task: AutoCombatTask, index, res_cd=0):
self.con_ready = False
self.is_current_char = False

@property
def name(self):
return self.__class__.__name__

def __eq__(self, other):
if isinstance(other, BaseChar):
return self.name == other.name
return False

def perform(self):
self.is_current_char = True
self.do_perform()
Expand All @@ -57,6 +66,7 @@ def __repr__(self):
return self.__class__.__name__

def switch_next_char(self, post_action=None):
self.normal_attack()
self.last_switch_time = self.task.switch_next_char(self, post_action=post_action)

def sleep(self, sec):
Expand Down Expand Up @@ -94,6 +104,8 @@ def do_get_switch_priority(self, current_char, has_intro=False):
priority += 1
if self.resonance_available():
priority += 1
if self.is_forte_full():
priority += 1
if priority > 0:
priority += Priority.SKILL_AVAILABLE
return priority
Expand Down
13 changes: 9 additions & 4 deletions src/char/CharFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ def get_char_by_pos(task, box, index):
'char_taoqi': {'cls': Taoqi, 'res_cd': 15, 'echo_cd': 20},
'char_rover': {'cls': HavocRover, 'res_cd': 12, 'echo_cd': 20}
}

highest_confidence = 0
info = None
for char_name, char_info in char_dict.items():
feature = task.find_feature(char_name, box=box, threshold=0.9)
feature = task.find_one(char_name, box=box, threshold=0.7)
if feature and feature.confidence > highest_confidence:
highest_confidence = feature.confidence
info = char_info
if feature:
task.log_info(f'found char {char_name}')
return char_info.get('cls')(task, index, char_info.get('res_cd'))
task.log_info(f'found char {char_name} {feature.confidence}')
if info is not None:
return info.get('cls')(task, index, info.get('res_cd'))
task.log_info(f'could not find char')
return BaseChar(task, index)
18 changes: 14 additions & 4 deletions src/char/HavocRover.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from ok.logging.Logger import get_logger

from src.char.BaseChar import BaseChar

logger = get_logger(__name__)


class HavocRover(BaseChar):
def do_perform(self):
if self.resonance_available():
logger.info(f'Use e')
self.click_resonance()
self.sleep(0.2)
if self.is_forte_full() and self.liberation_available():
logger.info(f'forte_full, and liberation_available, heavy attack')
self.heavy_attack()
self.sleep(0.2)
if self.resonance_available():
self.click_resonance()
self.sleep(0.2)
if self.liberation_available():
self.click_liberation()
self.sleep(2)
if self.resonance_available():
self.click_resonance()
if self.echo_available():
self.sleep(0.3)
self.click_echo()
self.sleep(0.3)
self.sleep(0.1)
self.switch_next_char()
2 changes: 1 addition & 1 deletion src/char/Verina.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ def do_get_switch_priority(self, current_char: BaseChar, has_intro=False):
if has_intro:
return super().do_get_switch_priority(current_char, has_intro) - 5
else:
return super().do_get_switch_priority(current_char, has_intro)
return super().do_get_switch_priority(current_char, has_intro) - 1
10 changes: 7 additions & 3 deletions src/char/Yinlin.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from ok.logging.Logger import get_logger

from src.char.BaseChar import BaseChar

logger = get_logger(__name__)


class Yinlin(BaseChar):
def do_perform(self):
if self.is_forte_full():
if not self.has_intro:
self.normal_attack()
self.sleep(0.2)
if self.liberation_available():
self.click_liberation()
self.sleep(3.1)
self.heavy_attack()
self.sleep(0.4)
elif self.resonance_available():
Expand Down
90 changes: 55 additions & 35 deletions src/task/AutoCombatTask.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import re
import time

from ok.color.Color import find_color_rectangles
from ok.feature.FindFeature import FindFeature
from ok.logging.Logger import get_logger
from ok.ocr.OCR import OCR
from ok.task.TriggerTask import TriggerTask
from ok.util.list import safe_get
from src.char import BaseChar
from src.char.CharFactory import get_char_by_pos

logger = get_logger(__name__)
Expand All @@ -14,16 +16,17 @@ class AutoCombatTask(TriggerTask, FindFeature, OCR):

def __init__(self):
super().__init__()
self.chars = list()
self.chars = [None, None, None]
self.char_texts = ['char_1_text', 'char_2_text', 'char_3_text']
self.default_config = {
self.default_config.update({
'Echo Key': 'q',
'Liberation Key': 'r',
'Resonance Key': 'e',
}
})
self.last_check_combat = time.time()
self._in_combat = False
self.char_texts = ['char_1_text', 'char_2_text', 'char_3_text']
self._need_check_char = True

def run(self):
self.load_chars()
Expand All @@ -39,6 +42,7 @@ def switch_next_char(self, current_char, post_action=None):
priority = 0
else:
priority = char.get_switch_priority(current_char, has_intro)
logger.info(f'switch priority: {char} {priority}')
if priority > max_priority:
max_priority = priority
switch_to = char
Expand Down Expand Up @@ -83,45 +87,62 @@ def in_combat(self):
def check_in_combat(self):
self.last_check_combat = time.time()
if self._in_combat:
if not self.in_team() or not self.ocr(0.1, 0, 0.9, 0.9, match=re.compile(r'^Lv'), target_height=720):
if not self.in_team() or not self.check_health_bar():
time.sleep(4)
if not self.in_team() or not self.ocr(0.1, 0, 0.9, 0.9, match=re.compile(r'^Lv'), target_height=720):
if not self.in_team() or not self.check_health_bar():
self._in_combat = False
self._need_check_char = True
else:
self._in_combat = self.in_team() and self.ocr(0.1, 0, 0.9, 0.9, match=re.compile(r'^Lv'), target_height=720)

# min_height = self.height_of_screen(10 / 2160)
# max_height = min_height * 3
# min_width = self.width_of_screen(90 / 3840)
# boxes = find_color_rectangles(self.frame, enemy_health_color_red, min_width, min_height, max_height=max_height)
#
# if len(boxes) > 0:
# self.draw_boxes('enemy_health_bar_red', boxes, color='blue')
# return True
# else:
# boxes = find_color_rectangles(self.frame, enemy_health_color_black, min_width, min_height,
# max_height=max_height)
# if len(boxes) > 0:
# self.draw_boxes('enemy_health_black', boxes, color='blue')
# return True
# else:
# boxes = find_color_rectangles(self.frame, boss_health_color, min_width, min_height * 1.5,
# box=self.box_of_screen(1269 / 3840, 58 / 2160, 2533 / 3840, 192 / 2160))
# if len(boxes) > 0:
# self.draw_boxes('boss_health', boxes, color='blue')
# return True
self._in_combat = self.in_team() and self.check_health_bar()

def check_health_bar(self):
min_height = self.height_of_screen(10 / 2160)
max_height = min_height * 3
min_width = self.width_of_screen(40 / 3840)
boxes = find_color_rectangles(self.frame, enemy_health_color_red, min_width, min_height, max_height=max_height)

if len(boxes) > 0:
self.draw_boxes('enemy_health_bar_red', boxes, color='blue')
return True
else:
boxes = find_color_rectangles(self.frame, enemy_health_color_black, min_width, min_height,
max_height=max_height)
if len(boxes) > 0:
self.draw_boxes('enemy_health_black', boxes, color='blue')
return True
else:
boxes = find_color_rectangles(self.frame, boss_health_color, min_width, min_height * 1.5,
box=self.box_of_screen(1269 / 3840, 58 / 2160, 2533 / 3840, 192 / 2160))
if len(boxes) > 0:
self.draw_boxes('boss_health', boxes, color='blue')
return True

def load_chars(self):
if self.chars:
if self.chars and not self._need_check_char:
return
if not self.in_team():
return
self.log_info('load chars')
self.chars.clear()
self.chars.append(get_char_by_pos(self, self.get_box_by_name('box_char_1'), 0))
self.chars.append(get_char_by_pos(self, self.get_box_by_name('box_char_2'), 1))
self.chars.append(get_char_by_pos(self, self.get_box_by_name('box_char_3'), 2))
self.log_info(f'load chars success {self.chars}', notify=True)
char = get_char_by_pos(self, self.get_box_by_name('box_char_1'), 0)
old_char = safe_get(self.chars, 0)
if (type(char) is BaseChar and old_char is None) or type(char) is not BaseChar:
self.chars[0] = char
logger.info(f'update char1 to {char.name}')

char = get_char_by_pos(self, self.get_box_by_name('box_char_2'), 1)
old_char = safe_get(self.chars, 1)
if (type(char) is BaseChar and old_char is None) or type(char) is not BaseChar:
self.chars[1] = char
logger.info(f'update char2 to {char.name}')

char = get_char_by_pos(self, self.get_box_by_name('box_char_3'), 2)
old_char = safe_get(self.chars, 2)
if (type(char) is BaseChar and old_char is None) or type(char) is not BaseChar:
self.chars[2] = char
logger.info(f'update char3 to {char.name}')

self.log_info(f'load chars success {self.chars}')
self._need_check_char = False

def box_resonance(self):
return self.get_box_by_name('box_resonance_cd')
Expand All @@ -136,7 +157,6 @@ def in_team(self):
c1 = self.find_one('char_1_text')
c2 = self.find_one('char_2_text')
c3 = self.find_one('char_3_text')
logger.debug(f'in team {c1} {c2} {c3}')
return sum(x is not None for x in [c1, c2, c3]) == 2


Expand Down

0 comments on commit a4ff0a7

Please sign in to comment.