Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Commit

Permalink
Bugfixes: A2 TP stuck, also recapture some templates for d2r v2.4
Browse files Browse the repository at this point in the history
* add _capture_template switch to template_finder.py _main function, replace eldritch templates

* update some trav templates

* update template_finder.py, update act 3 town templates

* update missing Eldritch templates

* add redudant eldritch templates and update nodes

* update shenk templates

* update trav assets, add redundant assets w nodes

* bugfix: a2 tp out of range of a2 templates

Co-authored-by: aeon0 <[email protected]>
  • Loading branch information
aliig and aeon0 authored Apr 20, 2022
1 parent 02aa9e3 commit bb8bf4c
Show file tree
Hide file tree
Showing 58 changed files with 91 additions and 26 deletions.
Binary file added assets/templates/a2_town/a2_town_26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_15.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_17.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_18.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_19.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/a3_town/a3_town_9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/eldritch/eldritch_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/templates/eldritch/eldritch_0_v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/templates/eldritch/eldritch_0_v3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/templates/eldritch/eldritch_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/templates/eldritch/eldritch_1_v2.png
Binary file modified assets/templates/eldritch/eldritch_2.png
Binary file added assets/templates/eldritch/eldritch_2_v2.png
Binary file modified assets/templates/eldritch/eldritch_3.png
Binary file modified assets/templates/eldritch/eldritch_4.png
Binary file modified assets/templates/eldritch/eldritch_5.png
Binary file modified assets/templates/eldritch/eldritch_6.png
Binary file modified assets/templates/eldritch/eldritch_7.png
Binary file added assets/templates/eldritch/eldritch_7_v2.png
Binary file modified assets/templates/eldritch/eldritch_8.png
Binary file added assets/templates/eldritch/eldritch_8_v2.png
Binary file modified assets/templates/eldritch/eldritch_9.png
Binary file modified assets/templates/eldritch/eldritch_start.png
Binary file added assets/templates/eldritch/eldritch_start_v2.png
Binary file modified assets/templates/shenk/shenk_12.png
Binary file modified assets/templates/shenk/shenk_13.png
Binary file modified assets/templates/shenk/shenk_17.png
Binary file modified assets/templates/shenk/shenk_v2_3.png
Binary file modified assets/templates/shenk/shenk_v2_6.png
Binary file added assets/templates/trav/trav_10_v2.png
Binary file modified assets/templates/trav/trav_11.png
Binary file modified assets/templates/trav/trav_20.png
Binary file added assets/templates/trav/trav_22_v2.png
Binary file modified assets/templates/trav/trav_23.png
Binary file modified assets/templates/trav/trav_27.png
Binary file modified assets/templates/trav/trav_4.png
Binary file modified assets/templates/trav/trav_5.png
Binary file modified assets/templates/trav/trav_7.png
Binary file added assets/templates/trav/trav_7_v2.png
Binary file modified assets/templates/trav/trav_8.png
22 changes: 11 additions & 11 deletions src/pather.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ def __init__(self):
103: {'PINDLE_3': (395, -75), 'PINDLE_4': (267, 52)},
104: {'PINDLE_4': (717, -117), 'PINDLE_3': (843, -244), 'PINDLE_5': (-187, 237), 'PINDLE_6': (-467, 89)},
# Eldritch
120: {'ELDRITCH_0': (293, 24), 'ELDRITCH_1': (-307, 76), 'ELDRITCH_5': (27, -164), 'ELDRITCH_6': (400, -50)},
121: {'ELDRITCH_6': (360, -244), 'ELDRITCH_1': (-329, -103), 'ELDRITCH_2': (411, 171), 'ELDRITCH_3': (-91, 198), 'ELDRITCH_7': (409, 180), 'ELDRITCH_8': (465, 345)},
122: {'ELDRITCH_2': (353, -145), 'ELDRITCH_3': (-149, -119), 'ELDRITCH_9': (-253, -118), 'ELDRITCH_7': (352, -134), 'ELDRITCH_8': (404, 29)},
123: {'ELDRITCH_3': (-99, -252), 'ELDRITCH_2': (403, -279), 'ELDRITCH_4': (-62, -109), 'ELDRITCH_9': (-204, -254), 'ELDRITCH_8': (400, -267), 'ELDRITCH_8': (454, -104)},
120: {'ELDRITCH_0': (293, 24), 'ELDRITCH_0_V2': (293, 24), 'ELDRITCH_0_V3': (293, 24), 'ELDRITCH_1': (-307, 76), 'ELDRITCH_1_V2': (-307, 76), 'ELDRITCH_5': (27, -164), 'ELDRITCH_6': (400, -50)},
121: {'ELDRITCH_6': (360, -244), 'ELDRITCH_1': (-329, -103), 'ELDRITCH_1_V2': (-329, -103), 'ELDRITCH_2': (411, 171), 'ELDRITCH_2_V2': (411, 171), 'ELDRITCH_3': (-91, 198), 'ELDRITCH_7': (409, 180), 'ELDRITCH_7_V2': (409, 180), 'ELDRITCH_8': (465, 345), 'ELDRITCH_8_V2': (465, 345)},
122: {'ELDRITCH_2': (353, -145), 'ELDRITCH_2_V2': (353, -145), 'ELDRITCH_3': (-149, -119), 'ELDRITCH_9': (-253, -118), 'ELDRITCH_7': (352, -134), 'ELDRITCH_7_V2': (352, -134), 'ELDRITCH_8': (404, 29), 'ELDRITCH_8_V2': (404, 29)},
123: {'ELDRITCH_3': (-99, -252), 'ELDRITCH_2': (403, -279), 'ELDRITCH_2_V2': (403, -279), 'ELDRITCH_4': (-62, -109), 'ELDRITCH_9': (-204, -254), 'ELDRITCH_8': (454, -104), 'ELDRITCH_8_V2': (454, -104)},
# Shenk
141: {'SHENK_0': (-129, 44), 'SHENK_1': (464, 107), 'SHENK_2': (-167, -34), 'SHENK_17': (-520, 528), 'SHENK_15': (77, 293), 'SHENK_18': (518, 512)},
142: {'SHENK_1': (584, 376), 'SHENK_4': (-443, -103), 'SHENK_2': (-52, 235), 'SHENK_3': (357, -129)},
Expand Down Expand Up @@ -161,10 +161,10 @@ def __init__(self):
# Trav
220: {"TRAV_0": (445, 384), "TRAV_20": (-259, 267), "TRAV_1": (-248, -139), "TRAV_2": (-682, 21), "TRAV_21": (25, 180)},
221: {"TRAV_2": (-153, -101), "TRAV_3": (-125, 201), "TRAV_20": (270, 145), "TRAV_1": (281, -261), "TRAV_4": (-459, 122)},
222: {"TRAV_5": (-218, 106), "TRAV_4": (120, 230), "TRAV_2": (426, 7), "TRAV_7": (-719, -166), "TRAV_1": (862, -153)},
223: {"TRAV_5": (344, 123), 'TRAV_4': (682, 247), "TRAV_8": (-353, -31), "TRAV_7": (-157, -149), "TRAV_22": (-368, -222), "TRAV_23": (-579, 116)},
224: {'TRAV_7': (411, -129), 'TRAV_27': (-363, 163), "TRAV_8": (214, -11), "TRAV_23": (-11, 136), "TRAV_10": (-130, -187), "TRAV_24": (-274, 15), "TRAV_22": (200, -202)},
225: {'TRAV_27': (96, 359), 'TRAV_8': (670, 187), 'TRAV_7': (867, 69), "TRAV_11": (10, 214), "TRAV_19": (-298, 539), "TRAV_24": (181, 213), "TRAV_12": (-408, -73), "TRAV_25": (-538, 132)},
222: {"TRAV_5": (-218, 106), "TRAV_4": (120, 230), "TRAV_2": (426, 7), "TRAV_7": (-719, -166), "TRAV_7_V2": (-719, -166), "TRAV_1": (862, -153)},
223: {"TRAV_5": (344, 123), 'TRAV_4': (682, 247), "TRAV_8": (-353, -31), "TRAV_7": (-157, -149), "TRAV_7_V2": (-157, -149), "TRAV_22": (-368, -222), "TRAV_22_V2": (-368, -222), "TRAV_23": (-579, 116)},
224: {'TRAV_7': (411, -129), 'TRAV_7_V2': (411, -129), 'TRAV_27': (-363, 163), "TRAV_8": (214, -11), "TRAV_23": (-11, 136), "TRAV_10": (-130, -187), "TRAV_10_V2": (-130, -187), "TRAV_24": (-274, 15), "TRAV_22": (200, -202), "TRAV_22_V2": (200, -202)},
225: {'TRAV_27': (96, 359), 'TRAV_8': (670, 187), 'TRAV_7': (867, 69), 'TRAV_7_V2': (867, 69), "TRAV_11": (10, 214), "TRAV_19": (-298, 539), "TRAV_24": (181, 213), "TRAV_12": (-408, -73), "TRAV_25": (-538, 132)},
226: {"TRAV_12": (-75, -172), "TRAV_25": (-205, 33), "TRAV_13": (-252, 195), "TRAV_11": (343, 115), "TRAV_18": (-514, 373), "TRAV_19": (35, 440), "TRAV_17": (-231, 242), "TRAV_27": (428, 263), "TRAV_29": (-929, 334), "TRAV_28": (-614, 338)},
227: {"TRAV_11": (65, -42), "TRAV_24": (236, -43), "TRAV_19": (-243, 283), 'TRAV_18': (-792, 216), "TRAV_12": (-356, -330), "TRAV_25": (-483, -124), 'TRAV_27': (154, 104)},
228: {"TRAV_13": (8, 9), "TRAV_17": (29, 56), "TRAV_25": (58, -152), "TRAV_16": (-198, -110), "TRAV_18": (-251, 188)},
Expand All @@ -182,7 +182,7 @@ def __init__(self):
404: {"A2_TOWN_14": (79, 190), "A2_TOWN_15": (244, -12), "A2_TOWN_13": (-270, 123), "A2_TOWN_11": (-258, -287), "A2_TOWN_12": (-599, -143)},
405: {"A2_TOWN_10": (65, -175), "A2_TOWN_17": (-108, 164), "A2_TOWN_16": (-304, -11), "A2_TOWN_9": (319, -68), "A2_TOWN_18": (-415, -284)},
406: {"A2_TOWN_18": (108, -143), "A2_TOWN_16": (219, 129), "A2_TOWN_19": (-293, 21), "A2_TOWN_17": (415, 304), "A2_TOWN_10": (588, -34)},
408: {"A2_TOWN_20": (-26, -109), "A2_TOWN_25": (-82, 278), "A2_TOWN_19": (344, 38), "A2_TOWN_18": (745, -125)},
408: {"A2_TOWN_20": (-26, -109), "A2_TOWN_25": (-82, 278), "A2_TOWN_19": (344, 38), "A2_TOWN_18": (745, -125), "A2_TOWN_26": (-831, -73)},
409: {"A2_TOWN_14": (477, 294), "A2_TOWN_13": (128, 226), "A2_TOWN_11": (140, -182), "A2_TOWN_12": (-201, -40)},
410: {"A2_TOWN_13": (416, 82), "A2_TOWN_12": (87, -184), "A2_TOWN_21": (-211, 10), "A2_TOWN_22": (-178, 269)},
411: {"A2_TOWN_22": (298, 0), "A2_TOWN_23": (0, 190), "A2_TOWN_21": (265, -260), "A2_TOWN_24": (-150, -185)},
Expand Down Expand Up @@ -732,7 +732,7 @@ def display_all_nodes(pather: Pather, filter: str = None):
cv2.circle(display_img, (x, y), 5, (0, 255, 0), 3)
cv2.putText(display_img, template_type, (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
wrt_origin = (-ref_pos_abs[0], -ref_pos_abs[1])
# print(f'"{template_type}": {wrt_origin}')
print(f'"{template_type}": {wrt_origin}')
# display_img = cv2.resize(display_img, None, fx=0.5, fy=0.5)
# if round(time.time() - start) % 3 == 0:
# cv2.imwrite("./info_screenshots/pather_" + time.strftime("%Y%m%d_%H%M%S") + ".png", display_img)
Expand All @@ -749,6 +749,6 @@ def display_all_nodes(pather: Pather, filter: str = None):
from char.hammerdin import Hammerdin
pather = Pather()

display_all_nodes(pather, "A2_TOWN")
display_all_nodes(pather, "ELDRITCH")

stop_detecting_window()
2 changes: 1 addition & 1 deletion src/run/shenk_eld.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def approach(self, start_loc: Location) -> Union[bool, Location, bool]:
def battle(self, do_shenk: bool, do_pre_buff: bool, game_stats) -> Union[bool, tuple[Location, bool]]:
# Eldritch
game_stats.update_location("Eld")
if not TemplateFinder().search_and_wait(["ELDRITCH_0", "ELDRITCH_START"], threshold=0.65, timeout=20).valid:
if not TemplateFinder().search_and_wait(["ELDRITCH_0", "ELDRITCH_0_V2", "ELDRITCH_0_V3", "ELDRITCH_START", "ELDRITCH_START_V2"], threshold=0.65, timeout=20).valid:
return False
if do_pre_buff:
self._char.pre_buff()
Expand Down
93 changes: 79 additions & 14 deletions src/template_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time
import os
from config import Config
from utils.misc import load_template, list_files_in_folder, alpha_to_mask, roi_center, color_filter
from utils.misc import cut_roi, load_template, list_files_in_folder, alpha_to_mask, roi_center, color_filter

template_finder_lock = threading.Lock()

Expand Down Expand Up @@ -244,29 +244,94 @@ def search_and_wait(
if __name__ == "__main__":
import keyboard
import os
from screen import start_detecting_window
start_detecting_window()
from screen import start_detecting_window, stop_detecting_window
from utils.misc import wait
from template_finder import TemplateFinder
keyboard.add_hotkey('f12', lambda: Logger.info('Force Exit (f12)') or os._exit(1))
print("Move to d2r window and press f11")
start_detecting_window()
wait(0.1)

print("\n== Hotkeys ==")
print("F11: Start")
print("F12: Exit")
print("Down arrow: decrease template matching threshold")
print("Up arrow: increase template matching threshold")
print("Left arrow: decrease template index")
print("Right arrow: increase template index")
print("F9: toggle all vs. individual template(s)")
print("F10: save visible template(s)")

keyboard.add_hotkey('f12', lambda: Logger.info('Force Exit (f12)') or stop_detecting_window() or os._exit(1))
keyboard.wait("f11")

search_templates = ["CORPSE", "CORPSE_BARB", "CORPSE_DRU", "CORPSE_NEC", "CORPSE_PAL", "CORPSE_SIN", "CORPSE_SORC", "CORPSE_ZON"]
# enter the template names you are trying to detect here
_template_list = ["TRAV_0","TRAV_1","TRAV_3","TRAV_4","TRAV_5","TRAV_7","TRAV_8","TRAV_10","TRAV_11","TRAV_12","TRAV_13","TRAV_16","TRAV_17","TRAV_18","TRAV_19","TRAV_2","TRAV_20","TRAV_21","TRAV_22","TRAV_23","TRAV_24","TRAV_25","TRAV_27","TRAV_28","TRAV_29","TRAV_V2_0","TRAV_V2_1","TRAV_V2_2","TRAV_V2_3","TRAV_V2_4","TRAV_V3_0","TRAV_V3_1","TRAV_V3_11","TRAV_V3_3","TRAV_V3_4","TRAV_V3_5","TRAV_V3_6","TRAV_V3_7","TRAV_V3_8"]

_current_template_idx = -1
_last_stored_idx = 0
_current_threshold = 0.5
_visible_templates = []

def _save_visible_templates():
if not os.path.exists("info_screenshots"):
os.system("mkdir info_screenshots")
for match in _visible_templates:
cv2.imwrite(match['filename'], match['img'])
Logger.info(f"{match['filename']} saved")

def _toggle_all_templates():
global _current_template_idx
_current_template_idx = -1 if _current_template_idx != -1 else _last_stored_idx
if _current_template_idx == -1:
Logger.info(f"Searching for templates: {_template_list}")
else:
Logger.info(f"Searching for template: {_template_list[_current_template_idx]}")

def _incr_template_idx(direction: int = 1):
global _current_template_idx, _last_stored_idx
if \
(-1 < _current_template_idx < len(_template_list) - 1) or \
(_current_template_idx == -1 and direction > 0) or \
(_current_template_idx == len(_template_list) - 1 and direction < 0):
_current_template_idx += direction
_last_stored_idx = _current_template_idx
if _current_template_idx == -1:
Logger.info(f"Searching for templates: {_template_list}")
else:
Logger.info(f"Searching for template: {_template_list[_current_template_idx]}")

def _incr_threshold(direction: int = 1):
global _current_threshold
if (_current_threshold < 1 and direction > 0) or (_current_threshold > 0 and direction < 0):
_current_threshold = round(_current_threshold + direction, 2)
Logger.info(f"_current_threshold = {_current_threshold}")

keyboard.add_hotkey('down', lambda: _incr_threshold(-0.05))
keyboard.add_hotkey('up', lambda: _incr_threshold(0.05))
keyboard.add_hotkey('left', lambda: _incr_template_idx(-1))
keyboard.add_hotkey('right', lambda: _incr_template_idx(1))
keyboard.add_hotkey('f9', lambda: _toggle_all_templates())
keyboard.add_hotkey('f10', lambda: _save_visible_templates())

while 1:
# img = cv2.imread("")
_visible_templates = []
img = grab()
display_img = img.copy()
start = time.time()
for key in search_templates:
template_match = TemplateFinder().search(key, img, best_match=True, threshold=0.5, use_grayscale=True)
if _current_template_idx < 0:
templates = _template_list
else:
templates = [_template_list[_current_template_idx]]
for key in templates:
template_match = TemplateFinder().search(key, img, threshold=_current_threshold)
if template_match.valid:
x, y = template_match.center
cv2.putText(display_img, str(template_match.name), template_match.center, cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
cv2.circle(display_img, template_match.center, 7, (255, 0, 0), thickness=5)
cv2.rectangle(display_img, template_match.region[:2], (template_match.region[0] + template_match.region[2], template_match.region[1] + template_match.region[3]), (0, 0, 255), 1)
print(f"Name: {template_match.name} Pos: {template_match.center}, Dist: {625-x, 360-y}, Score: {template_match.score}")

# print(time.time() - start)
# display_img = cv2.resize(display_img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)
match = {
'filename': f"./info_screenshots/{key.lower()}.png",
'img': cut_roi(img, template_match.region)
}
_visible_templates.append(match)
cv2.imshow('test', display_img)
key = cv2.waitKey(1)
key = cv2.waitKey(3000)

0 comments on commit bb8bf4c

Please sign in to comment.