osrlib 2.5.0__tar.gz → 2.5.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. {osrlib-2.5.0 → osrlib-2.5.1}/PKG-INFO +1 -1
  2. {osrlib-2.5.0 → osrlib-2.5.1}/pyproject.toml +1 -1
  3. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/_descriptions.py +24 -4
  4. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/dice.py +5 -0
  5. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/tables.py +38 -7
  6. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/commands.py +51 -21
  7. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/dungeon.py +36 -13
  8. {osrlib-2.5.0 → osrlib-2.5.1}/LICENSE +0 -0
  9. {osrlib-2.5.0 → osrlib-2.5.1}/LICENSE-OGL.md +0 -0
  10. {osrlib-2.5.0 → osrlib-2.5.1}/README.md +0 -0
  11. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/__init__.py +0 -0
  12. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/__init__.py +0 -0
  13. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/abilities.py +0 -0
  14. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/alignment.py +0 -0
  15. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/character.py +0 -0
  16. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/classes.py +0 -0
  17. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/clock.py +0 -0
  18. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/combat.py +0 -0
  19. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/creature.py +0 -0
  20. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/effects.py +0 -0
  21. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/events.py +0 -0
  22. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/frozen.py +0 -0
  23. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/items.py +0 -0
  24. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/monsters.py +0 -0
  25. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/npc.py +0 -0
  26. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/rng.py +0 -0
  27. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/ruleset.py +0 -0
  28. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/spells.py +0 -0
  29. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/treasure.py +0 -0
  30. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/core/validation.py +0 -0
  31. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/__init__.py +0 -0
  32. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_arrival.py +0 -0
  33. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_doors.py +0 -0
  34. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_gating.py +0 -0
  35. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_hauling.py +0 -0
  36. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_items.py +0 -0
  37. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_light.py +0 -0
  38. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_magic_items.py +0 -0
  39. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_movement.py +0 -0
  40. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_odometer.py +0 -0
  41. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_party_location.py +0 -0
  42. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_party_members.py +0 -0
  43. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_search.py +0 -0
  44. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_spells.py +0 -0
  45. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_town.py +0 -0
  46. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/_treasure.py +0 -0
  47. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/adventure.py +0 -0
  48. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/battle.py +0 -0
  49. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/content_pack.py +0 -0
  50. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/encounter.py +0 -0
  51. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/events.py +0 -0
  52. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/exploration.py +0 -0
  53. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/gates.py +0 -0
  54. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/interpreter.py +0 -0
  55. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/narrative.py +0 -0
  56. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/party.py +0 -0
  57. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/quests.py +0 -0
  58. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/session.py +0 -0
  59. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/stocking.py +0 -0
  60. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/triggers.py +0 -0
  61. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/crawl/views.py +0 -0
  62. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/LICENSE-OGL.md +0 -0
  63. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/__init__.py +0 -0
  64. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/abilities.json +0 -0
  65. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/classes.json +0 -0
  66. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/combat_tables.json +0 -0
  67. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/encounter_tables.json +0 -0
  68. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/equipment.json +0 -0
  69. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/languages.json +0 -0
  70. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/magic_items.json +0 -0
  71. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/monsters.json +0 -0
  72. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/spells.json +0 -0
  73. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/data/treasure.json +0 -0
  74. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/errors.py +0 -0
  75. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/messages.py +0 -0
  76. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/persistence.py +0 -0
  77. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/py.typed +0 -0
  78. {osrlib-2.5.0 → osrlib-2.5.1}/src/osrlib/versioning.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: osrlib
3
- Version: 2.5.0
3
+ Version: 2.5.1
4
4
  Summary: B/X (1981 Basic/Expert) tabletop RPG rules engine for turn-based dungeon crawlers
5
5
  Keywords: basic-expert,bx,dungeon-crawler,game-engine,osr,rpg,srd,ttrpg
6
6
  Author: Marsh Macy
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "osrlib"
3
- version = "2.5.0"
3
+ version = "2.5.1"
4
4
  description = "B/X (1981 Basic/Expert) tabletop RPG rules engine for turn-based dungeon crawlers"
5
5
  readme = "README.md"
6
6
  # The wheel ships Open Game Content (osrlib/data/*.json and its LICENSE-OGL.md
@@ -13,8 +13,10 @@ enum's schema.
13
13
 
