emergent 1.9.0

Toolset for producing emergent gameplay for games written in Rust
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
//! Tasks are units of work that perform small and concrete actions in time via memory manipulation.
//!
//! Tasks are used in decision makers as smaller building bocks that can be bundled together into
//! more complex behavior.
//! Each task has a set of life-cycle methods that user apply logic to.
//!
//! The mainly used ones are:
//! - [`Task::on_enter`]
//! - [`Task::on_exit`]
//! - [`Task::on_stop`]
//! - [`Task::on_update`]
//!
//! By default all life-cycle methods are auto-implemented so user when defining a new task, focuses
//! only on implementing these methods that are needed.
//!
//! Read more about creating custom tasks at [`Task`].
//!
//! There are two generic tasks created for the user:
//! - [`NoTask`]: used as empty task, a task without any work, you can use it in places where AI
//!   should simply do nothing while that task is active.
//! - [`ClosureTask`]: a wrapper around closure-based tasks where each life-cycle method is provided
//!   by the user as separate closures, best for prototyping or making small non-repetitive logic.

/// Describes why task has stopped its work.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum TaskStopReason {
    /// Task reached its desired end.
    Completed,
    /// Task got interrupted before reaching its desired end.
    Cancelled,
    /// Task got replaced by another task or state.
    Replaced,
}

/// Task represent unit of work, an action performed in a time.
///
/// Tasks can only manipulate what's in the memory passed to their life-cycle methods so common way
/// of manipulating the world with tasks is to make memory type that can either command the world
/// or even better, put triggers in the memory before running decision maker, then after that read
/// what changed in the memory and apply changes to the world.
///
/// Tasks are managed by calling their life-cycle methods by the decision makers so implement these
/// if you need to do something during that life-cycle phase.
///
/// # Example
/// ```
/// use emergent::prelude::*;
///
/// struct Memory { counter: usize }
///
/// struct Increment;
///
/// impl Task<Memory> for Increment {
///     fn on_enter(&mut self, memory: &mut Memory) {
///         memory.counter += 1;
///     }
/// }
///
/// let mut memory = Memory { counter: 1 };
/// Increment.on_enter(&mut memory);
/// assert_eq!(memory.counter, 2);
/// ```
pub trait Task<M = ()>: Send + Sync {
    /// Tells if task is locked (it's still running). Used by decision makers to
    /// decide if one can change its state (when current task is not locked).
    #[allow(unused_variables)]
    fn is_locked(&self, memory: &M) -> bool {
        false
    }

    /// Action performed when task starts its work.
    #[allow(unused_variables)]
    fn on_enter(&mut self, memory: &mut M) {}

    /// Action performed when task stops its work.
    #[allow(unused_variables)]
    fn on_exit(&mut self, memory: &mut M) {}

    /// Action performed when task stops its work with reason.
    ///
    /// By default it calls [`Task::on_exit`] for backward compatibility.
    #[allow(unused_variables)]
    fn on_stop(&mut self, memory: &mut M, reason: TaskStopReason) {
        self.on_exit(memory);
    }

    /// Action performed when task is active and gets updated.
    #[allow(unused_variables)]
    fn on_update(&mut self, memory: &mut M) {}

    /// Action performed when task is active but decision maker did not changed
    /// its state. This one is applicable for making hierarchical decision
    /// makers (telling children decision makers to decide on new state, because
    /// some if not all decision makers are tasks).
    ///
    /// Returns `true` if this task decided on new state.
    #[allow(unused_variables)]
    fn on_process(&mut self, memory: &mut M) -> bool {
        false
    }
}

/// Task that represent no work. Use it when AI has to do nothing.
#[derive(Debug, Default, Copy, Clone)]
pub struct NoTask;

impl<M> Task<M> for NoTask {}

