saints-mile 1.0.2

A frontier JRPG for the adults who loved those games first
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
//! Pressure engine — runtime for nonstandard encounters.
//!
//! FT-003: The pressure system was schema-only. This module provides
//! a PressureEngine that can run a pressure encounter to completion:
//! track bars, process actions, check thresholds, resolve outcomes.

use tracing::{debug, info};

use super::types::*;
use crate::scene::types::StateEffect;

/// Result of processing a single pressure action.
#[derive(Debug, Clone)]
pub struct PressureActionResult {
    pub actor: String,
    pub action_label: String,
    pub bar_id: String,
    pub delta: i32,
    pub bar_after: i32,
    pub description: String,
}

/// How the pressure encounter ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PressureOutcome {
    Success,
    Failure,
    /// Still in progress.
    InProgress,
}

/// Result of checking thresholds after an action.
#[derive(Debug, Clone)]
pub struct PressureResolution {
    pub outcome: PressureOutcome,
    pub effects: Vec<StateEffect>,
    /// Which bar triggered the resolution, if any.
    pub trigger_bar: Option<String>,
}

/// Live state of a pressure encounter in progress.
#[derive(Debug)]
pub struct PressureEngine {
    /// The encounter definition.
    pub encounter: PressureEncounter,
    /// Current round number.
    pub round: u32,
    /// Whether the encounter is resolved.
    pub outcome: PressureOutcome,
    /// Accumulated state effects to apply after resolution.
    pub pending_effects: Vec<StateEffect>,
    /// Cargo integrity tracking (for Escort type).
    pub cargo_state: Vec<CargoLiveState>,
}

/// Live state of a cargo item during an Escort pressure encounter.
#[derive(Debug, Clone)]
pub struct CargoLiveState {
    pub id: String,
    pub name: String,
    pub integrity: i32,
    pub max_integrity: i32,
    pub lost: bool,
    pub loss_effects: Vec<StateEffect>,
}

impl PressureEngine {
    /// Create a new pressure engine from an encounter definition.
    pub fn new(encounter: PressureEncounter) -> Self {
        let cargo_state = match &encounter.pressure_type {
            PressureType::Escort { cargo } => {
                cargo.iter().map(|c| CargoLiveState {
                    id: c.id.clone(),
                    name: c.name.clone(),
                    integrity: c.integrity,
                    max_integrity: c.max_integrity,
                    lost: false,
                    loss_effects: c.loss_effect.clone(),
                }).collect()
            }
            _ => Vec::new(),
        };

        PressureEngine {
            encounter,
            round: 0,
            outcome: PressureOutcome::InProgress,
            pending_effects: Vec::new(),
            cargo_state,
        }
    }

    /// Start a new round. Returns the round number.
    pub fn begin_round(&mut self) -> u32 {
        self.round += 1;
        debug!(round = self.round, "pressure round started");
        self.round
    }

    /// Get the current value of a pressure bar.
    pub fn get_bar(&self, bar_id: &str) -> Option<&PressureBar> {
        self.encounter.pressure_bars.iter().find(|b| b.id == bar_id)
    }

    /// Get a mutable reference to a pressure bar.
    fn get_bar_mut(&mut self, bar_id: &str) -> Option<&mut PressureBar> {
        self.encounter.pressure_bars.iter_mut().find(|b| b.id == bar_id)
    }

    /// Get all available actions for a character this round.
    pub fn available_actions(&self, character_id: &str) -> Vec<&PressureAction> {
        self.encounter.party_actions.iter()
            .filter(|pa| pa.character.0 == character_id)
            .flat_map(|pa| pa.actions.iter())
            .collect()
    }

