amble_engine 0.67.0

Data-first interactive fiction engine and REPL for worlds authored in the Amble DSL.
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Event Scheduler
//!
//! Simple future one-off or recurring events can be accomplished using flags and their associated
//! "turnstamps" (turn on which they were set.) This system will allow for more complicated series
//! of events scheduled at arbitrary times in the future. Because it is owned by `AmbleWorld`, all
//! scheduled events should persist correctly across saves.
//!
//! ### Designer Note:
//! This implementation as a priority queue using a binary heap is almost certainly overkill for most
//! likely use cases of this engine. (To be honest, I'm largely using it just to gain experience using
//! the `std::collections::BinaryHeap`). If problematic, a simpler Vec with a filter or partition on
//! turn due would be sufficient.

use std::cmp::Reverse;
use std::collections::BinaryHeap;

use log::info;
use serde::{Deserialize, Serialize};

use crate::trigger::{ScriptedAction, TriggerCondition};

#[cfg(test)]
const PLACEHOLDER_THRESHOLD: usize = 4;
#[cfg(not(test))]
const PLACEHOLDER_THRESHOLD: usize = 64;

/// The event scheduler.
///
/// Uses a reversed binary heap to maintain a priority queue for upcoming events.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Scheduler {
    pub heap: BinaryHeap<Reverse<(usize, usize)>>, /* (turn_due, event_idx) */
    pub events: Vec<ScheduledEvent>,
}
impl Scheduler {
    /// Schedule some `TriggerActions` to fire a specified number of turns in the future.
    pub fn schedule_in(&mut self, now: usize, turns_ahead: usize, actions: Vec<ScriptedAction>, note: Option<String>) {
        let idx = self.events.len();
        let on_turn = now + turns_ahead;
        let log_msg = match &note {
            Some(msg) => msg.as_str(),
            None => "<no note provided>",
        };
        info!("scheduling event (turn now/due = {now}/{on_turn}): \"{log_msg}\"");
        self.heap.push(Reverse((on_turn, idx)));
        self.events.push(ScheduledEvent {
            on_turn,
            actions,
            note,
            condition: None,
            on_false: OnFalsePolicy::Cancel,
        });
    }

    /// Schedule some `TriggerActions` to fire on a specific turn.
    pub fn schedule_on(&mut self, on_turn: usize, actions: Vec<ScriptedAction>, note: Option<String>) {
        let idx = self.events.len();
        let log_msg = match &note {
            Some(note) => note.as_str(),
            None => "<no note provided>",
        };
        info!("scheduling event (turn due = {on_turn}): \"{log_msg}\"");
        self.heap.push(Reverse((on_turn, idx)));
        self.events.push(ScheduledEvent {
            on_turn,
            actions,
            note,
            condition: None,
            on_false: OnFalsePolicy::Cancel,
        });
    }

    /// Schedule actions in the future with an optional condition and on-false policy.
    ///
    /// Primarily used by conditional scheduling trigger actions; events that fail
    /// the condition at execution time can be retried according to `on_false`.
    pub fn schedule_in_if(
        &mut self,
        now: usize,
        turns_ahead: usize,
        condition: Option<EventCondition>,
        on_false: OnFalsePolicy,
        actions: Vec<ScriptedAction>,
        note: Option<String>,
    ) {
        let idx = self.events.len();
        let on_turn = now + turns_ahead;
        let log_msg = match &note {
            Some(msg) => msg.as_str(),
            None => "<no note provided>",
        };
        info!("scheduling conditional event (turn now/due = {now}/{on_turn}): \"{log_msg}\"");
        self.heap.push(Reverse((on_turn, idx)));
        self.events.push(ScheduledEvent {
            on_turn,
            actions,
            note,
            condition,
            on_false,
        });
    }

    /// Schedule actions on a specific turn with an optional condition and on-false policy.
    pub fn schedule_on_if(
        &mut self,
        on_turn: usize,
        condition: Option<EventCondition>,
        on_false: OnFalsePolicy,
        actions: Vec<ScriptedAction>,
        note: Option<String>,
    ) {
        let idx = self.events.len();
        let log_msg = match &note {
            Some(note) => note.as_str(),
            None => "<no note provided>",
        };
        info!("scheduling conditional event (turn due = {on_turn}): \"{log_msg}\"");
        self.heap.push(Reverse((on_turn, idx)));
        self.events.push(ScheduledEvent {
            on_turn,
            actions,
            note,
            condition,
            on_false,
        });
    }

