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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Entity Search Module
//!
//! Purpose: In many handlers across the codebase, there is a need to take a string
//! from user input and match it to a nearby item or NPC -- making a natural choice
//! for a unified handler for that job.
//!
//! While this is straightforward and can be accomplished in a few handfuls of lines
//! on the surface, it is complicated by a variety of needs in terms of search scope:
//!
//! - some searches will only want items, some will want NPCS, some will want either
//! - visible items and reachable items are two different things (consider an item in a locked transparent container)
//!
//! Callers send:
//! - an immutable `AmbleWorld` reference
//! - the search string
//! - the [`SearchScope`] of interest
//!
//! Also, callers get in return:
//! - the Id of the found entity, OR
//! - the reason that an entity's ID is not being returned (`SearchError`)
//!
//! General usage:
//! `find_item_match(...)` when only interested in items for a given command
//! `find_npc_match(...)` when only interested in npcs
//! `find_entity_match(...)` when the input could refer to either an item or an npc

use std::collections::{HashMap, HashSet};
use std::hash::BuildHasher;

pub use crate::EntityId;
use crate::world::item_is_visible_item;
use crate::{Item, ItemId, Npc, NpcId, RoomId, View, ViewItem, WorldObject};
use colored::Colorize;
use thiserror::Error;
use variantly::Variantly;

use crate::{
    AmbleWorld,
    spinners::CoreSpinnerType,
    style::GameStyle,
    world::{nearby_reachable_items, nearby_vessel_items, nearby_visible_items},
};

/// Represents the scope of a requested search by the caller and includes the location to search.
#[derive(Debug, Clone, PartialEq)]
pub enum SearchScope {
    /// All items and NPCs within the player's sight
    AllVisible(RoomId),
    /// All items and NPCs that the player can touch
    AllTouchable(RoomId),
    /// Items the player can look at.
    VisibleItems(RoomId),
    /// NPCs the player can see.
    VisibleNpcs(RoomId),
    /// Items the player can touch (but not necessarily move) in room or inventory
    TouchableItems(RoomId),
    /// NPCs the player can touch.
    TouchableNpcs(RoomId),
    /// Nearby container items or NPCs which can potentially offer or accept an item.
    NearbyVessels(RoomId),
    /// Only items in player's inventory.
    Inventory,
    /// Only items in an NPC's inventory.
    NpcInventory(NpcId),
    /// Only items contained within a specific item container.
    ItemContents(ItemId),
}

/// Possible errors / situations causing a failed entity search.
#[derive(Debug, Error)]
pub enum SearchError {
    #[error("no entity in scope name matching user input '{0}'")]
    NoMatchingName(String),
    #[error("no item found with the supplied id {0}")]
    InvalidItemId(ItemId),
    #[error("no npc found with the supplied id {0}")]
    InvalidNpcId(NpcId),
    #[error("found no room with the supplied id ({0})")]
    InvalidRoomId(RoomId),
    #[error("invalid {0} search scope: includes only {1}s")]
    InvalidScope(String, String),
    #[error("unknown error: {0}")]
    Unknown(#[from] anyhow::Error),
}

/// Encapsulates references to different types of `WorldObjects` to allow search across different types.
#[derive(Debug, Variantly, Clone, Copy)]
pub enum WorldEntity<'a> {
    Item(&'a Item),
    Npc(&'a Npc),
}
impl WorldEntity<'_> {
    /// Get the name of the entity.
    pub fn name(&self) -> &str {
        match self {
            WorldEntity::Item(item) => item.name(),
            WorldEntity::Npc(npc) => npc.name(),
        }
    }

    /// Get the typed id of the entity.
    pub fn entity_id(&self) -> EntityId {
        match self {
            WorldEntity::Item(item) => EntityId::Item(item.id.clone()),
            WorldEntity::Npc(npc) => EntityId::Npc(npc.id.clone()),
        }
    }

    /// Get the symbol for the entity.
    pub fn symbol(&self) -> &str {
        match self {
            WorldEntity::Item(item) => item.symbol(),
            WorldEntity::Npc(npc) => npc.symbol(),
        }
    }
}

/// Search item and NPC ids for a name match and return the resolved world entity.
pub fn find_world_entity<'a, S: BuildHasher>(
    nearby_item_ids: impl IntoIterator<Item = &'a ItemId>,
    nearby_npc_ids: impl IntoIterator<Item = &'a NpcId>,
    world_items: &'a HashMap<ItemId, Item, S>,
    world_npcs: &'a HashMap<NpcId, Npc, S>,
    search_term: &str,
) -> Option<WorldEntity<'a>> {
    let lc_term = search_term.to_lowercase();
    for item_id in nearby_item_ids {
        if let Some(found_item) = world_items.get(item_id)
            && (found_item.name().to_lowercase().contains(&lc_term)
                || found_item
                    .aliases
                    .iter()
                    .any(|alias| alias.to_lowercase().contains(&lc_term)))
        {
            return Some(WorldEntity::Item(found_item));
        }
    }
    for npc_id in nearby_npc_ids {
        if let Some(found_npc) = world_npcs.get(npc_id)
            && found_npc.name().to_lowercase().contains(&lc_term)
        {
            return Some(WorldEntity::Npc(found_npc));
        }
    }
    None
}

