from PIL import Image

def find_reachable_atlas_nodes(img_path: str):
    img = Image.open(img_path)
    w, h = img.size
    
    # Exclude UI areas: top header y < 150, bottom bar y > 1300, right area if inventory open
    green_pts = []
    for y in range(150, int(h * 0.9), 2):
        for x in range(int(w * 0.05), int(w * 0.7), 2):
            r, g, b = img.getpixel((x, y))[:3]
            # Intense green glow: g high, r and b much lower
            if g > 140 and g > r + 35 and g > b + 35:
                green_pts.append((x, y))
    
    clusters = []
    for pt in green_pts:
        matched = False
        for c in clusters:
            if abs(pt[0] - c["cx"]) < 30 and abs(pt[1] - c["cy"]) < 30:
                c["pts"].append(pt)
                c["cx"] = sum(p[0] for p in c["pts"]) // len(c["pts"])
                c["cy"] = sum(p[1] for p in c["pts"]) // len(c["pts"])
                matched = True
                break
        if not matched:
            clusters.append({"cx": pt[0], "cy": pt[1], "pts": [pt]})
    
    # Keep clusters with at least 15 pixels
    valid = [c for c in clusters if len(c["pts"]) >= 15]
    valid.sort(key=lambda c: len(c["pts"]), reverse=True)
    return valid

for test_img in [
    "captures/20260914_002410_835_U_THEN_ENDGAME_LOADED.png",
    "captures/20260914_002804_111_STEP2_INVENTORY_ON_ATLAS.png"
]:
    clusters = find_reachable_atlas_nodes(test_img)
    print(f"=== {test_img} ===")
    for idx, c in enumerate(clusters):
        print(f"  Node {idx}: ({c['cx']}, {c['cy']}) [pixels={len(c['pts'])}]")