    /// Pop the next due event, if any.
    ///
    /// Returns `None` when the earliest scheduled event is still in the future.
    pub fn pop_due(&mut self, now: usize) -> Option<ScheduledEvent> {
        if let Some(Reverse((turn_due, idx))) = self.heap.peek().copied()
            && now >= turn_due
        {
            self.heap.pop();
            // "take" instead of "remove" keeps indices stable for the heap entries
            // leaves default placeholders
            let event = std::mem::take(&mut self.events[idx]);
            self.compact_if_needed();
            return Some(event);
        }
        None
    }

    /// Rebuild the underlying storage when too many placeholder tombstones accumulate.
    fn compact_if_needed(&mut self) {
        let placeholder_count = self.events.iter().filter(|e| e.is_placeholder()).count();
        if placeholder_count > PLACEHOLDER_THRESHOLD {
            let old_events = std::mem::take(&mut self.events);
            let mut index_map = vec![0; old_events.len()];
            for (old_idx, event) in old_events.into_iter().enumerate() {
                if event.is_placeholder() {
                    continue;
                }
                let new_idx = self.events.len();
                index_map[old_idx] = new_idx;
                self.events.push(event);
            }
            let mut new_heap = BinaryHeap::with_capacity(self.heap.len());
            while let Some(Reverse((turn_due, old_idx))) = self.heap.pop() {
                let new_idx = index_map[old_idx];
                new_heap.push(Reverse((turn_due, new_idx)));
            }
            self.heap = new_heap;
        }
    }
}

/// An event (sequence of `TriggerActions`) scheduled for a particular turn.
///
/// ### Fields:
/// `on_turn` = turn on which to fire
/// actions = list of `TriggerActions` to take when the turn arrives
/// note = description of event (for logging)
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct ScheduledEvent {
    pub on_turn: usize,
    pub actions: Vec<ScriptedAction>,
    pub note: Option<String>,
    /// Optional condition that must be true for the event to fire.
    pub condition: Option<EventCondition>,
    /// Policy to apply when the condition evaluates to false.
    pub on_false: OnFalsePolicy,
}

impl ScheduledEvent {
    /// Placeholder events mark consumed slots within the scheduler. Determine whether this is one of them.
    fn is_placeholder(&self) -> bool {
        *self == ScheduledEvent::default()
    }
}

/// Policy controlling behavior when a scheduled event's condition is false.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub enum OnFalsePolicy {
    /// Cancel the event and do not retry.
    #[default]
    Cancel,
    /// Retry after the specified number of turns.
    RetryAfter(usize),
    /// Retry on the next turn.
    RetryNextTurn,
}

/// Condition for scheduled events. Can wrap a `TriggerCondition` or combine multiple.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum EventCondition {
    /// Single trigger condition; evaluated via existing machinery.
    Trigger(TriggerCondition),
    /// All subconditions must be true.
    All(Vec<EventCondition>),
    /// Any subcondition must be true.
    Any(Vec<EventCondition>),
}

impl EventCondition {
    /// Evaluate the condition against the current world state and any recent events.
    pub fn eval_with_events(&self, world: &crate::world::AmbleWorld, events: &[TriggerCondition]) -> bool {
        match self {
            EventCondition::Trigger(tc) => tc.matches_event_in(events) || tc.is_ongoing(world),
            EventCondition::All(conds) => conds.iter().all(|c| c.eval_with_events(world, events)),
            EventCondition::Any(conds) => conds.iter().any(|c| c.eval_with_events(world, events)),
        }
    }

    /// Evaluate the condition against the current world state.
    pub fn eval(&self, world: &crate::world::AmbleWorld) -> bool {
        self.eval_with_events(world, &[])
    }

