underworld_core/handlers/
inspect_npc.rs

1use crate::{
2    actions::InspectNpc,
3    components::games::GameState,
4    errors::Error,
5    events::{Event, NpcHealthDiscovered, NpcPackedDiscovered},
6    utils::{ids::parse_id, rolls::roll_d6},
7};
8
9const DISCOVER_HEALTH_CHANCE: i32 = 5;
10const DISCOVER_PACKED_CHANCE: i32 = 4;
11
12pub fn handle(inspect_npc: &InspectNpc, state: &GameState) -> Result<Vec<Event>, Error> {
13    let mut events: Vec<Event> = Vec::new();
14    let npc_id = parse_id(&inspect_npc.npc_id)?;
15
16    let npc = match state.current_room().find_npc(&npc_id) {
17        Some(it) => it,
18        None => return Err(Error::NpcNotFoundError(npc_id.to_string())),
19    };
20
21    if npc.character.is_dead() {
22        events.push(Event::NpcHealthDiscovered(NpcHealthDiscovered { npc_id }));
23        events.push(Event::NpcPackedDiscovered(NpcPackedDiscovered { npc_id }));
24    } else {
25        let mut rng = rand::thread_rng();
26
27        if inspect_npc.discover_health && roll_d6(&mut rng, 1, 0) >= DISCOVER_HEALTH_CHANCE {
28            events.push(Event::NpcHealthDiscovered(NpcHealthDiscovered { npc_id }));
29        }
30
31        if inspect_npc.discover_packed_items && roll_d6(&mut rng, 1, 0) >= DISCOVER_PACKED_CHANCE {
32            events.push(Event::NpcPackedDiscovered(NpcPackedDiscovered { npc_id }));
33        }
34    }
35
36    Ok(events)
37}