Skip to content

List the spells each caster may prepare and learn in the player view - #333

Merged
mmacy merged 7 commits into
mainfrom
view-spell-lists
Nov 26, 2026
Merged

mmacy merged 7 commits into
mainfrom
view-spell-lists

Conversation

@mmacy

@mmacy mmacy commented Nov 26, 2026

Copy link
Copy Markdown
Owner

PlayerView.spell_names named a spell only once a member already had it. A divine caster may prepare any spell on their class's list with no spell book, and LearnSpell accepts any spell on an arcane caster's list, so a player couldn't find out that an adventure adds a spell their character may prepare or learn: a level 2 cleric could prepare a bundled balm while spell_names was {}. Planned for 2.8.0. Closes #326.

What changed

  • MemberView.preparable_spells lists the spell ids PrepareSpells accepts in a normal-form selection for the member, and MemberView.learnable_spells the ids LearnSpell accepts, both in the catalog's order. A spell is in a list exactly when its command is accepted, or refused only for the moment and with none of the codes that belong to the spell and the member. The lists don't depend on the moment: they're the same before the party has slept, during an encounter, and while the member can't act.
  • MemberView.spell_slots is the member's progression row, so a prepare dialog can be built from the view alone, including for a custom class.
  • PlayerView.spell_levels gives the level of every spell in spell_names, and spell_names and reversed_spell_names name every id in the two lists.
  • A spell no member may choose stays hidden, such as one the adventure bundles for a list the party's classes don't cast from, or one at a level no member can cast.
  • The view's lists and the commands' checks share private helpers in osrlib.core.spells, so they can't drift. PrepareSpells and LearnSpell accept and refuse exactly what they did.
  • The link references at the bottom of CHANGELOG.md compared Unreleased from v2.5.1 and had no 2.6.0 or 2.7.0 link. They're corrected.

Every field has a default, no draw is taken, and no golden file changes.

Cost

A player view is built after every command. With the lists, a view of a ten-member party takes about 0.74 ms, where the merge base took 0.35 ms. Each lister skips the spells the shared check would certainly refuse before it calls the check, which still decides every id that reaches a list.

Testing

tests/test_view_spell_lists.py runs a ten-member party of casters at several levels, a custom divine class with its own list, a drained magic-user, and a book holding a spell the catalog lacks, and checks each list against what the commands accept for every catalog id. tests/test_view_spell_names.py pins the names independently of the view, including a spell named only through its effect.

An independent review sent 147,060 PrepareSpells and LearnSpell commands on the merge base and on the branch, over every built-in class and two custom ones at every level, and the rejections, events, and save documents were byte-identical. It found no hidden spell in 42 views, and two adventures that differ only in a hidden bundled spell gave byte-identical player views. A mutation pass over the listers and the name sources fails at least one test for every mutation that changes behaviour.

https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3

…prepare or learn

