plotline 0.3.0

Authored sequences, gates, and unlock trees as plain data for any game engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//! Sequences of steps, held as data.

use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use core::borrow::Borrow;
use core::fmt::{Display, Formatter, Result as FmtResult};

use crate::rules::Requirement;

/// The name of a sequence in a [`Library`].
///
/// A step names its target, so a data file can refer to a sequence before
/// it defines it. [`Library::validate`] reports a name that no sequence has.
///
/// ```
/// use plotline::SequenceRef;
///
/// let hub = SequenceRef::from("hub");
/// assert_eq!(hub.as_str(), "hub");
/// assert_eq!(hub.to_string(), "hub");
/// ```
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct SequenceRef(String);

impl SequenceRef {
    /// Returns the name.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&str> for SequenceRef {
    fn from(name: &str) -> Self {
        Self(name.to_owned())
    }
}

impl From<String> for SequenceRef {
    fn from(name: String) -> Self {
        Self(name)
    }
}

impl Borrow<str> for SequenceRef {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl Display for SequenceRef {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str(&self.0)
    }
}

/// One step of a sequence.
///
/// `A` is the host's action type: a line of dialog, a camera shake, a grant.
/// The runner never interprets an action. It hands each one to the host and
/// waits for the host's [`Answer`](crate::Answer). `K` is the capability key
/// type that the rules read through [`Has`](crate::Has).
///
/// Every other variant is control flow, and the runner handles it without
/// the host.
///
/// ```
/// use plotline::{Requirement, Step};
///
/// enum Action {
///     Say(&'static str),
/// }
///
/// #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
/// enum Key {
///     HasRing,
/// }
///
/// let steps: Vec<Step<Action, Key>> = vec![
///     Step::act(Action::Say("Have you found my ring?")),
///     Step::branch(Requirement::has(Key::HasRing), "thanks", "come-back-later"),
/// ];
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub enum Step<A, K> {
    /// Hands an action to the host and waits for its answer.
    Act(A),
    /// Runs the inner step when the rule holds, and skips it otherwise.
    When {
        /// The rule that decides.
        rule: Requirement<K>,
        /// The step to run.
        step: Box<Step<A, K>>,
    },
    /// Starts one of two sequences, chosen by the rule, as [`Step::Goto`]
    /// does. A `None` target ends the chain.
    Branch {
        /// The rule that decides.
        rule: Requirement<K>,
        /// The target when the rule holds.
        if_true: Option<SequenceRef>,
        /// The target when the rule fails.
        if_false: Option<SequenceRef>,
    },
    /// Runs a sequence, then continues after this step.
    Call(SequenceRef),
    /// Clears the call stack and starts a sequence. `None` ends the chain.
    Goto(Option<SequenceRef>),
    /// Leaves the current sequence and continues after the step that called
    /// it. At the root of the chain, it ends the chain.
    Return,
}

impl<A, K> Step<A, K> {
    /// Creates a step that hands `action` to the host.
    #[must_use]
    pub fn act(action: A) -> Self {
        Self::Act(action)
    }

    /// Creates a step that runs `step` only when `rule` holds.
    #[must_use]
    pub fn when(rule: Requirement<K>, step: Self) -> Self {
        Self::When {
            rule,
            step: Box::new(step),
        }
    }

    /// Creates a step that starts `if_true` when `rule` holds, and `if_false`
    /// otherwise. Use the variant directly for a target that ends the chain.
    #[must_use]
    pub fn branch(
        rule: Requirement<K>,
        if_true: impl Into<SequenceRef>,
        if_false: impl Into<SequenceRef>,
    ) -> Self {
        Self::Branch {
            rule,
            if_true: Some(if_true.into()),
            if_false: Some(if_false.into()),
        }
    }

    /// Creates a step that runs `sequence`, then continues.
    #[must_use]
    pub fn call(sequence: impl Into<SequenceRef>) -> Self {
        Self::Call(sequence.into())
    }

