Skip to main content

osrlib

A Python library implementing the classic 1981 B/X (Basic/Expert) fantasy adventure game rules for turn-based, grid-based dungeon crawlers in the style of the original Bard's Tale. The rules are sourced from the Old-School Essentials System Reference Document, an Open Game Content restatement of the B/X rules. osrlib is the rules authority and game-state engine; your game supplies presentation, input, and content.

The library is headless and sans-I/O — it never renders, prompts, sleeps, or touches the network — and every game it runs is deterministic: the same seed and the same commands always replay the same game. Adventures carry their own content and behavior — bundled items, gated doors, triggers, and quests — and the library ships the interpreter that plays them through to a victory ending. Four kinds of consumer are first-class: a web or mobile backend (FastAPI over HTTP), a terminal game (a local TUI crawler), an LLM referee or narrator driven by structured events and typed commands, and scripts or simulations that call the rules kernel with no session at all.

Status: released — osrlib on PyPI. The public API is frozen, and the documentation site is the place to learn the library — quickstart, guides, front-end walk-throughs, and a full reference for every public symbol, command, event, rejection code, message code, RNG stream, and content id.

Installation

Requires Python ≥ 3.14. The only runtime dependency is pydantic.

uv add osrlib

or, with pip:

pip install osrlib

Quickstart

from osrlib.core.alignment import Alignment
from osrlib.core.character import CHARACTER_CREATION_STREAM, create_character
from osrlib.core.rng import RngStreams
from osrlib.core.ruleset import Ruleset
from osrlib.crawl.adventure import Adventure, TownSpec
from osrlib.crawl.commands import EnterDungeon, MoveParty, SessionMode
from osrlib.crawl.dungeon import Direction, DungeonSpec, Edge, EdgeKind, LevelSpec
from osrlib.crawl.party import Party
from osrlib.crawl.session import GameSession
from osrlib.messages import format_message
from osrlib.persistence import load_game, save_game

# Roll two 1st-level characters; every random draw comes from a named, seeded stream.
rules = Ruleset()
creation = RngStreams(master_seed=7).get(CHARACTER_CREATION_STREAM)
fighter = create_character(name="Hild", class_id="fighter", alignment=Alignment.LAWFUL, ruleset=rules, stream=creation)
cleric = create_character(name="Osric", class_id="cleric", alignment=Alignment.LAWFUL, ruleset=rules, stream=creation)
party = Party(members=[fighter.character, cleric.character])

# The smallest adventure: a town and a one-corridor dungeon, two cells joined west-east.
crypt = DungeonSpec(
    id="crypt",
    name="The Old Crypt",
    levels=(LevelSpec(number=1, width=2, height=1, entrance=(0, 0), edges={"1,0:west": Edge(kind=EdgeKind.OPEN)}),),
)
town = TownSpec(name="Threshold", travel_turns={"crypt": 1})
adventure = Adventure(name="A First Delve", town=town, dungeons=(crypt,))

# A session starts in town; entering the dungeon switches it to exploring.
session = GameSession.new(party, adventure, seed=7)
session.execute(EnterDungeon(dungeon_id="crypt"))
assert session.mode is SessionMode.EXPLORING

# Commands in, events out: every rules resolution is a typed event with a message code.
result = session.execute(MoveParty(direction=Direction.EAST))
assert result.accepted
lines = [format_message(event) for event in result.events]
assert lines  # every event formats to a default English line

# The whole session round-trips through JSON: same seed, same commands, same game.
document = save_game(session)
restored = load_game(document)
assert save_game(restored) == document

The documentation site walks this example step by step, then builds out from it: building an adventure, the session and event loop, gates, triggers, and quests — the authored layer above — and complete front-end walk-throughs for the two example games in examples/.

Determinism

Determinism is a public API guarantee. All randomness flows through named PCG64 streams forked from a master seed, so the same seed and the same key always produce the same stream — independently of any other stream:

from osrlib.core.dice import roll
from osrlib.core.rng import RngStreams

streams_a = RngStreams(master_seed=42)
streams_b = RngStreams(master_seed=42)

rolls_a = [roll("2d6×10", streams_a.get("treasure")).total for _ in range(3)]
rolls_b = [roll("2d6×10", streams_b.get("treasure")).total for _ in range(3)]
assert rolls_a == rolls_b  # same seed + same key → identical sequences

Successive rolls on one stream differ, of course; reproducibility across derivations is the contract. A saved game restores from its serialized state alone — no re-execution — while replay_game separately rebuilds the identical session by re-executing the seed and the command log from scratch; that the two paths always agree is the determinism guarantee, exercised as a standing test.

SRD data pipeline

The game data in src/osrlib/data/ is generated from the scraped SRD markdown in srd/ and is never hand-edited. Regenerate it with:

uv run python -m tools.srd_compile

CI regenerates the data and fails on any diff, so srd/, the compiler, and the generated data cannot silently drift. Parser corrections belong in tools/srd_compile/overrides/, never in the output; every override carries a reason and is recorded in the output entry's overrides_applied provenance list. Rules interpretations and adaptations are documented in the adaptations register.

Contributing

Requires Python ≥ 3.14 and uv. Install from source and run the checks the way CI does:

git clone https://github.com/mmacy/osrlib-python.git
cd osrlib-python
uv sync
uv run ruff format --check
uv run ruff check
uv run pyright
uv run pytest
uv run mkdocs build --strict

The design is documented in the specification: architecture, contracts, rules scope, and the phased roadmap.

Licensing

This repository contains two kinds of material under two licenses:

  • Library code is licensed under the MIT license.
  • SRD-derived content — the scraped SRD text in srd/ and the compiled game data in src/osrlib/data/ — is Open Game Content used under the Open Game License 1.0a, which includes the complete Section 15 copyright notice. The data package ships its own copy of the license, with the osrlib Section 15 entry, inside the built wheel.

osrlib is an independent project, not affiliated with or endorsed by Necrotic Gnome. "Old-School Essentials" is a trademark of Necrotic Gnome, used here only to identify the source document; no claim of compatibility is made.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osrlib-1.5.0.tar.gz (465.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osrlib-1.5.0-py3-none-any.whl (494.7 kB view details)

Uploaded Python 3

File details

Details for the file osrlib-1.5.0.tar.gz.

File metadata

  • Download URL: osrlib-1.5.0.tar.gz
  • Upload date:
  • Size: 465.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for osrlib-1.5.0.tar.gz
Algorithm Hash digest
SHA256 9ac5be9ef91b82410a277d6442179e45644925d8c55b34d3eef3cebaf69bc398
MD5 18da5c5acf867a09382173a75b97d0d0
BLAKE2b-256 29d8cd0ec59ff5fb9cf6d53a132517a18aa2b601caa096a6cacf8e55fc3abbc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for osrlib-1.5.0.tar.gz:

Publisher: release.yml on mmacy/osrlib-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file osrlib-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: osrlib-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 494.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for osrlib-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6beeaefc2612336c87b7f3c04dd1ea119f8eef6d7709d01ee45814ebd24401aa
MD5 91211a12718437175e7688a4a69947fa
BLAKE2b-256 7b43af513622f51b809794c2165aea4170403db503b2b2dbb3c9d3553180f750

See more details on using hashes here.

Provenance

The following attestation bundles were made for osrlib-1.5.0-py3-none-any.whl:

Publisher: release.yml on mmacy/osrlib-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

1.7.1

2 files

1.7.0

2 files

1.6.0

2 files

This release

1.5.0 This release

2 files

1.4.0

2 files

1.3.0

2 files

1.2.1

2 files

1.2.0

2 files

1.1.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page