    /// Process a character's chosen action. Returns the result.
    pub fn process_action(
        &mut self,
        character_id: &str,
        action_id: &str,
    ) -> Option<PressureActionResult> {
        if self.outcome != PressureOutcome::InProgress {
            return None;
        }

        // Find the action
        let action = self.encounter.party_actions.iter()
            .filter(|pa| pa.character.0 == character_id)
            .flat_map(|pa| pa.actions.iter())
            .find(|a| a.id == action_id)?
            .clone();

        // Apply delta to the target bar
        let bar = self.get_bar_mut(&action.target_bar)?;
        bar.current = (bar.current + action.delta).clamp(0, bar.max);
        let bar_after = bar.current;
        let bar_id = bar.id.clone();

        info!(
            character = character_id,
            action = action_id,
            bar = %bar_id,
            delta = action.delta,
            bar_after = bar_after,
            "pressure action processed"
        );

        Some(PressureActionResult {
            actor: character_id.to_string(),
            action_label: action.label.clone(),
            bar_id,
            delta: action.delta,
            bar_after,
            description: action.description.clone(),
        })
    }

    /// Damage a cargo item (for Escort encounters). Returns true if the item was lost.
    pub fn damage_cargo(&mut self, cargo_id: &str, damage: i32) -> bool {
        if let Some(cargo) = self.cargo_state.iter_mut().find(|c| c.id == cargo_id && !c.lost) {
            cargo.integrity = (cargo.integrity - damage).max(0);
            if cargo.integrity == 0 {
                cargo.lost = true;
                self.pending_effects.extend(cargo.loss_effects.clone());
                debug!(cargo = cargo_id, "cargo lost");
                return true;
            }
        }
        false
    }

    /// Check success/failure thresholds. Call after each action or event.
    pub fn check_thresholds(&mut self) -> PressureResolution {
        if self.outcome != PressureOutcome::InProgress {
            return PressureResolution {
                outcome: self.outcome,
                effects: Vec::new(),
                trigger_bar: None,
            };
        }

        // Check failure threshold first (failure takes priority)
        let failed = self.check_condition(&self.encounter.failure_threshold.clone());
        if let Some(bar_id) = failed {
            self.outcome = PressureOutcome::Failure;
            self.pending_effects.extend(self.encounter.outcome_effects.clone());
            info!(trigger = %bar_id, "pressure encounter FAILED");
            return PressureResolution {
                outcome: PressureOutcome::Failure,
                effects: self.pending_effects.clone(),
                trigger_bar: Some(bar_id),
            };
        }

        // Check success threshold
        let succeeded = self.check_condition(&self.encounter.success_threshold.clone());
        if let Some(bar_id) = succeeded {
            self.outcome = PressureOutcome::Success;
            self.pending_effects.extend(self.encounter.outcome_effects.clone());
            info!(trigger = %bar_id, "pressure encounter SUCCEEDED");
            return PressureResolution {
                outcome: PressureOutcome::Success,
                effects: self.pending_effects.clone(),
                trigger_bar: Some(bar_id),
            };
        }

        PressureResolution {
            outcome: PressureOutcome::InProgress,
            effects: Vec::new(),
            trigger_bar: None,
        }
    }

    /// Check if a condition is met. Returns the triggering bar ID if so.
    fn check_condition(&self, condition: &PressureCondition) -> Option<String> {
        match condition {
            PressureCondition::BarReached { bar_id, threshold } => {
                self.encounter.pressure_bars.iter()
                    .find(|b| b.id == *bar_id && b.current <= *threshold)
                    .map(|b| b.id.clone())
            }
            PressureCondition::AllBarsAboveFail => {
                // Success: all bars are above their fail_at threshold
                let all_above = self.encounter.pressure_bars.iter()
                    .all(|b| b.current > b.fail_at);
                if all_above {
                    Some("all_bars".to_string())
                } else {
                    None
                }
            }
            PressureCondition::TimeExpired => {
                // Not yet implemented — needs a time/round limit system
                None
            }
            PressureCondition::FlagSet(_flag_id) => {
                // Requires game state integration — stub for now
                None
            }
        }
    }

    /// Whether the encounter is still in progress.
    pub fn is_active(&self) -> bool {
        self.outcome == PressureOutcome::InProgress
    }

    /// Get a summary of all bar states for display.
    pub fn bar_summary(&self) -> Vec<BarStatus> {
        self.encounter.pressure_bars.iter()
            .filter(|b| b.visible)
            .map(|b| BarStatus {
                id: b.id.clone(),
                label: b.label.clone(),
                current: b.current,
                max: b.max,
                fail_at: b.fail_at,
                critical: b.current <= b.fail_at + (b.max / 5), // within 20% of fail
            })
            .collect()
    }
}

