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
This commit is contained in:
alex
2026-06-01 11:07:53 +02:00
parent cb3d49fa22
commit 30bf163ee3
2 changed files with 17 additions and 10 deletions
+2 -2
View File
@@ -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
+15 -8
View File
@@ -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