14
14
  A description reaches its reader as raw text, outside the documentation site, so `_describe` and `_StrEnum` reduce each
15
15
  mkdocstrings cross-reference, like ``[`MoveParty`][osrlib.crawl.commands.MoveParty]``, to its label, `` `MoveParty` ``.
16
- Markdown doesn't treat anything inside code as a link, so brackets in code, like `inventory["purse"]["gp"]`, stay as
17
- they are.
16
+ A label that is code and a code span that touches it become one code span, so
17
+ ``[`load_classes`][osrlib.data.load_classes]`().get(class_id)` `` becomes `` `load_classes().get(class_id)` ``, the
18
+ run of code that the documentation site shows. Markdown doesn't treat anything inside code as a link, so brackets in
19
+ code, like `inventory["purse"]["gp"]`, stay as they are.
18
20
  """
19
21
 
20
22
  import ast
@@ -41,15 +43,33 @@ _LITERAL = re.compile(r"(```.*?```|~~~.*?~~~|``.+?``)", re.DOTALL)
41
43
  _SPAN_OR_REFERENCE = re.compile(r"(`[^`\n]+`)|\[((?:[^\[\]`]|`[^`\n]+`)+)\]\[[^\]\s]+\]")
42
44
 
43
45
 
46
+ # Marks each end of a reduced label while `_plain` works: a NUL character, which Markdown prose doesn't contain.
47
+ _EDGE = "\x00"
48
+
49
+ # The end of one code span and the start of the next, with only the ends of reduced labels between them.
50
+ _JOIN = re.compile(f"`{_EDGE}+`")
51
+
52
+
44
53
  def _plain(text: str) -> str:
45
- """Return Markdown with each mkdocstrings cross-reference reduced to its label, and code left as it is."""
54
+ """Return Markdown with each mkdocstrings cross-reference reduced to its label, and code left as it is.
55
+
56
+ A label that is code can touch a code span, like ``[`load_classes`][osrlib.data.load_classes]`()` ``, which the
57
+ documentation site shows as one run of code with a link on its first part. `_plain` joins the two into one code
58
+ span, `` `load_classes()` ``. With a space between them, like ``[`load_spells`][osrlib.data.load_spells] `()` ``,
59
+ they stay two spans.
60
+ """
46
61
  if "][" not in text:
47
62
  return text
48
63
  parts = _LITERAL.split(text)
49
- parts[::2] = [_SPAN_OR_REFERENCE.sub(lambda match: match.group(1) or match.group(2), part) for part in parts[::2]]
64
+ parts[::2] = [_JOIN.sub("", _SPAN_OR_REFERENCE.sub(_mark_label, part)).replace(_EDGE, "") for part in parts[::2]]
50
65
  return "".join(parts)
51
66
 
52
67
 
68
+ def _mark_label(match: re.Match[str]) -> str:
69
+ """Return a code span as it is, or a cross-reference's label with each end marked."""
70
+ return match.group(1) or f"{_EDGE}{match.group(2)}{_EDGE}"
71
+
72
+
53
73
  @cache
54
74
  def _module_docstrings(module_name: str) -> dict[str, dict[str, str]]:
55
75
  """Map the qualified name of each class in a module to the plain docstrings of the fields it declares.
@@ -244,6 +244,11 @@ def _lowest_total(expression: DiceExpression) -> int:
244
244
  return (expression.count + expression.modifier) * expression.multiplier
245
245
 
246
246
 
247
+ def _highest_total(expression: DiceExpression) -> int:
248
+ """Return the highest total `expression` can roll, with every die showing its highest face."""
249
+ return (expression.count * expression.sides + expression.modifier) * expression.multiplier
250
+
251
+
247
252
  def roll(expression: str | DiceExpression, stream: RngStream) -> RollResult:
248
253
  """Roll a dice expression, drawing from the stream you pass.
249
254
 
@@ -59,7 +59,7 @@ from pydantic import ConfigDict, Field, field_validator, model_validator
59
59
 
60
60
  from osrlib.core._descriptions import _StrEnum
61
61
  from osrlib.core.classes import SavingThrows
62
- from osrlib.core.dice import _lowest_total, parse, roll
62
+ from osrlib.core.dice import _highest_total, _lowest_total, parse, roll
63
63
  from osrlib.core.frozen import FrozenMap, _Model
