Skip to main content

cardinal_kernel/engine/
triggers.rs

1use crate::{
2    model::event::Event,
3    model::command::Command,
4    engine::core::GameEngine,
5};
6
7/// Evaluate which triggers should fire in response to an event.
8/// Returns commands to execute (typically PushStack for triggered effects).
9/// This uses card definitions to determine which abilities should fire.
10pub fn evaluate_triggers(
11    engine: &mut GameEngine,
12    event: &Event,
13) -> Vec<Command> {
14    let mut commands = Vec::new();
15    let mut next_stack_id = engine.next_stack_id();
16
17    match event {
18        // CardMoved events can trigger "enters the battlefield" effects (ETB triggers)
19        Event::CardMoved { card, to, .. } => {
20            // Check if moving TO the field zone indicates "enters"
21            if to.0.starts_with("field") {
22                // Find the controller (zone owner)
23                if let Some(zone) = engine.state.zones.iter().find(|z| z.id == *to) {
24                    if let Some(controller) = zone.owner {
25                        // Look up card's abilities and fire matching triggers
26                        let ability_commands = crate::engine::cards::generate_ability_commands(
27                            *card,
28                            "etb",
29                            controller,
30                            &engine.cards,
31                            &mut next_stack_id,
32                        );
33                        commands.extend(ability_commands);
34                    }
35                }
36            }
37        }
38        // CardPlayed events can trigger on_play card abilities
39        Event::CardPlayed { player, card } => {
40            // Look up card's abilities and fire matching triggers
41            let ability_commands = crate::engine::cards::generate_ability_commands(
42                *card,
43                "on_play",
44                *player,
45                &engine.cards,
46                &mut next_stack_id,
47            );
48            commands.extend(ability_commands);
49        }
50        _ => {
51            // Other events don't trigger anything yet
52        }
53    }
54
55    commands
56}