/// Task thet wraps closures for each life-cycle method.
///
/// # Example
/// ```
/// use emergent::prelude::*;
///
/// struct Memory { counter: usize }
///
/// let mut memory = Memory { counter: 1 };
/// let mut task = ClosureTask::default().enter(|memory: &mut Memory| memory.counter += 1);
/// task.on_enter(&mut memory);
/// assert_eq!(memory.counter, 2);
/// ```
#[allow(clippy::type_complexity)]
pub struct ClosureTask<M = ()> {
    locked: Option<Box<dyn Fn(&M) -> bool + Send + Sync>>,
    enter: Option<Box<dyn FnMut(&mut M) + Send + Sync>>,
    exit: Option<Box<dyn FnMut(&mut M) + Send + Sync>>,
    stop: Option<Box<dyn FnMut(&mut M, TaskStopReason) + Send + Sync>>,
    update: Option<Box<dyn FnMut(&mut M) + Send + Sync>>,
    process: Option<Box<dyn FnMut(&mut M) -> bool + Send + Sync>>,
}

impl<M> Default for ClosureTask<M> {
    fn default() -> Self {
        Self {
            locked: None,
            enter: None,
            exit: None,
            stop: None,
            update: None,
            process: None,
        }
    }
}

impl<M> ClosureTask<M> {
    /// See [`Task::is_locked`]
    pub fn locked<F>(mut self, f: F) -> Self
    where
        F: Fn(&M) -> bool + 'static + Send + Sync,
    {
        self.locked = Some(Box::new(f));
        self
    }

    /// See [`Task::on_enter`]
    pub fn enter<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) + 'static + Send + Sync,
    {
        self.enter = Some(Box::new(f));
        self
    }

    /// See [`Task::on_exit`]
    pub fn exit<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) + 'static + Send + Sync,
    {
        self.exit = Some(Box::new(f));
        self
    }

    /// See [`Task::on_stop`]
    pub fn stop<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M, TaskStopReason) + 'static + Send + Sync,
    {
        self.stop = Some(Box::new(f));
        self
    }

    /// See [`Task::on_update`]
    pub fn update<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) + 'static + Send + Sync,
    {
        self.update = Some(Box::new(f));
        self
    }

    /// See [`Task::on_process`]
    pub fn process<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) -> bool + 'static + Send + Sync,
    {
        self.process = Some(Box::new(f));
        self
    }
}

impl<M> Task<M> for ClosureTask<M> {
    fn is_locked(&self, memory: &M) -> bool {
        self.locked.as_ref().map(|f| f(memory)).unwrap_or_default()
    }

    fn on_enter(&mut self, memory: &mut M) {
        if let Some(f) = &mut self.enter {
            f(memory)
        }
    }

    fn on_exit(&mut self, memory: &mut M) {
        if let Some(f) = &mut self.exit {
            f(memory)
        }
    }

    fn on_stop(&mut self, memory: &mut M, reason: TaskStopReason) {
        if let Some(f) = &mut self.stop {
            f(memory, reason);
        } else {
            self.on_exit(memory);
        }
    }

    fn on_update(&mut self, memory: &mut M) {
        if let Some(f) = &mut self.update {
            f(memory)
        }
    }

    fn on_process(&mut self, memory: &mut M) -> bool {
        self.process.as_mut().map(|f| f(memory)).unwrap_or_default()
    }
}

impl<M> std::fmt::Debug for ClosureTask<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClosureTask").finish()
    }
}

/// Decides what happens to undo records when a journaled transaction commits.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub enum TransactionCommitPolicy {
    /// Commit accepts this transaction's changes and discards its undo log.
    ///
    /// Parent transaction rollbacks won't undo changes recorded by this transaction.
    #[default]
    Isolated,
    /// Commit moves this transaction's undo log into its parent transaction.
    ///
    /// Parent transaction rollbacks can still undo changes recorded by this transaction.
    MergeIntoParent,
}

/// Stack of undo records used by journaled transactions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionJournal<U> {
    frames: Vec<Vec<U>>,
}