64
64
  from osrlib.core.monsters import MonsterHitDice
65
65
  from osrlib.core.rng import RngStream
@@ -594,6 +594,29 @@ assert isinstance(entry, MonsterEncounterEntry)
594
594
  """
595
595
 
596
596
 
597
+ _SPAWN_LIMIT = 100
598
+ """The most monsters or adventurers one count creates: a spawn command's count, a keyed
599
+ encounter's lines together, or a wandering monster table row's count. 100 is the largest number
600
+ in the B/X monster data, for normal bats and for wild horses in a lair. It lives here because
601
+ `osrlib.core` can't import `osrlib.crawl`, and the crawl models and commands that share it import
602
+ it from here. The `count_fixed` fields state it as a literal `le=100`, because the reference
603
+ pages render a field's value and must not show a private name."""
604
+
605
+
606
+ def _spawn_count_dice(expression: str) -> str:
607
+ """Return a `count_dice` expression, or raise `ValueError` when it can roll past `_SPAWN_LIMIT`.
608
+
609
+ The highest total, with every die showing its highest face, must be at most `_SPAWN_LIMIT`.
610
+ An expression that doesn't parse raises
611
+ [`ContentValidationError`][osrlib.errors.ContentValidationError] from
612
+ [`parse`][osrlib.core.dice.parse].
613
+ """
614
+ highest = _highest_total(parse(expression))
615
+ if highest > _SPAWN_LIMIT:
616
+ raise ValueError(f"{expression!r} can roll {highest}, and a spawn creates at most {_SPAWN_LIMIT}")
617
+ return expression
618
+
619
+
597
620
  class EncounterTableRow(_Model):
598
621
  """One d20 result on a wandering monster table: what appears, and how many.
599
622
 
@@ -603,6 +626,15 @@ class EncounterTableRow(_Model):
603
626
  [`select_encounter_individuals`][osrlib.core.tables.select_encounter_individuals]. For an
604
627
  [`NpcPartyEncounterEntry`][osrlib.core.tables.NpcPartyEncounterEntry], pass them to
605
628
  [`generate_npc_party`][osrlib.core.npc.generate_npc_party].
629
+
630
+ A row creates at most 100 monsters or adventurers, so an adventure's own wandering monster
631
+ table can't spawn more at once than a
632
+ [`SpawnMonsters`][osrlib.crawl.commands.SpawnMonsters] command can.
633
+
634
+ Raises:
635
+ ContentValidationError: If `count_dice` is not a valid dice expression.
636
+ ValueError: If both `count_dice` and `count_fixed` are given, or neither, if `count_fixed`
637
+ is outside 1 to 100, or if `count_dice` can roll more than 100.
606
638
  """
607
639
 
608
640
  model_config = ConfigDict(frozen=True)
@@ -620,11 +652,12 @@ class EncounterTableRow(_Model):
620
652
  """A dice expression for how many appear, like `"1d6"`, or `None` when the row has a fixed number.
621
653
 
622
654
  Roll it with [`roll`][osrlib.core.dice.roll]. Use this count rather than the number appearing in the monster's
623
- description, as the SRD's note on the dungeon tables says. Exactly one of this and `count_fixed` is set.
655
+ description, as the SRD's note on the dungeon tables says. Exactly one of this and `count_fixed` is set. The
656
+ expression's highest total must be at most 100, so `"1d100"` is legal and `"1d100+1"` isn't.
624
657
  """
625
658
 
626
- count_fixed: int | None = None
627
- """A fixed number of individuals, or `None` when the row has a dice expression instead.
659
+ count_fixed: int | None = Field(default=None, ge=1, le=100)
660
+ """A fixed number of individuals from 1 to 100, or `None` when the row has a dice expression instead.
628
661
 
629
662
  Exactly one of this and `count_dice` is set.