    /// Creates a step that clears the call stack and starts `sequence`.
    #[must_use]
    pub fn goto(sequence: impl Into<SequenceRef>) -> Self {
        Self::Goto(Some(sequence.into()))
    }

    /// Creates a step that ends the chain.
    #[must_use]
    pub const fn stop() -> Self {
        Self::Goto(None)
    }
}

/// Named sequences of steps.
///
/// A library is plain data. With the `serde` feature it loads from a file as
/// a map from each sequence name to its list of steps. Loading fails when a
/// name appears twice, or when a step holds a field it does not know.
///
/// ```
/// use plotline::{Library, Step};
///
/// let mut library: Library<&str, ()> = Library::new();
/// library.insert("greeting", [Step::act("Hello."), Step::call("farewell")]);
/// library.insert("farewell", [Step::act("Safe roads.")]);
///
/// assert_eq!(library.get("greeting").map(<[_]>::len), Some(2));
/// assert!(library.validate(|_| Vec::new()).is_empty());
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[cfg_attr(
    feature = "serde",
    serde(bound(deserialize = "A: serde::Deserialize<'de>, K: serde::Deserialize<'de>"))
)]
pub struct Library<A, K> {
    #[cfg_attr(
        feature = "serde",
        serde(deserialize_with = "crate::unique_map::deserialize")
    )]
    sequences: BTreeMap<SequenceRef, Vec<Step<A, K>>>,
}

impl<A, K> Default for Library<A, K> {
    fn default() -> Self {
        Self::new()
    }
}

impl<A, K> Library<A, K> {
    /// Creates an empty library.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            sequences: BTreeMap::new(),
        }
    }

    /// Adds a sequence and returns the steps it replaced.
    pub fn insert(
        &mut self,
        name: impl Into<SequenceRef>,
        steps: impl IntoIterator<Item = Step<A, K>>,
    ) -> Option<Vec<Step<A, K>>> {
        self.sequences
            .insert(name.into(), steps.into_iter().collect())
    }

    /// Returns the steps of a sequence.
    #[must_use]
    pub fn get(&self, name: &str) -> Option<&[Step<A, K>]> {
        self.sequences.get(name).map(Vec::as_slice)
    }

    /// Returns the steps of a sequence for editing.
    pub fn get_mut(&mut self, name: &str) -> Option<&mut Vec<Step<A, K>>> {
        self.sequences.get_mut(name)
    }

    /// Iterates over the sequences in name order.
    pub fn iter(&self) -> impl Iterator<Item = (&SequenceRef, &[Step<A, K>])> {
        self.sequences
            .iter()
            .map(|(name, steps)| (name, steps.as_slice()))
    }

    /// Returns the number of sequences.
    #[must_use]
    pub fn len(&self) -> usize {
        self.sequences.len()
    }

    /// Returns whether the library holds no sequences.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.sequences.is_empty()
    }

    /// Reports authoring problems across the library.
    ///
    /// `refs_in` lists the sequences an action names, such as the targets of
    /// a dialog choice. The crate cannot see inside an action, so a host
    /// whose actions name no sequences passes `|_| Vec::new()`.
    ///
    /// Cycles are valid, so they are not reported.
    #[must_use]
    pub fn validate(&self, refs_in: impl Fn(&A) -> Vec<SequenceRef>) -> Vec<Warning> {
        let mut warnings = Vec::new();
        for (name, steps) in &self.sequences {
            let mut report = |index: usize, problem: Problem| {
                warnings.push(Warning {
                    sequence: name.clone(),
                    index,
                    problem,
                });
            };

            for (index, step) in steps.iter().enumerate() {
                self.check_step(step, &refs_in, &mut |problem| report(index, problem));
            }

            let first_exit = steps.iter().position(Self::always_leaves);
            let first_dead = first_exit.map(|index| index + 1);
            if let Some(dead) = first_dead.filter(|dead| *dead < steps.len()) {
                report(dead, Problem::Unreachable);
            }
        }
        warnings
    }

    fn check_step(
        &self,
        step: &Step<A, K>,
        refs_in: &impl Fn(&A) -> Vec<SequenceRef>,
        report: &mut impl FnMut(Problem),
    ) {
        match step {
            Step::Act(action) => {
                for target in refs_in(action) {
                    self.check_target(&target, report);
                }
            }
            Step::When { rule, step } => {
                if let Some(message) = rule.warning() {
                    report(Problem::Rule(message));
                }
                self.check_step(step, refs_in, report);
            }
            Step::Branch {
                rule,
                if_true,
                if_false,
            } => {
                if let Some(message) = rule.warning() {
                    report(Problem::Rule(message));
                }
                for target in if_true.iter().chain(if_false) {
                    self.check_target(target, report);
                }
            }
            Step::Call(target) | Step::Goto(Some(target)) => self.check_target(target, report),
            Step::Goto(None) | Step::Return => {}
        }
    }

    fn check_target(&self, target: &SequenceRef, report: &mut impl FnMut(Problem)) {
        if !self.sequences.contains_key(target) {
            report(Problem::MissingSequence(target.clone()));
        }
    }

    /// Returns whether a step always leaves its sequence, whatever the rules
    /// and the host answer.
    fn always_leaves(step: &Step<A, K>) -> bool {
        matches!(step, Step::Branch { .. } | Step::Goto(_) | Step::Return)
    }
}

