Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
ok-oldking committed Jun 11, 2024
1 parent 8410a8c commit 285de56
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 55 deletions.
81 changes: 62 additions & 19 deletions src/char/BaseChar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

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


Expand All @@ -10,21 +10,22 @@ def __init__(self, task: AutoCombatTask, index):
self.task = task
self.sleep_adjust = 0.001
self.index = index
self.base_resonance_white_percentage = self.current_resonance()
self.base_echo_white_percentage = self.current_echo()
self.base_resonance_white_percentage = 0
self.base_echo_white_percentage = 0
self.base_liberation_white_percentage = 0
self.last_switch_time = time.time()
self.has_intro = False

def perform(self):
if self.liberation_available():
self.click_liberation()
self.sleep(1.5)
if self.resonance_available():
self.click_resonance()
if self.echo_available():
self.sleep(0.3)
self.click_echo()
self.sleep(0.3)
elif self.liberation_available():
self.click_liberation()
self.sleep(1.5)
self.switch_next_char()

def __repr__(self):
Expand All @@ -34,8 +35,8 @@ def switch_next_char(self, post_action=None):
self.last_switch_time = time.time()
self.task.switch_next_char(self, post_action=post_action)

def sleep(self, time):
self.task.sleep(time + self.sleep_adjust)
def sleep(self, sec):
self.task.sleep(sec + self.sleep_adjust)

def click_resonance(self):
self.task.send_key('e')
Expand All @@ -52,20 +53,56 @@ def get_liberation_key(self):
def get_echo_key(self):
return self.task.config['Echo Key']

def get_switch_priority(self, current_char):
def get_switch_priority(self, current_char, has_intro):
if time.time() - self.last_switch_time < 1:
return -2 # switch cd
return -1000 # switch cd
else:
return self.do_get_switch_priority(current_char)
return self.do_get_switch_priority(current_char, has_intro)

def do_get_switch_priority(self, current_char):
def do_get_switch_priority(self, current_char, has_intro=False):
return 1

def resonance_available(self):
return abs(self.base_resonance_white_percentage - self.current_resonance()) < self.white_off_threshold
snap1 = self.current_resonance()
if snap1 == 0:
return False
if self.base_resonance_white_percentage != 0:
return abs(self.base_resonance_white_percentage - snap1) < self.white_off_threshold
self.sleep(0.2)
snap2 = self.current_resonance()
if snap2 == snap1:
self.base_resonance_white_percentage = snap1
self.task.log_info(f'set base resonance to {self.base_resonance_white_percentage:.3f}')
return True

def echo_available(self):
return abs(self.base_echo_white_percentage - self.current_echo()) < self.white_off_threshold
snap1 = self.current_echo()
if snap1 == 0:
return False
if self.base_echo_white_percentage != 0:
return abs(self.base_echo_white_percentage - snap1) < self.white_off_threshold
self.sleep(0.2)
snap2 = self.current_echo()
if snap2 == snap1:
self.base_echo_white_percentage = snap1
self.task.log_info(f'set base resonance to {self.base_echo_white_percentage:.3f}')
return True

def is_con_full(self):
box = self.task.box_of_screen(1540 / 3840, 2007 / 2160, 1545 / 3840, 2010 / 2160, name='con_full')
colorfulness = calculate_colorfulness(self.task.frame, box)
box.confidence = colorfulness
self.task.draw_boxes('con_full', box)
if colorfulness > 0.1:
return True

def is_forte_full(self):
box = self.task.box_of_screen(2170 / 3840, 1998 / 2160, 2174 / 3840, 2007 / 2160, name='forte_full')
colorfulness = calculate_colorfulness(self.task.frame, box)
box.confidence = colorfulness
self.task.draw_boxes('forte_full', box)
if colorfulness > 0.1:
return True

def liberation_available(self):
snap1_lib = self.current_liberation()
Expand All @@ -80,6 +117,14 @@ def liberation_available(self):
self.task.log_info(f'set base liberation to {self.base_liberation_white_percentage:.3f}')
return True

def normal_attack(self):
self.task.click()

def heavy_attack(self):
self.task.mouse_down()
self.sleep(0.6)
self.task.mouse_up()

def current_resonance(self):
return self.task.calculate_color_percentage(white_color,
self.task.get_box_by_name('box_resonance'))
Expand All @@ -89,9 +134,7 @@ def current_echo(self):
self.task.get_box_by_name('box_echo'))

def current_liberation(self):
return self.task.calculate_color_percentage(white_color,
self.task.get_box_by_name('box_liberation'))
return self.task.calculate_color_percentage(white_color, self.task.get_box_by_name('box_liberation'))

def load(self, resonance_white_percentage, liberation_white_percentage):
self.base_resonance_white_percentage = resonance_white_percentage
self.liberation_white_percentage = liberation_white_percentage
def flying(self):
return self.current_resonance() == 0
19 changes: 18 additions & 1 deletion src/char/Taoqi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@


class Taoqi(BaseChar):
pass
def perform(self):
if self.has_intro:
self.normal_attack()
self.sleep(0.3)
self.normal_attack()
self.sleep(0.3)
self.normal_attack()
self.sleep(0.3)
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.switch_next_char()
21 changes: 20 additions & 1 deletion src/char/Verina.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@


