diff --git a/assets/templates/inventory/tab_indicator.png b/assets/templates/inventory/tab_indicator.png new file mode 100644 index 0000000..b35605b Binary files /dev/null and b/assets/templates/inventory/tab_indicator.png differ diff --git a/config/game.ini b/config/game.ini index 32785c4..e4bacac 100644 --- a/config/game.ini +++ b/config/game.ini @@ -132,6 +132,7 @@ npc_dialogue=458,4,21,140 bind_skill=516,619,251,29 quest_skill_btn=284,455,710,121 left_inventory_tabs=31,62,385,28 +tab_indicator=31,82,385,14 deposit_btn=454,365,186,43 [path] diff --git a/src/inventory/common.py b/src/inventory/common.py index c7d214b..1f8a369 100644 --- a/src/inventory/common.py +++ b/src/inventory/common.py @@ -9,11 +9,11 @@ from ui_manager import detect_screen_object, ScreenObjects, center_mouse, is_vis from utils.misc import wait, trim_black, color_filter, cut_roi from inventory import consumables, personal from ui import view -from screen import grab +from screen import convert_screen_to_monitor, grab from dataclasses import dataclass from logger import Logger from ocr import Ocr - +from template_finder import TemplateMatch @dataclass class BoxInfo: @@ -244,6 +244,44 @@ def left_inventory_ready(img = np.ndarray): return any(np.sum(i) > 0 for i in [text, red, blue]) return False +def tab_properties(idx: int = 0) -> dict[int, int, tuple]: + tab_width = round(int(Config().ui_roi["tab_indicator"][2]) / 4) + x_start = int(Config().ui_roi["tab_indicator"][0]) + left = idx * tab_width + x_start + right = (idx + 1) * tab_width + x_start + x_center = (left + right) / 2 + y_center = int(Config().ui_roi["tab_indicator"][1]) - 5 + return { + "left": round(left), + "right": round(right), + "center": (x_center, y_center) + } + +def indicator_location_to_tab_count(pos: tuple) -> int: + for i in range(3): + tab = tab_properties(i) + if tab["left"] <= pos[0] < tab["right"]: + return i + +def get_active_tab(indicator: TemplateMatch = None) -> int: + indicator = detect_screen_object(ScreenObjects.TabIndicator) if indicator is None else indicator + if indicator.valid: + return indicator_location_to_tab_count(indicator.center) + else: + Logger.error("common/get_active_tab(): Error finding tab indicator") + return False + +def select_tab(idx: int): + # stash or vendor must be open + # indices start from 0 + if not get_active_tab() == idx: + tab = tab_properties(idx) + pos = convert_screen_to_monitor(tab["center"]) + mouse.move(*pos) + wait(0.2, 0.3) + mouse.click("left") + wait(0.2, 0.3) + if __name__ == "__main__": import os import keyboard @@ -255,8 +293,9 @@ if __name__ == "__main__": print("Move to d2r window and press f11") keyboard.wait("f11") - color = Config().colors["tab_text"] + #select_tab(0) + color = Config().colors["tab_text"] while 1: # img = cv2.imread("") img = grab() diff --git a/src/town/town_manager.py b/src/town/town_manager.py index ec5f4a9..1e3db4e 100644 --- a/src/town/town_manager.py +++ b/src/town/town_manager.py @@ -98,6 +98,7 @@ class TownManager: if self._acts[curr_act].can_buy_pots(): new_loc = self._acts[curr_act].open_trade_menu(curr_loc) if not (new_loc and common.wait_for_left_inventory()): return False, items + common.select_tab(3) img=grab() # Buy HP pots if consumables.get_needs("health") > 0: diff --git a/src/ui_manager.py b/src/ui_manager.py index f5e0c08..49bd8a0 100644 --- a/src/ui_manager.py +++ b/src/ui_manager.py @@ -1,4 +1,3 @@ -from cv2 import norm import keyboard import os import numpy as np @@ -266,6 +265,11 @@ class ScreenObjects: use_grayscale=True, roi="quest_skill_btn" ) + TabIndicator=ScreenObject( + ref="TAB_INDICATOR", + roi="tab_indicator", + normalize_monitor=False + ) DepositBtn=ScreenObject( ref=["DEPOSIT_BTN", "DEPOSIT_BTN_BRIGHT"], threshold=0.8, @@ -342,7 +346,6 @@ if __name__ == "__main__": print("Go to D2R window and press f11 to start game") keyboard.wait("f11") from config import Config - # while 1: - # print(list_visible_objects()) - # time.sleep(1) - print(f"res = {wait_until_visible(ScreenObjects.BeltExpandable, 7)}") + while 1: + print(list_visible_objects()) + time.sleep(1)