/// A bar's current status for UI display.
#[derive(Debug, Clone)]
pub struct BarStatus {
    pub id: String,
    pub label: String,
    pub current: i32,
    pub max: i32,
    pub fail_at: i32,
    pub critical: bool,
}

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

    fn escort_encounter() -> PressureEncounter {
        PressureEncounter {
            id: "ch2_convoy_escort".to_string(),
            pressure_type: PressureType::Escort {
                cargo: vec![
                    CargoItem {
                        id: "medicine_crate".to_string(),
                        name: "Medicine Crate".to_string(),
                        integrity: 20,
                        max_integrity: 20,
                        loss_effect: vec![
                            StateEffect::SetFlag {
                                id: FlagId::new("medicine_lost"),
                                value: FlagValue::Bool(true),
                            },
                        ],
                    },
                    CargoItem {
                        id: "supplies".to_string(),
                        name: "Supply Wagon".to_string(),
                        integrity: 30,
                        max_integrity: 30,
                        loss_effect: vec![],
                    },
                ],
            },
            pressure_bars: vec![
                PressureBar {
                    id: "convoy_safety".to_string(),
                    label: "Convoy Safety".to_string(),
                    current: 80,
                    max: 100,
                    fail_at: 20,
                    visible: true,
                },
                PressureBar {
                    id: "bandit_pressure".to_string(),
                    label: "Bandit Threat".to_string(),
                    current: 40,
                    max: 100,
                    fail_at: 0,
                    visible: true,
                },
            ],
            party_actions: vec![
                PressurePartyAction {
                    character: CharacterId::new("galen"),
                    actions: vec![
                        PressureAction {
                            id: "scout_ahead".to_string(),
                            label: "Scout Ahead".to_string(),
                            description: "Ride ahead to check the trail.".to_string(),
                            target_bar: "convoy_safety".to_string(),
                            delta: 10,
                            conditions: vec![],
                        },
                        PressureAction {
                            id: "suppressive_fire".to_string(),
                            label: "Suppressive Fire".to_string(),
                            description: "Lay down covering fire on the ridge.".to_string(),
                            target_bar: "bandit_pressure".to_string(),
                            delta: -15,
                            conditions: vec![],
                        },
                    ],
                },
                PressurePartyAction {
                    character: CharacterId::new("eli"),
                    actions: vec![
                        PressureAction {
                            id: "talk_down".to_string(),
                            label: "Talk Them Down".to_string(),
                            description: "Try to convince the bandits to back off.".to_string(),
                            target_bar: "bandit_pressure".to_string(),
                            delta: -10,
                            conditions: vec![],
                        },
                        PressureAction {
                            id: "check_cargo".to_string(),
                            label: "Check Cargo".to_string(),
                            description: "Secure the wagon and check for damage.".to_string(),
                            target_bar: "convoy_safety".to_string(),
                            delta: 5,
                            conditions: vec![],
                        },
                    ],
                },
            ],
            success_threshold: PressureCondition::AllBarsAboveFail,
            failure_threshold: PressureCondition::BarReached {
                bar_id: "convoy_safety".to_string(),
                threshold: 20,
            },
            outcome_effects: vec![
                StateEffect::SetFlag {
                    id: FlagId::new("convoy_complete"),
                    value: FlagValue::Bool(true),
                },
            ],
        }
    }

    #[test]
    fn pressure_engine_creates_from_encounter() {
        let enc = escort_encounter();
        let engine = PressureEngine::new(enc);

        assert_eq!(engine.round, 0);
        assert_eq!(engine.outcome, PressureOutcome::InProgress);
        assert_eq!(engine.cargo_state.len(), 2);
        assert!(engine.is_active());
    }

    #[test]
    fn pressure_action_modifies_bar() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);
        engine.begin_round();

        let result = engine.process_action("galen", "scout_ahead");
        assert!(result.is_some());
        let result = result.unwrap();
        assert_eq!(result.bar_id, "convoy_safety");
        assert_eq!(result.delta, 10);
        assert_eq!(result.bar_after, 90); // 80 + 10

        let bar = engine.get_bar("convoy_safety").unwrap();
        assert_eq!(bar.current, 90);
    }

    #[test]
    fn pressure_bar_clamps_to_max() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);
        engine.begin_round();

        // Scout ahead 3 times to try to exceed max
        engine.process_action("galen", "scout_ahead");
        engine.process_action("galen", "scout_ahead");
        engine.process_action("galen", "scout_ahead");

        let bar = engine.get_bar("convoy_safety").unwrap();
        assert_eq!(bar.current, 100); // clamped to max
    }

    #[test]
    fn pressure_failure_triggers_on_bar_threshold() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);
        engine.begin_round();

        // Directly reduce convoy_safety below fail_at (20)
        // Start at 80, need to reduce by 65 to reach 15
        {
            let bar = engine.get_bar_mut("convoy_safety").unwrap();
            bar.current = 15; // below fail_at of 20
        }

        let resolution = engine.check_thresholds();
        assert_eq!(resolution.outcome, PressureOutcome::Failure);
        assert!(resolution.trigger_bar.is_some());
        assert_eq!(resolution.trigger_bar.unwrap(), "convoy_safety");
        assert!(!engine.is_active());
    }

    #[test]
    fn pressure_success_when_all_bars_above_fail() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);
        engine.begin_round();

        // Both bars start above fail_at: convoy_safety=80 (fail=20), bandit_pressure=40 (fail=0)
        let resolution = engine.check_thresholds();
        assert_eq!(resolution.outcome, PressureOutcome::Success);
        assert!(!engine.is_active());
    }

    #[test]
    fn cargo_damage_and_loss() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);

        // Damage medicine crate
        let lost = engine.damage_cargo("medicine_crate", 15);
        assert!(!lost); // 20 - 15 = 5, still alive
        assert_eq!(engine.cargo_state[0].integrity, 5);

        // Finish it off
        let lost = engine.damage_cargo("medicine_crate", 10);
        assert!(lost);
        assert_eq!(engine.cargo_state[0].integrity, 0);
        assert!(engine.cargo_state[0].lost);
        assert!(!engine.pending_effects.is_empty()); // loss effects applied
    }

    #[test]
    fn available_actions_per_character() {
        let enc = escort_encounter();
        let engine = PressureEngine::new(enc);

        let galen_actions = engine.available_actions("galen");
        assert_eq!(galen_actions.len(), 2);

        let eli_actions = engine.available_actions("eli");
        assert_eq!(eli_actions.len(), 2);

        let nobody_actions = engine.available_actions("nobody");
        assert_eq!(nobody_actions.len(), 0);
    }

    #[test]
    fn bar_summary_only_shows_visible() {
        let enc = escort_encounter();
        let engine = PressureEngine::new(enc);

        let summary = engine.bar_summary();
        assert_eq!(summary.len(), 2); // both visible

        // Check critical state — 80 is not critical for fail_at=20 (20% threshold = 20+20=40)
        assert!(!summary[0].critical); // convoy_safety at 80, critical threshold ~40
    }

    #[test]
    fn actions_blocked_after_resolution() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);

        // Force resolution
        engine.outcome = PressureOutcome::Success;

        let result = engine.process_action("galen", "scout_ahead");
        assert!(result.is_none(), "actions should be blocked after resolution");
    }

    #[test]
    fn negative_delta_reduces_bar() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);
        engine.begin_round();

        let result = engine.process_action("galen", "suppressive_fire");
        assert!(result.is_some());
        let result = result.unwrap();
        assert_eq!(result.bar_id, "bandit_pressure");
        assert_eq!(result.delta, -15);
        assert_eq!(result.bar_after, 25); // 40 - 15
    }

    #[test]
    fn full_pressure_round_flow() {
        let enc = escort_encounter();
        let mut engine = PressureEngine::new(enc);

        // Round 1
        let round = engine.begin_round();
        assert_eq!(round, 1);

        // Galen scouts, Eli checks cargo
        engine.process_action("galen", "scout_ahead");
        engine.process_action("eli", "check_cargo");

        // Check — should succeed (all bars above fail)
        let res = engine.check_thresholds();
        assert_eq!(res.outcome, PressureOutcome::Success);
    }
}