burnysc2 (python-sc2)

repository·develop·Indexed 20 days ago

https://github.com/burnysc2/python-sc2

A StarCraft II API client for Python 3 (version 7.3.0) designed for writing AI bots. It provides high and low-level abstractions for interacting with the StarCraft II scripted interface, including modules for game state management, game data (units, abilities, upgrades), spatial data (Point2, Point3, Rect), and protocol handling. The library supports deployment to AI Arena, Sc2AI, and Probots, and includes configurations for Linux and WSL environments.

Tokens
11K
Snippets
38
Records
67
Agent score
71%

What's inside burnysc2

  1. Use sc2.position classes for spatial data

    develop

    The sc2.position module provides several classes for representing spatial data in StarCraft II, including points, sizes, and rectangular areas. These classes are used to define coordinates, dimensions, and regions for unit movement, selection, and area effects.

    Available classes:

    • Pointlike: A base class for point-based objects.
    • Point2: Represents a 2D point (x, y).
    • Point3: Represents a 3D point (x, y, z).
    • Size: Represents a 2D dimension (width, height).
    • Rect: Represents a rectangular area defined by its position and size.
  2. Organize StarCraft II maps

    develop

    Map organization depends on the type of map being used:

    • Official Blizzard maps: Extract into subdirectories within the SC2 maps directory (e.g., install-dir/Maps/Ladder2017Season1/).
    • Bot ladder maps (SC2 AI Arena): Extract into the root of the SC2 maps directory (e.g., install-dir/Maps/AcropolisLE.SC2Map) to ensure replays work correctly.
  3. Install burnysc2 via pip

    develop

    Install the library using pip. Ensure Python 3.9 or newer is installed and that the StarCraft II client is located in the default installation path: C:\Program Files (x86)\StarCraft II.

    To install the stable version:

    pip install burnysc2

    If you need to install a specific branch (e.g., the develop branch) directly from GitHub:

    pip install --upgrade --force-reinstall https://github.com/BurnySc2/python-sc2/archive/develop.zip
    pip install burnysc2
  4. Prepare your bot for distribution

    develop

    To ensure your bot uses the correct version of the python-sc2 library and avoids conflicts with outdated system-wide installations, you must bundle the library directly with your bot code.

    1. Copy the python-sc2/sc2 folder into your bot's root directory.
    2. Update your bot's race configuration in run.py (line 8) and ladderbots.json (line 4).
    3. Zip the entire contents of your folder into a <YOUR_BOTS_NAME_HERE>.zip file. Ensure all files are located at the root of the zip archive.
    # Example of the required directory structure before zipping:
    my_bot_folder/
    ├── sc2/             # Copied from python-sc2/sc2
    ├── run.py
    ├── ladderbots.json
    └── requirements.txt
  5. Set up a StarCraft II bot environment using Docker

    develop

    You can use Docker to run multiple python-sc2 bots against each other using the Linux binary SC2 client. This approach is headless and does not require a GPU.

    Requirements

    • Docker installed and running
    • Internet access

    1. Pull the Docker image

    Pull the specific image containing the SC2 Linux binary and Python 3.10 environment:

    docker pull burnysc2/python-sc2-docker:release-python_3.10-sc2_4.10_arenaclient_burny

    2. Launch the container

    Launch a new container in interactive mode (which prevents it from shutting down immediately) and name it app:

    docker run -it -d --name app burnysc2/python-sc2-docker:release-python_3.10-sc2_4.10_arenaclient_burny

    3. Install bot dependencies

    Install burnysc2 and any other required libraries inside the running container using uv:

    docker exec -i app uv add "burnysc2>=0.12.12"

    Note: If you want to play against a Windows-compiled bot (.exe), you must manually install wine inside the container, as it is not included in the base image.

    docker pull burnysc2/python-sc2-docker:release-python_3.10-sc2_4.10_arenaclient_burny
    docker run -it -d --name app burnysc2/python-sc2-docker:release-python_3.10-sc2_4.10_arenaclient_burny
  6. Configure StarCraft II on Linux

    develop

    To run StarCraft II on Linux (via Wine or Lutris), you must set specific environment variables. If using Lutris, the default installation path is /home/burny/Games/battlenet/drive_c/Program Files (x86)/StarCraft II/.

    SC2PF=WineLinux
    WINE=/usr/bin/wine
    # Or a wine binary from lutris:
    WINE=/home/burny/.local/share/lutris/runners/wine/lutris-4.20-x86_64/bin/wine64
    # Default Lutris StarCraftII Installation path:
    SC2PATH='/home/burny/Games/battlenet/drive_c/Program Files (x86)/StarCraft II/'
  7. Run matches and retrieve results from Docker

    develop

    Running Matches

    Execute the matches in headless mode using the runner script inside the container:

    docker exec -i app uv run python /root/aiarena-client/arenaclient/run_local.py

    Retrieving Results and Replays

    After the matches finish, copy the data from the container to your host machine for analysis.

    Copy results.json:

    mkdir -p temp
    docker cp app:/root/aiarena-client/arenaclient/proxy/results.json temp/results.json

    Copy all replays:

    mkdir -p temp/replays
    docker cp app:/root/StarCraftII/Replays/. temp/replays
  8. Build structures manually or with convenience methods

    develop

    Building a structure requires a building type, affordability check, a worker, and a valid position.

    Manual Approach

    1. Check affordability: self.can_afford(UnitTypeId.BUILDING).
    2. Check if already building/built: Use self.already_pending(UnitTypeId.BUILDING) and check self.structures for ready units.
    3. Find a worker: Filter self.workers for those that are is_idle or is_collecting and not in self.unit_tags_received_action.
    4. Find a position: Use await self.find_placement(UnitTypeId.BUILDING, near=position) to find a valid spot near a target.
    5. Issue command: worker.build(UnitTypeId.BUILDING, placement_position).

    Convenience Approach

    Use await self.build(UnitTypeId.BUILDING, near=position) to automatically handle worker selection and placement logic.

    # Manual build pattern
    if self.can_afford(UnitTypeId.SPAWNINGPOOL) and \
       self.already_pending(UnitTypeId.SPAWNINGPOOL) + \
       self.structures.filter(lambda s: s.type_id == UnitTypeId.SPAWNINGPOOL and s.is_ready).amount == 0:
        
        worker_candidates = self.workers.filter(lambda w: (w.is_collecting or w.is_idle) and w.tag not in self.unit_tags_received_action)
        
        if worker_candidates:
            map_center = self.game_info.map_center
            pos_towards_center = self.start_location.towards(map_center, distance=5)
            placement_position = await self.find_placement(UnitTypeId.SPAWNINGPOOL, near=pos_towards_center, placement_step=1)
            
            if placement_position:
                build_worker = worker_candidates.closest_to(placement_position)
                build_worker.build(UnitTypeId.SPAWNINGPOOL, placement_position)
    
    # Convenience build pattern
    await self.build(UnitTypeId.SPAWNINGPOOL, near=pos_towards_center, placement_step=1)
  9. Configure StarCraft II on WSL

    develop

    The library detects WSL and defaults to starting the Windows version of StarCraft II.

    To force the Linux version, set SC2_WSL_DETECT to "0" in your Python code.

    For WSL version 2, you must provide the following environment variables to allow the bot to connect to the Windows host:

    • SC2CLIENTHOST: Your Windows IP (find via ipconfig /all in PowerShell).
    • SC2SERVERHOST: 0.0.0.0.
    import os
    # Disable automatic Windows SC2 detection in WSL
    os.environ["SC2_WSL_DETECT"] = "0"
    # Required for WSL2 connectivity
    export SC2CLIENTHOST=<your windows IP>
    export SC2SERVERHOST=0.0.0.0
  10. Copy bots and runner configuration to the Docker container

    develop

    To run your bots, you must move your local bot code and a customized runner script into the container.

    Copying Bot Code

    Bots must be located at app:/root/StarCraftII/Bots/<bot_name> inside the container.

    Important: You must also copy the sc2 folder from your local python-sc2 directory to ensure the container uses the correct version of the library.

    Example command to copy a competitive bot:

    docker cp examples/competetive/. app:/root/StarCraftII/Bots/my_bot

    Configuring the Runner

    1. Customize your local runner script (e.g., custom_run_local.py) by modifying the def main() function.
    2. (Optional) Customize the arenaclient settings (like max game time) located at /root/aiarena-client/arenaclient/proxy/settings.json.
    3. Copy your customized runner to the container's expected path:
    docker cp bat_files/docker/custom_run_local.py app:/root/aiarena-client/arenaclient/run_local.py
    docker cp examples/competetive/. app:/root/StarCraftII/Bots/my_bot
    docker cp bat_files/docker/custom_run_local.py app:/root/aiarena-client/arenaclient/run_local.py