Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
### Added

- Two house rules for character creation, which the SRD doesn't print and `Ruleset()` leaves off (#341). [`Ruleset.ability_score_method`][osrlib.core.ruleset.Ruleset.ability_score_method] is `"3d6"` by default. With `"4d6_drop_lowest"`, [`create_character`][osrlib.core.character.create_character] throws four d6 for each ability and drops the lowest, the first of them thrown when two or more tie, and [`roll_ability_scores`][osrlib.core.character.roll_ability_scores] does the same when you pass the method to its new `method` keyword. [`AbilityScoreRolls.rolls`][osrlib.core.character.AbilityScoreRolls.rolls] keeps the three dice that count, and the new [`AbilityScoreRolls.dropped`][osrlib.core.character.AbilityScoreRolls.dropped] has the dropped die of each ability, so a client can show all four. The method draws 24 dice instead of 18, so the same seed gives a different character, and NPC adventurers always roll 3d6. With [`Ruleset.max_hp_at_first_level`][osrlib.core.ruleset.Ruleset.max_hp_at_first_level] on, a first-level character's hit points are the hit die's size plus the CON modifier, and at least 1, and the new [`HitPointRoll.maximized`][osrlib.core.character.HitPointRoll.maximized] is true. The hit die and any rerolls of `hp_reroll_at_first_level` are still thrown, so the starting gold is the same as with the flag off. `examples/rulesets/creation_house_rules.json` is a ruleset with both house rules on and every other field at its default, for a front end to copy. With both flags at their defaults, every draw and result is what 2.8.0 gave. A 2.8.0 save loads with both off, a 2.8.0 creation result parses with an empty `dropped` and a false `maximized`, and [`SCHEMA_VERSION`][osrlib.versioning.SCHEMA_VERSION] stays 4.
- [`SpellMode.display_name`][osrlib.core.spells.SpellMode.display_name] is a short label for a mode, such as `"Blind a creature"`, for a client to show where a player chooses one of a form's modes, such as a cast dialog (#340). It says what the caster does, in sentence case with no closing punctuation, and in the built-in catalog no two modes of one form have the same label. Each built-in label comes from the lead-in that the OSE SRD prints before the usage, such as "Blinding a creature:". Where the page prints no lead-in, or one that names a situation such as *bless*'s "Battle:", the SRD compiler has a label written from the page's words, such as `"Bless allies in battle"`. A mode that leaves the label out, or gives one that is empty or only spaces, gets a label made from its key: each run of underscores becomes one space, the spaces at either end are dropped, and the first letter becomes a capital, so `"kill_green_slime"` is labelled `"Kill green slime"`. The one mode of a single-mode form is labelled the same way, such as *fire ball*'s `"Damage"`. As a result, a spell document, an adventure, or a save from 2.8.0 loads with a label on every mode, and [`SCHEMA_VERSION`][osrlib.versioning.SCHEMA_VERSION] stays 4.

### Changed

- Each built-in mode of the reverses of *light* and *continual light*, of *charm monster*, and of *silence 15' radius* has the part of the SRD text that covers it as its [`prose`][osrlib.core.spells.SpellMode.prose], so no two modes of one form have the same prose (#340). Before, every mode of one of these forms had the whole passage. For example, the `blind` mode of *darkness*, the reverse of *light*, had "Creates a 15’ radius area of magical blackness, preventing normal sight (but not infravision). Like light, it may alternatively be used to blind creatures or to dispel a light spell." and now has "Like light, it may alternatively be used to blind creatures." Each mode of *charm monster* has only its own alternative from the first sentence, and the `area` mode of *silence 15' radius* leaves out the "Casting upon a creature:" bullet. [`ReversedForm.prose`][osrlib.core.spells.ReversedForm.prose] still has the whole text of the reverse, and a spell that an adventure bundles keeps the prose its author wrote.