impl<U> Default for TransactionJournal<U> {
    fn default() -> Self {
        Self { frames: Vec::new() }
    }
}

impl<U> TransactionJournal<U> {
    /// Starts new transaction frame.
    pub fn begin(&mut self) {
        self.frames.push(Vec::new());
    }

    /// Records undo entry in currently active transaction frame.
    ///
    /// Returns `false` when there is no active transaction frame.
    pub fn record(&mut self, undo: U) -> bool {
        let Some(frame) = self.frames.last_mut() else {
            return false;
        };
        frame.push(undo);
        true
    }

    /// Commits currently active transaction frame.
    pub fn commit(&mut self, policy: TransactionCommitPolicy) {
        let Some(frame) = self.frames.pop() else {
            return;
        };

        if policy == TransactionCommitPolicy::MergeIntoParent
            && let Some(parent) = self.frames.last_mut()
        {
            parent.extend(frame);
        }
    }

    /// Rolls back currently active transaction frame.
    pub fn rollback(&mut self) -> impl Iterator<Item = U> {
        self.frames.pop().unwrap_or_default().into_iter().rev()
    }

    /// Tells if there is an active transaction frame.
    pub fn is_active(&self) -> bool {
        !self.frames.is_empty()
    }

    /// Returns number of active transaction frames.
    pub fn depth(&self) -> usize {
        self.frames.len()
    }
}

/// Memory that stores and applies journaled transaction undo records.
pub trait TransactionalMemory {
    /// Domain-specific undo record.
    type Undo;

    /// Starts new transaction frame.
    fn begin_transaction(&mut self);

    /// Commits currently active transaction frame.
    fn commit_transaction(&mut self, policy: TransactionCommitPolicy);

    /// Rolls back currently active transaction frame.
    fn rollback_transaction(&mut self);

    /// Records undo entry in currently active transaction frame.
    fn record_undo(&mut self, undo: Self::Undo);
}

/// Wrapper around task that creates transaction scope with undo records stored in memory.
pub struct JournaledTransactionTask<M = ()> {
    task: Box<dyn Task<M>>,
    active: bool,
    commit_policy: TransactionCommitPolicy,
}

impl<M> JournaledTransactionTask<M> {
    /// Constructs journaled transaction wrapper around task.
    pub fn new<T>(task: T) -> Self
    where
        T: Task<M> + 'static,
    {
        Self::new_raw(Box::new(task))
    }

    /// Constructs journaled transaction wrapper around raw task.
    pub fn new_raw(task: Box<dyn Task<M>>) -> Self {
        Self {
            task,
            active: false,
            commit_policy: TransactionCommitPolicy::default(),
        }
    }

    /// Sets commit policy.
    pub fn commit_policy(mut self, policy: TransactionCommitPolicy) -> Self {
        self.commit_policy = policy;
        self
    }

    /// Returns commit policy.
    pub fn get_commit_policy(&self) -> TransactionCommitPolicy {
        self.commit_policy
    }

    /// Returns immutable access to wrapped task.
    pub fn task(&self) -> &dyn Task<M> {
        self.task.as_ref()
    }

    /// Returns mutable access to wrapped task.
    pub fn task_mut(&mut self) -> &mut dyn Task<M> {
        self.task.as_mut()
    }
}

impl<M> Task<M> for JournaledTransactionTask<M>
where
    M: TransactionalMemory,
{
    fn is_locked(&self, memory: &M) -> bool {
        self.task.is_locked(memory)
    }

    fn on_enter(&mut self, memory: &mut M) {
        memory.begin_transaction();
        self.task.on_enter(memory);
        self.active = true;
    }

    fn on_exit(&mut self, memory: &mut M) {
        self.on_stop(memory, TaskStopReason::Cancelled);
    }

    fn on_stop(&mut self, memory: &mut M, reason: TaskStopReason) {
        self.task.on_stop(memory, reason);

        if !self.active {
            return;
        }

        match reason {
            TaskStopReason::Completed => {
                memory.commit_transaction(self.commit_policy);
            }
            TaskStopReason::Cancelled | TaskStopReason::Replaced => {
                memory.rollback_transaction();
            }
        }

        self.active = false;
    }

    fn on_update(&mut self, memory: &mut M) {
        self.task.on_update(memory);
    }

    fn on_process(&mut self, memory: &mut M) -> bool {
        self.task.on_process(memory)
    }
}

