From 30bf163ee36c73af704abc68603c91e397dec6db Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 1 Jun 2026 11:07:53 +0200 Subject: [PATCH] Fix move_d2r_window to position client area, not outer window - src/utils/misc.py: move_d2r_window now calculates outer window position to place client area at target coords (was incorrectly setting outer window to target, shifting client area unpredictably) - src/bot.py: Call move_d2r_window(5,98) at game start to restore original working client area position and prevent 13px offset drift --- src/bot.py | 4 ++-- src/utils/misc.py | 23 +++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/bot.py b/src/bot.py index 15133c9..f750bdf 100644 --- a/src/bot.py +++ b/src/bot.py @@ -261,9 +261,9 @@ class Bot: def on_init(self): self._game_stats.log_start_game() keyboard.release(Config().char["stand_still"]) - # Ensure D2R window is at a stable position to prevent template matching drift + # Force D2R client area to stable position to prevent offset drift from utils.misc import move_d2r_window - move_d2r_window(0, 0) + move_d2r_window(5, 98) wait(0.3) # If we're only doing run_level and character is already in-game, skip # the town detection and go straight to the run diff --git a/src/utils/misc.py b/src/utils/misc.py index 2f379c8..d8773c3 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -111,8 +111,8 @@ def find_d2r_window(spec: WindowSpec, offset = (0, 0)) -> tuple[int, int]: return (left + offset_x, top + offset_y) return None -def move_d2r_window(x, y): - """Move D2R window to a specific screen position to prevent offset drift.""" +def move_d2r_window(client_x, client_y): + """Move D2R window so the client area is at (client_x, client_y) to prevent offset drift.""" if os.name == 'nt': import ctypes from ctypes import wintypes @@ -122,12 +122,19 @@ def move_d2r_window(x, y): EnumWindows(lambda w, l: l.append((w, GetWindowText(w))), window_list) for w in window_list: if "Diablo II" in w[1]: - cr = wintypes.RECT() - user32.GetClientRect(w[0], ctypes.byref(cr)) - w_width = cr.right - w_height = cr.bottom - SetWindowPos(w[0], HWND_TOPMOST, x, y, w_width, w_height, SWP_SHOWWINDOW) - Logger.debug(f"Moved D2R window to ({x}, {y})") + # Get current client area position + tl = wintypes.POINT() + user32.ClientToScreen(w[0], ctypes.byref(tl)) + # Get window dimensions + wr = wintypes.RECT() + user32.GetWindowRect(w[0], ctypes.byref(wr)) + w_width = wr.right - wr.left + w_height = wr.bottom - wr.top + # Calculate outer window position to place client area at target + outer_x = client_x - (tl.x - wr.left) + outer_y = client_y - (tl.y - wr.top) + SetWindowPos(w[0], HWND_TOPMOST, outer_x, outer_y, w_width, w_height, SWP_SHOWWINDOW) + Logger.debug(f"Moved D2R client area to ({client_x}, {client_y})") return True return False