## [2.8.0] - 2026-11-25

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/authoring-custom-content.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ MEND_WOUNDS = SpellTemplate.model_validate(
)
```

Each `SpellMode` defines a castable `key`, a `targeting` spec, an optional `save`, and either an automated `effect` (such as `"heal"` or `"damage"` with `params`) or `manual=True` with `prose` (which expends the memorized spell and emits the cast event so your front end can resolve the effect). Reversible spells also accept an optional [`ReversedForm`][osrlib.core.spells.ReversedForm] with its reversed name and modes, matching built-in spells such as `cure_light_wounds`.
Each `SpellMode` defines a castable `key`, an optional [`display_name`][osrlib.core.spells.SpellMode.display_name] that your front end shows where a player chooses one of a form's modes, a `targeting` spec, an optional `save`, and either an automated `effect` (such as `"heal"` or `"damage"` with `params`) or `manual=True` with `prose` (which expends the memorized spell and emits the cast event so your front end can resolve the effect). Reversible spells also accept an optional [`ReversedForm`][osrlib.core.spells.ReversedForm] with its reversed name and modes, matching built-in spells such as `cure_light_wounds`. A mode that leaves `display_name` out, or gives one that is empty or only spaces, gets a label made from its key: each run of underscores becomes one space and the first letter becomes a capital, so `"mend"` is labelled `"Mend"` and `"kill_green_slime"` is labelled `"Kill green slime"`. When a form has more than one mode, give each one a label that says what the caster does, such as `"Blind a creature"`, and give no two of them the same label, so a player can tell them apart.

## Validate custom class and spell catalogs

Expand Down
56 changes: 53 additions & 3 deletions src/osrlib/core/spells.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ class SpellMode(_Model):

blind = light.mode("blind")
assert blind.save is not None # the target may save against being blinded
assert blind.display_name == "Blind a creature" # the label a cast dialog shows
assert blind.prose.startswith("Blinding a creature:")
```
"""
Expand All @@ -734,7 +735,28 @@ class SpellMode(_Model):

Pass it as `mode` to [`cast_spell`][osrlib.core.spells.cast_spell] and
[`validate_cast`][osrlib.core.spells.validate_cast]. A spell with a single usage still has a
mode, like *fire ball*'s `"damage"`.
mode, like *fire ball*'s `"damage"`. To show a mode to a player, use `display_name`.
"""

display_name: str = ""
"""A short label for the mode, like `"Blind a creature"`. Show it where a player chooses one of a form's modes.

The label says what the caster does, as an instruction in sentence case with no closing punctuation:
`"Conjure light"`, `"Cancel darkness"`, and `"Hold a group"`. In the built-in catalog, no two modes of one
form have the same label, in the normal form or in the reverse, so a cast dialog can list a form's
modes by label alone. A built-in label comes from the lead-in that the OSE SRD prints before the
usage, like "Blinding a creature:". A mode whose page prints no lead-in, or a lead-in that names a
situation rather than an action, like *bless*'s "Battle:", has a label written for it from the
page's words, like `"Bless allies in battle"` and the reverse of *light*'s `"Conjure darkness"`.

When you write a mode and leave the label out, or give one that is empty or only spaces, the
mode gets a label made from its key: each run of underscores becomes one space, the spaces at
either end are dropped, and the first letter becomes a capital, so `"darken"` is labelled
`"Darken"` and `"kill_green_slime"` is labelled `"Kill green slime"`. A key made only of
underscores is its own label, so a label is never empty. A spell you wrote for an earlier osrlib
loads this way, with a label on every mode. A spell with a single mode has nothing to choose
between, so the built-in catalog gives that mode the label from its key too, like *fire ball*'s
`"Damage"`. For a form with one mode, show the spell's name and not the mode's label.
"""

targeting: TargetingSpec | None = None
Expand All @@ -761,8 +783,31 @@ class SpellMode(_Model):
"""The SRD text for this usage.

Show it to the player. For a manual mode, it's all the information osrlib has about the result.
In the built-in catalog, no two modes of one form have the same prose. Where the page describes
several usages in one passage, like the reverse of *light*, each mode's prose is the part of
the passage that covers that usage, and the whole passage is the form's own prose, such as
[`ReversedForm.prose`][osrlib.core.spells.ReversedForm.prose].
"""

@model_validator(mode="before")
@classmethod
def _label_from_key(cls, data: object) -> object:
if not isinstance(data, dict):
return data
label = data.get("display_name")
if label is not None and not (isinstance(label, str) and not label.strip()):
return data
key = data.get("key")
if not isinstance(key, str):
return data
words = " ".join(part for part in key.split("_") if part).strip()
if not words:
return {**data, "display_name": key}
capital = words[0].upper()
if len(capital) == 1:
words = capital + words[1:]
return {**data, "display_name": words}

@model_validator(mode="after")
def _automated_modes_carry_structure(self) -> SpellMode:
if self.manual:
Expand Down Expand Up @@ -820,7 +865,11 @@ class ReversedForm(_Model):
"""

prose: str = ""
"""The SRD text for the reverse."""
"""The SRD text for the reverse, all of it.

Each of the reverse's `modes` has the part of this text that covers that mode as its own
[`prose`][osrlib.core.spells.SpellMode.prose].
"""

modes: tuple[SpellMode, ...] = Field(min_length=1)
"""One [`SpellMode`][osrlib.core.spells.SpellMode] per castable usage of the reverse, at least one, each with a
Expand Down Expand Up @@ -974,7 +1023,8 @@ def mode(self, key: str, *, reversed: bool = False) -> SpellMode:

Use this when you know which usage you want, to read its targeting, its save, or its prose
before you cast it. To offer a player a choice instead, iterate
[`modes`][osrlib.core.spells.SpellTemplate.modes] and show each mode's `prose`. The key you
[`modes`][osrlib.core.spells.SpellTemplate.modes] and show each mode's `display_name`, with its
`prose` for the details. The key you
pass here is the same string casting takes as its `mode` argument.

Args:
Expand Down
Loading
Loading