630
663
  """
@@ -632,9 +665,7 @@ class EncounterTableRow(_Model):
632
665
  @field_validator("count_dice")
633
666
  @classmethod
634
667
  def _dice_must_parse(cls, value: str | None) -> str | None:
635
- if value is not None:
636
- parse(value)
637
- return value
668
+ return value if value is None else _spawn_count_dice(value)
638
669
 
639
670
  @model_validator(mode="after")
640
671
  def _dice_or_fixed(self) -> EncounterTableRow:
@@ -184,12 +184,13 @@ from pydantic import ConfigDict, Field, TypeAdapter, ValidationError, field_vali
184
184
 
185
185
  from osrlib.core._descriptions import _StrEnum
186
186
  from osrlib.core.character import Character
187
- from osrlib.core.clock import TimeUnit
187
+ from osrlib.core.clock import ROUNDS_PER_DAY, ROUNDS_PER_TURN, TimeUnit
188
188
  from osrlib.core.dice import parse
189
189
  from osrlib.core.events import Event
190
190
  from osrlib.core.frozen import _Model
191
191
  from osrlib.core.items import Coins
192
192
  from osrlib.core.spells import MemorizedSpell
193
+ from osrlib.core.tables import _spawn_count_dice
193
194
  from osrlib.core.validation import Rejection
194
195
  from osrlib.crawl.dungeon import Direction, PartyLocation
195
196
  from osrlib.errors import ContentValidationError
@@ -341,6 +342,17 @@ class SessionMode(_StrEnum):
341
342
  return self in (SessionMode.GAME_OVER, SessionMode.VICTORY)
342
343
 
343
344
 
345
+ _ADVANCE_DAYS_LIMIT = 28
346
+ """The most days one `AdvanceTime` advances. 28 days is the longest time limit on raising the
347
+ dead, for a 14th-level cleric."""
348
+
349
+ _ROUNDS_PER_UNIT: dict[TimeUnit, int] = {
350
+ TimeUnit.ROUND: 1,
351
+ TimeUnit.TURN: ROUNDS_PER_TURN,
352
+ TimeUnit.DAY: ROUNDS_PER_DAY,
353
+ }
354
+
355
+
344
356
  def _dice_that_parse(expression: str) -> str:
345
357
  """Return a command's dice expression, or raise `ValueError` when it doesn't parse.
346
358
 
@@ -382,11 +394,12 @@ class Command(_Model):
382
394
 
383
395
  command_type: str
384
396
  """The name that identifies the command class in serialized data. Each command class fixes
385
- it to a distinct snake_case string, like `"move_party"` or `"open_door"`, and constructing
386
- the class sets it, so you never pass it yourself.
397
+ it to a distinct snake_case string, like `"move_party"` or `"open_door"`. In Python,
398
+ constructing the class sets it, so you don't pass it.
387
399
  [`parse_command`][osrlib.crawl.commands.parse_command] and the
388
400
  [`AnyCommand`][osrlib.crawl.commands.AnyCommand] union use it to rebuild the right class
389
- from a serialized mapping. A class's value never changes between releases."""
401
+ from a serialized mapping, so a mapping you pass to either one has to include it, like
402
+ the JSON body of a request. A class's value never changes between releases."""
390
403
  source: str | None = Field(default=None, min_length=1)
391
404
  """Who or what issued the command on whose behalf: an authored trigger or quest id, or the
392
405
  name of one of your game's systems. The engine never reads it, so a command with a
@@ -3332,12 +3345,13 @@ class GrantItem(Command):
3332
3345
  and the ids the adventure bundles. Then it looks in the magic item catalog from
3333
3346
  [`load_magic_items`][osrlib.data.load_magic_items] (see the
3334
3347
  [magic item id index](https://mmacy.github.io/osrlib-python/reference/content-ids/magic-items/))."""
3335
- quantity: int = Field(default=1, ge=1)
3336
- """How many to place, counted as single items rather than as purchase lots.
3348
+ quantity: int = Field(default=1, ge=1, le=1_000)
3349
+ """How many to place, from 1 to 1,000, counted as single items rather than as purchase lots.
3337
3350
  `quantity=6` grants six torches, the same number one lot of torches from
3338
3351
  [`PurchaseEquipment`][osrlib.crawl.commands.PurchaseEquipment] delivers. For a magic
3339
3352
  item, it's the number of separate magic items to create, each with its own instance id
3340
- and its own rolls. `quantity=2` of a bundle of arrows +1 grants two bundles."""
3353
+ and its own rolls. `quantity=2` of a bundle of arrows +1 grants two bundles. To grant more
3354
+ than 1,000, send the command again."""
3341
3355
 
3342
3356
 
