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
//! Health Module
//!
//! Handles health and health-related effects for living entities.
use std::cmp;

use log::info;
use serde::de::{self, Deserializer, EnumAccess, VariantAccess, Visitor};
use serde::{Deserialize, Serialize};

use crate::{ViewItem, WorldObject};

/// Outcome of ticking queued health effects for an entity.
pub struct HealthTickResult {
    pub view_items: Vec<ViewItem>,
    pub death_cause: Option<String>,
}

/// Represents the state of a living entity's health and related effects.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct HealthState {
    max_hp: u32,
    current_hp: u32,
    pub(crate) effects: Vec<HealthEffect>,
}
impl HealthState {
    /// Creates a new, empty `HealthState`
    pub fn new() -> HealthState {
        HealthState::default()
    }

    /// Create a clean `HealthState` with specified maximum health
    pub fn new_at_max(max_hp: u32) -> HealthState {
        HealthState {
            max_hp,
            current_hp: max_hp,
            effects: Vec::new(),
        }
    }

    /// Get the maximum HP for this entity
    pub fn max_hp(&self) -> u32 {
        self.max_hp
    }

    /// Get the current HP for this entity
    pub fn current_hp(&self) -> u32 {
        self.current_hp
    }

    /// Return whether this entity is alive or dead.
    /// In the future, there may be additional states -- so not using a boolean here.
    pub fn life_state(&self) -> LifeState {
        if self.current_hp > 0 {
            LifeState::Alive
        } else {
            LifeState::Dead
        }
    }

    /// Do damage to health. Saturates at zero.
    pub fn damage(&mut self, amount: u32) {
        self.current_hp = self.current_hp.saturating_sub(amount);
    }

    /// Heal the character. Saturates at max health.
    pub fn heal(&mut self, amount: u32) {
        self.current_hp = cmp::min(self.max_hp, self.current_hp.saturating_add(amount));
    }

    /// Add a `HealthEffect` to the queue
    pub fn add_effect(&mut self, fx: HealthEffect) {
        self.effects.push(fx);
    }

    /// Add a damage over time effect.
    pub fn add_dot_effect(&mut self, cause: &str, damage: u32, times: u32) {
        self.effects.push(HealthEffect::DamageOverTime {
            cause: cause.to_string(),
            amount: damage,
            times,
        });
    }
    /// Take an effect out of the queue
    pub fn remove_effect(&mut self, cause: &str) -> Option<HealthEffect> {
        if let Some(idx) = self.effects.iter().position(|fx| fx.cause_matches(cause)) {
            Some(self.effects.remove(idx))
        } else {
            None
        }
    }

    /// Iterate through pending health effects, applying each one.
    pub fn apply_effects(&mut self, display_name: &str) -> HealthTickResult {
        let mut hp_tally = self.current_hp;
        let mut unexpired_effects = Vec::new();
        let mut health_messages = Vec::new();
        let mut death_cause: Option<String> = None;

        for fx in &self.effects {
            log_and_display_health_change(display_name, &mut health_messages, fx);
            let effect_outcome = fx.apply(hp_tally, self.max_hp);
            hp_tally = effect_outcome.remaining_hp;
            // break out and return if character is dead!
            if effect_outcome.remaining_hp == 0 {
                self.current_hp = 0;
                death_cause = Some(fx.cause_string());
                break;
            }
            if let Some(effect) = effect_outcome.residual_effect {
                unexpired_effects.push(effect);
            }
        }
        self.current_hp = hp_tally;
        self.effects = unexpired_effects;
        HealthTickResult {
            view_items: health_messages,
            death_cause,
        }
    }
}

fn log_and_display_health_change(display_name: &str, health_messages: &mut Vec<ViewItem>, fx: &HealthEffect) {
    match &fx {
        HealthEffect::InstantDamage { cause, amount } => {
            info!("{display_name} damaged by '{cause}' (-{amount} hp)");
            health_messages.push(ViewItem::CharacterHarmed {
                name: display_name.into(),
                cause: cause.into(),
                amount: *amount,
            });
        },
        HealthEffect::InstantHeal { cause, amount } => {
            info!("{display_name} healed by '{cause}' (+{amount} hp)");
            health_messages.push(ViewItem::CharacterHealed {
                name: display_name.into(),
                cause: cause.into(),
                amount: *amount,
            });
        },
        HealthEffect::DamageOverTime { cause, amount, times } => {
            info!(
                "{display_name} damaged by '{cause}' d.o.t. (-{amount} hp, {} left)",
                times - 1
            );
            health_messages.push(ViewItem::CharacterHarmed {
                name: display_name.into(),
                cause: cause.into(),
                amount: *amount,
            });
        },
        HealthEffect::HealOverTime { cause, amount, times } => {
            info!(
                "{display_name} healed by '{cause}' h.o.t. (-{amount} hp, {} left)",
                times - 1
            );
            health_messages.push(ViewItem::CharacterHealed {
                name: display_name.into(),
                cause: cause.into(),
                amount: *amount,
            });
        },
    }
}

