Files
donnyandClaude Opus 5.5 c034354d12
CI / build (push) Successful in 57s
feat(decisions): host default, first brain, every choice through the seam; v0.2.0
BotHost.Decisions is the default decision maker for seats without a Decisions= token (builtin overrides it).
'first' answers from the request's JSON alone, sums and counters included. MSG_SORT_CHAIN, the field-first card
prompt and the effect prompt of an unknown card now ask the maker too (built-in answers unchanged); only
prompts with no choice are answered without asking, and seats with a maker log their counts per duel.

Tests: the built-in --seed 7 report equals the v0.1.0 release's (tests/fixture/expected, clocks aside); first vs
first, 100 duels: 15952 prompts asked, 0 handed back, 0 errors, unasked only 'chain: nothing to chain'.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 18:38:00 -07:00

163 lines
12 KiB
Markdown

# yugioh-legacy-bots
The bots of Yu-Gi-Oh! Legacy, as one library both the game and the server take as a release, the way both take
the engine (`donny/yugioh-legacy-engine`): WindBot, the parts of YGOSharp it uses, the SmartExecutor brain, the
in-process duel host, the character registry and the roster (`YGOLegacy.Bots.dll`), and the headless Bot Arena
on top of it (`BotArena`, Linux and Windows). The game plays these bots in solo duels; the server runs the arena.
The code came from the Unity repo (`donny/yugioh-legacy-unity`, branch `dev`, commit
`19c6158197a0a8f0817dd0924bb9a6a987c2bfc5`); the first commit here is that code, verbatim, in this layout.
## Layout
| Path | What |
|---|---|
| `src/YGOLegacy.Bots/` | The library: .NET Standard 2.1, C# 9, no Unity reference (Unity compiles against it). `WindBot/`, `YGOSharp/` (the client, the card managers, the enums), `Bot/` (SmartExecutor, BoardEvaluator, BrainRules, EffectSimulator, BattlePredictor, DuelStateBus, LocalDuelHost, LoopbackBotSeat, Lookahead, ArenaReport), `Characters/`, `Services/BotRoster.cs`, `Host/BotHost.cs` (the contract with the host) and `Decisions/` (the decision seam). |
| `src/BotArena/` | The headless arena, .NET 8, `linux-x64` and `win-x64`. Its own `README.md` has the options, the report and how it works. |
| `tests/YGOLegacy.Bots.Tests/` | NUnit: the library through its contract, and the Linux arena end to end. |
| `tests/fixture/` | `game/`, the smallest game root the tests duel on, and `make-fixture.py`, which builds it (see its `README.md`). |
| `engine.json` | The pin: the engine release this repo builds and tests against (that release's `engine.json`, verbatim). |
| `build/fetch-engine.sh` | Downloads the pinned release into `.engine/` and refuses one whose `engine.json` is not the pin. |
| `Dockerfile` | Every build and test. There is no `dotnet` on the build hosts. |
| `.gitea/workflows/ci.yml` | CI on every push; the release on a `vX.Y.Z` tag. |
| `CHANGELOG.md` | One section per release; it is the release's body. |
## The contract: what the game implements
The library reaches the program that runs it only through `YGOLegacy.Bots.BotHost`. The host sets it once at
start-up, before the first bot is built and before the character registry loads. Every path the bots read is
relative to the current directory, which must be the game root (`Data/locales/<locale>/cards.cdb`,
`Expansions/*.cdb`, `Data/Windbot/Decks/`, `Deck/`, `Data/Characters/`).
| `BotHost.` | Interface | The game (YGO-156) | The arena (`src/BotArena/ArenaHost.cs`) |
|---|---|---|---|
| `Log` | `IBotLog`: `Info`, `Warning`, `Error` | `UnityEngine.Debug.Log` / `LogWarning` / `LogError` | stderr, into the worker log |
| `Cards` | `ICardDatabase`: `IDbConnection Connect(string path)`, not yet opened | `new Mono.Data.Sqlite.SqliteConnection("Data Source=" + path)` | `Microsoft.Data.Sqlite`, read-only, shared |
| `Files` | `IGameFiles`: `Locale`, `ExpansionCardDatabases()` | `Language.GetConfig()`, `ResourceManager.GetAllCdbInZipPaths()` | `--locale`; the `.cdb` it extracts from `Expansions/*.ypk` |
| `DeckLimits` | `IDeckLimits`: `DeckCaps For(string rulesetId)` (the deck file's `#ruleset`, or null) | `DeckLimits.For(id)` → `new DeckCaps(r.MainMax, r.ExtraLimit(), r.SideMax)`, read at each call | the default, 60 / 99 / 15 |
| `BotConf` | `IBotConf`: `Entries()` (`BotConfEntry` name, command, desc, flags), `Reload()` | `SoloLoader.Catalog` (initialised if not ready), `SoloLoader.Reload()` | `Data/locales/<lang>/bot.conf` |
Defaults when a host sets nothing: `Log` the console, `Files` en-US with no expansion databases, `DeckLimits`
60 / 99 / 15, `BotConf` empty (the roster then uses its built-in Dovphin command). `Cards` has no default: loading
the card databases without it throws.
Two more things the game does on its side:
- **Characters.** `CharacterRegistry` is in the library without its UniTask surface. The game keeps its own
`Init()` / `Loaded`: `CharacterRegistry.Scan(CharacterRegistry.Root)` on the thread pool, then
`CharacterRegistry.Publish(scan)` on the main thread (exactly what `Init` did). Hosts without a player loop call
`CharacterRegistry.LoadFromDisk()`.
- **Internals.** The game calls internal members of WindBot (`WindBot.Program.YGOL_Main`, ...). The library keeps
them internal and lets `Assembly-CSharp`, `Assembly-CSharp-Editor`, `BotArena` and the tests see them
(`Properties/AssemblyInfo.cs`).
Namespaces are the ones the game already uses (`WindBot.*`, `YGOSharp.*`, `YGOLegacy.Bot`, `YGOLegacy.Characters`,
`YGOLegacy.Services` for `BotRoster` / `BotDef`), so the game's callers do not change. What stays in the game:
`GamePaths`, `DeckLimits` / `RulesetDeck` / `DeckPool` and the rulesets, `SoloLoader`, `CharacterLoader`,
`VoiceLineStore`, the in-editor arena (`Core/Bot/BotArena.cs`) and YGOSharp's server side (`YGOSharp/YGOSharp/*`,
`Api.cs`, `Duel.cs`, `MtRandom.cs`, the `Async*` and `NetworkServer` files).
The game references `YGOLegacy.Bots.dll` beside the engine's `YGOLegacy.Engine.dll` (same release as
`bots.json`'s `engine`) and its own Newtonsoft.Json (Unity's `com.unity.nuget.newtonsoft-json`, 13.x). Neither is
in `bots-lib.zip`.
## The decision seam: who answers a seat's prompts
One bot plays the same everywhere a duel runs: a solo duel in the game (engine and bots in the game's process, no
network, offline), a LAN room on a player's PC, a dedicated-server room, the headless arena. Every one of them
feeds the seat's WindBot `GameBehavior`, and every prompt the engine sends that seat (main phase or battle phase
action, cards to pick, chain or not, yes or no, a zone, a position, a declaration, sort, rock-paper-scissors, turn
order) goes through one point before WindBot's AI: `GameBehavior.YGOL_Decide`
(`src/YGOLegacy.Bots/WindBot/Game/GameBehavior.YGOL_Decisions.cs`).
```csharp
namespace YGOLegacy.Bots.Decisions
public interface IDecisionMaker
{
Decision Decide(DecisionRequest request); // null = let the seat's own brain answer
}
```
- **The built-in brain** is the seat's WindBot executor: SmartExecutor for character decks, Lucky for Dovphin, a
deck executor otherwise. A seat without a decision maker (the default, and every seat today) is answered by it
exactly as before: `YGOL_Decide` calls the GameAI and builds nothing else.
- **Another decision maker** is chosen per seat by the WindBot token `Decisions=<name>` (the arena:
`--decisionsA/--decisionsB <name>`); a seat without the token gets the host's default, `BotHost.Decisions`
(null: the executor). Names are registered with `DecisionMakers.Register(name, seat => maker)` by the host or a
plug-in. `builtin` is the executor (also over a host default); an unknown name is logged and gets the executor.
Built in, both reading only each request's JSON: `first` (the first legal option; sums and counters solved from
the options' values; deterministic, the test brain) and `random` (a uniformly random legal option; a baseline;
leaves sums and counters to the executor).
- **The question** is a `DecisionRequest`: `kind` (`DecisionKind`: idle, battle, card, tribute, unselectCard,
chain, effectYesNo, yesNo, option, position, place, sum, counter, attribute, race, number, announceCard, sort,
rockPaperScissors, turnOrder), the seat, `min`/`max`/`cancelable`, the engine's `hint` and `description`, the
legal `options` (each with its `action`, its card as a `CardView`, its description and value), and `duel`, a
`DuelView` of what this seat sees (turn, phase, LP, its hand and Extra Deck, both fields, graveyards, banished,
deck counts, the chain; hidden cards have code 0). **The answer** is a `Decision`: the chosen option indices
(counter prompts: one count per option). All plain public fields: Newtonsoft (or any JSON) round-trips them;
`version` names the format.
- **Fallback.** A maker that returns null, throws, or answers something illegal (an index that is not an option,
a count outside `min`..`max`) gets a `[YGOL decisions]` warning and the executor answers that prompt; the duel
goes on. So a maker can take over some kinds and leave the rest (`random` leaves sums and counters). The
executor's own plans assume it made the earlier choices; mixing is legal, not necessarily good play.
- **Prompts that are not asked** are the ones with no choice: a chain prompt with nothing to chain, a chain whose
only option is one forced activation, a position prompt with one position, rock-paper-scissors fixed by the
`Hand=` token. Everything else is asked, including the prompts WindBot's AI never decides (the chain order of
`MSG_SORT_CHAIN`, a "field first" card prompt), whose built-in answer stays WindBot's fixed one. A seat with a
decision maker logs, at the end of each duel, `[YGOL decisions] counts <seat>: asked=N (kind xN, ...)
handedBack=N unasked=N (kind: reason xN, ...)`.
- **A remote brain** (OpenRouter Jev or Claude through our server, a model on a RunPod box) is an `IDecisionMaker`
that sends the request as JSON and waits for the answer: `Decide` is called on the bot's thread and the duel
waits for it, the same in all four places. Its time limit, retries and fallback are its own; returning null
hands the prompt back. No remote maker is built yet.
## Build and test
```bash
docker build --target test . # build, then every test (the Linux arena plays the fixture duels)
docker build --target dist -o out . # out/lib, out/bot-arena-linux-x64, out/bot-arena-win-x64
docker build --target release -o dist . # the release assets
```
With a dotnet 8 SDK instead: `build/fetch-engine.sh`, then `dotnet build src/YGOLegacy.Bots`, `dotnet publish
src/BotArena -r linux-x64 --self-contained`, `BOTARENA_EXE=<that>/BotArena dotnet test tests/YGOLegacy.Bots.Tests`.
The tests: the library through `BotHost` (cards, deck caps and `#ruleset`, the roster's deck → character → brain
and its Dovphin fallback, a broken character logged, the game's scan-then-publish), the decision seam (the
registry, JSON round trips, the random maker's ranges), and the published Linux arena on the fixture: 100 duels
of `AI_OldSchool` against itself (both seats the Smart brain) with 0 errors; the same `--seed` giving the same
report at `--parallel 4` and `--parallel 2` (clocks aside); the built-in brain's `--seed 7` report equal to the
v0.1.0 release's (`tests/fixture/expected/v0.1.0-seed7-n100.json`, clocks aside); 100 duels with both seats
answered by `first`, every choice asked (none handed back, only no-choice prompts unasked); the same with `random`;
`Decisions=builtin` giving the same duels as no token; and the Smart brain beating `random`.
## Engine pin
`engine.json` is the engine release's own `engine.json`. `build/fetch-engine.sh` downloads that version's
`ygol-engine-binding.zip`, `ygol-engine-linux-x64.tar.gz` and `ygol-engine-win64.zip` and refuses a bundle that
disagrees with the pin. The library compiles against the binding; each arena ships its platform's native library
and the binding next to the exe. **Moving to a new engine release:** copy that release's `engine.json` over this
one, run `docker build --target test .`, commit, release. The game and the server should be on the same engine
release as the bots they take (`bots.json`'s `engine`).
## Release
```bash
# CHANGELOG.md: add "## vX.Y.Z — <date>" (the section is the release body), commit
git tag vX.Y.Z && git push origin main vX.Y.Z
```
The run (Actions tab) builds, tests and packs; the release named after the tag appears only when all of that
passed. A tag whose release already exists is refused: a bad release gets a new patch tag, never a re-upload.
`https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-bots/releases/download/<tag>/<asset>`:
| Asset | Contents |
|---|---|
| `bots-lib.zip` | `YGOLegacy.Bots.dll`, `YGOLegacy.Bots.pdb`, `bots.json` |
| `bot-arena-linux-x64.tar.gz` | `bot-arena-linux-x64/`: `BotArena` (self-contained), `libygol-engine.so`, `YGOLegacy.Engine.dll`, `engine.json`, `bots.json` |
| `bot-arena-win-x64.zip` | `bot-arena-win-x64/`: `BotArena.exe` (self-contained), `ygol-engine.dll`, `YGOLegacy.Engine.dll`, `engine.json`, `bots.json` |
| `bots.json` | `{ version, engine }`: this release and the engine release it was built and tested against |
`YGOLegacy.Bots.BotsBuild.Version` and `.Engine` carry the same two strings inside the library.