/// One problem found by [`Library::validate`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Warning {
    /// The sequence that holds the problem.
    pub sequence: SequenceRef,
    /// The position of the step that holds the problem.
    pub index: usize,
    /// What is wrong.
    pub problem: Problem,
}

/// What [`Library::validate`] found wrong with a step.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Problem {
    /// The step names a sequence that the library does not hold.
    MissingSequence(SequenceRef),
    /// A rule in the step reports an authoring warning.
    Rule(String),
    /// The step never runs, because an earlier step always leaves the
    /// sequence. The warning marks the first such step. Every later step in
    /// the sequence never runs either.
    Unreachable,
}

impl Display for Warning {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}[{}]: ", self.sequence, self.index)?;
        match &self.problem {
            Problem::MissingSequence(target) => {
                write!(f, "names missing sequence '{target}'")
            }
            Problem::Rule(message) => write!(f, "rule: {message}"),
            Problem::Unreachable => {
                f.write_str("never runs, because an earlier step always leaves")
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;
    use alloc::vec;

    use super::*;

    #[derive(Clone, Debug, PartialEq, Eq)]
    enum Action {
        Say(&'static str),
        Choose(Vec<SequenceRef>),
    }

    #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
    enum Key {
        Ring,
    }

    type Script = Library<Action, Key>;

    fn refs_in(action: &Action) -> Vec<SequenceRef> {
        match action {
            Action::Choose(targets) => targets.clone(),
            Action::Say(_) => Vec::new(),
        }
    }

    fn problems(library: &Script) -> Vec<Warning> {
        let mut found = library.validate(refs_in);
        found.sort_by(|a, b| (&a.sequence, a.index).cmp(&(&b.sequence, b.index)));
        found
    }

    fn at(sequence: &str, index: usize, problem: Problem) -> Warning {
        Warning {
            sequence: sequence.into(),
            index,
            problem,
        }
    }

    fn missing(name: &str) -> Problem {
        Problem::MissingSequence(name.into())
    }

    #[test]
    fn a_library_stores_named_sequences() {
        let mut library = Script::new();
        assert!(
            library
                .insert("hub", [Step::act(Action::Say("Hello."))])
                .is_none()
        );
        let replaced = library.insert("hub", [Step::act(Action::Say("Welcome back."))]);

        assert_eq!(replaced, Some(vec![Step::act(Action::Say("Hello."))]));
        assert_eq!(library.len(), 1);
        assert_eq!(
            library.get("hub"),
            Some(&[Step::act(Action::Say("Welcome back."))][..])
        );
        assert!(library.get("absent").is_none());

        library.get_mut("hub").unwrap().push(Step::stop());
        assert_eq!(library.get("hub").map(<[_]>::len), Some(2));
    }

    #[test]
    fn a_sound_library_has_no_warnings() {
        let mut library = Script::new();
        library.insert(
            "hub",
            [
                Step::act(Action::Choose(vec!["yes".into(), "no".into()])),
                Step::when(Requirement::has(Key::Ring), Step::goto("yes")),
                Step::call("no"),
                Step::branch(Requirement::has(Key::Ring), "yes", "no"),
            ],
        );
        library.insert("yes", [Step::Return]);
        library.insert("no", [Step::goto("hub")]);
        assert_eq!(problems(&library), vec![]);
    }

    #[test]
    fn validation_finds_every_missing_target() {
        let mut library = Script::new();
        library.insert(
            "hub",
            [
                Step::act(Action::Choose(vec!["in-an-action".into()])),
                Step::call("called"),
                Step::when(Requirement::has(Key::Ring), Step::goto("nested")),
                Step::branch(Requirement::has(Key::Ring), "if-true", "if-false"),
            ],
        );

        assert_eq!(
            problems(&library),
            vec![
                at("hub", 0, missing("in-an-action")),
                at("hub", 1, missing("called")),
                at("hub", 2, missing("nested")),
                at("hub", 3, missing("if-true")),
                at("hub", 3, missing("if-false")),
            ]
        );
    }

    #[test]
    fn validation_reports_rule_traps() {
        let mut library = Script::new();
        library.insert(
            "hub",
            [
                Step::when(Requirement::any([]), Step::act(Action::Say("Never."))),
                Step::Branch {
                    rule: Requirement::at_least(2, [Requirement::has(Key::Ring)]),
                    if_true: None,
                    if_false: None,
                },
            ],
        );

        let found = problems(&library);
        assert_eq!(found.len(), 2);
        assert!(matches!(&found[0].problem, Problem::Rule(_)));
        assert_eq!(found[0].index, 0);
        assert!(
            matches!(&found[1].problem, Problem::Rule(message) if message.contains("never hold"))
        );
        assert_eq!(found[1].index, 1);
    }

    #[test]
    fn validation_reports_steps_after_an_unconditional_exit() {
        for exit in [
            Step::goto("hub"),
            Step::stop(),
            Step::Return,
            Step::branch(Requirement::has(Key::Ring), "hub", "hub"),
        ] {
            let mut library = Script::new();
            library.insert(
                "hub",
                [
                    exit.clone(),
                    Step::act(Action::Say("Dead.")),
                    Step::act(Action::Say("Also dead.")),
                ],
            );
            assert_eq!(
                problems(&library),
                vec![at("hub", 1, Problem::Unreachable)],
                "{exit:?}"
            );
        }
    }

    #[test]
    fn a_conditional_or_answered_exit_leaves_later_steps_reachable() {
        let mut library = Script::new();
        library.insert(
            "hub",
            [
                Step::when(Requirement::has(Key::Ring), Step::stop()),
                // The host may answer this with a jump, or with Done.
                Step::act(Action::Choose(vec!["hub".into()])),
                Step::act(Action::Say("Reachable.")),
            ],
        );
        assert_eq!(problems(&library), vec![]);
    }

    #[test]
    fn a_warning_prints_where_and_what() {
        let warning = Warning {
            sequence: "hub".into(),
            index: 3,
            problem: Problem::MissingSequence("refused".into()),
        };
        assert_eq!(
            warning.to_string(),
            "hub[3]: names missing sequence 'refused'"
        );
    }
}