release: Storybook v0.2.0 - Major syntax and features update

BREAKING CHANGES:
- Relationship syntax now requires blocks for all participants
- Removed self/other perspective blocks from relationships
- Replaced 'guard' keyword with 'if' for behavior tree decorators

Language Features:
- Add tree-sitter grammar with improved if/condition disambiguation
- Add comprehensive tutorial and reference documentation
- Add SBIR v0.2.0 binary format specification
- Add resource linking system for behaviors and schedules
- Add year-long schedule patterns (day, season, recurrence)
- Add behavior tree enhancements (named nodes, decorators)

Documentation:
- Complete tutorial series (9 chapters) with baker family examples
- Complete reference documentation for all language features
- SBIR v0.2.0 specification with binary format details
- Added locations and institutions documentation

Examples:
- Convert all examples to baker family scenario
- Add comprehensive working examples

Tooling:
- Zed extension with LSP integration
- Tree-sitter grammar for syntax highlighting
- Build scripts and development tools

Version Updates:
- Main package: 0.1.0 → 0.2.0
- Tree-sitter grammar: 0.1.0 → 0.2.0
- Zed extension: 0.1.0 → 0.2.0
- Storybook editor: 0.1.0 → 0.2.0
This commit is contained in:
2026-02-13 21:52:03 +00:00
parent 80332971b8
commit 4596d34af7
290 changed files with 90316 additions and 5827 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
.claude/tmp
/target
.envrc
alice-in-wonderland.txt

View File

@@ -0,0 +1,127 @@
# LSP Phase 4: Navigation Features
## Status: ✅ Basic Implementation Complete
**Implemented:** Single-file go-to-definition and find-references
**Date:** 2026-02-09
**Tests:** 89/89 passing (11 new navigation tests)
## Features Implemented
### Go-to-Definition
- Jump to symbol definition when clicking on an identifier
- Works for: characters, templates, behaviors, species, life_arcs, schedules, etc.
- Returns accurate location with proper line/column positions
- Handles multiline documents correctly
### Find References
- Find all occurrences of a symbol in the current file
- Word boundary detection (won't match "Alice" in "Alison")
- Returns all locations where symbol is referenced
- Includes both declarations and uses
## Implementation Details
### Files Modified
- `storybook/src/lsp/definition.rs` - Fixed mutability, added proper position tracking
- `storybook/src/lsp/references.rs` - Fixed mutability, word boundary checking
- `storybook/src/lsp/server.rs` - Updated to use immutable references (read locks)
- `storybook/src/lsp/navigation_tests.rs` - 11 comprehensive tests
### Current Architecture
- **Single-file navigation**: Works within each open document independently
- **Symbol table**: Built from AST for each document (HashMap<String, Vec<SymbolLocation>>)
- **No cross-file support yet**: Each file is isolated
## Testing Coverage
### Navigation Tests (11 tests)
1. `test_goto_definition_character` - Basic go-to-definition
2. `test_goto_definition_not_found` - Handle missing symbols
3. `test_goto_definition_template` - Template navigation
4. `test_goto_definition_behavior` - Behavior tree navigation
5. `test_goto_definition_multiline` - Multiline document navigation
6. `test_find_references_character` - Basic find references
7. `test_find_references_single_occurrence` - Single reference
8. `test_find_references_not_found` - Handle edge cases
9. `test_find_references_word_boundaries` - Exact word matching
10. `test_find_references_multiple_files_same_name` - Template reuse
11. `test_find_references_species` - Species references
### Example Usage
**Go-to-Definition:**
```storybook
character Alice { age: 7 }
character Bob { friend: Alice } // Cmd+Click "Alice" → jumps to line 1
```
**Find References:**
```storybook
template Child { age: number }
character Alice: Child {} // Right-click "Child" → shows both uses
character Bob: Child {}
```
## Future Enhancements (Cross-File Navigation)
### What's Missing
- **Cross-file references**: Can't navigate to definitions in other files
- **Use declarations**: `use characters::Alice` not resolved
- **Workspace-level NameTable**: No global symbol resolution
- **Template includes**: Can't find where templates are used across files
- **Species inheritance**: Can't trace species usage across files
### Implementation Plan for Cross-File Support
1. Add workspace-level state to LSP server:
```rust
struct WorkspaceState {
name_table: NameTable,
file_mapping: HashMap<usize, Url>,
}
```
2. Build combined NameTable from all open documents
3. Rebuild on document changes
4. Use NameTable for cross-file navigation
5. Handle `use` declarations for imports
### Why Not Implemented Yet
- Requires significant architectural changes to server
- Need to track all files in workspace (not just open documents)
- Need file watching for unopened files
- More complex testing scenarios
- Current single-file solution covers 80% of use cases for MVP
## Phase 4 Verification Commands
### Run Navigation Tests
```bash
cd /Users/sienna/Development/storybook/storybook
cargo test --lib lsp::navigation_tests
```
### Run All LSP Tests
```bash
cd /Users/sienna/Development/storybook/storybook
cargo test --lib lsp
```
**Result:** 89/89 tests passing ✓
## Next Steps
**Phase 5 Options:**
1. **Continue with plan**: Implement editing assistance (completion, formatting)
2. **Enhance Phase 4**: Add cross-file navigation using NameTable
3. **Move to Phase 6**: Start on inspect panel with relationship graph
**Recommendation**: Continue with Phase 5 (editing assistance) since:
- Completion and formatting are high-value features
- Cross-file navigation can be added later as enhancement
- Better to have full single-file experience than partial multi-file
## Notes
- Single-file navigation works well for most development workflows
- Users typically work on one file at a time in Zed
- Cross-file can be added incrementally without breaking existing features
- Current implementation is robust and well-tested

View File

@@ -0,0 +1,142 @@
# LSP Testing Status (Phase 3 Complete)
## Summary
**Phase 3 (LSP Server Core Features) testing complete**
- All LSP tests passing: 78/78 ✓
- All tree-sitter grammar tests passing: 27/27 ✓
- Code coverage significantly improved
- Behavior tree support fully tested in both LSP and tree-sitter
## Test Coverage
### LSP Tests (78 total)
Location: `/Users/sienna/Development/storybook/storybook/src/lsp/`
#### Behavior Tests (8 tests) - `behavior_tests.rs`
- ✅ test_behavior_parsing
- ✅ test_behavior_symbols
- ✅ test_behavior_symbol_kinds
- ✅ test_behavior_in_document_symbols
- ✅ test_behavior_with_subtrees
- ✅ test_behavior_simple_action
- ✅ test_behavior_selectors_and_sequences
#### Diagnostics Tests (15 tests) - `diagnostics_tests.rs`
- ✅ Parse error detection and reporting
- ✅ Error position tracking
- ✅ Error severity levels
- ✅ Utility functions (byte_offset_to_position, create_diagnostic)
- ✅ Edge cases (empty input, whitespace, comments only)
#### Document Edge Tests (20 tests) - `document_edge_tests.rs`
- ✅ Word-at-offset with unicode, underscores, boundaries
- ✅ Document updates and symbol table management
- ✅ All declaration types: species, characters, templates, life_arcs, schedules, behaviors, institutions
- ✅ Symbol extraction for all entity types
#### Existing Tests (35 tests)
- Integration tests, formatting, hover, completion, definition, references, symbols
### Tree-sitter Grammar Tests (27 total)
Location: `/Users/sienna/Development/storybook/tree-sitter-storybook/test/corpus/`
#### Behavior Tests (7 tests) - `behaviors.txt`
- ✅ Simple Behavior (single action)
- ✅ Selector Behavior (? { ... })
- ✅ Sequence Behavior (> { ... })
- ✅ Nested Behavior (selectors + sequences)
- ✅ Repeat Behavior (* { ... })
- ✅ Behavior with Subtree (@path::notation)
- ✅ Complex Nested Behavior (all node types)
#### Declaration Tests (17 tests) - `declarations.txt`
- ✅ All declaration types (use, character, template, life_arc, schedule, behavior, etc.)
#### Basic Tests (3 tests) - `basic.txt`
- ✅ Simple character, use declaration, prose block
## Coverage Improvements
**Before testing push:**
- document.rs: 80.11%
- symbols.rs: 93.04%
- diagnostics.rs: 0%
- behavior support: untested
- Total LSP tests: 34
**After testing push:**
- document.rs: 97.79% (+17.68%)
- symbols.rs: 95.24% (+2.20%)
- diagnostics.rs: fully tested
- behavior support: comprehensive test coverage
- Total LSP tests: 78 (+44 new tests, +129% increase)
## Run Commands
### LSP Tests
```bash
cd /Users/sienna/Development/storybook/storybook
cargo test --lib lsp
```
### Tree-sitter Tests
```bash
cd /Users/sienna/Development/storybook/tree-sitter-storybook
npm run test
```
**Note:** If Bash tool fails, use Serena's `execute_shell_command` instead.
## Behavior Tree Support Verified
Both LSP and tree-sitter grammar now have comprehensive behavior tree support:
### Node Types Tested:
- **action_node**: Simple action identifiers (e.g., `walk_around`)
- **selector_node**: `? { ... }` - Try options in order until one succeeds
- **sequence_node**: `> { ... }` - Execute children in sequence
- **repeat_node**: `* { ... }` - Repeat child forever
- **subtree_node**: `@path::to::tree` - Reference external behavior tree
### Syntax Examples:
```storybook
behavior SimpleBehavior {
walk_around
}
behavior ComplexBehavior {
? {
> {
check_threat
flee_to_safety
}
> {
check_resources
gather_resources
}
* {
idle
}
}
}
behavior WithSubtree {
> {
@helpers::check_preconditions
main_action
}
}
```
## Next Steps (Phase 4)
- Navigation features (go-to-definition, find references)
- Cross-file symbol resolution
- Template includes and species references
## Files Modified
- Created: `storybook/src/lsp/behavior_tests.rs`
- Created: `storybook/src/lsp/diagnostics_tests.rs`
- Created: `storybook/src/lsp/document_edge_tests.rs`
- Created: `tree-sitter-storybook/test/corpus/behaviors.txt`
- Modified: `storybook/src/lsp/mod.rs` (added test modules)
- Modified: `storybook/src/lsp/server.rs` (fixed imports and mutability)

View File

@@ -0,0 +1,88 @@
# Tree-sitter Grammar Commands
## Location
Tree-sitter grammar is at: `/Users/sienna/Development/storybook/tree-sitter-storybook/`
## Running Tests
**Use Serena's execute_shell_command when Bash tool fails:**
```bash
cd /Users/sienna/Development/storybook/tree-sitter-storybook && npm run test
```
This runs the tree-sitter test suite against all corpus files in `test/corpus/`.
## Test Corpus Structure
Test files are in `test/corpus/*.txt` format:
```
==================
Test Name
==================
[input code]
---
[expected parse tree]
```
### Key Parse Tree Patterns for Behaviors:
1. **All declarations wrap in `(declaration ...)`**
2. **Behavior nodes wrap children in `(behavior_node ...)`**
3. **Paths have internal structure:**
```
(path
(path_segments
(identifier)
(identifier)))
```
Not just `(path)`
### Example Behavior Test Structure:
```
behavior SimpleBehavior {
walk_around
}
---
(source_file
(declaration
(behavior
name: (identifier)
root: (behavior_node
(action_node (identifier))))))
```
## Regenerating Parser
If grammar.js changes:
```bash
cd /Users/sienna/Development/storybook/tree-sitter-storybook && npm run build
```
This runs `tree-sitter generate` and compiles the parser.
## Test Results (as of 2026-02-09)
✅ All 27 tests passing:
- 17 declaration tests
- 7 behavior tree tests (selector, sequence, repeat, nested, subtree, etc.)
- 3 basic tests
## Behavior Tree Node Types Tested
- `action_node` - Simple action identifiers
- `selector_node` - `? { ... }` - Try options in order
- `sequence_node` - `> { ... }` - Execute in sequence
- `repeat_node` - `* { ... }` - Repeat forever
- `subtree_node` - `@path::to::tree` - Reference external behavior
## Common Issues
1. **Bash tool failing with exit code 1** - Use Serena's execute_shell_command instead
2. **Path structure mismatch** - Remember paths include path_segments wrapper
3. **Missing declaration wrapper** - All top-level items wrap in `(declaration ...)`
4. **Missing behavior_node wrapper** - Behavior children wrap in `(behavior_node ...)`

724
Cargo.lock generated
View File

@@ -91,6 +91,28 @@ dependencies = [
"term",
]
[[package]]
name = "async-trait"
version = "0.1.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "auto_impl"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffdcb70bdbc4d478427380519163274ac86e52916e10f0a8889adf0f96d3fee7"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "autocfg"
version = "1.5.0"
@@ -178,6 +200,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "bytes"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e748733b7cbc798e1434b6ac524f0c1ff2ab456fe201501e6497c8417a4fc33"
[[package]]
name = "cfg-if"
version = "1.0.4"
@@ -276,6 +304,19 @@ dependencies = [
"typenum",
]
[[package]]
name = "dashmap"
version = "5.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856"
dependencies = [
"cfg-if",
"hashbrown 0.14.5",
"lock_api",
"once_cell",
"parking_lot_core",
]
[[package]]
name = "digest"
version = "0.10.7"
@@ -307,6 +348,17 @@ dependencies = [
"winapi",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "either"
version = "1.15.0"
@@ -328,6 +380,29 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
[[package]]
name = "env_filter"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a1c3cc8e57274ec99de65301228b537f1e4eedc1b8e0f9411c6caac8ae7308f"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2daee4ea451f429a58296525ddf28b45a3b64f1acf6587e2067437bb11e218d"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"jiff",
"log",
]
[[package]]
name = "equivalent"
version = "1.0.2"
@@ -373,6 +448,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "form_urlencoded"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
dependencies = [
"percent-encoding",
]
[[package]]
name = "fsevent-sys"
version = "4.1.0"
@@ -382,6 +466,83 @@ dependencies = [
"libc",
]
[[package]]
name = "futures"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
name = "futures-core"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-io"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e575fab7d1e0dcb8d0c7bcf9a63ee213816ab51902e6d244a95819acacf1d4f7"
[[package]]
name = "futures-task"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988"
[[package]]
name = "futures-util"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "generic-array"
version = "0.14.7"
@@ -421,6 +582,12 @@ version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "hashbrown"
version = "0.16.1"
@@ -433,6 +600,114 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "httparse"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
[[package]]
name = "icu_collections"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43"
dependencies = [
"displaydoc",
"potential_utf",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locale_core"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_normalizer"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599"
dependencies = [
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a"
[[package]]
name = "icu_properties"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec"
dependencies = [
"icu_collections",
"icu_locale_core",
"icu_properties_data",
"icu_provider",
"zerotrie",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af"
[[package]]
name = "icu_provider"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614"
dependencies = [
"displaydoc",
"icu_locale_core",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
]
[[package]]
name = "idna"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "indexmap"
version = "2.13.0"
@@ -440,7 +715,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017"
dependencies = [
"equivalent",
"hashbrown",
"hashbrown 0.16.1",
]
[[package]]
@@ -496,6 +771,36 @@ dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
[[package]]
name = "jiff"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c867c356cc096b33f4981825ab281ecba3db0acefe60329f044c1789d94c6543"
dependencies = [
"jiff-static",
"log",
"portable-atomic",
"portable-atomic-util",
"serde_core",
]
[[package]]
name = "jiff-static"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7946b4325269738f270bb55b3c19ab5c5040525f83fd625259422a9d25d9be5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "keccak"
version = "0.1.5"
@@ -585,6 +890,12 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
[[package]]
name = "litemap"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77"
[[package]]
name = "lock_api"
version = "0.4.14"
@@ -633,6 +944,19 @@ dependencies = [
"logos-codegen",
]
[[package]]
name = "lsp-types"
version = "0.94.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c66bfd44a06ae10647fe3f8214762e9369fd4248df1350924b4ef9e770a85ea1"
dependencies = [
"bitflags 1.3.2",
"serde",
"serde_json",
"serde_repr",
"url",
]
[[package]]
name = "memchr"
version = "2.8.0"
@@ -690,6 +1014,17 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "mio"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a69bcab0ad47271a0234d9422b131806bf3968021e5dc9328caf2d4cd58557fc"
dependencies = [
"libc",
"wasi",
"windows-sys 0.61.2",
]
[[package]]
name = "new_debug_unreachable"
version = "1.0.6"
@@ -710,7 +1045,7 @@ dependencies = [
"kqueue",
"libc",
"log",
"mio",
"mio 0.8.11",
"walkdir",
"windows-sys 0.48.0",
]
@@ -774,6 +1109,12 @@ dependencies = [
"windows-link",
]
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "petgraph"
version = "0.6.5"
@@ -799,6 +1140,62 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
[[package]]
name = "pin-project"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "pin-utils"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
name = "portable-atomic"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c33a9471896f1c69cecef8d20cbe2f7accd12527ce60845ff44c153bb2a21b49"
[[package]]
name = "portable-atomic-util"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a9db96d7fa8782dd8c15ce32ffe8680bbd1e978a43bf51a34d39483540495f5"
dependencies = [
"portable-atomic",
]
[[package]]
name = "potential_utf"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77"
dependencies = [
"zerovec",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@@ -1041,6 +1438,30 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.149"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "serde_repr"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "175ee3e80ae9982737ca543e96133087cbd9a485eecc3bc4de9c1a37b47ea59c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.9"
@@ -1060,6 +1481,16 @@ dependencies = [
"keccak",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b"
dependencies = [
"errno",
"libc",
]
[[package]]
name = "similar"
version = "2.7.0"
@@ -1072,18 +1503,41 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e"
[[package]]
name = "slab"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"
[[package]]
name = "smallvec"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
[[package]]
name = "socket2"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f4aa3ad99f2088c990dfa82d367e19cb29268ed67c574d10d0a4bfe71f07e0"
dependencies = [
"libc",
"windows-sys 0.60.2",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "storybook"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"clap",
"env_logger",
"indexmap",
"insta",
"lalrpop",
@@ -1094,10 +1548,13 @@ dependencies = [
"petgraph",
"proptest",
"serde",
"serde_json",
"strsim",
"tempfile",
"thiserror",
"tokio",
"toml",
"tower-lsp",
"walkdir",
]
@@ -1151,6 +1608,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tempfile"
version = "3.24.0"
@@ -1215,6 +1683,57 @@ dependencies = [
"syn",
]
[[package]]
name = "tinystr"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]]
name = "tokio"
version = "1.49.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
dependencies = [
"bytes",
"libc",
"mio 1.1.1",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.61.2",
]
[[package]]
name = "tokio-macros"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tokio-util"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ae9cec805b01e8fc3fd2fe289f89149a9b66dd16786abd8b19cfa7b48cb0098"
dependencies = [
"bytes",
"futures-core",
"futures-sink",
"pin-project-lite",
"tokio",
]
[[package]]
name = "toml"
version = "0.8.23"
@@ -1256,6 +1775,97 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
[[package]]
name = "tower"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
"pin-project",
"pin-project-lite",
"tower-layer",
"tower-service",
]
[[package]]
name = "tower-layer"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
[[package]]
name = "tower-lsp"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4ba052b54a6627628d9b3c34c176e7eda8359b7da9acd497b9f20998d118508"
dependencies = [
"async-trait",
"auto_impl",
"bytes",
"dashmap",
"futures",
"httparse",
"lsp-types",
"memchr",
"serde",
"serde_json",
"tokio",
"tokio-util",
"tower",
"tower-lsp-macros",
"tracing",
]
[[package]]
name = "tower-lsp-macros"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84fd902d4e0b9a4b27f2f440108dc034e1758628a9b702f8ec61ad66355422fa"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tower-service"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]]
name = "tracing"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tracing-core"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
"once_cell",
]
[[package]]
name = "typenum"
version = "1.19.0"
@@ -1298,6 +1908,25 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "url"
version = "2.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
"serde",
"serde_derive",
]
[[package]]
name = "utf8_iter"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
@@ -1618,6 +2247,35 @@ version = "0.51.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7249219f66ced02969388cf2bb044a09756a083d0fab1e566056b04d9fbcaa5"
[[package]]
name = "writeable"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9"
[[package]]
name = "yoke"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954"
dependencies = [
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerocopy"
version = "0.8.39"
@@ -1637,3 +2295,63 @@ dependencies = [
"quote",
"syn",
]
[[package]]
name = "zerofrom"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerotrie"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
]
[[package]]
name = "zerovec"
version = "0.11.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zmij"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"

View File

@@ -1,6 +1,6 @@
[package]
name = "storybook"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
[lib]
@@ -11,6 +11,10 @@ path = "src/lib.rs"
name = "sb"
path = "src/bin/sb.rs"
[[bin]]
name = "storybook-lsp"
path = "src/bin/storybook-lsp.rs"
[dependencies]
logos = "0.14"
lalrpop-util = "0.21"
@@ -26,6 +30,11 @@ notify = "6.0" # Filesystem watching
toml = "0.8" # storybook.toml parsing
walkdir = "2.4" # Directory traversal
serde = { version = "1.0", features = ["derive"] }
# LSP Server dependencies
tower-lsp = "0.20"
tokio = { version = "1", features = ["full"] }
serde_json = "1.0"
env_logger = "0.11"
[build-dependencies]
lalrpop = "0.21"

696
LSP_AUDIT_REPORT.md Normal file
View File

@@ -0,0 +1,696 @@
# LSP Implementation Audit Report
**Date:** 2026-02-12
**Auditor:** LSP Test Engineer
**Status:** ⚠️ COMPILATION ERRORS BLOCKING TESTS
---
## Executive Summary
The Storybook LSP implementation is **functionally comprehensive** but currently **broken in compilation**. The implementation includes all major LSP features with extensive test coverage (147 tests across 7 test modules), but two compilation errors prevent the test suite from running.
**Critical Findings:**
- ✅ 9 core LSP features implemented
- ✅ 147 unit tests written (102 focused tests + supporting modules)
- ❌ 2 compilation errors blocking all tests
- ✅ Tree-sitter grammar: 27/27 tests passing
- ⚠️ Single-file navigation only (no cross-file support)
---
## 1. LSP Features Inventory
### 1.1 Implemented LSP Features (9 Total)
| Feature | Status | Implementation File | Test Coverage |
|---------|--------|---------------------|---------------|
| **Hover** | ✅ Implemented | `hover.rs` | Via integration tests |
| **Completion** | ✅ Implemented | `completion.rs` | 10 dedicated tests |
| **Go-to-Definition** | ✅ Implemented | `definition.rs` | 11 navigation tests |
| **Find References** | ✅ Implemented | `references.rs` | 11 navigation tests |
| **Document Symbols** | ✅ Implemented | `symbols.rs` | 17 edge tests |
| **Diagnostics** | ✅ Implemented | `diagnostics.rs` | 20 dedicated tests |
| **Formatting** | ✅ Implemented | `formatting.rs` | Via integration tests |
| **Rename** | ✅ Implemented | `rename.rs` | Via integration tests |
| **Code Actions** | ✅ Implemented | `code_actions.rs` | 27 dedicated tests |
| **Semantic Tokens** | ✅ Implemented | `semantic_tokens.rs` | Via integration tests |
| **Inlay Hints** | ⚠️ Broken | `inlay_hints.rs` | **COMPILATION ERROR** |
**Total Lines of LSP Code:** 9,788 lines
### 1.2 Feature Details
#### Hover
- **File:** `storybook/src/lsp/hover.rs`
- **Capability:** `HoverProviderCapability::Simple(true)`
- **Functionality:** Displays type information and documentation on hover
- **Status:** Working (integration tested)
#### Completion
- **File:** `storybook/src/lsp/completion.rs`
- **Capability:** Trigger characters: `.`, `:`, `@`
- **Functionality:** Context-aware code completion for identifiers, keywords, templates
- **Tests:** 10 tests in `completion_tests.rs`
- **Known Issue:** Type annotation error on line 428 (`nesting_level` needs `: i32`)
#### Go-to-Definition
- **File:** `storybook/src/lsp/definition.rs`
- **Capability:** `definition_provider: Some(OneOf::Left(true))`
- **Functionality:** Jump to symbol definition within current file
- **Limitation:** Single-file only (no cross-file navigation)
- **Tests:** Covered by 11 navigation tests
#### Find References
- **File:** `storybook/src/lsp/references.rs`
- **Capability:** `references_provider: Some(OneOf::Left(true))`
- **Functionality:** Find all symbol references within current file
- **Features:** Word boundary detection, multiline support
- **Limitation:** Single-file only
- **Tests:** Covered by 11 navigation tests
#### Document Symbols
- **File:** `storybook/src/lsp/symbols.rs`
- **Capability:** `document_symbol_provider: Some(OneOf::Left(true))`
- **Functionality:** Outline view of characters, templates, behaviors, life_arcs, schedules, etc.
- **Coverage:** All declaration types supported
- **Tests:** 17 edge case tests
#### Diagnostics
- **File:** `storybook/src/lsp/diagnostics.rs`
- **Functionality:** Parse error detection and reporting with severity levels
- **Tests:** 20 comprehensive tests
- **Coverage:** Parse errors, position tracking, edge cases (empty input, whitespace, comments)
#### Formatting
- **File:** `storybook/src/lsp/formatting.rs`
- **Capability:** `document_formatting_provider: Some(OneOf::Left(true))`
- **Functionality:** Auto-formatting of Storybook code
- **Status:** Working (integration tested)
#### Rename
- **File:** `storybook/src/lsp/rename.rs`
- **Capability:** Rename with prepare provider
- **Functionality:** Rename symbols across the document
- **Status:** Working (integration tested)
#### Code Actions
- **File:** `storybook/src/lsp/code_actions.rs`
- **Capability:** `code_action_provider: Simple(true)`
- **Tests:** 27 dedicated tests
- **Functionality:** Quick fixes and refactoring actions
#### Semantic Tokens
- **File:** `storybook/src/lsp/semantic_tokens.rs`
- **Capability:** Full semantic token support
- **Functionality:** Fine-grained syntax highlighting based on semantic understanding
- **Legend:** Custom token types (characters, templates, behaviors, etc.)
- **Status:** Working (integration tested)
#### Inlay Hints
- **File:** `storybook/src/lsp/inlay_hints.rs`
- **Capability:** `inlay_hint_provider: Some(OneOf::Left(true))`
- **Known Issue:** Import error - references `crate::project::positions::PositionTracker` which doesn't exist
- **Correct Path:** Should be `crate::position::PositionTracker`
---
## 2. Tree-sitter Grammar Capabilities
### 2.1 Grammar Overview
- **Location:** `/Users/sienna/Development/storybook/tree-sitter-storybook/`
- **Grammar File:** `grammar.js` (326 lines)
- **Test Status:** ✅ 27/27 tests passing
### 2.2 Supported Constructs
#### Declaration Types (10)
1. `use` - Import statements
2. `character` - Character definitions with species, templates
3. `template` - Reusable field templates with includes
4. `life_arc` - State machines with states and transitions
5. `schedule` - Daily schedules with time blocks
6. `behavior` - Behavior trees with all node types
7. `institution` - Organizations and groups
8. `relationship` - Multi-party relationships with named participants
9. `location` - Places and settings
10. `species` - Species definitions with template includes
11. `enum` - Enumeration types
#### Behavior Tree Node Types (5)
- **action_node** - Simple action identifiers (e.g., `walk_around`)
- **selector_node** - `? { ... }` - Try options in order until one succeeds
- **sequence_node** - `> { ... }` - Execute children in sequence
- **repeat_node** - `* { ... }` - Repeat child forever
- **subtree_node** - `@path::to::tree` - Reference external behavior tree
#### Special Features
- **Prose Blocks** - External scanner (C) handles `---tag\n...\n---` syntax
- **Expression Support** - Full condition parsing (e.g., `self.age >= 18 and self.location is wonderland`)
- **Override Syntax** - `@field remove`, `@field append`
- **Path Resolution** - Module-style paths with `::`
### 2.3 Query Files
Located in `tree-sitter-storybook/queries/`:
- **highlights.scm** (154 lines) - Syntax highlighting mappings
- **outline.scm** - Symbol outline for navigation
- **brackets.scm** - Bracket matching (including prose blocks)
- **indents.scm** - Auto-indentation rules
- **injections.scm** - Language injection support
### 2.4 Test Corpus
Located in `test/corpus/`:
- **behaviors.txt** - 7 behavior tree tests
- **declarations.txt** - 17 declaration tests
- **basic.txt** - 3 basic syntax tests
**All 27 tests passing**
---
## 3. Test Coverage Analysis
### 3.1 Test Suite Breakdown
| Test Module | Tests | Focus Area | Status |
|-------------|-------|------------|--------|
| `behavior_tests.rs` | 7 | Behavior tree parsing & symbols | ⚠️ Blocked |
| `code_actions_tests.rs` | 27 | Quick fixes & refactoring | ⚠️ Blocked |
| `completion_tests.rs` | 10 | Code completion contexts | ⚠️ Blocked |
| `diagnostics_tests.rs` | 20 | Error detection & reporting | ⚠️ Blocked |
| `document_edge_tests.rs` | 17 | Symbol extraction & edge cases | ⚠️ Blocked |
| `navigation_tests.rs` | 11 | Go-to-def & find references | ⚠️ Blocked |
| `validation_tests.rs` | 10 | Input validation | ⚠️ Blocked |
| **Total** | **102** | | **⚠️ Blocked** |
**Additional Tests:**
- Integration tests in `tests.rs` and `parser_test.rs` (~45 tests)
- **Grand Total:** ~147 test functions
### 3.2 Coverage Quality
**Well-Covered Areas:**
- ✅ Behavior tree parsing (7 tests covering all node types)
- ✅ Code actions (27 tests - most comprehensive)
- ✅ Diagnostics (20 tests including edge cases)
- ✅ Document symbols (17 tests for all declaration types)
- ✅ Navigation (11 tests for single-file nav)
**Under-Tested Areas:**
- ⚠️ Hover (only integration tests, no dedicated unit tests)
- ⚠️ Formatting (only integration tests)
- ⚠️ Rename (only integration tests)
- ⚠️ Semantic tokens (only integration tests)
- ❌ Inlay hints (broken, no tests can run)
**Missing Test Coverage:**
- ❌ Cross-file navigation (not implemented)
- ❌ Multi-file completion (not implemented)
- ❌ Workspace-level symbol resolution (not implemented)
- ❌ Use declaration resolution (not implemented)
- ❌ Performance/stress tests (large files, many symbols)
- ❌ Concurrency tests (multiple clients, rapid edits)
---
## 4. Critical Issues Blocking Tests
### Issue #1: Inlay Hints Import Error
**File:** `storybook/src/lsp/inlay_hints.rs:134`
**Error:**
```
error[E0433]: failed to resolve: could not find `project` in the crate root
--> src/lsp/inlay_hints.rs:134:28
|
134 | positions: &mut crate::project::positions::PositionTracker,
| ^^^^^^^ could not find `project` in the crate root
```
**Root Cause:** Incorrect module path
**Expected Path:** `crate::position::PositionTracker`
**Fix Required:** Change `crate::project::positions::PositionTracker``crate::position::PositionTracker`
**Impact:** Blocks all LSP tests from compiling
---
### Issue #2: Completion Type Annotation Error
**File:** `storybook/src/lsp/completion.rs:428`
**Error:**
```
error[E0689]: can't call method `saturating_sub` on ambiguous numeric type `{integer}`
--> src/lsp/completion.rs:428:60
|
428 | Token::RBrace => nesting_level = nesting_level.saturating_sub(1),
| ^^^^^^^^^^^^^^
```
**Root Cause:** Missing type annotation on `nesting_level` variable
**Fix Required:** Add type annotation `: i32` to `let mut nesting_level`
**Impact:** Blocks all LSP tests from compiling
---
## 5. Architecture Assessment
### 5.1 Current Architecture
```
StorybookLanguageServer
├── documents: Arc<RwLock<HashMap<Url, Document>>>
│ └── Per-document state (AST, symbols, errors, positions)
└── workspace: Arc<RwLock<WorkspaceState>>
├── name_table: NameTable (currently unused)
└── file_urls: HashMap<usize, Url>
```
**Document Model:**
```rust
pub struct Document {
pub text: String,
pub ast: Option<SourceFile>,
pub parse_errors: Vec<ParseError>,
pub positions: PositionTracker,
}
```
**Symbol Table:** Built per-document from AST via `symbols::extract_symbols()`
### 5.2 Single-File vs. Multi-File Navigation
#### Currently Implemented (Single-File)
- ✅ Each document maintains its own symbol table
- ✅ Go-to-definition works within current file
- ✅ Find references works within current file
- ✅ Fast and simple (no cross-file coordination)
#### Not Implemented (Multi-File)
- ❌ No workspace-level NameTable integration
- ❌ Use declarations not resolved
- ❌ Cross-file template references not resolved
- ❌ Species references not resolved across files
- ❌ No global symbol index
**Why Multi-File Not Implemented:**
> "Current single-file solution covers 80% of use cases for MVP. Cross-file can be added later as enhancement without breaking existing features." - From `lsp-phase4-navigation` memory
### 5.3 Workspace State (Stub Implementation)
The `WorkspaceState` struct exists but is **not fully utilized**:
```rust
fn rebuild(&mut self, documents: &HashMap<Url, Document>) {
self.name_table = NameTable::new();
self.file_urls.clear();
for (file_index, (url, doc)) in documents.iter().enumerate() {
self.file_urls.insert(file_index, url.clone());
if let Some(ref ast) = doc.ast {
// TODO: Properly merge file name tables with file_index
// For now, we'll use the simple single-file approach
let _ = (ast, file_index); // Use variables to avoid warnings
}
}
}
```
**Observation:** The NameTable infrastructure exists in `crate::resolve::names::NameTable` but is not being populated or used by the LSP server.
---
## 6. Stability Assessment
### 6.1 Known Stability Issues
1. **Compilation Failures** (Severity: CRITICAL)
- Two errors preventing tests from running
- Both are simple fixes (import path + type annotation)
2. **Limited Cross-File Support** (Severity: MEDIUM)
- Users cannot navigate to definitions in other files
- Use declarations don't resolve to imports
- Limits usefulness for multi-file projects
3. **Missing Error Handling** (Severity: LOW)
- Some LSP methods use `.unwrap()` or `.expect()` without graceful degradation
- Could cause server crashes on unexpected input
4. **Performance Unknowns** (Severity: MEDIUM)
- No stress tests for large files (>10,000 lines)
- No benchmarks for workspace rebuilds
- Unknown behavior with many open documents (100+)
### 6.2 Code Quality Observations
**Strengths:**
- ✅ Comprehensive async/await usage (tower-lsp pattern)
- ✅ Proper use of RwLock for concurrent document access
- ✅ Modular design (separate files for each LSP feature)
- ✅ Rich test coverage (102 focused unit tests)
**Weaknesses:**
- ⚠️ Incomplete type annotations (causing compilation errors)
- ⚠️ Unused infrastructure (WorkspaceState, NameTable)
- ⚠️ Some features lack dedicated unit tests (hover, formatting, rename)
- ⚠️ No CI/CD automation mentioned in codebase
---
## 7. Zed Editor Integration
### 7.1 Extension Structure
- **Location:** `/Users/sienna/Development/storybook/zed-storybook/`
- **Type:** WASM-based Zed extension
- **Grammar:** Embeds tree-sitter-storybook
- **LSP:** Configured to use Storybook LSP server
### 7.2 Extension Components
- `extension.toml` - Extension metadata
- `languages/storybook/config.toml` - Language configuration
- File extension: `.sb`
- Line comments: `//`
- Block comments: `/* */`
- `grammars/storybook/` - Embedded tree-sitter grammar
- `extension.wasm` - Compiled extension (405KB)
### 7.3 Integration Status
- ✅ Tree-sitter grammar integrated
- ✅ Syntax highlighting configured
- ⚠️ LSP server integration status unknown (needs testing)
- ⚠️ Extension build process documented (`build-extension.sh`)
---
## 8. Test Strategy Recommendations
### 8.1 Immediate Actions (Phase 1: Stabilization)
**Priority 1: Fix Compilation Errors** (Est: 15 minutes)
1. Fix `inlay_hints.rs` import path
2. Fix `completion.rs` type annotation
3. Verify all tests compile: `cargo test --lib lsp --no-run`
4. Run full test suite: `cargo test --lib lsp`
**Priority 2: Verify Test Suite** (Est: 30 minutes)
1. Confirm all 102 tests pass
2. Document any unexpected failures
3. Identify flaky tests
**Priority 3: Add Missing Unit Tests** (Est: 2-4 hours)
1. Create `hover_tests.rs` with 10+ hover scenarios
2. Create `formatting_tests.rs` with 10+ formatting cases
3. Create `rename_tests.rs` with 10+ rename scenarios
4. Create `semantic_tokens_tests.rs` with token validation
### 8.2 Short-Term Enhancements (Phase 2: Coverage)
**Add Edge Case Tests** (Est: 4-6 hours)
- Large file handling (>10,000 lines)
- Deeply nested structures (>10 levels)
- Unicode identifiers and content
- Malformed input (truncated files, invalid UTF-8)
- Empty documents
- Documents with only comments
**Add Integration Tests** (Est: 4-6 hours)
- Multi-file scenarios (even if features not fully supported)
- Rapid editing sequences (type, delete, undo)
- Concurrent client connections
- Document sync edge cases (did_open, did_change, did_close)
**Add Performance Tests** (Est: 3-4 hours)
- Benchmark parse time for files of varying sizes
- Benchmark symbol extraction
- Benchmark completion latency
- Benchmark workspace rebuild time
- Memory usage profiling
### 8.3 Long-Term Goals (Phase 3: Multi-File Support)
**Cross-File Navigation** (Est: 1-2 weeks)
1. Populate WorkspaceState.name_table from all documents
2. Implement use declaration resolution
3. Implement cross-file go-to-definition
4. Implement cross-file find references
5. Add comprehensive multi-file tests
**Workspace Management** (Est: 1 week)
1. File watching for unopened files
2. Incremental rebuilds (only changed files)
3. Lazy loading for large workspaces
4. Cache invalidation strategies
### 8.4 Infrastructure (Phase 4: Automation)
**CI/CD Setup** (Est: 1-2 days)
1. GitHub Actions workflow for tests
2. Automated test runs on PRs
3. Code coverage reporting (tarpaulin or similar)
4. Performance regression detection
**Documentation** (Est: 2-3 days)
1. Testing guide for contributors
2. LSP feature documentation
3. Architecture diagrams
4. Troubleshooting guide
---
## 9. Coverage Targets
### 9.1 Current Coverage Estimate
Based on manual analysis:
- **Completion:** ~60% (10 tests, but missing edge cases)
- **Diagnostics:** ~90% (20 comprehensive tests)
- **Symbols:** ~85% (17 edge case tests)
- **Navigation:** ~70% (11 tests, single-file only)
- **Code Actions:** ~80% (27 tests)
- **Hover:** ~40% (only integration tests)
- **Formatting:** ~40% (only integration tests)
- **Rename:** ~40% (only integration tests)
- **Semantic Tokens:** ~40% (only integration tests)
- **Inlay Hints:** 0% (broken)
**Overall Estimated Coverage:** ~55-60%
### 9.2 Target Coverage Goals
**Phase 1 (Stabilization):**
- All features compile: 100%
- All existing tests pass: 100%
- Critical paths covered: 70%
**Phase 2 (Enhancement):**
- Overall line coverage: 80%
- Branch coverage: 70%
- All features have dedicated unit tests: 100%
**Phase 3 (Multi-File):**
- Cross-file features: 70%
- Integration tests: 80%
- Overall coverage: 85%
---
## 10. Prioritized Issues List
### Critical (Blocking)
1. **Fix inlay_hints.rs import error** - Prevents all tests from running
2. **Fix completion.rs type annotation** - Prevents all tests from running
### High Priority (Stability)
3. **Add unit tests for hover** - Feature exists but untested
4. **Add unit tests for formatting** - Feature exists but untested
5. **Add unit tests for rename** - Feature exists but untested
6. **Add unit tests for semantic_tokens** - Feature exists but untested
7. **Add edge case tests** - Unicode, large files, malformed input
8. **Add performance benchmarks** - Unknown behavior at scale
### Medium Priority (Enhancements)
9. **Implement cross-file navigation** - High user value
10. **Populate workspace NameTable** - Infrastructure exists but unused
11. **Add integration tests** - Multi-file, concurrent access
12. **Set up CI/CD** - Automation for confidence
### Low Priority (Nice-to-Have)
13. **Improve error handling** - Remove unwraps, add graceful degradation
14. **Add code coverage reporting** - Quantify test coverage
15. **Create architecture documentation** - Onboarding for contributors
---
## 11. Test Plan Summary
### Testing Phases
**Phase 1: Fix & Validate (1 day)**
- Fix 2 compilation errors
- Run and document all existing test results
- Identify any test failures
**Phase 2: Fill Gaps (3-5 days)**
- Add unit tests for hover, formatting, rename, semantic_tokens
- Add edge case tests (large files, unicode, malformed input)
- Add performance benchmarks
**Phase 3: Integration (1 week)**
- Add multi-file test scenarios
- Add concurrent access tests
- Add stress tests
**Phase 4: Automation (2-3 days)**
- Set up CI/CD
- Add code coverage reporting
- Document testing procedures
**Phase 5: Multi-File Support (2-3 weeks)**
- Implement cross-file navigation
- Implement workspace NameTable population
- Comprehensive multi-file testing
---
## 12. Success Metrics
### Short-Term (1 week)
- ✅ All tests compile without errors
- ✅ 100% of existing tests pass
- ✅ All LSP features have dedicated unit tests
- ✅ Code coverage > 70%
### Medium-Term (1 month)
- ✅ Code coverage > 80%
- ✅ CI/CD runs all tests automatically
- ✅ Performance benchmarks established
- ✅ No known critical bugs
### Long-Term (3 months)
- ✅ Cross-file navigation implemented
- ✅ Code coverage > 85%
- ✅ Comprehensive integration tests
- ✅ Documentation complete
---
## 13. Resources Required
### Tools
- Rust testing framework (cargo test) - ✅ Already installed
- Code coverage tool (tarpaulin or llvm-cov) - ⚠️ Needs installation
- CI/CD platform (GitHub Actions) - ✅ Available
- Benchmarking tool (criterion) - ⚠️ Needs integration
### Time Estimates
- **Phase 1 (Fix & Validate):** 1 day
- **Phase 2 (Fill Gaps):** 3-5 days
- **Phase 3 (Integration):** 5-7 days
- **Phase 4 (Automation):** 2-3 days
- **Phase 5 (Multi-File):** 10-15 days
**Total Estimated Time:** 21-31 days (4-6 weeks)
---
## 14. Conclusion
The Storybook LSP implementation is **architecturally sound** and **feature-rich**, with 9 major LSP features implemented and 147 tests written. However, **two simple compilation errors** are blocking the entire test suite from running.
**Immediate Action Required:**
1. Fix the two compilation errors (15 minutes)
2. Verify all 102 tests pass
3. Proceed with test gap filling and stabilization
**Strategic Recommendation:**
- Focus on **stabilization first** (Phases 1-2) before attempting multi-file support
- The single-file navigation is sufficient for MVP
- Cross-file support can be added incrementally without breaking existing features
**Risk Assessment:**
- **Low risk** to fix compilation errors and achieve stability
- **Medium risk** for multi-file support (requires architectural changes)
- **High value** in completing test coverage for existing features
The implementation is **close to production-ready** once compilation errors are resolved and test coverage gaps are filled.
---
## Appendix A: Test File Inventory
```
storybook/src/lsp/
├── behavior_tests.rs (7 tests)
├── code_actions_tests.rs (27 tests)
├── completion_tests.rs (10 tests)
├── diagnostics_tests.rs (20 tests)
├── document_edge_tests.rs (17 tests)
├── navigation_tests.rs (11 tests)
├── validation_tests.rs (10 tests)
├── tests.rs (~30 integration tests)
└── parser_test.rs (~15 parser tests)
Total: ~147 test functions
```
## Appendix B: LSP Method Implementation Status
```rust
impl LanguageServer for StorybookLanguageServer {
async fn initialize()
async fn initialized()
async fn shutdown()
async fn did_open()
async fn did_change()
async fn did_close()
async fn hover()
async fn completion()
async fn goto_definition()
async fn references()
async fn formatting()
async fn rename()
async fn code_action()
async fn semantic_tokens_full()
async fn inlay_hint() // Broken import
}
```
## Appendix C: Tree-sitter Test Results
```bash
cd /Users/sienna/Development/storybook/tree-sitter-storybook
npm run test
```
**Output:**
```
✓ Simple character
✓ Use declaration
✓ Prose block
✓ Simple Behavior
✓ Selector Behavior
✓ Sequence Behavior
✓ Nested Behavior
✓ Repeat Behavior
✓ Behavior with Subtree
✓ Complex Nested Behavior
✓ Use declaration - simple
✓ Use declaration - grouped import
✓ Use declaration - wildcard
✓ Character - full featured
✓ Template with includes
✓ Life arc with states and transitions
✓ Schedule with time blocks
✓ Behavior tree - all node types
✓ Institution
✓ Relationship with participants
✓ Location
✓ Species with includes
✓ Enum declaration
✓ Override syntax
✓ Complex expressions in conditions
✓ List and object values
✓ Dotted field paths
27/27 tests passing ✅
```

464
LSP_LEXER_AUDIT.md Normal file
View File

@@ -0,0 +1,464 @@
# LSP Lexer Usage Audit
**Date:** 2026-02-12
**Auditor:** LSP Test Engineer
**Purpose:** Verify all LSP modules use the lexer exclusively (no ad-hoc parsing)
---
## Executive Summary
**Status:** ⚠️ **Mixed Compliance**
- **3 modules** properly use lexer ✅
- **8 modules** use ad-hoc parsing ❌
- **Critical Risk:** Hardcoded keyword lists in 3 modules
**Risk Level:** MEDIUM
- Inconsistent behavior between LSP and compiler
- Vulnerability to keyword changes (like recent behavior tree updates)
- Maintenance burden from duplicate logic
---
## Audit Results by Module
### ✅ Compliant Modules (Using Lexer)
#### 1. **completion.rs** - ✅ GOOD (with caveats)
**Status:** Uses Lexer properly for tokenization
**Lexer Usage:**
```rust
use crate::syntax::lexer::{Lexer, Token};
let lexer = Lexer::new(before);
```
**Found at lines:** 135, 142, 269, 277, 410, 417
**Issue:** ⚠️ Contains hardcoded keyword strings (8 occurrences)
- Lines with hardcoded keywords: "character", "template", "behavior", etc.
- **Risk:** Keywords could get out of sync with lexer
**Recommendation:** Extract keywords from lexer Token enum instead of hardcoding
---
#### 2. **semantic_tokens.rs** - ✅ EXCELLENT
**Status:** Uses Lexer exclusively, no manual parsing
**Lexer Usage:**
```rust
use crate::syntax::lexer::{Lexer, Token};
```
**Assessment:** **Best practice example** - Uses lexer for all tokenization, no hardcoded keywords, no manual string manipulation
---
#### 3. **code_actions.rs** - ✅ GOOD (with caveats)
**Status:** Uses Lexer for tokenization
**Issues:**
- ⚠️ Contains 15 instances of manual string parsing (`.split()`, `.chars()`, `.lines()`)
- ⚠️ Has 4 hardcoded keyword strings
- **Concern:** Mixed approach - uses lexer sometimes, manual parsing other times
**Recommendation:** Refactor to use lexer consistently throughout
---
### ❌ Non-Compliant Modules (Ad-Hoc Parsing)
#### 4. **hover.rs** - ❌ CRITICAL ISSUE
**Status:** NO lexer usage, extensive manual parsing
**Problems:**
**A. Manual Character-by-Character Parsing**
```rust
// Line 51-77: extract_word_at_position()
let chars: Vec<char> = line.chars().collect();
// Custom word boundary detection
while start > 0 && is_word_char(chars[start - 1]) {
start -= 1;
}
```
**Risk:** Custom tokenization logic differs from compiler's lexer
**B. Hardcoded Keyword List** (Lines 20-39)
```rust
match word.as_str() {
"character" => "**character** - Defines a character entity...",
"template" => "**template** - Defines a reusable field template...",
"life_arc" => "**life_arc** - Defines a state machine...",
"schedule" => "**schedule** - Defines a daily schedule...",
"behavior" => "**behavior** - Defines a behavior tree...",
"institution" => "**institution** - Defines an organization...",
"relationship" => "**relationship** - Defines a multi-party...",
"location" => "**location** - Defines a place...",
"species" => "**species** - Defines a species...",
"enum" => "**enum** - Defines an enumeration...",
"use" => "**use** - Imports declarations...",
"from" => "**from** - Applies templates...",
"include" => "**include** - Includes another template...",
"state" => "**state** - Defines a state...",
"on" => "**on** - Defines a transition...",
"strict" => "**strict** - Enforces that a template...",
_ => return None,
}
```
**Risk:** HIGH
- 16 hardcoded keywords that must stay in sync with lexer
- If lexer adds/removes keywords, hover won't update automatically
- Recent behavior tree changes could have broken this
**Recommendation:**
```rust
// BEFORE (current):
let word = extract_word_at_position(line_text, character)?;
// AFTER (proposed):
let lexer = Lexer::new(line_text);
let token_at_pos = find_token_at_position(&lexer, character)?;
match token_at_pos {
Token::Character => "**character** - Defines...",
Token::Template => "**template** - Defines...",
// etc - using Token enum from lexer
}
```
---
#### 5. **formatting.rs** - ❌ MANUAL PARSING
**Status:** NO lexer usage, line-by-line text processing
**Problems:**
```rust
// Line 53: Manual line processing
for line in text.lines() {
let trimmed = line.trim();
// Custom logic for braces, colons, etc.
}
```
**Risk:** MEDIUM
- Custom formatting logic may not respect language semantics
- Could format code incorrectly if it doesn't understand context
**Recommendation:** Use lexer to tokenize, then format based on tokens
- Preserves semantic understanding
- Respects string literals, comments, etc.
---
#### 6. **diagnostics.rs** - ❌ MANUAL PARSING
**Status:** NO lexer usage, manual brace counting
**Problems:**
```rust
// Lines 34-37: Manual brace counting
for (line_num, line) in text.lines().enumerate() {
let open_braces = line.chars().filter(|&c| c == '{').count();
let close_braces = line.chars().filter(|&c| c == '}').count();
}
// Line 79: Character-by-character processing
for ch in text.chars() {
// Custom logic
}
```
**Risk:** HIGH
- Brace counting doesn't account for braces in strings: `character Alice { name: "{" }`
- Doesn't respect comments: `// This { is a comment`
- Could generate false diagnostics
**Recommendation:** Use lexer tokens to track brace pairs accurately
---
#### 7. **references.rs** - ❌ MANUAL STRING PARSING
**Status:** NO lexer usage
**Problems:**
```rust
// Manual string parsing for word boundaries
```
**Risk:** MEDIUM
- May not correctly identify symbol boundaries
- Could match partial words
**Recommendation:** Use lexer to identify identifiers
---
#### 8. **rename.rs** - ❌ HARDCODED KEYWORDS
**Status:** NO lexer usage, contains hardcoded strings
**Problems:**
- 2 hardcoded keyword strings
- Manual symbol identification
**Risk:** MEDIUM
- May incorrectly rename keywords or miss valid renames
**Recommendation:** Use lexer to distinguish keywords from identifiers
---
#### 9. **definition.rs** - ⚠️ UNCLEAR
**Status:** No obvious lexer usage, no obvious manual parsing
**Assessment:** Likely uses AST-based approach (acceptable)
- May rely on symbols extracted from AST
- **Acceptable** if using `Document.ast` for symbol lookup
**Recommendation:** Verify it's using AST, not string parsing
---
#### 10. **inlay_hints.rs** - ⚠️ UNCLEAR
**Status:** No lexer usage detected, no obvious parsing
**Assessment:** Minimal code inspection needed
- May be stub or use AST-based approach
**Recommendation:** Full inspection needed
---
#### 11. **symbols.rs** - ✅ LIKELY COMPLIANT
**Status:** No manual parsing detected
**Assessment:** Appears to extract symbols from AST
- **Acceptable approach** - AST is the canonical representation
- No need for lexer if working from AST
---
## Summary Table
| Module | Lexer Usage | Manual Parsing | Hardcoded Keywords | Risk Level |
|--------|-------------|----------------|-------------------|------------|
| **completion.rs** | ✅ Yes | ❌ No | ⚠️ 8 keywords | MEDIUM |
| **semantic_tokens.rs** | ✅ Yes | ❌ No | ✅ None | LOW |
| **code_actions.rs** | ✅ Yes | ⚠️ 15 instances | ⚠️ 4 keywords | MEDIUM |
| **hover.rs** | ❌ No | ⚠️ Extensive | ⚠️ 16 keywords | **HIGH** |
| **formatting.rs** | ❌ No | ⚠️ Line-by-line | ✅ None | MEDIUM |
| **diagnostics.rs** | ❌ No | ⚠️ Char counting | ✅ None | **HIGH** |
| **references.rs** | ❌ No | ⚠️ Yes | ✅ None | MEDIUM |
| **rename.rs** | ❌ No | ⚠️ Yes | ⚠️ 2 keywords | MEDIUM |
| **definition.rs** | ⚠️ Unknown | ⚠️ Unknown | ✅ None | LOW |
| **inlay_hints.rs** | ⚠️ Unknown | ⚠️ Unknown | ✅ None | LOW |
| **symbols.rs** | N/A (AST) | ❌ No | ✅ None | LOW |
---
## Critical Findings
### 1. **hover.rs** - Highest Risk
- 16 hardcoded keywords
- Custom tokenization logic
- **Impact:** Recent behavior tree keyword changes may have broken hover
- **Fix Priority:** HIGH
### 2. **diagnostics.rs** - High Risk
- Manual brace counting fails in strings/comments
- Could generate false errors
- **Fix Priority:** HIGH
### 3. **Hardcoded Keywords** - Maintenance Burden
- **Total:** 30 hardcoded keyword strings across 4 files
- **Risk:** Keywords get out of sync with lexer
- **Recent Example:** Behavior tree syntax changes could have broken these
---
## Recommended Fixes
### Priority 1: hover.rs Refactoring
**Estimated Time:** 2-3 hours
**Changes:**
1. Add lexer import: `use crate::syntax::lexer::{Lexer, Token};`
2. Replace `extract_word_at_position()` with lexer-based token finding
3. Replace keyword match with Token enum match
4. Remove hardcoded keyword strings
**Benefits:**
- Automatic sync with lexer changes
- Consistent tokenization with compiler
- Easier maintenance
**Example Code:**
```rust
pub fn get_hover_info(text: &str, line: usize, character: usize) -> Option<Hover> {
let line_text = text.lines().nth(line)?;
let lexer = Lexer::new(line_text);
// Find token at character position
let token_at_pos = find_token_at_position(&lexer, character)?;
let content = match token_at_pos {
Token::Character => "**character** - Defines a character entity...",
Token::Template => "**template** - Defines a reusable field template...",
// Use Token enum - stays in sync automatically
};
Some(Hover { /* ... */ })
}
```
---
### Priority 2: diagnostics.rs Refactoring
**Estimated Time:** 1-2 hours
**Changes:**
1. Use lexer to tokenize text
2. Count LBrace/RBrace tokens (not characters)
3. Ignore braces in strings and comments automatically
**Benefits:**
- Correct handling of braces in all contexts
- No false positives
---
### Priority 3: Extract Keyword Definitions
**Estimated Time:** 1 hour
**Changes:**
1. Create `src/syntax/keywords.rs` module
2. Define keyword list from lexer Token enum
3. Use in completion, code_actions, rename
**Benefits:**
- Single source of truth for keywords
- Easy to keep in sync
---
## Testing Recommendations
After fixes, add tests for:
1. **Hover on keywords in strings** - Should NOT show hover
```rust
character Alice { name: "character" } // hovering "character" in string
```
2. **Diagnostics with braces in strings** - Should NOT count
```rust
character Alice { name: "{}" } // should not report brace mismatch
```
3. **Lexer consistency tests** - Verify LSP uses same tokens as compiler
```rust
#[test]
fn test_hover_uses_lexer_keywords() {
// Ensure hover keywords match lexer Token enum
}
```
---
## Long-Term Recommendations
### 1. Establish Coding Standards
Document that LSP modules should:
- ✅ Use lexer for all tokenization
- ✅ Use AST for semantic analysis
- ❌ Never do manual string parsing
- ❌ Never hardcode keywords
### 2. Code Review Checklist
Add to PR reviews:
- [ ] Does LSP code use lexer for tokenization?
- [ ] Are there any hardcoded keyword strings?
- [ ] Is there any manual `.split()`, `.chars()`, `.find()` parsing?
### 3. Shared Utilities
Create `src/lsp/lexer_utils.rs` with:
- `find_token_at_position()` - Common utility
- `get_keyword_docs()` - Centralized keyword documentation
- `is_keyword()` - Token-based keyword checking
---
## Impact Assessment
### Current State Risks
**Behavior Tree Changes (Recent):**
- Lexer/parser updated with new keywords
- ❌ `hover.rs` still has old hardcoded list
- ⚠️ Users may not see hover for new keywords
**Future Keyword Changes:**
- Any new keywords require updates in 4 separate files
- Easy to miss one, causing inconsistent behavior
**Bug Examples:**
```rust
// diagnostics.rs bug:
character Alice { description: "Contains } brace" }
// ^ Reports false "unmatched brace" error
// hover.rs bug:
character Alice { /* hover on "character" keyword here */ }
// ^ Works
"In a character block..." // hover on "character" in string
// ^ Also shows hover (WRONG - it's just a word in a string)
```
---
## Conclusion
**Overall Assessment:** NEEDS IMPROVEMENT
- **Compliance Rate:** 27% (3/11 modules)
- **Risk Level:** MEDIUM-HIGH
- **Recommended Action:** Refactor hover.rs and diagnostics.rs as priority
**Benefits of Fixing:**
1. Consistency with compiler behavior
2. Automatic sync with language changes
3. Reduced maintenance burden
4. Fewer bugs from edge cases
**Next Steps:**
1. Create GitHub issues for each Priority 1-2 fix
2. Refactor hover.rs (highest risk)
3. Refactor diagnostics.rs (high risk)
4. Extract keyword definitions (prevents future issues)
5. Add lexer usage to code review checklist
---
## Appendix: Files Analyzed
```
Total LSP modules audited: 11
✅ Compliant: 3 (27%)
⚠️ Partially compliant: 1 (9%)
❌ Non-compliant: 7 (64%)
Files examined:
- src/lsp/completion.rs
- src/lsp/hover.rs
- src/lsp/semantic_tokens.rs
- src/lsp/inlay_hints.rs
- src/lsp/definition.rs
- src/lsp/formatting.rs
- src/lsp/rename.rs
- src/lsp/references.rs
- src/lsp/code_actions.rs
- src/lsp/symbols.rs
- src/lsp/diagnostics.rs
```

1021
LSP_TEST_STRATEGY.md Normal file

File diff suppressed because it is too large Load Diff

906
TEAM_PLAN.md Normal file
View File

@@ -0,0 +1,906 @@
# Storybook Stabilization & Enhancement Team Plan
## Overview
This plan outlines a multi-agent team structure to tackle six critical workstreams:
1. **LSP Testing & Stabilization** - Comprehensive testing and bug fixing for the Language Server Protocol implementation
2. **Behavior Tree Refactoring** - Replacing symbolic syntax with human-friendly keywords (repeat, if, etc.)
3. **Resource Linking System** - Designing how to link behavior trees and schedules to characters and institutions
4. **SBIR Specification Validation** - Identifying gaps and missing elements in the IR spec
5. **Schedule System Enhancement** - Implementing year-long, composable schedule support
6. **Language Documentation** - Creating comprehensive, beginner-friendly documentation for non-programmer world builders
**Note:** The codebase uses "characters" not "entities". We can reuse existing infrastructure where possible.
## Team Structure
### Team Lead (Coordinator)
**Role:** Overall coordination, task assignment, progress tracking, and integration oversight
**Responsibilities:**
- Create and manage task board
- Assign tasks to specialists
- Monitor progress and unblock agents
- Coordinate dependencies between workstreams
- Synthesize findings into cohesive deliverables
- Handle final integration validation
---
### Agent 1: LSP Test Engineer
**Subagent Type:** `general-purpose` (needs full read/write/edit access)
**Primary Focus:** Testing and stabilizing the LSP implementation
**Assigned Tasks:**
- Task #1: Audit LSP implementation and create test strategy
- Task #2: Implement test suite and stabilization fixes
**Key Responsibilities:**
- Inventory all LSP features (hover, completion, go-to-definition, diagnostics, references, etc.)
- Analyze Tree-sitter grammar coverage
- Document current test coverage gaps
- Create comprehensive test strategy with prioritization
- Implement unit tests for each LSP feature
- Implement integration tests for multi-file scenarios
- Add edge case and error condition tests
- Fix identified stability issues
- Create regression tests
- Set up CI/CD for automated testing
**Deliverables:**
- LSP feature inventory document
- Test strategy and plan document
- Comprehensive test suite (unit + integration)
- Stability fixes and bug reports
- CI/CD configuration
- Testing documentation
**Success Criteria:**
- All LSP features have test coverage
- No known stability issues remain
- Tests run automatically in CI
- Documentation enables future contributors to add tests
---
### Agent 2: Behavior Tree Language Designer
**Subagent Type:** `general-purpose` (needs full read/write/edit access)
**Primary Focus:** Redesigning behavior tree syntax with human-friendly keywords
**Assigned Tasks:**
- Task #3: Analyze current syntax and design keyword system
- Task #4: Implement transformation system
**Key Responsibilities:**
- Catalog all current symbolic behavior tree representations
- Design keyword mappings:
- Decorators: `repeat(N)`, `retry(N)`, `timeout(seconds)`, `cooldown(seconds)`, `invert`, `if(condition)`
- Control: `if(condition)`, `sequence`, `selector`, `parallel`
- Actions: Human-readable action calls
- Create comprehensive grammar specification
- Update Tree-sitter grammar
- Modify parser to support new keywords
- Update compiler to translate keywords → SBIR
- Implement backward compatibility (if needed)
- Validate with existing examples (Alice in Wonderland)
- Update language documentation
**Deliverables:**
- Current syntax inventory
- Keyword mapping specification
- Grammar design document
- Example transformations (before/after)
- Updated Tree-sitter grammar
- Parser modifications
- Compiler updates
- Migration guide (if backward compatibility breaks)
- Updated documentation with new syntax examples
**Success Criteria:**
- All symbolic constructs have keyword equivalents
- Syntax is intuitive and readable
- Parser handles new keywords correctly
- Compiler generates correct SBIR
- Examples validate successfully
- Documentation is complete
---
### Agent 3: Resource Linking Architect
**Subagent Type:** `general-purpose` (needs full read/write/edit access)
**Primary Focus:** Designing and implementing comprehensive linking system for behaviors and schedules
**Assigned Tasks:**
- Task #5: Design resource linking system (behaviors + schedules)
- Task #6: Implement linking system
**Important:** Agent must discuss design and implementation with user at key checkpoints to ensure correctness.
**Key Responsibilities:**
**Behavior Linking:**
- Design behavior linking mechanisms for **characters** (note: codebase uses "characters" not "entities"):
- Direct assignment: `entity Alice { link behavior: WorkAtBakery }`
- Multiple behaviors: `link behaviors: [WorkAtBakery, SocialRoutine, RestAndRecover]`
- Priority system: `link behaviors: [{ tree: WorkAtBakery, priority: normal }, ...]`
- Conditional selection: `link behavior: WorkAtBakery when role == baker`
- Context-based: Link behaviors based on location/institution/state
- Design behavior linking for **institutions**:
- Institution default behaviors: `institution Bakery { link behavior: BakeryOperations }`
- Role-specific behaviors: `role worker { link behavior: BakeryWork }`
**Schedule Linking:**
- Design schedule linking mechanisms for **characters**:
- Direct assignment: `entity Alice { link schedule: BakerSchedule }`
- Conditional schedules: `link schedule: WorkdaySchedule when employed`
- State-based schedules: Different schedules for different life arc states
- Override system: Special schedules for events/holidays
- Design schedule linking for **institutions**:
- Operating hours: `institution Bakery { link schedule: BakeryHours }`
- Seasonal variations: `link schedule: WinterHours when season == winter`
**General Linking Design:**
- Design unified `link` keyword syntax
- Support linking in:
- Character definitions
- Species definitions (default links)
- Template definitions (inherited links)
- Institution definitions
- Institution role definitions
- Design SBIR representation:
- Link metadata in CHARACTERS section (currently ENTITIES in spec)
- Link metadata in INSTITUTIONS section
- Link resolution rules
- Priority and selection metadata
- Reuse existing infrastructure where possible
- Consider use cases:
- Character with multiple behaviors selected by context
- Character schedule changes based on life events
- Institution behaviors for different roles
- Institution schedules (hours of operation)
- Dynamic link resolution at runtime
- Coordinate with Behavior Tree Designer, Schedule Architect, and SBIR Analyst
- **Checkpoint 1:** Present linking design to user for review before implementation
- Implement parser support for `link` keyword (reuse existing parser infrastructure)
- Implement compiler translation to SBIR
- Create runtime link resolver
- **Checkpoint 2:** Review implementation approach with user before finalizing
- Validate with examples
- **Checkpoint 3:** Demo working implementation to user
**Deliverables:**
- Resource linking system architecture document
- Syntax specification for `link` keyword (behaviors + schedules)
- SBIR format proposal for links in CHARACTERS and INSTITUTIONS sections
- Selection/resolution algorithm specification
- Updated language syntax
- Parser modifications for `link` keyword
- Compiler updates
- Link resolver implementation
- Comprehensive examples:
- Entity with linked behavior
- Entity with linked schedule
- Entity with multiple linked behaviors
- Institution with linked behavior
- Institution with linked schedule (operating hours)
- Conditional/contextual linking
- State-based linking
- Priority-based selection
- Test suite for linking system
- Documentation
**Success Criteria:**
- Unified `link` keyword works for both behaviors and schedules
- Characters can link to behaviors AND schedules
- Institutions can link to behaviors AND schedules
- User has approved design and implementation approach
- Support for multiple links with selection rules
- Support for conditional/contextual selection
- Runtime resolver selects correct behavior/schedule
- Examples demonstrate real-world use cases
- SBIR encoding captures all linking semantics
- Integration with schedule system and life arcs works seamlessly
---
### Agent 4: Language Documentation Writer
**Subagent Type:** `general-purpose` (needs read access + ability to write documentation)
**Primary Focus:** Creating accessible, beginner-friendly language documentation for non-programmers
**Assigned Tasks:**
- Task #10: Create comprehensive language documentation for end users
**Key Responsibilities:**
- Create documentation for **non-technical world builders** (like Lonni)
- Document language from a **storyteller's perspective**, not a programmer's perspective
- Cover all language features with:
- Clear, simple explanations
- Practical examples from storytelling contexts
- Progressive complexity (beginner → intermediate → advanced)
- Common patterns and recipes
- Troubleshooting guides
- Structure documentation by use case:
- "Creating your first character"
- "Making characters behave realistically"
- "Setting up daily schedules"
- "Creating relationships between characters"
- "Designing locations and institutions"
- "Making characters respond to events"
- "Building complex behaviors"
- Document new features as they're implemented:
- Human-friendly behavior tree keywords (from Task #3/#4)
- Resource linking syntax (from Task #5/#6)
- Schedule composition (from Task #8/#9)
- Include:
- Tutorials
- How-to guides
- Reference documentation
- Concept explanations
- Glossary of terms
- Use consistent terminology throughout
- Coordinate with all other agents to document their features
- Get user feedback on documentation clarity
**Deliverables:**
- Getting Started Guide
- Language Tutorial (progressive lessons)
- Feature-by-feature reference
- Recipe/pattern library
- Troubleshooting guide
- Glossary
- Example storybooks (annotated)
- Quick reference card
**Success Criteria:**
- A non-programmer can learn the language from the documentation alone
- Documentation covers all language features
- Examples are compelling and storytelling-focused
- User (and test readers like Lonni) approve documentation clarity
- Documentation stays up-to-date with new features
---
### Agent 5: SBIR Specification Analyst
**Subagent Type:** `Explore` (read-only, deep analysis focus)
**Primary Focus:** Validating SBIR spec completeness and identifying gaps
**Assigned Tasks:**
- Task #7: Conduct comprehensive SBIR specification validation
**Key Responsibilities:**
- Review entire SBIR spec section-by-section
- Cross-reference spec against actual .sb language features
- Identify missing functionality:
- Type system gaps
- Missing instruction opcodes
- Incomplete constraint system
- Schedule representation limitations
- **Behavior linking representation** (coordinate with Agent 3)
- Debug information gaps
- Extensibility concerns
- Identify ambiguities or underspecified semantics
- Validate against use cases (especially schedules and behavior linking)
- Check binary format efficiency
- Review codegen contract completeness
- Propose additions and modifications
**Deliverables:**
- Specification gap analysis report
- List of ambiguities requiring clarification
- Proposed additions organized by section
- Use case validation results
- Priority ranking of issues
**Success Criteria:**
- Every .sb language feature is representable in SBIR
- All ambiguities are documented
- Schedule system gaps are clearly identified
- Behavior linking gaps are clearly identified
- Codegen contract covers all implementation needs
- Spec is sufficient for third-party implementations
---
### Agent 6: Schedule System Architect
**Subagent Type:** `general-purpose` (needs full read/write/edit access)
**Primary Focus:** Designing and implementing year-long composable schedules
**Assigned Tasks:**
- Task #8: Design year-long composable schedule system
- Task #9: Implement schedule system
**Key Responsibilities:**
- Design schedule composition hierarchy:
- Base: Daily schedule templates
- Week: 7-day compositions with variations
- Month: Weekly compositions with irregular handling
- Season: Quarterly templates
- Year: Full calendar year representation
- Design composition operators:
- Inheritance (extends base schedule)
- Override (replace blocks)
- Merge (combine schedules)
- Exception (one-off modifications)
- Design override system for:
- Holidays and festivals
- Special events
- Seasonal variations
- Life events
- Address calendar concerns:
- Leap years
- Different calendar systems
- Date arithmetic
- **Design integration with resource linking:**
- Schedule blocks can optionally specify behaviors
- Behavior selection based on time of day via schedule
- Schedule selection via `link schedule` in entities/institutions
- Coordinate with Agent 3 (Resource Linking Architect)
- Design SBIR representation:
- Schedule composition metadata
- Efficient encoding for repetition
- Override/exception format
- Behavior references in schedule blocks
- Implement in language:
- Syntax for schedule composition
- Parser support
- Compiler translation to SBIR
- Create runtime evaluation strategy
- Validate with realistic examples
**Deliverables:**
- Schedule system architecture document
- Composition operator specification
- SBIR format proposal (updates to section 9)
- Calendar system specification
- Updated language syntax for schedules
- Parser and compiler changes
- Schedule resolver/evaluator implementation
- Comprehensive examples:
- Simple daily schedule
- Weekly variations
- Seasonal schedules
- Holiday calendars
- Year-long character schedules with behavior links
- Test suite for schedule composition
- Documentation
**Success Criteria:**
- Can represent schedules up to one calendar year
- Schedules are composable with clear semantics
- Schedules can reference behaviors
- Minimal redundancy in representation
- Examples demonstrate real-world use cases
- Implementation matches specification
- SBIR encoding is space-efficient
- Integration with behavior linking works correctly
---
## Task Breakdown & Dependencies
### Phase 1: Analysis & Design (Parallel)
Can be worked on simultaneously by different agents:
**Task #1:** Audit LSP implementation and create test strategy
- **Agent:** LSP Test Engineer
- **Dependencies:** None
- **Duration Estimate:** Research/analysis phase
**Task #3:** Analyze current behavior tree syntax and design keyword system
- **Agent:** Behavior Tree Language Designer
- **Dependencies:** None
- **Duration Estimate:** Design phase
**Task #5:** Design resource linking system (behaviors + schedules)
- **Agent:** Resource Linking Architect
- **Dependencies:** None (but should coordinate with Tasks #3, #7, and #8)
- **Duration Estimate:** Design phase
**Task #7:** Conduct comprehensive SBIR specification validation
- **Agent:** SBIR Specification Analyst (Agent 5)
- **Dependencies:** None
- **Duration Estimate:** Analysis phase
**Task #8:** Design year-long composable schedule system
- **Agent:** Schedule System Architect (Agent 6)
- **Dependencies:** None (but should coordinate with Task #5 for behavior linking)
- **Duration Estimate:** Design phase
---
### Phase 2: Implementation (Partially Parallel)
**Task #2:** Implement LSP test suite and stabilization fixes
- **Agent:** LSP Test Engineer
- **Dependencies:** Task #1 (test strategy must be complete)
- **Duration Estimate:** Implementation phase
**Task #4:** Implement behavior tree keyword transformation system
- **Agent:** Behavior Tree Language Designer
- **Dependencies:** Task #3 (design must be complete)
- **Duration Estimate:** Implementation phase
**Task #6:** Implement resource linking system
- **Agent:** Resource Linking Architect
- **Dependencies:** Task #5 (design must be complete), Task #4 (behavior tree syntax should be stable)
- **Duration Estimate:** Implementation phase
**Task #9:** Implement year-long composable schedule system
- **Agent:** Schedule System Architect (Agent 6)
- **Dependencies:** Task #8 (design must be complete), Task #6 (behavior linking should be implemented for integration)
- **Duration Estimate:** Implementation phase
**Task #10:** Create comprehensive language documentation for end users
- **Agent:** Language Documentation Writer (Agent 4)
- **Dependencies:** None initially, but continuously updated as Tasks #4, #6, #9 complete
- **Duration Estimate:** Ongoing throughout all phases
---
### Phase 3: Consolidation (Sequential)
**Task #11:** Update SBIR specification with all enhancements
- **Agent:** Team Lead (or designated spec writer)
- **Dependencies:** Tasks #3, #5, #7, #8 (all designs and analysis must be complete)
- **Duration Estimate:** Documentation phase
**Responsibilities:**
- Incorporate findings from SBIR validation (Task #7)
- Add year-long schedule system spec (Task #8 output)
- Add behavior tree keyword documentation (Task #3 output)
- Add entity-behavior linking spec (Task #5 output)
- Update ENTITIES section for behavior links
- Update SCHEDULES section for behavior references
- Update instruction set if needed
- Add new sections if required
- Update examples throughout
- Increment version number (likely 0.2.0)
- Create migration guide
**Deliverables:**
- Updated SBIR specification (v0.2.0)
- Migration guide from v0.1.0
- Version changelog
- Updated examples
---
### Phase 4: Integration & Validation (Final)
**Task #12:** Integration validation and documentation
- **Agent:** Team Lead + all agents for their domains
- **Dependencies:** Tasks #2, #4, #6, #9, #10 (all implementations and spec must be complete)
- **Duration Estimate:** Testing and documentation phase
**Responsibilities:**
- Run full integration tests across all systems
- Validate LSP with new behavior tree keywords AND linking syntax
- Validate compiler with new schedule system
- Validate behavior linking and selection at runtime
- Test schedule + behavior linking integration
- Test SBIR serialization/deserialization
- Run Alice in Wonderland example through full pipeline
- Create end-to-end examples demonstrating all features
- Update user documentation
- Create developer guide
- Document breaking changes
- Create release notes
**Deliverables:**
- Integration test results
- End-to-end examples showing:
- Character with multiple behaviors
- Schedule-based behavior selection
- Conditional behavior linking
- Full lifecycle from .sb → .sbc → runtime
- User documentation (updated)
- Developer guide
- Release notes for v0.2.0
**Success Criteria:**
- All systems work together seamlessly
- Can create entity with behaviors linked via all supported mechanisms
- Schedule integration with behaviors works correctly
- LSP supports all new syntax
- Examples validate end-to-end
---
## Dependency Graph
```
┌───────────────────────────────────────────────────────────────────────┐
│ Phase 1: Analysis & Design (Parallel, with coordination) │
├───────────────────────────────────────────────────────────────────────┤
│ │
│ Task #1: LSP Audit Task #3: BT Design │
│ (LSP Test Engineer) (BT Designer) │
│ │ │ │
│ │ │ ← coordinates with → │
│ │ │ ↓ │
│ │ Task #5: Linking Design Task #8: Sched│
│ │ (Linking Architect) (Sched Arch) │
│ │ │ │ │
│ │ │ ← coordinates with → │ │
│ │ │ │
│ Task #7: SBIR Validation ←──────────┼───────────────────────────────│
│ (SBIR Analyst) │ │
│ │
└───────────────────────────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────────────────────────┐
│ Phase 2: Implementation (Partially Sequential) │
├───────────────────────────────────────────────────────────────────────┤
│ │
│ Task #2: LSP Tests Task #4: BT Implementation │
│ (depends on #1) (depends on #3) │
│ │ │
│ ↓ │
│ Task #6: Linking Implementation │
│ (depends on #4, #5) │
│ │ │
│ ↓ │
│ Task #9: Schedule Implementation │
│ (depends on #6, #8) │
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Task #10: Language Documentation (ongoing, updates as │ │
│ │ features complete: #4, #6, #9) │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└───────────────────────────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────────────────────────┐
│ Phase 3: Consolidation │
├───────────────────────────────────────────────────────────────────────┤
│ │
│ Task #11: Update SBIR Spec │
│ (depends on Tasks #3, #5, #7, #8) │
│ │
└───────────────────────────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────────────────────────┐
│ Phase 4: Integration │
├───────────────────────────────────────────────────────────────────────┤
│ │
│ Task #12: Integration Validation & Technical Docs │
│ (depends on Tasks #2, #4, #6, #9, #10, #11) │
│ │
│ Note: Task #10 (Language Docs) continues updating throughout │
│ │
└───────────────────────────────────────────────────────────────────────┘
```
---
## Resource Linking: Design Considerations
### Linking Mechanisms to Support
#### 1. Character Behavior Linking
**Simple Direct Link:**
```sb
character Alice : Human {
link behavior: CuriousExplorer
}
```
**Multiple Behaviors with Priorities:**
```sb
character Alice : Human {
link behaviors: [
{ tree: HandleUrgentNeeds, priority: critical }
{ tree: WorkAtBakery, priority: normal, when: schedule.activity == work }
{ tree: SocialInteraction, priority: normal, when: schedule.activity == leisure }
{ tree: Sleep, priority: normal, when: schedule.activity == sleep }
{ tree: Idle, priority: low }
]
}
```
**Context-Based Behavior:**
```sb
character Alice : Human {
link behaviors: [
{ tree: WorkAtBakery, when: at_location(Bakery) }
{ tree: HomeRoutine, when: at_location(Home) }
{ tree: Wander, default: true }
]
}
```
#### 2. Character Schedule Linking
**Direct Schedule Link:**
```sb
character Alice : Human {
link schedule: BakerSchedule
}
```
**Conditional Schedule Link:**
```sb
character Alice : Human {
link schedule: WorkdaySchedule when employed == true
link schedule: UnemployedSchedule when employed == false
}
```
**State-Based Schedule:**
```sb
character Alice : Human {
life_arc: PersonArc at Adult
link schedules: [
{ schedule: ChildSchedule, when: state == Child }
{ schedule: AdultSchedule, when: state == Adult }
{ schedule: ElderSchedule, when: state == Elder }
]
}
```
#### 3. Institution Behavior Linking
**Institution-Level Behavior:**
```sb
institution Bakery {
link behavior: BakeryOperations
roles {
owner {
link behavior: ManageBakery
}
worker {
link behavior: BakeryWork
}
}
}
```
**Contextual Institution Behavior:**
```sb
institution Bakery {
link behaviors: [
{ tree: BusyService, when: customer_queue > 5 }
{ tree: NormalService, default: true }
]
}
```
#### 4. Institution Schedule Linking
**Operating Hours:**
```sb
institution Bakery {
link schedule: BakeryHours
}
```
**Seasonal Schedules:**
```sb
institution Bakery {
link schedules: [
{ schedule: SummerHours, when: season == summer }
{ schedule: WinterHours, when: season == winter }
{ schedule: StandardHours, default: true }
]
}
```
**Role-Specific Schedules:**
```sb
institution Bakery {
roles {
owner {
link schedule: OwnerSchedule
}
worker {
link schedule: WorkerSchedule
}
}
}
```
#### 5. Species Default Links
**Default Behavior and Schedule:**
```sb
species Human {
components: HumanComponents
link behaviors: [HandleBasicNeeds, SocialDrive, CuriosityDrive]
link schedule: DefaultHumanSchedule
}
```
#### 6. Template Inherited Links
**Template with Links:**
```sb
template WonderlandCreature {
link behavior: WonderlandBehavior
link schedule: WonderlandSchedule
}
character CheshireCat : Cat using WonderlandCreature {
// inherits links from template
// can override with own links
link behavior: CheshireBehavior // overrides template
}
```
### SBIR Representation Questions
Agent 3 (Resource Linking Architect) should address:
**For Behavior Links:**
1. How are behavior references encoded in the CHARACTERS section (ENTITIES in spec)?
2. How are behavior references encoded in the INSTITUTIONS section?
3. How are selection rules/predicates encoded?
4. How are priorities represented?
5. How does the runtime resolve which behavior to execute?
6. Can multiple behaviors run simultaneously (parallel execution)?
7. How do conditions inject behaviors (override system)?
**For Schedule Links:**
8. How are schedule references encoded in the CHARACTERS section?
9. How are schedule references encoded in the INSTITUTIONS section?
10. How are conditional schedule links resolved at runtime?
11. How do schedule links interact with behavior links?
12. Can a character have both a direct schedule link AND schedule blocks with inline behaviors?
**General:**
13. What is the precedence order for link resolution (species → template → character)?
14. How are link overrides represented in SBIR?
15. What happens when multiple links match at runtime (conflict resolution)?
16. What existing infrastructure can be reused (parser, compiler, etc.)?
---
## Coordination Strategy
### Communication Patterns
1. **Phase Kickoffs:** Team lead provides context and coordinates agent startup for each phase
2. **Design Reviews with User:** After Tasks #1, #3, #5, #7, #8 complete, agents present designs to USER for approval before implementation
- **Critical:** Task #5 (linking design) MUST be reviewed by user before Task #6 (implementation)
3. **Cross-Agent Coordination:**
- Agent 3 (Linking) coordinates with Agent 2 (BT) on syntax
- Agent 3 (Linking) coordinates with Agent 5 (Schedule) on integration
- Agent 4 (SBIR) reviews all designs for spec implications
4. **Progress Updates:** Agents report completion and blockers to team lead AND user at checkpoints
5. **Implementation Checkpoints:** Agent 3 (Linking) discusses implementation approach with user before finalizing
6. **Integration Checkpoints:** Before Task #10, synchronize all design outputs
7. **Final Review:** Before Task #11, validate all implementations with user
### Handoff Points
- **LSP → BT Designer:** If LSP tests reveal issues with behavior tree parsing
- **BT Designer → Linking Architect:** Behavior tree syntax must be stable before implementing linking
- **Linking Architect → Schedule Architect:** Linking design informs schedule-behavior integration
- **SBIR Analyst → All Designers:** Gap analysis informs all designs
- **SBIR Analyst → Team Lead:** Findings inform Task #10 (spec updates)
- **All Designers → Team Lead:** Design outputs feed into Task #10
- **All Implementers → Team Lead:** Implementations validated in Task #11
---
## Risk Mitigation
### Potential Issues
1. **Backward Compatibility Breaks:** Behavior tree refactoring may break existing .sb files
- **Mitigation:** Design migration path; consider supporting both syntaxes temporarily
2. **SBIR Binary Format Changes:** Schedule and linking enhancements will require breaking changes
- **Mitigation:** Version bump to 0.2.0; provide conversion tooling
3. **LSP Instability During Development:** Changes to parser/compiler may break LSP
- **Mitigation:** LSP tests run continuously; prioritize stability fixes
4. **Linking System Complexity:** Multiple linking mechanisms may create ambiguity
- **Mitigation:** Clear precedence rules; comprehensive examples; runtime warnings for conflicts
5. **Scope Creep:** Each workstream could expand significantly
- **Mitigation:** Strict adherence to task definitions; defer non-essential work
6. **Integration Failures:** Systems may not work together cleanly
- **Mitigation:** Early integration checkpoints; continuous testing; coordination meetings
7. **Performance Concerns:** Complex behavior selection could impact runtime
- **Mitigation:** Design efficient selection algorithm; benchmark early
---
## Success Metrics
### LSP Workstream
- [ ] 90%+ test coverage for LSP features
- [ ] Zero known critical bugs
- [ ] All LSP features work with new syntax (BT keywords + linking)
- [ ] CI tests pass
### Behavior Tree Workstream
- [ ] All symbolic constructs have keyword replacements
- [ ] Alice in Wonderland example validates with new syntax
- [ ] Documentation includes before/after examples
- [ ] Compiler generates correct SBIR
### Resource Linking Workstream
- [ ] User approves linking design (Checkpoint 1)
- [ ] Unified `link` keyword supports behaviors and schedules
- [ ] Character behavior linking works (single + multiple + conditional)
- [ ] Character schedule linking works (single + multiple + conditional)
- [ ] Institution behavior linking works
- [ ] Institution schedule linking works (operating hours)
- [ ] All linking mechanisms implemented and tested
- [ ] User approves implementation approach (Checkpoint 2)
- [ ] Clear syntax for all use cases
- [ ] Runtime link resolver works correctly for behaviors
- [ ] Runtime link resolver works correctly for schedules
- [ ] Examples demonstrate all linking patterns
- [ ] SBIR encoding captures all linking semantics
- [ ] Integration between behavior links and schedule links works
- [ ] User validates working implementation (Checkpoint 3)
### SBIR Validation Workstream
- [ ] All identified gaps documented
- [ ] Priority recommendations provided
- [ ] Schedule system requirements captured
- [ ] Linking system requirements captured
- [ ] Spec ambiguities clarified
### Schedule System Workstream
- [ ] Can represent 365-day schedules
- [ ] Schedules compose with clear semantics
- [ ] Schedules can reference behaviors
- [ ] Examples demonstrate real-world use
- [ ] SBIR encoding is efficient
- [ ] Runtime resolver works correctly
- [ ] Integration with behavior linking works
### Integration
- [ ] All systems work together seamlessly
- [ ] End-to-end pipeline validated (source → SBIR → runtime)
- [ ] Can create complex entities with schedules + behaviors
- [ ] Documentation complete
- [ ] Release ready
---
## Next Steps
1. **Review this plan** - Approve, modify, or expand as needed
2. **Prioritize workstreams** - Determine if any workstream is more critical
3. **Spawn team** - Create agents with assigned roles
4. **Phase 1 kickoff** - Start analysis and design tasks with coordination meetings
5. **Design review** - Gate between Phase 1 and Phase 2
6. **Implementation** - Execute Phase 2 tasks sequentially where needed
7. **Specification update** - Execute Task #10
8. **Integration** - Execute Task #11
9. **Release** - Publish v0.2.0 with all enhancements
---
## Questions for Review
1. **Linking mechanisms:** Are all the proposed linking mechanisms necessary? Should we start with a subset?
2. **Link keyword scope:** Should `link` be the unified keyword for both behaviors and schedules, or use `link behavior:` and `link schedule:` explicitly?
3. **Institution linking:** Should institutions support the same linking flexibility as entities, or a simpler subset?
4. **Agent allocation:** Should any workstream have multiple agents (e.g., separate linking designer and implementer)?
5. **Timeline:** Are there hard deadlines or priorities among the five workstreams?
6. **Backward compatibility:** How important is maintaining compatibility with v0.1.0?
7. **Scope:** Should we defer any feature (e.g., parallel behavior execution, complex conditional linking) to v0.3.0?
8. **Integration complexity:** Should linking and schedules be designed together from the start, or sequentially?
9. **Success criteria:** Are the metrics sufficient, or should we add more specific targets?
10. **Link resolution algorithm:** Should behavior/schedule selection be designed upfront, or emerge from the linking design?
11. **Schedule-behavior interaction:** When a schedule block specifies a behavior AND the entity has linked behaviors, which takes precedence?

View File

@@ -0,0 +1,210 @@
# Behavior Tree Keyword Implementation Status
**Task:** #7 - Implement behavior tree keyword transformation system
**Status:** In Progress (60% complete)
**Date:** February 12, 2026
---
## ✅ Completed Work
### 1. Tree-sitter Grammar (`tree-sitter-storybook/grammar.js`)
- ✅ Updated `selector_node` to use `choose` keyword with optional label
- ✅ Updated `sequence_node` to use `then` keyword with optional label
- ✅ Added `condition_node` for `if(expr)` and `when(expr)`
- ✅ Added `decorator_node` with full parameter support
- ✅ Added `decorator_keyword` enumeration
- ✅ Added `decorator_params` for all parameter types
- ✅ Updated `subtree_node` to use `include` keyword
- ✅ Extended `duration` pattern to support days (`d`)
### 2. AST Structures (`src/syntax/ast.rs` + `storybook/src/syntax/ast.rs`)
- ✅ Restructured `BehaviorNode::Selector` with label support
- ✅ Restructured `BehaviorNode::Sequence` with label support
- ✅ Added `BehaviorNode::Decorator` with typed decorators
- ✅ Created `DecoratorType` enum with 10 variants
- ✅ Created `Duration` struct with `DurationUnit` enum
- ✅ Implemented `Duration::to_milliseconds()` conversion
### 3. LALRPOP Parser (`src/syntax/parser.lalrpop`)
- ✅ Updated `BehaviorNode` choice to include new node types
- ✅ Implemented `SelectorNode` with `choose` keyword and optional label
- ✅ Implemented `SequenceNode` with `then` keyword and optional label
- ✅ Implemented `ConditionNode` for `if`/`when` expressions
- ✅ Implemented all 10 decorator parsers:
- `DecoratorRepeat` (infinite)
- `DecoratorRepeatN(n)`
- `DecoratorRepeatRange(min..max)`
- `DecoratorInvert`
- `DecoratorRetry(n)`
- `DecoratorTimeout(duration)`
- `DecoratorCooldown(duration)`
- `DecoratorGuard(expr)`
- `DecoratorSucceedAlways`
- `DecoratorFailAlways`
- ✅ Updated `SubTreeNode` to use `include` keyword
### 4. Lexer Tokens (`src/syntax/lexer.rs` + `storybook/src/syntax/lexer.rs`)
- ✅ Added `Choose` token
- ✅ Added `Then` token
- ✅ Added `If` token
- ✅ Added `When` token
- ✅ Added `Repeat` token
- ✅ Added `Invert` token
- ✅ Added `Retry` token
- ✅ Added `Timeout` token
- ✅ Added `Cooldown` token
- ✅ Added `Guard` token
- ✅ Added `SucceedAlways` token
- ✅ Added `FailAlways` token
### 5. Example Files
- ✅ Created `examples/new-syntax-demo.sb` with comprehensive demonstrations
---
## 🚧 In Progress / Pending
### 6. Fix Compilation Errors
The AST changes will break existing code that pattern-matches on `BehaviorNode`:
- [ ] Update all `match BehaviorNode::Selector(nodes)` to `BehaviorNode::Selector { label, children }`
- [ ] Update all `match BehaviorNode::Sequence(nodes)` to `BehaviorNode::Sequence { label, children }`
- [ ] Update all `match BehaviorNode::Decorator(name, child)` to `BehaviorNode::Decorator { decorator_type, child }`
- [ ] Check files:
- `src/resolve/validate.rs`
- `src/resolve/types.rs`
- `src/types.rs`
- LSP code in `storybook/src/lsp/`
- Test files
### 7. Compiler / SBIR Generation
- [ ] Update SBIR node type encoding for decorators
- [ ] Implement serialization for `DecoratorType` variants
- [ ] Implement `Duration` serialization
- [ ] Update compiler to handle new AST structures
- [ ] Test SBIR output for correctness
### 8. Testing
- [ ] Rebuild Tree-sitter grammar (`npm run build` or similar)
- [ ] Test parsing `examples/new-syntax-demo.sb`
- [ ] Add unit tests for each decorator type
- [ ] Add integration tests for nested decorators
- [ ] Test round-trip: parse → compile → decompile
### 9. Example Migration
- [ ] Migrate `examples/alice-in-wonderland/world/characters/white_rabbit.sb`
- [ ] Migrate `examples/alice-in-wonderland/world/characters/mad_tea_party.sb` (add `repeat` decorator)
- [ ] Migrate `examples/alice-in-wonderland/world/characters/cheshire_cat.sb` (add decorators)
- [ ] Migrate `examples/alice-in-wonderland/world/characters/royal_court.sb` (add `repeat` decorators)
- [ ] Migrate `examples/alice-in-wonderland/world/characters/caterpillar.sb`
- [ ] Migrate `tests/examples/behavior_and_lifearc.sb`
- [ ] Update test corpus in `tree-sitter-storybook/test/corpus/behaviors.txt`
### 10. Documentation
- [ ] Update `design.md` Section 6 with new syntax
- [ ] Create migration guide
- [ ] Add tutorial examples highlighting named nodes
- [ ] Update CHANGELOG with breaking changes
- [ ] Prepare handoff notes for language documentation agent
---
## 📊 Progress Metrics
**Overall Completion:** 60%
**By Phase:**
- Phase 1 (Grammar & Parser): 100% ✅
- Phase 2 (AST & Compiler): 70% (AST done, compiler pending)
- Phase 3 (Examples & Tests): 10% (example created, migration pending)
- Phase 4 (Documentation): 0%
**By Component:**
- Grammar: 100% ✅
- AST: 100% ✅
- Lexer: 100% ✅
- Parser: 100% ✅
- Compilation fixes: 0%
- Compiler/SBIR: 0%
- Testing: 10%
- Migration: 0%
- Documentation: 0%
---
## 🎯 Next Immediate Actions
1. **Find and fix compilation errors** from AST changes
2. **Update compiler** to handle new `DecoratorType` enum
3. **Test parsing** the new syntax example
4. **Migrate one example** (White Rabbit) to validate end-to-end
5. **Add unit tests** for decorator parsing
---
## 📝 Design Decisions Implemented
Following Sienna's feedback from design review:
**Use `choose` only** (not `selector`)
**Use `then` only** (not `sequence`)
**Remove @ prefix for actions**
**Use `include` for subtrees**
**Named nodes are prominent** (optional label on choose/then)
**Decorators approved** (all 10 types implemented)
**No backward compatibility** (clean break from symbolic syntax)
---
## 🔄 Files Modified
**Grammar:**
- `tree-sitter-storybook/grammar.js`
**AST:**
- `src/syntax/ast.rs`
- `storybook/src/syntax/ast.rs`
**Parser:**
- `src/syntax/parser.lalrpop`
**Lexer:**
- `src/syntax/lexer.rs`
- `storybook/src/syntax/lexer.rs`
**Examples:**
- `examples/new-syntax-demo.sb` (new)
---
## 🐛 Known Issues / Blockers
**Compilation Errors Expected:**
The AST structure changes will cause compilation errors in any code that pattern-matches on `BehaviorNode`. These need to be fixed before the code will compile.
**Files Likely Affected:**
- `src/resolve/validate.rs` - behavior tree validation
- `src/resolve/types.rs` - type resolution
- `src/types.rs` - type definitions
- `storybook/src/lsp/semantic_tokens.rs` - LSP highlighting
- `storybook/src/lsp/navigation_tests.rs` - LSP tests
- Test files with behavior tree examples
**Resolution Strategy:**
1. Attempt to compile
2. Fix each compilation error by updating pattern matches
3. Test that fixes maintain correct behavior
4. Coordinate with LSP agent if LSP changes are extensive
---
## 📚 Resources
**Design Document:** `/Users/sienna/Development/storybook/design/behavior-tree-keywords-design.md`
**Example File:** `/Users/sienna/Development/storybook/examples/new-syntax-demo.sb`
**TEAM PLAN:** `/Users/sienna/Development/storybook/TEAM_PLAN.md` (Task #7)
---
**Last Updated:** 2026-02-12
**Next Update:** After fixing compilation errors

File diff suppressed because it is too large Load Diff

160
design/color-palette.md Normal file
View File

@@ -0,0 +1,160 @@
# Storybook Editor Color Palette
> Aubergine sophistication meets gold energy
## Primary Colors (Aubergine Foundation)
```
████ aubergine-900 #1a0f1e RGB(26, 15, 30) Background (darkest)
████ aubergine-800 #2b1a33 RGB(43, 26, 51) Surface (panels, cards)
████ aubergine-750 #382444 RGB(56, 36, 68) Alternate row color
████ aubergine-700 #3d2447 RGB(61, 36, 71) Surface hover
████ aubergine-600 #4f2e5b RGB(79, 46, 91) Borders, dividers
████ aubergine-500 #61386f RGB(97, 56, 111) Subtle highlights
████ aubergine-400 #805793 RGB(128, 87, 147) Active elements
████ aubergine-300 #9f76a7 RGB(159,118,167) Muted text, info
```
## Accent Colors (Gold/Orange)
```
████ gold-600 #e89350 RGB(232,147, 80) Deeper accent (optional)
████ gold-500 #f4a261 RGB(244,162, 97) PRIMARY ACCENT ⭐
████ gold-400 #f6b47a RGB(246,180,122) Hover state
████ gold-300 #f8c594 RGB(248,197,148) Pressed/active state
████ gold-200 #fad6ad RGB(250,214,173) Very subtle highlight
```
## Neutral Colors
```
████ cream #fdf8f3 RGB(253,248,243) Primary text, high contrast
████ warm-gray-100 #e8e3dd RGB(232,227,221) Secondary text
████ warm-gray-200 #d1cbc3 RGB(209,203,195) Tertiary text
████ warm-gray-400 #8b8680 RGB(139,134,128) Muted text, placeholders
████ warm-gray-700 #4a4845 RGB(74, 72, 69) Disabled elements
████ charcoal #1f1f1f RGB(31, 31, 31) Pure dark (code bg)
```
## Semantic Colors
```
████ success-light #8ab864 RGB(138,184,100) Success hover
████ success #6a994e RGB(106,153, 78) Validation passed, success ✓
████ success-dark #558039 RGB(85, 128, 57) Success pressed
████ warning-light #f6b47a RGB(246,180,122) Warning hover (reuse gold-400)
████ warning #f4a261 RGB(244,162, 97) Warning, attention ⚠
████ warning-dark #e89350 RGB(232,147, 80) Warning pressed
████ error-light #e54545 RGB(229, 69, 69) Error hover
████ error #d62828 RGB(214, 40, 40) Parse errors, failures ✗
████ error-dark #b81e1e RGB(184, 30, 30) Error pressed
████ info #9f76a7 RGB(159,118,167) Info, hints (reuse aubergine-300)
```
## Color Combinations (Contrast Ratios)
### Text on Backgrounds
| Foreground | Background | Ratio | Grade | Use |
|------------|------------|-------|-------|-----|
| cream | aubergine-900 | 15.2:1 | AAA | Primary text |
| warm-gray-100 | aubergine-900 | 11.8:1 | AAA | Secondary text |
| warm-gray-400 | aubergine-900 | 4.9:1 | AA | Muted text |
| gold-500 | aubergine-900 | 7.1:1 | AA Large | Accent text |
| aubergine-900 | gold-500 | 7.1:1 | AA Large | Gold buttons |
### Interactive Elements
| Element | Default | Hover | Active | Focus |
|---------|---------|-------|--------|-------|
| Primary Button | gold-500 bg | gold-400 bg | gold-300 bg | gold-500 + glow |
| Secondary Button | transparent | aubergine-700 | aubergine-600 | gold-500 border |
| Input Field | aubergine-800 | - | - | gold-500 border |
| List Item | transparent | aubergine-700 | aubergine-600 | gold-500 left border |
## Usage Guidelines
### Do's ✓
- Use **aubergine-900** for main background
- Use **aubergine-800** for panels and cards
- Use **cream** for all primary text
- Use **gold-500** sparingly for accents (buttons, highlights, selected states)
- Use semantic colors (success, error, warning) only for their purpose
- Maintain visual hierarchy with size + weight + color
### Don'ts ✗
- Don't use gold for large backgrounds (overwhelming)
- Don't use more than 2-3 colors in a single component
- Don't rely on color alone for information (use icons + text)
- Don't use pure black (#000) or pure white (#fff)
- Don't mix warm and cool grays
## Code Syntax Highlighting
Muted versions of semantic colors on dark background:
```rust
// Example syntax theme
Keywords: #f4a261 (gold-500) - fn, let, pub, impl
Types: #9f76a7 (aubergine-300) - String, Vec, Option
Functions: #fad6ad (gold-200) - function_name()
Strings: #8ab864 (success-light) - "hello"
Numbers: #f6b47a (gold-400) - 42, 3.14
Comments: #8b8680 (warm-gray-400) - // comment
Operators: #e8e3dd (warm-gray-100) - +, =, ->
Punctuation: #d1cbc3 (warm-gray-200) - {}, (), ;
```
## Gradients (Optional, Use Sparingly)
```
Gold Shimmer (for loading states):
linear-gradient(90deg,
gold-500 0%,
gold-400 50%,
gold-500 100%)
Aubergine Depth (for hero sections):
linear-gradient(180deg,
aubergine-800 0%,
aubergine-900 100%)
```
## Opacity Scale
For overlays and transparency:
```
10%: rgba(color, 0.1) - Very subtle tint
20%: rgba(color, 0.2) - Subtle overlay
40%: rgba(color, 0.4) - Muted element
60%: rgba(color, 0.6) - Semi-transparent
80%: rgba(color, 0.8) - Mostly opaque
```
## Color Psychology
| Color | Emotion | Usage |
|-------|---------|-------|
| Aubergine | Sophisticated, creative, mysterious | Base, establishes tone |
| Gold | Energetic, optimistic, valuable | Accents, calls-to-action |
| Cream | Calm, clean, readable | Text, content |
| Green | Success, growth, go | Validation, success states |
| Red | Error, stop, urgent | Errors, destructive actions |
## Accessibility Notes
- All text meets **WCAG AAA** for contrast
- Gold accent meets **WCAG AA** for large text
- Focus indicators are clearly visible (gold-500 + glow)
- Never use color as the only indicator (pair with icons/text)
- Test with colorblind simulators (deuteranopia, protanopia)
---
*Pair this palette with Geist (UI) and Monaspace Neon (code) for optimal effect.*

365
design/layout-mockup.txt Normal file
View File

@@ -0,0 +1,365 @@
STORYBOOK EDITOR - LAYOUT MOCKUPS
═══════════════════════════════════════════════════════════════════════════════
Color Legend:
████ aubergine-900 (background)
████ aubergine-800 (panels)
████ gold-500 (accents)
OPTION 1: THREE-COLUMN LAYOUT (Wide Screen Focus)
═══════════════════════════════════════════════════════════════════════════════
┌─────────────────────────────────────────────────────────────────────────────┐
│ ⚡ Storybook ◉ ○ ○ [Validate] [Save] │ aubergine-800
├─────────┬───────────────────────────────────────────────────┬───────────────┤
│ FILES │ EDITOR │ INSPECTOR │
│ │ │ │
│ 📁 alice│ character WhiteRabbit: Rabbit from Wonderland... │ WhiteRabbit │
│ └─ 📁 w│ │ ─────────────│
│ ├─ ch│ use schema::core_enums::{Size, EmotionalState}; │ │
│ ├─ ch│ use schema::templates::WonderlandCreature; │ Type: │
│ └─ wh│ │ Character │
│ 📁 schem│ character WhiteRabbit: Rabbit from WonderlandCr...│ │
│ └─ temp│ // Physical traits │ Species: │
│ │ current_size: small │ Rabbit │
│ [Search]│ wears_waistcoat: true │ │
│ │ has_pocket_watch: true │ Templates: │
│ Recent │ │ • WonderlandC.│
│ ───── │ // Personality │ • CourtMember │
│ • white│ emotional_state: frightened │ │
│ • alice│ awareness_of_absurdity: 0.3 │ Fields: 12 │
│ │ │ Prose: 1 │
│ │ ---backstory │ │
│ │ Always late, always anxious. Herald of │ Validation │
│ │ the Queen, perpetually checking his │ ─────────────│
│ │ pocket watch. │ ✓ All valid │
│ │ --- │ │
│ 240px │ │ 320px │
├─────────┴───────────────────────────────────────────────────┴───────────────┤
│ 🟢 Ready alice-in-wonderland (main) 12 characters, 7 relationships │
└─────────────────────────────────────────────────────────────────────────────┘
OPTION 2: TWO-COLUMN WITH BOTTOM DIAGNOSTICS
═══════════════════════════════════════════════════════════════════════════════
┌─────────────────────────────────────────────────────────────────────────────┐
│ ⚡ Storybook Editor 🔍 Search ✓ Validate 💾 Save ⚙️ │
├─────────┬───────────────────────────────────────────────────────────────────┤
│ FILES │ EDITOR │
│ │ │
│ 📂 world│ character MadHatter: Human from MadTeaPartyMember { │
│ ├─ 📂 c│ │
│ │ ├─ a│ // Physical traits │
│ │ ├─ w│ current_size: normal │
│ │ └─ m│ wears_top_hat: true ┌──────────────┐│
│ ├─ 📂 i│ hat_size_label: "10/6" │ QUICK ACTIONS││
│ └─ 📂 r│ │ ────────────│││
│ │ // Personality │ Add Field ││
│ 📂 schema│ emotional_state: confused │ Add Prose ││
│ ├─ core│ follows_logic: false │ Duplicate ││
│ ├─ temp│ awareness_of_absurdity: 0.1 │ Delete Char ││
│ └─ spec│ │ View Graph ││
│ │ // Tea party state └──────────────┘│
│ [+ New] │ stuck_at_teatime: true │
│ │ current_seat_position: 1 │
│ 280px │ │
├─────────┼───────────────────────────────────────────────────────────────────┤
│ DIAGNOSTICS & VALIDATION │
│ ──────────────────────────────────────────────────────────────────────────│
│ ✓ No errors (validated 0.3s ago) [Run Tests] │
│ │
│ INFO: MadHatter inherits 8 fields from MadTeaPartyMember │
│ INFO: Cross-file reference to schema/templates.sb resolved │
│ │
│ 180px │
└─────────────────────────────────────────────────────────────────────────────┘
OPTION 3: FOCUSED SINGLE-PANEL (Mobile/Compact)
═══════════════════════════════════════════════════════════════════════════════
┌─────────────────────────────────────────────────────────────────────────────┐
│ ☰ WhiteRabbit.sb ✓ Valid 💾 Save │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ use schema::templates::WonderlandCreature; │
│ │
│ character WhiteRabbit: Rabbit from WonderlandCreature { │
│ // Physical traits │
│ current_size: small │
│ wears_waistcoat: true │
│ has_pocket_watch: true │
│ │
│ // Personality │
│ emotional_state: frightened │
│ awareness_of_absurdity: 0.3 │
│ │
│ ---backstory │
│ Always late, always anxious. │
│ --- │
│ } │
│ │
│ │
├─────────────────────────────────────────────────────────────────────────────┤
│ ← Back to Files Templates: WonderlandCreature, CourtMember │
└─────────────────────────────────────────────────────────────────────────────┘
DETAILED THREE-COLUMN BREAKDOWN
═══════════════════════════════════════════════════════════════════════════════
┌──────────────────────── TITLE BAR (48px) ───────────────────────────────────┐
│ │
│ ⚡ Storybook Editor alice-in-wonderland ◉ Validate │
│ ↑ ↑ ↑ │
│ Logo + Title Project Name Primary CTA │
│ (gold accent) (muted) (gold button)│
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌──────────── LEFT: FILE BROWSER (240-320px, resizable) ─────────────────────┐
│ │
│ FILES [🔍] [+] │
│ ═════ │
│ │
│ 📂 alice-in-wonderland ← Project root │
│ ├─ 📂 world ← Expandable tree │
│ │ ├─ 📂 characters │
│ │ │ ├─ 📄 alice.sb │
│ │ │ ├─ 📄 white_rabbit.sb ← Selected (gold left border) │
│ │ │ ├─ 📄 cheshire_cat.sb │
│ │ │ └─ 📄 mad_tea_party.sb │
│ │ ├─ 📂 institutions │
│ │ └─ 📂 relationships │
│ └─ 📂 schema │
│ ├─ 📄 core_enums.sb │
│ ├─ 📄 templates.sb │
│ └─ 📄 beings.sb │
│ │
│ ───────────────────────── │
│ │
│ RECENT ← Collapsible section │
│ • white_rabbit.sb │
│ • alice.sb │
│ • wonderland_relationships.sb │
│ │
│ ───────────────────────── │
│ │
│ [+ New Character] ← Quick actions │
│ [+ New Relationship] │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌──────────────── CENTER: CODE EDITOR (flex, main area) ─────────────────────┐
│ │
│ white_rabbit.sb Modified • Auto-save: on │
│ ━━━━━━━━━━━━━━━━ ↑ File tab │
│ ↑ Status indicators │
│ ┌─ Line numbers │
│ │ │
│ 1 //! White Rabbit: The anxious herald of Wonderland │
│ 2 │
│ 3 use schema::core_enums::{Size, EmotionalState}; │
│ 4 use schema::templates::WonderlandCreature; │
│ 5 │
│ 6 character WhiteRabbit: Rabbit from WonderlandCreature { │
│ 7 // Physical traits │
│ 8 current_size: small │
│ 9 wears_waistcoat: true │
│ 10 has_pocket_watch: true │
│ 11 │
│ 12 // Personality │
│ 13 emotional_state: frightened │
│ 14 awareness_of_absurdity: 0.3 │
│ 15 │
│ 16 ---backstory ← Prose block marker │
│ 17 Always late, always anxious. Herald of (gold accent) │
│ 18 the Queen of Hearts, perpetually checking │
│ 19 his pocket watch and muttering about time. │
│ 20 --- │
│ 21 } │
│ 22 │
│ │
│ ┌─ Inline validation indicators │
│ └─ Line 6: ✓ WonderlandCreature template resolved │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌───────────── RIGHT: INSPECTOR/METADATA (280-400px, resizable) ─────────────┐
│ │
│ INSPECTOR [⚙️] │
│ ═════════ │
│ │
│ WhiteRabbit ← Current entity │
│ ━━━━━━━━━━━━ (gold underline) │
│ │
│ Type: Character │
│ Species: Rabbit │
│ File: world/characters/white_rabbit.sb │
│ │
│ ───────────────────────── │
│ │
│ TEMPLATES │
│ • WonderlandCreature [View] ← Clickable │
│ • CourtMember [View] │
│ │
│ FIELDS (12) │
│ • current_size: small │
│ • wears_waistcoat: true │
│ • has_pocket_watch: true │
│ • emotional_state: frightened │
│ • awareness_of_absurdity: 0.3 │
│ • loyalty_to_queen: 0.95 │
│ ... [6 more] │
│ │
│ PROSE BLOCKS (1) │
│ • backstory (47 words) │
│ │
│ ───────────────────────── │
│ │
│ VALIDATION ✓ │
│ All checks passed │
│ Last validated: 2s ago │
│ │
│ [◉ Validate Now] ← Gold button │
│ │
│ ───────────────────────── │
│ │
│ RELATIONSHIPS (2) │
│ • AliceAndWhiteRabbit [View] │
│ • WhiteRabbit_Anxiety [View] │
│ │
│ [+ Add Relationship] │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────── BOTTOM: STATUS BAR (32px) ──────────────────────────┐
│ │
│ 🟢 Ready alice-in-wonderland (main) 12 char 7 rel Ln 14 Col 8│
│ ↑ ↑ ↑ ↑ │
│ Status Project & Branch Stats Cursor │
│ (green) (muted text) (muted) (muted) │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
ALTERNATIVE: RELATIONSHIP GRAPH VIEW
═══════════════════════════════════════════════════════════════════════════════
┌─────────────────────────────────────────────────────────────────────────────┐
│ ⚡ Storybook Editor Relationship Graph [Code] [Graph] │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ●─────── pursuer ────────● │
│ Alice WhiteRabbit │
│ ● ● │
│ ╲
│ ╲ seeker_of_
│ ╲ guidance
│ ╲
│ ●─────────────● │
│ CheshireCat │
│ ● │
│ │ │
│ amused │
│ observer │
│ │ │
│ ● │
│ MadHatter ●────●────● MarchHare │
│ co- │
│ conspirators │
│ │
│ [Legend: ● Character ── Relationship Gold = Selected] │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
UI ELEMENT DETAILS
═══════════════════════════════════════════════════════════════════════════════
┌─ Button Styles ──────────────────────────────────────────────────────────┐
│ │
│ PRIMARY (Gold): [◉ Validate] │
│ ↑ gold-500 bg, aubergine-900 text │
│ │
│ SECONDARY (Ghost): [○ Save] │
│ ↑ transparent bg, aubergine-600 border │
│ │
│ TERTIARY (Text): View Edit Delete │
│ ↑ gold-500 text, no background │
│ │
│ ICON ONLY: [🔍] [+] [⚙️] │
│ ↑ 32x32px touch target │
│ │
└───────────────────────────────────────────────────────────────────────────┘
┌─ Input Field ────────────────────────────────────────────────────────────┐
│ │
│ ┌─────────────────────────────────────────────────────────────┐ │
│ │ Search files... │ │
│ └─────────────────────────────────────────────────────────────┘ │
│ ↑ aubergine-800 bg, warm-gray-400 placeholder │
│ ↑ Focus: gold-500 border + glow │
│ │
└───────────────────────────────────────────────────────────────────────────┘
┌─ List Item States ───────────────────────────────────────────────────────┐
│ │
│ 📄 alice.sb ← Default (transparent bg) │
│ 📄 white_rabbit.sb ← Hover (aubergine-700 bg) │
│ ┃ 📄 cheshire_cat.sb ← Selected (gold-500 left border) │
│ 📄 mad_tea_party.sb │
│ │
└───────────────────────────────────────────────────────────────────────────┘
┌─ Toast Notification ─────────────────────────────────────────────────────┐
│ │
│ ┌───────────────────────────────────────────────────┐ ← Top-right │
│ │ ✓ Validation passed [×] │ position │
│ │ All 12 characters valid │ │
│ └───────────────────────────────────────────────────┘ │
│ ↑ aubergine-700 bg, green icon, auto-dismiss 3s │
│ │
└───────────────────────────────────────────────────────────────────────────┘
SPACING REFERENCE (8px grid)
═══════════════════════════════════════════════════════════════════════════════
Panel Padding: 24px (3 × 8px)
Section Spacing: 16px (2 × 8px)
Element Spacing: 8px (1 × 8px)
Tight Spacing: 4px (0.5 × 8px)
Button Padding: 8px × 16px (vertical × horizontal)
Input Padding: 8px × 12px
List Item Height: 32px (4 × 8px)
Title Bar: 48px (6 × 8px)
Status Bar: 32px (4 × 8px)
INTERACTION PATTERNS
═══════════════════════════════════════════════════════════════════════════════
File Selection:
Click file → Highlight with gold border → Load in editor → Show metadata
Validation:
Press Validate → Spinner appears → Success/error toast → Update badges
Auto-save:
Type → 2s debounce → Save indicator → "Saved" confirmation
Panel Resize:
Hover divider → Cursor changes → Drag → Live resize → Snap to min/max
Focus Navigation:
Tab → Next focusable → Visible gold outline
Shift+Tab → Previous focusable
Escape → Close modal/blur input
═══════════════════════════════════════════════════════════════════════════════
END OF MOCKUPS
Which layout speaks to you? We can mix and match elements.
The three-column is great for wide screens, two-column is more focused.

View File

@@ -0,0 +1,486 @@
# Template Merge Implementation Notes
**Author:** Resource Linking Architect
**Date:** 2026-02-12
**Purpose:** Implementation planning for Phase 3 (Resolution)
---
## Core Merge Algorithm
### Data Structures
```rust
// In resolve/merge.rs
/// Tracks behavior links during merge to detect overrides
struct BehaviorMergeContext {
seen_behaviors: HashMap<String, BehaviorLink>, // behavior_name -> link
result: Vec<BehaviorLink>,
warnings: Vec<MergeWarning>,
}
enum MergeWarning {
DefaultConflict {
character_behavior: String,
template_behavior: String,
},
PriorityConflict {
behavior: String,
character_priority: Priority,
template_priority: Priority,
},
}
```
### Merge Function Signature
```rust
pub fn merge_behavior_links(
character_links: Vec<BehaviorLink>,
template_links: Vec<BehaviorLink>,
character_name: &str,
template_name: &str,
) -> Result<(Vec<ResolvedBehaviorLink>, Vec<MergeWarning>), MergeError> {
// Implementation
}
```
### Step-by-Step Algorithm
```rust
fn merge_behavior_links(
character_links: Vec<BehaviorLink>,
template_links: Vec<BehaviorLink>,
character_name: &str,
template_name: &str,
) -> Result<(Vec<ResolvedBehaviorLink>, Vec<MergeWarning>), MergeError> {
let mut ctx = BehaviorMergeContext {
seen_behaviors: HashMap::new(),
result: Vec::new(),
warnings: Vec::new(),
};
// Step 1: Add all character links (highest precedence)
for link in character_links {
let behavior_name = link.tree.join("::");
// Check for duplicate behavior in character itself
if ctx.seen_behaviors.contains_key(&behavior_name) {
return Err(MergeError::DuplicateBehavior {
behavior: behavior_name,
source: character_name.to_string(),
});
}
ctx.seen_behaviors.insert(behavior_name.clone(), link.clone());
ctx.result.push(link);
}
// Step 2: Add template links that aren't overridden
for link in template_links {
let behavior_name = link.tree.join("::");
if let Some(char_link) = ctx.seen_behaviors.get(&behavior_name) {
// Character overrides this behavior - check for conflicts
// Check priority conflict
if let (Some(char_pri), Some(tmpl_pri)) = (&char_link.priority, &link.priority) {
if char_pri != tmpl_pri {
ctx.warnings.push(MergeWarning::PriorityConflict {
behavior: behavior_name.clone(),
character_priority: char_pri.clone(),
template_priority: tmpl_pri.clone(),
});
}
}
// Skip template link (already have character's version)
continue;
}
// No override, add template link
ctx.result.push(link);
}
// Step 3: Check for default conflicts
let char_defaults: Vec<_> = ctx.result.iter()
.filter(|link| link.is_default)
.take(2) // Only need to check if >1
.collect();
if char_defaults.len() > 1 {
ctx.warnings.push(MergeWarning::DefaultConflict {
// ... warning details
});
}
Ok((ctx.result, ctx.warnings))
}
```
### Multi-Level Template Merge
```rust
pub fn resolve_template_hierarchy(
character: &Character,
templates: &HashMap<String, Template>,
) -> Result<(Vec<BehaviorLink>, Vec<ScheduleLink>), MergeError> {
// Step 1: Build template chain (deepest first)
let mut template_chain = Vec::new();
let mut current_template_name = character.template.clone();
while let Some(tmpl_name) = current_template_name {
let template = templates.get(&tmpl_name)
.ok_or(MergeError::UnresolvedTemplate { name: tmpl_name.clone() })?;
template_chain.push(template);
current_template_name = template.parent_template.clone();
}
// Step 2: Merge from deepest template up to character
template_chain.reverse(); // Now [deepest, ..., direct parent]
let mut merged_behaviors = Vec::new();
let mut merged_schedules = Vec::new();
for template in template_chain {
let (behaviors, warnings) = merge_behavior_links(
merged_behaviors,
template.behavior_links.clone(),
character.name,
&template.name,
)?;
merged_behaviors = behaviors;
// TODO: emit warnings
// Same for schedules...
}
// Step 3: Merge character on top
let (final_behaviors, warnings) = merge_behavior_links(
character.behavior_links.clone(),
merged_behaviors,
character.name,
"character",
)?;
Ok((final_behaviors, merged_schedules))
}
```
---
## Complex Merge Scenarios to Test
### Scenario 1: Simple Override
```storybook
template Worker {
uses behaviors: [A, B, C]
}
character Martha from Worker {
uses behaviors: [B, D] // Override B, add D
}
// Expected: [B(Martha), D, A, C]
// A and C come from Worker, B is Martha's version
```
### Scenario 2: Multi-Level with Overrides
```storybook
template Base {
uses behaviors: [A, B]
}
template Mid from Base {
uses behaviors: [B, C] // Override B from Base
}
character Char from Mid {
uses behaviors: [C, D] // Override C from Mid
}
// Merge order:
// 1. Base: [A, B(Base)]
// 2. Mid merges on Base: [B(Mid), C, A]
// 3. Char merges on Mid: [C(Char), D, B(Mid), A]
```
### Scenario 3: Priority Changes Through Chain
```storybook
template Base {
uses behaviors: [{ tree: Rest, priority: high }]
}
template Mid from Base {
uses behaviors: [{ tree: Rest, priority: normal }] // Warning!
}
character Char from Mid {
uses behaviors: [{ tree: Rest, priority: low }] // Warning!
}
// Expected: [Rest(low)]
// Warnings:
// - Mid changed Rest priority from high to normal
// - Char changed Rest priority from normal to low
```
### Scenario 4: Condition Overrides
```storybook
template Worker {
uses behaviors: [
{ tree: Work, when: employed }
]
}
character Martha from Worker {
uses behaviors: [
{ tree: Work, when: at_bakery } // Override condition
]
}
// Expected: [Work(when: at_bakery)]
// Template's Work(when: employed) is completely replaced
```
### Scenario 5: Default Conflicts
```storybook
template Worker {
uses behaviors: [
{ tree: Idle, default: true }
]
}
character Martha from Worker {
uses behaviors: [
{ tree: Rest, default: true }
]
}
// Expected: [Rest(default), Idle(not default)]
// Warning: Both template and character define defaults
```
### Scenario 6: Empty Array Edge Case
```storybook
template Worker {
uses behaviors: [A, B, C]
}
character Martha from Worker {
uses behaviors: [] // What does this mean?
}
// If empty = "clear all": []
// If empty = "ignore, inherit": [A, B, C]
// Waiting for Sienna's decision (Question 1)
```
### Scenario 7: Diamond Inheritance (Not Supported)
```storybook
template A { uses behaviors: [X] }
template B from A { uses behaviors: [Y] }
template C from A { uses behaviors: [Z] }
template D from B, C { ... } // ERROR: Multiple inheritance not supported
```
**Decision:** Single inheritance only (one parent template max).
---
## Error Message Design
### Error: Duplicate Behavior in Same Context
```
error: duplicate behavior definition
┌─ characters/martha.sb:10:9
10 │ { tree: BakeryWork, priority: normal }
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11 │ { tree: BakeryWork, priority: high }
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ duplicate definition
= note: behavior 'BakeryWork' is already defined in this character
= help: remove one of the duplicate definitions or use different behaviors
```
### Warning: Priority Conflict in Template Merge
```
warning: behavior priority changed from template
┌─ characters/martha.sb:9:9
9 │ { tree: Rest, priority: low }
│ ^^^^^^^^^^^^^^^^
= note: template 'Worker' defines 'Rest' with priority 'high'
= note: character's priority (low) will override template's (high)
= help: if this is intentional, no action needed
```
### Warning: Multiple Defaults
```
warning: multiple default behaviors defined
┌─ characters/martha.sb:10:9
10 │ { tree: Rest, default: true }
│ ^^^^^^^^^^^^^^^
= note: template 'Worker' also defines a default behavior: 'Idle'
= note: character's default (Rest) will be used; template's (Idle) remains as non-default
= help: only one default is active at runtime
```
### Error: Unresolved Template
```
error: unresolved template reference
┌─ characters/martha.sb:1:25
1 │ character Martha from Workerr {
│ ^^^^^^^ template 'Workerr' not found
= help: did you mean 'Worker'? (defined in templates/worker.sb)
```
### Warning: Condition Override
```
warning: behavior condition changed from template
┌─ characters/martha.sb:9:9
9 │ { tree: Work, when: at_bakery }
│ ^^^^^^^^^^^^^^^^^^^
= note: template 'Worker' defines 'Work' with condition 'employed'
= note: character's condition will replace template's condition
```
---
## Testing Strategy
### Unit Tests
```rust
#[cfg(test)]
mod merge_tests {
use super::*;
#[test]
fn test_simple_merge() {
let char_links = vec![
behavior_link("BakeryWork", None, None),
];
let tmpl_links = vec![
behavior_link("BasicNeeds", Some(Priority::Critical), None),
behavior_link("Rest", Some(Priority::Normal), None),
];
let (result, warnings) = merge_behavior_links(
char_links, tmpl_links, "Martha", "Worker"
).unwrap();
assert_eq!(result.len(), 3);
assert_eq!(result[0].tree, "BakeryWork");
assert_eq!(result[1].tree, "BasicNeeds");
assert_eq!(result[2].tree, "Rest");
assert!(warnings.is_empty());
}
#[test]
fn test_override_priority() {
let char_links = vec![
behavior_link("Rest", Some(Priority::Low), None),
];
let tmpl_links = vec![
behavior_link("Rest", Some(Priority::High), None),
];
let (result, warnings) = merge_behavior_links(
char_links, tmpl_links, "Martha", "Worker"
).unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].priority, Some(Priority::Low));
assert_eq!(warnings.len(), 1);
assert!(matches!(warnings[0], MergeWarning::PriorityConflict { .. }));
}
#[test]
fn test_multi_level_merge() {
// Base -> Mid -> Char
// Test that merging works through chain
}
#[test]
fn test_default_conflict() {
// Both template and character define defaults
}
#[test]
fn test_empty_array() {
// Waiting for Sienna's decision on semantics
}
}
```
### Integration Tests
```rust
#[test]
fn test_resolve_alice_wonderland() {
// Load Alice example with WonderlandCreature template
// Verify merged behaviors are correct
}
#[test]
fn test_three_level_inheritance() {
// Mortal -> Worker -> Baker -> Martha
// Verify all behaviors present and priorities correct
}
```
---
## Performance Considerations
### Merge Complexity
- Single template: O(n + m) where n=char links, m=template links
- k-level templates: O(k * (n + m))
- Typical case: k=1-3, n+m < 20, negligible overhead
### Optimization Opportunities
1. **Cache merged templates**: If Template X is used by multiple characters, cache its fully-merged result
2. **Early termination**: If character defines no links, skip merge entirely
3. **Lazy merging**: Only merge when links are actually accessed at runtime
**Decision:** Start with simple O(k*n) implementation. Optimize only if profiling shows bottleneck.
---
## Edge Cases Checklist
- [x] Character overrides all template behaviors
- [x] Character overrides some template behaviors
- [x] Character adds new behaviors to template
- [x] Multi-level template chain
- [x] Priority conflicts through chain
- [x] Condition overrides
- [x] Multiple defaults
- [ ] Empty array semantics (awaiting Sienna)
- [x] Diamond inheritance (not supported, single parent only)
- [x] Circular template references (should be caught in validation)
- [x] Template references non-existent parent (error)
- [x] Behavior name resolution fails (error)
---
**Status:** Implementation notes complete, awaiting Checkpoint 2 approval to begin coding.

View File

@@ -0,0 +1,658 @@
# Resource Linking System - Checkpoint 2 Addendum
**Author:** Resource Linking Architect
**Date:** 2026-02-12
**Status:** Draft for Checkpoint 2 Review
**Version:** 0.3
**Addresses:** Checkpoint 1 feedback from Sienna
---
## Changes from Checkpoint 1
This addendum addresses two required changes from Sienna's Checkpoint 1 review:
1. **Template Merging Support** (instead of all-or-nothing replacement)
2. **Dual Operator Support** (`==` and `is` both work)
---
## 1. Template Merging Design
### 1.1 The Problem
**Original Design (Rejected):**
```storybook
template Worker {
uses behaviors: [HandleBasicNeeds, RestWhenTired]
}
character Martha: Human from Worker {
uses behaviors: [BakeryWork] // REPLACES Worker behaviors entirely
}
// Result: Only [BakeryWork] - lost HandleBasicNeeds and RestWhenTired
```
**Requested Behavior:**
Martha should get BOTH Worker behaviors AND her own behaviors, composed together.
### 1.2 Proposed Merge Algorithm
**Merge Semantics:**
- Template links and character links are **concatenated**
- Character links come **first** (evaluated before template links) (no template links come first since that's the "base class")
- If same behavior appears in both, character's version **overrides** template's version
- Priority and conditions are preserved from each source
**Example:**
```storybook
template Worker {
uses behaviors: [
{ tree: HandleBasicNeeds, priority: critical }
{ tree: RestWhenTired, priority: normal }
{ tree: Idle, priority: low }
]
}
character Martha: Human from Worker {
uses behaviors: [
{ tree: BakeryWork, priority: normal }
{ tree: Idle, priority: normal } // Overrides Worker's Idle
]
}
// Merged result:
// [
// { tree: BakeryWork, priority: normal } // From Martha
// { tree: Idle, priority: normal } // From Martha (overrides template)
// { tree: HandleBasicNeeds, priority: critical } // From Worker
// { tree: RestWhenTired, priority: normal } // From Worker
// ]
```
### 1.3 Merge Rules
**Rule 1: Concatenation**
- Merge = `character_links ++ template_links`
- Character links are evaluated first at runtime (higher precedence)
**Rule 2: Override by Name**
- If character defines a link to behavior `X`, template's link to `X` is ignored
- Match by behavior tree name (not by priority or condition)
- Allows character to "replace" template's version of a behavior
**Rule 3: Preserve Priority**
- Priority values are NOT automatically adjusted
- Character can define same behavior with different priority than template
- Runtime selection uses declared priorities as-is
**Rule 4: Preserve Conditions**
- Conditions are NOT merged with boolean operators
- Each link keeps its own condition
- If both have same behavior with different conditions, character's wins (override)
**Rule 5: Default Behavior**
- If both character and template define `default: true` for same link type, character's default wins
- Warning issued if both define defaults (even for different behaviors)
### 1.4 Conflict Resolution
**Conflict Type 1: Same Behavior, Different Priority**
```storybook
template Worker {
uses behaviors: [
{ tree: Rest, priority: high }
]
}
character Martha: Human from Worker {
uses behaviors: [
{ tree: Rest, priority: normal } // Character overrides
]
}
// Result: Martha's Rest(normal) wins, template's Rest(high) ignored
```
**Conflict Type 2: Same Behavior, Different Conditions**
```storybook
template Worker {
uses behaviors: [
{ tree: Work, when: employed }
]
}
character Martha: Human from Worker {
uses behaviors: [
{ tree: Work, when: at_bakery } // Character overrides
]
}
// Result: Martha's Work(when: at_bakery) wins
```
**Conflict Type 3: Multiple Defaults**
```storybook
template Worker {
uses behaviors: [
{ tree: Idle, default: true }
]
}
character Martha: Human from Worker {
uses behaviors: [
{ tree: Rest, default: true } // Warning!
]
}
// Warning: Both template and character define default behaviors
// Resolution: Character's default wins (Rest), template's Idle kept but not default
```
### 1.5 Multi-Level Template Inheritance
Templates can inherit from templates:
```storybook
template LivingBeing {
uses behaviors: [
{ tree: Breathe, priority: critical }
{ tree: Eat, priority: high }
]
}
template Worker from LivingBeing {
uses behaviors: [
{ tree: Work, priority: normal }
{ tree: Rest, priority: normal }
]
}
character Martha: Human from Worker {
uses behaviors: [
{ tree: BakeryWork, priority: normal }
]
}
// Merge chain: LivingBeing -> Worker -> Martha
// Result (in evaluation order):
// [
// BakeryWork(normal), // Martha
// Work(normal), // Worker
// Rest(normal), // Worker
// Breathe(critical), // LivingBeing
// Eat(high) // LivingBeing
// ]
```
**Merge Algorithm for Multi-Level:**
1. Start with deepest template (LivingBeing)
2. Merge next template (Worker) on top
3. Merge character on top
4. At each level, apply override-by-name rule
### 1.6 Explicit Override Syntax (Future Extension)
If we want more control, we could add explicit `override` keyword:
```storybook
character Martha: Human from Worker {
uses behaviors: [
{ tree: Rest, priority: low, override: true } // Explicit override
{ tree: BakeryWork, priority: normal }
]
}
```
**Recommendation:** Start without `override` keyword. Add later if needed.
### 1.7 Empty Array Semantics
**Question:** What if character defines empty array?
```storybook
template Worker {
uses behaviors: [HandleBasicNeeds, Rest]
}
character Martha: Human from Worker {
uses behaviors: [] // Empty!
}
```
**Option A:** Empty array means "use no behaviors" (clear template behaviors)
**Option B:** Empty array is treated as "not defined", inherit from template
**Recommendation:** **Option B** - Only non-empty arrays trigger override/merge. Empty arrays are same as omitted field. (empty array just means "empty array", i think if it's a zero-length array then we would treat it as "not defined")
### 1.8 Updated Merge Pseudocode
```rust
fn merge_behavior_links(
character_links: Vec<BehaviorLink>,
template_links: Vec<BehaviorLink>
) -> Vec<BehaviorLink> {
let mut result = Vec::new();
let mut seen_behaviors = HashSet::new();
// Add character links first (higher precedence)
for link in character_links {
let behavior_name = link.tree.join("::");
seen_behaviors.insert(behavior_name.clone());
result.push(link);
}
// Add template links that don't conflict
for link in template_links {
let behavior_name = link.tree.join("::");
if !seen_behaviors.contains(&behavior_name) {
result.push(link);
}
// else: skip, character already defined this behavior
}
result
}
fn merge_character_with_template(
char: Character,
template: Template
) -> ResolvedCharacter {
// Recursively merge if template has parent template
let template_links = if let Some(parent_template) = template.parent {
merge_behavior_links(template.behavior_links, parent_template.behavior_links)
} else {
template.behavior_links
};
// Merge character on top of (potentially already merged) template
let behavior_links = if !char.behavior_links.is_empty() {
merge_behavior_links(char.behavior_links, template_links)
} else {
template_links // Character doesn't define any, inherit all
};
// Same logic for schedule_links...
ResolvedCharacter { behavior_links, schedule_links, ... }
}
```
### 1.9 Validation Rules
**New Validations:**
1. **Warn on default conflicts:**
```
warning: multiple default behaviors defined
┌─ characters/martha.sb:8:5
8 │ uses behaviors: [
9 │ { tree: Rest, default: true }
= note: template 'Worker' also defines a default behavior: Idle
= help: only one default is used at runtime (character's takes precedence)
```
2. **Warn on priority conflicts (optional):**
```
warning: behavior priority changed from template
┌─ characters/martha.sb:9:9
9 │ { tree: Rest, priority: low }
= note: template 'Worker' defines Rest with priority: high
= help: character's priority (low) will override template's (high)
```
### 1.10 Examples
**Example 1: Simple Composition**
```storybook
template Villager {
uses behaviors: [
{ tree: BasicNeeds, priority: critical }
{ tree: Socialize, priority: normal }
{ tree: Sleep, priority: normal }
]
}
character Martha: Human from Villager {
uses behaviors: [
{ tree: BakeryWork, priority: normal }
]
}
// Martha gets: [BakeryWork, BasicNeeds, Socialize, Sleep]
```
**Example 2: Selective Override**
```storybook
template Villager {
uses behaviors: [
{ tree: BasicNeeds, priority: critical }
{ tree: Sleep, priority: normal }
]
uses schedule: VillagerSchedule
}
character Martha: Human from Villager {
uses behaviors: [
{ tree: Sleep, priority: low } // Override just Sleep
]
uses schedule: BakerSchedule // Override schedule
}
// Behaviors: [Sleep(low), BasicNeeds(critical)]
// Schedule: BakerSchedule (replaces VillagerSchedule)
```
**Example 3: Multi-Level Inheritance**
```storybook
template Mortal {
uses behaviors: [{ tree: Age, priority: critical }]
}
template Worker from Mortal {
uses behaviors: [{ tree: Work, priority: normal }]
}
template Baker from Worker {
uses behaviors: [{ tree: BakeBreed, priority: normal }]
}
character Martha: Human from Baker {
uses behaviors: [{ tree: ManageBakery, priority: normal }]
}
// Result: [ManageBakery, BakeBread, Work, Age]
```
---
## 2. Dual Operator Support (`==` and `is`)
### 2.1 The Request
Sienna wants both `==` and `is` to work for equality comparisons (like Python).
**Examples:**
```storybook
uses behaviors: [
{ tree: Giant, when: current_size == huge } // C-style
{ tree: Tiny, when: current_size is tiny } // Python-style
]
```
### 2.2 Semantic Equivalence
Both operators mean **equality test**:
- `current_size == huge` → true if current_size equals huge
- `current_size is tiny` → true if current_size equals tiny
- No distinction between "identity" and "equality" (like Python's `is` vs `==`)
- They're syntactic alternatives for readability
### 2.3 Grammar Update
```lalrpop
// In parser.lalrpop
CompOp: CompOp = {
">=" => CompOp::Gte,
"<=" => CompOp::Lte,
"==" => CompOp::Eq,
"is" => CompOp::Eq, // NEW - maps to same enum variant
"!=" => CompOp::Neq,
">" => CompOp::Gt,
"<" => CompOp::Lt,
};
```
**AST Representation:**
```rust
pub enum CompOp {
Eq, // Both == and is map to this
Neq,
Lt,
Gt,
Lte,
Gte,
}
```
No AST change needed - both `==` and `is` produce the same `CompOp::Eq`.
### 2.4 Negation
**Question:** Should we support `is not` as well as `!=`? (yes i want this)
```storybook
when: current_size is not huge // Python style
when: current_size != huge // C style
```
**Recommendation:** **Yes**, support `is not` for consistency with Python:
```lalrpop
CompOp: CompOp = {
">=" => CompOp::Gte,
"<=" => CompOp::Lte,
"==" => CompOp::Eq,
"is" => CompOp::Eq,
"is" "not" => CompOp::Neq, // NEW
"!=" => CompOp::Neq,
">" => CompOp::Gt,
"<" => CompOp::Lt,
};
```
### 2.5 Updated Examples
**All valid:**
```storybook
uses behaviors: [
{ tree: Giant, when: size == huge }
{ tree: Giant, when: size is huge }
{ tree: Tiny, when: size != tiny }
{ tree: Tiny, when: size is not tiny }
]
```
### 2.6 Documentation Update
Update language docs to show both styles:
> **Condition Expressions**
>
> Use `when:` clauses to specify conditions for link selection:
>
> ```storybook
> when: emotional_state == frightened // C-style equality
> when: emotional_state is frightened // Python-style equality
> when: size != normal // C-style inequality
> when: size is not normal // Python-style inequality
> ```
>
> Both `==` and `is` mean equality. Both `!=` and `is not` mean inequality.
> Choose whichever reads more naturally for your condition.
---
## 3. Updated Implementation Plan
### Phase 1: AST Extension (Week 1) - UNCHANGED
No changes from original design.
### Phase 2: Parser Implementation (Week 1-2) - UPDATED
**Original:**
1. Implement `BehaviorLinkStmt` and `ScheduleLinkStmt` grammar
2. Implement `BehaviorLinkSpec` and `ScheduleLinkSpec` parsing
3. Add link parsing to character, institution, template productions
4. Write parser tests for all link variations
**Updated:**
1. Implement `BehaviorLinkStmt` and `ScheduleLinkStmt` grammar
2. Implement `BehaviorLinkSpec` and `ScheduleLinkSpec` parsing
3. Add link parsing to character, institution, template productions
4. **Add `is` and `is not` to `CompOp` production** ← NEW
5. Write parser tests for all link variations
6. **Write tests for both `==`/`is` and `!=`/`is not`** ← NEW
### Phase 3: Resolution (Week 2) - UPDATED
**Original:**
1. Implement behavior/schedule name resolution in `resolve/names.rs`
2. Add priority validation
3. Add condition expression validation
4. Implement template merge logic for links in `resolve/merge.rs`
5. Write resolution tests
**Updated:**
1. Implement behavior/schedule name resolution in `resolve/names.rs`
2. Add priority validation
3. Add condition expression validation
4. **Implement template merge algorithm with override-by-name** ← UPDATED
5. **Add multi-level template merge support** ← NEW
6. **Add validation warnings for default/priority conflicts** ← NEW
7. Write resolution tests
8. **Write tests for merge edge cases** ← NEW
### Phase 4: Resolved Types (Week 2) - UNCHANGED
No changes from original design.
### Phase 5: Validation & Diagnostics (Week 3) - UPDATED
**Original:**
1. Implement semantic validation (single default, etc.)
2. Add helpful error messages with fuzzy matching
3. Add warnings for incomplete condition coverage
4. Write validation tests
**Updated:**
1. Implement semantic validation (single default, etc.)
2. Add helpful error messages with fuzzy matching
3. Add warnings for incomplete condition coverage
4. **Add warnings for merge conflicts (defaults, priorities)** ← NEW
5. Write validation tests
### Phase 6: Integration & Documentation (Week 3) - UPDATED
**Original:**
1. Update examples to use new linking syntax
2. Update language documentation
3. Run full test suite
4. Create migration examples (if backward compatibility breaks)
**Updated:**
1. Update examples to use new linking syntax
2. **Update examples to show template merging** ← NEW
3. **Update examples to show both `==` and `is` operators** ← NEW
4. Update language documentation
5. **Document merge algorithm for users** ← NEW
6. Run full test suite
7. Create migration examples (if backward compatibility breaks)
**Total Estimate:** Still 3 weeks (merge adds complexity but not significant time)
---
## 4. Updated Success Criteria
### Must Have
- [x] Unified `uses` keyword for behaviors and schedules
- [x] Single-link syntax works (`uses behavior: Name`)
- [x] Multi-link syntax works (`uses behaviors: [...]`)
- [x] Conditions supported (`when: expression`)
- [x] **Both `==` and `is` operators supported** ← NEW
- [x] **Both `!=` and `is not` operators supported** ← NEW
- [x] Priorities supported for behaviors
- [x] Default fallback supported (`default: true`)
- [x] **Template merging works (concatenation + override)** ← UPDATED
- [x] **Multi-level template inheritance works** ← NEW
- [x] Character linking works
- [x] Institution linking works
- [x] Parser produces correct AST
- [x] Resolver validates references
- [x] **Warnings for merge conflicts** ← NEW
- [x] Clear error messages
- [x] SBIR format defined
---
## 5. Open Questions for Sienna (Checkpoint 2)
### Question 1: Empty Array Semantics ("not defined)
What should this mean?
```storybook
template Worker {
uses behaviors: [HandleNeeds, Rest]
}
character Martha: Human from Worker {
uses behaviors: [] // Empty array!
}
```
**Option A:** Empty = clear template behaviors (Martha has NO behaviors)
**Option B:** Empty = ignore, inherit from template (same as omitting field)
**Recommendation:** Option B
### Question 2: Priority Conflict Warnings (lsp warning is more than fine, yellow squiggly, internal behavior is to override at the character level)
Should we warn when character changes priority of template behavior?
```storybook
template Worker { uses behaviors: [{ tree: Rest, priority: high }] }
character Martha from Worker { uses behaviors: [{ tree: Rest, priority: low }] }
```
**Option A:** Silent override (no warning)
**Option B:** Warn about priority change
**Option C:** Error (must use same priority)
**Recommendation:** Option A (silent) or Option B (warning)
### Question 3: `is not` Support (absolutely yes)
Should we support `is not` alongside `!=`?
**Recommendation:** Yes, for consistency with Python
### Question 4: Merge Validation Level (um... strict is prolly best tbh)
How strict should merge conflict validation be?
**Option A:** Errors on any conflict (strict)
**Option B:** Warnings only (permissive)
**Option C:** Silent (trust user)
**Recommendation:** Option B (warnings)
---
## 6. Summary of Changes
### From Checkpoint 1 to Checkpoint 2:
**Major Change 1: Template Merging**
- ❌ Old: All-or-nothing replacement
- ✅ New: Concatenation with override-by-name
- Character links evaluated first
- Template links added if not overridden
- Multi-level inheritance supported
**Major Change 2: Operator Support**
- ✅ Added: `is` as synonym for `==`
- ✅ Added: `is not` as synonym for `!=`
- No AST changes needed
- Parser accepts both styles
**Minor Changes:**
- Added merge conflict warnings
- Updated examples to show composition
- Updated implementation plan phases
---
**End of Checkpoint 2 Addendum**
**Next Step:** Review with user (Sienna) for approval, then proceed to implementation (Task #9).

View File

@@ -0,0 +1,921 @@
# Resource Linking System Design
**Author:** Resource Linking Architect
**Date:** 2026-02-12
**Status:** Ready for Checkpoint 1 Review
**Version:** 0.2
**Keyword Decision:** `uses` (approved by Sienna)
---
## Executive Summary
This document proposes a unified `uses` keyword system for associating behaviors and schedules with characters and institutions in the Storybook DSL. The design enables:
- Characters using one or more behaviors
- Characters using one or more schedules
- Institutions using behaviors (for institutional operations)
- Institutions using schedules (operating hours, seasonal variations)
- Conditional/contextual selection of linked resources
- Priority-based behavior selection at runtime
---
## 1. Design Goals
### Primary Goals
1. **Unified Syntax**: Single `uses` keyword for both behaviors and schedules
2. **Simple Default Case**: Most common use case should be simple one-liner
3. **Powerful When Needed**: Support complex multi-link scenarios with conditions
4. **Clear Semantics**: Unambiguous about which behavior/schedule applies when
5. **Backward Compatible**: Existing .sb files continue to parse and work
### Non-Goals
1. **Not for Relationships**: Relationship linking remains separate (already exists)
2. **Not Inline Definitions**: Can only link to named behaviors/schedules, not define inline
3. **Not Dynamic Composition**: Links are static at author-time, selection is runtime
---
## 2. Syntax Design
### 2.1 Simple Single Link
The most common case: a character has one primary behavior and one schedule.
```storybook
character Martha: Human {
age: 34
uses behavior: WorkAtBakery
uses schedule: BakerSchedule
}
```
**AST Representation:**
```rust
// Add to Character struct in ast.rs
pub struct Character {
pub name: String,
pub species: Option<String>,
pub fields: Vec<Field>,
pub template: Option<Vec<String>>,
pub behavior_links: Vec<BehaviorLink>, // NEW
pub schedule_links: Vec<ScheduleLink>, // NEW
pub span: Span,
}
```
### 2.2 Multiple Links with Priorities
Characters may have multiple behaviors that activate based on context:
```storybook
character Alice: Human {
age: 7
uses behaviors: [
{ tree: HandleUrgentNeeds, priority: critical }
{ tree: CuriousExplorer, priority: normal }
{ tree: Idle, priority: low }
]
}
```
**Semantics:**
- Higher priority behaviors preempt lower priority ones
- Within same priority, declaration order determines evaluation
- `critical` > `high` > `normal` > `low`
### 2.3 Conditional Links
Links can have `when` conditions for context-based selection:
```storybook
character Alice: Human {
uses behaviors: [
{ tree: HandleUrgentNeeds, priority: critical }
{ tree: GiantBehavior, when: current_size == huge }
{ tree: TinyBehavior, when: current_size == tiny }
{ tree: NormalExploring, default: true }
]
uses schedules: [
{ schedule: SleepSchedule, when: emotional_state == exhausted }
{ schedule: AdventureSchedule, default: true }
]
}
```
**Condition Evaluation:**
- Conditions use the existing expression language (see design.md §5)
- `default: true` means "use this if no condition matches"
- Only one `default` allowed per link type
- Runtime evaluates conditions top-to-bottom
### 2.4 Institution Links
Institutions can link to behaviors and schedules:
```storybook
institution Bakery {
type: commercial
uses behavior: BakeryOperations
uses schedule: BakeryHours
}
```
**Multiple Schedules for Seasons:**
```storybook
institution Bakery {
uses schedules: [
{ schedule: SummerHours, when: season == summer }
{ schedule: WinterHours, when: season == winter }
{ schedule: StandardHours, default: true }
]
}
```
### 2.5 Template Inheritance
Templates can define default links that characters inherit:
```storybook
template WonderlandCreature {
uses behavior: WonderlandBehavior
uses schedule: WonderlandSchedule
}
character CheshireCat: Cat from WonderlandCreature {
// Inherits WonderlandBehavior and WonderlandSchedule
// Can override:
uses behavior: CheshireBehavior // Replaces WonderlandBehavior
}
```
**Override Semantics:**
- If character defines `uses behavior:`, it replaces template's behavior link entirely
- If character defines `uses behaviors: [...]`, it replaces template's behavior links
- No merging—it's full replacement (consistent with current template override system)
---
## 3. AST Design
### 3.1 New AST Types
```rust
// In src/syntax/ast.rs
/// A link to a behavior tree
#[derive(Debug, Clone, PartialEq)]
pub struct BehaviorLink {
pub tree: Vec<String>, // Qualified path to behavior
pub priority: Option<String>, // critical, high, normal, low
pub condition: Option<Expr>, // when clause
pub is_default: bool, // default: true
pub span: Span,
}
/// A link to a schedule
#[derive(Debug, Clone, PartialEq)]
pub struct ScheduleLink {
pub schedule: Vec<String>, // Qualified path to schedule
pub condition: Option<Expr>, // when clause
pub is_default: bool, // default: true
pub span: Span,
}
// Priority levels (could be enum or validated string)
pub enum Priority {
Critical,
High,
Normal,
Low,
}
```
### 3.2 Modified AST Structs
```rust
// Character gains link fields
pub struct Character {
pub name: String,
pub species: Option<String>,
pub fields: Vec<Field>,
pub template: Option<Vec<String>>,
pub behavior_links: Vec<BehaviorLink>, // NEW
pub schedule_links: Vec<ScheduleLink>, // NEW
pub span: Span,
}
// Institution gains link fields
pub struct Institution {
pub name: String,
pub fields: Vec<Field>,
pub behavior_links: Vec<BehaviorLink>, // NEW
pub schedule_links: Vec<ScheduleLink>, // NEW
pub span: Span,
}
// Template can also have links
pub struct Template {
pub name: String,
pub fields: Vec<Field>,
pub includes: Vec<Vec<String>>,
pub behavior_links: Vec<BehaviorLink>, // NEW
pub schedule_links: Vec<ScheduleLink>, // NEW
pub span: Span,
}
```
---
## 4. Parser Design (LALRPOP)
### 4.1 Grammar Productions
```lalrpop
// In parser.lalrpop
// Character definition with optional links
pub Character: Character = {
"character" <name:Ident> <species:SpeciesClause?> <template:TemplateClause?> "{"
<items:CharacterItem*>
"}" => {
let mut fields = vec![];
let mut behavior_links = vec![];
let mut schedule_links = vec![];
for item in items {
match item {
CharacterItem::Field(f) => fields.push(f),
CharacterItem::BehaviorLink(bl) => behavior_links.extend(bl),
CharacterItem::ScheduleLink(sl) => schedule_links.extend(sl),
}
}
Character { name, species, fields, template, behavior_links, schedule_links, span }
}
};
CharacterItem: CharacterItem = {
<Field> => CharacterItem::Field(<>),
<BehaviorLinkStmt> => CharacterItem::BehaviorLink(<>),
<ScheduleLinkStmt> => CharacterItem::ScheduleLink(<>),
};
// Behavior link statement
BehaviorLinkStmt: Vec<BehaviorLink> = {
// Single link: uses behavior: BehaviorName
"uses" "behavior" ":" <path:QualifiedPath> => {
vec![BehaviorLink {
tree: path,
priority: None,
condition: None,
is_default: false,
span,
}]
},
// Multiple links: uses behaviors: [...]
"uses" "behaviors" ":" "[" <links:Comma<BehaviorLinkSpec>> "]" => links,
};
BehaviorLinkSpec: BehaviorLink = {
// { tree: Name, priority: normal, when: condition, default: true }
"{" <fields:Comma<BehaviorLinkField>> "}" => {
let mut tree = None;
let mut priority = None;
let mut condition = None;
let mut is_default = false;
for field in fields {
match field {
BehaviorLinkField::Tree(path) => tree = Some(path),
BehaviorLinkField::Priority(p) => priority = Some(p),
BehaviorLinkField::Condition(c) => condition = Some(c),
BehaviorLinkField::Default => is_default = true,
}
}
BehaviorLink {
tree: tree.expect("tree field required"),
priority,
condition,
is_default,
span,
}
},
};
BehaviorLinkField: BehaviorLinkField = {
"tree" ":" <QualifiedPath> => BehaviorLinkField::Tree(<>),
"priority" ":" <Ident> => BehaviorLinkField::Priority(<>),
"when" ":" <Expr> => BehaviorLinkField::Condition(<>),
"default" ":" "true" => BehaviorLinkField::Default,
};
// Schedule link statement (parallel structure)
ScheduleLinkStmt: Vec<ScheduleLink> = {
"uses" "schedule" ":" <path:QualifiedPath> => {
vec![ScheduleLink {
schedule: path,
condition: None,
is_default: false,
span,
}]
},
"uses" "schedules" ":" "[" <links:Comma<ScheduleLinkSpec>> "]" => links,
};
ScheduleLinkSpec: ScheduleLink = {
"{" <fields:Comma<ScheduleLinkField>> "}" => {
let mut schedule = None;
let mut condition = None;
let mut is_default = false;
for field in fields {
match field {
ScheduleLinkField::Schedule(path) => schedule = Some(path),
ScheduleLinkField::Condition(c) => condition = Some(c),
ScheduleLinkField::Default => is_default = true,
}
}
ScheduleLink {
schedule: schedule.expect("schedule field required"),
condition,
is_default,
span,
}
},
};
ScheduleLinkField: ScheduleLinkField = {
"schedule" ":" <QualifiedPath> => ScheduleLinkField::Schedule(<>),
"when" ":" <Expr> => ScheduleLinkField::Condition(<>),
"default" ":" "true" => ScheduleLinkField::Default,
};
```
---
## 5. Resolution & Validation
### 5.1 Name Resolution
During the resolution pass (Pass 3 in design.md §4.7), the resolver must:
1. **Resolve Behavior Paths**: Each `tree: BehaviorName` must reference a valid `behavior` declaration
2. **Resolve Schedule Paths**: Each `schedule: ScheduleName` must reference a valid `schedule` declaration
3. **Validate Priorities**: Priority values must be one of {critical, high, normal, low}
4. **Validate Conditions**: Expressions in `when` clauses must be valid and type-check
**Error Examples:**
```
error: unresolved behavior reference
┌─ characters/alice.sb:12:23
12 │ uses behavior: CuriousExporer
│ ^^^^^^^^^^^^^^ no behavior named `CuriousExporer` exists
= help: did you mean `CuriousExplorer`? (defined in behaviors/alice_behaviors.sb)
```
### 5.2 Semantic Validation
1. **At Most One Default**: Each link array can have at most one `default: true`
2. **Priority + Default Conflicts**: If `default: true`, priority should be `low` or omitted
3. **Condition Completeness**: Warn if conditions are not exhaustive (no default + gaps in conditions)
**Warning Example:**
```
warning: conditions may not cover all cases
┌─ characters/alice.sb:8:5
8 │ uses behaviors: [
9 │ { tree: GiantBehavior, when: current_size == huge }
10 │ { tree: TinyBehavior, when: current_size == tiny }
11 │ ]
= note: no default behavior specified and conditions don't cover all size values
= help: add a default behavior: { tree: NormalBehavior, default: true }
```
### 5.3 Template Merge Logic
When a character uses `from Template`, behavior and schedule links are merged:
```rust
// Pseudocode for merge logic
fn merge_character_with_template(char: Character, template: Template) -> ResolvedCharacter {
let behavior_links = if !char.behavior_links.is_empty() {
char.behavior_links // Character overrides completely
} else {
template.behavior_links // Inherit from template
};
let schedule_links = if !char.schedule_links.is_empty() {
char.schedule_links
} else {
template.schedule_links
};
// ... merge fields, etc.
}
```
**Key Rule**: If character defines ANY behavior links, template's behavior links are ignored entirely. Same for schedules. This is all-or-nothing replacement, not merging. (no, i like merging. idk how we'll handle conflicts, but i want to support that kinda composition)
---
## 6. Resolved Type Representation
### 6.1 Resolved Link Types
```rust
// In src/types.rs
/// Resolved behavior link with all references resolved
#[derive(Debug, Clone, PartialEq)]
pub struct ResolvedBehaviorLink {
pub tree_name: String, // Fully qualified behavior name
pub priority: Priority, // Resolved to enum
pub condition: Option<Expr>, // Validated expression
pub is_default: bool,
}
/// Resolved schedule link
#[derive(Debug, Clone, PartialEq)]
pub struct ResolvedScheduleLink {
pub schedule_name: String, // Fully qualified schedule name
pub condition: Option<Expr>,
pub is_default: bool,
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Priority {
Critical = 3,
High = 2,
Normal = 1,
Low = 0,
}
```
### 6.2 Updated Resolved Structs
```rust
pub struct ResolvedCharacter {
pub name: String,
pub species: Option<String>,
pub fields: HashMap<String, Value>,
pub prose_blocks: HashMap<String, ProseBlock>,
pub behavior_links: Vec<ResolvedBehaviorLink>, // NEW
pub schedule_links: Vec<ResolvedScheduleLink>, // NEW
pub span: Span,
}
pub struct ResolvedInstitution {
pub name: String,
pub fields: HashMap<String, Value>,
pub behavior_links: Vec<ResolvedBehaviorLink>, // NEW
pub schedule_links: Vec<ResolvedScheduleLink>, // NEW
pub span: Span,
}
```
---
## 7. SBIR Representation Proposal
### 7.1 CHARACTERS Section Extension
Currently, the CHARACTERS section (called ENTITIES in some specs) stores character data. We extend it:
```
CHARACTERS Section:
- count: u32
- characters: [Character...]
Character:
- name: String
- species: Option<String>
- fields: Map<String, Value>
- behavior_links: [BehaviorLink...] <-- NEW
- schedule_links: [ScheduleLink...] <-- NEW
BehaviorLink:
- behavior_id: u32 (index into BEHAVIORS section)
- priority: u8 (0=low, 1=normal, 2=high, 3=critical)
- condition: Option<Expression>
- is_default: bool
ScheduleLink:
- schedule_id: u32 (index into SCHEDULES section)
- condition: Option<Expression>
- is_default: bool
Expression:
- (Existing expression bytecode format from design.md §5)
```
### 7.2 INSTITUTIONS Section Extension
```
INSTITUTIONS Section:
- count: u32
- institutions: [Institution...]
Institution:
- name: String
- fields: Map<String, Value>
- behavior_links: [BehaviorLink...] <-- NEW
- schedule_links: [ScheduleLink...] <-- NEW
```
### 7.3 BEHAVIORS and SCHEDULES Sections
These sections remain unchanged—they define the behavior trees and schedules. Links reference them by index.
**Index Resolution:**
- During compilation, behavior/schedule names are resolved to their index in the respective section
- At runtime, the engine uses the index to look up the behavior/schedule definition
---
## 8. Runtime Link Resolution Algorithm
### 8.1 Behavior Selection
When the engine needs to select a behavior for a character:
```rust
fn select_behavior(character: &Character, context: &RuntimeContext) -> Option<&Behavior> {
let mut candidates: Vec<_> = character.behavior_links
.iter()
.filter(|link| {
// Evaluate condition if present
link.condition.is_none() || evaluate_condition(&link.condition, context)
})
.collect();
if candidates.is_empty() {
return None;
}
// Sort by priority (descending)
candidates.sort_by(|a, b| b.priority.cmp(&a.priority));
// Return highest priority candidate
// (If multiple same priority, declaration order is preserved by stable sort)
let selected = candidates[0];
Some(get_behavior_by_id(selected.behavior_id))
}
```
**Key Properties:**
- Priority-based selection: higher priority wins
- Conditions filter candidates before priority sorting
- `default: true` only matters if no conditions match (it's implicitly `when: true`)
- Deterministic: same context always yields same behavior
### 8.2 Schedule Selection
```rust
fn select_schedule(entity: &Entity, context: &RuntimeContext) -> Option<&Schedule> {
for link in &entity.schedule_links {
if link.is_default {
continue; // Skip default, check it last
}
if link.condition.is_none() || evaluate_condition(&link.condition, context) {
return Some(get_schedule_by_id(link.schedule_id));
}
}
// No conditions matched, use default if present
entity.schedule_links
.iter()
.find(|link| link.is_default)
.map(|link| get_schedule_by_id(link.schedule_id))
}
```
**Key Properties:**
- First-match semantics: first condition that evaluates to true wins
- Default is fallback: only used if no condition matches
- Order matters: earlier links are checked first
---
## 9. Examples
### 9.1 Simple Character with Behavior and Schedule
```storybook
behavior BakerBehavior {
> {
check_oven
serve_customers
clean_workspace
}
}
schedule BakerSchedule {
block work { 5:00 - 13:00 }
block lunch { 13:00 - 14:00 }
block home { 14:00 - 22:00 }
block sleep { 22:00 - 5:00 }
}
character Martha: Human {
age: 34
occupation: baker
uses behavior: BakerBehavior
uses schedule: BakerSchedule
}
```
### 9.2 Character with Multiple Context-Dependent Behaviors
```storybook
character Alice: Human {
age: 7
current_size: normal
emotional_state: curious
uses behaviors: [
{ tree: PanicBehavior, priority: critical, when: emotional_state == frightened }
{ tree: GiantBehavior, when: current_size == huge }
{ tree: TinyBehavior, when: current_size == tiny }
{ tree: BraveBehavior, when: emotional_state == brave }
{ tree: CuriousExplorer, default: true }
]
uses schedules: [
{ schedule: SleepingSchedule, when: emotional_state == exhausted }
{ schedule: AdventureSchedule, default: true }
]
}
```
### 9.3 Institution with Seasonal Schedules
```storybook
schedule SummerHours {
block open { 6:00 - 20:00 }
block closed { 20:00 - 6:00 }
}
schedule WinterHours {
block open { 7:00 - 18:00 }
block closed { 18:00 - 7:00 }
}
institution Bakery {
type: commercial
uses behavior: BakeryOperations
uses schedules: [
{ schedule: SummerHours, when: season == summer }
{ schedule: WinterHours, when: season == winter }
]
}
```
### 9.4 Template with Default Links
```storybook
behavior WonderlandBehavior {
> {
speak_nonsense
violate_logic
}
}
schedule WonderlandSchedule {
block awake { 0:00 - 24:00 } // Always awake in dreams
}
template WonderlandCreature {
uses behavior: WonderlandBehavior
uses schedule: WonderlandSchedule
}
character CheshireCat: Cat from WonderlandCreature {
// Inherits WonderlandBehavior and WonderlandSchedule
can_vanish: true
}
character Alice: Human from WonderlandCreature {
// Overrides behavior but keeps schedule
uses behavior: CuriousExplorer
}
```
---
## 10. Open Questions for User Review (Checkpoint 1)
### Question 1: Priority vs. Declaration Order
**Current Design:** Priority determines order, then declaration order breaks ties.
**Alternative:** Remove priority, use only declaration order (simpler but less expressive).
**Recommendation:** Keep priority. It's more explicit and handles common use cases like "urgent needs always trump routine activities." (i guess priority is fine)
### Question 2: Condition Syntax Sugar
**Current Design:** Full condition expressions.
**Alternative:** Add syntactic sugar for common patterns:
```storybook
uses behaviors: [
{ tree: GiantBehavior, when: current_size == huge }
// vs.
{ tree: GiantBehavior, when current_size: huge } // shorter
]
```
**Recommendation:** Start with full expressions, add sugar if usage reveals patterns. (use `==` or `is`, support both like python does.)
### Question 3: Schedule-Behavior Integration
**Current Design:** Behaviors and schedules are separate links. Schedule determines WHEN, behavior determines WHAT.
**Alternative:** Allow schedules to specify behaviors inline:
```storybook
schedule WorkSchedule {
block work { 9:00 - 17:00, behavior: WorkBehavior }
}
```
**Recommendation:** Defer inline behaviors to schedule system design (Task #8). Keep linking separate for now. (yeah that's fine)
### Question 4: Link Override Semantics
**Current Design:** If character defines any behavior links, template's links are completely replaced.
**Alternative:** Merge character and template links (character links come first, then template links).
**Recommendation:** Keep replacement semantics. It's clearer and matches existing override system. (sure? i don't remember the nuances tbh)
### Question 5: Multiple Defaults
**Current Design:** At most one `default: true` per link type.
**Alternative:** Allow multiple defaults with priority order.
**Recommendation:** Keep single default. Multiple defaults creates ambiguity. (single default makes sense?)
---
## 11. Implementation Plan (for Task #6)
### Phase 1: AST Extension (Week 1)
1. Add `BehaviorLink` and `ScheduleLink` structs to `ast.rs`
2. Add link fields to `Character`, `Institution`, `Template`
3. Update `Declaration` enum if needed
### Phase 2: Parser Implementation (Week 1-2)
1. Implement `BehaviorLinkStmt` and `ScheduleLinkStmt` grammar
2. Implement `BehaviorLinkSpec` and `ScheduleLinkSpec` parsing
3. Add link parsing to character, institution, template productions
4. Write parser tests for all link variations
### Phase 3: Resolution (Week 2)
1. Implement behavior/schedule name resolution in `resolve/names.rs`
2. Add priority validation
3. Add condition expression validation
4. Implement template merge logic for links in `resolve/merge.rs`
5. Write resolution tests
### Phase 4: Resolved Types (Week 2)
1. Add `ResolvedBehaviorLink` and `ResolvedScheduleLink` to `types.rs`
2. Update `ResolvedCharacter` and `ResolvedInstitution`
3. Implement conversion in `resolve/convert.rs`
4. Write conversion tests
### Phase 5: Validation & Diagnostics (Week 3)
1. Implement semantic validation (single default, etc.)
2. Add helpful error messages with fuzzy matching
3. Add warnings for incomplete condition coverage
4. Write validation tests
### Phase 6: Integration & Documentation (Week 3)
1. Update examples to use new linking syntax
2. Update language documentation
3. Run full test suite
4. Create migration examples (if backward compatibility breaks)
**Total Estimate:** 3 weeks implementation after design approval.
---
## 12. Success Criteria
### Must Have
- [x] Unified `uses` keyword for behaviors and schedules
- [x] Single-link syntax works (`uses behavior: Name`)
- [x] Multi-link syntax works (`uses behaviors: [...]`)
- [x] Conditions supported (`when: expression`)
- [x] Priorities supported for behaviors
- [x] Default fallback supported (`default: true`)
- [x] Template inheritance works
- [x] Character linking works
- [x] Institution linking works
- [x] Parser produces correct AST
- [x] Resolver validates references
- [x] Clear error messages
- [x] SBIR format defined
### Should Have
- [ ] Warning for incomplete condition coverage
- [ ] Examples for all use cases
- [ ] Migration guide if needed
- [ ] Runtime selection algorithm specification
- [ ] Performance characteristics documented
### Nice to Have
- [ ] Visual editor support design
- [ ] Auto-completion for behavior/schedule names
- [ ] Link refactoring tools
---
## 13. Risks & Mitigation
| Risk | Likelihood | Impact | Mitigation |
|------|-----------|--------|------------|
| Syntax conflicts with relationship linking | Low | Medium | Different syntax context—parser can disambiguate |
| Complex condition expressions hard to debug | Medium | Medium | Good error messages, warnings for non-exhaustive conditions |
| Priority system confusing for users | Medium | Low | Clear documentation, examples, default priority=normal |
| Template override semantics unclear | Medium | Medium | Explicit documentation, validation warnings |
| SBIR encoding inefficient | Low | Low | Use indices for references, compress expressions |
| Runtime selection too slow | Low | Medium | Profile early, cache selections if needed |
---
## Appendix A: Comparison with Relationship Linking
**Relationship Linking** (existing):
- Top-level `relationship` declarations
- Participants within relationships
- Bidirectional by nature
- `self`/`other` blocks for asymmetry
**Resource Linking** (this design):
- Links within character/institution definitions
- References to external behaviors/schedules
- Unidirectional (character → behavior)
- Conditions/priorities for selection
These are complementary systems serving different purposes. No conflict.
---
## Appendix B: Grammar Sketch (Full)
```lalrpop
// Simplified grammar showing link integration
Character: Character = {
"character" <name:Ident> <species:SpeciesClause?> <template:TemplateClause?>
"{" <items:CharacterItem*> "}" => // ... construct Character
};
CharacterItem = {
Field,
BehaviorLinkStmt,
ScheduleLinkStmt,
ProseBlock,
};
BehaviorLinkStmt: Vec<BehaviorLink> = {
"uses" "behavior" ":" <QualifiedPath> => // single link
"uses" "behaviors" ":" "[" <Comma<BehaviorLinkSpec>> "]" => // multi link
};
BehaviorLinkSpec: BehaviorLink = {
"{" <Comma<BehaviorLinkField>> "}" => // parse fields into BehaviorLink
};
BehaviorLinkField = {
"tree" ":" QualifiedPath,
"priority" ":" Ident,
"when" ":" Expr,
"default" ":" "true",
};
// Parallel structure for schedules...
```
---
**End of Design Document**
**Next Step:** Present to user (Sienna) for Checkpoint 1 review and approval.

387
design/ui-architecture.md Normal file
View File

@@ -0,0 +1,387 @@
# Storybook Editor UI Architecture
> Structured creative tools for non-technical storytelling
## Design Philosophy
**Primary User (Lonni)**: Creative writer who thinks in characters, relationships, and narrative arcs—NOT files and syntax.
**Secondary User (Sienna)**: Developer who needs access to raw `.sb` files for behavior trees, schemas, and engine integration.
**Core Principle**: The editor should feel like **writing**, not coding. Structured panes guide creation. The file editor is "manual mode" for power users.
## Application Structure
```
┌─────────────────────────────────────────────────────────────────────┐
│ Storybook Editor - examples/alice-in-wonderland/ │
├──────────────┬──────────────────────────────────────┬───────────────┤
│ │ │ │
│ Entity │ Character Editor │ Quick │
│ Browser │ │ Actions │
│ │ ┌─────────────────────────────────┐ │ │
│ Characters │ │ Name: Alice │ │ ✓ Validate │
│ ├─ Alice │ │ Age: 7 │ │ ⎇ Versions │
│ ├─ Rabbit │ │ Species: Human │ │ ⊕ New Char │
│ └─ Queen │ │ │ │ ⊕ New Rel │
│ │ │ Traits │ │ ⊕ New Place │
│ Relations │ │ curiosity: 0.95 [━━━━━━━━━•─] │ │ │
│ ├─ Alice │ │ politeness: 0.75 [━━━━━━━•───] │ │ 🔍 Search... │
│ │ ↔ Rabbit │ │ │ │ │
│ └─ Queen │ │ Backstory │ │ Git │
│ ↔ King │ │ ┌─────────────────────────────┐ │ │ main │
│ │ │ │ Alice was beginning to get │ │ │ 3m ago │
│ Places │ │ │ very tired of sitting by │ │ │ 12 changes │
│ ├─ Rabbit │ │ │ her sister on the bank... │ │ │ │
│ │ Hole │ │ │ │ │ └───────────────┘
│ └─ Tea │ │ └─────────────────────────────┘ │
│ Party │ │ │
│ │ │ [Portrait slot: click to add] │
│ Schedules │ │ │
│ Templates │ │ [Save Character] │
│ Behaviors │ └─────────────────────────────────┘
│ │ │
│ [Manual] │ Tabs: Traits | Backstory | Schedule│
│ │ Relationships | Behavior │
└──────────────┴──────────────────────────────────────┴───────────────┘
```
## Primary Views (Structured Editing)
### 1. Character Editor
**Purpose**: Create and edit characters through forms and rich text, NOT raw `.sb` syntax.
**Layout**:
- **Header**: Name input (large, prominent)
- **Metadata Row**: Age, species, template dropdowns
- **Traits Section**: Sliders for numeric traits, chips for tags
- **Prose Sections**: Tabbed rich text areas for backstory, appearance, personality
- **Portrait**: Image upload with preview
- **Relationships**: Quick list with "Edit" buttons
- **Schedule**: Mini timeline preview with "Edit Full Schedule" button
**Interaction**:
- All changes auto-save to `.sb` file (no explicit save button for small edits)
- Git auto-commit on major milestones (e.g., "Update Alice: added backstory")
- Validation errors show inline (e.g., red border + tooltip for out-of-range trait)
**Example**:
```
┌─────────────────────────────────────────────────────┐
│ Name: Alice [Portrait]│
│ Age: 7 Species: Human Template: Child │
├─────────────────────────────────────────────────────┤
│ Traits │
│ curiosity 0.95 [━━━━━━━━━━━━━━━•─] │
│ politeness 0.75 [━━━━━━━━━━━•─────] │
│ fear_of_queen 0.10 [━•──────────────────] │
│ │
│ Tags: [imaginative] [brave] [+ Add Tag] │
├─────────────────────────────────────────────────────┤
│ ⎔ Backstory │ Appearance │ Personality │
├─────────────────────────────────────────────────────┤
│ Alice was beginning to get very tired of sitting │
│ by her sister on the bank, and of having nothing │
│ to do... │
│ │
│ [Rich text editor with basic formatting] │
└─────────────────────────────────────────────────────┘
```
### 2. Relationship Editor
**Purpose**: Visually connect characters and define relationship properties.
**Layout**:
- **Graph View**: Force-directed graph with character nodes
- **Edge Selection**: Click edge to edit relationship details
- **Properties Panel**: Bond strength, coordination level, bond type
- **Asymmetric Fields**: "From Alice's perspective" / "From Rabbit's perspective"
**Interaction**:
- Drag nodes to rearrange
- Click edge → properties panel appears
- Slider for bond strength (0.0 to 1.0)
- Dropdown for bond type (romantic, familial, friendship, etc.)
**Example**:
```
┌─────────────────────────────────────────────────────────────┐
│ Relationship: Alice ↔ White Rabbit │
├─────────────────────────────────────────────────────────────┤
│ Bond Strength: 0.65 [━━━━━━━•──────] │
│ Bond Type: friendship │
│ Coordination: ad_hoc │
├─────────────────────────────────────────────────────────────┤
│ From Alice's perspective: │
│ Respect for punctuality: 0.80 [━━━━━━━━•──] │
│ │
│ From White Rabbit's perspective: │
│ Anxiety about lateness: 0.95 [━━━━━━━━━━━•─] │
├─────────────────────────────────────────────────────────────┤
│ [Graph View] │
│ │
│ Alice ●━━━━━━● White Rabbit │
│ │
│ Queen ●━━━━━━━━━━● King │
│ ╲
│ ●━━━━━━━━━━● │
│ Guards │
└─────────────────────────────────────────────────────────────┘
```
### 3. Schedule Builder
**Purpose**: Visual timeline for daily routines (no typing time codes!).
**Layout**:
- **Timeline**: Horizontal 24-hour view with draggable blocks
- **Activity Palette**: Drag activities from sidebar onto timeline
- **Activity Editor**: Click block to edit parameters
**Interaction**:
- Drag edges to resize time blocks
- Drag from palette to add new activity
- Snap to 15-minute increments
- Validation: overlapping blocks show warning
**Example**:
```
┌─────────────────────────────────────────────────────────────┐
│ Schedule: Alice's Day │
├────┬────────────────────────────────────────────────────────┤
│ │ 00:00 06:00 12:00 18:00 24:00 │
│ ├───────────────────────────────────────────────────────┤
│ Su │ ████Sleep████│Breakfast│Play│Tea│Study│Play│█Sleep██ │
│ Mo │ ████Sleep████│Breakfast│School██████│Play│██Sleep███ │
│ └───────────────────────────────────────────────────────┘
│ │
│ Palette: │
│ [Sleep] [Eat] [Play] [Study] [Social] [+ Custom] │
│ │
│ Selected: Tea Party (16:00 → 17:00) │
│ Location: Garden │
│ Participants: Mad Hatter, March Hare, Dormouse │
└─────────────────────────────────────────────────────────────┘
```
### 4. Behavior Tree Builder (Advanced)
**Purpose**: Visual node graph for programmable behaviors (Sienna's domain, but approachable for Lonni).
**Layout**:
- **Canvas**: Infinite 2D space for nodes
- **Node Palette**: Selector, Sequence, Condition, Action, Decorator
- **Inspector**: Selected node's properties
**Interaction**:
- Drag nodes from palette
- Connect with edges (parent → children)
- Right-click node → context menu (delete, duplicate, etc.)
- Validation: orphan nodes, missing actions show errors
**Example**:
```
┌─────────────────────────────────────────────────────────────┐
│ Behavior: WorkAtBakery │
├─────────────────────────────────────────────────────────────┤
│ Palette: [Selector] [Sequence] [?Condition] [→Action] │
│ │
│ Canvas: │
│ │
│ ┌─────────────┐ │
│ │ Selector │ │
│ └──────┬──────┘ │
│ ┌────────┼────────┐ │
│ ▼ ▼ ▼ │
│ ┌──────┐ ┌──────┐ ┌──────┐ │
│ │ Prep │ │ Bake │ │ Sell │ │
│ │ Dough│ │Bread │ │ Bread│ │
│ └──────┘ └──────┘ └──────┘ │
│ │
│ Selected: PrepDough │
│ Action: knead_dough │
│ Duration: 30 minutes │
│ Required Skill: baking > 0.5 │
└─────────────────────────────────────────────────────────────┘
```
## Secondary View (Manual Mode)
### File Editor
**Purpose**: Direct `.sb` syntax editing for power users and edge cases.
**When to Use**:
- Behavior trees (until visual editor is built)
- Templates with ranges
- Advanced features not yet in GUI
- Bulk find-replace across characters
**Layout**:
- Syntax-highlighted text editor (Monaspace Neon)
- Live validation with inline errors
- File tree on left
**Interaction**:
- Standard text editor keybindings
- Ctrl+S saves (git auto-commit)
- Errors show inline with gold underline
- Click error → jump to diagnostic
## Entity Browser (Left Sidebar)
**Purpose**: Navigate the storybook's content hierarchy.
**Structure**:
```
📂 characters/
├─ 👤 Alice
├─ 🐰 White Rabbit
├─ 👑 Queen of Hearts
└─ 👑 King of Hearts
💑 relationships/
├─ Alice ↔ White Rabbit
├─ Queen ↔ King
└─ Queen → Subjects (all)
📍 locations/
├─ 🕳️ Rabbit Hole
├─ 🫖 Tea Party Garden
└─ 🏰 Queen's Castle
📋 templates/
├─ WonderlandCreature
├─ PlayingCard
└─ CourtMember
🎭 behaviors/
├─ WorkAtBakery
└─ AttendTeaParty
📅 schedules/
└─ DailyRoutine
```
**Interaction**:
- Click entity → load in main panel
- Right-click → context menu (delete, duplicate, etc.)
- Drag-drop to reorder or move files
- Search box filters by name
## Quick Actions Pane (Right Sidebar)
**Purpose**: Common tasks and project status at fingertips.
**Sections**:
1. **Actions**
- ✓ Validate All
- ⊕ New Character
- ⊕ New Relationship
- ⊕ New Location
- 🔍 Search Everything
2. **Git Status**
- Current branch: `main`
- Last change: `3 minutes ago`
- Uncommitted changes: `12 files`
- [View History] button → timeline view
3. **Diagnostics**
- ✓ No errors
- ⚠ 2 warnings
- Click to expand inline
4. **Project Info**
- Characters: 12
- Relationships: 7
- Locations: 5
## Responsive Behavior
### Wide Screen (> 1440px)
Three columns: Entity Browser | Main Editor | Quick Actions
### Standard (10241440px)
Two columns: Entity Browser | Main Editor
Quick Actions → collapsible panel
### Compact (< 1024px)
Single column focus mode
Entity Browser → drawer (slides in from left)
Quick Actions → toolbar at top
## Technical Implementation Notes
### Iced Framework
- Use `iced::widget::*` for built-in components
- Custom widgets for:
- Timeline (schedule builder)
- Graph (relationship map)
- Node Canvas (behavior tree builder)
- Trait Slider (with live value display)
### File Synchronization
- Watch `.sb` files for external changes
- Reload editor state on change
- Warn if unsaved changes conflict
### Git Integration
- Auto-commit on save with descriptive messages:
- "Update Alice: modified backstory"
- "Add relationship: Alice ↔ White Rabbit"
- "Create character: Cheshire Cat"
- Use `git2` crate (no shell commands)
- Expose branches as "versions" in UI
### Theme Application
- Load colors from `design/color-palette.md`
- Apply 8px grid to all spacing
- Use Monaspace Neon for code blocks
- Geist for UI text
- Gradient background: aubergine (sides) → cream (center) in main editor panel
## User Workflows
### Lonni Creates a New Character
1. Click "⊕ New Character" in Quick Actions
2. Fill in name, age, species (dropdowns guided by schema)
3. Adjust trait sliders (validation prevents out-of-range)
4. Write backstory in rich text area
5. Upload portrait image
6. Click "Save" → `.sb` file created, git commit happens
7. Character appears in Entity Browser
### Lonni Builds a Relationship
1. Click "⊕ New Relationship"
2. Select two characters from dropdowns
3. Drag bond strength slider
4. Pick bond type from predefined options
5. Fill in asymmetric fields (Alice's view vs. Rabbit's view)
6. Save → `.sb` file updated, git commit
### Sienna Edits a Behavior Tree
1. Click behavior in Entity Browser
2. See visual node graph
3. Drag new "Action" node from palette
4. Connect to parent Sequence
5. Set action parameters in inspector
6. Save → `.sb` file updated
### Both Users Check Validation
1. Quick Actions shows "⚠ 2 warnings"
2. Click to expand → shows inline errors
3. Click error → jumps to problematic entity
4. Fix in structured editor
5. Validation updates live
---
*This architecture prioritizes Lonni's creative flow while keeping power-user features accessible for Sienna.*

429
design/visual-design.md Normal file
View File

@@ -0,0 +1,429 @@
# Storybook Editor Visual Design System
> A synthesis of Teenage Engineering's bold minimalism and Dieter Rams' functionalist precision.
## Design Principles
### Core Tenets
1. **Functional First** - Every element serves a purpose. No decoration for decoration's sake.
2. **Grid-Based Precision** - All elements align to an 8px base grid. Spacing is deliberate.
3. **Restrained + Energetic** - Sophisticated aubergine foundation with bold gold accents.
4. **Tactile Clarity** - Interactions feel deliberate. Buttons have weight. Feedback is immediate.
5. **Honest Materials** - Code is monospace. Text is sans-serif. No skeuomorphism.
6. **Long-Lasting** - Timeless aesthetic that won't feel dated in 5 years.
### The Rams x TE Synthesis
| Dieter Rams (Braun) | Teenage Engineering | Our Synthesis |
|---------------------|---------------------|---------------|
| "Less, but better" | Bold, playful color | Minimal structure, bold accents |
| Neutral grays | Signature orange | Aubergine + gold |
| Pure function | Emotional response | Functional with character |
| Unobtrusive | Visual delight | Delightful when needed |
| Grid systems | Grid systems | 8px base grid |
## Color System
### Primary Palette
```
Aubergine Foundation:
├─ aubergine-900 #1a0f1e Background (darkest)
├─ aubergine-800 #2b1a33 Surface (cards, panels)
├─ aubergine-700 #3d2447 Surface hover
├─ aubergine-600 #4f2e5b Borders
├─ aubergine-500 #61386f Subtle highlights
├─ aubergine-400 #805793 Active elements
└─ aubergine-300 #9f76a7 Muted text
Gold Accents:
├─ gold-500 #f4a261 Primary accent (TE orange-gold)
├─ gold-400 #f6b47a Hover state
└─ gold-300 #f8c594 Pressed state
Supporting:
├─ cream #fdf8f3 High contrast text
├─ warm-gray-100 #e8e3dd Secondary text
├─ warm-gray-400 #8b8680 Muted text
└─ warm-gray-700 #4a4845 Disabled elements
```
### Semantic Colors
```
Success: #6a994e Muted olive green (validation passed)
Warning: #f4a261 Gold (reuse accent)
Error: #d62828 Deep red (parse errors)
Info: #9f76a7 Light aubergine (hints)
```
### Usage Guidelines
- **Backgrounds**: Always aubergine-900 or aubergine-800
- **Text**: Cream for primary, warm-gray-100 for secondary
- **Accents**: Gold only for interactive elements and key information
- **Borders**: aubergine-600 for subtle division, gold-500 for focus
- **Code**: Syntax highlighting uses muted versions of semantic colors
## Typography
### Typeface Stack
```
UI Text (Sans-Serif):
Primary: Geist, system-ui, -apple-system, sans-serif
Weights: 400 (regular), 500 (medium), 600 (semibold)
Code (Monospace):
Primary: "Monaspace Neon", "Monaspace", monospace
Weights: 400 (regular), 600 (semibold)
Features: Enable 'calt' for texture healing, ligatures
Fallback: "JetBrains Mono", "Fira Code", Consolas, monospace
Headings (Sans-Serif):
Primary: Same as UI but always semibold (600)
```
### Type Scale (8px base grid)
```
Heading 1: 24px / 32px semibold (Panel titles)
Heading 2: 20px / 28px semibold (Section headers)
Heading 3: 16px / 24px semibold (Subsections)
Body: 14px / 20px regular (Default text)
Body Small: 12px / 16px regular (Secondary info)
Caption: 11px / 16px regular (Labels, metadata)
Code: 13px / 20px regular (Monospace content)
```
### Guidelines
- **Line Height**: 1.41.6 for readability
- **Letter Spacing**: Default for body, +0.02em for labels/captions
- **Hierarchy**: Size + weight + color (don't rely on size alone)
- **Monospace**: Only for code, file paths, and data values
## Spacing System
### 8px Base Grid
```
Micro: 4px (Icon padding, tight spacing)
Small: 8px (Default gap between related elements)
Medium: 16px (Section spacing)
Large: 24px (Panel padding)
XLarge: 32px (Major section separation)
XXLarge: 48px (Page margins, hero spacing)
```
### Application
- All element sizes divisible by 8px
- Padding and margins follow the scale above
- Icons are 16px or 24px
- Buttons have 8px vertical padding, 16px horizontal
- Panel padding is 24px
## Component Patterns
### Buttons
```
Primary (Gold):
├─ Background: gold-500
├─ Text: aubergine-900 (dark text on light button)
├─ Padding: 8px 16px
├─ Border Radius: 4px
├─ Hover: gold-400
└─ Active: gold-300
Secondary (Ghost):
├─ Background: transparent
├─ Border: 1px solid aubergine-600
├─ Text: cream
├─ Hover: aubergine-700 background
└─ Active: aubergine-600 background
Tertiary (Text Only):
├─ Background: transparent
├─ Text: gold-500
├─ Hover: gold-400
└─ Underline on hover
```
### Input Fields
```
Text Input:
├─ Background: aubergine-800
├─ Border: 1px solid aubergine-600
├─ Text: cream
├─ Placeholder: warm-gray-400
├─ Focus: Border gold-500, glow 0 0 0 2px gold-500/20%
├─ Padding: 8px 12px
└─ Border Radius: 4px
Code Input (Textarea):
├─ Background: aubergine-900
├─ Font: JetBrains Mono 13px
├─ Padding: 16px
└─ Syntax highlighting with muted semantic colors
```
### Panels/Cards
```
Surface:
├─ Background: aubergine-800
├─ Border: 1px solid aubergine-600
├─ Border Radius: 8px
├─ Padding: 24px
└─ Shadow: 0 2px 8px rgba(0,0,0,0.3)
Nested Surface:
├─ Background: aubergine-700
└─ Same border/radius but lighter
```
### Lists
```
File List / Tree:
├─ Item height: 32px (divisible by 8)
├─ Hover: aubergine-700 background
├─ Selected: aubergine-600 + gold-500 left border (4px)
├─ Text: cream for name, warm-gray-400 for metadata
└─ Indent: 16px per level
Data List:
├─ Row height: 40px
├─ Alternating backgrounds: aubergine-800 / aubergine-750
├─ Border: 1px solid aubergine-600 between rows
└─ Hover: gold-500/10% overlay
```
### Status Indicators
```
Validation Badge:
├─ Success: 20px circle, green background, white checkmark
├─ Error: 20px circle, red background, white X
├─ Warning: 20px circle, gold background, dark !
└─ Position: Top-right corner of validated element
Loading Spinner:
├─ Size: 16px or 24px
├─ Color: gold-500
├─ Animation: Smooth rotation, 1s duration
└─ Use sparingly (only for async operations)
```
## Layout Architecture
### Grid System
```
Base Grid: 8px
Columns: 12 or 24 column system
Gutter: 16px
Max Width: 1600px (ultra-wide consideration)
```
### Panel Proportions
```
Three-Column Layout (File Browser | Editor | Inspector):
├─ File Browser: 240px320px (fixed, resizable)
├─ Editor: Flex-grow (takes remaining space)
└─ Inspector: 280px400px (fixed, resizable)
Two-Column Layout (File Browser | Editor):
├─ File Browser: 240px320px
└─ Editor: Flex-grow
Diagnostics Panel (Bottom):
├─ Height: 200px400px (resizable)
└─ Full width
```
### Z-Index Layers
```
1. Base (0): Background, surfaces
2. Content (1): Text, images, forms
3. Overlays (10): Dropdowns, tooltips
4. Modals (100): Dialogs, confirmations
5. Notifications (1000): Toast messages, alerts
```
## Interaction Design
### Animation Principles
1. **Fast but Noticeable** - 150200ms for most transitions
2. **Easing**: ease-out for entrances, ease-in for exits
3. **Purpose-Driven** - Animate only to provide feedback or guide attention
4. **Consistent** - Same animation for same action across the app
### Micro-interactions
```
Button Click:
└─ 100ms scale down to 0.98, then return
Hover:
└─ 150ms background color transition
Focus:
└─ Immediate gold border + glow (no delay)
Panel Resize:
└─ Live (no animation) - functional over flashy
Tab Switch:
└─ 200ms fade (crossfade if content changes)
```
### Feedback Patterns
- **Click**: Immediate visual response (scale/color change)
- **Hover**: Subtle background change (150ms transition)
- **Success**: Green flash + success icon (500ms)
- **Error**: Red border pulse + shake animation (300ms)
- **Loading**: Gold spinner appears after 300ms delay (don't flash for quick ops)
## Accessibility
### Contrast Ratios
- **Body text (cream on aubergine-900)**: 15.2:1 (AAA)
- **Secondary text (warm-gray-100 on aubergine-900)**: 11.8:1 (AAA)
- **Gold accent on aubergine-900**: 7.1:1 (AA Large)
- **Buttons**: Ensure 4.5:1 minimum
### Keyboard Navigation
- **Tab order**: Logical, follows visual layout
- **Focus indicators**: Gold border + glow (always visible)
- **Shortcuts**: Display in tooltips, use standard conventions
- **Escape**: Always closes modals/dialogs
### Screen Readers
- Semantic HTML where possible
- ARIA labels for custom components
- Status announcements for validation/errors
- Descriptive button text (no "click here")
## Icon System
### Style
- **Outline style** (not filled) - more Rams than TE
- **Stroke width**: 1.5px
- **Sizes**: 16px, 24px (following grid)
- **Color**: Inherits text color unless accent needed
### Common Icons
```
Navigation:
├─ Folder (closed/open)
├─ File
├─ Search
├─ Settings
└─ Home
Actions:
├─ Plus (add)
├─ X (close/remove)
├─ Check (confirm)
├─ Edit (pencil)
└─ Save (disk or checkmark)
States:
├─ Success (checkmark circle)
├─ Error (X circle)
├─ Warning (! triangle)
└─ Info (i circle)
```
## Dark Mode (Primary Mode)
The aubergine palette is designed dark-first. Light mode is not planned for v1, but if needed:
```
Light Mode Palette (Future):
├─ Background: cream (#fdf8f3)
├─ Surface: warm-gray-100 (#f5f1ec)
├─ Text: aubergine-900
├─ Accent: Slightly deeper gold (#e89350)
└─ Borders: warm-gray-400
```
## Responsive Considerations
### Breakpoints
```
Compact: < 1024px (Single panel focus mode)
Standard: 10241440px (Two panels)
Wide: > 1440px (Three panels comfortable)
```
### Adaptive Layout
- **< 1024px**: File browser as overlay/drawer
- **10241440px**: Two-panel default (browser + editor)
- **> 1440px**: Three-panel comfortable
## Implementation Notes
### Iced Styling
```rust
// Example theme structure
pub struct StorybookTheme {
pub palette: Palette,
pub spacing: Spacing,
pub typography: Typography,
}
pub struct Palette {
pub aubergine_900: Color,
pub gold_500: Color,
// etc.
}
```
### File Organization
```
storybook-editor/src/
├─ theme/
│ ├─ mod.rs (Theme struct)
│ ├─ colors.rs (Color constants)
│ ├─ typography.rs (Font definitions)
│ └─ components.rs (Component styles)
├─ components/
│ ├─ button.rs
│ ├─ input.rs
│ └─ panel.rs
└─ ui/
└─ (Application views)
```
## References
- **Teenage Engineering**: Bold minimalism, grid precision, playful accents
- **Dieter Rams**: "Less but better", functionalism, longevity
- **Apple HIG**: Clarity, depth, deference
- **Linear**: Modern dev tool aesthetic
- **Figma**: Panel-based interface patterns
---
*Version 1.0 - Initial design system for Storybook Editor*

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

86
docs/README.md Normal file
View File

@@ -0,0 +1,86 @@
# Storybook Language Guide
> **Create rich narrative simulations through code that reads like stories.**
Welcome to the complete guide for the Storybook narrative simulation language! Whether you're a creative writer bringing characters to life or a developer building simulation systems, this documentation will help you master Storybook.
---
**⚠️ Alpha Software Notice**
Storybook is currently in **alpha** and under active development at [r3t Studios](https://r3t.io). While the core language features are stable and ready to use, you should expect:
- **New features** to be added as we expand the language capabilities
- **Minor syntax adjustments** as we refine the design based on real-world usage
- **API changes** in the compiled output format as we optimize for game engine integration
We're committed to a thoughtful path toward version 1.0. Breaking changes will be clearly documented, and we'll provide migration guides when syntax evolves. Your feedback during this alpha period is invaluable in shaping the language's future!
---
## What is Storybook?
Storybook is a **compiled simulation language** designed for **open-world, autonomous game simulations**. While it includes a basic embedded virtual machine for terminal-based debugging, it's built to be integrated into game engines and developed hand-in-hand with technical game developers.
Storybook defines characters, behaviors, relationships, and narrative events for autonomous agents in dynamic worlds. It bridges the gap between storytelling and technical simulation through:
- **Readable syntax** - Code that looks like natural descriptions, but compiles to efficient bytecode
- **Named nodes** - Behavior trees you can read as stories, that drive AI decision-making
- **Prose blocks** - Embed narrative directly in definitions for context-aware storytelling
- **Rich semantics** - From simple traits to complex state machines and schedules
- **Game engine integration** - Designed to power autonomous NPCs in Unity, Unreal, Godot, and custom engines
## Choose Your Path
### 🎨 For Storytellers
**New to programming?** Start with the [Tutorial Track](tutorial/01-welcome.md) for a gentle, example-driven introduction. Learn by building a bakery simulation!
### 💻 For Developers
**Need technical precision?** Jump to the [Reference Guide](reference/09-overview.md) for complete syntax specifications and semantic details.
### ✨ For Everyone
**Want inspiration?** Browse the [Examples Gallery](examples/24-baker-family-complete.md) to see what's possible!
## Quick Start
```storybook
character Martha {
age: 34
skill_level: 0.95
---description
A master baker who learned from her grandmother
and now runs the most popular bakery in town.
---
}
behavior Baker_MorningRoutine {
choose daily_priority {
then prepare_sourdough { ... }
then serve_customers { ... }
then restock_display { ... }
}
}
```
[Continue to Tutorial →](tutorial/01-welcome.md)
## Documentation Structure
- **Part I: Getting Started** - Tutorials for learning Storybook
- **Part II: Complete Reference** - Technical specifications
- **Part III: Advanced Topics** - Patterns and integration
- **Part IV: Examples Gallery** - Complete working examples
## Getting Help
- **In-Editor**: Hover over keywords for quick help
- **Search**: Use the search box (top right) to find anything
- **Examples**: Working code is the best teacher!
---
**Ready to begin?** [Start with the Tutorial →](tutorial/01-welcome.md)

168
docs/SBIR-CHANGELOG.md Normal file
View File

@@ -0,0 +1,168 @@
# SBIR Changelog
All notable changes to the Storybook Intermediate Representation format.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
---
## [0.2.0] - 2026-02-13
### Added
**Resource Linking System:**
- `BehaviorLink` struct for conditional behavior attachment
- Priority system (Low, Normal, High, Critical)
- Conditional activation via expressions
- Default fallback behavior
- `ScheduleLink` struct for conditional schedule attachment
- First-match conditional selection
- Default fallback schedule
- Extended `Character` with `behavior_links` and `schedule_links` vectors
- Extended `Institution` with `behavior_links` and `schedule_links` vectors
**Year-Long Schedule System:**
- `Schedule.parent_schedule_id` for inheritance
- `Schedule.patterns` for temporal patterns
- `SchedulePattern` enum with three variants:
- `DayPattern` - day-specific blocks
- `SeasonPattern` - seasonal blocks
- `RecurrencePattern` - recurring events
- `RecurrenceSpec` with variants:
- Every N days
- Weekly on specific days
- Monthly on day N
- Annually on month/day
- Pattern-based schedule composition and merging
- User-definable calendar enums (DayOfWeek, Season, Month)
**Behavior Tree Enhancements:**
- Optional `label` field on Selector and Sequence nodes (for debugging)
- Parameterized decorators:
- `DecoratorRepeatN(u32)` - repeat N times
- `DecoratorRepeatRange(u32, u32)` - repeat min..max times
- `DecoratorRetry(u32)` - retry up to N times
- `DecoratorTimeout(u64)` - timeout after milliseconds
- `DecoratorCooldown(u64)` - cooldown period
- `DecoratorIf(Expression)` - conditional if
- `DecoratorSucceedAlways` - always succeed
- `DecoratorFailAlways` - always fail
- Keyword syntax support (choose, then, if, when, repeat, etc.)
### Changed
**Breaking:**
- `ScheduleBlock.name` is now required (was `Option<String>`)
- `ScheduleBlock.start` and `end` changed from `Time` struct to `u16` (minutes since midnight)
- `ScheduleBlock.activity: String` removed, replaced with `behavior_ref: Option<Vec<String>>`
- Binary format version updated from 0x0001 to 0x0002
- Character and Institution sections have different binary layout (added link vectors)
- Schedule section completely redesigned
**Non-breaking:**
- Behavior tree Selector/Sequence nodes extended (backward compatible if label is None)
### Removed
- `ScheduleBlock.activity` field (replaced by `behavior_ref`)
### Deprecated
- None (first formal release)
### Fixed
- None (first formal release)
### Security
- None
---
## [0.1.0] - Implicit (Pre-Release)
Initial format (inferred from codebase, never formally specified).
### Included
- Basic entity storage:
- Characters with species and field maps
- Templates with includes
- Species definitions
- Enums
- Simple schedules:
- Time blocks with activity strings
- Optional block names
- Behavior trees:
- Symbolic syntax (?, >, !, @, ~)
- Basic decorators (repeat with * sigil)
- Action nodes with parameters
- Conditions and subtree references
- Relationships with participants
- Locations
- Life arcs (state machines)
- Institutions
### Known Limitations
- No resource linking (behaviors/schedules hard-coded per entity)
- No schedule patterns (single 24-hour template only)
- No parameterized decorators
- No named behavior tree nodes
- No formal specification document
---
## Migration Notes
### From 0.1.0 to 0.2.0
**Required Actions:**
1. Recompile source files OR
2. Use `storybook-migrate` tool for binary migration
**Key Changes:**
- All schedules must have named blocks
- Activity strings converted to behavior references
- Characters/Institutions gain empty link vectors (if not specified)
- Time values converted to minutes since midnight
**See:** `/docs/SBIR-MIGRATION-GUIDE.md` for detailed instructions.
---
## Version Numbering
SBIR follows semantic versioning:
- **Major** (x.0.0): Incompatible changes requiring migration
- **Minor** (0.x.0): Backward-compatible additions
- **Patch** (0.0.x): Bug fixes, no format changes
**Current:** v0.2.0 (first formal release)
**Next Minor:** v0.3.0 (planned features TBD)
**Next Major:** v1.0.0 (when format stabilizes)
---
## Unreleased
_Track upcoming changes here_
### Planned for v0.3.0
- [ ] Compressed string table option
- [ ] Symbol index for faster lookups
- [ ] Metadata section for tooling info
### Under Consideration
- [ ] Module system for multi-file worlds
- [ ] Asset references (audio, images)
- [ ] Localization support
---
[0.2.0]: https://github.com/your-org/storybook/releases/tag/sbir-v0.2.0
[0.1.0]: (implicit, no release)

1037
docs/SBIR-v0.2.0-SPEC.md Normal file

File diff suppressed because it is too large Load Diff

53
docs/SUMMARY.md Normal file
View File

@@ -0,0 +1,53 @@
# Summary
[Introduction](README.md)
---
# Part I: Getting Started
- [Welcome to Storybook](tutorial/01-welcome.md)
- [Creating Characters](tutorial/02-creating-characters.md)
- [Your First Behavior Tree](tutorial/03-first-behavior-tree.md)
- [Making Characters Act](tutorial/04-making-characters-act.md)
- [Advanced Behaviors](tutorial/05-advanced-behaviors.md)
- [Character Relationships](tutorial/06-relationships.md)
- [Schedules and Time](tutorial/07-schedules.md)
- [Life Arcs](tutorial/08-life-arcs.md)
- [Locations and Institutions](tutorial/09-locations-institutions.md)
---
# Part II: Complete Reference
- [Language Overview](reference/09-overview.md)
- [Characters](reference/10-characters.md)
- [Behavior Trees](reference/11-behavior-trees.md)
- [Decorators](reference/12-decorators.md)
- [Life Arcs](reference/13-life-arcs.md)
- [Schedules](reference/14-schedules.md)
- [Relationships](reference/15-relationships.md)
- [Locations](reference/16a-locations.md)
- [Institutions](reference/16b-institutions.md)
- [Other Declarations](reference/16-other-declarations.md)
- [Expression Language](reference/17-expressions.md)
- [Value Types](reference/18-value-types.md)
- [Validation Rules](reference/19-validation.md)
---
# Part III: Advanced Topics
- [Design Patterns](advanced/20-patterns.md)
- [The SBIR Binary Format](advanced/21-sbir-format.md)
- [Integration Guide](advanced/22-integration.md)
- [Best Practices](advanced/23-best-practices.md)
---
# Part IV: Examples Gallery
- [Baker Family Complete](examples/24-baker-family-complete.md)
- [Day in the Life](examples/25-day-in-life.md)
- [Character Evolution](examples/26-character-evolution.md)
- [Multi-Character Interactions](examples/27-multi-character.md)

View File

@@ -0,0 +1,65 @@
# LSP Documentation Template for Keywords
# Copy this template for each keyword/declaration/type
[keyword]
name = "keyword_name"
category = "behavior_tree | declaration | value_type | decorator"
short_description = "One-sentence description (used in completion list)"
web_url = "https://r3t-studios.github.io/storybook/reference/page.html#anchor"
[syntax]
format = "keyword [optional] { required }"
example = """
keyword example {
child_element
}
"""
[hover]
markdown = """
**`keyword` - Short Name**
One-sentence description of purpose and behavior.
**Key behaviors:**
- Behavior 1
- Behavior 2
- Behavior 3
**Example:**
```storybook
keyword realistic_label {
meaningful_child
}
```
**Optional features:**
- Optional labels for debugging
- Optional parameters
[Learn more →](https://r3t-studios.github.io/storybook/reference/page.html#anchor)
"""
[completion]
snippet = "keyword ${1:label} {\n\t$0\n}"
description = "Short description for completion dropdown"
sort_text = "01-keyword" # Optional: control sort order
filter_text = "keyword" # Optional: control filtering
[signature]
# Optional: for keywords that take parameters
label = "keyword(param1: Type, param2: Type)"
parameters = [
"param1: Type - Description",
"param2: Type - Description"
]
[context]
# Optional: where this keyword is valid
valid_in = ["behavior_block", "life_arc_block"]
invalid_in = ["character_block"]
[related]
# Optional: related keywords for cross-reference
keywords = ["related1", "related2"]
concepts = ["concept1", "concept2"]

81
docs/_templates/reference-template.md vendored Normal file
View File

@@ -0,0 +1,81 @@
# [Concept Name]
> **Quick Summary**: [One-sentence description of what this is and what it does.]
## Syntax
```bnf
[Formal grammar or syntax pattern]
```
### Parameters
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `param1` | Type | Yes | What it does |
| `param2` | Type | No | What it does (default: value) |
## Semantics
[Precise description of behavior. What happens when this executes? What are the rules? What are the constraints?]
### Evaluation Order
[If relevant - describe how/when things are evaluated]
### Success and Failure
[If relevant - describe success/failure conditions]
## Minimal Example
```storybook
[Simplest possible working example]
```
## Complete Example
```storybook
[Realistic example showing all features]
```
## Validation Rules
- [Rule 1 with explanation]
- [Rule 2 with explanation]
- [Rule 3 with explanation]
## Common Errors
### Error: [Error Message]
**Cause**: [Why this happens]
**Solution**: [How to fix it]
```storybook
// ❌ Wrong
[bad example]
// ✅ Correct
[good example]
```
## Related Concepts
- [Related Concept 1](link) - [How they relate]
- [Related Concept 2](link) - [How they relate]
## See Also
- [Tutorial: Learning Chapter](../tutorial/chapter.md)
- [Examples: Use Case](../examples/example.md)
- [Reference: Related Feature](other-reference.md)
## Notes
[Edge cases, gotchas, performance considerations, design rationale]
---
**[Anchor ID for LSP linking]**

62
docs/_templates/tutorial-template.md vendored Normal file
View File

@@ -0,0 +1,62 @@
# [Chapter Title]
> **Learning Objective**: By the end of this chapter, you'll be able to [specific skill].
## Why This Matters
[Motivation - explain why this concept is important for storytelling. Connect to narrative creation, not just technical details. Make it inspiring!]
## The Concept
[Gentle introduction with metaphor or analogy. Explain in plain language before showing code. Build intuition.]
## Example: [Descriptive Name]
Let's see this in action with [character/scenario]:
```storybook
[Complete, working example with meaningful names]
```
### Walking Through the Example
[Step-by-step explanation of the code above. Call attention to important details. Explain each part's purpose.]
### Why It Works
[Explain the semantics - what happens when this code runs? Connect code structure to narrative outcome.]
## Try It Yourself
[Guided exercise with clear instructions. Suggest modifications. Encourage experimentation.]
```storybook
[Exercise template or starting point]
```
## Common Patterns
[Show 2-3 variations of this concept. Highlight when to use each.]
## Tips and Tricks
- **[Tip 1]**: [Practical advice]
- **[Tip 2]**: [Helpful hint]
- **[Tip 3]**: [Common pitfall to avoid]
## Key Takeaways
- [Main concept 1]
- [Main concept 2]
- [Main concept 3]
- [How this enables storytelling]
## What's Next
[Bridge to next chapter. Create excitement. Show how concepts build.]
→ [Next: Chapter Name](next-chapter.md)
---
**Quick Reference**: [Link to relevant reference section]

View File

@@ -0,0 +1,429 @@
# Design Patterns
This chapter presents proven patterns for structuring Storybook projects. These patterns have emerged from building complex narrative simulations and represent best practices for maintainability, reuse, and clarity.
## Behavior Tree Patterns
### Priority Fallback Chain
Use a selector to try increasingly desperate options:
```storybook
behavior Survival {
choose survival_priority {
then optimal {
if(health > 70 and has_supplies)
ProceedNormally
}
then cautious {
if(health > 30)
ProceedCarefully
}
then desperate {
if(health > 10)
SeekHelp
}
LastResortPanic
}
}
```
The tree naturally degrades: first tries the best option, then falls back through progressively worse alternatives.
### Conditional Behavior Switching
Use guards at the top level to switch between behavioral modes:
```storybook
behavior ModeSwitcher {
choose mode {
if(is_combat_mode) {
include CombatBehavior
}
if(is_exploration_mode) {
include ExplorationBehavior
}
if(is_social_mode) {
include SocialBehavior
}
include IdleBehavior
}
}
```
### Composite Subtree Pattern
Break complex behaviors into focused, reusable subtrees:
```storybook
// Atomic subtrees
behavior Navigate { then nav { PlanPath, FollowPath } }
behavior Interact { then talk { Approach, Greet, Converse } }
behavior Trade { then exchange { ShowGoods, Negotiate, Exchange } }
// Composed behavior
behavior Merchant_AI {
choose activity {
then serve_customer {
if(customer_present)
include Interact
include Trade
}
then travel_to_market {
if(is_market_day)
include Navigate
}
Idle
}
}
```
### Repeating Patrol with Interrupts
Use a repeating patrol that can be interrupted by higher-priority events:
```storybook
character Guard {
uses behaviors: [
{
tree: GuardCombat
when: threat_detected
priority: high
},
{
tree: GuardPatrol
priority: normal
}
]
}
behavior GuardPatrol {
repeat {
then patrol_loop {
MoveTo(destination: "Waypoint1")
WaitAndScan(duration: 5s)
MoveTo(destination: "Waypoint2")
WaitAndScan(duration: 5s)
}
}
}
```
The combat behavior preempts patrol when threats appear, then patrol resumes.
## Character Architecture Patterns
### Species + Templates Composition
Use species for identity and templates for capabilities:
```storybook
// Species: What they ARE
species Human { lifespan: 70 }
species Elf { lifespan: 1000 }
// Templates: What they HAVE
template Warrior { strength: 10..20, weapon_skill: 0.5..1.0 }
template Scholar { intelligence: 15..20, books_read: 50..500 }
template Leader { charisma: 12..18, followers: 5..50 }
// Characters: Combine both
character Aragorn: Human from Warrior, Leader {
strength: 18
charisma: 17
}
character Elrond: Elf from Scholar, Leader {
intelligence: 20
charisma: 18
}
```
### Strict Templates for Schema Enforcement
Use strict templates when you need controlled, uniform entities:
```storybook
template RecipeCard strict {
recipe_name: string
difficulty: Difficulty
prep_time_minutes: 10..180
}
// This works:
character SourdoughRecipe from RecipeCard {
recipe_name: "Classic Sourdough"
difficulty: intermediate
prep_time_minutes: 120
}
// This would error (extra field not allowed):
// character BadRecipe from RecipeCard {
// recipe_name: "Mystery Bread"
// difficulty: easy
// favorite_color: "blue" // Error!
// }
```
### Template Inheritance Chains
Build template hierarchies for progressive specialization:
```storybook
template Worker {
skill_level: 0.0..1.0
wage: 10..50
}
template SkilledWorker {
include Worker
specialization: "general"
tool_proficiency: 0.5..1.0
}
template MasterCraftsman {
include SkilledWorker
can_teach: true
reputation: 0.7..1.0
}
```
## Relationship Patterns
### Bidirectional Perspective
Model relationships where each side sees things differently:
```storybook
relationship MentorApprentice {
Master as mentor self {
patience: 0.7
investment_in_student: 0.9
} other {
sees_potential: 0.8
frustration_level: 0.3
}
Student as apprentice self {
dedication: 0.8
overwhelmed: 0.4
} other {
respect: 0.95
desire_to_impress: 0.9
}
bond: 0.75
years_together: 3
}
```
### Power Dynamic Pattern
Model unequal power relationships explicitly:
```storybook
relationship Vassalage {
King as lord self {
authority: 1.0
grants: "protection"
} other {
trusts_vassal: 0.6
}
Knight as vassal self {
loyalty: 0.9
ambition: 0.4
} other {
respects_lord: 0.8
fears_lord: 0.3
}
bond: 0.7
}
```
### Relationship Network
Build social graphs with multiple overlapping relationships:
```storybook
// Family
relationship BakerMarriage { Martha as spouse, David as spouse, bond: 0.9 }
relationship BakerParenting { Martha as parent, Tommy as child, bond: 0.95 }
// Professional
relationship BakerEmployment { Martha as employer, Elena as employee, bond: 0.8 }
relationship GuildMembership { Martha as member, BakersGuild as org }
// Social
relationship BakerFriendship { Martha, Neighbor, bond: 0.6 }
```
## Schedule Patterns
### Base Schedule with Specializations
```storybook
schedule BaseWorker {
block work { 09:00 - 17:00, action: work::standard }
block lunch { 12:00 - 13:00, action: social::lunch }
}
schedule EarlyBird extends BaseWorker {
block work { 05:00 - 13:00, action: work::early_shift }
block lunch { 11:00 - 12:00, action: social::lunch }
}
schedule NightOwl extends BaseWorker {
block work { 14:00 - 22:00, action: work::late_shift }
block lunch { 18:00 - 19:00, action: social::dinner }
}
```
### Seasonal Variation
```storybook
schedule FarmSchedule {
block spring_work {
06:00 - 18:00
action: farming::plant
on season spring
}
block summer_work {
05:00 - 20:00
action: farming::tend
on season summer
}
block fall_work {
06:00 - 20:00
action: farming::harvest
on season fall
}
block winter_work {
08:00 - 16:00
action: farming::maintain
on season winter
}
}
```
## Life Arc Patterns
### Progressive Development
```storybook
life_arc CareerProgression {
state novice {
on enter { Character.title: "Apprentice" }
on experience > 100 -> intermediate
}
state intermediate {
on enter { Character.title: "Journeyman" }
on experience > 500 -> expert
}
state expert {
on enter { Character.title: "Master", Character.can_teach: true }
}
}
```
### Emotional State Machine
```storybook
life_arc MoodSystem {
state neutral {
on provoked -> angry
on complimented -> happy
on tired -> sleepy
}
state angry {
on enter { Character.aggression: 0.9 }
on calmed_down -> neutral
on escalated -> furious
}
state furious {
on enter { Character.aggression: 1.0 }
on timeout_elapsed -> angry
}
state happy {
on enter { Character.gives_discounts: true }
on insulted -> neutral
}
state sleepy {
on enter { Character.responsiveness: 0.2 }
on woke_up -> neutral
}
}
```
## Project Organization Patterns
### Schema / World Separation
Keep type definitions separate from instance data:
```
my-project/
schema/ # Types and templates (reusable)
core_enums.sb
templates.sb
beings.sb
world/ # Instances (specific to this story)
characters/
behaviors/
relationships/
locations/
```
### Module per Domain
Group related declarations together:
```
world/
characters/
heroes.sb # All hero characters
villains.sb # All villain characters
npcs.sb # Background characters
behaviors/
combat.sb # Combat behaviors
social.sb # Social behaviors
exploration.sb # Exploration behaviors
```
## Anti-Patterns to Avoid
**Deep nesting**: More than 4-5 levels of behavior tree nesting is hard to read. Use `include` to flatten.
**God behaviors**: One massive behavior tree doing everything. Break it into focused subtrees.
**Deep species hierarchies**: More than 2-3 levels of species `includes` is rarely needed. Use templates for variation.
**Duplicated logic**: If two behaviors share logic, extract it into a shared subtree.
**Unnamed nodes**: Always label composite nodes in behavior trees for readability.
## Cross-References
- [Behavior Trees](../reference/11-behavior-trees.md) - Complete behavior syntax
- [Characters](../reference/10-characters.md) - Character architecture
- [Relationships](../reference/15-relationships.md) - Relationship modeling
- [Schedules](../reference/14-schedules.md) - Schedule composition
- [Life Arcs](../reference/13-life-arcs.md) - State machine patterns

View File

@@ -0,0 +1,82 @@
# The SBIR Binary Format
SBIR (Storybook Intermediate Representation) is the compiled binary format produced by the Storybook compiler. It transforms human-readable `.sb` files into an optimized, machine-consumable format for simulation runtimes.
## Compilation Pipeline
```
.sb files → Lexer → Parser → AST → Resolver → SBIR Binary
```
1. **Lexer**: Tokenizes raw text into tokens
2. **Parser**: Builds an Abstract Syntax Tree (AST) from tokens
3. **Resolver**: Validates, resolves cross-references, merges templates, and produces SBIR
## What SBIR Contains
SBIR represents the fully resolved state of a Storybook project:
- **Characters**: All fields resolved (species + templates merged, overrides applied)
- **Behaviors**: Behavior trees with all subtree references inlined
- **Life Arcs**: State machines with validated transitions
- **Schedules**: Time blocks with resolved action references
- **Relationships**: Participants with resolved entity references
- **Institutions**: Fully resolved field sets
- **Locations**: Fully resolved field sets
- **Species**: Fully resolved inheritance chains
- **Enums**: Complete variant lists
## Resolution Process
### Template Merging
When a character uses templates, SBIR contains the fully merged result:
**Source:**
```storybook
species Human { lifespan: 70, speed: 1.0 }
template Warrior { speed: 1.5, strength: 10 }
character Conan: Human from Warrior {
strength: 20
}
```
**In SBIR, Conan's fields are:**
- `lifespan: 70` (from Human)
- `speed: 1.5` (Warrior overrides Human)
- `strength: 20` (Conan overrides Warrior)
### Cross-File Reference Resolution
SBIR resolves all `use` statements and qualified paths. A relationship referencing `Martha` in a different file is resolved to the concrete character definition.
### Validation
Before producing SBIR, the resolver validates all constraints documented in [Validation Rules](../reference/19-validation.md):
- All references resolve to defined declarations
- No circular dependencies
- Type consistency
- Domain constraints (bond ranges, schedule validity)
## Design Goals
**Compact**: SBIR strips comments, whitespace, and redundant structure.
**Self-contained**: No external references -- everything is resolved and inlined.
**Fast to load**: Simulation runtimes can load SBIR without re-parsing or re-resolving.
**Validated**: If SBIR was produced, the source was valid. Runtimes do not need to re-validate.
## Usage
SBIR is consumed by simulation runtimes that drive character behavior, schedule execution, life arc transitions, and relationship queries. The specific binary format is implementation-defined and may evolve between versions.
For the current SBIR specification, see the [SBIR v0.2.0 Spec](../SBIR-v0.2.0-SPEC.md).
## Cross-References
- [Language Overview](../reference/09-overview.md) - Compilation model
- [Validation Rules](../reference/19-validation.md) - What is validated before SBIR production
- [Integration Guide](./22-integration.md) - How runtimes consume SBIR

View File

@@ -0,0 +1,160 @@
# Integration Guide
This chapter covers how to integrate Storybook into larger systems -- game engines, simulation frameworks, and custom applications.
## Architecture Overview
Storybook operates in two phases:
1. **Compile time**: `.sb` files are parsed, validated, and compiled into SBIR
2. **Runtime**: A simulation engine consumes SBIR and drives character behavior
```
Compile Time Runtime
.sb files → [Storybook Compiler] → SBIR → [Simulation Engine] → Character Actions
```
## The Storybook Compiler
The compiler is a Rust library and CLI tool that processes `.sb` files.
### CLI Usage
```bash
# Compile a directory of .sb files
storybook compile path/to/project/
# Compile with output path
storybook compile path/to/project/ -o output.sbir
# Validate without producing output
storybook check path/to/project/
```
### As a Library
The compiler can be embedded as a Rust dependency:
```rust
use storybook::syntax::parse_file;
use storybook::resolve::resolve_files;
// Parse .sb files into ASTs
let ast = parse_file(source_code)?;
// Resolve across multiple files
let resolved = resolve_files(vec![ast1, ast2, ast3])?;
// Access resolved data
for character in resolved.characters() {
println!("{}: {:?}", character.name, character.fields);
}
```
### Key Types
The resolved output provides these primary types:
| Type | Description |
|------|-------------|
| `ResolvedFile` | Container for all resolved declarations |
| `ResolvedCharacter` | Character with merged species/template fields |
| `ResolvedBehavior` | Behavior tree with resolved subtree references |
| `ResolvedLifeArc` | State machine with validated transitions |
| `ResolvedSchedule` | Schedule with resolved time blocks |
| `ResolvedRelationship` | Relationship with resolved participant references |
| `ResolvedInstitution` | Institution with resolved fields |
| `ResolvedLocation` | Location with resolved fields |
| `ResolvedSpecies` | Species with resolved includes chain |
| `ResolvedEnum` | Enum with variant list |
## Runtime Integration
### Behavior Tree Execution
Runtimes are responsible for:
1. **Tick-based evaluation**: Call the behavior tree root each frame/tick
2. **Action execution**: Interpret action nodes (e.g., `MoveTo`, `Attack`)
3. **Condition evaluation**: Evaluate expression nodes against current state
4. **Decorator state**: Maintain timer/counter state for stateful decorators
### Life Arc Execution
1. Track the current state for each life arc instance
2. Evaluate transition conditions each tick
3. Execute on-enter actions when transitioning
4. Maintain state persistence across ticks
### Schedule Execution
1. Get the current simulation time
2. Find the matching schedule block (considering temporal constraints and recurrences)
3. Execute the associated behavior tree action
### Relationship Queries
Provide APIs for querying the relationship graph:
- Find all relationships for a character
- Get bond strength between two entities
- Query perspective fields (self/other)
## LSP Integration
Storybook includes a Language Server Protocol (LSP) implementation for editor support:
- **Hover information**: Documentation for keywords and declarations
- **Go to definition**: Navigate to declaration sources
- **Diagnostics**: Real-time error reporting
- **Completions**: Context-aware suggestions
The LSP server reuses the compiler's parser and resolver, providing the same validation as the CLI.
### Editor Setup
The Storybook LSP works with any editor that supports LSP:
- **VS Code**: Via the Storybook extension
- **Zed**: Via the zed-storybook extension
- **Other editors**: Any LSP-compatible editor
## Tree-sitter Grammar
A Tree-sitter grammar is provided for syntax highlighting and structural queries:
```
tree-sitter-storybook/
grammar.js # Grammar definition
src/ # Generated parser
```
The Tree-sitter grammar supports:
- Syntax highlighting in editors
- Structural code queries
- Incremental parsing for large files
## File Organization for Integration
Organize your project to support both authoring and runtime consumption:
```
my-game/
storybook/ # Storybook source files
schema/
core_enums.sb
templates.sb
beings.sb
world/
characters/
behaviors/
relationships/
assets/
narrative.sbir # Compiled output for runtime
src/
narrative_engine.rs # Runtime that consumes SBIR
```
## Cross-References
- [SBIR Format](./21-sbir-format.md) - The compiled binary format
- [Validation Rules](../reference/19-validation.md) - What the compiler checks
- [Language Overview](../reference/09-overview.md) - Compilation pipeline

View File

@@ -0,0 +1,344 @@
# Best Practices
This chapter compiles best practices for writing clear, maintainable, and effective Storybook code. These guidelines apply across all declaration types and project sizes.
## Naming Conventions
### Declarations
Use **PascalCase** for all declaration names:
```storybook
character MasterBaker { } // Good
species DomesticCat { } // Good
behavior GuardPatrol { } // Good
character master_baker { } // Avoid
behavior guard-patrol { } // Invalid (no hyphens)
```
### Fields
Use **snake_case** for field names:
```storybook
character Martha {
skill_level: 0.95 // Good
emotional_state: focused // Good
years_experience: 22 // Good
skillLevel: 0.95 // Avoid (camelCase)
}
```
### Behavior Tree Labels
Use **snake_case** for node labels, with descriptive names:
```storybook
choose survival_instinct { // Good
then fight_response { } // Good
then flight_response { } // Good
}
choose s1 { // Avoid (meaningless)
then a { } // Avoid
}
```
### Enum Variants
Use **PascalCase** or **snake_case** consistently within an enum:
```storybook
// PascalCase (good for short names)
enum Size { Tiny, Small, Normal, Large, Huge }
// snake_case (good for compound names)
enum GovernmentStyle {
absolute_tyranny,
constitutional_monarchy,
direct_democracy
}
```
## File Organization
### Separate Schema from World
Keep reusable type definitions separate from instance data:
```
project/
schema/ # Reusable across stories
core_enums.sb # Enum definitions
templates.sb # Template definitions
beings.sb # Species definitions
world/ # Specific to this story
characters/ # Character instances
behaviors/ # Behavior trees
relationships/ # Relationship instances
locations/ # Location instances
```
### One Concern per File
Group related declarations, but avoid putting unrelated things together:
```storybook
// characters/bakery_staff.sb - Good: related characters together
character Martha { }
character Jane { }
character Elena { }
// everything.sb - Avoid: everything in one file
character Martha { }
behavior BakeRoutine { }
schedule DailyRoutine { }
relationship Partnership { }
```
### Explicit Imports
Prefer explicit imports over wildcards:
```storybook
// Good: clear what is being used
use schema::core_enums::{SkillLevel, Specialty};
use schema::beings::Human;
// Avoid: unclear dependencies
use schema::core_enums::*;
use schema::beings::*;
```
## Character Design
### Use Species for Identity, Templates for Traits
```storybook
// Species: ontological identity
species Human { lifespan: 70 }
// Templates: compositional traits
template Warrior { strength: 10..20 }
template Scholar { intelligence: 15..20 }
// Character: combines identity and traits
character Aragorn: Human from Warrior {
strength: 18
}
```
### Document with Prose Blocks
```storybook
character Martha: Human {
age: 34
---backstory
Martha learned to bake from her grandmother, starting at
age twelve. She now runs the most popular bakery in town.
---
---personality
Meticulous and patient, with an unwavering commitment to
quality. Tough but fair with her staff.
---
}
```
### Prefer Flat Inheritance
Avoid deep species hierarchies. Two or three levels is usually enough:
```storybook
// Good: shallow
species Mammal { warm_blooded: true }
species Human includes Mammal { sapient: true }
// Avoid: too deep
species Being { }
species LivingBeing includes Being { }
species Animal includes LivingBeing { }
species Vertebrate includes Animal { }
species Mammal includes Vertebrate { }
species Human includes Mammal { }
```
## Behavior Tree Design
### Name Every Composite Node
```storybook
// Good: self-documenting
choose daily_priority {
then handle_emergency { }
then do_work { }
then relax { }
}
// Avoid: anonymous nodes
choose {
then { }
then { }
}
```
### Keep Trees Shallow
Extract deep subtrees into separate behaviors:
```storybook
// Good: flat with includes
behavior Main {
choose mode {
include CombatBehavior
include ExplorationBehavior
include SocialBehavior
}
}
// Avoid: deeply nested
behavior Main {
choose {
then { choose { then { choose { then { Action } } } } }
}
}
```
### Use Decorators for Control Flow
```storybook
// Good: decorator handles timing
cooldown(30s) { CastSpell }
timeout(10s) { SolvePuzzle }
retry(3) { PickLock }
// Avoid: manual timing in actions
CheckCooldownTimer
IfCooldownReady { CastSpell }
```
## Expression Writing
### Use Parentheses for Clarity
```storybook
// Good: explicit grouping
on (health < 50 or is_poisoned) and has_antidote -> healing
// Risky: relies on precedence knowledge
on health < 50 or is_poisoned and has_antidote -> healing
```
### Break Complex Conditions into Multiple Transitions
```storybook
// Good: separate transitions, easy to read
state combat {
on health < 20 and not has_potion -> desperate
on surrounded and not has_escape -> desperate
on enemy_count > 10 -> desperate
}
// Avoid: one massive condition
state combat {
on (health < 20 and not has_potion) or (surrounded and not has_escape) or enemy_count > 10 -> desperate
}
```
### Use `is` for Enum Comparisons
```storybook
// Good: reads naturally
on status is active -> active_state
on skill_level is master -> teach_others
// Works but less readable
on status == active -> active_state
```
## Schedule Design
### Use Named Blocks for Override Support
```storybook
// Good: named blocks can be overridden
schedule Base {
block work { 09:00 - 17:00, action: standard_work }
}
schedule Variant extends Base {
block work { 05:00 - 13:00, action: early_work }
}
```
### Group Related Blocks
```storybook
schedule DailyRoutine {
// Morning
block wake { 06:00 - 07:00, action: morning_routine }
block breakfast { 07:00 - 08:00, action: eat }
// Work
block commute { 08:00 - 09:00, action: travel }
block work { 09:00 - 17:00, action: work }
// Evening
block leisure { 18:00 - 22:00, action: relax }
block sleep { 22:00 - 06:00, action: sleep }
}
```
## Relationship Design
### Use Roles for Clarity
```storybook
// Good: roles clarify the relationship
relationship Marriage {
Martha as spouse
David as spouse
bond: 0.9
}
// Less clear without roles
relationship Marriage {
Martha
David
bond: 0.9
}
```
### Use Perspectives for Asymmetry
```storybook
// Good: captures different viewpoints
relationship TeacherStudent {
Gandalf as teacher self { patience: 0.8 } other { potential: 0.9 }
Frodo as student self { motivation: 0.7 } other { admiration: 0.95 }
bond: 0.85
}
```
## General Principles
1. **Readability over brevity**: Storybook code should read like a narrative, not a puzzle.
2. **Explicit over implicit**: Say what you mean. Use named nodes, explicit imports, and clear field names.
3. **Flat over deep**: Shallow hierarchies, short behavior trees, and focused files are easier to maintain.
4. **Composition over inheritance**: Prefer combining templates over building deep species hierarchies.
5. **Document with prose**: Prose blocks are a feature, not clutter. Use them to explain intent alongside data.
6. **One concept per declaration**: Each behavior tree, life arc, or schedule should have a single clear purpose.
## Cross-References
- [Design Patterns](./20-patterns.md) - Common structural patterns
- [Validation Rules](../reference/19-validation.md) - What the compiler checks
- [Language Overview](../reference/09-overview.md) - Language philosophy

41
docs/book.toml Normal file
View File

@@ -0,0 +1,41 @@
[book]
title = "Storybook Language Guide"
authors = ["R3T Studios"]
description = "Comprehensive documentation for the Storybook narrative simulation language"
language = "en"
multilingual = false
src = "."
[build]
build-dir = "book"
create-missing = false
[output.html]
default-theme = "light"
preferred-dark-theme = "navy"
smart-punctuation = true
git-repository-url = "https://github.com/r3t-studios/storybook"
git-repository-icon = "fa-github"
site-url = "/storybook/"
[output.html.playground]
editable = false
line-numbers = true
[output.html.search]
enable = true
limit-results = 30
use-boolean-and = true
boost-title = 2
boost-hierarchy = 1
boost-paragraph = 1
expand = true
heading-split-level = 3
[output.html.fold]
enable = true
level = 1
# Custom theme and styling (to be added later)
# [output.html.additional-css]
# [output.html.additional-js]

1
docs/book/.nojekyll Normal file
View File

@@ -0,0 +1 @@
This file makes sure that Github Pages doesn't process mdBook's output.

216
docs/book/404.html Normal file
View File

@@ -0,0 +1,216 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Page not found - Storybook Language Guide</title>
<base href="/">
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="document-not-found-404"><a class="header" href="#document-not-found-404">Document not found (404)</a></h1>
<p>This URL is invalid, sorry. Please use the navigation bar or search to continue.</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

File diff suppressed because one or more lines are too long

Binary file not shown.

Binary file not shown.

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 434 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,65 @@
# LSP Documentation Template for Keywords
# Copy this template for each keyword/declaration/type
[keyword]
name = "keyword_name"
category = "behavior_tree | declaration | value_type | decorator"
short_description = "One-sentence description (used in completion list)"
web_url = "https://r3t-studios.github.io/storybook/reference/page.html#anchor"
[syntax]
format = "keyword [optional] { required }"
example = """
keyword example {
child_element
}
"""
[hover]
markdown = """
**`keyword` - Short Name**
One-sentence description of purpose and behavior.
**Key behaviors:**
- Behavior 1
- Behavior 2
- Behavior 3
**Example:**
```storybook
keyword realistic_label {
meaningful_child
}
```
**Optional features:**
- Optional labels for debugging
- Optional parameters
[Learn more →](https://r3t-studios.github.io/storybook/reference/page.html#anchor)
"""
[completion]
snippet = "keyword ${1:label} {\n\t$0\n}"
description = "Short description for completion dropdown"
sort_text = "01-keyword" # Optional: control sort order
filter_text = "keyword" # Optional: control filtering
[signature]
# Optional: for keywords that take parameters
label = "keyword(param1: Type, param2: Type)"
parameters = [
"param1: Type - Description",
"param2: Type - Description"
]
[context]
# Optional: where this keyword is valid
valid_in = ["behavior_block", "life_arc_block"]
invalid_in = ["character_block"]
[related]
# Optional: related keywords for cross-reference
keywords = ["related1", "related2"]
concepts = ["concept1", "concept2"]

View File

@@ -0,0 +1,579 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Design Patterns - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="design-patterns"><a class="header" href="#design-patterns">Design Patterns</a></h1>
<p>This chapter presents proven patterns for structuring Storybook projects. These patterns have emerged from building complex narrative simulations and represent best practices for maintainability, reuse, and clarity.</p>
<h2 id="behavior-tree-patterns"><a class="header" href="#behavior-tree-patterns">Behavior Tree Patterns</a></h2>
<h3 id="priority-fallback-chain"><a class="header" href="#priority-fallback-chain">Priority Fallback Chain</a></h3>
<p>Use a selector to try increasingly desperate options:</p>
<pre><code class="language-storybook">behavior Survival {
choose survival_priority {
then optimal {
if(health &gt; 70 and has_supplies)
ProceedNormally
}
then cautious {
if(health &gt; 30)
ProceedCarefully
}
then desperate {
if(health &gt; 10)
SeekHelp
}
LastResortPanic
}
}
</code></pre>
<p>The tree naturally degrades: first tries the best option, then falls back through progressively worse alternatives.</p>
<h3 id="conditional-behavior-switching"><a class="header" href="#conditional-behavior-switching">Conditional Behavior Switching</a></h3>
<p>Use guards at the top level to switch between behavioral modes:</p>
<pre><code class="language-storybook">behavior ModeSwitcher {
choose mode {
if(is_combat_mode) {
include CombatBehavior
}
if(is_exploration_mode) {
include ExplorationBehavior
}
if(is_social_mode) {
include SocialBehavior
}
include IdleBehavior
}
}
</code></pre>
<h3 id="composite-subtree-pattern"><a class="header" href="#composite-subtree-pattern">Composite Subtree Pattern</a></h3>
<p>Break complex behaviors into focused, reusable subtrees:</p>
<pre><code class="language-storybook">// Atomic subtrees
behavior Navigate { then nav { PlanPath, FollowPath } }
behavior Interact { then talk { Approach, Greet, Converse } }
behavior Trade { then exchange { ShowGoods, Negotiate, Exchange } }
// Composed behavior
behavior Merchant_AI {
choose activity {
then serve_customer {
if(customer_present)
include Interact
include Trade
}
then travel_to_market {
if(is_market_day)
include Navigate
}
Idle
}
}
</code></pre>
<h3 id="repeating-patrol-with-interrupts"><a class="header" href="#repeating-patrol-with-interrupts">Repeating Patrol with Interrupts</a></h3>
<p>Use a repeating patrol that can be interrupted by higher-priority events:</p>
<pre><code class="language-storybook">character Guard {
uses behaviors: [
{
tree: GuardCombat
when: threat_detected
priority: high
},
{
tree: GuardPatrol
priority: normal
}
]
}
behavior GuardPatrol {
repeat {
then patrol_loop {
MoveTo(destination: "Waypoint1")
WaitAndScan(duration: 5s)
MoveTo(destination: "Waypoint2")
WaitAndScan(duration: 5s)
}
}
}
</code></pre>
<p>The combat behavior preempts patrol when threats appear, then patrol resumes.</p>
<h2 id="character-architecture-patterns"><a class="header" href="#character-architecture-patterns">Character Architecture Patterns</a></h2>
<h3 id="species--templates-composition"><a class="header" href="#species--templates-composition">Species + Templates Composition</a></h3>
<p>Use species for identity and templates for capabilities:</p>
<pre><code class="language-storybook">// Species: What they ARE
species Human { lifespan: 70 }
species Elf { lifespan: 1000 }
// Templates: What they HAVE
template Warrior { strength: 10..20, weapon_skill: 0.5..1.0 }
template Scholar { intelligence: 15..20, books_read: 50..500 }
template Leader { charisma: 12..18, followers: 5..50 }
// Characters: Combine both
character Aragorn: Human from Warrior, Leader {
strength: 18
charisma: 17
}
character Elrond: Elf from Scholar, Leader {
intelligence: 20
charisma: 18
}
</code></pre>
<h3 id="strict-templates-for-schema-enforcement"><a class="header" href="#strict-templates-for-schema-enforcement">Strict Templates for Schema Enforcement</a></h3>
<p>Use strict templates when you need controlled, uniform entities:</p>
<pre><code class="language-storybook">template RecipeCard strict {
recipe_name: string
difficulty: Difficulty
prep_time_minutes: 10..180
}
// This works:
character SourdoughRecipe from RecipeCard {
recipe_name: "Classic Sourdough"
difficulty: intermediate
prep_time_minutes: 120
}
// This would error (extra field not allowed):
// character BadRecipe from RecipeCard {
// recipe_name: "Mystery Bread"
// difficulty: easy
// favorite_color: "blue" // Error!
// }
</code></pre>
<h3 id="template-inheritance-chains"><a class="header" href="#template-inheritance-chains">Template Inheritance Chains</a></h3>
<p>Build template hierarchies for progressive specialization:</p>
<pre><code class="language-storybook">template Worker {
skill_level: 0.0..1.0
wage: 10..50
}
template SkilledWorker {
include Worker
specialization: "general"
tool_proficiency: 0.5..1.0
}
template MasterCraftsman {
include SkilledWorker
can_teach: true
reputation: 0.7..1.0
}
</code></pre>
<h2 id="relationship-patterns"><a class="header" href="#relationship-patterns">Relationship Patterns</a></h2>
<h3 id="bidirectional-perspective"><a class="header" href="#bidirectional-perspective">Bidirectional Perspective</a></h3>
<p>Model relationships where each side sees things differently:</p>
<pre><code class="language-storybook">relationship MentorApprentice {
Master as mentor self {
patience: 0.7
investment_in_student: 0.9
} other {
sees_potential: 0.8
frustration_level: 0.3
}
Student as apprentice self {
dedication: 0.8
overwhelmed: 0.4
} other {
respect: 0.95
desire_to_impress: 0.9
}
bond: 0.75
years_together: 3
}
</code></pre>
<h3 id="power-dynamic-pattern"><a class="header" href="#power-dynamic-pattern">Power Dynamic Pattern</a></h3>
<p>Model unequal power relationships explicitly:</p>
<pre><code class="language-storybook">relationship Vassalage {
King as lord self {
authority: 1.0
grants: "protection"
} other {
trusts_vassal: 0.6
}
Knight as vassal self {
loyalty: 0.9
ambition: 0.4
} other {
respects_lord: 0.8
fears_lord: 0.3
}
bond: 0.7
}
</code></pre>
<h3 id="relationship-network"><a class="header" href="#relationship-network">Relationship Network</a></h3>
<p>Build social graphs with multiple overlapping relationships:</p>
<pre><code class="language-storybook">// Family
relationship BakerMarriage { Martha as spouse, David as spouse, bond: 0.9 }
relationship BakerParenting { Martha as parent, Tommy as child, bond: 0.95 }
// Professional
relationship BakerEmployment { Martha as employer, Elena as employee, bond: 0.8 }
relationship GuildMembership { Martha as member, BakersGuild as org }
// Social
relationship BakerFriendship { Martha, Neighbor, bond: 0.6 }
</code></pre>
<h2 id="schedule-patterns"><a class="header" href="#schedule-patterns">Schedule Patterns</a></h2>
<h3 id="base-schedule-with-specializations"><a class="header" href="#base-schedule-with-specializations">Base Schedule with Specializations</a></h3>
<pre><code class="language-storybook">schedule BaseWorker {
block work { 09:00 - 17:00, action: work::standard }
block lunch { 12:00 - 13:00, action: social::lunch }
}
schedule EarlyBird extends BaseWorker {
block work { 05:00 - 13:00, action: work::early_shift }
block lunch { 11:00 - 12:00, action: social::lunch }
}
schedule NightOwl extends BaseWorker {
block work { 14:00 - 22:00, action: work::late_shift }
block lunch { 18:00 - 19:00, action: social::dinner }
}
</code></pre>
<h3 id="seasonal-variation"><a class="header" href="#seasonal-variation">Seasonal Variation</a></h3>
<pre><code class="language-storybook">schedule FarmSchedule {
block spring_work {
06:00 - 18:00
action: farming::plant
on season spring
}
block summer_work {
05:00 - 20:00
action: farming::tend
on season summer
}
block fall_work {
06:00 - 20:00
action: farming::harvest
on season fall
}
block winter_work {
08:00 - 16:00
action: farming::maintain
on season winter
}
}
</code></pre>
<h2 id="life-arc-patterns"><a class="header" href="#life-arc-patterns">Life Arc Patterns</a></h2>
<h3 id="progressive-development"><a class="header" href="#progressive-development">Progressive Development</a></h3>
<pre><code class="language-storybook">life_arc CareerProgression {
state novice {
on enter { Character.title: "Apprentice" }
on experience &gt; 100 -&gt; intermediate
}
state intermediate {
on enter { Character.title: "Journeyman" }
on experience &gt; 500 -&gt; expert
}
state expert {
on enter { Character.title: "Master", Character.can_teach: true }
}
}
</code></pre>
<h3 id="emotional-state-machine"><a class="header" href="#emotional-state-machine">Emotional State Machine</a></h3>
<pre><code class="language-storybook">life_arc MoodSystem {
state neutral {
on provoked -&gt; angry
on complimented -&gt; happy
on tired -&gt; sleepy
}
state angry {
on enter { Character.aggression: 0.9 }
on calmed_down -&gt; neutral
on escalated -&gt; furious
}
state furious {
on enter { Character.aggression: 1.0 }
on timeout_elapsed -&gt; angry
}
state happy {
on enter { Character.gives_discounts: true }
on insulted -&gt; neutral
}
state sleepy {
on enter { Character.responsiveness: 0.2 }
on woke_up -&gt; neutral
}
}
</code></pre>
<h2 id="project-organization-patterns"><a class="header" href="#project-organization-patterns">Project Organization Patterns</a></h2>
<h3 id="schema--world-separation"><a class="header" href="#schema--world-separation">Schema / World Separation</a></h3>
<p>Keep type definitions separate from instance data:</p>
<pre><code>my-project/
schema/ # Types and templates (reusable)
core_enums.sb
templates.sb
beings.sb
world/ # Instances (specific to this story)
characters/
behaviors/
relationships/
locations/
</code></pre>
<h3 id="module-per-domain"><a class="header" href="#module-per-domain">Module per Domain</a></h3>
<p>Group related declarations together:</p>
<pre><code>world/
characters/
heroes.sb # All hero characters
villains.sb # All villain characters
npcs.sb # Background characters
behaviors/
combat.sb # Combat behaviors
social.sb # Social behaviors
exploration.sb # Exploration behaviors
</code></pre>
<h2 id="anti-patterns-to-avoid"><a class="header" href="#anti-patterns-to-avoid">Anti-Patterns to Avoid</a></h2>
<p><strong>Deep nesting</strong>: More than 4-5 levels of behavior tree nesting is hard to read. Use <code>include</code> to flatten.</p>
<p><strong>God behaviors</strong>: One massive behavior tree doing everything. Break it into focused subtrees.</p>
<p><strong>Deep species hierarchies</strong>: More than 2-3 levels of species <code>includes</code> is rarely needed. Use templates for variation.</p>
<p><strong>Duplicated logic</strong>: If two behaviors share logic, extract it into a shared subtree.</p>
<p><strong>Unnamed nodes</strong>: Always label composite nodes in behavior trees for readability.</p>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="../reference/11-behavior-trees.html">Behavior Trees</a> - Complete behavior syntax</li>
<li><a href="../reference/10-characters.html">Characters</a> - Character architecture</li>
<li><a href="../reference/15-relationships.html">Relationships</a> - Relationship modeling</li>
<li><a href="../reference/14-schedules.html">Schedules</a> - Schedule composition</li>
<li><a href="../reference/13-life-arcs.html">Life Arcs</a> - State machine patterns</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/19-validation.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/21-sbir-format.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/19-validation.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/21-sbir-format.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,289 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>The SBIR Binary Format - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="the-sbir-binary-format"><a class="header" href="#the-sbir-binary-format">The SBIR Binary Format</a></h1>
<p>SBIR (Storybook Intermediate Representation) is the compiled binary format produced by the Storybook compiler. It transforms human-readable <code>.sb</code> files into an optimized, machine-consumable format for simulation runtimes.</p>
<h2 id="compilation-pipeline"><a class="header" href="#compilation-pipeline">Compilation Pipeline</a></h2>
<pre><code>.sb files → Lexer → Parser → AST → Resolver → SBIR Binary
</code></pre>
<ol>
<li><strong>Lexer</strong>: Tokenizes raw text into tokens</li>
<li><strong>Parser</strong>: Builds an Abstract Syntax Tree (AST) from tokens</li>
<li><strong>Resolver</strong>: Validates, resolves cross-references, merges templates, and produces SBIR</li>
</ol>
<h2 id="what-sbir-contains"><a class="header" href="#what-sbir-contains">What SBIR Contains</a></h2>
<p>SBIR represents the fully resolved state of a Storybook project:</p>
<ul>
<li><strong>Characters</strong>: All fields resolved (species + templates merged, overrides applied)</li>
<li><strong>Behaviors</strong>: Behavior trees with all subtree references inlined</li>
<li><strong>Life Arcs</strong>: State machines with validated transitions</li>
<li><strong>Schedules</strong>: Time blocks with resolved action references</li>
<li><strong>Relationships</strong>: Participants with resolved entity references</li>
<li><strong>Institutions</strong>: Fully resolved field sets</li>
<li><strong>Locations</strong>: Fully resolved field sets</li>
<li><strong>Species</strong>: Fully resolved inheritance chains</li>
<li><strong>Enums</strong>: Complete variant lists</li>
</ul>
<h2 id="resolution-process"><a class="header" href="#resolution-process">Resolution Process</a></h2>
<h3 id="template-merging"><a class="header" href="#template-merging">Template Merging</a></h3>
<p>When a character uses templates, SBIR contains the fully merged result:</p>
<p><strong>Source:</strong></p>
<pre><code class="language-storybook">species Human { lifespan: 70, speed: 1.0 }
template Warrior { speed: 1.5, strength: 10 }
character Conan: Human from Warrior {
strength: 20
}
</code></pre>
<p><strong>In SBIR, Conans fields are:</strong></p>
<ul>
<li><code>lifespan: 70</code> (from Human)</li>
<li><code>speed: 1.5</code> (Warrior overrides Human)</li>
<li><code>strength: 20</code> (Conan overrides Warrior)</li>
</ul>
<h3 id="cross-file-reference-resolution"><a class="header" href="#cross-file-reference-resolution">Cross-File Reference Resolution</a></h3>
<p>SBIR resolves all <code>use</code> statements and qualified paths. A relationship referencing <code>Martha</code> in a different file is resolved to the concrete character definition.</p>
<h3 id="validation"><a class="header" href="#validation">Validation</a></h3>
<p>Before producing SBIR, the resolver validates all constraints documented in <a href="../reference/19-validation.html">Validation Rules</a>:</p>
<ul>
<li>All references resolve to defined declarations</li>
<li>No circular dependencies</li>
<li>Type consistency</li>
<li>Domain constraints (bond ranges, schedule validity)</li>
</ul>
<h2 id="design-goals"><a class="header" href="#design-goals">Design Goals</a></h2>
<p><strong>Compact</strong>: SBIR strips comments, whitespace, and redundant structure.</p>
<p><strong>Self-contained</strong>: No external references everything is resolved and inlined.</p>
<p><strong>Fast to load</strong>: Simulation runtimes can load SBIR without re-parsing or re-resolving.</p>
<p><strong>Validated</strong>: If SBIR was produced, the source was valid. Runtimes do not need to re-validate.</p>
<h2 id="usage"><a class="header" href="#usage">Usage</a></h2>
<p>SBIR is consumed by simulation runtimes that drive character behavior, schedule execution, life arc transitions, and relationship queries. The specific binary format is implementation-defined and may evolve between versions.</p>
<p>For the current SBIR specification, see the <a href="../SBIR-v0.2.0-SPEC.html">SBIR v0.2.0 Spec</a>.</p>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="../reference/09-overview.html">Language Overview</a> - Compilation model</li>
<li><a href="../reference/19-validation.html">Validation Rules</a> - What is validated before SBIR production</li>
<li><a href="./22-integration.html">Integration Guide</a> - How runtimes consume SBIR</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../advanced/20-patterns.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/22-integration.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../advanced/20-patterns.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/22-integration.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,361 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Integration Guide - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="integration-guide"><a class="header" href="#integration-guide">Integration Guide</a></h1>
<p>This chapter covers how to integrate Storybook into larger systems game engines, simulation frameworks, and custom applications.</p>
<h2 id="architecture-overview"><a class="header" href="#architecture-overview">Architecture Overview</a></h2>
<p>Storybook operates in two phases:</p>
<ol>
<li><strong>Compile time</strong>: <code>.sb</code> files are parsed, validated, and compiled into SBIR</li>
<li><strong>Runtime</strong>: A simulation engine consumes SBIR and drives character behavior</li>
</ol>
<pre><code> Compile Time Runtime
.sb files → [Storybook Compiler] → SBIR → [Simulation Engine] → Character Actions
</code></pre>
<h2 id="the-storybook-compiler"><a class="header" href="#the-storybook-compiler">The Storybook Compiler</a></h2>
<p>The compiler is a Rust library and CLI tool that processes <code>.sb</code> files.</p>
<h3 id="cli-usage"><a class="header" href="#cli-usage">CLI Usage</a></h3>
<pre><code class="language-bash"># Compile a directory of .sb files
storybook compile path/to/project/
# Compile with output path
storybook compile path/to/project/ -o output.sbir
# Validate without producing output
storybook check path/to/project/
</code></pre>
<h3 id="as-a-library"><a class="header" href="#as-a-library">As a Library</a></h3>
<p>The compiler can be embedded as a Rust dependency:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>use storybook::syntax::parse_file;
use storybook::resolve::resolve_files;
// Parse .sb files into ASTs
let ast = parse_file(source_code)?;
// Resolve across multiple files
let resolved = resolve_files(vec![ast1, ast2, ast3])?;
// Access resolved data
for character in resolved.characters() {
println!("{}: {:?}", character.name, character.fields);
}
<span class="boring">}</span></code></pre></pre>
<h3 id="key-types"><a class="header" href="#key-types">Key Types</a></h3>
<p>The resolved output provides these primary types:</p>
<div class="table-wrapper"><table><thead><tr><th>Type</th><th>Description</th></tr></thead><tbody>
<tr><td><code>ResolvedFile</code></td><td>Container for all resolved declarations</td></tr>
<tr><td><code>ResolvedCharacter</code></td><td>Character with merged species/template fields</td></tr>
<tr><td><code>ResolvedBehavior</code></td><td>Behavior tree with resolved subtree references</td></tr>
<tr><td><code>ResolvedLifeArc</code></td><td>State machine with validated transitions</td></tr>
<tr><td><code>ResolvedSchedule</code></td><td>Schedule with resolved time blocks</td></tr>
<tr><td><code>ResolvedRelationship</code></td><td>Relationship with resolved participant references</td></tr>
<tr><td><code>ResolvedInstitution</code></td><td>Institution with resolved fields</td></tr>
<tr><td><code>ResolvedLocation</code></td><td>Location with resolved fields</td></tr>
<tr><td><code>ResolvedSpecies</code></td><td>Species with resolved includes chain</td></tr>
<tr><td><code>ResolvedEnum</code></td><td>Enum with variant list</td></tr>
</tbody></table>
</div>
<h2 id="runtime-integration"><a class="header" href="#runtime-integration">Runtime Integration</a></h2>
<h3 id="behavior-tree-execution"><a class="header" href="#behavior-tree-execution">Behavior Tree Execution</a></h3>
<p>Runtimes are responsible for:</p>
<ol>
<li><strong>Tick-based evaluation</strong>: Call the behavior tree root each frame/tick</li>
<li><strong>Action execution</strong>: Interpret action nodes (e.g., <code>MoveTo</code>, <code>Attack</code>)</li>
<li><strong>Condition evaluation</strong>: Evaluate expression nodes against current state</li>
<li><strong>Decorator state</strong>: Maintain timer/counter state for stateful decorators</li>
</ol>
<h3 id="life-arc-execution"><a class="header" href="#life-arc-execution">Life Arc Execution</a></h3>
<ol>
<li>Track the current state for each life arc instance</li>
<li>Evaluate transition conditions each tick</li>
<li>Execute on-enter actions when transitioning</li>
<li>Maintain state persistence across ticks</li>
</ol>
<h3 id="schedule-execution"><a class="header" href="#schedule-execution">Schedule Execution</a></h3>
<ol>
<li>Get the current simulation time</li>
<li>Find the matching schedule block (considering temporal constraints and recurrences)</li>
<li>Execute the associated behavior tree action</li>
</ol>
<h3 id="relationship-queries"><a class="header" href="#relationship-queries">Relationship Queries</a></h3>
<p>Provide APIs for querying the relationship graph:</p>
<ul>
<li>Find all relationships for a character</li>
<li>Get bond strength between two entities</li>
<li>Query perspective fields (self/other)</li>
</ul>
<h2 id="lsp-integration"><a class="header" href="#lsp-integration">LSP Integration</a></h2>
<p>Storybook includes a Language Server Protocol (LSP) implementation for editor support:</p>
<ul>
<li><strong>Hover information</strong>: Documentation for keywords and declarations</li>
<li><strong>Go to definition</strong>: Navigate to declaration sources</li>
<li><strong>Diagnostics</strong>: Real-time error reporting</li>
<li><strong>Completions</strong>: Context-aware suggestions</li>
</ul>
<p>The LSP server reuses the compilers parser and resolver, providing the same validation as the CLI.</p>
<h3 id="editor-setup"><a class="header" href="#editor-setup">Editor Setup</a></h3>
<p>The Storybook LSP works with any editor that supports LSP:</p>
<ul>
<li><strong>VS Code</strong>: Via the Storybook extension</li>
<li><strong>Zed</strong>: Via the zed-storybook extension</li>
<li><strong>Other editors</strong>: Any LSP-compatible editor</li>
</ul>
<h2 id="tree-sitter-grammar"><a class="header" href="#tree-sitter-grammar">Tree-sitter Grammar</a></h2>
<p>A Tree-sitter grammar is provided for syntax highlighting and structural queries:</p>
<pre><code>tree-sitter-storybook/
grammar.js # Grammar definition
src/ # Generated parser
</code></pre>
<p>The Tree-sitter grammar supports:</p>
<ul>
<li>Syntax highlighting in editors</li>
<li>Structural code queries</li>
<li>Incremental parsing for large files</li>
</ul>
<h2 id="file-organization-for-integration"><a class="header" href="#file-organization-for-integration">File Organization for Integration</a></h2>
<p>Organize your project to support both authoring and runtime consumption:</p>
<pre><code>my-game/
storybook/ # Storybook source files
schema/
core_enums.sb
templates.sb
beings.sb
world/
characters/
behaviors/
relationships/
assets/
narrative.sbir # Compiled output for runtime
src/
narrative_engine.rs # Runtime that consumes SBIR
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./21-sbir-format.html">SBIR Format</a> - The compiled binary format</li>
<li><a href="../reference/19-validation.html">Validation Rules</a> - What the compiler checks</li>
<li><a href="../reference/09-overview.html">Language Overview</a> - Compilation pipeline</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../advanced/21-sbir-format.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/23-best-practices.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../advanced/21-sbir-format.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/23-best-practices.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,499 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Best Practices - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h1>
<p>This chapter compiles best practices for writing clear, maintainable, and effective Storybook code. These guidelines apply across all declaration types and project sizes.</p>
<h2 id="naming-conventions"><a class="header" href="#naming-conventions">Naming Conventions</a></h2>
<h3 id="declarations"><a class="header" href="#declarations">Declarations</a></h3>
<p>Use <strong>PascalCase</strong> for all declaration names:</p>
<pre><code class="language-storybook">character MasterBaker { } // Good
species DomesticCat { } // Good
behavior GuardPatrol { } // Good
character master_baker { } // Avoid
behavior guard-patrol { } // Invalid (no hyphens)
</code></pre>
<h3 id="fields"><a class="header" href="#fields">Fields</a></h3>
<p>Use <strong>snake_case</strong> for field names:</p>
<pre><code class="language-storybook">character Martha {
skill_level: 0.95 // Good
emotional_state: focused // Good
years_experience: 22 // Good
skillLevel: 0.95 // Avoid (camelCase)
}
</code></pre>
<h3 id="behavior-tree-labels"><a class="header" href="#behavior-tree-labels">Behavior Tree Labels</a></h3>
<p>Use <strong>snake_case</strong> for node labels, with descriptive names:</p>
<pre><code class="language-storybook">choose survival_instinct { // Good
then fight_response { } // Good
then flight_response { } // Good
}
choose s1 { // Avoid (meaningless)
then a { } // Avoid
}
</code></pre>
<h3 id="enum-variants"><a class="header" href="#enum-variants">Enum Variants</a></h3>
<p>Use <strong>PascalCase</strong> or <strong>snake_case</strong> consistently within an enum:</p>
<pre><code class="language-storybook">// PascalCase (good for short names)
enum Size { Tiny, Small, Normal, Large, Huge }
// snake_case (good for compound names)
enum GovernmentStyle {
absolute_tyranny,
constitutional_monarchy,
direct_democracy
}
</code></pre>
<h2 id="file-organization"><a class="header" href="#file-organization">File Organization</a></h2>
<h3 id="separate-schema-from-world"><a class="header" href="#separate-schema-from-world">Separate Schema from World</a></h3>
<p>Keep reusable type definitions separate from instance data:</p>
<pre><code>project/
schema/ # Reusable across stories
core_enums.sb # Enum definitions
templates.sb # Template definitions
beings.sb # Species definitions
world/ # Specific to this story
characters/ # Character instances
behaviors/ # Behavior trees
relationships/ # Relationship instances
locations/ # Location instances
</code></pre>
<h3 id="one-concern-per-file"><a class="header" href="#one-concern-per-file">One Concern per File</a></h3>
<p>Group related declarations, but avoid putting unrelated things together:</p>
<pre><code class="language-storybook">// characters/bakery_staff.sb - Good: related characters together
character Martha { }
character Jane { }
character Elena { }
// everything.sb - Avoid: everything in one file
character Martha { }
behavior BakeRoutine { }
schedule DailyRoutine { }
relationship Partnership { }
</code></pre>
<h3 id="explicit-imports"><a class="header" href="#explicit-imports">Explicit Imports</a></h3>
<p>Prefer explicit imports over wildcards:</p>
<pre><code class="language-storybook">// Good: clear what is being used
use schema::core_enums::{SkillLevel, Specialty};
use schema::beings::Human;
// Avoid: unclear dependencies
use schema::core_enums::*;
use schema::beings::*;
</code></pre>
<h2 id="character-design"><a class="header" href="#character-design">Character Design</a></h2>
<h3 id="use-species-for-identity-templates-for-traits"><a class="header" href="#use-species-for-identity-templates-for-traits">Use Species for Identity, Templates for Traits</a></h3>
<pre><code class="language-storybook">// Species: ontological identity
species Human { lifespan: 70 }
// Templates: compositional traits
template Warrior { strength: 10..20 }
template Scholar { intelligence: 15..20 }
// Character: combines identity and traits
character Aragorn: Human from Warrior {
strength: 18
}
</code></pre>
<h3 id="document-with-prose-blocks"><a class="header" href="#document-with-prose-blocks">Document with Prose Blocks</a></h3>
<pre><code class="language-storybook">character Martha: Human {
age: 34
---backstory
Martha learned to bake from her grandmother, starting at
age twelve. She now runs the most popular bakery in town.
---
---personality
Meticulous and patient, with an unwavering commitment to
quality. Tough but fair with her staff.
---
}
</code></pre>
<h3 id="prefer-flat-inheritance"><a class="header" href="#prefer-flat-inheritance">Prefer Flat Inheritance</a></h3>
<p>Avoid deep species hierarchies. Two or three levels is usually enough:</p>
<pre><code class="language-storybook">// Good: shallow
species Mammal { warm_blooded: true }
species Human includes Mammal { sapient: true }
// Avoid: too deep
species Being { }
species LivingBeing includes Being { }
species Animal includes LivingBeing { }
species Vertebrate includes Animal { }
species Mammal includes Vertebrate { }
species Human includes Mammal { }
</code></pre>
<h2 id="behavior-tree-design"><a class="header" href="#behavior-tree-design">Behavior Tree Design</a></h2>
<h3 id="name-every-composite-node"><a class="header" href="#name-every-composite-node">Name Every Composite Node</a></h3>
<pre><code class="language-storybook">// Good: self-documenting
choose daily_priority {
then handle_emergency { }
then do_work { }
then relax { }
}
// Avoid: anonymous nodes
choose {
then { }
then { }
}
</code></pre>
<h3 id="keep-trees-shallow"><a class="header" href="#keep-trees-shallow">Keep Trees Shallow</a></h3>
<p>Extract deep subtrees into separate behaviors:</p>
<pre><code class="language-storybook">// Good: flat with includes
behavior Main {
choose mode {
include CombatBehavior
include ExplorationBehavior
include SocialBehavior
}
}
// Avoid: deeply nested
behavior Main {
choose {
then { choose { then { choose { then { Action } } } } }
}
}
</code></pre>
<h3 id="use-decorators-for-control-flow"><a class="header" href="#use-decorators-for-control-flow">Use Decorators for Control Flow</a></h3>
<pre><code class="language-storybook">// Good: decorator handles timing
cooldown(30s) { CastSpell }
timeout(10s) { SolvePuzzle }
retry(3) { PickLock }
// Avoid: manual timing in actions
CheckCooldownTimer
IfCooldownReady { CastSpell }
</code></pre>
<h2 id="expression-writing"><a class="header" href="#expression-writing">Expression Writing</a></h2>
<h3 id="use-parentheses-for-clarity"><a class="header" href="#use-parentheses-for-clarity">Use Parentheses for Clarity</a></h3>
<pre><code class="language-storybook">// Good: explicit grouping
on (health &lt; 50 or is_poisoned) and has_antidote -&gt; healing
// Risky: relies on precedence knowledge
on health &lt; 50 or is_poisoned and has_antidote -&gt; healing
</code></pre>
<h3 id="break-complex-conditions-into-multiple-transitions"><a class="header" href="#break-complex-conditions-into-multiple-transitions">Break Complex Conditions into Multiple Transitions</a></h3>
<pre><code class="language-storybook">// Good: separate transitions, easy to read
state combat {
on health &lt; 20 and not has_potion -&gt; desperate
on surrounded and not has_escape -&gt; desperate
on enemy_count &gt; 10 -&gt; desperate
}
// Avoid: one massive condition
state combat {
on (health &lt; 20 and not has_potion) or (surrounded and not has_escape) or enemy_count &gt; 10 -&gt; desperate
}
</code></pre>
<h3 id="use-is-for-enum-comparisons"><a class="header" href="#use-is-for-enum-comparisons">Use <code>is</code> for Enum Comparisons</a></h3>
<pre><code class="language-storybook">// Good: reads naturally
on status is active -&gt; active_state
on skill_level is master -&gt; teach_others
// Works but less readable
on status == active -&gt; active_state
</code></pre>
<h2 id="schedule-design"><a class="header" href="#schedule-design">Schedule Design</a></h2>
<h3 id="use-named-blocks-for-override-support"><a class="header" href="#use-named-blocks-for-override-support">Use Named Blocks for Override Support</a></h3>
<pre><code class="language-storybook">// Good: named blocks can be overridden
schedule Base {
block work { 09:00 - 17:00, action: standard_work }
}
schedule Variant extends Base {
block work { 05:00 - 13:00, action: early_work }
}
</code></pre>
<h3 id="group-related-blocks"><a class="header" href="#group-related-blocks">Group Related Blocks</a></h3>
<pre><code class="language-storybook">schedule DailyRoutine {
// Morning
block wake { 06:00 - 07:00, action: morning_routine }
block breakfast { 07:00 - 08:00, action: eat }
// Work
block commute { 08:00 - 09:00, action: travel }
block work { 09:00 - 17:00, action: work }
// Evening
block leisure { 18:00 - 22:00, action: relax }
block sleep { 22:00 - 06:00, action: sleep }
}
</code></pre>
<h2 id="relationship-design"><a class="header" href="#relationship-design">Relationship Design</a></h2>
<h3 id="use-roles-for-clarity"><a class="header" href="#use-roles-for-clarity">Use Roles for Clarity</a></h3>
<pre><code class="language-storybook">// Good: roles clarify the relationship
relationship Marriage {
Martha as spouse
David as spouse
bond: 0.9
}
// Less clear without roles
relationship Marriage {
Martha
David
bond: 0.9
}
</code></pre>
<h3 id="use-perspectives-for-asymmetry"><a class="header" href="#use-perspectives-for-asymmetry">Use Perspectives for Asymmetry</a></h3>
<pre><code class="language-storybook">// Good: captures different viewpoints
relationship TeacherStudent {
Gandalf as teacher self { patience: 0.8 } other { potential: 0.9 }
Frodo as student self { motivation: 0.7 } other { admiration: 0.95 }
bond: 0.85
}
</code></pre>
<h2 id="general-principles"><a class="header" href="#general-principles">General Principles</a></h2>
<ol>
<li>
<p><strong>Readability over brevity</strong>: Storybook code should read like a narrative, not a puzzle.</p>
</li>
<li>
<p><strong>Explicit over implicit</strong>: Say what you mean. Use named nodes, explicit imports, and clear field names.</p>
</li>
<li>
<p><strong>Flat over deep</strong>: Shallow hierarchies, short behavior trees, and focused files are easier to maintain.</p>
</li>
<li>
<p><strong>Composition over inheritance</strong>: Prefer combining templates over building deep species hierarchies.</p>
</li>
<li>
<p><strong>Document with prose</strong>: Prose blocks are a feature, not clutter. Use them to explain intent alongside data.</p>
</li>
<li>
<p><strong>One concept per declaration</strong>: Each behavior tree, life arc, or schedule should have a single clear purpose.</p>
</li>
</ol>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./20-patterns.html">Design Patterns</a> - Common structural patterns</li>
<li><a href="../reference/19-validation.html">Validation Rules</a> - What the compiler checks</li>
<li><a href="../reference/09-overview.html">Language Overview</a> - Language philosophy</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../advanced/22-integration.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/24-baker-family-complete.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../advanced/22-integration.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/24-baker-family-complete.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,78 @@
/*
Based off of the Ayu theme
Original by Dempfi (https://github.com/dempfi/ayu)
*/
.hljs {
display: block;
overflow-x: auto;
background: #191f26;
color: #e6e1cf;
}
.hljs-comment,
.hljs-quote {
color: #5c6773;
font-style: italic;
}
.hljs-variable,
.hljs-template-variable,
.hljs-attribute,
.hljs-attr,
.hljs-regexp,
.hljs-link,
.hljs-selector-id,
.hljs-selector-class {
color: #ff7733;
}
.hljs-number,
.hljs-meta,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params {
color: #ffee99;
}
.hljs-string,
.hljs-bullet {
color: #b8cc52;
}
.hljs-title,
.hljs-built_in,
.hljs-section {
color: #ffb454;
}
.hljs-keyword,
.hljs-selector-tag,
.hljs-symbol {
color: #ff7733;
}
.hljs-name {
color: #36a3d9;
}
.hljs-tag {
color: #00568d;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
.hljs-addition {
color: #91b362;
}
.hljs-deletion {
color: #d96c75;
}

690
docs/book/book.js Normal file
View File

@@ -0,0 +1,690 @@
"use strict";
// Fix back button cache problem
window.onunload = function () { };
// Global variable, shared between modules
function playground_text(playground, hidden = true) {
let code_block = playground.querySelector("code");
if (window.ace && code_block.classList.contains("editable")) {
let editor = window.ace.edit(code_block);
return editor.getValue();
} else if (hidden) {
return code_block.textContent;
} else {
return code_block.innerText;
}
}
(function codeSnippets() {
function fetch_with_timeout(url, options, timeout = 6000) {
return Promise.race([
fetch(url, options),
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout))
]);
}
var playgrounds = Array.from(document.querySelectorAll(".playground"));
if (playgrounds.length > 0) {
fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
headers: {
'Content-Type': "application/json",
},
method: 'POST',
mode: 'cors',
})
.then(response => response.json())
.then(response => {
// get list of crates available in the rust playground
let playground_crates = response.crates.map(item => item["id"]);
playgrounds.forEach(block => handle_crate_list_update(block, playground_crates));
});
}
function handle_crate_list_update(playground_block, playground_crates) {
// update the play buttons after receiving the response
update_play_button(playground_block, playground_crates);
// and install on change listener to dynamically update ACE editors
if (window.ace) {
let code_block = playground_block.querySelector("code");
if (code_block.classList.contains("editable")) {
let editor = window.ace.edit(code_block);
editor.addEventListener("change", function (e) {
update_play_button(playground_block, playground_crates);
});
// add Ctrl-Enter command to execute rust code
editor.commands.addCommand({
name: "run",
bindKey: {
win: "Ctrl-Enter",
mac: "Ctrl-Enter"
},
exec: _editor => run_rust_code(playground_block)
});
}
}
}
// updates the visibility of play button based on `no_run` class and
// used crates vs ones available on https://play.rust-lang.org
function update_play_button(pre_block, playground_crates) {
var play_button = pre_block.querySelector(".play-button");
// skip if code is `no_run`
if (pre_block.querySelector('code').classList.contains("no_run")) {
play_button.classList.add("hidden");
return;
}
// get list of `extern crate`'s from snippet
var txt = playground_text(pre_block);
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
var snippet_crates = [];
var item;
while (item = re.exec(txt)) {
snippet_crates.push(item[1]);
}
// check if all used crates are available on play.rust-lang.org
var all_available = snippet_crates.every(function (elem) {
return playground_crates.indexOf(elem) > -1;
});
if (all_available) {
play_button.classList.remove("hidden");
} else {
play_button.classList.add("hidden");
}
}
function run_rust_code(code_block) {
var result_block = code_block.querySelector(".result");
if (!result_block) {
result_block = document.createElement('code');
result_block.className = 'result hljs language-bash';
code_block.append(result_block);
}
let text = playground_text(code_block);
let classes = code_block.querySelector('code').classList;
let edition = "2015";
if(classes.contains("edition2018")) {
edition = "2018";
} else if(classes.contains("edition2021")) {
edition = "2021";
}
var params = {
version: "stable",
optimize: "0",
code: text,
edition: edition
};
if (text.indexOf("#![feature") !== -1) {
params.version = "nightly";
}
result_block.innerText = "Running...";
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
headers: {
'Content-Type': "application/json",
},
method: 'POST',
mode: 'cors',
body: JSON.stringify(params)
})
.then(response => response.json())
.then(response => {
if (response.result.trim() === '') {
result_block.innerText = "No output";
result_block.classList.add("result-no-output");
} else {
result_block.innerText = response.result;
result_block.classList.remove("result-no-output");
}
})
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
}
// Syntax highlighting Configuration
hljs.configure({
tabReplace: ' ', // 4 spaces
languages: [], // Languages used for auto-detection
});
let code_nodes = Array
.from(document.querySelectorAll('code'))
// Don't highlight `inline code` blocks in headers.
.filter(function (node) {return !node.parentElement.classList.contains("header"); });
if (window.ace) {
// language-rust class needs to be removed for editable
// blocks or highlightjs will capture events
code_nodes
.filter(function (node) {return node.classList.contains("editable"); })
.forEach(function (block) { block.classList.remove('language-rust'); });
code_nodes
.filter(function (node) {return !node.classList.contains("editable"); })
.forEach(function (block) { hljs.highlightBlock(block); });
} else {
code_nodes.forEach(function (block) { hljs.highlightBlock(block); });
}
// Adding the hljs class gives code blocks the color css
// even if highlighting doesn't apply
code_nodes.forEach(function (block) { block.classList.add('hljs'); });
Array.from(document.querySelectorAll("code.hljs")).forEach(function (block) {
var lines = Array.from(block.querySelectorAll('.boring'));
// If no lines were hidden, return
if (!lines.length) { return; }
block.classList.add("hide-boring");
var buttons = document.createElement('div');
buttons.className = 'buttons';
buttons.innerHTML = "<button class=\"fa fa-eye\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
// add expand button
var pre_block = block.parentNode;
pre_block.insertBefore(buttons, pre_block.firstChild);
pre_block.querySelector('.buttons').addEventListener('click', function (e) {
if (e.target.classList.contains('fa-eye')) {
e.target.classList.remove('fa-eye');
e.target.classList.add('fa-eye-slash');
e.target.title = 'Hide lines';
e.target.setAttribute('aria-label', e.target.title);
block.classList.remove('hide-boring');
} else if (e.target.classList.contains('fa-eye-slash')) {
e.target.classList.remove('fa-eye-slash');
e.target.classList.add('fa-eye');
e.target.title = 'Show hidden lines';
e.target.setAttribute('aria-label', e.target.title);
block.classList.add('hide-boring');
}
});
});
if (window.playground_copyable) {
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
var pre_block = block.parentNode;
if (!pre_block.classList.contains('playground')) {
var buttons = pre_block.querySelector(".buttons");
if (!buttons) {
buttons = document.createElement('div');
buttons.className = 'buttons';
pre_block.insertBefore(buttons, pre_block.firstChild);
}
var clipButton = document.createElement('button');
clipButton.className = 'clip-button';
clipButton.title = 'Copy to clipboard';
clipButton.setAttribute('aria-label', clipButton.title);
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
buttons.insertBefore(clipButton, buttons.firstChild);
}
});
}
// Process playground code blocks
Array.from(document.querySelectorAll(".playground")).forEach(function (pre_block) {
// Add play button
var buttons = pre_block.querySelector(".buttons");
if (!buttons) {
buttons = document.createElement('div');
buttons.className = 'buttons';
pre_block.insertBefore(buttons, pre_block.firstChild);
}
var runCodeButton = document.createElement('button');
runCodeButton.className = 'fa fa-play play-button';
runCodeButton.hidden = true;
runCodeButton.title = 'Run this code';
runCodeButton.setAttribute('aria-label', runCodeButton.title);
buttons.insertBefore(runCodeButton, buttons.firstChild);
runCodeButton.addEventListener('click', function (e) {
run_rust_code(pre_block);
});
if (window.playground_copyable) {
var copyCodeClipboardButton = document.createElement('button');
copyCodeClipboardButton.className = 'clip-button';
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
copyCodeClipboardButton.title = 'Copy to clipboard';
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
}
let code_block = pre_block.querySelector("code");
if (window.ace && code_block.classList.contains("editable")) {
var undoChangesButton = document.createElement('button');
undoChangesButton.className = 'fa fa-history reset-button';
undoChangesButton.title = 'Undo changes';
undoChangesButton.setAttribute('aria-label', undoChangesButton.title);
buttons.insertBefore(undoChangesButton, buttons.firstChild);
undoChangesButton.addEventListener('click', function () {
let editor = window.ace.edit(code_block);
editor.setValue(editor.originalCode);
editor.clearSelection();
});
}
});
})();
(function themes() {
var html = document.querySelector('html');
var themeToggleButton = document.getElementById('theme-toggle');
var themePopup = document.getElementById('theme-list');
var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
var themeIds = [];
themePopup.querySelectorAll('button.theme').forEach(function (el) {
themeIds.push(el.id);
});
var stylesheets = {
ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"),
tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"),
highlight: document.querySelector("[href$='highlight.css']"),
};
function showThemes() {
themePopup.style.display = 'block';
themeToggleButton.setAttribute('aria-expanded', true);
themePopup.querySelector("button#" + get_theme()).focus();
}
function updateThemeSelected() {
themePopup.querySelectorAll('.theme-selected').forEach(function (el) {
el.classList.remove('theme-selected');
});
themePopup.querySelector("button#" + get_theme()).classList.add('theme-selected');
}
function hideThemes() {
themePopup.style.display = 'none';
themeToggleButton.setAttribute('aria-expanded', false);
themeToggleButton.focus();
}
function get_theme() {
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch (e) { }
if (theme === null || theme === undefined || !themeIds.includes(theme)) {
return default_theme;
} else {
return theme;
}
}
function set_theme(theme, store = true) {
let ace_theme;
if (theme == 'coal' || theme == 'navy') {
stylesheets.ayuHighlight.disabled = true;
stylesheets.tomorrowNight.disabled = false;
stylesheets.highlight.disabled = true;
ace_theme = "ace/theme/tomorrow_night";
} else if (theme == 'ayu') {
stylesheets.ayuHighlight.disabled = false;
stylesheets.tomorrowNight.disabled = true;
stylesheets.highlight.disabled = true;
ace_theme = "ace/theme/tomorrow_night";
} else {
stylesheets.ayuHighlight.disabled = true;
stylesheets.tomorrowNight.disabled = true;
stylesheets.highlight.disabled = false;
ace_theme = "ace/theme/dawn";
}
setTimeout(function () {
themeColorMetaTag.content = getComputedStyle(document.documentElement).backgroundColor;
}, 1);
if (window.ace && window.editors) {
window.editors.forEach(function (editor) {
editor.setTheme(ace_theme);
});
}
var previousTheme = get_theme();
if (store) {
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
}
html.classList.remove(previousTheme);
html.classList.add(theme);
updateThemeSelected();
}
// Set theme
var theme = get_theme();
set_theme(theme, false);
themeToggleButton.addEventListener('click', function () {
if (themePopup.style.display === 'block') {
hideThemes();
} else {
showThemes();
}
});
themePopup.addEventListener('click', function (e) {
var theme;
if (e.target.className === "theme") {
theme = e.target.id;
} else if (e.target.parentElement.className === "theme") {
theme = e.target.parentElement.id;
} else {
return;
}
set_theme(theme);
});
themePopup.addEventListener('focusout', function(e) {
// e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
if (!!e.relatedTarget && !themeToggleButton.contains(e.relatedTarget) && !themePopup.contains(e.relatedTarget)) {
hideThemes();
}
});
// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang/mdBook/issues/628
document.addEventListener('click', function(e) {
if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
hideThemes();
}
});
document.addEventListener('keydown', function (e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
if (!themePopup.contains(e.target)) { return; }
switch (e.key) {
case 'Escape':
e.preventDefault();
hideThemes();
break;
case 'ArrowUp':
e.preventDefault();
var li = document.activeElement.parentElement;
if (li && li.previousElementSibling) {
li.previousElementSibling.querySelector('button').focus();
}
break;
case 'ArrowDown':
e.preventDefault();
var li = document.activeElement.parentElement;
if (li && li.nextElementSibling) {
li.nextElementSibling.querySelector('button').focus();
}
break;
case 'Home':
e.preventDefault();
themePopup.querySelector('li:first-child button').focus();
break;
case 'End':
e.preventDefault();
themePopup.querySelector('li:last-child button').focus();
break;
}
});
})();
(function sidebar() {
var body = document.querySelector("body");
var sidebar = document.getElementById("sidebar");
var sidebarLinks = document.querySelectorAll('#sidebar a');
var sidebarToggleButton = document.getElementById("sidebar-toggle");
var sidebarResizeHandle = document.getElementById("sidebar-resize-handle");
var firstContact = null;
function showSidebar() {
body.classList.remove('sidebar-hidden')
body.classList.add('sidebar-visible');
Array.from(sidebarLinks).forEach(function (link) {
link.setAttribute('tabIndex', 0);
});
sidebarToggleButton.setAttribute('aria-expanded', true);
sidebar.setAttribute('aria-hidden', false);
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
}
function hideSidebar() {
body.classList.remove('sidebar-visible')
body.classList.add('sidebar-hidden');
Array.from(sidebarLinks).forEach(function (link) {
link.setAttribute('tabIndex', -1);
});
sidebarToggleButton.setAttribute('aria-expanded', false);
sidebar.setAttribute('aria-hidden', true);
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
}
// Toggle sidebar
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
if (body.classList.contains("sidebar-hidden")) {
var current_width = parseInt(
document.documentElement.style.getPropertyValue('--sidebar-width'), 10);
if (current_width < 150) {
document.documentElement.style.setProperty('--sidebar-width', '150px');
}
showSidebar();
} else if (body.classList.contains("sidebar-visible")) {
hideSidebar();
} else {
if (getComputedStyle(sidebar)['transform'] === 'none') {
hideSidebar();
} else {
showSidebar();
}
}
});
sidebarResizeHandle.addEventListener('mousedown', initResize, false);
function initResize(e) {
window.addEventListener('mousemove', resize, false);
window.addEventListener('mouseup', stopResize, false);
body.classList.add('sidebar-resizing');
}
function resize(e) {
var pos = (e.clientX - sidebar.offsetLeft);
if (pos < 20) {
hideSidebar();
} else {
if (body.classList.contains("sidebar-hidden")) {
showSidebar();
}
pos = Math.min(pos, window.innerWidth - 100);
document.documentElement.style.setProperty('--sidebar-width', pos + 'px');
}
}
//on mouseup remove windows functions mousemove & mouseup
function stopResize(e) {
body.classList.remove('sidebar-resizing');
window.removeEventListener('mousemove', resize, false);
window.removeEventListener('mouseup', stopResize, false);
}
document.addEventListener('touchstart', function (e) {
firstContact = {
x: e.touches[0].clientX,
time: Date.now()
};
}, { passive: true });
document.addEventListener('touchmove', function (e) {
if (!firstContact)
return;
var curX = e.touches[0].clientX;
var xDiff = curX - firstContact.x,
tDiff = Date.now() - firstContact.time;
if (tDiff < 250 && Math.abs(xDiff) >= 150) {
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
showSidebar();
else if (xDiff < 0 && curX < 300)
hideSidebar();
firstContact = null;
}
}, { passive: true });
})();
(function chapterNavigation() {
document.addEventListener('keydown', function (e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
if (window.search && window.search.hasFocus()) { return; }
var html = document.querySelector('html');
function next() {
var nextButton = document.querySelector('.nav-chapters.next');
if (nextButton) {
window.location.href = nextButton.href;
}
}
function prev() {
var previousButton = document.querySelector('.nav-chapters.previous');
if (previousButton) {
window.location.href = previousButton.href;
}
}
switch (e.key) {
case 'ArrowRight':
e.preventDefault();
if (html.dir == 'rtl') {
prev();
} else {
next();
}
break;
case 'ArrowLeft':
e.preventDefault();
if (html.dir == 'rtl') {
next();
} else {
prev();
}
break;
}
});
})();
(function clipboard() {
var clipButtons = document.querySelectorAll('.clip-button');
function hideTooltip(elem) {
elem.firstChild.innerText = "";
elem.className = 'clip-button';
}
function showTooltip(elem, msg) {
elem.firstChild.innerText = msg;
elem.className = 'clip-button tooltipped';
}
var clipboardSnippets = new ClipboardJS('.clip-button', {
text: function (trigger) {
hideTooltip(trigger);
let playground = trigger.closest("pre");
return playground_text(playground, false);
}
});
Array.from(clipButtons).forEach(function (clipButton) {
clipButton.addEventListener('mouseout', function (e) {
hideTooltip(e.currentTarget);
});
});
clipboardSnippets.on('success', function (e) {
e.clearSelection();
showTooltip(e.trigger, "Copied!");
});
clipboardSnippets.on('error', function (e) {
showTooltip(e.trigger, "Clipboard error!");
});
})();
(function scrollToTop () {
var menuTitle = document.querySelector('.menu-title');
menuTitle.addEventListener('click', function () {
document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' });
});
})();
(function controllMenu() {
var menu = document.getElementById('menu-bar');
(function controllPosition() {
var scrollTop = document.scrollingElement.scrollTop;
var prevScrollTop = scrollTop;
var minMenuY = -menu.clientHeight - 50;
// When the script loads, the page can be at any scroll (e.g. if you reforesh it).
menu.style.top = scrollTop + 'px';
// Same as parseInt(menu.style.top.slice(0, -2), but faster
var topCache = menu.style.top.slice(0, -2);
menu.classList.remove('sticky');
var stickyCache = false; // Same as menu.classList.contains('sticky'), but faster
document.addEventListener('scroll', function () {
scrollTop = Math.max(document.scrollingElement.scrollTop, 0);
// `null` means that it doesn't need to be updated
var nextSticky = null;
var nextTop = null;
var scrollDown = scrollTop > prevScrollTop;
var menuPosAbsoluteY = topCache - scrollTop;
if (scrollDown) {
nextSticky = false;
if (menuPosAbsoluteY > 0) {
nextTop = prevScrollTop;
}
} else {
if (menuPosAbsoluteY > 0) {
nextSticky = true;
} else if (menuPosAbsoluteY < minMenuY) {
nextTop = prevScrollTop + minMenuY;
}
}
if (nextSticky === true && stickyCache === false) {
menu.classList.add('sticky');
stickyCache = true;
} else if (nextSticky === false && stickyCache === true) {
menu.classList.remove('sticky');
stickyCache = false;
}
if (nextTop !== null) {
menu.style.top = nextTop + 'px';
topCache = nextTop;
}
prevScrollTop = scrollTop;
}, { passive: true });
})();
(function controllBorder() {
function updateBorder() {
if (menu.offsetTop === 0) {
menu.classList.remove('bordered');
} else {
menu.classList.add('bordered');
}
}
updateBorder();
document.addEventListener('scroll', updateBorder, { passive: true });
})();
})();

41
docs/book/book.toml Normal file
View File

@@ -0,0 +1,41 @@
[book]
title = "Storybook Language Guide"
authors = ["R3T Studios"]
description = "Comprehensive documentation for the Storybook narrative simulation language"
language = "en"
multilingual = false
src = "."
[build]
build-dir = "book"
create-missing = false
[output.html]
default-theme = "light"
preferred-dark-theme = "navy"
smart-punctuation = true
git-repository-url = "https://github.com/r3t-studios/storybook"
git-repository-icon = "fa-github"
site-url = "/storybook/"
[output.html.playground]
editable = false
line-numbers = true
[output.html.search]
enable = true
limit-results = 30
use-boolean-and = true
boost-title = 2
boost-hierarchy = 1
boost-paragraph = 1
expand = true
heading-split-level = 3
[output.html.fold]
enable = true
level = 1
# Custom theme and styling (to be added later)
# [output.html.additional-css]
# [output.html.additional-js]

7
docs/book/clipboard.min.js vendored Normal file

File diff suppressed because one or more lines are too long

640
docs/book/css/chrome.css Normal file
View File

@@ -0,0 +1,640 @@
/* CSS for UI elements (a.k.a. chrome) */
html {
scrollbar-color: var(--scrollbar) var(--bg);
}
#searchresults a,
.content a:link,
a:visited,
a > .hljs {
color: var(--links);
}
/*
body-container is necessary because mobile browsers don't seem to like
overflow-x on the body tag when there is a <meta name="viewport"> tag.
*/
#body-container {
/*
This is used when the sidebar pushes the body content off the side of
the screen on small screens. Without it, dragging on mobile Safari
will want to reposition the viewport in a weird way.
*/
overflow-x: clip;
}
/* Menu Bar */
#menu-bar,
#menu-bar-hover-placeholder {
z-index: 101;
margin: auto calc(0px - var(--page-padding));
}
#menu-bar {
position: relative;
display: flex;
flex-wrap: wrap;
background-color: var(--bg);
border-block-end-color: var(--bg);
border-block-end-width: 1px;
border-block-end-style: solid;
}
#menu-bar.sticky,
#menu-bar-hover-placeholder:hover + #menu-bar,
#menu-bar:hover,
html.sidebar-visible #menu-bar {
position: -webkit-sticky;
position: sticky;
top: 0 !important;
}
#menu-bar-hover-placeholder {
position: sticky;
position: -webkit-sticky;
top: 0;
height: var(--menu-bar-height);
}
#menu-bar.bordered {
border-block-end-color: var(--table-border-color);
}
#menu-bar i, #menu-bar .icon-button {
position: relative;
padding: 0 8px;
z-index: 10;
line-height: var(--menu-bar-height);
cursor: pointer;
transition: color 0.5s;
}
@media only screen and (max-width: 420px) {
#menu-bar i, #menu-bar .icon-button {
padding: 0 5px;
}
}
.icon-button {
border: none;
background: none;
padding: 0;
color: inherit;
}
.icon-button i {
margin: 0;
}
.right-buttons {
margin: 0 15px;
}
.right-buttons a {
text-decoration: none;
}
.left-buttons {
display: flex;
margin: 0 5px;
}
html:not(.js) .left-buttons button {
display: none;
}
.menu-title {
display: inline-block;
font-weight: 200;
font-size: 2.4rem;
line-height: var(--menu-bar-height);
text-align: center;
margin: 0;
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.menu-title {
cursor: pointer;
}
.menu-bar,
.menu-bar:visited,
.nav-chapters,
.nav-chapters:visited,
.mobile-nav-chapters,
.mobile-nav-chapters:visited,
.menu-bar .icon-button,
.menu-bar a i {
color: var(--icons);
}
.menu-bar i:hover,
.menu-bar .icon-button:hover,
.nav-chapters:hover,
.mobile-nav-chapters i:hover {
color: var(--icons-hover);
}
/* Nav Icons */
.nav-chapters {
font-size: 2.5em;
text-align: center;
text-decoration: none;
position: fixed;
top: 0;
bottom: 0;
margin: 0;
max-width: 150px;
min-width: 90px;
display: flex;
justify-content: center;
align-content: center;
flex-direction: column;
transition: color 0.5s, background-color 0.5s;
}
.nav-chapters:hover {
text-decoration: none;
background-color: var(--theme-hover);
transition: background-color 0.15s, color 0.15s;
}
.nav-wrapper {
margin-block-start: 50px;
display: none;
}
.mobile-nav-chapters {
font-size: 2.5em;
text-align: center;
text-decoration: none;
width: 90px;
border-radius: 5px;
background-color: var(--sidebar-bg);
}
/* Only Firefox supports flow-relative values */
.previous { float: left; }
[dir=rtl] .previous { float: right; }
/* Only Firefox supports flow-relative values */
.next {
float: right;
right: var(--page-padding);
}
[dir=rtl] .next {
float: left;
right: unset;
left: var(--page-padding);
}
/* Use the correct buttons for RTL layouts*/
[dir=rtl] .previous i.fa-angle-left:before {content:"\f105";}
[dir=rtl] .next i.fa-angle-right:before { content:"\f104"; }
@media only screen and (max-width: 1080px) {
.nav-wide-wrapper { display: none; }
.nav-wrapper { display: block; }
}
/* sidebar-visible */
@media only screen and (max-width: 1380px) {
#sidebar-toggle-anchor:checked ~ .page-wrapper .nav-wide-wrapper { display: none; }
#sidebar-toggle-anchor:checked ~ .page-wrapper .nav-wrapper { display: block; }
}
/* Inline code */
:not(pre) > .hljs {
display: inline;
padding: 0.1em 0.3em;
border-radius: 3px;
}
:not(pre):not(a) > .hljs {
color: var(--inline-code-color);
overflow-x: initial;
}
a:hover > .hljs {
text-decoration: underline;
}
pre {
position: relative;
}
pre > .buttons {
position: absolute;
z-index: 100;
right: 0px;
top: 2px;
margin: 0px;
padding: 2px 0px;
color: var(--sidebar-fg);
cursor: pointer;
visibility: hidden;
opacity: 0;
transition: visibility 0.1s linear, opacity 0.1s linear;
}
pre:hover > .buttons {
visibility: visible;
opacity: 1
}
pre > .buttons :hover {
color: var(--sidebar-active);
border-color: var(--icons-hover);
background-color: var(--theme-hover);
}
pre > .buttons i {
margin-inline-start: 8px;
}
pre > .buttons button {
cursor: inherit;
margin: 0px 5px;
padding: 4px 4px 3px 5px;
font-size: 23px;
border-style: solid;
border-width: 1px;
border-radius: 4px;
border-color: var(--icons);
background-color: var(--theme-popup-bg);
transition: 100ms;
transition-property: color,border-color,background-color;
color: var(--icons);
}
pre > .buttons button.clip-button {
padding: 2px 4px 0px 6px;
}
pre > .buttons button.clip-button::before {
/* clipboard image from octicons (https://github.com/primer/octicons/tree/v2.0.0) MIT license
*/
content: url('data:image/svg+xml,<svg width="21" height="20" viewBox="0 0 24 25" \
xmlns="http://www.w3.org/2000/svg" aria-label="Copy to clipboard">\
<path d="M18 20h2v3c0 1-1 2-2 2H2c-.998 0-2-1-2-2V5c0-.911.755-1.667 1.667-1.667h5A3.323 3.323 0 \
0110 0a3.323 3.323 0 013.333 3.333h5C19.245 3.333 20 4.09 20 5v8.333h-2V9H2v14h16v-3zM3 \
7h14c0-.911-.793-1.667-1.75-1.667H13.5c-.957 0-1.75-.755-1.75-1.666C11.75 2.755 10.957 2 10 \
2s-1.75.755-1.75 1.667c0 .911-.793 1.666-1.75 1.666H4.75C3.793 5.333 3 6.09 3 7z"/>\
<path d="M4 19h6v2H4zM12 11H4v2h8zM4 17h4v-2H4zM15 15v-3l-4.5 4.5L15 21v-3l8.027-.032L23 15z"/>\
</svg>');
filter: var(--copy-button-filter);
}
pre > .buttons button.clip-button:hover::before {
filter: var(--copy-button-filter-hover);
}
@media (pointer: coarse) {
pre > .buttons button {
/* On mobile, make it easier to tap buttons. */
padding: 0.3rem 1rem;
}
.sidebar-resize-indicator {
/* Hide resize indicator on devices with limited accuracy */
display: none;
}
}
pre > code {
display: block;
padding: 1rem;
}
/* FIXME: ACE editors overlap their buttons because ACE does absolute
positioning within the code block which breaks padding. The only solution I
can think of is to move the padding to the outer pre tag (or insert a div
wrapper), but that would require fixing a whole bunch of CSS rules.
*/
.hljs.ace_editor {
padding: 0rem 0rem;
}
pre > .result {
margin-block-start: 10px;
}
/* Search */
#searchresults a {
text-decoration: none;
}
mark {
border-radius: 2px;
padding-block-start: 0;
padding-block-end: 1px;
padding-inline-start: 3px;
padding-inline-end: 3px;
margin-block-start: 0;
margin-block-end: -1px;
margin-inline-start: -3px;
margin-inline-end: -3px;
background-color: var(--search-mark-bg);
transition: background-color 300ms linear;
cursor: pointer;
}
mark.fade-out {
background-color: rgba(0,0,0,0) !important;
cursor: auto;
}
.searchbar-outer {
margin-inline-start: auto;
margin-inline-end: auto;
max-width: var(--content-max-width);
}
#searchbar {
width: 100%;
margin-block-start: 5px;
margin-block-end: 0;
margin-inline-start: auto;
margin-inline-end: auto;
padding: 10px 16px;
transition: box-shadow 300ms ease-in-out;
border: 1px solid var(--searchbar-border-color);
border-radius: 3px;
background-color: var(--searchbar-bg);
color: var(--searchbar-fg);
}
#searchbar:focus,
#searchbar.active {
box-shadow: 0 0 3px var(--searchbar-shadow-color);
}
.searchresults-header {
font-weight: bold;
font-size: 1em;
padding-block-start: 18px;
padding-block-end: 0;
padding-inline-start: 5px;
padding-inline-end: 0;
color: var(--searchresults-header-fg);
}
.searchresults-outer {
margin-inline-start: auto;
margin-inline-end: auto;
max-width: var(--content-max-width);
border-block-end: 1px dashed var(--searchresults-border-color);
}
ul#searchresults {
list-style: none;
padding-inline-start: 20px;
}
ul#searchresults li {
margin: 10px 0px;
padding: 2px;
border-radius: 2px;
}
ul#searchresults li.focus {
background-color: var(--searchresults-li-bg);
}
ul#searchresults span.teaser {
display: block;
clear: both;
margin-block-start: 5px;
margin-block-end: 0;
margin-inline-start: 20px;
margin-inline-end: 0;
font-size: 0.8em;
}
ul#searchresults span.teaser em {
font-weight: bold;
font-style: normal;
}
/* Sidebar */
.sidebar {
position: fixed;
left: 0;
top: 0;
bottom: 0;
width: var(--sidebar-width);
font-size: 0.875em;
box-sizing: border-box;
-webkit-overflow-scrolling: touch;
overscroll-behavior-y: contain;
background-color: var(--sidebar-bg);
color: var(--sidebar-fg);
}
.sidebar-iframe-inner {
background-color: var(--sidebar-bg);
color: var(--sidebar-fg);
padding: 10px 10px;
margin: 0;
font-size: 1.4rem;
}
.sidebar-iframe-outer {
border: none;
height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
[dir=rtl] .sidebar { left: unset; right: 0; }
.sidebar-resizing {
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
html:not(.sidebar-resizing) .sidebar {
transition: transform 0.3s; /* Animation: slide away */
}
.sidebar code {
line-height: 2em;
}
.sidebar .sidebar-scrollbox {
overflow-y: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
padding: 10px 10px;
}
.sidebar .sidebar-resize-handle {
position: absolute;
cursor: col-resize;
width: 0;
right: calc(var(--sidebar-resize-indicator-width) * -1);
top: 0;
bottom: 0;
display: flex;
align-items: center;
}
.sidebar-resize-handle .sidebar-resize-indicator {
width: 100%;
height: 12px;
background-color: var(--icons);
margin-inline-start: var(--sidebar-resize-indicator-space);
}
[dir=rtl] .sidebar .sidebar-resize-handle {
left: calc(var(--sidebar-resize-indicator-width) * -1);
right: unset;
}
.js .sidebar .sidebar-resize-handle {
cursor: col-resize;
width: calc(var(--sidebar-resize-indicator-width) - var(--sidebar-resize-indicator-space));
}
/* sidebar-hidden */
#sidebar-toggle-anchor:not(:checked) ~ .sidebar {
transform: translateX(calc(0px - var(--sidebar-width) - var(--sidebar-resize-indicator-width)));
z-index: -1;
}
[dir=rtl] #sidebar-toggle-anchor:not(:checked) ~ .sidebar {
transform: translateX(calc(var(--sidebar-width) + var(--sidebar-resize-indicator-width)));
}
.sidebar::-webkit-scrollbar {
background: var(--sidebar-bg);
}
.sidebar::-webkit-scrollbar-thumb {
background: var(--scrollbar);
}
/* sidebar-visible */
#sidebar-toggle-anchor:checked ~ .page-wrapper {
transform: translateX(calc(var(--sidebar-width) + var(--sidebar-resize-indicator-width)));
}
[dir=rtl] #sidebar-toggle-anchor:checked ~ .page-wrapper {
transform: translateX(calc(0px - var(--sidebar-width) - var(--sidebar-resize-indicator-width)));
}
@media only screen and (min-width: 620px) {
#sidebar-toggle-anchor:checked ~ .page-wrapper {
transform: none;
margin-inline-start: calc(var(--sidebar-width) + var(--sidebar-resize-indicator-width));
}
[dir=rtl] #sidebar-toggle-anchor:checked ~ .page-wrapper {
transform: none;
}
}
.chapter {
list-style: none outside none;
padding-inline-start: 0;
line-height: 2.2em;
}
.chapter ol {
width: 100%;
}
.chapter li {
display: flex;
color: var(--sidebar-non-existant);
}
.chapter li a {
display: block;
padding: 0;
text-decoration: none;
color: var(--sidebar-fg);
}
.chapter li a:hover {
color: var(--sidebar-active);
}
.chapter li a.active {
color: var(--sidebar-active);
}
.chapter li > a.toggle {
cursor: pointer;
display: block;
margin-inline-start: auto;
padding: 0 10px;
user-select: none;
opacity: 0.68;
}
.chapter li > a.toggle div {
transition: transform 0.5s;
}
/* collapse the section */
.chapter li:not(.expanded) + li > ol {
display: none;
}
.chapter li.chapter-item {
line-height: 1.5em;
margin-block-start: 0.6em;
}
.chapter li.expanded > a.toggle div {
transform: rotate(90deg);
}
.spacer {
width: 100%;
height: 3px;
margin: 5px 0px;
}
.chapter .spacer {
background-color: var(--sidebar-spacer);
}
@media (-moz-touch-enabled: 1), (pointer: coarse) {
.chapter li a { padding: 5px 0; }
.spacer { margin: 10px 0; }
}
.section {
list-style: none outside none;
padding-inline-start: 20px;
line-height: 1.9em;
}
/* Theme Menu Popup */
.theme-popup {
position: absolute;
left: 10px;
top: var(--menu-bar-height);
z-index: 1000;
border-radius: 4px;
font-size: 0.7em;
color: var(--fg);
background: var(--theme-popup-bg);
border: 1px solid var(--theme-popup-border);
margin: 0;
padding: 0;
list-style: none;
display: none;
/* Don't let the children's background extend past the rounded corners. */
overflow: hidden;
}
[dir=rtl] .theme-popup { left: unset; right: 10px; }
.theme-popup .default {
color: var(--icons);
}
.theme-popup .theme {
width: 100%;
border: 0;
margin: 0;
padding: 2px 20px;
line-height: 25px;
white-space: nowrap;
text-align: start;
cursor: pointer;
color: inherit;
background: inherit;
font-size: inherit;
}
.theme-popup .theme:hover {
background-color: var(--theme-hover);
}
.theme-selected::before {
display: inline-block;
content: "✓";
margin-inline-start: -14px;
width: 14px;
}

242
docs/book/css/general.css Normal file
View File

@@ -0,0 +1,242 @@
/* Base styles and content styles */
:root {
/* Browser default font-size is 16px, this way 1 rem = 10px */
font-size: 62.5%;
color-scheme: var(--color-scheme);
}
html {
font-family: "Open Sans", sans-serif;
color: var(--fg);
background-color: var(--bg);
text-size-adjust: none;
-webkit-text-size-adjust: none;
}
body {
margin: 0;
font-size: 1.6rem;
overflow-x: hidden;
}
code {
font-family: var(--mono-font) !important;
font-size: var(--code-font-size);
direction: ltr !important;
}
/* make long words/inline code not x overflow */
main {
overflow-wrap: break-word;
}
/* make wide tables scroll if they overflow */
.table-wrapper {
overflow-x: auto;
}
/* Don't change font size in headers. */
h1 code, h2 code, h3 code, h4 code, h5 code, h6 code {
font-size: unset;
}
.left { float: left; }
.right { float: right; }
.boring { opacity: 0.6; }
.hide-boring .boring { display: none; }
.hidden { display: none !important; }
h2, h3 { margin-block-start: 2.5em; }
h4, h5 { margin-block-start: 2em; }
.header + .header h3,
.header + .header h4,
.header + .header h5 {
margin-block-start: 1em;
}
h1:target::before,
h2:target::before,
h3:target::before,
h4:target::before,
h5:target::before,
h6:target::before {
display: inline-block;
content: "»";
margin-inline-start: -30px;
width: 30px;
}
/* This is broken on Safari as of version 14, but is fixed
in Safari Technology Preview 117 which I think will be Safari 14.2.
https://bugs.webkit.org/show_bug.cgi?id=218076
*/
:target {
/* Safari does not support logical properties */
scroll-margin-top: calc(var(--menu-bar-height) + 0.5em);
}
.page {
outline: 0;
padding: 0 var(--page-padding);
margin-block-start: calc(0px - var(--menu-bar-height)); /* Compensate for the #menu-bar-hover-placeholder */
}
.page-wrapper {
box-sizing: border-box;
background-color: var(--bg);
}
.no-js .page-wrapper,
.js:not(.sidebar-resizing) .page-wrapper {
transition: margin-left 0.3s ease, transform 0.3s ease; /* Animation: slide away */
}
[dir=rtl] .js:not(.sidebar-resizing) .page-wrapper {
transition: margin-right 0.3s ease, transform 0.3s ease; /* Animation: slide away */
}
.content {
overflow-y: auto;
padding: 0 5px 50px 5px;
}
.content main {
margin-inline-start: auto;
margin-inline-end: auto;
max-width: var(--content-max-width);
}
.content p { line-height: 1.45em; }
.content ol { line-height: 1.45em; }
.content ul { line-height: 1.45em; }
.content a { text-decoration: none; }
.content a:hover { text-decoration: underline; }
.content img, .content video { max-width: 100%; }
.content .header:link,
.content .header:visited {
color: var(--fg);
}
.content .header:link,
.content .header:visited:hover {
text-decoration: none;
}
table {
margin: 0 auto;
border-collapse: collapse;
}
table td {
padding: 3px 20px;
border: 1px var(--table-border-color) solid;
}
table thead {
background: var(--table-header-bg);
}
table thead td {
font-weight: 700;
border: none;
}
table thead th {
padding: 3px 20px;
}
table thead tr {
border: 1px var(--table-header-bg) solid;
}
/* Alternate background colors for rows */
table tbody tr:nth-child(2n) {
background: var(--table-alternate-bg);
}
blockquote {
margin: 20px 0;
padding: 0 20px;
color: var(--fg);
background-color: var(--quote-bg);
border-block-start: .1em solid var(--quote-border);
border-block-end: .1em solid var(--quote-border);
}
.warning {
margin: 20px;
padding: 0 20px;
border-inline-start: 2px solid var(--warning-border);
}
.warning:before {
position: absolute;
width: 3rem;
height: 3rem;
margin-inline-start: calc(-1.5rem - 21px);
content: "ⓘ";
text-align: center;
background-color: var(--bg);
color: var(--warning-border);
font-weight: bold;
font-size: 2rem;
}
blockquote .warning:before {
background-color: var(--quote-bg);
}
kbd {
background-color: var(--table-border-color);
border-radius: 4px;
border: solid 1px var(--theme-popup-border);
box-shadow: inset 0 -1px 0 var(--theme-hover);
display: inline-block;
font-size: var(--code-font-size);
font-family: var(--mono-font);
line-height: 10px;
padding: 4px 5px;
vertical-align: middle;
}
sup {
/* Set the line-height for superscript and footnote references so that there
isn't an awkward space appearing above lines that contain the footnote.
See https://github.com/rust-lang/mdBook/pull/2443#discussion_r1813773583
for an explanation.
*/
line-height: 0;
}
:not(.footnote-definition) + .footnote-definition,
.footnote-definition + :not(.footnote-definition) {
margin-block-start: 2em;
}
.footnote-definition {
font-size: 0.9em;
margin: 0.5em 0;
}
.footnote-definition p {
display: inline;
}
.tooltiptext {
position: absolute;
visibility: hidden;
color: #fff;
background-color: #333;
transform: translateX(-50%); /* Center by moving tooltip 50% of its width left */
left: -8px; /* Half of the width of the icon */
top: -35px;
font-size: 0.8em;
text-align: center;
border-radius: 6px;
padding: 5px 8px;
margin: 5px;
z-index: 1000;
}
.tooltipped .tooltiptext {
visibility: visible;
}
.chapter li.part-title {
color: var(--sidebar-fg);
margin: 5px 0px;
font-weight: bold;
}
.result-no-output {
font-style: italic;
}

50
docs/book/css/print.css Normal file
View File

@@ -0,0 +1,50 @@
#sidebar,
#menu-bar,
.nav-chapters,
.mobile-nav-chapters {
display: none;
}
#page-wrapper.page-wrapper {
transform: none !important;
margin-inline-start: 0px;
overflow-y: initial;
}
#content {
max-width: none;
margin: 0;
padding: 0;
}
.page {
overflow-y: initial;
}
code {
direction: ltr !important;
}
pre > .buttons {
z-index: 2;
}
a, a:visited, a:active, a:hover {
color: #4183c4;
text-decoration: none;
}
h1, h2, h3, h4, h5, h6 {
page-break-inside: avoid;
page-break-after: avoid;
}
pre, code {
page-break-inside: avoid;
white-space: pre-wrap;
}
.fa {
display: none !important;
}

309
docs/book/css/variables.css Normal file
View File

@@ -0,0 +1,309 @@
/* Globals */
:root {
--sidebar-width: 300px;
--sidebar-resize-indicator-width: 8px;
--sidebar-resize-indicator-space: 2px;
--page-padding: 15px;
--content-max-width: 750px;
--menu-bar-height: 50px;
--mono-font: "Source Code Pro", Consolas, "Ubuntu Mono", Menlo, "DejaVu Sans Mono", monospace, monospace;
--code-font-size: 0.875em /* please adjust the ace font size accordingly in editor.js */
}
/* Themes */
.ayu {
--bg: hsl(210, 25%, 8%);
--fg: #c5c5c5;
--sidebar-bg: #14191f;
--sidebar-fg: #c8c9db;
--sidebar-non-existant: #5c6773;
--sidebar-active: #ffb454;
--sidebar-spacer: #2d334f;
--scrollbar: var(--sidebar-fg);
--icons: #737480;
--icons-hover: #b7b9cc;
--links: #0096cf;
--inline-code-color: #ffb454;
--theme-popup-bg: #14191f;
--theme-popup-border: #5c6773;
--theme-hover: #191f26;
--quote-bg: hsl(226, 15%, 17%);
--quote-border: hsl(226, 15%, 22%);
--warning-border: #ff8e00;
--table-border-color: hsl(210, 25%, 13%);
--table-header-bg: hsl(210, 25%, 28%);
--table-alternate-bg: hsl(210, 25%, 11%);
--searchbar-border-color: #848484;
--searchbar-bg: #424242;
--searchbar-fg: #fff;
--searchbar-shadow-color: #d4c89f;
--searchresults-header-fg: #666;
--searchresults-border-color: #888;
--searchresults-li-bg: #252932;
--search-mark-bg: #e3b171;
--color-scheme: dark;
/* Same as `--icons` */
--copy-button-filter: invert(45%) sepia(6%) saturate(621%) hue-rotate(198deg) brightness(99%) contrast(85%);
/* Same as `--sidebar-active` */
--copy-button-filter-hover: invert(68%) sepia(55%) saturate(531%) hue-rotate(341deg) brightness(104%) contrast(101%);
}
.coal {
--bg: hsl(200, 7%, 8%);
--fg: #98a3ad;
--sidebar-bg: #292c2f;
--sidebar-fg: #a1adb8;
--sidebar-non-existant: #505254;
--sidebar-active: #3473ad;
--sidebar-spacer: #393939;
--scrollbar: var(--sidebar-fg);
--icons: #43484d;
--icons-hover: #b3c0cc;
--links: #2b79a2;
--inline-code-color: #c5c8c6;
--theme-popup-bg: #141617;
--theme-popup-border: #43484d;
--theme-hover: #1f2124;
--quote-bg: hsl(234, 21%, 18%);
--quote-border: hsl(234, 21%, 23%);
--warning-border: #ff8e00;
--table-border-color: hsl(200, 7%, 13%);
--table-header-bg: hsl(200, 7%, 28%);
--table-alternate-bg: hsl(200, 7%, 11%);
--searchbar-border-color: #aaa;
--searchbar-bg: #b7b7b7;
--searchbar-fg: #000;
--searchbar-shadow-color: #aaa;
--searchresults-header-fg: #666;
--searchresults-border-color: #98a3ad;
--searchresults-li-bg: #2b2b2f;
--search-mark-bg: #355c7d;
--color-scheme: dark;
/* Same as `--icons` */
--copy-button-filter: invert(26%) sepia(8%) saturate(575%) hue-rotate(169deg) brightness(87%) contrast(82%);
/* Same as `--sidebar-active` */
--copy-button-filter-hover: invert(36%) sepia(70%) saturate(503%) hue-rotate(167deg) brightness(98%) contrast(89%);
}
.light, html:not(.js) {
--bg: hsl(0, 0%, 100%);
--fg: hsl(0, 0%, 0%);
--sidebar-bg: #fafafa;
--sidebar-fg: hsl(0, 0%, 0%);
--sidebar-non-existant: #aaaaaa;
--sidebar-active: #1f1fff;
--sidebar-spacer: #f4f4f4;
--scrollbar: #8F8F8F;
--icons: #747474;
--icons-hover: #000000;
--links: #20609f;
--inline-code-color: #301900;
--theme-popup-bg: #fafafa;
--theme-popup-border: #cccccc;
--theme-hover: #e6e6e6;
--quote-bg: hsl(197, 37%, 96%);
--quote-border: hsl(197, 37%, 91%);
--warning-border: #ff8e00;
--table-border-color: hsl(0, 0%, 95%);
--table-header-bg: hsl(0, 0%, 80%);
--table-alternate-bg: hsl(0, 0%, 97%);
--searchbar-border-color: #aaa;
--searchbar-bg: #fafafa;
--searchbar-fg: #000;
--searchbar-shadow-color: #aaa;
--searchresults-header-fg: #666;
--searchresults-border-color: #888;
--searchresults-li-bg: #e4f2fe;
--search-mark-bg: #a2cff5;
--color-scheme: light;
/* Same as `--icons` */
--copy-button-filter: invert(45.49%);
/* Same as `--sidebar-active` */
--copy-button-filter-hover: invert(14%) sepia(93%) saturate(4250%) hue-rotate(243deg) brightness(99%) contrast(130%);
}
.navy {
--bg: hsl(226, 23%, 11%);
--fg: #bcbdd0;
--sidebar-bg: #282d3f;
--sidebar-fg: #c8c9db;
--sidebar-non-existant: #505274;
--sidebar-active: #2b79a2;
--sidebar-spacer: #2d334f;
--scrollbar: var(--sidebar-fg);
--icons: #737480;
--icons-hover: #b7b9cc;
--links: #2b79a2;
--inline-code-color: #c5c8c6;
--theme-popup-bg: #161923;
--theme-popup-border: #737480;
--theme-hover: #282e40;
--quote-bg: hsl(226, 15%, 17%);
--quote-border: hsl(226, 15%, 22%);
--warning-border: #ff8e00;
--table-border-color: hsl(226, 23%, 16%);
--table-header-bg: hsl(226, 23%, 31%);
--table-alternate-bg: hsl(226, 23%, 14%);
--searchbar-border-color: #aaa;
--searchbar-bg: #aeaec6;
--searchbar-fg: #000;
--searchbar-shadow-color: #aaa;
--searchresults-header-fg: #5f5f71;
--searchresults-border-color: #5c5c68;
--searchresults-li-bg: #242430;
--search-mark-bg: #a2cff5;
--color-scheme: dark;
/* Same as `--icons` */
--copy-button-filter: invert(51%) sepia(10%) saturate(393%) hue-rotate(198deg) brightness(86%) contrast(87%);
/* Same as `--sidebar-active` */
--copy-button-filter-hover: invert(46%) sepia(20%) saturate(1537%) hue-rotate(156deg) brightness(85%) contrast(90%);
}
.rust {
--bg: hsl(60, 9%, 87%);
--fg: #262625;
--sidebar-bg: #3b2e2a;
--sidebar-fg: #c8c9db;
--sidebar-non-existant: #505254;
--sidebar-active: #e69f67;
--sidebar-spacer: #45373a;
--scrollbar: var(--sidebar-fg);
--icons: #737480;
--icons-hover: #262625;
--links: #2b79a2;
--inline-code-color: #6e6b5e;
--theme-popup-bg: #e1e1db;
--theme-popup-border: #b38f6b;
--theme-hover: #99908a;
--quote-bg: hsl(60, 5%, 75%);
--quote-border: hsl(60, 5%, 70%);
--warning-border: #ff8e00;
--table-border-color: hsl(60, 9%, 82%);
--table-header-bg: #b3a497;
--table-alternate-bg: hsl(60, 9%, 84%);
--searchbar-border-color: #aaa;
--searchbar-bg: #fafafa;
--searchbar-fg: #000;
--searchbar-shadow-color: #aaa;
--searchresults-header-fg: #666;
--searchresults-border-color: #888;
--searchresults-li-bg: #dec2a2;
--search-mark-bg: #e69f67;
/* Same as `--icons` */
--copy-button-filter: invert(51%) sepia(10%) saturate(393%) hue-rotate(198deg) brightness(86%) contrast(87%);
/* Same as `--sidebar-active` */
--copy-button-filter-hover: invert(77%) sepia(16%) saturate(1798%) hue-rotate(328deg) brightness(98%) contrast(83%);
}
@media (prefers-color-scheme: dark) {
html:not(.js) {
--bg: hsl(200, 7%, 8%);
--fg: #98a3ad;
--sidebar-bg: #292c2f;
--sidebar-fg: #a1adb8;
--sidebar-non-existant: #505254;
--sidebar-active: #3473ad;
--sidebar-spacer: #393939;
--scrollbar: var(--sidebar-fg);
--icons: #43484d;
--icons-hover: #b3c0cc;
--links: #2b79a2;
--inline-code-color: #c5c8c6;
--theme-popup-bg: #141617;
--theme-popup-border: #43484d;
--theme-hover: #1f2124;
--quote-bg: hsl(234, 21%, 18%);
--quote-border: hsl(234, 21%, 23%);
--warning-border: #ff8e00;
--table-border-color: hsl(200, 7%, 13%);
--table-header-bg: hsl(200, 7%, 28%);
--table-alternate-bg: hsl(200, 7%, 11%);
--searchbar-border-color: #aaa;
--searchbar-bg: #b7b7b7;
--searchbar-fg: #000;
--searchbar-shadow-color: #aaa;
--searchresults-header-fg: #666;
--searchresults-border-color: #98a3ad;
--searchresults-li-bg: #2b2b2f;
--search-mark-bg: #355c7d;
--color-scheme: dark;
/* Same as `--icons` */
--copy-button-filter: invert(26%) sepia(8%) saturate(575%) hue-rotate(169deg) brightness(87%) contrast(82%);
/* Same as `--sidebar-active` */
--copy-button-filter-hover: invert(36%) sepia(70%) saturate(503%) hue-rotate(167deg) brightness(98%) contrast(89%);
}
}

10
docs/book/elasticlunr.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,808 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Baker Family Complete - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="baker-family-complete"><a class="header" href="#baker-family-complete">Baker Family Complete</a></h1>
<p>This example demonstrates a complete Storybook project modeling Marthas bakery and the people around it. It showcases all major language features working together.</p>
<h2 id="project-structure"><a class="header" href="#project-structure">Project Structure</a></h2>
<pre><code>baker-family/
schema/
core_enums.sb # Enum definitions
templates.sb # Reusable templates
beings.sb # Species definitions
world/
characters/
martha.sb # Martha, master baker
jane.sb # Jane, pastry specialist
elena.sb # Elena, apprentice
gregory.sb # Gregory, regular customer
family.sb # David, Tommy, Emma
behaviors/
bakery_behaviors.sb
relationships/
bakery_relationships.sb
locations/
bakery_places.sb
institutions/
bakery_institutions.sb
schedules/
bakery_schedules.sb
</code></pre>
<h2 id="schema-layer"><a class="header" href="#schema-layer">Schema Layer</a></h2>
<h3 id="enums"><a class="header" href="#enums">Enums</a></h3>
<pre><code class="language-storybook">// schema/core_enums.sb
enum SkillLevel {
novice,
beginner,
intermediate,
advanced,
expert,
master
}
enum Specialty {
sourdough,
pastries,
cakes,
general,
bread
}
enum Confidence {
timid,
uncertain,
growing,
steady,
confident,
commanding
}
enum DayPart {
early_morning,
morning,
midday,
afternoon,
evening,
night
}
</code></pre>
<h3 id="species"><a class="header" href="#species">Species</a></h3>
<pre><code class="language-storybook">// schema/beings.sb
species Human {
lifespan: 80
---description
Bipedal mammals with complex language and tool use.
---
}
species Cat {
lifespan: 15
---description
Domestic cats often found in bakeries for pest control
and companionship.
---
}
</code></pre>
<h3 id="templates"><a class="header" href="#templates">Templates</a></h3>
<pre><code class="language-storybook">// schema/templates.sb
template SkilledWorker {
skill_level: SkillLevel
confidence: Confidence
years_experience: 0..50
can_work_independently: false
}
template Baker {
include SkilledWorker
specialty: Specialty
recipes_mastered: 0..200
sourdough_starter_health: 0.0..1.0
}
template BusinessOwner {
include SkilledWorker
revenue_monthly: 0..100000
employees: 0..20
years_in_business: 0..50
}
template Apprentice {
include SkilledWorker
skill_level: novice
confidence: timid
mentor: string
dedication: 0.0..1.0
}
</code></pre>
<h2 id="characters"><a class="header" href="#characters">Characters</a></h2>
<h3 id="martha"><a class="header" href="#martha">Martha</a></h3>
<pre><code class="language-storybook">// world/characters/martha.sb
use schema::core_enums::{SkillLevel, Specialty, Confidence};
use schema::templates::{Baker, BusinessOwner};
use schema::beings::Human;
character Martha: Human from Baker, BusinessOwner {
uses behaviors: [
{ tree: BakerMorningRoutine },
{ tree: HandleEmergency, when: emergency_detected, priority: critical }
]
age: 34
specialty: sourdough
skill_level: master
confidence: commanding
recipes_mastered: 85
years_experience: 22
sourdough_starter_health: 0.95
revenue_monthly: 12000
employees: 3
years_in_business: 8
---backstory
Martha learned to bake from her grandmother, starting at age twelve.
By twenty she had won regional competitions. At twenty-six she opened
her own bakery, which quickly became the most popular in town. Her
sourdough is legendary -- she maintains a starter that is fifteen
years old.
---
}
life_arc MarthaCareerArc {
---description
Tracks Martha's evolution from established baker to community leader.
---
state running_bakery {
on enter {
Martha.confidence: commanding
Martha.skill_level: master
}
on employees &gt; 5 -&gt; expanding
}
state expanding {
on enter {
Martha.revenue_monthly: 20000
}
on second_location_opened -&gt; community_leader
}
state community_leader {
on enter {
Martha.can_teach: true
Martha.mentors_count: 3
}
---narrative
Martha's bakery has become a training ground for the next
generation of bakers. She sits on the guild board and her
sourdough recipe is studied at culinary schools.
---
}
}
</code></pre>
<h3 id="jane"><a class="header" href="#jane">Jane</a></h3>
<pre><code class="language-storybook">// world/characters/jane.sb
use schema::core_enums::{SkillLevel, Specialty, Confidence};
use schema::templates::Baker;
use schema::beings::Human;
character Jane: Human from Baker {
age: 36
specialty: pastries
skill_level: expert
confidence: confident
recipes_mastered: 120
years_experience: 18
can_work_independently: true
---backstory
Jane trained at a prestigious culinary school before joining
Martha's bakery as co-owner. Where Martha excels at bread, Jane
is a pastry artist. Her croissants draw customers from three
towns over.
---
---appearance
A focused woman with flour-dusted apron and steady hands.
Known for her intricate pastry decorations and precise
temperature control.
---
}
</code></pre>
<h3 id="elena"><a class="header" href="#elena">Elena</a></h3>
<pre><code class="language-storybook">// world/characters/elena.sb
use schema::core_enums::{SkillLevel, Confidence};
use schema::templates::Apprentice;
use schema::beings::Human;
character Elena: Human from Apprentice {
age: 16
skill_level: novice
confidence: timid
mentor: "Martha"
dedication: 0.9
natural_talent: 0.8
recipes_mastered: 2
---backstory
Elena comes from a family of farmers who could never afford to
buy bread from the bakery. When Martha offered her an apprenticeship,
she jumped at the chance to learn a trade.
---
}
life_arc ElenaCareer {
---description
Tracks Elena's progression from nervous apprentice to confident
master baker. Each state represents a key phase of her career.
---
state early_apprentice {
on enter {
Elena.skill_level: novice
Elena.confidence: timid
}
on recipes_mastered &gt; 5 -&gt; growing_apprentice
---narrative
Elena's hands shake as she measures flour. She checks the
recipe three times before adding each ingredient. Martha
patiently corrects her technique.
---
}
state growing_apprentice {
on enter {
Elena.skill_level: beginner
Elena.confidence: uncertain
}
on recipes_mastered &gt; 15 -&gt; journeyman
---narrative
The shaking stops. Elena can make basic breads without
looking at the recipe. She still doubts herself but
Martha's encouragement is taking root.
---
}
state journeyman {
on enter {
Elena.skill_level: intermediate
Elena.confidence: growing
Elena.can_work_independently: true
}
on recipes_mastered &gt; 30 -&gt; senior_journeyman
---narrative
Elena runs the morning shift alone while Martha handles
special orders. Customers start asking for "Elena's rolls."
She begins experimenting with her own recipes.
---
}
state senior_journeyman {
on enter {
Elena.skill_level: advanced
Elena.confidence: steady
}
on recipes_mastered &gt; 50 -&gt; master
---narrative
Elena develops her signature recipe: rosemary olive bread
that becomes the bakery's bestseller. She handles difficult
customers with grace and trains new helpers.
---
}
state master {
on enter {
Elena.skill_level: master
Elena.confidence: commanding
Elena.can_teach: true
}
---narrative
Master Baker Elena. She has earned it. The guild acknowledges
her mastery, and Martha beams with pride. Elena begins
mentoring her own apprentice.
---
}
}
</code></pre>
<h3 id="gregory"><a class="header" href="#gregory">Gregory</a></h3>
<pre><code class="language-storybook">// world/characters/gregory.sb
use schema::beings::Human;
character Gregory: Human {
age: 68
occupation: "retired_teacher"
always_orders: "sourdough_loaf"
visits_daily: true
years_as_customer: 15
knows_everyone: true
---backstory
Gregory has been buying Martha's bread every morning for
fifteen years. Their brief daily exchanges about the weather
and local gossip are a comforting routine for both of them.
---
}
</code></pre>
<h2 id="behaviors"><a class="header" href="#behaviors">Behaviors</a></h2>
<pre><code class="language-storybook">// world/behaviors/bakery_behaviors.sb
behavior BakerMorningRoutine {
---description
Martha's morning routine: prepare dough step by step,
from mixing to shaping to baking.
---
then morning_baking {
// Start with sourdough
then prepare_starter {
CheckStarter
FeedStarter
WaitForActivity
}
// Mix the dough
then mix_dough {
MeasureFlour
AddWater
IncorporateStarter
}
// Knead and shape
then shape_loaves {
KneadDough
FirstRise
ShapeLoaves
}
// Bake
then bake {
PreHeatOven
LoadLoaves
MonitorBaking
}
}
}
behavior CustomerServiceLoop {
---description
The bakery's continuous customer service loop. Uses infinite
repeat decorator to serve customers throughout the day.
---
repeat {
then service_cycle {
// Check for customers
choose service_mode {
then serve_waiting {
if(customer_waiting)
GreetCustomer
TakeOrder
}
then restock_display {
if(display_low)
FetchFromKitchen
ArrangeOnShelves
}
}
// Process payment
CollectPayment
ThankCustomer
// Brief pause between customers
timeout(5s) {
CleanCounter
}
PrepareForNextCustomer
}
}
}
</code></pre>
<h2 id="relationships"><a class="header" href="#relationships">Relationships</a></h2>
<pre><code class="language-storybook">// world/relationships/bakery_relationships.sb
relationship MarthaAndGregory {
Martha {
role: shopkeeper
values_loyalty: 0.9
---perspective
Martha appreciates Gregory's unwavering loyalty. He has
been buying her sourdough loaf every morning for fifteen
years. Their brief daily exchanges about the weather and
local gossip are a comforting routine.
---
}
Gregory {
role: regular_customer
always_orders: "sourdough_loaf"
---perspective
Gregory considers Martha's bakery a cornerstone of his
daily routine. The bread is excellent, but it is the brief
human connection that keeps him coming back.
---
}
}
relationship MentorApprentice {
Martha {
role: mentor
teaching_style: "patient"
investment: 0.9
---perspective
Martha sees Elena as the daughter she might have had in
the trade. She recognizes the same passion she felt at
that age and pushes Elena harder because she knows the
talent is there. Every correction comes from love.
---
}
Elena {
role: apprentice
dedication: 0.9
anxiety: 0.4
---perspective
Elena idolizes Martha's skill but fears disappointing
her. Every morning she arrives thirty minutes early to
practice techniques before Martha gets in. She keeps a
notebook of every correction, reviewing them each night.
---
}
bond: 0.85
}
relationship BakeryPartnership {
Martha {
role: co_owner
specialty: "bread"
handles_finances: true
---perspective
Martha and Jane complement each other perfectly. Martha
handles the bread and business side while Jane creates
the pastries that draw customers in. Together they have
built something neither could alone.
---
}
Jane {
role: co_owner
specialty: "pastries"
handles_creativity: true
---perspective
Jane considers Martha the steady foundation of their
partnership. While Jane experiments and creates, Martha
ensures the bakery runs like clockwork. Their different
strengths make the bakery stronger.
---
}
bond: 0.9
}
</code></pre>
<h2 id="locations"><a class="header" href="#locations">Locations</a></h2>
<pre><code class="language-storybook">// world/locations/bakery_places.sb
location MarthasBakery {
type: "commercial"
established: "2018"
square_feet: 1200
has_kitchen: true
has_storefront: true
seating_capacity: 12
---description
A warm, inviting bakery on Main Street. The aroma of fresh
bread wafts out the door every morning at 4 AM. Exposed brick
walls, a glass display case, and a view into the kitchen where
customers can watch the bakers at work.
---
}
location FarmersMarket {
type: "outdoor_market"
operates_on: "saturday"
stalls: 30
foot_traffic: "high"
---description
The weekly Saturday market where Martha sells her bread directly
to the community. Her stall is always the first to sell out.
---
}
location BakeryKitchen {
type: "commercial_kitchen"
ovens: 3
prep_stations: 4
walk_in_cooler: true
---description
The heart of the bakery. Three professional ovens line the back
wall, each at a different temperature for different breads. The
sourdough starter sits on a shelf near the warmest oven, bubbling
contentedly in its ceramic crock.
---
}
</code></pre>
<h2 id="institutions"><a class="header" href="#institutions">Institutions</a></h2>
<pre><code class="language-storybook">// world/institutions/bakery_institutions.sb
institution BakersGuild {
type: professional_guild
members: 45
founded: "1952"
meets_monthly: true
---description
The local bakers' guild that sets quality standards, organizes
competitions, and mentors apprentices. Martha has been a board
member for three years.
---
}
</code></pre>
<h2 id="key-takeaways"><a class="header" href="#key-takeaways">Key Takeaways</a></h2>
<p>This example demonstrates:</p>
<ol>
<li><strong>Layered architecture</strong>: Schema (types) separated from world (instances)</li>
<li><strong>Species + Templates</strong>: <code>Human</code> species combined with <code>Baker</code> and <code>BusinessOwner</code> templates</li>
<li><strong>Rich behavior trees</strong>: Morning routine and customer service with choose, then, and repeat</li>
<li><strong>Asymmetric relationships</strong>: Martha and Elena see their mentorship differently</li>
<li><strong>Life arcs</strong>: Elenas career journey modeled as a state machine</li>
<li><strong>Prose everywhere</strong>: Every declaration includes narrative context</li>
</ol>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="../tutorial/01-welcome.html">Tutorial</a> - Step-by-step learning</li>
<li><a href="../reference/10-characters.html">Characters Reference</a> - Character syntax</li>
<li><a href="../reference/11-behavior-trees.html">Behavior Trees Reference</a> - Behavior syntax</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../advanced/23-best-practices.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/25-day-in-life.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../advanced/23-best-practices.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/25-day-in-life.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,537 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Day in the Life - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="day-in-the-life"><a class="header" href="#day-in-the-life">Day in the Life</a></h1>
<p>This example models a complete day for a baker character, showing how schedules, behaviors, and life arcs work together to create a rich daily simulation.</p>
<h2 id="the-baker"><a class="header" href="#the-baker">The Baker</a></h2>
<pre><code class="language-storybook">use schema::core_enums::{Season, DayOfWeek};
use schema::beings::Human;
use schema::templates::SkilledWorker;
character Martha: Human from SkilledWorker {
age: 42
occupation: "Master Baker"
skill_level: 0.95
energy: 1.0
mood: 0.8
uses schedule: MarthaDailySchedule
uses behaviors: [
{ tree: BakerRoutine, priority: normal },
{ tree: HandleEmergency, when: emergency_detected, priority: critical }
]
---backstory
Martha has been baking since she was twelve, learning from her
grandmother. She now runs the most popular bakery in town and
is known for her sourdough bread and apple pastries.
---
}
</code></pre>
<h2 id="the-schedule"><a class="header" href="#the-schedule">The Schedule</a></h2>
<pre><code class="language-storybook">schedule MarthaDailySchedule {
block wake_up {
04:00 - 04:30
action: routines::morning_wake
}
block early_baking {
04:30 - 07:00
action: baking::prepare_morning_goods
}
block open_shop {
07:00 - 07:15
action: shop::open_for_business
}
block morning_rush {
07:15 - 10:00
action: shop::serve_morning_customers
}
block midday_baking {
10:00 - 12:00
action: baking::prepare_afternoon_goods
}
block lunch_break {
12:00 - 13:00
action: social::lunch_with_family
}
block afternoon_sales {
13:00 - 16:00
action: shop::serve_afternoon_customers
}
block close_shop {
16:00 - 16:30
action: shop::close_for_day
}
block evening_prep {
16:30 - 17:30
action: baking::prepare_dough_for_tomorrow
}
block family_time {
18:00 - 21:00
action: social::family_evening
}
block sleep {
21:00 - 04:00
action: routines::sleep
}
// Saturday: Market day
recurs MarketDay on day saturday {
block market_prep {
03:00 - 05:00
action: baking::market_batch
}
block market_sales {
06:00 - 14:00
action: market::sell_at_stall
}
block market_cleanup {
14:00 - 15:00
action: market::pack_up
}
}
// Summer: Extended hours
block summer_afternoon {
13:00 - 18:00
action: shop::extended_summer_hours
on season summer
}
}
</code></pre>
<h2 id="behaviors"><a class="header" href="#behaviors">Behaviors</a></h2>
<h3 id="morning-routine"><a class="header" href="#morning-routine">Morning Routine</a></h3>
<pre><code class="language-storybook">behavior BakerMorningRoutine {
then morning_sequence {
WakeUp
WashFace
DressInWorkClothes
// Check the sourdough starter
then check_starter {
ExamineStarter
if(starter_healthy) {
FeedStarter
}
}
WalkToKitchen
LightOven
}
}
</code></pre>
<h3 id="baking-behavior"><a class="header" href="#baking-behavior">Baking Behavior</a></h3>
<pre><code class="language-storybook">behavior BakerRoutine {
choose baking_priority {
// Handle special orders first
then special_orders {
if(has_special_orders)
then fill_order {
ReviewOrderDetails
GatherSpecialIngredients
PrepareSpecialItem
PackageForCustomer
}
}
// Regular daily baking
then daily_bread {
then sourdough {
MixDough(recipe: "sourdough", quantity: 10)
KneadDough(duration: 15m)
FirstRise(duration: 2h)
ShapLoaves
SecondRise(duration: 1h)
BakeLoaves(temperature: 230, duration: 35m)
}
}
// Pastries if time permits
then pastries {
succeed_always {
then apple_pastries {
PrepareFillingApple
RollPastryDough
AssemblePastries
BakePastries(temperature: 200, duration: 25m)
}
}
}
}
}
</code></pre>
<h3 id="customer-service"><a class="header" href="#customer-service">Customer Service</a></h3>
<pre><code class="language-storybook">behavior ServeCustomer {
then service_sequence {
GreetCustomer
if(customer_is_regular) {
RecallPreferences
}
choose service_type {
then take_order {
if(customer_knows_what_they_want)
AcceptOrder
PackageItem
}
then help_decide {
if(not customer_knows_what_they_want)
OfferRecommendation
ProvidesSample
AcceptOrder
PackageItem
}
}
CollectPayment
ThankCustomer
}
}
</code></pre>
<h3 id="emergency-handling"><a class="header" href="#emergency-handling">Emergency Handling</a></h3>
<pre><code class="language-storybook">behavior HandleEmergency {
choose emergency_type {
then oven_fire {
if(oven_overheating)
TurnOffOven
GrabFireExtinguisher
ExtinguishFire
AssessDamage
}
then ingredient_shortage {
if(critical_ingredient_missing)
CheckBackupSupply
choose procurement {
SendApprenticeToMarket
SubstituteIngredient
AdjustMenu
}
}
then equipment_failure {
if(equipment_broken)
StopProduction
AttemptQuickFix
choose fallback {
UseBackupEquipment
CallRepairPerson
}
}
}
}
</code></pre>
<h2 id="life-arc-career-and-energy"><a class="header" href="#life-arc-career-and-energy">Life Arc: Career and Energy</a></h2>
<pre><code class="language-storybook">life_arc MarthaEnergyLevel {
state rested {
on enter {
Martha.energy: 1.0
Martha.mood: 0.8
}
on energy &lt; 0.5 -&gt; tired
}
state tired {
on enter {
Martha.mood: 0.6
}
on energy &lt; 0.2 -&gt; exhausted
on energy &gt; 0.7 -&gt; rested
}
state exhausted {
on enter {
Martha.mood: 0.3
Martha.quality_output: 0.7
}
on energy &gt; 0.5 -&gt; tired
}
}
</code></pre>
<h2 id="relationships"><a class="header" href="#relationships">Relationships</a></h2>
<pre><code class="language-storybook">relationship MarthaAndApprentice {
Martha as mentor self {
patience: 0.8
investment: 0.9
} other {
sees_potential: 0.85
}
Elena as apprentice self {
dedication: 0.9
learning_rate: 0.7
} other {
respect: 0.95
admiration: 0.8
}
bond: 0.85
years_together: 2
}
relationship MarthaAndRegularCustomer {
Martha as shopkeeper
OldManGregory as regular_customer
bond: 0.7
years_known: 15
always_orders: "sourdough_loaf"
---dynamics
Gregory has been buying Martha's bread every morning for
fifteen years. They exchange brief pleasantries about the
weather and local gossip. He is her most reliable customer.
---
}
</code></pre>
<h2 id="key-takeaways"><a class="header" href="#key-takeaways">Key Takeaways</a></h2>
<p>This example demonstrates:</p>
<ol>
<li><strong>Schedule-driven daily flow</strong>: Precise time blocks govern Marthas entire day</li>
<li><strong>Seasonal and weekly variations</strong>: Summer hours and Saturday market</li>
<li><strong>Layered behaviors</strong>: Emergency behavior preempts normal routine via priority</li>
<li><strong>Realistic action sequences</strong>: Baking modeled step by step with parameters</li>
<li><strong>Energy management</strong>: Life arc tracks fatigue affecting mood and output quality</li>
<li><strong>Social connections</strong>: Relationships with apprentice and customers add depth</li>
</ol>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="../reference/14-schedules.html">Schedules Reference</a> - Schedule syntax</li>
<li><a href="../reference/11-behavior-trees.html">Behavior Trees Reference</a> - Behavior syntax</li>
<li><a href="../reference/13-life-arcs.html">Life Arcs Reference</a> - Life arc syntax</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../examples/24-baker-family-complete.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/26-character-evolution.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../examples/24-baker-family-complete.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/26-character-evolution.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,552 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Character Evolution - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="character-evolution"><a class="header" href="#character-evolution">Character Evolution</a></h1>
<p>This example models a character who evolves through multiple life stages, demonstrating how life arcs, behavior trees, and templates work together to represent growth over time.</p>
<h2 id="the-apprentices-journey"><a class="header" href="#the-apprentices-journey">The Apprentices Journey</a></h2>
<p>Elena starts as a nervous apprentice and grows into a confident master baker. Her evolution touches every aspect of her character: skills, personality, relationships, and daily routine.</p>
<h3 id="schema"><a class="header" href="#schema">Schema</a></h3>
<pre><code class="language-storybook">enum SkillLevel { novice, beginner, intermediate, advanced, expert, master }
enum Confidence { timid, uncertain, growing, steady, confident, commanding }
template Apprentice {
skill_level: novice
confidence: timid
can_work_independently: false
recipes_mastered: 0..5
}
template Journeyman {
skill_level: intermediate
confidence: growing
can_work_independently: true
recipes_mastered: 10..30
}
template MasterBaker {
skill_level: master
confidence: commanding
can_work_independently: true
can_teach: true
recipes_mastered: 50..200
}
</code></pre>
<h3 id="the-character-at-different-stages"><a class="header" href="#the-character-at-different-stages">The Character at Different Stages</a></h3>
<pre><code class="language-storybook">// Elena starts as an apprentice
character Elena: Human from Apprentice {
age: 16
natural_talent: 0.8
dedication: 0.9
recipes_mastered: 2
confidence: timid
mentor: Martha
---backstory
Elena comes from a family of farmers who could never afford to
buy bread from the bakery. When Martha offered her an apprenticeship,
she jumped at the chance to learn a trade.
---
}
</code></pre>
<h3 id="the-evolution-life-arc"><a class="header" href="#the-evolution-life-arc">The Evolution Life Arc</a></h3>
<pre><code class="language-storybook">life_arc ElenaCareer {
---description
Tracks Elena's progression from nervous apprentice to
confident master baker over several years.
---
state apprentice_early {
on enter {
Elena.skill_level: novice
Elena.confidence: timid
Elena.can_work_independently: false
}
on recipes_mastered &gt; 5 -&gt; apprentice_growing
---narrative
Elena's hands shake as she measures flour. She checks the
recipe three times before adding each ingredient. Martha
patiently corrects her technique.
---
}
state apprentice_growing {
on enter {
Elena.skill_level: beginner
Elena.confidence: uncertain
}
on recipes_mastered &gt; 15 -&gt; journeyman
---narrative
The shaking stops. Elena can make basic breads without
looking at the recipe. She still doubts herself but
Martha's encouragement is taking root.
---
}
state journeyman {
on enter {
Elena.skill_level: intermediate
Elena.confidence: growing
Elena.can_work_independently: true
}
on recipes_mastered &gt; 30 and confidence is steady -&gt; senior_journeyman
---narrative
Elena runs the morning shift alone while Martha handles
special orders. Customers start asking for "Elena's rolls."
She begins experimenting with her own recipes.
---
}
state senior_journeyman {
on enter {
Elena.skill_level: advanced
Elena.confidence: steady
}
on recipes_mastered &gt; 50 and passed_master_trial -&gt; master
---narrative
Elena develops her signature recipe: rosemary olive bread
that becomes the bakery's bestseller. She handles difficult
customers with grace and trains new helpers.
---
}
state master {
on enter {
Elena.skill_level: master
Elena.confidence: commanding
Elena.can_teach: true
}
---narrative
Master Baker Elena. She has earned it. The guild acknowledges
her mastery, and Martha beams with pride. Elena begins
mentoring her own apprentice.
---
}
}
</code></pre>
<h3 id="evolving-behaviors"><a class="header" href="#evolving-behaviors">Evolving Behaviors</a></h3>
<p>Elenas behavior changes as she progresses:</p>
<pre><code class="language-storybook">// Early apprentice: hesitant, checks everything
behavior Elena_ApprenticeEarly {
then cautious_baking {
CheckRecipeThreeTimes
MeasureCarefully
AskMarthaForConfirmation
ProceedSlowly
CheckResultAnxiously
}
}
// Growing apprentice: more confident
behavior Elena_ApprenticeGrowing {
then competent_baking {
ReviewRecipe
MeasureIngredients
MixWithConfidence
choose problem_handling {
then handle_alone {
if(confidence &gt; 0.4)
AssessSituation
ApplyLearning
}
AskMarthaForHelp
}
}
}
// Journeyman: independent and creative
behavior Elena_Journeyman {
choose work_mode {
then creative_mode {
if(inspiration_high)
ExperimentWithRecipe
TasteTest
if(result_good) {
RecordNewRecipe
}
}
then production_mode {
ExecuteRecipeFromMemory
MonitorOvenTimings
ManageMultipleBatches
}
then teaching_mode {
if(helper_present)
DemonstrateTeechnique
ObserveHelper
ProvideGentleFeedback
}
}
}
// Master: leadership and mentoring
behavior Elena_Master {
choose master_activity {
then mentor_apprentice {
if(apprentice_needs_guidance)
AssessApprenticeProgress
DesignLearningChallenge
ObserveAndFeedback
}
then innovate {
if(creative_energy_high)
ResearchNewTechniques
ExperimentWithIngredients
DocumentFindings
}
then lead_production {
PlanDailyProduction
DelegateToTeam
QualityCheckResults
}
}
}
</code></pre>
<h3 id="evolving-relationships"><a class="header" href="#evolving-relationships">Evolving Relationships</a></h3>
<p>The mentor relationship changes as Elena grows:</p>
<pre><code class="language-storybook">// Early apprenticeship
relationship EarlyMentorship {
Martha as mentor self {
patience: 0.9
teaching_intensity: 0.8
} other {
sees_potential: 0.8
reminds_her_of_herself: true
}
Elena as apprentice self {
gratitude: 1.0
anxiety: 0.7
} other {
admiration: 0.95
intimidated: 0.5
}
bond: 0.6
}
// Later: colleagues and friends
relationship MaturePartnership {
Martha as senior_partner self {
pride_in_elena: 0.95
ready_to_step_back: 0.6
} other {
sees_equal: 0.8
trusts_judgment: 0.9
}
Elena as junior_partner self {
confidence: 0.85
gratitude: 0.9
} other {
respect: 0.95
sees_as_mother_figure: 0.7
}
bond: 0.95
}
</code></pre>
<h3 id="evolving-schedules"><a class="header" href="#evolving-schedules">Evolving Schedules</a></h3>
<p>Elenas schedule changes as she takes on more responsibility:</p>
<pre><code class="language-storybook">// Apprentice schedule: supervised hours
schedule ElenaApprentice {
block arrive {
06:00 - 06:15
action: routines::arrive_early
}
block learn_and_assist {
06:15 - 14:00
action: baking::assist_martha
}
block cleanup_duty {
14:00 - 15:00
action: shop::cleanup
}
block study {
15:00 - 16:00
action: learning::study_recipes
}
}
// Master schedule: leadership hours
schedule ElenaMaster extends ElenaApprentice {
block arrive {
04:00 - 04:15
action: routines::open_bakery
}
block learn_and_assist {
04:15 - 12:00
action: baking::lead_production
}
block cleanup_duty {
12:00 - 13:00
action: social::lunch_with_team
}
block study {
13:00 - 15:00
action: baking::mentor_apprentice
}
block business {
15:00 - 17:00
action: management::business_planning
}
}
</code></pre>
<h2 id="key-takeaways"><a class="header" href="#key-takeaways">Key Takeaways</a></h2>
<p>This example demonstrates:</p>
<ol>
<li><strong>Life arcs as character development</strong>: Elenas career progression modeled as states</li>
<li><strong>Evolving behaviors</strong>: Different behavior trees for each stage of growth</li>
<li><strong>Changing relationships</strong>: The mentor dynamic shifts from dependency to partnership</li>
<li><strong>Schedule evolution</strong>: Responsibilities grow with skill level</li>
<li><strong>Narrative prose</strong>: Each life arc state tells a story about who Elena is becoming</li>
<li><strong>Template progression</strong>: Templates define the capability profile at each stage</li>
</ol>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="../reference/13-life-arcs.html">Life Arcs Reference</a> - State machine syntax</li>
<li><a href="../advanced/20-patterns.html">Design Patterns</a> - Progressive development pattern</li>
<li><a href="../advanced/23-best-practices.html">Best Practices</a> - Character design guidelines</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../examples/25-day-in-life.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/27-multi-character.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../examples/25-day-in-life.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../examples/27-multi-character.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,737 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Multi-Character Interactions - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="multi-character-interactions"><a class="header" href="#multi-character-interactions">Multi-Character Interactions</a></h1>
<p>This example models a complex social scene: a busy Saturday morning at Marthas bakery. Multiple characters interact simultaneously with interlocking behaviors, relationships, and a shared location buzzing with activity.</p>
<h2 id="the-setting"><a class="header" href="#the-setting">The Setting</a></h2>
<pre><code class="language-storybook">enum RushLevel { calm, busy, hectic, overwhelming }
enum ServiceMode { normal, rush, emergency }
location BakeryStorefront {
rush_level: busy
current_time: 07:30
customers_waiting: 8
display_items_remaining: 45
oven_batches_in_progress: 3
coffee_machine_running: true
---description
Saturday morning at Martha's bakery. The line stretches out
the door. The display case gleams with fresh bread, pastries,
and Elena's famous rosemary olive rolls. The air is warm with
the smell of baking and the hum of conversation.
---
---atmosphere
This is the bakery at its best and most stressful. Every
Saturday brings the regulars, the farmers' market overflow,
and tourists who heard about Martha's sourdough. The whole
team works in concert to keep up.
---
}
institution SaturdayMorningCrew {
type: work_team
purpose: serve_customers_and_bake
members: 4
coordination_level: 0.9
---description
The Saturday crew operates like a well-oiled machine. Martha
runs the kitchen, Jane handles pastries, Elena manages the
front counter, and Gregory -- the loyal regular -- unofficially
helps direct the line.
---
}
</code></pre>
<h2 id="the-characters"><a class="header" href="#the-characters">The Characters</a></h2>
<pre><code class="language-storybook">use schema::core_enums::{SkillLevel, Confidence, Specialty};
use schema::templates::{Baker, BusinessOwner, Apprentice};
use schema::beings::Human;
character Martha: Human from Baker, BusinessOwner {
age: 34
specialty: sourdough
skill_level: master
confidence: commanding
energy: 0.8
stress_level: 0.4
loaves_baked_today: 24
orders_pending: 6
---personality
Calm under pressure. Martha thrives on Saturday mornings --
the rush brings out her best. She coordinates the team with
quiet efficiency, stepping in wherever needed while keeping
the ovens running on schedule.
---
}
character Jane: Human from Baker {
age: 36
specialty: pastries
skill_level: expert
confidence: confident
energy: 0.9
creative_mode: true
pastries_decorated_today: 18
---personality
Jane works in focused silence during the rush. Her hands
move with precision, piping decorations and assembling
layered pastries. She communicates with Martha through
glances and nods -- years of partnership have made words
unnecessary.
---
}
character Elena: Human from Apprentice {
age: 17
skill_level: intermediate
confidence: growing
energy: 1.0
customers_served_today: 32
mistakes_today: 1
---personality
Elena has grown into the front-counter role. She remembers
regulars' names and orders, handles complaints with grace,
and only calls Martha when truly stuck. The nervous girl
who started a year ago is barely recognizable.
---
}
character Gregory: Human {
age: 68
role: "regular_customer"
visits_today: 1
helping_with_line: true
knows_everyone: true
---personality
Gregory arrives at exactly 7:15 every Saturday. He buys
his sourdough loaf, then lingers near the door, chatting
with other customers and unofficially managing the line.
He considers this his contribution to the bakery.
---
}
</code></pre>
<h2 id="interlocking-behaviors"><a class="header" href="#interlocking-behaviors">Interlocking Behaviors</a></h2>
<h3 id="marthas-behavior"><a class="header" href="#marthas-behavior">Marthas Behavior</a></h3>
<pre><code class="language-storybook">behavior Martha_SaturdayMorning {
---description
Martha's Saturday morning routine: managing the kitchen,
coordinating the team, and keeping the ovens running.
---
repeat {
choose saturday_priority {
// Check ovens first (highest priority)
then oven_management {
if(oven_timer_near_done)
CheckOvenTemperature
RemoveFinishedBatch
LoadNextBatch
SetTimer
}
// Handle special orders
then special_orders {
if(has_special_orders)
choose order_type {
PrepareWeddingCake
BoxCustomOrder
DecorateSpecialLoaf
}
}
// Support Elena at counter
then help_counter {
if(elena_needs_help)
choose counter_support {
AnswerCustomerQuestion
HandleComplaint
ProcessLargeOrder
}
}
// Coordinate with Jane
then coordinate_pastries {
if(display_items_remaining &lt; 10)
SignalJaneToRestockPastries
RearrangeDisplay
}
// Default: knead next batch
then prep_dough {
MixNextBatch
KneadDough
ShapeLoaves
}
}
}
}
</code></pre>
<h3 id="janes-behavior"><a class="header" href="#janes-behavior">Janes Behavior</a></h3>
<pre><code class="language-storybook">behavior Jane_SaturdayMorning {
repeat {
choose jane_priority {
// Restock display when signaled
then restock_pastries {
if(martha_signaled_restock)
PlateFinishedPastries
CarryToDisplay
ArrangeAttractively
}
// Decorate current batch
then decorating {
if(has_undecorated_pastries)
PipeIcing
AddGarnish
InspectQuality
}
// Start new pastry batch
then new_batch {
if(pastry_dough_ready)
RollPastryDough
CutShapes
AddFilling
PlaceOnBakingSheet
}
// Prepare specialty items
then specialty_items {
if(specialty_order_pending)
ReviewOrderNotes
SelectPremiumIngredients
CraftSpecialtyItem
}
}
}
}
</code></pre>
<h3 id="elenas-behavior"><a class="header" href="#elenas-behavior">Elenas Behavior</a></h3>
<pre><code class="language-storybook">behavior Elena_SaturdayCounter {
choose counter_state {
// Serve waiting customers
then serve_customer {
if(customer_waiting)
then service_sequence {
GreetCustomer
if(customer_is_regular) {
RecallPreferences
}
choose order_handling {
then quick_order {
if(customer_knows_what_they_want)
AcceptOrder
PackageItem
}
then help_decide {
if(not customer_knows_what_they_want)
OfferRecommendation
OfferSample
AcceptOrder
PackageItem
}
}
CollectPayment
ThankCustomer
}
}
// Handle problems
then handle_issue {
if(customer_has_complaint)
choose resolution {
then resolve_alone {
if(confidence &gt; 0.5)
ListenCarefully
OfferSolution
ApplyResolution
}
then escalate {
if(confidence &lt;= 0.5)
AcknowledgeProblem
CallMarthaForHelp
}
}
}
// Manage the line
then manage_queue {
if(line_length &gt; 5)
AnnounceWaitTime
SuggestPopularItems
}
}
}
</code></pre>
<h3 id="gregorys-behavior"><a class="header" href="#gregorys-behavior">Gregorys Behavior</a></h3>
<pre><code class="language-storybook">behavior Gregory_SaturdayVisit {
then saturday_routine {
// Arrive and order
then arrival {
EnterBakery
GreetElena
OrderSourdoughLoaf
PayExactChange
}
// Linger and help
choose lingering_activity {
then manage_line {
if(line_is_long)
DirectNewCustomersToEndOfLine
ChatWithWaitingCustomers
RecommendPopularItems
}
then catch_up {
if(sees_familiar_face)
GreetNeighbor
ExchangeLocalNews
DiscussWeather
}
then observe_elena {
if(elena_handling_difficult_customer)
StandNearbyForMoralSupport
NodEncouragingly
}
}
// Eventually leave
then departure {
WaveToMartha
SayGoodbyeToElena
ExitWithBread
}
}
}
</code></pre>
<h2 id="relationships"><a class="header" href="#relationships">Relationships</a></h2>
<pre><code class="language-storybook">relationship BakeryPartnership {
Martha {
role: co_owner
coordination: 1.0
handles_bread: true
---perspective
Martha and Jane communicate without words during the rush.
A glance toward the display case means "we're running low."
A nod means "I'll handle it." Years of working side by side
have created an effortless rhythm.
---
}
Jane {
role: co_owner
coordination: 1.0
handles_pastries: true
---perspective
Jane trusts Martha's judgment completely during the Saturday
rush. If Martha signals, Jane reprioritizes. If Jane needs
oven time, Martha adjusts. They are two halves of a single
well-run kitchen.
---
}
bond: 0.95
}
relationship TeamAndApprentice {
Martha as mentor
Jane as senior_colleague
Elena as apprentice
bond: 0.8
---dynamics
Elena looks up to both Martha and Jane, but in different ways.
Martha teaches her the fundamentals -- technique, discipline,
consistency. Jane shows her the creative side -- decoration,
presentation, flavor combinations. Together they are shaping
Elena into a complete baker.
---
}
relationship GregoryAtTheBakery {
Gregory {
role: loyal_customer
attachment: 0.9
unofficial_helper: true
---perspective
The bakery is Gregory's third place -- not home, not the
library where he used to teach, but the warm space where
he belongs. He has watched Elena grow from a nervous girl
to a confident young woman. He is proud, though he would
never say so directly.
---
}
Elena {
role: counter_staff
fondness: 0.8
sees_as: "grandfather_figure"
---perspective
Elena looks forward to Gregory's arrival every morning.
His exact-change payment and dry humor are a reliable
anchor in the chaos of the morning rush.
---
}
bond: 0.7
}
</code></pre>
<h2 id="the-saturday-schedule"><a class="header" href="#the-saturday-schedule">The Saturday Schedule</a></h2>
<pre><code class="language-storybook">schedule SaturdayRush {
block early_prep {
03:00 - 06:00
action: baking::saturday_batch
}
block opening {
06:00 - 06:15
action: shop::open_doors
}
block morning_rush {
06:15 - 11:00
action: shop::saturday_rush_service
}
block midday_restock {
11:00 - 12:00
action: baking::midday_supplemental
}
block afternoon_wind_down {
12:00 - 14:00
action: shop::afternoon_sales
}
block close_and_clean {
14:00 - 15:00
action: shop::saturday_cleanup
}
}
</code></pre>
<h2 id="life-arc-elenas-saturday-confidence"><a class="header" href="#life-arc-elenas-saturday-confidence">Life Arc: Elenas Saturday Confidence</a></h2>
<pre><code class="language-storybook">life_arc ElenaSaturdayGrowth {
state nervous_start {
on enter {
Elena.confidence: uncertain
Elena.energy: 1.0
}
on customers_served_today &gt; 5 -&gt; finding_rhythm
---narrative
The first few customers are always the hardest. Elena
fumbles with the register, second-guesses prices, and
looks to Martha for confirmation. But each successful
transaction builds her up.
---
}
state finding_rhythm {
on enter {
Elena.confidence: growing
}
on customers_served_today &gt; 15 -&gt; in_the_zone
---narrative
Something clicks. Elena stops thinking about each step
and starts flowing. She remembers Mrs. Patterson's usual
order before she says it. She bags the croissants without
looking.
---
}
state in_the_zone {
on enter {
Elena.confidence: confident
}
on handled_complaint_alone -&gt; proud_moment
on energy &lt; 0.3 -&gt; running_on_fumes
---narrative
Elena is running the counter like she was born to it.
Gregory gives her a quiet nod of approval from his spot
by the door. She barely notices -- she is too busy being
competent.
---
}
state proud_moment {
on enter {
Elena.confidence: confident
Elena.self_respect: 0.9
}
---narrative
A customer complained about a stale roll. Elena apologized,
replaced it with a fresh one, and offered a free cookie.
The customer left smiling. Elena handled it alone, without
calling Martha. She stands a little taller afterward.
---
}
state running_on_fumes {
on enter {
Elena.energy: 0.2
Elena.confidence: uncertain
}
on break_taken -&gt; finding_rhythm
---narrative
The rush has been going for four hours. Elena's smile
is getting harder to maintain. Martha notices and sends
her to the back for a five-minute break and a pastry.
---
}
}
</code></pre>
<h2 id="key-takeaways"><a class="header" href="#key-takeaways">Key Takeaways</a></h2>
<p>This example demonstrates:</p>
<ol>
<li><strong>Multiple characters with interlocking behaviors</strong>: Martha, Jane, Elena, and Gregory react to each other</li>
<li><strong>Character coordination</strong>: Martha and Jane operate as a seamless team</li>
<li><strong>Asymmetric group dynamics</strong>: Gregory is an unofficial helper, Elena is growing into her role</li>
<li><strong>Location as context</strong>: The busy bakery storefront defines the scene</li>
<li><strong>Institution modeling</strong>: The Saturday crew as a coordinated work team</li>
<li><strong>Visitor arc</strong>: Elenas confidence through the Saturday rush modeled as a life arc</li>
<li><strong>Rich prose</strong>: Every character and relationship includes narrative perspective</li>
</ol>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="../reference/15-relationships.html">Relationships Reference</a> - Multi-party relationships</li>
<li><a href="../reference/11-behavior-trees.html">Behavior Trees Reference</a> - Coordinated behaviors</li>
<li><a href="../reference/13-life-arcs.html">Life Arcs Reference</a> - Scene-based state machines</li>
<li><a href="./24-baker-family-complete.html">Baker Family Complete</a> - Full project context</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../examples/26-character-evolution.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../examples/26-character-evolution.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

BIN
docs/book/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

22
docs/book/favicon.svg Normal file
View File

@@ -0,0 +1,22 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 199.7 184.2">
<style>
@media (prefers-color-scheme: dark) {
svg { fill: white; }
}
</style>
<path d="M189.5,36.8c0.2,2.8,0,5.1-0.6,6.8L153,162c-0.6,2.1-2,3.7-4.2,5c-2.2,1.2-4.4,1.9-6.7,1.9H31.4c-9.6,0-15.3-2.8-17.3-8.4
c-0.8-2.2-0.8-3.9,0.1-5.2c0.9-1.2,2.4-1.8,4.6-1.8H123c7.4,0,12.6-1.4,15.4-4.1s5.7-8.9,8.6-18.4l32.9-108.6
c1.8-5.9,1-11.1-2.2-15.6S169.9,0,164,0H72.7c-1,0-3.1,0.4-6.1,1.1l0.1-0.4C64.5,0.2,62.6,0,61,0.1s-3,0.5-4.3,1.4
c-1.3,0.9-2.4,1.8-3.2,2.8S52,6.5,51.2,8.1c-0.8,1.6-1.4,3-1.9,4.3s-1.1,2.7-1.8,4.2c-0.7,1.5-1.3,2.7-2,3.7c-0.5,0.6-1.2,1.5-2,2.5
s-1.6,2-2.2,2.8s-0.9,1.5-1.1,2.2c-0.2,0.7-0.1,1.8,0.2,3.2c0.3,1.4,0.4,2.4,0.4,3.1c-0.3,3-1.4,6.9-3.3,11.6
c-1.9,4.7-3.6,8.1-5.1,10.1c-0.3,0.4-1.2,1.3-2.6,2.7c-1.4,1.4-2.3,2.6-2.6,3.7c-0.3,0.4-0.3,1.5-0.1,3.4c0.3,1.8,0.4,3.1,0.3,3.8
c-0.3,2.7-1.3,6.3-3,10.8c-1.7,4.5-3.4,8.2-5,11c-0.2,0.5-0.9,1.4-2,2.8c-1.1,1.4-1.8,2.5-2,3.4c-0.2,0.6-0.1,1.8,0.1,3.4
c0.2,1.6,0.2,2.8-0.1,3.6c-0.6,3-1.8,6.7-3.6,11c-1.8,4.3-3.6,7.9-5.4,11c-0.5,0.8-1.1,1.7-2,2.8c-0.8,1.1-1.5,2-2,2.8
s-0.8,1.6-1,2.5c-0.1,0.5,0,1.3,0.4,2.3c0.3,1.1,0.4,1.9,0.4,2.6c-0.1,1.1-0.2,2.6-0.5,4.4c-0.2,1.8-0.4,2.9-0.4,3.2
c-1.8,4.8-1.7,9.9,0.2,15.2c2.2,6.2,6.2,11.5,11.9,15.8c5.7,4.3,11.7,6.4,17.8,6.4h110.7c5.2,0,10.1-1.7,14.7-5.2s7.7-7.8,9.2-12.9
l33-108.6c1.8-5.8,1-10.9-2.2-15.5C194.9,39.7,192.6,38,189.5,36.8z M59.6,122.8L73.8,80c0,0,7,0,10.8,0s28.8-1.7,25.4,17.5
c-3.4,19.2-18.8,25.2-36.8,25.4S59.6,122.8,59.6,122.8z M78.6,116.8c4.7-0.1,18.9-2.9,22.1-17.1S89.2,86.3,89.2,86.3l-8.9,0
l-10.2,30.5C70.2,116.9,74,116.9,78.6,116.8z M75.3,68.7L89,26.2h9.8l0.8,34l23.6-34h9.9l-13.6,42.5h-7.1l12.5-35.4l-24.5,35.4h-6.8
l-0.8-35L82,68.7H75.3z"/>
</svg>
<!-- Original image Copyright Dave Gandy — CC BY 4.0 License -->

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

@@ -0,0 +1,202 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@@ -0,0 +1,93 @@
Copyright 2010, 2012 Adobe Systems Incorporated (http://www.adobe.com/), with Reserved Font Name 'Source'. All Rights Reserved. Source is a trademark of Adobe Systems Incorporated in the United States and/or other countries.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

100
docs/book/fonts/fonts.css Normal file
View File

@@ -0,0 +1,100 @@
/* Open Sans is licensed under the Apache License, Version 2.0. See http://www.apache.org/licenses/LICENSE-2.0 */
/* Source Code Pro is under the Open Font License. See https://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL */
/* open-sans-300 - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 300;
src: local('Open Sans Light'), local('OpenSans-Light'),
url('open-sans-v17-all-charsets-300.woff2') format('woff2');
}
/* open-sans-300italic - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 300;
src: local('Open Sans Light Italic'), local('OpenSans-LightItalic'),
url('open-sans-v17-all-charsets-300italic.woff2') format('woff2');
}
/* open-sans-regular - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans Regular'), local('OpenSans-Regular'),
url('open-sans-v17-all-charsets-regular.woff2') format('woff2');
}
/* open-sans-italic - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 400;
src: local('Open Sans Italic'), local('OpenSans-Italic'),
url('open-sans-v17-all-charsets-italic.woff2') format('woff2');
}
/* open-sans-600 - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 600;
src: local('Open Sans SemiBold'), local('OpenSans-SemiBold'),
url('open-sans-v17-all-charsets-600.woff2') format('woff2');
}
/* open-sans-600italic - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 600;
src: local('Open Sans SemiBold Italic'), local('OpenSans-SemiBoldItalic'),
url('open-sans-v17-all-charsets-600italic.woff2') format('woff2');
}
/* open-sans-700 - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 700;
src: local('Open Sans Bold'), local('OpenSans-Bold'),
url('open-sans-v17-all-charsets-700.woff2') format('woff2');
}
/* open-sans-700italic - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 700;
src: local('Open Sans Bold Italic'), local('OpenSans-BoldItalic'),
url('open-sans-v17-all-charsets-700italic.woff2') format('woff2');
}
/* open-sans-800 - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 800;
src: local('Open Sans ExtraBold'), local('OpenSans-ExtraBold'),
url('open-sans-v17-all-charsets-800.woff2') format('woff2');
}
/* open-sans-800italic - latin_vietnamese_latin-ext_greek-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 800;
src: local('Open Sans ExtraBold Italic'), local('OpenSans-ExtraBoldItalic'),
url('open-sans-v17-all-charsets-800italic.woff2') format('woff2');
}
/* source-code-pro-500 - latin_vietnamese_latin-ext_greek_cyrillic-ext_cyrillic */
@font-face {
font-family: 'Source Code Pro';
font-style: normal;
font-weight: 500;
src: url('source-code-pro-v11-all-charsets-500.woff2') format('woff2');
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

83
docs/book/highlight.css Normal file
View File

@@ -0,0 +1,83 @@
/*
* An increased contrast highlighting scheme loosely based on the
* "Base16 Atelier Dune Light" theme by Bram de Haan
* (http://atelierbram.github.io/syntax-highlighting/atelier-schemes/dune)
* Original Base16 color scheme by Chris Kempson
* (https://github.com/chriskempson/base16)
*/
/* Comment */
.hljs-comment,
.hljs-quote {
color: #575757;
}
/* Red */
.hljs-variable,
.hljs-template-variable,
.hljs-attribute,
.hljs-attr,
.hljs-tag,
.hljs-name,
.hljs-regexp,
.hljs-link,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class {
color: #d70025;
}
/* Orange */
.hljs-number,
.hljs-meta,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params {
color: #b21e00;
}
/* Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet {
color: #008200;
}
/* Blue */
.hljs-title,
.hljs-section {
color: #0030f2;
}
/* Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #9d00ec;
}
.hljs {
display: block;
overflow-x: auto;
background: #f6f7f6;
color: #000;
}
.hljs-emphasis {
font-style: italic;
}
.hljs-strong {
font-weight: bold;
}
.hljs-addition {
color: #22863a;
background-color: #f0fff4;
}
.hljs-deletion {
color: #b31d28;
background-color: #ffeef0;
}

54
docs/book/highlight.js Normal file

File diff suppressed because one or more lines are too long

286
docs/book/index.html Normal file
View File

@@ -0,0 +1,286 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Introduction - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="storybook-language-guide"><a class="header" href="#storybook-language-guide">Storybook Language Guide</a></h1>
<blockquote>
<p><strong>Create rich narrative simulations through code that reads like stories.</strong></p>
</blockquote>
<p>Welcome to the complete guide for the Storybook narrative simulation language! Whether youre a creative writer bringing characters to life or a developer building simulation systems, this documentation will help you master Storybook.</p>
<hr />
<p><strong>⚠️ Alpha Software Notice</strong></p>
<p>Storybook is currently in <strong>alpha</strong> and under active development at <a href="https://r3t.io">r3t Studios</a>. While the core language features are stable and ready to use, you should expect:</p>
<ul>
<li><strong>New features</strong> to be added as we expand the language capabilities</li>
<li><strong>Minor syntax adjustments</strong> as we refine the design based on real-world usage</li>
<li><strong>API changes</strong> in the compiled output format as we optimize for game engine integration</li>
</ul>
<p>Were committed to a thoughtful path toward version 1.0. Breaking changes will be clearly documented, and well provide migration guides when syntax evolves. Your feedback during this alpha period is invaluable in shaping the languages future!</p>
<hr />
<h2 id="what-is-storybook"><a class="header" href="#what-is-storybook">What is Storybook?</a></h2>
<p>Storybook is a <strong>compiled simulation language</strong> designed for <strong>open-world, autonomous game simulations</strong>. While it includes a basic embedded virtual machine for terminal-based debugging, its built to be integrated into game engines and developed hand-in-hand with technical game developers.</p>
<p>Storybook defines characters, behaviors, relationships, and narrative events for autonomous agents in dynamic worlds. It bridges the gap between storytelling and technical simulation through:</p>
<ul>
<li><strong>Readable syntax</strong> - Code that looks like natural descriptions, but compiles to efficient bytecode</li>
<li><strong>Named nodes</strong> - Behavior trees you can read as stories, that drive AI decision-making</li>
<li><strong>Prose blocks</strong> - Embed narrative directly in definitions for context-aware storytelling</li>
<li><strong>Rich semantics</strong> - From simple traits to complex state machines and schedules</li>
<li><strong>Game engine integration</strong> - Designed to power autonomous NPCs in Unity, Unreal, Godot, and custom engines</li>
</ul>
<h2 id="choose-your-path"><a class="header" href="#choose-your-path">Choose Your Path</a></h2>
<h3 id="-for-storytellers"><a class="header" href="#-for-storytellers">🎨 For Storytellers</a></h3>
<p><strong>New to programming?</strong> Start with the <a href="tutorial/01-welcome.html">Tutorial Track</a> for a gentle, example-driven introduction. Learn by building a bakery simulation!</p>
<h3 id="-for-developers"><a class="header" href="#-for-developers">💻 For Developers</a></h3>
<p><strong>Need technical precision?</strong> Jump to the <a href="reference/09-overview.html">Reference Guide</a> for complete syntax specifications and semantic details.</p>
<h3 id="-for-everyone"><a class="header" href="#-for-everyone">✨ For Everyone</a></h3>
<p><strong>Want inspiration?</strong> Browse the <a href="examples/24-baker-family-complete.html">Examples Gallery</a> to see whats possible!</p>
<h2 id="quick-start"><a class="header" href="#quick-start">Quick Start</a></h2>
<pre><code class="language-storybook">character Martha {
age: 34
skill_level: 0.95
---description
A master baker who learned from her grandmother
and now runs the most popular bakery in town.
---
}
behavior Baker_MorningRoutine {
choose daily_priority {
then prepare_sourdough { ... }
then serve_customers { ... }
then restock_display { ... }
}
}
</code></pre>
<p><a href="tutorial/01-welcome.html">Continue to Tutorial →</a></p>
<h2 id="documentation-structure"><a class="header" href="#documentation-structure">Documentation Structure</a></h2>
<ul>
<li><strong>Part I: Getting Started</strong> - Tutorials for learning Storybook</li>
<li><strong>Part II: Complete Reference</strong> - Technical specifications</li>
<li><strong>Part III: Advanced Topics</strong> - Patterns and integration</li>
<li><strong>Part IV: Examples Gallery</strong> - Complete working examples</li>
</ul>
<h2 id="getting-help"><a class="header" href="#getting-help">Getting Help</a></h2>
<ul>
<li><strong>In-Editor</strong>: Hover over keywords for quick help</li>
<li><strong>Search</strong>: Use the search box (top right) to find anything</li>
<li><strong>Examples</strong>: Working code is the best teacher!</li>
</ul>
<hr />
<p><strong>Ready to begin?</strong> <a href="tutorial/01-welcome.html">Start with the Tutorial →</a></p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="next prefetch" href="tutorial/01-welcome.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="next prefetch" href="tutorial/01-welcome.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,61 @@
# LSP Documentation for 'choose' keyword
[keyword]
name = "choose"
category = "behavior_tree"
short_description = "Selector node that tries children in order until one succeeds"
web_url = "https://r3t-studios.github.io/storybook/reference/behavior-trees.html#choose"
[syntax]
format = "choose [label] { children... }"
example = """
choose panic_response {
then time_panic { ... }
then obstacle_panic { ... }
then default_panic { ... }
}
"""
[hover]
markdown = """
**`choose` - Selector Node**
Tries each child behavior in order until one succeeds.
**Behavior:**
- Evaluates children left-to-right
- Returns **success** on first successful child
- Skips remaining children after first success
- Returns **failure** only if all children fail
**Named nodes (optional):**
```storybook
choose state_dependent_action {
then appear { ... }
then confuse_alice { ... }
then fade_away { ... }
}
```
Labels make intent clear and aid debugging!
**Use cases:**
- Decision-making between alternatives
- Fallback chains (try A, if fails try B)
- Priority ordering (prefer A, accept B)
[Learn more →](https://r3t-studios.github.io/storybook/reference/behavior-trees.html#choose)
"""
[completion]
snippet = "choose ${1:label} {\n\t$0\n}"
description = "Selector node (tries children until one succeeds)"
sort_text = "01-choose"
[context]
valid_in = ["behavior_block", "choose_block", "then_block", "decorator_block"]
invalid_in = ["character_block", "life_arc_block", "schedule_block"]
[related]
keywords = ["then", "if", "when"]
concepts = ["behavior_trees", "named_nodes", "selectors"]

7
docs/book/mark.min.js vendored Normal file

File diff suppressed because one or more lines are too long

11335
docs/book/print.html Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,409 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Language Overview - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="language-overview"><a class="header" href="#language-overview">Language Overview</a></h1>
<blockquote>
<p><strong>The Storybook language enables narrative simulation through structured declarations of characters, behaviors, relationships, and events.</strong></p>
</blockquote>
<h2 id="philosophy"><a class="header" href="#philosophy">Philosophy</a></h2>
<p>Storybook is a domain-specific language for narrative simulation, influenced by:</p>
<ul>
<li><strong>Rust</strong>: Strong typing, explicit declarations, and clear ownership semantics</li>
<li><strong>C#</strong>: Object-oriented patterns with declarative syntax</li>
<li><strong>Python</strong>: Readable, accessible syntax that prioritizes clarity</li>
</ul>
<p>The language balances <strong>technical precision</strong> with <strong>narrative expressiveness</strong>, making it accessible to storytellers while maintaining the rigor developers need.</p>
<h2 id="design-principles"><a class="header" href="#design-principles">Design Principles</a></h2>
<h3 id="1-code-as-narrative"><a class="header" href="#1-code-as-narrative">1. Code as Narrative</a></h3>
<p>Named nodes and prose blocks let code read like stories:</p>
<pre><code class="language-storybook">behavior Baker_MorningRoutine {
choose daily_priority {
then prepare_sourdough { ... }
then serve_customers { ... }
then restock_display { ... }
}
}
</code></pre>
<h3 id="2-explicit-is-better-than-implicit"><a class="header" href="#2-explicit-is-better-than-implicit">2. Explicit is Better Than Implicit</a></h3>
<p>Every declaration is self-documenting:</p>
<ul>
<li>Character fields show what defines them</li>
<li>Behavior trees show decision structures</li>
<li>Relationships name their participants</li>
</ul>
<h3 id="3-progressive-disclosure"><a class="header" href="#3-progressive-disclosure">3. Progressive Disclosure</a></h3>
<p>Simple cases are simple, complex cases are possible:</p>
<ul>
<li>Basic characters need just a name and fields</li>
<li>Templates enable inheritance and reuse</li>
<li>Advanced features (state machines, decorators) available when needed</li>
</ul>
<h3 id="4-semantic-validation"><a class="header" href="#4-semantic-validation">4. Semantic Validation</a></h3>
<p>The compiler catches narrative errors:</p>
<ul>
<li>Bond values must be 0.0..1.0</li>
<li>Schedule blocks cant overlap</li>
<li>Life arc transitions must reference valid states</li>
</ul>
<h2 id="language-structure"><a class="header" href="#language-structure">Language Structure</a></h2>
<h3 id="declaration-types"><a class="header" href="#declaration-types">Declaration Types</a></h3>
<p>Storybook has 10 top-level declaration types:</p>
<div class="table-wrapper"><table><thead><tr><th>Declaration</th><th>Purpose</th><th>Example</th></tr></thead><tbody>
<tr><td><code>character</code></td><td>Define entities with traits and behaviors</td><td>A baker with skills and schedule</td></tr>
<tr><td><code>template</code></td><td>Reusable patterns with ranges</td><td>A generic NPC template</td></tr>
<tr><td><code>behavior</code></td><td>Decision trees for actions</td><td>How a character responds to events</td></tr>
<tr><td><code>life_arc</code></td><td>State machines for life stages</td><td>Apprentice → Baker → Master</td></tr>
<tr><td><code>schedule</code></td><td>Time-based activities</td><td>Daily routine from 6am to 10pm</td></tr>
<tr><td><code>relationship</code></td><td>Connections between entities</td><td>Parent-child with bond values</td></tr>
<tr><td><code>institution</code></td><td>Organizations and groups</td><td>A bakery with employees</td></tr>
<tr><td><code>location</code></td><td>Places with properties</td><td>The town square</td></tr>
<tr><td><code>species</code></td><td>Type definitions with traits</td><td>Human vs Cat vs Rabbit</td></tr>
<tr><td><code>enum</code></td><td>Named value sets</td><td>EmotionalState options</td></tr>
</tbody></table>
</div>
<h3 id="value-types"><a class="header" href="#value-types">Value Types</a></h3>
<p>Fields can contain:</p>
<ul>
<li><strong>Primitives</strong>: <code>42</code>, <code>3.14</code>, <code>"text"</code>, <code>true</code></li>
<li><strong>Time</strong>: <code>08:30:00</code>, <code>14:15</code></li>
<li><strong>Duration</strong>: <code>2h30m</code>, <code>45s</code></li>
<li><strong>Ranges</strong>: <code>20..40</code> (for templates)</li>
<li><strong>Identifiers</strong>: <code>OtherCharacter</code>, <code>path::to::Thing</code></li>
<li><strong>Lists</strong>: <code>[1, 2, 3]</code></li>
<li><strong>Objects</strong>: <code>{ field: value }</code></li>
<li><strong>Prose blocks</strong>: <code> ---tag\nMulti-line\ntext\n---</code></li>
</ul>
<h3 id="expression-language"><a class="header" href="#expression-language">Expression Language</a></h3>
<p>Conditions and queries use:</p>
<ul>
<li><strong>Comparisons</strong>: <code>age &gt; 18</code>, <code>energy &lt;= 0.5</code></li>
<li><strong>Equality</strong>: <code>status is active</code>, <code>ready is true</code></li>
<li><strong>Logic</strong>: <code>tired and hungry</code>, <code>rich or lucky</code>, <code>not ready</code></li>
<li><strong>Field access</strong>: <code>self.health</code>, <code>other.bond</code></li>
<li><strong>Quantifiers</strong>: <code>forall x in children: x.happy</code></li>
</ul>
<h2 id="compilation-model"><a class="header" href="#compilation-model">Compilation Model</a></h2>
<h3 id="source--ast--sbir--runtime"><a class="header" href="#source--ast--sbir--runtime">Source → AST → SBIR → Runtime</a></h3>
<pre><code>.sb files → Parser → Abstract Syntax Tree → Resolver → SBIR Binary
</code></pre>
<p><strong>SBIR</strong> (Storybook Intermediate Representation) is a compact binary format that:</p>
<ul>
<li>Resolves all cross-file references</li>
<li>Validates semantic constraints</li>
<li>Optimizes for simulation runtime</li>
</ul>
<h3 id="validation-layers"><a class="header" href="#validation-layers">Validation Layers</a></h3>
<ol>
<li><strong>Lexical</strong>: Valid tokens and syntax</li>
<li><strong>Syntactic</strong>: Correct grammar structure</li>
<li><strong>Semantic</strong>: Type checking, reference resolution</li>
<li><strong>Domain</strong>: Narrative constraints (bond ranges, schedule overlaps)</li>
</ol>
<h2 id="file-organization"><a class="header" href="#file-organization">File Organization</a></h2>
<h3 id="project-structure"><a class="header" href="#project-structure">Project Structure</a></h3>
<pre><code>my-storybook/
├── characters/
│ ├── baker.sb
│ └── family.sb
├── behaviors/
│ └── daily_routine.sb
├── world/
│ ├── locations.sb
│ └── institutions.sb
└── schema/
├── species.sb
└── templates.sb
</code></pre>
<h3 id="import-system"><a class="header" href="#import-system">Import System</a></h3>
<p>Use <code>use</code> statements to reference definitions from other files:</p>
<pre><code class="language-storybook">use schema::species::Human;
use schema::templates::Adult;
character Baker: Human from Adult {
// ...
}
</code></pre>
<p><strong>Resolution order:</strong></p>
<ol>
<li>Same file</li>
<li>Explicitly imported</li>
<li>Error if not found</li>
</ol>
<h2 id="quick-reference"><a class="header" href="#quick-reference">Quick Reference</a></h2>
<h3 id="character-declaration"><a class="header" href="#character-declaration">Character Declaration</a></h3>
<pre><code class="language-storybook">character Name: Species from Template {
field: value
field: value
---prose_tag
Text content
---
}
</code></pre>
<h3 id="behavior-tree"><a class="header" href="#behavior-tree">Behavior Tree</a></h3>
<pre><code class="language-storybook">behavior Name {
choose label { // Selector
then label { ... } // Sequence
if (condition) // Condition
ActionName // Action
include path // Subtree
}
}
</code></pre>
<h3 id="life-arc"><a class="header" href="#life-arc">Life Arc</a></h3>
<pre><code class="language-storybook">life_arc Name {
state StateName {
on condition -&gt; NextState
}
}
</code></pre>
<h3 id="schedule"><a class="header" href="#schedule">Schedule</a></h3>
<pre><code class="language-storybook">schedule Name {
08:00 -&gt; 12:00: activity { }
12:00 -&gt; 13:00: lunch { }
}
</code></pre>
<h3 id="relationship"><a class="header" href="#relationship">Relationship</a></h3>
<pre><code class="language-storybook">relationship Name {
Person1 as role
Person2 as role
bond: 0.85
}
</code></pre>
<h2 id="next-steps"><a class="header" href="#next-steps">Next Steps</a></h2>
<p>Dive deeper into each declaration type:</p>
<ul>
<li><a href="10-characters.html">Characters</a> - Detailed character syntax</li>
<li><a href="11-behavior-trees.html">Behavior Trees</a> - Complete behavior node reference</li>
<li><a href="12-decorators.html">Decorators</a> - All decorator types</li>
<li><a href="13-life-arcs.html">Life Arcs</a> - State machine semantics</li>
<li><a href="14-schedules.html">Schedules</a> - Time-based planning</li>
<li><a href="15-relationships.html">Relationships</a> - Connection modeling</li>
<li><a href="16-other-declarations.html">Other Declarations</a> - Templates, institutions, etc.</li>
<li><a href="17-expressions.html">Expressions</a> - Full expression language</li>
<li><a href="18-value-types.html">Value Types</a> - All field value types</li>
<li><a href="19-validation.html">Validation</a> - Error checking rules</li>
</ul>
<hr />
<p><strong>Philosophy Note</strong>: Storybook treats narrative as data. Characters arent objects with methods - theyre <strong>declarations</strong> of traits, connected by <strong>behaviors</strong> and <strong>relationships</strong>. This separation enables rich analysis, modification, and simulation of narrative worlds.</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../tutorial/09-locations-institutions.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/10-characters.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../tutorial/09-locations-institutions.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/10-characters.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,611 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Characters - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="characters"><a class="header" href="#characters">Characters</a></h1>
<p>Characters are the primary entities in Storybook—the people, creatures, and beings that inhabit your world. Each character has a set of attributes that define who they are, what they can do, and how they relate to the world around them.</p>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;character-decl&gt; ::= "character" &lt;identifier&gt; &lt;species-clause&gt;? &lt;template-clause&gt;? &lt;body&gt;
&lt;species-clause&gt; ::= ":" &lt;qualified-path&gt;
&lt;template-clause&gt; ::= "from" &lt;qualified-path&gt; ("," &lt;qualified-path&gt;)*
&lt;body&gt; ::= "{" &lt;body-item&gt;* "}"
&lt;body-item&gt; ::= &lt;field&gt;
| &lt;uses-behaviors&gt;
| &lt;uses-schedule&gt;
&lt;uses-behaviors&gt; ::= "uses" "behaviors" ":" &lt;behavior-link-list&gt;
&lt;uses-schedule&gt; ::= "uses" ("schedule" | "schedules") ":" (&lt;qualified-path&gt; | &lt;schedule-list&gt;)
&lt;behavior-link-list&gt; ::= "[" &lt;behavior-link&gt; ("," &lt;behavior-link&gt;)* "]"
&lt;behavior-link&gt; ::= "{" &lt;behavior-link-field&gt;* "}"
&lt;behavior-link-field&gt; ::= "tree" ":" &lt;qualified-path&gt;
| "when" ":" &lt;expression&gt;
| "priority" ":" ("low" | "normal" | "high" | "critical")
&lt;schedule-list&gt; ::= "[" &lt;qualified-path&gt; ("," &lt;qualified-path&gt;)* "]"
&lt;field&gt; ::= &lt;identifier&gt; ":" &lt;value&gt;
&lt;value&gt; ::= &lt;literal&gt;
| &lt;qualified-path&gt;
| &lt;list&gt;
| &lt;object&gt;
| &lt;prose-block&gt;
| &lt;override&gt;
&lt;prose-block&gt; ::= "---" &lt;identifier&gt; &lt;content&gt; "---"
</code></pre>
<h2 id="components"><a class="header" href="#components">Components</a></h2>
<h3 id="name"><a class="header" href="#name">Name</a></h3>
<p>The characters identifier. Must be unique within its scope and follow standard identifier rules (alphanumeric + underscore, cannot start with digit).</p>
<h3 id="species-optional"><a class="header" href="#species-optional">Species (Optional)</a></h3>
<p>The species clause (<code>: SpeciesName</code>) defines what the character fundamentally <em>is</em>. This is distinct from templates, which define what attributes they <em>have</em>.</p>
<ul>
<li><strong>Purpose</strong>: Ontological typing—what kind of being this is</li>
<li><strong>Validation</strong>: Must reference a defined <code>species</code> declaration</li>
<li><strong>Single inheritance</strong>: A character can only have one species</li>
<li><strong>Default behavior</strong>: Species fields are inherited automatically</li>
</ul>
<p>Example:</p>
<pre><code class="language-storybook">character Martha: Human {
age: 34
}
</code></pre>
<h3 id="template-inheritance-optional"><a class="header" href="#template-inheritance-optional">Template Inheritance (Optional)</a></h3>
<p>The template clause (<code>from Template1, Template2</code>) specifies templates from which the character inherits fields. Templates provide reusable attribute sets.</p>
<ul>
<li><strong>Purpose</strong>: Compositional inheritance—mix-and-match capabilities and traits</li>
<li><strong>Multiple inheritance</strong>: Characters can inherit from multiple templates</li>
<li><strong>Merge semantics</strong>: Fields from later templates override earlier ones</li>
<li><strong>Override allowed</strong>: Character fields override all inherited fields</li>
</ul>
<p>Example:</p>
<pre><code class="language-storybook">character Martha: Human from Baker, BusinessOwner {
specialty: "sourdough"
}
</code></pre>
<h3 id="fields"><a class="header" href="#fields">Fields</a></h3>
<p>Fields define the characters attributes using the standard field syntax. All <a href="./18-value-types.html">value types</a> are supported.</p>
<p><strong>Common field categories:</strong></p>
<ul>
<li><strong>Physical traits</strong>: <code>height</code>, <code>weight</code>, <code>age</code>, <code>eye_color</code></li>
<li><strong>Personality</strong>: <code>confidence</code>, <code>patience</code>, <code>dedication</code></li>
<li><strong>Professional</strong>: <code>skill_level</code>, <code>specialty</code>, <code>recipes_mastered</code></li>
<li><strong>State tracking</strong>: <code>energy</code>, <code>mood</code>, <code>orders_today</code></li>
<li><strong>Capabilities</strong>: <code>can_teach</code>, <code>can_work_independently</code></li>
</ul>
<h3 id="prose-blocks"><a class="header" href="#prose-blocks">Prose Blocks</a></h3>
<p>Characters can contain multiple prose blocks for narrative content. Common tags:</p>
<ul>
<li><code>---backstory</code>: Character history and origin</li>
<li><code>---appearance</code>: Physical description</li>
<li><code>---personality</code>: Behavioral traits and quirks</li>
<li><code>---motivation</code>: Goals and desires</li>
<li><code>---secrets</code>: Hidden information</li>
</ul>
<p>Prose blocks are narrative-only and do not affect simulation logic.</p>
<h3 id="behavior-integration"><a class="header" href="#behavior-integration">Behavior Integration</a></h3>
<p>Characters can link to <a href="./14-behavior-trees.html">behavior trees</a> using the <code>uses behaviors</code> clause.</p>
<pre><code class="language-storybook">character Guard {
uses behaviors: [
{
tree: combat::patrol_route
priority: normal
},
{
tree: combat::engage_intruder
when: threat_detected
priority: high
}
]
}
</code></pre>
<p>Each behavior link includes:</p>
<ul>
<li><strong><code>tree</code></strong>: Qualified path to the behavior tree (required)</li>
<li><strong><code>when</code></strong>: Condition expression for activation (optional)</li>
<li><strong><code>priority</code></strong>: Execution priority (optional, default: <code>normal</code>)</li>
</ul>
<p>See <a href="./11-behavior-trees.html">Behavior Trees</a> for details on behavior tree syntax and semantics.</p>
<h3 id="schedule-integration"><a class="header" href="#schedule-integration">Schedule Integration</a></h3>
<p>Characters can follow <a href="./16-schedules.html">schedules</a> using the <code>uses schedule</code> or <code>uses schedules</code> clause.</p>
<pre><code class="language-storybook">character Baker {
uses schedule: BakerySchedule
}
character Innkeeper {
uses schedules: [WeekdaySchedule, WeekendSchedule]
}
</code></pre>
<ul>
<li>Single schedule: <code>uses schedule: ScheduleName</code></li>
<li>Multiple schedules: <code>uses schedules: [Schedule1, Schedule2]</code></li>
</ul>
<p>The runtime selects the appropriate schedule based on temporal constraints. See <a href="./14-schedules.html">Schedules</a> for composition and selection semantics.</p>
<h2 id="species-vs-templates"><a class="header" href="#species-vs-templates">Species vs. Templates</a></h2>
<p>The distinction between species (<code>:</code>) and templates (<code>from</code>) reflects ontological vs. compositional typing:</p>
<div class="table-wrapper"><table><thead><tr><th>Feature</th><th>Species (<code>:</code>)</th><th>Templates (<code>from</code>)</th></tr></thead><tbody>
<tr><td><strong>Semantics</strong></td><td>What the character <em>is</em></td><td>What the character <em>has</em></td></tr>
<tr><td><strong>Cardinality</strong></td><td>Exactly one</td><td>Zero or more</td></tr>
<tr><td><strong>Example</strong></td><td><code>: Human</code>, <code>: Dragon</code></td><td><code>from Warrior, Mage</code></td></tr>
<tr><td><strong>Purpose</strong></td><td>Fundamental nature</td><td>Reusable trait sets</td></tr>
<tr><td><strong>Override</strong></td><td>Can override species fields</td><td>Can override template fields</td></tr>
</tbody></table>
</div>
<p>Example showing both:</p>
<pre><code class="language-storybook">species Dragon {
max_lifespan: 1000
can_fly: true
}
template Hoarder {
treasure_value: 0..1000000
greed_level: 0.0..1.0
}
template Ancient {
age: 500..1000
wisdom: 0.8..1.0
}
character Smaug: Dragon from Hoarder, Ancient {
age: 850
treasure_value: 500000
greed_level: 0.95
}
</code></pre>
<h2 id="field-resolution-order"><a class="header" href="#field-resolution-order">Field Resolution Order</a></h2>
<p>When a character inherits from species and templates, fields are resolved in this order (later overrides earlier):</p>
<ol>
<li><strong>Species fields</strong> (base ontology)</li>
<li><strong>Template fields</strong> (left to right in <code>from</code> clause)</li>
<li><strong>Character fields</strong> (highest priority)</li>
</ol>
<p>Example:</p>
<pre><code class="language-storybook">species Human {
lifespan: 80
speed: 1.0
}
template Warrior {
speed: 1.5
strength: 10
}
template Berserker {
speed: 2.0
strength: 15
}
character Conan: Human from Warrior, Berserker {
strength: 20
}
// Resolved fields:
// lifespan: 80 (from Human)
// speed: 2.0 (Berserker overrides Warrior overrides Human)
// strength: 20 (character overrides Berserker)
</code></pre>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<p>The Storybook compiler enforces these validation rules:</p>
<ol>
<li><strong>Unique names</strong>: Character names must be unique within their module</li>
<li><strong>Species exists</strong>: If specified, the species must reference a defined <code>species</code> declaration</li>
<li><strong>Templates exist</strong>: All templates in the <code>from</code> clause must reference defined <code>template</code> declarations</li>
<li><strong>No circular inheritance</strong>: Templates cannot form circular dependency chains</li>
<li><strong>Field type consistency</strong>: Field values must match expected types from species/templates</li>
<li><strong>Reserved fields</strong>: Cannot use reserved keywords as field names</li>
<li><strong>Behavior trees exist</strong>: All behavior tree references must resolve to defined <code>behavior</code> declarations</li>
<li><strong>Schedules exist</strong>: All schedule references must resolve to defined <code>schedule</code> declarations</li>
<li><strong>Prose tag uniqueness</strong>: Each prose tag can appear at most once per character</li>
</ol>
<h2 id="examples"><a class="header" href="#examples">Examples</a></h2>
<h3 id="basic-character"><a class="header" href="#basic-character">Basic Character</a></h3>
<pre><code class="language-storybook">character SimpleMerchant {
name: "Gregor"
occupation: "Fish Merchant"
wealth: 50
---personality
A straightforward fish seller at the market. Honest, hardworking,
and always smells faintly of mackerel.
---
}
</code></pre>
<h3 id="character-with-species"><a class="header" href="#character-with-species">Character with Species</a></h3>
<pre><code class="language-storybook">character Martha: Human {
age: 34
skill_level: 0.95
specialty: "sourdough"
---backstory
Martha learned to bake from her grandmother, starting at age
twelve. She now runs the most popular bakery in town.
---
}
</code></pre>
<h3 id="character-with-template-inheritance"><a class="header" href="#character-with-template-inheritance">Character with Template Inheritance</a></h3>
<pre><code class="language-storybook">character Jane: Human from Baker, PastrySpecialist {
age: 36
specialty: "pastries"
recipes_mastered: 120
years_experience: 18
can_teach: true
---appearance
A focused woman with flour-dusted apron and steady hands.
Known for her intricate pastry decorations and precise
temperature control.
---
}
</code></pre>
<h3 id="character-with-all-features"><a class="header" href="#character-with-all-features">Character with All Features</a></h3>
<pre><code class="language-storybook">character CityGuard: Human from CombatTraining, LawEnforcement {
age: 30
rank: "Sergeant"
// Physical traits
height: 175
strength: 12
// Equipment
has_weapon: true
armor_level: 2
// Behavior integration
uses behaviors: [
{
tree: guards::patrol_route
priority: normal
},
{
tree: guards::engage_hostile
when: threat_detected
priority: high
},
{
tree: guards::sound_alarm
when: emergency
priority: critical
}
]
// Schedule integration
uses schedules: [guards::day_shift, guards::night_shift]
---backstory
A veteran of the city watch, now responsible for training new recruits
while maintaining order in the merchant district.
---
---personality
Gruff exterior with a hidden soft spot for street children. Follows
the rules but knows when to look the other way.
---
}
</code></pre>
<h3 id="character-with-overrides"><a class="header" href="#character-with-overrides">Character with Overrides</a></h3>
<pre><code class="language-storybook">template WeaponUser {
damage: 5..15
accuracy: 0.7
}
character MasterSwordsman: Human from WeaponUser {
// Override template range with specific value
damage: 15
accuracy: 0.95
// Add character-specific fields
signature_move: "Whirlwind Strike"
}
</code></pre>
<h2 id="use-cases"><a class="header" href="#use-cases">Use Cases</a></h2>
<h3 id="protagonist-definition"><a class="header" href="#protagonist-definition">Protagonist Definition</a></h3>
<p>Define rich, dynamic protagonists with complex attributes:</p>
<pre><code class="language-storybook">character Elena: Human from Scholar, Diplomat {
age: 28
intelligence: 18
charisma: 16
languages_known: ["Common", "Elvish", "Draconic"]
books_read: 347
current_quest: "Broker peace between warring kingdoms"
---backstory
Raised in the Grand Library, Elena discovered ancient texts that
hinted at a forgotten alliance between humans and dragons. She now
seeks to revive that alliance to end the current war.
---
}
</code></pre>
<h3 id="npc-templates"><a class="header" href="#npc-templates">NPC Templates</a></h3>
<p>Create diverse NPCs from templates:</p>
<pre><code class="language-storybook">template Villager {
occupation: "Farmer"
wealth: 10..50
disposition: 0.0..1.0 // 0=hostile, 1=friendly
}
character Oswald: Human from Villager {
occupation: "Blacksmith"
wealth: 45
disposition: 0.8
}
character Mildred: Human from Villager {
occupation: "Baker"
wealth: 35
disposition: 0.95
}
</code></pre>
<h3 id="ensemble-casts"><a class="header" href="#ensemble-casts">Ensemble Casts</a></h3>
<p>Define multiple related characters:</p>
<pre><code class="language-storybook">template BakeryStaff {
punctuality: 0.5..1.0
teamwork: 0.5..1.0
}
template Apprentice {
skill_level: 0.0..0.5
dedication: 0.5..1.0
}
character Elena: Human from BakeryStaff, Apprentice {
age: 16
natural_talent: 0.8
dedication: 0.9
---backstory
Elena comes from a family of farmers who could never afford to
buy bread from the bakery. When Martha offered her an apprenticeship,
she jumped at the chance to learn a trade.
---
}
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./16-other-declarations.html#species">Species</a> - Species declarations</li>
<li><a href="./16-other-declarations.html#templates">Templates</a> - Template definitions and strict mode</li>
<li><a href="./18-value-types.html">Value Types</a> - All supported value types</li>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> - Character behavior integration</li>
<li><a href="./14-schedules.html">Schedules</a> - Character schedule integration</li>
<li><a href="./15-relationships.html">Relationships</a> - Relationships between characters</li>
<li><a href="./13-life-arcs.html">Life Arcs</a> - Character state machines over time</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Instantiation</strong>: Characters are concrete instances; they cannot be instantiated further</li>
<li><strong>Composition</strong>: Prefer template composition over deep species hierarchies</li>
<li><strong>Modularity</strong>: Characters can reference behaviors and schedules from other modules</li>
<li><strong>Narrative-driven</strong>: Use prose blocks to embed storytelling directly with data</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/09-overview.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/11-behavior-trees.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/09-overview.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/11-behavior-trees.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,883 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Behavior Trees - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="behavior-trees"><a class="header" href="#behavior-trees">Behavior Trees</a></h1>
<p>Behavior trees define the decision-making logic for characters, institutions, and other entities. They model how an entity chooses actions, responds to conditions, and adapts to its environment. Storybook uses behavior trees for character AI, NPC routines, and complex reactive behavior.</p>
<h2 id="what-is-a-behavior-tree"><a class="header" href="#what-is-a-behavior-tree">What is a Behavior Tree?</a></h2>
<p>A behavior tree is a hierarchical structure that executes from root to leaves, making decisions based on success/failure of child nodes:</p>
<ul>
<li><strong>Composite nodes</strong> (choose, then) have multiple children and control flow</li>
<li><strong>Condition nodes</strong> (if, when) test predicates</li>
<li><strong>Action nodes</strong> execute concrete behaviors</li>
<li><strong>Decorator nodes</strong> (repeat, invert, timeout, etc.) modify child behavior</li>
<li><strong>Subtree nodes</strong> (include) reference other behavior trees</li>
</ul>
<p>Behavior trees are evaluated every tick (frame), flowing through the tree from root to leaves until a node returns success or failure.</p>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;behavior-decl&gt; ::= "behavior" &lt;identifier&gt; &lt;body&gt;
&lt;body&gt; ::= "{" &lt;prose-blocks&gt;* &lt;behavior-node&gt; "}"
&lt;behavior-node&gt; ::= &lt;selector&gt;
| &lt;sequence&gt;
| &lt;condition&gt;
| &lt;action&gt;
| &lt;decorator&gt;
| &lt;subtree&gt;
&lt;selector&gt; ::= "choose" &lt;label&gt;? "{" &lt;behavior-node&gt;+ "}"
&lt;sequence&gt; ::= "then" &lt;label&gt;? "{" &lt;behavior-node&gt;+ "}"
&lt;condition&gt; ::= ("if" | "when") "(" &lt;expression&gt; ")"
&lt;action&gt; ::= &lt;identifier&gt; ( "(" &lt;param-list&gt; ")" )?
&lt;param-list&gt; ::= &lt;field&gt; ("," &lt;field&gt;)*
&lt;decorator&gt; ::= &lt;decorator-type&gt; ("(" &lt;params&gt; ")")? "{" &lt;behavior-node&gt; "}"
&lt;subtree&gt; ::= "include" &lt;qualified-path&gt;
&lt;label&gt; ::= &lt;identifier&gt;
</code></pre>
<h2 id="composite-nodes"><a class="header" href="#composite-nodes">Composite Nodes</a></h2>
<h3 id="selector-choose"><a class="header" href="#selector-choose">Selector: <code>choose</code></a></h3>
<p>A selector tries its children in order until one succeeds. Returns success if any child succeeds, failure if all fail.</p>
<p><strong>Execution:</strong></p>
<ol>
<li>Try first child</li>
<li>If succeeds -&gt; return success</li>
<li>If fails -&gt; try next child</li>
<li>If all fail -&gt; return failure</li>
</ol>
<p><strong>Use case:</strong> “Try A, if that fails try B, if that fails try C…”</p>
<pre><code class="language-storybook">behavior GuardBehavior {
choose guard_actions {
AttackIntruder // Try first
SoundAlarm // If attack fails, sound alarm
FleeInPanic // If alarm fails, flee
}
}
</code></pre>
<p><strong>Named selectors:</strong></p>
<pre><code class="language-storybook">choose service_options {
then serve_regular {
CustomerIsWaiting
TakeOrder
}
then restock_display {
DisplayIsLow
FetchFromKitchen
}
}
</code></pre>
<p>Labels are optional but recommended for complex treesthey improve readability and debugging.</p>
<h3 id="sequence-then"><a class="header" href="#sequence-then">Sequence: <code>then</code></a></h3>
<p>A sequence runs its children in order until one fails. Returns success only if all children succeed, failure if any fails.</p>
<p><strong>Execution:</strong></p>
<ol>
<li>Run first child</li>
<li>If fails -&gt; return failure</li>
<li>If succeeds -&gt; run next child</li>
<li>If all succeed -&gt; return success</li>
</ol>
<p><strong>Use case:</strong> “Do A, then B, then C… all must succeed”</p>
<pre><code class="language-storybook">behavior BrewingSequence {
then brew_potion {
GatherIngredients // Must succeed
MixIngredients // Then this must succeed
Boil // Then this must succeed
BottlePotion // Finally this
}
}
</code></pre>
<p><strong>Named sequences:</strong></p>
<pre><code class="language-storybook">then prepare_sourdough {
MixDough
KneadDough
ShapeLoaves
}
</code></pre>
<h3 id="nesting-composites"><a class="header" href="#nesting-composites">Nesting Composites</a></h3>
<p>Composite nodes can nest arbitrarily deep:</p>
<pre><code class="language-storybook">behavior ComplexAI {
choose root_decision {
// Combat branch
then engage_combat {
if(enemy_nearby)
choose combat_tactics {
AttackWithSword
AttackWithMagic
DefendAndWait
}
}
// Exploration branch
then explore_area {
if(safe)
choose exploration_mode {
SearchForTreasure
MapTerritory
Rest
}
}
// Default: Idle
Idle
}
}
</code></pre>
<h2 id="condition-nodes"><a class="header" href="#condition-nodes">Condition Nodes</a></h2>
<p>Conditions test expressions and return success/failure based on the result.</p>
<h3 id="if-vs-when"><a class="header" href="#if-vs-when"><code>if</code> vs. <code>when</code></a></h3>
<p>Both are semantically identicaluse whichever reads better in context:</p>
<pre><code class="language-storybook">behavior Example {
choose options {
// "if" for state checks
then branch_a {
if(player_nearby)
Attack
}
// "when" for event-like conditions
then branch_b {
when(alarm_triggered)
Flee
}
}
}
</code></pre>
<h3 id="condition-syntax"><a class="header" href="#condition-syntax">Condition Syntax</a></h3>
<pre><code class="language-storybook">if(health &lt; 20) // Comparison
when(is_hostile) // Boolean field
if(distance &gt; 10 and has_weapon) // Logical AND
when(not is_stunned or is_enraged) // Logical OR with NOT
</code></pre>
<p>See <a href="./17-expressions.html">Expression Language</a> for complete expression syntax.</p>
<h2 id="action-nodes"><a class="header" href="#action-nodes">Action Nodes</a></h2>
<p>Actions are leaf nodes that execute concrete behaviors. They reference action implementations in the runtime.</p>
<h3 id="basic-actions"><a class="header" href="#basic-actions">Basic Actions</a></h3>
<pre><code class="language-storybook">behavior SimpleActions {
then do_things {
MoveForward
TurnLeft
Attack
}
}
</code></pre>
<h3 id="actions-with-parameters"><a class="header" href="#actions-with-parameters">Actions with Parameters</a></h3>
<p>Actions can have named parameters using parenthesis syntax:</p>
<pre><code class="language-storybook">behavior ParameterizedActions {
then patrol {
MoveTo(destination: "Waypoint1", speed: 1.5)
WaitFor(duration: 5s)
MoveTo(destination: "Waypoint2", speed: 1.5)
}
}
</code></pre>
<p><strong>Parameter passing:</strong></p>
<ul>
<li>Parameters are passed as fields (name: value)</li>
<li>All <a href="./18-value-types.html">value types</a> supported</li>
<li>Runtime validates parameter types</li>
</ul>
<pre><code class="language-storybook">behavior RichParameters {
then complex_action {
CastSpell(spell: "Fireball", target: enemy_position, power: 75, multicast: false)
Heal(amount: 50, target: self)
}
}
</code></pre>
<h2 id="decorator-nodes"><a class="header" href="#decorator-nodes">Decorator Nodes</a></h2>
<p>Decorators wrap a single child node and modify its behavior. See <a href="./12-decorators.html">Decorators</a> for complete reference.</p>
<h3 id="common-decorators"><a class="header" href="#common-decorators">Common Decorators</a></h3>
<pre><code class="language-storybook">behavior DecoratorExamples {
choose {
// Repeat infinitely
repeat {
PatrolRoute
}
// Repeat exactly 3 times
repeat(3) {
CheckDoor
}
// Repeat 2 to 5 times
repeat(2..5) {
SearchArea
}
// Invert success/failure
invert {
IsEnemyNearby // Returns success if enemy NOT nearby
}
// Retry up to 5 times on failure
retry(5) {
OpenLockedDoor
}
// Timeout after 10 seconds
timeout(10s) {
SolveComplexPuzzle
}
// Cooldown: only run once per 30 seconds
cooldown(30s) {
FireCannon
}
// If: only run if condition true
if(health &gt; 50) {
AggressiveAttack
}
// Always succeed regardless of child result
succeed_always {
AttemptOptionalTask
}
// Always fail regardless of child result
fail_always {
ImpossipleTask
}
}
}
</code></pre>
<h2 id="subtree-references"><a class="header" href="#subtree-references">Subtree References</a></h2>
<p>The <code>include</code> keyword references another behavior tree, enabling modularity and reuse.</p>
<h3 id="basic-subtree"><a class="header" href="#basic-subtree">Basic Subtree</a></h3>
<pre><code class="language-storybook">behavior PatrolRoute {
then patrol {
MoveTo(destination: "Waypoint1")
MoveTo(destination: "Waypoint2")
MoveTo(destination: "Waypoint3")
}
}
behavior GuardBehavior {
choose guard_ai {
then combat {
if(enemy_nearby)
include combat::engage
}
include PatrolRoute // Reuse patrol behavior
}
}
</code></pre>
<h3 id="qualified-subtree-references"><a class="header" href="#qualified-subtree-references">Qualified Subtree References</a></h3>
<pre><code class="language-storybook">behavior ComplexAI {
choose ai_modes {
include behaviors::combat::melee
include behaviors::combat::ranged
include behaviors::exploration::search
include behaviors::social::greet
}
}
</code></pre>
<h2 id="named-nodes"><a class="header" href="#named-nodes">Named Nodes</a></h2>
<p>Both composite nodes (<code>choose</code>, <code>then</code>) can have optional labels:</p>
<pre><code class="language-storybook">behavior NamedNodeExample {
choose daily_priority { // Named selector
then handle_special_orders { // Named sequence
CheckOrderQueue
PrepareSpecialIngredients
BakeSpecialItem
}
choose regular_work { // Named selector
then morning_bread { // Named sequence
MixSourdough
BakeLoaves
}
}
}
}
</code></pre>
<p><strong>Benefits:</strong></p>
<ul>
<li><strong>Readability</strong>: Tree structure is self-documenting</li>
<li><strong>Debugging</strong>: Named nodes appear in execution traces</li>
<li><strong>Narrative</strong>: Labels can be narrative (“handle_special_orders” vs. anonymous node)</li>
</ul>
<p><strong>Guidelines:</strong></p>
<ul>
<li>Use labels for important structural nodes</li>
<li>Use descriptive, narrative names</li>
<li>Omit labels for trivial nodes</li>
</ul>
<h2 id="complete-examples"><a class="header" href="#complete-examples">Complete Examples</a></h2>
<h3 id="simple-guard-ai"><a class="header" href="#simple-guard-ai">Simple Guard AI</a></h3>
<pre><code class="language-storybook">behavior GuardPatrol {
choose guard_logic {
// Combat if enemy nearby
then combat_mode {
if(enemy_nearby and has_weapon)
Attack
}
// Sound alarm if see intruder
then alarm_mode {
if(intruder_detected)
SoundAlarm
}
// Default: Patrol
then patrol_mode {
include PatrolRoute
}
}
}
</code></pre>
<h3 id="complex-npc-behavior"><a class="header" href="#complex-npc-behavior">Complex NPC Behavior</a></h3>
<pre><code class="language-storybook">behavior Innkeeper_DailyRoutine {
---description
The innkeeper's behavior throughout the day. Uses selectors for
decision-making and sequences for multi-step actions.
---
choose daily_activity {
// Morning: Prepare for opening
then morning_prep {
when(time_is_morning)
then prep_sequence {
UnlockDoor
LightFireplace
PrepareBreakfast
WaitForGuests
}
}
// Day: Serve customers
then day_service {
when(time_is_daytime)
choose service_mode {
// Serve customer if present
then serve {
if(customer_waiting)
GreetCustomer
TakeOrder
ServeMeal
CollectPayment
}
// Restock if needed
then restock {
if(inventory_low)
ReplenishInventory
}
// Default: Clean
CleanTables
}
}
// Night: Close up
then evening_close {
when(time_is_evening)
then close_sequence {
DismissRemainingGuests
LockDoor
CountMoney
GoToBed
}
}
}
}
</code></pre>
<h3 id="morning-baking-routine"><a class="header" href="#morning-baking-routine">Morning Baking Routine</a></h3>
<pre><code class="language-storybook">behavior Baker_MorningRoutine {
---description
Martha's morning routine: prepare dough step by step,
from mixing to shaping to baking.
---
then morning_baking {
// Start with sourdough
then prepare_starter {
CheckStarter
FeedStarter
WaitForActivity
}
// Mix the dough
then mix_dough {
MeasureFlour
AddWater
IncorporateStarter
}
// Knead and shape
then shape_loaves {
KneadDough
FirstRise
ShapeLoaves
}
// Bake
then bake {
PreHeatOven
LoadLoaves
MonitorBaking
}
}
}
</code></pre>
<h3 id="repeating-behavior"><a class="header" href="#repeating-behavior">Repeating Behavior</a></h3>
<pre><code class="language-storybook">behavior Bakery_CustomerServiceLoop {
---description
The bakery's continuous customer service loop. Uses infinite
repeat decorator to serve customers throughout the day.
---
repeat {
then service_cycle {
// Check for customers
choose service_mode {
then serve_waiting {
if(customer_waiting)
GreetCustomer
TakeOrder
}
then restock_display {
if(display_low)
FetchFromKitchen
ArrangeOnShelves
}
}
// Process payment
CollectPayment
ThankCustomer
// Brief pause between customers
timeout(5s) {
CleanCounter
}
PrepareForNextCustomer
}
}
}
</code></pre>
<h3 id="retry-logic-for-delicate-recipes"><a class="header" href="#retry-logic-for-delicate-recipes">Retry Logic for Delicate Recipes</a></h3>
<pre><code class="language-storybook">behavior Baker_DelicatePastry {
---description
Elena attempting a delicate pastry recipe that requires
precise technique. Uses retry decorator for persistence.
---
choose baking_strategy {
// Try when conditions are right
then attempt_pastry {
if(oven_at_temperature)
// Try up to 3 times
retry(3) {
then make_pastry {
RollDoughThin
ApplyFilling
FoldAndSeal
CheckForLeaks
}
}
}
// If oven not ready, prepare meanwhile
then prepare_meanwhile {
if(not oven_at_temperature)
then prep_sequence {
PrepareIngredients
MixFilling
ChillDough
}
}
}
}
</code></pre>
<h2 id="integration-with-characters"><a class="header" href="#integration-with-characters">Integration with Characters</a></h2>
<p>Characters link to behaviors using the <code>uses behaviors</code> clause:</p>
<pre><code class="language-storybook">character Guard {
uses behaviors: [
{
tree: guards::patrol_route
priority: normal
},
{
tree: guards::engage_hostile
when: threat_detected
priority: high
},
{
tree: guards::sound_alarm
when: emergency
priority: critical
}
]
}
</code></pre>
<p><strong>Behavior selection:</strong></p>
<ul>
<li>Multiple behaviors evaluated by priority (critical &gt; high &gt; normal &gt; low)</li>
<li>Conditions (<code>when</code> clause) gate behavior activation</li>
<li>Higher-priority behaviors preempt lower-priority ones</li>
</ul>
<p>See <a href="./10-characters.html#behavior-integration">Characters</a> for complete behavior linking syntax.</p>
<h2 id="execution-semantics"><a class="header" href="#execution-semantics">Execution Semantics</a></h2>
<h3 id="tick-based-evaluation"><a class="header" href="#tick-based-evaluation">Tick-Based Evaluation</a></h3>
<p>Behavior trees execute every “tick” (typically once per frame):</p>
<ol>
<li><strong>Start at root</strong> node</li>
<li><strong>Traverse</strong> down to leaves based on composite logic</li>
<li><strong>Execute</strong> leaf nodes (conditions, actions)</li>
<li><strong>Return</strong> success/failure up the tree</li>
<li><strong>Repeat</strong> next tick</li>
</ol>
<h3 id="node-return-values"><a class="header" href="#node-return-values">Node Return Values</a></h3>
<p>Every node returns one of:</p>
<ul>
<li><strong>Success</strong>: Node completed successfully</li>
<li><strong>Failure</strong>: Node failed</li>
<li><strong>Running</strong>: Node still executing (async action)</li>
</ul>
<h3 id="stateful-vs-stateless"><a class="header" href="#stateful-vs-stateless">Stateful vs. Stateless</a></h3>
<ul>
<li><strong>Stateless nodes</strong>: Evaluate fresh every tick (conditions, simple actions)</li>
<li><strong>Stateful nodes</strong>: Maintain state across ticks (decorators, long actions)</li>
</ul>
<p>Example of stateful behavior:</p>
<pre><code class="language-storybook">behavior LongAction {
timeout(30s) { // Stateful: tracks elapsed time
ComplexCalculation // May take multiple ticks
}
}
</code></pre>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>At least one node</strong>: Behavior body must contain at least one node</li>
<li><strong>Composite children</strong>: <code>choose</code> and <code>then</code> require at least one child</li>
<li><strong>Decorator child</strong>: Decorators require exactly one child</li>
<li><strong>Action exists</strong>: Action names must reference registered actions in runtime</li>
<li><strong>Subtree exists</strong>: <code>include</code> must reference a defined <code>behavior</code> declaration</li>
<li><strong>Expression validity</strong>: Condition expressions must be well-formed</li>
<li><strong>Duration format</strong>: Decorator durations must be valid (e.g., <code>5s</code>, <code>10m</code>)</li>
<li><strong>Unique labels</strong>: Node labels (if used) should be unique within their parent</li>
<li><strong>Parameter types</strong>: Action parameters must match expected types</li>
</ol>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<h3 id="1-prefer-shallow-trees"><a class="header" href="#1-prefer-shallow-trees">1. Prefer Shallow Trees</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">choose {
then { then { then { then { Action } } } } // Too deep!
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">choose root {
include combat_tree
include exploration_tree
include social_tree
}
</code></pre>
<h3 id="2-use-named-nodes-for-clarity"><a class="header" href="#2-use-named-nodes-for-clarity">2. Use Named Nodes for Clarity</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">choose {
then {
IsHungry
FindFood
Eat
}
Wander
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">choose survival {
then eat_if_hungry {
IsHungry
FindFood
Eat
}
Wander
}
</code></pre>
<h3 id="3-subtrees-for-reusability"><a class="header" href="#3-subtrees-for-reusability">3. Subtrees for Reusability</a></h3>
<p><strong>Avoid:</strong> Duplicating logic across behaviors</p>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">behavior Combat_Common {
then attack_sequence {
DrawWeapon
Approach
Strike
}
}
behavior Warrior {
include Combat_Common
}
behavior Guard {
include Combat_Common
}
</code></pre>
<h3 id="4-decorators-for-timing"><a class="header" href="#4-decorators-for-timing">4. Decorators for Timing</a></h3>
<p><strong>Avoid:</strong> Manual timing in actions</p>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">timeout(10s) {
ComplexTask
}
cooldown(30s) {
SpecialAbility
}
</code></pre>
<h3 id="5-guard-for-preconditions"><a class="header" href="#5-guard-for-preconditions">5. Guard for Preconditions</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">then problematic {
ExpensiveAction // Always runs even if inappropriate
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">if(can_afford_action) {
ExpensiveAction // Only runs when condition passes
}
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./12-decorators.html">Decorators</a> - Complete decorator reference</li>
<li><a href="./10-characters.html">Characters</a> - Linking behaviors to characters</li>
<li><a href="./17-expressions.html">Expression Language</a> - Condition expression syntax</li>
<li><a href="./18-value-types.html">Value Types</a> - Parameter value types</li>
<li><a href="../advanced/20-patterns.html">Design Patterns</a> - Common behavior tree patterns</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Reactive AI</strong>: Behavior trees continuously react to changing conditions</li>
<li><strong>Hierarchical decision-making</strong>: Composite nodes create decision hierarchies</li>
<li><strong>Modularity</strong>: Subtrees enable behavior reuse and composition</li>
<li><strong>Narrative-driven design</strong>: Named nodes make behavior trees readable as stories</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/10-characters.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/12-decorators.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/10-characters.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/12-decorators.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,837 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Decorators - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="decorators"><a class="header" href="#decorators">Decorators</a></h1>
<p>Decorators are special nodes that wrap a single child and modify its execution behavior. They enable timing control, retry logic, conditional execution, and result inversion without modifying the child node itself.</p>
<h2 id="what-are-decorators"><a class="header" href="#what-are-decorators">What Are Decorators?</a></h2>
<p>Decorators sit between a parent and child node, transforming the childs behavior:</p>
<pre><code>Parent
└─ Decorator
└─ Child
</code></pre>
<p>The decorator intercepts the childs execution, potentially:</p>
<ul>
<li>Repeating it multiple times</li>
<li>Timing it out</li>
<li>Inverting its result</li>
<li>Guarding its execution</li>
<li>Forcing a specific result</li>
</ul>
<h2 id="decorator-types"><a class="header" href="#decorator-types">Decorator Types</a></h2>
<p>Storybook provides 10 decorator types:</p>
<div class="table-wrapper"><table><thead><tr><th>Decorator</th><th>Purpose</th><th>Example</th></tr></thead><tbody>
<tr><td><code>repeat</code></td><td>Loop infinitely</td><td><code>repeat { Patrol }</code></td></tr>
<tr><td><code>repeat(N)</code></td><td>Loop N times</td><td><code>repeat(3) { Check }</code></td></tr>
<tr><td><code>repeat(min..max)</code></td><td>Loop min to max times</td><td><code>repeat(2..5) { Search }</code></td></tr>
<tr><td><code>invert</code></td><td>Flip success/failure</td><td><code>invert { IsEnemy }</code></td></tr>
<tr><td><code>retry(N)</code></td><td>Retry on failure (max N times)</td><td><code>retry(5) { Open }</code></td></tr>
<tr><td><code>timeout(duration)</code></td><td>Fail after duration</td><td><code>timeout(10s) { Solve }</code></td></tr>
<tr><td><code>cooldown(duration)</code></td><td>Run at most once per duration</td><td><code>cooldown(30s) { Fire }</code></td></tr>
<tr><td><code>if(condition)</code></td><td>Only run if condition true</td><td><code>if(health &gt; 50) { Attack }</code></td></tr>
<tr><td><code>succeed_always</code></td><td>Always return success</td><td><code>succeed_always { Try }</code></td></tr>
<tr><td><code>fail_always</code></td><td>Always return failure</td><td><code>fail_always { Test }</code></td></tr>
</tbody></table>
</div>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;decorator&gt; ::= &lt;repeat-decorator&gt;
| &lt;invert-decorator&gt;
| &lt;retry-decorator&gt;
| &lt;timeout-decorator&gt;
| &lt;cooldown-decorator&gt;
| &lt;if-decorator&gt;
| &lt;force-result-decorator&gt;
&lt;repeat-decorator&gt; ::= "repeat" &lt;repeat-spec&gt;? "{" &lt;behavior-node&gt; "}"
&lt;repeat-spec&gt; ::= "(" &lt;number&gt; ")"
| "(" &lt;number&gt; ".." &lt;number&gt; ")"
&lt;invert-decorator&gt; ::= "invert" "{" &lt;behavior-node&gt; "}"
&lt;retry-decorator&gt; ::= "retry" "(" &lt;number&gt; ")" "{" &lt;behavior-node&gt; "}"
&lt;timeout-decorator&gt; ::= "timeout" "(" &lt;duration-literal&gt; ")" "{" &lt;behavior-node&gt; "}"
&lt;cooldown-decorator&gt; ::= "cooldown" "(" &lt;duration-literal&gt; ")" "{" &lt;behavior-node&gt; "}"
&lt;if-decorator&gt; ::= "if" "(" &lt;expression&gt; ")" "{" &lt;behavior-node&gt; "}"
&lt;force-result-decorator&gt; ::= ("succeed_always" | "fail_always") "{" &lt;behavior-node&gt; "}"
&lt;duration-literal&gt; ::= &lt;number&gt; ("s" | "m" | "h" | "d")
</code></pre>
<h2 id="repeat-decorators"><a class="header" href="#repeat-decorators">Repeat Decorators</a></h2>
<h3 id="infinite-repeat-repeat"><a class="header" href="#infinite-repeat-repeat">Infinite Repeat: <code>repeat</code></a></h3>
<p>Loops the child infinitely. The child is re-executed immediately after completing (success or failure).</p>
<pre><code class="language-storybook">behavior InfinitePatrol {
repeat {
PatrolRoute
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Run child</li>
<li>Child completes (success or failure)</li>
<li>Immediately run child again</li>
<li>Go to step 2 (forever)</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Perpetual patrols</li>
<li>Endless background processes</li>
<li>Continuous monitoring</li>
</ul>
<p><strong>Warning:</strong> Infinite loops never return to parent. Ensure theyre appropriate for your use case.</p>
<h3 id="repeat-n-times-repeat-n"><a class="header" href="#repeat-n-times-repeat-n">Repeat N Times: <code>repeat N</code></a></h3>
<p>Repeats the child exactly N times, then returns success.</p>
<pre><code class="language-storybook">behavior CheckThreeTimes {
repeat(3) {
CheckDoor
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Counter = 0</li>
<li>Run child</li>
<li>Counter++</li>
<li>If counter &lt; N, go to step 2</li>
<li>Return success</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Fixed iteration counts</li>
<li>“Try three times then give up”</li>
<li>Deterministic looping</li>
</ul>
<h3 id="repeat-range-repeat-minmax"><a class="header" href="#repeat-range-repeat-minmax">Repeat Range: <code>repeat min..max</code></a></h3>
<p>Repeats the child between min and max times. At runtime, a specific count is selected within the range.</p>
<pre><code class="language-storybook">behavior SearchRandomly {
repeat(2..5) {
SearchArea
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Select count C randomly from [min, max]</li>
<li>Repeat child C times (as in <code>repeat N</code>)</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Variable behavior</li>
<li>Procedural variation</li>
<li>Non-deterministic actions</li>
</ul>
<p><strong>Validation:</strong></p>
<ul>
<li>min ≥ 0</li>
<li>max ≥ min</li>
<li>Both must be integers</li>
</ul>
<h2 id="invert-decorator"><a class="header" href="#invert-decorator">Invert Decorator</a></h2>
<p>Inverts the childs return value: success becomes failure, failure becomes success.</p>
<pre><code class="language-storybook">behavior AvoidEnemies {
invert {
IsEnemyNearby // Success if NOT nearby
}
}
</code></pre>
<p><strong>Truth table:</strong></p>
<div class="table-wrapper"><table><thead><tr><th>Child returns</th><th>Decorator returns</th></tr></thead><tbody>
<tr><td>Success</td><td>Failure</td></tr>
<tr><td>Failure</td><td>Success</td></tr>
<tr><td>Running</td><td>Running (unchanged)</td></tr>
</tbody></table>
</div>
<p><strong>Use cases:</strong></p>
<ul>
<li>Negating conditions (“if NOT X”)</li>
<li>Inverting success criteria</li>
<li>Converting “found” to “not found”</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">behavior SafeExploration {
choose safe_actions {
// Only explore if NOT dangerous
then explore {
invert { IsDangerous }
ExploreArea
}
// Default: Stay put
Wait
}
}
</code></pre>
<h2 id="retry-decorator"><a class="header" href="#retry-decorator">Retry Decorator</a></h2>
<p>Retries the child up to N times on failure. Returns success if any attempt succeeds, failure if all N attempts fail.</p>
<pre><code class="language-storybook">behavior PersistentDoor {
retry(5) {
OpenLockedDoor
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Attempts = 0</li>
<li>Run child</li>
<li>If child succeeds → return success</li>
<li>If child fails:
<ul>
<li>Attempts++</li>
<li>If attempts &lt; N, go to step 2</li>
<li>Else return failure</li>
</ul>
</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Unreliable actions (lockpicking, persuasion)</li>
<li>Network/resource operations</li>
<li>Probabilistic success</li>
</ul>
<p><strong>Example with context:</strong></p>
<pre><code class="language-storybook">behavior Thief_PickLock {
then attempt_entry {
// Try to pick lock (may fail)
retry(3) {
PickLock
}
// If succeeded, enter
EnterBuilding
// If failed after 3 tries, give up
}
}
</code></pre>
<p><strong>Validation:</strong></p>
<ul>
<li>N must be ≥ 1</li>
</ul>
<h2 id="timeout-decorator"><a class="header" href="#timeout-decorator">Timeout Decorator</a></h2>
<p>Fails the child if it doesnt complete within the specified duration.</p>
<pre><code class="language-storybook">behavior TimeLimitedPuzzle {
timeout(30s) {
SolvePuzzle
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Start timer</li>
<li>Run child each tick</li>
<li>If child completes before timeout → return childs result</li>
<li>If timer expires → return failure (interrupt child)</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Time-limited actions</li>
<li>Preventing infinite loops</li>
<li>Enforcing deadlines</li>
</ul>
<p><strong>Duration formats:</strong></p>
<ul>
<li><code>5s</code> - 5 seconds</li>
<li><code>10m</code> - 10 minutes</li>
<li><code>2h</code> - 2 hours</li>
<li><code>3d</code> - 3 days</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">behavior QuickDecision {
choose timed_choice {
// Give AI 5 seconds to find optimal move
timeout(5s) {
CalculateOptimalStrategy
}
// Fallback: Use simple heuristic
UseQuickHeuristic
}
}
</code></pre>
<p><strong>Notes:</strong></p>
<ul>
<li>Timer starts when decorator is first entered</li>
<li>Timer resets if decorator exits and re-enters</li>
<li>Child node should handle interruption gracefully</li>
</ul>
<h2 id="cooldown-decorator"><a class="header" href="#cooldown-decorator">Cooldown Decorator</a></h2>
<p>Prevents the child from running more than once per cooldown period. Fails immediately if called within cooldown.</p>
<pre><code class="language-storybook">behavior SpecialAbility {
cooldown(30s) {
FireCannon
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Check last execution time</li>
<li>If (current_time - last_time) &lt; cooldown → return failure</li>
<li>Else:
<ul>
<li>Run child</li>
<li>Record current_time as last_time</li>
<li>Return childs result</li>
</ul>
</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Rate-limiting abilities</li>
<li>Resource cooldowns (spells, items)</li>
<li>Preventing spam</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">behavior Mage_SpellCasting {
choose spells {
// Fireball: 10 second cooldown
cooldown(10s) {
CastFireball
}
// Lightning: 5 second cooldown
cooldown(5s) {
CastLightning
}
// Basic attack: No cooldown
MeleeAttack
}
}
</code></pre>
<p><strong>State management:</strong></p>
<ul>
<li>Cooldown state persists across behavior tree ticks</li>
<li>Each cooldown decorator instance has independent state</li>
<li>Cooldown timers are per-entity (not global)</li>
</ul>
<h2 id="if-decorator"><a class="header" href="#if-decorator">If Decorator</a></h2>
<p>Only runs the child if the condition is true. Fails immediately if condition is false.</p>
<pre><code class="language-storybook">behavior ConditionalAttack {
if(health &gt; 50) {
AggressiveAttack
}
}
</code></pre>
<p><strong>Execution:</strong></p>
<ol>
<li>Evaluate condition expression</li>
<li>If true → run child and return its result</li>
<li>If false → return failure (do not run child)</li>
</ol>
<p><strong>Use cases:</strong></p>
<ul>
<li>Preconditions (“only if X”)</li>
<li>Resource checks (“only if have mana”)</li>
<li>Safety checks (“only if safe”)</li>
</ul>
<p><strong>Expression syntax:</strong>
See <a href="./17-expressions.html">Expression Language</a> for complete syntax.</p>
<p><strong>Examples:</strong></p>
<pre><code class="language-storybook">behavior GuardedActions {
choose options {
// Only attack if have weapon and enemy close
if(has_weapon and distance &lt; 10) {
Attack
}
// Only heal if health below 50%
if(health &lt; (max_health * 0.5)) {
Heal
}
// Only flee if outnumbered
if(enemy_count &gt; ally_count) {
Flee
}
}
}
</code></pre>
<p><strong>Comparison with bare <code>if</code> conditions:</strong></p>
<pre><code class="language-storybook">// Using bare 'if' condition (checks every tick, no body)
then approach_and_attack {
if(enemy_nearby)
Approach
Attack
}
// Using 'if' decorator with body (precondition check, fails fast)
if(enemy_nearby) {
then do_attack {
Approach
Attack
}
}
</code></pre>
<p>The <code>if</code> decorator with a body is more efficient for gating expensive subtrees.</p>
<h2 id="force-result-decorators"><a class="header" href="#force-result-decorators">Force Result Decorators</a></h2>
<h3 id="succeed_always"><a class="header" href="#succeed_always"><code>succeed_always</code></a></h3>
<p>Always returns success, regardless of childs actual result.</p>
<pre><code class="language-storybook">behavior TryOptionalTask {
succeed_always {
AttemptBonus // Even if fails, we don't care
}
}
</code></pre>
<p><strong>Use cases:</strong></p>
<ul>
<li>Optional tasks that shouldnt block progress</li>
<li>Logging/monitoring actions</li>
<li>Best-effort operations</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">behavior QuestSequence {
then main_quest {
TalkToNPC
// Try to find secret, but don't fail quest if not found
succeed_always {
SearchForSecretDoor
}
ReturnToQuestGiver
}
}
</code></pre>
<h3 id="fail_always"><a class="header" href="#fail_always"><code>fail_always</code></a></h3>
<p>Always returns failure, regardless of childs actual result.</p>
<pre><code class="language-storybook">behavior TestFailure {
fail_always {
AlwaysSucceedsAction // But we force it to fail
}
}
</code></pre>
<p><strong>Use cases:</strong></p>
<ul>
<li>Testing/debugging</li>
<li>Forcing alternative paths</li>
<li>Disabling branches temporarily</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">behavior UnderConstruction {
choose abilities {
// Temporarily disabled feature
fail_always {
NewExperimentalAbility
}
// Fallback to old ability
ClassicAbility
}
}
</code></pre>
<h2 id="combining-decorators"><a class="header" href="#combining-decorators">Combining Decorators</a></h2>
<p>Decorators can nest to create complex behaviors:</p>
<pre><code class="language-storybook">behavior ComplexPattern {
// Repeat 3 times, each with 10 second timeout
repeat(3) {
timeout(10s) {
SolveSubproblem
}
}
}
</code></pre>
<p><strong>More complex nesting:</strong></p>
<pre><code class="language-storybook">behavior ResilientAction {
// If: Only if health &gt; 30
if(health &gt; 30) {
// Timeout: Must complete in 20 seconds
timeout(20s) {
// Retry: Try up to 5 times
retry(5) {
// Cooldown: Can only run once per minute
cooldown(1m) {
PerformComplexAction
}
}
}
}
}
</code></pre>
<p><strong>Execution order:</strong> Outside → Inside</p>
<ol>
<li>If checks condition</li>
<li>Timeout starts timer</li>
<li>Retry begins first attempt</li>
<li>Cooldown checks last execution time</li>
<li>Child action runs</li>
</ol>
<h2 id="duration-syntax"><a class="header" href="#duration-syntax">Duration Syntax</a></h2>
<p>Timeout and cooldown decorators use duration literals:</p>
<pre><code class="language-bnf">&lt;duration-literal&gt; ::= &lt;number&gt; &lt;unit&gt;
&lt;unit&gt; ::= "s" // seconds
| "m" // minutes
| "h" // hours
| "d" // days
&lt;number&gt; ::= &lt;digit&gt;+
</code></pre>
<p><strong>Examples:</strong></p>
<ul>
<li><code>5s</code> - 5 seconds</li>
<li><code>30s</code> - 30 seconds</li>
<li><code>2m</code> - 2 minutes</li>
<li><code>10m</code> - 10 minutes</li>
<li><code>1h</code> - 1 hour</li>
<li><code>24h</code> - 24 hours</li>
<li><code>7d</code> - 7 days</li>
</ul>
<p><strong>Validation:</strong></p>
<ul>
<li>Number must be positive integer</li>
<li>No compound durations (use <code>120s</code> not <code>2m</code> if runtime expects seconds)</li>
<li>No fractional units (<code>1.5m</code> not allowed; use <code>90s</code>)</li>
</ul>
<h2 id="comparison-table"><a class="header" href="#comparison-table">Comparison Table</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Decorator</th><th>Affects Success</th><th>Affects Failure</th><th>Affects Running</th><th>Stateful</th></tr></thead><tbody>
<tr><td><code>repeat</code></td><td>Repeat</td><td>Repeat</td><td>Wait</td><td>Yes (counter)</td></tr>
<tr><td><code>repeat N</code></td><td>Repeat</td><td>Repeat</td><td>Wait</td><td>Yes (counter)</td></tr>
<tr><td><code>repeat min..max</code></td><td>Repeat</td><td>Repeat</td><td>Wait</td><td>Yes (counter)</td></tr>
<tr><td><code>invert</code></td><td>→ Failure</td><td>→ Success</td><td>Unchanged</td><td>No</td></tr>
<tr><td><code>retry N</code></td><td>→ Success</td><td>Retry or fail</td><td>Wait</td><td>Yes (attempts)</td></tr>
<tr><td><code>timeout dur</code></td><td>→ Success</td><td>→ Success</td><td>→ Failure if expired</td><td>Yes (timer)</td></tr>
<tr><td><code>cooldown dur</code></td><td>→ Success</td><td>→ Success</td><td>→ Success</td><td>Yes (last time)</td></tr>
<tr><td><code>if(expr)</code></td><td>→ Success</td><td>→ Success</td><td>→ Success</td><td>No</td></tr>
<tr><td><code>succeed_always</code></td><td>→ Success</td><td>→ Success</td><td>→ Success</td><td>No</td></tr>
<tr><td><code>fail_always</code></td><td>→ Failure</td><td>→ Failure</td><td>→ Failure</td><td>No</td></tr>
</tbody></table>
</div>
<p><strong>Stateful decorators</strong> maintain state across ticks. <strong>Stateless decorators</strong> evaluate fresh every tick.</p>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>Child required</strong>: All decorators must have exactly one child node</li>
<li><strong>Repeat count</strong>: <code>repeat N</code> requires N ≥ 0</li>
<li><strong>Repeat range</strong>: <code>repeat min..max</code> requires 0 ≤ min ≤ max</li>
<li><strong>Retry count</strong>: <code>retry N</code> requires N ≥ 1</li>
<li><strong>Duration positive</strong>: Timeout/cooldown durations must be &gt; 0</li>
<li><strong>Duration format</strong>: Must match <code>&lt;number&gt;&lt;unit&gt;</code> (e.g., <code>10s</code>, <code>5m</code>)</li>
<li><strong>Guard expression</strong>: Guard condition must be valid expression</li>
<li><strong>No empty decorators</strong>: <code>repeat { }</code> is invalid (missing child)</li>
</ol>
<h2 id="use-cases-by-category"><a class="header" href="#use-cases-by-category">Use Cases by Category</a></h2>
<h3 id="timing-control"><a class="header" href="#timing-control">Timing Control</a></h3>
<ul>
<li><strong>timeout</strong>: Prevent infinite loops, enforce time limits</li>
<li><strong>cooldown</strong>: Rate-limit abilities, prevent spam</li>
<li><strong>repeat</strong>: Continuous processes, patrols</li>
</ul>
<h3 id="reliability"><a class="header" href="#reliability">Reliability</a></h3>
<ul>
<li><strong>retry</strong>: Handle unreliable actions, probabilistic success</li>
<li><strong>if</strong>: Precondition checks, resource validation</li>
<li><strong>succeed_always</strong>: Optional tasks, best-effort</li>
</ul>
<h3 id="logic-control"><a class="header" href="#logic-control">Logic Control</a></h3>
<ul>
<li><strong>invert</strong>: Negate conditions, flip results</li>
<li><strong>fail_always</strong>: Disable branches, testing</li>
</ul>
<h3 id="iteration"><a class="header" href="#iteration">Iteration</a></h3>
<ul>
<li><strong>repeat N</strong>: Fixed loops, deterministic behavior</li>
<li><strong>repeat min..max</strong>: Variable loops, procedural variation</li>
</ul>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<h3 id="1-use-guards-for-expensive-checks"><a class="header" href="#1-use-guards-for-expensive-checks">1. Use Guards for Expensive Checks</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">then expensive {
if(complex_condition)
ExpensiveOperation
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">if(complex_condition) {
ExpensiveOperation // Only runs if condition passes
}
</code></pre>
<h3 id="2-combine-timeout-with-retry"><a class="header" href="#2-combine-timeout-with-retry">2. Combine Timeout with Retry</a></h3>
<p><strong>Avoid:</strong> Infinite retry loops</p>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">timeout(30s) {
retry(5) {
UnreliableAction
}
}
</code></pre>
<h3 id="3-use-cooldowns-for-rate-limiting"><a class="header" href="#3-use-cooldowns-for-rate-limiting">3. Use Cooldowns for Rate Limiting</a></h3>
<p><strong>Avoid:</strong> Manual timing in actions</p>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">cooldown(10s) {
FireCannon
}
</code></pre>
<h3 id="4-invert-for-readable-conditions"><a class="header" href="#4-invert-for-readable-conditions">4. Invert for Readable Conditions</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">choose options {
then branch_a {
if(not is_dangerous)
Explore
}
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">choose options {
then branch_a {
invert { IsDangerous }
Explore
}
}
</code></pre>
<h3 id="5-succeed_always-for-optional-tasks"><a class="header" href="#5-succeed_always-for-optional-tasks">5. succeed_always for Optional Tasks</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">then quest {
MainTask
choose optional {
BonusTask
DoNothing // Awkward fallback
}
NextTask
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">then quest {
MainTask
succeed_always { BonusTask } // Try bonus, don't fail quest
NextTask
}
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> - Using decorators in behavior trees</li>
<li><a href="./17-expressions.html">Expression Language</a> - Guard condition syntax</li>
<li><a href="./18-value-types.html">Value Types</a> - Duration literals</li>
<li><a href="../advanced/20-patterns.html">Design Patterns</a> - Common decorator patterns</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Composability</strong>: Decorators can nest for complex control flow</li>
<li><strong>Separation of concerns</strong>: Decorators handle control flow, children handle logic</li>
<li><strong>State management</strong>: Stateful decorators (repeat, retry, timeout, cooldown) persist across ticks</li>
<li><strong>Performance</strong>: Guards prevent unnecessary child execution</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/11-behavior-trees.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/13-life-arcs.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/11-behavior-trees.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/13-life-arcs.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,845 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Life Arcs - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="life-arcs"><a class="header" href="#life-arcs">Life Arcs</a></h1>
<p>Life arcs are state machines that model how characters or other entities evolve over time. They define discrete states and the conditions under which an entity transitions between states. Life arcs capture character development, quest progress, relationship phases, and any other finite-state processes.</p>
<h2 id="what-is-a-life-arc"><a class="header" href="#what-is-a-life-arc">What is a Life Arc?</a></h2>
<p>A life arc is a finite state machine (FSM) composed of:</p>
<ul>
<li><strong>States</strong>: Discrete phases or modes (e.g., “happy”, “angry”, “sleeping”)</li>
<li><strong>Transitions</strong>: Conditional edges between states (e.g., “when health &lt; 20, go to fleeing”)</li>
<li><strong>On-enter actions</strong>: Field updates that occur when entering a state</li>
</ul>
<pre><code>[State A] --condition--&gt; [State B] --condition--&gt; [State C]
| | |
on enter on enter on enter
(set fields) (set fields) (set fields)
</code></pre>
<p>Life arcs run continuously, evaluating transitions every tick and moving between states as conditions become true.</p>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;life-arc-decl&gt; ::= "life_arc" &lt;identifier&gt; &lt;body&gt;
&lt;body&gt; ::= "{" &lt;prose-blocks&gt;* &lt;state&gt;+ "}"
&lt;state&gt; ::= "state" &lt;identifier&gt; "{" &lt;state-body&gt;* "}"
&lt;state-body&gt; ::= &lt;on-enter-block&gt;
| &lt;transition&gt;
| &lt;prose-block&gt;
&lt;on-enter-block&gt; ::= "on" "enter" "{" &lt;field&gt;+ "}"
&lt;transition&gt; ::= "on" &lt;expression&gt; "-&gt;" &lt;identifier&gt;
&lt;prose-block&gt; ::= "---" &lt;identifier&gt; &lt;content&gt; "---"
</code></pre>
<h2 id="states"><a class="header" href="#states">States</a></h2>
<p>A state represents a discrete phase in an entitys lifecycle.</p>
<h3 id="basic-state"><a class="header" href="#basic-state">Basic State</a></h3>
<pre><code class="language-storybook">life_arc SimpleArc {
state idle {
---narrative
The character is doing nothing, waiting for something to happen.
---
}
state active {
---narrative
The character is engaged in their primary activity.
---
}
}
</code></pre>
<h3 id="state-names"><a class="header" href="#state-names">State Names</a></h3>
<p>State names are identifiers that must be unique within the life arc. Use descriptive names:</p>
<ul>
<li><strong>Good</strong>: <code>idle</code>, <code>combat</code>, <code>sleeping</code>, <code>enlightened</code></li>
<li><strong>Avoid</strong>: <code>state1</code>, <code>s</code>, <code>temp</code></li>
</ul>
<h2 id="on-enter-actions"><a class="header" href="#on-enter-actions">On-Enter Actions</a></h2>
<p>When entering a state, the <code>on enter</code> block updates entity fields.</p>
<h3 id="syntax-1"><a class="header" href="#syntax-1">Syntax</a></h3>
<pre><code class="language-storybook">state state_name {
on enter {
EntityName.field_name: value
EntityName.other_field: other_value
}
}
</code></pre>
<h3 id="examples"><a class="header" href="#examples">Examples</a></h3>
<pre><code class="language-storybook">life_arc CharacterMood {
state happy {
on enter {
Martha.emotional_state: "happy"
Martha.energy: 100
}
}
state tired {
on enter {
Martha.emotional_state: "exhausted"
Martha.energy: 20
}
}
}
</code></pre>
<h3 id="field-update-semantics"><a class="header" href="#field-update-semantics">Field Update Semantics</a></h3>
<ul>
<li><strong>Target</strong>: <code>EntityName.field_name</code> must reference an existing character/entity</li>
<li><strong>Value</strong>: Any valid <a href="./18-value-types.html">value type</a></li>
<li><strong>Effect</strong>: Field is set to the specified value when state is entered</li>
<li><strong>Timing</strong>: Happens immediately upon transition to the state</li>
</ul>
<h3 id="multiple-field-updates"><a class="header" href="#multiple-field-updates">Multiple Field Updates</a></h3>
<pre><code class="language-storybook">state exhausted_baker {
on enter {
Martha.energy: 0.1
Martha.mood: "stressed"
Martha.quality_output: 0.7
Martha.needs_break: true
}
---narrative
After a sixteen-hour shift during the harvest festival,
Martha's hands are shaking. She knows her bread quality is
suffering and reluctantly steps away from the oven.
---
}
</code></pre>
<h2 id="transitions"><a class="header" href="#transitions">Transitions</a></h2>
<p>Transitions define conditional edges between states. When a transitions condition becomes true, the state machine moves to the target state.</p>
<h3 id="syntax-2"><a class="header" href="#syntax-2">Syntax</a></h3>
<pre><code class="language-storybook">state source_state {
on condition_expression -&gt; target_state
on another_condition -&gt; another_target
}
</code></pre>
<h3 id="basic-transitions"><a class="header" href="#basic-transitions">Basic Transitions</a></h3>
<pre><code class="language-storybook">life_arc Combat {
state fighting {
on health &lt; 20 -&gt; fleeing
on enemy_defeated -&gt; victorious
}
state fleeing {
on safe_distance_reached -&gt; idle
}
state victorious {
on celebration_complete -&gt; idle
}
state idle {}
}
</code></pre>
<h3 id="expression-conditions"><a class="header" href="#expression-conditions">Expression Conditions</a></h3>
<p>Transitions use the full <a href="./17-expressions.html">expression language</a>:</p>
<p><strong>Comparisons:</strong></p>
<pre><code class="language-storybook">on health &lt; 20 -&gt; low_health
on distance &gt; 100 -&gt; far_away
on count == 0 -&gt; empty
</code></pre>
<p><strong>Boolean fields:</strong></p>
<pre><code class="language-storybook">on is_hostile -&gt; combat
on completed -&gt; done
on not ready -&gt; waiting
</code></pre>
<p><strong>Logical operators:</strong></p>
<pre><code class="language-storybook">on health &lt; 20 and not has_potion -&gt; desperate
on is_day or is_lit -&gt; visible
</code></pre>
<p><strong>Complex conditions:</strong></p>
<pre><code class="language-storybook">on (health &lt; 50 and enemy_count &gt; 3) or surrounded -&gt; retreat
on forall e in enemies: e.defeated -&gt; victory
</code></pre>
<h3 id="multiple-transitions"><a class="header" href="#multiple-transitions">Multiple Transitions</a></h3>
<p>A state can have multiple outgoing transitions:</p>
<pre><code class="language-storybook">state monitoring {
on status is "active" -&gt; active_state
on status is "inactive" -&gt; inactive_state
on status is "error" -&gt; error_state
on shutdown_requested -&gt; shutting_down
}
</code></pre>
<p><strong>Evaluation order:</strong></p>
<ul>
<li>Transitions are evaluated in declaration order (top to bottom)</li>
<li>First transition with a true condition is taken</li>
<li>If no conditions are true, state remains unchanged</li>
</ul>
<h3 id="self-transitions"><a class="header" href="#self-transitions">Self-Transitions</a></h3>
<p>A state can transition to itself:</p>
<pre><code class="language-storybook">state patrolling {
on enemy_spotted -&gt; combat
on checkpoint_reached -&gt; patrolling // Reset patrol state
}
</code></pre>
<h2 id="complete-examples"><a class="header" href="#complete-examples">Complete Examples</a></h2>
<h3 id="elenas-career-journey"><a class="header" href="#elenas-career-journey">Elenas Career Journey</a></h3>
<pre><code class="language-storybook">life_arc ElenaCareer {
---description
Tracks Elena's progression from nervous apprentice to confident
master baker. Each state represents a key phase of her career.
---
state early_apprentice {
on enter {
Elena.skill_level: novice
Elena.confidence: timid
}
on recipes_mastered &gt; 5 -&gt; growing_apprentice
---narrative
Elena's hands shake as she measures flour. She checks the
recipe three times before adding each ingredient. Martha
patiently corrects her technique.
---
}
state growing_apprentice {
on enter {
Elena.skill_level: beginner
Elena.confidence: uncertain
}
on recipes_mastered &gt; 15 -&gt; journeyman
---narrative
The shaking stops. Elena can make basic breads without
looking at the recipe. She still doubts herself but
Martha's encouragement is taking root.
---
}
state journeyman {
on enter {
Elena.skill_level: intermediate
Elena.confidence: growing
Elena.can_work_independently: true
}
on recipes_mastered &gt; 30 -&gt; senior_journeyman
---narrative
Elena runs the morning shift alone while Martha handles
special orders. Customers start asking for "Elena's rolls."
She begins experimenting with her own recipes.
---
}
state senior_journeyman {
on enter {
Elena.skill_level: advanced
Elena.confidence: steady
}
on recipes_mastered &gt; 50 -&gt; master
---narrative
Elena develops her signature recipe: rosemary olive bread
that becomes the bakery's bestseller. She handles difficult
customers with grace and trains new helpers.
---
}
state master {
on enter {
Elena.skill_level: master
Elena.confidence: commanding
Elena.can_teach: true
}
---narrative
Master Baker Elena. She has earned it. The guild acknowledges
her mastery, and Martha beams with pride. Elena begins
mentoring her own apprentice.
---
}
}
</code></pre>
<h3 id="quest-progress"><a class="header" href="#quest-progress">Quest Progress</a></h3>
<pre><code class="language-storybook">life_arc HeroQuest {
state not_started {
on talked_to_elder -&gt; received_quest
}
state received_quest {
on enter {
Hero.quest_active: true
Hero.quest_step: 0
}
on found_first_artifact -&gt; collecting
}
state collecting {
on enter {
Hero.quest_step: 1
}
on artifact_count == 3 -&gt; returning
on failed_trial -&gt; failed
}
state returning {
on enter {
Hero.quest_step: 2
}
on reached_elder -&gt; completed
}
state completed {
on enter {
Hero.quest_active: false
Hero.quest_completed: true
Hero.reputation: 100
}
---narrative
The hero returns triumphant, artifacts in hand. The elder
bestows great rewards and the village celebrates.
---
}
state failed {
on enter {
Hero.quest_active: false
Hero.quest_failed: true
}
on retry_accepted -&gt; received_quest
}
}
</code></pre>
<h3 id="name-checker-equality-examples"><a class="header" href="#name-checker-equality-examples">Name Checker (Equality Examples)</a></h3>
<pre><code class="language-storybook">life_arc NameCheck {
state checking {
on name is "Martha" -&gt; found_martha
on name is "Jane" -&gt; found_jane
}
state found_martha {
on ready -&gt; checking
}
state found_jane {
on ready -&gt; checking
}
}
</code></pre>
<h3 id="status-monitor"><a class="header" href="#status-monitor">Status Monitor</a></h3>
<pre><code class="language-storybook">life_arc StatusMonitor {
state monitoring {
on status is active -&gt; active_state
on status is inactive -&gt; inactive_state
on status is error -&gt; error_state
}
state active_state {
on enter {
System.led_color: "green"
}
on status is inactive -&gt; inactive_state
on status is error -&gt; error_state
}
state inactive_state {
on enter {
System.led_color: "yellow"
}
on status is active -&gt; active_state
on status is error -&gt; error_state
}
state error_state {
on enter {
System.led_color: "red"
System.alarm: true
}
on error_cleared -&gt; monitoring
}
}
</code></pre>
<h3 id="character-mood-swings"><a class="header" href="#character-mood-swings">Character Mood Swings</a></h3>
<pre><code class="language-storybook">life_arc MoodSwings {
state neutral {
on provoked -&gt; angry
on complimented -&gt; happy
on tired -&gt; sleepy
}
state angry {
on enter {
Character.aggression: 0.9
Character.willingness_to_talk: 0.1
}
on calmed_down -&gt; neutral
on escalated -&gt; furious
}
state furious {
on enter {
Character.aggression: 1.0
Character.will_attack: true
}
on timeout_elapsed -&gt; angry
on apologized_to -&gt; neutral
}
state happy {
on enter {
Character.aggression: 0.0
Character.willingness_to_talk: 1.0
Character.gives_discounts: true
}
on insulted -&gt; neutral
on bored -&gt; neutral
}
state sleepy {
on enter {
Character.responsiveness: 0.2
}
on woke_up -&gt; neutral
on fell_asleep -&gt; sleeping
}
state sleeping {
on enter {
Character.responsiveness: 0.0
Character.is_vulnerable: true
}
on woke_up -&gt; neutral
}
}
</code></pre>
<h2 id="execution-semantics"><a class="header" href="#execution-semantics">Execution Semantics</a></h2>
<h3 id="tick-based-evaluation"><a class="header" href="#tick-based-evaluation">Tick-Based Evaluation</a></h3>
<p>Life arcs evaluate transitions every “tick” (typically once per frame):</p>
<ol>
<li><strong>Current state</strong>: Start in the current state</li>
<li><strong>Evaluate transitions</strong>: Check each outgoing transitions condition (in order)</li>
<li><strong>First true transition</strong>: Take the first transition with a true condition</li>
<li><strong>Enter new state</strong>: Execute the new states <code>on enter</code> block</li>
<li><strong>Repeat next tick</strong></li>
</ol>
<h3 id="transition-priority"><a class="header" href="#transition-priority">Transition Priority</a></h3>
<p>When multiple transitions could fire, the <strong>first one in declaration order</strong> is taken:</p>
<pre><code class="language-storybook">state combat {
on health &lt; 10 -&gt; desperate // Checked first
on health &lt; 50 -&gt; defensive // Checked second
on surrounded -&gt; retreat // Checked third
}
</code></pre>
<p>If health is 5, only <code>desperate</code> transition fires (even though <code>defensive</code> condition is also true).</p>
<h3 id="state-persistence"><a class="header" href="#state-persistence">State Persistence</a></h3>
<p>The current state persists across ticks until a transition fires.</p>
<pre><code class="language-storybook">state waiting {
on signal_received -&gt; active
}
</code></pre>
<p>The entity remains in <code>waiting</code> state indefinitely until <code>signal_received</code> becomes true.</p>
<h3 id="on-enter-execution"><a class="header" href="#on-enter-execution">On-Enter Execution</a></h3>
<p><code>on enter</code> blocks execute <strong>once</strong> when entering the state, not every tick:</p>
<pre><code class="language-storybook">state combat {
on enter {
Character.weapon_drawn: true // Runs once when entering combat
}
}
</code></pre>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>At least one state</strong>: Life arc must contain at least one state</li>
<li><strong>Unique state names</strong>: State names must be unique within the life arc</li>
<li><strong>Valid transitions</strong>: Transition targets must reference defined states</li>
<li><strong>No orphan states</strong>: All states should be reachable (warning, not error)</li>
<li><strong>Expression validity</strong>: Transition conditions must be well-formed expressions</li>
<li><strong>Field targets</strong>: On-enter field updates must reference valid entities/fields</li>
<li><strong>No cyclic immediate transitions</strong>: Avoid transitions that fire immediately in a loop</li>
</ol>
<h2 id="design-patterns"><a class="header" href="#design-patterns">Design Patterns</a></h2>
<h3 id="1-hub-and-spoke"><a class="header" href="#1-hub-and-spoke">1. Hub-and-Spoke</a></h3>
<p>A central “hub” state with transitions to specialized states:</p>
<pre><code class="language-storybook">life_arc HubPattern {
state idle {
on combat_triggered -&gt; combat
on quest_accepted -&gt; questing
on entered_shop -&gt; shopping
}
state combat {
on combat_ended -&gt; idle
}
state questing {
on quest_completed -&gt; idle
}
state shopping {
on left_shop -&gt; idle
}
}
</code></pre>
<h3 id="2-linear-progression"><a class="header" href="#2-linear-progression">2. Linear Progression</a></h3>
<p>States form a linear sequence (quests, tutorials):</p>
<pre><code class="language-storybook">life_arc Tutorial {
state intro {
on clicked_start -&gt; movement
}
state movement {
on moved_forward -&gt; combat
}
state combat {
on defeated_enemy -&gt; inventory
}
state inventory {
on opened_inventory -&gt; complete
}
state complete {}
}
</code></pre>
<h3 id="3-cyclic-states"><a class="header" href="#3-cyclic-states">3. Cyclic States</a></h3>
<p>States form a cycle (day/night, seasons):</p>
<pre><code class="language-storybook">life_arc DayNightCycle {
state dawn {
on hour &gt;= 8 -&gt; day
}
state day {
on hour &gt;= 18 -&gt; dusk
}
state dusk {
on hour &gt;= 20 -&gt; night
}
state night {
on hour &gt;= 6 -&gt; dawn
}
}
</code></pre>
<h3 id="4-hierarchical-simulated"><a class="header" href="#4-hierarchical-simulated">4. Hierarchical (Simulated)</a></h3>
<p>Use multiple life arcs for hierarchical state:</p>
<pre><code class="language-storybook">// Top-level life arc
life_arc CharacterState {
state alive {
on health &lt;= 0 -&gt; dead
}
state dead {}
}
// Nested life arc (only active when alive)
life_arc CombatState {
state idle {
on enemy_nearby -&gt; combat
}
state combat {
on enemy_defeated -&gt; idle
}
}
</code></pre>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<h3 id="1-use-descriptive-state-names"><a class="header" href="#1-use-descriptive-state-names">1. Use Descriptive State Names</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">state s1 { ... }
state s2 { ... }
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">state waiting_for_player { ... }
state engaged_in_combat { ... }
</code></pre>
<h3 id="2-add-narrative-prose-blocks"><a class="header" href="#2-add-narrative-prose-blocks">2. Add Narrative Prose Blocks</a></h3>
<pre><code class="language-storybook">state master_baker {
---narrative
Master Baker Elena. She has earned it. The guild acknowledges
her mastery, and Martha beams with pride. Elena begins
mentoring her own apprentice.
---
}
</code></pre>
<h3 id="3-order-transitions-by-priority"><a class="header" href="#3-order-transitions-by-priority">3. Order Transitions by Priority</a></h3>
<pre><code class="language-storybook">state health_check {
on health &lt;= 0 -&gt; dead // Most urgent
on health &lt; 20 -&gt; critical // Very urgent
on health &lt; 50 -&gt; wounded // Moderate
}
</code></pre>
<h3 id="4-avoid-orphan-states"><a class="header" href="#4-avoid-orphan-states">4. Avoid Orphan States</a></h3>
<p>Ensure all states are reachable from the initial state.</p>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">life_arc Broken {
state start {
on ready -&gt; middle
}
state middle {
on done -&gt; end
}
state unreachable {} // No transition leads here!
state end {}
}
</code></pre>
<h3 id="5-use-on-enter-for-state-initialization"><a class="header" href="#5-use-on-enter-for-state-initialization">5. Use On-Enter for State Initialization</a></h3>
<pre><code class="language-storybook">state combat {
on enter {
Character.weapon_drawn: true
Character.combat_stance: "aggressive"
Character.target: nearest_enemy
}
}
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> - Characters can have associated life arcs</li>
<li><a href="./17-expressions.html">Expression Language</a> - Transition condition syntax</li>
<li><a href="./18-value-types.html">Value Types</a> - On-enter field value types</li>
<li><a href="./19-validation.html">Validation Rules</a> - Life arc validation constraints</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Finite State Machines (FSM)</strong>: Life arcs are FSMs</li>
<li><strong>Character development</strong>: Track character growth over time</li>
<li><strong>Quest states</strong>: Model quest progression</li>
<li><strong>Mood systems</strong>: Model emotional states</li>
<li><strong>Lifecycle modeling</strong>: Birth, growth, aging, death</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/12-decorators.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/14-schedules.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/12-decorators.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/14-schedules.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,865 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Schedules - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="schedules"><a class="header" href="#schedules">Schedules</a></h1>
<p>Schedules define time-based routines for characters and institutions. They specify what activities occur during specific time ranges, support seasonal variations, recurring events, and template composition. Schedules enable rich temporal behavior from simple daily routines to complex year-long patterns.</p>
<h2 id="what-is-a-schedule"><a class="header" href="#what-is-a-schedule">What is a Schedule?</a></h2>
<p>A schedule is a collection of time blocks that define activities throughout a day, week, season, or year:</p>
<ul>
<li><strong>Blocks</strong>: Time ranges (e.g., <code>06:00 - 14:00</code>) with associated activities/behaviors</li>
<li><strong>Temporal constraints</strong>: When blocks apply (season, day of week, month)</li>
<li><strong>Recurrence patterns</strong>: Repeating events (e.g., “Market Day every Earthday”)</li>
<li><strong>Composition</strong>: Schedules can extend and override other schedules</li>
</ul>
<pre><code>Schedule: BakerySchedule
├─ Block: 06:00 - 08:00 → prepare_dough
├─ Block: 08:00 - 14:00 → serve_customers
├─ Recurrence: Market Day (on Earthday) → special_market_hours
└─ Extends: BaseBusiness (inherits closing hours)
</code></pre>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;schedule-decl&gt; ::= "schedule" &lt;identifier&gt; &lt;extends-clause&gt;? &lt;body&gt;
&lt;extends-clause&gt; ::= "extends" &lt;identifier&gt;
&lt;body&gt; ::= "{" &lt;schedule-item&gt;* "}"
&lt;schedule-item&gt; ::= &lt;schedule-block&gt;
| &lt;recurrence-pattern&gt;
&lt;schedule-block&gt; ::= "block" &lt;block-name&gt;? "{" &lt;block-content&gt;+ "}"
&lt;block-name&gt; ::= &lt;identifier&gt;
&lt;block-content&gt; ::= &lt;time-range&gt;
| "action" ":" &lt;qualified-path&gt;
| &lt;temporal-constraint&gt;
| &lt;field&gt;
&lt;time-range&gt; ::= &lt;time&gt; "-" &lt;time&gt;
&lt;temporal-constraint&gt; ::= "on" &lt;temporal-spec&gt;
&lt;temporal-spec&gt; ::= "season" &lt;identifier&gt;
| "day" &lt;identifier&gt;
| "month" &lt;identifier&gt;
| "dates" &lt;string&gt; ".." &lt;string&gt;
&lt;recurrence-pattern&gt; ::= "recurs" &lt;identifier&gt; &lt;temporal-constraint&gt; "{" &lt;schedule-block&gt;+ "}"
</code></pre>
<h2 id="schedule-blocks"><a class="header" href="#schedule-blocks">Schedule Blocks</a></h2>
<p>A schedule block defines an activity during a specific time range.</p>
<h3 id="basic-block"><a class="header" href="#basic-block">Basic Block</a></h3>
<pre><code class="language-storybook">schedule SimpleBaker {
block {
06:00 - 14:00
action: baking::prepare_and_sell
}
}
</code></pre>
<h3 id="named-block"><a class="header" href="#named-block">Named Block</a></h3>
<p>Named blocks support the override system:</p>
<pre><code class="language-storybook">schedule BakeryBase {
block work {
09:00 - 17:00
action: baking::standard_work
}
}
schedule EarlyBaker extends BakeryBase {
block work { // Override by name
05:00 - 13:00
action: baking::early_shift
}
}
</code></pre>
<h3 id="block-content"><a class="header" href="#block-content">Block Content</a></h3>
<p>Blocks can contain:</p>
<ol>
<li><strong>Time range</strong> (required): <code>HH:MM - HH:MM</code></li>
<li><strong>Action</strong> (optional): Behavior tree reference</li>
<li><strong>Temporal constraint</strong> (optional): When the block applies</li>
<li><strong>Fields</strong> (optional): Additional metadata</li>
</ol>
<pre><code class="language-storybook">schedule CompleteBlock {
block morning_work {
06:00 - 12:00
action: work::morning_routine
on season summer
location: "Outdoor Market"
}
}
</code></pre>
<h2 id="time-ranges"><a class="header" href="#time-ranges">Time Ranges</a></h2>
<p>Time ranges use 24-hour clock format.</p>
<h3 id="syntax-1"><a class="header" href="#syntax-1">Syntax</a></h3>
<pre><code class="language-storybook">HH:MM - HH:MM
</code></pre>
<ul>
<li><strong>HH</strong>: Hour (00-23)</li>
<li><strong>MM</strong>: Minute (00-59)</li>
<li>Optional seconds: <code>HH:MM:SS - HH:MM:SS</code></li>
</ul>
<h3 id="examples"><a class="header" href="#examples">Examples</a></h3>
<pre><code class="language-storybook">schedule Examples {
block early { 05:00 - 08:00, action: prep }
block midday { 12:00 - 13:00, action: lunch }
block late { 20:00 - 23:59, action: closing }
}
</code></pre>
<h3 id="overnight-ranges"><a class="header" href="#overnight-ranges">Overnight Ranges</a></h3>
<p>Blocks can span midnight:</p>
<pre><code class="language-storybook">schedule NightShift {
block night_work {
22:00 - 06:00 // 10 PM to 6 AM next day
action: security::patrol
}
}
</code></pre>
<p>The system interprets this as 22:00-24:00 (day 1) + 00:00-06:00 (day 2).</p>
<h2 id="actions"><a class="header" href="#actions">Actions</a></h2>
<p>The <code>action</code> field links a schedule block to a behavior tree.</p>
<h3 id="syntax-2"><a class="header" href="#syntax-2">Syntax</a></h3>
<pre><code class="language-storybook">action: &lt;qualified-path&gt;
</code></pre>
<h3 id="examples-1"><a class="header" href="#examples-1">Examples</a></h3>
<pre><code class="language-storybook">schedule BakerSchedule {
block morning {
05:00 - 08:00
action: baking::prepare_dough
}
block sales {
08:00 - 14:00
action: baking::serve_customers
}
block cleanup {
14:00 - 15:00
action: baking::close_shop
}
}
</code></pre>
<h3 id="qualified-paths"><a class="header" href="#qualified-paths">Qualified Paths</a></h3>
<p>Actions can reference behaviors from other modules:</p>
<pre><code class="language-storybook">schedule GuardSchedule {
block patrol {
08:00 - 16:00
action: behaviors::guards::patrol_route
}
block training {
16:00 - 18:00
action: combat::training::drills
}
}
</code></pre>
<h2 id="temporal-constraints"><a class="header" href="#temporal-constraints">Temporal Constraints</a></h2>
<p>Temporal constraints specify when a block applies (season, day of week, month, date range).</p>
<h3 id="season-constraint"><a class="header" href="#season-constraint">Season Constraint</a></h3>
<pre><code class="language-storybook">schedule SeasonalHours {
block summer_hours {
06:00 - 20:00
action: work::long_day
on season summer
}
block winter_hours {
07:00 - 18:00
action: work::short_day
on season winter
}
}
</code></pre>
<p><strong>Season values</strong> are defined by enums in your storybook:</p>
<pre><code class="language-storybook">enum Season {
spring
summer
fall
winter
}
</code></pre>
<h3 id="day-of-week-constraint"><a class="header" href="#day-of-week-constraint">Day of Week Constraint</a></h3>
<pre><code class="language-storybook">schedule WeeklyPattern {
block weekday_work {
09:00 - 17:00
action: work::standard
on day monday // Also: tuesday, wednesday, thursday, friday, saturday, sunday
}
block weekend_rest {
10:00 - 16:00
action: leisure::relax
on day saturday
}
}
</code></pre>
<p><strong>Day values</strong> are typically defined by a <code>DayOfWeek</code> enum:</p>
<pre><code class="language-storybook">enum DayOfWeek {
monday
tuesday
wednesday
thursday
friday
saturday
sunday
}
</code></pre>
<h3 id="month-constraint"><a class="header" href="#month-constraint">Month Constraint</a></h3>
<pre><code class="language-storybook">schedule AnnualPattern {
block harvest {
06:00 - 20:00
action: farming::harvest_crops
on month october
}
block spring_planting {
07:00 - 19:00
action: farming::plant_seeds
on month april
}
}
</code></pre>
<h3 id="date-range-constraint"><a class="header" href="#date-range-constraint">Date Range Constraint</a></h3>
<pre><code class="language-storybook">schedule TouristSeason {
block high_season {
08:00 - 22:00
action: tourism::busy_service
on dates "Jun 1" .. "Sep 1"
}
}
</code></pre>
<p><strong>Note</strong>: Date format is implementation-specific. Check your runtime for supported formats.</p>
<h2 id="recurrence-patterns"><a class="header" href="#recurrence-patterns">Recurrence Patterns</a></h2>
<p>Recurrence patterns define recurring events that modify the schedule.</p>
<h3 id="syntax-3"><a class="header" href="#syntax-3">Syntax</a></h3>
<pre><code class="language-storybook">recurs EventName on &lt;temporal-constraint&gt; {
&lt;schedule-block&gt;+
}
</code></pre>
<h3 id="examples-2"><a class="header" href="#examples-2">Examples</a></h3>
<h4 id="weekly-recurring-event"><a class="header" href="#weekly-recurring-event">Weekly Recurring Event</a></h4>
<pre><code class="language-storybook">schedule MarketSchedule {
// Regular daily hours
block work {
08:00 - 17:00
action: shop::regular_sales
}
// Market day every saturday
recurs MarketDay on day saturday {
block setup {
06:00 - 08:00
action: market::setup_stall
}
block busy_market {
08:00 - 18:00
action: market::busy_sales
}
block teardown {
18:00 - 20:00
action: market::pack_up
}
}
}
</code></pre>
<h4 id="monthly-recurring-event"><a class="header" href="#monthly-recurring-event">Monthly Recurring Event</a></h4>
<pre><code class="language-storybook">schedule TownSchedule {
recurs CouncilMeeting on month first_monday {
block meeting {
18:00 - 21:00
action: governance::council_session
}
}
}
</code></pre>
<h4 id="seasonal-recurring-event"><a class="header" href="#seasonal-recurring-event">Seasonal Recurring Event</a></h4>
<pre><code class="language-storybook">schedule FarmSchedule {
recurs SpringPlanting on season spring {
block planting {
06:00 - 18:00
action: farming::plant_all_fields
}
}
recurs FallHarvest on season fall {
block harvest {
06:00 - 20:00
action: farming::harvest_all_crops
}
}
}
</code></pre>
<h2 id="schedule-composition"><a class="header" href="#schedule-composition">Schedule Composition</a></h2>
<p>Schedules can extend other schedules using <code>extends</code>, inheriting and overriding blocks.</p>
<h3 id="extends-clause"><a class="header" href="#extends-clause">Extends Clause</a></h3>
<pre><code class="language-storybook">schedule Base {
block work {
09:00 - 17:00
action: work::standard
}
}
schedule Extended extends Base {
block work { // Override inherited block
05:00 - 13:00
action: work::early_shift
}
}
</code></pre>
<h3 id="override-semantics"><a class="header" href="#override-semantics">Override Semantics</a></h3>
<p>When extending a schedule:</p>
<ol>
<li><strong>Named blocks</strong> with matching names override base blocks</li>
<li><strong>Unnamed blocks</strong> are appended</li>
<li><strong>Time range</strong> can change</li>
<li><strong>Action</strong> can change</li>
<li><strong>Constraints</strong> can be added/changed</li>
</ol>
<h3 id="multiple-levels"><a class="header" href="#multiple-levels">Multiple Levels</a></h3>
<pre><code class="language-storybook">schedule BaseWork {
block work {
09:00 - 17:00
action: work::standard
}
}
schedule BakerWork extends BaseWork {
block work {
05:00 - 13:00 // Earlier hours
action: baking::work
}
}
schedule MasterBaker extends BakerWork {
block work {
03:00 - 11:00 // Even earlier!
action: baking::master_work
}
block teaching {
14:00 - 16:00
action: baking::teach_apprentice
}
}
</code></pre>
<p><strong>Resolution:</strong></p>
<ul>
<li><code>work</code> block: 03:00-11:00 with <code>baking::master_work</code> (MasterBaker overrides BakerWork overrides BaseWork)</li>
<li><code>teaching</code> block: 14:00-16:00 with <code>baking::teach_apprentice</code> (from MasterBaker)</li>
</ul>
<h2 id="complete-examples"><a class="header" href="#complete-examples">Complete Examples</a></h2>
<h3 id="simple-daily-schedule"><a class="header" href="#simple-daily-schedule">Simple Daily Schedule</a></h3>
<pre><code class="language-storybook">schedule SimpleFarmer {
block sleep {
22:00 - 05:00
action: rest::sleep
}
block morning_chores {
05:00 - 08:00
action: farming::feed_animals
}
block fieldwork {
08:00 - 17:00
action: farming::tend_crops
}
block evening_chores {
17:00 - 19:00
action: farming::evening_tasks
}
block leisure {
19:00 - 22:00
action: social::family_time
}
}
</code></pre>
<h3 id="seasonal-variation"><a class="header" href="#seasonal-variation">Seasonal Variation</a></h3>
<pre><code class="language-storybook">schedule SeasonalBaker {
block summer_work {
06:00 - 20:00
action: baking::long_shift
on season summer
}
block winter_work {
07:00 - 18:00
action: baking::short_shift
on season winter
}
block spring_work {
06:30 - 19:00
action: baking::medium_shift
on season spring
}
block fall_work {
06:30 - 19:00
action: baking::medium_shift
on season fall
}
}
</code></pre>
<h3 id="weekly-pattern-with-recurrence"><a class="header" href="#weekly-pattern-with-recurrence">Weekly Pattern with Recurrence</a></h3>
<pre><code class="language-storybook">schedule InnkeeperSchedule {
// Weekday routine
block weekday_service {
08:00 - 22:00
action: inn::regular_service
on day monday // Repeat for each weekday
}
block weekday_service {
08:00 - 22:00
action: inn::regular_service
on day tuesday
}
// ... (wednesday, thursday, friday)
// Weekend hours
block weekend_service {
10:00 - 24:00
action: inn::busy_service
on day saturday
}
block weekend_service {
10:00 - 24:00
action: inn::busy_service
on day sunday
}
// Weekly market day
recurs MarketDay on day wednesday {
block market_prep {
06:00 - 08:00
action: inn::prepare_market_goods
}
block market_rush {
08:00 - 16:00
action: inn::market_day_chaos
}
}
}
</code></pre>
<h3 id="extended-schedule"><a class="header" href="#extended-schedule">Extended Schedule</a></h3>
<pre><code class="language-storybook">schedule BaseShopkeeper {
block open {
09:00 - 17:00
action: shop::standard_hours
}
}
schedule BlacksmithSchedule extends BaseShopkeeper {
block open { // Override
06:00 - 18:00 // Longer hours
action: smithing::work
}
block forge_heat { // New block
05:00 - 06:00
action: smithing::heat_forge
}
}
</code></pre>
<h3 id="complex-year-round-schedule"><a class="header" href="#complex-year-round-schedule">Complex Year-Round Schedule</a></h3>
<pre><code class="language-storybook">schedule MasterBakerYear {
// Daily base pattern
block prep {
04:00 - 06:00
action: baking::prepare
}
block baking {
06:00 - 10:00
action: baking::bake
}
block sales {
10:00 - 16:00
action: baking::serve
}
block cleanup {
16:00 - 17:00
action: baking::clean
}
// Seasonal variations
block summer_hours {
10:00 - 20:00 // Extended sales
action: baking::busy_summer
on season summer
}
// Weekly market
recurs MarketDay on day saturday {
block market_prep {
02:00 - 04:00
action: baking::market_prep
}
block market_sales {
08:00 - 18:00
action: baking::market_rush
}
}
// Annual events
recurs HarvestFestival on dates "Sep 20" .. "Sep 25" {
block festival {
06:00 - 23:00
action: baking::festival_mode
}
}
}
</code></pre>
<h2 id="integration-with-characters"><a class="header" href="#integration-with-characters">Integration with Characters</a></h2>
<p>Characters link to schedules using the <code>uses schedule</code> clause:</p>
<pre><code class="language-storybook">character Martha {
uses schedule: BakerySchedule
}
</code></pre>
<p><strong>Multiple schedules:</strong></p>
<pre><code class="language-storybook">character Innkeeper {
uses schedules: [WeekdaySchedule, WeekendSchedule]
}
</code></pre>
<p>See <a href="./10-characters.html#schedule-integration">Characters</a> for complete integration syntax.</p>
<h2 id="execution-semantics"><a class="header" href="#execution-semantics">Execution Semantics</a></h2>
<h3 id="time-based-selection"><a class="header" href="#time-based-selection">Time-Based Selection</a></h3>
<p>At any given time, the runtime:</p>
<ol>
<li><strong>Evaluates temporal constraints</strong>: Which blocks apply right now?</li>
<li><strong>Selects matching block</strong>: First block whose time range and constraint match</li>
<li><strong>Executes action</strong>: Runs the associated behavior tree</li>
<li><strong>Repeats next tick</strong></li>
</ol>
<h3 id="priority-order"><a class="header" href="#priority-order">Priority Order</a></h3>
<p>When multiple blocks could apply:</p>
<ol>
<li><strong>Recurrences</strong> take priority over regular blocks</li>
<li><strong>Constraints</strong> filter blocks (season, day, month)</li>
<li><strong>Time ranges</strong> must overlap current time</li>
<li><strong>First match</strong> wins (declaration order)</li>
</ol>
<h3 id="block-overlap"><a class="header" href="#block-overlap">Block Overlap</a></h3>
<p>Blocks can overlap in time. The runtime selects the first matching block.</p>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">schedule Overlapping {
block general {
08:00 - 17:00
action: work::general
}
block specialized {
10:00 - 12:00 // Overlaps with general
action: work::specialized
on day wednesday
}
}
</code></pre>
<p>On Wednesday at 11:00, <code>specialized</code> block is selected (more specific constraint).</p>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>Time format</strong>: Times must be valid HH:MM or HH:MM:SS (24-hour)</li>
<li><strong>Time order</strong>: Start time must be before end time (unless spanning midnight)</li>
<li><strong>Extends exists</strong>: If using <code>extends</code>, base schedule must be defined</li>
<li><strong>No circular extends</strong>: Cannot form circular dependency chains</li>
<li><strong>Named blocks unique</strong>: Block names must be unique within a schedule</li>
<li><strong>Action exists</strong>: Action references must resolve to defined behaviors</li>
<li><strong>Constraint values</strong>: Temporal constraint values must reference defined enums</li>
<li><strong>Recurrence names unique</strong>: Recurrence names must be unique within a schedule</li>
</ol>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<h3 id="1-use-named-blocks-for-overrides"><a class="header" href="#1-use-named-blocks-for-overrides">1. Use Named Blocks for Overrides</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">schedule Extended extends Base {
block { 05:00 - 13:00, action: work } // Appends instead of overriding
}
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">schedule Extended extends Base {
block work { 05:00 - 13:00, action: early_work } // Overrides by name
}
</code></pre>
<h3 id="2-group-related-blocks"><a class="header" href="#2-group-related-blocks">2. Group Related Blocks</a></h3>
<pre><code class="language-storybook">schedule DailyRoutine {
// Sleep blocks
block night_sleep { 22:00 - 05:00, action: sleep }
// Morning blocks
block wake_up { 05:00 - 06:00, action: morning_routine }
block breakfast { 06:00 - 07:00, action: eat_breakfast }
// Work blocks
block commute { 07:00 - 08:00, action: travel_to_work }
block work { 08:00 - 17:00, action: work }
}
</code></pre>
<h3 id="3-use-recurrences-for-special-events"><a class="header" href="#3-use-recurrences-for-special-events">3. Use Recurrences for Special Events</a></h3>
<p><strong>Avoid:</strong> Duplicating blocks manually</p>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">recurs MarketDay on day saturday {
block market { 08:00 - 16:00, action: market_work }
}
</code></pre>
<h3 id="4-extend-for-variations"><a class="header" href="#4-extend-for-variations">4. Extend for Variations</a></h3>
<p><strong>Avoid:</strong> Duplicating entire schedules</p>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">schedule WorkerBase { ... }
schedule EarlyWorker extends WorkerBase { ... }
schedule NightWorker extends WorkerBase { ... }
</code></pre>
<h3 id="5-use-temporal-constraints-for-clarity"><a class="header" href="#5-use-temporal-constraints-for-clarity">5. Use Temporal Constraints for Clarity</a></h3>
<pre><code class="language-storybook">block summer_hours {
06:00 - 20:00
action: long_shift
on season summer // Explicit and readable
}
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> - Linking schedules to characters</li>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> - Actions reference behavior trees</li>
<li><a href="./18-value-types.html">Value Types</a> - Time and duration literals</li>
<li><a href="../reference/16-other-declarations.html#enums">Enums</a> - Defining seasons, days, months</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Time-based AI</strong>: Schedules drive time-dependent behavior</li>
<li><strong>Template inheritance</strong>: <code>extends</code> enables schedule reuse</li>
<li><strong>Temporal constraints</strong>: Filter blocks by season, day, month</li>
<li><strong>Recurrence patterns</strong>: Define repeating events</li>
<li><strong>Composition</strong>: Build complex schedules from simple parts</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/13-life-arcs.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/15-relationships.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/13-life-arcs.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/15-relationships.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,836 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Relationships - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="relationships"><a class="header" href="#relationships">Relationships</a></h1>
<p>Relationships define connections between characters, institutions, and other entities. They capture social bonds, power dynamics, emotional ties, and interactive patterns. Relationships can be symmetric (friendship) or asymmetric (parent-child), and support bidirectional perspectives where each participant has different fields.</p>
<h2 id="what-is-a-relationship"><a class="header" href="#what-is-a-relationship">What is a Relationship?</a></h2>
<p>A relationship connects two or more participants and describes:</p>
<ul>
<li><strong>Participants</strong>: The entities involved (characters, institutions)</li>
<li><strong>Roles</strong>: Optional labels (parent, employer, spouse)</li>
<li><strong>Shared fields</strong>: Properties of the relationship itself (bond strength, duration)</li>
<li><strong>Perspective fields</strong>: How each participant views the relationship (<code>self</code>/<code>other</code> blocks)</li>
</ul>
<pre><code>Relationship: ParentChild
├─ Participant: Martha (as parent)
│ ├─ self: { responsibility: 1.0, protective: 0.9 }
│ └─ other: { dependent: 0.8 }
├─ Participant: Tommy (as child)
└─ Shared: { bond: 0.9, years_together: 8 }
</code></pre>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;relationship-decl&gt; ::= "relationship" &lt;identifier&gt; &lt;body&gt;
&lt;body&gt; ::= "{" &lt;participant&gt;+ &lt;field&gt;* "}"
&lt;participant&gt; ::= &lt;qualified-path&gt; &lt;role-clause&gt;? &lt;perspective-blocks&gt;?
&lt;role-clause&gt; ::= "as" &lt;identifier&gt;
&lt;perspective-blocks&gt; ::= "self" "{" &lt;field&gt;* "}" "other" "{" &lt;field&gt;* "}"
| "self" "{" &lt;field&gt;* "}"
| "other" "{" &lt;field&gt;* "}"
&lt;field&gt; ::= &lt;identifier&gt; ":" &lt;value&gt;
</code></pre>
<h2 id="basic-relationships"><a class="header" href="#basic-relationships">Basic Relationships</a></h2>
<h3 id="simple-two-party-relationship"><a class="header" href="#simple-two-party-relationship">Simple Two-Party Relationship</a></h3>
<pre><code class="language-storybook">relationship Friendship {
Martha
Gregory
bond: 0.8
years_known: 15
}
</code></pre>
<p><strong>Semantics:</strong></p>
<ul>
<li>Two participants: Martha and Gregory</li>
<li>Shared field: <code>bond</code> (strength of friendship)</li>
<li>Shared field: <code>years_known</code> (duration)</li>
</ul>
<h3 id="multi-party-relationship"><a class="header" href="#multi-party-relationship">Multi-Party Relationship</a></h3>
<pre><code class="language-storybook">relationship Family {
Martha
David
Tommy
Elena
household: "Baker Residence"
family_bond: 0.95
}
</code></pre>
<p>Relationships can have any number of participants.</p>
<h2 id="roles"><a class="header" href="#roles">Roles</a></h2>
<p>Roles label a participants function in the relationship.</p>
<h3 id="syntax-1"><a class="header" href="#syntax-1">Syntax</a></h3>
<pre><code class="language-storybook">ParticipantName as role_name
</code></pre>
<h3 id="examples"><a class="header" href="#examples">Examples</a></h3>
<pre><code class="language-storybook">relationship Marriage {
Martha as spouse
David as spouse
bond: 0.9
anniversary: "2015-06-20"
}
</code></pre>
<pre><code class="language-storybook">relationship ParentChild {
Martha as parent
Tommy as child
bond: 0.95
guardianship: true
}
</code></pre>
<pre><code class="language-storybook">relationship EmployerEmployee {
Martha as employer
Elena as employee
workplace: "Martha's Bakery"
salary: 45000
}
</code></pre>
<h3 id="role-semantics"><a class="header" href="#role-semantics">Role Semantics</a></h3>
<ul>
<li><strong>Labels</strong>: Roles are descriptive labels, not types</li>
<li><strong>Multiple roles</strong>: Same person can have different roles in different relationships</li>
<li><strong>Optional</strong>: Roles are optional—participants can be unnamed</li>
<li><strong>Flexibility</strong>: No predefined role vocabulary—use what makes sense</li>
</ul>
<h2 id="perspective-fields-selfother-blocks"><a class="header" href="#perspective-fields-selfother-blocks">Perspective Fields (Self/Other Blocks)</a></h2>
<p>Perspective fields specify how each participant views the relationship. They enable asymmetric, bidirectional relationships.</p>
<h3 id="syntax-2"><a class="header" href="#syntax-2">Syntax</a></h3>
<pre><code class="language-storybook">ParticipantName as role self {
// Fields describing this participant's perspective
} other {
// Fields describing how this participant views others
}
</code></pre>
<h3 id="self-block"><a class="header" href="#self-block">Self Block</a></h3>
<p>The <code>self</code> block contains fields about how the participant experiences the relationship:</p>
<pre><code class="language-storybook">relationship ParentChild {
Martha as parent self {
responsibility: 1.0
protective: 0.9
anxiety_level: 0.6
}
Tommy as child
}
</code></pre>
<p><strong>Marthas perspective:</strong></p>
<ul>
<li><code>responsibility</code>: She feels 100% responsible</li>
<li><code>protective</code>: Shes highly protective (90%)</li>
<li><code>anxiety_level</code>: Moderate anxiety about parenting</li>
</ul>
<h3 id="other-block"><a class="header" href="#other-block">Other Block</a></h3>
<p>The <code>other</code> block contains fields about how the participant views the other participants:</p>
<pre><code class="language-storybook">relationship ParentChild {
Martha as parent self {
responsibility: 1.0
} other {
dependent: 0.8 // She sees Tommy as 80% dependent
mature_for_age: 0.7 // She thinks he's fairly mature
}
Tommy as child
}
</code></pre>
<h3 id="both-blocks"><a class="header" href="#both-blocks">Both Blocks</a></h3>
<pre><code class="language-storybook">relationship EmployerEmployee {
Martha as employer self {
authority: 0.9
stress: 0.6
} other {
respect: 0.8
trust: 0.85
}
Elena as employee
}
</code></pre>
<p><strong>Marthas perspective:</strong></p>
<ul>
<li><strong>Self</strong>: She has high authority (90%), moderate stress (60%)</li>
<li><strong>Other</strong>: She respects Elena (80%), trusts her (85%)</li>
</ul>
<h3 id="asymmetric-relationships"><a class="header" href="#asymmetric-relationships">Asymmetric Relationships</a></h3>
<p>Different participants can have different perspective fields:</p>
<pre><code class="language-storybook">relationship TeacherStudent {
Gandalf as teacher self {
patience: 0.8
wisdom_to_impart: 1.0
} other {
potential: 0.9
ready_to_learn: 0.6
}
Frodo as student self {
eager_to_learn: 0.7
overwhelmed: 0.5
} other {
admiration: 0.95
intimidated: 0.4
}
bond: 0.85
}
</code></pre>
<p><strong>Gandalfs view:</strong></p>
<ul>
<li>Hes patient (80%), has much wisdom to share</li>
<li>Sees Frodo as having high potential but only moderately ready</li>
</ul>
<p><strong>Frodos view:</strong></p>
<ul>
<li>Hes eager but overwhelmed</li>
<li>Deeply admires Gandalf, slightly intimidated</li>
</ul>
<h2 id="shared-fields"><a class="header" href="#shared-fields">Shared Fields</a></h2>
<p>Fields declared at the relationship level (not in <code>self</code>/<code>other</code> blocks) are <strong>shared</strong> among all participants.</p>
<pre><code class="language-storybook">relationship Friendship {
Martha
Gregory
bond: 0.8 // Shared: mutual bond strength
years_known: 15 // Shared: how long they've known each other
shared_routines: 3 // Shared: number of daily routines
}
</code></pre>
<p><strong>Common shared fields:</strong></p>
<ul>
<li><code>bond</code>: Relationship strength (0.0 to 1.0)</li>
<li><code>years_known</code>: Duration of relationship</li>
<li><code>trust</code>: Mutual trust level</li>
<li><code>commitment</code>: Commitment to the relationship</li>
<li><code>compatibility</code>: How well they get along</li>
</ul>
<h2 id="prose-blocks"><a class="header" href="#prose-blocks">Prose Blocks</a></h2>
<p>Relationships can include prose blocks for narrative context.</p>
<pre><code class="language-storybook">relationship MarthaAndGregory {
Martha {
role: shopkeeper
values_loyalty: 0.9
---perspective
Martha appreciates Gregory's unwavering loyalty. He has
been buying her sourdough loaf every morning for fifteen
years. Their brief daily exchanges about the weather and
local gossip are a comforting routine.
---
}
Gregory {
role: regular_customer
always_orders: "sourdough_loaf"
---perspective
Gregory considers Martha's bakery a cornerstone of his
daily routine. The bread is excellent, but it is the brief
human connection that keeps him coming back.
---
}
}
</code></pre>
<p><strong>Common prose tags:</strong></p>
<ul>
<li><code>---perspective</code>: How a participant views the relationship</li>
<li><code>---history</code>: Background of the relationship</li>
<li><code>---dynamics</code>: How the relationship functions</li>
<li><code>---notes</code>: Design or development notes</li>
</ul>
<p>Note: In this syntax, perspective fields are inside participant blocks (not separate <code>self</code>/<code>other</code> blocks).</p>
<h2 id="complete-examples"><a class="header" href="#complete-examples">Complete Examples</a></h2>
<h3 id="simple-friendship"><a class="header" href="#simple-friendship">Simple Friendship</a></h3>
<pre><code class="language-storybook">relationship Friendship {
Martha
NeighborBaker
bond: 0.6
years_known: 10
}
</code></pre>
<h3 id="marriage-with-roles"><a class="header" href="#marriage-with-roles">Marriage with Roles</a></h3>
<pre><code class="language-storybook">relationship Marriage {
Martha as spouse
David as spouse
bond: 0.9
anniversary: "2015-06-20"
children: 2
}
</code></pre>
<h3 id="parent-child-with-perspectives"><a class="header" href="#parent-child-with-perspectives">Parent-Child with Perspectives</a></h3>
<pre><code class="language-storybook">relationship ParentChild {
Martha as parent self {
responsibility: 1.0
protective: 0.9
anxiety_level: 0.6
} other {
dependent: 0.8
mature_for_age: 0.7
}
Tommy as child self {
seeks_independence: 0.7
appreciates_parent: 0.9
} other {
feels_understood: 0.6
wants_more_freedom: 0.8
}
bond: 0.95
years_together: 8
}
</code></pre>
<h3 id="employer-employee"><a class="header" href="#employer-employee">Employer-Employee</a></h3>
<pre><code class="language-storybook">relationship EmployerEmployee {
Martha as employer self {
authority: 0.9
satisfaction_with_employee: 0.85
} other {
respect: 0.8
trust: 0.85
}
Elena as employee self {
job_satisfaction: 0.8
career_growth: 0.7
} other {
respects_boss: 0.9
appreciates_flexibility: 0.95
}
workplace: "Martha's Bakery"
salary: 45000
employment_start: "2023-01-15"
}
</code></pre>
<h3 id="complex-multi-perspective"><a class="header" href="#complex-multi-perspective">Complex Multi-Perspective</a></h3>
<pre><code class="language-storybook">relationship MentorApprentice {
Martha {
role: mentor
teaching_style: "patient"
investment: 0.9
---perspective
Martha sees Elena as the daughter she might have had in
the trade. She recognizes the same passion she felt at
that age and pushes Elena harder because she knows the
talent is there. Every correction comes from love.
---
}
Elena {
role: apprentice
dedication: 0.9
anxiety: 0.4
---perspective
Elena idolizes Martha's skill but fears disappointing
her. Every morning she arrives thirty minutes early to
practice techniques before Martha gets in. She keeps a
notebook of every correction, reviewing them each night.
"I want to be half as good as her someday" -- this quiet
ambition drives everything Elena does.
---
}
bond: 0.85
}
</code></pre>
<h3 id="business-rivalry"><a class="header" href="#business-rivalry">Business Rivalry</a></h3>
<pre><code class="language-storybook">relationship BakeryRivalry {
Martha {
role: established_baker
aware_of_competition: true
respects_rival: 0.6
---perspective
Martha views the new bakery across town as healthy
competition. She respects their pastry work but knows
her sourdough is unmatched. The rivalry pushes her to
keep innovating.
---
}
RivalBaker {
role: newcomer
wants_to_surpass: true
studies_martha: 0.8
---perspective
The rival baker moved to town specifically because of
Martha's reputation. They study her techniques, buy her
bread to analyze it, and dream of the day a customer
chooses their loaf over Martha's sourdough.
---
}
bond: 0.3
}
</code></pre>
<h3 id="multi-party-family"><a class="header" href="#multi-party-family">Multi-Party Family</a></h3>
<pre><code class="language-storybook">relationship BakerFamily {
Martha as parent
David as parent
Tommy as child
Elena as child
household: "Baker Residence"
family_bond: 0.95
dinner_time: 18:00
---dynamics
A loving queer family running a bakery together. Martha and
David met at culinary school, married, and adopted Tommy and
Elena. The whole family works at the bakery on weekends.
---
}
</code></pre>
<h3 id="co-owners-partnership"><a class="header" href="#co-owners-partnership">Co-Owners Partnership</a></h3>
<pre><code class="language-storybook">relationship BakeryPartnership {
Martha {
role: co_owner
specialty: "bread"
handles_finances: true
---perspective
Martha and Jane complement each other perfectly. Martha
handles the bread and business side while Jane creates
the pastries that draw customers in. Together they have
built something neither could alone.
---
}
Jane {
role: co_owner
specialty: "pastries"
handles_creativity: true
---perspective
Jane considers Martha the steady foundation of their
partnership. While Jane experiments and creates, Martha
ensures the bakery runs like clockwork. Their different
strengths make the bakery stronger.
---
}
bond: 0.9
}
</code></pre>
<h2 id="participant-types"><a class="header" href="#participant-types">Participant Types</a></h2>
<p>Participants can be:</p>
<ol>
<li><strong>Characters</strong>: Most common</li>
<li><strong>Institutions</strong>: Organizations in relationships</li>
<li><strong>Locations</strong>: Less common, but valid (e.g., “GuardianOfPlace”)</li>
</ol>
<pre><code class="language-storybook">relationship GuildMembership {
Elena as member
BakersGuild as organization
membership_since: "2023-01-01"
standing: "good"
dues_paid: true
}
</code></pre>
<h2 id="field-resolution"><a class="header" href="#field-resolution">Field Resolution</a></h2>
<p>When a relationship is resolved, fields are merged:</p>
<ol>
<li><strong>Relationship shared fields</strong> apply to all participants</li>
<li><strong>Participant <code>self</code> blocks</strong> apply to that participant</li>
<li><strong>Participant <code>other</code> blocks</strong> describe how that participant views others</li>
</ol>
<p><strong>Example:</strong></p>
<pre><code class="language-storybook">relationship Example {
Martha as friend self {
loyalty: 0.9
} other {
trust: 0.8
}
Gregory as friend
bond: 0.85
}
</code></pre>
<p><strong>Resolution:</strong></p>
<ul>
<li>Martha gets: <code>loyalty: 0.9</code> (from self), <code>trust: 0.8</code> (towards Gregory, from other), <code>bond: 0.85</code> (shared)</li>
<li>Gregory gets: <code>bond: 0.85</code> (shared)</li>
<li>Relationship gets: <code>bond: 0.85</code></li>
</ul>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>At least two participants</strong>: Relationships require ≥2 participants</li>
<li><strong>Participants exist</strong>: All participant names must reference defined entities</li>
<li><strong>Unique participant names</strong>: Each participant appears at most once</li>
<li><strong>Field type consistency</strong>: Fields must have valid value types</li>
<li><strong>No circular relationships</strong>: Avoid infinite relationship chains (warning)</li>
<li><strong>Self/other completeness</strong>: If using <code>self</code>/<code>other</code>, both blocks should be present (best practice)</li>
</ol>
<h2 id="use-cases"><a class="header" href="#use-cases">Use Cases</a></h2>
<h3 id="social-networks"><a class="header" href="#social-networks">Social Networks</a></h3>
<pre><code class="language-storybook">relationship BakerFriendship {
Martha
Gregory
bond: 0.8
}
relationship SupplierPartnership {
Martha
Farmer_Jenkins
bond: 0.7
}
relationship BakeryRivalry {
Martha
RivalBaker
bond: 0.2
competitive: true
}
</code></pre>
<h3 id="family-structures"><a class="header" href="#family-structures">Family Structures</a></h3>
<pre><code class="language-storybook">relationship ParentChild {
Martha as parent
Tommy as child
bond: 0.95
}
relationship Siblings {
Tommy as older_sibling
Elena as younger_sibling
bond: 0.85
rivalry: 0.3
}
</code></pre>
<h3 id="power-dynamics"><a class="header" href="#power-dynamics">Power Dynamics</a></h3>
<pre><code class="language-storybook">relationship Vassalage {
King as lord self {
grants: "protection"
expects: "loyalty"
} other {
trusts: 0.6
}
Knight as vassal self {
swears: "fealty"
expects: "land"
} other {
respects: 0.9
}
oath_date: "1205-03-15"
}
</code></pre>
<h3 id="asymmetric-awareness"><a class="header" href="#asymmetric-awareness">Asymmetric Awareness</a></h3>
<pre><code class="language-storybook">relationship StalkingVictim {
Stalker as pursuer self {
obsession: 0.95
distance_maintained: 50 // meters
} other {
believes_unnoticed: true
}
Victim as unaware_target self {
awareness: 0.0
}
danger_level: 0.8
}
</code></pre>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<h3 id="1-use-descriptive-relationship-names"><a class="header" href="#1-use-descriptive-relationship-names">1. Use Descriptive Relationship Names</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">relationship R1 { ... }
relationship MarthaGregory { ... }
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">relationship Friendship { ... }
relationship ParentChild { ... }
relationship MentorApprentice { ... }
</code></pre>
<h3 id="2-use-roles-for-clarity"><a class="header" href="#2-use-roles-for-clarity">2. Use Roles for Clarity</a></h3>
<pre><code class="language-storybook">relationship Marriage {
Martha as spouse
David as spouse
}
</code></pre>
<p>Better than:</p>
<pre><code class="language-storybook">relationship Marriage {
Martha
David
}
</code></pre>
<h3 id="3-shared-fields-for-mutual-properties"><a class="header" href="#3-shared-fields-for-mutual-properties">3. Shared Fields for Mutual Properties</a></h3>
<pre><code class="language-storybook">relationship Partnership {
Martha
Jane
bond: 0.9 // Mutual bond
years_together: 5 // Shared history
}
</code></pre>
<h3 id="4-selfother-for-perspectives"><a class="header" href="#4-selfother-for-perspectives">4. Self/Other for Perspectives</a></h3>
<pre><code class="language-storybook">relationship TeacherStudent {
Teacher as mentor self {
patience: 0.8
} other {
potential: 0.9
}
Student as learner self {
motivation: 0.7
} other {
admiration: 0.95
}
}
</code></pre>
<h3 id="5-prose-blocks-for-rich-context"><a class="header" href="#5-prose-blocks-for-rich-context">5. Prose Blocks for Rich Context</a></h3>
<pre><code class="language-storybook">relationship ComplexDynamic {
Martha { ... }
Jane { ... }
---dynamics
Their relationship is characterized by mutual respect but
divergent goals. Martha focuses on traditional bread while Jane
pushes for experimental pastries, creating creative tension.
---
}
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> - Participants are typically characters</li>
<li><a href="./16-other-declarations.html#institutions">Institutions</a> - Institutions can participate</li>
<li><a href="./18-value-types.html">Value Types</a> - Field value types</li>
<li><a href="./17-expressions.html">Expression Language</a> - Querying relationships</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Bidirectional modeling</strong>: <code>self</code>/<code>other</code> blocks enable asymmetric perspectives</li>
<li><strong>Social simulation</strong>: Relationships drive character interactions</li>
<li><strong>Narrative depth</strong>: Prose blocks embed storytelling in relationship data</li>
<li><strong>Power dynamics</strong>: Roles and perspective fields model hierarchies</li>
<li><strong>Emotional bonds</strong>: Bond strength and trust metrics quantify connections</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/14-schedules.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/16a-locations.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/14-schedules.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/16a-locations.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,477 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Locations - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="locations"><a class="header" href="#locations">Locations</a></h1>
<p>Locations define places in your world rooms, buildings, cities, landscapes, or abstract spaces. They provide spatial context for characters, events, and narratives.</p>
<hr />
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;location-decl&gt; ::= "location" &lt;identifier&gt; "{" &lt;field&gt;* &lt;prose-block&gt;* "}"
</code></pre>
<p>A location declaration consists of:</p>
<ul>
<li>The <code>location</code> keyword</li>
<li>A unique name (identifier)</li>
<li>A body block containing fields and optional prose blocks</li>
</ul>
<p>Locations are one of the simpler declaration types they hold fields and prose blocks but do not support resource linking (<code>uses behaviors</code> / <code>uses schedule</code>) like characters or institutions.</p>
<hr />
<h2 id="basic-location"><a class="header" href="#basic-location">Basic Location</a></h2>
<p>The simplest location has a name and descriptive fields:</p>
<pre><code class="language-storybook">location BakersBakery {
type: bakery
capacity: 30
address: "14 Main Street"
open_hours: "06:00-18:00"
}
</code></pre>
<p>Fields can use any <a href="./18-value-types.html">value type</a>: integers, floats, strings, booleans, enums, lists, ranges, and durations.</p>
<hr />
<h2 id="fields"><a class="header" href="#fields">Fields</a></h2>
<p>Location fields describe properties of the place:</p>
<pre><code class="language-storybook">location BakerHome {
type: residence
bedrooms: 3
has_garden: true
garden_size_sqft: 450.0
residents: ["Martha", "David", "Jane"]
comfort_level: 0.85
}
</code></pre>
<h3 id="common-field-patterns"><a class="header" href="#common-field-patterns">Common Field Patterns</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody>
<tr><td><code>type</code></td><td>enum/identifier</td><td>Category of the place</td></tr>
<tr><td><code>capacity</code></td><td>integer</td><td>How many can be in this place</td></tr>
<tr><td><code>size</code></td><td>enum/float</td><td>Physical size</td></tr>
<tr><td><code>coordinates</code></td><td>integer/float</td><td>Position in a world map</td></tr>
<tr><td><code>accessible</code></td><td>boolean</td><td>Whether characters can enter</td></tr>
</tbody></table>
</div>
<p>These are conventions, not enforced schema. You define whatever fields are meaningful for your world.</p>
<hr />
<h2 id="prose-blocks"><a class="header" href="#prose-blocks">Prose Blocks</a></h2>
<p>Locations support prose blocks for rich narrative content. Prose blocks are delimited by <code>---tag</code> markers:</p>
<pre><code class="language-storybook">location BakersBakery {
type: bakery
capacity: 30
---description
A warm, inviting bakery on Main Street. The smell of fresh bread
wafts out the door every morning at dawn. Martha has run the shop
for fifteen years, and the locals consider it the heart of the
neighborhood.
---
---atmosphere
Flour dust catches the light from tall windows. A display case
holds rows of golden pastries. Behind the counter, the kitchen
hums with activity from 4 AM onward.
---
}
</code></pre>
<p><strong>Prose block rules:</strong></p>
<ul>
<li>Start with <code>---tag_name</code> on its own line</li>
<li>Content is free-form text (Markdown supported)</li>
<li>End with <code>---</code> on its own line</li>
<li>The tag name becomes the key for retrieval</li>
<li>Multiple prose blocks per location are allowed</li>
<li>Each tag must be unique within the location</li>
</ul>
<hr />
<h2 id="ranges"><a class="header" href="#ranges">Ranges</a></h2>
<p>Locations can use range values for procedural variation:</p>
<pre><code class="language-storybook">location MarketSquare {
type: outdoor_market
stalls: 10..25
daily_visitors: 50..200
noise_level: 0.4..0.9
}
</code></pre>
<p>When instantiated, values are selected from within the specified range. This is useful for locations that should vary across simulation runs.</p>
<hr />
<h2 id="lists"><a class="header" href="#lists">Lists</a></h2>
<p>Locations can hold list values:</p>
<pre><code class="language-storybook">location BakersBakery {
type: bakery
products: ["sourdough", "croissants", "rye bread", "cinnamon rolls"]
equipment: ["stone oven", "mixing station", "proofing cabinet"]
}
</code></pre>
<hr />
<h2 id="referencing-other-entities"><a class="header" href="#referencing-other-entities">Referencing Other Entities</a></h2>
<p>Location fields can reference other declarations by name:</p>
<pre><code class="language-storybook">location BakersBakery {
type: bakery
owner: Martha
neighborhood: MainStreet
part_of: TownCenter
}
</code></pre>
<p>These are identifier references they are not validated as cross-references at parse time, but the resolver checks that referenced names exist in the name table.</p>
<hr />
<h2 id="nested-structure-with-fields"><a class="header" href="#nested-structure-with-fields">Nested Structure with Fields</a></h2>
<p>You can model spatial hierarchy using fields:</p>
<pre><code class="language-storybook">location BakersBakery {
type: bakery
parent: MainStreet
// Zones within the bakery
has_kitchen: true
has_storefront: true
has_storage_room: true
// Dimensions
total_sqft: 1200
kitchen_sqft: 600
storefront_sqft: 400
storage_sqft: 200
}
location MainStreet {
type: street
parent: TownCenter
length_meters: 500
shops: 12
}
location TownCenter {
type: district
population: 2000
}
</code></pre>
<p>There is no built-in parent-child relationship for locations you model hierarchy through conventional field names like <code>part_of</code>, <code>parent</code>, or <code>contains</code>.</p>
<hr />
<h2 id="location-with-enum-fields"><a class="header" href="#location-with-enum-fields">Location with Enum Fields</a></h2>
<p>Use <a href="./16-other-declarations.html#enums">enums</a> for type-safe categorization:</p>
<pre><code class="language-storybook">enum PlaceType {
residence, shop, workshop, office,
park, street, square, church
}
enum Accessibility {
public, private, restricted, members_only
}
location BakersBakery {
type: shop
accessibility: public
capacity: 30
}
location BakerHome {
type: residence
accessibility: private
capacity: 8
}
</code></pre>
<hr />
<h2 id="complete-example-baker-family-locations"><a class="header" href="#complete-example-baker-family-locations">Complete Example: Baker Family Locations</a></h2>
<pre><code class="language-storybook">use schema::enums::{PlaceType, Accessibility};
location BakersBakery {
type: shop
accessibility: public
owner: Martha
address: "14 Main Street"
capacity: 30
employees: 4
established: "2011"
specialty: "artisan sourdough"
daily_output_loaves: 80..120
---description
Martha Baker's artisan bakery, known throughout town for its
sourdough and pastries. The shop opens at 6 AM sharp, and by
mid-morning there's usually a line out the door.
---
---history
Originally a hardware store, Martha converted the space after
winning a local baking competition. The stone oven was imported
from France and is the heart of the operation.
---
}
location BakerHome {
type: residence
accessibility: private
address: "22 Elm Lane"
bedrooms: 4
has_garden: true
garden_size_sqft: 600
residents: ["Martha", "David", "Jane", "Tom"]
comfort_level: 0.9
---description
A comfortable family home on a quiet street. The kitchen is
oversized (Martha insisted) and there's always something
baking, even at home.
---
}
location BakersGuildHall {
type: office
accessibility: members_only
address: "7 Guild Row"
capacity: 100
meeting_room_capacity: 40
established: "1892"
---description
The historic headquarters of the Bakers Guild, where trade
matters are discussed and apprenticeships are arranged.
---
}
</code></pre>
<hr />
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>Unique names</strong>: Location names must be unique within their scope</li>
<li><strong>Valid field values</strong>: All fields must have values that conform to <a href="./18-value-types.html">value types</a></li>
<li><strong>Unique field names</strong>: No duplicate field names within a location</li>
<li><strong>Unique prose tags</strong>: No duplicate prose block tags within a location</li>
<li><strong>Valid identifiers</strong>: Location names must follow identifier rules (<code>[a-zA-Z_][a-zA-Z0-9_]*</code>)</li>
</ol>
<hr />
<h2 id="locations-vs-other-declarations"><a class="header" href="#locations-vs-other-declarations">Locations vs. Other Declarations</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Aspect</th><th>Locations</th><th>Institutions</th><th>Characters</th></tr></thead><tbody>
<tr><td>Purpose</td><td>Physical/abstract places</td><td>Organizations/groups</td><td>Individuals</td></tr>
<tr><td>Resource linking</td><td>No</td><td>Yes</td><td>Yes</td></tr>
<tr><td>Prose blocks</td><td>Yes</td><td>Yes</td><td>Yes</td></tr>
<tr><td>Species</td><td>No</td><td>No</td><td>Yes</td></tr>
<tr><td>Templates</td><td>No</td><td>No</td><td>Yes</td></tr>
</tbody></table>
</div>
<p>Locations are intentionally simple. They define <em>where</em> things happen. For <em>who</em> does things, use <a href="./10-characters.html">characters</a>. For <em>organizational structures</em>, use <a href="./16b-institutions.html">institutions</a>.</p>
<hr />
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> Characters can reference locations via fields</li>
<li><a href="./16b-institutions.html">Institutions</a> Institutions can be associated with locations</li>
<li><a href="./14-schedules.html">Schedules</a> Schedule activities can reference locations</li>
<li><a href="./18-value-types.html">Value Types</a> All field value types</li>
<li><a href="./16-other-declarations.html">Other Declarations</a> Overview of all utility declarations</li>
<li><a href="./19-validation.html">Validation Rules</a> Complete validation reference</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/15-relationships.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/16b-institutions.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/15-relationships.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/16b-institutions.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,569 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Institutions - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="institutions"><a class="header" href="#institutions">Institutions</a></h1>
<p>Institutions define organizations, groups, and systems entities that function like characters but represent collectives rather than individuals. Unlike locations (which model <em>where</em>), institutions model <em>what structures and organizations</em> operate in your world.</p>
<hr />
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;institution-decl&gt; ::= "institution" &lt;identifier&gt; "{" &lt;institution-body&gt; "}"
&lt;institution-body&gt; ::= (&lt;field&gt; | &lt;uses-behaviors&gt; | &lt;uses-schedule&gt; | &lt;prose-block&gt;)*
&lt;uses-behaviors&gt; ::= "uses" "behaviors" ":" "[" &lt;behavior-link&gt; ("," &lt;behavior-link&gt;)* "]"
&lt;behavior-link&gt; ::= "{" "tree" ":" &lt;path&gt; ","?
("when" ":" &lt;expression&gt; ","?)?
("priority" ":" &lt;priority-level&gt; ","?)? "}"
&lt;priority-level&gt; ::= "low" | "normal" | "high" | "critical"
&lt;uses-schedule&gt; ::= "uses" "schedule" ":" &lt;identifier&gt;
| "uses" "schedules" ":" "[" &lt;identifier&gt; ("," &lt;identifier&gt;)* "]"
</code></pre>
<p>An institution declaration consists of:</p>
<ul>
<li>The <code>institution</code> keyword</li>
<li>A unique name (identifier)</li>
<li>A body block containing fields, resource links, and optional prose blocks</li>
</ul>
<hr />
<h2 id="basic-institution"><a class="header" href="#basic-institution">Basic Institution</a></h2>
<p>The simplest institution has a name and descriptive fields:</p>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
members: 50
founded: "1892"
reputation: 0.85
}
</code></pre>
<hr />
<h2 id="fields"><a class="header" href="#fields">Fields</a></h2>
<p>Institution fields describe properties of the organization:</p>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
members: 50
founded: "1892"
reputation: 0.85
dues_annual: 120
meeting_frequency: "monthly"
accepts_apprentices: true
max_apprentices: 10
location: BakersGuildHall
}
</code></pre>
<h3 id="common-field-patterns"><a class="header" href="#common-field-patterns">Common Field Patterns</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody>
<tr><td><code>type</code></td><td>enum/identifier</td><td>Category of organization</td></tr>
<tr><td><code>members</code></td><td>integer</td><td>Membership count</td></tr>
<tr><td><code>founded</code></td><td>string</td><td>Establishment date</td></tr>
<tr><td><code>reputation</code></td><td>float</td><td>Public standing (0.0-1.0)</td></tr>
<tr><td><code>location</code></td><td>identifier</td><td>Where the institution operates</td></tr>
<tr><td><code>leader</code></td><td>identifier</td><td>Who runs the institution</td></tr>
</tbody></table>
</div>
<p>These are conventions you define whatever fields make sense for your world.</p>
<hr />
<h2 id="prose-blocks"><a class="header" href="#prose-blocks">Prose Blocks</a></h2>
<p>Institutions support prose blocks for rich narrative documentation:</p>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
members: 50
---description
The Bakers Guild has been the backbone of the town's bread trade
since 1892. Members share recipes, arrange apprenticeships, and
collectively negotiate flour prices with suppliers.
---
---charter
Article I: All members shall maintain the highest standards of
baking quality. Article II: Apprentices must complete a three-year
training program. Article III: Monthly meetings are mandatory.
---
---traditions
Every autumn, the Guild hosts the Great Bake-Off, a competition
open to all members. The winner earns the title of Master Baker
for the following year.
---
}
</code></pre>
<p><strong>Prose block rules:</strong></p>
<ul>
<li>Start with <code>---tag_name</code> on its own line</li>
<li>Content is free-form text (Markdown supported)</li>
<li>End with <code>---</code> on its own line</li>
<li>Multiple prose blocks per institution are allowed</li>
<li>Each tag must be unique within the institution</li>
</ul>
<hr />
<h2 id="resource-linking-behaviors"><a class="header" href="#resource-linking-behaviors">Resource Linking: Behaviors</a></h2>
<p>Institutions can link to <a href="./11-behavior-trees.html">behavior trees</a> using the <code>uses behaviors</code> clause. This defines what actions the institution takes as a collective entity.</p>
<h3 id="simple-behavior-linking"><a class="header" href="#simple-behavior-linking">Simple Behavior Linking</a></h3>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
members: 50
uses behaviors: [
{ tree: ManageApprentices },
{ tree: NegotiateSuppliers },
{ tree: HostEvents }
]
}
</code></pre>
<p>Each behavior link is an object with a <code>tree</code> field referencing a behavior tree by name or path.</p>
<h3 id="behavior-links-with-priority"><a class="header" href="#behavior-links-with-priority">Behavior Links with Priority</a></h3>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
uses behaviors: [
{ tree: ManageApprentices, priority: normal },
{ tree: NegotiateSuppliers, priority: high },
{ tree: HostEvents, priority: low }
]
}
</code></pre>
<p>Priority levels: <code>low</code>, <code>normal</code> (default), <code>high</code>, <code>critical</code>.</p>
<h3 id="conditional-behavior-links"><a class="header" href="#conditional-behavior-links">Conditional Behavior Links</a></h3>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
uses behaviors: [
{ tree: ManageApprentices },
{ tree: NegotiateSuppliers, priority: high },
{ tree: EmergencyMeeting, when: reputation &lt; 0.3, priority: critical }
]
}
</code></pre>
<p>The <code>when</code> clause takes an <a href="./17-expressions.html">expression</a> that determines when the behavior activates.</p>
<h3 id="behavior-link-fields"><a class="header" href="#behavior-link-fields">Behavior Link Fields</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Field</th><th>Required</th><th>Type</th><th>Description</th></tr></thead><tbody>
<tr><td><code>tree</code></td><td>Yes</td><td>path</td><td>Reference to a behavior tree</td></tr>
<tr><td><code>priority</code></td><td>No</td><td>priority level</td><td>Execution priority (default: <code>normal</code>)</td></tr>
<tr><td><code>when</code></td><td>No</td><td>expression</td><td>Activation condition</td></tr>
</tbody></table>
</div>
<hr />
<h2 id="resource-linking-schedules"><a class="header" href="#resource-linking-schedules">Resource Linking: Schedules</a></h2>
<p>Institutions can link to <a href="./14-schedules.html">schedules</a> using the <code>uses schedule</code> or <code>uses schedules</code> clause.</p>
<h3 id="single-schedule"><a class="header" href="#single-schedule">Single Schedule</a></h3>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
uses schedule: GuildOperatingHours
}
</code></pre>
<h3 id="multiple-schedules"><a class="header" href="#multiple-schedules">Multiple Schedules</a></h3>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
uses schedules: [WeekdaySchedule, WeekendSchedule, HolidaySchedule]
}
</code></pre>
<hr />
<h2 id="complete-syntax-example"><a class="header" href="#complete-syntax-example">Complete Syntax Example</a></h2>
<p>An institution using all available features:</p>
<pre><code class="language-storybook">institution BakersGuild {
type: trade_guild
members: 50
founded: "1892"
reputation: 0.85
dues_annual: 120
location: BakersGuildHall
leader: Martha
accepts_apprentices: true
uses behaviors: [
{ tree: ManageApprentices, priority: normal },
{ tree: NegotiateSuppliers, priority: high },
{ tree: HostAnnualBakeOff, when: month is october, priority: high },
{ tree: EmergencyMeeting, when: reputation &lt; 0.3, priority: critical }
]
uses schedule: GuildOperatingHours
---description
The Bakers Guild has been the backbone of the town's bread trade
since 1892. Martha Baker currently serves as Guild Master.
---
---membership_rules
New members must be nominated by an existing member and pass
a baking trial. Apprentices are accepted from age 16.
---
}
</code></pre>
<hr />
<h2 id="membership-modeling"><a class="header" href="#membership-modeling">Membership Modeling</a></h2>
<p>Institutions themselves dont have a built-in membership list. Instead, model membership through character fields or relationships:</p>
<h3 id="via-character-fields"><a class="header" href="#via-character-fields">Via Character Fields</a></h3>
<pre><code class="language-storybook">character Martha {
age: 45
guild_member: true
guild_role: guild_master
guild: BakersGuild
}
character Jane {
age: 19
guild_member: true
guild_role: apprentice
guild: BakersGuild
}
</code></pre>
<h3 id="via-relationships"><a class="header" href="#via-relationships">Via Relationships</a></h3>
<pre><code class="language-storybook">relationship GuildMembership {
Martha as guild_master { }
BakersGuild as organization { }
bond: 0.95
years_active: 20
}
relationship Apprenticeship {
Jane as apprentice { }
BakersGuild as guild { }
Martha as mentor { }
years_completed: 1
years_remaining: 2
}
</code></pre>
<hr />
<h2 id="institutions-vs-characters"><a class="header" href="#institutions-vs-characters">Institutions vs. Characters</a></h2>
<p>Both institutions and characters can use behaviors and schedules, but they serve different purposes:</p>
<div class="table-wrapper"><table><thead><tr><th>Aspect</th><th>Institutions</th><th>Characters</th></tr></thead><tbody>
<tr><td>Represents</td><td>Organizations, collectives</td><td>Individuals</td></tr>
<tr><td>Species</td><td>No</td><td>Yes (optional)</td></tr>
<tr><td>Templates</td><td>No</td><td>Yes (optional)</td></tr>
<tr><td><code>uses behaviors</code></td><td>Yes</td><td>Yes</td></tr>
<tr><td><code>uses schedule</code></td><td>Yes</td><td>Yes</td></tr>
<tr><td>Prose blocks</td><td>Yes</td><td>Yes</td></tr>
<tr><td>Participation in relationships</td><td>Yes (via name)</td><td>Yes (via name)</td></tr>
</tbody></table>
</div>
<p>Use institutions when you need an entity that acts collectively: a guild votes, a government issues decrees, a school enrolls students.</p>
<hr />
<h2 id="use-cases"><a class="header" href="#use-cases">Use Cases</a></h2>
<ul>
<li><strong>Organizations</strong>: Guilds, companies, governments, religions</li>
<li><strong>Social structures</strong>: Families, tribes, castes, classes</li>
<li><strong>Systems</strong>: Economic systems, magical systems, legal frameworks</li>
<li><strong>Abstract entities</strong>: Dream logic, fate, cultural norms</li>
<li><strong>Collectives</strong>: Military units, sports teams, musical ensembles</li>
</ul>
<hr />
<h2 id="complete-example-baker-family-world"><a class="header" href="#complete-example-baker-family-world">Complete Example: Baker Family World</a></h2>
<pre><code class="language-storybook">use schema::enums::{GuildRole, InstitutionType};
institution BakersGuild {
type: trade_guild
members: 50
founded: "1892"
reputation: 0.85
location: BakersGuildHall
leader: Martha
annual_dues: 120
accepts_apprentices: true
max_apprentices_per_mentor: 2
uses behaviors: [
{ tree: ManageApprentices, priority: normal },
{ tree: NegotiateSuppliers, priority: high },
{ tree: HostAnnualBakeOff, when: month is october, priority: high }
]
uses schedule: GuildOperatingHours
---description
The Bakers Guild has served the town since 1892, ensuring quality
standards and fair pricing across all bakeries. Monthly meetings
are held at the Guild Hall, and the annual Bake-Off is the
highlight of the autumn calendar.
---
}
institution TownCouncil {
type: government
members: 12
meets: "first Monday of each month"
location: TownHall
jurisdiction: "town limits"
uses behaviors: [
{ tree: ReviewPetitions },
{ tree: AllocateBudget, priority: high }
]
---description
The elected body governing the town. Martha Baker has been
lobbying them for years about flour import tariffs.
---
}
institution BakersBakeryBusiness {
type: business
owner: Martha
employees: 4
location: BakersBakery
annual_revenue: 180000
established: "2011"
uses behaviors: [
{ tree: DailyBakingOps, priority: high },
{ tree: InventoryManagement },
{ tree: CustomerService }
]
uses schedule: BakeryOperatingHours
---description
The business entity behind Baker's Bakery. Martha runs the
operation with help from her daughter Jane (apprentice baker),
two full-time bakers, and a part-time cashier.
---
}
</code></pre>
<hr />
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>Unique names</strong>: Institution names must be unique within their scope</li>
<li><strong>Valid field values</strong>: All fields must have values conforming to <a href="./18-value-types.html">value types</a></li>
<li><strong>Unique field names</strong>: No duplicate field names within an institution</li>
<li><strong>Unique prose tags</strong>: No duplicate prose block tags within an institution</li>
<li><strong>Valid behavior links</strong>: Each behavior link must have a <code>tree</code> field</li>
<li><strong>Valid priority levels</strong>: Priority must be <code>low</code>, <code>normal</code>, <code>high</code>, or <code>critical</code></li>
<li><strong>Valid schedule references</strong>: Schedule names in <code>uses schedule</code>/<code>uses schedules</code> must be valid identifiers</li>
<li><strong>Valid identifiers</strong>: Institution names must follow identifier rules (<code>[a-zA-Z_][a-zA-Z0-9_]*</code>)</li>
</ol>
<hr />
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> Characters can be members of institutions</li>
<li><a href="./16a-locations.html">Locations</a> Institutions can be associated with locations</li>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> Institutions can use behaviors</li>
<li><a href="./14-schedules.html">Schedules</a> Institutions can use schedules</li>
<li><a href="./15-relationships.html">Relationships</a> Model membership via relationships</li>
<li><a href="./17-expressions.html">Expressions</a> Conditional behavior activation</li>
<li><a href="./18-value-types.html">Value Types</a> All field value types</li>
<li><a href="./16-other-declarations.html">Other Declarations</a> Overview of all utility declarations</li>
<li><a href="./19-validation.html">Validation Rules</a> Complete validation reference</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/16a-locations.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/16-other-declarations.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/16a-locations.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/16-other-declarations.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,745 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Expression Language - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="expression-language"><a class="header" href="#expression-language">Expression Language</a></h1>
<p>The Storybook expression language enables conditions, queries, and logic throughout the system. Expressions appear in life arc transitions, behavior tree guards, decorator conditions, and relationship queries. This chapter provides a complete reference for expression syntax and semantics.</p>
<h2 id="what-are-expressions"><a class="header" href="#what-are-expressions">What are Expressions?</a></h2>
<p>Expressions are logical statements that evaluate to <code>true</code> or <code>false</code>. They combine:</p>
<ul>
<li><strong>Literals</strong>: Numbers, strings, booleans</li>
<li><strong>Identifiers</strong>: References to fields and entities</li>
<li><strong>Comparisons</strong>: <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code></li>
<li><strong>Logical operators</strong>: <code>and</code>, <code>or</code>, <code>not</code></li>
<li><strong>Field access</strong>: <code>self.field</code>, <code>other.field</code></li>
<li><strong>Quantifiers</strong>: <code>forall</code>, <code>exists</code> (for collections)</li>
</ul>
<h2 id="syntax"><a class="header" href="#syntax">Syntax</a></h2>
<pre><code class="language-bnf">&lt;expression&gt; ::= &lt;literal&gt;
| &lt;identifier&gt;
| &lt;field-access&gt;
| &lt;comparison&gt;
| &lt;logical&gt;
| &lt;unary&gt;
| &lt;quantifier&gt;
| "(" &lt;expression&gt; ")"
&lt;literal&gt; ::= &lt;int&gt; | &lt;float&gt; | &lt;string&gt; | &lt;bool&gt;
&lt;identifier&gt; ::= &lt;simple-name&gt;
| &lt;qualified-path&gt;
&lt;field-access&gt; ::= &lt;expression&gt; "." &lt;identifier&gt;
| "self" "." &lt;identifier&gt;
| "other" "." &lt;identifier&gt;
&lt;comparison&gt; ::= &lt;expression&gt; &lt;comp-op&gt; &lt;expression&gt;
&lt;comp-op&gt; ::= "==" | "!=" | "&lt;" | "&lt;=" | "&gt;" | "&gt;="
&lt;logical&gt; ::= &lt;expression&gt; "and" &lt;expression&gt;
| &lt;expression&gt; "or" &lt;expression&gt;
&lt;unary&gt; ::= "not" &lt;expression&gt;
| "-" &lt;expression&gt;
&lt;quantifier&gt; ::= ("forall" | "exists") &lt;identifier&gt; "in" &lt;expression&gt; ":" &lt;expression&gt;
</code></pre>
<h2 id="literals"><a class="header" href="#literals">Literals</a></h2>
<h3 id="integer-literals"><a class="header" href="#integer-literals">Integer Literals</a></h3>
<pre><code class="language-storybook">42
-7
0
1000
</code></pre>
<h3 id="float-literals"><a class="header" href="#float-literals">Float Literals</a></h3>
<pre><code class="language-storybook">3.14
-0.5
0.0
100.25
</code></pre>
<h3 id="string-literals"><a class="header" href="#string-literals">String Literals</a></h3>
<pre><code class="language-storybook">"Martha"
"Sourdough takes patience."
"active"
</code></pre>
<p>Strings are enclosed in double quotes. Escape sequences: <code>\n</code>, <code>\t</code>, <code>\\</code>, <code>\"</code>.</p>
<h3 id="boolean-literals"><a class="header" href="#boolean-literals">Boolean Literals</a></h3>
<pre><code class="language-storybook">true
false
</code></pre>
<h2 id="identifiers"><a class="header" href="#identifiers">Identifiers</a></h2>
<p>Identifiers reference fields or entities.</p>
<h3 id="simple-identifiers"><a class="header" href="#simple-identifiers">Simple Identifiers</a></h3>
<pre><code class="language-storybook">health
enemy_count
is_ready
</code></pre>
<h3 id="qualified-paths"><a class="header" href="#qualified-paths">Qualified Paths</a></h3>
<pre><code class="language-storybook">Martha.skill_level
Character.emotional_state
</code></pre>
<h2 id="comparison-operators"><a class="header" href="#comparison-operators">Comparison Operators</a></h2>
<h3 id="equality-"><a class="header" href="#equality-">Equality: <code>==</code></a></h3>
<p>Tests if two values are equal.</p>
<pre><code class="language-storybook">name == "Martha"
count == 5
status == active
</code></pre>
<p><strong>Type compatibility:</strong></p>
<ul>
<li>Both operands must be the same type</li>
<li>Works with: int, float, string, bool, enum values</li>
</ul>
<h3 id="inequality-"><a class="header" href="#inequality-">Inequality: <code>!=</code></a></h3>
<p>Tests if two values are not equal.</p>
<pre><code class="language-storybook">name != "Gregory"
health != 0
ready != true
</code></pre>
<h3 id="less-than"><a class="header" href="#less-than">Less Than: <code>&lt;</code></a></h3>
<p>Tests if left operand is less than right.</p>
<pre><code class="language-storybook">health &lt; 20
age &lt; 18
distance &lt; 10.0
</code></pre>
<p><strong>Valid types:</strong> int, float</p>
<h3 id="less-than-or-equal-"><a class="header" href="#less-than-or-equal-">Less Than or Equal: <code>&lt;=</code></a></h3>
<pre><code class="language-storybook">health &lt;= 50
count &lt;= max_count
</code></pre>
<h3 id="greater-than"><a class="header" href="#greater-than">Greater Than: <code>&gt;</code></a></h3>
<pre><code class="language-storybook">strength &gt; 10
bond &gt; 0.5
</code></pre>
<h3 id="greater-than-or-equal-"><a class="header" href="#greater-than-or-equal-">Greater Than or Equal: <code>&gt;=</code></a></h3>
<pre><code class="language-storybook">age &gt;= 21
score &gt;= 100
</code></pre>
<h2 id="logical-operators"><a class="header" href="#logical-operators">Logical Operators</a></h2>
<h3 id="and-and"><a class="header" href="#and-and">AND: <code>and</code></a></h3>
<p>Both operands must be true.</p>
<pre><code class="language-storybook">health &lt; 50 and has_potion
is_ready and not is_busy
age &gt;= 18 and age &lt; 65
</code></pre>
<p><strong>Evaluation:</strong> Short-circuit (if left is false, right is not evaluated)</p>
<h3 id="or-or"><a class="header" href="#or-or">OR: <code>or</code></a></h3>
<p>At least one operand must be true.</p>
<pre><code class="language-storybook">is_day or is_lit
health &lt; 20 or surrounded
enemy_count == 0 or all_enemies_dead
</code></pre>
<p><strong>Evaluation:</strong> Short-circuit (if left is true, right is not evaluated)</p>
<h3 id="operator-precedence"><a class="header" href="#operator-precedence">Operator Precedence</a></h3>
<p>From highest to lowest:</p>
<ol>
<li><strong>Parentheses</strong>: <code>(...)</code></li>
<li><strong>Unary</strong>: <code>not</code>, <code>-</code></li>
<li><strong>Comparisons</strong>: <code>==</code>, <code>!=</code>, <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code></li>
<li><strong>AND</strong>: <code>and</code></li>
<li><strong>OR</strong>: <code>or</code></li>
</ol>
<p><strong>Examples:</strong></p>
<pre><code class="language-storybook">not is_ready and is_awake
// Equivalent to: (not is_ready) and is_awake
health &lt; 50 or is_poisoned and has_antidote
// Equivalent to: (health &lt; 50) or (is_poisoned and has_antidote)
// Use parentheses for clarity:
(health &lt; 50 or is_poisoned) and has_antidote
</code></pre>
<h2 id="unary-operators"><a class="header" href="#unary-operators">Unary Operators</a></h2>
<h3 id="not-not"><a class="header" href="#not-not">NOT: <code>not</code></a></h3>
<p>Inverts a boolean value.</p>
<pre><code class="language-storybook">not is_ready
not (health &lt; 20)
not enemy_nearby and safe
</code></pre>
<h3 id="negation--"><a class="header" href="#negation--">Negation: <code>-</code></a></h3>
<p>Negates a numeric value.</p>
<pre><code class="language-storybook">-health
-10
-(max_value - current_value)
</code></pre>
<h2 id="field-access"><a class="header" href="#field-access">Field Access</a></h2>
<h3 id="direct-field-access"><a class="header" href="#direct-field-access">Direct Field Access</a></h3>
<pre><code class="language-storybook">health
bond
emotional_state
</code></pre>
<p>References a field on the current entity.</p>
<h3 id="dot-access"><a class="header" href="#dot-access">Dot Access</a></h3>
<pre><code class="language-storybook">Martha.skill_level
Character.emotional_state
enemy.health
</code></pre>
<p>Access fields on other entities.</p>
<h3 id="self-access"><a class="header" href="#self-access">Self Access</a></h3>
<p>In relationships and certain contexts, <code>self</code> refers to the current participant:</p>
<pre><code class="language-storybook">self.bond
self.responsibility
self.trust
</code></pre>
<p><strong>Use case:</strong> Relationship transitions, symmetric queries</p>
<h3 id="other-access"><a class="header" href="#other-access">Other Access</a></h3>
<p>In relationships, <code>other</code> refers to other participants:</p>
<pre><code class="language-storybook">other.bond
other.aware_of_mentor
other.respect
</code></pre>
<p><strong>Use case:</strong> Relationship queries with perspective</p>
<h3 id="example-in-life-arcs"><a class="header" href="#example-in-life-arcs">Example in Life Arcs</a></h3>
<pre><code class="language-storybook">life_arc RelationshipState {
state new {
on self.bond &gt; 0.7 and other.bond &gt; 0.7 -&gt; stable
}
state stable {
on self.bond &lt; 0.3 -&gt; troubled
on other.bond &lt; 0.3 -&gt; troubled
}
state troubled {
on self.bond &lt; 0.1 or other.bond &lt; 0.1 -&gt; broken
}
state broken {}
}
</code></pre>
<h2 id="quantifiers"><a class="header" href="#quantifiers">Quantifiers</a></h2>
<p>Quantifiers test conditions over collections.</p>
<h3 id="forall-forall"><a class="header" href="#forall-forall">ForAll: <code>forall</code></a></h3>
<p>Tests if a condition holds for all elements in a collection.</p>
<pre><code class="language-storybook">forall e in enemies: e.defeated
forall item in inventory: item.weight &lt; 10
</code></pre>
<p><strong>Syntax:</strong></p>
<pre><code class="language-bnf">forall &lt;variable&gt; in &lt;collection&gt;: &lt;predicate&gt;
</code></pre>
<p><strong>Semantics:</strong></p>
<ul>
<li>Returns <code>true</code> if predicate is true for every element</li>
<li>Returns <code>true</code> for empty collections (vacuously true)</li>
</ul>
<p><strong>Examples:</strong></p>
<pre><code class="language-storybook">// All enemies defeated?
forall enemy in enemies: enemy.health &lt;= 0
// All party members ready?
forall member in party: member.is_ready
// All doors locked?
forall door in doors: door.is_locked
</code></pre>
<h3 id="exists-exists"><a class="header" href="#exists-exists">Exists: <code>exists</code></a></h3>
<p>Tests if a condition holds for at least one element.</p>
<pre><code class="language-storybook">exists e in enemies: e.is_hostile
exists item in inventory: item.is_healing_potion
</code></pre>
<p><strong>Syntax:</strong></p>
<pre><code class="language-bnf">exists &lt;variable&gt; in &lt;collection&gt;: &lt;predicate&gt;
</code></pre>
<p><strong>Semantics:</strong></p>
<ul>
<li>Returns <code>true</code> if predicate is true for any element</li>
<li>Returns <code>false</code> for empty collections</li>
</ul>
<p><strong>Examples:</strong></p>
<pre><code class="language-storybook">// Any enemy nearby?
exists enemy in enemies: enemy.distance &lt; 10
// Any door unlocked?
exists door in doors: not door.is_locked
// Any ally wounded?
exists ally in allies: ally.health &lt; ally.max_health * 0.5
</code></pre>
<h3 id="nested-quantifiers"><a class="header" href="#nested-quantifiers">Nested Quantifiers</a></h3>
<p>Quantifiers can nest:</p>
<pre><code class="language-storybook">forall team in teams: exists player in team: player.is_leader
// Every team has at least one leader
exists room in dungeon: forall enemy in room.enemies: enemy.defeated
// At least one room has all enemies defeated
</code></pre>
<h2 id="usage-in-context"><a class="header" href="#usage-in-context">Usage in Context</a></h2>
<h3 id="life-arc-transitions"><a class="header" href="#life-arc-transitions">Life Arc Transitions</a></h3>
<pre><code class="language-storybook">life_arc CombatState {
state idle {
on enemy_count &gt; 0 -&gt; combat
}
state combat {
on health &lt; 20 -&gt; fleeing
on enemy_count == 0 -&gt; victorious
}
state fleeing {
on distance_from_enemies &gt; 100 -&gt; safe
}
state victorious {
on celebration_complete -&gt; idle
}
state safe {
on health &gt;= 50 -&gt; idle
}
}
</code></pre>
<h3 id="behavior-tree-conditions"><a class="header" href="#behavior-tree-conditions">Behavior Tree Conditions</a></h3>
<pre><code class="language-storybook">behavior GuardedAction {
if(health &gt; 50 and has_weapon) {
AggressiveAttack
}
}
behavior ConditionalChoice {
choose tactics {
then melee {
if(distance &lt; 5 and weapon_type == "sword")
MeleeAttack
}
then ranged {
if(distance &gt;= 5 and has_arrows)
RangedAttack
}
}
}
</code></pre>
<h3 id="behavior-tree-conditions-1"><a class="header" href="#behavior-tree-conditions-1">Behavior Tree Conditions</a></h3>
<pre><code class="language-storybook">behavior SmartAI {
choose strategy {
then aggressive {
if(health &gt; 70 and enemy_count &lt; 3)
Attack
}
then defensive {
if(health &lt; 30 or enemy_count &gt;= 5)
Defend
}
then balanced {
if(health &gt;= 30 and health &lt;= 70)
TacticalManeuver
}
}
}
</code></pre>
<h2 id="type-system"><a class="header" href="#type-system">Type System</a></h2>
<h3 id="type-compatibility"><a class="header" href="#type-compatibility">Type Compatibility</a></h3>
<p>Comparisons require compatible types:</p>
<div class="table-wrapper"><table><thead><tr><th>Operator</th><th>Left Type</th><th>Right Type</th><th>Valid?</th></tr></thead><tbody>
<tr><td><code>==</code>, <code>!=</code></td><td>int</td><td>int</td><td></td></tr>
<tr><td><code>==</code>, <code>!=</code></td><td>float</td><td>float</td><td></td></tr>
<tr><td><code>==</code>, <code>!=</code></td><td>string</td><td>string</td><td></td></tr>
<tr><td><code>==</code>, <code>!=</code></td><td>bool</td><td>bool</td><td></td></tr>
<tr><td><code>==</code>, <code>!=</code></td><td>enum</td><td>same enum</td><td></td></tr>
<tr><td><code>==</code>, <code>!=</code></td><td>int</td><td>float</td><td></td></tr>
<tr><td><code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code></td><td>int</td><td>int</td><td></td></tr>
<tr><td><code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code></td><td>float</td><td>float</td><td></td></tr>
<tr><td><code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code></td><td>string</td><td>string</td><td></td></tr>
</tbody></table>
</div>
<h3 id="implicit-coercion"><a class="header" href="#implicit-coercion">Implicit Coercion</a></h3>
<p><strong>None.</strong> Storybook has no implicit type coercion. All comparisons must be between compatible types.</p>
<p><strong>Error:</strong></p>
<pre><code class="language-storybook">count == "5" // Error: int vs string
health &lt; true // Error: int vs bool
</code></pre>
<p><strong>Correct:</strong></p>
<pre><code class="language-storybook">count == 5
health &lt; 50
</code></pre>
<h2 id="special-keyword-is"><a class="header" href="#special-keyword-is">Special Keyword: <code>is</code></a></h2>
<p>The <code>is</code> keyword provides syntactic sugar for equality with enum values:</p>
<pre><code class="language-storybook">// Instead of:
status == active
// You can write:
status is active
</code></pre>
<p><strong>More examples:</strong></p>
<pre><code class="language-storybook">name is "Martha"
skill_level is master
emotional_state is focused
</code></pre>
<p>This is purely syntactic—<code>is</code> and <code>==</code> are equivalent.</p>
<h2 id="complete-examples"><a class="header" href="#complete-examples">Complete Examples</a></h2>
<h3 id="simple-conditions"><a class="header" href="#simple-conditions">Simple Conditions</a></h3>
<pre><code class="language-storybook">health &lt; 20
enemy_nearby
not is_ready
count &gt; 5
</code></pre>
<h3 id="complex-conditions"><a class="header" href="#complex-conditions">Complex Conditions</a></h3>
<pre><code class="language-storybook">(health &lt; 20 and not has_potion) or surrounded
forall e in enemies: e.defeated
exists item in inventory: item.is_healing_potion and item.quantity &gt; 0
</code></pre>
<h3 id="life-arc-with-complex-conditions"><a class="header" href="#life-arc-with-complex-conditions">Life Arc with Complex Conditions</a></h3>
<pre><code class="language-storybook">life_arc CharacterMood {
state content {
on health &lt; 30 or hunger &gt; 80 -&gt; distressed
on social_interaction &gt; 0.8 -&gt; happy
}
state distressed {
on health &gt;= 50 and hunger &lt; 30 -&gt; content
on (health &lt; 10 or hunger &gt; 95) and help_available -&gt; desperate
}
state happy {
on social_interaction &lt; 0.3 -&gt; content
on received_bad_news -&gt; distressed
}
state desperate {
on help_received -&gt; distressed
}
}
</code></pre>
<h3 id="behavior-with-quantifiers"><a class="header" href="#behavior-with-quantifiers">Behavior with Quantifiers</a></h3>
<pre><code class="language-storybook">behavior SquadLeader {
choose leadership {
then regroup {
if(squad_has_wounded)
OrderRetreat
}
then advance {
if(squad_all_ready)
OrderAdvance
}
then hold_position {
if(not squad_all_ready)
OrderHold
}
}
}
</code></pre>
<h3 id="relationship-query"><a class="header" href="#relationship-query">Relationship Query</a></h3>
<pre><code class="language-storybook">life_arc FriendshipQuality {
state new_friends {
on self.bond &gt; 0.7 and other.bond &gt; 0.7 -&gt; strong_bond
on self.trust &lt; 0.3 or other.trust &lt; 0.3 -&gt; shaky
}
state strong_bond {
on self.bond &lt; 0.5 -&gt; weakening
}
state weakening {
on self.bond &lt; 0.2 or other.bond &lt; 0.2 -&gt; ended
on self.bond &gt; 0.7 and other.bond &gt; 0.7 -&gt; strong_bond
}
state shaky {
on self.trust &gt; 0.6 and other.trust &gt; 0.6 -&gt; new_friends
on self.trust &lt; 0.1 or other.trust &lt; 0.1 -&gt; ended
}
state ended {}
}
</code></pre>
<h2 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h2>
<ol>
<li><strong>Type consistency</strong>: Both sides of comparison must be compatible types</li>
<li><strong>Boolean context</strong>: Logical operators (<code>and</code>, <code>or</code>, <code>not</code>) require boolean operands</li>
<li><strong>Field existence</strong>: Referenced fields must exist on the entity</li>
<li><strong>Collection validity</strong>: Quantifiers require collection-typed expressions</li>
<li><strong>Variable scope</strong>: Quantifier variables only valid within their predicate</li>
<li><strong>No division by zero</strong>: Arithmetic operations must not divide by zero</li>
<li><strong>Enum validity</strong>: Enum comparisons must reference defined enum values</li>
</ol>
<h2 id="best-practices"><a class="header" href="#best-practices">Best Practices</a></h2>
<h3 id="1-use-parentheses-for-clarity"><a class="header" href="#1-use-parentheses-for-clarity">1. Use Parentheses for Clarity</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">health &lt; 50 or is_poisoned and has_antidote
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">(health &lt; 50 or is_poisoned) and has_antidote
</code></pre>
<h3 id="2-break-complex-conditions"><a class="header" href="#2-break-complex-conditions">2. Break Complex Conditions</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">on (health &lt; 20 and not has_potion) or (surrounded and not has_escape) or (enemy_count &gt; 10 and weapon_broken) -&gt; desperate
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">state combat {
on health &lt; 20 and not has_potion -&gt; desperate
on surrounded and not has_escape -&gt; desperate
on enemy_count &gt; 10 and weapon_broken -&gt; desperate
}
</code></pre>
<h3 id="3-name-complex-conditions"><a class="header" href="#3-name-complex-conditions">3. Name Complex Conditions</a></h3>
<p>For repeated complex conditions, consider using intermediate fields:</p>
<p><strong>Instead of:</strong></p>
<pre><code class="language-storybook">on health &lt; (max_health * 0.2) and enemy_count &gt; 5 -&gt; flee
</code></pre>
<p><strong>Consider:</strong></p>
<pre><code class="language-storybook">// In character definition:
critically_wounded: health &lt; (max_health * 0.2)
outnumbered: enemy_count &gt; 5
// In life arc:
on critically_wounded and outnumbered -&gt; flee
</code></pre>
<h3 id="4-use-is-for-enums"><a class="header" href="#4-use-is-for-enums">4. Use <code>is</code> for Enums</a></h3>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">status is active
emotional_state is focused
</code></pre>
<p><strong>Over:</strong></p>
<pre><code class="language-storybook">status == active
emotional_state == focused
</code></pre>
<h3 id="5-quantifiers-for-collections"><a class="header" href="#5-quantifiers-for-collections">5. Quantifiers for Collections</a></h3>
<p><strong>Avoid:</strong></p>
<pre><code class="language-storybook">// Manual checks for each element
if enemy1.defeated and enemy2.defeated and enemy3.defeated
</code></pre>
<p><strong>Prefer:</strong></p>
<pre><code class="language-storybook">if forall enemy in enemies: enemy.defeated
</code></pre>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./13-life-arcs.html">Life Arcs</a> - Transition conditions</li>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> - Guard and condition nodes</li>
<li><a href="./12-decorators.html">Decorators</a> - Guard decorator</li>
<li><a href="./15-relationships.html">Relationships</a> - Self/other field access</li>
<li><a href="./18-value-types.html">Value Types</a> - Literal value types</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Type safety</strong>: Strong typing prevents type errors at compile time</li>
<li><strong>Short-circuit evaluation</strong>: AND/OR operators optimize evaluation</li>
<li><strong>Quantifiers</strong>: Enable expressive collection queries</li>
<li><strong>Field access</strong>: Context-sensitive (<code>self</code>, <code>other</code>) for relationships</li>
<li><strong>Boolean algebra</strong>: Standard logical operators with expected semantics</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/16-other-declarations.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/18-value-types.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/16-other-declarations.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/18-value-types.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,857 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Value Types - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="values"><a class="header" href="#values">Values</a></h1>
<p>Values are the fundamental data types in Storybook. Every field in a character, template, or other declaration contains a value. This chapter provides a complete reference for all supported value types.</p>
<h2 id="value-types-overview"><a class="header" href="#value-types-overview">Value Types Overview</a></h2>
<p>Storybook supports 12 value types:</p>
<div class="table-wrapper"><table><thead><tr><th>Type</th><th>Example</th><th>Use Case</th></tr></thead><tbody>
<tr><td><strong>Int</strong></td><td><code>42</code>, <code>-7</code></td><td>Quantities, IDs, counts</td></tr>
<tr><td><strong>Float</strong></td><td><code>3.14</code>, <code>-0.5</code></td><td>Measurements, probabilities</td></tr>
<tr><td><strong>String</strong></td><td><code>"Hello"</code>, <code>"Martha"</code></td><td>Text, names, descriptions</td></tr>
<tr><td><strong>Bool</strong></td><td><code>true</code>, <code>false</code></td><td>Flags, switches</td></tr>
<tr><td><strong>Time</strong></td><td><code>14:30</code>, <code>09:15:30</code></td><td>Clock times, schedule blocks</td></tr>
<tr><td><strong>Duration</strong></td><td><code>2h30m</code>, <code>45s</code></td><td>Time intervals</td></tr>
<tr><td><strong>Range</strong></td><td><code>20..40</code>, <code>0.5..1.0</code></td><td>Template variation bounds</td></tr>
<tr><td><strong>Identifier</strong></td><td><code>Martha</code>, <code>items::sword</code></td><td>References to other declarations</td></tr>
<tr><td><strong>List</strong></td><td><code>[1, 2, 3]</code>, <code>["a", "b"]</code></td><td>Collections</td></tr>
<tr><td><strong>Object</strong></td><td><code>{x: 10, y: 20}</code></td><td>Structured data</td></tr>
<tr><td><strong>ProseBlock</strong></td><td><code>---tag content ---</code></td><td>Long-form narrative</td></tr>
<tr><td><strong>Override</strong></td><td><code>TemplateX with {...}</code></td><td>Template instantiation with modifications</td></tr>
</tbody></table>
</div>
<h2 id="integer"><a class="header" href="#integer">Integer</a></h2>
<p>Signed 64-bit integers.</p>
<h3 id="syntax"><a class="header" href="#syntax">Syntax</a></h3>
<pre><code class="language-bnf">&lt;int&gt; ::= ["-"] &lt;digit&gt;+
&lt;digit&gt; ::= "0".."9"
</code></pre>
<h3 id="examples"><a class="header" href="#examples">Examples</a></h3>
<pre><code class="language-storybook">character Hero {
age: 25
gold: 1500
reputation: -10
}
</code></pre>
<h3 id="range"><a class="header" href="#range">Range</a></h3>
<ul>
<li>Minimum: <code>-9,223,372,036,854,775,808</code></li>
<li>Maximum: <code>9,223,372,036,854,775,807</code></li>
</ul>
<h3 id="use-cases"><a class="header" href="#use-cases">Use Cases</a></h3>
<ul>
<li>Counts (items, population)</li>
<li>Identifiers (IDs, keys)</li>
<li>Scores (reputation, alignment)</li>
<li>Whole quantities (gold pieces, HP)</li>
</ul>
<h2 id="float"><a class="header" href="#float">Float</a></h2>
<p>64-bit floating-point numbers (IEEE 754 double precision).</p>
<h3 id="syntax-1"><a class="header" href="#syntax-1">Syntax</a></h3>
<pre><code class="language-bnf">&lt;float&gt; ::= ["-"] &lt;digit&gt;+ "." &lt;digit&gt;+
| ["-"] &lt;digit&gt;+ ("e" | "E") ["+"|"-"] &lt;digit&gt;+
</code></pre>
<h3 id="examples-1"><a class="header" href="#examples-1">Examples</a></h3>
<pre><code class="language-storybook">character Wizard {
mana: 100.0
spell_power: 1.5
corruption: 0.03
precision: 1e-6
}
</code></pre>
<h3 id="special-values"><a class="header" href="#special-values">Special Values</a></h3>
<ul>
<li>No <code>NaN</code> or <code>Infinity</code> support (use validation to prevent)</li>
<li>Negative zero (<code>-0.0</code>) equals positive zero (<code>0.0</code>)</li>
</ul>
<h3 id="use-cases-1"><a class="header" href="#use-cases-1">Use Cases</a></h3>
<ul>
<li>Probabilities (0.0 to 1.0)</li>
<li>Percentages (0.0 to 100.0)</li>
<li>Measurements with precision</li>
<li>Ratios and scaling factors</li>
</ul>
<h2 id="string"><a class="header" href="#string">String</a></h2>
<p>UTF-8 encoded text enclosed in double quotes.</p>
<h3 id="syntax-2"><a class="header" href="#syntax-2">Syntax</a></h3>
<pre><code class="language-bnf">&lt;string&gt; ::= '"' &lt;string-char&gt;* '"'
&lt;string-char&gt; ::= &lt;any-char-except-quote-or-backslash&gt;
| &lt;escape-sequence&gt;
&lt;escape-sequence&gt; ::= "\n" | "\r" | "\t" | "\\" | "\""
</code></pre>
<h3 id="escape-sequences"><a class="header" href="#escape-sequences">Escape Sequences</a></h3>
<ul>
<li><code>\n</code> - Newline</li>
<li><code>\r</code> - Carriage return</li>
<li><code>\t</code> - Tab</li>
<li><code>\\</code> - Backslash</li>
<li><code>\"</code> - Double quote</li>
</ul>
<h3 id="examples-2"><a class="header" href="#examples-2">Examples</a></h3>
<pre><code class="language-storybook">character Martha {
name: "Martha Baker"
greeting: "Fresh from the oven!"
multiline: "Line 1\nLine 2\nLine 3"
quote: "She said, \"The bread is ready!\""
}
</code></pre>
<h3 id="limitations"><a class="header" href="#limitations">Limitations</a></h3>
<ul>
<li>No raw strings (r“…“) or multi-line literals</li>
<li>For long text, use <a href="#prose-blocks">prose blocks</a></li>
<li>Maximum length: Implementation-defined (typically several MB)</li>
</ul>
<h3 id="use-cases-2"><a class="header" href="#use-cases-2">Use Cases</a></h3>
<ul>
<li>Character names</li>
<li>Short descriptions</li>
<li>Dialogue snippets</li>
<li>Enum-like values (before proper enums)</li>
</ul>
<h2 id="boolean"><a class="header" href="#boolean">Boolean</a></h2>
<p>Logical true/false values.</p>
<h3 id="syntax-3"><a class="header" href="#syntax-3">Syntax</a></h3>
<pre><code class="language-bnf">&lt;bool&gt; ::= "true" | "false"
</code></pre>
<h3 id="examples-3"><a class="header" href="#examples-3">Examples</a></h3>
<pre><code class="language-storybook">character Guard {
is_awake: true
has_seen_player: false
loyal_to_king: true
}
</code></pre>
<h3 id="use-cases-3"><a class="header" href="#use-cases-3">Use Cases</a></h3>
<ul>
<li>Feature flags (can_fly, is_hostile)</li>
<li>State tracking (door_open, quest_complete)</li>
<li>Conditions (is_friendly, accepts_bribes)</li>
</ul>
<h2 id="time"><a class="header" href="#time">Time</a></h2>
<p>Clock time in 24-hour format.</p>
<h3 id="syntax-4"><a class="header" href="#syntax-4">Syntax</a></h3>
<pre><code class="language-bnf">&lt;time&gt; ::= &lt;hour&gt; ":" &lt;minute&gt;
| &lt;hour&gt; ":" &lt;minute&gt; ":" &lt;second&gt;
&lt;hour&gt; ::= "0".."23"
&lt;minute&gt; ::= "0".."59"
&lt;second&gt; ::= "0".."59"
</code></pre>
<h3 id="examples-4"><a class="header" href="#examples-4">Examples</a></h3>
<pre><code class="language-storybook">schedule BakerySchedule {
block {
start: 06:00
end: 08:30
action: baking::prepare_dough
}
block {
start: 14:30:15 // With seconds
end: 15:00:00
action: baking::afternoon_cleanup
}
}
character EarlyRiser {
wake_time: 05:30
bedtime: 21:00
}
</code></pre>
<h3 id="validation"><a class="header" href="#validation">Validation</a></h3>
<ul>
<li>Hours: 0-23 (24-hour format)</li>
<li>Minutes: 0-59</li>
<li>Seconds: 0-59 (optional)</li>
<li>No AM/PM notation</li>
<li>No timezone support (context-dependent)</li>
</ul>
<h3 id="use-cases-4"><a class="header" href="#use-cases-4">Use Cases</a></h3>
<ul>
<li>Schedule blocks (see <a href="./16-schedules.html">Schedules</a>)</li>
<li>Character routines</li>
<li>Event timing</li>
<li>Time-based conditions</li>
</ul>
<h2 id="duration"><a class="header" href="#duration">Duration</a></h2>
<p>Time intervals with hour/minute/second components.</p>
<h3 id="syntax-5"><a class="header" href="#syntax-5">Syntax</a></h3>
<pre><code class="language-bnf">&lt;duration&gt; ::= &lt;duration-component&gt;+
&lt;duration-component&gt; ::= &lt;number&gt; ("h" | "m" | "s")
&lt;number&gt; ::= &lt;digit&gt;+
</code></pre>
<h3 id="examples-5"><a class="header" href="#examples-5">Examples</a></h3>
<pre><code class="language-storybook">character Traveler {
journey_time: 2h30m
rest_needed: 8h
sprint_duration: 45s
}
behavior TimedAction {
choose {
timeout(5m) {
CompleteObjective
}
cooldown(30s) {
UseSpecialAbility
}
}
}
</code></pre>
<h3 id="components"><a class="header" href="#components">Components</a></h3>
<ul>
<li><code>h</code> - Hours</li>
<li><code>m</code> - Minutes</li>
<li><code>s</code> - Seconds</li>
</ul>
<p>Can combine multiple components: <code>1h30m15s</code></p>
<h3 id="validation-1"><a class="header" href="#validation-1">Validation</a></h3>
<ul>
<li>All components non-negative</li>
<li>No fractional components (<code>1.5h</code> not allowed; use <code>1h30m</code>)</li>
<li>Can specify same unit multiple times: <code>90m</code> = <code>1h30m</code> (normalized)</li>
</ul>
<h3 id="use-cases-5"><a class="header" href="#use-cases-5">Use Cases</a></h3>
<ul>
<li>Behavior tree timeouts/cooldowns</li>
<li>Travel times</li>
<li>Cooldown periods</li>
<li>Event durations</li>
</ul>
<h2 id="range-1"><a class="header" href="#range-1">Range</a></h2>
<p>Numeric range with inclusive bounds (for templates only).</p>
<h3 id="syntax-6"><a class="header" href="#syntax-6">Syntax</a></h3>
<pre><code class="language-bnf">&lt;range&gt; ::= &lt;value&gt; ".." &lt;value&gt;
</code></pre>
<p>Both bounds must be the same type (both int or both float).</p>
<h3 id="examples-6"><a class="header" href="#examples-6">Examples</a></h3>
<pre><code class="language-storybook">template Villager {
age: 18..65
wealth: 10..100
height: 150.0..190.0 // Float range
}
template RandomEvent {
probability: 0.0..1.0
damage: 5..20
}
</code></pre>
<h3 id="semantics"><a class="header" href="#semantics">Semantics</a></h3>
<ul>
<li><strong>Templates only</strong>: Ranges are only valid in templates</li>
<li><strong>Instantiation</strong>: When a template is used, a specific value within the range is selected</li>
<li><strong>Inclusive bounds</strong>: Both <code>min</code> and <code>max</code> are included</li>
<li><strong>Order matters</strong>: <code>min</code> must be ≤ <code>max</code></li>
</ul>
<h3 id="validation-2"><a class="header" href="#validation-2">Validation</a></h3>
<ul>
<li>Both bounds same type</li>
<li><code>min</code><code>max</code></li>
<li>Cannot use ranges in character/species/etc. (only templates)</li>
</ul>
<h3 id="use-cases-6"><a class="header" href="#use-cases-6">Use Cases</a></h3>
<ul>
<li>Template variation</li>
<li>Procedural generation</li>
<li>Random NPC attributes</li>
<li>Loot table ranges</li>
</ul>
<h2 id="identifier"><a class="header" href="#identifier">Identifier</a></h2>
<p>Reference to another declaration by qualified path.</p>
<h3 id="syntax-7"><a class="header" href="#syntax-7">Syntax</a></h3>
<pre><code class="language-bnf">&lt;identifier&gt; ::= &lt;simple-name&gt;
| &lt;qualified-path&gt;
&lt;simple-name&gt; ::= &lt;ident&gt;
&lt;qualified-path&gt; ::= &lt;ident&gt; ("::" &lt;ident&gt;)+
</code></pre>
<h3 id="examples-7"><a class="header" href="#examples-7">Examples</a></h3>
<pre><code class="language-storybook">character Martha: Human { // Species reference
// ...
}
character Elena from Apprentice { // Template reference
// ...
}
character Guard {
uses behaviors: [
{ tree: guards::patrol } // Behavior reference
]
uses schedule: DailyRoutine // Schedule reference
}
relationship Partnership {
Martha // Character reference
Jane // Character reference
}
</code></pre>
<h3 id="resolution"><a class="header" href="#resolution">Resolution</a></h3>
<ul>
<li><strong>Unqualified</strong>: Searches current module, then imports</li>
<li><strong>Qualified</strong>: <code>module::submodule::Name</code></li>
<li><strong>Validation</strong>: Compiler ensures all references resolve</li>
</ul>
<h3 id="use-cases-7"><a class="header" href="#use-cases-7">Use Cases</a></h3>
<ul>
<li>Species typing (<code>: Species</code>)</li>
<li>Template inheritance (<code>from Template</code>)</li>
<li>Behavior tree references</li>
<li>Schedule references</li>
<li>Relationship participants</li>
<li>Cross-references</li>
</ul>
<h2 id="list"><a class="header" href="#list">List</a></h2>
<p>Ordered collection of values.</p>
<h3 id="syntax-8"><a class="header" href="#syntax-8">Syntax</a></h3>
<pre><code class="language-bnf">&lt;list&gt; ::= "[" "]"
| "[" &lt;value&gt; ("," &lt;value&gt;)* "]"
</code></pre>
<h3 id="examples-8"><a class="header" href="#examples-8">Examples</a></h3>
<pre><code class="language-storybook">character Mage {
known_spells: ["Fireball", "Lightning", "Shield"]
spell_levels: [3, 5, 2]
party_members: [Martha, Jane, Elena]
}
character Traveler {
inventory: [
{item: "Sword", damage: 10},
{item: "Potion", healing: 50},
{item: "Key", opens: "TowerDoor"}
]
}
</code></pre>
<h3 id="type-constraints"><a class="header" href="#type-constraints">Type Constraints</a></h3>
<ul>
<li><strong>Homogeneous preferred</strong>: All elements should be the same type</li>
<li><strong>Heterogeneous allowed</strong>: Mixed types permitted but discouraged</li>
<li><strong>Empty lists</strong>: <code>[]</code> is valid</li>
</ul>
<h3 id="operations"><a class="header" href="#operations">Operations</a></h3>
<p>Lists are immutable at declaration time. Runtime list operations depend on the execution environment.</p>
<h3 id="use-cases-8"><a class="header" href="#use-cases-8">Use Cases</a></h3>
<ul>
<li>Collections (inventory, spells, party members)</li>
<li>References (multiple behaviors, schedules, participants)</li>
<li>Enum-like sets (before proper enums)</li>
</ul>
<h2 id="object"><a class="header" href="#object">Object</a></h2>
<p>Structured data with named fields.</p>
<h3 id="syntax-9"><a class="header" href="#syntax-9">Syntax</a></h3>
<pre><code class="language-bnf">&lt;object&gt; ::= "{" "}"
| "{" &lt;field&gt; ("," &lt;field&gt;)* "}"
&lt;field&gt; ::= &lt;identifier&gt; ":" &lt;value&gt;
</code></pre>
<h3 id="examples-9"><a class="header" href="#examples-9">Examples</a></h3>
<pre><code class="language-storybook">character Hero {
position: {x: 100, y: 200}
stats: {
strength: 15,
dexterity: 12,
intelligence: 10
}
equipment: {
weapon: {
name: "Longsword",
damage: 10,
enchantment: "Fire"
},
armor: {
name: "Plate Mail",
defense: 8
}
}
}
</code></pre>
<h3 id="nesting"><a class="header" href="#nesting">Nesting</a></h3>
<p>Objects can be nested arbitrarily deep:</p>
<pre><code class="language-storybook">character Complex {
deep: {
level1: {
level2: {
level3: {
value: 42
}
}
}
}
}
</code></pre>
<h3 id="use-cases-9"><a class="header" href="#use-cases-9">Use Cases</a></h3>
<ul>
<li>Structured data (position, stats)</li>
<li>Nested configurations</li>
<li>Inline data structures</li>
<li>Complex field values</li>
</ul>
<h2 id="prose-blocks"><a class="header" href="#prose-blocks">Prose Blocks</a></h2>
<p>Long-form narrative text with semantic tags.</p>
<h3 id="syntax-10"><a class="header" href="#syntax-10">Syntax</a></h3>
<pre><code class="language-bnf">&lt;prose-block&gt; ::= "---" &lt;tag&gt; &lt;content&gt; "---"
&lt;tag&gt; ::= &lt;identifier&gt;
&lt;content&gt; ::= &lt;any-text-except-triple-dash&gt;
</code></pre>
<h3 id="common-tags"><a class="header" href="#common-tags">Common Tags</a></h3>
<ul>
<li><code>---description</code>: General description</li>
<li><code>---backstory</code>: Character history</li>
<li><code>---appearance</code>: Physical description</li>
<li><code>---personality</code>: Behavioral traits</li>
<li><code>---motivation</code>: Goals and desires</li>
<li><code>---notes</code>: Meta-commentary</li>
<li><code>---ecology</code>: Species habitat/behavior</li>
<li><code>---culture</code>: Social structures</li>
<li><code>---narrative</code>: Story context</li>
</ul>
<h3 id="examples-10"><a class="header" href="#examples-10">Examples</a></h3>
<pre><code class="language-storybook">character Martha {
age: 34
---backstory
Martha learned to bake from her grandmother, starting at age
twelve. She now runs the most popular bakery in town, known
for her sourdough bread and unwavering quality standards.
---
---personality
Meticulous and patient, with an unwavering commitment to
quality. Tough but fair with her staff, and deeply loyal
to the customers who have supported her bakery for years.
---
}
species Dragon {
lifespan: 1000
---ecology
Dragons nest in high mountain caves and emerge every few decades
to hunt large prey. They hoard treasure as a mating display.
---
}
</code></pre>
<h3 id="formatting"><a class="header" href="#formatting">Formatting</a></h3>
<ul>
<li>Leading/trailing whitespace is preserved</li>
<li>No escape sequences needed</li>
<li>Can contain any characters except <code>---</code> on its own line</li>
<li>Indentation is significant for readability but not semantics</li>
</ul>
<h3 id="multiple-prose-blocks"><a class="header" href="#multiple-prose-blocks">Multiple Prose Blocks</a></h3>
<p>A single declaration can have multiple prose blocks with different tags:</p>
<pre><code class="language-storybook">character Gandalf {
---appearance
An old man with a long gray beard, pointed hat, and staff.
---
---backstory
One of the Istari sent to Middle-earth to contest Sauron.
---
---personality
Patient and wise, but with a mischievous streak.
---
}
</code></pre>
<h3 id="use-cases-10"><a class="header" href="#use-cases-10">Use Cases</a></h3>
<ul>
<li>Character backstories</li>
<li>Species descriptions</li>
<li>World-building flavor text</li>
<li>Design notes</li>
<li>Narrative context</li>
</ul>
<h2 id="override"><a class="header" href="#override">Override</a></h2>
<p>Template instantiation with field modifications.</p>
<h3 id="syntax-11"><a class="header" href="#syntax-11">Syntax</a></h3>
<pre><code class="language-bnf">&lt;override&gt; ::= &lt;qualified-path&gt; "with" "{" &lt;override-op&gt;* "}"
&lt;override-op&gt; ::= &lt;identifier&gt; ":" &lt;value&gt; // Set
| "remove" &lt;identifier&gt; // Remove
| "append" &lt;identifier&gt; ":" &lt;value&gt; // Append (for lists)
</code></pre>
<h3 id="examples-11"><a class="header" href="#examples-11">Examples</a></h3>
<h4 id="basic-override"><a class="header" href="#basic-override">Basic Override</a></h4>
<pre><code class="language-storybook">template BaseWarrior {
strength: 10
dexterity: 8
weapon: "Sword"
}
character StrongWarrior {
stats: BaseWarrior with {
strength: 15 // Override strength
}
}
</code></pre>
<h4 id="remove-fields"><a class="header" href="#remove-fields">Remove Fields</a></h4>
<pre><code class="language-storybook">template FullEquipment {
helmet: "Iron"
chest: "Plate"
legs: "Mail"
boots: "Leather"
}
character LightFighter {
equipment: FullEquipment with {
remove helmet
remove chest
}
}
</code></pre>
<h4 id="append-to-lists"><a class="header" href="#append-to-lists">Append to Lists</a></h4>
<pre><code class="language-storybook">template BasicSpells {
spells: ["Fireball", "Shield"]
}
character AdvancedMage {
magic: BasicSpells with {
append spells: "Teleport"
append spells: "Lightning"
}
// Result: ["Fireball", "Shield", "Teleport", "Lightning"]
}
</code></pre>
<h4 id="complex-override"><a class="header" href="#complex-override">Complex Override</a></h4>
<pre><code class="language-storybook">template RogueTemplate {
stealth: 15
lockpicking: 12
backstab_damage: 20
equipment: ["Dagger", "Lockpicks"]
}
character MasterThief {
abilities: RogueTemplate with {
stealth: 20 // Set
remove backstab_damage // Remove
append equipment: "Grappling Hook" // Append
}
}
</code></pre>
<h3 id="operations-1"><a class="header" href="#operations-1">Operations</a></h3>
<h4 id="set-default"><a class="header" href="#set-default"><code>set</code> (default)</a></h4>
<p>Replace a fields value:</p>
<pre><code class="language-storybook">field_name: new_value
</code></pre>
<h4 id="remove"><a class="header" href="#remove"><code>remove</code></a></h4>
<p>Delete a field from the template:</p>
<pre><code class="language-storybook">remove field_name
</code></pre>
<h4 id="append"><a class="header" href="#append"><code>append</code></a></h4>
<p>Add to a list field (field must be a list):</p>
<pre><code class="language-storybook">append field_name: value_to_add
</code></pre>
<h3 id="validation-3"><a class="header" href="#validation-3">Validation</a></h3>
<ul>
<li>Base template must exist</li>
<li>Overridden fields must exist in template</li>
<li>Removed fields must exist in template</li>
<li>Appended fields must be lists</li>
<li>Type compatibility enforced</li>
</ul>
<h3 id="use-cases-11"><a class="header" href="#use-cases-11">Use Cases</a></h3>
<ul>
<li>Customizing template instances</li>
<li>Procedural character generation</li>
<li>Variant creation</li>
<li>Data composition</li>
</ul>
<h2 id="type-coercion"><a class="header" href="#type-coercion">Type Coercion</a></h2>
<p>Storybook has <strong>no implicit type coercion</strong>. All type conversions must be explicit.</p>
<p><strong>Not allowed:</strong></p>
<pre><code class="language-storybook">character Wrong {
count: "42" // Error: expected int, got string
flag: 1 // Error: expected bool, got int
}
</code></pre>
<p><strong>Correct:</strong></p>
<pre><code class="language-storybook">character Right {
count: 42
flag: true
}
</code></pre>
<h2 id="null-and-optional"><a class="header" href="#null-and-optional">Null and Optional</a></h2>
<p>Storybook <strong>does not have <code>null</code></strong>. For optional values, use:</p>
<ol>
<li>
<p><strong>Template ranges</strong> with 0 as lower bound:</p>
<pre><code class="language-storybook">template MaybeWeapon {
weapon_count: 0..5
}
</code></pre>
</li>
<li>
<p><strong>Boolean flags</strong>:</p>
<pre><code class="language-storybook">character Guard {
has_weapon: false
}
</code></pre>
</li>
<li>
<p><strong>Empty lists</strong>:</p>
<pre><code class="language-storybook">character Unarmed {
weapons: []
}
</code></pre>
</li>
</ol>
<h2 id="summary-table"><a class="header" href="#summary-table">Summary Table</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Type</th><th>Mutable?</th><th>Comparable?</th><th>Valid in Templates?</th><th>Notes</th></tr></thead><tbody>
<tr><td>Int</td><td>No</td><td>Yes</td><td>Yes</td><td>64-bit signed</td></tr>
<tr><td>Float</td><td>No</td><td>Yes</td><td>Yes</td><td>64-bit IEEE 754</td></tr>
<tr><td>String</td><td>No</td><td>Yes</td><td>Yes</td><td>UTF-8</td></tr>
<tr><td>Bool</td><td>No</td><td>Yes</td><td>Yes</td><td>true/false</td></tr>
<tr><td>Time</td><td>No</td><td>Yes</td><td>No</td><td>HH:MM or HH:MM:SS</td></tr>
<tr><td>Duration</td><td>No</td><td>Yes</td><td>No</td><td>Compounds (2h30m)</td></tr>
<tr><td>Range</td><td>No</td><td>No</td><td>Yes (only)</td><td>Template variation</td></tr>
<tr><td>Identifier</td><td>No</td><td>Yes</td><td>Yes</td><td>Declaration reference</td></tr>
<tr><td>List</td><td>No</td><td>Yes</td><td>Yes</td><td>Ordered collection</td></tr>
<tr><td>Object</td><td>No</td><td>Yes</td><td>Yes</td><td>Named fields</td></tr>
<tr><td>ProseBlock</td><td>No</td><td>No</td><td>Yes</td><td>Narrative text</td></tr>
<tr><td>Override</td><td>No</td><td>No</td><td>Yes</td><td>Template modification</td></tr>
</tbody></table>
</div>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> - Using values in character fields</li>
<li><a href="./16-other-declarations.html#templates">Templates</a> - Range values and override syntax</li>
<li><a href="./14-schedules.html">Schedules</a> - Time and duration in schedules</li>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> - Duration in decorators</li>
<li><a href="./17-expressions.html">Expressions</a> - Using values in conditions</li>
</ul>
<h2 id="related-concepts"><a class="header" href="#related-concepts">Related Concepts</a></h2>
<ul>
<li><strong>Immutability</strong>: All values are immutable at declaration time</li>
<li><strong>Type safety</strong>: Strong static typing with no implicit coercion</li>
<li><strong>Structural equivalence</strong>: Objects/lists compared by structure, not identity</li>
<li><strong>Prose as data</strong>: Narrative text is first-class data, not comments</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/17-expressions.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/19-validation.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/17-expressions.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../reference/19-validation.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

View File

@@ -0,0 +1,530 @@
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Validation Rules - Storybook Language Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="Comprehensive documentation for the Storybook narrative simulation language">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../favicon.svg">
<link rel="shortcut icon" href="../favicon.png">
<link rel="stylesheet" href="../css/variables.css">
<link rel="stylesheet" href="../css/general.css">
<link rel="stylesheet" href="../css/chrome.css">
<link rel="stylesheet" href="../css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../highlight.css">
<link rel="stylesheet" href="../tomorrow-night.css">
<link rel="stylesheet" href="../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Start loading toc.js asap -->
<script src="../toc.js"></script>
</head>
<body>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="../toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Storybook Language Guide</h1>
<div class="right-buttons">
<a href="../print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/r3t-studios/storybook" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="validation-rules"><a class="header" href="#validation-rules">Validation Rules</a></h1>
<p>The Storybook compiler performs multi-layered validation to catch errors before runtime. This chapter documents all validation rules, organized by declaration type, along with the error messages you can expect and how to fix them.</p>
<h2 id="validation-layers"><a class="header" href="#validation-layers">Validation Layers</a></h2>
<p>Storybook validation happens in four stages:</p>
<ol>
<li><strong>Lexical</strong>: Tokenization of raw text (invalid characters, malformed literals)</li>
<li><strong>Syntactic</strong>: Grammar structure (missing braces, wrong keyword order)</li>
<li><strong>Semantic</strong>: Cross-reference resolution, type checking, field merging</li>
<li><strong>Domain</strong>: Narrative-specific constraints (bond ranges, schedule overlaps)</li>
</ol>
<p>Errors at earlier stages prevent later stages from running.</p>
<h2 id="character-validation"><a class="header" href="#character-validation">Character Validation</a></h2>
<h3 id="required-rules"><a class="header" href="#required-rules">Required Rules</a></h3>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Unique name</td><td>Character names must be unique within their module</td><td>Error</td></tr>
<tr><td>Species exists</td><td>If <code>: Species</code> is used, the species must be defined</td><td>Error</td></tr>
<tr><td>Templates exist</td><td>All templates in <code>from</code> clause must be defined</td><td>Error</td></tr>
<tr><td>No circular inheritance</td><td>Template chains cannot form cycles</td><td>Error</td></tr>
<tr><td>Field type consistency</td><td>Field values must match expected types</td><td>Error</td></tr>
<tr><td>Behavior trees exist</td><td>All <code>uses behaviors</code> references must resolve</td><td>Error</td></tr>
<tr><td>Schedules exist</td><td>All <code>uses schedule</code> references must resolve</td><td>Error</td></tr>
<tr><td>Prose tag uniqueness</td><td>Each prose tag can appear at most once per character</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples"><a class="header" href="#examples">Examples</a></h3>
<p><strong>Species not found:</strong></p>
<pre><code class="language-storybook">character Martha: Hobbit { // Error: species 'Hobbit' not defined
age: 34
}
</code></pre>
<p><strong>Fix:</strong> Define the species or correct the reference:</p>
<pre><code class="language-storybook">species Hobbit {
lifespan: 130
}
character Martha: Hobbit {
age: 34
}
</code></pre>
<p><strong>Duplicate character name:</strong></p>
<pre><code class="language-storybook">character Martha { age: 34 }
character Martha { age: 36 } // Error: duplicate character name 'Martha'
</code></pre>
<h2 id="template-validation"><a class="header" href="#template-validation">Template Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Unique name</td><td>Template names must be unique within their module</td><td>Error</td></tr>
<tr><td>Includes exist</td><td>All included templates must be defined</td><td>Error</td></tr>
<tr><td>No circular includes</td><td>Include chains cannot form cycles</td><td>Error</td></tr>
<tr><td>Range validity</td><td>Range bounds must satisfy min &lt;= max</td><td>Error</td></tr>
<tr><td>Range type match</td><td>Both bounds of a range must be the same type</td><td>Error</td></tr>
<tr><td>Strict enforcement</td><td>Characters using strict templates cannot add extra fields</td><td>Error</td></tr>
<tr><td>Resource links valid</td><td>Behavior/schedule references must resolve</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-1"><a class="header" href="#examples-1">Examples</a></h3>
<p><strong>Invalid range:</strong></p>
<pre><code class="language-storybook">template BadRange {
age: 65..18 // Error: range min (65) must be &lt;= max (18)
}
</code></pre>
<p><strong>Strict template violation:</strong></p>
<pre><code class="language-storybook">template Rigid strict {
required_stat: 10
}
character Constrained from Rigid {
required_stat: 15
extra_field: 42 // Error: field 'extra_field' not allowed by strict template 'Rigid'
}
</code></pre>
<h2 id="behavior-tree-validation"><a class="header" href="#behavior-tree-validation">Behavior Tree Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>At least one node</td><td>Behavior body must contain at least one node</td><td>Error</td></tr>
<tr><td>Composite children</td><td><code>choose</code> and <code>then</code> require at least one child</td><td>Error</td></tr>
<tr><td>Decorator child</td><td>Decorators require exactly one child</td><td>Error</td></tr>
<tr><td>Subtree exists</td><td><code>include</code> must reference a defined behavior</td><td>Error</td></tr>
<tr><td>Expression validity</td><td>Condition expressions must be well-formed</td><td>Error</td></tr>
<tr><td>Duration format</td><td>Decorator durations must be valid (e.g., <code>5s</code>, <code>10m</code>)</td><td>Error</td></tr>
<tr><td>Repeat count valid</td><td><code>repeat N</code> requires N &gt;= 0</td><td>Error</td></tr>
<tr><td>Repeat range valid</td><td><code>repeat min..max</code> requires 0 &lt;= min &lt;= max</td><td>Error</td></tr>
<tr><td>Retry count valid</td><td><code>retry N</code> requires N &gt;= 1</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-2"><a class="header" href="#examples-2">Examples</a></h3>
<p><strong>Empty composite:</strong></p>
<pre><code class="language-storybook">behavior Empty {
choose options {
// Error: 'choose' requires at least one child
}
}
</code></pre>
<p><strong>Invalid subtree reference:</strong></p>
<pre><code class="language-storybook">behavior Main {
include NonExistentBehavior // Error: behavior 'NonExistentBehavior' not defined
}
</code></pre>
<h2 id="life-arc-validation"><a class="header" href="#life-arc-validation">Life Arc Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>At least one state</td><td>Life arc must contain at least one state</td><td>Error</td></tr>
<tr><td>Unique state names</td><td>State names must be unique within the life arc</td><td>Error</td></tr>
<tr><td>Valid transitions</td><td>Transition targets must reference defined states</td><td>Error</td></tr>
<tr><td>Expression validity</td><td>Transition conditions must be well-formed</td><td>Error</td></tr>
<tr><td>Field targets valid</td><td>On-enter field references must resolve</td><td>Error</td></tr>
<tr><td>Reachable states</td><td>All states should be reachable from initial state</td><td>Warning</td></tr>
</tbody></table>
</div>
<h3 id="examples-3"><a class="header" href="#examples-3">Examples</a></h3>
<p><strong>Invalid transition target:</strong></p>
<pre><code class="language-storybook">life_arc Broken {
state active {
on timer_expired -&gt; nonexistent // Error: state 'nonexistent' not defined
}
}
</code></pre>
<p><strong>Unreachable state (warning):</strong></p>
<pre><code class="language-storybook">life_arc HasOrphan {
state start {
on ready -&gt; middle
}
state middle {
on done -&gt; end
}
state orphan {} // Warning: state 'orphan' is not reachable
state end {}
}
</code></pre>
<h2 id="schedule-validation"><a class="header" href="#schedule-validation">Schedule Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Time format</td><td>Times must be valid HH:MM or HH:MM:SS</td><td>Error</td></tr>
<tr><td>Extends exists</td><td>Base schedule must be defined</td><td>Error</td></tr>
<tr><td>No circular extends</td><td>Schedule chains cannot form cycles</td><td>Error</td></tr>
<tr><td>Named blocks unique</td><td>Block names must be unique within a schedule</td><td>Error</td></tr>
<tr><td>Action references valid</td><td>Action references must resolve to defined behaviors</td><td>Error</td></tr>
<tr><td>Constraint values valid</td><td>Temporal constraint values must reference defined enums</td><td>Error</td></tr>
<tr><td>Recurrence names unique</td><td>Recurrence names must be unique within a schedule</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-4"><a class="header" href="#examples-4">Examples</a></h3>
<p><strong>Invalid time format:</strong></p>
<pre><code class="language-storybook">schedule Bad {
block work {
25:00 - 17:00 // Error: invalid hour '25'
action: work
}
}
</code></pre>
<p><strong>Circular extends:</strong></p>
<pre><code class="language-storybook">schedule A extends B { }
schedule B extends A { } // Error: circular schedule extension detected
</code></pre>
<h2 id="relationship-validation"><a class="header" href="#relationship-validation">Relationship Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>At least two participants</td><td>Relationships require &gt;= 2 participants</td><td>Error</td></tr>
<tr><td>Participants exist</td><td>All participant names must reference defined entities</td><td>Error</td></tr>
<tr><td>Unique participants</td><td>Each participant appears at most once</td><td>Error</td></tr>
<tr><td>Field type consistency</td><td>Fields must have valid value types</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-5"><a class="header" href="#examples-5">Examples</a></h3>
<p><strong>Too few participants:</strong></p>
<pre><code class="language-storybook">relationship Lonely {
Martha // Error: relationship requires at least 2 participants
bond: 0.5
}
</code></pre>
<h2 id="species-validation"><a class="header" href="#species-validation">Species Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Unique name</td><td>Species names must be unique within their module</td><td>Error</td></tr>
<tr><td>No circular includes</td><td>Include chains cannot form cycles</td><td>Error</td></tr>
<tr><td>Includes exist</td><td>All included species must be defined</td><td>Error</td></tr>
<tr><td>Field type consistency</td><td>Fields must have valid values</td><td>Error</td></tr>
<tr><td>Prose tag uniqueness</td><td>Each prose tag can appear at most once</td><td>Error</td></tr>
</tbody></table>
</div>
<h2 id="enum-validation"><a class="header" href="#enum-validation">Enum Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Unique enum name</td><td>Enum names must be unique within their module</td><td>Error</td></tr>
<tr><td>Unique variants</td><td>Variant names must be unique within the enum</td><td>Error</td></tr>
<tr><td>Non-empty</td><td>Enums must have at least one variant</td><td>Error</td></tr>
<tr><td>Valid identifiers</td><td>Variants must follow identifier rules</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-6"><a class="header" href="#examples-6">Examples</a></h3>
<p><strong>Duplicate variant:</strong></p>
<pre><code class="language-storybook">enum Size {
tiny,
small,
small, // Error: duplicate variant 'small' in enum 'Size'
large
}
</code></pre>
<h2 id="institution-and-location-validation"><a class="header" href="#institution-and-location-validation">Institution and Location Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Unique name</td><td>Names must be unique within their module</td><td>Error</td></tr>
<tr><td>Resource links valid</td><td>Behavior/schedule references must resolve</td><td>Error</td></tr>
<tr><td>Field type consistency</td><td>Fields must have valid values</td><td>Error</td></tr>
</tbody></table>
</div>
<h2 id="expression-validation"><a class="header" href="#expression-validation">Expression Validation</a></h2>
<p>Expressions are validated wherever they appear (life arc transitions, behavior tree conditions, if decorators).</p>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Type consistency</td><td>Both sides of comparison must have compatible types</td><td>Error</td></tr>
<tr><td>Boolean context</td><td>Logical operators require boolean operands</td><td>Error</td></tr>
<tr><td>Field existence</td><td>Referenced fields must exist on the entity</td><td>Error</td></tr>
<tr><td>Collection validity</td><td>Quantifiers require collection-typed expressions</td><td>Error</td></tr>
<tr><td>Variable scope</td><td>Quantifier variables only valid within their predicate</td><td>Error</td></tr>
<tr><td>Enum validity</td><td>Enum comparisons must reference defined values</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-7"><a class="header" href="#examples-7">Examples</a></h3>
<p><strong>Type mismatch:</strong></p>
<pre><code class="language-storybook">life_arc TypeError {
state checking {
on count == "five" -&gt; done // Error: cannot compare int with string
}
state done {}
}
</code></pre>
<h2 id="use-statement-validation"><a class="header" href="#use-statement-validation">Use Statement Validation</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>Path exists</td><td>Imported paths must reference defined modules/items</td><td>Error</td></tr>
<tr><td>No circular imports</td><td>Modules cannot form circular dependency chains</td><td>Error</td></tr>
<tr><td>Valid identifiers</td><td>All path segments must be valid identifiers</td><td>Error</td></tr>
<tr><td>Grouped import validity</td><td>All items in <code>{}</code> must exist in the target module</td><td>Error</td></tr>
</tbody></table>
</div>
<h3 id="examples-8"><a class="header" href="#examples-8">Examples</a></h3>
<p><strong>Missing import:</strong></p>
<pre><code class="language-storybook">use schema::nonexistent::Thing; // Error: module 'schema::nonexistent' not found
</code></pre>
<h2 id="cross-file-validation"><a class="header" href="#cross-file-validation">Cross-File Validation</a></h2>
<p>When resolving across multiple <code>.sb</code> files, the compiler performs additional checks:</p>
<div class="table-wrapper"><table><thead><tr><th>Rule</th><th>Description</th><th>Severity</th></tr></thead><tbody>
<tr><td>All references resolve</td><td>Cross-file references must find their targets</td><td>Error</td></tr>
<tr><td>No naming conflicts</td><td>Declarations must not collide across files in the same module</td><td>Error</td></tr>
<tr><td>Import visibility</td><td>Only public declarations can be imported</td><td>Error</td></tr>
</tbody></table>
</div>
<h2 id="common-error-patterns"><a class="header" href="#common-error-patterns">Common Error Patterns</a></h2>
<h3 id="missing-definitions"><a class="header" href="#missing-definitions">Missing Definitions</a></h3>
<p>The most common error is referencing something that does not exist:</p>
<pre><code class="language-storybook">character Martha: Human from Baker {
specialty: sourdough
}
</code></pre>
<p>If <code>Human</code>, <code>Baker</code>, or the <code>sourdough</code> enum variant are not defined or imported, the compiler will report an error. Fix by adding the appropriate <code>use</code> statements:</p>
<pre><code class="language-storybook">use schema::core_enums::{SkillLevel, Specialty};
use schema::templates::Baker;
use schema::beings::Human;
character Martha: Human from Baker {
specialty: sourdough
}
</code></pre>
<h3 id="circular-dependencies"><a class="header" href="#circular-dependencies">Circular Dependencies</a></h3>
<p>Circular references are rejected at every level:</p>
<ul>
<li>Templates including each other</li>
<li>Species including each other</li>
<li>Schedules extending each other</li>
<li>Modules importing each other</li>
</ul>
<p>Break cycles by restructuring into a hierarchy or extracting shared parts into a common module.</p>
<h3 id="type-mismatches"><a class="header" href="#type-mismatches">Type Mismatches</a></h3>
<p>Storybook has no implicit type coercion. Ensure values match their expected types:</p>
<pre><code class="language-storybook">// Wrong:
character Bad {
age: "twenty" // Error: expected int, got string
is_ready: 1 // Error: expected bool, got int
}
// Correct:
character Good {
age: 20
is_ready: true
}
</code></pre>
<h2 id="validation-summary"><a class="header" href="#validation-summary">Validation Summary</a></h2>
<div class="table-wrapper"><table><thead><tr><th>Declaration</th><th>Key Constraints</th></tr></thead><tbody>
<tr><td>Character</td><td>Unique name, valid species/templates, no circular inheritance</td></tr>
<tr><td>Template</td><td>Unique name, valid includes, valid ranges, strict enforcement</td></tr>
<tr><td>Behavior</td><td>Non-empty, valid composites, valid decorators, valid subtrees</td></tr>
<tr><td>Life Arc</td><td>Non-empty, unique states, valid transitions, reachable states</td></tr>
<tr><td>Schedule</td><td>Valid times, valid extends chain, unique block names</td></tr>
<tr><td>Relationship</td><td>&gt;= 2 participants, valid references</td></tr>
<tr><td>Species</td><td>Unique name, valid includes, no cycles</td></tr>
<tr><td>Enum</td><td>Unique name, unique variants, non-empty</td></tr>
<tr><td>Institution</td><td>Unique name, valid resource links</td></tr>
<tr><td>Location</td><td>Unique name, valid field types</td></tr>
<tr><td>Use</td><td>Valid paths, no circular imports</td></tr>
</tbody></table>
</div>
<h2 id="cross-references"><a class="header" href="#cross-references">Cross-References</a></h2>
<ul>
<li><a href="./10-characters.html">Characters</a> - Character-specific validation</li>
<li><a href="./11-behavior-trees.html">Behavior Trees</a> - Behavior validation</li>
<li><a href="./13-life-arcs.html">Life Arcs</a> - Life arc validation</li>
<li><a href="./14-schedules.html">Schedules</a> - Schedule validation</li>
<li><a href="./17-expressions.html">Expression Language</a> - Expression validation</li>
<li><a href="./18-value-types.html">Value Types</a> - Type system constraints</li>
</ul>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../reference/18-value-types.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/20-patterns.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../reference/18-value-types.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../advanced/20-patterns.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<!-- Livereload script (if served using the cli tool) -->
<script>
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
const socket = new WebSocket(wsAddress);
socket.onmessage = function (event) {
if (event.data === "reload") {
socket.close();
location.reload();
}
};
window.onbeforeunload = function() {
socket.close();
}
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../elasticlunr.min.js"></script>
<script src="../mark.min.js"></script>
<script src="../searcher.js"></script>
<script src="../clipboard.min.js"></script>
<script src="../highlight.js"></script>
<script src="../book.js"></script>
<!-- Custom JS scripts -->
</div>
</body>
</html>

483
docs/book/searcher.js Normal file
View File

@@ -0,0 +1,483 @@
"use strict";
window.search = window.search || {};
(function search(search) {
// Search functionality
//
// You can use !hasFocus() to prevent keyhandling in your key
// event handlers while the user is typing their search.
if (!Mark || !elasticlunr) {
return;
}
//IE 11 Compatibility from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(search, pos) {
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
};
}
var search_wrap = document.getElementById('search-wrapper'),
searchbar = document.getElementById('searchbar'),
searchbar_outer = document.getElementById('searchbar-outer'),
searchresults = document.getElementById('searchresults'),
searchresults_outer = document.getElementById('searchresults-outer'),
searchresults_header = document.getElementById('searchresults-header'),
searchicon = document.getElementById('search-toggle'),
content = document.getElementById('content'),
searchindex = null,
doc_urls = [],
results_options = {
teaser_word_count: 30,
limit_results: 30,
},
search_options = {
bool: "AND",
expand: true,
fields: {
title: {boost: 1},
body: {boost: 1},
breadcrumbs: {boost: 0}
}
},
mark_exclude = [],
marker = new Mark(content),
current_searchterm = "",
URL_SEARCH_PARAM = 'search',
URL_MARK_PARAM = 'highlight',
teaser_count = 0,
SEARCH_HOTKEY_KEYCODE = 83,
ESCAPE_KEYCODE = 27,
DOWN_KEYCODE = 40,
UP_KEYCODE = 38,
SELECT_KEYCODE = 13;
function hasFocus() {
return searchbar === document.activeElement;
}
function removeChildren(elem) {
while (elem.firstChild) {
elem.removeChild(elem.firstChild);
}
}
// Helper to parse a url into its building blocks.
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
params: (function(){
var ret = {};
var seg = a.search.replace(/^\?/,'').split('&');
var len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^/])/,'/$1')
};
}
// Helper to recreate a url string from its building blocks.
function renderURL(urlobject) {
var url = urlobject.protocol + "://" + urlobject.host;
if (urlobject.port != "") {
url += ":" + urlobject.port;
}
url += urlobject.path;
var joiner = "?";
for(var prop in urlobject.params) {
if(urlobject.params.hasOwnProperty(prop)) {
url += joiner + prop + "=" + urlobject.params[prop];
joiner = "&";
}
}
if (urlobject.hash != "") {
url += "#" + urlobject.hash;
}
return url;
}
// Helper to escape html special chars for displaying the teasers
var escapeHTML = (function() {
var MAP = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&#34;',
"'": '&#39;'
};
var repl = function(c) { return MAP[c]; };
return function(s) {
return s.replace(/[&<>'"]/g, repl);
};
})();
function formatSearchMetric(count, searchterm) {
if (count == 1) {
return count + " search result for '" + searchterm + "':";
} else if (count == 0) {
return "No search results for '" + searchterm + "'.";
} else {
return count + " search results for '" + searchterm + "':";
}
}
function formatSearchResult(result, searchterms) {
var teaser = makeTeaser(escapeHTML(result.doc.body), searchterms);
teaser_count++;
// The ?URL_MARK_PARAM= parameter belongs inbetween the page and the #heading-anchor
var url = doc_urls[result.ref].split("#");
if (url.length == 1) { // no anchor found
url.push("");
}
// encodeURIComponent escapes all chars that could allow an XSS except
// for '. Due to that we also manually replace ' with its url-encoded
// representation (%27).
var searchterms = encodeURIComponent(searchterms.join(" ")).replace(/\'/g, "%27");
return '<a href="' + path_to_root + url[0] + '?' + URL_MARK_PARAM + '=' + searchterms + '#' + url[1]
+ '" aria-details="teaser_' + teaser_count + '">' + result.doc.breadcrumbs + '</a>'
+ '<span class="teaser" id="teaser_' + teaser_count + '" aria-label="Search Result Teaser">'
+ teaser + '</span>';
}
function makeTeaser(body, searchterms) {
// The strategy is as follows:
// First, assign a value to each word in the document:
// Words that correspond to search terms (stemmer aware): 40
// Normal words: 2
// First word in a sentence: 8
// Then use a sliding window with a constant number of words and count the
// sum of the values of the words within the window. Then use the window that got the
// maximum sum. If there are multiple maximas, then get the last one.
// Enclose the terms in <em>.
var stemmed_searchterms = searchterms.map(function(w) {
return elasticlunr.stemmer(w.toLowerCase());
});
var searchterm_weight = 40;
var weighted = []; // contains elements of ["word", weight, index_in_document]
// split in sentences, then words
var sentences = body.toLowerCase().split('. ');
var index = 0;
var value = 0;
var searchterm_found = false;
for (var sentenceindex in sentences) {
var words = sentences[sentenceindex].split(' ');
value = 8;
for (var wordindex in words) {
var word = words[wordindex];
if (word.length > 0) {
for (var searchtermindex in stemmed_searchterms) {
if (elasticlunr.stemmer(word).startsWith(stemmed_searchterms[searchtermindex])) {
value = searchterm_weight;
searchterm_found = true;
}
};
weighted.push([word, value, index]);
value = 2;
}
index += word.length;
index += 1; // ' ' or '.' if last word in sentence
};
index += 1; // because we split at a two-char boundary '. '
};
if (weighted.length == 0) {
return body;
}
var window_weight = [];
var window_size = Math.min(weighted.length, results_options.teaser_word_count);
var cur_sum = 0;
for (var wordindex = 0; wordindex < window_size; wordindex++) {
cur_sum += weighted[wordindex][1];
};
window_weight.push(cur_sum);
for (var wordindex = 0; wordindex < weighted.length - window_size; wordindex++) {
cur_sum -= weighted[wordindex][1];
cur_sum += weighted[wordindex + window_size][1];
window_weight.push(cur_sum);
};
if (searchterm_found) {
var max_sum = 0;
var max_sum_window_index = 0;
// backwards
for (var i = window_weight.length - 1; i >= 0; i--) {
if (window_weight[i] > max_sum) {
max_sum = window_weight[i];
max_sum_window_index = i;
}
};
} else {
max_sum_window_index = 0;
}
// add <em/> around searchterms
var teaser_split = [];
var index = weighted[max_sum_window_index][2];
for (var i = max_sum_window_index; i < max_sum_window_index+window_size; i++) {
var word = weighted[i];
if (index < word[2]) {
// missing text from index to start of `word`
teaser_split.push(body.substring(index, word[2]));
index = word[2];
}
if (word[1] == searchterm_weight) {
teaser_split.push("<em>")
}
index = word[2] + word[0].length;
teaser_split.push(body.substring(word[2], index));
if (word[1] == searchterm_weight) {
teaser_split.push("</em>")
}
};
return teaser_split.join('');
}
function init(config) {
results_options = config.results_options;
search_options = config.search_options;
searchbar_outer = config.searchbar_outer;
doc_urls = config.doc_urls;
searchindex = elasticlunr.Index.load(config.index);
// Set up events
searchicon.addEventListener('click', function(e) { searchIconClickHandler(); }, false);
searchbar.addEventListener('keyup', function(e) { searchbarKeyUpHandler(); }, false);
document.addEventListener('keydown', function(e) { globalKeyHandler(e); }, false);
// If the user uses the browser buttons, do the same as if a reload happened
window.onpopstate = function(e) { doSearchOrMarkFromUrl(); };
// Suppress "submit" events so the page doesn't reload when the user presses Enter
document.addEventListener('submit', function(e) { e.preventDefault(); }, false);
// If reloaded, do the search or mark again, depending on the current url parameters
doSearchOrMarkFromUrl();
}
function unfocusSearchbar() {
// hacky, but just focusing a div only works once
var tmp = document.createElement('input');
tmp.setAttribute('style', 'position: absolute; opacity: 0;');
searchicon.appendChild(tmp);
tmp.focus();
tmp.remove();
}
// On reload or browser history backwards/forwards events, parse the url and do search or mark
function doSearchOrMarkFromUrl() {
// Check current URL for search request
var url = parseURL(window.location.href);
if (url.params.hasOwnProperty(URL_SEARCH_PARAM)
&& url.params[URL_SEARCH_PARAM] != "") {
showSearch(true);
searchbar.value = decodeURIComponent(
(url.params[URL_SEARCH_PARAM]+'').replace(/\+/g, '%20'));
searchbarKeyUpHandler(); // -> doSearch()
} else {
showSearch(false);
}
if (url.params.hasOwnProperty(URL_MARK_PARAM)) {
var words = decodeURIComponent(url.params[URL_MARK_PARAM]).split(' ');
marker.mark(words, {
exclude: mark_exclude
});
var markers = document.querySelectorAll("mark");
function hide() {
for (var i = 0; i < markers.length; i++) {
markers[i].classList.add("fade-out");
window.setTimeout(function(e) { marker.unmark(); }, 300);
}
}
for (var i = 0; i < markers.length; i++) {
markers[i].addEventListener('click', hide);
}
}
}
// Eventhandler for keyevents on `document`
function globalKeyHandler(e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text' || !hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName)) { return; }
if (e.keyCode === ESCAPE_KEYCODE) {
e.preventDefault();
searchbar.classList.remove("active");
setSearchUrlParameters("",
(searchbar.value.trim() !== "") ? "push" : "replace");
if (hasFocus()) {
unfocusSearchbar();
}
showSearch(false);
marker.unmark();
} else if (!hasFocus() && e.keyCode === SEARCH_HOTKEY_KEYCODE) {
e.preventDefault();
showSearch(true);
window.scrollTo(0, 0);
searchbar.select();
} else if (hasFocus() && e.keyCode === DOWN_KEYCODE) {
e.preventDefault();
unfocusSearchbar();
searchresults.firstElementChild.classList.add("focus");
} else if (!hasFocus() && (e.keyCode === DOWN_KEYCODE
|| e.keyCode === UP_KEYCODE
|| e.keyCode === SELECT_KEYCODE)) {
// not `:focus` because browser does annoying scrolling
var focused = searchresults.querySelector("li.focus");
if (!focused) return;
e.preventDefault();
if (e.keyCode === DOWN_KEYCODE) {
var next = focused.nextElementSibling;
if (next) {
focused.classList.remove("focus");
next.classList.add("focus");
}
} else if (e.keyCode === UP_KEYCODE) {
focused.classList.remove("focus");
var prev = focused.previousElementSibling;
if (prev) {
prev.classList.add("focus");
} else {
searchbar.select();
}
} else { // SELECT_KEYCODE
window.location.assign(focused.querySelector('a'));
}
}
}
function showSearch(yes) {
if (yes) {
search_wrap.classList.remove('hidden');
searchicon.setAttribute('aria-expanded', 'true');
} else {
search_wrap.classList.add('hidden');
searchicon.setAttribute('aria-expanded', 'false');
var results = searchresults.children;
for (var i = 0; i < results.length; i++) {
results[i].classList.remove("focus");
}
}
}
function showResults(yes) {
if (yes) {
searchresults_outer.classList.remove('hidden');
} else {
searchresults_outer.classList.add('hidden');
}
}
// Eventhandler for search icon
function searchIconClickHandler() {
if (search_wrap.classList.contains('hidden')) {
showSearch(true);
window.scrollTo(0, 0);
searchbar.select();
} else {
showSearch(false);
}
}
// Eventhandler for keyevents while the searchbar is focused
function searchbarKeyUpHandler() {
var searchterm = searchbar.value.trim();
if (searchterm != "") {
searchbar.classList.add("active");
doSearch(searchterm);
} else {
searchbar.classList.remove("active");
showResults(false);
removeChildren(searchresults);
}
setSearchUrlParameters(searchterm, "push_if_new_search_else_replace");
// Remove marks
marker.unmark();
}
// Update current url with ?URL_SEARCH_PARAM= parameter, remove ?URL_MARK_PARAM and #heading-anchor .
// `action` can be one of "push", "replace", "push_if_new_search_else_replace"
// and replaces or pushes a new browser history item.
// "push_if_new_search_else_replace" pushes if there is no `?URL_SEARCH_PARAM=abc` yet.
function setSearchUrlParameters(searchterm, action) {
var url = parseURL(window.location.href);
var first_search = ! url.params.hasOwnProperty(URL_SEARCH_PARAM);
if (searchterm != "" || action == "push_if_new_search_else_replace") {
url.params[URL_SEARCH_PARAM] = searchterm;
delete url.params[URL_MARK_PARAM];
url.hash = "";
} else {
delete url.params[URL_MARK_PARAM];
delete url.params[URL_SEARCH_PARAM];
}
// A new search will also add a new history item, so the user can go back
// to the page prior to searching. A updated search term will only replace
// the url.
if (action == "push" || (action == "push_if_new_search_else_replace" && first_search) ) {
history.pushState({}, document.title, renderURL(url));
} else if (action == "replace" || (action == "push_if_new_search_else_replace" && !first_search) ) {
history.replaceState({}, document.title, renderURL(url));
}
}
function doSearch(searchterm) {
// Don't search the same twice
if (current_searchterm == searchterm) { return; }
else { current_searchterm = searchterm; }
if (searchindex == null) { return; }
// Do the actual search
var results = searchindex.search(searchterm, search_options);
var resultcount = Math.min(results.length, results_options.limit_results);
// Display search metrics
searchresults_header.innerText = formatSearchMetric(resultcount, searchterm);
// Clear and insert results
var searchterms = searchterm.split(' ');
removeChildren(searchresults);
for(var i = 0; i < resultcount ; i++){
var resultElem = document.createElement('li');
resultElem.innerHTML = formatSearchResult(results[i], searchterms);
searchresults.appendChild(resultElem);
}
// Display results
showResults(true);
}
fetch(path_to_root + 'searchindex.json')
.then(response => response.json())
.then(json => init(json))
.catch(error => { // Try to load searchindex.js if fetch failed
var script = document.createElement('script');
script.src = path_to_root + 'searchindex.js';
script.onload = () => init(window.search);
document.head.appendChild(script);
});
// Exported functions
search.hasFocus = hasFocus;
})(window.search);

1
docs/book/searchindex.js Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

32
docs/book/toc.html Normal file

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More