The new fields are stubs with defaults: `preparable_spells`, `learnable_spells`, and
`spell_slots` on `MemberView`, and `spell_levels` on `PlayerView` (#326). The docstrings of
`MemberView.spell_book` and `PlayerView.spell_names` now say that the two lists show a spell and
that a spell no member may choose stays hidden. The tests in tests/test_view_spell_lists.py are
marked xfail until the chunk lands, except the cases a stub already satisfies, which run as
guards. tests/saves/view_spell_lists_2.7.0.json is a save of the test party written by osrlib
2.7.0.

Ten tests in tests/test_view_spell_names.py pinned the whole of `spell_names` for casters who may
learn or prepare more spells, so they would fail once the lists name those spells. The
magic-users' books are now full at every level they have slots for, and the cleric tests name the
spells the cleric may prepare beside the ones they pin, so the tests hold before and after the
chunk.

The changelog's compare links pointed Unreleased at v2.5.1 and had no 2.6.0 or 2.7.0 entry, so
they now compare from v2.7.0 and list both releases.

Claude-Session: https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3
MemberView now fills spell_slots from the class's progression row, and preparable_spells and
learnable_spells with the spell ids that PrepareSpells and LearnSpell accept for the member, in the
catalog's order. PlayerView.spell_levels gives the level of every spell in spell_names, and
spell_names and reversed_spell_names name every id in the two lists (#326).

The lists come from the commands' own checks. memorize_spells now runs each copy through
_selection_rejection and counts slots with _slot_count, and add_spell_to_book runs through
_book_rejection; _preparable_spell_ids and _learnable_spell_ids in core/spells.py apply the same
functions to every spell in the catalog. In crawl/_spells.py, _unknown_book_spell is the session's
refusal of every LearnSpell while a book holds a spell the catalog lacks, shared by the handler and
_learnable_spells. The checks of the moment (needs_sleep, wrong_mode, member_incapacitated) are not
part of either list. No public signature changes, and the view takes no draw.

The views guide, the custom content guide, the effective_spells docstring, and the PrepareSpells
and LearnSpell field docstrings point a client at the lists. No golden file changes.

Claude-Session: https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3
Four cleric tests in tests/test_view_spell_names.py compared `spell_names` with the
cleric's own `preparable_spells`, so they no longer pinned the names, and a session
that dropped effect kinds from `spell_names` still passed the suite. They now state
a 3rd-level cleric's 1st-level spells and their reverses from the cleric list.

Two new tests each show an id in one place only: a spell running as an effect that
no member may choose or holds, and a spell left in a drained book or memorized
without a slot. In tests/test_view_spell_lists.py, the listed ids and the full set of
named spells now come from the expected lists rather than the view.

Claude-Session: https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3
A player view is built after every command, and the two lists made it 7 to 12 times slower: for
every spell in the catalog, _book_rejection repeated caster_profile and a scanning
SpellCatalog.get, and each refused spell built a Rejection model.

_book_rejection now makes the class and id checks and hands the template to the new
_book_template_rejection, which makes the rest. _learnable_spell_ids calls that directly with the
profile, the template, and the open slots it computed once. Both listers first skip a spell on
another list, one outside (or already in) the book, and one at a level with no slot or room. Each
of those tests skips only spells the shared check refuses, and the shared check still decides every
spell left, so the lists can't drift from the commands. _unknown_book_spell no longer builds the
catalog's id set for an empty book.

Per player view, best of five runs of 200, before and after this commit: four members with two
casters went from 0.870 to 0.268 ms on the built-in catalog and from 1.419 to 0.304 ms with 50
bundled spells, and the ten-member test party from 2.501 to 0.645 ms and from 4.012 to 0.756 ms.

Claude-Session: https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3
The changelog and the views guide said that a spell is in a list exactly when the command accepts
it. For PrepareSpells the list means that one normal-form copy is refused with none of the codes
that belong to the spell and the member, so a selection over the slots, or one sent before the
party has slept, is refused although every spell in it is listed. Both now name the codes or the
checks, and say that the selection still has to fit the slots.

The PrepareSpells.selections docstring now says that an arcane caster may also select the reversed
form of a listed spell that has an entry in PlayerView.reversed_spell_names, and the LearnSpell
docstring says the list holds the spells the caster may learn.

The views guide said a spell on a scroll is named only when a member could prepare or learn it. It's
also named when a member has it in a book, has it memorized, or is under its effect, so the guide
now names the five places.

Claude-Session: https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3
…y what a list promises

The changelog said a spell is listed when its command is refused with none of
five codes, which reads as if the command is always refused. It now says the
command is accepted or refused only for the moment, and that an arcane caster
may also prepare the reverse of a listed spell.

Claude-Session: https://claude.ai/code/session_01V7Ertq3gKJ46oFQD6635f3
# Conflicts:
#	CHANGELOG.md
#	src/osrlib/crawl/views.py
@mmacy
mmacy merged commit 3f35056 into main Nov 26, 2026
5 checks passed
@mmacy
mmacy deleted the view-spell-lists branch November 26, 2026 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The player view doesn't name a bundled spell a caster could prepare or learn

1 participant