    /// Determine whether the condition contains an Ambient that applies to the current
    /// player location.
    pub fn eval_ambient(&self, world: &crate::world::AmbleWorld) -> bool {
        match self {
            EventCondition::Trigger(tc) => match tc {
                TriggerCondition::Ambient { room_ids, .. } => world
                    .player
                    .location
                    .room_id()
                    .is_ok_and(|room_id| room_ids.is_empty() || room_ids.contains(&room_id)),
                _ => tc.is_ongoing(world),
            },
            EventCondition::All(conds) => conds.iter().all(|c| c.eval_ambient(world)),
            EventCondition::Any(conds) => conds.iter().any(|c| c.eval_ambient(world)),
        }
    }

    /// Returns true if any nested trigger condition satisfies the matcher.
    pub fn any_trigger<F>(&self, matcher: F) -> bool
    where
        F: FnMut(&TriggerCondition) -> bool,
    {
        let mut matcher = matcher;
        self.any_trigger_inner(&mut matcher)
    }

    /// Apply a visitor closure to every nested trigger condition.
    pub fn for_each_condition<F>(&self, visitor: F)
    where
        F: FnMut(&TriggerCondition),
    {
        let mut visitor = visitor;
        self.for_each_trigger_inner(&mut visitor);
    }

    fn any_trigger_inner<F>(&self, matcher: &mut F) -> bool
    where
        F: FnMut(&TriggerCondition) -> bool,
    {
        match self {
            EventCondition::Trigger(tc) => matcher(tc),
            EventCondition::All(conds) | EventCondition::Any(conds) => {
                conds.iter().any(|c| c.any_trigger_inner(matcher))
            },
        }
    }