/// Holds the result of one 'tick' of a particular health effect.
pub struct EffectResult {
    remaining_hp: u32,
    residual_effect: Option<HealthEffect>,
}
impl From<(u32, Option<HealthEffect>)> for EffectResult {
    fn from(value: (u32, Option<HealthEffect>)) -> Self {
        EffectResult {
            remaining_hp: value.0,
            residual_effect: value.1,
        }
    }
}

/// Functionality common to all game entities that are alive
pub trait LivingEntity: WorldObject {
    /// The amount of health this entity has when fully healed.
    fn max_hp(&self) -> u32;

    /// The current level of this entity's health.
    fn current_hp(&self) -> u32;

    /// Reduce this entity's health by `amount`.
    fn damage(&mut self, amount: u32);

    /// Increase this entity's health by `amount` (up to defined maximum)
    fn heal(&mut self, amount: u32);

    /// Determine the life status of this entity.
    ///
    /// For now, the means `Alive` or `Dead` but could later include other
    /// variants like "Revived" or "Stasis" or "Undead".
    fn life_state(&self) -> LifeState;

    /// Add a health effect to start processing on the next turn.
    fn add_health_effect(&mut self, effect: HealthEffect);

    /// Remove a pending health effect from the queue.
    fn remove_health_effect(&mut self, cause: &str) -> Option<HealthEffect>;

    /// Apply and update queued health effects when advancing a turn.
    #[must_use] /* life or death notifications may be returned! */
    fn tick_health_effects(&mut self) -> HealthTickResult;
}

/// Possible life states for living entities
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum LifeState {
    Alive,
    Dead,
}

/// Types of health effects that can be applied to living game entities.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum HealthEffect {
    InstantDamage { cause: String, amount: u32 },
    InstantHeal { cause: String, amount: u32 },
    DamageOverTime { cause: String, amount: u32, times: u32 },
    HealOverTime { cause: String, amount: u32, times: u32 },
}

#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
enum HealthEffectKind {
    InstantDamage,
    InstantHeal,
    DamageOverTime,
    HealOverTime,
}

impl HealthEffectKind {
    fn from_str(value: &str) -> Option<Self> {
        match value {
            v if v.eq_ignore_ascii_case("instantDamage") => Some(Self::InstantDamage),
            v if v.eq_ignore_ascii_case("instantHeal") => Some(Self::InstantHeal),
            v if v.eq_ignore_ascii_case("damageOverTime") => Some(Self::DamageOverTime),
            v if v.eq_ignore_ascii_case("healOverTime") => Some(Self::HealOverTime),
            _ => None,
        }
    }
}

impl<'de> Deserialize<'de> for HealthEffectKind {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct KindVisitor;

        impl<'de> Visitor<'de> for KindVisitor {
            type Value = HealthEffectKind;

            fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                f.write_str("health effect type identifier")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                HealthEffectKind::from_str(value).ok_or_else(|| {
                    de::Error::unknown_variant(
                        value,
                        &["instantDamage", "instantHeal", "damageOverTime", "healOverTime"],
                    )
                })
            }

            fn visit_borrowed_str<E>(self, value: &'de str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                self.visit_str(value)
            }

            fn visit_string<E>(self, value: String) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                self.visit_str(&value)
            }

            fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
            where
                A: EnumAccess<'de>,
            {
                let (variant, access) = data.variant::<String>()?;
                access.unit_variant()?;
                self.visit_str(&variant)
            }
        }

        deserializer.deserialize_any(KindVisitor)
    }
}

#[derive(Deserialize)]
struct HealthEffectRepr {
    #[serde(rename = "type")]
    kind: HealthEffectKind,
    cause: String,
    #[serde(default)]
    amount: u32,
    #[serde(default)]
    times: u32,
}

impl<'de> Deserialize<'de> for HealthEffect {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let repr = HealthEffectRepr::deserialize(deserializer)?;
        Ok(match repr.kind {
            HealthEffectKind::InstantDamage => HealthEffect::InstantDamage {
                cause: repr.cause,
                amount: repr.amount,
            },
            HealthEffectKind::InstantHeal => HealthEffect::InstantHeal {
                cause: repr.cause,
                amount: repr.amount,
            },
            HealthEffectKind::DamageOverTime => HealthEffect::DamageOverTime {
                cause: repr.cause,
                amount: repr.amount,
                times: repr.times,
            },
            HealthEffectKind::HealOverTime => HealthEffect::HealOverTime {
                cause: repr.cause,
                amount: repr.amount,
                times: repr.times,
            },
        })
    }
}

