From 1adeb3a32a0e136ad0606fba2ec61699f2129b63 Mon Sep 17 00:00:00 2001 From: mgleed Date: Wed, 16 Mar 2022 21:52:29 -0400 Subject: [PATCH] Bugfix: Some NPC's don't open to misc tab (#642) * init * cleanup * clean * cleanup --- assets/templates/inventory/tab_indicator.png | Bin 0 -> 2164 bytes config/game.ini | 1 + src/inventory/common.py | 45 +++++++++++++++++-- src/town/town_manager.py | 1 + src/ui_manager.py | 13 +++--- 5 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 assets/templates/inventory/tab_indicator.png diff --git a/assets/templates/inventory/tab_indicator.png b/assets/templates/inventory/tab_indicator.png new file mode 100644 index 0000000000000000000000000000000000000000..b35605b34af98a4ef6c1627aa713c50c42ca9232 GIT binary patch literal 2164 zcmcImeQXnD9PScW#y3b<2r=<;g9v2T`)IH29V=s7E348mw<04F;Jx?W>&~@z-Q8{N zHYH*tfXL9Hm;lj$K@bcCB=WI11SA9@A$|}DWGW97!{Hbast(lQ-y!%IT2zR?mBChzeIrpl&vZD3jkS*g$GQ#U z9gf1yYNWwx2+tCs9(Mv+myk0RPhhmeQC6Nx0N8{qLPAkh^OHBa50Zo``^mX{m=>N_37T~>_MCFSaJYYHJf26JRF;QMYunQGblK{NsNplwrPLqQ!`%?U5mkVknNeO^jvJ`VwN5fqbu z0s{l&kgWmxFrJrt4@NHNgOQ<4|wK<0q}1Ri== z7fkEI;{!Tuv7NBTXL2j+5Od_+6p=@E`2@sJE|%vg#Jd&B=jGj$0wBvO9tA>%%dyQi zRQ&3I*u$*0Dj74nSp^_o@=}sd;wjFDRrwgnMJXPaSM~{t;!`lsgSu6EOgF;1jNNsS zgbYPRH{t;2M~#Chq7z!wPBUi5v#3^0+`6{x`%#aYvu81tNQX`Y(9XJ_gm#mWOlEJZ zk8p2bIy1D%MubWGX?_M~>WY;F2C9f+r~flt^y9>vptXPL18e;MC_QT{Yy?^q;gypn z?a9#g5|e2P{qKG~=yxtN6*$dyH_faKl^~ioWcs5c>gC)VC-Xez6_7$f-px@G2R)P< zu#yZURsfL6sEDZ#xN^btuo7aC7gBCn0eI1MLke&RQZAndcw8(Cd>;Ox(dpS_MA7}L zPa_{%YI9cL_1iwp?;Q9dzxN;w+h*XMr8`#I?r;>egaQ?j@$U{kbc%kl!12Y+8y)w4 zIlbMxXYuXIi*sX}LdS}0iVlI|WqqKOw-dFM&Qg`VFvYVWtJiv7WN{C`YbHK=cY z=ZTWHd-_(EkN&Z`cV^e5ZC73z^XclpM#n~$E#;c;ZCJPE^w#cagQiYB`~BI8>pNz+ zpJ=|?R=?Ggs}EUw;Md|Qa^bQ& z_t{xhKTkS*_3+atE}dlk;*y=wk>^*vapcUKpWR(@^TG?;nktLxZ*0+fwRv41OrF*A z=Wz7-TgSO?#{Beb`KB)yPKT~~NxpVw@BL5IbJNaG*ymfYvHR=82Uc#>w54OCm$ojN zSG-{Vj*n*uMZb^ioKrk`!Syxj;!>wG2 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)