/// Feedback to player if an entity search comes up empty.
pub fn entity_not_found(world: &AmbleWorld, view: &mut View, search_text: &str) {
    view.push(ViewItem::Error(format!(
        "\"{}\"? {}\n{}",
        search_text.error_style(),
        world.spin_core(CoreSpinnerType::EntityNotFound, "What's that?"),
        "(word not understood in context)".to_string().italic().dimmed()
    )));
}

/// Find an `Item` with name matching `pattern` in the given `SearchScope` and return its id.
///
/// # Errors
/// - if no match found in the specified scope
/// - if an invalid scope for an item search is specified
/// - if the supplied room or NPC ids are invalid
pub fn find_item_match(world: &AmbleWorld, pattern: &str, scope: SearchScope) -> Result<ItemId, SearchError> {
    // construct a HashSet of item ids in scope for this search
    let haystack: HashSet<_> = match scope {
        SearchScope::VisibleItems(room_id) | SearchScope::AllVisible(room_id) => {
            let room_items = nearby_visible_items(world, &room_id).map_err(|_| SearchError::InvalidRoomId(room_id))?;
            room_items.union(&world.player.inventory).cloned().collect()
        },
        SearchScope::TouchableItems(room_id) | SearchScope::AllTouchable(room_id) => {
            let room_items =
                nearby_reachable_items(world, &room_id).map_err(|_| SearchError::InvalidRoomId(room_id))?;
            room_items.union(&world.player.inventory).cloned().collect()
        },
        SearchScope::NearbyVessels(room_id) => {
            nearby_vessel_items(world, &room_id).map_err(|_| SearchError::InvalidRoomId(room_id.clone()))?
        },
        SearchScope::Inventory => world.player.inventory.clone(),
        SearchScope::NpcInventory(npc_id) => {
            let npc = world.npcs.get(&npc_id).ok_or(SearchError::InvalidNpcId(npc_id))?;
            filter_visible_items(&npc.inventory, world)?
        },
        SearchScope::ItemContents(item_id) => {
            let item = world.items.get(&item_id).ok_or(SearchError::InvalidItemId(item_id))?;
            filter_visible_items(&item.contents, world)?
        },
        SearchScope::VisibleNpcs(_) | SearchScope::TouchableNpcs(_) => {
            return Err(SearchError::InvalidScope("item".to_string(), "NPC".to_string()));
        },
    };

    let Some(entity) = find_world_entity(
        haystack.iter(),
        std::iter::empty::<&NpcId>(),
        &world.items,
        &world.npcs,
        pattern,
    ) else {
        return Err(SearchError::NoMatchingName(pattern.to_string()));
    };
    match entity.entity_id() {
        EntityId::Item(item_id) => Ok(item_id),
        EntityId::Npc(_) => Err(SearchError::InvalidScope("item".into(), "NPC".into())),
    }
}

/// Takes a set of `ItemId`s and returns a new set, filtering out any items that
/// shouldn't be visible.
/// # Errors
/// - if an `ItemId` is invalid
pub(crate) fn filter_visible_items(
    item_set: &HashSet<ItemId>,
    world: &AmbleWorld,
) -> Result<HashSet<ItemId>, SearchError> {
    let mut visible_items = HashSet::new();
    for item_id in item_set {
        let item = world
            .items
            .get(item_id)
            .ok_or(SearchError::InvalidItemId(item_id.clone()))?;
        if item_is_visible_item(item, world) {
            visible_items.insert(item_id.clone());
        }
    }
    Ok(visible_items)
}

