Opponent avoidance scoring

From wenmin-wu/ds-skills · View interactive version

Opponent avoidance scoring is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Score candidate movement directions by average distance to nearby opponents and pick the safest path for ball-carrying agents in game AI

How to use this skill

Skill file

Opponent Avoidance Scoring

Overview

When an agent controls the ball in a game AI, choosing which direction to advance requires evaluating opponent density ahead. Score each candidate direction (right, top-right, bottom-right) by the average distance to opponents in that sector. Pick the direction with the greatest average distance — or trigger a long pass if all directions are blocked (too many opponents nearby).

Quick Start

import numpy as np

def avg_distance_to_opponents(obs, target_x, target_y):
    opponents = obs["right_team"]
    dists = [np.sqrt((ox - target_x)**2 + (oy - target_y)**2) for ox, oy in opponents]
    nearby = [d for d in dists if d < 0.05]
    return np.mean(nearby) if nearby else 2.0, len(nearby)

def pick_direction(obs, player_x, player_y):
    directions = {
        "right": (player_x + 0.01, player_y),
        "top_right": (player_x + 0.01, player_y - 0.01),
        "bottom_right": (player_x + 0.01, player_y + 0.01),
    }
    best_dir, best_dist = "right", 0
    total_nearby = 0
    for name, (tx, ty) in directions.items():
        dist, count = avg_distance_to_opponents(obs, tx, ty)
        total_nearby += count
        if dist > best_dist:
            best_dist = dist
            best_dir = name
    if total_nearby >= 3:
        return "high_pass"  # surrounded — clear the ball
    return best_dir

Workflow

  1. Define candidate directions as small offsets from current position
  2. For each direction, compute average distance to opponents within a proximity radius
  3. Pick the direction with the greatest average distance (safest path)
  4. If total nearby opponents across all directions exceeds a threshold, trigger a pass/clearance
  5. Add field boundary constraints (avoid going out of bounds near sidelines)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.