impl<M> std::fmt::Debug for JournaledTransactionTask<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("JournaledTransactionTask")
            .field("active", &self.active)
            .field("commit_policy", &self.commit_policy)
            .finish()
    }
}

/// Wrapper around task that creates transaction scope with custom begin/commit/rollback hooks.
#[allow(clippy::type_complexity)]
pub struct TransactionScopeTask<M = ()> {
    task: Box<dyn Task<M>>,
    active: bool,
    begin: Option<Box<dyn FnMut(&mut M) + Send + Sync>>,
    commit: Option<Box<dyn FnMut(&mut M) + Send + Sync>>,
    rollback: Option<Box<dyn FnMut(&mut M) + Send + Sync>>,
}

impl<M> TransactionScopeTask<M> {
    /// Constructs transaction wrapper around task.
    pub fn new<T>(task: T) -> Self
    where
        T: Task<M> + 'static,
    {
        Self::new_raw(Box::new(task))
    }

    /// Constructs transaction wrapper around raw task.
    pub fn new_raw(task: Box<dyn Task<M>>) -> Self {
        Self {
            task,
            active: false,
            begin: None,
            commit: None,
            rollback: None,
        }
    }

    /// Sets begin transaction hook.
    pub fn begin<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) + 'static + Send + Sync,
    {
        self.begin = Some(Box::new(f));
        self
    }

    /// Sets commit transaction hook.
    pub fn commit<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) + 'static + Send + Sync,
    {
        self.commit = Some(Box::new(f));
        self
    }

    /// Sets rollback transaction hook.
    pub fn rollback<F>(mut self, f: F) -> Self
    where
        F: FnMut(&mut M) + 'static + Send + Sync,
    {
        self.rollback = Some(Box::new(f));
        self
    }

    /// Returns immutable access to wrapped task.
    pub fn task(&self) -> &dyn Task<M> {
        self.task.as_ref()
    }

    /// Returns mutable access to wrapped task.
    pub fn task_mut(&mut self) -> &mut dyn Task<M> {
        self.task.as_mut()
    }
}

impl<M> Task<M> for TransactionScopeTask<M> {
    fn is_locked(&self, memory: &M) -> bool {
        self.task.is_locked(memory)
    }

    fn on_enter(&mut self, memory: &mut M) {
        if let Some(f) = &mut self.begin {
            f(memory);
        }
        self.task.on_enter(memory);
        self.active = true;
    }

    fn on_exit(&mut self, memory: &mut M) {
        self.on_stop(memory, TaskStopReason::Cancelled);
    }

    fn on_stop(&mut self, memory: &mut M, reason: TaskStopReason) {
        self.task.on_stop(memory, reason);

        if !self.active {
            return;
        }

        match reason {
            TaskStopReason::Completed => {
                if let Some(f) = &mut self.commit {
                    f(memory);
                }
            }
            TaskStopReason::Cancelled | TaskStopReason::Replaced => {
                if let Some(f) = &mut self.rollback {
                    f(memory);
                }
            }
        }

        self.active = false;
    }

    fn on_update(&mut self, memory: &mut M) {
        self.task.on_update(memory);
    }

    fn on_process(&mut self, memory: &mut M) -> bool {
        self.task.on_process(memory)
    }
}

impl<M> std::fmt::Debug for TransactionScopeTask<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TransactionScopeTask")
            .field("active", &self.active)
            .finish()
    }
}