/// Find either an `NPC` or an `Item` with name matching `pattern` within the `SearchScope`.
///
/// # Panics
/// - never: the call to `expect` is guarded by an `is_ok()`
///
/// # Errors
/// - `SearchError` variants of any type, if no match is found or if the scope type or supplied ids are invalid
pub fn find_entity_match(world: &AmbleWorld, pattern: &str, scope: SearchScope) -> Result<EntityId, SearchError> {
    // return any item matched first -- these will account for most searches
    // no_match and scope errors are ignored on this pass because they may
    // not be errors when run against the NPCs
    match find_item_match(world, pattern, scope.clone()) {
        Ok(item_id) => return Ok(EntityId::Item(item_id)),
        Err(SearchError::NoMatchingName(_) | SearchError::InvalidScope(_, _)) => (),
        Err(e) => return Err(e),
    }

    // less often looking for an NPC match -- return that now if found
    match find_npc_match(world, pattern, scope) {
        Ok(npc_id) => Ok(EntityId::Npc(npc_id)),
        Err(e) => Err(e),
    }
}

/// Find an `Npc` with name matching `pattern` in the specified `SearchScope`.
///
/// # Errors
/// - if no match found in the specified scope
/// - if an invalid scope for an npc search is specified
/// - if the supplied room id is invalid
pub fn find_npc_match(world: &AmbleWorld, pattern: &str, scope: SearchScope) -> Result<NpcId, SearchError> {
    let haystack = match scope {
        // currently there is no distinction between NPCs you can see and those you could touch
        // both scopes kept for now as this may change in the future
        SearchScope::VisibleNpcs(room_id)
        | SearchScope::TouchableNpcs(room_id)
        | SearchScope::NearbyVessels(room_id)
        | SearchScope::AllVisible(room_id)
        | SearchScope::AllTouchable(room_id) => {
            let room = world.rooms.get(&room_id).ok_or(SearchError::InvalidRoomId(room_id))?;
            room.npcs.clone()
        },
        SearchScope::VisibleItems(_)
        | SearchScope::TouchableItems(_)
        | SearchScope::Inventory
        | SearchScope::NpcInventory(_)
        | SearchScope::ItemContents(_) => {
            return Err(SearchError::InvalidScope("npc".into(), "item".into()));
        },
    };

    let Some(entity) = find_world_entity(
        std::iter::empty::<&ItemId>(),
        haystack.iter(),
        &world.items,
        &world.npcs,
        pattern,
    ) else {
        return Err(SearchError::NoMatchingName(pattern.to_string()));
    };

    match entity.entity_id() {
        EntityId::Npc(npc_id) => Ok(npc_id),
        EntityId::Item(_) => Err(SearchError::InvalidScope("npc".into(), "item".into())),
    }
}

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

    use crate::{
        AmbleWorld, Item, Npc, Room,
        health::HealthState,
        item::{ContainerState, Movability},
        npc::NpcState,
        scheduler::EventCondition,
        trigger::TriggerCondition,
        world::Location,
    };
    use std::collections::{HashMap, HashSet};

    fn insert_room(world: &mut AmbleWorld, name: &str) -> RoomId {
        let room_id = crate::idgen::new_room_id();
        let room = Room {
            id: room_id.clone(),
            symbol: format!("room_{room_id}"),
            name: name.to_string(),
            base_description: format!("{name} description"),
            overlays: Vec::new(),
            scenery: Vec::new(),
            scenery_default: None,
            location: Location::Nowhere,
            visited: false,
            exits: HashMap::new(),
            contents: HashSet::new(),
            npcs: HashSet::new(),
        };
        world.rooms.insert(room_id.clone(), room);
        room_id
    }

    fn insert_item(
        world: &mut AmbleWorld,
        name: &str,
        location: Location,
        container_state: Option<ContainerState>,
    ) -> ItemId {
        let item_id: ItemId = crate::idgen::new_id().into();
        let item = Item {
            id: item_id.clone(),
            symbol: format!("item_{item_id}"),
            name: name.to_string(),
            description: format!("{name} item"),
            location,
            visibility: crate::item::ItemVisibility::Listed,
            visible_when: None,
            aliases: Vec::new(),
            movability: Movability::Free,
            container_state,
            contents: HashSet::new(),
            abilities: HashSet::new(),
            interaction_requires: HashMap::new(),
            text: None,
            consumable: None,
        };
        world.items.insert(item_id.clone(), item);
        item_id
    }

    fn insert_npc(world: &mut AmbleWorld, name: &str, location: Location) -> NpcId {
        let npc_id: NpcId = crate::idgen::new_id().into();
        let npc = Npc {
            id: npc_id.clone(),
            symbol: format!("npc_{npc_id}"),
            name: name.to_string(),
            description: format!("{name} npc"),
            location,
            inventory: HashSet::new(),
            dialogue: HashMap::new(),
            state: NpcState::Normal,
            movement: None,
            health: HealthState::new(),
        };
        world.npcs.insert(npc_id.clone(), npc);
        npc_id
    }

    #[test]
    fn find_item_match_includes_inventory_items_in_visible_scope() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Atrium");
        let coin_id = insert_item(&mut world, "Lucky Coin", Location::Inventory, None);
        world.player.inventory.insert(coin_id.clone());

        let result = find_item_match(&world, "coin", SearchScope::VisibleItems(room_id)).unwrap();
        assert_eq!(result, coin_id);
    }

    #[test]
    fn find_item_match_returns_nearby_vessels() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Vault");
        let chest_id = insert_item(
            &mut world,
            "Ancient Chest",
            Location::Room(room_id.clone()),
            Some(ContainerState::Open),
        );
        world.rooms.get_mut(&room_id).unwrap().contents.insert(chest_id.clone());

        let result = find_item_match(&world, "chest", SearchScope::NearbyVessels(room_id)).unwrap();
        assert_eq!(result, chest_id);
    }

    #[test]
    fn find_item_match_returns_items_from_selected_container_only() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Vault");
        let chest_id = insert_item(
            &mut world,
            "Ancient Chest",
            Location::Room(room_id.clone()),
            Some(ContainerState::Open),
        );
        let chest_gem_id = insert_item(&mut world, "Gem", Location::Item(chest_id.clone()), None);
        let floor_gem_id = insert_item(&mut world, "Gem", Location::Room(room_id.clone()), None);

        world.rooms.get_mut(&room_id).unwrap().contents.insert(chest_id.clone());
        world.rooms.get_mut(&room_id).unwrap().contents.insert(floor_gem_id);
        world
            .items
            .get_mut(&chest_id)
            .unwrap()
            .contents
            .insert(chest_gem_id.clone());

        let result = find_item_match(&world, "gem", SearchScope::ItemContents(chest_id)).unwrap();
        assert_eq!(result, chest_gem_id);
    }

    #[test]
    fn find_item_match_filters_hidden_items_from_npc_inventory() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Garden");
        let npc_id = insert_npc(&mut world, "Caretaker", Location::Room(room_id.clone()));
        world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());

        let hidden_gem_id = insert_item(&mut world, "Gem", Location::Npc(npc_id.clone()), None);
        world.items.get_mut(&hidden_gem_id).unwrap().visible_when = Some(EventCondition::Trigger(
            TriggerCondition::HasFlag("npc_inventory_visible".to_string()),
        ));
        let visible_coin_id = insert_item(&mut world, "Coin", Location::Npc(npc_id.clone()), None);

        world.npcs.get_mut(&npc_id).unwrap().inventory.insert(hidden_gem_id);
        world
            .npcs
            .get_mut(&npc_id)
            .unwrap()
            .inventory
            .insert(visible_coin_id.clone());

        let result = find_item_match(&world, "coin", SearchScope::NpcInventory(npc_id.clone())).unwrap();
        assert_eq!(result, visible_coin_id);

        let err = find_item_match(&world, "gem", SearchScope::NpcInventory(npc_id)).unwrap_err();
        assert!(matches!(err, SearchError::NoMatchingName(name) if name == "gem"));
    }

    #[test]
    fn find_item_match_filters_hidden_items_from_container_contents() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Vault");
        let chest_id = insert_item(
            &mut world,
            "Ancient Chest",
            Location::Room(room_id.clone()),
            Some(ContainerState::Open),
        );
        let hidden_gem_id = insert_item(&mut world, "Gem", Location::Item(chest_id.clone()), None);
        world.items.get_mut(&hidden_gem_id).unwrap().visible_when = Some(EventCondition::Trigger(
            TriggerCondition::HasFlag("container_contents_visible".to_string()),
        ));
        let visible_coin_id = insert_item(&mut world, "Coin", Location::Item(chest_id.clone()), None);

        world.rooms.get_mut(&room_id).unwrap().contents.insert(chest_id.clone());
        world.items.get_mut(&chest_id).unwrap().contents.insert(hidden_gem_id);
        world
            .items
            .get_mut(&chest_id)
            .unwrap()
            .contents
            .insert(visible_coin_id.clone());

        let result = find_item_match(&world, "coin", SearchScope::ItemContents(chest_id.clone())).unwrap();
        assert_eq!(result, visible_coin_id);

        let err = find_item_match(&world, "gem", SearchScope::ItemContents(chest_id)).unwrap_err();
        assert!(matches!(err, SearchError::NoMatchingName(name) if name == "gem"));
    }

    #[test]
    fn find_item_match_errors_when_container_missing() {
        let world = AmbleWorld::new_empty();
        let item_id: ItemId = crate::idgen::new_id().into();

        let err = find_item_match(&world, "gem", SearchScope::ItemContents(item_id.clone())).unwrap_err();
        match err {
            SearchError::InvalidItemId(id) => assert_eq!(id, item_id),
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn find_item_match_errors_when_room_missing() {
        let world = AmbleWorld::new_empty();
        let room_id = crate::idgen::new_room_id();

        let err = find_item_match(&world, "coin", SearchScope::VisibleItems(room_id.clone())).unwrap_err();
        match err {
            SearchError::InvalidRoomId(id) => assert_eq!(id, room_id),
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn find_item_match_rejects_npc_scopes() {
        let world = AmbleWorld::new_empty();
        let scope = SearchScope::VisibleNpcs(crate::idgen::new_room_id());

        let err = find_item_match(&world, "coin", scope).unwrap_err();
        match err {
            SearchError::InvalidScope(kind, only) => {
                assert_eq!(kind, "item");
                assert_eq!(only, "NPC");
            },
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn find_npc_match_returns_visible_npc() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Garden");
        let npc_id = insert_npc(&mut world, "Caretaker", Location::Room(room_id.clone()));
        world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());

        let result = find_npc_match(&world, "take", SearchScope::VisibleNpcs(room_id)).unwrap();
        assert_eq!(result, npc_id);
    }

    #[test]
    fn find_npc_match_errors_when_room_missing() {
        let world = AmbleWorld::new_empty();
        let room_id = crate::idgen::new_room_id();

        let err = find_npc_match(&world, "caretaker", SearchScope::VisibleNpcs(room_id.clone())).unwrap_err();
        match err {
            SearchError::InvalidRoomId(id) => assert_eq!(id, room_id),
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn find_npc_match_rejects_item_scopes() {
        let world = AmbleWorld::new_empty();

        let err = find_npc_match(&world, "caretaker", SearchScope::Inventory).unwrap_err();
        match err {
            SearchError::InvalidScope(kind, only) => {
                assert_eq!(kind, "npc");
                assert_eq!(only, "item");
            },
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn find_entity_match_prefers_items() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Vault");
        let vessel_id = insert_item(
            &mut world,
            "Guardian Chest",
            Location::Room(room_id.clone()),
            Some(ContainerState::Open),
        );
        world
            .rooms
            .get_mut(&room_id)
            .unwrap()
            .contents
            .insert(vessel_id.clone());
        let npc_id = insert_npc(&mut world, "Guardian", Location::Room(room_id.clone()));
        world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());

        let result = find_entity_match(&world, "guardian", SearchScope::NearbyVessels(room_id)).unwrap();
        assert_eq!(result, EntityId::Item(vessel_id));
    }

    #[test]
    fn find_entity_match_returns_npc_when_no_item_found() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Library");
        let npc_id = insert_npc(&mut world, "Archivist", Location::Room(room_id.clone()));
        world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());

        let result = find_entity_match(&world, "archivist", SearchScope::VisibleNpcs(room_id)).unwrap();
        assert_eq!(result, EntityId::Npc(npc_id));
    }

    #[test]
    fn find_entity_match_returns_no_matching_name_when_none_found() {
        let mut world = AmbleWorld::new_empty();
        let room_id = insert_room(&mut world, "Workshop");
        let vessel_id = insert_item(
            &mut world,
            "Toolbox",
            Location::Room(room_id.clone()),
            Some(ContainerState::Open),
        );
        world
            .rooms
            .get_mut(&room_id)
            .unwrap()
            .contents
            .insert(vessel_id.clone());
        let npc_id = insert_npc(&mut world, "Mechanic", Location::Room(room_id.clone()));
        world.rooms.get_mut(&room_id).unwrap().npcs.insert(npc_id.clone());

        let err = find_entity_match(&world, "lantern", SearchScope::NearbyVessels(room_id)).unwrap_err();
        match err {
            SearchError::NoMatchingName(term) => assert_eq!(term, "lantern"),
            other => panic!("unexpected error: {other:?}"),
        }
    }
}