class Verina(BaseChar):
pass

def perform(self):
if self.liberation_available():
self.click_liberation()
self.sleep(1.5)
if self.resonance_available():
self.click_resonance()
if self.echo_available():
self.sleep(0.3)
self.click_echo()
self.sleep(0.3)
else:
self.heavy_attack()
self.switch_next_char()

def do_get_switch_priority(self, current_char, has_intro=False):
if has_intro:
return -3
else:
return 2
25 changes: 18 additions & 7 deletions src/char/Yinlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,31 @@

class Yinlin(BaseChar):
def perform(self):
if self.resonance_available():
self.click_resonance()
if self.is_forte_full():
if not self.has_intro:
self.normal_attack()
self.sleep(0.2)
self.heavy_attack()
self.sleep(0.4)
elif self.resonance_available():
self.click_resonance()
if self.liberation_available():
self.sleep(0.2)
self.click_liberation()
self.sleep(2.5)
else:
self.sleep(0.4)
self.click_resonance()
self.sleep(.6)
self.switch_next_char()
elif self.echo_available():
echo_key = self.get_echo_key()
self.task.send_key_down(echo_key)
self.sleep(1)
self.sleep(.8)
self.switch_next_char(post_action=self.echo_post_action)
else:
self.sleep(0.5)
self.switch_next_char()
elif self.liberation_available():
self.click_liberation()
self.sleep(2.5)
self.switch_next_char()

def echo_post_action(self): # hold down the echo for 1 seconds and switch and then release the echo key
self.task.send_key_up(self.get_echo_key())
Expand Down
77 changes: 50 additions & 27 deletions src/task/AutoCombatTask.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import re
import time

from ok.color.Color import white_color, find_color_rectangles
from ok.color.Color import white_color
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 src.char.CharFactory import get_char_by_pos

logger = get_logger(__name__)


class AutoCombatTask(TriggerTask, FindFeature):
class AutoCombatTask(TriggerTask, FindFeature, OCR):

def __init__(self):
super().__init__()
Expand All @@ -22,28 +24,40 @@ def __init__(self):
}
self.last_check_combat = time.time()
self._in_combat = False
self.char_texts = ['char_1_text', 'char_2_text', 'char_3_text']

def run(self):
self.load_chars()
if self.in_combat():
if self.chars and self.in_combat():
self.get_current_char().perform()

def switch_next_char(self, current_char, post_action=None):
max_priority = -1
switch_to = None
has_intro = current_char.is_con_full()
for i, char in enumerate(self.chars):
if char == current_char:
priority = 0
else:
priority = char.get_switch_priority(current_char)
priority = char.get_switch_priority(current_char, has_intro)
if priority > max_priority:
max_priority = priority
switch_to = char
if switch_to == current_char:
self.sleep(0.5)
self.sleep(0.2)
current_char.normal_attack()
logger.warning(f"can't find next char to switch to, maybe switching too fast, sleep and wait")
return self.switch_next_char(current_char, post_action)
switch_to.has_intro = has_intro
self.send_key(switch_to.index + 1)
while True:
self.sleep(0.01)
if self.find_one(self.char_texts[switch_to.index]):
self.send_key(switch_to.index + 1)
logger.info('switch not detected, try click again')
else:
break

if post_action:
post_action()

Expand All @@ -67,18 +81,28 @@ def in_combat(self):

def check_in_combat(self):
self.last_check_combat = time.time()
min_height = self.height_of_screen(9 / 2160)
min_width = self.width_of_screen(60 / 3840)
boxes = find_color_rectangles(self.frame, enemy_health_color_red, min_width, min_height)

if len(boxes) > 0:
self.draw_boxes('enemy_health_bar', boxes, color='blue')
return True
else:
boxes = find_color_rectangles(self.frame, enemy_health_color_black, min_width, min_height)
if len(boxes) > 0:
self.draw_boxes('enemy_health_black', boxes, color='blue')
return True
return 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

def load_chars(self):
if self.chars:
Expand All @@ -87,15 +111,8 @@ def load_chars(self):
return
self.log_info('load chars')
self.chars.clear()
self.sleep(1)
self.send_key(1)
self.sleep(1)
self.chars.append(get_char_by_pos(self, self.get_box_by_name('box_char_1'), 0))
self.send_key(2)
self.sleep(1)
self.chars.append(get_char_by_pos(self, self.get_box_by_name('box_char_2'), 1))
self.send_key(3)
self.sleep(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)

Expand Down Expand Up @@ -123,7 +140,13 @@ def in_team(self):
} # 207,75,60

enemy_health_color_black = {
'r': (45, 55), # Red range
'g': (28, 38), # Green range
'b': (18, 28) # Blue range
'r': (10, 55), # Red range
'g': (28, 50), # Green range
'b': (18, 70) # Blue range
}

boss_health_color = {
'r': (250, 255), # Red range
'g': (30, 180), # Green range
'b': (4, 75) # Blue range
}

0 comments on commit 285de56

Please sign in to comment.