    fn for_each_trigger_inner<F>(&self, visitor: &mut F)
    where
        F: FnMut(&TriggerCondition),
    {
        match self {
            EventCondition::Trigger(tc) => visitor(tc),
            EventCondition::All(conds) | EventCondition::Any(conds) => {
                for cond in conds {
                    cond.for_each_trigger_inner(visitor);
                }
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::trigger::TriggerAction;

    fn create_test_action() -> ScriptedAction {
        ScriptedAction::new(TriggerAction::ShowMessage("Test message".to_string()))
    }

    fn create_test_actions(count: usize) -> Vec<ScriptedAction> {
        (0..count)
            .map(|i| ScriptedAction::new(TriggerAction::ShowMessage(format!("Message {i}"))))
            .collect()
    }

    #[test]
    fn scheduler_new_is_empty() {
        let scheduler = Scheduler::default();
        assert!(scheduler.heap.is_empty());
        assert!(scheduler.events.is_empty());
    }

    #[test]
    fn schedule_in_adds_event_correctly() {
        let mut scheduler = Scheduler::default();
        let actions = vec![create_test_action()];
        let note = Some("Test event".to_string());

        scheduler.schedule_in(5, 3, actions.clone(), note.clone());

        assert_eq!(scheduler.events.len(), 1);
        assert_eq!(scheduler.heap.len(), 1);

        let event = &scheduler.events[0];
        assert_eq!(event.on_turn, 8); // 5 + 3
        assert_eq!(event.actions.len(), 1);
        assert_eq!(event.note, note);
    }

    #[test]
    fn schedule_on_adds_event_correctly() {
        let mut scheduler = Scheduler::default();
        let actions = vec![create_test_action()];
        let note = Some("Direct schedule test".to_string());

        scheduler.schedule_on(10, actions.clone(), note.clone());

        assert_eq!(scheduler.events.len(), 1);
        assert_eq!(scheduler.heap.len(), 1);

        let event = &scheduler.events[0];
        assert_eq!(event.on_turn, 10);
        assert_eq!(event.actions.len(), 1);
        assert_eq!(event.note, note);
    }

    #[test]
    fn schedule_multiple_events() {
        let mut scheduler = Scheduler::default();

        scheduler.schedule_in(0, 5, vec![create_test_action()], Some("Event 1".to_string()));
        scheduler.schedule_in(0, 3, vec![create_test_action()], Some("Event 2".to_string()));
        scheduler.schedule_on(10, vec![create_test_action()], Some("Event 3".to_string()));

        assert_eq!(scheduler.events.len(), 3);
        assert_eq!(scheduler.heap.len(), 3);
    }

    #[test]
    fn pop_due_returns_none_when_nothing_due() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_in(5, 5, vec![create_test_action()], None);

        let result = scheduler.pop_due(8); // Event due on turn 10
        assert!(result.is_none());
        assert_eq!(scheduler.heap.len(), 1); // Event should still be in heap
    }

    #[test]
    fn pop_due_returns_event_when_due() {
        let mut scheduler = Scheduler::default();
        let actions = vec![create_test_action()];
        let note = Some("Due event".to_string());

        scheduler.schedule_in(5, 3, actions.clone(), note.clone());

        let result = scheduler.pop_due(8); // Event due exactly on turn 8
        assert!(result.is_some());

        let event = result.unwrap();
        assert_eq!(event.on_turn, 8);
        assert_eq!(event.note, note);
        assert_eq!(event.actions.len(), 1);

        // Heap should now be empty
        assert!(scheduler.heap.is_empty());
    }

    #[test]
    fn pop_due_returns_event_when_overdue() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_in(5, 3, vec![create_test_action()], Some("Overdue event".to_string()));

        let result = scheduler.pop_due(10); // Event was due on turn 8, now turn 10
        assert!(result.is_some());

        let event = result.unwrap();
        assert_eq!(event.on_turn, 8);
    }

    #[test]
    fn events_fire_in_correct_order() {
        let mut scheduler = Scheduler::default();

        // Schedule events in reverse chronological order
        scheduler.schedule_on(15, create_test_actions(1), Some("Third".to_string()));
        scheduler.schedule_on(5, create_test_actions(1), Some("First".to_string()));
        scheduler.schedule_on(10, create_test_actions(1), Some("Second".to_string()));

        // Pop events in chronological order
        let first = scheduler.pop_due(5).unwrap();
        assert_eq!(first.note, Some("First".to_string()));
        assert_eq!(first.on_turn, 5);

        let second = scheduler.pop_due(10).unwrap();
        assert_eq!(second.note, Some("Second".to_string()));
        assert_eq!(second.on_turn, 10);

        let third = scheduler.pop_due(15).unwrap();
        assert_eq!(third.note, Some("Third".to_string()));
        assert_eq!(third.on_turn, 15);

        // Nothing left
        assert!(scheduler.pop_due(20).is_none());
    }

    #[test]
    fn events_with_same_turn_fire_in_fifo_order() {
        let mut scheduler = Scheduler::default();

        // Schedule multiple events for the same turn
        scheduler.schedule_on(10, create_test_actions(1), Some("First scheduled".to_string()));
        scheduler.schedule_on(10, create_test_actions(1), Some("Second scheduled".to_string()));
        scheduler.schedule_on(10, create_test_actions(1), Some("Third scheduled".to_string()));

        // They should come out in FIFO order (first scheduled, first fired)
        let first = scheduler.pop_due(10).unwrap();
        assert_eq!(first.note, Some("First scheduled".to_string()));

        let second = scheduler.pop_due(10).unwrap();
        assert_eq!(second.note, Some("Second scheduled".to_string()));

        let third = scheduler.pop_due(10).unwrap();
        assert_eq!(third.note, Some("Third scheduled".to_string()));
    }

    #[test]
    fn pop_due_multiple_events_same_turn() {
        let mut scheduler = Scheduler::default();

        scheduler.schedule_on(5, create_test_actions(1), Some("Event A".to_string()));
        scheduler.schedule_on(5, create_test_actions(1), Some("Event B".to_string()));
        scheduler.schedule_on(10, create_test_actions(1), Some("Event C".to_string()));

        // Pop all events due on turn 5
        let mut events_turn_5 = Vec::new();
        while let Some(event) = scheduler.pop_due(5) {
            if event.on_turn == 5 {
                events_turn_5.push(event);
            } else {
                break;
            }
        }

        assert_eq!(events_turn_5.len(), 2);
        assert!(events_turn_5.iter().any(|e| e.note == Some("Event A".to_string())));
        assert!(events_turn_5.iter().any(|e| e.note == Some("Event B".to_string())));

        // Event C should still be in scheduler
        let event_c = scheduler.pop_due(10).unwrap();
        assert_eq!(event_c.note, Some("Event C".to_string()));
    }

    #[test]
    fn schedule_with_no_note() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_in(0, 5, vec![create_test_action()], None);

        let event = scheduler.pop_due(5).unwrap();
        assert_eq!(event.note, None);
    }