impl HealthEffect {
    /// Returns `true` if the `cause` matches the supplied string
    pub fn cause_matches(&self, pattern: &str) -> bool {
        match &self {
            Self::DamageOverTime { cause, .. }
            | Self::HealOverTime { cause, .. }
            | Self::InstantDamage { cause, .. }
            | Self::InstantHeal { cause, .. } => cause == pattern,
        }
    }

    /// Returns a string describing what caused the health change (e.g. "drank potion", "played with fire")
    pub fn cause_string(&self) -> String {
        match &self {
            Self::DamageOverTime { cause, .. }
            | Self::HealOverTime { cause, .. }
            | Self::InstantDamage { cause, .. }
            | Self::InstantHeal { cause, .. } => cause.clone(),
        }
    }

    /// Applies this effect to the supplied `HealthState`
    ///
    /// The current and max hp are passed in. The result is a tuple containing updated hp
    /// after processing the effect, and an optional follow up effect (if any) for
    /// over-time effects.
    pub fn apply(&self, current_hp: u32, max_hp: u32) -> EffectResult {
        match self {
            Self::InstantDamage { amount, .. } => (current_hp.saturating_sub(*amount), None).into(),
            Self::InstantHeal { amount, .. } => (cmp::min(max_hp, current_hp.saturating_add(*amount)), None).into(),
            Self::DamageOverTime { cause, amount, times } => {
                let times_left: u32 = times.saturating_sub(1);
                let hp_left = current_hp.saturating_sub(*amount);
                let follow_up = if times_left > 0 {
                    Some(Self::DamageOverTime {
                        cause: cause.clone(),
                        amount: *amount,
                        times: times_left,
                    })
                } else {
                    None
                };
                (hp_left, follow_up).into()
            },
            Self::HealOverTime { cause, amount, times } => {
                let times_left: u32 = times.saturating_sub(1);
                let healed_hp = cmp::min(current_hp.saturating_add(*amount), max_hp);
                let follow_up = if times_left > 0 {
                    Some(Self::HealOverTime {
                        cause: cause.clone(),
                        amount: *amount,
                        times: times_left,
                    })
                } else {
                    None
                };
                (healed_hp, follow_up).into()
            },
        }
    }
}

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

    #[test]
    fn heal_saturates_at_max_hp() {
        let mut state = HealthState::new_at_max(10);
        state.damage(5);
        state.heal(3);
        assert_eq!(state.current_hp, 8);

        state.heal(10);
        assert_eq!(state.current_hp, 10);
    }

    #[test]
    fn instant_heal_effect_respects_max_hp() {
        let effect = HealthEffect::InstantHeal {
            cause: "potion".into(),
            amount: 5,
        };

        let outcome = effect.apply(8, 10);
        assert_eq!(outcome.remaining_hp, 10);
        assert!(outcome.residual_effect.is_none());
    }

    #[test]
    fn apply_effects_enqueues_follow_up_for_overtime_healing() {
        let mut state = HealthState {
            max_hp: 10,
            current_hp: 6,
            effects: vec![HealthEffect::HealOverTime {
                cause: "campfire".into(),
                amount: 3,
                times: 2,
            }],
        };

        state.apply_effects("test");
        assert_eq!(state.current_hp, 9);
        assert_eq!(state.effects.len(), 1);

        match &state.effects[0] {
            HealthEffect::HealOverTime { amount, times, .. } => {
                assert_eq!(*amount, 3);
                assert_eq!(*times, 1);
            },
            unexpected => panic!("unexpected effect remaining: {unexpected:?}"),
        }

        state.apply_effects("test");
        assert_eq!(state.current_hp, 10);
        assert!(state.effects.is_empty());
    }

    #[test]
    fn lethal_effects_stop_processing_remaining_queue() {
        let mut state = HealthState {
            max_hp: 10,
            current_hp: 4,
            effects: vec![
                HealthEffect::DamageOverTime {
                    cause: "poison cloud".into(),
                    amount: 5,
                    times: 1,
                },
                HealthEffect::InstantHeal {
                    cause: "healing potion".into(),
                    amount: 5,
                },
            ],
        };

        state.apply_effects("test");
        assert_eq!(state.current_hp, 0);
        assert!(state.effects.is_empty());
    }
}