3343
3357
  class GrantCoins(Command):
@@ -3771,10 +3785,15 @@ class SpawnMonsters(Command):
3771
3785
  [`ENCOUNTER_STREAM`][osrlib.crawl.session.ENCOUNTER_STREAM]. Use it when the number should
3772
3786
  depend on the seed, and use `count_fixed` when it shouldn't. Set exactly one of the two. A
3773
3787
  malformed expression fails validation when the command is constructed, so the session never
3774
- sees it. A roll below 1 counts as 1."""
3775
- count_fixed: int | None = Field(default=None, ge=1)
3776
- """How many to spawn, as an exact number. Use it when the author chose the number. Set
3777
- exactly one of this and `count_dice`."""
3788
+ sees it, and so does an expression that can roll more than 100, like `"1d100+1"` or
3789
+ `"11d10"`. A roll below 1 counts as 1. For more than 100 monsters, send the command again
3790
+ while the encounter is open, and the new group joins it. The session refuses that second
3791
+ command during a pursuit."""
3792
+ count_fixed: int | None = Field(default=None, ge=1, le=100)
3793
+ """How many to spawn, as an exact number from 1 to 100. Use it when the author chose the
3794
+ number. Set exactly one of this and `count_dice`. For more than 100 monsters, send the
3795
+ command again while the encounter is open, and the new group joins it. The session refuses
3796
+ that second command during a pursuit."""
3778
3797
  distance_feet: int = Field(ge=0)
3779
3798
  """How far away the monsters appear, in feet. A new encounter opens at this distance, and
3780
3799
  a group that joins an open encounter starts at it. The distance determines whether
@@ -3785,7 +3804,7 @@ class SpawnMonsters(Command):
3785
3804
  @field_validator("count_dice")
3786
3805
  @classmethod
3787
3806
  def _dice_must_parse(cls, value: str | None) -> str | None:
3788
- return value if value is None else _dice_that_parse(value)
3807
+ return value if value is None else _spawn_count_dice(_dice_that_parse(value))
3789
3808
 
3790
3809
  @model_validator(mode="after")
3791
3810
  def _dice_or_fixed(self) -> SpawnMonsters:
@@ -3829,15 +3848,18 @@ class SpawnNpcParty(Command):
3829
3848
  count_dice: str | None = None
3830
3849
  """How many adventurers, as a dice expression rolled on the session's
3831
3850
  [`ENCOUNTER_STREAM`][osrlib.crawl.session.ENCOUNTER_STREAM]. A malformed expression fails
3832
- validation when the command is constructed. A roll below 1 counts as 1. Don't set it
3833
- together with `count_fixed`.
3851
+ validation when the command is constructed, and so does an expression that can roll more
3852
+ than 100, like `"1d100+1"`. A roll below 1 counts as 1. Don't set it together with
3853
+ `count_fixed`.
3834
3854
 
3835
3855
  When both this and `count_fixed` are `None`, the default, the engine rolls the B/X number
3836
3856
  for `party_kind`: 1d4+4 for `basic` and 1d6+3 for `expert`."""
3837
- count_fixed: int | None = Field(default=None, ge=1)
3838
- """How many adventurers, as an exact number. Use it when the adventure's author chose the
3839
- size of the band. The session rolls nothing for the size. Don't set it together with
3840
- `count_dice`."""
3857
+ count_fixed: int | None = Field(default=None, ge=1, le=100)
3858
+ """How many adventurers, as an exact number from 1 to 100. Use it when the adventure's
3859
+ author chose the size of the band. The session rolls nothing for the size. Don't set it
3860
+ together with `count_dice`. A band has at most 100 adventurers, whether you set this or
3861
+ `count_dice`. The command can't add adventurers to an open encounter, so a larger band
3862
+ isn't possible."""
3841
3863
  distance_feet: int = Field(ge=0)
3842
3864
  """How far away the NPC party appears, in feet, on the same terms as
3843
3865
  [`SpawnMonsters.distance_feet`][osrlib.crawl.commands.SpawnMonsters.distance_feet]."""
@@ -3845,7 +3867,7 @@ class SpawnNpcParty(Command):
3845
3867
  @field_validator("count_dice")
3846
3868
  @classmethod
3847
3869
  def _dice_must_parse(cls, value: str | None) -> str | None:
