Skip to main content
Glama
README.md
# HOI4 Background Co-op

**Experimental Windows adapter for an AI agent to play its own HOI4 client alongside a human.**

[中文说明](README.zh-CN.md) · [Validation](docs/VALIDATION.md) · [Architecture](docs/ARCHITECTURE.md)

The human uses a foreground client. The agent uses a separate game copy on a
private Windows desktop, exposed through MCP. Inputs are posted to that bound
window; the adapter does not move the real cursor or activate the foreground.

This is an input/isolation and local-multiplayer integration project. It is not
a complete HOI4 strategy bot, a new game engine, or a replacement for
[HOI4-Live-Tools](https://github.com/klimPaskov/HOI4-Live-Tools).

## Status: v0.1.0, LAN prototype

| Capability | Evidence |
| --- | --- |
| Same-machine human and agent clients | Verified in HOI4 1.19.5, vanilla, checksum `c184` |
| Agent runs on a private desktop | Verified in the original adapter |
| Private-room entry with distinct player seats | Verified: human Brazil, agent Germany |
| Advancing synchronized game dates | Verified from Jan 1 to Jan 2, 1936 |
| Game-reported peer latency | Two observed samples: 59 and 127 ms; **not model decision latency** |
| Complete autonomous campaign / defeating the USSR | **Not demonstrated** |
| Joining normal Steam public/friend rooms without another account | **Not supported by the local compatibility setup** |
| Other game versions, platforms, overhaul mods | Not qualified |

The original integrated prototype was tested with DSH. This extracted package
has offline packaging/protocol checks; a clean-machine end-to-end game run remains
to be done. See the distinction in [VALIDATION.md](docs/VALIDATION.md).

## What is included

- A small GUI-only MCP server: status, launch, close, OCR/image view, click, drag,
  scroll, text input, named menu keys, and short action batches.
- Exact executable-path binding and private-desktop window selection.
- Preparation scripts for separate agent/host userdata and game copies.
- Source-only steamemu compatibility patches used for the local LAN experiment:
  a valid lobby instance flag and the legacy game-server initialization export.
- Generic agent instructions for observed actions and compact session handoffs.

No game files, Steamworks SDK, compatibility DLLs, model credentials, account
configuration, chat history, saves, or private memory are included.

## Requirements

- Windows x64, an unlocked interactive user session, Python 3.11+.
- Your own working HOI4 installation; both clients must have matching versions,
  mods and checksum. This release was qualified only on 1.19.5 (`c184`).
- Enough RAM/commit capacity to run two game instances.
- An MCP host (DSH was used in the original experiment) and your chosen model.
- For the tested no-second-account local setup: a separately obtained/built
  steamemu library. Read [patches/README.md](patches/README.md) and upstream terms.
  This project does not bundle or install that dependency automatically.

## Install

In PowerShell, from the cloned repository:

```powershell
git clone https://github.com/F15EXII/hoi4-background-coop.git
cd hoi4-background-coop
python -m venv .venv
.\.venv\Scripts\python.exe -m pip install ".[ocr]"
```

For image-only operation, install `.` without the OCR extra. OCR uses the optional
`rapidocr-onnxruntime` dependency. The MCP transport uses the official Python SDK.

## Prepare clients

After building the external compatibility DLL, prepare a **new** runtime directory:

```powershell
.\scripts\prepare-clients.ps1 `
  -GameDirectory 'C:\path\to\Hearts of Iron IV' `
  -OutputDirectory 'C:\hoi4-coop' `
  -CompatibilityDll 'C:\path\to\built\steam_api64.dll'
```

The script copies root game files, links asset directories with junctions, writes
separate userdata paths, and generates two distinct local test identities. It
refuses an existing output directory and does not launch either game. Linked
assets depend on the original install; recreate the copies after game updates.
Do not edit game assets through those junctions.

Omitting `-CompatibilityDll` prepares isolated copies only; it does **not** make
ordinary Steam clients acquire independent multiplayer identities.

Configure the agent process and inspect status:

```powershell
$env:HOI4_COOP_CONFIG = 'C:\hoi4-coop\agent-config.json'
.\.venv\Scripts\python.exe -m hoi4_background_coop status
.\.venv\Scripts\python.exe -m hoi4_background_coop launch
```

Open `C:\hoi4-coop\host\game\hoi4.exe` yourself. For this setup, do not substitute
a normally launched Steam client for one of the matching prepared clients.

## Connect an MCP host

Adapt [examples/mcp.config.json](examples/mcp.config.json) to your Python and
configuration paths. The stdio command is:

```text
python -m hoi4_background_coop serve
```

The human creates a room and supplies the game's room ID. The agent joins using
its own UI, selects its country, and readies up. Confirm two players, countries,
then advancing synchronized dates. A nonzero ID alone is not an acceptance test.

The default OCR view returns text and coordinates in the same scaled image space
used by click/drag/scroll (at most 1100 x 680). `view(format="image")` is available
when OCR is insufficient. Stable observed paths can use `sequence` to reduce
model round trips. A tool's submitted receipt never proves game acceptance.

Use [the example agent instructions](examples/agent-instructions.md). Strategic
planning, cost limits, memory, session rotation and continuation belong to your
agent host. The original DSH-specific QQ supervisor is not bundled: it depended
on private application APIs and personal configuration.

## Limitations and next steps

Private desktops separate input surfaces; they are **not a security sandbox**.
The agent process runs as your Windows user. Only connect a trusted agent host.

The game still consumes CPU/GPU resources. The input technique depends on HOI4's
window behavior and may break with DPI, layout or version changes. Posted menu
keys may have no effect; observe and use the visible UI button when needed.
Multiplayer pause/speed permissions remain the game's own rules.

The main unsolved performance problem is agent perception and decision latency.
Low network latency does not make repeated OCR/model/click cycles fast enough
for an active front. The next useful work is broader live qualification and
integration with existing semantic game-action tools, not a larger untested catalog.

## Development

```powershell
.\.venv\Scripts\python.exe -m unittest discover -s tests -v
```

Tests do not launch HOI4, send live input, or prove campaign ability.
Contributions should state what was tested offline versus in the actual game.
See [NOTICE.md](NOTICE.md) for third-party scope and [LICENSE](LICENSE) for original code.