    #[test]
    fn schedule_with_empty_actions() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_in(0, 5, vec![], Some("Empty actions".to_string()));

        let event = scheduler.pop_due(5).unwrap();
        assert!(event.actions.is_empty());
        assert_eq!(event.note, Some("Empty actions".to_string()));
    }

    #[test]
    fn schedule_with_multiple_actions() {
        let mut scheduler = Scheduler::default();
        let actions = create_test_actions(5);

        scheduler.schedule_in(0, 3, actions.clone(), Some("Multi-action event".to_string()));

        let event = scheduler.pop_due(3).unwrap();
        assert_eq!(event.actions.len(), 5);
    }

    #[test]
    fn scheduled_event_default() {
        let event = ScheduledEvent::default();
        assert_eq!(event.on_turn, 0);
        assert!(event.actions.is_empty());
        assert_eq!(event.note, None);
    }

    #[test]
    fn mem_take_leaves_default_placeholder() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_in(0, 5, vec![create_test_action()], Some("Test".to_string()));

        let _event = scheduler.pop_due(5).unwrap();

        // The event vector should still have the placeholder
        assert_eq!(scheduler.events.len(), 1);
        let placeholder = &scheduler.events[0];
        assert_eq!(placeholder.on_turn, 0);
        assert!(placeholder.actions.is_empty());
        assert_eq!(placeholder.note, None);
    }

    #[test]
    fn compact_events_when_placeholder_threshold_exceeded() {
        let mut scheduler = Scheduler::default();

        for i in 1..=6 {
            scheduler.schedule_on(i, create_test_actions(1), Some(format!("Event {i}")));
        }

        for turn in 1..=5 {
            let ev = scheduler.pop_due(turn).unwrap();
            assert_eq!(ev.note, Some(format!("Event {turn}")));
        }

        assert_eq!(scheduler.events.len(), 1);
        assert_eq!(scheduler.heap.len(), 1);
        assert_eq!(scheduler.events[0].note, Some("Event 6".to_string()));

        let final_event = scheduler.pop_due(6).unwrap();
        assert_eq!(final_event.note, Some("Event 6".to_string()));
    }

    #[test]
    fn edge_case_turn_zero() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_on(0, vec![create_test_action()], Some("Turn zero".to_string()));

        let event = scheduler.pop_due(0).unwrap();
        assert_eq!(event.on_turn, 0);
    }

    #[test]
    fn edge_case_large_turn_numbers() {
        let mut scheduler = Scheduler::default();
        let large_turn = usize::MAX - 1000;

        scheduler.schedule_on(large_turn, vec![create_test_action()], Some("Large turn".to_string()));

        let event = scheduler.pop_due(large_turn).unwrap();
        assert_eq!(event.on_turn, large_turn);
    }

    #[test]
    fn serialization_roundtrip() {
        let mut scheduler = Scheduler::default();
        scheduler.schedule_in(5, 10, create_test_actions(3), Some("Serialization test".to_string()));
        scheduler.schedule_on(20, create_test_actions(2), None);

        // Serialize
        let serialized = serde_json::to_string(&scheduler).expect("Failed to serialize");

        // Deserialize
        let deserialized: Scheduler = serde_json::from_str(&serialized).expect("Failed to deserialize");

        // Verify structure is preserved
        assert_eq!(deserialized.events.len(), scheduler.events.len());
        assert_eq!(deserialized.heap.len(), scheduler.heap.len());

        // Verify functionality is preserved
        let mut des_scheduler = deserialized;
        let event1 = des_scheduler.pop_due(15).unwrap();
        assert_eq!(event1.on_turn, 15);
        assert_eq!(event1.actions.len(), 3);

        let event2 = des_scheduler.pop_due(20).unwrap();
        assert_eq!(event2.on_turn, 20);
        assert_eq!(event2.actions.len(), 2);
        assert_eq!(event2.note, None);
    }
}