3848
- return value if value is None else _dice_that_parse(value)
3870
+ return value if value is None else _spawn_count_dice(_dice_that_parse(value))
3849
3871
 
3850
3872
  @model_validator(mode="after")
3851
3873
  def _dice_or_fixed(self) -> SpawnNpcParty:
@@ -4046,13 +4068,21 @@ class AdvanceTime(Command):
4046
4068
 
4047
4069
  command_type: Literal["advance_time"] = "advance_time"
4048
4070
  n: int = Field(ge=0)
4049
- """How many of `unit` to advance. `0` is legal, advances nothing, and still emits the
4050
- `TimeAdvancedEvent`."""
4071
+ """How many of `unit` to advance, up to 28 days of game time: 28 days, 4,032 turns, or
4072
+ 241,920 rounds. A larger `n` for its `unit` fails validation when the command is
4073
+ constructed, so the session never sees it. For more time, send the command again. `0` is
4074
+ legal, advances nothing, and still emits the `TimeAdvancedEvent`."""
4051
4075
  unit: TimeUnit
4052
4076
  """Which unit `n` counts: rounds, turns, or days. A B/X turn is 10 minutes, or 60 rounds,
4053
4077
  and a day is 144 turns. Effects expire across the whole time whichever unit you pick, and
4054
4078
  the party eats provisions at each day boundary the time crosses."""
4055
4079
 
4080
+ @model_validator(mode="after")
4081
+ def _within_limit(self) -> AdvanceTime:
4082
+ if self.n * _ROUNDS_PER_UNIT[self.unit] > _ADVANCE_DAYS_LIMIT * ROUNDS_PER_DAY:
4083
+ raise ValueError(f"advance at most {_ADVANCE_DAYS_LIMIT} days of game time in one command")
4084
+ return self
4085
+
4056
4086
 
4057
4087
  class RollDice(Command):
4058
4088
  """Referee: roll dice through the session, so the roll is seeded, logged, and replayed.
@@ -80,12 +80,12 @@ from pydantic import ConfigDict, Field, PrivateAttr, field_validator, model_vali
80
80
  from osrlib.core._descriptions import _StrEnum
81
81
  from osrlib.core.alignment import Alignment
82
82
  from osrlib.core.clock import TimeUnit
83
- from osrlib.core.dice import parse
83
+ from osrlib.core.dice import _highest_total, parse
84
84
  from osrlib.core.effects import Condition
85
85
  from osrlib.core.frozen import FrozenMap, _Model
86
86
  from osrlib.core.items import Coins, MagicItemInstance, ValuableInstance
87
87
  from osrlib.core.spells import SaveSpec
88
- from osrlib.core.tables import EncounterTable, ReactionResult
88
+ from osrlib.core.tables import _SPAWN_LIMIT, EncounterTable, ReactionResult, _spawn_count_dice
89
89
  from osrlib.crawl.gates import GateSpec
90
90
 
91
91
  __all__ = [
@@ -1065,7 +1065,8 @@ class KeyedMonster(_Model):
1065
1065
 
1066
1066
  A [`KeyedEncounter`][osrlib.crawl.dungeon.KeyedEncounter] has a tuple of these, so a room with
1067
1067
  four orcs and their ogre bodyguard has two lines. Give each line either a fixed count or count
1068
- dice, not both.
1068
+ dice, not both. A line creates at most 100 monsters, the limit a
1069
+ [`SpawnMonsters`][osrlib.crawl.commands.SpawnMonsters] command has.
1069
1070
 
1070
1071
  Attributes:
1071
1072
  template_id: Which monster.
@@ -1074,7 +1075,8 @@ class KeyedMonster(_Model):
1074
1075
 
1075
1076
  Raises:
1076
1077
  ContentValidationError: If `count_dice` is not a valid dice expression.
1077
- ValueError: If both `count_dice` and `count_fixed` are given, or neither.
1078
+ ValueError: If both `count_dice` and `count_fixed` are given, or neither, if `count_fixed`
1079
+ is outside 1 to 100, or if `count_dice` can roll more than 100.
1078
1080
 
1079
1081
  Examples:
1080
1082
  ```python
@@ -1096,18 +1098,17 @@ class KeyedMonster(_Model):
1096
1098
  """How many appear, as a dice expression like `"2d4"`. The session rolls it on the
