import time
import ctypes
import struct

user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

hwinsta = user32.OpenWindowStationW("WinSta0", False, 0x10000000)
if hwinsta: user32.SetProcessWindowStation(hwinsta)
hdesk = user32.OpenDesktopW("Default", 0, False, 0x10000000)
if hdesk: user32.SetThreadDesktop(hdesk)

hwnd = user32.FindWindowW("POEWindowClass", None)
user32.ShowWindow(hwnd, 9)
user32.SetForegroundWindow(hwnd)
user32.BringWindowToTop(hwnd)
time.sleep(0.2)

# Focus click
user32.SetCursorPos(1280, 5)
user32.mouse_event(0x0002, 0, 0, 0, 0)
time.sleep(0.02)
user32.mouse_event(0x0004, 0, 0, 0, 0)
time.sleep(1.0) # Wait 1.0s for attack animation to finish

pid = 43084
h = kernel32.OpenProcess(0x0010, False, pid)
player_ptr = 0x2AF791A0010

size = 0x1500
buf0 = ctypes.create_string_buffer(size)
br = ctypes.c_size_t()
kernel32.ReadProcessMemory(h, ctypes.c_void_p(player_ptr), buf0, size, ctypes.byref(br))

# Aim cursor towards North-West (stream path back up) (1000, 400)
user32.SetCursorPos(1000, 400)
time.sleep(0.05)

# Hold 'W' + 'A' for 1.5 seconds
print("Holding 'W' + 'A' for 1500ms...")
user32.keybd_event(0x57, 0x11, 0, 0) # W down
user32.keybd_event(0x41, 0x1E, 0, 0) # A down
time.sleep(1.5)
user32.keybd_event(0x57, 0x11, 2, 0) # W up
user32.keybd_event(0x41, 0x1E, 2, 0) # A up
print("Released 'W' + 'A'")

time.sleep(0.5)

buf1 = ctypes.create_string_buffer(size)
kernel32.ReadProcessMemory(h, ctypes.c_void_p(player_ptr), buf1, size, ctypes.byref(br))
kernel32.CloseHandle(h)

d0 = buf0.raw
d1 = buf1.raw

print("\n--- Comparing 4-byte floats that changed in PlayerEntity [0x0 .. 0x1500] ---")
found = 0
for off in range(0, size - 12, 4):
    f0_x, f0_y, f0_z = struct.unpack('<fff', d0[off:off+12])
    f1_x, f1_y, f1_z = struct.unpack('<fff', d1[off:off+12])
    if f0_x != f1_x or f0_y != f1_y:
        # Check if they look like coordinates (-5000 to 5000)
        if -5000 < f0_x < 5000 and -5000 < f0_y < 5000 and -5000 < f1_x < 5000 and -5000 < f1_y < 5000:
            if abs(f0_x) > 1.0 or abs(f0_y) > 1.0:
                print(f"Offset +0x{off:03X}: Before=({f0_x:.2f}, {f0_y:.2f}, {f0_z:.2f}) -> After=({f1_x:.2f}, {f1_y:.2f}, {f1_z:.2f}) [Diff: dX={f1_x-f0_x:.2f}, dY={f1_y-f0_y:.2f}]")
                found += 1

print(f"Total coordinate-like float changes found: {found}")

import sys
sys.path.insert(0, r"C:\Projects\AutoPOE2")
from tools.autonomous_map_runner import ScreenCapturerGDI
c = ScreenCapturerGDI()
c.capture(r"C:\Projects\AutoPOE2\captures\test_diff_movement.png")
print("Captured test_diff_movement.png")