1097
1099
  [`WANDERING_STREAM`][osrlib.crawl.session.WANDERING_STREAM] when the monsters spawn, and a roll
1098
1100
  below 1 counts as 1. Set this or `count_fixed`, not both. The model checks the expression when
1099
- you construct it, so a malformed one fails then, not during play."""
1100
- count_fixed: int | None = None
1101
- """How many appear, as a fixed number. Set this or `count_dice`, not both. A printed adventure
1102
- usually gives a fixed number, and so does [`stock_area`][osrlib.crawl.stocking.stock_area] when
1103
- it rolls a room for you."""
1101
+ you construct it, so a malformed one fails then, not during play, and so does an expression that
1102
+ can roll more than 100, like `"1d100+1"` or `"11d10"`."""
1103
+ count_fixed: int | None = Field(default=None, ge=1, le=100)
1104
+ """How many appear, as a fixed number from 1 to 100. Set this or `count_dice`, not both. A
1105
+ printed adventure usually gives a fixed number, and so does
1106
+ [`stock_area`][osrlib.crawl.stocking.stock_area] when it rolls a room for you."""
1104
1107
 
1105
1108
  @field_validator("count_dice")
1106
1109
  @classmethod
1107
1110
  def _dice_must_parse(cls, value: str | None) -> str | None:
1108
- if value is not None:
1109
- parse(value)
1110
- return value
1111
+ return value if value is None else _spawn_count_dice(value)
1111
1112
 
1112
1113
  @model_validator(mode="after")
1113
1114
  def _dice_or_fixed(self) -> KeyedMonster:
@@ -1132,6 +1133,10 @@ class KeyedEncounter(_Model):
1132
1133
  stance: A fixed reaction, instead of a reaction roll.
1133
1134
  hoard: Whether the monsters have their lair treasure.
1134
1135
 
1136
+ Raises:
1137
+ ValueError: If the lines can create more than 100 monsters together, counting each line at
1138
+ its `count_fixed` or at the highest total its `count_dice` can roll.
1139
+
1135
1140
  Examples:
1136
1141
  ```python
1137
1142
  from osrlib.core.tables import ReactionResult
@@ -1151,7 +1156,10 @@ class KeyedEncounter(_Model):
1151
1156
 
1152
1157
  monsters: tuple[KeyedMonster, ...] = Field(min_length=1)
1153
1158
  """The monster lines in the encounter, at least one. See
1154
- [`KeyedMonster`][osrlib.crawl.dungeon.KeyedMonster]."""
1159
+ [`KeyedMonster`][osrlib.crawl.dungeon.KeyedMonster]. The lines create at most 100 monsters
1160
+ together, counting each line at its `count_fixed` or at the highest total its `count_dice` can
1161
+ roll, so a line of 60 goblins and a line of `"2d20"` hobgoblins is legal, and 60 goblins with
1162
+ `"1d6x7"` hobgoblins isn't."""
1155
1163
  alignment: Alignment | None = None
1156
1164
  """The alignment every spawned monster takes, for a template that allows more than one. With
1157
1165
  `None`, the default, the session rolls it as usual. It has to be an alignment that each line's template allows, and
@@ -1172,6 +1180,21 @@ class KeyedEncounter(_Model):
1172
1180
  dungeon by the B/X rules often produces such a room, because only some monster rooms get treasure
1173
1181
  on the stocking roll."""
1174
1182
 
1183
+ @model_validator(mode="after")
1184
+ def _monsters_within_limit(self) -> KeyedEncounter:
1185
+ most = 0
1186
+ for line in self.monsters:
1187
+ if line.count_fixed is not None:
1188
+ most += line.count_fixed
1189
+ elif line.count_dice is not None:
1190
+ # A roll below 1 counts as 1, so every line creates at least one monster.
1191
+ most += max(1, _highest_total(parse(line.count_dice)))
1192
+ if most > _SPAWN_LIMIT:
1193
+ raise ValueError(
1194
+ f"the encounter's lines can create {most} monsters, and a spawn creates at most {_SPAWN_LIMIT}"
1195
+ )
1196
+ return self
1197
+
1175
1198
 
1176
1199
  class AreaSpec(_Model):
1177
1200
  """A keyed room or cave: a named group of cells with content bound to it.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes